Skip to content

Commit

Permalink
✨ (signer-btc): Create SignPsbt DA & task & s
Browse files Browse the repository at this point in the history
  • Loading branch information
jdabbech-ledger committed Dec 27, 2024
1 parent c3296da commit 1b61add
Show file tree
Hide file tree
Showing 19 changed files with 1,244 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-points-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/device-signer-kit-bitcoin": minor
---

Create SignPsbt API
17 changes: 7 additions & 10 deletions packages/signer/signer-btc/src/api/SignerBtc.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
// import { type AddressOptions } from "@api/model/AddressOptions";
// import { type Psbt } from "@api/model/Psbt";
// import { type Signature } from "@api/model/Signature";
// import { type Wallet } from "@api/model/Wallet";
import { type GetExtendedPublicKeyDAReturnType } from "@api/app-binder/GetExtendedPublicKeyDeviceActionTypes";
import { type SignMessageDAReturnType } from "@api/app-binder/SignMessageDeviceActionTypes";
import { type SignPsbtDAReturnType } from "@api/app-binder/SignPsbtDeviceActionTypes";
import { type AddressOptions } from "@api/model/AddressOptions";
import {
type GetExtendedPublicKeyReturnType,
type SignMessageDAReturnType,
} from "@root/src";
import { type Psbt } from "@api/model/Psbt";
import { type Wallet } from "@api/model/Wallet";

export interface SignerBtc {
getExtendedPublicKey: (
derivationPath: string,
options: AddressOptions,
) => GetExtendedPublicKeyReturnType;
) => GetExtendedPublicKeyDAReturnType;
signMessage: (
derivationPath: string,
message: string,
) => SignMessageDAReturnType;
signPsbt: (wallet: Wallet, psbt: Psbt) => SignPsbtDAReturnType;
// getAddress: (wallet: Wallet, options?: AddressOptions) => Promise<string>;
// signPsbt: (wallet: Wallet, psbt: Psbt) => Promise<Psbt>;
// signTransaction: (wallet: Wallet, psbt: Psbt) => Promise<Uint8Array>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
type CommandErrorResult,
type DeviceActionState,
type ExecuteDeviceActionReturnType,
type OpenAppDAError,
type OpenAppDARequiredInteraction,
} from "@ledgerhq/device-management-kit";

import { type Psbt } from "@api/model/Psbt";
import { type Signature } from "@api/model/Signature";
import { type Wallet } from "@api/model/Wallet";
import { type BtcErrorCodes } from "@internal/app-binder/command/utils/bitcoinAppErrors";

export type SignPsbtDAOutput = Signature;

export type SignPsbtDAInput = {
psbt: Psbt;
wallet: Wallet;
};

export type SignPsbtDAError =
| OpenAppDAError
| CommandErrorResult<BtcErrorCodes>["error"];

type SignPsbtDARequiredInteraction = OpenAppDARequiredInteraction;

export type SignPsbtDAIntermediateValue = {
requiredUserInteraction: SignPsbtDARequiredInteraction;
};

export type SignPsbtDAState = DeviceActionState<
SignPsbtDAOutput,
SignPsbtDAError,
SignPsbtDAIntermediateValue
>;

export type SignPsbtDAInternalState = {
readonly error: SignPsbtDAError | null;
readonly signature: Signature | null;
};

export type SignPsbtDAReturnType = ExecuteDeviceActionReturnType<
SignPsbtDAOutput,
SignPsbtDAError,
SignPsbtDAIntermediateValue
>;
5 changes: 3 additions & 2 deletions packages/signer/signer-btc/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export type {
SignMessageDAIntermediateValue,
SignMessageDAOutput,
SignMessageDAState,
} from "@api/app-binder/SignMessageDeviceActionType";
export * from "@api/app-binder/SignMessageDeviceActionType";
} from "@api/app-binder/SignMessageDeviceActionTypes";
export * from "@api/app-binder/SignPsbtDeviceActionTypes";
export { DefaultDescriptorTemplate, DefaultWallet } from "@api/model/Wallet";
export * from "@api/SignerBtc";
export * from "@api/SignerBtcBuilder";
11 changes: 10 additions & 1 deletion packages/signer/signer-btc/src/internal/DefaultSignerBtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import {
} from "@ledgerhq/device-management-kit";
import { type Container } from "inversify";

import { type SignMessageDAReturnType } from "@api/app-binder/SignMessageDeviceActionType";
import { type SignMessageDAReturnType } from "@api/app-binder/SignMessageDeviceActionTypes";
import { type AddressOptions } from "@api/model/AddressOptions";
import { type Psbt } from "@api/model/Psbt";
import { type Wallet } from "@api/model/Wallet";
import { type SignerBtc } from "@api/SignerBtc";
import { useCasesTypes } from "@internal/use-cases/di/useCasesTypes";
import { type GetExtendedPublicKeyUseCase } from "@internal/use-cases/get-extended-public-key/GetExtendedPublicKeyUseCase";
import { type SignPsbtUseCase } from "@internal/use-cases/sign-psbt/SignPsbtUseCase";

import { type SignMessageUseCase } from "./use-cases/sign-message/SignMessageUseCase";
import { makeContainer } from "./di";
Expand All @@ -25,6 +28,12 @@ export class DefaultSignerBtc implements SignerBtc {
this._container = makeContainer({ dmk, sessionId });
}

signPsbt(wallet: Wallet, psbt: Psbt) {
return this._container
.get<SignPsbtUseCase>(useCasesTypes.SignPsbtUseCase)
.execute(wallet, psbt);
}

getExtendedPublicKey(
derivationPath: string,
{ checkOnDevice = false }: AddressOptions,
Expand Down
22 changes: 19 additions & 3 deletions packages/signer/signer-btc/src/internal/app-binder/BtcAppBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import { inject, injectable } from "inversify";

import {
GetExtendedPublicKeyDAInput,
GetExtendedPublicKeyReturnType,
GetExtendedPublicKeyDAReturnType,
} from "@api/app-binder/GetExtendedPublicKeyDeviceActionTypes";
import { SignMessageDAReturnType } from "@api/index";
import { SignMessageDAReturnType } from "@api/app-binder/SignMessageDeviceActionTypes";
import { SignPsbtDAReturnType } from "@api/app-binder/SignPsbtDeviceActionTypes";
import { Psbt } from "@api/model/Psbt";
import { Wallet } from "@api/model/Wallet";
import { GetExtendedPublicKeyCommand } from "@internal/app-binder/command/GetExtendedPublicKeyCommand";
import { SignPsbtDeviceAction } from "@internal/app-binder/device-action/SignPsbt/SignPsbtDeviceAction";
import { externalTypes } from "@internal/externalTypes";

import { SignMessageDeviceAction } from "./device-action/SignMessage/SignMessageDeviceAction";
Expand All @@ -25,7 +29,7 @@ export class BtcAppBinder {

getExtendedPublicKey(
args: GetExtendedPublicKeyDAInput,
): GetExtendedPublicKeyReturnType {
): GetExtendedPublicKeyDAReturnType {
return this.dmk.executeDeviceAction({
sessionId: this.sessionId,
deviceAction: new SendCommandInAppDeviceAction({
Expand Down Expand Up @@ -54,4 +58,16 @@ export class BtcAppBinder {
}),
});
}

signPsbt(args: { psbt: Psbt; wallet: Wallet }): SignPsbtDAReturnType {
return this.dmk.executeDeviceAction({
sessionId: this.sessionId,
deviceAction: new SignPsbtDeviceAction({
input: {
psbt: args.psbt,
wallet: args.wallet,
},
}),
});
}
}
Loading

0 comments on commit 1b61add

Please sign in to comment.