Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Use HD Wallet for account generation #44

Merged
merged 2 commits into from
Mar 25, 2016
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
44 changes: 42 additions & 2 deletions bin/testrpc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node
var argv = require('yargs').argv;
var TestRPC = require('..');
var pkg = require("../package.json");

function parseAccounts(accounts) {
function splitAccount(account) {
Expand All @@ -24,10 +25,49 @@ if (argv.d || argv.deterministic) {
argv.s = "TestRPC is awesome!";
}

TestRPC.startServer(console, {
port: argv.p || argv.port,
var options = {
port: argv.p || argv.port || 8545,
debug: argv.d || argv.debug,
seed: argv.s || argv.seed,
mnemonic: argv.m || argv.mnemonic,
total_accounts: argv.a || argv.accounts,
accounts: parseAccounts(argv.account)
}

var server = TestRPC.server(console, options);

console.log("EthereumJS TestRPC v" + pkg.version);

server.listen(options.port, function(err, blockchain) {
if (err) {
console.log(err);
return;
}
console.log("");
console.log("Available Accounts");
console.log("==================");

var accounts = blockchain.accounts;
var addresses = Object.keys(accounts);

addresses.forEach(function(address) {
console.log(address);
});

console.log("");
console.log("Private Keys");
console.log("==================");

addresses.forEach(function(address) {
console.log(accounts[address].secretKey.toString("hex"));
});

console.log("");
console.log("HD Wallet");
console.log("==================");
console.log("Mnemonic: " + blockchain.mnemonic);
console.log("Base HD Path: " + blockchain.wallet_hdpath + "{account_index}")

console.log("");
console.log("Listening on localhost:" + options.port);
});
16 changes: 15 additions & 1 deletion lib/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var FakeTransaction = require('ethereumjs-tx/fake.js');
var utils = require('ethereumjs-util');
var seedrandom = require('seedrandom');
var random = require('./random');
var bip39 = require('bip39')
var hdkey = require('ethereumjs-wallet/hdkey')

Blockchain = function(logger, options) {
this.stateTrie = new Trie();
Expand All @@ -25,6 +27,9 @@ Blockchain = function(logger, options) {
this.snapshots = [];
this.logger = logger || console;
this.rng = seedrandom(options.seed);
this.mnemonic = options.mnemonic || bip39.entropyToMnemonic(random.randomBytes(16, this.rng));
this.wallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(this.mnemonic));
this.wallet_hdpath = "m/44'/60'/0'/0/";

if (options.debug == true) {
this.vm.on('step', function(info){
Expand Down Expand Up @@ -56,7 +61,16 @@ Blockchain.prototype.toHex = function(val) {
Blockchain.prototype.addAccount = function(opts, callback) {
var self = this;

var secretKey = opts.secretKey || random.randomBytes(32, this.rng);
var secretKey;

if (opts.secretKey) {
this.secretKey = opts.secretKey;
} else {
var index = Object.keys(this.accounts).length;
var account = this.wallet.derivePath(this.wallet_hdpath + index) // index is a number
secretKey = account.getWallet().getPrivateKey() // Buffer
}

var publicKey = utils.privateToPublic(new Buffer(secretKey));
var address = utils.pubToAddress(new Buffer(publicKey));

Expand Down
4 changes: 2 additions & 2 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Manager.prototype.initialize = function(callback) {
});
} else {
// Add 10 accounts, for testing purposes.
async.times(this.total_accounts, function(n, next) {
async.timesSeries(this.total_accounts, function(n, next) {
self.blockchain.addAccount({}, next);
}, function() {
self.initialized = true;
Expand All @@ -43,7 +43,7 @@ Manager.prototype.waitForInitialization = function(callback) {
self.waitForInitialization(callback);
}, 100);
} else {
callback(null, this.blockchain.accounts);
callback(null, this.blockchain);
}
}

Expand Down
41 changes: 0 additions & 41 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var Manager = require('./manager.js');
var pkg = require("../package.json");
var http = require("http");

var ProviderEngine = require("web3-provider-engine");
Expand Down Expand Up @@ -190,46 +189,6 @@ Server = {
throw new Error("Synchronous requests are not supported.");
}
};
},

startServer: function(logger, options, callback) {
var self = this;
var port = options.port;

if (port == null) {
port = 8545;
}

if (logger == null) {
logger = console;
}

var server = this.server(logger, options);

logger.log("EthereumJS TestRPC v" + pkg.version);

server.listen(port, function(err, accounts) {
if (err) {
logger.log(err);
return;
}
logger.log("");
logger.log("Available Accounts");
logger.log("==================");

accounts = Object.keys(accounts);

for (var i = 0; i < accounts.length; i++) {
logger.log(accounts[i]);
}

logger.log("");
logger.log("Listening on localhost:" + port);

if (callback) {
callback();
}
});
}
}

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
"dependencies": {
"async": "^1.5.0",
"bignumber.js": "^2.1.4",
"bip39": "^2.2.0",
"ethereumjs-account": "^2.0.2",
"ethereumjs-block": "^1.2.2",
"ethereumjs-tx": "^1.1.0",
"ethereumjs-util": "^4.0.1",
"ethereumjs-vm": "^1.2.1",
"ethereumjs-wallet": "^0.5.0",
"merkle-patricia-tree": "2.1.2",
"seedrandom": "^2.4.2",
"shelljs": "^0.6.0",
Expand Down
2 changes: 1 addition & 1 deletion test/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ var tests = function(web3) {

web3.eth.sendTransaction(tx_data, function(err, result) {
if (err) {
assert.equal(err.message.indexOf("Error: could not unlock signer account"), 0);
assert.notEqual(err.message.indexOf("could not unlock signer account"), -1);
done();
} else {
assert.fail("Should have received an error")
Expand Down