Skip to content

Commit

Permalink
♻️ (signer-btc): Plug BtcAppBinder DI
Browse files Browse the repository at this point in the history
  • Loading branch information
jdabbech-ledger committed Jan 13, 2025
1 parent a7556f1 commit 0d522da
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import {

import { type Signature } from "@api/model/Signature";
import { type BtcErrorCodes } from "@internal/app-binder/command/utils/bitcoinAppErrors";
import { type DataStoreService } from "@internal/data-store/service/DataStoreService";

export type SignMessageDAOutput = Signature;

export type SignMessageDAInput = {
readonly derivationPath: string;
readonly message: string;
readonly dataStoreService: DataStoreService;
};

export type SignMessageDAError =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,27 @@ import {
} from "@ledgerhq/device-management-kit";

import { type Psbt } from "@api/model/Psbt";
import { type Wallet } from "@api/model/Wallet";
import { type Wallet as ApiWallet } from "@api/model/Wallet";
import { type BtcErrorCodes } from "@internal/app-binder/command/utils/bitcoinAppErrors";
import { type BuildPsbtTaskResult } from "@internal/app-binder/task/BuildPsbtTask";
import { type PsbtSignature } from "@internal/app-binder/task/SignPsbtTask";
import { type DataStoreService } from "@internal/data-store/service/DataStoreService";
import { type PsbtMapper } from "@internal/psbt/service/psbt/PsbtMapper";
import { type ValueParser } from "@internal/psbt/service/value/ValueParser";
import { type Wallet as InternalWallet } from "@internal/wallet/model/Wallet";
import { type WalletBuilder } from "@internal/wallet/service/WalletBuilder";
import { type WalletSerializer } from "@internal/wallet/service/WalletSerializer";

// @toDo Update this return value to Psbt once it would be updated in SignPsbtTask
export type SignPsbtDAOutput = Uint8Array[];
export type SignPsbtDAOutput = PsbtSignature[];

export type SignPsbtDAInput = {
psbt: Psbt;
wallet: Wallet;
wallet: ApiWallet;
walletBuilder: WalletBuilder;
walletSerializer: WalletSerializer;
dataStoreService: DataStoreService;
psbtMapper: PsbtMapper;
valueParser: ValueParser;
};

export type SignPsbtDAError =
Expand All @@ -39,8 +51,9 @@ export type SignPsbtDAState = DeviceActionState<

export type SignPsbtDAInternalState = {
readonly error: SignPsbtDAError | null;
// [SHOULD] be psbt instead of signature
readonly signature: Uint8Array[] | null;
readonly wallet: InternalWallet | null;
readonly buildPsbtResult: BuildPsbtTaskResult | null;
readonly signatures: PsbtSignature[] | null;
};

export type SignPsbtDAReturnType = ExecuteDeviceActionReturnType<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import {
import { type Signature } from "@api/model/Signature";
import { BtcAppBinder } from "@internal/app-binder/BtcAppBinder";
import { GetExtendedPublicKeyCommand } from "@internal/app-binder/command/GetExtendedPublicKeyCommand";
import { type DataStoreService } from "@internal/data-store/service/DataStoreService";
import { type PsbtMapper } from "@internal/psbt/service/psbt/PsbtMapper";
import { type ValueParser } from "@internal/psbt/service/value/ValueParser";
import { type WalletBuilder } from "@internal/wallet/service/WalletBuilder";
import { type WalletSerializer } from "@internal/wallet/service/WalletSerializer";

describe("BtcAppBinder", () => {
const mockedDmk: DeviceManagementKit = {
Expand All @@ -36,6 +41,11 @@ describe("BtcAppBinder", () => {
const binder = new BtcAppBinder(
{} as DeviceManagementKit,
{} as DeviceSessionId,
{} as WalletBuilder,
{} as WalletSerializer,
{} as DataStoreService,
{} as PsbtMapper,
{} as ValueParser,
);
expect(binder).toBeDefined();
});
Expand Down Expand Up @@ -66,7 +76,15 @@ describe("BtcAppBinder", () => {
});

// WHEN
const appBinder = new BtcAppBinder(mockedDmk, "sessionId");
const appBinder = new BtcAppBinder(
mockedDmk,
"sessionId",
{} as WalletBuilder,
{} as WalletSerializer,
{} as DataStoreService,
{} as PsbtMapper,
{} as ValueParser,
);
const { observable } = appBinder.getExtendedPublicKey({
derivationPath: "44'/501'",
checkOnDevice: false,
Expand Down Expand Up @@ -116,7 +134,15 @@ describe("BtcAppBinder", () => {
};

// WHEN
const appBinder = new BtcAppBinder(mockedDmk, "sessionId");
const appBinder = new BtcAppBinder(
mockedDmk,
"sessionId",
{} as WalletBuilder,
{} as WalletSerializer,
{} as DataStoreService,
{} as PsbtMapper,
{} as ValueParser,
);
appBinder.getExtendedPublicKey(params);

// THEN
Expand All @@ -141,7 +167,15 @@ describe("BtcAppBinder", () => {
};

// WHEN
const appBinder = new BtcAppBinder(mockedDmk, "sessionId");
const appBinder = new BtcAppBinder(
mockedDmk,
"sessionId",
{} as WalletBuilder,
{} as WalletSerializer,
{} as DataStoreService,
{} as PsbtMapper,
{} as ValueParser,
);
appBinder.getExtendedPublicKey(params);

// THEN
Expand Down Expand Up @@ -184,7 +218,15 @@ describe("BtcAppBinder", () => {
});

// WHEN
const appBinder = new BtcAppBinder(mockedDmk, "sessionId");
const appBinder = new BtcAppBinder(
mockedDmk,
"sessionId",
{} as WalletBuilder,
{} as WalletSerializer,
{} as DataStoreService,
{} as PsbtMapper,
{} as ValueParser,
);
const { observable } = appBinder.signMessage({
derivationPath: "44'/60'/3'/2/1",
message,
Expand Down
42 changes: 34 additions & 8 deletions packages/signer/signer-btc/src/internal/app-binder/BtcAppBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,42 @@ 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 { dataStoreTypes } from "@internal/data-store/di/dataStoreTypes";
import type { DataStoreService } from "@internal/data-store/service/DataStoreService";
import { externalTypes } from "@internal/externalTypes";
import { psbtTypes } from "@internal/psbt/di/psbtTypes";
import type { PsbtMapper } from "@internal/psbt/service/psbt/PsbtMapper";
import type { ValueParser } from "@internal/psbt/service/value/ValueParser";
import { walletTypes } from "@internal/wallet/di/walletTypes";
import type { WalletBuilder } from "@internal/wallet/service/WalletBuilder";
import type { WalletSerializer } from "@internal/wallet/service/WalletSerializer";

import { SignMessageDeviceAction } from "./device-action/SignMessage/SignMessageDeviceAction";

@injectable()
export class BtcAppBinder {
constructor(
@inject(externalTypes.Dmk) private dmk: DeviceManagementKit,
@inject(externalTypes.SessionId) private sessionId: DeviceSessionId,
@inject(externalTypes.Dmk)
private readonly _dmk: DeviceManagementKit,
@inject(externalTypes.SessionId)
private readonly _sessionId: DeviceSessionId,
@inject(walletTypes.WalletBuilder)
private readonly _walletBuilder: WalletBuilder,
@inject(walletTypes.WalletSerializer)
private readonly _walletSerializer: WalletSerializer,
@inject(dataStoreTypes.DataStoreService)
private readonly _dataStoreService: DataStoreService,
@inject(psbtTypes.PsbtMapper)
private readonly _psbtMapper: PsbtMapper,
@inject(psbtTypes.ValueParser)
private readonly _valueParser: ValueParser,
) {}

getExtendedPublicKey(
args: GetExtendedPublicKeyDAInput,
): GetExtendedPublicKeyDAReturnType {
return this.dmk.executeDeviceAction({
sessionId: this.sessionId,
return this._dmk.executeDeviceAction({
sessionId: this._sessionId,
deviceAction: new SendCommandInAppDeviceAction({
input: {
command: new GetExtendedPublicKeyCommand(args),
Expand All @@ -48,24 +68,30 @@ export class BtcAppBinder {
derivationPath: string;
message: string;
}): SignMessageDAReturnType {
return this.dmk.executeDeviceAction({
sessionId: this.sessionId,
return this._dmk.executeDeviceAction({
sessionId: this._sessionId,
deviceAction: new SignMessageDeviceAction({
input: {
derivationPath: args.derivationPath,
message: args.message,
dataStoreService: this._dataStoreService,
},
}),
});
}

signPsbt(args: { psbt: Psbt; wallet: Wallet }): SignPsbtDAReturnType {
return this.dmk.executeDeviceAction({
sessionId: this.sessionId,
return this._dmk.executeDeviceAction({
sessionId: this._sessionId,
deviceAction: new SignPsbtDeviceAction({
input: {
psbt: args.psbt,
wallet: args.wallet,
walletBuilder: this._walletBuilder,
walletSerializer: this._walletSerializer,
dataStoreService: this._dataStoreService,
psbtMapper: this._psbtMapper,
valueParser: this._valueParser,
},
}),
});
Expand Down
13 changes: 12 additions & 1 deletion packages/signer/signer-btc/src/internal/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import {
} from "@ledgerhq/device-management-kit";
import { Container } from "inversify";

import { dataStoreModuleFactory } from "@internal/data-store/di/dataStoreModule";
import { externalTypes } from "@internal/externalTypes";
import { merkleTreeModuleFactory } from "@internal/merkle-tree/di/merkleTreeModule";
import { psbtModuleFactory } from "@internal/psbt/di/psbtModule";
import { useCasesModuleFactory } from "@internal/use-cases/di/useCasesModule";
import { walletModuleFactory } from "@internal/wallet/di/walletModule";

import { appBinderModuleFactory } from "./app-binder/di/appBinderModule";

Expand All @@ -21,7 +25,14 @@ export const makeContainer = ({ dmk, sessionId }: MakeContainerProps) => {
.bind<DeviceSessionId>(externalTypes.SessionId)
.toConstantValue(sessionId);

container.load(appBinderModuleFactory(), useCasesModuleFactory());
container.load(
appBinderModuleFactory(),
useCasesModuleFactory(),
walletModuleFactory(),
psbtModuleFactory(),
dataStoreModuleFactory(),
merkleTreeModuleFactory(),
);

return container;
};

0 comments on commit 0d522da

Please sign in to comment.