Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: event.source.postMessage instead of window.opener.postMessage #290

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions src/handlers/signer-errors.handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@ describe('Signer-errors.handlers', () => {

const testOrigin = 'https://hello.com';

let originalOpener: typeof window.opener;
let sourceMock: Window;

let postMessageMock: Mock;

beforeEach(() => {
originalOpener = window.opener;

postMessageMock = vi.fn();

vi.stubGlobal('opener', {postMessage: postMessageMock});
sourceMock = {
postMessage: postMessageMock
} as unknown as Window;

requestId = crypto.randomUUID();
});

afterEach(() => {
window.opener = originalOpener;

vi.clearAllMocks();
});

Expand All @@ -44,15 +42,15 @@ describe('Signer-errors.handlers', () => {
message: 'The request sent by the relying party is not supported by the signer.'
};

notifyErrorRequestNotSupported({id: requestId, origin: testOrigin});
notifyErrorRequestNotSupported({id: requestId, origin: testOrigin, source: sourceMock});

const expectedMessage: RpcResponseWithError = {
jsonrpc: JSON_RPC_VERSION_2,
id: requestId,
error
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, testOrigin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});

it('should post an error message with custom message when provided', () => {
Expand All @@ -63,15 +61,20 @@ describe('Signer-errors.handlers', () => {
message
};

notifyErrorRequestNotSupported({id: requestId, origin: testOrigin, message});
notifyErrorRequestNotSupported({
id: requestId,
origin: testOrigin,
message,
source: sourceMock
});

const expectedMessage: RpcResponseWithError = {
jsonrpc: JSON_RPC_VERSION_2,
id: requestId,
error
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, testOrigin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});
});

Expand All @@ -83,29 +86,29 @@ describe('Signer-errors.handlers', () => {
'The signer has not granted the necessary permissions to process the request from the relying party.'
};

notifyErrorPermissionNotGranted({id: requestId, origin: testOrigin});
notifyErrorPermissionNotGranted({id: requestId, origin: testOrigin, source: sourceMock});

const expectedMessage: RpcResponseWithError = {
jsonrpc: JSON_RPC_VERSION_2,
id: requestId,
error
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, testOrigin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});
});

describe('notifyErrorActionAborted', () => {
it('should post an error message indicating action was aborted', () => {
notifyErrorActionAborted({id: requestId, origin: testOrigin});
notifyErrorActionAborted({id: requestId, origin: testOrigin, source: sourceMock});

const expectedMessage: RpcResponseWithError = {
jsonrpc: JSON_RPC_VERSION_2,
id: requestId,
error: mockErrorNotify
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, testOrigin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});
});

Expand All @@ -117,15 +120,20 @@ describe('Signer-errors.handlers', () => {
message: customMessage
};

notifyNetworkError({id: requestId, origin: testOrigin, message: customMessage});
notifyNetworkError({
id: requestId,
origin: testOrigin,
message: customMessage,
source: sourceMock
});

const expectedMessage: RpcResponseWithError = {
jsonrpc: JSON_RPC_VERSION_2,
id: requestId,
error
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, testOrigin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});
});

Expand All @@ -136,15 +144,15 @@ describe('Signer-errors.handlers', () => {
message: 'The signer has not registered a prompt to respond to permission requests.'
};

notifyErrorMissingPrompt({id: requestId, origin: testOrigin});
notifyErrorMissingPrompt({id: requestId, origin: testOrigin, source: sourceMock});

const expectedMessage: RpcResponseWithError = {
jsonrpc: JSON_RPC_VERSION_2,
id: requestId,
error
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, testOrigin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});
});

Expand All @@ -155,15 +163,15 @@ describe('Signer-errors.handlers', () => {
message: 'The sender must match the owner of the signer.'
};

notifyErrorSenderNotAllowed({id: requestId, origin: testOrigin});
notifyErrorSenderNotAllowed({id: requestId, origin: testOrigin, source: sourceMock});

const expectedMessage: RpcResponseWithError = {
jsonrpc: JSON_RPC_VERSION_2,
id: requestId,
error
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, testOrigin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});
});

Expand All @@ -175,15 +183,15 @@ describe('Signer-errors.handlers', () => {
'The signer is currently processing a request and cannot handle new requests at this time.'
};

notifyErrorBusy({id: requestId, origin: testOrigin});
notifyErrorBusy({id: requestId, origin: testOrigin, source: sourceMock});

const expectedMessage: RpcResponseWithError = {
jsonrpc: JSON_RPC_VERSION_2,
id: requestId,
error
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, testOrigin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});
});
});
29 changes: 14 additions & 15 deletions src/handlers/signer-success.handlers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,41 @@ describe('Signer-success.handlers', () => {
let id: RpcId;
const origin = 'https://hello.com';

let originalOpener: typeof window.opener;
let sourceMock: Window;

let postMessageMock: Mock;

beforeEach(() => {
id = crypto.randomUUID();
originalOpener = window.opener;

postMessageMock = vi.fn();

vi.stubGlobal('opener', {postMessage: postMessageMock});
sourceMock = {
postMessage: postMessageMock
} as unknown as Window;
});

afterEach(() => {
window.opener = originalOpener;

vi.restoreAllMocks();
});

describe('notifyReady', () => {
it('should post a message with the msg', () => {
notifyReady({id, origin});
notifyReady({id, origin, source: sourceMock});

const expectedMessage: IcrcReadyResponse = {
jsonrpc: JSON_RPC_VERSION_2,
id,
result: 'ready'
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, origin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});
});

describe('notifySupportedStandards', () => {
it('should post a message with the msg', () => {
notifySupportedStandards({id, origin});
notifySupportedStandards({id, origin, source: sourceMock});

const expectedMessage: IcrcSupportedStandardsResponse = {
jsonrpc: JSON_RPC_VERSION_2,
Expand All @@ -67,7 +66,7 @@ describe('Signer-success.handlers', () => {
}
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, origin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});
});

Expand All @@ -82,7 +81,7 @@ describe('Signer-success.handlers', () => {
}
];

notifyPermissionScopes({id, origin, scopes});
notifyPermissionScopes({id, origin, scopes, source: sourceMock});

const expectedMessage: IcrcScopesResponse = {
jsonrpc: JSON_RPC_VERSION_2,
Expand All @@ -92,35 +91,35 @@ describe('Signer-success.handlers', () => {
}
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, origin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});
});

describe('notifyAccounts', () => {
it('should post a message with the accounts', () => {
notifyAccounts({id, origin, accounts: mockAccounts});
notifyAccounts({id, origin, accounts: mockAccounts, source: sourceMock});

const expectedMessage = {
jsonrpc: JSON_RPC_VERSION_2,
id,
result: {accounts: mockAccounts}
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, origin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});
});

describe('notifyCallCanister', () => {
it('should post a message with the call canister result', () => {
notifyCallCanister({id, origin, result: mockCallCanisterSuccess});
notifyCallCanister({id, origin, result: mockCallCanisterSuccess, source: sourceMock});

const expectedMessage = {
jsonrpc: JSON_RPC_VERSION_2,
id,
result: mockCallCanisterSuccess
};

expect(postMessageMock).toHaveBeenCalledWith(expectedMessage, origin);
expect(postMessageMock).toHaveBeenCalledWith(expectedMessage);
});
});
});
20 changes: 10 additions & 10 deletions src/handlers/signer-success.handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import {JSON_RPC_VERSION_2} from '../types/rpc';
import type {Notify} from '../types/signer-handlers';
import {notify} from './signer.handlers';

export const notifyReady = ({id, origin}: Notify): void => {
export const notifyReady = ({id, origin, source}: Notify): void => {
const msg: IcrcReadyResponse = {
jsonrpc: JSON_RPC_VERSION_2,
id,
result: 'ready'
};

notify({msg, origin});
notify({msg, origin, source});
};

export const notifySupportedStandards = ({id, origin}: Notify): void => {
export const notifySupportedStandards = ({id, origin, source}: Notify): void => {
const msg: IcrcSupportedStandardsResponse = {
jsonrpc: JSON_RPC_VERSION_2,
id,
Expand All @@ -32,41 +32,41 @@ export const notifySupportedStandards = ({id, origin}: Notify): void => {
}
};

notify({msg, origin});
notify({msg, origin, source});
};

export type NotifyPermissions = Notify & {scopes: IcrcScopesArray};

export const notifyPermissionScopes = ({id, origin, scopes}: NotifyPermissions): void => {
export const notifyPermissionScopes = ({id, origin, scopes, source}: NotifyPermissions): void => {
const msg: IcrcScopesResponse = {
jsonrpc: JSON_RPC_VERSION_2,
id,
result: {scopes}
};

notify({msg, origin});
notify({msg, origin, source});
};

export type NotifyAccounts = Notify & {accounts: IcrcAccounts};

export const notifyAccounts = ({id, origin, accounts}: NotifyAccounts): void => {
export const notifyAccounts = ({id, origin, accounts, source}: NotifyAccounts): void => {
const msg: IcrcAccountsResponse = {
jsonrpc: JSON_RPC_VERSION_2,
id,
result: {accounts}
};

notify({msg, origin});
notify({msg, origin, source});
};

export type NotifyCallCanister = Notify & {result: IcrcCallCanisterResult};

export const notifyCallCanister = ({id, origin, result}: NotifyCallCanister): void => {
export const notifyCallCanister = ({id, origin, result, source}: NotifyCallCanister): void => {
const msg: IcrcCallCanisterResponse = {
jsonrpc: JSON_RPC_VERSION_2,
id,
result
};

notify({msg, origin});
notify({msg, origin, source});
};
Loading
Loading