Skip to content

Commit e210d0d

Browse files
authored
Merge pull request #645 from MetaMask/FixPasswording
Fix account unlocking
2 parents b8f75e3 + 0ea0a98 commit e210d0d

File tree

9 files changed

+170
-91
lines changed

9 files changed

+170
-91
lines changed

app/scripts/lib/id-management.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/* ID Management
2+
*
3+
* This module exists to hold the decrypted credentials for the current session.
4+
* It therefore exposes sign methods, because it is able to perform these
5+
* with noa dditional authentication, because its very instantiation
6+
* means the vault is unlocked.
7+
*/
8+
19
const ethUtil = require('ethereumjs-util')
210
const Transaction = require('ethereumjs-tx')
311

app/scripts/lib/idStore.js

Lines changed: 82 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const inherits = require('util').inherits
33
const async = require('async')
44
const ethUtil = require('ethereumjs-util')
55
const EthQuery = require('eth-query')
6-
const LightwalletKeyStore = require('eth-lightwallet').keystore
6+
const KeyStore = require('eth-lightwallet').keystore
77
const clone = require('clone')
88
const extend = require('xtend')
99
const createId = require('web3-provider-engine/util/random-id')
@@ -50,15 +50,16 @@ IdentityStore.prototype.createNewVault = function (password, entropy, cb) {
5050
if (serializedKeystore) {
5151
this.configManager.setData({})
5252
}
53-
this._createIdmgmt(password, null, entropy, (err) => {
53+
54+
this.purgeCache()
55+
this._createVault(password, null, entropy, (err) => {
5456
if (err) return cb(err)
5557

56-
this._loadIdentities()
57-
this._didUpdate()
5858
this._autoFaucet()
5959

6060
this.configManager.setShowSeedWords(true)
6161
var seedWords = this._idmgmt.getSeed()
62+
6263
cb(null, seedWords)
6364
})
6465
}
@@ -71,11 +72,12 @@ IdentityStore.prototype.recoverSeed = function (cb) {
7172
}
7273

7374
IdentityStore.prototype.recoverFromSeed = function (password, seed, cb) {
74-
this._createIdmgmt(password, seed, null, (err) => {
75+
this.purgeCache()
76+
77+
this._createVault(password, seed, null, (err) => {
7578
if (err) return cb(err)
7679

7780
this._loadIdentities()
78-
this._didUpdate()
7981
cb(null, this.getState())
8082
})
8183
}
@@ -125,15 +127,20 @@ IdentityStore.prototype.getSelectedAddress = function () {
125127
return configManager.getSelectedAccount()
126128
}
127129

128-
IdentityStore.prototype.setSelectedAddress = function (address, cb) {
130+
IdentityStore.prototype.setSelectedAddressSync = function (address) {
129131
const configManager = this.configManager
130132
if (!address) {
131133
var addresses = this._getAddresses()
132134
address = addresses[0]
133135
}
134136

135137
configManager.setSelectedAccount(address)
136-
if (cb) return cb(null, address)
138+
return address
139+
}
140+
141+
IdentityStore.prototype.setSelectedAddress = function (address, cb) {
142+
const resultAddress = this.setSelectedAddressSync(address)
143+
if (cb) return cb(null, resultAddress)
137144
}
138145

139146
IdentityStore.prototype.revealAccount = function (cb) {
@@ -143,6 +150,11 @@ IdentityStore.prototype.revealAccount = function (cb) {
143150

144151
keyStore.setDefaultHdDerivationPath(this.hdPathString)
145152
keyStore.generateNewAddress(derivedKey, 1)
153+
const addresses = keyStore.getAddresses()
154+
const address = addresses[ addresses.length - 1 ]
155+
156+
this._ethStore.addAccount(ethUtil.addHexPrefix(address))
157+
146158
configManager.setWallet(keyStore.serialize())
147159

148160
this._loadIdentities()
@@ -393,7 +405,7 @@ IdentityStore.prototype._loadIdentities = function () {
393405
var addresses = this._getAddresses()
394406
addresses.forEach((address, i) => {
395407
// // add to ethStore
396-
this._ethStore.addAccount(address)
408+
this._ethStore.addAccount(ethUtil.addHexPrefix(address))
397409
// add to identities
398410
const defaultLabel = 'Wallet ' + (i + 1)
399411
const nickname = configManager.nicknameForWallet(address)
@@ -412,7 +424,6 @@ IdentityStore.prototype.saveAccountLabel = function (account, label, cb) {
412424
configManager.setNicknameForWallet(account, label)
413425
this._loadIdentities()
414426
cb(null, label)
415-
this._didUpdate()
416427
}
417428

418429
// mayBeFauceting
@@ -436,77 +447,87 @@ IdentityStore.prototype._mayBeFauceting = function (i) {
436447
//
437448

438449
IdentityStore.prototype.tryPassword = function (password, cb) {
439-
this._createIdmgmt(password, null, null, cb)
450+
var serializedKeystore = this.configManager.getWallet()
451+
var keyStore = KeyStore.deserialize(serializedKeystore)
452+
453+
keyStore.keyFromPassword(password, (err, pwDerivedKey) => {
454+
if (err) return cb(err)
455+
456+
const isCorrect = keyStore.isDerivedKeyCorrect(pwDerivedKey)
457+
if (!isCorrect) return cb(new Error('Lightwallet - password incorrect'))
458+
459+
this._keyStore = keyStore
460+
this._createIdMgmt(pwDerivedKey)
461+
cb()
462+
})
440463
}
441464

442-
IdentityStore.prototype._createIdmgmt = function (password, seed, entropy, cb) {
443-
const configManager = this.configManager
465+
IdentityStore.prototype._createVault = function (password, seedPhrase, entropy, cb) {
466+
const opts = {
467+
password,
468+
hdPathString: this.hdPathString,
469+
}
470+
471+
if (seedPhrase) {
472+
opts.seedPhrase = seedPhrase
473+
}
444474

445-
var keyStore = null
446-
LightwalletKeyStore.deriveKeyFromPassword(password, (err, derivedKey) => {
475+
KeyStore.createVault(opts, (err, keyStore) => {
447476
if (err) return cb(err)
448-
var serializedKeystore = configManager.getWallet()
449-
450-
if (seed) {
451-
try {
452-
keyStore = this._restoreFromSeed(password, seed, derivedKey)
453-
} catch (e) {
454-
return cb(e)
455-
}
456-
457-
// returning user, recovering from storage
458-
} else if (serializedKeystore) {
459-
keyStore = LightwalletKeyStore.deserialize(serializedKeystore)
460-
var isCorrect = keyStore.isDerivedKeyCorrect(derivedKey)
461-
if (!isCorrect) return cb(new Error('Lightwallet - password incorrect'))
462-
463-
// first time here
464-
} else {
465-
keyStore = this._createFirstWallet(entropy, derivedKey)
466-
}
467477

468478
this._keyStore = keyStore
469-
this._idmgmt = new IdManagement({
470-
keyStore: keyStore,
471-
derivedKey: derivedKey,
472-
hdPathSTring: this.hdPathString,
473-
configManager: this.configManager,
474-
})
475479

476-
cb()
480+
keyStore.keyFromPassword(password, (err, derivedKey) => {
481+
if (err) return cb(err)
482+
483+
this.purgeCache()
484+
485+
keyStore.addHdDerivationPath(this.hdPathString, derivedKey, {curve: 'secp256k1', purpose: 'sign'})
486+
487+
this._createFirstWallet(derivedKey)
488+
this._createIdMgmt(derivedKey)
489+
this.setSelectedAddressSync()
490+
491+
cb()
492+
})
477493
})
478494
}
479495

480-
IdentityStore.prototype._restoreFromSeed = function (password, seed, derivedKey) {
481-
const configManager = this.configManager
482-
var keyStore = new LightwalletKeyStore(seed, derivedKey, this.hdPathString)
483-
keyStore.addHdDerivationPath(this.hdPathString, derivedKey, {curve: 'secp256k1', purpose: 'sign'})
484-
keyStore.setDefaultHdDerivationPath(this.hdPathString)
496+
IdentityStore.prototype._createIdMgmt = function (derivedKey) {
497+
this._idmgmt = new IdManagement({
498+
keyStore: this._keyStore,
499+
derivedKey: derivedKey,
500+
configManager: this.configManager,
501+
})
502+
}
485503

486-
keyStore.generateNewAddress(derivedKey, 1)
487-
configManager.setWallet(keyStore.serialize())
488-
if (global.METAMASK_DEBUG) {
489-
console.log('restored from seed. saved to keystore')
504+
IdentityStore.prototype.purgeCache = function () {
505+
this._currentState.identities = {}
506+
let accounts
507+
try {
508+
Object.keys(this._ethStore._currentState.accounts)
509+
} catch (e) {
510+
accounts = []
490511
}
491-
return keyStore
512+
accounts.forEach((address) => {
513+
this._ethStore.removeAccount(address)
514+
})
492515
}
493516

494-
IdentityStore.prototype._createFirstWallet = function (entropy, derivedKey) {
495-
const configManager = this.configManager
496-
var secretSeed = LightwalletKeyStore.generateRandomSeed(entropy)
497-
var keyStore = new LightwalletKeyStore(secretSeed, derivedKey, this.hdPathString)
498-
keyStore.addHdDerivationPath(this.hdPathString, derivedKey, {curve: 'secp256k1', purpose: 'sign'})
517+
IdentityStore.prototype._createFirstWallet = function (derivedKey) {
518+
const keyStore = this._keyStore
499519
keyStore.setDefaultHdDerivationPath(this.hdPathString)
500-
501520
keyStore.generateNewAddress(derivedKey, 1)
502-
configManager.setWallet(keyStore.serialize())
503-
console.log('saved to keystore')
504-
return keyStore
521+
this.configManager.setWallet(keyStore.serialize())
522+
var addresses = keyStore.getAddresses()
523+
this._ethStore.addAccount(ethUtil.addHexPrefix(addresses[0]))
505524
}
506525

507526
// get addresses and normalize address hexString
508527
IdentityStore.prototype._getAddresses = function () {
509-
return this._keyStore.getAddresses(this.hdPathString).map((address) => { return '0x' + address })
528+
return this._keyStore.getAddresses(this.hdPathString).map((address) => {
529+
return ethUtil.addHexPrefix(address)
530+
})
510531
}
511532

512533
IdentityStore.prototype._autoFaucet = function () {

development/states.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

development/states/account-detail.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@
3333
"accounts": {
3434
"0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc": {
3535
"code": "0x",
36-
"balance": "0x0",
36+
"balance": "0x100000000000",
3737
"nonce": "0x0",
3838
"address": "0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc"
3939
},
4040
"0xec1adf982415d2ef5ec55899b9bfb8bc0f29251b": {
4141
"code": "0x",
4242
"nonce": "0x0",
43-
"balance": "0x0",
43+
"balance": "0x100000000000",
4444
"address": "0xec1adf982415d2ef5ec55899b9bfb8bc0f29251b"
4545
},
4646
"0xeb9e64b93097bc15f01f13eae97015c57ab64823": {
4747
"code": "0x",
4848
"nonce": "0x0",
49-
"balance": "0x0",
49+
"balance": "0x100000000000",
5050
"address": "0xeb9e64b93097bc15f01f13eae97015c57ab64823"
5151
},
5252
"0x704107d04affddd9b66ab9de3dd7b095852e9b69": {

development/states/accounts.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@
2929
"unconfTxs": {},
3030
"accounts": {
3131
"0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc": {
32-
"balance": "0x0",
32+
"balance": "0x100000000000000",
3333
"nonce": "0x0",
3434
"code": "0x",
3535
"address": "0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc"
3636
},
3737
"0xec1adf982415d2ef5ec55899b9bfb8bc0f29251b": {
38-
"balance": "0x0",
38+
"balance": "0x100000000000000",
3939
"nonce": "0x0",
4040
"code": "0x",
4141
"address": "0xec1adf982415d2ef5ec55899b9bfb8bc0f29251b"
4242
},
4343
"0xeb9e64b93097bc15f01f13eae97015c57ab64823": {
44-
"balance": "0x0",
44+
"balance": "0x100000000000",
4545
"nonce": "0x0",
4646
"code": "0x",
4747
"address": "0xeb9e64b93097bc15f01f13eae97015c57ab64823"
4848
},
4949
"0x704107d04affddd9b66ab9de3dd7b095852e9b69": {
50-
"balance": "0x0",
50+
"balance": "0x100000000000",
5151
"code": "0x",
5252
"nonce": "0x0",
5353
"address": "0x704107d04affddd9b66ab9de3dd7b095852e9b69"
@@ -82,4 +82,4 @@
8282
"scrollToBottom": true
8383
},
8484
"identities": {}
85-
}
85+
}

0 commit comments

Comments
 (0)