Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Add nonce tracking #66

Merged
merged 3 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ You can use this provider wherever a Web3 provider is needed, not just in Truffl

```javascript
var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = "opinion destroy betray ..."; // 12 word mnemonic
var mnemonic = "mountains supernatural bird..."; // 12 word mnemonic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

giphy

var provider = new HDWalletProvider(mnemonic, "http://localhost:8545");

// Or, alternatively pass in a zero-based address index.
Expand All @@ -29,9 +29,13 @@ By default, the `HDWalletProvider` will use the address of the first address tha

Parameters:

- `mnemonic`: `string`. 12 word mnemonic which addresses are created from.
- `provider_uri`: `string`. URI of Ethereum client to send all other non-transaction-related Web3 requests.
- `address_index`: `number`, optional. If specified, will tell the provider to manage the address at the index specified. Defaults to the first address (index `0`).
| Parameter | Type | Default | Description |
| ------ | ---- | ------- | ----------- |
| mnemonic | *string* | null | <required> 12 word mnemonic which addresses are created from. |
| provider_uri | *string* | null | <required> URI of Ethereum client to send all other non-transaction-related Web3 requests |
| address_index | *number* | 0 | <optional> If specified, will tell the provider to manage the address at the index specified |
| num_addresses | *number* | 1 | <optional> If specified, will create `number` addresses when instantiated |
| shareNonce | *boolean* | true | <optional> If false, a new WalletProvider will track its own nonce-state |

## Truffle Usage

Expand All @@ -41,7 +45,7 @@ truffle.js
```javascript
var HDWalletProvider = require("truffle-hdwallet-provider");

var mnemonic = "opinion destroy betray ...";
var mnemonic = "mountains supernatural bird ...";

module.exports = {
networks: {
Expand Down
21 changes: 20 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@ var bip39 = require("bip39");
var hdkey = require('ethereumjs-wallet/hdkey');
var ProviderEngine = require("web3-provider-engine");
var FiltersSubprovider = require('web3-provider-engine/subproviders/filters.js');
var NonceSubProvider = require('web3-provider-engine/subproviders/nonce-tracker.js');
var HookedSubprovider = require('web3-provider-engine/subproviders/hooked-wallet.js');
var ProviderSubprovider = require("web3-provider-engine/subproviders/provider.js");
var Web3 = require("web3");
var Transaction = require('ethereumjs-tx');

function HDWalletProvider(mnemonic, provider_url, address_index=0, num_addresses=1) {
// This line shares nonce state across multiple provider instances. Necessary
// because within truffle the wallet is repeatedly newed if it's declared in the config within a
// function, resetting nonce from tx to tx. An instance can opt out
// of this behavior by passing `shareNonce=false` to the constructor.
// See issue #65 for more
var singletonNonceSubProvider = new NonceSubProvider();

function HDWalletProvider(
mnemonic,
provider_url,
address_index=0,
num_addresses=1,
shareNonce=true
) {
this.mnemonic = mnemonic;
this.hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(mnemonic));
this.wallet_hdpath = "m/44'/60'/0'/0/";
Expand Down Expand Up @@ -41,6 +55,11 @@ function HDWalletProvider(mnemonic, provider_url, address_index=0, num_addresses
cb(null, rawTx);
}
}));

(!shareNonce)
? this.engine.addProvider(new NonceSubProvider())
: this.engine.addProvider(singletonNonceSubProvider);

this.engine.addProvider(new FiltersSubprovider());
this.engine.addProvider(new ProviderSubprovider(new Web3.providers.HttpProvider(provider_url)));
this.engine.start(); // Required by the provider engine.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "truffle-hdwallet-provider",
"version": "0.0.6",
"version": "0.0.7-beta.0",
"description": "HD Wallet-enabled Web3 provider",
"main": "index.js",
"scripts": {
Expand Down