|
|
@ -46,7 +46,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getState() { |
|
|
|
getState () { |
|
|
|
const configManager = this.configManager |
|
|
|
const configManager = this.configManager |
|
|
|
const address = configManager.getSelectedAccount() |
|
|
|
const address = configManager.getSelectedAccount() |
|
|
|
const wallet = configManager.getWallet() // old style vault
|
|
|
|
const wallet = configManager.getWallet() // old style vault
|
|
|
@ -72,18 +72,18 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setStore(ethStore) { |
|
|
|
setStore (ethStore) { |
|
|
|
this.ethStore = ethStore |
|
|
|
this.ethStore = ethStore |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
createNewVaultAndKeychain(password, entropy, cb) { |
|
|
|
createNewVaultAndKeychain (password, entropy, cb) { |
|
|
|
this.createNewVault(password, entropy, (err) => { |
|
|
|
this.createNewVault(password, entropy, (err) => { |
|
|
|
if (err) return cb(err) |
|
|
|
if (err) return cb(err) |
|
|
|
this.createFirstKeyTree(password, cb) |
|
|
|
this.createFirstKeyTree(password, cb) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
createNewVaultAndRestore(password, seed, cb) { |
|
|
|
createNewVaultAndRestore (password, seed, cb) { |
|
|
|
if (typeof password !== 'string') { |
|
|
|
if (typeof password !== 'string') { |
|
|
|
return cb('Password must be text.') |
|
|
|
return cb('Password must be text.') |
|
|
|
} |
|
|
|
} |
|
|
@ -114,7 +114,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
migrateAndGetKey(password) { |
|
|
|
migrateAndGetKey (password) { |
|
|
|
let key |
|
|
|
let key |
|
|
|
const shouldMigrate = !!this.configManager.getWallet() && !this.configManager.getVault() |
|
|
|
const shouldMigrate = !!this.configManager.getWallet() && !this.configManager.getVault() |
|
|
|
|
|
|
|
|
|
|
@ -134,7 +134,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
createNewVault(password, entropy, cb) { |
|
|
|
createNewVault (password, entropy, cb) { |
|
|
|
const configManager = this.configManager |
|
|
|
const configManager = this.configManager |
|
|
|
const salt = this.encryptor.generateSalt() |
|
|
|
const salt = this.encryptor.generateSalt() |
|
|
|
configManager.setSalt(salt) |
|
|
|
configManager.setSalt(salt) |
|
|
@ -151,7 +151,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
createFirstKeyTree(password, cb) { |
|
|
|
createFirstKeyTree (password, cb) { |
|
|
|
this.clearKeyrings() |
|
|
|
this.clearKeyrings() |
|
|
|
this.addNewKeyring('HD Key Tree', {n: 1}, (err) => { |
|
|
|
this.addNewKeyring('HD Key Tree', {n: 1}, (err) => { |
|
|
|
const firstKeyring = this.keyrings[0] |
|
|
|
const firstKeyring = this.keyrings[0] |
|
|
@ -179,7 +179,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
this.configManager.setSeedWords(seedWords) |
|
|
|
this.configManager.setSeedWords(seedWords) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
submitPassword(password, cb) { |
|
|
|
submitPassword (password, cb) { |
|
|
|
this.migrateAndGetKey(password) |
|
|
|
this.migrateAndGetKey(password) |
|
|
|
.then((key) => { |
|
|
|
.then((key) => { |
|
|
|
return this.unlockKeyrings(key) |
|
|
|
return this.unlockKeyrings(key) |
|
|
@ -196,7 +196,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
loadKey(password) { |
|
|
|
loadKey (password) { |
|
|
|
const salt = this.configManager.getSalt() || this.encryptor.generateSalt() |
|
|
|
const salt = this.configManager.getSalt() || this.encryptor.generateSalt() |
|
|
|
return this.encryptor.keyFromPassword(password + salt) |
|
|
|
return this.encryptor.keyFromPassword(password + salt) |
|
|
|
.then((key) => { |
|
|
|
.then((key) => { |
|
|
@ -206,7 +206,7 @@ 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() |
|
|
|
const accounts = keyring.getAccounts() |
|
|
@ -222,7 +222,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
addNewAccount(keyRingNum = 0, cb) { |
|
|
|
addNewAccount (keyRingNum = 0, cb) { |
|
|
|
const ring = this.keyrings[keyRingNum] |
|
|
|
const ring = this.keyrings[keyRingNum] |
|
|
|
const accounts = ring.addAccounts(1) |
|
|
|
const accounts = ring.addAccounts(1) |
|
|
|
this.setupAccounts(accounts) |
|
|
|
this.setupAccounts(accounts) |
|
|
@ -235,7 +235,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setupAccounts(accounts) { |
|
|
|
setupAccounts (accounts) { |
|
|
|
var arr = accounts || this.getAccounts() |
|
|
|
var arr = accounts || this.getAccounts() |
|
|
|
arr.forEach((account) => { |
|
|
|
arr.forEach((account) => { |
|
|
|
this.loadBalanceAndNickname(account) |
|
|
|
this.loadBalanceAndNickname(account) |
|
|
@ -244,13 +244,13 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
|
|
|
|
|
|
|
|
// Takes an account address and an iterator representing
|
|
|
|
// Takes an account address and an iterator representing
|
|
|
|
// the current number of named accounts.
|
|
|
|
// the current number of named accounts.
|
|
|
|
loadBalanceAndNickname(account) { |
|
|
|
loadBalanceAndNickname (account) { |
|
|
|
const address = normalize(account) |
|
|
|
const address = normalize(account) |
|
|
|
this.ethStore.addAccount(address) |
|
|
|
this.ethStore.addAccount(address) |
|
|
|
this.createNickname(address) |
|
|
|
this.createNickname(address) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
createNickname(address) { |
|
|
|
createNickname (address) { |
|
|
|
const hexAddress = normalize(address) |
|
|
|
const hexAddress = normalize(address) |
|
|
|
var i = Object.keys(this.identities).length |
|
|
|
var i = Object.keys(this.identities).length |
|
|
|
const oldNickname = this.configManager.nicknameForWallet(address) |
|
|
|
const oldNickname = this.configManager.nicknameForWallet(address) |
|
|
@ -274,7 +274,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
persistAllKeyrings() { |
|
|
|
persistAllKeyrings () { |
|
|
|
const serialized = this.keyrings.map((k) => { |
|
|
|
const serialized = this.keyrings.map((k) => { |
|
|
|
return { |
|
|
|
return { |
|
|
|
type: k.type, |
|
|
|
type: k.type, |
|
|
@ -289,7 +289,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unlockKeyrings(key) { |
|
|
|
unlockKeyrings (key) { |
|
|
|
const encryptedVault = this.configManager.getVault() |
|
|
|
const encryptedVault = this.configManager.getVault() |
|
|
|
return this.encryptor.decryptWithKey(key, encryptedVault) |
|
|
|
return this.encryptor.decryptWithKey(key, encryptedVault) |
|
|
|
.then((vault) => { |
|
|
|
.then((vault) => { |
|
|
@ -298,7 +298,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
restoreKeyring(serialized) { |
|
|
|
restoreKeyring (serialized) { |
|
|
|
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() |
|
|
@ -311,7 +311,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
return keyring |
|
|
|
return keyring |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getKeyringClassForType(type) { |
|
|
|
getKeyringClassForType (type) { |
|
|
|
const Keyring = this.keyringTypes.reduce((res, kr) => { |
|
|
|
const Keyring = this.keyringTypes.reduce((res, kr) => { |
|
|
|
if (kr.type() === type) { |
|
|
|
if (kr.type() === type) { |
|
|
|
return kr |
|
|
|
return kr |
|
|
@ -322,7 +322,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
return Keyring |
|
|
|
return Keyring |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getAccounts() { |
|
|
|
getAccounts () { |
|
|
|
const keyrings = this.keyrings || [] |
|
|
|
const keyrings = this.keyrings || [] |
|
|
|
return keyrings.map(kr => kr.getAccounts()) |
|
|
|
return keyrings.map(kr => kr.getAccounts()) |
|
|
|
.reduce((res, arr) => { |
|
|
|
.reduce((res, arr) => { |
|
|
@ -330,13 +330,13 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
}, []) |
|
|
|
}, []) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setSelectedAddress(address, cb) { |
|
|
|
setSelectedAddress (address, cb) { |
|
|
|
var addr = normalize(address) |
|
|
|
var addr = normalize(address) |
|
|
|
this.configManager.setSelectedAccount(addr) |
|
|
|
this.configManager.setSelectedAccount(addr) |
|
|
|
cb(null, addr) |
|
|
|
cb(null, addr) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
addUnconfirmedTransaction(txParams, onTxDoneCb, cb) { |
|
|
|
addUnconfirmedTransaction (txParams, onTxDoneCb, cb) { |
|
|
|
var self = this |
|
|
|
var self = this |
|
|
|
const configManager = this.configManager |
|
|
|
const configManager = this.configManager |
|
|
|
|
|
|
|
|
|
|
@ -369,7 +369,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
], didComplete) |
|
|
|
], didComplete) |
|
|
|
|
|
|
|
|
|
|
|
// perform static analyis on the target contract code
|
|
|
|
// perform static analyis on the target contract code
|
|
|
|
function analyzeForDelegateCall(cb){ |
|
|
|
function analyzeForDelegateCall (cb) { |
|
|
|
if (txParams.to) { |
|
|
|
if (txParams.to) { |
|
|
|
query.getCode(txParams.to, function (err, result) { |
|
|
|
query.getCode(txParams.to, function (err, result) { |
|
|
|
if (err) return cb(err) |
|
|
|
if (err) return cb(err) |
|
|
@ -388,8 +388,8 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function estimateGas(cb){ |
|
|
|
function estimateGas (cb) { |
|
|
|
query.estimateGas(txParams, function(err, result){ |
|
|
|
query.estimateGas(txParams, function (err, result) { |
|
|
|
if (err) return cb(err) |
|
|
|
if (err) return cb(err) |
|
|
|
txData.estimatedGas = self.addGasBuffer(result) |
|
|
|
txData.estimatedGas = self.addGasBuffer(result) |
|
|
|
cb() |
|
|
|
cb() |
|
|
@ -406,7 +406,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
addUnconfirmedMessage(msgParams, cb) { |
|
|
|
addUnconfirmedMessage (msgParams, cb) { |
|
|
|
// create txData obj with parameters and meta data
|
|
|
|
// create txData obj with parameters and meta data
|
|
|
|
var time = (new Date()).getTime() |
|
|
|
var time = (new Date()).getTime() |
|
|
|
var msgId = createId() |
|
|
|
var msgId = createId() |
|
|
@ -428,7 +428,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
return msgId |
|
|
|
return msgId |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
approveTransaction(txId, cb) { |
|
|
|
approveTransaction (txId, cb) { |
|
|
|
const configManager = this.configManager |
|
|
|
const configManager = this.configManager |
|
|
|
var approvalCb = this._unconfTxCbs[txId] || noop |
|
|
|
var approvalCb = this._unconfTxCbs[txId] || noop |
|
|
|
|
|
|
|
|
|
|
@ -441,7 +441,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
this.emit('update') |
|
|
|
this.emit('update') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cancelTransaction(txId, cb) { |
|
|
|
cancelTransaction (txId, cb) { |
|
|
|
const configManager = this.configManager |
|
|
|
const configManager = this.configManager |
|
|
|
var approvalCb = this._unconfTxCbs[txId] || noop |
|
|
|
var approvalCb = this._unconfTxCbs[txId] || noop |
|
|
|
|
|
|
|
|
|
|
@ -456,7 +456,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
signTransaction(txParams, cb) { |
|
|
|
signTransaction (txParams, cb) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
const address = normalize(txParams.from) |
|
|
|
const address = normalize(txParams.from) |
|
|
|
const keyring = this.getKeyringForAccount(address) |
|
|
|
const keyring = this.getKeyringForAccount(address) |
|
|
@ -492,7 +492,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
signMessage(msgParams, cb) { |
|
|
|
signMessage (msgParams, cb) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
const keyring = this.getKeyringForAccount(msgParams.from) |
|
|
|
const keyring = this.getKeyringForAccount(msgParams.from) |
|
|
|
const address = normalize(msgParams.from) |
|
|
|
const address = normalize(msgParams.from) |
|
|
@ -503,7 +503,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getKeyringForAccount(address) { |
|
|
|
getKeyringForAccount (address) { |
|
|
|
const hexed = normalize(address) |
|
|
|
const hexed = normalize(address) |
|
|
|
return this.keyrings.find((ring) => { |
|
|
|
return this.keyrings.find((ring) => { |
|
|
|
return ring.getAccounts() |
|
|
|
return ring.getAccounts() |
|
|
@ -512,19 +512,19 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cancelMessage(msgId, cb) { |
|
|
|
cancelMessage (msgId, cb) { |
|
|
|
if (cb && typeof cb === 'function') { |
|
|
|
if (cb && typeof cb === 'function') { |
|
|
|
cb() |
|
|
|
cb() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setLocked(cb) { |
|
|
|
setLocked (cb) { |
|
|
|
this.key = null |
|
|
|
this.key = null |
|
|
|
this.keyrings = [] |
|
|
|
this.keyrings = [] |
|
|
|
cb() |
|
|
|
cb() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
exportAccount(address, cb) { |
|
|
|
exportAccount (address, cb) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
const keyring = this.getKeyringForAccount(address) |
|
|
|
const keyring = this.getKeyringForAccount(address) |
|
|
|
const privateKey = keyring.exportAccount(normalize(address)) |
|
|
|
const privateKey = keyring.exportAccount(normalize(address)) |
|
|
@ -534,19 +534,19 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
addGasBuffer(gas) { |
|
|
|
addGasBuffer (gas) { |
|
|
|
const gasBuffer = new BN('100000', 10) |
|
|
|
const gasBuffer = new BN('100000', 10) |
|
|
|
const bnGas = new BN(ethUtil.stripHexPrefix(gas), 16) |
|
|
|
const bnGas = new BN(ethUtil.stripHexPrefix(gas), 16) |
|
|
|
const correct = bnGas.add(gasBuffer) |
|
|
|
const correct = bnGas.add(gasBuffer) |
|
|
|
return ethUtil.addHexPrefix(correct.toString(16)) |
|
|
|
return ethUtil.addHexPrefix(correct.toString(16)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
clearSeedWordCache(cb) { |
|
|
|
clearSeedWordCache (cb) { |
|
|
|
this.configManager.setSeedWords(null) |
|
|
|
this.configManager.setSeedWords(null) |
|
|
|
cb(null, this.configManager.getSelectedAccount()) |
|
|
|
cb(null, this.configManager.getSelectedAccount()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
clearKeyrings() { |
|
|
|
clearKeyrings () { |
|
|
|
let accounts |
|
|
|
let accounts |
|
|
|
try { |
|
|
|
try { |
|
|
|
accounts = Object.keys(this.ethStore._currentState.accounts) |
|
|
|
accounts = Object.keys(this.ethStore._currentState.accounts) |
|
|
@ -564,7 +564,7 @@ module.exports = class KeyringController extends EventEmitter { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function normalize(address) { |
|
|
|
function normalize (address) { |
|
|
|
if (!address) return |
|
|
|
if (!address) return |
|
|
|
return ethUtil.addHexPrefix(address.toLowerCase()) |
|
|
|
return ethUtil.addHexPrefix(address.toLowerCase()) |
|
|
|
} |
|
|
|
} |
|
|
|