- Console packages:
- id: Nethereum.HdWallet version: 2.1.0
- id: Nethereum.Web3 version: 2.0.1
Sending Ether using Infura and an HD Wallet
Walkthrough on how to instantiate an HD wallet using the mnemonic backup seed words, retrieve the first account private key and finally transfer some Ether using Infura.
First of all we need to add a nuget package reference to Nethereum.HdWallet
and declare the required namespaces:
using Nethereum.Web3; using Nethereum.Web3.Accounts; using Nethereum.Util; using Nethereum.Hex.HexConvertors.Extensions; using Nethereum.HdWallet;
Retrieve the account using the mnemonic backup seed words
Using our backup seed words we will retrieve the Account to be able to sign the transactions. The first step will be to declare our word list:
var wordlist = "brass bus same payment express already energy direct type have venture afraid";
Next step is to retrieve the address that we will use to sign the transaction, in this scenario is the first indexed address. Note: Using HD wallets, we can generate and re-generate addresses based on an index number from the same seed.
var address = "0xE65B318b9dECf504d1cb6Ea5C367Ca657a070Db1"
Now with the backup seed and address we can create the HD wallet and retrive our account. The account contains the private key to sign our transactions.
var account = new Wallet(wordlist, null).GetAccount(address);
If we want to connect to the Ethereum mainnet (public Ethereum network) we will use the mainnet url https://mainnet.infura.io , and together with our Account we can create a new instance of Web3. In this scenario we are connecting to the testnet rinkeby.
var web3 = new Web3(account, "https://rinkeby.infura.io");
Converting Ether to Wei
Ether needs to be converted to Wei before sending it, for this we will use the Conversion Utility.
So if we were going to send 1 Ether we will use:
var wei = Web3.Convert.ToWei(1);
Finally we will just set the address that we want to send some Ether, and using the Transaction manager this will be signed with our private key and and sent to the network via Infura public node.
var toAddress = "0x12890D2cce102216644c59daE5baed380d84830c"; var transaction = await web3.TransactionManager.SendTransactionAsync(account.Address, toAddress, new Nethereum.Hex.HexTypes.HexBigInteger(wei));