|
1 | 1 | /** |
2 | | - * TypeScript wrapper for BuilderNamespace |
| 2 | + * Transaction building from high-level intents. |
| 3 | + * |
| 4 | + * Provides the `buildTransaction()` function for building DOT transactions. |
| 5 | + * Follows wallet-platform pattern: buildTransaction(intent, context) |
3 | 6 | */ |
4 | 7 |
|
5 | | -import { |
6 | | - BuilderNamespace, |
7 | | - BuildContextJs, |
8 | | - MaterialBuilderJs, |
9 | | - ValidityBuilderJs, |
10 | | -} from './wasm/wasm_dot'; |
11 | | -import { DotTransaction } from './transaction'; |
12 | | -import type { BuildContext, TransactionIntent } from './types'; |
| 8 | +import { BuilderNamespace } from "./wasm/wasm_dot"; |
| 9 | +import { DotTransaction } from "./transaction"; |
| 10 | +import type { TransactionIntent, BuildContext, BatchIntent } from "./types"; |
13 | 11 |
|
14 | 12 | /** |
15 | | - * DOT Transaction Builder |
| 13 | + * Build a DOT transaction from an intent and context. |
| 14 | + * |
| 15 | + * This function takes a declarative TransactionIntent and BuildContext, |
| 16 | + * producing a Transaction object that can be inspected, signed, and serialized. |
| 17 | + * |
| 18 | + * The returned transaction is unsigned - signatures should be added via |
| 19 | + * `addSignature()` before serializing with `toBytes()` and broadcasting. |
| 20 | + * |
| 21 | + * @param intent - What to do (transfer, stake, etc.) |
| 22 | + * @param context - How to build it (sender, nonce, material, validity, referenceBlock) |
| 23 | + * @returns A Transaction object that can be inspected, signed, and serialized |
| 24 | + * @throws Error if the intent cannot be built (e.g., invalid addresses) |
| 25 | + * |
| 26 | + * @example |
| 27 | + * ```typescript |
| 28 | + * import { buildTransaction } from '@bitgo/wasm-dot'; |
| 29 | + * |
| 30 | + * // Build a simple DOT transfer |
| 31 | + * const tx = buildTransaction( |
| 32 | + * { type: 'transfer', to: '5FHneW46...', amount: '1000000000000', keepAlive: true }, |
| 33 | + * { |
| 34 | + * sender: '5EGoFA95omzemRssELLDjVenNZ68aXyUeqtKQScXSEBvVJkr', |
| 35 | + * nonce: 5, |
| 36 | + * material: { |
| 37 | + * genesisHash: '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3', |
| 38 | + * chainName: 'Polkadot', |
| 39 | + * specName: 'polkadot', |
| 40 | + * specVersion: 9150, |
| 41 | + * txVersion: 9 |
| 42 | + * }, |
| 43 | + * validity: { firstValid: 1000, maxDuration: 2400 }, |
| 44 | + * referenceBlock: '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3' |
| 45 | + * } |
| 46 | + * ); |
| 47 | + * |
| 48 | + * // Inspect the transaction |
| 49 | + * console.log(tx.nonce); |
| 50 | + * |
| 51 | + * // Get the signable payload for signing |
| 52 | + * const payload = tx.signablePayload(); |
16 | 53 | * |
17 | | - * Provides methods for building DOT transactions from intents |
| 54 | + * // Add signature and serialize |
| 55 | + * tx.addSignature(signerPubkey, signature); |
| 56 | + * const txBytes = tx.toBytes(); |
| 57 | + * ``` |
| 58 | + * |
| 59 | + * @example |
| 60 | + * ```typescript |
| 61 | + * // Build with batch (multiple operations) |
| 62 | + * const tx = buildTransaction( |
| 63 | + * { |
| 64 | + * type: 'batch', |
| 65 | + * calls: [ |
| 66 | + * { type: 'transfer', to: recipient, amount: '1000000000000' }, |
| 67 | + * { type: 'stake', amount: '5000000000000', payee: { type: 'staked' } } |
| 68 | + * ], |
| 69 | + * atomic: true |
| 70 | + * }, |
| 71 | + * context |
| 72 | + * ); |
| 73 | + * ``` |
18 | 74 | */ |
19 | | -export class DotBuilder { |
20 | | - /** |
21 | | - * Build a transaction from an intent |
22 | | - * |
23 | | - * @param intent - Transaction intent describing what to do |
24 | | - * @param context - Build context with sender, nonce, material, validity |
25 | | - * @returns Transaction ready for signing |
26 | | - * |
27 | | - * @example |
28 | | - * ```typescript |
29 | | - * const tx = DotBuilder.buildTransaction({ |
30 | | - * type: 'transfer', |
31 | | - * to: '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty', |
32 | | - * amount: '1000000000000', |
33 | | - * keepAlive: true, |
34 | | - * }, context); |
35 | | - * ``` |
36 | | - */ |
37 | | - static buildTransaction(intent: TransactionIntent, context: BuildContext): DotTransaction { |
38 | | - const ctx = createBuildContext(context); |
39 | | - const inner = BuilderNamespace.buildTransaction(intent, ctx); |
40 | | - return DotTransaction.fromInner(inner as any); |
41 | | - } |
| 75 | +export function buildTransaction(intent: TransactionIntent, context: BuildContext): DotTransaction { |
| 76 | + // Convert BigInt amounts to strings for JSON serialization |
| 77 | + const serializedIntent = serializeIntent(intent); |
| 78 | + const serializedContext = serializeContext(context); |
42 | 79 |
|
43 | | - /** |
44 | | - * Build a transfer transaction |
45 | | - * |
46 | | - * Convenience method for simple transfers |
47 | | - * |
48 | | - * @param to - Recipient address (SS58) |
49 | | - * @param amount - Amount in planck (as string for BigInt) |
50 | | - * @param keepAlive - Use transferKeepAlive (default: true) |
51 | | - * @param context - Build context |
52 | | - * @returns Transaction ready for signing |
53 | | - */ |
54 | | - static buildTransfer( |
55 | | - to: string, |
56 | | - amount: string, |
57 | | - keepAlive: boolean, |
58 | | - context: BuildContext |
59 | | - ): DotTransaction { |
60 | | - const ctx = createBuildContext(context); |
61 | | - const inner = BuilderNamespace.buildTransfer(to, amount, keepAlive, ctx); |
62 | | - return DotTransaction.fromInner(inner as any); |
63 | | - } |
| 80 | + const inner = BuilderNamespace.buildTransaction(serializedIntent, serializedContext); |
| 81 | + return DotTransaction.fromInner(inner as any); |
| 82 | +} |
64 | 83 |
|
65 | | - /** |
66 | | - * Build a staking (bond) transaction |
67 | | - * |
68 | | - * @param amount - Amount to stake in planck (as string for BigInt) |
69 | | - * @param payee - Where to send staking rewards ("staked", "stash", "controller", or address) |
70 | | - * @param context - Build context |
71 | | - * @returns Transaction ready for signing |
72 | | - */ |
73 | | - static buildStake(amount: string, payee: string, context: BuildContext): DotTransaction { |
74 | | - const ctx = createBuildContext(context); |
75 | | - const inner = BuilderNamespace.buildStake(amount, payee, ctx); |
76 | | - return DotTransaction.fromInner(inner as any); |
| 84 | +/** |
| 85 | + * Serialize intent for WASM, converting BigInt to string |
| 86 | + */ |
| 87 | +function serializeIntent(intent: TransactionIntent): TransactionIntent { |
| 88 | + if ("amount" in intent && typeof intent.amount === "bigint") { |
| 89 | + return { ...intent, amount: intent.amount.toString() }; |
77 | 90 | } |
78 | | - |
79 | | - /** |
80 | | - * Build an unstake (unbond) transaction |
81 | | - * |
82 | | - * @param amount - Amount to unstake in planck (as string for BigInt) |
83 | | - * @param context - Build context |
84 | | - * @returns Transaction ready for signing |
85 | | - */ |
86 | | - static buildUnstake(amount: string, context: BuildContext): DotTransaction { |
87 | | - const ctx = createBuildContext(context); |
88 | | - const inner = BuilderNamespace.buildUnstake(amount, ctx); |
89 | | - return DotTransaction.fromInner(inner as any); |
| 91 | + if (intent.type === "batch") { |
| 92 | + return { |
| 93 | + ...intent, |
| 94 | + calls: (intent as BatchIntent).calls.map(serializeIntent), |
| 95 | + }; |
90 | 96 | } |
| 97 | + return intent; |
91 | 98 | } |
92 | 99 |
|
93 | 100 | /** |
94 | | - * Create a BuildContextJs from BuildContext |
| 101 | + * Serialize context for WASM, converting BigInt to string |
95 | 102 | */ |
96 | | -function createBuildContext(ctx: BuildContext): BuildContextJs { |
97 | | - const material = new MaterialBuilderJs( |
98 | | - ctx.material.genesisHash, |
99 | | - ctx.material.chainName, |
100 | | - ctx.material.specName, |
101 | | - ctx.material.specVersion, |
102 | | - ctx.material.txVersion |
103 | | - ); |
104 | | - const validity = new ValidityBuilderJs(ctx.validity.firstValid, ctx.validity.maxDuration); |
105 | | - return new BuildContextJs( |
106 | | - ctx.sender, |
107 | | - ctx.nonce, |
108 | | - ctx.tip ?? '0', |
109 | | - material, |
110 | | - validity, |
111 | | - ctx.referenceBlock |
112 | | - ); |
| 103 | +function serializeContext(context: BuildContext): BuildContext { |
| 104 | + return { |
| 105 | + ...context, |
| 106 | + tip: context.tip?.toString() ?? "0", |
| 107 | + }; |
113 | 108 | } |
| 109 | + |
| 110 | +// Re-export types for convenience |
| 111 | +export type { TransactionIntent, BuildContext } from "./types"; |
0 commit comments