|
|
@ -3,7 +3,7 @@ const inherits = require('util').inherits |
|
|
|
const async = require('async') |
|
|
|
const async = require('async') |
|
|
|
const ethUtil = require('ethereumjs-util') |
|
|
|
const ethUtil = require('ethereumjs-util') |
|
|
|
const EthQuery = require('eth-query') |
|
|
|
const EthQuery = require('eth-query') |
|
|
|
const LightwalletKeyStore = require('eth-lightwallet').keystore |
|
|
|
const KeyStore = require('eth-lightwallet').keystore |
|
|
|
const clone = require('clone') |
|
|
|
const clone = require('clone') |
|
|
|
const extend = require('xtend') |
|
|
|
const extend = require('xtend') |
|
|
|
const createId = require('web3-provider-engine/util/random-id') |
|
|
|
const createId = require('web3-provider-engine/util/random-id') |
|
|
@ -50,15 +50,16 @@ IdentityStore.prototype.createNewVault = function (password, entropy, cb) { |
|
|
|
if (serializedKeystore) { |
|
|
|
if (serializedKeystore) { |
|
|
|
this.configManager.setData({}) |
|
|
|
this.configManager.setData({}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this._createIdmgmt(password, null, entropy, (err) => { |
|
|
|
this._createIdmgmt(password, null, entropy, (err) => { |
|
|
|
if (err) return cb(err) |
|
|
|
if (err) return cb(err) |
|
|
|
|
|
|
|
|
|
|
|
this._loadIdentities() |
|
|
|
|
|
|
|
this._didUpdate() |
|
|
|
|
|
|
|
this._autoFaucet() |
|
|
|
this._autoFaucet() |
|
|
|
|
|
|
|
|
|
|
|
this.configManager.setShowSeedWords(true) |
|
|
|
this.configManager.setShowSeedWords(true) |
|
|
|
var seedWords = this._idmgmt.getSeed() |
|
|
|
var seedWords = this._idmgmt.getSeed() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cb(null, seedWords) |
|
|
|
cb(null, seedWords) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
@ -75,7 +76,6 @@ IdentityStore.prototype.recoverFromSeed = function (password, seed, cb) { |
|
|
|
if (err) return cb(err) |
|
|
|
if (err) return cb(err) |
|
|
|
|
|
|
|
|
|
|
|
this._loadIdentities() |
|
|
|
this._loadIdentities() |
|
|
|
this._didUpdate() |
|
|
|
|
|
|
|
cb(null, this.getState()) |
|
|
|
cb(null, this.getState()) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
@ -125,7 +125,7 @@ IdentityStore.prototype.getSelectedAddress = function () { |
|
|
|
return configManager.getSelectedAccount() |
|
|
|
return configManager.getSelectedAccount() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
IdentityStore.prototype.setSelectedAddress = function (address, cb) { |
|
|
|
IdentityStore.prototype.setSelectedAddressSync = function (address) { |
|
|
|
const configManager = this.configManager |
|
|
|
const configManager = this.configManager |
|
|
|
if (!address) { |
|
|
|
if (!address) { |
|
|
|
var addresses = this._getAddresses() |
|
|
|
var addresses = this._getAddresses() |
|
|
@ -133,7 +133,12 @@ IdentityStore.prototype.setSelectedAddress = function (address, cb) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
configManager.setSelectedAccount(address) |
|
|
|
configManager.setSelectedAccount(address) |
|
|
|
if (cb) return cb(null, address) |
|
|
|
return address |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IdentityStore.prototype.setSelectedAddress = function (address, cb) { |
|
|
|
|
|
|
|
const resultAddress = this.setSelectedAddressSync(address) |
|
|
|
|
|
|
|
if (cb) return cb(null, resultAddress) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
IdentityStore.prototype.revealAccount = function (cb) { |
|
|
|
IdentityStore.prototype.revealAccount = function (cb) { |
|
|
@ -143,6 +148,7 @@ IdentityStore.prototype.revealAccount = function (cb) { |
|
|
|
|
|
|
|
|
|
|
|
keyStore.setDefaultHdDerivationPath(this.hdPathString) |
|
|
|
keyStore.setDefaultHdDerivationPath(this.hdPathString) |
|
|
|
keyStore.generateNewAddress(derivedKey, 1) |
|
|
|
keyStore.generateNewAddress(derivedKey, 1) |
|
|
|
|
|
|
|
|
|
|
|
configManager.setWallet(keyStore.serialize()) |
|
|
|
configManager.setWallet(keyStore.serialize()) |
|
|
|
|
|
|
|
|
|
|
|
this._loadIdentities() |
|
|
|
this._loadIdentities() |
|
|
@ -393,7 +399,6 @@ IdentityStore.prototype._loadIdentities = function () { |
|
|
|
var addresses = this._getAddresses() |
|
|
|
var addresses = this._getAddresses() |
|
|
|
addresses.forEach((address, i) => { |
|
|
|
addresses.forEach((address, i) => { |
|
|
|
// // add to ethStore
|
|
|
|
// // add to ethStore
|
|
|
|
this._ethStore.addAccount(address) |
|
|
|
|
|
|
|
// add to identities
|
|
|
|
// add to identities
|
|
|
|
const defaultLabel = 'Wallet ' + (i + 1) |
|
|
|
const defaultLabel = 'Wallet ' + (i + 1) |
|
|
|
const nickname = configManager.nicknameForWallet(address) |
|
|
|
const nickname = configManager.nicknameForWallet(address) |
|
|
@ -412,7 +417,6 @@ IdentityStore.prototype.saveAccountLabel = function (account, label, cb) { |
|
|
|
configManager.setNicknameForWallet(account, label) |
|
|
|
configManager.setNicknameForWallet(account, label) |
|
|
|
this._loadIdentities() |
|
|
|
this._loadIdentities() |
|
|
|
cb(null, label) |
|
|
|
cb(null, label) |
|
|
|
this._didUpdate() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// mayBeFauceting
|
|
|
|
// mayBeFauceting
|
|
|
@ -436,77 +440,76 @@ IdentityStore.prototype._mayBeFauceting = function (i) { |
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
IdentityStore.prototype.tryPassword = function (password, cb) { |
|
|
|
IdentityStore.prototype.tryPassword = function (password, cb) { |
|
|
|
this._createIdmgmt(password, null, null, cb) |
|
|
|
var serializedKeystore = this.configManager.getWallet() |
|
|
|
} |
|
|
|
var keyStore = KeyStore.deserialize(serializedKeystore) |
|
|
|
|
|
|
|
|
|
|
|
IdentityStore.prototype._createIdmgmt = function (password, seed, entropy, cb) { |
|
|
|
|
|
|
|
const configManager = this.configManager |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var keyStore = null |
|
|
|
keyStore.keyFromPassword(password, (err, pwDerivedKey) => { |
|
|
|
LightwalletKeyStore.deriveKeyFromPassword(password, (err, derivedKey) => { |
|
|
|
|
|
|
|
if (err) return cb(err) |
|
|
|
if (err) return cb(err) |
|
|
|
var serializedKeystore = configManager.getWallet() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (seed) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
keyStore = this._restoreFromSeed(password, seed, derivedKey) |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
return cb(e) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// returning user, recovering from storage
|
|
|
|
|
|
|
|
} else if (serializedKeystore) { |
|
|
|
|
|
|
|
keyStore = LightwalletKeyStore.deserialize(serializedKeystore) |
|
|
|
|
|
|
|
var isCorrect = keyStore.isDerivedKeyCorrect(derivedKey) |
|
|
|
|
|
|
|
if (!isCorrect) return cb(new Error('Lightwallet - password incorrect')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// first time here
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
keyStore = this._createFirstWallet(entropy, derivedKey) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._keyStore = keyStore |
|
|
|
const isCorrect = keyStore.isDerivedKeyCorrect(pwDerivedKey) |
|
|
|
this._idmgmt = new IdManagement({ |
|
|
|
if (!isCorrect) return cb(new Error('Lightwallet - password incorrect')) |
|
|
|
keyStore: keyStore, |
|
|
|
|
|
|
|
derivedKey: derivedKey, |
|
|
|
|
|
|
|
hdPathSTring: this.hdPathString, |
|
|
|
|
|
|
|
configManager: this.configManager, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cb() |
|
|
|
cb() |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
IdentityStore.prototype._restoreFromSeed = function (password, seed, derivedKey) { |
|
|
|
IdentityStore.prototype._createIdmgmt = function (password, seedPhrase, entropy, cb) { |
|
|
|
const configManager = this.configManager |
|
|
|
const opts = { |
|
|
|
var keyStore = new LightwalletKeyStore(seed, derivedKey, this.hdPathString) |
|
|
|
password, |
|
|
|
keyStore.addHdDerivationPath(this.hdPathString, derivedKey, {curve: 'secp256k1', purpose: 'sign'}) |
|
|
|
hdPathString: this.hdPathString, |
|
|
|
keyStore.setDefaultHdDerivationPath(this.hdPathString) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
keyStore.generateNewAddress(derivedKey, 1) |
|
|
|
if (seedPhrase) { |
|
|
|
configManager.setWallet(keyStore.serialize()) |
|
|
|
opts.seedPhrase = seedPhrase |
|
|
|
if (global.METAMASK_DEBUG) { |
|
|
|
|
|
|
|
console.log('restored from seed. saved to keystore') |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return keyStore |
|
|
|
|
|
|
|
|
|
|
|
KeyStore.createVault(opts, (err, keyStore) => { |
|
|
|
|
|
|
|
if (err) return cb(err) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._keyStore = keyStore |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
keyStore.keyFromPassword(password, (err, derivedKey) => { |
|
|
|
|
|
|
|
if (err) return cb(err) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.purgeCache() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
keyStore.addHdDerivationPath(this.hdPathString, derivedKey, {curve: 'secp256k1', purpose: 'sign'}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._createFirstWallet(derivedKey) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._idmgmt = new IdManagement({ |
|
|
|
|
|
|
|
keyStore: keyStore, |
|
|
|
|
|
|
|
derivedKey: derivedKey, |
|
|
|
|
|
|
|
configManager: this.configManager, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.setSelectedAddressSync() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cb() |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
IdentityStore.prototype._createFirstWallet = function (entropy, derivedKey) { |
|
|
|
IdentityStore.prototype.purgeCache = function () { |
|
|
|
const configManager = this.configManager |
|
|
|
this._getAddresses().forEach((address) => { |
|
|
|
var secretSeed = LightwalletKeyStore.generateRandomSeed(entropy) |
|
|
|
this._ethStore.del(address) |
|
|
|
var keyStore = new LightwalletKeyStore(secretSeed, derivedKey, this.hdPathString) |
|
|
|
}) |
|
|
|
keyStore.addHdDerivationPath(this.hdPathString, derivedKey, {curve: 'secp256k1', purpose: 'sign'}) |
|
|
|
} |
|
|
|
keyStore.setDefaultHdDerivationPath(this.hdPathString) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IdentityStore.prototype._createFirstWallet = function (derivedKey) { |
|
|
|
|
|
|
|
const keyStore = this._keyStore |
|
|
|
|
|
|
|
keyStore.setDefaultHdDerivationPath(this.hdPathString) |
|
|
|
keyStore.generateNewAddress(derivedKey, 1) |
|
|
|
keyStore.generateNewAddress(derivedKey, 1) |
|
|
|
configManager.setWallet(keyStore.serialize()) |
|
|
|
this.configManager.setWallet(keyStore.serialize()) |
|
|
|
console.log('saved to keystore') |
|
|
|
var addresses = keyStore.getAddresses() |
|
|
|
return keyStore |
|
|
|
this._ethStore.addAccount(ethUtil.addHexPrefix(addresses[0])) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// get addresses and normalize address hexString
|
|
|
|
// get addresses and normalize address hexString
|
|
|
|
IdentityStore.prototype._getAddresses = function () { |
|
|
|
IdentityStore.prototype._getAddresses = function () { |
|
|
|
return this._keyStore.getAddresses(this.hdPathString).map((address) => { return '0x' + address }) |
|
|
|
return this._keyStore.getAddresses(this.hdPathString).map((address) => { |
|
|
|
|
|
|
|
return ethUtil.addHexPrefix(address) |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
IdentityStore.prototype._autoFaucet = function () { |
|
|
|
IdentityStore.prototype._autoFaucet = function () { |
|
|
|