@@ -3,7 +3,7 @@ const inherits = require('util').inherits
33const async = require ( 'async' )
44const ethUtil = require ( 'ethereumjs-util' )
55const EthQuery = require ( 'eth-query' )
6- const LightwalletKeyStore = require ( 'eth-lightwallet' ) . keystore
6+ const KeyStore = require ( 'eth-lightwallet' ) . keystore
77const clone = require ( 'clone' )
88const extend = require ( 'xtend' )
99const 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
7374IdentityStore . 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
139146IdentityStore . 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
438449IdentityStore . 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
508527IdentityStore . 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
512533IdentityStore . prototype . _autoFaucet = function ( ) {
0 commit comments