feature/default_network_editable
commit
7cba71fc55
@ -0,0 +1,102 @@ |
||||
const scrypt = require('scrypt-async') |
||||
const bitcore = require('bitcore-lib') |
||||
const configManager = require('./lib/config-manager') |
||||
const EventEmitter = require('events').EventEmitter |
||||
|
||||
module.exports = class KeyringController extends EventEmitter { |
||||
|
||||
constructor (opts) { |
||||
super() |
||||
this.configManager = opts.configManager |
||||
this.ethStore = opts.ethStore |
||||
this.keyChains = [] |
||||
} |
||||
|
||||
getKeyForPassword(password, callback) { |
||||
let salt = this.configManager.getSalt() |
||||
|
||||
if (!salt) { |
||||
salt = generateSalt(32) |
||||
configManager.setSalt(salt) |
||||
} |
||||
|
||||
var logN = 14 |
||||
var r = 8 |
||||
var dkLen = 32 |
||||
var interruptStep = 200 |
||||
|
||||
var cb = function(derKey) { |
||||
try { |
||||
var ui8arr = (new Uint8Array(derKey)) |
||||
this.pwDerivedKey = ui8arr |
||||
callback(null, ui8arr) |
||||
} catch (err) { |
||||
callback(err) |
||||
} |
||||
} |
||||
|
||||
scrypt(password, salt, logN, r, dkLen, interruptStep, cb, null) |
||||
} |
||||
|
||||
getState() { |
||||
return {} |
||||
} |
||||
|
||||
setStore(ethStore) { |
||||
this.ethStore = ethStore |
||||
} |
||||
|
||||
createNewVault(password, entropy, cb) { |
||||
cb() |
||||
} |
||||
|
||||
submitPassword(password, cb) { |
||||
cb() |
||||
} |
||||
|
||||
setSelectedAddress(address, cb) { |
||||
this.selectedAddress = address |
||||
cb(null, address) |
||||
} |
||||
|
||||
approveTransaction(txId, cb) { |
||||
cb() |
||||
} |
||||
|
||||
cancelTransaction(txId, cb) { |
||||
if (cb && typeof cb === 'function') { |
||||
cb() |
||||
} |
||||
} |
||||
|
||||
signMessage(msgParams, cb) { |
||||
cb() |
||||
} |
||||
|
||||
cancelMessage(msgId, cb) { |
||||
if (cb && typeof cb === 'function') { |
||||
cb() |
||||
} |
||||
} |
||||
|
||||
setLocked(cb) { |
||||
cb() |
||||
} |
||||
|
||||
exportAccount(address, cb) { |
||||
cb(null, '0xPrivateKey') |
||||
} |
||||
|
||||
saveAccountLabel(account, label, cb) { |
||||
cb(/* null, label */) |
||||
} |
||||
|
||||
tryPassword(password, cb) { |
||||
cb() |
||||
} |
||||
|
||||
} |
||||
|
||||
function generateSalt (byteCount) { |
||||
return bitcore.crypto.Random.getRandomBuffer(byteCount || 32).toString('base64') |
||||
} |
@ -0,0 +1,188 @@ |
||||
https://hackmd.io/JwIwDMDGKQZgtAFgKZjEgbARhPAhgKxZbwAcA7LAWOQCaKEgFA==?edit |
||||
|
||||
Subscribablez(initState) |
||||
.subscribe() |
||||
.emitUpdate(newState) |
||||
//.getState() |
||||
|
||||
|
||||
var initState = fromDisk() |
||||
ReduxStore(reducer, initState) |
||||
.reduce(action) -> .emitUpdate() |
||||
|
||||
ReduxStore.subscribe(toDisk) |
||||
|
||||
|
||||
### KeyChainManager / idStore 2.0 (maybe just in MetaMaskController) |
||||
keychains: [] |
||||
getAllAccounts(cb) |
||||
getAllKeychainViewStates(cb) -> returns [ KeyChainViewState] |
||||
|
||||
#### Old idStore external methods, for feature parity: |
||||
|
||||
- init(configManager) |
||||
- setStore(ethStore) |
||||
- getState() |
||||
- getSelectedAddres() |
||||
- setSelectedAddress() |
||||
- createNewVault() |
||||
- recoverFromSeed() |
||||
- submitPassword() |
||||
- approveTransaction() |
||||
- cancelTransaction() |
||||
- addUnconfirmedMessage(msgParams, cb) |
||||
- signMessage() |
||||
- cancelMessage() |
||||
- setLocked() |
||||
- clearSeedWordCache() |
||||
- exportAccount() |
||||
- revealAccount() |
||||
- saveAccountLabel() |
||||
- tryPassword() |
||||
- recoverSeed() |
||||
- getNetwork() |
||||
|
||||
##### Of those methods |
||||
|
||||
Where they should end up: |
||||
|
||||
##### MetaMaskController |
||||
|
||||
- getNetwork() |
||||
|
||||
##### KeyChainManager |
||||
|
||||
- init(configManager) |
||||
- setStore(ethStore) |
||||
- getState() // Deprecate for unidirectional flow |
||||
- on('update', cb) |
||||
- createNewVault(password) |
||||
- getSelectedAddres() |
||||
- setSelectedAddress() |
||||
- submitPassword() |
||||
- tryPassword() |
||||
- approveTransaction() |
||||
- cancelTransaction() |
||||
- signMessage() |
||||
- cancelMessage() |
||||
- setLocked() |
||||
- exportAccount() |
||||
|
||||
##### Bip44 KeyChain |
||||
|
||||
- getState() // Deprecate for unidirectional flow |
||||
- on('update', cb) |
||||
|
||||
If we adopt a ReactStore style unidirectional action dispatching data flow, these methods will be unified under a `dispatch` method, and rather than having a cb will emit an update to the UI: |
||||
|
||||
- createNewKeyChain(entropy) |
||||
- recoverFromSeed() |
||||
- approveTransaction() |
||||
- signMessage() |
||||
- clearSeedWordCache() |
||||
- exportAccount() |
||||
- revealAccount() |
||||
- saveAccountLabel() |
||||
- recoverSeed() |
||||
|
||||
Additional methods, new to this: |
||||
- serialize() |
||||
- Returns pojo with optional `secret` key whose contents will be encrypted with the users' password and salt when written to disk. |
||||
- The isolation of secrets is to preserve performance when decrypting user data. |
||||
- deserialize(pojo) |
||||
|
||||
### KeyChain (ReduxStore?) |
||||
// attributes |
||||
@name |
||||
|
||||
signTx(txParams, cb) |
||||
signMsg(msg, cb) |
||||
|
||||
getAddressList(cb) |
||||
|
||||
getViewState(cb) -> returns KeyChainViewState |
||||
|
||||
serialize(cb) -> obj |
||||
deserialize(obj) |
||||
|
||||
dispatch({ type: <str>, value: <pojo> }) |
||||
|
||||
|
||||
### KeyChainViewState |
||||
// The serialized, renderable keychain data |
||||
accountList: [], |
||||
typeName: 'uPort', |
||||
iconAddress: 'uport.gif', |
||||
internal: {} // Subclass-defined metadata |
||||
|
||||
### KeyChainReactComponent |
||||
// takes a KeyChainViewState |
||||
|
||||
// Subclasses of this: |
||||
- KeyChainListItemComponent |
||||
- KeyChainInitComponent - Maybe part of the List Item |
||||
- KeyChainAccountHeaderComponent |
||||
- KeyChainConfirmationComponent |
||||
// Account list item, tx confirmation extra data (like a QR code), |
||||
// Maybe an options screen, init screen, |
||||
|
||||
how to send actions? |
||||
emitAction(keychains.<id>.didInit) |
||||
|
||||
|
||||
gimmeRemoteKeychain((err, remoteKeychain)=> |
||||
|
||||
) |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
KeyChainReactComponent({ |
||||
keychain |
||||
}) |
||||
|
||||
Keychain: |
||||
methods:{}, |
||||
cachedAccountList: [], |
||||
name: '', |
||||
|
||||
|
||||
CoinbaseKeychain |
||||
getAccountList |
||||
|
||||
|
||||
CoinbaseKeychainComponent |
||||
isLoading = true |
||||
keychain.getAccountList(()=>{ |
||||
isLoading=false |
||||
accountList=accounts |
||||
}) |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
KeyChainViewState { |
||||
attributes: { |
||||
//mandatory: |
||||
accountList: [], |
||||
typeName: 'uPort', |
||||
iconAddress: 'uport.gif', |
||||
|
||||
internal: { |
||||
// keychain-specific metadata |
||||
proxyAddresses: { |
||||
0xReal: '0xProxy' |
||||
} |
||||
}, |
||||
}, |
||||
methods: { |
||||
// arbitrary, internal |
||||
} |
||||
} |
||||
|
||||
## A note on the security of arbitrary action dispatchers |
||||
|
||||
Since keychains will be dispatching actions that are then passed through the background process to be routed, we should not trust or require them to include their own keychain ID as a prefix to their action, but we should tack it on ourselves, so that no action dispatched by a KeyChainComponent ever reaches any KeyChain other than its own. |
||||
|
@ -0,0 +1,141 @@ |
||||
var assert = require('assert') |
||||
var KeyringController = require('../../app/scripts/keyring-controller') |
||||
var configManagerGen = require('../lib/mock-config-manager') |
||||
const ethUtil = require('ethereumjs-util') |
||||
const async = require('async') |
||||
|
||||
describe('KeyringController', function() { |
||||
|
||||
describe('#createNewVault', function () { |
||||
let keyringController |
||||
let password = 'password123' |
||||
let entropy = 'entripppppyy duuude' |
||||
let seedWords |
||||
let accounts = [] |
||||
let originalKeystore |
||||
|
||||
before(function(done) { |
||||
window.localStorage = {} // Hacking localStorage support into JSDom
|
||||
|
||||
keyringController = new KeyringController({ |
||||
configManager: configManagerGen(), |
||||
ethStore: { |
||||
addAccount(acct) { accounts.push(ethUtil.addHexPrefix(acct)) }, |
||||
}, |
||||
}) |
||||
|
||||
keyringController.createNewVault(password, entropy, (err, seeds) => { |
||||
assert.ifError(err, 'createNewVault threw error') |
||||
seedWords = seeds |
||||
originalKeystore = keyringController._idmgmt.keyStore |
||||
done() |
||||
}) |
||||
}) |
||||
|
||||
describe('#recoverFromSeed', function() { |
||||
let newAccounts = [] |
||||
|
||||
before(function() { |
||||
window.localStorage = {} // Hacking localStorage support into JSDom
|
||||
|
||||
keyringController = new KeyringController({ |
||||
configManager: configManagerGen(), |
||||
ethStore: { |
||||
addAccount(acct) { newAccounts.push(ethUtil.addHexPrefix(acct)) }, |
||||
}, |
||||
}) |
||||
}) |
||||
|
||||
it('should return the expected keystore', function (done) { |
||||
|
||||
keyringController.recoverFromSeed(password, seedWords, (err) => { |
||||
assert.ifError(err) |
||||
|
||||
let newKeystore = keyringController._idmgmt.keyStore |
||||
assert.equal(newAccounts[0], accounts[0]) |
||||
done() |
||||
}) |
||||
}) |
||||
}) |
||||
}) |
||||
|
||||
describe('#recoverFromSeed BIP44 compliance', function() { |
||||
const salt = 'lightwalletSalt' |
||||
|
||||
let password = 'secret!' |
||||
let accounts = {} |
||||
let keyringController |
||||
|
||||
var assertions = [ |
||||
{ |
||||
seed: 'picnic injury awful upper eagle junk alert toss flower renew silly vague', |
||||
account: '0x5d8de92c205279c10e5669f797b853ccef4f739a', |
||||
}, |
||||
{ |
||||
seed: 'radar blur cabbage chef fix engine embark joy scheme fiction master release', |
||||
account: '0xe15d894becb0354c501ae69429b05143679f39e0', |
||||
}, |
||||
{ |
||||
seed: 'phone coyote caught pattern found table wedding list tumble broccoli chief swing', |
||||
account: '0xb0e868f24bc7fec2bce2efc2b1c344d7569cd9d2', |
||||
}, |
||||
{ |
||||
seed: 'recycle tag bird palace blue village anxiety census cook soldier example music', |
||||
account: '0xab34a45920afe4af212b96ec51232aaa6a33f663', |
||||
}, |
||||
{ |
||||
seed: 'half glimpse tape cute harvest sweet bike voyage actual floor poet lazy', |
||||
account: '0x28e9044597b625ac4beda7250011670223de43b2', |
||||
}, |
||||
{ |
||||
seed: 'flavor tiger carpet motor angry hungry document inquiry large critic usage liar', |
||||
account: '0xb571be96558940c4e9292e1999461aa7499fb6cd', |
||||
}, |
||||
] |
||||
|
||||
before(function() { |
||||
window.localStorage = {} // Hacking localStorage support into JSDom
|
||||
|
||||
keyringController = new KeyringController({ |
||||
configManager: configManagerGen(), |
||||
ethStore: { |
||||
addAccount(acct) { accounts[acct] = acct}, |
||||
del(acct) { delete accounts[acct] }, |
||||
}, |
||||
}) |
||||
}) |
||||
|
||||
it('should enforce seed compliance with TestRPC', function (done) { |
||||
this.timeout(10000) |
||||
const tests = assertions.map((assertion) => { |
||||
return function (cb) { |
||||
|
||||
keyringController.recoverFromSeed(password, assertion.seed, (err) => { |
||||
assert.ifError(err) |
||||
|
||||
var expected = assertion.account.toLowerCase() |
||||
var received = accounts[expected].toLowerCase() |
||||
assert.equal(received, expected) |
||||
|
||||
keyringController.tryPassword(password, function (err) { |
||||
|
||||
assert.ok(keyringController._isUnlocked(), 'should unlock the id store') |
||||
|
||||
keyringController.submitPassword(password, function(err, account) { |
||||
assert.ifError(err) |
||||
assert.equal(account, expected) |
||||
assert.equal(Object.keys(keyringController._getAddresses()).length, 1, 'only one account on restore') |
||||
cb() |
||||
}) |
||||
}) |
||||
}) |
||||
} |
||||
}) |
||||
|
||||
async.series(tests, function(err, results) { |
||||
assert.ifError(err) |
||||
done() |
||||
}) |
||||
}) |
||||
}) |
||||
}) |
Loading…
Reference in new issue