Esempio n. 1
0
        private static byte[] SignData(FiscoTransModel trans, ECKeyPair key)
        {
            byte[][] b = new byte[10][];
            b[0] = trans.data.AccountNonce;
            b[1] = LibraryHelper.FromBigInteger(trans.data.Price);
            b[2] = LibraryHelper.FromBigInteger(trans.data.GasLimit);
            b[3] = LibraryHelper.FromBigInteger(trans.data.BlockLimit);
            b[4] = trans.data.Recipient;
            b[5] = trans.data.Amount == null ? null : LibraryHelper.FromBigInteger(trans.data.Amount);
            b[6] = trans.data.Payload;
            b[7] = LibraryHelper.FromBigInteger(trans.data.ChainID);
            b[8] = LibraryHelper.FromBigInteger(trans.data.GroupID);
            b[9] = trans.data.ExtraData;

            var txb = Nethereum.RLP.RLP.EncodeElementsAndList(b);

            if (trans.smcrypto)
            {
                BigInteger r, s;
                SM2Utils.Sign(SM2Utils.SM3Hash(txb), key.prik, out r, out s);
                trans.data.R = LibraryHelper.FromBigInteger(r);
                trans.data.S = LibraryHelper.FromBigInteger(s);

                var    pub1 = key.pubk.Q.XCoord.GetEncoded();
                var    pub2 = key.pubk.Q.YCoord.GetEncoded();
                byte[] v    = new byte[pub1.Length + pub2.Length];
                Array.Copy(pub1, 0, v, 0, pub1.Length);
                Array.Copy(pub2, 0, v, pub1.Length, pub2.Length);
                Console.WriteLine(string.Format("sign.V:{0}", Hex.ToHexString(v)));
                trans.data.V = v;
            }
            else
            {
                var bytes = EthUtils.ConvertSHA256byte(txb);
                var sign  = EthUtils.Sign(bytes, key.prik);
                trans.data.R = sign.Item1;
                trans.data.S = sign.Item2;
                trans.data.V = sign.Item3;
            }
            byte[][] si = new byte[b.Length + 3][];
            Array.Copy(b, 0, si, 0, b.Length);
            si[b.Length]     = trans.data.V;
            si[b.Length + 1] = trans.data.R;
            si[b.Length + 2] = trans.data.S;

            var txs = Nethereum.RLP.RLP.EncodeElementsAndList(si);

            return(txs);
        }