From 6a3259ec9b63af0018b88d3b0e25924d4dd78ccd Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:50:50 +0200 Subject: [PATCH 1/4] fix: allow ramps dev environment on Flask --- .../lib/snap-keyring/keyring-snaps-permissions.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts b/app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts index 2c72ce8ba85b..24badb4e2556 100644 --- a/app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts +++ b/app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts @@ -5,9 +5,14 @@ import { import { KeyringRpcMethod } from '@metamask/keyring-api'; /** - * The origin of the Portfolio dapp. + * The origins of the Portfolio dapp. */ -const PORTFOLIO_ORIGIN = 'https://portfolio.metamask.io'; +const PORTFOLIO_ORIGINS = [ + 'https://portfolio.metamask.io', + ///: BEGIN:ONLY_INCLUDE_IF(build-flask) + 'https://ramps-dev.portfolio.metamask.io', + ///: END:ONLY_INCLUDE_IF +]; /** * List of keyring methods MetaMask can call. @@ -94,7 +99,7 @@ export function keyringSnapPermissionsBuilder( return METAMASK_ALLOWED_METHODS; } - if (origin === PORTFOLIO_ORIGIN) { + if (PORTFOLIO_ORIGINS.includes(origin)) { return PORTFOLIO_ALLOWED_METHODS; } From a174a1689187a9f0504590c172c622c0740e009d Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Wed, 3 Jul 2024 17:55:57 +0200 Subject: [PATCH 2/4] chore: add another dev origin --- app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts b/app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts index 24badb4e2556..01729f9b8ca0 100644 --- a/app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts +++ b/app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts @@ -10,6 +10,7 @@ import { KeyringRpcMethod } from '@metamask/keyring-api'; const PORTFOLIO_ORIGINS = [ 'https://portfolio.metamask.io', ///: BEGIN:ONLY_INCLUDE_IF(build-flask) + 'https://dev.portfolio.metamask.io', 'https://ramps-dev.portfolio.metamask.io', ///: END:ONLY_INCLUDE_IF ]; From 12ffede1753003ea3a1af76ba1c1d77c8e57ea35 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Thu, 4 Jul 2024 16:12:35 +0200 Subject: [PATCH 3/4] test: also test dev origins --- .../keyring-snaps-permissions.test.ts | 15 ++++++++++++--- .../lib/snap-keyring/keyring-snaps-permissions.ts | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/scripts/lib/snap-keyring/keyring-snaps-permissions.test.ts b/app/scripts/lib/snap-keyring/keyring-snaps-permissions.test.ts index 6d22b14ed241..ac6fe9862564 100644 --- a/app/scripts/lib/snap-keyring/keyring-snaps-permissions.test.ts +++ b/app/scripts/lib/snap-keyring/keyring-snaps-permissions.test.ts @@ -8,6 +8,12 @@ import { keyringSnapPermissionsBuilder, } from './keyring-snaps-permissions'; +const PORTFOLIO_ORIGINS: string[] = [ + 'https://portfolio.metamask.io', + 'https://dev.portfolio.metamask.io', + 'https://ramps-dev.portfolio.metamask.io', +]; + describe('keyringSnapPermissionsBuilder', () => { const mockController = new SubjectMetadataController({ subjectCacheLimit: 100, @@ -26,7 +32,8 @@ describe('keyringSnapPermissionsBuilder', () => { }); describe('Portfolio origin', () => { - it('returns the methods Portfolio can call', () => { + // @ts-expect-error This is missing from the Mocha type definitions + it.each(PORTFOLIO_ORIGINS)('returns the methods Portfolio can call', () => { const permissions = keyringSnapPermissionsBuilder( mockController, 'https://portfolio.metamask.io', @@ -39,7 +46,8 @@ describe('keyringSnapPermissionsBuilder', () => { ]); }); - it('cannot create an account', () => { + // @ts-expect-error This is missing from the Mocha type definitions + it.each(PORTFOLIO_ORIGINS)('cannot create an account', () => { const permissions = keyringSnapPermissionsBuilder( mockController, 'https://portfolio.metamask.io', @@ -47,7 +55,8 @@ describe('keyringSnapPermissionsBuilder', () => { expect(permissions()).not.toContain(KeyringRpcMethod.CreateAccount); }); - it('can submit a request', () => { + // @ts-expect-error This is missing from the Mocha type definitions + it.each(PORTFOLIO_ORIGINS)('can submit a request', () => { const permissions = keyringSnapPermissionsBuilder( mockController, 'https://portfolio.metamask.io', diff --git a/app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts b/app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts index 01729f9b8ca0..bdcd29acc9cb 100644 --- a/app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts +++ b/app/scripts/lib/snap-keyring/keyring-snaps-permissions.ts @@ -7,7 +7,7 @@ import { KeyringRpcMethod } from '@metamask/keyring-api'; /** * The origins of the Portfolio dapp. */ -const PORTFOLIO_ORIGINS = [ +const PORTFOLIO_ORIGINS: string[] = [ 'https://portfolio.metamask.io', ///: BEGIN:ONLY_INCLUDE_IF(build-flask) 'https://dev.portfolio.metamask.io', From 90c9282f0dde332683e3a9451c16c41ccb984e81 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Thu, 4 Jul 2024 17:14:32 +0200 Subject: [PATCH 4/4] test: fix Portfolio unit tests --- .../keyring-snaps-permissions.test.ts | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/app/scripts/lib/snap-keyring/keyring-snaps-permissions.test.ts b/app/scripts/lib/snap-keyring/keyring-snaps-permissions.test.ts index ac6fe9862564..71f420f7b2d7 100644 --- a/app/scripts/lib/snap-keyring/keyring-snaps-permissions.test.ts +++ b/app/scripts/lib/snap-keyring/keyring-snaps-permissions.test.ts @@ -33,34 +33,37 @@ describe('keyringSnapPermissionsBuilder', () => { describe('Portfolio origin', () => { // @ts-expect-error This is missing from the Mocha type definitions - it.each(PORTFOLIO_ORIGINS)('returns the methods Portfolio can call', () => { - const permissions = keyringSnapPermissionsBuilder( - mockController, - 'https://portfolio.metamask.io', - ); - expect(permissions()).toStrictEqual([ - KeyringRpcMethod.ListAccounts, - KeyringRpcMethod.GetAccount, - KeyringRpcMethod.GetAccountBalances, - KeyringRpcMethod.SubmitRequest, - ]); - }); + it.each(PORTFOLIO_ORIGINS)( + 'returns the methods that can be called by %s', + (origin: string) => { + const permissions = keyringSnapPermissionsBuilder( + mockController, + origin, + ); + expect(permissions()).toStrictEqual([ + KeyringRpcMethod.ListAccounts, + KeyringRpcMethod.GetAccount, + KeyringRpcMethod.GetAccountBalances, + KeyringRpcMethod.SubmitRequest, + ]); + }, + ); // @ts-expect-error This is missing from the Mocha type definitions - it.each(PORTFOLIO_ORIGINS)('cannot create an account', () => { - const permissions = keyringSnapPermissionsBuilder( - mockController, - 'https://portfolio.metamask.io', - ); - expect(permissions()).not.toContain(KeyringRpcMethod.CreateAccount); - }); + it.each(PORTFOLIO_ORIGINS)( + '%s cannot create an account', + (origin: string) => { + const permissions = keyringSnapPermissionsBuilder( + mockController, + origin, + ); + expect(permissions()).not.toContain(KeyringRpcMethod.CreateAccount); + }, + ); // @ts-expect-error This is missing from the Mocha type definitions - it.each(PORTFOLIO_ORIGINS)('can submit a request', () => { - const permissions = keyringSnapPermissionsBuilder( - mockController, - 'https://portfolio.metamask.io', - ); + it.each(PORTFOLIO_ORIGINS)('%s can submit a request', (origin: string) => { + const permissions = keyringSnapPermissionsBuilder(mockController, origin); expect(permissions()).toContain(KeyringRpcMethod.SubmitRequest); }); });