Skip to content

Commit

Permalink
Revert "Fix contradictory typing of IFrameMessageResponse and refac…
Browse files Browse the repository at this point in the history
…tor into modular types (#207)" (#213)

This reverts commit 2f0da8f.
  • Loading branch information
legobeat authored Dec 3, 2023
1 parent 4f24e1d commit b5cbd56
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 74 deletions.
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ module.exports = {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 68.06,
branches: 65.42,
functions: 88.57,
lines: 81.29,
statements: 81.21,
lines: 81.63,
statements: 81.55,
},
},

Expand Down
3 changes: 1 addition & 2 deletions src/ledger-iframe-bridge.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { hasProperty } from '@metamask/utils';

import {
type IFrameMessageResponse,
IFrameMessageAction,
LedgerIframeBridge,
} from './ledger-iframe-bridge';
Expand Down Expand Up @@ -63,7 +62,7 @@ describe('LedgerIframeBridge', function () {
*/
function stubKeyringIFramePostMessage(
bridgeInstance: LedgerIframeBridge,
fn: (message: IFrameMessageResponse) => void,
fn: (message: any) => void,
) {
if (!isIFrameValid(bridgeInstance.iframe)) {
throw new Error('the iframe is not valid');
Expand Down
123 changes: 54 additions & 69 deletions src/ledger-iframe-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,46 @@ export enum IFrameMessageAction {
LedgerSignTypedData = 'ledger-sign-typed-data',
}

type IFrameMessageResponseStub<
SuccessResult extends Record<string, unknown>,
FailureResult = Error,
> = {
type IFrameMessageResponse<TAction extends IFrameMessageAction> = {
action: TAction;
messageId: number;
} & (
| { success: true; payload: SuccessResult }
| { success: false; payload: { error: FailureResult } }
| {
action: IFrameMessageAction.LedgerConnectionChange;
payload: { connected: boolean };
}
| ({
action: IFrameMessageAction.LedgerMakeApp;
} & ({ success: true } | { success: false; error?: unknown }))
| {
action: IFrameMessageAction.LedgerUpdateTransport;
success: boolean;
}
| ({
action: IFrameMessageAction.LedgerUnlock;
} & (
| { success: true; payload: GetPublicKeyResponse }
| { success: false; payload: { error: Error } }
))
| ({
action: IFrameMessageAction.LedgerSignTransaction;
} & (
| { success: true; payload: LedgerSignTransactionResponse }
| { success: false; payload: { error: Error } }
))
| ({
action:
| IFrameMessageAction.LedgerSignPersonalMessage
| IFrameMessageAction.LedgerSignTypedData;
} & (
| {
success: true;
payload: LedgerSignMessageResponse | LedgerSignTypedDataResponse;
}
| { success: false; payload: { error: Error } }
))
);

type LedgerConnectionChangeActionResponse = {
messageId: number;
action: IFrameMessageAction.LedgerConnectionChange;
payload: { connected: boolean };
};

type LedgerMakeAppActionResponse = {
messageId: number;
action: IFrameMessageAction.LedgerMakeApp;
} & ({ success: true } | { success: false; error?: unknown });

type LedgerUpdateTransportActionResponse = {
messageId: number;
action: IFrameMessageAction.LedgerUpdateTransport;
success: boolean;
};

type LedgerUnlockActionResponse = {
action: IFrameMessageAction.LedgerUnlock;
} & IFrameMessageResponseStub<GetPublicKeyResponse>;

type LedgerSignTransactionActionResponse = {
action: IFrameMessageAction.LedgerSignTransaction;
} & IFrameMessageResponseStub<LedgerSignTransactionResponse>;

type LedgerSignPersonalMessageActionResponse = {
action: IFrameMessageAction.LedgerSignPersonalMessage;
} & IFrameMessageResponseStub<LedgerSignMessageResponse>;

type LedgerSignTypedDataActionResponse = {
action: IFrameMessageAction.LedgerSignTypedData;
} & IFrameMessageResponseStub<LedgerSignTypedDataResponse>;

export type IFrameMessageResponse =
| LedgerConnectionChangeActionResponse
| LedgerMakeAppActionResponse
| LedgerUpdateTransportActionResponse
| LedgerUnlockActionResponse
| LedgerSignTransactionActionResponse
| LedgerSignPersonalMessageActionResponse
| LedgerSignTypedDataActionResponse;

type IFrameMessage<TAction extends IFrameMessageAction> = {
action: TAction;
params?: Readonly<Record<string, unknown>>;
Expand All @@ -92,15 +80,17 @@ export class LedgerIframeBridge implements LedgerBridge {

eventListener?: (eventMessage: {
origin: string;
data: IFrameMessageResponse;
data: IFrameMessageResponse<IFrameMessageAction>;
}) => void;

isDeviceConnected = false;

currentMessageId = 0;

messageCallbacks: Record<number, (response: IFrameMessageResponse) => void> =
{};
messageCallbacks: Record<
number,
(response: IFrameMessageResponse<IFrameMessageAction>) => void
> = {};

delayedPromise?: {
resolve: (value: boolean) => void;
Expand Down Expand Up @@ -129,12 +119,10 @@ export class LedgerIframeBridge implements LedgerBridge {
action: IFrameMessageAction.LedgerMakeApp,
},
(response) => {
if ('success' in response && response.success) {
if (response.success) {
resolve(true);
} else if ('error' in response) {
reject(response.error);
} else {
reject(new Error('Unknown error occurred'));
reject(response.error);
}
},
);
Expand All @@ -159,8 +147,8 @@ export class LedgerIframeBridge implements LedgerBridge {
action: IFrameMessageAction.LedgerUpdateTransport,
params: { transportType },
},
(response) => {
if ('success' in response && response.success) {
({ success }) => {
if (success) {
return resolve(true);
}
return reject(new Error('Ledger transport could not be updated'));
Expand Down Expand Up @@ -235,16 +223,11 @@ export class LedgerIframeBridge implements LedgerBridge {
action,
params,
},
(response) => {
if ('payload' in response && response.payload) {
if ('success' in response && response.success) {
return resolve(response.payload);
}
if ('error' in response.payload) {
return reject(response.payload.error);
}
({ success, payload }) => {
if (success) {
return resolve(payload);
}
return reject(new Error('Unknown error occurred'));
return reject(payload.error);
},
);
});
Expand Down Expand Up @@ -284,7 +267,7 @@ export class LedgerIframeBridge implements LedgerBridge {
bridgeUrl: string,
eventMessage: {
origin: string;
data: IFrameMessageResponse;
data: IFrameMessageResponse<IFrameMessageAction>;
},
) {
if (eventMessage.origin !== this.#getOrigin(bridgeUrl)) {
Expand All @@ -306,7 +289,7 @@ export class LedgerIframeBridge implements LedgerBridge {

#sendMessage<TAction extends IFrameMessageAction>(
message: IFrameMessage<TAction>,
callback: (response: IFrameMessageResponse) => void,
callback: (response: IFrameMessageResponse<TAction>) => void,
) {
this.currentMessageId += 1;

Expand All @@ -316,7 +299,9 @@ export class LedgerIframeBridge implements LedgerBridge {
target: LEDGER_IFRAME_ID,
};

this.messageCallbacks[this.currentMessageId] = callback;
this.messageCallbacks[this.currentMessageId] = callback as (
response: IFrameMessageResponse<IFrameMessageAction>,
) => void;

if (!this.iframeLoaded || !this.iframe || !this.iframe.contentWindow) {
throw new Error('The iframe is not loaded yet');
Expand Down

0 comments on commit b5cbd56

Please sign in to comment.