From 02dd40e6d80b4ddb57e1456f732eba40c778fcc7 Mon Sep 17 00:00:00 2001 From: jdabbech-ledger Date: Thu, 9 Jan 2025 16:19:38 +0100 Subject: [PATCH] :art: (signer-btc): Reviews --- .../src/components/SignerBtcView/index.tsx | 2 +- .../app-binder/SignPsbtDeviceActionTypes.ts | 5 +++- .../SignPsbt/SignPsbtDeviceAction.ts | 10 +++++++ .../sign-psbt/SignPsbtUseCase.test.ts | 30 +++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 packages/signer/signer-btc/src/internal/use-cases/sign-psbt/SignPsbtUseCase.test.ts diff --git a/apps/sample/src/components/SignerBtcView/index.tsx b/apps/sample/src/components/SignerBtcView/index.tsx index a3746963b..32c94ae0f 100644 --- a/apps/sample/src/components/SignerBtcView/index.tsx +++ b/apps/sample/src/components/SignerBtcView/index.tsx @@ -102,7 +102,7 @@ export const SignerBtcView: React.FC<{ sessionId: string }> = ({ }, initialValues: { derivationPath: DEFAULT_DERIVATION_PATH, - psbt: "70736274ff0104010101fb0402000000010204020000000105010100011004000000000103040100000001007102000000013daeeb9a92e7b5af90c787d53f0e60d2cf4cfd47bca9a0d8bc77a7464b024c0b00000000000000000002ff0300000000000016001402fe597c6ec0e2982712929bcf079a4e11d37e8d950b0000000000001600144dc432cb6a26c52a1e6ddd2bcf0ee49199fae0cc000000002206031869567d5e88d988ff7baf6827983f89530ddd79dbaeadaa6ec538a8f03dea8b18f5acc2fd540000800000008000000080000000000000000001011fff0300000000000016001402fe597c6ec0e2982712929bcf079a4e11d37e8d010e200cf08d04fa11ff024d5a50165ba65e495409b50ba6657788dfa15274adb682df010f0400000000000103086b01000000000000010416001429159115f12bb6a7e977439c83d3f8d555d72d5f00", + psbt: "", }, deviceModelId, } satisfies DeviceActionProps< diff --git a/packages/signer/signer-btc/src/api/app-binder/SignPsbtDeviceActionTypes.ts b/packages/signer/signer-btc/src/api/app-binder/SignPsbtDeviceActionTypes.ts index 2d7bdf573..e2e4a6f0d 100644 --- a/packages/signer/signer-btc/src/api/app-binder/SignPsbtDeviceActionTypes.ts +++ b/packages/signer/signer-btc/src/api/app-binder/SignPsbtDeviceActionTypes.ts @@ -4,6 +4,7 @@ import { type ExecuteDeviceActionReturnType, type OpenAppDAError, type OpenAppDARequiredInteraction, + type UserInteractionRequired, } from "@ledgerhq/device-management-kit"; import { type Psbt } from "@api/model/Psbt"; @@ -22,7 +23,9 @@ export type SignPsbtDAError = | OpenAppDAError | CommandErrorResult["error"]; -type SignPsbtDARequiredInteraction = OpenAppDARequiredInteraction; +type SignPsbtDARequiredInteraction = + | OpenAppDARequiredInteraction + | UserInteractionRequired.SignTransaction; export type SignPsbtDAIntermediateValue = { requiredUserInteraction: SignPsbtDARequiredInteraction; diff --git a/packages/signer/signer-btc/src/internal/app-binder/device-action/SignPsbt/SignPsbtDeviceAction.ts b/packages/signer/signer-btc/src/internal/app-binder/device-action/SignPsbt/SignPsbtDeviceAction.ts index 4f32cc61a..53aac18e5 100644 --- a/packages/signer/signer-btc/src/internal/app-binder/device-action/SignPsbt/SignPsbtDeviceAction.ts +++ b/packages/signer/signer-btc/src/internal/app-binder/device-action/SignPsbt/SignPsbtDeviceAction.ts @@ -147,6 +147,16 @@ export class SignPsbtDeviceAction extends XStateDeviceAction< ], }, SignPsbt: { + entry: assign({ + intermediateValue: { + requiredUserInteraction: UserInteractionRequired.SignTransaction, + }, + }), + exit: assign({ + intermediateValue: { + requiredUserInteraction: UserInteractionRequired.None, + }, + }), invoke: { id: "signPsbt", src: "signPsbt", diff --git a/packages/signer/signer-btc/src/internal/use-cases/sign-psbt/SignPsbtUseCase.test.ts b/packages/signer/signer-btc/src/internal/use-cases/sign-psbt/SignPsbtUseCase.test.ts new file mode 100644 index 000000000..a6bcc80b6 --- /dev/null +++ b/packages/signer/signer-btc/src/internal/use-cases/sign-psbt/SignPsbtUseCase.test.ts @@ -0,0 +1,30 @@ +import { DefaultDescriptorTemplate, DefaultWallet } from "@api/model/Wallet"; +import { type BtcAppBinder } from "@internal/app-binder/BtcAppBinder"; + +import { SignPsbtUseCase } from "./SignPsbtUseCase"; + +describe("SignPsbtUseCase", () => { + it("should call signPsbt on appBinder with the correct arguments", () => { + // Given + const wallet = new DefaultWallet( + "84'/0'/0'", + DefaultDescriptorTemplate.NATIVE_SEGWIT, + ); + const psbt = "some-psbt"; + const appBinder = { + signPsbt: jest.fn(), + }; + const signPsbtUseCase = new SignPsbtUseCase( + appBinder as unknown as BtcAppBinder, + ); + + // When + signPsbtUseCase.execute(wallet, psbt); + + // Then + expect(appBinder.signPsbt).toHaveBeenCalledWith({ + wallet, + psbt, + }); + }); +});