Skip to content

Commit

Permalink
✨ (keyring-eth): Add ProvideTransactionContextTask
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyuzhuang committed Aug 28, 2024
1 parent e5ddcfe commit cd2bc78
Show file tree
Hide file tree
Showing 15 changed files with 398 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-parents-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/keyring-eth": patch
---

Implement ProvideTransactionContextTask
5 changes: 5 additions & 0 deletions .changeset/slow-eggs-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/context-module": patch
---

Improve code visibility and update command implementations
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@ import {
type ApduBuilderArgs,
ApduResponse,
type Command,
CommandResult,
type CommandResult,
CommandResultFactory,
CommandUtils,
GlobalCommandErrorHandler,
} from "@ledgerhq/device-sdk-core";

export type ProvideDomainNameCommandArgs = {
/**
* The chunk of the stringified hexa representation of the domain name prefixed by its length in two bytes.
* If the index equals 0, the first two bytes are the length of the domain name, else all the bytes are the chunk data.
* @example "00064C6564676572" (hexa for "Ledger", first chunk and only chunk)
*/
data: Uint8Array;
/**
* The index of the chunk.
*/
isFirstChunk: boolean;
};

/**
* The length of the payload will take 2 bytes in the APDU.
*/
export const PAYLOAD_LENGTH_BYTES = 2;

/**
* The command that provides a chunk of the domain name to the device.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
ApduParser,
ApduResponse,
type Command,
CommandErrorArgs,
CommandErrors,
CommandResult,
type CommandErrorArgs,
type CommandErrors,
type CommandResult,
CommandResultFactory,
CommandUtils,
DeviceExchangeError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
type ApduBuilderArgs,
ApduParser,
ApduResponse,
Command,
CommandResult,
type Command,
type CommandResult,
CommandResultFactory,
CommandUtils,
GlobalCommandErrorHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
type ApduBuilderArgs,
ApduResponse,
type Command,
CommandResult,
type CommandResult,
CommandResultFactory,
CommandUtils,
GlobalCommandErrorHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import {
SetExternalPluginCommandError,
} from "@internal/app-binder/command/SetExternalPluginCommand";

/** Test payload contains:
/**
* Test payload contains:
* Length of plugin name : 08
* Plugin Name : Paraswap
* contract address: 0xdef171fe48cf0115b1d80b88dc8eab59176fee57
* method selector: 0xa9059cbb
* **/
*/
const SET_EXTERNAL_PLUGIN_PAYLOAD = [
0x08, 0x50, 0x61, 0x72, 0x61, 0x73, 0x77, 0x61, 0x70, 0xde, 0xf1, 0x71, 0xfe,
0x48, 0xcf, 0x01, 0x15, 0xb1, 0xd8, 0x0b, 0x88, 0xdc, 0x8e, 0xab, 0x59, 0x17,
Expand Down Expand Up @@ -67,24 +68,28 @@ describe("Set External plugin", () => {
${Uint8Array.from([0x6d, 0x00])} | ${"6d00"}
`(
"should return an error for the response status code $errorCode",
({ apduResponseCode, errorCode }) => {
({
apduResponseCode,
errorCode,
}: Record<"apduResponseCode" | "errorCode", Uint8Array>) => {
// GIVEN
const response = new ApduResponse({
data: Uint8Array.from([]),
statusCode: apduResponseCode,
});
const command = new SetExternalPluginCommand({
payload: Uint8Array.from([]),
signature: Uint8Array.from([]),
payload: "",
signature: "",
});
// WHEN
const result = command.parseResponse(response);
// THEN
expect(isSuccessCommandResult(result)).toBe(false);
// @ts-ignore
expect(result.error).toBeInstanceOf(SetExternalPluginCommandError);
// @ts-ignore
expect(result.error.errorCode).toStrictEqual(errorCode);
if (!isSuccessCommandResult(result)) {
expect(result.error).toBeInstanceOf(SetExternalPluginCommandError);
if (result.error instanceof SetExternalPluginCommandError)
expect(result.error.errorCode).toStrictEqual(errorCode);
}
},
);
it("should return a global error", () => {
Expand All @@ -101,10 +106,11 @@ describe("Set External plugin", () => {
// then
const result = command.parseResponse(apduResponse);
expect(isSuccessCommandResult(result)).toBe(false);
// @ts-ignore
expect(result.error).toBeInstanceOf(GlobalCommandError);
// @ts-ignore
expect(result.error.errorCode).toStrictEqual("5515");
if (!isSuccessCommandResult(result)) {
expect(result.error).toBeInstanceOf(GlobalCommandError);
if (result.error instanceof GlobalCommandError)
expect(result.error.errorCode).toStrictEqual("5515");
}
});
it("should return void if status is success", () => {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
type ApduBuilderArgs,
ApduParser,
ApduResponse,
Command,
CommandErrorArgs,
CommandErrors,
CommandResult,
type Command,
type CommandErrorArgs,
type CommandErrors,
type CommandResult,
CommandResultFactory,
CommandUtils,
DeviceExchangeError,
Expand All @@ -21,7 +21,7 @@ type SetExternalPluginCommandArgs = {
signature?: string;
};

type SetExternalPluginCommandErrorCodes = "6a80" | "6984" | "6d00";
export type SetExternalPluginCommandErrorCodes = "6a80" | "6984" | "6d00";

const SET_EXTERNAL_PLUGIN_ERRORS: CommandErrors<SetExternalPluginCommandErrorCodes> =
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {

import {
SetPluginCommand,
SetPluginCommandArgs,
type SetPluginCommandArgs,
SetPluginCommandError,
} from "./SetPluginCommand";

Expand Down Expand Up @@ -46,7 +46,10 @@ describe("SetPluginCommand", () => {
${Uint8Array.from([0x6d, 0x00])} | ${"6d00"}
`(
"should return an error for the response status code $errorCode",
({ apduResponseCode, errorCode }) => {
({
apduResponseCode,
errorCode,
}: Record<"apduResponseCode" | "errorCode", Uint8Array>) => {
// GIVEN
const response = new ApduResponse({
data: Uint8Array.from([]),
Expand All @@ -57,10 +60,11 @@ describe("SetPluginCommand", () => {
const result = command.parseResponse(response);
// THEN
expect(isSuccessCommandResult(result)).toBe(false);
// @ts-ignore
expect(result.error).toBeInstanceOf(SetPluginCommandError);
// @ts-ignore
expect(result.error.errorCode).toStrictEqual(errorCode);
if (!isSuccessCommandResult(result)) {
expect(result.error).toBeInstanceOf(SetPluginCommandError);
if (result.error instanceof SetPluginCommandError)
expect(result.error.errorCode).toStrictEqual(errorCode);
}
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import {
ApduParser,
ApduResponse,
type Command,
CommandErrorArgs,
CommandErrors,
CommandResult,
type CommandErrorArgs,
type CommandErrors,
type CommandResult,
CommandResultFactory,
CommandUtils,
DeviceExchangeError,
GlobalCommandErrorHandler,
isCommandErrorCode,
} from "@ledgerhq/device-sdk-core";

type SetPluginCommandErrorCodes = "6984" | "6d00";
export type SetPluginCommandErrorCodes = "6984" | "6d00";

const SET_PLUGIN_ERRORS: CommandErrors<SetPluginCommandErrorCodes> = {
"6984": { message: "The requested plugin is not installed on the device" },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { ClearSignContext } from "@ledgerhq/context-module";
import {
ClearSignContext,
ClearSignContextType,
} from "@ledgerhq/context-module";
import { Transaction } from "ethers-v6";
import { Left, Right } from "purify-ts";

Expand Down Expand Up @@ -61,11 +64,11 @@ describe("BuildTransactionContextTask", () => {
const serializedTransaction = new Uint8Array([0x01, 0x02, 0x03]);
const clearSignContexts: ClearSignContext[] = [
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-1",
},
{
type: "nft",
type: ClearSignContextType.NFT,
payload: "payload-2",
},
];
Expand Down Expand Up @@ -169,19 +172,19 @@ describe("BuildTransactionContextTask", () => {
const serializedTransaction = new Uint8Array([0x01, 0x02, 0x03]);
const clearSignContexts: ClearSignContext[] = [
{
type: "error",
type: ClearSignContextType.ERROR,
error: new Error("error"),
},
{
type: "token",
type: ClearSignContextType.TOKEN,
payload: "payload-1",
},
{
type: "error",
type: ClearSignContextType.ERROR,
error: new Error("error"),
},
{
type: "nft",
type: ClearSignContextType.NFT,
payload: "payload-2",
},
];
Expand Down
Loading

0 comments on commit cd2bc78

Please sign in to comment.