2021-05-07 21:38:24 +02:00
|
|
|
import { strict as assert } from 'assert';
|
2021-02-04 19:15:23 +01:00
|
|
|
import sinon from 'sinon';
|
|
|
|
import { cloneDeep } from 'lodash';
|
|
|
|
import nock from 'nock';
|
|
|
|
import { obj as createThoughStream } from 'through2';
|
|
|
|
import EthQuery from 'eth-query';
|
|
|
|
import proxyquire from 'proxyquire';
|
2022-04-27 20:14:10 +02:00
|
|
|
import browser from 'webextension-polyfill';
|
2023-01-21 00:03:11 +01:00
|
|
|
import { wordlist as englishWordlist } from '@metamask/scure-bip39/dist/wordlists/english';
|
2023-01-18 15:47:29 +01:00
|
|
|
import { TransactionStatus } from '../../shared/constants/transaction';
|
2021-03-16 22:00:08 +01:00
|
|
|
import createTxMeta from '../../test/lib/createTxMeta';
|
2022-09-14 16:55:31 +02:00
|
|
|
import { NETWORK_TYPES } from '../../shared/constants/network';
|
2023-03-08 18:35:45 +01:00
|
|
|
import { createTestProviderTools } from '../../test/stub/provider';
|
2023-03-21 15:43:22 +01:00
|
|
|
import { HardwareDeviceNames } from '../../shared/constants/hardware-wallets';
|
|
|
|
import { KeyringType } from '../../shared/constants/keyring';
|
2023-01-21 00:03:11 +01:00
|
|
|
import { deferredPromise } from './lib/util';
|
2018-04-19 23:36:33 +02:00
|
|
|
|
2021-03-16 22:00:08 +01:00
|
|
|
const Ganache = require('../../test/e2e/ganache');
|
|
|
|
|
2021-02-04 19:50:58 +01:00
|
|
|
const ganacheServer = new Ganache();
|
|
|
|
|
2022-03-18 20:07:05 +01:00
|
|
|
const browserPolyfillMock = {
|
2019-12-20 16:32:31 +01:00
|
|
|
runtime: {
|
|
|
|
id: 'fake-extension-id',
|
2020-10-06 20:28:38 +02:00
|
|
|
onInstalled: {
|
|
|
|
addListener: () => undefined,
|
|
|
|
},
|
2022-04-27 20:14:10 +02:00
|
|
|
onMessageExternal: {
|
|
|
|
addListener: () => undefined,
|
|
|
|
},
|
2021-04-30 17:28:07 +02:00
|
|
|
getPlatformInfo: async () => 'mac',
|
2019-12-20 16:32:31 +01:00
|
|
|
},
|
2023-04-06 16:43:01 +02:00
|
|
|
storage: {
|
|
|
|
session: {},
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-12-20 16:32:31 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let loggerMiddlewareMock;
|
2020-06-23 18:12:11 +02:00
|
|
|
const initializeMockMiddlewareLog = () => {
|
|
|
|
loggerMiddlewareMock = {
|
|
|
|
requests: [],
|
|
|
|
responses: [],
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
};
|
2020-06-23 18:12:11 +02:00
|
|
|
const tearDownMockMiddlewareLog = () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
loggerMiddlewareMock = undefined;
|
|
|
|
};
|
2020-06-23 18:12:11 +02:00
|
|
|
|
|
|
|
const createLoggerMiddlewareMock = () => (req, res, next) => {
|
|
|
|
if (loggerMiddlewareMock) {
|
2021-02-04 19:15:23 +01:00
|
|
|
loggerMiddlewareMock.requests.push(req);
|
2020-06-23 18:12:11 +02:00
|
|
|
next((cb) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
loggerMiddlewareMock.responses.push(res);
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
return;
|
2020-06-23 18:12:11 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
next();
|
|
|
|
};
|
2020-06-23 18:12:11 +02:00
|
|
|
|
2023-03-08 18:35:45 +01:00
|
|
|
const MOCK_TOKEN_BALANCE = '888';
|
|
|
|
|
|
|
|
function MockEthContract() {
|
|
|
|
return () => {
|
|
|
|
return {
|
|
|
|
at: () => {
|
|
|
|
return {
|
|
|
|
balanceOf: () => MOCK_TOKEN_BALANCE,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO, Feb 24, 2023:
|
|
|
|
// ethjs-contract is being added to proxyquire, but we might want to discontinue proxyquire
|
|
|
|
// this is for expediency as we resolve a bug for v10.26.0. The proper solution here would have
|
|
|
|
// us set up the test infrastructure for a mocked provider. Github ticket for that is:
|
|
|
|
// https://github.com/MetaMask/metamask-extension/issues/17890
|
2021-03-16 22:00:08 +01:00
|
|
|
const MetaMaskController = proxyquire('./metamask-controller', {
|
|
|
|
'./lib/createLoggerMiddleware': { default: createLoggerMiddlewareMock },
|
2023-03-08 18:35:45 +01:00
|
|
|
'ethjs-contract': MockEthContract,
|
2021-03-16 22:00:08 +01:00
|
|
|
}).default;
|
2019-09-16 19:11:01 +02:00
|
|
|
|
2023-04-06 16:43:01 +02:00
|
|
|
const MetaMaskControllerMV3 = proxyquire('./metamask-controller', {
|
|
|
|
'../../shared/modules/mv3.utils': { isManifestV3: true },
|
|
|
|
}).default;
|
|
|
|
|
2022-09-29 05:26:01 +02:00
|
|
|
const currentNetworkId = '5';
|
2021-02-04 19:15:23 +01:00
|
|
|
const DEFAULT_LABEL = 'Account 1';
|
2020-11-03 00:41:28 +01:00
|
|
|
const TEST_SEED =
|
2021-02-04 19:15:23 +01:00
|
|
|
'debris dizzy just program just float decrease vacant alarm reduce speak stadium';
|
|
|
|
const TEST_ADDRESS = '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc';
|
|
|
|
const TEST_ADDRESS_2 = '0xec1adf982415d2ef5ec55899b9bfb8bc0f29251b';
|
|
|
|
const TEST_ADDRESS_3 = '0xeb9e64b93097bc15f01f13eae97015c57ab64823';
|
2020-11-03 00:41:28 +01:00
|
|
|
const TEST_SEED_ALT =
|
2021-02-04 19:15:23 +01:00
|
|
|
'setup olympic issue mobile velvet surge alcohol burger horse view reopen gentle';
|
|
|
|
const TEST_ADDRESS_ALT = '0xc42edfcc21ed14dda456aa0756c153f7985d8813';
|
2023-03-09 22:00:28 +01:00
|
|
|
|
|
|
|
const NOTIFICATION_ID = 'NHL8f2eSSTn9TKBamRLiU';
|
|
|
|
|
|
|
|
const ALT_MAINNET_RPC_URL = 'http://localhost:8545';
|
|
|
|
const POLYGON_RPC_URL = 'https://polygon.llamarpc.com';
|
|
|
|
const POLYGON_RPC_URL_2 = 'https://polygon-rpc.com';
|
|
|
|
|
|
|
|
const NETWORK_CONFIGURATION_ID_1 = 'networkConfigurationId1';
|
|
|
|
const NETWORK_CONFIGURATION_ID_2 = 'networkConfigurationId2';
|
|
|
|
const NETWORK_CONFIGURATION_ID_3 = 'networkConfigurationId3';
|
|
|
|
|
|
|
|
const ETH = 'ETH';
|
|
|
|
const MATIC = 'MATIC';
|
|
|
|
|
|
|
|
const POLYGON_CHAIN_ID = '0x89';
|
|
|
|
const MAINNET_CHAIN_ID = '0x1';
|
|
|
|
|
|
|
|
const firstTimeState = {
|
|
|
|
config: {},
|
|
|
|
NetworkController: {
|
2023-05-02 17:53:20 +02:00
|
|
|
providerConfig: {
|
2023-03-09 22:00:28 +01:00
|
|
|
type: NETWORK_TYPES.RPC,
|
|
|
|
rpcUrl: ALT_MAINNET_RPC_URL,
|
|
|
|
chainId: MAINNET_CHAIN_ID,
|
|
|
|
ticker: ETH,
|
|
|
|
nickname: 'Alt Mainnet',
|
|
|
|
id: NETWORK_CONFIGURATION_ID_1,
|
|
|
|
},
|
|
|
|
networkConfigurations: {
|
|
|
|
[NETWORK_CONFIGURATION_ID_1]: {
|
|
|
|
rpcUrl: ALT_MAINNET_RPC_URL,
|
|
|
|
type: NETWORK_TYPES.RPC,
|
|
|
|
chainId: MAINNET_CHAIN_ID,
|
|
|
|
ticker: ETH,
|
|
|
|
nickname: 'Alt Mainnet',
|
|
|
|
id: NETWORK_CONFIGURATION_ID_1,
|
|
|
|
},
|
|
|
|
[NETWORK_CONFIGURATION_ID_2]: {
|
|
|
|
rpcUrl: POLYGON_RPC_URL,
|
|
|
|
type: NETWORK_TYPES.RPC,
|
|
|
|
chainId: POLYGON_CHAIN_ID,
|
|
|
|
ticker: MATIC,
|
|
|
|
nickname: 'Polygon',
|
|
|
|
id: NETWORK_CONFIGURATION_ID_2,
|
|
|
|
},
|
|
|
|
[NETWORK_CONFIGURATION_ID_3]: {
|
|
|
|
rpcUrl: POLYGON_RPC_URL_2,
|
|
|
|
type: NETWORK_TYPES.RPC,
|
|
|
|
chainId: POLYGON_CHAIN_ID,
|
|
|
|
ticker: MATIC,
|
|
|
|
nickname: 'Alt Polygon',
|
|
|
|
id: NETWORK_CONFIGURATION_ID_1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
networkDetails: {
|
|
|
|
EIPS: {
|
|
|
|
1559: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
NotificationController: {
|
|
|
|
notifications: {
|
|
|
|
[NOTIFICATION_ID]: {
|
|
|
|
id: NOTIFICATION_ID,
|
|
|
|
origin: 'local:http://localhost:8086/',
|
|
|
|
createdDate: 1652967897732,
|
|
|
|
readDate: null,
|
|
|
|
message: 'Hello, http://localhost:8086!',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2017-01-28 08:04:34 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('MetaMaskController', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
let metamaskController;
|
2022-11-22 17:56:26 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const sandbox = sinon.createSandbox();
|
|
|
|
const noop = () => undefined;
|
2016-09-07 00:23:48 +02:00
|
|
|
|
2023-04-06 16:43:01 +02:00
|
|
|
browserPolyfillMock.storage.session.set = sandbox.spy();
|
|
|
|
|
2021-02-04 19:50:58 +01:00
|
|
|
before(async function () {
|
2022-11-22 17:56:26 +01:00
|
|
|
globalThis.isFirstTimeProfileLoaded = true;
|
2021-02-04 19:50:58 +01:00
|
|
|
await ganacheServer.start();
|
2022-11-22 17:56:26 +01:00
|
|
|
sinon.spy(MetaMaskController.prototype, 'resetStates');
|
2023-04-06 16:43:01 +02:00
|
|
|
sinon.spy(MetaMaskControllerMV3.prototype, 'resetStates');
|
2021-02-04 19:50:58 +01:00
|
|
|
});
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
beforeEach(function () {
|
2019-06-01 00:14:22 +02:00
|
|
|
nock('https://min-api.cryptocompare.com')
|
|
|
|
.persist()
|
2020-08-14 13:48:42 +02:00
|
|
|
.get(/.*/u)
|
2021-02-04 19:15:23 +01:00
|
|
|
.reply(200, '{"JPY":12415.9}');
|
2022-12-13 21:20:24 +01:00
|
|
|
nock('https://static.metafi.codefi.network')
|
|
|
|
.persist()
|
2023-02-24 16:09:00 +01:00
|
|
|
.get('/api/v1/lists/stalelist.json')
|
2022-12-13 21:20:24 +01:00
|
|
|
.reply(
|
|
|
|
200,
|
|
|
|
JSON.stringify({
|
|
|
|
version: 2,
|
|
|
|
tolerance: 2,
|
|
|
|
fuzzylist: [],
|
2023-02-24 16:09:00 +01:00
|
|
|
allowlist: [],
|
|
|
|
blocklist: ['127.0.0.1'],
|
|
|
|
lastUpdated: 0,
|
2022-12-13 21:20:24 +01:00
|
|
|
}),
|
|
|
|
)
|
2023-02-24 16:09:00 +01:00
|
|
|
.get('/api/v1/lists/hotlist.json')
|
|
|
|
.reply(
|
|
|
|
200,
|
|
|
|
JSON.stringify([
|
|
|
|
{ url: '127.0.0.1', targetList: 'blocklist', timestamp: 0 },
|
|
|
|
]),
|
|
|
|
);
|
2019-06-01 00:14:22 +02:00
|
|
|
|
2022-04-27 20:14:10 +02:00
|
|
|
sandbox.replace(browser, 'runtime', {
|
|
|
|
sendMessage: sandbox.stub().rejects(),
|
|
|
|
});
|
|
|
|
|
2018-02-15 17:34:31 +01:00
|
|
|
metamaskController = new MetaMaskController({
|
2020-11-16 17:32:53 +01:00
|
|
|
showUserConfirmation: noop,
|
2018-02-15 17:34:31 +01:00
|
|
|
encryptor: {
|
2020-11-03 00:41:28 +01:00
|
|
|
encrypt(_, object) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.object = object;
|
|
|
|
return Promise.resolve('mock-encrypted');
|
2018-02-15 17:34:31 +01:00
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
decrypt() {
|
2021-02-04 19:15:23 +01:00
|
|
|
return Promise.resolve(this.object);
|
2018-02-15 17:34:31 +01:00
|
|
|
},
|
|
|
|
},
|
2020-01-29 18:14:33 +01:00
|
|
|
initState: cloneDeep(firstTimeState),
|
2020-12-02 22:41:30 +01:00
|
|
|
initLangCode: 'en_US',
|
2020-11-03 00:41:28 +01:00
|
|
|
platform: {
|
|
|
|
showTransactionNotification: () => undefined,
|
|
|
|
getVersion: () => 'foo',
|
|
|
|
},
|
2022-03-18 20:07:05 +01:00
|
|
|
browser: browserPolyfillMock,
|
2020-09-10 18:16:00 +02:00
|
|
|
infuraProjectId: 'foo',
|
2023-04-06 16:43:01 +02:00
|
|
|
isFirstMetaMaskControllerSetup: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-08-25 22:54:15 +02:00
|
|
|
|
2018-06-05 22:31:34 +02:00
|
|
|
// add sinon method spies
|
2020-11-03 00:41:28 +01:00
|
|
|
sandbox.spy(
|
|
|
|
metamaskController.keyringController,
|
|
|
|
'createNewVaultAndKeychain',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
sandbox.spy(
|
|
|
|
metamaskController.keyringController,
|
|
|
|
'createNewVaultAndRestore',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2016-09-07 00:23:48 +02:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
afterEach(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
nock.cleanAll();
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
2016-09-07 00:23:48 +02:00
|
|
|
|
2021-02-04 19:50:58 +01:00
|
|
|
after(async function () {
|
|
|
|
await ganacheServer.quit();
|
|
|
|
});
|
|
|
|
|
2022-11-22 17:56:26 +01:00
|
|
|
describe('should reset states on first time profile load', function () {
|
2023-04-06 16:43:01 +02:00
|
|
|
it('in mv2, it should reset state without attempting to call browser storage', function () {
|
|
|
|
assert.equal(metamaskController.resetStates.callCount, 1);
|
|
|
|
assert.equal(browserPolyfillMock.storage.session.set.callCount, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('in mv3, it should reset state', function () {
|
|
|
|
MetaMaskControllerMV3.prototype.resetStates.resetHistory();
|
|
|
|
const metamaskControllerMV3 = new MetaMaskControllerMV3({
|
|
|
|
showUserConfirmation: noop,
|
|
|
|
encryptor: {
|
|
|
|
encrypt(_, object) {
|
|
|
|
this.object = object;
|
|
|
|
return Promise.resolve('mock-encrypted');
|
|
|
|
},
|
|
|
|
decrypt() {
|
|
|
|
return Promise.resolve(this.object);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
initState: cloneDeep(firstTimeState),
|
|
|
|
initLangCode: 'en_US',
|
|
|
|
platform: {
|
|
|
|
showTransactionNotification: () => undefined,
|
|
|
|
getVersion: () => 'foo',
|
|
|
|
},
|
|
|
|
browser: browserPolyfillMock,
|
|
|
|
infuraProjectId: 'foo',
|
|
|
|
isFirstMetaMaskControllerSetup: true,
|
|
|
|
});
|
|
|
|
assert.equal(metamaskControllerMV3.resetStates.callCount, 1);
|
|
|
|
assert.equal(browserPolyfillMock.storage.session.set.callCount, 1);
|
|
|
|
assert.deepEqual(
|
|
|
|
browserPolyfillMock.storage.session.set.getCall(0).args[0],
|
|
|
|
{
|
|
|
|
isFirstMetaMaskControllerSetup: false,
|
|
|
|
},
|
|
|
|
);
|
2022-11-22 17:56:26 +01:00
|
|
|
});
|
|
|
|
|
2023-04-06 16:43:01 +02:00
|
|
|
it('in mv3, it should not reset states if isFirstMetaMaskControllerSetup is false', function () {
|
|
|
|
MetaMaskControllerMV3.prototype.resetStates.resetHistory();
|
|
|
|
browserPolyfillMock.storage.session.set.resetHistory();
|
|
|
|
const metamaskControllerMV3 = new MetaMaskControllerMV3({
|
|
|
|
showUserConfirmation: noop,
|
|
|
|
encryptor: {
|
|
|
|
encrypt(_, object) {
|
|
|
|
this.object = object;
|
|
|
|
return Promise.resolve('mock-encrypted');
|
|
|
|
},
|
|
|
|
decrypt() {
|
|
|
|
return Promise.resolve(this.object);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
initState: cloneDeep(firstTimeState),
|
|
|
|
initLangCode: 'en_US',
|
|
|
|
platform: {
|
|
|
|
showTransactionNotification: () => undefined,
|
|
|
|
getVersion: () => 'foo',
|
|
|
|
},
|
|
|
|
browser: browserPolyfillMock,
|
|
|
|
infuraProjectId: 'foo',
|
|
|
|
isFirstMetaMaskControllerSetup: false,
|
|
|
|
});
|
|
|
|
assert.equal(metamaskControllerMV3.resetStates.callCount, 0);
|
|
|
|
assert.equal(browserPolyfillMock.storage.session.set.callCount, 0);
|
2022-11-22 17:56:26 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-26 20:52:51 +02:00
|
|
|
describe('#importAccountWithStrategy', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const importPrivkey =
|
2021-02-04 19:15:23 +01:00
|
|
|
'4cfd3e90fc78b0f86bf7524722150bb8da9c60cd532564d7ff43f5716514f553';
|
2019-09-26 20:52:51 +02:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const password = 'a-fake-password';
|
|
|
|
await metamaskController.createNewVaultAndRestore(password, TEST_SEED);
|
2020-11-03 00:41:28 +01:00
|
|
|
await metamaskController.importAccountWithStrategy('Private Key', [
|
|
|
|
importPrivkey,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
|
|
|
});
|
2019-09-26 20:52:51 +02:00
|
|
|
|
|
|
|
it('adds private key to keyrings in KeyringController', async function () {
|
2022-07-31 20:26:40 +02:00
|
|
|
const simpleKeyrings =
|
|
|
|
metamaskController.keyringController.getKeyringsByType(
|
2023-03-21 15:43:22 +01:00
|
|
|
KeyringType.imported,
|
2022-07-31 20:26:40 +02:00
|
|
|
);
|
2023-01-21 00:03:11 +01:00
|
|
|
const pubAddressHexArr = await simpleKeyrings[0].getAccounts();
|
|
|
|
const privKeyHex = await simpleKeyrings[0].exportAccount(
|
|
|
|
pubAddressHexArr[0],
|
|
|
|
);
|
|
|
|
assert.equal(privKeyHex, importPrivkey);
|
|
|
|
assert.equal(
|
|
|
|
pubAddressHexArr[0],
|
|
|
|
'0xe18035bf8712672935fdb4e5e431b1a0183d2dfc',
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-09-26 20:52:51 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('adds 1 account', async function () {
|
2022-07-31 20:26:40 +02:00
|
|
|
const keyringAccounts =
|
|
|
|
await metamaskController.keyringController.getAccounts();
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
keyringAccounts[keyringAccounts.length - 1],
|
|
|
|
'0xe18035bf8712672935fdb4e5e431b1a0183d2dfc',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2019-09-26 20:52:51 +02:00
|
|
|
|
2018-06-04 22:28:46 +02:00
|
|
|
describe('submitPassword', function () {
|
2022-10-31 17:20:50 +01:00
|
|
|
it('removes any identities that do not correspond to known accounts.', async function () {
|
|
|
|
const password = 'password';
|
2021-02-04 19:15:23 +01:00
|
|
|
await metamaskController.createNewVaultAndKeychain(password);
|
2018-06-04 22:28:46 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const fakeAddress = '0xbad0';
|
|
|
|
metamaskController.preferencesController.addAddresses([fakeAddress]);
|
|
|
|
await metamaskController.submitPassword(password);
|
2018-06-04 22:28:46 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const identities = Object.keys(
|
|
|
|
metamaskController.preferencesController.store.getState().identities,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2022-07-31 20:26:40 +02:00
|
|
|
const addresses =
|
|
|
|
await metamaskController.keyringController.getAccounts();
|
2018-06-04 22:28:46 +02:00
|
|
|
|
|
|
|
identities.forEach((identity) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.ok(
|
|
|
|
addresses.includes(identity),
|
|
|
|
`addresses should include all IDs: ${identity}`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-06-04 22:28:46 +02:00
|
|
|
|
|
|
|
addresses.forEach((address) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.ok(
|
|
|
|
identities.includes(address),
|
|
|
|
`identities should include all Addresses: ${address}`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-06-04 22:28:46 +02:00
|
|
|
|
2018-02-15 17:34:31 +01:00
|
|
|
describe('#createNewVaultAndKeychain', function () {
|
|
|
|
it('can only create new vault on keyringController once', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const selectStub = sandbox.stub(
|
|
|
|
metamaskController,
|
|
|
|
'selectFirstIdentity',
|
|
|
|
);
|
2018-01-04 01:44:14 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const password = 'a-fake-password';
|
2017-11-20 23:15:00 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
await metamaskController.createNewVaultAndKeychain(password);
|
|
|
|
await metamaskController.createNewVaultAndKeychain(password);
|
2017-11-20 23:44:12 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
assert(
|
|
|
|
metamaskController.keyringController.createNewVaultAndKeychain
|
|
|
|
.calledOnce,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2017-11-20 23:15:00 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
selectStub.reset();
|
|
|
|
});
|
|
|
|
});
|
2018-02-15 17:34:31 +01:00
|
|
|
|
|
|
|
describe('#createNewVaultAndRestore', function () {
|
|
|
|
it('should be able to call newVaultAndRestore despite a mistake.', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const password = 'what-what-what';
|
|
|
|
sandbox.stub(metamaskController, 'getBalance');
|
2019-11-20 01:03:20 +01:00
|
|
|
metamaskController.getBalance.callsFake(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return Promise.resolve('0x0');
|
|
|
|
});
|
2018-07-27 05:40:11 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
await metamaskController
|
|
|
|
.createNewVaultAndRestore(password, TEST_SEED.slice(0, -1))
|
2021-02-04 19:15:23 +01:00
|
|
|
.catch(() => null);
|
|
|
|
await metamaskController.createNewVaultAndRestore(password, TEST_SEED);
|
2018-01-04 01:44:14 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
assert(
|
|
|
|
metamaskController.keyringController.createNewVaultAndRestore
|
|
|
|
.calledTwice,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-04-30 05:18:34 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should clear previous identities after vault restoration', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
sandbox.stub(metamaskController, 'getBalance');
|
2019-11-20 01:03:20 +01:00
|
|
|
metamaskController.getBalance.callsFake(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return Promise.resolve('0x0');
|
|
|
|
});
|
2018-07-27 05:40:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let startTime = Date.now();
|
|
|
|
await metamaskController.createNewVaultAndRestore(
|
|
|
|
'foobar1337',
|
|
|
|
TEST_SEED,
|
|
|
|
);
|
|
|
|
let endTime = Date.now();
|
2020-04-16 20:20:01 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const firstVaultIdentities = cloneDeep(
|
|
|
|
metamaskController.getState().identities,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-04-16 20:20:01 +02:00
|
|
|
assert.ok(
|
2020-11-03 00:41:28 +01:00
|
|
|
firstVaultIdentities[TEST_ADDRESS].lastSelected >= startTime &&
|
|
|
|
firstVaultIdentities[TEST_ADDRESS].lastSelected <= endTime,
|
2020-07-14 17:20:41 +02:00
|
|
|
`'${firstVaultIdentities[TEST_ADDRESS].lastSelected}' expected to be between '${startTime}' and '${endTime}'`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
delete firstVaultIdentities[TEST_ADDRESS].lastSelected;
|
2020-04-16 20:20:01 +02:00
|
|
|
assert.deepEqual(firstVaultIdentities, {
|
2018-04-30 05:18:34 +02:00
|
|
|
[TEST_ADDRESS]: { address: TEST_ADDRESS, name: DEFAULT_LABEL },
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-04-30 05:18:34 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
await metamaskController.preferencesController.setAccountLabel(
|
|
|
|
TEST_ADDRESS,
|
|
|
|
'Account Foo',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-04-16 20:20:01 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const labelledFirstVaultIdentities = cloneDeep(
|
|
|
|
metamaskController.getState().identities,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
delete labelledFirstVaultIdentities[TEST_ADDRESS].lastSelected;
|
2020-04-16 20:20:01 +02:00
|
|
|
assert.deepEqual(labelledFirstVaultIdentities, {
|
2018-04-30 05:18:34 +02:00
|
|
|
[TEST_ADDRESS]: { address: TEST_ADDRESS, name: 'Account Foo' },
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-04-30 05:18:34 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
startTime = Date.now();
|
2020-11-03 00:41:28 +01:00
|
|
|
await metamaskController.createNewVaultAndRestore(
|
|
|
|
'foobar1337',
|
|
|
|
TEST_SEED_ALT,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
endTime = Date.now();
|
2020-04-16 20:20:01 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const secondVaultIdentities = cloneDeep(
|
|
|
|
metamaskController.getState().identities,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-04-16 20:20:01 +02:00
|
|
|
assert.ok(
|
2020-11-03 00:41:28 +01:00
|
|
|
secondVaultIdentities[TEST_ADDRESS_ALT].lastSelected >= startTime &&
|
|
|
|
secondVaultIdentities[TEST_ADDRESS_ALT].lastSelected <= endTime,
|
2020-07-14 17:20:41 +02:00
|
|
|
`'${secondVaultIdentities[TEST_ADDRESS_ALT].lastSelected}' expected to be between '${startTime}' and '${endTime}'`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
delete secondVaultIdentities[TEST_ADDRESS_ALT].lastSelected;
|
2020-04-16 20:20:01 +02:00
|
|
|
assert.deepEqual(secondVaultIdentities, {
|
2018-04-30 05:18:34 +02:00
|
|
|
[TEST_ADDRESS_ALT]: { address: TEST_ADDRESS_ALT, name: DEFAULT_LABEL },
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-07-27 05:40:11 +02:00
|
|
|
|
2021-09-16 23:20:57 +02:00
|
|
|
it('should restore any consecutive accounts with balances without extra zero balance accounts', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
sandbox.stub(metamaskController, 'getBalance');
|
2018-07-27 05:40:11 +02:00
|
|
|
metamaskController.getBalance.withArgs(TEST_ADDRESS).callsFake(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return Promise.resolve('0x14ced5122ce0a000');
|
|
|
|
});
|
2018-07-27 05:40:11 +02:00
|
|
|
metamaskController.getBalance.withArgs(TEST_ADDRESS_2).callsFake(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return Promise.resolve('0x0');
|
|
|
|
});
|
2018-07-27 05:40:11 +02:00
|
|
|
metamaskController.getBalance.withArgs(TEST_ADDRESS_3).callsFake(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return Promise.resolve('0x14ced5122ce0a000');
|
|
|
|
});
|
2018-07-27 05:40:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const startTime = Date.now();
|
|
|
|
await metamaskController.createNewVaultAndRestore(
|
|
|
|
'foobar1337',
|
|
|
|
TEST_SEED,
|
|
|
|
);
|
2020-04-16 20:20:01 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const identities = cloneDeep(metamaskController.getState().identities);
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.ok(
|
|
|
|
identities[TEST_ADDRESS].lastSelected >= startTime &&
|
|
|
|
identities[TEST_ADDRESS].lastSelected <= Date.now(),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
delete identities[TEST_ADDRESS].lastSelected;
|
2020-04-16 20:20:01 +02:00
|
|
|
assert.deepEqual(identities, {
|
2018-07-27 05:40:11 +02:00
|
|
|
[TEST_ADDRESS]: { address: TEST_ADDRESS, name: DEFAULT_LABEL },
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-07-27 05:40:11 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('#getBalance', function () {
|
|
|
|
it('should return the balance known by accountTracker', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const accounts = {};
|
|
|
|
const balance = '0x14ced5122ce0a000';
|
|
|
|
accounts[TEST_ADDRESS] = { balance };
|
2018-07-27 05:40:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
metamaskController.accountTracker.store.putState({ accounts });
|
2018-07-27 05:40:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const gotten = await metamaskController.getBalance(TEST_ADDRESS);
|
2018-07-27 05:40:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(balance, gotten);
|
|
|
|
});
|
2018-07-27 05:40:11 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should ask the network for a balance when not known by accountTracker', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const accounts = {};
|
|
|
|
const balance = '0x14ced5122ce0a000';
|
|
|
|
const ethQuery = new EthQuery();
|
2019-05-08 21:51:33 +02:00
|
|
|
sinon.stub(ethQuery, 'getBalance').callsFake((_, callback) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
callback(undefined, balance);
|
|
|
|
});
|
2018-07-27 05:40:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
metamaskController.accountTracker.store.putState({ accounts });
|
2018-07-27 05:40:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const gotten = await metamaskController.getBalance(
|
|
|
|
TEST_ADDRESS,
|
|
|
|
ethQuery,
|
|
|
|
);
|
2018-07-27 05:40:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(balance, gotten);
|
|
|
|
});
|
|
|
|
});
|
2018-03-27 23:02:00 +02:00
|
|
|
|
|
|
|
describe('#getApi', function () {
|
2021-12-08 22:36:53 +01:00
|
|
|
it('getState', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const getApi = metamaskController.getApi();
|
2021-12-08 22:36:53 +01:00
|
|
|
const state = getApi.getState();
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.deepEqual(state, metamaskController.getState());
|
|
|
|
});
|
|
|
|
});
|
2018-03-27 23:02:00 +02:00
|
|
|
|
|
|
|
describe('#selectFirstIdentity', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
let identities, address;
|
2018-03-27 23:02:00 +02:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
address = '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc';
|
2018-03-27 23:02:00 +02:00
|
|
|
identities = {
|
2018-05-21 23:57:57 +02:00
|
|
|
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc': {
|
2020-08-19 18:27:05 +02:00
|
|
|
address,
|
2020-11-03 00:41:28 +01:00
|
|
|
name: 'Account 1',
|
2018-05-21 23:57:57 +02:00
|
|
|
},
|
|
|
|
'0xc42edfcc21ed14dda456aa0756c153f7985d8813': {
|
2020-11-03 00:41:28 +01:00
|
|
|
address: '0xc42edfcc21ed14dda456aa0756c153f7985d8813',
|
|
|
|
name: 'Account 2',
|
2018-03-27 23:02:00 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
metamaskController.preferencesController.store.updateState({
|
|
|
|
identities,
|
|
|
|
});
|
|
|
|
metamaskController.selectFirstIdentity();
|
|
|
|
});
|
2018-03-27 23:02:00 +02:00
|
|
|
|
|
|
|
it('changes preferences controller select address', function () {
|
2022-07-31 20:26:40 +02:00
|
|
|
const preferenceControllerState =
|
|
|
|
metamaskController.preferencesController.store.getState();
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(preferenceControllerState.selectedAddress, address);
|
|
|
|
});
|
2018-03-27 23:02:00 +02:00
|
|
|
|
|
|
|
it('changes metamask controller selected address', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const metamaskState = metamaskController.getState();
|
|
|
|
assert.equal(metamaskState.selectedAddress, address);
|
|
|
|
});
|
|
|
|
});
|
2018-03-27 23:02:00 +02:00
|
|
|
|
2018-07-17 01:36:08 +02:00
|
|
|
describe('connectHardware', function () {
|
|
|
|
it('should throw if it receives an unknown device name', async function () {
|
|
|
|
try {
|
2020-11-03 00:41:28 +01:00
|
|
|
await metamaskController.connectHardware(
|
|
|
|
'Some random device name',
|
|
|
|
0,
|
|
|
|
`m/44/0'/0'`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-17 01:36:08 +02:00
|
|
|
} catch (e) {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
2021-05-07 21:38:24 +02:00
|
|
|
e.message,
|
|
|
|
'MetamaskController:getKeyringForDevice - Unknown device',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-17 01:36:08 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-07-17 01:36:08 +02:00
|
|
|
|
|
|
|
it('should add the Trezor Hardware keyring', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
sinon.spy(metamaskController.keyringController, 'addNewKeyring');
|
2022-02-16 21:54:30 +01:00
|
|
|
await metamaskController
|
2023-01-20 16:14:40 +01:00
|
|
|
.connectHardware(HardwareDeviceNames.trezor, 0)
|
2022-02-16 21:54:30 +01:00
|
|
|
.catch(() => null);
|
2022-07-31 20:26:40 +02:00
|
|
|
const keyrings =
|
|
|
|
await metamaskController.keyringController.getKeyringsByType(
|
2023-03-21 15:43:22 +01:00
|
|
|
KeyringType.trezor,
|
2022-07-31 20:26:40 +02:00
|
|
|
);
|
2021-04-26 20:05:48 +02:00
|
|
|
assert.deepEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskController.keyringController.addNewKeyring.getCall(0).args,
|
2023-03-21 15:43:22 +01:00
|
|
|
[KeyringType.trezor],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
assert.equal(keyrings.length, 1);
|
|
|
|
});
|
2018-07-17 01:36:08 +02:00
|
|
|
|
2018-08-14 09:42:23 +02:00
|
|
|
it('should add the Ledger Hardware keyring', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
sinon.spy(metamaskController.keyringController, 'addNewKeyring');
|
2022-02-16 21:54:30 +01:00
|
|
|
await metamaskController
|
2023-01-20 16:14:40 +01:00
|
|
|
.connectHardware(HardwareDeviceNames.ledger, 0)
|
2022-02-16 21:54:30 +01:00
|
|
|
.catch(() => null);
|
2022-07-31 20:26:40 +02:00
|
|
|
const keyrings =
|
|
|
|
await metamaskController.keyringController.getKeyringsByType(
|
2023-03-21 15:43:22 +01:00
|
|
|
KeyringType.ledger,
|
2022-07-31 20:26:40 +02:00
|
|
|
);
|
2021-04-26 20:05:48 +02:00
|
|
|
assert.deepEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskController.keyringController.addNewKeyring.getCall(0).args,
|
2023-03-21 15:43:22 +01:00
|
|
|
[KeyringType.ledger],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
assert.equal(keyrings.length, 1);
|
|
|
|
});
|
|
|
|
});
|
2018-07-17 01:36:08 +02:00
|
|
|
|
2023-01-21 00:03:11 +01:00
|
|
|
describe('getPrimaryKeyringMnemonic', function () {
|
2023-01-23 20:41:04 +01:00
|
|
|
it('should return a mnemonic as a Uint8Array', function () {
|
2023-01-21 00:03:11 +01:00
|
|
|
const mockMnemonic =
|
|
|
|
'above mercy benefit hospital call oval domain student sphere interest argue shock';
|
|
|
|
const mnemonicIndices = mockMnemonic
|
|
|
|
.split(' ')
|
|
|
|
.map((word) => englishWordlist.indexOf(word));
|
|
|
|
const uint8ArrayMnemonic = new Uint8Array(
|
|
|
|
new Uint16Array(mnemonicIndices).buffer,
|
|
|
|
);
|
|
|
|
|
|
|
|
const mockHDKeyring = {
|
|
|
|
type: 'HD Key Tree',
|
|
|
|
mnemonic: uint8ArrayMnemonic,
|
|
|
|
};
|
|
|
|
sinon
|
|
|
|
.stub(metamaskController.keyringController, 'getKeyringsByType')
|
|
|
|
.returns([mockHDKeyring]);
|
|
|
|
|
|
|
|
const recoveredMnemonic = metamaskController.getPrimaryKeyringMnemonic();
|
|
|
|
|
2023-01-23 20:41:04 +01:00
|
|
|
assert.equal(recoveredMnemonic, uint8ArrayMnemonic);
|
2023-01-21 00:03:11 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-17 01:36:08 +02:00
|
|
|
describe('checkHardwareStatus', function () {
|
|
|
|
it('should throw if it receives an unknown device name', async function () {
|
|
|
|
try {
|
2020-11-03 00:41:28 +01:00
|
|
|
await metamaskController.checkHardwareStatus(
|
|
|
|
'Some random device name',
|
|
|
|
`m/44/0'/0'`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-17 01:36:08 +02:00
|
|
|
} catch (e) {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
2021-05-07 21:38:24 +02:00
|
|
|
e.message,
|
|
|
|
'MetamaskController:getKeyringForDevice - Unknown device',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-17 01:36:08 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-07-17 01:36:08 +02:00
|
|
|
|
|
|
|
it('should be locked by default', async function () {
|
2022-02-16 21:54:30 +01:00
|
|
|
await metamaskController
|
2023-01-20 16:14:40 +01:00
|
|
|
.connectHardware(HardwareDeviceNames.trezor, 0)
|
2022-02-16 21:54:30 +01:00
|
|
|
.catch(() => null);
|
|
|
|
const status = await metamaskController.checkHardwareStatus(
|
2023-01-20 16:14:40 +01:00
|
|
|
HardwareDeviceNames.trezor,
|
2022-02-16 21:54:30 +01:00
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(status, false);
|
|
|
|
});
|
|
|
|
});
|
2018-07-17 01:36:08 +02:00
|
|
|
|
|
|
|
describe('forgetDevice', function () {
|
|
|
|
it('should throw if it receives an unknown device name', async function () {
|
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await metamaskController.forgetDevice('Some random device name');
|
2018-07-17 01:36:08 +02:00
|
|
|
} catch (e) {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
2021-05-07 21:38:24 +02:00
|
|
|
e.message,
|
|
|
|
'MetamaskController:getKeyringForDevice - Unknown device',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-17 01:36:08 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-07-17 01:36:08 +02:00
|
|
|
|
|
|
|
it('should wipe all the keyring info', async function () {
|
2022-02-16 21:54:30 +01:00
|
|
|
await metamaskController
|
2023-01-20 16:14:40 +01:00
|
|
|
.connectHardware(HardwareDeviceNames.trezor, 0)
|
2022-02-16 21:54:30 +01:00
|
|
|
.catch(() => null);
|
2023-01-20 16:14:40 +01:00
|
|
|
await metamaskController.forgetDevice(HardwareDeviceNames.trezor);
|
2022-07-31 20:26:40 +02:00
|
|
|
const keyrings =
|
|
|
|
await metamaskController.keyringController.getKeyringsByType(
|
2023-03-21 15:43:22 +01:00
|
|
|
KeyringType.trezor,
|
2022-07-31 20:26:40 +02:00
|
|
|
);
|
2018-07-17 01:36:08 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.deepEqual(keyrings[0].accounts, []);
|
|
|
|
assert.deepEqual(keyrings[0].page, 0);
|
|
|
|
assert.deepEqual(keyrings[0].isUnlocked(), false);
|
|
|
|
});
|
|
|
|
});
|
2018-07-17 01:36:08 +02:00
|
|
|
|
2018-08-14 09:42:23 +02:00
|
|
|
describe('unlockHardwareWalletAccount', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
let accountToUnlock;
|
|
|
|
let windowOpenStub;
|
|
|
|
let addNewAccountStub;
|
|
|
|
let getAccountsStub;
|
2018-07-17 07:17:18 +02:00
|
|
|
beforeEach(async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
accountToUnlock = 10;
|
|
|
|
windowOpenStub = sinon.stub(window, 'open');
|
|
|
|
windowOpenStub.returns(noop);
|
2018-07-17 07:17:18 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
addNewAccountStub = sinon.stub(
|
|
|
|
metamaskController.keyringController,
|
|
|
|
'addNewAccount',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
addNewAccountStub.returns({});
|
2018-07-17 07:17:18 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
getAccountsStub = sinon.stub(
|
|
|
|
metamaskController.keyringController,
|
|
|
|
'getAccounts',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-17 07:17:18 +02:00
|
|
|
// Need to return different address to mock the behavior of
|
|
|
|
// adding a new account from the keyring
|
2021-02-04 19:15:23 +01:00
|
|
|
getAccountsStub.onCall(0).returns(Promise.resolve(['0x1']));
|
|
|
|
getAccountsStub.onCall(1).returns(Promise.resolve(['0x2']));
|
|
|
|
getAccountsStub.onCall(2).returns(Promise.resolve(['0x3']));
|
|
|
|
getAccountsStub.onCall(3).returns(Promise.resolve(['0x4']));
|
|
|
|
sinon.spy(metamaskController.preferencesController, 'setAddresses');
|
|
|
|
sinon.spy(metamaskController.preferencesController, 'setSelectedAddress');
|
|
|
|
sinon.spy(metamaskController.preferencesController, 'setAccountLabel');
|
2020-11-03 00:41:28 +01:00
|
|
|
await metamaskController
|
2023-01-20 16:14:40 +01:00
|
|
|
.connectHardware(HardwareDeviceNames.trezor, 0, `m/44'/1'/0'/0`)
|
2021-02-04 19:15:23 +01:00
|
|
|
.catch(() => null);
|
2020-11-03 00:41:28 +01:00
|
|
|
await metamaskController.unlockHardwareWalletAccount(
|
|
|
|
accountToUnlock,
|
2023-01-20 16:14:40 +01:00
|
|
|
HardwareDeviceNames.trezor,
|
2021-11-30 15:28:28 +01:00
|
|
|
`m/44'/1'/0'/0`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-07-17 07:17:18 +02:00
|
|
|
|
|
|
|
afterEach(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
window.open.restore();
|
|
|
|
metamaskController.keyringController.addNewAccount.restore();
|
|
|
|
metamaskController.keyringController.getAccounts.restore();
|
|
|
|
metamaskController.preferencesController.setAddresses.restore();
|
|
|
|
metamaskController.preferencesController.setSelectedAddress.restore();
|
|
|
|
metamaskController.preferencesController.setAccountLabel.restore();
|
|
|
|
});
|
2018-07-17 07:17:18 +02:00
|
|
|
|
2018-08-15 03:19:01 +02:00
|
|
|
it('should set unlockedAccount in the keyring', async function () {
|
2022-07-31 20:26:40 +02:00
|
|
|
const keyrings =
|
|
|
|
await metamaskController.keyringController.getKeyringsByType(
|
2023-03-21 15:43:22 +01:00
|
|
|
KeyringType.trezor,
|
2022-07-31 20:26:40 +02:00
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(keyrings[0].unlockedAccount, accountToUnlock);
|
|
|
|
});
|
2018-07-17 07:17:18 +02:00
|
|
|
|
2018-08-15 03:19:01 +02:00
|
|
|
it('should call keyringController.addNewAccount', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
assert(metamaskController.keyringController.addNewAccount.calledOnce);
|
|
|
|
});
|
2018-07-17 07:17:18 +02:00
|
|
|
|
|
|
|
it('should call keyringController.getAccounts ', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
assert(metamaskController.keyringController.getAccounts.called);
|
|
|
|
});
|
2018-07-17 07:17:18 +02:00
|
|
|
|
|
|
|
it('should call preferencesController.setAddresses', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
assert(metamaskController.preferencesController.setAddresses.calledOnce);
|
|
|
|
});
|
2018-07-17 07:17:18 +02:00
|
|
|
|
|
|
|
it('should call preferencesController.setSelectedAddress', async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert(
|
|
|
|
metamaskController.preferencesController.setSelectedAddress.calledOnce,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-07-17 07:17:18 +02:00
|
|
|
|
|
|
|
it('should call preferencesController.setAccountLabel', async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert(
|
|
|
|
metamaskController.preferencesController.setAccountLabel.calledOnce,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-07-17 01:36:08 +02:00
|
|
|
|
2018-03-27 23:02:00 +02:00
|
|
|
describe('#addNewAccount', function () {
|
|
|
|
it('errors when an primary keyring is does not exist', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const addNewAccount = metamaskController.addNewAccount();
|
2020-02-11 17:51:13 +01:00
|
|
|
|
2018-03-27 23:02:00 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await addNewAccount;
|
|
|
|
assert.fail('should throw');
|
2018-03-27 23:02:00 +02:00
|
|
|
} catch (e) {
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(e.message, 'MetamaskController - No HD Key Tree found');
|
2018-03-27 23:02:00 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-04-11 17:34:35 +02:00
|
|
|
|
|
|
|
describe('#verifyseedPhrase', function () {
|
|
|
|
it('errors when no keying is provided', async function () {
|
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await metamaskController.verifySeedPhrase();
|
2018-04-11 17:34:35 +02:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(
|
|
|
|
error.message,
|
|
|
|
'MetamaskController - No HD Key Tree found',
|
|
|
|
);
|
2018-04-11 17:34:35 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-04-11 17:34:35 +02:00
|
|
|
|
2018-04-19 23:36:33 +02:00
|
|
|
it('#addNewAccount', async function () {
|
2023-01-21 00:03:11 +01:00
|
|
|
await metamaskController.createNewVaultAndKeychain('password');
|
2022-08-16 08:12:00 +02:00
|
|
|
await metamaskController.addNewAccount(1);
|
2022-07-31 20:26:40 +02:00
|
|
|
const getAccounts =
|
|
|
|
await metamaskController.keyringController.getAccounts();
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(getAccounts.length, 2);
|
|
|
|
});
|
|
|
|
});
|
2018-04-19 23:36:33 +02:00
|
|
|
|
|
|
|
describe('#resetAccount', function () {
|
2020-02-11 17:51:13 +01:00
|
|
|
it('wipes transactions from only the correct network id and with the selected address', async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const selectedAddressStub = sinon.stub(
|
|
|
|
metamaskController.preferencesController,
|
|
|
|
'getSelectedAddress',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
const getNetworkIdStub = sinon.stub(
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskController.txController.txStateManager,
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
'getNetworkId',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-04-19 23:36:33 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
selectedAddressStub.returns('0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc');
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
getNetworkIdStub.returns(42);
|
2018-04-19 23:36:33 +02:00
|
|
|
|
2021-03-30 16:54:05 +02:00
|
|
|
metamaskController.txController.txStateManager._addTransactionsToState([
|
2020-11-03 00:41:28 +01:00
|
|
|
createTxMeta({
|
|
|
|
id: 1,
|
2023-01-18 15:47:29 +01:00
|
|
|
status: TransactionStatus.unapproved,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: currentNetworkId,
|
|
|
|
txParams: { from: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc' },
|
|
|
|
}),
|
|
|
|
createTxMeta({
|
|
|
|
id: 1,
|
2023-01-18 15:47:29 +01:00
|
|
|
status: TransactionStatus.unapproved,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: currentNetworkId,
|
|
|
|
txParams: { from: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc' },
|
|
|
|
}),
|
2020-11-07 08:38:12 +01:00
|
|
|
createTxMeta({
|
|
|
|
id: 2,
|
2023-01-18 15:47:29 +01:00
|
|
|
status: TransactionStatus.rejected,
|
2020-11-07 08:38:12 +01:00
|
|
|
metamaskNetworkId: '32',
|
|
|
|
}),
|
2020-11-03 00:41:28 +01:00
|
|
|
createTxMeta({
|
|
|
|
id: 3,
|
2023-01-18 15:47:29 +01:00
|
|
|
status: TransactionStatus.submitted,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: currentNetworkId,
|
|
|
|
txParams: { from: '0xB09d8505E1F4EF1CeA089D47094f5DD3464083d4' },
|
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2018-04-19 23:36:33 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
await metamaskController.resetAccount();
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
2021-03-30 16:54:05 +02:00
|
|
|
metamaskController.txController.txStateManager.getTransaction(1),
|
2020-11-03 00:41:28 +01:00
|
|
|
undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-04-11 17:34:35 +02:00
|
|
|
|
2018-07-17 07:44:28 +02:00
|
|
|
describe('#removeAccount', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
let ret;
|
|
|
|
const addressToRemove = '0x1';
|
2022-06-21 21:03:54 +02:00
|
|
|
let mockKeyring;
|
2018-07-17 07:44:28 +02:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
2022-06-21 21:03:54 +02:00
|
|
|
mockKeyring = {
|
|
|
|
getAccounts: sinon.stub().returns(Promise.resolve([])),
|
|
|
|
destroy: sinon.stub(),
|
|
|
|
};
|
2021-02-04 19:15:23 +01:00
|
|
|
sinon.stub(metamaskController.preferencesController, 'removeAddress');
|
|
|
|
sinon.stub(metamaskController.accountTracker, 'removeAccount');
|
|
|
|
sinon.stub(metamaskController.keyringController, 'removeAccount');
|
Permission System 2.0 (#12243)
# Permission System 2.0
## Background
This PR migrates the extension permission system to [the new `PermissionController`](https://github.com/MetaMask/snaps-skunkworks/tree/main/packages/controllers/src/permissions).
The original permission system, based on [`rpc-cap`](https://github.com/MetaMask/rpc-cap), introduced [`ZCAP-LD`](https://w3c-ccg.github.io/zcap-ld/)-like permissions to our JSON-RPC stack.
We used it to [implement](https://github.com/MetaMask/metamask-extension/pull/7004) what we called "LoginPerSite" in [version 7.7.0](https://github.com/MetaMask/metamask-extension/releases/tag/v7.7.0) of the extension, which enabled the user to choose which accounts, if any, should be exposed to each dapp.
While that was a worthwhile feature in and of itself, we wanted a permission _system_ in order to enable everything we are going to with Snaps.
Unfortunately, the original permission system was difficult to use, and necessitated the creation of the original `PermissionsController` (note the "s"), which was more or less a wrapper for `rpc-cap`.
With this PR, we shake off the yoke of the original permission system, in favor of the modular, self-contained, ergonomic, and more mature permission system 2.0.
Note that [the `PermissionController` readme](https://github.com/MetaMask/snaps-skunkworks/tree/main/packages/controllers/src/permissions/README.md) explains how the new permission system works.
The `PermissionController` and `SubjectMetadataController` are currently shipped via `@metamask/snap-controllers`. This is a temporary state of affairs, and we'll move them to `@metamask/controllers` once they've landed in prod.
## Changes in Detail
First, the changes in this PR are not as big as they seem. Roughly half of the additions in this PR are fixtures in the test for the new migration (number 68), and a significant portion of the remaining ~2500 lines are due to find-and-replace changes in other test fixtures and UI files.
- The extension `PermissionsController` has been deleted, and completely replaced with the new `PermissionController` from [`@metamask/snap-controllers`](https://www.npmjs.com/package/@metamask/snap-controllers).
- The original `PermissionsController` "domain metadata" functionality is now managed by the new `SubjectMetadataController`, also from [`@metamask/snap-controllers`](https://www.npmjs.com/package/@metamask/snap-controllers).
- The permission activity and history log controller has been renamed `PermissionLogController` and has its own top-level state key, but is otherwise functionally equivalent to the existing implementation.
- Migration number 68 has been added to account for the new state changes.
- The tests in `app/scripts/controllers/permissions` have been migrated from `mocha` to `jest`.
Reviewers should focus their attention on the following files:
- `app/scripts/`
- `metamask-controller.js`
- This is where most of the integration work for the new `PermissionController` occurs.
Some functions that were internal to the original controller were moved here.
- `controllers/permissions/`
- `selectors.js`
- These selectors are for `ControllerMessenger` selector subscriptions. The actual subscriptions occur in `metamask-controller.js`. See the `ControllerMessenger` implementation for details.
- `specifications.js`
- The caveat and permission specifications are required by the new `PermissionController`, and are used to specify the `eth_accounts` permission and its JSON-RPC method implementation.
See the `PermissionController` readme for details.
- `migrations/068.js`
- The new state should be cross-referenced with the controllers that manage it.
The accompanying tests should also be thoroughly reviewed.
Some files may appear new but have just moved and/or been renamed:
- `app/scripts/lib/rpc-method-middleware/handlers/request-accounts.js`
- This was previously implemented in `controllers/permissions/permissionsMethodMiddleware.js`.
- `test/mocks/permissions.js`
- A truncated version of `test/mocks/permission-controller.js`.
Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2021-12-07 04:16:49 +01:00
|
|
|
sinon.stub(metamaskController, 'removeAllAccountPermissions');
|
2022-06-21 21:03:54 +02:00
|
|
|
sinon
|
|
|
|
.stub(metamaskController.keyringController, 'getKeyringForAccount')
|
|
|
|
.returns(Promise.resolve(mockKeyring));
|
2018-07-17 07:44:28 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
ret = await metamaskController.removeAccount(addressToRemove);
|
|
|
|
});
|
2018-07-17 07:44:28 +02:00
|
|
|
|
|
|
|
afterEach(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
metamaskController.keyringController.removeAccount.restore();
|
|
|
|
metamaskController.accountTracker.removeAccount.restore();
|
|
|
|
metamaskController.preferencesController.removeAddress.restore();
|
Permission System 2.0 (#12243)
# Permission System 2.0
## Background
This PR migrates the extension permission system to [the new `PermissionController`](https://github.com/MetaMask/snaps-skunkworks/tree/main/packages/controllers/src/permissions).
The original permission system, based on [`rpc-cap`](https://github.com/MetaMask/rpc-cap), introduced [`ZCAP-LD`](https://w3c-ccg.github.io/zcap-ld/)-like permissions to our JSON-RPC stack.
We used it to [implement](https://github.com/MetaMask/metamask-extension/pull/7004) what we called "LoginPerSite" in [version 7.7.0](https://github.com/MetaMask/metamask-extension/releases/tag/v7.7.0) of the extension, which enabled the user to choose which accounts, if any, should be exposed to each dapp.
While that was a worthwhile feature in and of itself, we wanted a permission _system_ in order to enable everything we are going to with Snaps.
Unfortunately, the original permission system was difficult to use, and necessitated the creation of the original `PermissionsController` (note the "s"), which was more or less a wrapper for `rpc-cap`.
With this PR, we shake off the yoke of the original permission system, in favor of the modular, self-contained, ergonomic, and more mature permission system 2.0.
Note that [the `PermissionController` readme](https://github.com/MetaMask/snaps-skunkworks/tree/main/packages/controllers/src/permissions/README.md) explains how the new permission system works.
The `PermissionController` and `SubjectMetadataController` are currently shipped via `@metamask/snap-controllers`. This is a temporary state of affairs, and we'll move them to `@metamask/controllers` once they've landed in prod.
## Changes in Detail
First, the changes in this PR are not as big as they seem. Roughly half of the additions in this PR are fixtures in the test for the new migration (number 68), and a significant portion of the remaining ~2500 lines are due to find-and-replace changes in other test fixtures and UI files.
- The extension `PermissionsController` has been deleted, and completely replaced with the new `PermissionController` from [`@metamask/snap-controllers`](https://www.npmjs.com/package/@metamask/snap-controllers).
- The original `PermissionsController` "domain metadata" functionality is now managed by the new `SubjectMetadataController`, also from [`@metamask/snap-controllers`](https://www.npmjs.com/package/@metamask/snap-controllers).
- The permission activity and history log controller has been renamed `PermissionLogController` and has its own top-level state key, but is otherwise functionally equivalent to the existing implementation.
- Migration number 68 has been added to account for the new state changes.
- The tests in `app/scripts/controllers/permissions` have been migrated from `mocha` to `jest`.
Reviewers should focus their attention on the following files:
- `app/scripts/`
- `metamask-controller.js`
- This is where most of the integration work for the new `PermissionController` occurs.
Some functions that were internal to the original controller were moved here.
- `controllers/permissions/`
- `selectors.js`
- These selectors are for `ControllerMessenger` selector subscriptions. The actual subscriptions occur in `metamask-controller.js`. See the `ControllerMessenger` implementation for details.
- `specifications.js`
- The caveat and permission specifications are required by the new `PermissionController`, and are used to specify the `eth_accounts` permission and its JSON-RPC method implementation.
See the `PermissionController` readme for details.
- `migrations/068.js`
- The new state should be cross-referenced with the controllers that manage it.
The accompanying tests should also be thoroughly reviewed.
Some files may appear new but have just moved and/or been renamed:
- `app/scripts/lib/rpc-method-middleware/handlers/request-accounts.js`
- This was previously implemented in `controllers/permissions/permissionsMethodMiddleware.js`.
- `test/mocks/permissions.js`
- A truncated version of `test/mocks/permission-controller.js`.
Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2021-12-07 04:16:49 +01:00
|
|
|
metamaskController.removeAllAccountPermissions.restore();
|
2022-06-21 21:03:54 +02:00
|
|
|
|
|
|
|
mockKeyring.getAccounts.resetHistory();
|
|
|
|
mockKeyring.destroy.resetHistory();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-07-17 07:44:28 +02:00
|
|
|
|
|
|
|
it('should call preferencesController.removeAddress', async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert(
|
|
|
|
metamaskController.preferencesController.removeAddress.calledWith(
|
|
|
|
addressToRemove,
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-07-17 07:44:28 +02:00
|
|
|
it('should call accountTracker.removeAccount', async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert(
|
|
|
|
metamaskController.accountTracker.removeAccount.calledWith([
|
|
|
|
addressToRemove,
|
|
|
|
]),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-07-17 07:44:28 +02:00
|
|
|
it('should call keyringController.removeAccount', async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert(
|
|
|
|
metamaskController.keyringController.removeAccount.calledWith(
|
|
|
|
addressToRemove,
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
Permission System 2.0 (#12243)
# Permission System 2.0
## Background
This PR migrates the extension permission system to [the new `PermissionController`](https://github.com/MetaMask/snaps-skunkworks/tree/main/packages/controllers/src/permissions).
The original permission system, based on [`rpc-cap`](https://github.com/MetaMask/rpc-cap), introduced [`ZCAP-LD`](https://w3c-ccg.github.io/zcap-ld/)-like permissions to our JSON-RPC stack.
We used it to [implement](https://github.com/MetaMask/metamask-extension/pull/7004) what we called "LoginPerSite" in [version 7.7.0](https://github.com/MetaMask/metamask-extension/releases/tag/v7.7.0) of the extension, which enabled the user to choose which accounts, if any, should be exposed to each dapp.
While that was a worthwhile feature in and of itself, we wanted a permission _system_ in order to enable everything we are going to with Snaps.
Unfortunately, the original permission system was difficult to use, and necessitated the creation of the original `PermissionsController` (note the "s"), which was more or less a wrapper for `rpc-cap`.
With this PR, we shake off the yoke of the original permission system, in favor of the modular, self-contained, ergonomic, and more mature permission system 2.0.
Note that [the `PermissionController` readme](https://github.com/MetaMask/snaps-skunkworks/tree/main/packages/controllers/src/permissions/README.md) explains how the new permission system works.
The `PermissionController` and `SubjectMetadataController` are currently shipped via `@metamask/snap-controllers`. This is a temporary state of affairs, and we'll move them to `@metamask/controllers` once they've landed in prod.
## Changes in Detail
First, the changes in this PR are not as big as they seem. Roughly half of the additions in this PR are fixtures in the test for the new migration (number 68), and a significant portion of the remaining ~2500 lines are due to find-and-replace changes in other test fixtures and UI files.
- The extension `PermissionsController` has been deleted, and completely replaced with the new `PermissionController` from [`@metamask/snap-controllers`](https://www.npmjs.com/package/@metamask/snap-controllers).
- The original `PermissionsController` "domain metadata" functionality is now managed by the new `SubjectMetadataController`, also from [`@metamask/snap-controllers`](https://www.npmjs.com/package/@metamask/snap-controllers).
- The permission activity and history log controller has been renamed `PermissionLogController` and has its own top-level state key, but is otherwise functionally equivalent to the existing implementation.
- Migration number 68 has been added to account for the new state changes.
- The tests in `app/scripts/controllers/permissions` have been migrated from `mocha` to `jest`.
Reviewers should focus their attention on the following files:
- `app/scripts/`
- `metamask-controller.js`
- This is where most of the integration work for the new `PermissionController` occurs.
Some functions that were internal to the original controller were moved here.
- `controllers/permissions/`
- `selectors.js`
- These selectors are for `ControllerMessenger` selector subscriptions. The actual subscriptions occur in `metamask-controller.js`. See the `ControllerMessenger` implementation for details.
- `specifications.js`
- The caveat and permission specifications are required by the new `PermissionController`, and are used to specify the `eth_accounts` permission and its JSON-RPC method implementation.
See the `PermissionController` readme for details.
- `migrations/068.js`
- The new state should be cross-referenced with the controllers that manage it.
The accompanying tests should also be thoroughly reviewed.
Some files may appear new but have just moved and/or been renamed:
- `app/scripts/lib/rpc-method-middleware/handlers/request-accounts.js`
- This was previously implemented in `controllers/permissions/permissionsMethodMiddleware.js`.
- `test/mocks/permissions.js`
- A truncated version of `test/mocks/permission-controller.js`.
Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2021-12-07 04:16:49 +01:00
|
|
|
it('should call metamaskController.removeAllAccountPermissions', async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
assert(
|
Permission System 2.0 (#12243)
# Permission System 2.0
## Background
This PR migrates the extension permission system to [the new `PermissionController`](https://github.com/MetaMask/snaps-skunkworks/tree/main/packages/controllers/src/permissions).
The original permission system, based on [`rpc-cap`](https://github.com/MetaMask/rpc-cap), introduced [`ZCAP-LD`](https://w3c-ccg.github.io/zcap-ld/)-like permissions to our JSON-RPC stack.
We used it to [implement](https://github.com/MetaMask/metamask-extension/pull/7004) what we called "LoginPerSite" in [version 7.7.0](https://github.com/MetaMask/metamask-extension/releases/tag/v7.7.0) of the extension, which enabled the user to choose which accounts, if any, should be exposed to each dapp.
While that was a worthwhile feature in and of itself, we wanted a permission _system_ in order to enable everything we are going to with Snaps.
Unfortunately, the original permission system was difficult to use, and necessitated the creation of the original `PermissionsController` (note the "s"), which was more or less a wrapper for `rpc-cap`.
With this PR, we shake off the yoke of the original permission system, in favor of the modular, self-contained, ergonomic, and more mature permission system 2.0.
Note that [the `PermissionController` readme](https://github.com/MetaMask/snaps-skunkworks/tree/main/packages/controllers/src/permissions/README.md) explains how the new permission system works.
The `PermissionController` and `SubjectMetadataController` are currently shipped via `@metamask/snap-controllers`. This is a temporary state of affairs, and we'll move them to `@metamask/controllers` once they've landed in prod.
## Changes in Detail
First, the changes in this PR are not as big as they seem. Roughly half of the additions in this PR are fixtures in the test for the new migration (number 68), and a significant portion of the remaining ~2500 lines are due to find-and-replace changes in other test fixtures and UI files.
- The extension `PermissionsController` has been deleted, and completely replaced with the new `PermissionController` from [`@metamask/snap-controllers`](https://www.npmjs.com/package/@metamask/snap-controllers).
- The original `PermissionsController` "domain metadata" functionality is now managed by the new `SubjectMetadataController`, also from [`@metamask/snap-controllers`](https://www.npmjs.com/package/@metamask/snap-controllers).
- The permission activity and history log controller has been renamed `PermissionLogController` and has its own top-level state key, but is otherwise functionally equivalent to the existing implementation.
- Migration number 68 has been added to account for the new state changes.
- The tests in `app/scripts/controllers/permissions` have been migrated from `mocha` to `jest`.
Reviewers should focus their attention on the following files:
- `app/scripts/`
- `metamask-controller.js`
- This is where most of the integration work for the new `PermissionController` occurs.
Some functions that were internal to the original controller were moved here.
- `controllers/permissions/`
- `selectors.js`
- These selectors are for `ControllerMessenger` selector subscriptions. The actual subscriptions occur in `metamask-controller.js`. See the `ControllerMessenger` implementation for details.
- `specifications.js`
- The caveat and permission specifications are required by the new `PermissionController`, and are used to specify the `eth_accounts` permission and its JSON-RPC method implementation.
See the `PermissionController` readme for details.
- `migrations/068.js`
- The new state should be cross-referenced with the controllers that manage it.
The accompanying tests should also be thoroughly reviewed.
Some files may appear new but have just moved and/or been renamed:
- `app/scripts/lib/rpc-method-middleware/handlers/request-accounts.js`
- This was previously implemented in `controllers/permissions/permissionsMethodMiddleware.js`.
- `test/mocks/permissions.js`
- A truncated version of `test/mocks/permission-controller.js`.
Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2021-12-07 04:16:49 +01:00
|
|
|
metamaskController.removeAllAccountPermissions.calledWith(
|
2020-11-03 00:41:28 +01:00
|
|
|
addressToRemove,
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-07-17 07:44:28 +02:00
|
|
|
it('should return address', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(ret, '0x1');
|
|
|
|
});
|
2022-06-21 21:03:54 +02:00
|
|
|
it('should call keyringController.getKeyringForAccount', async function () {
|
|
|
|
assert(
|
|
|
|
metamaskController.keyringController.getKeyringForAccount.calledWith(
|
|
|
|
addressToRemove,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('should call keyring.destroy', async function () {
|
|
|
|
assert(mockKeyring.destroy.calledOnce);
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-07-17 07:44:28 +02:00
|
|
|
|
2018-04-24 22:33:31 +02:00
|
|
|
describe('#setupUntrustedCommunication', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const mockTxParams = { from: TEST_ADDRESS };
|
2020-06-23 18:12:11 +02:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
initializeMockMiddlewareLog();
|
|
|
|
});
|
2020-06-23 18:12:11 +02:00
|
|
|
|
|
|
|
after(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
tearDownMockMiddlewareLog();
|
|
|
|
});
|
2020-06-23 18:12:11 +02:00
|
|
|
|
2020-05-07 23:21:47 +02:00
|
|
|
it('sets up phishing stream for untrusted communication', async function () {
|
2020-02-11 17:51:13 +01:00
|
|
|
const phishingMessageSender = {
|
|
|
|
url: 'http://myethereumwalletntw.com',
|
|
|
|
tab: {},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-08-17 18:56:07 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { promise, resolve } = deferredPromise();
|
2020-02-11 17:51:13 +01:00
|
|
|
const streamTest = createThoughStream((chunk, _, cb) => {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (chunk.name !== 'phishing') {
|
2021-02-04 19:15:23 +01:00
|
|
|
cb();
|
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
chunk.data.hostname,
|
|
|
|
new URL(phishingMessageSender.url).hostname,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
resolve();
|
|
|
|
cb();
|
|
|
|
});
|
2018-08-17 18:56:07 +02:00
|
|
|
|
2022-01-28 22:42:32 +01:00
|
|
|
metamaskController.setupUntrustedCommunication({
|
|
|
|
connectionStream: streamTest,
|
|
|
|
sender: phishingMessageSender,
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
await promise;
|
|
|
|
streamTest.end();
|
|
|
|
});
|
2020-02-20 23:39:00 +01:00
|
|
|
|
|
|
|
it('adds a tabId and origin to requests', function (done) {
|
|
|
|
const messageSender = {
|
|
|
|
url: 'http://mycrypto.com',
|
|
|
|
tab: { id: 456 },
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-02-20 23:39:00 +01:00
|
|
|
const streamTest = createThoughStream((chunk, _, cb) => {
|
|
|
|
if (chunk.data && chunk.data.method) {
|
2021-02-04 19:15:23 +01:00
|
|
|
cb(null, chunk);
|
|
|
|
return;
|
2020-02-20 23:39:00 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
cb();
|
|
|
|
});
|
2020-02-20 23:39:00 +01:00
|
|
|
|
2022-01-28 22:42:32 +01:00
|
|
|
metamaskController.setupUntrustedCommunication({
|
|
|
|
connectionStream: streamTest,
|
|
|
|
sender: messageSender,
|
|
|
|
});
|
2020-02-20 23:39:00 +01:00
|
|
|
|
|
|
|
const message = {
|
|
|
|
id: 1999133338649204,
|
|
|
|
jsonrpc: '2.0',
|
2020-06-23 18:12:11 +02:00
|
|
|
params: [{ ...mockTxParams }],
|
2020-02-20 23:39:00 +01:00
|
|
|
method: 'eth_sendTransaction',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-11-03 00:41:28 +01:00
|
|
|
streamTest.write(
|
|
|
|
{
|
2020-12-08 20:48:47 +01:00
|
|
|
name: 'metamask-provider',
|
2020-11-03 00:41:28 +01:00
|
|
|
data: message,
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
assert.deepStrictEqual(loggerMiddlewareMock.requests[0], {
|
2020-06-23 18:12:11 +02:00
|
|
|
...message,
|
|
|
|
origin: 'http://mycrypto.com',
|
|
|
|
tabId: 456,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2020-02-20 23:39:00 +01:00
|
|
|
|
|
|
|
it('should add only origin to request if tabId not provided', function (done) {
|
|
|
|
const messageSender = {
|
|
|
|
url: 'http://mycrypto.com',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-02-20 23:39:00 +01:00
|
|
|
const streamTest = createThoughStream((chunk, _, cb) => {
|
|
|
|
if (chunk.data && chunk.data.method) {
|
2021-02-04 19:15:23 +01:00
|
|
|
cb(null, chunk);
|
|
|
|
return;
|
2020-02-20 23:39:00 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
cb();
|
|
|
|
});
|
2020-02-20 23:39:00 +01:00
|
|
|
|
2022-01-28 22:42:32 +01:00
|
|
|
metamaskController.setupUntrustedCommunication({
|
|
|
|
connectionStream: streamTest,
|
|
|
|
sender: messageSender,
|
|
|
|
});
|
2020-02-20 23:39:00 +01:00
|
|
|
|
|
|
|
const message = {
|
|
|
|
id: 1999133338649204,
|
|
|
|
jsonrpc: '2.0',
|
2020-06-23 18:12:11 +02:00
|
|
|
params: [{ ...mockTxParams }],
|
2020-02-20 23:39:00 +01:00
|
|
|
method: 'eth_sendTransaction',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-11-03 00:41:28 +01:00
|
|
|
streamTest.write(
|
|
|
|
{
|
2020-12-08 20:48:47 +01:00
|
|
|
name: 'metamask-provider',
|
2020-11-03 00:41:28 +01:00
|
|
|
data: message,
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
assert.deepStrictEqual(loggerMiddlewareMock.requests[0], {
|
2020-06-23 18:12:11 +02:00
|
|
|
...message,
|
|
|
|
origin: 'http://mycrypto.com',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-04-24 22:33:31 +02:00
|
|
|
|
|
|
|
describe('#setupTrustedCommunication', function () {
|
2021-03-18 19:23:46 +01:00
|
|
|
it('sets up controller JSON-RPC api for trusted communication', async function () {
|
2019-12-20 16:32:31 +01:00
|
|
|
const messageSender = {
|
|
|
|
url: 'http://mycrypto.com',
|
|
|
|
tab: {},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
const { promise, resolve } = deferredPromise();
|
2020-02-11 17:51:13 +01:00
|
|
|
const streamTest = createThoughStream((chunk, _, cb) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(chunk.name, 'controller');
|
|
|
|
resolve();
|
|
|
|
cb();
|
|
|
|
});
|
2018-04-24 22:33:31 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
metamaskController.setupTrustedCommunication(streamTest, messageSender);
|
|
|
|
await promise;
|
|
|
|
streamTest.end();
|
|
|
|
});
|
|
|
|
});
|
2018-04-24 22:33:31 +02:00
|
|
|
|
2018-04-23 20:21:48 +02:00
|
|
|
describe('#markPasswordForgotten', function () {
|
|
|
|
it('adds and sets forgottenPassword to config data to true', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
metamaskController.markPasswordForgotten(noop);
|
|
|
|
const state = metamaskController.getState();
|
|
|
|
assert.equal(state.forgottenPassword, true);
|
|
|
|
});
|
|
|
|
});
|
2018-04-23 20:21:48 +02:00
|
|
|
|
|
|
|
describe('#unMarkPasswordForgotten', function () {
|
|
|
|
it('adds and sets forgottenPassword to config data to false', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
metamaskController.unMarkPasswordForgotten(noop);
|
|
|
|
const state = metamaskController.getState();
|
|
|
|
assert.equal(state.forgottenPassword, false);
|
|
|
|
});
|
|
|
|
});
|
2018-04-23 20:21:48 +02:00
|
|
|
|
2018-08-20 16:37:44 +02:00
|
|
|
describe('#_onKeyringControllerUpdate', function () {
|
|
|
|
it('should do nothing if there are no keyrings in state', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const syncAddresses = sinon.fake();
|
|
|
|
const syncWithAddresses = sinon.fake();
|
2018-08-20 16:37:44 +02:00
|
|
|
sandbox.replace(metamaskController, 'preferencesController', {
|
2020-03-20 20:37:27 +01:00
|
|
|
syncAddresses,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-08-20 16:37:44 +02:00
|
|
|
sandbox.replace(metamaskController, 'accountTracker', {
|
|
|
|
syncWithAddresses,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-08-20 16:37:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const oldState = metamaskController.getState();
|
|
|
|
await metamaskController._onKeyringControllerUpdate({ keyrings: [] });
|
2018-08-20 16:37:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.ok(syncAddresses.notCalled);
|
|
|
|
assert.ok(syncWithAddresses.notCalled);
|
|
|
|
assert.deepEqual(metamaskController.getState(), oldState);
|
|
|
|
});
|
2018-08-20 16:37:44 +02:00
|
|
|
|
2020-03-20 20:37:27 +01:00
|
|
|
it('should sync addresses if there are keyrings in state', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const syncAddresses = sinon.fake();
|
|
|
|
const syncWithAddresses = sinon.fake();
|
2018-08-20 16:37:44 +02:00
|
|
|
sandbox.replace(metamaskController, 'preferencesController', {
|
2020-03-20 20:37:27 +01:00
|
|
|
syncAddresses,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-08-20 16:37:44 +02:00
|
|
|
sandbox.replace(metamaskController, 'accountTracker', {
|
|
|
|
syncWithAddresses,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-08-20 16:37:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const oldState = metamaskController.getState();
|
2018-08-20 16:37:44 +02:00
|
|
|
await metamaskController._onKeyringControllerUpdate({
|
2020-11-03 00:41:28 +01:00
|
|
|
keyrings: [
|
|
|
|
{
|
|
|
|
accounts: ['0x1', '0x2'],
|
|
|
|
},
|
|
|
|
],
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-08-20 16:37:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.deepEqual(syncAddresses.args, [[['0x1', '0x2']]]);
|
|
|
|
assert.deepEqual(syncWithAddresses.args, [[['0x1', '0x2']]]);
|
|
|
|
assert.deepEqual(metamaskController.getState(), oldState);
|
|
|
|
});
|
2018-08-20 16:37:44 +02:00
|
|
|
|
|
|
|
it('should NOT update selected address if already unlocked', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const syncAddresses = sinon.fake();
|
|
|
|
const syncWithAddresses = sinon.fake();
|
2018-08-20 16:37:44 +02:00
|
|
|
sandbox.replace(metamaskController, 'preferencesController', {
|
2020-03-20 20:37:27 +01:00
|
|
|
syncAddresses,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-08-20 16:37:44 +02:00
|
|
|
sandbox.replace(metamaskController, 'accountTracker', {
|
|
|
|
syncWithAddresses,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-08-20 16:37:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const oldState = metamaskController.getState();
|
2018-08-20 16:37:44 +02:00
|
|
|
await metamaskController._onKeyringControllerUpdate({
|
|
|
|
isUnlocked: true,
|
2020-11-03 00:41:28 +01:00
|
|
|
keyrings: [
|
|
|
|
{
|
|
|
|
accounts: ['0x1', '0x2'],
|
|
|
|
},
|
|
|
|
],
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-08-20 16:37:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.deepEqual(syncAddresses.args, [[['0x1', '0x2']]]);
|
|
|
|
assert.deepEqual(syncWithAddresses.args, [[['0x1', '0x2']]]);
|
|
|
|
assert.deepEqual(metamaskController.getState(), oldState);
|
|
|
|
});
|
|
|
|
});
|
2022-06-01 19:09:13 +02:00
|
|
|
|
|
|
|
describe('markNotificationsAsRead', function () {
|
|
|
|
it('marks the notification as read', function () {
|
|
|
|
metamaskController.markNotificationsAsRead([NOTIFICATION_ID]);
|
2022-07-31 20:26:40 +02:00
|
|
|
const readNotification =
|
|
|
|
metamaskController.getState().notifications[NOTIFICATION_ID];
|
2022-06-01 19:09:13 +02:00
|
|
|
assert.notEqual(readNotification.readDate, null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('dismissNotifications', function () {
|
|
|
|
it('deletes the notification from state', function () {
|
|
|
|
metamaskController.dismissNotifications([NOTIFICATION_ID]);
|
|
|
|
const state = metamaskController.getState().notifications;
|
|
|
|
assert.ok(
|
|
|
|
!Object.values(state).includes(NOTIFICATION_ID),
|
|
|
|
'Object should not include the deleted notification',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2023-03-08 18:35:45 +01:00
|
|
|
|
|
|
|
describe('getTokenStandardAndDetails', function () {
|
|
|
|
it('gets token data from the token list if available, and with a balance retrieved by fetchTokenBalance', async function () {
|
|
|
|
const providerResultStub = {
|
|
|
|
eth_getCode: '0x123',
|
|
|
|
eth_call:
|
|
|
|
'0x00000000000000000000000000000000000000000000000029a2241af62c0000',
|
|
|
|
};
|
|
|
|
const { provider } = createTestProviderTools({
|
|
|
|
scaffold: providerResultStub,
|
|
|
|
networkId: '5',
|
|
|
|
chainId: '5',
|
|
|
|
});
|
|
|
|
|
|
|
|
const tokenData = {
|
|
|
|
decimals: 18,
|
|
|
|
symbol: 'DAI',
|
|
|
|
};
|
|
|
|
|
|
|
|
metamaskController.tokenListController.update(() => {
|
|
|
|
return {
|
|
|
|
tokenList: {
|
|
|
|
'0x6b175474e89094c44da98b954eedeac495271d0f': tokenData,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
metamaskController.provider = provider;
|
|
|
|
const tokenDetails = await metamaskController.getTokenStandardAndDetails(
|
|
|
|
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
|
|
|
|
'0xf0d172594caedee459b89ad44c94098e474571b6',
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.standard === 'ERC20',
|
|
|
|
'tokenDetails should include token standard in upper case',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.decimals === String(tokenData.decimals),
|
|
|
|
'tokenDetails should include token decimals as a string',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.symbol === tokenData.symbol,
|
|
|
|
'tokenDetails should include token symbol',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.balance === '3000000000000000000',
|
|
|
|
'tokenDetails should include a balance',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets token data from tokens if available, and with a balance retrieved by fetchTokenBalance', async function () {
|
|
|
|
const providerResultStub = {
|
|
|
|
eth_getCode: '0x123',
|
|
|
|
eth_call:
|
|
|
|
'0x00000000000000000000000000000000000000000000000029a2241af62c0000',
|
|
|
|
};
|
|
|
|
const { provider } = createTestProviderTools({
|
|
|
|
scaffold: providerResultStub,
|
|
|
|
networkId: '5',
|
|
|
|
chainId: '5',
|
|
|
|
});
|
|
|
|
|
|
|
|
const tokenData = {
|
|
|
|
decimals: 18,
|
|
|
|
symbol: 'DAI',
|
|
|
|
};
|
|
|
|
|
|
|
|
metamaskController.tokensController.update({
|
|
|
|
tokens: [
|
|
|
|
{
|
|
|
|
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
|
|
|
|
...tokenData,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
metamaskController.provider = provider;
|
|
|
|
const tokenDetails = await metamaskController.getTokenStandardAndDetails(
|
|
|
|
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
|
|
|
|
'0xf0d172594caedee459b89ad44c94098e474571b6',
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.standard === 'ERC20',
|
|
|
|
'tokenDetails should include token standard in upper case',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.decimals === String(tokenData.decimals),
|
|
|
|
'tokenDetails should include token decimals as a string',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.symbol === tokenData.symbol,
|
|
|
|
'tokenDetails should include token symbol',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.balance === '3000000000000000000',
|
|
|
|
'tokenDetails should include a balance',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets token data from contract-metadata if available, and with a balance retrieved by fetchTokenBalance', async function () {
|
|
|
|
const providerResultStub = {
|
|
|
|
eth_getCode: '0x123',
|
|
|
|
eth_call:
|
|
|
|
'0x00000000000000000000000000000000000000000000000029a2241af62c0000',
|
|
|
|
};
|
|
|
|
const { provider } = createTestProviderTools({
|
|
|
|
scaffold: providerResultStub,
|
|
|
|
networkId: '5',
|
|
|
|
chainId: '5',
|
|
|
|
});
|
|
|
|
|
|
|
|
metamaskController.provider = provider;
|
|
|
|
const tokenDetails = await metamaskController.getTokenStandardAndDetails(
|
|
|
|
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
|
|
|
|
'0xf0d172594caedee459b89ad44c94098e474571b6',
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.standard === 'ERC20',
|
|
|
|
'tokenDetails should include token standard in upper case',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.decimals === '18',
|
|
|
|
'tokenDetails should include token decimals as a string',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.symbol === 'DAI',
|
|
|
|
'tokenDetails should include token symbol',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.balance === '3000000000000000000',
|
|
|
|
'tokenDetails should include a balance',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets token data from the blockchain, via the assetsContractController, if not available through other sources', async function () {
|
|
|
|
const providerResultStub = {
|
|
|
|
eth_getCode: '0x123',
|
|
|
|
eth_call:
|
|
|
|
'0x00000000000000000000000000000000000000000000000029a2241af62c0000',
|
|
|
|
};
|
|
|
|
const { provider } = createTestProviderTools({
|
|
|
|
scaffold: providerResultStub,
|
|
|
|
networkId: '5',
|
|
|
|
chainId: '5',
|
|
|
|
});
|
|
|
|
|
|
|
|
const tokenData = {
|
|
|
|
standard: 'ERC20',
|
|
|
|
decimals: 18,
|
|
|
|
symbol: 'DAI',
|
|
|
|
balance: '333',
|
|
|
|
};
|
|
|
|
|
|
|
|
metamaskController.tokenListController.update(() => {
|
|
|
|
return {
|
|
|
|
tokenList: {
|
|
|
|
'0x6b175474e89094c44da98b954eedeac495271d0f': {},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
metamaskController.provider = provider;
|
|
|
|
|
|
|
|
sandbox
|
|
|
|
.stub(
|
|
|
|
metamaskController.assetsContractController,
|
|
|
|
'getTokenStandardAndDetails',
|
|
|
|
)
|
|
|
|
.callsFake(() => {
|
|
|
|
return tokenData;
|
|
|
|
});
|
|
|
|
|
|
|
|
const tokenDetails = await metamaskController.getTokenStandardAndDetails(
|
|
|
|
'0xNotInTokenList',
|
|
|
|
'0xf0d172594caedee459b89ad44c94098e474571b6',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.standard === tokenData.standard.toUpperCase(),
|
|
|
|
'tokenDetails should include token standard in upper case',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.decimals === String(tokenData.decimals),
|
|
|
|
'tokenDetails should include token decimals as a string',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.symbol === tokenData.symbol,
|
|
|
|
'tokenDetails should include token symbol',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.balance === tokenData.balance,
|
|
|
|
'tokenDetails should include a balance',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets token data from the blockchain, via the assetsContractController, if it is in the token list but is an ERC721', async function () {
|
|
|
|
const providerResultStub = {
|
|
|
|
eth_getCode: '0x123',
|
|
|
|
eth_call:
|
|
|
|
'0x00000000000000000000000000000000000000000000000029a2241af62c0000',
|
|
|
|
};
|
|
|
|
const { provider } = createTestProviderTools({
|
|
|
|
scaffold: providerResultStub,
|
|
|
|
networkId: '5',
|
|
|
|
chainId: '5',
|
|
|
|
});
|
|
|
|
|
|
|
|
const tokenData = {
|
|
|
|
standard: 'ERC721',
|
|
|
|
decimals: 18,
|
|
|
|
symbol: 'DAI',
|
|
|
|
balance: '333',
|
|
|
|
};
|
|
|
|
|
|
|
|
metamaskController.tokenListController.update(() => {
|
|
|
|
return {
|
|
|
|
tokenList: {
|
|
|
|
'0xaaa75474e89094c44da98b954eedeac495271d0f': tokenData,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
metamaskController.provider = provider;
|
|
|
|
|
|
|
|
sandbox
|
|
|
|
.stub(
|
|
|
|
metamaskController.assetsContractController,
|
|
|
|
'getTokenStandardAndDetails',
|
|
|
|
)
|
|
|
|
.callsFake(() => {
|
|
|
|
return tokenData;
|
|
|
|
});
|
|
|
|
|
|
|
|
const tokenDetails = await metamaskController.getTokenStandardAndDetails(
|
|
|
|
'0xAAA75474e89094c44da98b954eedeac495271d0f',
|
|
|
|
'0xf0d172594caedee459b89ad44c94098e474571b6',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.standard === tokenData.standard.toUpperCase(),
|
|
|
|
'tokenDetails should include token standard in upper case',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.decimals === String(tokenData.decimals),
|
|
|
|
'tokenDetails should include token decimals as a string',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.symbol === tokenData.symbol,
|
|
|
|
'tokenDetails should include token symbol',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.balance === tokenData.balance,
|
|
|
|
'tokenDetails should include a balance',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets token data from the blockchain, via the assetsContractController, if it is in the token list but is an ERC1155', async function () {
|
|
|
|
const providerResultStub = {
|
|
|
|
eth_getCode: '0x123',
|
|
|
|
eth_call:
|
|
|
|
'0x00000000000000000000000000000000000000000000000029a2241af62c0000',
|
|
|
|
};
|
|
|
|
const { provider } = createTestProviderTools({
|
|
|
|
scaffold: providerResultStub,
|
|
|
|
networkId: '5',
|
|
|
|
chainId: '5',
|
|
|
|
});
|
|
|
|
|
|
|
|
const tokenData = {
|
|
|
|
standard: 'ERC1155',
|
|
|
|
decimals: 18,
|
|
|
|
symbol: 'DAI',
|
|
|
|
balance: '333',
|
|
|
|
};
|
|
|
|
|
|
|
|
metamaskController.tokenListController.update(() => {
|
|
|
|
return {
|
|
|
|
tokenList: {
|
|
|
|
'0xaaa75474e89094c44da98b954eedeac495271d0f': tokenData,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
metamaskController.provider = provider;
|
|
|
|
|
|
|
|
sandbox
|
|
|
|
.stub(
|
|
|
|
metamaskController.assetsContractController,
|
|
|
|
'getTokenStandardAndDetails',
|
|
|
|
)
|
|
|
|
.callsFake(() => {
|
|
|
|
return tokenData;
|
|
|
|
});
|
|
|
|
|
|
|
|
const tokenDetails = await metamaskController.getTokenStandardAndDetails(
|
|
|
|
'0xAAA75474e89094c44da98b954eedeac495271d0f',
|
|
|
|
'0xf0d172594caedee459b89ad44c94098e474571b6',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.standard === tokenData.standard.toUpperCase(),
|
|
|
|
'tokenDetails should include token standard in upper case',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.decimals === String(tokenData.decimals),
|
|
|
|
'tokenDetails should include token decimals as a string',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.symbol === tokenData.symbol,
|
|
|
|
'tokenDetails should include token symbol',
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
tokenDetails.balance === tokenData.balance,
|
|
|
|
'tokenDetails should include a balance',
|
|
|
|
);
|
|
|
|
});
|
2023-03-09 22:00:28 +01:00
|
|
|
|
|
|
|
describe('findNetworkConfigurationBy', function () {
|
|
|
|
it('returns null if passed an object containing a valid networkConfiguration key but no matching value is found', function () {
|
|
|
|
assert.strictEqual(
|
|
|
|
metamaskController.findNetworkConfigurationBy({
|
|
|
|
chainId: '0xnone',
|
|
|
|
}),
|
|
|
|
null,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('returns null if passed an object containing an invalid networkConfiguration key', function () {
|
|
|
|
assert.strictEqual(
|
|
|
|
metamaskController.findNetworkConfigurationBy({
|
|
|
|
invalidKey: '0xnone',
|
|
|
|
}),
|
|
|
|
null,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns matching networkConfiguration when passed a chainId that matches an existing configuration', function () {
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
metamaskController.findNetworkConfigurationBy({
|
|
|
|
chainId: MAINNET_CHAIN_ID,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
chainId: MAINNET_CHAIN_ID,
|
|
|
|
nickname: 'Alt Mainnet',
|
|
|
|
id: NETWORK_CONFIGURATION_ID_1,
|
|
|
|
rpcUrl: ALT_MAINNET_RPC_URL,
|
|
|
|
ticker: ETH,
|
|
|
|
type: NETWORK_TYPES.RPC,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns matching networkConfiguration when passed a ticker that matches an existing configuration', function () {
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
metamaskController.findNetworkConfigurationBy({
|
|
|
|
ticker: MATIC,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
rpcUrl: POLYGON_RPC_URL,
|
|
|
|
type: NETWORK_TYPES.RPC,
|
|
|
|
chainId: POLYGON_CHAIN_ID,
|
|
|
|
ticker: MATIC,
|
|
|
|
nickname: 'Polygon',
|
|
|
|
id: NETWORK_CONFIGURATION_ID_2,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns matching networkConfiguration when passed a nickname that matches an existing configuration', function () {
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
metamaskController.findNetworkConfigurationBy({
|
|
|
|
nickname: 'Alt Mainnet',
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
chainId: MAINNET_CHAIN_ID,
|
|
|
|
nickname: 'Alt Mainnet',
|
|
|
|
id: NETWORK_CONFIGURATION_ID_1,
|
|
|
|
rpcUrl: ALT_MAINNET_RPC_URL,
|
|
|
|
ticker: ETH,
|
|
|
|
type: NETWORK_TYPES.RPC,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns null if passed an object containing mismatched networkConfiguration key/value combination', function () {
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
metamaskController.findNetworkConfigurationBy({
|
|
|
|
nickname: MAINNET_CHAIN_ID,
|
|
|
|
}),
|
|
|
|
null,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the first networkConfiguration added if passed an key/value combination for which there are multiple matching configurations', function () {
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
metamaskController.findNetworkConfigurationBy({
|
|
|
|
chainId: POLYGON_CHAIN_ID,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
rpcUrl: POLYGON_RPC_URL,
|
|
|
|
type: NETWORK_TYPES.RPC,
|
|
|
|
chainId: POLYGON_CHAIN_ID,
|
|
|
|
ticker: MATIC,
|
|
|
|
nickname: 'Polygon',
|
|
|
|
id: NETWORK_CONFIGURATION_ID_2,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2023-03-08 18:35:45 +01:00
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|