aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2017-03-17 02:16:03 +0800
committerDan Finlay <dan@danfinlay.com>2017-03-17 02:16:03 +0800
commitb6e2eaf7b1d14fcca8ed614791937a5ccbfc00dd (patch)
tree9e718e5d775371ada9d389233fd72d06721f047e /app
parent00f1a2d78d01301f51157e9c8b8352dc0796d034 (diff)
downloadtangerine-wallet-browser-b6e2eaf7b1d14fcca8ed614791937a5ccbfc00dd.tar
tangerine-wallet-browser-b6e2eaf7b1d14fcca8ed614791937a5ccbfc00dd.tar.gz
tangerine-wallet-browser-b6e2eaf7b1d14fcca8ed614791937a5ccbfc00dd.tar.bz2
tangerine-wallet-browser-b6e2eaf7b1d14fcca8ed614791937a5ccbfc00dd.tar.lz
tangerine-wallet-browser-b6e2eaf7b1d14fcca8ed614791937a5ccbfc00dd.tar.xz
tangerine-wallet-browser-b6e2eaf7b1d14fcca8ed614791937a5ccbfc00dd.tar.zst
tangerine-wallet-browser-b6e2eaf7b1d14fcca8ed614791937a5ccbfc00dd.zip
Remove eth-lightwallet
Diffstat (limited to 'app')
-rw-r--r--app/scripts/lib/id-management.js90
-rw-r--r--app/scripts/lib/idStore-migrator.js80
-rw-r--r--app/scripts/lib/idStore.js343
-rw-r--r--app/scripts/metamask-controller.js38
4 files changed, 1 insertions, 550 deletions
diff --git a/app/scripts/lib/id-management.js b/app/scripts/lib/id-management.js
deleted file mode 100644
index 90b3fdb13..000000000
--- a/app/scripts/lib/id-management.js
+++ /dev/null
@@ -1,90 +0,0 @@
-/* ID Management
- *
- * This module exists to hold the decrypted credentials for the current session.
- * It therefore exposes sign methods, because it is able to perform these
- * with noa dditional authentication, because its very instantiation
- * means the vault is unlocked.
- */
-
-const ethUtil = require('ethereumjs-util')
-const Transaction = require('ethereumjs-tx')
-
-module.exports = IdManagement
-
-function IdManagement (opts) {
- if (!opts) opts = {}
-
- this.keyStore = opts.keyStore
- this.derivedKey = opts.derivedKey
- this.configManager = opts.configManager
- this.hdPathString = "m/44'/60'/0'/0"
-
- this.getAddresses = function () {
- return this.keyStore.getAddresses(this.hdPathString).map(function (address) { return '0x' + address })
- }
-
- this.signTx = function (txParams) {
-
- // normalize values
- txParams.gasPrice = ethUtil.intToHex(txParams.gasPrice)
- txParams.to = ethUtil.addHexPrefix(txParams.to)
- txParams.from = ethUtil.addHexPrefix(txParams.from.toLowerCase())
- txParams.value = ethUtil.addHexPrefix(txParams.value)
- txParams.data = ethUtil.addHexPrefix(txParams.data)
- txParams.gasLimit = ethUtil.addHexPrefix(txParams.gasLimit || txParams.gas)
- txParams.nonce = ethUtil.addHexPrefix(txParams.nonce)
- var tx = new Transaction(txParams)
-
- // sign tx
- var privKeyHex = this.exportPrivateKey(txParams.from)
- var privKey = ethUtil.toBuffer(privKeyHex)
- tx.sign(privKey)
-
- // Add the tx hash to the persisted meta-tx object
- var txHash = ethUtil.bufferToHex(tx.hash())
- var metaTx = this.configManager.getTx(txParams.metamaskId)
- metaTx.hash = txHash
- this.configManager.updateTx(metaTx)
-
- // return raw serialized tx
- var rawTx = ethUtil.bufferToHex(tx.serialize())
- return rawTx
- }
-
- this.signMsg = function (address, message) {
- // sign message
- var privKeyHex = this.exportPrivateKey(address.toLowerCase())
- var privKey = ethUtil.toBuffer(privKeyHex)
- var msgSig = ethUtil.ecsign(new Buffer(message.replace('0x', ''), 'hex'), privKey)
- var rawMsgSig = ethUtil.bufferToHex(concatSig(msgSig.v, msgSig.r, msgSig.s))
- return rawMsgSig
- }
-
- this.getSeed = function () {
- return this.keyStore.getSeed(this.derivedKey)
- }
-
- this.exportPrivateKey = function (address) {
- var privKeyHex = ethUtil.addHexPrefix(this.keyStore.exportPrivateKey(address, this.derivedKey, this.hdPathString))
- return privKeyHex
- }
-}
-
-function padWithZeroes (number, length) {
- var myString = '' + number
- while (myString.length < length) {
- myString = '0' + myString
- }
- return myString
-}
-
-function concatSig (v, r, s) {
- const rSig = ethUtil.fromSigned(r)
- const sSig = ethUtil.fromSigned(s)
- const vSig = ethUtil.bufferToInt(v)
- const rStr = padWithZeroes(ethUtil.toUnsigned(rSig).toString('hex'), 64)
- const sStr = padWithZeroes(ethUtil.toUnsigned(sSig).toString('hex'), 64)
- const vStr = ethUtil.stripHexPrefix(ethUtil.intToHex(vSig))
- return ethUtil.addHexPrefix(rStr.concat(sStr, vStr)).toString('hex')
-}
-
diff --git a/app/scripts/lib/idStore-migrator.js b/app/scripts/lib/idStore-migrator.js
deleted file mode 100644
index 62d21eee7..000000000
--- a/app/scripts/lib/idStore-migrator.js
+++ /dev/null
@@ -1,80 +0,0 @@
-const IdentityStore = require('./idStore')
-const HdKeyring = require('eth-hd-keyring')
-const sigUtil = require('eth-sig-util')
-const normalize = sigUtil.normalize
-const denodeify = require('denodeify')
-
-module.exports = class IdentityStoreMigrator {
-
- constructor ({ configManager }) {
- this.configManager = configManager
- const hasOldVault = this.hasOldVault()
- if (!hasOldVault) {
- this.idStore = new IdentityStore({ configManager })
- }
- }
-
- migratedVaultForPassword (password) {
- const hasOldVault = this.hasOldVault()
- const configManager = this.configManager
-
- if (!this.idStore) {
- this.idStore = new IdentityStore({ configManager })
- }
-
- if (!hasOldVault) {
- return Promise.resolve(null)
- }
-
- const idStore = this.idStore
- const submitPassword = denodeify(idStore.submitPassword.bind(idStore))
-
- return submitPassword(password)
- .then(() => {
- const serialized = this.serializeVault()
- return this.checkForLostAccounts(serialized)
- })
- }
-
- serializeVault () {
- const mnemonic = this.idStore._idmgmt.getSeed()
- const numberOfAccounts = this.idStore._getAddresses().length
-
- return {
- type: 'HD Key Tree',
- data: { mnemonic, numberOfAccounts },
- }
- }
-
- checkForLostAccounts (serialized) {
- const hd = new HdKeyring()
- return hd.deserialize(serialized.data)
- .then((hexAccounts) => {
- const newAccounts = hexAccounts.map(normalize)
- const oldAccounts = this.idStore._getAddresses().map(normalize)
- const lostAccounts = oldAccounts.reduce((result, account) => {
- if (newAccounts.includes(account)) {
- return result
- } else {
- result.push(account)
- return result
- }
- }, [])
-
- return {
- serialized,
- lostAccounts: lostAccounts.map((address) => {
- return {
- address,
- privateKey: this.idStore.exportAccount(address),
- }
- }),
- }
- })
- }
-
- hasOldVault () {
- const wallet = this.configManager.getWallet()
- return wallet
- }
-}
diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js
deleted file mode 100644
index 01474035e..000000000
--- a/app/scripts/lib/idStore.js
+++ /dev/null
@@ -1,343 +0,0 @@
-const EventEmitter = require('events').EventEmitter
-const inherits = require('util').inherits
-const ethUtil = require('ethereumjs-util')
-const KeyStore = require('eth-lightwallet').keystore
-const clone = require('clone')
-const extend = require('xtend')
-const autoFaucet = require('./auto-faucet')
-const DEFAULT_RPC = 'https://testrpc.metamask.io/'
-const IdManagement = require('./id-management')
-
-
-module.exports = IdentityStore
-
-inherits(IdentityStore, EventEmitter)
-function IdentityStore (opts = {}) {
- EventEmitter.call(this)
-
- // we just use the ethStore to auto-add accounts
- this._ethStore = opts.ethStore
- this.configManager = opts.configManager
- // lightwallet key store
- this._keyStore = null
- // lightwallet wrapper
- this._idmgmt = null
-
- this.hdPathString = "m/44'/60'/0'/0"
-
- this._currentState = {
- selectedAddress: null,
- identities: {},
- }
- // not part of serilized metamask state - only kept in memory
-}
-
-//
-// public
-//
-
-IdentityStore.prototype.createNewVault = function (password, cb) {
- delete this._keyStore
- var serializedKeystore = this.configManager.getWallet()
-
- if (serializedKeystore) {
- this.configManager.setData({})
- }
-
- this.purgeCache()
- this._createVault(password, null, (err) => {
- if (err) return cb(err)
-
- this._autoFaucet()
-
- this.configManager.setShowSeedWords(true)
- var seedWords = this._idmgmt.getSeed()
-
- this._loadIdentities()
-
- cb(null, seedWords)
- })
-}
-
-IdentityStore.prototype.recoverSeed = function (cb) {
- this.configManager.setShowSeedWords(true)
- if (!this._idmgmt) return cb(new Error('Unauthenticated. Please sign in.'))
- var seedWords = this._idmgmt.getSeed()
- cb(null, seedWords)
-}
-
-IdentityStore.prototype.recoverFromSeed = function (password, seed, cb) {
- this.purgeCache()
-
- this._createVault(password, seed, (err) => {
- if (err) return cb(err)
-
- this._loadIdentities()
- cb(null, this.getState())
- })
-}
-
-IdentityStore.prototype.setStore = function (store) {
- this._ethStore = store
-}
-
-IdentityStore.prototype.clearSeedWordCache = function (cb) {
- const configManager = this.configManager
- configManager.setShowSeedWords(false)
- cb(null, configManager.getSelectedAccount())
-}
-
-IdentityStore.prototype.getState = function () {
- const configManager = this.configManager
- var seedWords = this.getSeedIfUnlocked()
- return clone(extend(this._currentState, {
- isInitialized: !!configManager.getWallet() && !seedWords,
- isUnlocked: this._isUnlocked(),
- seedWords: seedWords,
- selectedAddress: configManager.getSelectedAccount(),
- }))
-}
-
-IdentityStore.prototype.getSeedIfUnlocked = function () {
- const configManager = this.configManager
- var showSeed = configManager.getShouldShowSeedWords()
- var idmgmt = this._idmgmt
- var shouldShow = showSeed && !!idmgmt
- var seedWords = shouldShow ? idmgmt.getSeed() : null
- return seedWords
-}
-
-IdentityStore.prototype.getSelectedAddress = function () {
- const configManager = this.configManager
- return configManager.getSelectedAccount()
-}
-
-IdentityStore.prototype.setSelectedAddressSync = function (address) {
- const configManager = this.configManager
- if (!address) {
- var addresses = this._getAddresses()
- address = addresses[0]
- }
-
- configManager.setSelectedAccount(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) {
- const derivedKey = this._idmgmt.derivedKey
- const keyStore = this._keyStore
- const configManager = this.configManager
-
- keyStore.setDefaultHdDerivationPath(this.hdPathString)
- keyStore.generateNewAddress(derivedKey, 1)
- const addresses = keyStore.getAddresses()
- const address = addresses[ addresses.length - 1 ]
-
- this._ethStore.addAccount(ethUtil.addHexPrefix(address))
-
- configManager.setWallet(keyStore.serialize())
-
- this._loadIdentities()
- this._didUpdate()
- cb(null)
-}
-
-IdentityStore.prototype.getNetwork = function (err) {
- if (err) {
- this._currentState.network = 'loading'
- this._didUpdate()
- }
-
- this.web3.version.getNetwork((err, network) => {
- if (err) {
- this._currentState.network = 'loading'
- return this._didUpdate()
- }
- if (global.METAMASK_DEBUG) {
- console.log('web3.getNetwork returned ' + network)
- }
- this._currentState.network = network
- this._didUpdate()
- })
-}
-
-IdentityStore.prototype.setLocked = function (cb) {
- delete this._keyStore
- delete this._idmgmt
- cb()
-}
-
-IdentityStore.prototype.submitPassword = function (password, cb) {
- const configManager = this.configManager
- this.tryPassword(password, (err) => {
- if (err) return cb(err)
- // load identities before returning...
- this._loadIdentities()
- cb(null, configManager.getSelectedAccount())
- })
-}
-
-IdentityStore.prototype.exportAccount = function (address, cb) {
- var privateKey = this._idmgmt.exportPrivateKey(address)
- if (cb) cb(null, privateKey)
- return privateKey
-}
-
-// private
-//
-
-IdentityStore.prototype._didUpdate = function () {
- this.emit('update', this.getState())
-}
-
-IdentityStore.prototype._isUnlocked = function () {
- var result = Boolean(this._keyStore) && Boolean(this._idmgmt)
- return result
-}
-
-// load identities from keyStoreet
-IdentityStore.prototype._loadIdentities = function () {
- const configManager = this.configManager
- if (!this._isUnlocked()) throw new Error('not unlocked')
-
- var addresses = this._getAddresses()
- addresses.forEach((address, i) => {
- // // add to ethStore
- if (this._ethStore) {
- this._ethStore.addAccount(ethUtil.addHexPrefix(address))
- }
- // add to identities
- const defaultLabel = 'Account ' + (i + 1)
- const nickname = configManager.nicknameForWallet(address)
- var identity = {
- name: nickname || defaultLabel,
- address: address,
- mayBeFauceting: this._mayBeFauceting(i),
- }
- this._currentState.identities[address] = identity
- })
- this._didUpdate()
-}
-
-IdentityStore.prototype.saveAccountLabel = function (account, label, cb) {
- const configManager = this.configManager
- configManager.setNicknameForWallet(account, label)
- this._loadIdentities()
- cb(null, label)
-}
-
-// mayBeFauceting
-// If on testnet, index 0 may be fauceting.
-// The UI will have to check the balance to know.
-// If there is no balance and it mayBeFauceting,
-// then it is in fact fauceting.
-IdentityStore.prototype._mayBeFauceting = function (i) {
- const configManager = this.configManager
- var config = configManager.getProvider()
- if (i === 0 &&
- config.type === 'rpc' &&
- config.rpcTarget === DEFAULT_RPC) {
- return true
- }
- return false
-}
-
-//
-// keyStore managment - unlocking + deserialization
-//
-
-IdentityStore.prototype.tryPassword = function (password, cb) {
- var serializedKeystore = this.configManager.getWallet()
- var keyStore = KeyStore.deserialize(serializedKeystore)
-
- keyStore.keyFromPassword(password, (err, pwDerivedKey) => {
- if (err) return cb(err)
-
- const isCorrect = keyStore.isDerivedKeyCorrect(pwDerivedKey)
- if (!isCorrect) return cb(new Error('Lightwallet - password incorrect'))
-
- this._keyStore = keyStore
- this._createIdMgmt(pwDerivedKey)
- cb()
- })
-}
-
-IdentityStore.prototype._createVault = function (password, seedPhrase, cb) {
- const opts = {
- password,
- hdPathString: this.hdPathString,
- }
-
- if (seedPhrase) {
- opts.seedPhrase = seedPhrase
- }
-
- 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._createIdMgmt(derivedKey)
- this.setSelectedAddressSync()
-
- cb()
- })
- })
-}
-
-IdentityStore.prototype._createIdMgmt = function (derivedKey) {
- this._idmgmt = new IdManagement({
- keyStore: this._keyStore,
- derivedKey: derivedKey,
- configManager: this.configManager,
- })
-}
-
-IdentityStore.prototype.purgeCache = function () {
- this._currentState.identities = {}
- let accounts
- try {
- accounts = Object.keys(this._ethStore._currentState.accounts)
- } catch (e) {
- accounts = []
- }
- accounts.forEach((address) => {
- this._ethStore.removeAccount(address)
- })
-}
-
-IdentityStore.prototype._createFirstWallet = function (derivedKey) {
- const keyStore = this._keyStore
- keyStore.setDefaultHdDerivationPath(this.hdPathString)
- keyStore.generateNewAddress(derivedKey, 1)
- this.configManager.setWallet(keyStore.serialize())
- var addresses = keyStore.getAddresses()
- this._ethStore.addAccount(ethUtil.addHexPrefix(addresses[0]))
-}
-
-// get addresses and normalize address hexString
-IdentityStore.prototype._getAddresses = function () {
- return this._keyStore.getAddresses(this.hdPathString).map((address) => {
- return ethUtil.addHexPrefix(address)
- })
-}
-
-IdentityStore.prototype._autoFaucet = function () {
- var addresses = this._getAddresses()
- autoFaucet(addresses[0])
-}
-
-// util
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js
index 25f9d9e5d..92533e022 100644
--- a/app/scripts/metamask-controller.js
+++ b/app/scripts/metamask-controller.js
@@ -23,7 +23,6 @@ const ConfigManager = require('./lib/config-manager')
const extension = require('./lib/extension')
const autoFaucet = require('./lib/auto-faucet')
const nodeify = require('./lib/nodeify')
-const IdStoreMigrator = require('./lib/idStore-migrator')
const accountImporter = require('./account-import-strategies')
const version = require('../manifest.json').version
@@ -115,11 +114,6 @@ module.exports = class MetamaskController extends EventEmitter {
this.personalMessageManager = new PersonalMessageManager()
this.publicConfigStore = this.initPublicConfigStore()
- // TEMPORARY UNTIL FULL DEPRECATION:
- this.idStoreMigrator = new IdStoreMigrator({
- configManager: this.configManager,
- })
-
// manual disk state subscriptions
this.txManager.store.subscribe((state) => {
this.store.updateState({ TransactionManager: state })
@@ -366,8 +360,7 @@ module.exports = class MetamaskController extends EventEmitter {
//
submitPassword (password, cb) {
- this.migrateOldVaultIfAny(password)
- .then(this.keyringController.submitPassword.bind(this.keyringController, password))
+ return this.keyringController.submitPassword(password)
.then((newState) => { cb(null, newState) })
.catch((reason) => { cb(reason) })
}
@@ -562,35 +555,6 @@ module.exports = class MetamaskController extends EventEmitter {
cb(null, this.getState())
}
- // Migrate Old Vault If Any
- // @string password
- //
- // returns Promise()
- //
- // Temporary step used when logging in.
- // Checks if old style (pre-3.0.0) Metamask Vault exists.
- // If so, persists that vault in the new vault format
- // with the provided password, so the other unlock steps
- // may be completed without interruption.
- migrateOldVaultIfAny (password) {
-
- if (!this.checkIfShouldMigrate()) {
- return Promise.resolve(password)
- }
-
- const keyringController = this.keyringController
-
- return this.idStoreMigrator.migratedVaultForPassword(password)
- .then(this.restoreOldVaultAccounts.bind(this))
- .then(this.restoreOldLostAccounts.bind(this))
- .then(keyringController.persistAllKeyrings.bind(keyringController, password))
- .then(() => password)
- }
-
- checkIfShouldMigrate() {
- return !!this.configManager.getWallet() && !this.configManager.getVault()
- }
-
restoreOldVaultAccounts(migratorOutput) {
const { serialized } = migratorOutput
return this.keyringController.restoreKeyring(serialized)