From fa2077d9f88f7607175e6c08d042defb3486013b Mon Sep 17 00:00:00 2001 From: cgewecke Date: Wed, 29 Aug 2018 15:42:25 -0700 Subject: [PATCH 1/3] Add nonce subprovider --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index a91a661..c7a6f3d 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ 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"); @@ -41,6 +42,8 @@ function HDWalletProvider(mnemonic, provider_url, address_index=0, num_addresses cb(null, rawTx); } })); + + this.engine.addProvider(new NonceSubProvider()); this.engine.addProvider(new FiltersSubprovider()); this.engine.addProvider(new ProviderSubprovider(new Web3.providers.HttpProvider(provider_url))); this.engine.start(); // Required by the provider engine. From de40c0ac855cc5f9a49ee2c8871f61d50a45d43d Mon Sep 17 00:00:00 2001 From: cgewecke Date: Wed, 29 Aug 2018 15:43:36 -0700 Subject: [PATCH 2/3] Update version to 0.0.7-beta.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7c0a011..974eec8 100644 --- a/package.json +++ b/package.json @@ -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": { From 6293685cdc820747d739b20768eea99d913e64d3 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Wed, 5 Sep 2018 11:30:48 -0700 Subject: [PATCH 3/3] Default to singleton nonce-provider --- README.md | 14 +++++++++----- index.js | 20 ++++++++++++++++++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d99b0e9..768d377 100644 --- a/README.md +++ b/README.md @@ -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 var provider = new HDWalletProvider(mnemonic, "http://localhost:8545"); // Or, alternatively pass in a zero-based address index. @@ -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 | 12 word mnemonic which addresses are created from. | +| provider_uri | *string* | null | URI of Ethereum client to send all other non-transaction-related Web3 requests | +| address_index | *number* | 0 | If specified, will tell the provider to manage the address at the index specified | +| num_addresses | *number* | 1 | If specified, will create `number` addresses when instantiated | +| shareNonce | *boolean* | true | If false, a new WalletProvider will track its own nonce-state | ## Truffle Usage @@ -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: { diff --git a/index.js b/index.js index c7a6f3d..007d356 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,20 @@ 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/"; @@ -43,7 +56,10 @@ function HDWalletProvider(mnemonic, provider_url, address_index=0, num_addresses } })); - this.engine.addProvider(new NonceSubProvider()); + (!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.