2017-01-28 08:04:34 +01:00
|
|
|
const assert = require('assert')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const clone = require('clone')
|
2018-02-15 17:34:31 +01:00
|
|
|
const nock = require('nock')
|
2017-01-28 08:04:34 +01:00
|
|
|
const MetaMaskController = require('../../app/scripts/metamask-controller')
|
2018-02-15 17:34:31 +01:00
|
|
|
const blacklistJSON = require('../stub/blacklist')
|
2018-02-28 00:50:49 +01:00
|
|
|
const firstTimeState = require('../../app/scripts/first-time-state')
|
2017-01-28 08:04:34 +01:00
|
|
|
|
2018-04-30 05:18:34 +02:00
|
|
|
const DEFAULT_LABEL = 'Account 1'
|
|
|
|
const TEST_SEED = 'debris dizzy just program just float decrease vacant alarm reduce speak stadium'
|
|
|
|
const TEST_ADDRESS = '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc'
|
|
|
|
const TEST_SEED_ALT = 'setup olympic issue mobile velvet surge alcohol burger horse view reopen gentle'
|
|
|
|
const TEST_ADDRESS_ALT = '0xc42edfcc21ed14dda456aa0756c153f7985d8813'
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('MetaMaskController', function () {
|
2018-02-15 17:34:31 +01:00
|
|
|
let metamaskController
|
|
|
|
const sandbox = sinon.sandbox.create()
|
|
|
|
const noop = () => { }
|
2016-09-07 00:23:48 +02:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
beforeEach(function () {
|
2018-02-15 17:34:31 +01:00
|
|
|
|
|
|
|
nock('https://api.infura.io')
|
|
|
|
.persist()
|
|
|
|
.get('/v2/blacklist')
|
|
|
|
.reply(200, blacklistJSON)
|
|
|
|
|
2018-02-28 00:50:49 +01:00
|
|
|
nock('https://api.infura.io')
|
2018-02-15 17:34:31 +01:00
|
|
|
.persist()
|
2018-02-28 00:50:49 +01:00
|
|
|
.get(/.*/)
|
2018-02-15 17:34:31 +01:00
|
|
|
.reply(200)
|
|
|
|
|
|
|
|
metamaskController = new MetaMaskController({
|
|
|
|
showUnapprovedTx: noop,
|
|
|
|
encryptor: {
|
|
|
|
encrypt: function (password, object) {
|
|
|
|
this.object = object
|
|
|
|
return Promise.resolve()
|
|
|
|
},
|
|
|
|
decrypt: function () {
|
|
|
|
return Promise.resolve(this.object)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
initState: clone(firstTimeState),
|
|
|
|
})
|
|
|
|
sandbox.spy(metamaskController.keyringController, 'createNewVaultAndKeychain')
|
|
|
|
sandbox.spy(metamaskController.keyringController, 'createNewVaultAndRestore')
|
2016-09-07 00:23:48 +02:00
|
|
|
})
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
afterEach(function () {
|
2018-02-15 17:34:31 +01:00
|
|
|
nock.cleanAll()
|
|
|
|
sandbox.restore()
|
2016-09-07 00:23:48 +02:00
|
|
|
})
|
|
|
|
|
2018-02-15 17:34:31 +01:00
|
|
|
describe('#getGasPrice', function () {
|
|
|
|
it('gives the 50th percentile lowest accepted gas price from recentBlocksController', async function () {
|
|
|
|
const realRecentBlocksController = metamaskController.recentBlocksController
|
|
|
|
metamaskController.recentBlocksController = {
|
|
|
|
store: {
|
|
|
|
getState: () => {
|
|
|
|
return {
|
|
|
|
recentBlocks: [
|
|
|
|
{ gasPrices: [ '0x3b9aca00', '0x174876e800'] },
|
|
|
|
{ gasPrices: [ '0x3b9aca00', '0x174876e800'] },
|
|
|
|
{ gasPrices: [ '0x174876e800', '0x174876e800' ]},
|
|
|
|
{ gasPrices: [ '0x174876e800', '0x174876e800' ]},
|
|
|
|
],
|
2018-01-09 00:16:08 +01:00
|
|
|
}
|
2018-02-15 17:34:31 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-01-09 00:16:08 +01:00
|
|
|
|
2018-02-15 17:34:31 +01:00
|
|
|
const gasPrice = metamaskController.getGasPrice()
|
|
|
|
assert.equal(gasPrice, '0x3b9aca00', 'accurately estimates 50th percentile accepted gas price')
|
2018-01-09 00:16:08 +01:00
|
|
|
|
2018-02-15 17:34:31 +01:00
|
|
|
metamaskController.recentBlocksController = realRecentBlocksController
|
2018-01-06 06:24:10 +01:00
|
|
|
})
|
2018-02-15 17:34:31 +01:00
|
|
|
})
|
2018-01-06 06:24:10 +01:00
|
|
|
|
2018-02-15 17:34:31 +01:00
|
|
|
describe('#createNewVaultAndKeychain', function () {
|
|
|
|
it('can only create new vault on keyringController once', async function () {
|
|
|
|
const selectStub = sandbox.stub(metamaskController, 'selectFirstIdentity')
|
2018-01-04 01:44:14 +01:00
|
|
|
|
2018-02-15 17:34:31 +01:00
|
|
|
const password = 'a-fake-password'
|
2017-11-20 23:15:00 +01:00
|
|
|
|
2018-02-15 17:34:31 +01:00
|
|
|
await metamaskController.createNewVaultAndKeychain(password)
|
|
|
|
await metamaskController.createNewVaultAndKeychain(password)
|
2017-11-20 23:44:12 +01:00
|
|
|
|
2018-02-15 17:34:31 +01:00
|
|
|
assert(metamaskController.keyringController.createNewVaultAndKeychain.calledOnce)
|
2017-11-20 23:15:00 +01:00
|
|
|
|
2018-02-15 17:34:31 +01:00
|
|
|
selectStub.reset()
|
2017-11-20 23:15:00 +01:00
|
|
|
})
|
2018-02-15 17:34:31 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('#createNewVaultAndRestore', function () {
|
|
|
|
it('should be able to call newVaultAndRestore despite a mistake.', async function () {
|
|
|
|
const password = 'what-what-what'
|
2018-04-30 05:18:34 +02:00
|
|
|
await metamaskController.createNewVaultAndRestore(password, TEST_SEED.slice(0, -1)).catch((e) => null)
|
|
|
|
await metamaskController.createNewVaultAndRestore(password, TEST_SEED)
|
2018-01-04 01:44:14 +01:00
|
|
|
|
2018-02-15 17:34:31 +01:00
|
|
|
assert(metamaskController.keyringController.createNewVaultAndRestore.calledTwice)
|
2018-01-04 01:44:14 +01:00
|
|
|
})
|
2018-04-30 05:18:34 +02:00
|
|
|
|
|
|
|
it('should clear previous identities after vault restoration', async () => {
|
|
|
|
await metamaskController.createNewVaultAndRestore('foobar1337', TEST_SEED)
|
|
|
|
assert.deepEqual(metamaskController.getState().identities, {
|
|
|
|
[TEST_ADDRESS]: { address: TEST_ADDRESS, name: DEFAULT_LABEL },
|
|
|
|
})
|
|
|
|
|
|
|
|
await metamaskController.keyringController.saveAccountLabel(TEST_ADDRESS, 'Account Foo')
|
|
|
|
assert.deepEqual(metamaskController.getState().identities, {
|
|
|
|
[TEST_ADDRESS]: { address: TEST_ADDRESS, name: 'Account Foo' },
|
|
|
|
})
|
|
|
|
|
|
|
|
await metamaskController.createNewVaultAndRestore('foobar1337', TEST_SEED_ALT)
|
|
|
|
assert.deepEqual(metamaskController.getState().identities, {
|
|
|
|
[TEST_ADDRESS_ALT]: { address: TEST_ADDRESS_ALT, name: DEFAULT_LABEL },
|
|
|
|
})
|
|
|
|
})
|
2017-05-04 23:35:10 +02:00
|
|
|
})
|
|
|
|
})
|