|
| 1 | +import { RpcEndpointType } from '@metamask/network-controller'; |
| 2 | +import { |
| 3 | + ClientConfigApiService, |
| 4 | + ClientType, |
| 5 | + DistributionType, |
| 6 | + EnvironmentType, |
| 7 | +} from '@metamask/remote-feature-flag-controller'; |
| 8 | +import { TransactionController } from '@metamask/transaction-controller'; |
| 9 | +import { enableNetConnect } from 'nock'; |
| 10 | + |
| 11 | +import { startAnvil } from '../test/anvil'; |
| 12 | +import type { AnvilInstance } from '../test/anvil'; |
| 13 | +import * as initializationModule from './initialization'; |
| 14 | +import { |
| 15 | + createSecretRecoveryPhrase, |
| 16 | + importSecretRecoveryPhrase, |
| 17 | + sendTransaction, |
| 18 | +} from './utilities'; |
| 19 | +import { Wallet } from './Wallet'; |
| 20 | + |
| 21 | +const TEST_PHRASE = |
| 22 | + 'test test test test test test test test test test test ball'; |
| 23 | +const TEST_PASSWORD = 'testpass'; |
| 24 | + |
| 25 | +async function setupWallet(): Promise<Wallet> { |
| 26 | + const wallet = new Wallet({ |
| 27 | + infuraProjectId: 'fake-infura-project-id', |
| 28 | + clientVersion: '1.0.0', |
| 29 | + showApprovalRequest: (): undefined => undefined, |
| 30 | + clientConfigApiService: new ClientConfigApiService({ |
| 31 | + fetch: globalThis.fetch, |
| 32 | + config: { |
| 33 | + client: ClientType.Extension, |
| 34 | + distribution: DistributionType.Main, |
| 35 | + environment: EnvironmentType.Production, |
| 36 | + }, |
| 37 | + }), |
| 38 | + getMetaMetricsId: (): string => 'fake-metrics-id', |
| 39 | + }); |
| 40 | + |
| 41 | + await importSecretRecoveryPhrase(wallet, TEST_PASSWORD, TEST_PHRASE); |
| 42 | + |
| 43 | + return wallet; |
| 44 | +} |
| 45 | + |
| 46 | +describe('Wallet', () => { |
| 47 | + let wallet: Wallet; |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + jest.useFakeTimers({ doNotFake: ['nextTick', 'queueMicrotask'] }); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(async () => { |
| 54 | + await wallet?.destroy(); |
| 55 | + enableNetConnect(); |
| 56 | + jest.useRealTimers(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('can unlock and populate accounts', async () => { |
| 60 | + wallet = await setupWallet(); |
| 61 | + const { messenger } = wallet; |
| 62 | + |
| 63 | + expect( |
| 64 | + messenger |
| 65 | + .call('AccountsController:listAccounts') |
| 66 | + .map((account) => account.address), |
| 67 | + ).toStrictEqual(['0xc6d5a3c98ec9073b54fa0969957bd582e8d874bf']); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('with local chain', () => { |
| 71 | + let anvil: AnvilInstance; |
| 72 | + |
| 73 | + beforeAll(async () => { |
| 74 | + anvil = await startAnvil({ mnemonic: TEST_PHRASE }); |
| 75 | + }); |
| 76 | + |
| 77 | + afterAll(async () => { |
| 78 | + await anvil?.stop(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('signs transactions', async () => { |
| 82 | + enableNetConnect(); |
| 83 | + |
| 84 | + wallet = await setupWallet(); |
| 85 | + |
| 86 | + const networkConfig = wallet.messenger.call( |
| 87 | + 'NetworkController:addNetwork', |
| 88 | + { |
| 89 | + chainId: '0x7a69', |
| 90 | + name: 'Anvil', |
| 91 | + nativeCurrency: 'ETH', |
| 92 | + blockExplorerUrls: [], |
| 93 | + defaultRpcEndpointIndex: 0, |
| 94 | + rpcEndpoints: [ |
| 95 | + { |
| 96 | + type: RpcEndpointType.Custom, |
| 97 | + url: anvil.rpcUrl, |
| 98 | + }, |
| 99 | + ], |
| 100 | + }, |
| 101 | + ); |
| 102 | + |
| 103 | + const { networkClientId } = networkConfig.rpcEndpoints[0]; |
| 104 | + |
| 105 | + const addresses = wallet.messenger |
| 106 | + .call('AccountsController:listAccounts') |
| 107 | + .map((account) => account.address); |
| 108 | + |
| 109 | + const { result, transactionMeta } = await sendTransaction( |
| 110 | + wallet, |
| 111 | + { from: addresses[0], to: addresses[0] }, |
| 112 | + { networkClientId }, |
| 113 | + ); |
| 114 | + |
| 115 | + // Advance timers by an arbitrary value to trigger downstream timer logic. |
| 116 | + const hash = await jest |
| 117 | + .advanceTimersByTimeAsync(60_000) |
| 118 | + .then(() => result); |
| 119 | + |
| 120 | + expect(hash).toStrictEqual(expect.any(String)); |
| 121 | + expect(transactionMeta).toStrictEqual( |
| 122 | + expect.objectContaining({ |
| 123 | + txParams: expect.objectContaining({ |
| 124 | + from: addresses[0], |
| 125 | + to: addresses[0], |
| 126 | + value: '0x0', |
| 127 | + type: '0x2', |
| 128 | + }), |
| 129 | + }), |
| 130 | + ); |
| 131 | + }, 15_000); |
| 132 | + }); |
| 133 | + |
| 134 | + it('can create secret recovery phrase', async () => { |
| 135 | + wallet = new Wallet({ |
| 136 | + infuraProjectId: 'fake-infura-project-id', |
| 137 | + clientVersion: '1.0.0', |
| 138 | + showApprovalRequest: (): undefined => undefined, |
| 139 | + clientConfigApiService: new ClientConfigApiService({ |
| 140 | + fetch: globalThis.fetch, |
| 141 | + config: { |
| 142 | + client: ClientType.Extension, |
| 143 | + distribution: DistributionType.Main, |
| 144 | + environment: EnvironmentType.Production, |
| 145 | + }, |
| 146 | + }), |
| 147 | + getMetaMetricsId: (): string => 'fake-metrics-id', |
| 148 | + }); |
| 149 | + |
| 150 | + await createSecretRecoveryPhrase(wallet, TEST_PASSWORD); |
| 151 | + |
| 152 | + expect( |
| 153 | + wallet.messenger.call('AccountsController:listAccounts'), |
| 154 | + ).toHaveLength(1); |
| 155 | + }); |
| 156 | + |
| 157 | + it('exposes state', async () => { |
| 158 | + wallet = await setupWallet(); |
| 159 | + const { state } = wallet; |
| 160 | + |
| 161 | + expect(state.KeyringController).toStrictEqual({ |
| 162 | + isUnlocked: true, |
| 163 | + keyrings: expect.any(Array), |
| 164 | + encryptionKey: expect.any(String), |
| 165 | + encryptionSalt: expect.any(String), |
| 166 | + vault: expect.any(String), |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + describe('lifecycle', () => { |
| 171 | + const options = { |
| 172 | + infuraProjectId: 'fake-infura-project-id', |
| 173 | + clientVersion: '1.0.0', |
| 174 | + showApprovalRequest: (): undefined => undefined, |
| 175 | + clientConfigApiService: new ClientConfigApiService({ |
| 176 | + fetch: globalThis.fetch, |
| 177 | + config: { |
| 178 | + client: ClientType.Extension, |
| 179 | + distribution: DistributionType.Main, |
| 180 | + environment: EnvironmentType.Production, |
| 181 | + }, |
| 182 | + }), |
| 183 | + getMetaMetricsId: (): string => 'fake-metrics-id', |
| 184 | + }; |
| 185 | + |
| 186 | + it('exposes controllerMetadata for each initialized controller', () => { |
| 187 | + wallet = new Wallet(options); |
| 188 | + |
| 189 | + const names = Object.keys(wallet.controllerMetadata); |
| 190 | + expect(names).toStrictEqual(Object.keys(wallet.state)); |
| 191 | + for (const name of names) { |
| 192 | + expect(wallet.controllerMetadata[name]).toBeDefined(); |
| 193 | + } |
| 194 | + }); |
| 195 | + |
| 196 | + it('omits instances without a metadata property from controllerMetadata', () => { |
| 197 | + const fakeMetadata = { |
| 198 | + foo: { persist: true, includeInDebugSnapshot: false }, |
| 199 | + }; |
| 200 | + jest.spyOn(initializationModule, 'initialize').mockReturnValueOnce({ |
| 201 | + WithMeta: { state: {}, metadata: fakeMetadata }, |
| 202 | + NoMeta: { state: {} }, |
| 203 | + } as never); |
| 204 | + |
| 205 | + wallet = new Wallet(options); |
| 206 | + |
| 207 | + expect(wallet.controllerMetadata).toStrictEqual({ |
| 208 | + WithMeta: fakeMetadata, |
| 209 | + }); |
| 210 | + expect(Object.keys(wallet.state)).toStrictEqual(['WithMeta', 'NoMeta']); |
| 211 | + }); |
| 212 | + |
| 213 | + it('publishes Wallet:destroyed exactly once on destroy', async () => { |
| 214 | + wallet = new Wallet(options); |
| 215 | + |
| 216 | + const listener = jest.fn(); |
| 217 | + wallet.messenger.subscribe('Wallet:destroyed', listener); |
| 218 | + |
| 219 | + await wallet.destroy(); |
| 220 | + await wallet.destroy(); |
| 221 | + |
| 222 | + expect(listener).toHaveBeenCalledTimes(1); |
| 223 | + }); |
| 224 | + |
| 225 | + it('publishes Wallet:destroyed even if a controller destroy throws synchronously', async () => { |
| 226 | + wallet = new Wallet(options); |
| 227 | + |
| 228 | + jest |
| 229 | + .spyOn(TransactionController.prototype, 'destroy') |
| 230 | + .mockImplementation(() => { |
| 231 | + throw new Error('sync destroy error'); |
| 232 | + }); |
| 233 | + |
| 234 | + const listener = jest.fn(); |
| 235 | + wallet.messenger.subscribe('Wallet:destroyed', listener); |
| 236 | + |
| 237 | + await wallet.destroy(); |
| 238 | + |
| 239 | + expect(listener).toHaveBeenCalledTimes(1); |
| 240 | + }); |
| 241 | + |
| 242 | + it('publishes Wallet:destroyed even if a controller destroy rejects', async () => { |
| 243 | + wallet = new Wallet(options); |
| 244 | + |
| 245 | + jest |
| 246 | + .spyOn(TransactionController.prototype, 'destroy') |
| 247 | + .mockRejectedValue(new Error('async destroy error') as never); |
| 248 | + |
| 249 | + const listener = jest.fn(); |
| 250 | + wallet.messenger.subscribe('Wallet:destroyed', listener); |
| 251 | + |
| 252 | + await wallet.destroy(); |
| 253 | + |
| 254 | + expect(listener).toHaveBeenCalledTimes(1); |
| 255 | + }); |
| 256 | + }); |
| 257 | +}); |
0 commit comments