|
1 | 1 | import queryString from 'query-string';
|
2 | 2 | import { seeIfValidTonAddress, seeIfValidTronAddress } from '../utils/common';
|
| 3 | +import { TON_CONNECT_MSG_VARIANTS_ID, TonConnectTransactionPayload } from '../entries/tonConnect'; |
| 4 | +import { |
| 5 | + Address, |
| 6 | + beginCell, |
| 7 | + Cell, |
| 8 | + comment, |
| 9 | + CommonMessageInfoRelaxedInternal, |
| 10 | + storeStateInit |
| 11 | +} from '@ton/core'; |
| 12 | +import { DNSApi } from '../tonApiV2'; |
| 13 | +import { APIConfig } from '../entries/apis'; |
| 14 | +import { JettonEncoder } from './ton-blockchain/encoder/jetton-encoder'; |
3 | 15 |
|
4 | 16 | export function seeIfBringToFrontLink(options: { url: string }) {
|
5 | 17 | const { query } = queryString.parseUrl(options.url);
|
@@ -58,6 +70,163 @@ export function parseTonTransferWithAddress(options: { url: string }) {
|
58 | 70 | }
|
59 | 71 | }
|
60 | 72 |
|
| 73 | +// eslint-disable-next-line complexity |
| 74 | +export async function parseTonTransaction( |
| 75 | + url: string, |
| 76 | + { |
| 77 | + api, |
| 78 | + walletAddress, |
| 79 | + batteryResponse, |
| 80 | + gaslessResponse |
| 81 | + }: { |
| 82 | + api: APIConfig; |
| 83 | + walletAddress: string; |
| 84 | + batteryResponse: string; |
| 85 | + gaslessResponse: string; |
| 86 | + } |
| 87 | +): Promise< |
| 88 | + | { |
| 89 | + type: 'complex'; |
| 90 | + params: TonConnectTransactionPayload; |
| 91 | + } |
| 92 | + | { |
| 93 | + type: 'simple'; |
| 94 | + params: Omit<TonTransferParams, 'address'> & { address: string }; |
| 95 | + } |
| 96 | + | null |
| 97 | +> { |
| 98 | + try { |
| 99 | + const data = queryString.parseUrl(url); |
| 100 | + |
| 101 | + let paths = data.url.split('/'); |
| 102 | + paths = paths.slice(2); |
| 103 | + |
| 104 | + if (paths.length !== 2 || paths[0] !== 'transfer') { |
| 105 | + throw new Error('Unsupported link'); |
| 106 | + } |
| 107 | + |
| 108 | + const addrParam = paths[1]; |
| 109 | + if (typeof addrParam !== 'string') { |
| 110 | + throw new Error('Unsupported link: wrong address'); |
| 111 | + } |
| 112 | + |
| 113 | + let to = undefined; |
| 114 | + if (seeIfValidTonAddress(addrParam)) { |
| 115 | + to = addrParam; |
| 116 | + } else { |
| 117 | + const result = await new DNSApi(api.tonApiV2).dnsResolve({ domainName: addrParam }); |
| 118 | + if (result.wallet?.address && seeIfValidTonAddress(result.wallet?.address)) { |
| 119 | + to = result.wallet?.address; |
| 120 | + } else { |
| 121 | + throw new Error('Unsupported link: wrong dns'); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + let value: string; |
| 126 | + if (data.query.amount && typeof data.query.amount === 'string') { |
| 127 | + if (isFinite(parseInt(data.query.amount))) { |
| 128 | + value = data.query.amount; |
| 129 | + } else { |
| 130 | + throw new Error('Unsupported link: no amount'); |
| 131 | + } |
| 132 | + } else { |
| 133 | + return { |
| 134 | + type: 'simple', |
| 135 | + params: { |
| 136 | + address: to, |
| 137 | + text: typeof data.query.text === 'string' ? data.query.text : undefined, |
| 138 | + jetton: typeof data.query.jetton === 'string' ? data.query.jetton : undefined |
| 139 | + } |
| 140 | + }; |
| 141 | + } |
| 142 | + |
| 143 | + let validUntil = Math.ceil(Date.now() / 1000 + 5 * 60); |
| 144 | + if ( |
| 145 | + data.query.exp && |
| 146 | + typeof data.query.exp === 'string' && |
| 147 | + isFinite(parseInt(data.query.exp)) |
| 148 | + ) { |
| 149 | + validUntil = parseInt(data.query.exp); |
| 150 | + if (validUntil - 2000 < Date.now() / 1000) { |
| 151 | + throw new Error('Unsupported link: expired'); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + let payload = undefined; |
| 156 | + if (data.query.bin && typeof data.query.bin === 'string') { |
| 157 | + payload = data.query.bin; |
| 158 | + } |
| 159 | + |
| 160 | + if (data.query.text && typeof data.query.text === 'string') { |
| 161 | + if (payload !== undefined) { |
| 162 | + throw new Error('Unsupported link: payload and text'); |
| 163 | + } |
| 164 | + |
| 165 | + payload = comment(data.query.text).toBoc().toString('base64'); |
| 166 | + } |
| 167 | + |
| 168 | + if (data.query.jetton && typeof data.query.jetton === 'string') { |
| 169 | + const jetton = data.query.jetton; |
| 170 | + if (!seeIfValidTonAddress(jetton)) { |
| 171 | + throw new Error('Unsupported link: wrong jetton address'); |
| 172 | + } |
| 173 | + |
| 174 | + const params = { |
| 175 | + valid_until: validUntil, |
| 176 | + messages: await encodeJettonMessage( |
| 177 | + { to, value, payload, jetton }, |
| 178 | + { api, walletAddress } |
| 179 | + ), |
| 180 | + messagesVariants: { |
| 181 | + [TON_CONNECT_MSG_VARIANTS_ID.BATTERY]: { |
| 182 | + messages: await encodeJettonMessage( |
| 183 | + { to, value, payload, jetton, responseAddress: batteryResponse }, |
| 184 | + { api, walletAddress } |
| 185 | + ) |
| 186 | + }, |
| 187 | + [TON_CONNECT_MSG_VARIANTS_ID.GASLESS]: { |
| 188 | + messages: await encodeJettonMessage( |
| 189 | + { to, value, payload, jetton, responseAddress: gaslessResponse }, |
| 190 | + { api, walletAddress } |
| 191 | + ), |
| 192 | + options: { |
| 193 | + asset: jetton |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + } satisfies TonConnectTransactionPayload; |
| 198 | + return { |
| 199 | + type: 'complex', |
| 200 | + params |
| 201 | + }; |
| 202 | + } |
| 203 | + |
| 204 | + let stateInit = undefined; |
| 205 | + if (data.query.init && typeof data.query.init === 'string') { |
| 206 | + stateInit = data.query.init; |
| 207 | + } |
| 208 | + |
| 209 | + const params = { |
| 210 | + valid_until: validUntil, |
| 211 | + messages: [ |
| 212 | + { |
| 213 | + address: to, |
| 214 | + amount: value, |
| 215 | + payload, |
| 216 | + stateInit |
| 217 | + } |
| 218 | + ] |
| 219 | + } satisfies TonConnectTransactionPayload; |
| 220 | + return { |
| 221 | + type: 'complex', |
| 222 | + params |
| 223 | + }; |
| 224 | + } catch (e) { |
| 225 | + console.error(e); |
| 226 | + return null; |
| 227 | + } |
| 228 | +} |
| 229 | + |
61 | 230 | export function parseTronTransferWithAddress(options: { url: string }) {
|
62 | 231 | try {
|
63 | 232 | const data = queryString.parseUrl(options.url);
|
@@ -90,3 +259,56 @@ export function parseTronTransferWithAddress(options: { url: string }) {
|
90 | 259 | return null;
|
91 | 260 | }
|
92 | 261 | }
|
| 262 | + |
| 263 | +async function encodeJettonMessage( |
| 264 | + { |
| 265 | + to, |
| 266 | + value, |
| 267 | + payload, |
| 268 | + jetton, |
| 269 | + responseAddress |
| 270 | + }: { to: string; value: string; payload?: string; jetton: string; responseAddress?: string }, |
| 271 | + { |
| 272 | + api, |
| 273 | + walletAddress |
| 274 | + }: { |
| 275 | + api: APIConfig; |
| 276 | + walletAddress: string; |
| 277 | + } |
| 278 | +) { |
| 279 | + const je = new JettonEncoder(api, walletAddress); |
| 280 | + const jettonTransfer = await je.encodeTransfer({ |
| 281 | + to, |
| 282 | + amount: { |
| 283 | + asset: { |
| 284 | + address: Address.parse(jetton) |
| 285 | + }, |
| 286 | + stringWeiAmount: value |
| 287 | + }, |
| 288 | + responseAddress, |
| 289 | + payload: payload |
| 290 | + ? { |
| 291 | + type: 'raw', |
| 292 | + value: Cell.fromBase64(payload) |
| 293 | + } |
| 294 | + : undefined |
| 295 | + }); |
| 296 | + |
| 297 | + if (jettonTransfer.messages.length !== 1) { |
| 298 | + throw new Error('Unsupported link: wrong jetton encoding result'); |
| 299 | + } |
| 300 | + |
| 301 | + const msg = jettonTransfer.messages[0]; |
| 302 | + const info = msg.info as CommonMessageInfoRelaxedInternal; |
| 303 | + |
| 304 | + return [ |
| 305 | + { |
| 306 | + address: info.dest.toRawString(), |
| 307 | + amount: info.value.coins.toString(), |
| 308 | + payload: msg.body.toBoc().toString('base64'), |
| 309 | + stateInit: msg.init |
| 310 | + ? beginCell().store(storeStateInit(msg.init)).endCell().toBoc().toString('base64') |
| 311 | + : undefined |
| 312 | + } |
| 313 | + ] satisfies TonConnectTransactionPayload['messages']; |
| 314 | +} |
0 commit comments