Skip to content

Commit

Permalink
♻️ (signer-btc): Improve sign task & device actions with di
Browse files Browse the repository at this point in the history
  • Loading branch information
jdabbech-ledger committed Jan 13, 2025
1 parent 0d522da commit 118746b
Show file tree
Hide file tree
Showing 14 changed files with 751 additions and 640 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import {
type SignPsbtCommandArgs,
} from "@internal/app-binder/command/SignPsbtCommand";

const GLOBAL_COMMITMENTS = Uint8Array.from([
const GLOBAL_COMMITMENT = Uint8Array.from([
0x05, 0x51, 0x9b, 0x38, 0xda, 0xe7, 0x44, 0x47, 0xb7, 0x21, 0x51, 0xf3, 0x54,
0xcb, 0x13, 0x8c, 0xa3, 0x59, 0x1a, 0x5f, 0xf8, 0xac, 0x81, 0x32, 0x89, 0xb1,
0x8a, 0x00, 0x4e, 0x31, 0x32, 0x16, 0x20, 0x3a, 0x22, 0x1f, 0x4b, 0xb9, 0x5e,
0x1e, 0x63, 0x81, 0x43, 0x14, 0xa6, 0x93, 0xb3, 0x59, 0x35, 0x3d, 0x46, 0x60,
0xa3, 0x43, 0x51, 0x65, 0xd3, 0xdf, 0xb7, 0x35, 0xce, 0x2d, 0xf5, 0xf5, 0x8f,
]);

const INPUTS_COMMITMENTS = Uint8Array.from([
const INPUTS_ROOT = Uint8Array.from([
0x01, 0x2a, 0xc8, 0xcd, 0xbc, 0x6f, 0xd6, 0x43, 0x70, 0x05, 0x56, 0x63, 0xf9,
0x50, 0x2f, 0xe3, 0x66, 0xed, 0xf8, 0x49, 0x70, 0xcc, 0x7d, 0x7e, 0xe8, 0xf6,
0xba, 0x47, 0x59, 0x9f, 0x11, 0x05, 0xc2,
]);

const OUTPUTS_COMMITMENTS = Uint8Array.from([
const OUTPUTS_ROOT = Uint8Array.from([
0x01, 0xd9, 0x35, 0x14, 0xd4, 0x29, 0x68, 0x8d, 0x76, 0x57, 0xc9, 0xaf, 0x0a,
0x08, 0x86, 0xac, 0x74, 0x4b, 0xd0, 0x88, 0x1c, 0x4a, 0x19, 0x10, 0xb5, 0x37,
0xfa, 0xba, 0x28, 0xcd, 0xca, 0x2e, 0x11,
Expand All @@ -43,22 +43,22 @@ const SIGN_PSBT_APDU = Uint8Array.from([
0x00,
0x01,
0xc5,
...GLOBAL_COMMITMENTS,
...GLOBAL_COMMITMENT,
0x01,
...INPUTS_COMMITMENTS,
...INPUTS_ROOT,
0x01,
...OUTPUTS_COMMITMENTS,
...OUTPUTS_ROOT,
...WALLET_ID,
...WALLET_HMAC,
]);

describe("SignPsbtCommand", () => {
const args: SignPsbtCommandArgs = {
globalCommitments: GLOBAL_COMMITMENTS,
globalCommitment: GLOBAL_COMMITMENT,
inputsCount: 1,
inputsCommitments: INPUTS_COMMITMENTS,
inputsRoot: INPUTS_ROOT,
outputsCount: 1,
outputsCommitments: OUTPUTS_COMMITMENTS,
outputsRoot: OUTPUTS_ROOT,
walletId: WALLET_ID,
walletHmac: WALLET_HMAC,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import { PROTOCOL_VERSION } from "@internal/app-binder/command/utils/constants";
import { BtcCommandUtils } from "@internal/utils/BtcCommandUtils";

export type SignPsbtCommandArgs = {
globalCommitments: Uint8Array;
globalCommitment: Uint8Array;
inputsCount: number;
inputsCommitments: Uint8Array;
inputsRoot: Uint8Array;
outputsCount: number;
outputsCommitments: Uint8Array;
outputsRoot: Uint8Array;
walletId: Uint8Array;
walletHmac: Uint8Array;
};
Expand Down Expand Up @@ -53,21 +53,21 @@ export class SignPsbtCommand
p2: PROTOCOL_VERSION,
});
const {
globalCommitments,
globalCommitment,
inputsCount,
inputsCommitments,
inputsRoot,
outputsCount,
outputsCommitments,
outputsRoot,
walletHmac,
walletId,
} = this._args;

return builder
.addBufferToData(globalCommitments)
.addBufferToData(globalCommitment)
.add8BitUIntToData(inputsCount)
.addBufferToData(inputsCommitments)
.addBufferToData(inputsRoot)
.add8BitUIntToData(outputsCount)
.addBufferToData(outputsCommitments)
.addBufferToData(outputsRoot)
.addBufferToData(walletId)
.addBufferToData(walletHmac)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { type SignMessageDAState } from "@api/index";
import { makeDeviceActionInternalApiMock } from "@internal/app-binder/device-action/__test-utils__/makeInternalApi";
import { setupOpenAppDAMock } from "@internal/app-binder/device-action/__test-utils__/setupOpenAppDAMock";
import { testDeviceActionStates } from "@internal/app-binder/device-action/__test-utils__/testDeviceActionStates";
import { type DataStoreService } from "@internal/data-store/service/DataStoreService";

import { SignMessageDeviceAction } from "./SignMessageDeviceAction";

Expand All @@ -34,7 +35,7 @@ describe("SignMessageDeviceAction", () => {
};
}

beforeEach(() => {
afterEach(() => {
jest.resetAllMocks();
});

Expand All @@ -46,6 +47,7 @@ describe("SignMessageDeviceAction", () => {
input: {
derivationPath: "44'/60'/0'/0/0",
message: "Hello world",
dataStoreService: "DataStoreService" as unknown as DataStoreService,
},
});

Expand Down Expand Up @@ -99,7 +101,6 @@ describe("SignMessageDeviceAction", () => {
deviceAction,
expectedStates,
makeDeviceActionInternalApiMock(),
done,
);

// Verify mocks calls parameters
Expand All @@ -110,9 +111,11 @@ describe("SignMessageDeviceAction", () => {
input: {
derivationPath: "44'/60'/0'/0/0",
message: "Hello world",
dataStoreService: "DataStoreService",
},
}),
);
done();
},
});
});
Expand Down Expand Up @@ -145,6 +148,7 @@ describe("SignMessageDeviceAction", () => {
input: {
derivationPath: "44'/60'/0'/0/0",
message: "Hello world",
dataStoreService: "DataStoreService" as unknown as DataStoreService,
},
});

Expand All @@ -163,6 +167,7 @@ describe("SignMessageDeviceAction", () => {
input: {
derivationPath: "44'/60'/0'/0/0",
message: "Hello world",
dataStoreService: "DataStoreService" as unknown as DataStoreService,
},
});

Expand Down Expand Up @@ -217,6 +222,7 @@ describe("SignMessageDeviceAction", () => {
input: {
derivationPath: "44'/60'/0'/0/0",
message: "Hello world",
dataStoreService: "DataStoreService" as unknown as DataStoreService,
},
});

Expand Down Expand Up @@ -269,6 +275,7 @@ describe("SignMessageDeviceAction", () => {
input: {
derivationPath: "44'/60'/0'/0/0",
message: "Hello world",
dataStoreService: "DataStoreService" as unknown as DataStoreService,
},
});

Expand Down Expand Up @@ -323,6 +330,7 @@ describe("SignMessageDeviceAction", () => {
input: {
derivationPath: "44'/60'/0'/0/0",
message: "Hello world",
dataStoreService: "DataStoreService" as unknown as DataStoreService,
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import {
SendSignMessageTask,
type SendSignMessageTaskArgs,
} from "@internal/app-binder/task/SignMessageTask";
import { type DataStoreService } from "@internal/data-store/service/DataStoreService";

export type MachineDependencies = {
readonly signMessage: (arg0: {
input: SendSignMessageTaskArgs;
input: SendSignMessageTaskArgs & { dataStoreService: DataStoreService };
}) => Promise<CommandResult<Signature, BtcErrorCodes>>;
};

Expand Down Expand Up @@ -161,6 +162,7 @@ export class SignMessageDeviceAction extends XStateDeviceAction<
input: ({ context }) => ({
derivationPath: context.input.derivationPath,
message: context.input.message,
dataStoreService: context.input.dataStoreService,
}),
onDone: {
target: "SignMessageResultCheck",
Expand Down Expand Up @@ -215,8 +217,18 @@ export class SignMessageDeviceAction extends XStateDeviceAction<
input: {
derivationPath: string;
message: string;
dataStoreService: DataStoreService;
};
}) => new SendSignMessageTask(internalApi, arg0.input).run();
}) => {
const {
input: { derivationPath, message, dataStoreService },
} = arg0;
return new SendSignMessageTask(
internalApi,
{ derivationPath, message },
dataStoreService,
).run();
};

return {
signMessage,
Expand Down
Loading

0 comments on commit 118746b

Please sign in to comment.