@blazium/ton-connect-mobile 1.1.4 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Transaction Builder Utilities
3
+ * Helper functions for building TON Connect transaction requests
4
+ */
5
+
6
+ import type { SendTransactionRequest, TransactionMessage } from '../types';
7
+
8
+ /**
9
+ * Convert TON amount to nanotons
10
+ * @param tonAmount - Amount in TON (e.g., 1.5 for 1.5 TON)
11
+ * @returns Amount in nanotons as string
12
+ */
13
+ export function tonToNano(tonAmount: number | string): string {
14
+ const amount = typeof tonAmount === 'string' ? parseFloat(tonAmount) : tonAmount;
15
+ if (isNaN(amount) || amount < 0) {
16
+ throw new Error('Invalid TON amount');
17
+ }
18
+ const nanotons = Math.floor(amount * 1_000_000_000);
19
+ return nanotons.toString();
20
+ }
21
+
22
+ /**
23
+ * Convert nanotons to TON
24
+ * @param nanotons - Amount in nanotons as string
25
+ * @returns Amount in TON as number
26
+ */
27
+ export function nanoToTon(nanotons: string): number {
28
+ const nano = BigInt(nanotons);
29
+ return Number(nano) / 1_000_000_000;
30
+ }
31
+
32
+ /**
33
+ * Build a simple TON transfer transaction
34
+ * @param to - Recipient address (EQ... format)
35
+ * @param amount - Amount in TON (will be converted to nanotons)
36
+ * @param validUntil - Optional expiration timestamp (default: 5 minutes from now)
37
+ * @returns Transaction request
38
+ */
39
+ export function buildTransferTransaction(
40
+ to: string,
41
+ amount: number | string,
42
+ validUntil?: number
43
+ ): SendTransactionRequest {
44
+ return {
45
+ validUntil: validUntil || Date.now() + 5 * 60 * 1000, // 5 minutes default
46
+ messages: [
47
+ {
48
+ address: to,
49
+ amount: tonToNano(amount),
50
+ },
51
+ ],
52
+ };
53
+ }
54
+
55
+ /**
56
+ * Build a transaction with multiple recipients
57
+ * @param transfers - Array of {to, amount} transfers
58
+ * @param validUntil - Optional expiration timestamp (default: 5 minutes from now)
59
+ * @returns Transaction request
60
+ */
61
+ export function buildMultiTransferTransaction(
62
+ transfers: Array<{ to: string; amount: number | string }>,
63
+ validUntil?: number
64
+ ): SendTransactionRequest {
65
+ return {
66
+ validUntil: validUntil || Date.now() + 5 * 60 * 1000,
67
+ messages: transfers.map((transfer) => ({
68
+ address: transfer.to,
69
+ amount: tonToNano(transfer.amount),
70
+ })),
71
+ };
72
+ }
73
+
74
+ /**
75
+ * Build a transaction with custom payload
76
+ * @param to - Recipient address
77
+ * @param amount - Amount in TON
78
+ * @param payload - Base64 encoded payload
79
+ * @param validUntil - Optional expiration timestamp
80
+ * @returns Transaction request
81
+ */
82
+ export function buildTransactionWithPayload(
83
+ to: string,
84
+ amount: number | string,
85
+ payload: string,
86
+ validUntil?: number
87
+ ): SendTransactionRequest {
88
+ return {
89
+ validUntil: validUntil || Date.now() + 5 * 60 * 1000,
90
+ messages: [
91
+ {
92
+ address: to,
93
+ amount: tonToNano(amount),
94
+ payload,
95
+ },
96
+ ],
97
+ };
98
+ }
99
+
100
+ /**
101
+ * Build a transaction with state init (for contract deployment)
102
+ * @param to - Recipient address
103
+ * @param amount - Amount in TON
104
+ * @param stateInit - Base64 encoded state init
105
+ * @param validUntil - Optional expiration timestamp
106
+ * @returns Transaction request
107
+ */
108
+ export function buildTransactionWithStateInit(
109
+ to: string,
110
+ amount: number | string,
111
+ stateInit: string,
112
+ validUntil?: number
113
+ ): SendTransactionRequest {
114
+ return {
115
+ validUntil: validUntil || Date.now() + 5 * 60 * 1000,
116
+ messages: [
117
+ {
118
+ address: to,
119
+ amount: tonToNano(amount),
120
+ stateInit,
121
+ },
122
+ ],
123
+ };
124
+ }
125
+
126
+ /**
127
+ * Validate TON address format
128
+ * @param address - Address to validate
129
+ * @returns true if address is valid
130
+ */
131
+ export function isValidTonAddress(address: string): boolean {
132
+ // TON addresses start with EQ or 0Q and are 48 characters long (base64)
133
+ return /^(EQ|0Q)[A-Za-z0-9_-]{46}$/.test(address);
134
+ }
135
+
136
+ /**
137
+ * Format TON address for display (with ellipsis)
138
+ * @param address - Full address
139
+ * @param startLength - Characters to show at start (default: 6)
140
+ * @param endLength - Characters to show at end (default: 4)
141
+ * @returns Formatted address
142
+ */
143
+ export function formatTonAddress(
144
+ address: string,
145
+ startLength: number = 6,
146
+ endLength: number = 4
147
+ ): string {
148
+ if (address.length <= startLength + endLength) {
149
+ return address;
150
+ }
151
+ return `${address.substring(0, startLength)}...${address.substring(address.length - endLength)}`;
152
+ }
153
+