Пример #1
0
        public EthECDSASignature SignAndCalculateV(byte[] hash, BigInteger chainId)
        {
#if NETCOREAPP3_1 || NET5_0_OR_GREATER
            if (SignRecoverable)
            {
                var privKey = Context.Instance.CreateECPrivKey(GetPrivateKeyAsBytes());
                privKey.TrySignRecoverable(hash, out var recSignature);
                recSignature.Deconstruct(out Scalar r, out var s, out var recId);
                var vChain = CalculateV(chainId, recId);
                return(EthECDSASignatureFactory.FromComponents(r.ToBytes(), s.ToBytes(),
                                                               vChain.ToBytesForRLPEncoding()));
            }
            else
            {
#endif
            var signature = _ecKey.Sign(hash);
            var recId     = CalculateRecId(signature, hash);
            var vChain    = CalculateV(chainId, recId);
            signature.V = vChain.ToBytesForRLPEncoding();
            return(new EthECDSASignature(signature));

#if NETCOREAPP3_1 || NET5_0_OR_GREATER
        }
#endif
        }
Пример #2
0
 public RLPSigner(byte[][] data, byte[] r, byte[] s, byte[] v, int numberOfEncodingElements)
 {
     this.numberOfEncodingElements = numberOfEncodingElements;
     this.data = data;
     signature = EthECDSASignatureFactory.FromComponents(r, s, v);
     decoded   = true;
 }
Пример #3
0
 public RLPSigner(byte[][] data, byte[] r, byte[] s, byte v)
 {
     numberOfElements = data.Length;
     this.data        = data;
     signature        = EthECDSASignatureFactory.FromComponents(r, s, v);
     decoded          = true;
 }
Пример #4
0
        public static ISignedTransaction Create1559Transaction(BigInteger?chainId, BigInteger?nonce,
                                                               BigInteger?maxPriorityFeePerGas, BigInteger?maxFeePerGas,
                                                               BigInteger?gasLimit, string to, BigInteger?amount, string data,
                                                               List <AccessListItem> accessList, string r, string s, string v)
        {
            var rBytes = r.HexToByteArray();
            var sBytes = s.HexToByteArray();
            var vBytes = v.HexToByteArray();

            var signature = EthECDSASignatureFactory.FromComponents(rBytes, sBytes, vBytes);

            return(new Transaction1559(chainId ?? 0, nonce ?? 0, maxPriorityFeePerGas ?? 0, maxFeePerGas ?? 0,
                                       gasLimit ?? 0, to, amount ?? 0, data, accessList,
                                       signature));
        }
Пример #5
0
        public EthECDSASignature SignAndCalculateV(byte[] hash)
        {
#if NETCOREAPP3_1
            var privKey = NBitcoin.Secp256k1.Context.Instance.CreateECPrivKey(GetPrivateKeyAsBytes());
            NBitcoin.Secp256k1.SecpRecoverableECDSASignature recSignature;
            privKey.TrySignRecoverable(hash, out recSignature);
            NBitcoin.Secp256k1.Scalar r;
            NBitcoin.Secp256k1.Scalar s;
            int recId;
            recSignature.Deconstruct(out r, out s, out recId);
            return(EthECDSASignatureFactory.FromComponents(r.ToBytes(), s.ToBytes(), new[] { (byte)(recId + 27) }));
#else
            var signature = _ecKey.Sign(hash);
            var recId     = CalculateRecId(signature, hash);
            signature.V = new[] { (byte)(recId + 27) };
            return(new EthECDSASignature(signature));
#endif
        }
Пример #6
0
        public static EthECDSASignature DecodeSignature(RLPCollection decodedElements, int numberOfEncodingElements)
        {
            EthECDSASignature signature = null;

            if (decodedElements.Count > numberOfEncodingElements && decodedElements[numberOfEncodingElements + 1].RLPData != null)
            {
                var v = new byte[] { 0 };
                //Decode Signature
                if (decodedElements[numberOfEncodingElements].RLPData != null)
                {
                    v = decodedElements[numberOfEncodingElements].RLPData;
                }

                var r = decodedElements[numberOfEncodingElements + 1].RLPData;
                var s = decodedElements[numberOfEncodingElements + 2].RLPData;
                signature = EthECDSASignatureFactory.FromComponents(r, s, v);
            }

            return(signature);
        }
Пример #7
0
        public static ISignedTransaction CreateLegacyTransaction(string to, BigInteger gas, BigInteger gasPrice, BigInteger amount, string data, BigInteger nonce, string r, string s, string v)
        {
            var rBytes = r.HexToByteArray();
            var sBytes = s.HexToByteArray();
            var vBytes = v.HexToByteArray();

            var signature = EthECDSASignatureFactory.FromComponents(rBytes, sBytes, vBytes);

            if (signature.IsVSignedForChain())
            {
                var vBigInteger = vBytes.ToBigIntegerFromRLPDecoded();
                var chainId     = EthECKey.GetChainFromVChain(vBigInteger);
                return(new LegacyTransactionChainId(nonce.ToBytesForRLPEncoding(), gasPrice.ToBytesForRLPEncoding(), gas.ToBytesForRLPEncoding(),
                                                    to.HexToByteArray(), amount.ToBytesForRLPEncoding(), data.HexToByteArray(), chainId.ToBytesForRLPEncoding(), rBytes, sBytes, vBytes));
            }
            else
            {
                return(new LegacyTransaction(nonce.ToBytesForRLPEncoding(), gasPrice.ToBytesForRLPEncoding(), gas.ToBytesForRLPEncoding(),
                                             to.HexToByteArray(), amount.ToBytesForRLPEncoding(), data.HexToByteArray(), rBytes, sBytes, vBytes[0]));
            }
        }
Пример #8
0
        public static EthECDSASignature ExtractEcdsaSignature(string signature)
        {
            var signatureArray = signature.HexToByteArray();

            var v = signatureArray[64];

            if ((v == 0) || (v == 1))
            {
                v = (byte)(v + 27);
            }

            var r = new byte[32];

            Array.Copy(signatureArray, r, 32);
            var s = new byte[32];

            Array.Copy(signatureArray, 32, s, 0, 32);

            var ecdaSignature = EthECDSASignatureFactory.FromComponents(r, s, v);

            return(ecdaSignature);
        }
Пример #9
0
        public static SignedData DecodeSigned(byte[] rawdata, int numberOfEncodingElements)
        {
            var decodedList             = RLP.RLP.Decode(rawdata);
            var decodedData             = new List <byte[]>();
            var decodedElements         = (RLPCollection)decodedList[0];
            EthECDSASignature signature = null;

            for (var i = 0; i < numberOfEncodingElements; i++)
            {
                decodedData.Add(decodedElements[i].RLPData);
            }
            // only parse signature in case is signed
            if (decodedElements[numberOfEncodingElements].RLPData != null)
            {
                //Decode Signature
                var v = decodedElements[numberOfEncodingElements].RLPData;
                var r = decodedElements[numberOfEncodingElements + 1].RLPData;
                var s = decodedElements[numberOfEncodingElements + 2].RLPData;
                signature = EthECDSASignatureFactory.FromComponents(r, s, v);
            }
            return(new SignedData(decodedData.ToArray(), signature));
        }
Пример #10
0
        public EthECDSASignature SignAndCalculateYParityV(byte[] hash)
        {
#if NETCOREAPP3_1 || NET5_0_OR_GREATER
            if (SignRecoverable)
            {
                var privKey = Context.Instance.CreateECPrivKey(GetPrivateKeyAsBytes());
                privKey.TrySignRecoverable(hash, out var recSignature);
                recSignature.Deconstruct(out Scalar r, out var s, out var recId);
                return(EthECDSASignatureFactory.FromComponents(r.ToBytes(), s.ToBytes(), new[] { (byte)(recId) }));
            }
            else
            {
#endif
            var signature = _ecKey.Sign(hash);
            var recId     = CalculateRecId(signature, hash);
            signature.V = new[] { (byte)(recId) };
            return(new EthECDSASignature(signature));

#if NETCOREAPP3_1 || NET5_0_OR_GREATER
        }
#endif
        }
Пример #11
0
        public void RlpDecode()
        {
            var decodedList     = RLP.RLP.Decode(GetRLPEncoded());
            var decodedData     = new List <byte[]>();
            var decodedElements = (RLPCollection)decodedList[0];

            for (var i = 0; i < numberOfElements; i++)
            {
                decodedData.Add(decodedElements[i].RLPData);
            }
            // only parse signature in case is signed
            if (decodedElements[numberOfElements].RLPData != null)
            {
                var v = decodedElements[numberOfElements].RLPData[0];
                var r = decodedElements[numberOfElements + 1].RLPData;
                var s = decodedElements[numberOfElements + 2].RLPData;

                signature = EthECDSASignatureFactory.FromComponents(r, s, v);
            }
            data    = decodedData.ToArray();
            decoded = true;
        }