Esempio n. 1
0
        public override async Task SignAsync(LegacyTransactionChainId transaction)
        {
            var txMessage = new EthereumSignTx
            {
                Nonce     = transaction.Nonce,
                GasPrice  = transaction.GasPrice,
                GasLimit  = transaction.GasLimit,
                To        = transaction.ReceiveAddress,
                Value     = transaction.Value,
                AddressNs = GetPath(),
                ChainId   = (uint)new BigInteger(transaction.ChainId)
            };

            if (transaction.Data.Length > 0)
            {
                txMessage.DataInitialChunk = transaction.Data;
                txMessage.DataLength       = (uint)transaction.Data.Length;
            }

            var signature = await TrezorManager.SendMessageAsync <EthereumTxRequest, EthereumSignTx>(txMessage).ConfigureAwait(false);

            if (signature.SignatureS == null || signature.SignatureR == null)
            {
                throw new Exception("Signing failure or not accepted");
            }
            transaction.SetSignature(EthECDSASignatureFactory.FromComponents(signature.SignatureR, signature.SignatureS, (byte)signature.SignatureV));
        }
Esempio n. 2
0
        public void BasicSigningETC()
        {
            var nonce    = 9.ToBytesForRLPEncoding();
            var gasPrice = BigInteger.Parse("20" + "000" + "000" + "000").ToBytesForRLPEncoding();
            var gasLimit = 21000.ToBytesForRLPEncoding();
            var to       = "0x3535353535353535353535353535353535353535".HexToByteArray();
            var amount   = BigInteger.Parse("1" + "000" + "000" + "000" +
                                            "000" + "000" + "000").ToBytesForRLPEncoding();
            var data = "".HexToByteArray();

            var chainId = ((int)Chain.ClassicMainNet).ToBytesForRLPEncoding();

            //Create a transaction from scratch
            var tx = new LegacyTransactionChainId(nonce, gasPrice, gasLimit, to, amount, data, chainId);

            var privateKey = "4646464646464646464646464646464646464646464646464646464646464646";
            var key        = new EthECKey(privateKey);

            tx.Sign(key);

            var recoveryTransaction = new LegacyTransactionChainId(tx.GetRLPEncoded());

            Assert.True(recoveryTransaction.Key.VerifyAllowingOnlyLowS(recoveryTransaction.RawHash, recoveryTransaction.Signature));

            Assert.Equal(key.GetPublicAddress(), recoveryTransaction.Key.GetPublicAddress());
        }
Esempio n. 3
0
 public override Task SignAsync(LegacyTransactionChainId transaction) => SignHashTransactionAsync(transaction);
Esempio n. 4
0
 public override async Task SignAsync(LegacyTransactionChainId transaction)
 {
     await SignRLPTransactionAsync(transaction).ConfigureAwait(false);
 }
Esempio n. 5
0
 public override Task SignAsync(LegacyTransactionChainId transaction)
 {
     return(SignHashTransactionAsync(transaction));
 }
Esempio n. 6
0
        // ported from the main example in the spec:
        // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
        // Modified from the original pull request of Andres Aragoneses (TDD) @Knocte
        public void BasicSigning()
        {
            var nonce    = 9.ToBytesForRLPEncoding();
            var gasPrice = BigInteger.Parse("20" + "000" + "000" + "000").ToBytesForRLPEncoding();
            var gasLimit = 21000.ToBytesForRLPEncoding();
            var to       = "0x3535353535353535353535353535353535353535".HexToByteArray();
            var amount   = BigInteger.Parse("1" + "000" + "000" + "000" +
                                            "000" + "000" + "000").ToBytesForRLPEncoding();
            var data = "".HexToByteArray();

            byte[] ZERO_BYTE_ARRAY = 0.ToBytesForRLPEncoding();

            var chain   = 1;
            var chainId = ((int)chain).ToBytesForRLPEncoding();

            //Create a transaction from scratch
            var tx = new LegacyTransactionChainId(nonce, gasPrice, gasLimit, to, amount, data, chainId);

            Assert.Equal(chainId, tx.ChainId);
            Assert.Equal(ZERO_BYTE_ARRAY, tx.RHash);
            Assert.Equal(ZERO_BYTE_ARRAY, tx.SHash);

            var expectedSigningData =
                "ec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080018080";

            Assert.Equal(expectedSigningData, tx.GetRLPEncodedRaw().ToHex());

            var expectedSigningHash = "daf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53";

            Assert.Equal(expectedSigningHash, tx.RawHash.ToHex());

            var privateKey = "4646464646464646464646464646464646464646464646464646464646464646";
            var key        = new EthECKey(privateKey);

            tx.Sign(key);

            var rFromSignature         = tx.Signature.R.ToBigIntegerFromRLPDecoded();
            var expectedRFromSignature = "18515461264373351373200002665853028612451056578545711640558177340181847433846";

            Assert.Equal(expectedRFromSignature, rFromSignature.ToString());

            var sFromSignature         = tx.Signature.S.ToBigIntegerFromRLPDecoded();
            var expectedSFromSignature = "46948507304638947509940763649030358759909902576025900602547168820602576006531";

            Assert.Equal(expectedSFromSignature, sFromSignature.ToString());

            byte[] vFromSignature = tx.Signature.V;

            Assert.Equal(37, vFromSignature.ToIntFromRLPDecoded());

            var expectedSignedTx = "f86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83";

            Assert.Equal(expectedSignedTx.Length, tx.GetRLPEncoded().ToHex().Length);
            Assert.Equal(expectedSignedTx, tx.GetRLPEncoded().ToHex());

            var recoveryTransaction = new LegacyTransactionChainId(tx.GetRLPEncoded());

            Assert.True(recoveryTransaction.Key.VerifyAllowingOnlyLowS(recoveryTransaction.RawHash, recoveryTransaction.Signature));

            Assert.Equal(key.GetPublicAddress(), recoveryTransaction.Key.GetPublicAddress());
        }