Beispiel #1
0
        public override async Task SignAsync(CeloTransactionChainId 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);

            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));
        }
    /// <summary>
    /// Gets the signed transaction data from the Trezor wallet.
    /// </summary>
    /// <param name="transaction"> The transaction to sign. </param>
    /// <param name="path"> The path of the address to sign the transaction with. </param>
    /// <param name="onSignatureRequestSent"> Action to call once the signature request has been sent. </param>
    /// <returns> Task returning the SignedTransactionDataHolder instance. </returns>
    protected override async Task <SignedTransactionDataHolder> GetSignedTransactionData(Transaction transaction, string path, Action onSignatureRequestSent)
    {
        var trezorManager = TrezorConnector.GetWindowsConnectedTrezor(GetSignedTransactionDataEnterPin);

        if (trezorManager == null)
        {
            return(null);
        }

        var signedTransactionResponse = (EthereumTxRequest)null;
        var signedTransactionRequest  = new EthereumSignTx
        {
            Nonce            = transaction.Nonce,
            AddressNs        = KeyPath.Parse(path).Indexes,
            GasPrice         = transaction.GasPrice,
            GasLimit         = transaction.GasLimit,
            To               = transaction.ReceiveAddress,
            Value            = transaction.Value,
            DataInitialChunk = transaction.Data,
            DataLength       = (uint)transaction.Data.Length,
            ChainId          = (uint)ethereumNetworkSettings.networkType
        };

        while (true)
        {
            try
            {
                signedTransactionResponse = await trezorManager.SendMessageAsync <EthereumTxRequest, EthereumSignTx>(signedTransactionRequest, onSignatureRequestSent)
                                            .ConfigureAwait(false);

                break;
            }
            catch (FailureException <Failure> )
            {
                if (forceCancel)
                {
                    forceCancel = false;
                    break;
                }

                if (trezorManager.PinRequest.HasValue && trezorManager.PinRequest == false)
                {
                    break;
                }
            }
        }

        if (signedTransactionResponse == null)
        {
            return(new SignedTransactionDataHolder());
        }

        return(new SignedTransactionDataHolder
        {
            signed = true,
            v = signedTransactionResponse.SignatureV,
            r = signedTransactionResponse.SignatureR,
            s = signedTransactionResponse.SignatureS
        });
    }
Beispiel #3
0
        public async Task SignEthereumTransaction2()
        {
            var txMessage = new EthereumSignTx
            {
                Nonce     = 10.ToBytesForRLPEncoding().ToHex().ToHexBytes(),
                GasPrice  = 1000000000.ToBytesForRLPEncoding().ToHex().ToHexBytes(),
                GasLimit  = 21000.ToBytesForRLPEncoding().ToHex().ToHexBytes(),
                To        = "689c56aef474df92d44a1b70850f808488f9769c",
                Value     = BigInteger.Parse("10000000000000000000").ToBytesForRLPEncoding().ToHex().ToHexBytes(),
                AddressNs = AddressPathBase.Parse <BIP44AddressPath>("m/44'/60'/0'/0/0").ToArray(),
                ChainId   = 1
            };
            var transaction = await TrezorManager.SendMessageAsync <EthereumTxRequest, EthereumSignTx>(txMessage);

            Assert.AreEqual(transaction.SignatureR.Length, 32);
            Assert.AreEqual(transaction.SignatureS.Length, 32);
        }
Beispiel #4
0
        public async Task SignEthereumTransaction()
        {
            //Note: these are not reasonable values. They should not be used for a transaction. Looking for a better example here...
            var txMessage = new EthereumSignTx
            {
                Nonce     = 0.ToHexBytes(),
                GasPrice  = 1000000000.ToHexBytes(),
                GasLimit  = 21000.ToHexBytes(),
                To        = "689c56aef474df92d44a1b70850f808488f9769c",
                Value     = 100000000000000.ToHexBytes(),
                AddressNs = new BIP44AddressPath(false, 60, 0, false, 0).ToArray(),
                ChainId   = 4
            };

            var transaction = await TrezorManager.SendMessageAsync <EthereumTxRequest, EthereumSignTx>(txMessage);

            Assert.AreEqual(transaction.SignatureR.Length, 32);
            Assert.AreEqual(transaction.SignatureS.Length, 32);
        }
Beispiel #5
0
        public async Task SignEthereumTransaction()
        {
            await GetAndInitialize();

            var txMessage = new EthereumSignTx
            {
                Nonce     = 10.ToBytesForRLPEncoding().ToHex().ToHexBytes(),
                GasPrice  = 1000000000.ToBytesForRLPEncoding().ToHex().ToHexBytes(),
                GasLimit  = 21000.ToBytesForRLPEncoding().ToHex().ToHexBytes(),
                To        = "689c56aef474df92d44a1b70850f808488f9769c".ToHexBytes(),
                Value     = BigInteger.Parse("10000000000000000000").ToBytesForRLPEncoding().ToHex().ToHexBytes(),
                AddressNs = KeyPath.Parse("m/44'/60'/0'/0/0").Indexes,
                ChainId   = 1
            };
            var transaction = await KeepKeyManager.SendMessageAsync <EthereumTxRequest, EthereumSignTx>(txMessage);

            Assert.AreEqual(transaction.SignatureR.Length, 32);
            Assert.AreEqual(transaction.SignatureS.Length, 32);
        }
Beispiel #6
0
        public async Task SignEthereumTransaction()
        {
            await GetAndInitialize();

            //Note: these are not reasonable values. They should not be used for a transaction. Looking for a better example here...
            var txMessage = new EthereumSignTx
            {
                Nonce     = "0".ToHexBytes(),
                GasPrice  = 1000000000.ToHexBytes(),
                GasLimit  = 21000.ToHexBytes(),
                To        = "689c56aef474df92d44a1b70850f808488f9769c".ToHexBytes(),
                Value     = "de0b6b3a7640000".ToHexBytes(),
                AddressNs = ManagerHelpers.GetAddressPath(false, 0, false, 0, 60),
                ChainId   = 1
            };

            var transaction = await TrezorManager.SendMessageAsync <EthereumTxRequest, EthereumSignTx>(txMessage);

            Assert.AreEqual(transaction.SignatureR.Length, 32);
            Assert.AreEqual(transaction.SignatureS.Length, 32);
        }
Beispiel #7
0
        public override async Task SignAsync(Transaction transaction)
        {
            var txMessage = new EthereumSignTx
            {
                Nonce     = transaction.Nonce,
                GasPrice  = transaction.GasPrice,
                GasLimit  = transaction.GasLimit,
                To        = transaction.ReceiveAddress,
                Value     = transaction.Value,
                AddressNs = GetPath(),
            };

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

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

            transaction.SetSignature(EthECDSASignatureFactory.FromComponents(signature.SignatureR, signature.SignatureS, (byte)signature.SignatureV));
        }
 public async Task <EthereumTxRequest> SignEthereumTransactionAsync(EthereumSignTx signTx)
 {
     return(await SendMessageAsync <EthereumTxRequest, EthereumSignTx>(signTx));
 }
Beispiel #9
0
        /// <summary>
        /// TODO: This should be made in to a unit test but it's annoying to add the UI for a unit test as the Trezor requires human intervention for the pin
        /// </summary>
        /// <returns></returns>
        private async static Task Go()
        {
            try
            {
                using (var trezorHid = await Connect())
                {
                    using (var trezorManager = new TrezorManager(GetPin, trezorHid))
                    {
                        await trezorManager.InitializeAsync();

                        var tasks = new List <Task>();

                        for (var i = 0; i < 50; i++)
                        {
                            tasks.Add(DoGetAddress(trezorManager, i));
                        }

                        await Task.WhenAll(tasks);

                        for (var i = 0; i < 50; i++)
                        {
                            var address = await GetAddress(trezorManager, i);

                            Console.WriteLine($"Index: {i} (No change) - Address: {address}");

                            if (address != _Addresses[i])
                            {
                                throw new Exception("The ordering got messed up");
                            }
                        }

                        var ethAddress = await trezorManager.GetAddressAsync("ETH", 60, false, 0, false, AddressType.Ethereum);

                        Console.WriteLine($"First ETH address: {ethAddress}");

                        //This fails with 'Safety check failed'
                        //See https://github.com/trezor/trezor-mcu/issues/143
                        //https://github.com/trezor/trezor-mcu/blob/master/firmware/ethereum.c
                        //Which values here need leading zeros? Is Unicode the correct encoding?



                        var message = new EthereumSignMessage
                        {
                            AddressNs = KeyPath.Parse("m/44'/60'/0'/0/0").Indexes,
                            Message   = Encoding.UTF8.GetBytes("Hello").ToHex().ToHexBytes()
                        };

                        var messageSignature = await trezorManager.SendMessageAsync <EthereumMessageSignature, EthereumSignMessage>(message);

                        //Same as first address
                        Console.WriteLine(ToHex(messageSignature.Address));

                        var signer            = new Nethereum.Signer.EthereumMessageSigner();
                        var messageSignature2 = signer.EncodeUTF8AndSign("Hello2", new EthECKey(
                                                                             "0x2e14c29aaecd1b7c681154d41f50c4bb8b6e4299a431960ed9e860e39cae6d29"));
                        // var recoveredAddress = signer.EncodeUTF8AndEcRecover("Hello", messageSignature.Signature.ToHex());
                        //Same as recovered address
                        //Console.WriteLine(recoveredAddress);

                        //var txMessage = new EthereumSignTx
                        //{
                        //    Nonce = 1.ToHexBytes(),
                        //    GasPrice = 1000000000.ToHexBytes(),
                        //    GasLimit = 21000.ToHexBytes(),
                        //    To = "689c56aef474df92d44a1b70850f808488f9769c".ToHexBytes(),
                        //    Value = 1000000000000000.ToHexBytes(),
                        //    //Value = "de0b6b3a7640000".ToHexBytes(),
                        //    AddressNs = KeyPath.Parse("m/44'/60'/0'/0/0").Indexes,
                        //    ChainId = 1
                        //};



                        var txMessage = new EthereumSignTx
                        {
                            Nonce    = 10.ToBytesForRLPEncoding().ToHex().ToHexBytes(),
                            GasPrice = 10.ToBytesForRLPEncoding().ToHex().ToHexBytes(),
                            GasLimit = 21000.ToBytesForRLPEncoding().ToHex().ToHexBytes(),
                            To       = "689c56aef474df92d44a1b70850f808488f9769c".ToHexBytes(),
                            Value    = BigInteger.Parse("10000000000000000000").ToBytesForRLPEncoding().ToHex().ToHexBytes(),
                            //Value = "de0b6b3a7640000".ToHexBytes(),
                            AddressNs = KeyPath.Parse("m/44'/60'/0'/0/0").Indexes,
                            //ChainId = 1
                        };
                        var transaction = await trezorManager.SendMessageAsync <EthereumTxRequest, EthereumSignTx>(txMessage);

                        var transactionSigner = new TransactionSigner();
                        var sigature2         = transactionSigner.SignTransaction("0x2e14c29aaecd1b7c681154d41f50c4bb8b6e4299a431960ed9e860e39cae6d29",
                                                                                  "0x689c56aef474df92d44a1b70850f808488f9769c", 10, 10, 10, 21000);
                        var transactionRecovered = new Transaction(sigature2.HexToByteArray());
                        //var transactionChainId = new TransactionChainId(sigature2.HexToByteArray(), 1);


                        Console.WriteLine("All good");

                        Console.ReadLine();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }
        }