1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/test/unit/keyring-controller-test.js

168 lines
5.3 KiB
JavaScript
Raw Normal View History

const assert = require('assert')
const KeyringController = require('../../app/scripts/keyring-controller')
const configManagerGen = require('../lib/mock-config-manager')
2016-10-12 00:09:22 +02:00
const ethUtil = require('ethereumjs-util')
2016-11-07 21:18:02 +01:00
const BN = ethUtil.BN
const mockEncryptor = require('../lib/mock-encryptor')
const sinon = require('sinon')
2016-10-12 00:09:22 +02:00
2017-05-04 23:35:10 +02:00
describe('KeyringController', function () {
let keyringController
const password = 'password123'
const seedWords = 'puzzle seed penalty soldier say clay field arctic metal hen cage runway'
const addresses = ['eF35cA8EbB9669A35c31b5F6f249A9941a812AC1'.toLowerCase()]
const accounts = []
// let originalKeystore
2016-10-12 00:09:22 +02:00
2017-05-04 23:35:10 +02:00
beforeEach(function (done) {
this.sinon = sinon.sandbox.create()
window.localStorage = {} // Hacking localStorage support into JSDom
keyringController = new KeyringController({
configManager: configManagerGen(),
txManager: {
getTxList: () => [],
2017-05-04 23:35:10 +02:00
getUnapprovedTxList: () => [],
},
ethStore: {
2017-05-04 23:35:10 +02:00
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
keyringController.createNewVaultAndKeychain(password)
.then(function (newState) {
2017-05-04 23:35:10 +02:00
newState
done()
})
.catch((err) => {
done(err)
})
})
2017-05-04 23:35:10 +02:00
afterEach(function () {
// Cleanup mocks
this.sinon.restore()
})
describe('#createNewVaultAndKeychain', function () {
2016-11-03 19:34:57 +01:00
this.timeout(10000)
2017-05-04 23:35:10 +02:00
it('should set a vault on the configManager', function (done) {
keyringController.store.updateState({ vault: null })
assert(!keyringController.store.getState().vault, 'no previous vault')
keyringController.createNewVaultAndKeychain(password)
.then(() => {
const vault = keyringController.store.getState().vault
assert(vault, 'vault created')
2016-10-12 00:09:22 +02:00
done()
})
.catch((reason) => {
done(reason)
})
2016-10-12 00:09:22 +02:00
})
})
2017-05-04 23:35:10 +02:00
describe('#restoreKeyring', function () {
it(`should pass a keyring's serialized data back to the correct type.`, function (done) {
const mockSerialized = {
2016-11-03 19:34:57 +01:00
type: 'HD Key Tree',
data: {
mnemonic: seedWords,
numberOfAccounts: 1,
2017-05-04 23:35:10 +02:00
},
}
const mock = this.sinon.mock(keyringController)
2016-11-15 20:07:15 +01:00
mock.expects('getBalanceAndNickname')
.exactly(1)
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])
mock.verify()
done()
})
2016-11-23 20:58:34 +01:00
.catch((reason) => {
done(reason)
})
})
})
2017-05-04 23:35:10 +02:00
describe('#createNickname', function () {
it('should add the address to the identities hash', function () {
2016-11-03 19:34:57 +01:00
const fakeAddress = '0x12345678'
keyringController.createNickname(fakeAddress)
const identities = keyringController.memStore.getState().identities
2016-11-03 19:34:57 +01:00
const identity = identities[fakeAddress]
assert.equal(identity.address, fakeAddress)
})
})
2017-05-04 23:35:10 +02:00
describe('#saveAccountLabel', function () {
it('sets the nickname', function (done) {
const account = addresses[0]
var nick = 'Test nickname'
const identities = keyringController.memStore.getState().identities
identities[ethUtil.addHexPrefix(account)] = {}
keyringController.memStore.updateState({ identities })
keyringController.saveAccountLabel(account, nick)
.then((label) => {
try {
assert.equal(label, nick)
const persisted = keyringController.store.getState().walletNicknames[account]
assert.equal(persisted, nick)
done()
} catch (err) {
done()
}
2016-11-03 21:44:29 +01:00
})
.catch((reason) => {
done(reason)
})
2016-11-03 21:44:29 +01:00
})
})
2016-11-03 21:44:29 +01:00
2017-05-04 23:35:10 +02:00
describe('#getAccounts', function () {
it('returns the result of getAccounts for each keyring', function (done) {
2016-11-03 21:44:29 +01:00
keyringController.keyrings = [
2017-05-04 23:35:10 +02:00
{ getAccounts () { return Promise.resolve([1, 2, 3]) } },
{ getAccounts () { return Promise.resolve([4, 5, 6]) } },
2016-11-03 21:44:29 +01:00
]
keyringController.getAccounts()
.then((result) => {
2017-05-04 23:35:10 +02:00
assert.deepEqual(result, [1, 2, 3, 4, 5, 6])
done()
})
2016-11-03 21:44:29 +01:00
})
})
2017-05-04 23:35:10 +02:00
describe('#addGasBuffer', function () {
it('adds 100k gas buffer to estimates', function () {
2016-11-07 21:00:14 +01:00
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)
2017-05-04 23:35:10 +02:00
// const tooBig = new BN(tooBigOutput, 16)
2016-11-07 21:00:14 +01:00
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
})