|
|
@ -98,17 +98,20 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
this.addNewKeyring('HD Key Tree', { |
|
|
|
this.addNewKeyring('HD Key Tree', { |
|
|
|
mnemonic: seed, |
|
|
|
mnemonic: seed, |
|
|
|
numberOfAccounts: 1, |
|
|
|
numberOfAccounts: 1, |
|
|
|
}, (err) => { |
|
|
|
}).then(() => { |
|
|
|
if (err) return cb(err) |
|
|
|
|
|
|
|
const firstKeyring = this.keyrings[0] |
|
|
|
const firstKeyring = this.keyrings[0] |
|
|
|
const accounts = firstKeyring.getAccounts() |
|
|
|
return firstKeyring.getAccounts() |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.then((accounts) => { |
|
|
|
const firstAccount = accounts[0] |
|
|
|
const firstAccount = accounts[0] |
|
|
|
const hexAccount = normalize(firstAccount) |
|
|
|
const hexAccount = normalize(firstAccount) |
|
|
|
this.configManager.setSelectedAccount(hexAccount) |
|
|
|
this.configManager.setSelectedAccount(hexAccount) |
|
|
|
this.setupAccounts(accounts) |
|
|
|
this.setupAccounts(accounts) |
|
|
|
|
|
|
|
this.persistAllKeyrings() |
|
|
|
|
|
|
|
.then(() => { |
|
|
|
this.emit('update') |
|
|
|
this.emit('update') |
|
|
|
cb() |
|
|
|
cb(err, this.getState()) |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
@ -117,14 +120,20 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
const shouldMigrate = !!this.configManager.getWallet() && !this.configManager.getVault() |
|
|
|
const shouldMigrate = !!this.configManager.getWallet() && !this.configManager.getVault() |
|
|
|
return this.idStoreMigrator.migratedVaultForPassword(password) |
|
|
|
return this.idStoreMigrator.migratedVaultForPassword(password) |
|
|
|
.then((serialized) => { |
|
|
|
.then((serialized) => { |
|
|
|
if (serialized && shouldMigrate) { |
|
|
|
|
|
|
|
this.password = password |
|
|
|
this.password = password |
|
|
|
const keyring = this.restoreKeyring(serialized) |
|
|
|
|
|
|
|
this.keyrings.push(keyring) |
|
|
|
if (serialized && shouldMigrate) { |
|
|
|
this.configManager.setSelectedAccount(keyring.getAccounts()[0]) |
|
|
|
return this.restoreKeyring(serialized) |
|
|
|
|
|
|
|
.then((keyring) => { |
|
|
|
|
|
|
|
return keyring.getAccounts() |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.then((accounts) => { |
|
|
|
|
|
|
|
this.configManager.setSelectedAccount(accounts[0]) |
|
|
|
return this.persistAllKeyrings() |
|
|
|
return this.persistAllKeyrings() |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return Promise.resolve() |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -145,7 +154,9 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
createFirstKeyTree (password, cb) { |
|
|
|
createFirstKeyTree (password, cb) { |
|
|
|
this.clearKeyrings() |
|
|
|
this.clearKeyrings() |
|
|
|
this.addNewKeyring('HD Key Tree', {numberOfAccounts: 1}, (err) => { |
|
|
|
this.addNewKeyring('HD Key Tree', {numberOfAccounts: 1}, (err) => { |
|
|
|
const accounts = this.keyrings[0].getAccounts() |
|
|
|
if (err) return cb(err) |
|
|
|
|
|
|
|
this.keyrings[0].getAccounts() |
|
|
|
|
|
|
|
.then((accounts) => { |
|
|
|
const firstAccount = accounts[0] |
|
|
|
const firstAccount = accounts[0] |
|
|
|
const hexAccount = normalize(firstAccount) |
|
|
|
const hexAccount = normalize(firstAccount) |
|
|
|
this.configManager.setSelectedAccount(firstAccount) |
|
|
|
this.configManager.setSelectedAccount(firstAccount) |
|
|
@ -153,9 +164,10 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
this.placeSeedWords() |
|
|
|
this.placeSeedWords() |
|
|
|
this.emit('newAccount', hexAccount) |
|
|
|
this.emit('newAccount', hexAccount) |
|
|
|
this.setupAccounts(accounts) |
|
|
|
this.setupAccounts(accounts) |
|
|
|
this.persistAllKeyrings() |
|
|
|
return this.persistAllKeyrings() |
|
|
|
|
|
|
|
}) |
|
|
|
.then(() => { |
|
|
|
.then(() => { |
|
|
|
cb(err) |
|
|
|
cb() |
|
|
|
}) |
|
|
|
}) |
|
|
|
.catch((reason) => { |
|
|
|
.catch((reason) => { |
|
|
|
cb(reason) |
|
|
|
cb(reason) |
|
|
@ -165,13 +177,17 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
|
|
|
|
|
|
|
|
placeSeedWords (cb) { |
|
|
|
placeSeedWords (cb) { |
|
|
|
const firstKeyring = this.keyrings[0] |
|
|
|
const firstKeyring = this.keyrings[0] |
|
|
|
const seedWords = firstKeyring.serialize().mnemonic |
|
|
|
firstKeyring.serialize() |
|
|
|
|
|
|
|
.then((serialized) => { |
|
|
|
|
|
|
|
const seedWords = serialized.mnemonic |
|
|
|
this.configManager.setSeedWords(seedWords) |
|
|
|
this.configManager.setSeedWords(seedWords) |
|
|
|
|
|
|
|
|
|
|
|
if (cb) { |
|
|
|
if (cb) { |
|
|
|
cb() |
|
|
|
cb() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.emit('update') |
|
|
|
this.emit('update') |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
submitPassword (password, cb) { |
|
|
|
submitPassword (password, cb) { |
|
|
@ -193,24 +209,35 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
addNewKeyring (type, opts, cb) { |
|
|
|
addNewKeyring (type, opts, cb) { |
|
|
|
const Keyring = this.getKeyringClassForType(type) |
|
|
|
const Keyring = this.getKeyringClassForType(type) |
|
|
|
const keyring = new Keyring(opts) |
|
|
|
const keyring = new Keyring(opts) |
|
|
|
const accounts = keyring.getAccounts() |
|
|
|
return keyring.getAccounts() |
|
|
|
|
|
|
|
.then((accounts) => { |
|
|
|
this.keyrings.push(keyring) |
|
|
|
this.keyrings.push(keyring) |
|
|
|
this.setupAccounts(accounts) |
|
|
|
return this.setupAccounts(accounts) |
|
|
|
this.persistAllKeyrings() |
|
|
|
}).then(() => { |
|
|
|
.then(() => { |
|
|
|
return this.persistAllKeyrings() |
|
|
|
cb() |
|
|
|
}).then(() => { |
|
|
|
|
|
|
|
if (cb) { |
|
|
|
|
|
|
|
cb(null, keyring) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return keyring |
|
|
|
}) |
|
|
|
}) |
|
|
|
.catch((reason) => { |
|
|
|
.catch((reason) => { |
|
|
|
|
|
|
|
if (cb) { |
|
|
|
cb(reason) |
|
|
|
cb(reason) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return reason |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
addNewAccount (keyRingNum = 0, cb) { |
|
|
|
addNewAccount (keyRingNum = 0, cb) { |
|
|
|
const ring = this.keyrings[keyRingNum] |
|
|
|
const ring = this.keyrings[keyRingNum] |
|
|
|
const accounts = ring.addAccounts(1) |
|
|
|
return ring.addAccounts(1) |
|
|
|
this.setupAccounts(accounts) |
|
|
|
.then((accounts) => { |
|
|
|
this.persistAllKeyrings() |
|
|
|
return this.setupAccounts(accounts) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.then(() => { |
|
|
|
|
|
|
|
return this.persistAllKeyrings() |
|
|
|
|
|
|
|
}) |
|
|
|
.then(() => { |
|
|
|
.then(() => { |
|
|
|
cb() |
|
|
|
cb() |
|
|
|
}) |
|
|
|
}) |
|
|
@ -220,10 +247,13 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setupAccounts (accounts) { |
|
|
|
setupAccounts (accounts) { |
|
|
|
var arr = accounts || this.getAccounts() |
|
|
|
return this.getAccounts() |
|
|
|
|
|
|
|
.then((loadedAccounts) => { |
|
|
|
|
|
|
|
const arr = accounts || loadedAccounts |
|
|
|
arr.forEach((account) => { |
|
|
|
arr.forEach((account) => { |
|
|
|
this.getBalanceAndNickname(account) |
|
|
|
this.getBalanceAndNickname(account) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Takes an account address and an iterator representing
|
|
|
|
// Takes an account address and an iterator representing
|
|
|
@ -259,13 +289,19 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
persistAllKeyrings () { |
|
|
|
persistAllKeyrings () { |
|
|
|
const serialized = this.keyrings.map((keyring) => { |
|
|
|
return Promise.all(this.keyrings.map((keyring) => { |
|
|
|
|
|
|
|
return Promise.all([keyring.type, keyring.serialize()]) |
|
|
|
|
|
|
|
.then((serializedKeyringArray) => { |
|
|
|
|
|
|
|
// Label the output values on each serialized Keyring:
|
|
|
|
return { |
|
|
|
return { |
|
|
|
type: keyring.type, |
|
|
|
type: serializedKeyringArray[0], |
|
|
|
data: keyring.serialize(), |
|
|
|
data: serializedKeyringArray[1], |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
return this.encryptor.encrypt(this.password, serialized) |
|
|
|
})) |
|
|
|
|
|
|
|
.then((serializedKeyrings) => { |
|
|
|
|
|
|
|
return this.encryptor.encrypt(this.password, serializedKeyrings) |
|
|
|
|
|
|
|
}) |
|
|
|
.then((encryptedString) => { |
|
|
|
.then((encryptedString) => { |
|
|
|
this.configManager.setVault(encryptedString) |
|
|
|
this.configManager.setVault(encryptedString) |
|
|
|
return true |
|
|
|
return true |
|
|
@ -286,13 +322,15 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
const { type, data } = serialized |
|
|
|
const { type, data } = serialized |
|
|
|
const Keyring = this.getKeyringClassForType(type) |
|
|
|
const Keyring = this.getKeyringClassForType(type) |
|
|
|
const keyring = new Keyring() |
|
|
|
const keyring = new Keyring() |
|
|
|
keyring.deserialize(data) |
|
|
|
return keyring.deserialize(data) |
|
|
|
|
|
|
|
.then(() => { |
|
|
|
const accounts = keyring.getAccounts() |
|
|
|
return keyring.getAccounts() |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.then((accounts) => { |
|
|
|
this.setupAccounts(accounts) |
|
|
|
this.setupAccounts(accounts) |
|
|
|
|
|
|
|
|
|
|
|
this.keyrings.push(keyring) |
|
|
|
this.keyrings.push(keyring) |
|
|
|
return keyring |
|
|
|
return keyring |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getKeyringClassForType (type) { |
|
|
|
getKeyringClassForType (type) { |
|
|
@ -308,10 +346,10 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
|
|
|
|
|
|
|
|
getAccounts () { |
|
|
|
getAccounts () { |
|
|
|
const keyrings = this.keyrings || [] |
|
|
|
const keyrings = this.keyrings || [] |
|
|
|
return keyrings.map(kr => kr.getAccounts()) |
|
|
|
return Promise.all(keyrings.map(kr => kr.getAccounts()) |
|
|
|
.reduce((res, arr) => { |
|
|
|
.reduce((res, arr) => { |
|
|
|
return res.concat(arr) |
|
|
|
return res.concat(arr) |
|
|
|
}, []) |
|
|
|
}, [])) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setSelectedAccount (address, cb) { |
|
|
|
setSelectedAccount (address, cb) { |
|
|
@ -514,9 +552,9 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
txParams.gasLimit = normalize(txParams.gasLimit || txParams.gas) |
|
|
|
txParams.gasLimit = normalize(txParams.gasLimit || txParams.gas) |
|
|
|
txParams.nonce = normalize(txParams.nonce) |
|
|
|
txParams.nonce = normalize(txParams.nonce) |
|
|
|
|
|
|
|
|
|
|
|
let tx = new Transaction(txParams) |
|
|
|
const tx = new Transaction(txParams) |
|
|
|
tx = keyring.signTransaction(address, tx) |
|
|
|
keyring.signTransaction(address, tx) |
|
|
|
|
|
|
|
.then((tx) => { |
|
|
|
// Add the tx hash to the persisted meta-tx object
|
|
|
|
// Add the tx hash to the persisted meta-tx object
|
|
|
|
var txHash = ethUtil.bufferToHex(tx.hash()) |
|
|
|
var txHash = ethUtil.bufferToHex(tx.hash()) |
|
|
|
var metaTx = this.configManager.getTx(txParams.metamaskId) |
|
|
|
var metaTx = this.configManager.getTx(txParams.metamaskId) |
|
|
@ -526,6 +564,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
// return raw serialized tx
|
|
|
|
// return raw serialized tx
|
|
|
|
var rawTx = ethUtil.bufferToHex(tx.serialize()) |
|
|
|
var rawTx = ethUtil.bufferToHex(tx.serialize()) |
|
|
|
cb(null, rawTx) |
|
|
|
cb(null, rawTx) |
|
|
|
|
|
|
|
}) |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
cb(e) |
|
|
|
cb(e) |
|
|
|
} |
|
|
|
} |
|
|
@ -535,8 +574,11 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
try { |
|
|
|
try { |
|
|
|
const keyring = this.getKeyringForAccount(msgParams.from) |
|
|
|
const keyring = this.getKeyringForAccount(msgParams.from) |
|
|
|
const address = normalize(msgParams.from) |
|
|
|
const address = normalize(msgParams.from) |
|
|
|
const rawSig = keyring.signMessage(address, msgParams.data) |
|
|
|
return keyring.signMessage(address, msgParams.data) |
|
|
|
|
|
|
|
.then((rawSig) => { |
|
|
|
cb(null, rawSig) |
|
|
|
cb(null, rawSig) |
|
|
|
|
|
|
|
return rawSig |
|
|
|
|
|
|
|
}) |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
cb(e) |
|
|
|
cb(e) |
|
|
|
} |
|
|
|
} |
|
|
@ -567,10 +609,14 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
exportAccount (address, cb) { |
|
|
|
exportAccount (address, cb) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
const keyring = this.getKeyringForAccount(address) |
|
|
|
const keyring = this.getKeyringForAccount(address) |
|
|
|
const privateKey = keyring.exportAccount(normalize(address)) |
|
|
|
return keyring.exportAccount(normalize(address)) |
|
|
|
|
|
|
|
.then((privateKey) => { |
|
|
|
cb(null, privateKey) |
|
|
|
cb(null, privateKey) |
|
|
|
|
|
|
|
return privateKey |
|
|
|
|
|
|
|
}) |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
cb(e) |
|
|
|
cb(e) |
|
|
|
|
|
|
|
return Promise.reject(e) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|