From 37fd45e5b70a66ff976cab0d1815374063e29d10 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Fri, 25 Mar 2016 12:41:18 -0700 Subject: Convert to bip44 hdTrees Added initial test just to verify we can recover the accounts we generate in this way. Still need to add compliance test to make sure this interoperates with testrpc's new mnemonic flag. --- app/scripts/lib/idStore.js | 30 ++++++++++++------------ package.json | 7 ++++-- test/index.js | 57 +++++++++++++++++++++++++++++++++++++++------- 3 files changed, 70 insertions(+), 24 deletions(-) diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index c4547b07f..1a3e7e06f 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -1,6 +1,7 @@ const EventEmitter = require('events').EventEmitter const inherits = require('util').inherits const Transaction = require('ethereumjs-tx') +const Lightwallet = require('eth-lightwallet') const LightwalletKeyStore = require('eth-lightwallet').keystore const LightwalletSigner = require('eth-lightwallet').signing const async = require('async') @@ -14,23 +15,24 @@ module.exports = IdentityStore inherits(IdentityStore, EventEmitter) function IdentityStore(ethStore) { - const self = this - EventEmitter.call(self) + EventEmitter.call(this) // we just use the ethStore to auto-add accounts - self._ethStore = ethStore + this._ethStore = ethStore // lightwallet key store - self._keyStore = null + this._keyStore = null // lightwallet wrapper - self._idmgmt = null + this._idmgmt = null + + this.hdPathString = "m/44'/60'/0'/0" - self._currentState = { + this._currentState = { selectedAddress: null, identities: {}, unconfTxs: {}, } // not part of serilized metamask state - only kept in memory - self._unconfTxCbs = {} + this._unconfTxCbs = {} } // @@ -122,7 +124,6 @@ IdentityStore.prototype.addUnconfirmedTransaction = function(txParams, cb){ status: 'unconfirmed', } self._currentState.unconfTxs[txId] = txData - console.log('addUnconfirmedTransaction:', txData) // keep the cb around for after approval (requires user interaction) self._unconfTxCbs[txId] = cb @@ -201,7 +202,7 @@ IdentityStore.prototype._loadIdentities = function(){ const self = this if (!self._isUnlocked()) throw new Error('not unlocked') // get addresses and normalize address hexString - var addresses = self._keyStore.getAddresses().map(function(address){ return '0x'+address }) + var addresses = self._keyStore.getAddresses(this.hdPathString).map(function(address){ return '0x'+address }) addresses.forEach(function(address){ // // add to ethStore self._ethStore.addAccount(address) @@ -257,7 +258,7 @@ IdentityStore.prototype._createIdmgmt = function(password, seed, entropy, cb){ IdentityStore.prototype._restoreFromSeed = function(keyStore, seed, derivedKey) { keyStore = new LightwalletKeyStore(seed, derivedKey) - keyStore.generateNewAddress(derivedKey, 3) + keyStore.generateNewAddress(derivedKey, 3, hdPathString) window.localStorage['lightwallet'] = keyStore.serialize() console.log('restored from seed. saved to keystore localStorage') } @@ -268,19 +269,20 @@ IdentityStore.prototype._loadFromLocalStorage = function(serializedKeystore, der IdentityStore.prototype._createFirstWallet = function(entropy, derivedKey) { var secretSeed = LightwalletKeyStore.generateRandomSeed(entropy) - var keyStore = new LightwalletKeyStore(secretSeed, derivedKey) - keyStore.generateNewAddress(derivedKey, 3) + var keyStore = new LightwalletKeyStore(secretSeed, derivedKey, this.hdPathString) + keyStore.generateNewAddress(derivedKey, 3, this.hdPathString) window.localStorage['lightwallet'] = keyStore.serialize() console.log('saved to keystore localStorage') return keyStore } -function IdManagement( opts = { keyStore: null, derivedKey: null } ) { +function IdManagement( opts = { keyStore: null, derivedKey: null, hdPathString: null } ) { this.keyStore = opts.keyStore this.derivedKey = opts.derivedKey + this.hdPathString = opts.hdPathString this.getAddresses = function(){ - return keyStore.getAddresses().map(function(address){ return '0x'+address }) + return keyStore.getAddresses(this.hdPathString).map(function(address){ return '0x'+address }) } this.signTx = function(txParams){ diff --git a/package.json b/package.json index fec837640..da7686c0a 100644 --- a/package.json +++ b/package.json @@ -5,11 +5,11 @@ "private": true, "scripts": { "start": "gulp dev", - "test": "mocha --compilers js:babel-register" + "test": "mocha --compilers js:babel-register", + "watch": "mocha watch --compilers js:babel-register" }, "dependencies": { "async": "^1.5.2", - "bip39": "^2.2.0", "clone": "^1.0.2", "dnode": "^1.2.2", "end-of-stream": "^1.1.0", @@ -39,8 +39,11 @@ "gulp-sourcemaps": "^1.6.0", "gulp-util": "^3.0.7", "gulp-watch": "^4.3.5", + "jsdom": "^8.1.0", "jshint-stylish": "~0.1.5", "lodash.assign": "^4.0.6", + "mocha": "^2.4.5", + "mocha-jsdom": "^1.1.0", "tape": "^4.5.1", "vinyl-buffer": "^1.0.0", "vinyl-source-stream": "^1.1.0", diff --git a/test/index.js b/test/index.js index 1ff7b673a..5a8df78b8 100644 --- a/test/index.js +++ b/test/index.js @@ -1,11 +1,52 @@ -var assert = require('assert'); -var idStore = require('../app/scripts/lib/idStore') +var assert = require('assert') +var IdentityStore = require('../app/scripts/lib/idStore') +var jsdom = require('mocha-jsdom') +jsdom() describe('IdentityStore', function() { - describe('#_createFirstWallet', function () { - it('should return the expected keystore', function () { - assert.equal(1,1) - }); - }); -}); + describe('#createNewVault', function () { + let idStore + let password = 'password123' + let entropy = 'entripppppyy duuude' + let seedWords + let accounts = [] + let originalKeystore + + before(function(done) { + window.localStorage = {} // Hacking localStorage support into JSDom + + idStore = new IdentityStore({ + addAccount(acct) { accounts.push(acct) }, + }) + + idStore.createNewVault(password, entropy, (err, seeds) => { + seedWords = seeds + originalKeystore = idStore._idmgmt.keyStore + done() + }) + }) + + describe('#recoverFromSeed', function() { + + before(function() { + window.localStorage = {} // Hacking localStorage support into JSDom + accounts = [] + + idStore = new IdentityStore({ + addAccount(acct) { accounts.push(acct) }, + }) + }) + + it('should return the expected keystore', function () { + + idStore.recoverFromSeed(password, seedWords, (err) => { + assert.ifError(err) + + let newKeystore = idStore._idmgmt.keyStore + assert.equal(newKeystore, originalKeystore) + }) + }) + }) + }) +}) -- cgit v1.2.3