Skip to content

Commit

Permalink
feat: add stacks family (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wozacosta authored Apr 4, 2024
1 parent f3b495b commit 0719773
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/big-waves-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/wallet-api-core": minor
---

feat: add stacks family
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"devDependencies": {
"@ledgerhq/jest-shared-config": "workspace:*",
"@stacks/transactions": "^6.13.0",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.19",
"@types/uuid": "^9.0.8",
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/families/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const FAMILIES = [
"cardano",
"solana",
"vechain",
"stacks",
] 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 @@ -17,6 +17,7 @@ export * from "./elrond/types";
export * from "./cardano/types";
export * from "./solana/types";
export * from "./vechain/types";
export * from "./stacks/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 @@ -68,6 +68,10 @@ import {
deserializeVechainTransaction,
serializeVechainTransaction,
} from "./vechain/serializer";
import {
deserializeStacksTransaction,
serializeStacksTransaction,
} from "./stacks/serializer";
import type { RawTransaction, Transaction } from "./types";

/**
Expand Down Expand Up @@ -117,6 +121,8 @@ export function serializeTransaction(transaction: Transaction): RawTransaction {
return serializeSolanaTransaction(transaction);
case "vechain":
return serializeVechainTransaction(transaction);
case "stacks":
return serializeStacksTransaction(transaction);
default: {
const exhaustiveCheck: never = transaction; // https://www.typescriptlang.org/docs/handbook/2/narrowing.html#exhaustiveness-checking
return exhaustiveCheck;
Expand Down Expand Up @@ -174,6 +180,8 @@ export function deserializeTransaction(
return deserializeSolanaTransaction(rawTransaction);
case "vechain":
return deserializeVechainTransaction(rawTransaction);
case "stacks":
return deserializeStacksTransaction(rawTransaction);
default: {
const exhaustiveCheck: never = rawTransaction; // https://www.typescriptlang.org/docs/handbook/2/narrowing.html#exhaustiveness-checking
return exhaustiveCheck;
Expand Down
42 changes: 42 additions & 0 deletions packages/core/src/families/stacks/serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import BigNumber from "bignumber.js";
import type { RawStacksTransaction, StacksTransaction } from "./types";

export const serializeStacksTransaction = ({
amount,
recipient,
family,
fee,
nonce,
memo,
network,
anchorMode,
}: StacksTransaction): RawStacksTransaction => ({
amount: amount.toString(),
recipient,
family,
fee: fee?.toString(),
nonce: nonce?.toString(),
memo,
network,
anchorMode,
});

export const deserializeStacksTransaction = ({
amount,
recipient,
family,
fee,
nonce,
memo,
network,
anchorMode,
}: RawStacksTransaction): StacksTransaction => ({
amount: new BigNumber(amount),
recipient,
family,
fee: fee ? new BigNumber(fee) : undefined,
nonce: nonce ? new BigNumber(nonce) : undefined,
memo,
network: network === "mainnet" ? network : "testnet",
anchorMode,
});
18 changes: 18 additions & 0 deletions packages/core/src/families/stacks/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { z } from "zod";
import type { TransactionCommon } from "../index";
import type { schemaRawStacksTransaction } from "./validation";
import type BigNumber from "bignumber.js";
import type { AnchorMode } from "@stacks/transactions";

export type StacksNetworks = "mainnet" | "testnet";

export type StacksTransaction = TransactionCommon & {
readonly family: RawStacksTransaction["family"];
fee?: BigNumber;
nonce?: BigNumber;
memo?: string;
network: StacksNetworks;
anchorMode: AnchorMode;
};

export type RawStacksTransaction = z.infer<typeof schemaRawStacksTransaction>;
11 changes: 11 additions & 0 deletions packages/core/src/families/stacks/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { z } from "zod";
import { schemaFamilies, schemaTransactionCommon } from "../common";

export const schemaRawStacksTransaction = schemaTransactionCommon.extend({
family: z.literal(schemaFamilies.enum.stacks),
fee: z.string().optional(),
nonce: z.string().optional(),
memo: z.string().optional(),
network: z.string(),
anchorMode: z.number(),
});
4 changes: 3 additions & 1 deletion packages/core/src/families/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { TezosTransaction } from "./tezos/types";
import type { TronTransaction } from "./tron/types";
import type { VechainTransaction } from "./vechain/types";
import type { schemaRawTransaction } from "./validation";
import type { StacksTransaction } from "./stacks/types";

/**
* Supported coin families
Expand Down Expand Up @@ -81,4 +82,5 @@ export type Transaction =
| ElrondTransaction
| CardanoTransaction
| SolanaTransaction
| VechainTransaction;
| VechainTransaction
| StacksTransaction;
2 changes: 2 additions & 0 deletions packages/core/src/families/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { schemaRawFilecoinTransaction } from "./filecoin/validation";
import { schemaRawElrondTransaction } from "./elrond/validation";
import { schemaRawCardanoTransaction } from "./cardano/validation";
import { schemaRawVechainTransaction } from "./vechain/validation";
import { schemaRawStacksTransaction } from "./stacks/validation";

export const schemaRawTransaction = z.discriminatedUnion("family", [
schemaRawAlgorandTransaction,
Expand All @@ -39,4 +40,5 @@ export const schemaRawTransaction = z.discriminatedUnion("family", [
schemaRawCardanoTransaction,
schemaRawSolanaTransaction,
schemaRawVechainTransaction,
schemaRawStacksTransaction,
]);
76 changes: 76 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0719773

Please sign in to comment.