aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/scripts/keyrings/hd.js16
-rw-r--r--test/unit/keyrings/hd-test.js97
2 files changed, 109 insertions, 4 deletions
diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js
index a852c9d29..61df8f28e 100644
--- a/app/scripts/keyrings/hd.js
+++ b/app/scripts/keyrings/hd.js
@@ -5,7 +5,9 @@ const ethUtil = require('ethereumjs-util')
const type = 'HD Key Tree'
const sigUtil = require('../lib/sig-util')
-module.exports = class SimpleKeyring extends EventEmitter {
+const hdPathString = `m/44'/60'/0'/0`
+
+module.exports = class HdKeyring extends EventEmitter {
static type() {
return type
@@ -28,7 +30,7 @@ module.exports = class SimpleKeyring extends EventEmitter {
const seed = bip39.mnemonicToSeed(mnemonic)
this.mnemonic = mnemonic
this.hdWallet = hdkey.fromMasterSeed(seed)
- this.seed = bip39.mnemonicToSeedHex(seed)
+ this.root = this.hdWallet.derivePath(hdPathString)
}
serialize() {
@@ -39,9 +41,15 @@ module.exports = class SimpleKeyring extends EventEmitter {
}
addAccounts(n = 1) {
+ if (!this.root) {
+ this.initFromMnemonic(bip39.generateMnemonic())
+ }
+
+ const oldLen = this.wallets.length
const newWallets = []
- for (let i = 0; i < n; i++) {
- const wallet = this.hdWallet.getWallet()
+ for (let i = oldLen; i < n + oldLen; i++) {
+ const child = this.root.deriveChild(i)
+ const wallet = child.getWallet()
newWallets.push(wallet)
this.wallets.push(wallet)
}
diff --git a/test/unit/keyrings/hd-test.js b/test/unit/keyrings/hd-test.js
new file mode 100644
index 000000000..a2284995a
--- /dev/null
+++ b/test/unit/keyrings/hd-test.js
@@ -0,0 +1,97 @@
+const assert = require('assert')
+const extend = require('xtend')
+const HdKeyring = require('../../../app/scripts/keyrings/hd')
+
+// Sample account:
+const privKeyHex = 'b8a9c05beeedb25df85f8d641538cbffedf67216048de9c678ee26260eb91952'
+
+const sampleMnemonic = 'finish oppose decorate face calm tragic certain desk hour urge dinosaur mango'
+const firstAcct = '1c96099350f13d558464ec79b9be4445aa0ef579'
+const secondAcct = '1b00aed43a693f3a957f9feb5cc08afa031e37a0'
+
+describe('simple-keyring', function() {
+
+ let keyring
+ beforeEach(function() {
+ keyring = new HdKeyring()
+ })
+
+ describe('Keyring.type()', function() {
+ it('is a class method that returns the type string.', function() {
+ const type = HdKeyring.type()
+ assert.equal(typeof type, 'string')
+ })
+ })
+
+ describe('#type', function() {
+ it('returns the correct value', function() {
+ const type = keyring.type
+ const correct = HdKeyring.type()
+ assert.equal(type, correct)
+ })
+ })
+
+ describe('#serialize empty wallets.', function() {
+ it('serializes a new mnemonic', function() {
+ const output = keyring.serialize()
+ assert.equal(output.n, 0)
+ assert.equal(output.mnemonic, null)
+ })
+ })
+
+ describe('#deserialize a private key', function() {
+ it('serializes what it deserializes', function() {
+ keyring.deserialize({
+ mnemonic: sampleMnemonic,
+ n: 1
+ })
+ assert.equal(keyring.wallets.length, 1, 'restores two accounts')
+ keyring.addAccounts(1)
+
+ const accounts = keyring.getAccounts()
+ assert.equal(accounts[0], firstAcct)
+ assert.equal(accounts[1], secondAcct)
+ assert.equal(accounts.length, 2)
+
+ const serialized = keyring.serialize()
+ assert.equal(serialized.mnemonic, sampleMnemonic)
+ })
+ })
+
+ describe('#addAccounts', function() {
+ describe('with no arguments', function() {
+ it('creates a single wallet', function() {
+ keyring.addAccounts()
+ assert.equal(keyring.wallets.length, 1)
+ })
+ })
+
+ describe('with a numeric argument', function() {
+ it('creates that number of wallets', function() {
+ keyring.addAccounts(3)
+ assert.equal(keyring.wallets.length, 3)
+ })
+ })
+ })
+
+ describe('#getAccounts', function() {
+ it('calls getAddress on each wallet', function() {
+
+ // Push a mock wallet
+ const desiredOutput = 'foo'
+ keyring.wallets.push({
+ getAddress() {
+ return {
+ toString() {
+ return desiredOutput
+ }
+ }
+ }
+ })
+
+ const output = keyring.getAccounts()
+ assert.equal(output[0], desiredOutput)
+ assert.equal(output.length, 1)
+ })
+ })
+})