diff --git a/packages/tron-wallet-snap/CHANGELOG.md b/packages/tron-wallet-snap/CHANGELOG.md index 4cc38345..2509a5c7 100644 --- a/packages/tron-wallet-snap/CHANGELOG.md +++ b/packages/tron-wallet-snap/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add Core messenger plumbing (`getMessenger`, `RemoteFeatureFlagsProvider`, `AssetsProvider`) for upcoming AssetsController migration ([#95](https://github.com/MetaMask/internal-snaps/pull/95)) +### Fixed + +- Scope `bip44:discover` activity checks and account creation to the networks declared in the snap manifest, preventing unnecessary calls to testnet APIs during discovery ([#135](https://github.com/MetaMask/internal-snaps/pull/135)) + ## [3.0.0] ### Added diff --git a/packages/tron-wallet-snap/snap.manifest.json b/packages/tron-wallet-snap/snap.manifest.json index 30ac1291..6a08ba2b 100644 --- a/packages/tron-wallet-snap/snap.manifest.json +++ b/packages/tron-wallet-snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/internal-snaps.git" }, "source": { - "shasum": "B9kqmuUcYgPAD3ojseIm2HymqK77Q4NEzULU876+5Wc=", + "shasum": "C13HWdtafD3wHLxY5EH6CQlnUuu8RQoe1YcaTX+P+rE=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/tron-wallet-snap/src/services/accounts/AccountsService.test.ts b/packages/tron-wallet-snap/src/services/accounts/AccountsService.test.ts index e8bdd0a7..d9b467ad 100644 --- a/packages/tron-wallet-snap/src/services/accounts/AccountsService.test.ts +++ b/packages/tron-wallet-snap/src/services/accounts/AccountsService.test.ts @@ -12,7 +12,6 @@ import { AccountCreationType, KeyringEvent, TrxAccountType, - TrxScope, } from '@metamask/keyring-api'; import { emitSnapKeyringEvent, @@ -30,7 +29,7 @@ import type { ConfigProvider } from '../config'; import type { Config } from '../config/ConfigProvider'; import type { TransactionsService } from '../transactions/TransactionsService'; import type { AccountsRepository } from './AccountsRepository'; -import { AccountsService } from './AccountsService'; +import { AccountsService, SUPPORTED_SCOPES } from './AccountsService'; jest.mock('@metamask/keyring-snap-sdk', () => ({ emitSnapKeyringEvent: jest.fn(), @@ -333,7 +332,7 @@ describe('AccountsService', () => { derivationPath: "m/44'/195'/0'/0/0", index: 0, type: TrxAccountType.Eoa, - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES, methods: ['signMessage', 'signTransaction'], }); expect(result.id).toBeDefined(); @@ -483,7 +482,7 @@ describe('AccountsService', () => { index: 0, type: TrxAccountType.Eoa, address: 'TConcurrent0', - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: {}, methods: ['signMessage', 'signTransaction'], }; @@ -516,7 +515,7 @@ describe('AccountsService', () => { index: 0, type: TrxAccountType.Eoa, address: 'TExisting0', - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: {}, methods: ['signMessage', 'signTransaction'], }; @@ -527,7 +526,7 @@ describe('AccountsService', () => { index: 1, type: TrxAccountType.Eoa, address: 'TExisting1', - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: {}, methods: ['signMessage', 'signTransaction'], }; @@ -636,9 +635,9 @@ describe('AccountsService', () => { mockAccountsRepository, mockTransactionsService, }) => { - mockTransactionsService.checkAddressActivity - .mockResolvedValueOnce(false) - .mockResolvedValueOnce(true); + mockTransactionsService.checkAddressActivity.mockResolvedValueOnce( + true, + ); const result = await accountsService.createAccounts({ type: AccountCreationType.Bip44Discover, @@ -673,7 +672,7 @@ describe('AccountsService', () => { index: 0, type: TrxAccountType.Eoa, address: 'TTestAddress1234567890123456789', - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: { entropy: { type: 'mnemonic', @@ -716,7 +715,7 @@ describe('AccountsService', () => { index: 0, type: TrxAccountType.Eoa, address: 'TExistingDefault0', - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: {}, methods: ['signMessage', 'signTransaction'], }; @@ -733,7 +732,7 @@ describe('AccountsService', () => { index: 1, type: TrxAccountType.Eoa, address: 'TDefaultCreate1', - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: { entropy: { type: 'mnemonic', @@ -769,7 +768,7 @@ describe('AccountsService', () => { index: 0, type: TrxAccountType.Eoa, address: 'TExisting123456789012345678901', - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: {}, methods: ['signMessage', 'signTransaction'], }; @@ -804,7 +803,7 @@ describe('AccountsService', () => { index: 0, type: TrxAccountType.Eoa, address: 'TRollback12345678901234567890', - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: { entropy: { type: 'mnemonic', @@ -849,7 +848,7 @@ describe('AccountsService', () => { index: 0, type: TrxAccountType.Eoa, address: 'TRollback12345678901234567890', - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: { entropy: { type: 'mnemonic', @@ -892,7 +891,7 @@ describe('AccountsService', () => { index: 0, type: TrxAccountType.Eoa, address: 'TMeta1234567890123456789012', - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: {}, methods: ['signMessage', 'signTransaction'], }); @@ -921,7 +920,7 @@ describe('AccountsService', () => { index: 0, type: TrxAccountType.Eoa, address: 'TConflict12345678901234567890', - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: {}, methods: ['signMessage', 'signTransaction'], }; diff --git a/packages/tron-wallet-snap/src/services/accounts/AccountsService.ts b/packages/tron-wallet-snap/src/services/accounts/AccountsService.ts index 3d9bd1b7..3f2674fe 100644 --- a/packages/tron-wallet-snap/src/services/accounts/AccountsService.ts +++ b/packages/tron-wallet-snap/src/services/accounts/AccountsService.ts @@ -9,7 +9,6 @@ import { assertCreateAccountOptionIsSupported, KeyringEvent, TrxAccountType, - TrxScope, } from '@metamask/keyring-api'; import { emitSnapKeyringEvent, @@ -21,6 +20,7 @@ import { hexToBytes } from '@metamask/utils'; import { computeAddress } from 'ethers'; import { TronWeb } from 'tronweb'; +import snapManifest from '../../../snap.manifest.json'; import type { SnapClient } from '../../clients/snap/SnapClient'; import { Network } from '../../constants'; import { asStrictKeyringAccount } from '../../entities/keyring-account'; @@ -47,6 +47,10 @@ const CURVE = 'secp256k1' as const; */ const MAX_BIP44_ACCOUNT_INDEX = 0x7fffffff; +export const SUPPORTED_SCOPES = snapManifest.initialPermissions[ + 'endowment:keyring' +].capabilities.scopes as readonly Network[]; + /** * Range of inclusive account indices to create. * @@ -217,7 +221,7 @@ export class AccountsService { index, type: TrxAccountType.Eoa, address, - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: { entropy: { type: 'mnemonic', @@ -357,7 +361,7 @@ export class AccountsService { index: groupIndex, }); const activityChecks = await Promise.all( - Object.values(Network).map((scope) => + SUPPORTED_SCOPES.map((scope) => this.#transactionsService.checkAddressActivity( scope, derivedAccount.address, @@ -418,7 +422,7 @@ export class AccountsService { index: groupIndex, type: TrxAccountType.Eoa, address, - scopes: [TrxScope.Mainnet, TrxScope.Nile, TrxScope.Shasta], + scopes: SUPPORTED_SCOPES as unknown as Network[], options: { entropy: { type: 'mnemonic',