From a2af615eb368cd17511a37f296a5c8e1910a144e Mon Sep 17 00:00:00 2001 From: Samy Rabah Date: Wed, 24 Apr 2024 14:44:11 +0200 Subject: [PATCH] adds icp (#343) --- .changeset/violet-dingos-smash.md | 5 +++ packages/core/src/families/common.ts | 1 + packages/core/src/families/index.ts | 1 + .../families/internet_computer/serializer.ts | 33 +++++++++++++++++++ .../src/families/internet_computer/types.ts | 14 ++++++++ .../families/internet_computer/validation.ts | 9 +++++ packages/core/src/families/serializer.ts | 8 +++++ packages/core/src/families/types.ts | 4 ++- packages/core/src/families/validation.ts | 2 ++ 9 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 .changeset/violet-dingos-smash.md create mode 100644 packages/core/src/families/internet_computer/serializer.ts create mode 100644 packages/core/src/families/internet_computer/types.ts create mode 100644 packages/core/src/families/internet_computer/validation.ts diff --git a/.changeset/violet-dingos-smash.md b/.changeset/violet-dingos-smash.md new file mode 100644 index 00000000..ed4d98b9 --- /dev/null +++ b/.changeset/violet-dingos-smash.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/wallet-api-core": minor +--- + +feat: add internet_computer family diff --git a/packages/core/src/families/common.ts b/packages/core/src/families/common.ts index d8e09f50..1dafde60 100644 --- a/packages/core/src/families/common.ts +++ b/packages/core/src/families/common.ts @@ -26,6 +26,7 @@ export const FAMILIES = [ "solana", "vechain", "stacks", + "internet_computer", ] as const; export const schemaFamilies = z.enum(FAMILIES); diff --git a/packages/core/src/families/index.ts b/packages/core/src/families/index.ts index e6d1735e..c0d7ea2d 100644 --- a/packages/core/src/families/index.ts +++ b/packages/core/src/families/index.ts @@ -18,6 +18,7 @@ export * from "./cardano/types"; export * from "./solana/types"; export * from "./vechain/types"; export * from "./stacks/types"; +export * from "./internet_computer/types"; export * from "./common"; export * from "./serializer"; diff --git a/packages/core/src/families/internet_computer/serializer.ts b/packages/core/src/families/internet_computer/serializer.ts new file mode 100644 index 00000000..39cc8059 --- /dev/null +++ b/packages/core/src/families/internet_computer/serializer.ts @@ -0,0 +1,33 @@ +import BigNumber from "bignumber.js"; +import type { + RawInternetComputerTransaction, + InternetComputerTransaction, +} from "./types"; + +export const serializeInternetComputerTransaction = ({ + amount, + recipient, + family, + fees, + memo, +}: InternetComputerTransaction): RawInternetComputerTransaction => ({ + amount: amount.toFixed(), + recipient, + family, + fees: fees.toString(), + memo, +}); + +export const deserializeInternetComputerTransaction = ({ + amount, + recipient, + family, + fees, + memo, +}: RawInternetComputerTransaction): InternetComputerTransaction => ({ + amount: new BigNumber(amount), + recipient, + family, + fees: new BigNumber(fees), + memo, +}); diff --git a/packages/core/src/families/internet_computer/types.ts b/packages/core/src/families/internet_computer/types.ts new file mode 100644 index 00000000..a6186f4f --- /dev/null +++ b/packages/core/src/families/internet_computer/types.ts @@ -0,0 +1,14 @@ +import type { z } from "zod"; +import type { TransactionCommon } from "../index"; +import type { schemaRawInternetComputerTransaction } from "./validation"; +import type BigNumber from "bignumber.js"; + +export type InternetComputerTransaction = TransactionCommon & { + readonly family: RawInternetComputerTransaction["family"]; + fees: BigNumber; + memo?: string; +}; + +export type RawInternetComputerTransaction = z.infer< + typeof schemaRawInternetComputerTransaction +>; diff --git a/packages/core/src/families/internet_computer/validation.ts b/packages/core/src/families/internet_computer/validation.ts new file mode 100644 index 00000000..acc1e2c9 --- /dev/null +++ b/packages/core/src/families/internet_computer/validation.ts @@ -0,0 +1,9 @@ +import { z } from "zod"; +import { schemaFamilies, schemaTransactionCommon } from "../common"; + +export const schemaRawInternetComputerTransaction = + schemaTransactionCommon.extend({ + family: z.literal(schemaFamilies.enum.internet_computer), + fees: z.string(), + memo: z.string().optional(), + }); diff --git a/packages/core/src/families/serializer.ts b/packages/core/src/families/serializer.ts index 1dce7e69..777579a3 100644 --- a/packages/core/src/families/serializer.ts +++ b/packages/core/src/families/serializer.ts @@ -72,6 +72,10 @@ import { deserializeStacksTransaction, serializeStacksTransaction, } from "./stacks/serializer"; +import { + deserializeInternetComputerTransaction, + serializeInternetComputerTransaction, +} from "./internet_computer/serializer"; import type { RawTransaction, Transaction } from "./types"; /** @@ -123,6 +127,8 @@ export function serializeTransaction(transaction: Transaction): RawTransaction { return serializeVechainTransaction(transaction); case "stacks": return serializeStacksTransaction(transaction); + case "internet_computer": + return serializeInternetComputerTransaction(transaction); default: { const exhaustiveCheck: never = transaction; // https://www.typescriptlang.org/docs/handbook/2/narrowing.html#exhaustiveness-checking return exhaustiveCheck; @@ -182,6 +188,8 @@ export function deserializeTransaction( return deserializeVechainTransaction(rawTransaction); case "stacks": return deserializeStacksTransaction(rawTransaction); + case "internet_computer": + return deserializeInternetComputerTransaction(rawTransaction); default: { const exhaustiveCheck: never = rawTransaction; // https://www.typescriptlang.org/docs/handbook/2/narrowing.html#exhaustiveness-checking return exhaustiveCheck; diff --git a/packages/core/src/families/types.ts b/packages/core/src/families/types.ts index a5ae9458..2ac5d063 100644 --- a/packages/core/src/families/types.ts +++ b/packages/core/src/families/types.ts @@ -22,6 +22,7 @@ import type { TronTransaction } from "./tron/types"; import type { VechainTransaction } from "./vechain/types"; import type { schemaRawTransaction } from "./validation"; import type { StacksTransaction } from "./stacks/types"; +import type { InternetComputerTransaction } from "./internet_computer/types"; /** * Supported coin families @@ -83,4 +84,5 @@ export type Transaction = | CardanoTransaction | SolanaTransaction | VechainTransaction - | StacksTransaction; + | StacksTransaction + | InternetComputerTransaction; diff --git a/packages/core/src/families/validation.ts b/packages/core/src/families/validation.ts index 1ba5ee6f..5de98e3b 100644 --- a/packages/core/src/families/validation.ts +++ b/packages/core/src/families/validation.ts @@ -19,6 +19,7 @@ import { schemaRawElrondTransaction } from "./elrond/validation"; import { schemaRawCardanoTransaction } from "./cardano/validation"; import { schemaRawVechainTransaction } from "./vechain/validation"; import { schemaRawStacksTransaction } from "./stacks/validation"; +import { schemaRawInternetComputerTransaction } from "./internet_computer/validation"; export const schemaRawTransaction = z.discriminatedUnion("family", [ schemaRawAlgorandTransaction, @@ -41,4 +42,5 @@ export const schemaRawTransaction = z.discriminatedUnion("family", [ schemaRawSolanaTransaction, schemaRawVechainTransaction, schemaRawStacksTransaction, + schemaRawInternetComputerTransaction, ]);