From c3b9d1ed4c4f8a5465ada5131c990f992abaf22a Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 24 May 2019 17:46:32 -0400 Subject: [PATCH] support specifying account index --- accounts.example.json | 3 ++- src/actions.js | 16 +++++++++++++--- src/index.js | 4 +++- src/provider.js | 4 ++-- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/accounts.example.json b/accounts.example.json index dee00d1..3c6e12e 100644 --- a/accounts.example.json +++ b/accounts.example.json @@ -1,7 +1,8 @@ { "accounts": [ { - "mnemonic": "add mnemonic here" + "mnemonic": "add mnemonic here", + "numAddresses": 1 } ] } diff --git a/src/actions.js b/src/actions.js index 6e0716e..318dffd 100644 --- a/src/actions.js +++ b/src/actions.js @@ -47,9 +47,9 @@ class Actions { this.provider = new Provider(); this.provider.initAccounts(this.accounts); if (url.indexOf("https") >= 0) { - this.provider.startWeb3Provider("rpc", url) + this.provider.startWeb3Provider("rpc", url, (options.accountIndex || 0)) } else { - this.provider.startWeb3Provider("ws", url) + this.provider.startWeb3Provider("ws", url, (options.accountIndex || 0)) } } else { this.web3 = new Web3(); @@ -64,7 +64,17 @@ class Actions { let accounts = await this.web3.eth.getAccounts(); console.dir("== accounts"); console.dir(accounts); - this.web3.eth.defaultAccount = accounts[0] + this.web3.eth.defaultAccount = accounts[(options.accountIndex || 0)] + if (this.web3.eth.defaultAccount) { + console.dir("using account: " + this.web3.eth.defaultAccount); + } else { + console.log("==================================="); + console.log("==================================="); + console.error("no account found at index " + (options.accountIndex || 0)); + console.log("==================================="); + console.log("==================================="); + process.exit(); + } let contracts = new Contracts(this.chain, this.web3); contracts.loadContracts(); diff --git a/src/index.js b/src/index.js index e429409..7b9faf9 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,7 @@ program .option('-u, --url [url]', "host to connect to (default: ws://localhost:8556)") .option('-a, --accounts [accounts]', "accounts file, if not defined uses accounts in the connecting node") .option('-c, --chain [chain]', "environment to run, can be mainnet, ropsten, development (default: development)") + .option('-i, --index [index]', "account index to use (default: 0)") .parse(process.argv); let accounts = []; @@ -17,7 +18,8 @@ if (program.accounts) { const actions = new Actions(program.chain || "development", accounts || []); actions.connect({ - url: (program.url || "ws://localhost:8556") + url: (program.url || "ws://localhost:8556"), + accountIndex: (program.index || 0) }, async () => { cmd(actions) }); diff --git a/src/provider.js b/src/provider.js index b947645..3a2bd84 100644 --- a/src/provider.js +++ b/src/provider.js @@ -35,7 +35,7 @@ class Provider { } } - startWeb3Provider(type, web3Endpoint) { + startWeb3Provider(type, web3Endpoint, accountIndex) { const self = this; if (type === 'rpc') { @@ -69,7 +69,7 @@ class Provider { self.addresses = [...new Set(self.addresses)]; // Remove duplicates if (self.accounts.length) { - self.web3.eth.defaultAccount = self.addresses[0]; + self.web3.eth.defaultAccount = self.addresses[accountIndex || 0]; } const realSend = self.provider.send.bind(self.provider);