Skip to content

Commit

Permalink
better types
Browse files Browse the repository at this point in the history
  • Loading branch information
bergarces committed May 4, 2023
1 parent 6f29029 commit 6dfe796
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
27 changes: 19 additions & 8 deletions src/ledger-iframe-bridge.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { hasProperty } from '@metamask/utils';

import { LedgerIframeBridge } from './ledger-iframe-bridge';
import {
IFrameMessageAction,
LedgerIframeBridge,
} from './ledger-iframe-bridge';
import documentShim from '../test/document.shim';
import windowShim from '../test/window.shim';

Expand Down Expand Up @@ -113,14 +116,16 @@ describe('LedgerIframeBridge', function () {
it('sends and processes a successful ledger-make-app message', async function () {
stubKeyringIFramePostMessage(bridge, (message) => {
expect(message).toStrictEqual({
action: 'ledger-make-app',
action: IFrameMessageAction.LedgerMakeApp,
messageId: 1,
target: 'LEDGER-IFRAME',
});

bridge.messageCallbacks[message.messageId]?.({
action: IFrameMessageAction.LedgerMakeApp,
messageId: 1,
success: true,
} as any);
});
});

const result = await bridge.attemptMakeApp();
Expand All @@ -136,15 +141,17 @@ describe('LedgerIframeBridge', function () {

stubKeyringIFramePostMessage(bridge, (message) => {
expect(message).toStrictEqual({
action: 'ledger-make-app',
action: IFrameMessageAction.LedgerMakeApp,
messageId: 1,
target: 'LEDGER-IFRAME',
});

bridge.messageCallbacks[message.messageId]?.({
action: IFrameMessageAction.LedgerMakeApp,
messageId: 1,
error: new Error(errorMessage),
success: false,
} as any);
});
});

await expect(bridge.attemptMakeApp()).rejects.toThrow(errorMessage);
Expand All @@ -162,15 +169,17 @@ describe('LedgerIframeBridge', function () {

stubKeyringIFramePostMessage(bridge, (message) => {
expect(message).toStrictEqual({
action: 'ledger-update-transport',
action: IFrameMessageAction.LedgerUpdateTransport,
params: { transportType },
messageId: 1,
target: 'LEDGER-IFRAME',
});

bridge.messageCallbacks[message.messageId]?.({
action: IFrameMessageAction.LedgerUpdateTransport,
messageId: 1,
success: true,
} as any);
});
});

const result = await bridge.updateTransportMethod(transportType);
Expand All @@ -195,8 +204,10 @@ describe('LedgerIframeBridge', function () {
});

bridge.messageCallbacks[message.messageId]?.({
action: IFrameMessageAction.LedgerUpdateTransport,
messageId: 1,
success: false,
} as any);
});
});

await expect(bridge.updateTransportMethod(transportType)).rejects.toThrow(
Expand Down
5 changes: 3 additions & 2 deletions src/ledger-iframe-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

const LEDGER_IFRAME_ID = 'LEDGER-IFRAME';

enum IFrameMessageAction {
export enum IFrameMessageAction {
LedgerConnectionChange = 'ledger-connection-change',
LedgerUnlock = 'ledger-unlock',
LedgerMakeApp = 'ledger-make-app',
Expand Down Expand Up @@ -62,7 +62,7 @@ type IFrameMessageResponse = {
success: boolean;
action: IFrameMessageAction;
messageId: number;
payload: IFrameMessageResponsePayload;
payload?: IFrameMessageResponsePayload;
error?: unknown;
};

Expand Down Expand Up @@ -250,6 +250,7 @@ export class LedgerIframeBridge implements LedgerBridge {
messageCallback(params.data);
} else if (
params.data.action === IFrameMessageAction.LedgerConnectionChange &&
params.data.payload &&
isConnectionChangedResponse(params.data.payload)
) {
this.isDeviceConnected = params.data.payload.connected;
Expand Down
14 changes: 1 addition & 13 deletions src/ledger-keyring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,7 @@ describe('LedgerKeyring', function () {
}

beforeEach(async function () {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};
bridge = {
init: noop,
destroy: noop,
attemptMakeApp: noop,
updateTransportMethod: noop,
getPublicKey: noop,
deviceSignTransaction: noop,
deviceSignMessage: noop,
deviceSignTypedData: noop,
} as any;

bridge = new LedgerIframeBridge();
keyring = new LedgerKeyring({ bridge });
keyring.hdk = fakeHdKey;
await keyring.deserialize();
Expand Down

0 comments on commit 6dfe796

Please sign in to comment.