コード例 #1
0
        protected override async Task <ECDSASignature> SignExternallyAsync(byte[] bytes)
        {
            var message = new EthereumSignMessage
            {
                AddressNs = GetPath(),
                Message   = bytes
            };

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

            return(ECDSASignatureFactory.ExtractECDSASignature(messageSignature.Signature));
        }
コード例 #2
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();
            }
        }