Skip to content

Commit

Permalink
✨ (signer-eth): Allow signing a message as a byte array
Browse files Browse the repository at this point in the history
  • Loading branch information
paoun-ledger committed Nov 6, 2024
1 parent 62638cc commit 0dcccfc
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-impalas-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/device-signer-kit-ethereum": patch
---

Allow signing a message as a byte array
2 changes: 1 addition & 1 deletion packages/signer/signer-eth/src/api/SignerEth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface SignerEth {
) => SignTransactionDAReturnType;
signMessage: (
derivationPath: string,
message: string,
message: string | Uint8Array,
) => SignPersonalMessageDAReturnType;
signTypedData: (
derivationPath: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type SignPersonalMessageDAOutput = Signature;

export type SignPersonalMessageDAInput = {
readonly derivationPath: string;
readonly message: string;
readonly message: string | Uint8Array;
};

export type SignPersonalMessageDAError =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class DefaultSignerEth implements SignerEth {

signMessage(
_derivationPath: string,
_message: string,
_message: string | Uint8Array,
): SignPersonalMessageDAReturnType {
return this._container
.get<SignMessageUseCase>(messageTypes.SignMessageUseCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class EthAppBinder {

signPersonalMessage(args: {
derivationPath: string;
message: string;
message: string | Uint8Array;
}): SignPersonalMessageDAReturnType {
return this.dmk.executeDeviceAction({
sessionId: this.sessionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type MachineDependencies = {
readonly signPersonalMessage: (arg0: {
input: {
derivationPath: string;
message: string;
message: string | Uint8Array;
};
}) => Promise<CommandResult<Signature>>;
};
Expand Down Expand Up @@ -215,7 +215,7 @@ export class SignPersonalMessageDeviceAction extends XStateDeviceAction<
const signPersonalMessage = async (arg0: {
input: {
derivationPath: string;
message: string;
message: string | Uint8Array;
};
}) => new SendSignPersonalMessageTask(internalApi, arg0.input).run();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { makeDeviceActionInternalApiMock } from "@internal/app-binder/device-act
import { SendSignPersonalMessageTask } from "./SendSignPersonalMessageTask";

const SEND_MESSAGE_HELLO_WORLD = "Hello, World!";
const SEND_MESSAGE_HELLO_WORLD_BYTES = new Uint8Array([
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21,
]);
const SEND_MESSAGE_HELLO_WORLD_DATA = new Uint8Array([
0x05, 0x80, 0x00, 0x00, 0x2c, 0x80, 0x00, 0x00, 0x3c, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x48,
Expand Down Expand Up @@ -97,6 +100,30 @@ describe("SendSignPersonalMessageTask", () => {
expect((result as any).data).toStrictEqual(signature);
});

it("should send the message as byte arrays", async () => {
// GIVEN
const args = {
derivationPath: "44'/60'/0'/0/0",
message: SEND_MESSAGE_HELLO_WORLD_BYTES,
};
apiMock.sendCommand.mockResolvedValueOnce(resultOk);
apiMock.sendCommand.mockResolvedValueOnce(resultNothing);

// WHEN
const result = await new SendSignPersonalMessageTask(apiMock, args).run();

// THEN
expect(apiMock.sendCommand.mock.calls).toHaveLength(1);
expect(apiMock.sendCommand.mock.calls[0]![0]).toStrictEqual(
new SignPersonalMessageCommand({
data: new Uint8Array(SEND_MESSAGE_HELLO_WORLD_DATA),
isFirstChunk: true,
}),
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result as any).data).toStrictEqual(signature);
});

it("should send the long message in chunks", async () => {
// GIVEN
const args = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const PATH_SIZE = 4;

type SendSignPersonalMessageTaskArgs = {
derivationPath: string;
message: string;
message: string | Uint8Array;
};

export class SendSignPersonalMessageTask {
Expand All @@ -45,7 +45,11 @@ export class SendSignPersonalMessageTask {
// add message length
builder.add32BitUIntToData(message.length);
// add the message
builder.addAsciiStringToData(message);
if (typeof message === "string") {
builder.addAsciiStringToData(message);
} else {
builder.addBufferToData(message);
}

const buffer = builder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class SignMessageUseCase {

execute(
derivationPath: string,
message: string,
message: string | Uint8Array,
): SignPersonalMessageDAReturnType {
// 1- Sign the transaction using the app binding
return this._appBinding.signPersonalMessage({
Expand Down

0 comments on commit 0dcccfc

Please sign in to comment.