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)); }
public App(IDevice trezorHidDevice) { _TrezorManager = new TrezorManager(TrezorPinPad.GetPin, trezorHidDevice, new DefaultCoinUtility()); InitializeComponent(); trezorHidDevice.Connected += TrezorHidDevice_Connected; MainNavigationPage = new NavigationPage(new MainPage()); MainPage = MainNavigationPage; }
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)); }
public override async Task Initialize() { if (AddressDeriver != null) { return; } var trezorHidDevice = await Connect(); var trezorManager = new TrezorManager(GetPin, trezorHidDevice); await trezorManager.InitializeAsync(); AddressDeriver = trezorManager; }
/// <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 static async Task Go() { try { using (var trezorHid = await Connect()) { using (var trezorManager = new TrezorManager(GetPin, trezorHid, new DefaultCoinUtility())) { await trezorManager.InitializeAsync(); var tasks = new List <Task>(); for (uint i = 0; i < 50; i++) { tasks.Add(DoGetAddress(trezorManager, i)); } await Task.WhenAll(tasks); for (uint 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 addressPath = new AddressPath(false, 60, 0, false, 0); var ethAddress = await trezorManager.GetAddressAsync(addressPath, false, false); Console.WriteLine($"First ETH address: {ethAddress}"); Console.WriteLine("All good"); Console.ReadLine(); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); } }
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)); }
private static async Task <string> GetAddress(TrezorManager trezorManager, uint i) { return(await trezorManager.GetAddressAsync(new AddressPath(true, 0, 0, false, i), false, false)); }
private static async Task DoGetAddress(TrezorManager trezorManager, uint i) { var address = await GetAddress(trezorManager, i); _Addresses[i] = address; }
private static async Task <string> GetAddress(TrezorManager trezorManager, uint i) => await trezorManager.GetAddressAsync(new BIP44AddressPath(true, 0, 0, false, i), false, false).ConfigureAwait(false);
public override async Task <string> GetAddressAsync() { var addressResponse = await TrezorManager.SendMessageAsync <EthereumAddress, EthereumGetAddress>(new EthereumGetAddress { ShowDisplay = false, AddressNs = GetPath() }); return(addressResponse.Address.ToHex(true).ConvertToEthereumChecksumAddress()); }
public CeloTrezorExternalSigner(TrezorManager trezorManager, string customPath, uint index) { _customPath = customPath; _index = index; TrezorManager = trezorManager; }
public CeloTrezorExternalSigner(TrezorManager trezorManager, uint index) { _index = index; TrezorManager = trezorManager; }
/// <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(); } }
private static async Task <string> GetAddress(TrezorManager trezorManager, int i) { return(await trezorManager.GetAddressAsync("BTC", 0, false, (uint)i, false, AddressType.Bitcoin)); }