Skip to content

Commit

Permalink
✨ (context-module): Implement generic transaction context loader
Browse files Browse the repository at this point in the history
  • Loading branch information
paoun-ledger committed Nov 20, 2024
1 parent 9037e4b commit 282f536
Show file tree
Hide file tree
Showing 15 changed files with 1,395 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eighty-houses-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/context-module": minor
---

Implement generic transaction context loader
5 changes: 5 additions & 0 deletions packages/signer/context-module/src/ContextModuleBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { externalPluginTypes } from "@/external-plugin/di/externalPluginTypes";
import { forwardDomainTypes } from "@/forward-domain/di/forwardDomainTypes";
import { nftTypes } from "@/nft/di/nftTypes";
import { tokenTypes } from "@/token/di/tokenTypes";
import { transactionTypes } from "@/transaction/di/transactionTypes";
import { typedDataTypes } from "@/typed-data/di/typedDataTypes";

import { type ContextModuleConfig } from "./config/model/ContextModuleConfig";
Expand All @@ -10,6 +11,7 @@ import { type ForwardDomainContextLoader } from "./forward-domain/domain/Forward
import { type NftContextLoader } from "./nft/domain/NftContextLoader";
import { type ContextLoader } from "./shared/domain/ContextLoader";
import { type TokenContextLoader } from "./token/domain/TokenContextLoader";
import { type TransactionContextLoader } from "./transaction/domain/TransactionContextLoader";
import { type TypedDataContextLoader } from "./typed-data/domain/TypedDataContextLoader";
import { type ContextModule } from "./ContextModule";
import { DefaultContextModule } from "./DefaultContextModule";
Expand Down Expand Up @@ -94,6 +96,9 @@ export class ContextModuleBuilder {
),
container.get<NftContextLoader>(nftTypes.NftContextLoader),
container.get<TokenContextLoader>(tokenTypes.TokenContextLoader),
container.get<TransactionContextLoader>(
transactionTypes.TransactionContextLoader,
),
];
const defaultTypedDataLoader = container.get<TypedDataContextLoader>(
typedDataTypes.TypedDataContextLoader,
Expand Down
2 changes: 2 additions & 0 deletions packages/signer/context-module/src/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { externalPluginModuleFactory } from "@/external-plugin/di/externalPlugin
import { forwardDomainModuleFactory } from "@/forward-domain/di/forwardDomainModuleFactory";
import { nftModuleFactory } from "@/nft/di/nftModuleFactory";
import { tokenModuleFactory } from "@/token/di/tokenModuleFactory";
import { transactionModuleFactory } from "@/transaction/di/transactionModuleFactory";
import { typedDataModuleFactory } from "@/typed-data/di/typedDataModuleFactory";

type MakeContainerArgs = {
Expand All @@ -21,6 +22,7 @@ export const makeContainer = ({ config }: MakeContainerArgs) => {
forwardDomainModuleFactory(),
nftModuleFactory(),
tokenModuleFactory(),
transactionModuleFactory(),
typedDataModuleFactory(),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { type GenericPath } from "./GenericPath";

export enum ClearSignContextType {
TOKEN = "token",
NFT = "nft",
DOMAIN_NAME = "domainName",
TRUSTED_NAME = "trustedName",
PLUGIN = "plugin",
EXTERNAL_PLUGIN = "externalPlugin",
TRANSACTION_INFO = "transactionInfo",
Expand All @@ -10,12 +13,30 @@ export enum ClearSignContextType {
ERROR = "error",
}

export type ClearSignContextReference =
| {
type: ClearSignContextType.TOKEN | ClearSignContextType.NFT;
valuePath: GenericPath;
}
| {
type: ClearSignContextType.TRUSTED_NAME;
valuePath: GenericPath;
types: string[];
sources: string[];
};

export type ClearSignContextSuccess = {
type: Exclude<ClearSignContextType, ClearSignContextType.ERROR>;
/**
* Hexadecimal string representation of the payload.
*/
payload: string;
/**
* Optional reference to another asset descriptor.
* ie: a 'transactionFieldDescription' descriptor can reference a token or
* a trusted name.
*/
reference?: ClearSignContextReference;
};

export type ClearSignContextError = {
Expand Down
167 changes: 167 additions & 0 deletions packages/signer/context-module/src/transaction/data/CalldataDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
export interface CalldataDto {
descriptors_calldata: {
[address: string]: {
[selector: string]: CalldataDescriptor;
};
};
}

export type CalldataDescriptor = CalldataDescriptorV1; // For now only V1 descriptors are supported

export interface CalldataDescriptorV1 {
type: "calldata";
version: "v1";
transaction_info: CalldataTransactionInfoV1;
enums: CalldataEnumV1[];
fields: CalldataFieldV1[];
}

export type CalldataTransactionDescriptor = {
data: string;
signatures: CalldataSignatures;
};

export type CalldataSignatures =
| {
prod: string;
test?: string;
}
| {
prod?: string;
test: string;
};

export interface CalldataTransactionInfoV1 {
descriptor: CalldataTransactionDescriptor;
}

export interface CalldataEnumV1 {
descriptor: string;
}

export interface CalldataFieldV1 {
descriptor: string;
param: CalldataDescriptorParam;
}

export type CalldataDescriptorParam =
| CalldataDescriptorParamRawV1
| CalldataDescriptorParamAmountV1
| CalldataDescriptorParamTokenAmountV1
| CalldataDescriptorParamNFTV1
| CalldataDescriptorParamDatetimeV1
| CalldataDescriptorParamDurationV1
| CalldataDescriptorParamUnitV1
| CalldataDescriptorParamEnumV1
| CalldataDescriptorParamTrustedNameV1;

export interface CalldataDescriptorParamRawV1 {
type: "RAW";
value: CalldataDescriptorValueV1;
}

export interface CalldataDescriptorParamAmountV1 {
type: "AMOUNT";
value: CalldataDescriptorValueV1;
}

export interface CalldataDescriptorParamTokenAmountV1 {
type: "TOKEN_AMOUNT";
value: CalldataDescriptorValueV1;
token?: CalldataDescriptorValueV1;
}

export interface CalldataDescriptorParamNFTV1 {
type: "NFT";
value: CalldataDescriptorValueV1;
collection: CalldataDescriptorValueV1;
}

export interface CalldataDescriptorParamDatetimeV1 {
type: "DATETIME";
value: CalldataDescriptorValueV1;
}

export interface CalldataDescriptorParamDurationV1 {
type: "DURATION";
value: CalldataDescriptorValueV1;
}

export interface CalldataDescriptorParamUnitV1 {
type: "UNIT";
value: CalldataDescriptorValueV1;
}

export interface CalldataDescriptorParamEnumV1 {
type: "ENUM";
value: CalldataDescriptorValueV1;
}

export interface CalldataDescriptorParamTrustedNameV1 {
type: "TRUSTED_NAME";
value: CalldataDescriptorValueV1;
types: string[];
sources: string[];
}

export interface CalldataDescriptorValueV1 {
binary_path:
| CalldataDescriptorContainerPathV1
| CalldataDescriptorPathElementsV1;
type_family: CalldataDescriptorTypeFamilyV1;
type_size?: number;
}

export interface CalldataDescriptorPathElementsV1 {
elements: CalldataDescriptorPathElementV1[];
}

export type CalldataDescriptorPathElementV1 =
| CalldataDescriptorPathElementTupleV1
| CalldataDescriptorPathElementArrayV1
| CalldataDescriptorPathElementRefV1
| CalldataDescriptorPathElementLeafV1
| CalldataDescriptorPathElementSliceV1;

export interface CalldataDescriptorPathElementTupleV1 {
type: "TUPLE";
offset: number;
}

export interface CalldataDescriptorPathElementArrayV1 {
type: "ARRAY";
start?: number;
length?: number;
weight: number;
}

export interface CalldataDescriptorPathElementRefV1 {
type: "REF";
}

export interface CalldataDescriptorPathElementLeafV1 {
type: "LEAF";
leaf_type: CalldataDescriptorPathLeafTypeV1;
}

export interface CalldataDescriptorPathElementSliceV1 {
type: "SLICE";
start?: number;
end?: number;
}

export type CalldataDescriptorContainerPathV1 = "FROM" | "TO" | "VALUE";
export type CalldataDescriptorPathLeafTypeV1 =
| "ARRAY_LEAF"
| "TUPLE_LEAF"
| "STATIC_LEAF"
| "DYNAMIC_LEAF";
export type CalldataDescriptorTypeFamilyV1 =
| "UINT"
| "INT"
| "UFIXED"
| "FIXED"
| "ADDRESS"
| "BOOL"
| "BYTES"
| "STRING";
Loading

0 comments on commit 282f536

Please sign in to comment.