1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

background controller - extract KeyringC.placeSeedWords to MetamaskC

This commit is contained in:
kumavis 2017-01-24 15:33:33 -08:00
parent bef4b78196
commit 463a56ff54
4 changed files with 34 additions and 23 deletions

View File

@ -150,12 +150,13 @@ module.exports = class KeyringController extends EventEmitter {
mnemonic: seed,
numberOfAccounts: 1,
})
}).then(() => {
const firstKeyring = this.keyrings[0]
})
.then((firstKeyring) => {
return firstKeyring.getAccounts()
})
.then((accounts) => {
const firstAccount = accounts[0]
if (!firstAccount) throw new Error('KeyringController - First Account not found.')
const hexAccount = normalize(firstAccount)
this.configManager.setSelectedAccount(hexAccount)
return this.setupAccounts(accounts)
@ -164,22 +165,6 @@ module.exports = class KeyringController extends EventEmitter {
.then(this.fullUpdate.bind(this))
}
// PlaceSeedWords
// returns Promise( @object state )
//
// Adds the current vault's seed words to the UI's state tree.
//
// Used when creating a first vault, to allow confirmation.
// Also used when revealing the seed words in the confirmation view.
placeSeedWords (selectedKeyring) {
return selectedKeyring.serialize()
.then((serialized) => {
const seedWords = serialized.mnemonic
this.configManager.setSeedWords(seedWords)
return this.fullUpdate()
})
}
// ClearSeedWordCache
//
// returns Promise( @string currentSelectedAccount )
@ -424,15 +409,15 @@ module.exports = class KeyringController extends EventEmitter {
this.clearKeyrings()
return this.addNewKeyring('HD Key Tree', { numberOfAccounts: 1 })
.then((keyring) => {
const accounts = keyring.getAccounts()
return keyring.getAccounts()
})
.then((accounts) => {
const firstAccount = accounts[0]
if (!firstAccount) throw new Error('KeyringController - No account found on keychain.')
const hexAccount = normalize(firstAccount)
this.configManager.setSelectedAccount(hexAccount)
this.emit('newAccount', hexAccount)
return this.setupAccounts(accounts)
}).then(() => {
return this.placeSeedWords(this.getKeyringsByType('HD Key Tree')[0])
})
.then(this.persistAllKeyrings.bind(this))
}

View File

@ -107,10 +107,19 @@ module.exports = class MetamaskController extends EventEmitter {
// forward directly to keyringController
createNewVaultAndKeychain: nodeify(keyringController.createNewVaultAndKeychain).bind(keyringController),
createNewVaultAndRestore: nodeify(keyringController.createNewVaultAndRestore).bind(keyringController),
// Adds the current vault's seed words to the UI's state tree.
//
// Used when creating a first vault, to allow confirmation.
// Also used when revealing the seed words in the confirmation view.
placeSeedWords: (cb) => {
const primaryKeyring = keyringController.getKeyringsByType('HD Key Tree')[0]
if (!primaryKeyring) return cb(new Error('MetamaskController - No HD Key Tree found'))
promiseToCallback(keyringController.placeSeedWords(primaryKeyring))(cb)
primaryKeyring.serialize()
.then((serialized) => {
const seedWords = serialized.mnemonic
this.configManager.setSeedWords(seedWords)
promiseToCallback(this.keyringController.fullUpdate())(cb)
})
},
clearSeedWordCache: nodeify(keyringController.clearSeedWordCache).bind(keyringController),
setLocked: nodeify(keyringController.setLocked).bind(keyringController),

View File

@ -41,6 +41,9 @@ describe('KeyringController', function() {
state = newState
done()
})
.catch((err) => {
done(err)
})
})
afterEach(function() {

View File

@ -230,7 +230,21 @@ function createNewVaultAndRestore (password, seed) {
}
function createNewVaultAndKeychain (password) {
return callBackgroundThenUpdate(background.createNewVaultAndKeychain, password)
return (dispatch) => {
dispatch(actions.showLoadingIndication())
background.createNewVaultAndKeychain(password, (err, newState) => {
if (err) {
return dispatch(actions.displayWarning(err.message))
}
background.placeSeedWords((err, newState) => {
if (err) {
return dispatch(actions.displayWarning(err.message))
}
dispatch(actions.hideLoadingIndication())
dispatch(actions.updateMetamaskState(newState))
})
})
}
}
function revealSeedConfirmation () {