Skip to content

Commit

Permalink
Add Ethereum Support for HD Keys
Browse files Browse the repository at this point in the history
This PR adds support for each Caliper client to have its own Ethereum
account to be deterministically derived from a seed value using BIP-44
style key derivation. This is only supported for the `fromAddress`
value.

To use this instead of specifying a `fromAddresss` and optional private
key instead a `fromAddressSeed` is specified, which is a 128 bit hex
encoded value used as the seed for key derivation.  Each client uses its
client index as the account field.  In the future where multiple
accounts are needed per caliper client the address_index field will be
incremented or mapped to the account value.

The genesis file in the samples and integration tests for besu have had
the first 10 keys pre-seeded with test ether.  The sample and
integration test for besu has also been updated to use a from address
seed.

Signed-off-by: Danno Ferrin <[email protected]>
  • Loading branch information
shemnon committed Nov 15, 2019
1 parent 86af1dd commit 8593915
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 12 deletions.
15 changes: 12 additions & 3 deletions packages/caliper-ethereum/lib/ethereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

'use strict';

const EthereumHDKey = require('ethereumjs-wallet/hdkey');
const Web3 = require('web3');
const {BlockchainInterface, CaliperUtils, TxStatus} = require('@hyperledger/caliper-core');
const logger = CaliperUtils.getLogger('ethereum.js');
Expand Down Expand Up @@ -94,9 +95,8 @@ class Ethereum extends BlockchainInterface {
let context = {
clientIdx: clientIdx,
contracts: {},
fromAddress: this.ethereumConfig.fromAddress,
nonces: {},
web3: this.web3,
web3: this.web3
};
for (const key of Object.keys(args.contracts)) {
context.contracts[key] = {
Expand All @@ -105,7 +105,16 @@ class Ethereum extends BlockchainInterface {
estimateGas: args.contracts[key].estimateGas
};
}
if (this.ethereumConfig.fromAddressPrivateKey) {
if (this.ethereumConfig.fromAddress) {
context.fromAddress = this.ethereumConfig.fromAddress;
}
if (this.ethereumConfig.fromAddressSeed) {
let hdwallet = EthereumHDKey.fromMasterSeed(this.ethereumConfig.fromAddressSeed);
let wallet = hdwallet.derivePath('m/44\'/60\'/' + clientIdx + '\'/0/0').getWallet();
context.fromAddress = wallet.getChecksumAddressString();
context.nonces[context.fromAddress] = await this.web3.eth.getTransactionCount(context.fromAddress);
this.web3.eth.accounts.wallet.add(wallet.getPrivateKeyString());
} else if (this.ethereumConfig.fromAddressPrivateKey) {
context.nonces[this.ethereumConfig.fromAddress] = await this.web3.eth.getTransactionCount(this.ethereumConfig.fromAddress);
this.web3.eth.accounts.wallet.add(this.ethereumConfig.fromAddressPrivateKey);
} else if (this.ethereumConfig.fromAddressPassword) {
Expand Down
3 changes: 2 additions & 1 deletion packages/caliper-ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"npm": ">=5.6.0"
},
"dependencies": {
"@hyperledger/caliper-core": "0.2.0"
"@hyperledger/caliper-core": "0.2.0",
"ethereumjs-wallet": "^0.6.3"
},
"devDependencies": {
"web3": "1.2.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,56 @@
"alloc": {
"0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000"
},
"0xD1cf9D73a91DE6630c2bb068Ba5fDdF9F0DEac09": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x797c13f7235c627f6bd013dc17fff4c12213ab49abcf091f77c83f16db10e90b",
"derivation": "m/44'/60'/0'/0/0"
},
"0xd826E703658FA2CE8D1018D42E637C08d9846A17": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x4be84b298ccb944e96befc4f03b943ec13709f6a8e5b5d8595c486dfa88fd254",
"derivation": "m/44'/60'/1'/0/0"
},
"0x247DdfA00415710E0416f8c103b5E6428782C916": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0xaa682edca26f3e17889d69276e6c6a4fcf1594af97e48f4dc3ac6dbb29e7924b",
"derivation": "m/44'/60'/2'/0/0"
},
"0x500a8704F86C0d9126Dfb949fDC4dc248c228fB4": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x45d09c64e4973b16422b31d5e4a1fbf57ae8fd25037566af8bc1d046022d754b",
"derivation": "m/44'/60'/3'/0/0"
},
"0x32d9ad8F398995fFF658E74430749d65E0ee6702": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x2f51f2d881e0a6f7f249139a2afe8da084424616d08aa0ce9fb1c960b49f022f",
"derivation": "m/44'/60'/4'/0/0"
},
"0xd32d3f6dA1664C4ee7acae64fEC3661cEa6ee448": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x87abeb4c0230ca6fdf42f7d2cb2baa510856e42f039ab7670b13845e45dc3245",
"derivation": "m/44'/60'/5'/0/0"
},
"0x5F6C5F2431CE84f2Fa84deB6B27A1764DB6feB1a": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x5f0ad910e681553c9407582f620d9560541fa8b8c4d03e9e2e3caed5eea6a220",
"derivation": "m/44'/60'/6'/0/0"
},
"0x9fCDe74bf5465D5C32a09f158047803a061B3C1B": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x76930906e95c96a9be735b0f182184424f450feca5ea71bf743a8c47ea6e2635",
"derivation": "m/44'/60'/7'/0/0"
},
"0xb582703c0a0e2b598f9362A5e5c17Ab6865b0342": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x32e90654e141cb4f92dd9fccef100778c112636b20b9017a9ae56be9a8c2c648",
"derivation": "m/44'/60'/8'/0/0"
},
"0x179740E29B1E7b2C61F6004b8d5caD575D6eaa88": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x028bf0ee2807cf9a8d22db99f12aea783a2e6b4897c9ddb7557e33291c7d35c5",
"derivation": "m/44'/60'/9'/0/0"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
},
"ethereum": {
"url": "http://localhost:8545",
"contractDeployerAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2",
"contractDeployerAddressPrivateKey": "0xa67bee5d6fb43acd42e307eb67547ab5006ad2fbb9567829e9b4b2ef3580acea",
"fromAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2",
"fromAddressPrivateKey": "0xa67bee5d6fb43acd42e307eb67547ab5006ad2fbb9567829e9b4b2ef3580acea",
"contractDeployerAddress": "0xD1cf9D73a91DE6630c2bb068Ba5fDdF9F0DEac09",
"contractDeployerAddressPrivateKey": "0x797c13f7235c627f6bd013dc17fff4c12213ab49abcf091f77c83f16db10e90b",
"fromAddressSeed": "0x3f841bf589fdf83a521e55d51afddc34fa65351161eead24f064855fc29c9580",
"transactionConfirmationBlocks": 2,
"contracts": {
"simple": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,56 @@
"alloc": {
"0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000"
},
"0xD1cf9D73a91DE6630c2bb068Ba5fDdF9F0DEac09": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x797c13f7235c627f6bd013dc17fff4c12213ab49abcf091f77c83f16db10e90b",
"derivation": "m/44'/60'/0'/0/0"
},
"0xd826E703658FA2CE8D1018D42E637C08d9846A17": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x4be84b298ccb944e96befc4f03b943ec13709f6a8e5b5d8595c486dfa88fd254",
"derivation": "m/44'/60'/1'/0/0"
},
"0x247DdfA00415710E0416f8c103b5E6428782C916": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0xaa682edca26f3e17889d69276e6c6a4fcf1594af97e48f4dc3ac6dbb29e7924b",
"derivation": "m/44'/60'/2'/0/0"
},
"0x500a8704F86C0d9126Dfb949fDC4dc248c228fB4": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x45d09c64e4973b16422b31d5e4a1fbf57ae8fd25037566af8bc1d046022d754b",
"derivation": "m/44'/60'/3'/0/0"
},
"0x32d9ad8F398995fFF658E74430749d65E0ee6702": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x2f51f2d881e0a6f7f249139a2afe8da084424616d08aa0ce9fb1c960b49f022f",
"derivation": "m/44'/60'/4'/0/0"
},
"0xd32d3f6dA1664C4ee7acae64fEC3661cEa6ee448": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x87abeb4c0230ca6fdf42f7d2cb2baa510856e42f039ab7670b13845e45dc3245",
"derivation": "m/44'/60'/5'/0/0"
},
"0x5F6C5F2431CE84f2Fa84deB6B27A1764DB6feB1a": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x5f0ad910e681553c9407582f620d9560541fa8b8c4d03e9e2e3caed5eea6a220",
"derivation": "m/44'/60'/6'/0/0"
},
"0x9fCDe74bf5465D5C32a09f158047803a061B3C1B": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x76930906e95c96a9be735b0f182184424f450feca5ea71bf743a8c47ea6e2635",
"derivation": "m/44'/60'/7'/0/0"
},
"0xb582703c0a0e2b598f9362A5e5c17Ab6865b0342": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x32e90654e141cb4f92dd9fccef100778c112636b20b9017a9ae56be9a8c2c648",
"derivation": "m/44'/60'/8'/0/0"
},
"0x179740E29B1E7b2C61F6004b8d5caD575D6eaa88": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000",
"privatekey": "0x028bf0ee2807cf9a8d22db99f12aea783a2e6b4897c9ddb7557e33291c7d35c5",
"derivation": "m/44'/60'/9'/0/0"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
},
"ethereum": {
"url": "http://localhost:8545",
"contractDeployerAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2",
"contractDeployerAddressPrivateKey": "0xa67bee5d6fb43acd42e307eb67547ab5006ad2fbb9567829e9b4b2ef3580acea",
"fromAddress": "0xc0A8e4D217eB85b812aeb1226fAb6F588943C2C2",
"fromAddressPrivateKey": "0xa67bee5d6fb43acd42e307eb67547ab5006ad2fbb9567829e9b4b2ef3580acea",
"contractDeployerAddress": "0xD1cf9D73a91DE6630c2bb068Ba5fDdF9F0DEac09",
"contractDeployerAddressPrivateKey": "0x797c13f7235c627f6bd013dc17fff4c12213ab49abcf091f77c83f16db10e90b",
"fromAddressSeed": "0x3f841bf589fdf83a521e55d51afddc34fa65351161eead24f064855fc29c9580",
"transactionConfirmationBlocks": 2,
"contracts": {
"simple": {
Expand Down

0 comments on commit 8593915

Please sign in to comment.