Skip to content

Commit

Permalink
feat: add TON support (#389)
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Prohaszka <[email protected]>
  • Loading branch information
sprohaszka-ledger authored Sep 26, 2024
1 parent 996c943 commit d7910f0
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-buttons-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/wallet-api-core": minor
---

Add TON support
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |||
Expand Down
29 changes: 15 additions & 14 deletions packages/core/src/families/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
15 changes: 8 additions & 7 deletions packages/core/src/families/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/families/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ import {
deserializeTezosTransaction,
serializeTezosTransaction,
} from "./tezos/serializer";
import {
deserializeTonTransaction,
serializeTonTransaction,
} from "./ton/serializer";
import {
deserializeTronTransaction,
serializeTronTransaction,
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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":
Expand Down
30 changes: 30 additions & 0 deletions packages/core/src/families/ton/serializer.ts
Original file line number Diff line number Diff line change
@@ -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,
});
14 changes: 14 additions & 0 deletions packages/core/src/families/ton/types.ts
Original file line number Diff line number Diff line change
@@ -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<typeof schemaTonComment>;

export type TonTransaction = TransactionCommon & {
readonly family: RawTonTransaction["family"];
fees: BigNumber;
comment: TonComment;
};

export type RawTonTransaction = z.infer<typeof schemaRawTonTransaction>;
13 changes: 13 additions & 0 deletions packages/core/src/families/ton/validation.ts
Original file line number Diff line number Diff line change
@@ -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,
});
2 changes: 2 additions & 0 deletions packages/core/src/families/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -78,6 +79,7 @@ export type Transaction =
| TezosTransaction
| PolkadotTransaction
| StellarTransaction
| TonTransaction
| TronTransaction
| NearTransaction
| NeoTransaction
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/families/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -37,6 +38,7 @@ export const schemaRawTransaction = z.discriminatedUnion("family", [
schemaRawRippleTransaction,
schemaRawStellarTransaction,
schemaRawTezosTransaction,
schemaRawTonTransaction,
schemaRawTronTransaction,
schemaRawElrondTransaction,
schemaRawCardanoTransaction,
Expand Down

0 comments on commit d7910f0

Please sign in to comment.