Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (signer-solana) [DSDK-565]: Implement SignTransaction usecase #489

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pink-hats-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/device-signer-kit-solana": minor
---

Add SignTransaction usecase
5 changes: 5 additions & 0 deletions .changeset/spicy-toes-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/device-management-kit-sample": patch
---

Add signTransaction usecase for Solana signer
36 changes: 36 additions & 0 deletions apps/sample/src/components/SignerSolanaView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import React, { useMemo } from "react";
import {
base64StringToBuffer,
isBase64String,
} from "@ledgerhq/device-management-kit";
import {
type GetAddressDAError,
type GetAddressDAIntermediateValue,
Expand All @@ -7,6 +11,9 @@ import {
type GetAppConfigurationDAIntermediateValue,
type GetAppConfigurationDAOutput,
SignerSolanaBuilder,
type SignTransactionDAError,
type SignTransactionDAIntermediateValue,
type SignTransactionDAOutput,
} from "@ledgerhq/device-signer-kit-solana";

import { DeviceActionsList } from "@/components/DeviceActionsView/DeviceActionsList";
Expand Down Expand Up @@ -51,6 +58,35 @@ export const SignerSolanaView: React.FC<{ sessionId: string }> = ({
GetAddressDAError,
GetAddressDAIntermediateValue
>,
{
title: "Sign Transaction",
description:
"Perform all the actions necessary to sign a Solana transaction with the device",
executeDeviceAction: ({ derivationPath, transaction }) => {
const serializedTransaction =
base64StringToBuffer(transaction) ?? new Uint8Array();
return signer.signTransaction(
derivationPath,
serializedTransaction,
{},
);
},
initialValues: {
derivationPath: DEFAULT_DERIVATION_PATH,
transaction: "",
},
deviceModelId,
validateValues: ({ transaction }) =>
isBase64String(transaction) && transaction.length > 0,
} satisfies DeviceActionProps<
SignTransactionDAOutput,
{
derivationPath: string;
transaction: string;
},
SignTransactionDAError,
SignTransactionDAIntermediateValue
>,
{
title: "Get app configuration",
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export type SignTransactionDAState = DeviceActionState<

export type SignTransactionDAInternalState = {
readonly error: SignTransactionDAError | null;
readonly serializedTransaction: Uint8Array | null;
readonly signature: Signature | null;
};

Expand Down
9 changes: 9 additions & 0 deletions packages/signer/signer-solana/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,14 @@ export type {
GetAppConfigurationDAIntermediateValue,
GetAppConfigurationDAOutput,
} from "@api/app-binder/GetAppConfigurationDeviceActionTypes";
export type {
SignTransactionDAError,
SignTransactionDAIntermediateValue,
SignTransactionDAOutput,
SignTransactionDAReturnType,
} from "@api/app-binder/SignTransactionDeviceActionTypes";
export type { Signature } from "@api/model/Signature";
export type { Transaction } from "@api/model/Transaction";
export type { TransactionOptions } from "@api/model/TransactionOptions";
export type { SignerSolana } from "@api/SignerSolana";
export { SignerSolanaBuilder } from "@api/SignerSolanaBuilder";
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ describe("DefaultSignerSolana", () => {
expect(dmk.executeDeviceAction).toHaveBeenCalled();
});

it("should call signTransaction", () => {
const dmk = {
executeDeviceAction: jest.fn(),
} as unknown as DeviceManagementKit;
const sessionId = {} as DeviceSessionId;
const signer = new DefaultSignerSolana({ dmk, sessionId });
signer.signTransaction("derivationPath", new Uint8Array(), {});
expect(dmk.executeDeviceAction).toHaveBeenCalled();
});

it("should call getAppConfiguration", () => {
const dmk = {
executeDeviceAction: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { type SignerSolana } from "@api/SignerSolana";
import { type GetAddressUseCase } from "./use-cases/address/GetAddressUseCase";
import { type GetAppConfigurationUseCase } from "./use-cases/app-configuration/GetAppConfigurationUseCase";
import { useCasesTypes } from "./use-cases/di/useCasesTypes";
import { type SignTransactionUseCase } from "./use-cases/transaction/SignTransactionUseCase";
import { makeContainer } from "./di";

export type DefaultSignerSolanaConstructorArgs = {
Expand All @@ -31,11 +32,13 @@ export class DefaultSignerSolana implements SignerSolana {
}

signTransaction(
_derivationPath: string,
_transaction: Transaction,
_options?: TransactionOptions,
derivationPath: string,
transaction: Transaction,
options?: TransactionOptions,
): SignTransactionDAReturnType {
return {} as SignTransactionDAReturnType;
return this._container
.get<SignTransactionUseCase>(useCasesTypes.SignTransactionUseCase)
.execute(derivationPath, transaction, options);
}

signMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ import {
type GetAddressDAError,
type GetAddressDAIntermediateValue,
type GetAddressDAOutput,
type SignTransactionDAError,
type SignTransactionDAIntermediateValue,
type SignTransactionDAOutput,
} from "@api/index";

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

describe("SolanaAppBinder", () => {
Expand Down Expand Up @@ -153,6 +157,84 @@ describe("SolanaAppBinder", () => {
});
});

describe("signTransaction", () => {
it("should return the signature", (done) => {
// GIVEN
const signature = new Uint8Array([0x01, 0x02, 0x03]);

jest.spyOn(mockedDmk, "executeDeviceAction").mockReturnValue({
observable: from([
{
status: DeviceActionStatus.Completed,
output: signature,
} as DeviceActionState<
SignTransactionDAOutput,
SignTransactionDAError,
SignTransactionDAIntermediateValue
>,
]),
cancel: jest.fn(),
});

// WHEN
const appBinder = new SolanaAppBinder(mockedDmk, "sessionId");
const { observable } = appBinder.signTransaction({
derivationPath: "44'/501'",
transaction: new Uint8Array([0x01, 0x02, 0x03, 0x04]),
});

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

it("should call executeDeviceAction with the correct params", () => {
// GIVEN
const derivationPath = "44'/60'/3'/2/1";
const transaction = new Uint8Array([0x01, 0x02, 0x03, 0x04]);

// WHEN
const appBinder = new SolanaAppBinder(mockedDmk, "sessionId");
appBinder.signTransaction({ derivationPath, transaction });

// THEN
expect(mockedDmk.executeDeviceAction).toHaveBeenCalledWith({
deviceAction: new SignTransactionDeviceAction({
input: {
derivationPath,
transaction,
options: {},
},
}),
sessionId: "sessionId",
});
});
});

describe("getAppConfiguration", () => {
it("should return the app configuration", (done) => {
// GIVEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import { GetAppConfigurationDAReturnType } from "@api/app-binder/GetAppConfigura
import { SignMessageDAReturnType } from "@api/app-binder/SignMessageDeviceActionTypes";
import { SignTransactionDAReturnType } from "@api/app-binder/SignTransactionDeviceActionTypes";
import { Transaction } from "@api/model/Transaction";
import { TransactionOptions } from "@api/model/TransactionOptions";
import { externalTypes } from "@internal/externalTypes";

import { GetAppConfigurationCommand } from "./command/GetAppConfigurationCommand";
import { GetPubKeyCommand } from "./command/GetPubKeyCommand";
import { SignTransactionDeviceAction } from "./device-action/SignTransactionDeviceAction";

@injectable()
export class SolanaAppBinder {
Expand All @@ -41,11 +43,21 @@ export class SolanaAppBinder {
});
}

signTransaction(_args: {
signTransaction(args: {
derivationPath: string;
transaction: Transaction;
options?: TransactionOptions;
}): SignTransactionDAReturnType {
return {} as SignTransactionDAReturnType;
return this.dmk.executeDeviceAction({
sessionId: this.sessionId,
deviceAction: new SignTransactionDeviceAction({
input: {
derivationPath: args.derivationPath,
transaction: args.transaction,
options: args.options ?? {},
},
}),
});
}

signMessage(_args: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
describe("SignTransactionCommand", () => {
const defaultArgs: SignTransactionCommandArgs = {
serializedTransaction: new Uint8Array(),
more: false,
extend: false,
};

describe("getApdu", () => {
Expand All @@ -37,6 +39,8 @@ describe("SignTransactionCommand", () => {
// GIVEN
const command = new SignTransactionCommand({
serializedTransaction: new Uint8Array([0x01, 0x02, 0x03]),
more: false,
extend: false,
});

// WHEN
Expand All @@ -49,6 +53,63 @@ describe("SignTransactionCommand", () => {
expect(apdu.p1).toBe(0x01);
expect(apdu.p2).toBe(0x00);
});

it("should return the correct APDU when the more flag is set", () => {
// GIVEN
const command = new SignTransactionCommand({
serializedTransaction: new Uint8Array([0x01, 0x02, 0x03]),
more: true,
extend: false,
});

// WHEN
const apdu = command.getApdu();

// THEN
expect(apdu.data).toStrictEqual(new Uint8Array([0x01, 0x02, 0x03]));
expect(apdu.cla).toBe(0xe0);
expect(apdu.ins).toBe(0x06);
expect(apdu.p1).toBe(0x01);
expect(apdu.p2).toBe(0x02);
});

it("should return the correct APDU when the extend flag is set", () => {
// GIVEN
const command = new SignTransactionCommand({
serializedTransaction: new Uint8Array([0x01, 0x02, 0x03]),
more: false,
extend: true,
});

// WHEN
const apdu = command.getApdu();

// THEN
expect(apdu.data).toStrictEqual(new Uint8Array([0x01, 0x02, 0x03]));
expect(apdu.cla).toBe(0xe0);
expect(apdu.ins).toBe(0x06);
expect(apdu.p1).toBe(0x01);
expect(apdu.p2).toBe(0x01);
});

it("should return the correct APDU when the more and extend flags are set", () => {
// GIVEN
const command = new SignTransactionCommand({
serializedTransaction: new Uint8Array([0x01, 0x02, 0x03]),
more: true,
extend: true,
});

// WHEN
const apdu = command.getApdu();

// THEN
expect(apdu.data).toStrictEqual(new Uint8Array([0x01, 0x02, 0x03]));
expect(apdu.cla).toBe(0xe0);
expect(apdu.ins).toBe(0x06);
expect(apdu.p1).toBe(0x01);
expect(apdu.p2).toBe(0x03);
});
});

describe("parseResponse", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export type SignTransactionCommandArgs = {
* Chunked serialized transaction
*/
readonly serializedTransaction: Uint8Array;
readonly more: boolean;
readonly extend: boolean;
};

export class SignTransactionCommand
Expand All @@ -36,12 +38,16 @@ export class SignTransactionCommand
}

getApdu(): Apdu {
const { serializedTransaction } = this.args;
const { more, extend, serializedTransaction } = this.args;
let p2 = 0x00;
if (more) p2 |= 0x02;
if (extend) p2 |= 0x01;

const signTransactionArgs: ApduBuilderArgs = {
cla: 0xe0,
ins: 0x06,
p1: 0x01,
p2: 0x00,
p2,
};

return new ApduBuilder(signTransactionArgs)
Expand Down
Loading