|
| 1 | +# @truffle/hdwallet-provider |
| 2 | +HD Wallet-enabled Web3 provider. Use it to sign transactions for addresses derived from a 12 or 24 word mnemonic. |
| 3 | + |
| 4 | +## Install |
| 5 | + |
| 6 | +``` |
| 7 | +$ npm install @truffle/hdwallet-provider |
| 8 | +``` |
| 9 | + |
| 10 | +## Requirements |
| 11 | +``` |
| 12 | +Node >= 12 |
| 13 | +Web3 ^1.2.0 |
| 14 | +``` |
| 15 | + |
| 16 | +## General Usage |
| 17 | + |
| 18 | +You can use this provider wherever a Web3 provider is needed, not just in Truffle. For Truffle-specific usage, see next section. |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +### Instantiation |
| 23 | + |
| 24 | +You can instantiate `hdwallet-provider` with options passed in an object with |
| 25 | +named keys. You can specify the following options in your object: |
| 26 | + |
| 27 | +Parameters: |
| 28 | + |
| 29 | +| Parameter | Type | Default | Required | Description | |
| 30 | +| ------ | ---- | ------- | ----------- | ----------- | |
| 31 | +| `mnemonic` | `object\|string` | `null` | [ ] | Object containing `phrase` and `password` (optional) properties. `phrase` is a 12 word mnemonic string which addresses are created from. Alternately the value for mnemonic can be a string with your mnemonic phrase. | |
| 32 | +| `privateKeys` | `string[]` | `null` | [ ] | Array containing 1 or more private keys. | |
| 33 | +| `providerOrUrl` | `string\|object` | `null` | [x] | URI or Ethereum client to send all other non-transaction-related Web3 requests | |
| 34 | +| `addressIndex` | `number` | `0` | [ ] | If specified, will tell the provider to manage the address at the index specified | |
| 35 | +| `numberOfAddresses` | `number` | `1` | [ ] | If specified, will create `numberOfAddresses` addresses when instantiated | |
| 36 | +| `shareNonce` | `boolean` | `true` | [ ] | If `false`, a new WalletProvider will track its own nonce-state | |
| 37 | +| `derivationPath` | `string` | `"m/44'/60'/0'/0/"` | [ ] | If specified, will tell the wallet engine what derivation path should use to derive addresses. | |
| 38 | +| `pollingInterval` | `number` | `4000` | [ ] | If specified, will tell the wallet engine to use a custom interval when polling to track blocks. Specified in milliseconds. | |
| 39 | +| `chainId` | `number\|string` | `undefined` | [ ] | Specify to enable signed transactions that are EIP-155 compliant for major chains. | |
| 40 | + |
| 41 | +Some examples can be found below: |
| 42 | + |
| 43 | +```javascript |
| 44 | +const HDWalletProvider = require("@truffle/hdwallet-provider"); |
| 45 | +const Web3 = require("web3"); |
| 46 | +const mnemonicPhrase = "mountains supernatural bird..."; // 12 word mnemonic |
| 47 | +let provider = new HDWalletProvider({ |
| 48 | + mnemonic: { |
| 49 | + phrase: mnemonicPhrase |
| 50 | + }, |
| 51 | + providerOrUrl: "http://localhost:8545" |
| 52 | +}); |
| 53 | + |
| 54 | +// Or, alternatively pass in a zero-based address index. |
| 55 | +provider = new HDWalletProvider({ |
| 56 | + mnemonic: mnemonicPhrase, |
| 57 | + providerOrUrl: "http://localhost:8545", |
| 58 | + addressIndex: 5 |
| 59 | +}); |
| 60 | + |
| 61 | +// Or, use your own hierarchical derivation path |
| 62 | +provider = new HDWalletProvider({ |
| 63 | + mnemonic: mnemonicPhrase, |
| 64 | + providerOrUrl: "http://localhost:8545", |
| 65 | + numberOfAddresses: 1, |
| 66 | + shareNonce: true, |
| 67 | + derivationPath: "m/44'/137'/0'/0/" |
| 68 | +}); |
| 69 | + |
| 70 | +// To make HDWallet less "chatty" over JSON-RPC, |
| 71 | +// configure a higher value for the polling interval. |
| 72 | +provider = new HDWalletProvider({ |
| 73 | + mnemonic: { |
| 74 | + phrase: mnemonicPhrase |
| 75 | + }, |
| 76 | + providerOrUrl: "http://localhost:8545", |
| 77 | + pollingInterval: 8000 |
| 78 | +}); |
| 79 | + |
| 80 | +// HDWalletProvider is compatible with Web3. Use it at Web3 constructor, just like any other Web3 Provider |
| 81 | +const web3 = new Web3(provider); |
| 82 | + |
| 83 | +// Or, if web3 is alreay initialized, you can call the 'setProvider' on web3, web3.eth, web3.shh and/or web3.bzz |
| 84 | +web3.setProvider(provider) |
| 85 | + |
| 86 | +// ... |
| 87 | +// Write your code here. |
| 88 | +// ... |
| 89 | + |
| 90 | +// At termination, `provider.engine.stop()' should be called to finish the process elegantly. |
| 91 | +provider.engine.stop(); |
| 92 | +``` |
| 93 | + |
| 94 | +**Note: If both mnemonic and private keys are provided, the mnemonic is used.** |
| 95 | + |
| 96 | +### Using the legacy interface (deprecated) |
| 97 | + |
| 98 | +The legacy interface is deprecated and it is recommended to pass options in an |
| 99 | +object as detailed above. The following method of passing options is here |
| 100 | +primarily to document the interface thoroughly and avoid confusion. |
| 101 | + |
| 102 | +You can specify the following options in the order below. |
| 103 | +Pass `undefined` if you want to omit a parameter. |
| 104 | + |
| 105 | +Parameters: |
| 106 | + |
| 107 | +| Parameter | Type | Default | Required | Description | |
| 108 | +| ------ | ---- | ------- | ----------- | ----------- | |
| 109 | +| `mnemonic`/`privateKeys` | `string`/`string[]` | `null` | [x] | 12 word mnemonic which addresses are created from or array of private keys. | |
| 110 | +| `providerOrUrl` | `string\|object` | `null` | [x] | URI or Ethereum client to send all other non-transaction-related Web3 requests | |
| 111 | +| `addressIndex` | `number` | `0` | [ ] | If specified, will tell the provider to manage the address at the index specified | |
| 112 | +| `numberOfAddresses` | `number` | `1` | [ ] | If specified, will create `numberOfAddresses` addresses when instantiated | |
| 113 | +| `shareNonce` | `boolean` | `true` | [ ] | If `false`, a new WalletProvider will track its own nonce-state | |
| 114 | +| `derivationPath` | `string` | `"m/44'/60'/0'/0/"` | [ ] | If specified, will tell the wallet engine what derivation path should use to derive addresses. | |
| 115 | +| `chainId` | `number\|string` | `undefined` | [ ] | Specify to enable signed transactions that are EIP-155 compliant for major chains. | |
| 116 | + |
| 117 | +Instead of a mnemonic, you can alternatively provide a private key or array of |
| 118 | +private keys as the first parameter. When providing an array, `addressIndex` |
| 119 | +and `numberOfAddresses` are fully supported. |
| 120 | + |
| 121 | +```javascript |
| 122 | +const HDWalletProvider = require("@truffle/hdwallet-provider"); |
| 123 | +//load single private key as string |
| 124 | +let provider = new HDWalletProvider("3f841bf589fdf83a521e55d51afddc34fa65351161eead24f064855fc29c9580", "http://localhost:8545"); |
| 125 | + |
| 126 | +// Or, pass an array of private keys, and optionally use a certain subset of addresses |
| 127 | +const privateKeys = [ |
| 128 | + "3f841bf589fdf83a521e55d51afddc34fa65351161eead24f064855fc29c9580", |
| 129 | + "9549f39decea7b7504e15572b2c6a72766df0281cea22bd1a3bc87166b1ca290", |
| 130 | +]; |
| 131 | +provider = new HDWalletProvider(privateKeys, "http://localhost:8545", 0, 2); //start at address_index 0 and load both addresses |
| 132 | +``` |
| 133 | +**NOTE: This is just an example. NEVER hard code production/mainnet private |
| 134 | +keys in your code or commit them to git. They should always be loaded from |
| 135 | +environment variables or a secure secret management system.** |
| 136 | + |
| 137 | +## Truffle Usage |
| 138 | + |
| 139 | +You can easily use this within a Truffle configuration. For instance: |
| 140 | + |
| 141 | +truffle-config.js |
| 142 | +```javascript |
| 143 | +const HDWalletProvider = require("@truffle/hdwallet-provider"); |
| 144 | + |
| 145 | +const mnemonicPhrase = "mountains supernatural bird ..."; |
| 146 | + |
| 147 | +module.exports = { |
| 148 | + networks: { |
| 149 | + development: { |
| 150 | + host: "localhost", |
| 151 | + port: 8545, |
| 152 | + network_id: "*" // Match any network id |
| 153 | + }, |
| 154 | + ropsten: { |
| 155 | + // must be a thunk, otherwise truffle commands may hang in CI |
| 156 | + provider: () => |
| 157 | + new HDWalletProvider({ |
| 158 | + mnemonic: { |
| 159 | + phrase: mnemonicPhrase |
| 160 | + }, |
| 161 | + providerOrUrl: "https://ropsten.infura.io/v3/YOUR-PROJECT-ID", |
| 162 | + numberOfAddresses: 1, |
| 163 | + shareNonce: true, |
| 164 | + derivationPath: "m/44'/1'/0'/0/" |
| 165 | + }), |
| 166 | + network_id: '3', |
| 167 | + } |
| 168 | + } |
| 169 | +}; |
| 170 | +``` |
0 commit comments