diff --git a/packages/signer/context-module/src/external-plugin/domain/ExternalPluginContextLoader.test.ts b/packages/signer/context-module/src/external-plugin/domain/ExternalPluginContextLoader.test.ts index 88c3c5c04..4433eeb92 100644 --- a/packages/signer/context-module/src/external-plugin/domain/ExternalPluginContextLoader.test.ts +++ b/packages/signer/context-module/src/external-plugin/domain/ExternalPluginContextLoader.test.ts @@ -6,6 +6,7 @@ import { ExternalPluginDataSource } from "@/external-plugin/data/ExternalPluginD import { ExternalPluginContextLoader } from "@/external-plugin/domain/ExternalPluginContextLoader"; import { DappInfos } from "@/external-plugin/model/DappInfos"; import { SelectorDetails } from "@/external-plugin/model/SelectorDetails"; +import { ClearSignContextType } from "@/shared/model/ClearSignContext"; import { TransactionContext } from "@/shared/model/TransactionContext"; import { TokenDataSource } from "@/token/data/TokenDataSource"; @@ -135,7 +136,7 @@ describe("ExternalPluginContextLoader", () => { // THEN expect(result).toEqual([ { - type: "externalPlugin", + type: ClearSignContextType.EXTERNAL_PLUGIN, payload: "1234567890", }, ]); @@ -164,11 +165,11 @@ describe("ExternalPluginContextLoader", () => { expect(result).toEqual( expect.arrayContaining([ { - type: "externalPlugin", + type: ClearSignContextType.EXTERNAL_PLUGIN, payload: "1234567890", }, { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", }, ]), @@ -200,11 +201,11 @@ describe("ExternalPluginContextLoader", () => { // THEN expect(result).toEqual([ { - type: "error", + type: ClearSignContextType.ERROR, error: new Error("error"), }, { - type: "externalPlugin", + type: ClearSignContextType.EXTERNAL_PLUGIN, payload: "1234567890", }, ]); @@ -234,15 +235,15 @@ describe("ExternalPluginContextLoader", () => { expect(result).toEqual( expect.arrayContaining([ { - type: "externalPlugin", + type: ClearSignContextType.EXTERNAL_PLUGIN, payload: "1234567890", }, { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", }, { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0xdAC17F958D2ee523a2206206994597C13D831ec7", }, ]), @@ -280,25 +281,25 @@ describe("ExternalPluginContextLoader", () => { // THEN expect(result).toEqual([ { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", }, { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0xdAC17F958D2ee523a2206206994597C13D831ec7", }, // fromToken.2 { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0xB8c77482e45F1F44dE1745F52C74426C631bDD52", }, // fromToken.-1 { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0xB8c77482e45F1F44dE1745F52C74426C631bDD52", }, { - type: "externalPlugin", + type: ClearSignContextType.EXTERNAL_PLUGIN, payload: "1234567890", }, ]); @@ -329,11 +330,11 @@ describe("ExternalPluginContextLoader", () => { // THEN expect(result).toEqual([ { - type: "error", + type: ClearSignContextType.ERROR, error: new Error("error"), }, { - type: "externalPlugin", + type: ClearSignContextType.EXTERNAL_PLUGIN, payload: "1234567890", }, ]); @@ -361,13 +362,13 @@ describe("ExternalPluginContextLoader", () => { // THEN expect(result).toEqual([ { - type: "error", + type: ClearSignContextType.ERROR, error: new Error( "[ContextModule] ExternalPluginContextLoader: Unable to parse abi", ), }, { - type: "externalPlugin", + type: ClearSignContextType.EXTERNAL_PLUGIN, payload: "1234567890", }, ]); @@ -395,7 +396,7 @@ describe("ExternalPluginContextLoader", () => { // THEN expect(result).toEqual([ { - type: "error", + type: ClearSignContextType.ERROR, error: new Error( "[ContextModule] ExternalPluginContextLoader: Unable to get address", ), @@ -429,7 +430,7 @@ describe("ExternalPluginContextLoader", () => { // THEN expect(result).toEqual([ { - type: "error", + type: ClearSignContextType.ERROR, error: new RangeError("out of result range"), }, ]); @@ -483,31 +484,31 @@ describe("ExternalPluginContextLoader", () => { // THEN expect(result).toEqual([ { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", }, { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0xdAC17F958D2ee523a2206206994597C13D831ec7", }, { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE", }, { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE", }, { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0xB8c77482e45F1F44dE1745F52C74426C631bDD52", }, { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0xB8c77482e45F1F44dE1745F52C74426C631bDD52", }, { - type: "externalPlugin", + type: ClearSignContextType.EXTERNAL_PLUGIN, payload: "1234567890", }, ]); @@ -528,7 +529,7 @@ describe("ExternalPluginContextLoader", () => { // THEN expect(result).toEqual([ { - type: "error", + type: ClearSignContextType.ERROR, error: new Error("error"), }, ]); @@ -547,7 +548,7 @@ describe("ExternalPluginContextLoader", () => { // THEN expect(result).toEqual([ { - type: "error", + type: ClearSignContextType.ERROR, error: new Error("Invalid selector"), }, ]); diff --git a/packages/signer/context-module/src/external-plugin/domain/ExternalPluginContextLoader.ts b/packages/signer/context-module/src/external-plugin/domain/ExternalPluginContextLoader.ts index 4e262ae17..014a7b61c 100644 --- a/packages/signer/context-module/src/external-plugin/domain/ExternalPluginContextLoader.ts +++ b/packages/signer/context-module/src/external-plugin/domain/ExternalPluginContextLoader.ts @@ -6,7 +6,10 @@ import { Either, EitherAsync, Left, Right } from "purify-ts"; import type { ExternalPluginDataSource } from "@/external-plugin/data/ExternalPluginDataSource"; import { externalPluginTypes } from "@/external-plugin/di/externalPluginTypes"; import { ContextLoader } from "@/shared/domain/ContextLoader"; -import { ClearSignContext } from "@/shared/model/ClearSignContext"; +import { + ClearSignContext, + ClearSignContextType, +} from "@/shared/model/ClearSignContext"; import { TransactionContext } from "@/shared/model/TransactionContext"; import type { TokenDataSource } from "@/token/data/TokenDataSource"; import { tokenTypes } from "@/token/di/tokenTypes"; @@ -25,7 +28,7 @@ export class ExternalPluginContextLoader implements ContextLoader { this._tokenDataSource = tokenDataSource; } - async load(transaction: TransactionContext) { + async load(transaction: TransactionContext): Promise { if (!transaction.to || !transaction.data || transaction.data === "0x") { return []; } @@ -33,7 +36,12 @@ export class ExternalPluginContextLoader implements ContextLoader { const selector = transaction.data.slice(0, 10); if (!isHexaString(selector)) { - return [{ type: "error" as const, error: new Error("Invalid selector") }]; + return [ + { + type: ClearSignContextType.ERROR, + error: new Error("Invalid selector"), + }, + ]; } const eitherDappInfos = await this._externalPluginDataSource.getDappInfos({ @@ -52,7 +60,7 @@ export class ExternalPluginContextLoader implements ContextLoader { } const externalPluginContext: ClearSignContext = { - type: "externalPlugin", + type: ClearSignContextType.EXTERNAL_PLUGIN, payload: dappInfos.selectorDetails.serializedData.concat( dappInfos.selectorDetails.signature, ), @@ -68,7 +76,10 @@ export class ExternalPluginContextLoader implements ContextLoader { // but also the externalPluginContext because it is still valid if (decodedCallData.isLeft()) { return [ - { type: "error", error: decodedCallData.extract() }, + { + type: ClearSignContextType.ERROR, + error: decodedCallData.extract(), + }, externalPluginContext, ]; } @@ -93,15 +104,15 @@ export class ExternalPluginContextLoader implements ContextLoader { // map the payload or the error to a ClearSignContext const contexts: ClearSignContext[] = tokensPayload.map((eitherToken) => eitherToken.caseOf({ - Left: (error) => ({ type: "error", error }), - Right: (payload) => ({ type: "token", payload }), + Left: (error) => ({ type: ClearSignContextType.ERROR, error }), + Right: (payload) => ({ type: ClearSignContextType.TOKEN, payload }), }), ); return [...contexts, externalPluginContext]; }).caseOf({ // parse all errors into ClearSignContext - Left: (error) => [{ type: "error", error }], + Left: (error) => [{ type: ClearSignContextType.ERROR, error }], Right: (contexts) => contexts, }); } diff --git a/packages/signer/context-module/src/forward-domain/domain/ForwardDomainContextLoader.test.ts b/packages/signer/context-module/src/forward-domain/domain/ForwardDomainContextLoader.test.ts index 022a888a5..789ead0d6 100644 --- a/packages/signer/context-module/src/forward-domain/domain/ForwardDomainContextLoader.test.ts +++ b/packages/signer/context-module/src/forward-domain/domain/ForwardDomainContextLoader.test.ts @@ -2,6 +2,7 @@ import { Left, Right } from "purify-ts"; import { ForwardDomainDataSource } from "@/forward-domain/data/ForwardDomainDataSource"; import { ForwardDomainContextLoader } from "@/forward-domain/domain/ForwardDomainContextLoader"; +import { ClearSignContextType } from "@/shared/model/ClearSignContext"; import { TransactionContext } from "@/shared/model/TransactionContext"; describe("ForwardDomainContextLoader", () => { @@ -39,7 +40,7 @@ describe("ForwardDomainContextLoader", () => { expect(result).toEqual([ { - type: "error", + type: ClearSignContextType.ERROR, error: new Error( "[ContextModule] ForwardDomainLoader: invalid domain", ), @@ -59,7 +60,7 @@ describe("ForwardDomainContextLoader", () => { expect(result).toEqual([ { - type: "error", + type: ClearSignContextType.ERROR, error: new Error( "[ContextModule] ForwardDomainLoader: invalid domain", ), @@ -80,7 +81,7 @@ describe("ForwardDomainContextLoader", () => { expect(result).toEqual([ { - type: "domainName", + type: ClearSignContextType.DOMAIN_NAME, payload: "payload", }, ]); @@ -103,7 +104,9 @@ describe("ForwardDomainContextLoader", () => { const result = await loader.load(transaction); // THEN - expect(result).toEqual([{ type: "error", error: new Error("error") }]); + expect(result).toEqual([ + { type: ClearSignContextType.ERROR, error: new Error("error") }, + ]); }); }); }); diff --git a/packages/signer/context-module/src/forward-domain/domain/ForwardDomainContextLoader.ts b/packages/signer/context-module/src/forward-domain/domain/ForwardDomainContextLoader.ts index f4b7a7faf..769f548c6 100644 --- a/packages/signer/context-module/src/forward-domain/domain/ForwardDomainContextLoader.ts +++ b/packages/signer/context-module/src/forward-domain/domain/ForwardDomainContextLoader.ts @@ -3,7 +3,10 @@ import { inject, injectable } from "inversify"; import type { ForwardDomainDataSource } from "@/forward-domain/data/ForwardDomainDataSource"; import { forwardDomainTypes } from "@/forward-domain/di/forwardDomainTypes"; import { ContextLoader } from "@/shared/domain/ContextLoader"; -import { ClearSignContext } from "@/shared/model/ClearSignContext"; +import { + ClearSignContext, + ClearSignContextType, +} from "@/shared/model/ClearSignContext"; import { TransactionContext } from "@/shared/model/TransactionContext"; @injectable() @@ -29,7 +32,7 @@ export class ForwardDomainContextLoader implements ContextLoader { if (!this.isDomainValid(domain)) { return [ { - type: "error", + type: ClearSignContextType.ERROR, error: new Error( "[ContextModule] ForwardDomainLoader: invalid domain", ), @@ -45,11 +48,11 @@ export class ForwardDomainContextLoader implements ContextLoader { return [ payload.caseOf({ Left: (error): ClearSignContext => ({ - type: "error", + type: ClearSignContextType.ERROR, error: error, }), Right: (value): ClearSignContext => ({ - type: "domainName", + type: ClearSignContextType.DOMAIN_NAME, payload: value, }), }), diff --git a/packages/signer/context-module/src/nft/domain/NftContextLoader.test.ts b/packages/signer/context-module/src/nft/domain/NftContextLoader.test.ts index b5c098c79..72c44fbcd 100644 --- a/packages/signer/context-module/src/nft/domain/NftContextLoader.test.ts +++ b/packages/signer/context-module/src/nft/domain/NftContextLoader.test.ts @@ -2,6 +2,7 @@ import { Left, Right } from "purify-ts"; import { NftDataSource } from "@/nft/data/NftDataSource"; import { NftContextLoader } from "@/nft/domain/NftContextLoader"; +import { ClearSignContextType } from "@/shared/model/ClearSignContext"; import { TransactionContext } from "@/shared/model/TransactionContext"; describe("NftContextLoader", () => { @@ -71,7 +72,7 @@ describe("NftContextLoader", () => { expect(result).toEqual([ { - type: "error", + type: ClearSignContextType.ERROR, error: new Error("Invalid selector"), }, ]); @@ -88,7 +89,7 @@ describe("NftContextLoader", () => { expect(result).toEqual([ { - type: "error", + type: ClearSignContextType.ERROR, error: new Error("error"), }, ]); @@ -106,7 +107,7 @@ describe("NftContextLoader", () => { expect(result).toEqual([ { - type: "error", + type: ClearSignContextType.ERROR, error: new Error("error"), }, ]); @@ -124,11 +125,11 @@ describe("NftContextLoader", () => { expect(result).toEqual([ { - type: "plugin", + type: ClearSignContextType.PLUGIN, payload: "payload1", }, { - type: "nft", + type: ClearSignContextType.NFT, payload: "payload2", }, ]); diff --git a/packages/signer/context-module/src/nft/domain/NftContextLoader.ts b/packages/signer/context-module/src/nft/domain/NftContextLoader.ts index a8fd3bc44..95846f5ef 100644 --- a/packages/signer/context-module/src/nft/domain/NftContextLoader.ts +++ b/packages/signer/context-module/src/nft/domain/NftContextLoader.ts @@ -4,7 +4,10 @@ import { inject, injectable } from "inversify"; import type { NftDataSource } from "@/nft/data/NftDataSource"; import { nftTypes } from "@/nft/di/nftTypes"; import { ContextLoader } from "@/shared/domain/ContextLoader"; -import { ClearSignContext } from "@/shared/model/ClearSignContext"; +import { + ClearSignContext, + ClearSignContextType, +} from "@/shared/model/ClearSignContext"; import { TransactionContext } from "@/shared/model/TransactionContext"; enum ERC721_SUPPORTED_SELECTOR { @@ -44,7 +47,12 @@ export class NftContextLoader implements ContextLoader { const selector = transaction.data.slice(0, 10); if (!isHexaString(selector)) { - return [{ type: "error", error: new Error("Invalid selector") }]; + return [ + { + type: ClearSignContextType.ERROR, + error: new Error("Invalid selector"), + }, + ]; } if (!this.isSelectorSupported(selector)) { @@ -63,13 +71,16 @@ export class NftContextLoader implements ContextLoader { const pluginPayload = getPluginPayloadResponse.caseOf({ Left: (error): ClearSignContext => ({ - type: "error", + type: ClearSignContextType.ERROR, error, }), - Right: (value): ClearSignContext => ({ type: "plugin", payload: value }), + Right: (value): ClearSignContext => ({ + type: ClearSignContextType.PLUGIN, + payload: value, + }), }); - if (pluginPayload.type === "error") { + if (pluginPayload.type === ClearSignContextType.ERROR) { return [pluginPayload]; } @@ -83,13 +94,16 @@ export class NftContextLoader implements ContextLoader { const nftInfosPayload = getNftInfosPayloadResponse.caseOf({ Left: (error): ClearSignContext => ({ - type: "error", + type: ClearSignContextType.ERROR, error, }), - Right: (value): ClearSignContext => ({ type: "nft", payload: value }), + Right: (value): ClearSignContext => ({ + type: ClearSignContextType.NFT, + payload: value, + }), }); - if (nftInfosPayload.type === "error") { + if (nftInfosPayload.type === ClearSignContextType.ERROR) { return [nftInfosPayload]; } diff --git a/packages/signer/context-module/src/shared/model/ClearSignContext.ts b/packages/signer/context-module/src/shared/model/ClearSignContext.ts index 52a03889f..78a039d17 100644 --- a/packages/signer/context-module/src/shared/model/ClearSignContext.ts +++ b/packages/signer/context-module/src/shared/model/ClearSignContext.ts @@ -1,10 +1,22 @@ +export enum ClearSignContextType { + TOKEN = "token", + NFT = "nft", + DOMAIN_NAME = "domainName", + PLUGIN = "plugin", + EXTERNAL_PLUGIN = "externalPlugin", + ERROR = "error", +} + export type ClearSignContextSuccess = { - type: "token" | "nft" | "domainName" | "plugin" | "externalPlugin"; + type: Exclude; + /** + * Hexadecimal string representation of the payload. + */ payload: string; }; export type ClearSignContextError = { - type: "error"; + type: ClearSignContextType.ERROR; error: Error; }; diff --git a/packages/signer/context-module/src/token/domain/TokenContextLoader.test.ts b/packages/signer/context-module/src/token/domain/TokenContextLoader.test.ts index 7e1e23640..5f1cc09a1 100644 --- a/packages/signer/context-module/src/token/domain/TokenContextLoader.test.ts +++ b/packages/signer/context-module/src/token/domain/TokenContextLoader.test.ts @@ -1,5 +1,6 @@ import { Left, Right } from "purify-ts"; +import { ClearSignContextType } from "@/shared/model/ClearSignContext"; import { TransactionContext } from "@/shared/model/TransactionContext"; import { TokenDataSource } from "@/token/data/TokenDataSource"; import { TokenContextLoader } from "@/token/domain/TokenContextLoader"; @@ -85,7 +86,10 @@ describe("TokenContextLoader", () => { // THEN expect(result).toEqual([ - { type: "error", error: new Error("Invalid selector") }, + { + type: ClearSignContextType.ERROR, + error: new Error("Invalid selector"), + }, ]); }); @@ -104,7 +108,9 @@ describe("TokenContextLoader", () => { const result = await loader.load(transaction); // THEN - expect(result).toEqual([{ type: "error", error: new Error("error") }]); + expect(result).toEqual([ + { type: ClearSignContextType.ERROR, error: new Error("error") }, + ]); }); it("should return a correct response", async () => { @@ -121,7 +127,7 @@ describe("TokenContextLoader", () => { // THEN expect(result).toEqual([ { - type: "token", + type: ClearSignContextType.TOKEN, payload: "payload-0xdAC17F958D2ee523a2206206994597C13D831ec7", }, ]); diff --git a/packages/signer/context-module/src/token/domain/TokenContextLoader.ts b/packages/signer/context-module/src/token/domain/TokenContextLoader.ts index 9ce08bf4e..b2f589f50 100644 --- a/packages/signer/context-module/src/token/domain/TokenContextLoader.ts +++ b/packages/signer/context-module/src/token/domain/TokenContextLoader.ts @@ -2,7 +2,10 @@ import { HexaString, isHexaString } from "@ledgerhq/device-sdk-core"; import { inject, injectable } from "inversify"; import { ContextLoader } from "@/shared/domain/ContextLoader"; -import { ClearSignContext } from "@/shared/model/ClearSignContext"; +import { + ClearSignContext, + ClearSignContextType, +} from "@/shared/model/ClearSignContext"; import { TransactionContext } from "@/shared/model/TransactionContext"; import type { TokenDataSource } from "@/token/data/TokenDataSource"; import { tokenTypes } from "@/token/di/tokenTypes"; @@ -32,7 +35,12 @@ export class TokenContextLoader implements ContextLoader { const selector = transaction.data.slice(0, 10); if (!isHexaString(selector)) { - return [{ type: "error", error: new Error("Invalid selector") }]; + return [ + { + type: ClearSignContextType.ERROR, + error: new Error("Invalid selector"), + }, + ]; } if (!this.isSelectorSupported(selector)) { @@ -47,10 +55,13 @@ export class TokenContextLoader implements ContextLoader { return [ payload.caseOf({ Left: (error): ClearSignContext => ({ - type: "error", + type: ClearSignContextType.ERROR, error, }), - Right: (value): ClearSignContext => ({ type: "token", payload: value }), + Right: (value): ClearSignContext => ({ + type: ClearSignContextType.TOKEN, + payload: value, + }), }), ]; } diff --git a/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideDomainNameCommand.test.ts b/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideDomainNameCommand.test.ts index 0de4c64f4..2a87c04f0 100644 --- a/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideDomainNameCommand.test.ts +++ b/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideDomainNameCommand.test.ts @@ -17,8 +17,8 @@ describe("ProvideDomainNameCommand", () => { it("should return the raw APDU", () => { // GIVEN const args: ProvideDomainNameCommandArgs = { - data: "00064C6564676572", - index: 0, + data: FIRST_CHUNK_APDU.slice(5), + isFirstChunk: true, }; // WHEN const command = new ProvideDomainNameCommand(args); @@ -36,7 +36,10 @@ describe("ProvideDomainNameCommand", () => { statusCode: Buffer.from([0x6a, 0x80]), // Invalid status code }; // WHEN - const command = new ProvideDomainNameCommand({ data: "", index: 0 }); + const command = new ProvideDomainNameCommand({ + data: Uint8Array.from([]), + isFirstChunk: true, + }); const result = command.parseResponse(response); // THEN expect(isSuccessCommandResult(result)).toBe(false); @@ -49,8 +52,12 @@ describe("ProvideDomainNameCommand", () => { statusCode: Buffer.from([0x90, 0x00]), // Success status code }; // WHEN - const command = new ProvideDomainNameCommand({ data: "", index: 0 }); + const command = new ProvideDomainNameCommand({ + data: Uint8Array.from([]), + isFirstChunk: true, + }); const result = command.parseResponse(response); + // THEN expect(isSuccessCommandResult(result)).toBe(true); }); }); diff --git a/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideDomainNameCommand.ts b/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideDomainNameCommand.ts index 08b53b37e..f48d3658d 100644 --- a/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideDomainNameCommand.ts +++ b/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideDomainNameCommand.ts @@ -17,11 +17,11 @@ export type ProvideDomainNameCommandArgs = { * 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: string; + data: Uint8Array; /** * The index of the chunk. */ - index: number; + isFirstChunk: boolean; }; /** @@ -30,19 +30,18 @@ export type ProvideDomainNameCommandArgs = { export class ProvideDomainNameCommand implements Command { - constructor(private args: ProvideDomainNameCommandArgs) {} + constructor(private readonly args: ProvideDomainNameCommandArgs) {} getApdu(): Apdu { - const isFirstChunk = this.args.index === 0; const apduBuilderArgs: ApduBuilderArgs = { cla: 0xe0, ins: 0x22, - p1: isFirstChunk ? 0x01 : 0x00, + p1: this.args.isFirstChunk ? 0x01 : 0x00, p2: 0x00, }; return new ApduBuilder(apduBuilderArgs) - .addHexaStringToData(this.args.data) + .addBufferToData(this.args.data) .build(); } diff --git a/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideNFTInformationCommand.test.ts b/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideNFTInformationCommand.test.ts index c3f15dbe1..496a35318 100644 --- a/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideNFTInformationCommand.test.ts +++ b/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideNFTInformationCommand.test.ts @@ -29,7 +29,7 @@ describe("ProvideNFTInformationCommand", () => { it("should return the raw APDU", () => { // GIVEN const args: ProvideNFTInformationCommandArgs = { - data: NFT_INFORMATION_PAYLOAD, + payload: NFT_INFORMATION_PAYLOAD, }; // WHEN const command = new ProvideNFTInformationCommand(args); @@ -47,7 +47,7 @@ describe("ProvideNFTInformationCommand", () => { statusCode: Buffer.from([0x6d, 0x00]), // Invalid status code }; // WHEN - const command = new ProvideNFTInformationCommand({ data: "" }); + const command = new ProvideNFTInformationCommand({ payload: "" }); const result = command.parseResponse(response); // THEN expect(isSuccessCommandResult(result)).toBe(false); @@ -60,7 +60,7 @@ describe("ProvideNFTInformationCommand", () => { statusCode: Buffer.from([0x90, 0x00]), // Success status code }; // WHEN - const command = new ProvideNFTInformationCommand({ data: "" }); + const command = new ProvideNFTInformationCommand({ payload: "" }); const result = command.parseResponse(response); // THEN expect(isSuccessCommandResult(result)).toBe(true); diff --git a/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideNFTInformationCommand.ts b/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideNFTInformationCommand.ts index a17f7987f..3b3395801 100644 --- a/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideNFTInformationCommand.ts +++ b/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideNFTInformationCommand.ts @@ -20,7 +20,7 @@ export type ProvideNFTInformationCommandArgs = { /** * The stringified hexa representation of the NFT data. */ - data: string; + payload: string; }; export type ProvideNFTInformationCommandErrorCodes = "6d00"; @@ -44,7 +44,7 @@ export class ProvideNFTInformationCommand ProvideNFTInformationCommandErrorCodes > { - constructor(private args: ProvideNFTInformationCommandArgs) {} + constructor(private readonly args: ProvideNFTInformationCommandArgs) {} getApdu(): Apdu { const apduBuilderArgs: ApduBuilderArgs = { @@ -53,9 +53,8 @@ export class ProvideNFTInformationCommand p1: 0x00, p2: 0x00, }; - return new ApduBuilder(apduBuilderArgs) - .addHexaStringToData(this.args.data) + .addHexaStringToData(this.args.payload) .build(); } diff --git a/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideTokenInformationCommand.ts b/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideTokenInformationCommand.ts index e6a1607fd..71edc9333 100644 --- a/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideTokenInformationCommand.ts +++ b/packages/signer/keyring-eth/src/internal/app-binder/command/ProvideTokenInformationCommand.ts @@ -2,7 +2,7 @@ import { Apdu, ApduBuilder, - ApduBuilderArgs, + type ApduBuilderArgs, ApduParser, ApduResponse, Command, @@ -28,11 +28,7 @@ export class ProvideTokenInformationCommand ProvideTokenInformationCommandArgs > { - args: ProvideTokenInformationCommandArgs; - - constructor(args: ProvideTokenInformationCommandArgs) { - this.args = args; - } + constructor(private readonly args: ProvideTokenInformationCommandArgs) {} getApdu(): Apdu { const getEthAddressArgs: ApduBuilderArgs = { @@ -41,9 +37,9 @@ export class ProvideTokenInformationCommand p1: 0x00, p2: 0x00, }; - const builder = new ApduBuilder(getEthAddressArgs); - builder.addHexaStringToData(this.args.payload); - return builder.build(); + return new ApduBuilder(getEthAddressArgs) + .addHexaStringToData(this.args.payload) + .build(); } parseResponse( diff --git a/packages/signer/keyring-eth/src/internal/app-binder/command/SendEIP712StructImplemCommand.ts b/packages/signer/keyring-eth/src/internal/app-binder/command/SendEIP712StructImplemCommand.ts index 5ca6f12c9..156c3ecb2 100644 --- a/packages/signer/keyring-eth/src/internal/app-binder/command/SendEIP712StructImplemCommand.ts +++ b/packages/signer/keyring-eth/src/internal/app-binder/command/SendEIP712StructImplemCommand.ts @@ -31,7 +31,7 @@ export type SendEIP712StructImplemCommandArgs = value: { /** * The chunk of the data that is ready to send, that is to say, prefixed by its length in two bytes. - * Eg. 01020304 => [0x00, 0x04, 0x01, 0x02, 0x03, 0x04] where 0x00, 0x04 are the length of the data. + * Eg. 01020304 => [0x00, 0x04, 0x01, 0x02, 0x03, 0x04] where [0x00, 0x04] are the length of the data. */ data: Uint8Array; isLastChunk: boolean; diff --git a/packages/signer/keyring-eth/src/internal/app-binder/command/SetExternalPluginCommand.test.ts b/packages/signer/keyring-eth/src/internal/app-binder/command/SetExternalPluginCommand.test.ts index 2d7d4d454..54a003b51 100644 --- a/packages/signer/keyring-eth/src/internal/app-binder/command/SetExternalPluginCommand.test.ts +++ b/packages/signer/keyring-eth/src/internal/app-binder/command/SetExternalPluginCommand.test.ts @@ -44,8 +44,12 @@ describe("Set External plugin", () => { it("should retrieve correct apdu", () => { // given const command = new SetExternalPluginCommand({ - payload: Uint8Array.from(SET_EXTERNAL_PLUGIN_PAYLOAD), - signature: Uint8Array.from(SET_EXTERNAL_PLUGIN_SIGNATURE), + payload: SET_EXTERNAL_PLUGIN_PAYLOAD.map((x) => + x.toString(16).padStart(2, "0"), + ).join(""), + signature: SET_EXTERNAL_PLUGIN_SIGNATURE.map((x) => + x.toString(16).padStart(2, "0"), + ).join(""), }); // when const apdu = command.getApdu(); @@ -86,8 +90,8 @@ describe("Set External plugin", () => { it("should return a global error", () => { // given const command = new SetExternalPluginCommand({ - payload: Uint8Array.from([]), - signature: Uint8Array.from([]), + payload: "", + signature: "", }); // when const apduResponse = new ApduResponse({ @@ -105,8 +109,8 @@ describe("Set External plugin", () => { it("should return void if status is success", () => { // given const command = new SetExternalPluginCommand({ - payload: Uint8Array.from([]), - signature: Uint8Array.from([]), + payload: "", + signature: "", }); // when const apduResponse = new ApduResponse({ diff --git a/packages/signer/keyring-eth/src/internal/app-binder/command/SetExternalPluginCommand.ts b/packages/signer/keyring-eth/src/internal/app-binder/command/SetExternalPluginCommand.ts index 025beeaf4..a428fe550 100644 --- a/packages/signer/keyring-eth/src/internal/app-binder/command/SetExternalPluginCommand.ts +++ b/packages/signer/keyring-eth/src/internal/app-binder/command/SetExternalPluginCommand.ts @@ -1,9 +1,8 @@ // https://github.com/LedgerHQ/app-ethereum/blob/develop/doc/ethapp.adoc#set-external-plugin - import { Apdu, ApduBuilder, - ApduBuilderArgs, + type ApduBuilderArgs, ApduParser, ApduResponse, Command, @@ -18,8 +17,8 @@ import { } from "@ledgerhq/device-sdk-core"; type SetExternalPluginCommandArgs = { - payload: Uint8Array; - signature: Uint8Array; + payload: string; + signature?: string; }; type SetExternalPluginCommandErrorCodes = "6a80" | "6984" | "6d00"; @@ -51,18 +50,21 @@ export class SetExternalPluginCommand constructor(private readonly args: SetExternalPluginCommandArgs) {} getApdu(): Apdu { - const { payload, signature } = this.args; const setExternalPluginBuilderArgs: ApduBuilderArgs = { cla: 0xe0, ins: 0x12, p1: 0x00, p2: 0x00, }; - const builder = new ApduBuilder(setExternalPluginBuilderArgs); - builder.addBufferToData(payload); - builder.addBufferToData(signature); - - return builder.build(); + return ( + new ApduBuilder(setExternalPluginBuilderArgs) + .addHexaStringToData(this.args.payload) + /** + * The signature is normally integrated in the payload, but keeping this step for safety reasons and will be removed in the future. + */ + .addHexaStringToData(this.args.signature ?? "") + .build() + ); } parseResponse( diff --git a/packages/signer/keyring-eth/src/internal/app-binder/command/SetPluginCommand.test.ts b/packages/signer/keyring-eth/src/internal/app-binder/command/SetPluginCommand.test.ts index 1bd683dd1..3a60082af 100644 --- a/packages/signer/keyring-eth/src/internal/app-binder/command/SetPluginCommand.test.ts +++ b/packages/signer/keyring-eth/src/internal/app-binder/command/SetPluginCommand.test.ts @@ -30,7 +30,7 @@ describe("SetPluginCommand", () => { it("returns the correct APDU", () => { // GIVEN const args: SetPluginCommandArgs = { - data: SET_PLUGIN_COMMAND_PAYLOAD, + payload: SET_PLUGIN_COMMAND_PAYLOAD, }; // WHEN const command = new SetPluginCommand(args); @@ -52,7 +52,7 @@ describe("SetPluginCommand", () => { data: Uint8Array.from([]), statusCode: apduResponseCode, }); - const command = new SetPluginCommand({ data: "" }); + const command = new SetPluginCommand({ payload: "" }); // WHEN const result = command.parseResponse(response); // THEN @@ -71,7 +71,7 @@ describe("SetPluginCommand", () => { statusCode: Buffer.from([0x90, 0x00]), // Success status code }; // WHEN - const command = new SetPluginCommand({ data: "" }); + const command = new SetPluginCommand({ payload: "" }); const result = command.parseResponse(response); // THEN expect(isSuccessCommandResult(result)).toBe(true); diff --git a/packages/signer/keyring-eth/src/internal/app-binder/command/SetPluginCommand.ts b/packages/signer/keyring-eth/src/internal/app-binder/command/SetPluginCommand.ts index 68604940c..feb93a327 100644 --- a/packages/signer/keyring-eth/src/internal/app-binder/command/SetPluginCommand.ts +++ b/packages/signer/keyring-eth/src/internal/app-binder/command/SetPluginCommand.ts @@ -27,7 +27,7 @@ export type SetPluginCommandArgs = { /** * The stringified hexa representation of the plugin signature. */ - data: string; + payload: string; }; export class SetPluginCommandError extends DeviceExchangeError { @@ -39,7 +39,7 @@ export class SetPluginCommandError extends DeviceExchangeError { - constructor(private args: SetPluginCommandArgs) {} + constructor(private readonly args: SetPluginCommandArgs) {} getApdu(): Apdu { const apduBuilderArgs: ApduBuilderArgs = { @@ -48,9 +48,8 @@ export class SetPluginCommand p1: 0x00, p2: 0x00, }; - return new ApduBuilder(apduBuilderArgs) - .addHexaStringToData(this.args.data) + .addHexaStringToData(this.args.payload) .build(); }