@@ -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+
5354 this . _createIdmgmt ( password , null , entropy , ( err ) => {
5455 if ( err ) return cb ( err )
5556
56- this . _loadIdentities ( )
57- this . _didUpdate ( )
5857 this . _autoFaucet ( )
5958
6059 this . configManager . setShowSeedWords ( true )
6160 var seedWords = this . _idmgmt . getSeed ( )
61+
62+
6263 cb ( null , seedWords )
6364 } )
6465}
@@ -75,7 +76,6 @@ IdentityStore.prototype.recoverFromSeed = function (password, seed, cb) {
7576 if ( err ) return cb ( err )
7677
7778 this . _loadIdentities ( )
78- this . _didUpdate ( )
7979 cb ( null , this . getState ( ) )
8080 } )
8181}
@@ -125,15 +125,20 @@ IdentityStore.prototype.getSelectedAddress = function () {
125125 return configManager . getSelectedAccount ( )
126126}
127127
128- IdentityStore . prototype . setSelectedAddress = function ( address , cb ) {
128+ IdentityStore . prototype . setSelectedAddressSync = function ( address ) {
129129 const configManager = this . configManager
130130 if ( ! address ) {
131131 var addresses = this . _getAddresses ( )
132132 address = addresses [ 0 ]
133133 }
134134
135135 configManager . setSelectedAccount ( address )
136- if ( cb ) return cb ( null , address )
136+ return address
137+ }
138+
139+ IdentityStore . prototype . setSelectedAddress = function ( address , cb ) {
140+ const resultAddress = this . setSelectedAddressSync ( address )
141+ if ( cb ) return cb ( null , resultAddress )
137142}
138143
139144IdentityStore . prototype . revealAccount = function ( cb ) {
@@ -143,6 +148,7 @@ IdentityStore.prototype.revealAccount = function (cb) {
143148
144149 keyStore . setDefaultHdDerivationPath ( this . hdPathString )
145150 keyStore . generateNewAddress ( derivedKey , 1 )
151+
146152 configManager . setWallet ( keyStore . serialize ( ) )
147153
148154 this . _loadIdentities ( )
@@ -393,7 +399,6 @@ IdentityStore.prototype._loadIdentities = function () {
393399 var addresses = this . _getAddresses ( )
394400 addresses . forEach ( ( address , i ) => {
395401 // // add to ethStore
396- this . _ethStore . addAccount ( address )
397402 // add to identities
398403 const defaultLabel = 'Wallet ' + ( i + 1 )
399404 const nickname = configManager . nicknameForWallet ( address )
@@ -412,7 +417,6 @@ IdentityStore.prototype.saveAccountLabel = function (account, label, cb) {
412417 configManager . setNicknameForWallet ( account , label )
413418 this . _loadIdentities ( )
414419 cb ( null , label )
415- this . _didUpdate ( )
416420}
417421
418422// mayBeFauceting
@@ -436,77 +440,76 @@ IdentityStore.prototype._mayBeFauceting = function (i) {
436440//
437441
438442IdentityStore . prototype . tryPassword = function ( password , cb ) {
439- this . _createIdmgmt ( password , null , null , cb )
440- }
441-
442- IdentityStore . prototype . _createIdmgmt = function ( password , seed , entropy , cb ) {
443- const configManager = this . configManager
443+ var serializedKeystore = this . configManager . getWallet ( )
444+ var keyStore = KeyStore . deserialize ( serializedKeystore )
444445
445- var keyStore = null
446- LightwalletKeyStore . deriveKeyFromPassword ( password , ( err , derivedKey ) => {
446+ keyStore . keyFromPassword ( password , ( err , pwDerivedKey ) => {
447447 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- }
467448
468- this . _keyStore = keyStore
469- this . _idmgmt = new IdManagement ( {
470- keyStore : keyStore ,
471- derivedKey : derivedKey ,
472- hdPathSTring : this . hdPathString ,
473- configManager : this . configManager ,
474- } )
449+ const isCorrect = keyStore . isDerivedKeyCorrect ( pwDerivedKey )
450+ if ( ! isCorrect ) return cb ( new Error ( 'Lightwallet - password incorrect' ) )
475451
476452 cb ( )
477453 } )
478454}
479455
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 )
456+ IdentityStore . prototype . _createIdmgmt = function ( password , seedPhrase , entropy , cb ) {
457+ const opts = {
458+ password ,
459+ hdPathString : this . hdPathString ,
460+ }
485461
486- keyStore . generateNewAddress ( derivedKey , 1 )
487- configManager . setWallet ( keyStore . serialize ( ) )
488- if ( global . METAMASK_DEBUG ) {
489- console . log ( 'restored from seed. saved to keystore' )
462+ if ( seedPhrase ) {
463+ opts . seedPhrase = seedPhrase
490464 }
491- return keyStore
465+
466+ KeyStore . createVault ( opts , ( err , keyStore ) => {
467+ if ( err ) return cb ( err )
468+
469+ this . _keyStore = keyStore
470+
471+ keyStore . keyFromPassword ( password , ( err , derivedKey ) => {
472+ if ( err ) return cb ( err )
473+
474+ this . purgeCache ( )
475+
476+ keyStore . addHdDerivationPath ( this . hdPathString , derivedKey , { curve : 'secp256k1' , purpose : 'sign' } )
477+
478+ this . _createFirstWallet ( derivedKey )
479+
480+ this . _idmgmt = new IdManagement ( {
481+ keyStore : keyStore ,
482+ derivedKey : derivedKey ,
483+ configManager : this . configManager ,
484+ } )
485+
486+ this . setSelectedAddressSync ( )
487+
488+ cb ( )
489+ } )
490+ } )
492491}
493492
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' } )
499- keyStore . setDefaultHdDerivationPath ( this . hdPathString )
493+ IdentityStore . prototype . purgeCache = function ( ) {
494+ this . _getAddresses ( ) . forEach ( ( address ) => {
495+ this . _ethStore . del ( address )
496+ } )
497+ }
500498
499+ IdentityStore . prototype . _createFirstWallet = function ( derivedKey ) {
500+ const keyStore = this . _keyStore
501+ keyStore . setDefaultHdDerivationPath ( this . hdPathString )
501502 keyStore . generateNewAddress ( derivedKey , 1 )
502- configManager . setWallet ( keyStore . serialize ( ) )
503- console . log ( 'saved to keystore' )
504- return keyStore
503+ this . configManager . setWallet ( keyStore . serialize ( ) )
504+ var addresses = keyStore . getAddresses ( )
505+ this . _ethStore . addAccount ( ethUtil . addHexPrefix ( addresses [ 0 ] ) )
505506}
506507
507508// get addresses and normalize address hexString
508509IdentityStore . prototype . _getAddresses = function ( ) {
509- return this . _keyStore . getAddresses ( this . hdPathString ) . map ( ( address ) => { return '0x' + address } )
510+ return this . _keyStore . getAddresses ( this . hdPathString ) . map ( ( address ) => {
511+ return ethUtil . addHexPrefix ( address )
512+ } )
510513}
511514
512515IdentityStore . prototype . _autoFaucet = function ( ) {
0 commit comments