|
|
@ -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() |
|
|
|
this.emit('update') |
|
|
|
.then(() => { |
|
|
|
cb() |
|
|
|
this.emit('update') |
|
|
|
|
|
|
|
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) => { |
|
|
|
|
|
|
|
this.password = password |
|
|
|
|
|
|
|
|
|
|
|
if (serialized && shouldMigrate) { |
|
|
|
if (serialized && shouldMigrate) { |
|
|
|
this.password = password |
|
|
|
return this.restoreKeyring(serialized) |
|
|
|
const keyring = this.restoreKeyring(serialized) |
|
|
|
.then((keyring) => { |
|
|
|
this.keyrings.push(keyring) |
|
|
|
return keyring.getAccounts() |
|
|
|
this.configManager.setSelectedAccount(keyring.getAccounts()[0]) |
|
|
|
}) |
|
|
|
return this.persistAllKeyrings() |
|
|
|
.then((accounts) => { |
|
|
|
|
|
|
|
this.configManager.setSelectedAccount(accounts[0]) |
|
|
|
|
|
|
|
return this.persistAllKeyrings() |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return Promise.resolve() |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -145,17 +154,20 @@ 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) |
|
|
|
const firstAccount = accounts[0] |
|
|
|
this.keyrings[0].getAccounts() |
|
|
|
const hexAccount = normalize(firstAccount) |
|
|
|
.then((accounts) => { |
|
|
|
this.configManager.setSelectedAccount(firstAccount) |
|
|
|
const firstAccount = accounts[0] |
|
|
|
|
|
|
|
const hexAccount = normalize(firstAccount) |
|
|
|
|
|
|
|
this.configManager.setSelectedAccount(firstAccount) |
|
|
|
|
|
|
|
|
|
|
|
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() |
|
|
|
this.configManager.setSeedWords(seedWords) |
|
|
|
.then((serialized) => { |
|
|
|
|
|
|
|
const seedWords = serialized.mnemonic |
|
|
|
|
|
|
|
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) => { |
|
|
|
cb(reason) |
|
|
|
if (cb) { |
|
|
|
|
|
|
|
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,9 +247,12 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setupAccounts (accounts) { |
|
|
|
setupAccounts (accounts) { |
|
|
|
var arr = accounts || this.getAccounts() |
|
|
|
return this.getAccounts() |
|
|
|
arr.forEach((account) => { |
|
|
|
.then((loadedAccounts) => { |
|
|
|
this.getBalanceAndNickname(account) |
|
|
|
const arr = accounts || loadedAccounts |
|
|
|
|
|
|
|
arr.forEach((account) => { |
|
|
|
|
|
|
|
this.getBalanceAndNickname(account) |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -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 { |
|
|
|
return Promise.all([keyring.type, keyring.serialize()]) |
|
|
|
type: keyring.type, |
|
|
|
.then((serializedKeyringArray) => { |
|
|
|
data: keyring.serialize(), |
|
|
|
// Label the output values on each serialized Keyring:
|
|
|
|
} |
|
|
|
return { |
|
|
|
|
|
|
|
type: serializedKeyringArray[0], |
|
|
|
|
|
|
|
data: serializedKeyringArray[1], |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
.then((serializedKeyrings) => { |
|
|
|
|
|
|
|
return this.encryptor.encrypt(this.password, serializedKeyrings) |
|
|
|
}) |
|
|
|
}) |
|
|
|
return this.encryptor.encrypt(this.password, serialized) |
|
|
|
|
|
|
|
.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() |
|
|
|
this.setupAccounts(accounts) |
|
|
|
}) |
|
|
|
|
|
|
|
.then((accounts) => { |
|
|
|
this.keyrings.push(keyring) |
|
|
|
this.setupAccounts(accounts) |
|
|
|
return keyring |
|
|
|
this.keyrings.push(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,18 +552,19 @@ 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) |
|
|
|
metaTx.hash = txHash |
|
|
|
metaTx.hash = txHash |
|
|
|
this.configManager.updateTx(metaTx) |
|
|
|
this.configManager.updateTx(metaTx) |
|
|
|
|
|
|
|
|
|
|
|
// 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) |
|
|
|
cb(null, rawSig) |
|
|
|
.then((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)) |
|
|
|
cb(null, privateKey) |
|
|
|
.then((privateKey) => { |
|
|
|
|
|
|
|
cb(null, privateKey) |
|
|
|
|
|
|
|
return privateKey |
|
|
|
|
|
|
|
}) |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
cb(e) |
|
|
|
cb(e) |
|
|
|
|
|
|
|
return Promise.reject(e) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|