@@ -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 KeyStore = require ( 'eth-lightwallet' ) . keystore
6+ const LightwalletKeyStore = require ( 'eth-lightwallet' ) . keystore
77const clone = require ( 'clone' )
88const extend = require ( 'xtend' )
99const createId = require ( 'web3-provider-engine/util/random-id' )
@@ -50,16 +50,15 @@ IdentityStore.prototype.createNewVault = function (password, entropy, cb) {
5050 if ( serializedKeystore ) {
5151 this . configManager . setData ( { } )
5252 }
53-
5453 this . _createIdmgmt ( password , null , entropy , ( err ) => {
5554 if ( err ) return cb ( err )
5655
56+ this . _loadIdentities ( )
57+ this . _didUpdate ( )
5758 this . _autoFaucet ( )
5859
5960 this . configManager . setShowSeedWords ( true )
6061 var seedWords = this . _idmgmt . getSeed ( )
61-
62-
6362 cb ( null , seedWords )
6463 } )
6564}
@@ -76,6 +75,7 @@ IdentityStore.prototype.recoverFromSeed = function (password, seed, cb) {
7675 if ( err ) return cb ( err )
7776
7877 this . _loadIdentities ( )
78+ this . _didUpdate ( )
7979 cb ( null , this . getState ( ) )
8080 } )
8181}
@@ -125,20 +125,15 @@ IdentityStore.prototype.getSelectedAddress = function () {
125125 return configManager . getSelectedAccount ( )
126126}
127127
128- IdentityStore . prototype . setSelectedAddressSync = function ( address ) {
128+ IdentityStore . prototype . setSelectedAddress = function ( address , cb ) {
129129 const configManager = this . configManager
130130 if ( ! address ) {
131131 var addresses = this . _getAddresses ( )
132132 address = addresses [ 0 ]
133133 }
134134
135135 configManager . setSelectedAccount ( 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 )
136+ if ( cb ) return cb ( null , address )
142137}
143138
144139IdentityStore . prototype . revealAccount = function ( cb ) {
@@ -148,7 +143,6 @@ IdentityStore.prototype.revealAccount = function (cb) {
148143
149144 keyStore . setDefaultHdDerivationPath ( this . hdPathString )
150145 keyStore . generateNewAddress ( derivedKey , 1 )
151-
152146 configManager . setWallet ( keyStore . serialize ( ) )
153147
154148 this . _loadIdentities ( )
@@ -399,6 +393,7 @@ IdentityStore.prototype._loadIdentities = function () {
399393 var addresses = this . _getAddresses ( )
400394 addresses . forEach ( ( address , i ) => {
401395 // // add to ethStore
396+ this . _ethStore . addAccount ( address )
402397 // add to identities
403398 const defaultLabel = 'Wallet ' + ( i + 1 )
404399 const nickname = configManager . nicknameForWallet ( address )
@@ -417,6 +412,7 @@ IdentityStore.prototype.saveAccountLabel = function (account, label, cb) {
417412 configManager . setNicknameForWallet ( account , label )
418413 this . _loadIdentities ( )
419414 cb ( null , label )
415+ this . _didUpdate ( )
420416}
421417
422418// mayBeFauceting
@@ -440,76 +436,77 @@ IdentityStore.prototype._mayBeFauceting = function (i) {
440436//
441437
442438IdentityStore . prototype . tryPassword = function ( password , cb ) {
443- var serializedKeystore = this . configManager . getWallet ( )
444- var keyStore = KeyStore . deserialize ( serializedKeystore )
445-
446- keyStore . keyFromPassword ( password , ( err , pwDerivedKey ) => {
447- if ( err ) return cb ( err )
448-
449- const isCorrect = keyStore . isDerivedKeyCorrect ( pwDerivedKey )
450- if ( ! isCorrect ) return cb ( new Error ( 'Lightwallet - password incorrect' ) )
451-
452- cb ( )
453- } )
439+ this . _createIdmgmt ( password , null , null , cb )
454440}
455441
456- IdentityStore . prototype . _createIdmgmt = function ( password , seedPhrase , entropy , cb ) {
457- const opts = {
458- password,
459- hdPathString : this . hdPathString ,
460- }
461-
462- if ( seedPhrase ) {
463- opts . seedPhrase = seedPhrase
464- }
442+ IdentityStore . prototype . _createIdmgmt = function ( password , seed , entropy , cb ) {
443+ const configManager = this . configManager
465444
466- KeyStore . createVault ( opts , ( err , keyStore ) => {
445+ var keyStore = null
446+ LightwalletKeyStore . deriveKeyFromPassword ( password , ( err , derivedKey ) => {
467447 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+ }
468467
469468 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 ( )
469+ this . _idmgmt = new IdManagement ( {
470+ keyStore : keyStore ,
471+ derivedKey : derivedKey ,
472+ hdPathSTring : this . hdPathString ,
473+ configManager : this . configManager ,
489474 } )
475+
476+ cb ( )
490477 } )
491478}
492479
493- IdentityStore . prototype . purgeCache = function ( ) {
494- this . _getAddresses ( ) . forEach ( ( address ) => {
495- this . _ethStore . del ( address )
496- } )
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 )
485+
486+ keyStore . generateNewAddress ( derivedKey , 1 )
487+ configManager . setWallet ( keyStore . serialize ( ) )
488+ if ( global . METAMASK_DEBUG ) {
489+ console . log ( 'restored from seed. saved to keystore' )
490+ }
491+ return keyStore
497492}
498493
499- IdentityStore . prototype . _createFirstWallet = function ( derivedKey ) {
500- const keyStore = this . _keyStore
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' } )
501499 keyStore . setDefaultHdDerivationPath ( this . hdPathString )
500+
502501 keyStore . generateNewAddress ( derivedKey , 1 )
503- this . configManager . setWallet ( keyStore . serialize ( ) )
504- var addresses = keyStore . getAddresses ( )
505- this . _ethStore . addAccount ( ethUtil . addHexPrefix ( addresses [ 0 ] ) )
502+ configManager . setWallet ( keyStore . serialize ( ) )
503+ console . log ( 'saved to keystore' )
504+ return keyStore
506505}
507506
508507// get addresses and normalize address hexString
509508IdentityStore . prototype . _getAddresses = function ( ) {
510- return this . _keyStore . getAddresses ( this . hdPathString ) . map ( ( address ) => {
511- return ethUtil . addHexPrefix ( address )
512- } )
509+ return this . _keyStore . getAddresses ( this . hdPathString ) . map ( ( address ) => { return '0x' + address } )
513510}
514511
515512IdentityStore . prototype . _autoFaucet = function ( ) {
0 commit comments