diff --git a/packages/profile-sync-controller/src/sdk/authentication-jwt-bearer/flow-srp.ts b/packages/profile-sync-controller/src/sdk/authentication-jwt-bearer/flow-srp.ts index c8763aae0b..8d766e75f3 100644 --- a/packages/profile-sync-controller/src/sdk/authentication-jwt-bearer/flow-srp.ts +++ b/packages/profile-sync-controller/src/sdk/authentication-jwt-bearer/flow-srp.ts @@ -1,6 +1,10 @@ import { ValidationError } from '../errors'; import { getMetaMaskProviderEIP6963 } from '../utils/eip-6963-metamask-provider'; -import { MESSAGE_SIGNING_SNAP } from '../utils/messaging-signing-snap-requests'; +import { + MESSAGE_SIGNING_SNAP, + connectSnap, + isSnapConnected, +} from '../utils/messaging-signing-snap-requests'; import { validateLoginResponse } from '../utils/validate-login-response'; import { authenticate, authorizeOIDC, getNonce } from './services'; import type { @@ -20,19 +24,21 @@ type JwtBearerAuth_SRP_Options = { signing?: AuthSigningOptions; }; +const getDefaultEIP6963Provider = async () => { + const provider = await getMetaMaskProviderEIP6963(); + if (!provider) { + throw new ValidationError('No MetaMask wallet connected'); + } + return provider; +}; + const defaultEIP6963SigningOptions: AuthSigningOptions = { getIdentifier: async (): Promise => { - const provider = await getMetaMaskProviderEIP6963(); - if (!provider) { - throw new ValidationError('No MetaMask wallet connected'); - } + const provider = await getDefaultEIP6963Provider(); return await MESSAGE_SIGNING_SNAP.getPublicKey(provider); }, signMessage: async (message: string): Promise => { - const provider = await getMetaMaskProviderEIP6963(); - if (!provider) { - throw new ValidationError('No MetaMask wallet connected'); - } + const provider = await getDefaultEIP6963Provider(); if (!message.startsWith('metamask:')) { throw new ValidationError('message must start with "metamask:"'); } @@ -85,6 +91,22 @@ export class SRPJwtBearerAuth implements IBaseAuth { return await this.#options.signing.signMessage(message); } + async isSnapConnected(): Promise { + const provider = await getMetaMaskProviderEIP6963(); + if (!provider) { + return false; + } + + const isConnected = await isSnapConnected(provider); + return isConnected; + } + + async connectSnap(): Promise { + const provider = await getDefaultEIP6963Provider(); + const res = await connectSnap(provider); + return res; + } + // convert expiresIn from seconds to milliseconds and use 90% of expiresIn async #getAuthSession(): Promise { const auth = await this.#options.storage.getLoginResponse(); diff --git a/packages/profile-sync-controller/src/sdk/authentication.ts b/packages/profile-sync-controller/src/sdk/authentication.ts index f8f5e966a8..6d321f8c7d 100644 --- a/packages/profile-sync-controller/src/sdk/authentication.ts +++ b/packages/profile-sync-controller/src/sdk/authentication.ts @@ -8,11 +8,6 @@ import type { UserProfile, Pair } from './authentication-jwt-bearer/types'; import { AuthType } from './authentication-jwt-bearer/types'; import type { Env } from './env'; import { PairError, UnsupportedAuthTypeError } from './errors'; -import { getMetaMaskProviderEIP6963 } from './utils/eip-6963-metamask-provider'; -import { - connectSnap, - isSnapConnected, -} from './utils/messaging-signing-snap-requests'; // Computing the Classes, so we only get back the public methods for the interface. // TODO: Either fix this lint violation or explain why it's necessary to ignore. @@ -54,22 +49,13 @@ export class JwtBearerAuth implements SIWEInterface, SRPInterface { } async connectSnap(): Promise { - const provider = await getMetaMaskProviderEIP6963(); - if (!provider) { - throw new Error('failed to get MetaMaskProviderEIP6963 provider'); - } - const res = await connectSnap(provider); - return res; + this.#assertSRP(this.#type, this.#sdk); + return this.#sdk.connectSnap(); } async isSnapConnected(): Promise { - const provider = await getMetaMaskProviderEIP6963(); - if (!provider) { - return false; - } - - const isConnected = await isSnapConnected(provider); - return isConnected; + this.#assertSRP(this.#type, this.#sdk); + return this.#sdk.isSnapConnected(); } async getUserProfile(): Promise { @@ -132,8 +118,8 @@ export class JwtBearerAuth implements SIWEInterface, SRPInterface { #assertSIWE( type: AuthType, - sdk: SIWEJwtBearerAuth | SRPJwtBearerAuth, - ): asserts sdk is SIWEJwtBearerAuth { + _sdk: SIWEJwtBearerAuth | SRPJwtBearerAuth, + ): asserts _sdk is SIWEJwtBearerAuth { if (type === AuthType.SiWE) { return; } @@ -142,6 +128,19 @@ export class JwtBearerAuth implements SIWEInterface, SRPInterface { 'This method is only available via SIWE auth type', ); } + + #assertSRP( + type: AuthType, + _sdk: SIWEJwtBearerAuth | SRPJwtBearerAuth, + ): asserts _sdk is SRPJwtBearerAuth { + if (type === AuthType.SRP) { + return; + } + + throw new UnsupportedAuthTypeError( + 'This method is only available via SRP auth type', + ); + } } export { SIWEJwtBearerAuth } from './authentication-jwt-bearer/flow-siwe'; diff --git a/packages/profile-sync-controller/src/sdk/utils/messaging-signing-snap-requests.test.ts b/packages/profile-sync-controller/src/sdk/utils/messaging-signing-snap-requests.test.ts index 24f24f0e77..b7db331ce8 100644 --- a/packages/profile-sync-controller/src/sdk/utils/messaging-signing-snap-requests.test.ts +++ b/packages/profile-sync-controller/src/sdk/utils/messaging-signing-snap-requests.test.ts @@ -6,7 +6,6 @@ import { MESSAGE_SIGNING_SNAP, SNAP_ORIGIN, connectSnap, - getSnap, getSnaps, isSnapConnected, } from './messaging-signing-snap-requests'; @@ -66,44 +65,6 @@ describe('isSnapConnected() tests', () => { }); }); -describe('getSnap() tests', () => { - it('tests invocation', async () => { - const { mockProvider, mockRequest } = arrangeMockProvider(); - - const mockSnap: Snap = { id: SNAP_ORIGIN } as MockVariable; - mockRequest.mockResolvedValue({ [SNAP_ORIGIN]: mockSnap }); - - const result = await getSnap(mockProvider); - expect(mockRequest).toHaveBeenCalled(); - expect(result).toBeDefined(); - }); - - it('returns undefined if unable to find snap', async () => { - const { mockProvider, mockRequest } = arrangeMockProvider(); - - const mockSnap: Snap = { id: 'A differentSnap' } as MockVariable; - mockRequest.mockResolvedValue({ diffSnap: mockSnap }); - - const result1 = await getSnap(mockProvider); - expect(mockRequest).toHaveBeenCalled(); - expect(result1).toBeUndefined(); - - // Another test in case the wallet request returns null - mockRequest.mockResolvedValue(null); - const result2 = await getSnap(mockProvider); - expect(result2).toBeUndefined(); - }); - - it('returns undefined if an error is thrown when making provider request', async () => { - const { mockProvider, mockRequest } = arrangeMockProvider(); - mockRequest.mockRejectedValue(new Error('MOCK ERROR')); - - const result = await getSnap(mockProvider); - expect(mockRequest).toHaveBeenCalled(); - expect(result).toBeUndefined(); - }); -}); - describe('MESSAGE_SIGNING_SNAP.getPublicKey() tests', () => { it('tests invocation', async () => { const { mockProvider, mockRequest } = arrangeMockProvider(); diff --git a/packages/profile-sync-controller/src/sdk/utils/messaging-signing-snap-requests.ts b/packages/profile-sync-controller/src/sdk/utils/messaging-signing-snap-requests.ts index 24d8396f12..8945b501fa 100644 --- a/packages/profile-sync-controller/src/sdk/utils/messaging-signing-snap-requests.ts +++ b/packages/profile-sync-controller/src/sdk/utils/messaging-signing-snap-requests.ts @@ -10,8 +10,6 @@ export type GetSnapsResponse = Record; export const SNAP_ORIGIN = 'npm:@metamask/message-signing-snap'; -const foundSnap = (snap: Snap) => snap.id === SNAP_ORIGIN; - /** * Requests Connection to the Message Signing Snap * @@ -66,22 +64,6 @@ export async function isSnapConnected( } } -/** - * Will return the message signing snap if installed - * @param provider - MetaMask Wallet Provider - */ -export async function getSnap( - provider: Eip1193Provider, -): Promise { - try { - const snaps = await getSnaps(provider); - return Object.values(snaps ?? {}).find((snap) => foundSnap(snap)); - } catch (e) { - console.error('Failed to obtain installed snap', e); - return undefined; - } -} - export const MESSAGE_SIGNING_SNAP = { async getPublicKey(provider: Eip1193Provider) { const publicKey: string = await provider.request({