Merge pull request #413 from embark-framework/features/wallet

HD Wallet when providing key or mnemonic
This commit is contained in:
Iuri Matias 2018-05-15 13:07:30 -04:00 committed by GitHub
commit 69e264e765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1475 additions and 155 deletions

View File

@ -9,6 +9,7 @@ var Blockchain = function(options) {
this.blockchainConfig = options.blockchainConfig;
this.env = options.env || 'development';
this.client = options.client;
this.isDev = options.isDev;
if ((this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') && this.env !== 'development') {
console.log("===> " + __("warning: running default config on a non-development environment"));
@ -50,21 +51,13 @@ var Blockchain = function(options) {
this.config.datadir = fs.embarkPath(".embark/development/datadir");
}
if (this.blockchainConfig.isDev) {
this.isDev = true;
} else if (this.blockchainConfig.isDev === false) {
this.isDev = false;
} else {
this.isDev = this.env === 'development';
}
this.client = new options.client({config: this.config, env: this.env, isDev: this.isDev});
};
Blockchain.prototype.runCommand = function(cmd, options) {
console.log(__("running: %s", cmd.underline).green);
return shelljs.exec(cmd, options, (err, stdout, _stderr) => {
if (err && this.env === 'development' && stdout.indexOf('Failed to unlock developer account') > 0) {
if (err && this.env === 'development' && stdout.indexOf('Failed to unlock') > 0) {
console.warn('\n' + __('Development blockchain has changed to use the --dev option.').yellow);
console.warn(__('You can reset your workspace to fix the problem with').yellow + ' embark reset'.cyan);
console.warn(__('Otherwise, you can change your data directory in blockchain.json (datadir)').yellow);
@ -131,9 +124,9 @@ Blockchain.prototype.initChainAndGetAddress = function() {
return address;
};
var BlockchainClient = function(blockchainConfig, client, env) {
var BlockchainClient = function(blockchainConfig, client, env, isDev) {
if (client === 'geth') {
return new Blockchain({blockchainConfig: blockchainConfig, client: GethCommands, env: env});
return new Blockchain({blockchainConfig: blockchainConfig, client: GethCommands, env: env, isDev});
} else {
throw new Error('unknown client');
}

121
lib/contracts/provider.js Normal file
View File

@ -0,0 +1,121 @@
const ProviderEngine = require('web3-provider-engine');
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js');
const bip39 = require("bip39");
const hdkey = require('ethereumjs-wallet/hdkey');
const fs = require('../core/fs');
class Provider {
constructor(options) {
const self = this;
this.web3 = options.web3;
this.accountsConfig = options.accountsConfig;
this.logger = options.logger;
this.isDev = options.isDev;
this.engine = new ProviderEngine();
this.asyncMethods = {};
this.engine.addProvider(new RpcSubprovider({
rpcUrl: options.web3Endpoint
}));
if (this.accountsConfig && this.accountsConfig.length) {
this.accounts = [];
this.addresses = [];
this.accountsConfig.forEach(accountConfig => {
const account = this.getAccount(accountConfig);
if (!account) {
return;
}
if (Array.isArray(account)) {
this.accounts = this.accounts.concat(account);
account.forEach(acc => {
this.addresses.push(acc.address);
});
return;
}
this.accounts.push(account);
this.addresses.push(account.address);
});
if (this.accounts.length) {
this.accounts.forEach(account => {
this.web3.eth.accounts.wallet.add(account);
});
this.asyncMethods = {
eth_accounts: self.eth_accounts.bind(this)
};
if (this.isDev) {
this.logger.warn('You are using your own account in the develop environment. It might not be funded.');
}
}
}
// network connectivity error
this.engine.on('error', (err) => {
// report connectivity errors
this.logger.error(err.stack);
});
this.engine.start();
}
getAccount(accountConfig) {
if (accountConfig.privateKey) {
if (!accountConfig.privateKey.startsWith('0x')) {
accountConfig.privateKey = '0x' + accountConfig.privateKey;
}
return this.web3.eth.accounts.privateKeyToAccount(accountConfig.privateKey);
}
if (accountConfig.privateKeyFile) {
let fileContent = fs.readFileSync(fs.dappPath(accountConfig.privateKeyFile)).toString();
fileContent = fileContent.trim().split(/[,;]/);
return fileContent.map(key => {
if (!key.startsWith('0x')) {
key = '0x' + key;
}
return this.web3.eth.accounts.privateKeyToAccount(key);
});
}
if (accountConfig.mnemonic) {
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(accountConfig.mnemonic.trim()));
const addressIndex = accountConfig.addressIndex || 0;
const numAddresses = accountConfig.numAddresses || 1;
const wallet_hdpath = accountConfig.hdpath || "m/44'/60'/0'/0/";
const accounts = [];
for (let i = addressIndex; i < addressIndex + numAddresses; i++) {
const wallet = hdwallet.derivePath(wallet_hdpath + i).getWallet();
accounts.push(this.web3.eth.accounts.privateKeyToAccount('0x' + wallet.getPrivateKey().toString('hex')));
}
return accounts;
}
this.logger.warn('Unsupported account configuration: ' + JSON.stringify(accountConfig));
this.logger.warn('Try using one of those: ' +
'{ "privateKey": "your-private-key", "privateKeyFile": "path/to/file/containing/key", "mnemonic": "12 word mnemonic" }');
return null;
}
eth_accounts(payload, cb) {
return cb(null, this.addresses);
}
sendAsync(payload, callback) {
let method = this.asyncMethods[payload.method];
if (method) {
return method.call(method, payload, (err, result) => {
if (err) {
return callback(err);
}
let response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result};
callback(null, response);
});
}
this.engine.sendAsync.apply(this.engine, arguments);
}
send() {
return this.engine.send.apply(this.engine, arguments);
}
}
module.exports = Provider;

View File

@ -10,10 +10,12 @@ const Watch = require('../pipeline/watch.js');
const LibraryManager = require('../versions/library_manager.js');
const Pipeline = require('../pipeline/pipeline.js');
const async = require('async');
const Provider = require('../contracts/provider');
class Engine {
constructor(options) {
this.env = options.env;
this.isDev = options.isDev;
this.embarkConfig = options.embarkConfig;
this.interceptLogs = options.interceptLogs;
this.version = options.version;
@ -271,8 +273,15 @@ class Engine {
if (this.web3 === undefined) {
this.web3 = new Web3();
if (this.config.contractsConfig.deployment.type === "rpc") {
let web3Endpoint = 'http://' + this.config.contractsConfig.deployment.host + ':' + this.config.contractsConfig.deployment.port;
this.web3.setProvider(new this.web3.providers.HttpProvider(web3Endpoint));
const web3Endpoint = 'http://' + this.config.contractsConfig.deployment.host + ':' + this.config.contractsConfig.deployment.port;
const providerOptions = {
web3: this.web3,
accountsConfig: this.config.contractsConfig.deployment.accounts,
logger: this.logger,
isDev: this.isDev,
web3Endpoint
};
this.web3.setProvider(new Provider(providerOptions));
} else {
throw new Error("contracts config error: unknown deployment type " + this.config.contractsConfig.deployment.type);
}

View File

@ -75,5 +75,24 @@
"writing file": "writing file",
"errors found while generating": "errors found while generating",
"Looking for documentation? You can find it at": "Looking for documentation? You can find it at",
"Ready": "Ready"
"Ready": "Ready",
"Graph will not include undeployed contracts": "Graph will not include undeployed contracts",
"Graph will not include functions": "Graph will not include functions",
"Graph will not include events": "Graph will not include events",
"Embark Blockchain Using: %s": "Embark Blockchain Using: %s",
"running: %s": "running: %s",
"Initializing Embark Template....": "Initializing Embark Template....",
"Init complete": "Init complete",
"App ready at ": "App ready at ",
"already initialized": "already initialized",
"deployed at": "deployed at",
"executing onDeploy commands": "executing onDeploy commands",
"executing: ": "executing: ",
"no config file found at %s using default config": "no config file found at %s using default config",
"Development blockchain has changed to use the --dev option.": "Development blockchain has changed to use the --dev option.",
"You can reset your workspace to fix the problem with": "You can reset your workspace to fix the problem with",
"Otherwise, you can change your data directory in blockchain.json (datadir)": "Otherwise, you can change your data directory in blockchain.json (datadir)",
"tip: you can resize the terminal or disable the dashboard with": "tip: you can resize the terminal or disable the dashboard with",
"help": "help",
"quit": "quit"
}

View File

@ -38,6 +38,15 @@ class Embark {
this.plugins = this.config.plugins;
}
isDev(env) {
if (this.config && this.config.blockchainConfig && this.config.blockchainConfig.isDev) {
return true;
} else if (this.config && this.config.blockchainConfig && this.config.blockchainConfig.isDev === false) {
return false;
}
return (env === 'development');
}
blockchain(env, client) {
this.context = [constants.contexts.blockchain];
let blockchainConfig = this.config.blockchainConfig;
@ -52,7 +61,7 @@ class Embark {
if(webServerConfig) blockchainConfig.wsOrigins = `http://${webServerConfig.host}:${webServerConfig.port}`;
if(storageConfig) blockchainConfig.wsOrigins += `${blockchainConfig.wsOrigins.length ? ',' : ''}${storageConfig.protocol}://${storageConfig.host}:${storageConfig.port}`;
}
return require('./cmds/blockchain/blockchain.js')(blockchainConfig, client, env).run();
return require('./cmds/blockchain/blockchain.js')(blockchainConfig, client, env, this.isDev(env)).run();
}
simulator(options) {
@ -77,6 +86,7 @@ class Embark {
let engine = new Engine({
env: options.env,
isDev: this.isDev(options.env),
version: this.version,
embarkConfig: options.embarkConfig || 'embark.json',
logFile: options.logFile,
@ -169,6 +179,7 @@ class Embark {
let engine = new Engine({
env: options.env,
isDev: this.isDev(options.env),
version: this.version,
embarkConfig: 'embark.json',
interceptLogs: false,
@ -228,6 +239,7 @@ class Embark {
let engine = new Engine({
env: options.env,
isDev: this.isDev(options.env),
version: this.version,
embarkConfig: options.embarkConfig || 'embark.json',
logFile: options.logFile,
@ -282,6 +294,7 @@ class Embark {
let engine = new Engine({
env: options.env,
isDev: this.isDev(options.env),
version: this.version,
embarkConfig: 'embark.json',
interceptLogs: false,

1360
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -30,6 +30,7 @@
"babel-preset-es2016": "^6.24.1",
"babel-preset-es2017": "6.24.1",
"babel-preset-react": "^6.24.1",
"bip39": "^2.5.0",
"blessed": "^0.1.81",
"chokidar": "^2.0.3",
"colors": "^1.1.2",
@ -39,6 +40,7 @@
"ejs": "^2.5.8",
"eth-lib": "^0.2.8",
"ethereumjs-testrpc": "^6.0.3",
"ethereumjs-wallet": "^0.6.0",
"file-loader": "^1.1.5",
"finalhandler": "^1.1.1",
"follow-redirects": "^1.2.4",
@ -67,6 +69,7 @@
"url-loader": "^0.6.2",
"viz.js": "^1.8.1",
"web3": "1.0.0-beta.34",
"web3-provider-engine": "^14.0.5",
"webpack": "^3.10.0",
"window-size": "^1.1.0"
},

1
test/keyFiles/twoKeys Normal file
View File

@ -0,0 +1 @@
key1;key2

81
test/provider.js Normal file
View File

@ -0,0 +1,81 @@
/*global describe, it, before*/
const assert = require('assert');
const sinon = require('sinon');
const Provider = require('../lib/contracts/provider');
let TestLogger = require('../lib/tests/test_logger.js');
describe('embark.provider', function () {
describe('#getAccount', function () {
let provider;
before(() => {
const web3 = {
eth: {
accounts: {
privateKeyToAccount: sinon.stub().callsFake((key) => {
return {key};
})
}
}
};
provider = new Provider({
accountsConfig: [],
logger: new TestLogger({}),
web3Endpoint: 'http:localhost:555',
web3
});
});
it('should return one account with the key', function () {
const account = provider.getAccount({
privateKey: 'myKey'
});
assert.deepEqual(account, {key: '0xmyKey'});
});
it('should return two accounts from the keys in the file', function () {
const account = provider.getAccount({
privateKeyFile: 'test/keyFiles/twoKeys'
});
assert.deepEqual(account, [
{key: '0xkey1'},
{key: '0xkey2'}
]);
});
it('should return one account from the mnemonic', function () {
const account = provider.getAccount({
mnemonic: 'example exile argue silk regular smile grass bomb merge arm assist farm'
});
assert.deepEqual(account,
[{key: "0xf942d5d524ec07158df4354402bfba8d928c99d0ab34d0799a6158d56156d986"}]);
});
it('should return two accounts from the mnemonic using numAddresses', function () {
const account = provider.getAccount({
mnemonic: 'example exile argue silk regular smile grass bomb merge arm assist farm',
numAddresses: 2
});
assert.deepEqual(account,
[
{key: "0xf942d5d524ec07158df4354402bfba8d928c99d0ab34d0799a6158d56156d986"},
{key: "0x88f37cfbaed8c0c515c62a17a3a1ce2f397d08bbf20dcc788b69f11b5a5c9791"}
]);
});
it('should return nothing with bad config', function () {
const account = provider.getAccount({
badConfig: 'not working'
});
assert.strictEqual(account, null);
});
});
});