Skip to content

Commit

Permalink
adds icp (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wozacosta authored Apr 24, 2024
1 parent 091cf53 commit a2af615
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/violet-dingos-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/wallet-api-core": minor
---

feat: add internet_computer family
1 change: 1 addition & 0 deletions packages/core/src/families/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const FAMILIES = [
"solana",
"vechain",
"stacks",
"internet_computer",
] as const;

export const schemaFamilies = z.enum(FAMILIES);
1 change: 1 addition & 0 deletions packages/core/src/families/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
33 changes: 33 additions & 0 deletions packages/core/src/families/internet_computer/serializer.ts
Original file line number Diff line number Diff line change
@@ -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,
});
14 changes: 14 additions & 0 deletions packages/core/src/families/internet_computer/types.ts
Original file line number Diff line number Diff line change
@@ -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
>;
9 changes: 9 additions & 0 deletions packages/core/src/families/internet_computer/validation.ts
Original file line number Diff line number Diff line change
@@ -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(),
});
8 changes: 8 additions & 0 deletions packages/core/src/families/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ import {
deserializeStacksTransaction,
serializeStacksTransaction,
} from "./stacks/serializer";
import {
deserializeInternetComputerTransaction,
serializeInternetComputerTransaction,
} from "./internet_computer/serializer";
import type { RawTransaction, Transaction } from "./types";

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/families/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -83,4 +84,5 @@ export type Transaction =
| CardanoTransaction
| SolanaTransaction
| VechainTransaction
| StacksTransaction;
| StacksTransaction
| InternetComputerTransaction;
2 changes: 2 additions & 0 deletions packages/core/src/families/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -41,4 +42,5 @@ export const schemaRawTransaction = z.discriminatedUnion("family", [
schemaRawSolanaTransaction,
schemaRawVechainTransaction,
schemaRawStacksTransaction,
schemaRawInternetComputerTransaction,
]);

0 comments on commit a2af615

Please sign in to comment.