From 4f01dfe8d92b87e19e495b47c333974bd407d799 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 +++- .../command/GetWalletAddressCommand.ts | 12 ++++---- .../SignPsbt/SignPsbtDeviceAction.test.ts | 10 +++---- .../SignPsbt/SignPsbtDeviceAction.ts | 10 +++++++ .../sign-psbt/SignPsbtUseCase.test.ts | 30 +++++++++++++++++++ 6 files changed, 56 insertions(+), 13 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/command/GetWalletAddressCommand.ts b/packages/signer/signer-btc/src/internal/app-binder/command/GetWalletAddressCommand.ts index 9750d6f3e..e2842b29d 100644 --- a/packages/signer/signer-btc/src/internal/app-binder/command/GetWalletAddressCommand.ts +++ b/packages/signer/signer-btc/src/internal/app-binder/command/GetWalletAddressCommand.ts @@ -41,7 +41,7 @@ export class GetWalletAddressCommand > { constructor( - private readonly args: GetWalletAddressCommandArgs, + private readonly _args: GetWalletAddressCommandArgs, private readonly _errorHelper = new CommandErrorHelper< GetWalletAddressCommandResponse, BtcErrorCodes @@ -59,11 +59,11 @@ export class GetWalletAddressCommand p1: 0x00, p2: PROTOCOL_VERSION, }) - .addBufferToData(Uint8Array.from([this.args.display ? 1 : 0])) - .addBufferToData(this.args.walletId) - .addBufferToData(this.args.walletHmac) - .addBufferToData(Uint8Array.from([this.args.change ? 1 : 0])) - .add32BitUIntToData(this.args.addressIndex) + .addBufferToData(Uint8Array.from([this._args.display ? 1 : 0])) + .addBufferToData(this._args.walletId) + .addBufferToData(this._args.walletHmac) + .addBufferToData(Uint8Array.from([this._args.change ? 1 : 0])) + .add32BitUIntToData(this._args.addressIndex) .build(); } diff --git a/packages/signer/signer-btc/src/internal/app-binder/device-action/SignPsbt/SignPsbtDeviceAction.test.ts b/packages/signer/signer-btc/src/internal/app-binder/device-action/SignPsbt/SignPsbtDeviceAction.test.ts index 3e97da352..a4b39b68b 100644 --- a/packages/signer/signer-btc/src/internal/app-binder/device-action/SignPsbt/SignPsbtDeviceAction.test.ts +++ b/packages/signer/signer-btc/src/internal/app-binder/device-action/SignPsbt/SignPsbtDeviceAction.test.ts @@ -77,7 +77,7 @@ describe("SignPsbtDeviceAction", () => { }, { intermediateValue: { - requiredUserInteraction: UserInteractionRequired.None, + requiredUserInteraction: UserInteractionRequired.SignTransaction, }, status: DeviceActionStatus.Pending, }, @@ -184,7 +184,7 @@ describe("SignPsbtDeviceAction", () => { { status: DeviceActionStatus.Pending, intermediateValue: { - requiredUserInteraction: UserInteractionRequired.None, + requiredUserInteraction: UserInteractionRequired.SignTransaction, }, }, { @@ -235,7 +235,7 @@ describe("SignPsbtDeviceAction", () => { { status: DeviceActionStatus.Pending, intermediateValue: { - requiredUserInteraction: UserInteractionRequired.None, + requiredUserInteraction: UserInteractionRequired.SignTransaction, }, }, { @@ -288,7 +288,7 @@ describe("SignPsbtDeviceAction", () => { { status: DeviceActionStatus.Pending, intermediateValue: { - requiredUserInteraction: UserInteractionRequired.None, + requiredUserInteraction: UserInteractionRequired.SignTransaction, }, }, { @@ -341,7 +341,7 @@ describe("SignPsbtDeviceAction", () => { { status: DeviceActionStatus.Pending, intermediateValue: { - requiredUserInteraction: UserInteractionRequired.None, + requiredUserInteraction: UserInteractionRequired.SignTransaction, }, }, { 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, + }); + }); +});