From d7910f0c0ca983862610ee6982cd7a98f260d6a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Prohaszka?= <104785785+sprohaszka-ledger@users.noreply.github.com> Date: Thu, 26 Sep 2024 14:11:41 +0200 Subject: [PATCH] feat: add TON support (#389) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Prohaszka --- .changeset/kind-buttons-thank.md | 5 ++++ README.md | 1 + packages/core/src/families/common.ts | 29 ++++++++++--------- packages/core/src/families/index.ts | 15 +++++----- packages/core/src/families/serializer.ts | 8 ++++++ packages/core/src/families/ton/serializer.ts | 30 ++++++++++++++++++++ packages/core/src/families/ton/types.ts | 14 +++++++++ packages/core/src/families/ton/validation.ts | 13 +++++++++ packages/core/src/families/types.ts | 2 ++ packages/core/src/families/validation.ts | 2 ++ 10 files changed, 98 insertions(+), 21 deletions(-) create mode 100644 .changeset/kind-buttons-thank.md create mode 100644 packages/core/src/families/ton/serializer.ts create mode 100644 packages/core/src/families/ton/types.ts create mode 100644 packages/core/src/families/ton/validation.ts diff --git a/.changeset/kind-buttons-thank.md b/.changeset/kind-buttons-thank.md new file mode 100644 index 00000000..3e4e98d2 --- /dev/null +++ b/.changeset/kind-buttons-thank.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/wallet-api-core": minor +--- + +Add TON support diff --git a/README.md b/README.md index b866c15b..f5055738 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ Each wallet and application then needs to implement this interface. In this repo | crypto_org_croeseid (crypto_org) | ✅ | ✅ | | celo | ✅ | ✅ | | dash (bitcoin) | ✅ | ✅ | +| ton | ✅ | ✅ | | tron | ✅ | ✅ | | tezos | ✅ | ✅ | | elrond | ✅ | ✅ | diff --git a/packages/core/src/families/common.ts b/packages/core/src/families/common.ts index f562f813..81a8359f 100644 --- a/packages/core/src/families/common.ts +++ b/packages/core/src/families/common.ts @@ -6,28 +6,29 @@ export const schemaTransactionCommon = z.object({ }); export const FAMILIES = [ - "bitcoin", - "ethereum", "algorand", + "bitcoin", + "cardano", + "casper", + "celo", "crypto_org", - "ripple", "cosmos", - "celo", - "hedera", + "elrond", + "ethereum", "filecoin", - "tezos", - "polkadot", - "stellar", - "tron", + "hedera", + "internet_computer", "near", "neo", - "elrond", - "cardano", + "polkadot", + "ripple", "solana", - "vechain", "stacks", - "internet_computer", - "casper", + "stellar", + "tezos", + "ton", + "tron", + "vechain", ] 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 3ac84e48..efc9f41f 100644 --- a/packages/core/src/families/index.ts +++ b/packages/core/src/families/index.ts @@ -1,25 +1,26 @@ export * from "./algorand/types"; export * from "./bitcoin/types"; +export * from "./cardano/types"; +export * from "./casper/types"; export * from "./celo/types"; export * from "./cosmos/types"; export * from "./crypto_org/types"; +export * from "./elrond/types"; export * from "./ethereum/types"; -export * from "./hedera/types"; export * from "./filecoin/types"; +export * from "./hedera/types"; +export * from "./internet_computer/types"; export * from "./near/types"; export * from "./neo/types"; export * from "./polkadot/types"; export * from "./ripple/types"; +export * from "./solana/types"; +export * from "./stacks/types"; export * from "./stellar/types"; export * from "./tezos/types"; +export * from "./ton/types"; export * from "./tron/types"; -export * from "./elrond/types"; -export * from "./cardano/types"; -export * from "./solana/types"; export * from "./vechain/types"; -export * from "./stacks/types"; -export * from "./internet_computer/types"; -export * from "./casper/types"; export * from "./common"; export * from "./serializer"; diff --git a/packages/core/src/families/serializer.ts b/packages/core/src/families/serializer.ts index 8e5449d4..37488265 100644 --- a/packages/core/src/families/serializer.ts +++ b/packages/core/src/families/serializer.ts @@ -48,6 +48,10 @@ import { deserializeTezosTransaction, serializeTezosTransaction, } from "./tezos/serializer"; +import { + deserializeTonTransaction, + serializeTonTransaction, +} from "./ton/serializer"; import { deserializeTronTransaction, serializeTronTransaction, @@ -115,6 +119,8 @@ export function serializeTransaction(transaction: Transaction): RawTransaction { return serializePolkadotTransaction(transaction); case "stellar": return serializeStellarTransaction(transaction); + case "ton": + return serializeTonTransaction(transaction); case "tron": return serializeTronTransaction(transaction); case "near": @@ -178,6 +184,8 @@ export function deserializeTransaction( return deserializePolkadotTransaction(rawTransaction); case "stellar": return deserializeStellarTransaction(rawTransaction); + case "ton": + return deserializeTonTransaction(rawTransaction); case "tron": return deserializeTronTransaction(rawTransaction); case "near": diff --git a/packages/core/src/families/ton/serializer.ts b/packages/core/src/families/ton/serializer.ts new file mode 100644 index 00000000..43e88230 --- /dev/null +++ b/packages/core/src/families/ton/serializer.ts @@ -0,0 +1,30 @@ +import BigNumber from "bignumber.js"; +import type { RawTonTransaction, TonTransaction } from "./types"; + +export const serializeTonTransaction = ({ + amount, + recipient, + family, + fees, + comment, +}: TonTransaction): RawTonTransaction => ({ + amount: amount.toString(), + recipient, + family, + fees: fees.toString(), + comment, +}); + +export const deserializeTonTransaction = ({ + amount, + recipient, + family, + fees, + comment, +}: RawTonTransaction): TonTransaction => ({ + amount: new BigNumber(amount), + recipient, + family, + fees: new BigNumber(fees), + comment, +}); diff --git a/packages/core/src/families/ton/types.ts b/packages/core/src/families/ton/types.ts new file mode 100644 index 00000000..3b1e45b8 --- /dev/null +++ b/packages/core/src/families/ton/types.ts @@ -0,0 +1,14 @@ +import BigNumber from "bignumber.js"; +import type { z } from "zod"; +import type { TransactionCommon } from "../index"; +import type { schemaTonComment, schemaRawTonTransaction } from "./validation"; + +export type TonComment = z.infer; + +export type TonTransaction = TransactionCommon & { + readonly family: RawTonTransaction["family"]; + fees: BigNumber; + comment: TonComment; +}; + +export type RawTonTransaction = z.infer; diff --git a/packages/core/src/families/ton/validation.ts b/packages/core/src/families/ton/validation.ts new file mode 100644 index 00000000..1489ed78 --- /dev/null +++ b/packages/core/src/families/ton/validation.ts @@ -0,0 +1,13 @@ +import { z } from "zod"; +import { schemaFamilies, schemaTransactionCommon } from "../common"; + +export const schemaTonComment = z.object({ + isEncrypted: z.boolean(), + text: z.string(), +}); + +export const schemaRawTonTransaction = schemaTransactionCommon.extend({ + family: z.literal(schemaFamilies.enum.ton), + fees: z.string(), + comment: schemaTonComment, +}); diff --git a/packages/core/src/families/types.ts b/packages/core/src/families/types.ts index 167da4e6..402787c9 100644 --- a/packages/core/src/families/types.ts +++ b/packages/core/src/families/types.ts @@ -18,6 +18,7 @@ import type { RippleTransaction } from "./ripple/types"; import type { SolanaTransaction } from "./solana/types"; import type { StellarTransaction } from "./stellar/types"; import type { TezosTransaction } from "./tezos/types"; +import type { TonTransaction } from "./ton/types"; import type { TronTransaction } from "./tron/types"; import type { VechainTransaction } from "./vechain/types"; import type { schemaRawTransaction } from "./validation"; @@ -78,6 +79,7 @@ export type Transaction = | TezosTransaction | PolkadotTransaction | StellarTransaction + | TonTransaction | TronTransaction | NearTransaction | NeoTransaction diff --git a/packages/core/src/families/validation.ts b/packages/core/src/families/validation.ts index c81d70d6..14a777f3 100644 --- a/packages/core/src/families/validation.ts +++ b/packages/core/src/families/validation.ts @@ -12,6 +12,7 @@ import { schemaRawRippleTransaction } from "./ripple/validation"; import { schemaRawSolanaTransaction } from "./solana/validation"; import { schemaRawStellarTransaction } from "./stellar/validation"; import { schemaRawTezosTransaction } from "./tezos/validation"; +import { schemaRawTonTransaction } from "./ton/validation"; import { schemaRawTronTransaction } from "./tron/validation"; import { schemaRawHederaTransaction } from "./hedera/validation"; import { schemaRawFilecoinTransaction } from "./filecoin/validation"; @@ -37,6 +38,7 @@ export const schemaRawTransaction = z.discriminatedUnion("family", [ schemaRawRippleTransaction, schemaRawStellarTransaction, schemaRawTezosTransaction, + schemaRawTonTransaction, schemaRawTronTransaction, schemaRawElrondTransaction, schemaRawCardanoTransaction,