|
|
@ -1,12 +1,12 @@ |
|
|
|
const ethUtil = require('ethereumjs-util') |
|
|
|
const ethUtil = require('ethereumjs-util') |
|
|
|
|
|
|
|
const BN = ethUtil.BN |
|
|
|
const bip39 = require('bip39') |
|
|
|
const bip39 = require('bip39') |
|
|
|
const EventEmitter = require('events').EventEmitter |
|
|
|
const EventEmitter = require('events').EventEmitter |
|
|
|
|
|
|
|
const ObservableStore = require('obs-store') |
|
|
|
const filter = require('promise-filter') |
|
|
|
const filter = require('promise-filter') |
|
|
|
const encryptor = require('browser-passworder') |
|
|
|
const encryptor = require('browser-passworder') |
|
|
|
|
|
|
|
const normalizeAddress = require('./lib/sig-util').normalize |
|
|
|
const normalize = require('./lib/sig-util').normalize |
|
|
|
function noop () {} |
|
|
|
const BN = ethUtil.BN |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Keyrings:
|
|
|
|
// Keyrings:
|
|
|
|
const SimpleKeyring = require('./keyrings/simple') |
|
|
|
const SimpleKeyring = require('./keyrings/simple') |
|
|
|
const HdKeyring = require('./keyrings/hd') |
|
|
|
const HdKeyring = require('./keyrings/hd') |
|
|
@ -15,7 +15,7 @@ const keyringTypes = [ |
|
|
|
HdKeyring, |
|
|
|
HdKeyring, |
|
|
|
] |
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
module.exports = class KeyringController extends EventEmitter { |
|
|
|
class KeyringController extends EventEmitter { |
|
|
|
|
|
|
|
|
|
|
|
// PUBLIC METHODS
|
|
|
|
// PUBLIC METHODS
|
|
|
|
//
|
|
|
|
//
|
|
|
@ -26,6 +26,8 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
|
|
|
|
|
|
|
|
constructor (opts) { |
|
|
|
constructor (opts) { |
|
|
|
super() |
|
|
|
super() |
|
|
|
|
|
|
|
const initState = opts.initState || {} |
|
|
|
|
|
|
|
this.store = new ObservableStore(initState) |
|
|
|
this.configManager = opts.configManager |
|
|
|
this.configManager = opts.configManager |
|
|
|
this.ethStore = opts.ethStore |
|
|
|
this.ethStore = opts.ethStore |
|
|
|
this.encryptor = encryptor |
|
|
|
this.encryptor = encryptor |
|
|
@ -65,27 +67,29 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
// in this class, but will need to be Promisified when we move our
|
|
|
|
// in this class, but will need to be Promisified when we move our
|
|
|
|
// persistence to an async model.
|
|
|
|
// persistence to an async model.
|
|
|
|
getState () { |
|
|
|
getState () { |
|
|
|
const configManager = this.configManager |
|
|
|
return Promise.all(this.keyrings.map(this.displayForKeyring)) |
|
|
|
const address = configManager.getSelectedAccount() |
|
|
|
|
|
|
|
const wallet = configManager.getWallet() // old style vault
|
|
|
|
|
|
|
|
const vault = configManager.getVault() // new style vault
|
|
|
|
|
|
|
|
const keyrings = this.keyrings |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Promise.all(keyrings.map(this.displayForKeyring)) |
|
|
|
|
|
|
|
.then((displayKeyrings) => { |
|
|
|
.then((displayKeyrings) => { |
|
|
|
|
|
|
|
const state = this.store.getState() |
|
|
|
|
|
|
|
// old wallet
|
|
|
|
|
|
|
|
const wallet = this.configManager.getWallet() |
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
// computed
|
|
|
|
|
|
|
|
isInitialized: (!!wallet || !!state.vault), |
|
|
|
|
|
|
|
isUnlocked: (!!this.password), |
|
|
|
|
|
|
|
keyrings: displayKeyrings, |
|
|
|
|
|
|
|
// hard coded
|
|
|
|
|
|
|
|
keyringTypes: this.keyringTypes.map(krt => krt.type), |
|
|
|
|
|
|
|
// memStore
|
|
|
|
|
|
|
|
identities: this.identities, |
|
|
|
|
|
|
|
// diskStore
|
|
|
|
|
|
|
|
selectedAccount: state.selectedAccount, |
|
|
|
|
|
|
|
// configManager
|
|
|
|
seedWords: this.configManager.getSeedWords(), |
|
|
|
seedWords: this.configManager.getSeedWords(), |
|
|
|
isInitialized: (!!wallet || !!vault), |
|
|
|
|
|
|
|
isUnlocked: Boolean(this.password), |
|
|
|
|
|
|
|
isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), |
|
|
|
isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), |
|
|
|
selectedAccount: address, |
|
|
|
|
|
|
|
shapeShiftTxList: this.configManager.getShapeShiftTxList(), |
|
|
|
|
|
|
|
currentFiat: this.configManager.getCurrentFiat(), |
|
|
|
currentFiat: this.configManager.getCurrentFiat(), |
|
|
|
conversionRate: this.configManager.getConversionRate(), |
|
|
|
conversionRate: this.configManager.getConversionRate(), |
|
|
|
conversionDate: this.configManager.getConversionDate(), |
|
|
|
conversionDate: this.configManager.getConversionDate(), |
|
|
|
keyringTypes: this.keyringTypes.map(krt => krt.type), |
|
|
|
// messageManager
|
|
|
|
identities: this.identities, |
|
|
|
|
|
|
|
keyrings: displayKeyrings, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
@ -138,8 +142,8 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
.then((accounts) => { |
|
|
|
.then((accounts) => { |
|
|
|
const firstAccount = accounts[0] |
|
|
|
const firstAccount = accounts[0] |
|
|
|
if (!firstAccount) throw new Error('KeyringController - First Account not found.') |
|
|
|
if (!firstAccount) throw new Error('KeyringController - First Account not found.') |
|
|
|
const hexAccount = normalize(firstAccount) |
|
|
|
const hexAccount = normalizeAddress(firstAccount) |
|
|
|
this.configManager.setSelectedAccount(hexAccount) |
|
|
|
this.setSelectedAccount(hexAccount) |
|
|
|
return this.setupAccounts(accounts) |
|
|
|
return this.setupAccounts(accounts) |
|
|
|
}) |
|
|
|
}) |
|
|
|
.then(this.persistAllKeyrings.bind(this, password)) |
|
|
|
.then(this.persistAllKeyrings.bind(this, password)) |
|
|
@ -236,9 +240,9 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
//
|
|
|
|
//
|
|
|
|
// Sets the state's `selectedAccount` value
|
|
|
|
// Sets the state's `selectedAccount` value
|
|
|
|
// to the specified address.
|
|
|
|
// to the specified address.
|
|
|
|
setSelectedAccount (address) { |
|
|
|
setSelectedAccount (account) { |
|
|
|
var addr = normalize(address) |
|
|
|
var address = normalizeAddress(account) |
|
|
|
this.configManager.setSelectedAccount(addr) |
|
|
|
this.store.updateState({ selectedAccount: address }) |
|
|
|
return this.fullUpdate() |
|
|
|
return this.fullUpdate() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -250,11 +254,19 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
//
|
|
|
|
//
|
|
|
|
// Persists a nickname equal to `label` for the specified account.
|
|
|
|
// Persists a nickname equal to `label` for the specified account.
|
|
|
|
saveAccountLabel (account, label) { |
|
|
|
saveAccountLabel (account, label) { |
|
|
|
const address = normalize(account) |
|
|
|
try { |
|
|
|
const configManager = this.configManager |
|
|
|
const hexAddress = normalizeAddress(account) |
|
|
|
configManager.setNicknameForWallet(address, label) |
|
|
|
// update state on diskStore
|
|
|
|
this.identities[address].name = label |
|
|
|
const state = this.store.getState() |
|
|
|
|
|
|
|
const walletNicknames = state.walletNicknames || {} |
|
|
|
|
|
|
|
walletNicknames[hexAddress] = label |
|
|
|
|
|
|
|
this.store.updateState({ walletNicknames }) |
|
|
|
|
|
|
|
// update state on memStore
|
|
|
|
|
|
|
|
this.identities[hexAddress].name = label |
|
|
|
return Promise.resolve(label) |
|
|
|
return Promise.resolve(label) |
|
|
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
|
|
return Promise.reject(err) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Export Account
|
|
|
|
// Export Account
|
|
|
@ -270,7 +282,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
try { |
|
|
|
try { |
|
|
|
return this.getKeyringForAccount(address) |
|
|
|
return this.getKeyringForAccount(address) |
|
|
|
.then((keyring) => { |
|
|
|
.then((keyring) => { |
|
|
|
return keyring.exportAccount(normalize(address)) |
|
|
|
return keyring.exportAccount(normalizeAddress(address)) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
return Promise.reject(e) |
|
|
|
return Promise.reject(e) |
|
|
@ -284,7 +296,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
// TX Manager to update the state after signing
|
|
|
|
// TX Manager to update the state after signing
|
|
|
|
|
|
|
|
|
|
|
|
signTransaction (ethTx, _fromAddress) { |
|
|
|
signTransaction (ethTx, _fromAddress) { |
|
|
|
const fromAddress = normalize(_fromAddress) |
|
|
|
const fromAddress = normalizeAddress(_fromAddress) |
|
|
|
return this.getKeyringForAccount(fromAddress) |
|
|
|
return this.getKeyringForAccount(fromAddress) |
|
|
|
.then((keyring) => { |
|
|
|
.then((keyring) => { |
|
|
|
return keyring.signTransaction(fromAddress, ethTx) |
|
|
|
return keyring.signTransaction(fromAddress, ethTx) |
|
|
@ -328,8 +340,8 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
.then((accounts) => { |
|
|
|
.then((accounts) => { |
|
|
|
const firstAccount = accounts[0] |
|
|
|
const firstAccount = accounts[0] |
|
|
|
if (!firstAccount) throw new Error('KeyringController - No account found on keychain.') |
|
|
|
if (!firstAccount) throw new Error('KeyringController - No account found on keychain.') |
|
|
|
const hexAccount = normalize(firstAccount) |
|
|
|
const hexAccount = normalizeAddress(firstAccount) |
|
|
|
this.configManager.setSelectedAccount(hexAccount) |
|
|
|
this.setSelectedAccount(hexAccount) |
|
|
|
this.emit('newAccount', hexAccount) |
|
|
|
this.emit('newAccount', hexAccount) |
|
|
|
return this.setupAccounts(accounts) |
|
|
|
return this.setupAccounts(accounts) |
|
|
|
}) |
|
|
|
}) |
|
|
@ -365,7 +377,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
if (!account) { |
|
|
|
if (!account) { |
|
|
|
throw new Error('Problem loading account.') |
|
|
|
throw new Error('Problem loading account.') |
|
|
|
} |
|
|
|
} |
|
|
|
const address = normalize(account) |
|
|
|
const address = normalizeAddress(account) |
|
|
|
this.ethStore.addAccount(address) |
|
|
|
this.ethStore.addAccount(address) |
|
|
|
return this.createNickname(address) |
|
|
|
return this.createNickname(address) |
|
|
|
} |
|
|
|
} |
|
|
@ -377,10 +389,11 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
//
|
|
|
|
//
|
|
|
|
// Takes an address, and assigns it an incremented nickname, persisting it.
|
|
|
|
// Takes an address, and assigns it an incremented nickname, persisting it.
|
|
|
|
createNickname (address) { |
|
|
|
createNickname (address) { |
|
|
|
const hexAddress = normalize(address) |
|
|
|
const hexAddress = normalizeAddress(address) |
|
|
|
var i = Object.keys(this.identities).length |
|
|
|
const currentIdentityCount = Object.keys(this.identities).length + 1 |
|
|
|
const oldNickname = this.configManager.nicknameForWallet(address) |
|
|
|
const nicknames = this.store.getState().walletNicknames || {} |
|
|
|
const name = oldNickname || `Account ${++i}` |
|
|
|
const existingNickname = nicknames[hexAddress] |
|
|
|
|
|
|
|
const name = existingNickname || `Account ${currentIdentityCount}` |
|
|
|
this.identities[hexAddress] = { |
|
|
|
this.identities[hexAddress] = { |
|
|
|
address: hexAddress, |
|
|
|
address: hexAddress, |
|
|
|
name, |
|
|
|
name, |
|
|
@ -415,7 +428,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
return this.encryptor.encrypt(this.password, serializedKeyrings) |
|
|
|
return this.encryptor.encrypt(this.password, serializedKeyrings) |
|
|
|
}) |
|
|
|
}) |
|
|
|
.then((encryptedString) => { |
|
|
|
.then((encryptedString) => { |
|
|
|
this.configManager.setVault(encryptedString) |
|
|
|
this.store.updateState({ vault: encryptedString }) |
|
|
|
return true |
|
|
|
return true |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
@ -428,7 +441,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
// Attempts to unlock the persisted encrypted storage,
|
|
|
|
// Attempts to unlock the persisted encrypted storage,
|
|
|
|
// initializing the persisted keyrings to RAM.
|
|
|
|
// initializing the persisted keyrings to RAM.
|
|
|
|
unlockKeyrings (password) { |
|
|
|
unlockKeyrings (password) { |
|
|
|
const encryptedVault = this.configManager.getVault() |
|
|
|
const encryptedVault = this.store.getState().vault |
|
|
|
if (!encryptedVault) { |
|
|
|
if (!encryptedVault) { |
|
|
|
throw new Error('Cannot unlock without a previous vault.') |
|
|
|
throw new Error('Cannot unlock without a previous vault.') |
|
|
|
} |
|
|
|
} |
|
|
@ -508,7 +521,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
// Returns the currently initialized keyring that manages
|
|
|
|
// Returns the currently initialized keyring that manages
|
|
|
|
// the specified `address` if one exists.
|
|
|
|
// the specified `address` if one exists.
|
|
|
|
getKeyringForAccount (address) { |
|
|
|
getKeyringForAccount (address) { |
|
|
|
const hexed = normalize(address) |
|
|
|
const hexed = normalizeAddress(address) |
|
|
|
|
|
|
|
|
|
|
|
return Promise.all(this.keyrings.map((keyring) => { |
|
|
|
return Promise.all(this.keyrings.map((keyring) => { |
|
|
|
return Promise.all([ |
|
|
|
return Promise.all([ |
|
|
@ -517,7 +530,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
]) |
|
|
|
]) |
|
|
|
})) |
|
|
|
})) |
|
|
|
.then(filter((candidate) => { |
|
|
|
.then(filter((candidate) => { |
|
|
|
const accounts = candidate[1].map(normalize) |
|
|
|
const accounts = candidate[1].map(normalizeAddress) |
|
|
|
return accounts.includes(hexed) |
|
|
|
return accounts.includes(hexed) |
|
|
|
})) |
|
|
|
})) |
|
|
|
.then((winners) => { |
|
|
|
.then((winners) => { |
|
|
@ -575,7 +588,9 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
|
|
|
|
|
|
|
|
this.keyrings = [] |
|
|
|
this.keyrings = [] |
|
|
|
this.identities = {} |
|
|
|
this.identities = {} |
|
|
|
this.configManager.setSelectedAccount() |
|
|
|
this.setSelectedAccount() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = KeyringController |
|
|
|