From 363c2a0939aba5fa73e08ff8e6d65581031242d5 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 12 Sep 2016 11:07:25 -0700 Subject: Fix account unlocking SubmitPassword was not creating a new id-management This is because I broke up the old "createIdmgmt" method to not perform as much conditional logic. Now the pieces are reusable and do what they should do. --- app/scripts/lib/id-management.js | 8 ++++++++ app/scripts/lib/idStore.js | 23 +++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/id-management.js b/app/scripts/lib/id-management.js index 9b8ceb415..e250943a0 100644 --- a/app/scripts/lib/id-management.js +++ b/app/scripts/lib/id-management.js @@ -1,3 +1,11 @@ +/* ID Management + * + * This module exists to hold the decrypted credentials for the current session. + * It therefore exposes sign methods, because it is able to perform these + * with noa dditional authentication, because its very instantiation + * means the vault is unlocked. + */ + const ethUtil = require('ethereumjs-util') const Transaction = require('ethereumjs-tx') diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index 8b7e3ad3b..1126b83e7 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -51,7 +51,7 @@ IdentityStore.prototype.createNewVault = function (password, entropy, cb) { this.configManager.setData({}) } - this._createIdmgmt(password, null, entropy, (err) => { + this._createVault(password, null, entropy, (err) => { if (err) return cb(err) this._autoFaucet() @@ -72,7 +72,7 @@ IdentityStore.prototype.recoverSeed = function (cb) { } IdentityStore.prototype.recoverFromSeed = function (password, seed, cb) { - this._createIdmgmt(password, seed, null, (err) => { + this._createVault(password, seed, null, (err) => { if (err) return cb(err) this._loadIdentities() @@ -449,11 +449,12 @@ IdentityStore.prototype.tryPassword = function (password, cb) { const isCorrect = keyStore.isDerivedKeyCorrect(pwDerivedKey) if (!isCorrect) return cb(new Error('Lightwallet - password incorrect')) + this._createIdMgmt(derivedKey) cb() }) } -IdentityStore.prototype._createIdmgmt = function (password, seedPhrase, entropy, cb) { +IdentityStore.prototype._createVault = function (password, seedPhrase, entropy, cb) { const opts = { password, hdPathString: this.hdPathString, @@ -476,13 +477,7 @@ IdentityStore.prototype._createIdmgmt = function (password, seedPhrase, entropy, keyStore.addHdDerivationPath(this.hdPathString, derivedKey, {curve: 'secp256k1', purpose: 'sign'}) this._createFirstWallet(derivedKey) - - this._idmgmt = new IdManagement({ - keyStore: keyStore, - derivedKey: derivedKey, - configManager: this.configManager, - }) - + this._createIdMgmt(derivedKey) this.setSelectedAddressSync() cb() @@ -490,6 +485,14 @@ IdentityStore.prototype._createIdmgmt = function (password, seedPhrase, entropy, }) } +IdentityStore.prototype._createIdMgmt = function (derivedKey) { + this._idmgmt = new IdManagement({ + keyStore: this.keyStore, + derivedKey: derivedKey, + configManager: this.configManager, + }) +} + IdentityStore.prototype.purgeCache = function () { this._getAddresses().forEach((address) => { this._ethStore.del(address) -- cgit From e0e38b879f4d8ea367a0ea77be633d0b4d6762fa Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 12 Sep 2016 11:21:27 -0700 Subject: Fix some references --- app/scripts/lib/idStore.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index 1126b83e7..4160466e1 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -449,7 +449,7 @@ IdentityStore.prototype.tryPassword = function (password, cb) { const isCorrect = keyStore.isDerivedKeyCorrect(pwDerivedKey) if (!isCorrect) return cb(new Error('Lightwallet - password incorrect')) - this._createIdMgmt(derivedKey) + this._createIdMgmt(pwDerivedKey) cb() }) } @@ -487,7 +487,7 @@ IdentityStore.prototype._createVault = function (password, seedPhrase, entropy, IdentityStore.prototype._createIdMgmt = function (derivedKey) { this._idmgmt = new IdManagement({ - keyStore: this.keyStore, + keyStore: this._keyStore, derivedKey: derivedKey, configManager: this.configManager, }) -- cgit From 4a47f26e8caaea5e85820f0684fdb86ccb43196b Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 12 Sep 2016 11:25:30 -0700 Subject: Fix keystore reference --- app/scripts/lib/idStore.js | 1 + 1 file changed, 1 insertion(+) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index 4160466e1..d33a67387 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -449,6 +449,7 @@ IdentityStore.prototype.tryPassword = function (password, cb) { const isCorrect = keyStore.isDerivedKeyCorrect(pwDerivedKey) if (!isCorrect) return cb(new Error('Lightwallet - password incorrect')) + this._keyStore = keyStore this._createIdMgmt(pwDerivedKey) cb() }) -- cgit From 0d11eab8cbb8f88c77a5d9eebf87a5796a585c93 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 12 Sep 2016 14:22:06 -0700 Subject: Add accounts back to metamask state --- app/scripts/lib/idStore.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index d33a67387..0c6d7c2dd 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -148,6 +148,10 @@ IdentityStore.prototype.revealAccount = function (cb) { keyStore.setDefaultHdDerivationPath(this.hdPathString) keyStore.generateNewAddress(derivedKey, 1) + const addresses = ks.getAddresses(); + const address = addresses[ addresses.length -1 ] + + this._ethStore.addAccount(ethUtil.addHexPrefix(address)) configManager.setWallet(keyStore.serialize()) @@ -496,7 +500,7 @@ IdentityStore.prototype._createIdMgmt = function (derivedKey) { IdentityStore.prototype.purgeCache = function () { this._getAddresses().forEach((address) => { - this._ethStore.del(address) + this._ethStore.del(ethUtil.addHexPrefix(address)) }) } -- cgit From 1ba83766bb1ba52e79fae57882692d4cc0b64fdc Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 12 Sep 2016 14:33:54 -0700 Subject: Load accounts into ethStore on unlock --- app/scripts/lib/idStore.js | 1 + 1 file changed, 1 insertion(+) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index 0c6d7c2dd..16a86036a 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -403,6 +403,7 @@ IdentityStore.prototype._loadIdentities = function () { var addresses = this._getAddresses() addresses.forEach((address, i) => { // // add to ethStore + this._ethStore.addAccount(ethUtil.addHexPrefix(address)) // add to identities const defaultLabel = 'Wallet ' + (i + 1) const nickname = configManager.nicknameForWallet(address) -- cgit From 02b57adbf1c96df87f5a9ca77793eef71b45348e Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 12 Sep 2016 14:34:45 -0700 Subject: Linted --- app/scripts/lib/idStore.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index 16a86036a..0c5260ffd 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -148,8 +148,8 @@ IdentityStore.prototype.revealAccount = function (cb) { keyStore.setDefaultHdDerivationPath(this.hdPathString) keyStore.generateNewAddress(derivedKey, 1) - const addresses = ks.getAddresses(); - const address = addresses[ addresses.length -1 ] + const addresses = keyStore.getAddresses() + const address = addresses[ addresses.length - 1 ] this._ethStore.addAccount(ethUtil.addHexPrefix(address)) -- cgit From c3d1404e72f4978db5b4522cb793ffd22653d9ff Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 12 Sep 2016 15:18:32 -0700 Subject: Fix clearing of account cache on vault restore --- app/scripts/lib/idStore.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index 0c5260ffd..e84e1aba0 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -51,6 +51,7 @@ IdentityStore.prototype.createNewVault = function (password, entropy, cb) { this.configManager.setData({}) } + this.purgeCache() this._createVault(password, null, entropy, (err) => { if (err) return cb(err) @@ -59,7 +60,6 @@ IdentityStore.prototype.createNewVault = function (password, entropy, cb) { this.configManager.setShowSeedWords(true) var seedWords = this._idmgmt.getSeed() - cb(null, seedWords) }) } @@ -72,6 +72,8 @@ IdentityStore.prototype.recoverSeed = function (cb) { } IdentityStore.prototype.recoverFromSeed = function (password, seed, cb) { + this.purgeCache() + this._createVault(password, seed, null, (err) => { if (err) return cb(err) @@ -500,8 +502,10 @@ IdentityStore.prototype._createIdMgmt = function (derivedKey) { } IdentityStore.prototype.purgeCache = function () { - this._getAddresses().forEach((address) => { - this._ethStore.del(ethUtil.addHexPrefix(address)) + this._currentState.identities = {} + var accounts = Object.keys(this._ethStore._currentState.accounts) + accounts.forEach((address) => { + this._ethStore.removeAccount(address) }) } -- cgit From 0ea0a9813d1e33613d19a6a74f8966a6e8f4a2ac Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 12 Sep 2016 15:26:07 -0700 Subject: Make cache clearning error tolerant --- app/scripts/lib/idStore.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index e84e1aba0..b5b13a57a 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -503,7 +503,12 @@ IdentityStore.prototype._createIdMgmt = function (derivedKey) { IdentityStore.prototype.purgeCache = function () { this._currentState.identities = {} - var accounts = Object.keys(this._ethStore._currentState.accounts) + let accounts + try { + Object.keys(this._ethStore._currentState.accounts) + } catch (e) { + accounts = [] + } accounts.forEach((address) => { this._ethStore.removeAccount(address) }) -- cgit From b508541935b5cbd8202e3d6cfc4d0584e6518b46 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Mon, 12 Sep 2016 22:13:52 -0700 Subject: Fix cache clearing reference for Opera For some reason Chrome didn't mind this awful bug, but Opera caught it. --- app/scripts/lib/idStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index b5b13a57a..89c0c3abc 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -505,7 +505,7 @@ IdentityStore.prototype.purgeCache = function () { this._currentState.identities = {} let accounts try { - Object.keys(this._ethStore._currentState.accounts) + accounts = Object.keys(this._ethStore._currentState.accounts) } catch (e) { accounts = [] } -- cgit