This is a fork of the HD Wallet-enabled Web3 provider from @truffle/hdwallet-provider.
This fork updates the web3 package and fixes typings.
Use it to sign transactions for addresses derived from a 12 or 24 word mnemonic.
npm install @switcheo/hdwallet-provider
- Node >= 7.6
- Web3 ^1.2.9
You can use this provider wherever a Web3 provider is needed, not just in Truffle. For Truffle-specific usage, see next section.
const HDWalletProvider = ;const Web3 = ;const mnemonic = "mountains supernatural bird..."; // 12 word mnemoniclet provider = mnemonic "";// Or, alternatively pass in a zero-based address index.provider = mnemonic "" 5;// Or, use your own hierarchical derivation pathprovider = mnemonic "" 5 1 true "m/44'/137'/0'/0/";// HDWalletProvider is compatible with Web3. Use it at Web3 constructor, just like any other Web3 Providerconst web3 = provider;// Or, if web3 is alreay initialized, you can call the 'setProvider' on web3, web3.eth, web3.shh and/or web3.bzzweb3// ...// Write your code here.// ...// At termination, `provider.engine.stop()' should be called to finish the process elegantly.providerengine;
By default, the
HDWalletProvider will use the address of the first address that's generated from the mnemonic. If you pass in a specific index, it'll use that address instead.
||null||[x]||12 word mnemonic which addresses are created from.|
||[x]||URI or Ethereum client to send all other non-transaction-related Web3 requests|
||[ ]||If specified, will tell the provider to manage the address at the index specified|
||[ ]||If specified, will create
||[ ]||If false, a new WalletProvider will track its own nonce-state|
||[ ]||If specified, will tell the wallet engine what derivation path should use to derive addresses.|
Instead of a mnemonic, you can alternatively provide a private key or array of private keys as the first parameter. When providing an array,
num_addresses are fully supported.
const HDWalletProvider = ;//load single private key as stringlet provider = "3f841bf589fdf83a521e55d51afddc34fa65351161eead24f064855fc29c9580" "";// Or, pass an array of private keys, and optionally use a certain subset of addressesconst privateKeys ="3f841bf589fdf83a521e55d51afddc34fa65351161eead24f064855fc29c9580""9549f39decea7b7504e15572b2c6a72766df0281cea22bd1a3bc87166b1ca290";provider = privateKeys "" 0 2; //start at address_index 0 and load both addresses
NOTE: This is just an example. NEVER hard code production/mainnet private keys in your code or commit them to git. They should always be loaded from environment variables or a secure secret management system.
You can easily use this within a Truffle configuration. For instance:
const HDWalletProvider = ;const mnemonic = "mountains supernatural bird ...";moduleexports =networks:development:host: "localhost"port: 8545network_id: "*" // Match any network idropsten:// must be a thunk, otherwise truffle commands may hang in CImnemonic ""0 1 true "m/44'/1'/0'/0/"network_id: '3';