Skip to content

Commit

Permalink
✨ (signer-solana): Add solana SignMessageUseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
fAnselmi-Ledger committed Nov 4, 2024
1 parent ba940af commit c86d8a4
Show file tree
Hide file tree
Showing 14 changed files with 839 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-starfishes-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/device-signer-kit-solana": patch
---

Added Solana SignMessageUseCase
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
type DeviceActionIntermediateValue,
type DeviceActionState,
DeviceActionStatus,
type DeviceManagementKit,
type DeviceSessionId,
type DmkError,
SendCommandInAppDeviceAction,
UserInteractionRequired,
} from "@ledgerhq/device-management-kit";
Expand All @@ -15,6 +17,7 @@ import {
} from "@api/index";

import { GetPubKeyCommand } from "./command/GetPubKeyCommand";
import { SignMessageDeviceAction } from "./device-action/SignPersonalMessage/SignMessageDeviceAction";
import { SolanaAppBinder } from "./SolanaAppBinder";

describe("SolanaAppBinder", () => {
Expand Down Expand Up @@ -147,4 +150,98 @@ describe("SolanaAppBinder", () => {
});
});
});
describe("SolanaAppBinder", () => {
const mockedDmk: DeviceManagementKit = {
sendCommand: jest.fn(),
executeDeviceAction: jest.fn(),
} as unknown as DeviceManagementKit;

beforeEach(() => {
jest.clearAllMocks();
});

it("should be defined", () => {
const binder = new SolanaAppBinder(
{} as DeviceManagementKit,
{} as DeviceSessionId,
);
expect(binder).toBeDefined();
});

describe("signMessage", () => {
it("should return the signed message", (done) => {
// GIVEN
const signedMessage = new Uint8Array([0x1c, 0x8a, 0x54, 0x05, 0x10]); // Example signed message data
const signMessageArgs = {
derivationPath: "44'/501'",
message: "Hello world",
};

jest.spyOn(mockedDmk, "executeDeviceAction").mockReturnValue({
observable: from([
{
status: DeviceActionStatus.Completed,
output: signedMessage,
} as DeviceActionState<
Uint8Array,
DmkError,
DeviceActionIntermediateValue
>,
]),
cancel: jest.fn(),
});

// WHEN
const appBinder = new SolanaAppBinder(mockedDmk, "sessionId");
const { observable } = appBinder.signMessage(signMessageArgs);

// THEN
const states: DeviceActionState<Uint8Array, unknown, unknown>[] = [];
observable.subscribe({
next: (state) => {
states.push(state);
},
error: (err) => {
done(err);
},
complete: () => {
try {
expect(states).toEqual([
{
status: DeviceActionStatus.Completed,
output: signedMessage,
},
]);
done();
} catch (err) {
done(err);
}
},
});
});

it("should call executeDeviceAction with correct parameters", () => {
// GIVEN
const signMessageArgs = {
derivationPath: "44'/501'",
message: "Hello world",
};

// WHEN
const appBinder = new SolanaAppBinder(mockedDmk, "sessionId");
appBinder.signMessage(signMessageArgs);

// THEN
expect(mockedDmk.executeDeviceAction).toHaveBeenCalledWith({
sessionId: "sessionId",
deviceAction: new SignMessageDeviceAction({
input: {
derivationPath: signMessageArgs.derivationPath,
message: signMessageArgs.message,
},
}),
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Transaction } from "@api/model/Transaction";
import { externalTypes } from "@internal/externalTypes";

import { GetPubKeyCommand } from "./command/GetPubKeyCommand";
import { SignMessageDeviceAction } from "./device-action/SignPersonalMessage/SignMessageDeviceAction";

@injectable()
export class SolanaAppBinder {
Expand Down Expand Up @@ -47,11 +48,19 @@ export class SolanaAppBinder {
return {} as SignTransactionDAReturnType;
}

signMessage(_args: {
signMessage(args: {
derivationPath: string;
message: string;
}): SignMessageDAReturnType {
return {} as SignMessageDAReturnType;
return this.dmk.executeDeviceAction({
sessionId: this.sessionId,
deviceAction: new SignMessageDeviceAction({
input: {
derivationPath: args.derivationPath,
message: args.message,
},
}),
});
}

getAppConfiguration(): GetAppConfigurationDAReturnType {
Expand Down
Loading

0 comments on commit c86d8a4

Please sign in to comment.