2016-10-12 00:09:22 +02:00
|
|
|
var assert = require('assert')
|
|
|
|
var KeyringController = require('../../app/scripts/keyring-controller')
|
|
|
|
var configManagerGen = require('../lib/mock-config-manager')
|
|
|
|
const ethUtil = require('ethereumjs-util')
|
2016-11-07 21:18:02 +01:00
|
|
|
const BN = ethUtil.BN
|
2016-10-12 00:09:22 +02:00
|
|
|
const async = require('async')
|
2016-10-21 21:11:54 +02:00
|
|
|
const mockEncryptor = require('../lib/mock-encryptor')
|
2016-10-21 22:11:30 +02:00
|
|
|
const MockSimpleKeychain = require('../lib/mock-simple-keychain')
|
|
|
|
const sinon = require('sinon')
|
2016-10-12 00:09:22 +02:00
|
|
|
|
|
|
|
describe('KeyringController', function() {
|
|
|
|
|
2016-11-03 19:34:57 +01:00
|
|
|
let keyringController, state
|
2016-10-21 21:11:54 +02:00
|
|
|
let password = 'password123'
|
2016-11-03 19:34:57 +01:00
|
|
|
let seedWords = 'puzzle seed penalty soldier say clay field arctic metal hen cage runway'
|
|
|
|
let addresses = ['eF35cA8EbB9669A35c31b5F6f249A9941a812AC1'.toLowerCase()]
|
2016-10-21 21:11:54 +02:00
|
|
|
let accounts = []
|
|
|
|
let originalKeystore
|
|
|
|
|
2016-10-21 21:26:56 +02:00
|
|
|
beforeEach(function(done) {
|
2016-10-21 22:11:30 +02:00
|
|
|
this.sinon = sinon.sandbox.create()
|
2016-10-21 21:11:54 +02:00
|
|
|
window.localStorage = {} // Hacking localStorage support into JSDom
|
|
|
|
|
|
|
|
keyringController = new KeyringController({
|
|
|
|
configManager: configManagerGen(),
|
2016-12-14 21:56:53 +01:00
|
|
|
txManager: {
|
|
|
|
getTxList: () => [],
|
|
|
|
getUnapprovedTxList: () => []
|
|
|
|
},
|
2016-10-21 21:11:54 +02:00
|
|
|
ethStore: {
|
|
|
|
addAccount(acct) { accounts.push(ethUtil.addHexPrefix(acct)) },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Stub out the browser crypto for a mock encryptor.
|
|
|
|
// Browser crypto is tested in the integration test suite.
|
|
|
|
keyringController.encryptor = mockEncryptor
|
2016-10-21 21:26:56 +02:00
|
|
|
|
2016-11-28 21:43:44 +01:00
|
|
|
keyringController.createNewVaultAndKeychain(password)
|
|
|
|
.then(function (newState) {
|
2016-11-03 19:34:57 +01:00
|
|
|
state = newState
|
2016-10-21 21:26:56 +02:00
|
|
|
done()
|
|
|
|
})
|
2016-10-21 21:11:54 +02:00
|
|
|
})
|
|
|
|
|
2016-10-21 22:11:30 +02:00
|
|
|
afterEach(function() {
|
|
|
|
// Cleanup mocks
|
|
|
|
this.sinon.restore()
|
|
|
|
})
|
|
|
|
|
2016-11-02 23:04:50 +01:00
|
|
|
describe('#createNewVaultAndKeychain', function () {
|
2016-11-03 19:34:57 +01:00
|
|
|
this.timeout(10000)
|
|
|
|
|
2016-10-21 21:11:54 +02:00
|
|
|
it('should set a vault on the configManager', function(done) {
|
2016-10-21 22:11:30 +02:00
|
|
|
keyringController.configManager.setVault(null)
|
2016-10-21 21:11:54 +02:00
|
|
|
assert(!keyringController.configManager.getVault(), 'no previous vault')
|
2016-11-28 21:43:44 +01:00
|
|
|
keyringController.createNewVaultAndKeychain(password)
|
|
|
|
.then(() => {
|
2016-10-21 21:11:54 +02:00
|
|
|
const vault = keyringController.configManager.getVault()
|
|
|
|
assert(vault, 'vault created')
|
2016-10-12 00:09:22 +02:00
|
|
|
done()
|
|
|
|
})
|
2016-11-28 21:43:44 +01:00
|
|
|
.catch((reason) => {
|
|
|
|
assert.ifError(reason)
|
|
|
|
done()
|
|
|
|
})
|
2016-10-12 00:09:22 +02:00
|
|
|
})
|
|
|
|
})
|
2016-10-21 22:11:30 +02:00
|
|
|
|
2016-11-02 23:04:50 +01:00
|
|
|
describe('#restoreKeyring', function() {
|
2016-10-21 22:11:30 +02:00
|
|
|
|
2016-11-23 09:23:41 +01:00
|
|
|
it(`should pass a keyring's serialized data back to the correct type.`, function(done) {
|
2016-10-21 22:11:30 +02:00
|
|
|
const mockSerialized = {
|
2016-11-03 19:34:57 +01:00
|
|
|
type: 'HD Key Tree',
|
|
|
|
data: {
|
|
|
|
mnemonic: seedWords,
|
2016-11-12 02:06:37 +01:00
|
|
|
numberOfAccounts: 1,
|
2016-11-03 19:34:57 +01:00
|
|
|
}
|
2016-10-21 22:11:30 +02:00
|
|
|
}
|
|
|
|
const mock = this.sinon.mock(keyringController)
|
|
|
|
|
2016-11-15 20:07:15 +01:00
|
|
|
mock.expects('getBalanceAndNickname')
|
2016-10-21 22:11:30 +02:00
|
|
|
.exactly(1)
|
|
|
|
|
2016-11-23 09:23:41 +01:00
|
|
|
keyringController.restoreKeyring(mockSerialized)
|
|
|
|
.then((keyring) => {
|
|
|
|
assert.equal(keyring.wallets.length, 1, 'one wallet restored')
|
2016-11-23 20:58:34 +01:00
|
|
|
return keyring.getAccounts()
|
|
|
|
})
|
|
|
|
.then((accounts) => {
|
|
|
|
assert.equal(accounts[0], addresses[0])
|
2016-11-23 09:23:41 +01:00
|
|
|
mock.verify()
|
|
|
|
done()
|
|
|
|
})
|
2016-11-23 20:58:34 +01:00
|
|
|
.catch((reason) => {
|
|
|
|
assert.ifError(reason)
|
2016-11-23 09:23:41 +01:00
|
|
|
done()
|
2016-11-23 00:54:51 +01:00
|
|
|
})
|
2016-11-02 23:04:50 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-03 19:34:57 +01:00
|
|
|
describe('#createNickname', function() {
|
|
|
|
it('should add the address to the identities hash', function() {
|
|
|
|
const fakeAddress = '0x12345678'
|
|
|
|
keyringController.createNickname(fakeAddress)
|
|
|
|
const identities = keyringController.identities
|
|
|
|
const identity = identities[fakeAddress]
|
|
|
|
assert.equal(identity.address, fakeAddress)
|
|
|
|
|
|
|
|
const nick = keyringController.configManager.nicknameForWallet(fakeAddress)
|
|
|
|
assert.equal(typeof nick, 'string')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-03 19:59:20 +01:00
|
|
|
describe('#saveAccountLabel', function() {
|
2016-11-23 09:23:41 +01:00
|
|
|
it ('sets the nickname', function(done) {
|
2016-11-03 19:59:20 +01:00
|
|
|
const account = addresses[0]
|
|
|
|
var nick = 'Test nickname'
|
2016-11-07 21:19:56 +01:00
|
|
|
keyringController.identities[ethUtil.addHexPrefix(account)] = {}
|
2016-11-28 21:43:44 +01:00
|
|
|
keyringController.saveAccountLabel(account, nick)
|
|
|
|
.then((label) => {
|
2016-11-23 09:23:41 +01:00
|
|
|
assert.equal(label, nick)
|
|
|
|
const persisted = keyringController.configManager.nicknameForWallet(account)
|
|
|
|
assert.equal(persisted, nick)
|
|
|
|
done()
|
|
|
|
})
|
2016-11-28 21:43:44 +01:00
|
|
|
.catch((reason) => {
|
|
|
|
assert.ifError(reason)
|
|
|
|
done()
|
|
|
|
})
|
2016-11-03 19:59:20 +01:00
|
|
|
})
|
2016-11-03 21:44:29 +01:00
|
|
|
|
|
|
|
this.timeout(10000)
|
|
|
|
it('retrieves the persisted nickname', function(done) {
|
|
|
|
const account = addresses[0]
|
|
|
|
var nick = 'Test nickname'
|
|
|
|
keyringController.configManager.setNicknameForWallet(account, nick)
|
2016-11-28 21:43:44 +01:00
|
|
|
keyringController.createNewVaultAndRestore(password, seedWords)
|
|
|
|
.then((state) => {
|
2016-11-03 21:44:29 +01:00
|
|
|
|
|
|
|
const identity = keyringController.identities['0x' + account]
|
|
|
|
assert.equal(identity.name, nick)
|
|
|
|
|
|
|
|
assert(accounts)
|
|
|
|
done()
|
|
|
|
})
|
2016-11-28 21:43:44 +01:00
|
|
|
.catch((reason) => {
|
|
|
|
assert.ifError(reason)
|
|
|
|
done()
|
|
|
|
})
|
2016-11-03 21:44:29 +01:00
|
|
|
})
|
2016-11-03 19:59:20 +01:00
|
|
|
})
|
2016-11-03 21:44:29 +01:00
|
|
|
|
|
|
|
describe('#getAccounts', function() {
|
|
|
|
it('returns the result of getAccounts for each keyring', function() {
|
|
|
|
keyringController.keyrings = [
|
2016-11-23 09:23:41 +01:00
|
|
|
{ getAccounts() { return Promise.resolve([1,2,3]) } },
|
|
|
|
{ getAccounts() { return Promise.resolve([4,5,6]) } },
|
2016-11-03 21:44:29 +01:00
|
|
|
]
|
|
|
|
|
2016-11-23 09:23:41 +01:00
|
|
|
keyringController.getAccounts()
|
|
|
|
.then((result) => {
|
|
|
|
assert.deepEqual(result, [1,2,3,4,5,6])
|
|
|
|
done()
|
|
|
|
})
|
2016-11-03 21:44:29 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-07 21:00:14 +01:00
|
|
|
describe('#addGasBuffer', function() {
|
|
|
|
it('adds 100k gas buffer to estimates', function() {
|
|
|
|
|
|
|
|
const gas = '0x04ee59' // Actual estimated gas example
|
|
|
|
const tooBigOutput = '0x80674f9' // Actual bad output
|
|
|
|
const bnGas = new BN(ethUtil.stripHexPrefix(gas), 16)
|
|
|
|
const correctBuffer = new BN('100000', 10)
|
|
|
|
const correct = bnGas.add(correctBuffer)
|
|
|
|
|
|
|
|
const tooBig = new BN(tooBigOutput, 16)
|
|
|
|
const result = keyringController.addGasBuffer(gas)
|
|
|
|
const bnResult = new BN(ethUtil.stripHexPrefix(result), 16)
|
|
|
|
|
|
|
|
assert.equal(result.indexOf('0x'), 0, 'included hex prefix')
|
|
|
|
assert(bnResult.gt(bnGas), 'Estimate increased in value.')
|
|
|
|
assert.equal(bnResult.sub(bnGas).toString(10), '100000', 'added 100k gas')
|
|
|
|
assert.equal(result, '0x' + correct.toString(16), 'Added the right amount')
|
|
|
|
assert.notEqual(result, tooBigOutput, 'not that bad estimate')
|
|
|
|
})
|
|
|
|
})
|
2016-10-12 00:09:22 +02:00
|
|
|
})
|