public static async Task TestTransferTokenSigning() { using (var trezorManager = await TrezorFactory.GetWindowsConnectedLedgerManagerAsync(GetPin)) { await trezorManager.InitializeAsync(); var signer = new TrezorExternalSigner(trezorManager, 0); var account = new ExternalAccount(signer); await account.InitialiseAsync(); var rpcClient = new RpcClient(new Uri("http://localhost:8545")); account.InitialiseDefaultTransactionManager(rpcClient); var web3 = new Web3.Web3(account, rpcClient); var tx = new TransferFunction() { Nonce = 10, GasPrice = 10, Gas = 21000, To = "0x689c56aef474df92d44a1b70850f808488f9769c", Value = 100, FromAddress = "0x6A1D4583b83E5ef91EeA1E591aD333BD04853399" }; var signature = await web3.Eth.GetContractTransactionHandler <TransferFunction>().SignTransactionAsync("0x6810e776880c02933d47db1b9fc05908e5386b96", tx); var web32 = new Web3.Web3(new Account("0x2e14c29aaecd1b7c681154d41f50c4bb8b6e4299a431960ed9e860e39cae6d29")); var signatureNethereum = await web32.Eth.GetContractTransactionHandler <TransferFunction>() .SignTransactionAsync("0x6810e776880c02933d47db1b9fc05908e5386b96", tx); System.Console.WriteLine("Trezor: " + signature); System.Console.WriteLine("Nethereum: " + signatureNethereum); } }
public static async Task TestTransactionSigning() { using (var trezorManager = await TrezorFactory.GetWindowsConnectedLedgerManagerAsync(GetPin)) { await trezorManager.InitializeAsync(); var signer = new TrezorExternalSigner(trezorManager, 0); var account = new ExternalAccount(signer); await account.InitialiseAsync(); account.InitialiseDefaultTransactionManager(new RpcClient(new Uri("http://localhost:8545"))); var tx = new TransactionInput() { Nonce = new HexBigInteger(10), GasPrice = new HexBigInteger(10), Gas = new HexBigInteger(21000), To = "0x689c56aef474df92d44a1b70850f808488f9769c", Value = new HexBigInteger(BigInteger.Parse("10000000000000000000")), From = "0x6A1D4583b83E5ef91EeA1E591aD333BD04853399" }; var signature = await account.TransactionManager.SignTransactionAsync(tx); var accountNethereum = new Account("0x2e14c29aaecd1b7c681154d41f50c4bb8b6e4299a431960ed9e860e39cae6d29"); accountNethereum.TransactionManager.Client = new RpcClient(new Uri("http://localhost:8545")); var signatureNethereum = await accountNethereum.TransactionManager.SignTransactionAsync(tx); System.Console.WriteLine("Trezor: " + signature); System.Console.WriteLine("Nethereum: " + signatureNethereum); } }
public EthereumOracleConnector(string endpoint, string contractAddress, string pkcsUser, string key) { this.contractAddress = contractAddress; var client = new Nethereum.JsonRpc.Client.RpcClient(new Uri(endpoint)); account = new ExternalAccount("0x7957Db97cB19fB029968595E9325e2E5C92EAF33", new HSMExternalEthSigner(pkcsUser, key), 1); account.NonceService = new InMemoryNonceService("0x7957Db97cB19fB029968595E9325e2E5C92EAF33", client); account.InitialiseDefaultTransactionManager(client); web3 = new Web3Geth(account, endpoint); contract = web3.Eth.GetContract(abi, contractAddress); tokenDictionary.Add("USDT", "0xdac17f958d2ee523a2206206994597c13d831ec7"); tokenDictionary.Add("MTL", "0xF433089366899D83a9f26A773D59ec7eCF30355e"); tokenDictionary.Add("USDC", "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"); tokenDictionary.Add("BAT", "0x0d8775f648430679a709e98d2b0cb6250d2887ef"); tokenDictionary.Add("CRO", "0xa0b73e1ff0b80914ab6fe0444e65848c4c34450b"); tokenDictionary.Add("DEX", "0x497bAEF294c11a5f0f5Bea3f2AdB3073DB448B56"); tokenDictionary.Add("TUSD", "0x0000000000085d4780B73119b644AE5ecd22b376"); tokenDictionary.Add("DAI", "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"); tokenDictionary.Add("EGT", "0x8e1b448ec7adfc7fa35fc2e885678bd323176e34"); tokenDictionary.Add("ENJ", "0xf629cbd94d3791c9250152bd8dfbdf380e2a3b9c"); tokenDictionary.Add("HT", "0x6f259637dcd74c767781e37bc6133cd6a68aa161"); tokenDictionary.Add("INB", "0x17aa18a4b64a55abed7fa543f2ba4e91f2dce482"); tokenDictionary.Add("KCS", "0x039b5649a59967e3e936d7471f9c3700100ee1ab"); tokenDictionary.Add("ELF", "0xbf2179859fc6D5BEE9Bf9158632Dc51678a4100e"); tokenDictionary.Add("ZRX", "0xe41d2489571d322189246dafa5ebde1f4699f498"); tokenDictionary.Add("LINK", "0x514910771af9ca656af840dff83e8264ecf986ca"); tokenDictionary.Add("MANA", "0x0f5d2fb29fb7d3cfee444a200298f468908cc942"); tokenDictionary.Add("MATIC", "0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0"); tokenDictionary.Add("MCO", "0xb63b606ac810a52cca15e44bb630fd42d8d1d83d"); tokenDictionary.Add("MKR", "0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2"); tokenDictionary.Add("ZB", "0xbd0793332e9fb844a52a205a233ef27a5b34b927"); tokenDictionary.Add("OMG", "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"); tokenDictionary.Add("PAX", "0x8e870d67f660d95d5be530380d0ec0bd388289e1"); tokenDictionary.Add("PERL", "0xb5a73f5fc8bbdbce59bfd01ca8d35062e0dad801"); tokenDictionary.Add("LAMB", "0x8971f9fd7196e5cee2c1032b50f656855af7dd26"); tokenDictionary.Add("SEELE", "0xb1eef147028e9f480dbc5ccaa3277d417d1b85f0"); tokenDictionary.Add("REP", "0x1985365e9f78359a9B6AD760e32412f4a445E862"); tokenDictionary.Add("REALT", "0x46cc7ec70746f4cbd56ce5fa9bb7d648398eaa5c"); tokenDictionary.Add("BRZ", "0x420412e765bfa6d85aaac94b4f7b708c89be2e2b"); tokenDictionary.Add("SNT", "0x744d70fdbe2ba4cf95131626614a1763df805b9e"); tokenDictionary.Add("NXPS", "0xa15c7ebe1f07caf6bff097d8a589fb8ac49ae5b3"); tokenDictionary.Add("GUSD", "0x056fd409e1d7a124bd7017459dfea2f387b6d5cd"); tokenDictionary.Add("BNB", "0xB8c77482e45F1F44dE1745F52C74426C631bDD52"); }
public async Task Test(string addressFrom, string privateKey, bool legacy) { var transfer = new TransferFunction(); transfer.To = "0x12890d2cce102216644c59daE5baed380d848301"; transfer.FromAddress = addressFrom; transfer.Value = 1; transfer.Nonce = 1; transfer.GasPrice = 100; transfer.Gas = 1000; var rpcClient = new HttpClient(new Uri("http://localhost:8545"), defaultTimeOutForTests); var transactionInput = transfer.CreateTransactionInput("0x12890d2cce102216644c59daE5baed380d84830c"); var account = new Account(privateKey, Chain.MainNet); account.TransactionManager.Client = rpcClient; var signature = await account.TransactionManager.SignTransactionAsync(transactionInput); var ledgerManager = await LedgerFactory.GetWindowsConnectedLedgerManager(); var externalAccount = new ExternalAccount(new LedgerExternalSigner(ledgerManager, 0, legacy), 1); await externalAccount.InitialiseAsync(); externalAccount.InitialiseDefaultTransactionManager(rpcClient); //Ensure contract data is enable in the settings of ledger nano var signature2 = await externalAccount.TransactionManager.SignTransactionAsync(transactionInput); Assert.Equal(signature, signature2); //Signing just transfer var signature3 = await externalAccount.TransactionManager.SignTransactionAsync(new TransactionInput() { From = addressFrom, GasPrice = new HexBigInteger(Transaction.DEFAULT_GAS_PRICE), Gas = new HexBigInteger(Transaction.DEFAULT_GAS_LIMIT), Nonce = new HexBigInteger(1), To = "0x12890d2cce102216644c59daE5baed380d848301", Value = new HexBigInteger(100) }); }
public async Task TestSignature() { var transfer = new TransferFunction(); transfer.To = "0x12890d2cce102216644c59daE5baed380d848301"; transfer.FromAddress = "0x1996a57077877D38e18A1BE44A55100D77b8fA1D"; transfer.Value = 1; transfer.Nonce = 1; transfer.GasPrice = 100; transfer.Gas = 1000; var rpcClient = new RpcClient(new Uri("http://localhost:8545")); var transactionInput = transfer.CreateTransactionInput("0x12890d2cce102216644c59daE5baed380d84830c"); var account = new Account("0x128c6818917d98a3b933de1d400e777963424ce71f0a58755b092d1b670394eb", Chain.MainNet); account.TransactionManager.Client = rpcClient; var signature = await account.TransactionManager.SignTransactionAsync(transactionInput); var externalAccount = new ExternalAccount(new EthECKeyExternalSigner(new LedgerExternalSigner()), 1); await externalAccount.InitialiseAsync(); externalAccount.InitialiseDefaultTransactionManager(rpcClient); //Ensure contract data is enable in the settings of ledger nano var signature2 = await externalAccount.TransactionManager.SignTransactionAsync(transactionInput); Assert.Equal(signature, signature2); //Signing just transfer var signature3 = await externalAccount.TransactionManager.SignTransactionAsync(new TransactionInput() { From = "0x1996a57077877D38e18A1BE44A55100D77b8fA1D", GasPrice = new HexBigInteger(Transaction.DEFAULT_GAS_PRICE), Gas = new HexBigInteger(Transaction.DEFAULT_GAS_LIMIT), Nonce = new HexBigInteger(1), To = "0x12890d2cce102216644c59daE5baed380d848301", Value = new HexBigInteger(100) }); }
static void Main(string[] args) { KeyVaultClient kvc = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken)); var signer = new AzureKeyVaultExternalSigner(kvc, URI); var publicKey = signer.GetPublicKeyAsync().Result; System.Console.WriteLine(publicKey.ToHex()); var msgHash = new Util.Sha3Keccack().CalculateHash("Hello").HexToByteArray(); var ethExternalSigner = new EthECKeyExternalSigner(signer); var signature = ethExternalSigner.SignAndCalculateVAsync(msgHash).Result; var publicKeyRecovered = EthECKey.RecoverFromSignature(signature, msgHash); System.Console.WriteLine(publicKeyRecovered.GetPubKey().ToHex()); var transfer = new TransferFunction(); transfer.To = "0x1996a57077877D38e18A1BE44A55100D77b8fA1D"; transfer.FromAddress = publicKeyRecovered.GetPublicAddress(); transfer.Value = 1; transfer.Nonce = 1; transfer.GasPrice = 100; transfer.Gas = 1000; var rpcClient = new RpcClient(new Uri("http://localhost:8545")); var transactionInput = transfer.CreateTransactionInput("0x12890d2cce102216644c59daE5baed380d84830c"); var externalAccount = new ExternalAccount(ethExternalSigner, 1); externalAccount.InitialiseAsync().Wait(); externalAccount.InitialiseDefaultTransactionManager(rpcClient); var signature2 = externalAccount.TransactionManager.SignTransactionAsync(transactionInput).Result; var transactionSigner = new TransactionSigner(); var publicKeyRecovered2 = transactionSigner.GetPublicKey(signature2, 1); System.Console.WriteLine(publicKeyRecovered2.ToHex()); System.Console.ReadLine(); }
public async Task <TransactionReturnInfo> CreateTransactionWithExternalAccountAsync(string ContractAddress, ContractInfo contractInfo, string FunctionName, ExternalAccount externalAccount, object[] inputParams = null, List <string> PrivateFor = null) { if (web3 == null) { throw new Exception("web3 handler has not been set - please call SetWeb3Handler First"); } if (PrivateFor != null && PrivateFor?.Count != 0) { web3.ClearPrivateForRequestParameters(); web3.SetPrivateRequestParameters(PrivateFor); } var contract = web3.Eth.GetContract(contractInfo.ContractABI, ContractAddress); if (contract == null) { throw new Exception("Could not find contract with ABI at specified address"); } var contractFunction = contract.GetFunction(FunctionName); if (contractFunction == null) { throw new Exception("Could not find function with name " + FunctionName); } try { await externalAccount.InitialiseAsync(); externalAccount.InitialiseDefaultTransactionManager(web3.Client); //default RealGas var realGas = new HexBigInteger(500000); var txInput = contractFunction.CreateTransactionInput(externalAccount.Address, realGas, new HexBigInteger(0), new HexBigInteger(0), inputParams); //var txCountNonce = await externalAccount.NonceService.GetNextNonceAsync(); //--- set nonce? ---// //txInput.Nonce = txCountNonce; //var supposedNextNonce = await account.NonceService.GetNextNonceAsync(); //log.LogInformation("Nonce txInput Value: " + txCountNonce.Value ); //Console.WriteLine("Nonce txInput Value: " + txCountNonce.Value ); var transactionReceipt = await externalAccount.TransactionManager.SendTransactionAndWaitForReceiptAsync(txInput, null); if (transactionReceipt != null) { Console.WriteLine($"Processed Transaction - txHash: {transactionReceipt.TransactionHash}"); return(new TransactionReturnInfo { TransactionHash = transactionReceipt.TransactionHash, BlockHash = transactionReceipt.BlockHash, BlockNumber = transactionReceipt.BlockNumber.Value, ContractAddress = ContractAddress }); } return(null); } catch (Exception e) { Console.WriteLine(e.Message); return(null); } }
public async Task <TransactionReturnInfo> CreateContractWithExternalAccountAsync(ContractInfo contractInfo, ExternalAccount externalAccount, object[] inputParams = null, List <string> PrivateFor = null) { if (web3 == null) { throw new Exception("web3 handler has not been set - please call SetWeb3Handler First"); } if (PrivateFor != null && PrivateFor?.Count != 0) { web3.ClearPrivateForRequestParameters(); web3.SetPrivateRequestParameters(PrivateFor); } await externalAccount.InitialiseAsync(); Console.WriteLine("externalAccount InitializeAsync() completed"); externalAccount.InitialiseDefaultTransactionManager(web3.Client); Console.WriteLine("externalAccount.InitialiseDefaultTransactionManager() completed"); //--- get transaction count to set nonce ---// var txCount = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(externalAccount.Address, BlockParameter.CreatePending()); Console.WriteLine("web3.Eth.Transactions.GetTransactionCount.SendRequestAsync() completed"); try { var gasDeploy = await web3.Eth.DeployContract.EstimateGasAsync( abi : contractInfo.ContractABI, contractByteCode : contractInfo.ContractByteCode, from : externalAccount.Address, values : inputParams == null?new object[] {} : inputParams); Console.WriteLine("Creating new contract and waiting for address"); // Gas estimate is usually low - with private quorum we don't need to worry about gas so lets just multiply it by 5. var realGas = new HexBigInteger(gasDeploy.Value * 5); Console.WriteLine("About to call web3.Eth.DeployContract.GetData"); var txData = web3.Eth.DeployContract.GetData( contractInfo.ContractByteCode, contractInfo.ContractABI, inputParams ); Console.WriteLine("Returned from web3.Eth.DeployContract.GetData with the returned data of: {0}", txData); var txInput = new TransactionInput( txData, externalAccount.Address, realGas, new HexBigInteger(0) ); Console.WriteLine("About to call externalAccount.TransactionManager.SendTransactionAndWaitForReceiptAsync"); var transactionReceipt = await externalAccount.TransactionManager.SendTransactionAndWaitForReceiptAsync(txInput, null); Console.WriteLine("Returned from externalAccount.TransactionManager.SendTransactionAndWaitForReceiptAsync"); Console.WriteLine(transactionReceipt.ContractAddress); return(new TransactionReturnInfo { TransactionHash = transactionReceipt.TransactionHash, BlockHash = transactionReceipt.BlockHash, BlockNumber = transactionReceipt.BlockNumber.Value, ContractAddress = transactionReceipt.ContractAddress }); } catch (Exception e) { Console.WriteLine(e.Message); return(null); } }
static void Main(string[] args) { //var credential = new DefaultAzureCredential( // new DefaultAzureCredentialOptions // { // //VisualStudioTenantId = TENANT_ID, // //VisualStudioCodeTenantId = TENANT_ID, // }); var credential = new ClientSecretCredential(TENANT_ID, APP_ID, APP_PASSWORD); var signer = new AzureKeyVaultExternalSigner(KEY_NAME, URI, credential); var address = signer.GetAddressAsync().Result; System.Console.WriteLine(address); var msgHash = new Util.Sha3Keccack().CalculateHash("Hello").HexToByteArray(); var signature = signer.SignAsync(msgHash).Result; var publicKeyRecovered = EthECKey.RecoverFromSignature(signature, msgHash); System.Console.WriteLine(publicKeyRecovered.GetPubKey().ToHex()); var transfer = new TransferFunction(); transfer.To = "0x1996a57077877D38e18A1BE44A55100D77b8fA1D"; transfer.FromAddress = publicKeyRecovered.GetPublicAddress(); transfer.Value = 1; transfer.Nonce = 1; transfer.GasPrice = 100; transfer.Gas = 1000; var rpcClient = new RpcClient(new Uri("http://localhost:8545")); var transactionInput = transfer.CreateTransactionInput("0x12890d2cce102216644c59daE5baed380d84830c"); var externalAccount = new ExternalAccount(signer, 1); externalAccount.InitialiseAsync().Wait(); externalAccount.InitialiseDefaultTransactionManager(rpcClient); var signature2 = externalAccount.TransactionManager.SignTransactionAsync(transactionInput).Result; var publicKeyRecovered2 = TransactionVerificationAndRecovery.GetPublicKey(signature2); var transactionFromSignature = TransactionFactory.CreateTransaction(signature2); System.Console.WriteLine("Recovered public key"); System.Console.WriteLine(publicKeyRecovered2.ToHex()); System.Console.WriteLine("Recovered transaction Type"); System.Console.WriteLine(transactionFromSignature.TransactionType.ToString()); System.Console.WriteLine("Signing EIP1559"); var transferEip1559 = new TransferFunction(); transferEip1559.To = "0x1996a57077877D38e18A1BE44A55100D77b8fA1D"; transferEip1559.FromAddress = publicKeyRecovered.GetPublicAddress(); transferEip1559.Value = 1; transferEip1559.Nonce = 1; transferEip1559.MaxFeePerGas = 1000; transferEip1559.MaxPriorityFeePerGas = 999; transferEip1559.Gas = 1000; var transactionInputEip1559 = transferEip1559.CreateTransactionInput("0x12890d2cce102216644c59daE5baed380d84830c"); var signature3 = externalAccount.TransactionManager.SignTransactionAsync(transactionInputEip1559).Result; var publicKeyRecovered3 = TransactionVerificationAndRecovery.GetPublicKey(signature3); var transactionFromSignatureEIP1559 = TransactionFactory.CreateTransaction(signature3); System.Console.WriteLine("Recovered public key"); System.Console.WriteLine(publicKeyRecovered3.ToHex()); System.Console.WriteLine("Recovered transaction Type"); System.Console.WriteLine(transactionFromSignatureEIP1559.TransactionType.ToString()); System.Console.ReadLine(); }