2016-10-25 01:12:44 +02:00
|
|
|
const async = require('async')
|
2016-10-17 23:48:25 +02:00
|
|
|
const assert = require('assert')
|
2016-09-10 21:08:27 +02:00
|
|
|
const ethUtil = require('ethereumjs-util')
|
2016-10-17 21:47:37 +02:00
|
|
|
const BN = ethUtil.BN
|
2016-10-25 01:12:44 +02:00
|
|
|
const configManagerGen = require('../lib/mock-config-manager')
|
|
|
|
const delegateCallCode = require('../lib/example-code.json').delegateCallCode
|
|
|
|
const IdentityStore = require('../../app/scripts/lib/idStore')
|
2016-10-17 23:48:25 +02:00
|
|
|
|
2016-03-25 01:51:46 +01:00
|
|
|
describe('IdentityStore', function() {
|
|
|
|
|
2016-03-25 20:41:18 +01:00
|
|
|
describe('#createNewVault', function () {
|
|
|
|
let idStore
|
|
|
|
let password = 'password123'
|
|
|
|
let seedWords
|
|
|
|
let accounts = []
|
|
|
|
let originalKeystore
|
|
|
|
|
|
|
|
before(function(done) {
|
|
|
|
window.localStorage = {} // Hacking localStorage support into JSDom
|
|
|
|
|
|
|
|
idStore = new IdentityStore({
|
2016-06-25 01:13:27 +02:00
|
|
|
configManager: configManagerGen(),
|
2016-04-28 03:14:59 +02:00
|
|
|
ethStore: {
|
2016-09-10 21:08:27 +02:00
|
|
|
addAccount(acct) { accounts.push(ethUtil.addHexPrefix(acct)) },
|
2016-04-28 03:14:59 +02:00
|
|
|
},
|
2016-03-25 20:41:18 +01:00
|
|
|
})
|
|
|
|
|
2016-11-22 00:49:03 +01:00
|
|
|
idStore.createNewVault(password, (err, seeds) => {
|
2016-09-10 04:42:18 +02:00
|
|
|
assert.ifError(err, 'createNewVault threw error')
|
2016-03-25 20:41:18 +01:00
|
|
|
seedWords = seeds
|
|
|
|
originalKeystore = idStore._idmgmt.keyStore
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#recoverFromSeed', function() {
|
2016-03-25 23:38:20 +01:00
|
|
|
let newAccounts = []
|
2016-03-25 20:41:18 +01:00
|
|
|
|
|
|
|
before(function() {
|
|
|
|
window.localStorage = {} // Hacking localStorage support into JSDom
|
|
|
|
|
|
|
|
idStore = new IdentityStore({
|
2016-06-25 01:13:27 +02:00
|
|
|
configManager: configManagerGen(),
|
2016-04-28 03:14:59 +02:00
|
|
|
ethStore: {
|
2016-09-10 21:08:27 +02:00
|
|
|
addAccount(acct) { newAccounts.push(ethUtil.addHexPrefix(acct)) },
|
2016-04-28 03:14:59 +02:00
|
|
|
},
|
2016-03-25 20:41:18 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-03-25 22:51:19 +01:00
|
|
|
it('should return the expected keystore', function (done) {
|
2016-03-25 20:41:18 +01:00
|
|
|
|
|
|
|
idStore.recoverFromSeed(password, seedWords, (err) => {
|
|
|
|
assert.ifError(err)
|
|
|
|
|
|
|
|
let newKeystore = idStore._idmgmt.keyStore
|
2016-03-25 23:38:20 +01:00
|
|
|
assert.equal(newAccounts[0], accounts[0])
|
2016-03-25 22:51:19 +01:00
|
|
|
done()
|
2016-03-25 20:41:18 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 22:51:19 +01:00
|
|
|
|
|
|
|
describe('#recoverFromSeed BIP44 compliance', function() {
|
2016-09-10 21:15:05 +02:00
|
|
|
const salt = 'lightwalletSalt'
|
2016-09-10 21:08:27 +02:00
|
|
|
|
2016-03-25 22:51:19 +01:00
|
|
|
let password = 'secret!'
|
2016-09-13 00:18:32 +02:00
|
|
|
let accounts = {}
|
2016-03-25 22:51:19 +01:00
|
|
|
let idStore
|
|
|
|
|
2016-09-10 21:35:52 +02:00
|
|
|
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',
|
2016-09-11 00:45:34 +02:00
|
|
|
account: '0xe15d894becb0354c501ae69429b05143679f39e0',
|
2016-09-10 21:35:52 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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',
|
2016-09-10 21:38:04 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
seed: 'flavor tiger carpet motor angry hungry document inquiry large critic usage liar',
|
|
|
|
account: '0xb571be96558940c4e9292e1999461aa7499fb6cd',
|
|
|
|
},
|
2016-09-10 21:35:52 +02:00
|
|
|
]
|
|
|
|
|
2016-03-25 22:51:19 +01:00
|
|
|
before(function() {
|
|
|
|
window.localStorage = {} // Hacking localStorage support into JSDom
|
|
|
|
|
|
|
|
idStore = new IdentityStore({
|
2016-06-25 01:13:27 +02:00
|
|
|
configManager: configManagerGen(),
|
2016-04-28 03:14:59 +02:00
|
|
|
ethStore: {
|
2016-09-13 00:18:32 +02:00
|
|
|
addAccount(acct) { accounts[acct] = acct},
|
|
|
|
del(acct) { delete accounts[acct] },
|
2016-03-25 22:51:19 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-09-10 21:35:52 +02:00
|
|
|
it('should enforce seed compliance with TestRPC', function (done) {
|
2016-09-12 20:21:27 +02:00
|
|
|
this.timeout(10000)
|
2016-09-10 21:35:52 +02:00
|
|
|
const tests = assertions.map((assertion) => {
|
|
|
|
return function (cb) {
|
2016-09-13 00:18:32 +02:00
|
|
|
|
2016-09-10 21:35:52 +02:00
|
|
|
idStore.recoverFromSeed(password, assertion.seed, (err) => {
|
|
|
|
assert.ifError(err)
|
|
|
|
|
2016-09-11 00:45:34 +02:00
|
|
|
var expected = assertion.account.toLowerCase()
|
2016-09-13 00:18:32 +02:00
|
|
|
var received = accounts[expected].toLowerCase()
|
2016-09-11 00:45:34 +02:00
|
|
|
assert.equal(received, expected)
|
2016-09-12 20:07:25 +02:00
|
|
|
|
|
|
|
idStore.tryPassword(password, function (err) {
|
|
|
|
|
2016-09-12 20:21:27 +02:00
|
|
|
assert.ok(idStore._isUnlocked(), 'should unlock the id store')
|
2016-09-12 20:07:25 +02:00
|
|
|
|
|
|
|
idStore.submitPassword(password, function(err, account) {
|
|
|
|
assert.ifError(err)
|
|
|
|
assert.equal(account, expected)
|
2016-09-13 00:18:32 +02:00
|
|
|
assert.equal(Object.keys(idStore._getAddresses()).length, 1, 'only one account on restore')
|
2016-09-12 20:07:25 +02:00
|
|
|
cb()
|
|
|
|
})
|
|
|
|
})
|
2016-09-10 21:35:52 +02:00
|
|
|
})
|
|
|
|
}
|
2016-03-25 22:51:19 +01:00
|
|
|
})
|
2016-09-10 21:08:27 +02:00
|
|
|
|
2016-09-10 21:35:52 +02:00
|
|
|
async.series(tests, function(err, results) {
|
2016-09-10 21:15:05 +02:00
|
|
|
assert.ifError(err)
|
2016-09-10 21:08:27 +02:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 22:51:19 +01:00
|
|
|
})
|
2016-10-17 21:47:37 +02:00
|
|
|
|
|
|
|
describe('#addGasBuffer', function() {
|
2016-11-07 20:25:23 +01:00
|
|
|
it('formats the result correctly', function() {
|
|
|
|
const idStore = new IdentityStore({
|
|
|
|
configManager: configManagerGen(),
|
|
|
|
ethStore: {
|
|
|
|
addAccount(acct) { accounts.push(ethUtil.addHexPrefix(acct)) },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const gas = '0x01'
|
|
|
|
const bnGas = new BN(gas, 16)
|
2016-11-22 21:10:49 +01:00
|
|
|
const bnResult = idStore.addGasBuffer(bnGas)
|
2016-11-07 20:25:23 +01:00
|
|
|
|
|
|
|
assert.ok(bnResult.gt(gas), 'added more gas as buffer.')
|
2016-10-17 21:47:37 +02:00
|
|
|
})
|
|
|
|
|
2016-11-08 17:39:41 +01:00
|
|
|
it('buffers 20%', function() {
|
2016-11-07 20:25:23 +01:00
|
|
|
const idStore = new IdentityStore({
|
|
|
|
configManager: configManagerGen(),
|
|
|
|
ethStore: {
|
|
|
|
addAccount(acct) { accounts.push(ethUtil.addHexPrefix(acct)) },
|
|
|
|
},
|
|
|
|
})
|
2016-10-17 21:47:37 +02:00
|
|
|
|
2016-11-07 20:25:23 +01:00
|
|
|
const gas = '0x04ee59' // Actual estimated gas example
|
2016-11-07 20:55:17 +01:00
|
|
|
const bnGas = new BN(ethUtil.stripHexPrefix(gas), 16)
|
2016-11-08 17:39:41 +01:00
|
|
|
const five = new BN('5', 10)
|
|
|
|
const correctBuffer = bnGas.div(five)
|
2016-11-07 20:25:23 +01:00
|
|
|
const correct = bnGas.add(correctBuffer)
|
|
|
|
|
2016-11-22 21:10:49 +01:00
|
|
|
const bnResult = idStore.addGasBuffer(bnGas)
|
2016-11-07 20:25:23 +01:00
|
|
|
|
2016-11-07 20:55:17 +01:00
|
|
|
assert(bnResult.gt(bnGas), 'Estimate increased in value.')
|
2016-11-08 17:39:41 +01:00
|
|
|
assert.equal(bnResult.sub(bnGas).toString(10), correctBuffer.toString(10), 'added 20% gas')
|
2016-11-07 20:25:23 +01:00
|
|
|
})
|
2016-10-17 21:47:37 +02:00
|
|
|
})
|
2016-10-17 23:48:25 +02:00
|
|
|
|
|
|
|
describe('#checkForDelegateCall', function() {
|
|
|
|
const idStore = new IdentityStore({
|
|
|
|
configManager: configManagerGen(),
|
|
|
|
ethStore: {
|
|
|
|
addAccount(acct) { accounts.push(ethUtil.addHexPrefix(acct)) },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2016-10-25 01:12:44 +02:00
|
|
|
var result = idStore.checkForDelegateCall(delegateCallCode)
|
|
|
|
assert.equal(result, true, 'no delegate call in provided code')
|
2016-10-17 23:48:25 +02:00
|
|
|
})
|
2016-11-07 20:25:23 +01:00
|
|
|
|
2016-03-25 20:41:18 +01:00
|
|
|
})
|