|
1 | 1 | import { ControllerMessenger } from '@metamask/base-controller';
|
| 2 | +import type { |
| 3 | + AccountAssetListUpdatedEventPayload, |
| 4 | + AccountBalancesUpdatedEventPayload, |
| 5 | + AccountTransactionsUpdatedEventPayload, |
| 6 | +} from '@metamask/keyring-api'; |
2 | 7 | import {
|
3 | 8 | BtcAccountType,
|
4 | 9 | EthAccountType,
|
@@ -134,7 +139,7 @@ const mockAccount4: InternalAccount = {
|
134 | 139 | };
|
135 | 140 |
|
136 | 141 | class MockNormalAccountUUID {
|
137 |
| - #accountIds: Record<string, string> = {}; |
| 142 | + readonly #accountIds: Record<string, string> = {}; |
138 | 143 |
|
139 | 144 | constructor(accounts: InternalAccount[]) {
|
140 | 145 | for (const account of accounts) {
|
@@ -299,6 +304,9 @@ function buildAccountsControllerMessenger(messenger = buildMessenger()) {
|
299 | 304 | allowedEvents: [
|
300 | 305 | 'SnapController:stateChange',
|
301 | 306 | 'KeyringController:stateChange',
|
| 307 | + 'SnapKeyring:accountAssetListUpdated', |
| 308 | + 'SnapKeyring:accountBalancesUpdated', |
| 309 | + 'SnapKeyring:accountTransactionsUpdated', |
302 | 310 | ],
|
303 | 311 | allowedActions: [
|
304 | 312 | 'KeyringController:getAccounts',
|
@@ -1393,6 +1401,119 @@ describe('AccountsController', () => {
|
1393 | 1401 | );
|
1394 | 1402 | });
|
1395 | 1403 |
|
| 1404 | + describe('onSnapKeyringEvents', () => { |
| 1405 | + const setupTest = () => { |
| 1406 | + const account = createExpectedInternalAccount({ |
| 1407 | + id: 'mock-id', |
| 1408 | + name: 'Bitcoin Account', |
| 1409 | + address: 'tb1q4q7h8wuplrpmkxqvv6rrrq7qyhhjsj5uqcsxqu', |
| 1410 | + keyringType: KeyringTypes.snap, |
| 1411 | + snapId: 'mock-snap', |
| 1412 | + type: BtcAccountType.P2wpkh, |
| 1413 | + }); |
| 1414 | + |
| 1415 | + const messenger = buildMessenger(); |
| 1416 | + const { accountsController } = setupAccountsController({ |
| 1417 | + initialState: { |
| 1418 | + internalAccounts: { |
| 1419 | + accounts: { |
| 1420 | + [account.id]: account, |
| 1421 | + }, |
| 1422 | + selectedAccount: account.id, |
| 1423 | + }, |
| 1424 | + }, |
| 1425 | + messenger, |
| 1426 | + }); |
| 1427 | + |
| 1428 | + return { messenger, account, accountsController }; |
| 1429 | + }; |
| 1430 | + |
| 1431 | + it('re-publishes keyring events: SnapKeyring:accountBalancesUpdated', () => { |
| 1432 | + const { account, messenger } = setupTest(); |
| 1433 | + |
| 1434 | + const payload: AccountBalancesUpdatedEventPayload = { |
| 1435 | + balances: { |
| 1436 | + [account.id]: { |
| 1437 | + 'bip122:000000000019d6689c085ae165831e93/slip44:0': { |
| 1438 | + amount: '0.1', |
| 1439 | + unit: 'BTC', |
| 1440 | + }, |
| 1441 | + }, |
| 1442 | + }, |
| 1443 | + }; |
| 1444 | + |
| 1445 | + const mockRePublishedCallback = jest.fn(); |
| 1446 | + messenger.subscribe( |
| 1447 | + 'AccountsController:accountBalancesUpdated', |
| 1448 | + mockRePublishedCallback, |
| 1449 | + ); |
| 1450 | + messenger.publish('SnapKeyring:accountBalancesUpdated', payload); |
| 1451 | + expect(mockRePublishedCallback).toHaveBeenCalledWith(payload); |
| 1452 | + }); |
| 1453 | + |
| 1454 | + it('re-publishes keyring events: SnapKeyring:accountAssetListUpdated', () => { |
| 1455 | + const { account, messenger } = setupTest(); |
| 1456 | + |
| 1457 | + const payload: AccountAssetListUpdatedEventPayload = { |
| 1458 | + assets: { |
| 1459 | + [account.id]: { |
| 1460 | + added: ['bip122:000000000019d6689c085ae165831e93/slip44:0'], |
| 1461 | + removed: ['bip122:000000000933ea01ad0ee984209779ba/slip44:0'], |
| 1462 | + }, |
| 1463 | + }, |
| 1464 | + }; |
| 1465 | + |
| 1466 | + const mockRePublishedCallback = jest.fn(); |
| 1467 | + messenger.subscribe( |
| 1468 | + 'AccountsController:accountAssetListUpdated', |
| 1469 | + mockRePublishedCallback, |
| 1470 | + ); |
| 1471 | + messenger.publish('SnapKeyring:accountAssetListUpdated', payload); |
| 1472 | + expect(mockRePublishedCallback).toHaveBeenCalledWith(payload); |
| 1473 | + }); |
| 1474 | + |
| 1475 | + it('re-publishes keyring events: SnapKeyring:accountTransactionsUpdated', () => { |
| 1476 | + const { account, messenger } = setupTest(); |
| 1477 | + |
| 1478 | + const payload: AccountTransactionsUpdatedEventPayload = { |
| 1479 | + transactions: { |
| 1480 | + [account.id]: [ |
| 1481 | + { |
| 1482 | + id: 'f5d8ee39a430901c91a5917b9f2dc19d6d1a0e9cea205b009ca73dd04470b9a6', |
| 1483 | + timestamp: null, |
| 1484 | + chain: 'bip122:000000000019d6689c085ae165831e93', |
| 1485 | + status: 'submitted', |
| 1486 | + type: 'receive', |
| 1487 | + account: account.id, |
| 1488 | + from: [], |
| 1489 | + to: [], |
| 1490 | + fees: [ |
| 1491 | + { |
| 1492 | + type: 'base', |
| 1493 | + asset: { |
| 1494 | + fungible: true, |
| 1495 | + type: 'bip122:000000000019d6689c085ae165831e93/slip44:0', |
| 1496 | + unit: 'BTC', |
| 1497 | + amount: '0.0001', |
| 1498 | + }, |
| 1499 | + }, |
| 1500 | + ], |
| 1501 | + events: [], |
| 1502 | + }, |
| 1503 | + ], |
| 1504 | + }, |
| 1505 | + }; |
| 1506 | + |
| 1507 | + const mockRePublishedCallback = jest.fn(); |
| 1508 | + messenger.subscribe( |
| 1509 | + 'AccountsController:accountTransactionsUpdated', |
| 1510 | + mockRePublishedCallback, |
| 1511 | + ); |
| 1512 | + messenger.publish('SnapKeyring:accountTransactionsUpdated', payload); |
| 1513 | + expect(mockRePublishedCallback).toHaveBeenCalledWith(payload); |
| 1514 | + }); |
| 1515 | + }); |
| 1516 | + |
1396 | 1517 | describe('updateAccounts', () => {
|
1397 | 1518 | const mockAddress1 = '0x123';
|
1398 | 1519 | const mockAddress2 = '0x456';
|
|
0 commit comments