aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2016-11-22 11:40:30 +0800
committerDan Finlay <dan@danfinlay.com>2016-11-22 11:40:58 +0800
commitced36eb20186b0f1bf0c744ac31a5b7804120065 (patch)
treeb36c37132abd4dee19e580436622cf25dee4046a /app
parentecfda5bcc54bc444f3620a18a561847d0d2634dd (diff)
downloadtangerine-wallet-browser-ced36eb20186b0f1bf0c744ac31a5b7804120065.tar
tangerine-wallet-browser-ced36eb20186b0f1bf0c744ac31a5b7804120065.tar.gz
tangerine-wallet-browser-ced36eb20186b0f1bf0c744ac31a5b7804120065.tar.bz2
tangerine-wallet-browser-ced36eb20186b0f1bf0c744ac31a5b7804120065.tar.lz
tangerine-wallet-browser-ced36eb20186b0f1bf0c744ac31a5b7804120065.tar.xz
tangerine-wallet-browser-ced36eb20186b0f1bf0c744ac31a5b7804120065.tar.zst
tangerine-wallet-browser-ced36eb20186b0f1bf0c744ac31a5b7804120065.zip
Improve Keyring organization
Separated public & private methods. (Fixes #845) Made class method `type()` into a simple property. (Fixes #846)
Diffstat (limited to 'app')
-rw-r--r--app/scripts/keyring-controller.js4
-rw-r--r--app/scripts/keyrings/hd.js67
-rw-r--r--app/scripts/keyrings/simple.js21
3 files changed, 56 insertions, 36 deletions
diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js
index cf761c88c..fdef9d7b3 100644
--- a/app/scripts/keyring-controller.js
+++ b/app/scripts/keyring-controller.js
@@ -67,7 +67,7 @@ module.exports = class KeyringController extends EventEmitter {
currentFiat: this.configManager.getCurrentFiat(),
conversionRate: this.configManager.getConversionRate(),
conversionDate: this.configManager.getConversionDate(),
- keyringTypes: this.keyringTypes.map((krt) => krt.type()),
+ keyringTypes: this.keyringTypes.map(krt => krt.type),
identities: this.identities,
}
}
@@ -312,7 +312,7 @@ module.exports = class KeyringController extends EventEmitter {
getKeyringClassForType (type) {
const Keyring = this.keyringTypes.reduce((res, kr) => {
- if (kr.type() === type) {
+ if (kr.type === type) {
return kr
} else {
return res
diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js
index b98f29187..a28ef6736 100644
--- a/app/scripts/keyrings/hd.js
+++ b/app/scripts/keyrings/hd.js
@@ -2,17 +2,17 @@ const EventEmitter = require('events').EventEmitter
const hdkey = require('ethereumjs-wallet/hdkey')
const bip39 = require('bip39')
const ethUtil = require('ethereumjs-util')
-const sigUtil = require('../lib/sig-util')
-const type = 'HD Key Tree'
+// *Internal Deps
+const sigUtil = require('../lib/sig-util')
+// Options:
const hdPathString = `m/44'/60'/0'/0`
+const type = 'HD Key Tree'
-module.exports = class HdKeyring extends EventEmitter {
+class HdKeyring extends EventEmitter {
- static type () {
- return type
- }
+ /* PUBLIC METHODS */
constructor (opts = {}) {
super()
@@ -20,6 +20,13 @@ module.exports = class HdKeyring extends EventEmitter {
this.deserialize(opts)
}
+ serialize () {
+ return {
+ mnemonic: this.mnemonic,
+ numberOfAccounts: this.wallets.length,
+ }
+ }
+
deserialize (opts = {}) {
this.opts = opts || {}
this.wallets = []
@@ -27,7 +34,7 @@ module.exports = class HdKeyring extends EventEmitter {
this.root = null
if ('mnemonic' in opts) {
- this.initFromMnemonic(opts.mnemonic)
+ this._initFromMnemonic(opts.mnemonic)
}
if ('numberOfAccounts' in opts) {
@@ -35,28 +42,9 @@ module.exports = class HdKeyring extends EventEmitter {
}
}
- initFromMnemonic (mnemonic) {
- this.mnemonic = mnemonic
- const seed = bip39.mnemonicToSeed(mnemonic)
- this.hdWallet = hdkey.fromMasterSeed(seed)
- this.root = this.hdWallet.derivePath(hdPathString)
- }
-
- serialize () {
- return {
- mnemonic: this.mnemonic,
- numberOfAccounts: this.wallets.length,
- }
- }
-
- exportAccount (address) {
- const wallet = this.getWalletForAccount(address)
- return wallet.getPrivateKey().toString('hex')
- }
-
addAccounts (numberOfAccounts = 1) {
if (!this.root) {
- this.initFromMnemonic(bip39.generateMnemonic())
+ this._initFromMnemonic(bip39.generateMnemonic())
}
const oldLen = this.wallets.length
@@ -76,7 +64,7 @@ module.exports = class HdKeyring extends EventEmitter {
// tx is an instance of the ethereumjs-transaction class.
signTransaction (address, tx) {
- const wallet = this.getWalletForAccount(address)
+ const wallet = this._getWalletForAccount(address)
var privKey = wallet.getPrivateKey()
tx.sign(privKey)
return tx
@@ -84,7 +72,7 @@ module.exports = class HdKeyring extends EventEmitter {
// For eth_sign, we need to sign transactions:
signMessage (withAccount, data) {
- const wallet = this.getWalletForAccount(withAccount)
+ const wallet = this._getWalletForAccount(withAccount)
const message = ethUtil.removeHexPrefix(data)
var privKey = wallet.getPrivateKey()
var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey)
@@ -92,10 +80,29 @@ module.exports = class HdKeyring extends EventEmitter {
return rawMsgSig
}
- getWalletForAccount (account) {
+ exportAccount (address) {
+ const wallet = this._getWalletForAccount(address)
+ return wallet.getPrivateKey().toString('hex')
+ }
+
+
+ /* PRIVATE METHODS */
+
+ _initFromMnemonic (mnemonic) {
+ this.mnemonic = mnemonic
+ const seed = bip39.mnemonicToSeed(mnemonic)
+ this.hdWallet = hdkey.fromMasterSeed(seed)
+ this.root = this.hdWallet.derivePath(hdPathString)
+ }
+
+
+ _getWalletForAccount (account) {
return this.wallets.find((w) => {
const address = w.getAddress().toString('hex')
return ((address === account) || (sigUtil.normalize(address) === account))
})
}
}
+
+HdKeyring.type = type
+module.exports = HdKeyring
diff --git a/app/scripts/keyrings/simple.js b/app/scripts/keyrings/simple.js
index ee743bc03..4fdccc4f7 100644
--- a/app/scripts/keyrings/simple.js
+++ b/app/scripts/keyrings/simple.js
@@ -4,7 +4,9 @@ const ethUtil = require('ethereumjs-util')
const type = 'Simple Key Pair'
const sigUtil = require('../lib/sig-util')
-module.exports = class SimpleKeyring extends EventEmitter {
+class SimpleKeyring extends EventEmitter {
+
+ /* PUBLIC METHODS */
static type () {
return type
@@ -44,7 +46,7 @@ module.exports = class SimpleKeyring extends EventEmitter {
// tx is an instance of the ethereumjs-transaction class.
signTransaction (address, tx) {
- const wallet = this.getWalletForAccount(address)
+ const wallet = this._getWalletForAccount(address)
var privKey = wallet.getPrivateKey()
tx.sign(privKey)
return tx
@@ -52,7 +54,7 @@ module.exports = class SimpleKeyring extends EventEmitter {
// For eth_sign, we need to sign transactions:
signMessage (withAccount, data) {
- const wallet = this.getWalletForAccount(withAccount)
+ const wallet = this._getWalletForAccount(withAccount)
const message = ethUtil.removeHexPrefix(data)
var privKey = wallet.getPrivateKey()
var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey)
@@ -60,8 +62,19 @@ module.exports = class SimpleKeyring extends EventEmitter {
return rawMsgSig
}
- getWalletForAccount (account) {
+ exportAccount (address) {
+ const wallet = this._getWalletForAccount(address)
+ return wallet.getPrivateKey().toString('hex')
+ }
+
+
+ /* PRIVATE METHODS */
+
+ _getWalletForAccount (account) {
return this.wallets.find(w => w.getAddress().toString('hex') === account)
}
}
+
+SimpleKeyring.type = type
+module.exports = SimpleKeyring