@gibs/bridge-sdk 1.12.0 → 1.13.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.
- package/LICENSE +15 -0
- package/README.md +234 -432
- package/dist/abis.d.ts +1 -0
- package/dist/bridge-limits.d.ts +103 -0
- package/dist/bridge-limits.js +84 -0
- package/dist/chain-info.d.ts +169 -0
- package/dist/chain-info.js +410 -0
- package/dist/chain-names.d.ts +160 -0
- package/dist/chain-names.js +200 -0
- package/dist/chains.d.ts +65 -0
- package/dist/chains.js +284 -0
- package/dist/config.d.ts +216 -0
- package/dist/config.js +493 -0
- package/dist/consolidated-quote.d.ts +554 -0
- package/dist/consolidated-quote.js +773 -0
- package/dist/destination-calldata.d.ts +118 -0
- package/dist/destination-calldata.js +152 -0
- package/dist/ecosystems.d.ts +217 -0
- package/dist/ecosystems.js +288 -0
- package/dist/fee-type.d.ts +10 -0
- package/dist/fee-type.js +9 -0
- package/dist/hyperliquid-execution.d.ts +152 -0
- package/dist/hyperliquid-execution.js +168 -0
- package/dist/image-links.d.ts +117 -0
- package/dist/image-links.js +142 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +20 -0
- package/dist/lifi-execution-guard.d.ts +199 -0
- package/dist/lifi-execution-guard.js +400 -0
- package/dist/near-intents-conformance.d.ts +85 -0
- package/dist/near-intents-conformance.js +329 -0
- package/dist/near-intents-route.d.ts +47 -0
- package/dist/near-intents-route.js +265 -0
- package/dist/near-intents.d.ts +524 -0
- package/dist/near-intents.js +1055 -0
- package/dist/origin-deposit.d.ts +326 -0
- package/dist/origin-deposit.js +68 -0
- package/dist/pre-quote.d.ts +198 -0
- package/dist/pre-quote.js +175 -0
- package/dist/prices.d.ts +107 -0
- package/dist/prices.js +162 -0
- package/dist/providers.d.ts +221 -0
- package/dist/providers.js +157 -0
- package/dist/quote-fees.d.ts +102 -0
- package/dist/quote-fees.js +195 -0
- package/dist/referral.d.ts +77 -0
- package/dist/referral.js +72 -0
- package/dist/relay-currencies.d.ts +47 -0
- package/dist/relay-currencies.js +89 -0
- package/dist/relay-quote.d.ts +301 -0
- package/dist/relay-quote.js +983 -0
- package/dist/route-steps.d.ts +138 -0
- package/dist/route-steps.js +305 -0
- package/dist/routing/amounts.d.ts +80 -0
- package/dist/routing/amounts.js +120 -0
- package/dist/routing/candidate-route.d.ts +43 -0
- package/dist/routing/candidate-route.js +45 -0
- package/dist/routing/candidate.d.ts +237 -0
- package/dist/routing/candidate.js +96 -0
- package/dist/routing/carriers/ask-answer.d.ts +58 -0
- package/dist/routing/carriers/ask-answer.js +1 -0
- package/dist/routing/carriers/omnibridge.d.ts +137 -0
- package/dist/routing/carriers/omnibridge.js +419 -0
- package/dist/routing/carriers/pulsex.d.ts +111 -0
- package/dist/routing/carriers/pulsex.js +404 -0
- package/dist/routing/carriers/wrap.d.ts +25 -0
- package/dist/routing/carriers/wrap.js +115 -0
- package/dist/routing/composed-route-quality.d.ts +103 -0
- package/dist/routing/composed-route-quality.js +108 -0
- package/dist/routing/entry-chain-anchor.d.ts +88 -0
- package/dist/routing/entry-chain-anchor.js +55 -0
- package/dist/routing/format.d.ts +31 -0
- package/dist/routing/format.js +58 -0
- package/dist/routing/index.d.ts +34 -0
- package/dist/routing/index.js +42 -0
- package/dist/routing/producers.d.ts +770 -0
- package/dist/routing/producers.js +1564 -0
- package/dist/routing/providers.d.ts +26 -0
- package/dist/routing/providers.js +38 -0
- package/dist/routing/route-data.d.ts +355 -0
- package/dist/routing/route-data.js +1 -0
- package/dist/routing/search/compose.d.ts +168 -0
- package/dist/routing/search/compose.js +98 -0
- package/dist/routing/search/graph.d.ts +639 -0
- package/dist/routing/search/graph.js +445 -0
- package/dist/routing/search/plan-route-data.d.ts +244 -0
- package/dist/routing/search/plan-route-data.js +767 -0
- package/dist/routing/search/score.d.ts +324 -0
- package/dist/routing/search/score.js +400 -0
- package/dist/routing/usd-value.d.ts +17 -0
- package/dist/routing/usd-value.js +25 -0
- package/dist/settings.d.ts +692 -0
- package/dist/settings.js +909 -0
- package/dist/ton-execution.d.ts +117 -0
- package/dist/ton-execution.js +247 -0
- package/dist/tron-execution.d.ts +146 -0
- package/dist/tron-execution.js +239 -0
- package/dist/types.d.ts +51 -0
- package/dist/types.js +1 -0
- package/package.json +203 -23
- package/src/chain-info.test.ts +0 -968
- package/src/chain-info.ts +0 -498
- package/src/chains.test.ts +0 -123
- package/src/chains.ts +0 -88
- package/src/config.test.ts +0 -679
- package/src/config.ts +0 -500
- package/src/fee-type.test.ts +0 -45
- package/src/fee-type.ts +0 -14
- package/src/image-links.test.ts +0 -160
- package/src/image-links.ts +0 -65
- package/src/index.ts +0 -8
- package/src/settings.test.ts +0 -2754
- package/src/settings.ts +0 -875
- package/src/types.ts +0 -48
- package/tsconfig.json +0 -17
- package/vitest.config.ts +0 -13
- /package/{src/abis.ts → dist/abis.js} +0 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { TonDeposit } from './origin-deposit.js';
|
|
2
|
+
/**
|
|
3
|
+
* Executing a deposit on The Open Network, as pure data.
|
|
4
|
+
*
|
|
5
|
+
* Relay describes a deposit as a list of internal messages carrying `to`,
|
|
6
|
+
* `value`, `body` and a `bounce` flag. A connected wallet, however, is driven
|
|
7
|
+
* through the TON Connect protocol, whose request shape is different in one way
|
|
8
|
+
* that silently loses money:
|
|
9
|
+
*
|
|
10
|
+
* **TON Connect has no per-message `bounce` field.** A wallet derives whether a
|
|
11
|
+
* message bounces from the FORM OF THE ADDRESS — the bounceable `EQ…` prefix or
|
|
12
|
+
* the non-bounceable `UQ…` one. Passing Relay's address through unchanged while
|
|
13
|
+
* dropping its `bounce: false` therefore sends a deposit to the bounceable form,
|
|
14
|
+
* and the depository bounces the funds straight back to the sender. The transfer
|
|
15
|
+
* looks like it succeeded and the route never fills.
|
|
16
|
+
*
|
|
17
|
+
* {@link buildTonConnectRequest} folds `bounce` into the address so the flag
|
|
18
|
+
* cannot be dropped on the way to a wallet.
|
|
19
|
+
*
|
|
20
|
+
* Kept free of transport and of clock reads so it is fully testable: nothing
|
|
21
|
+
* here signs, fetches, or calls `Date.now`. The caller supplies the deadline and
|
|
22
|
+
* performs the request.
|
|
23
|
+
*
|
|
24
|
+
* The rules encoded here are those of Relay's own wallet adapter, which is the
|
|
25
|
+
* only reference implementation of this mapping.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* The TON Connect network identifier for mainnet. Relay bridges only mainnet, so
|
|
29
|
+
* a request is always tagged with this.
|
|
30
|
+
*/
|
|
31
|
+
export declare const tonConnectMainnetNetwork = "-239";
|
|
32
|
+
/** How long a request stays valid by default, in seconds, matching Relay's adapter. */
|
|
33
|
+
export declare const tonConnectDefaultValidForSeconds = 300;
|
|
34
|
+
/** An address split into the parts that survive a change of encoding. */
|
|
35
|
+
type ParsedTonAddress = {
|
|
36
|
+
workchain: number;
|
|
37
|
+
hash: Uint8Array;
|
|
38
|
+
/** Preserved rather than assumed: re-encoding a testnet address as mainnet would misdirect it. */
|
|
39
|
+
testOnly: boolean;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Parses either address form The Open Network uses.
|
|
43
|
+
*
|
|
44
|
+
* @param value - a friendly (`EQ…`/`UQ…`) or raw (`0:…`) address
|
|
45
|
+
* @returns the address parts, or null when it is neither form or fails its checksum
|
|
46
|
+
*/
|
|
47
|
+
export declare const parseTonAddress: (value: string) => ParsedTonAddress | null;
|
|
48
|
+
/**
|
|
49
|
+
* Re-encodes an address into the friendly form matching a bounce setting.
|
|
50
|
+
*
|
|
51
|
+
* This is the whole point of the module: the bounce flag has nowhere else to
|
|
52
|
+
* live in a TON Connect request, so it has to be carried by the address itself.
|
|
53
|
+
*
|
|
54
|
+
* @param address - the address in either form
|
|
55
|
+
* @param options.bounceable - whether the message may bounce back to the sender
|
|
56
|
+
* @returns the friendly address, or null when the input cannot be parsed
|
|
57
|
+
*/
|
|
58
|
+
export declare const encodeTonAddress: (address: string, { bounceable }: {
|
|
59
|
+
bounceable: boolean;
|
|
60
|
+
}) => string | null;
|
|
61
|
+
/** One message of a TON Connect request. Note `address`/`amount`, not `to`/`value`. */
|
|
62
|
+
export type TonConnectMessage = {
|
|
63
|
+
address: string;
|
|
64
|
+
/** Attached amount in nanotons, as a decimal string. */
|
|
65
|
+
amount: string;
|
|
66
|
+
/** Base64 bag-of-cells body, omitted when the message carries none. */
|
|
67
|
+
payload?: string;
|
|
68
|
+
};
|
|
69
|
+
/** The request handed to a wallet's TON Connect `sendTransaction` call. */
|
|
70
|
+
export type TonConnectRequest = {
|
|
71
|
+
/** Unix seconds after which a wallet must refuse the request. */
|
|
72
|
+
validUntil: number;
|
|
73
|
+
network: string;
|
|
74
|
+
/** The sending account, when the deposit names one. */
|
|
75
|
+
from?: string;
|
|
76
|
+
messages: TonConnectMessage[];
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Builds the TON Connect request for a deposit.
|
|
80
|
+
*
|
|
81
|
+
* @param deposit - the parsed origin deposit
|
|
82
|
+
* @param options.validUntil - unix seconds after which the request expires.
|
|
83
|
+
* Supplied by the caller rather than read from a clock here so the transform
|
|
84
|
+
* stays pure; {@link tonConnectDefaultValidForSeconds} is the usual offset.
|
|
85
|
+
* @returns the request, or null when the deposit carries no messages or any
|
|
86
|
+
* destination address cannot be re-encoded — never a request whose bounce
|
|
87
|
+
* setting has been silently dropped
|
|
88
|
+
*/
|
|
89
|
+
export declare const buildTonConnectRequest: (deposit: TonDeposit, { validUntil }: {
|
|
90
|
+
validUntil: number;
|
|
91
|
+
}) => TonConnectRequest | null;
|
|
92
|
+
/**
|
|
93
|
+
* Derives the transaction identifier from a wallet's `sendTransaction` response.
|
|
94
|
+
*
|
|
95
|
+
* A wallet answers with a `boc`: the base64 bag-of-cells of the signed
|
|
96
|
+
* external-in message it broadcast — NOT the resulting transaction, and NOT a
|
|
97
|
+
* hash. The identifier explorers and indexers (toncenter, TonAPI) resolve a
|
|
98
|
+
* transaction by is the hash of that external message, but it must be the
|
|
99
|
+
* NORMALIZED hash of TEP-467, not the hash of the bytes the wallet happened to
|
|
100
|
+
* send.
|
|
101
|
+
*
|
|
102
|
+
* The distinction is the whole point, and getting it wrong loses a resume record
|
|
103
|
+
* for real funds. An external-in message can be serialized several equivalent
|
|
104
|
+
* ways — a wallet may or may not include its own `src`, may set a non-zero
|
|
105
|
+
* `import_fee`, may attach a `stateInit`, may store the body inline or as a
|
|
106
|
+
* reference. The raw cell hash changes with each of those wallet-specific
|
|
107
|
+
* choices, so it frequently does NOT match what an indexer keyed the same
|
|
108
|
+
* transaction under. TEP-467 rebuilds the message with those variable fields
|
|
109
|
+
* stripped (`src` to none, `import_fee` to zero, `init` dropped, body forced to a
|
|
110
|
+
* reference) so the hash is stable across wallets and matches the indexers.
|
|
111
|
+
*
|
|
112
|
+
* @param boc - the wallet's base64 `boc` field from `sendTransaction`
|
|
113
|
+
* @returns the 64-character hex normalized hash, or null when the input is not a
|
|
114
|
+
* parseable external-in message
|
|
115
|
+
*/
|
|
116
|
+
export declare const tonExternalMessageHash: (boc: string) => string | null;
|
|
117
|
+
export {};
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { Cell, beginCell, loadMessage, storeMessage } from '@ton/core';
|
|
2
|
+
/**
|
|
3
|
+
* Executing a deposit on The Open Network, as pure data.
|
|
4
|
+
*
|
|
5
|
+
* Relay describes a deposit as a list of internal messages carrying `to`,
|
|
6
|
+
* `value`, `body` and a `bounce` flag. A connected wallet, however, is driven
|
|
7
|
+
* through the TON Connect protocol, whose request shape is different in one way
|
|
8
|
+
* that silently loses money:
|
|
9
|
+
*
|
|
10
|
+
* **TON Connect has no per-message `bounce` field.** A wallet derives whether a
|
|
11
|
+
* message bounces from the FORM OF THE ADDRESS — the bounceable `EQ…` prefix or
|
|
12
|
+
* the non-bounceable `UQ…` one. Passing Relay's address through unchanged while
|
|
13
|
+
* dropping its `bounce: false` therefore sends a deposit to the bounceable form,
|
|
14
|
+
* and the depository bounces the funds straight back to the sender. The transfer
|
|
15
|
+
* looks like it succeeded and the route never fills.
|
|
16
|
+
*
|
|
17
|
+
* {@link buildTonConnectRequest} folds `bounce` into the address so the flag
|
|
18
|
+
* cannot be dropped on the way to a wallet.
|
|
19
|
+
*
|
|
20
|
+
* Kept free of transport and of clock reads so it is fully testable: nothing
|
|
21
|
+
* here signs, fetches, or calls `Date.now`. The caller supplies the deadline and
|
|
22
|
+
* performs the request.
|
|
23
|
+
*
|
|
24
|
+
* The rules encoded here are those of Relay's own wallet adapter, which is the
|
|
25
|
+
* only reference implementation of this mapping.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* The TON Connect network identifier for mainnet. Relay bridges only mainnet, so
|
|
29
|
+
* a request is always tagged with this.
|
|
30
|
+
*/
|
|
31
|
+
export const tonConnectMainnetNetwork = '-239';
|
|
32
|
+
/** How long a request stays valid by default, in seconds, matching Relay's adapter. */
|
|
33
|
+
export const tonConnectDefaultValidForSeconds = 300;
|
|
34
|
+
/** Address tag byte for the bounceable (`EQ…`) form. */
|
|
35
|
+
const bounceableTag = 0x11;
|
|
36
|
+
/** Address tag byte for the non-bounceable (`UQ…`) form. */
|
|
37
|
+
const nonBounceableTag = 0x51;
|
|
38
|
+
/** Bit set on the tag byte for testnet-only addresses (`kQ…` / `0Q…`). */
|
|
39
|
+
const testnetTagFlag = 0x80;
|
|
40
|
+
/** A friendly address is a 36-byte structure: tag, workchain, hash, checksum. */
|
|
41
|
+
const friendlyAddressByteLength = 36;
|
|
42
|
+
/** An account identifier is always 32 bytes, in every address form. */
|
|
43
|
+
const accountHashByteLength = 32;
|
|
44
|
+
/**
|
|
45
|
+
* Cyclic redundancy check (16-bit, the XModem variant) over an address body.
|
|
46
|
+
* The Open Network appends it to every friendly address, and a re-encoded
|
|
47
|
+
* address whose checksum is not recomputed is rejected by wallets as malformed.
|
|
48
|
+
*/
|
|
49
|
+
const checksum = (data) => {
|
|
50
|
+
let remainder = 0;
|
|
51
|
+
for (const byte of data) {
|
|
52
|
+
remainder ^= byte << 8;
|
|
53
|
+
for (let bit = 0; bit < 8; bit += 1) {
|
|
54
|
+
remainder =
|
|
55
|
+
remainder & 0x8000 ? ((remainder << 1) ^ 0x1021) & 0xffff : (remainder << 1) & 0xffff;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return remainder;
|
|
59
|
+
};
|
|
60
|
+
/** Decodes the url-safe base64 alphabet the friendly address form uses. */
|
|
61
|
+
const decodeBase64Url = (value) => {
|
|
62
|
+
const standard = value.replaceAll('-', '+').replaceAll('_', '/');
|
|
63
|
+
const padded = standard.padEnd(Math.ceil(standard.length / 4) * 4, '=');
|
|
64
|
+
try {
|
|
65
|
+
const binary = atob(padded);
|
|
66
|
+
return Uint8Array.from(binary, (character) => character.charCodeAt(0));
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
/** Encodes bytes back into the url-safe, unpadded base64 form. */
|
|
73
|
+
const encodeBase64Url = (bytes) => {
|
|
74
|
+
let binary = '';
|
|
75
|
+
for (const byte of bytes) {
|
|
76
|
+
binary += String.fromCharCode(byte);
|
|
77
|
+
}
|
|
78
|
+
return btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '');
|
|
79
|
+
};
|
|
80
|
+
const parseRawAddress = (value) => {
|
|
81
|
+
const separator = value.indexOf(':');
|
|
82
|
+
if (separator < 0) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
const workchain = Number(value.slice(0, separator));
|
|
86
|
+
const hex = value.slice(separator + 1);
|
|
87
|
+
if (!Number.isInteger(workchain) || hex.length !== accountHashByteLength * 2) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
const hash = new Uint8Array(accountHashByteLength);
|
|
91
|
+
for (let index = 0; index < accountHashByteLength; index += 1) {
|
|
92
|
+
const byte = Number.parseInt(hex.slice(index * 2, index * 2 + 2), 16);
|
|
93
|
+
if (Number.isNaN(byte)) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
hash[index] = byte;
|
|
97
|
+
}
|
|
98
|
+
// The raw form carries no network tag, so mainnet is the only safe assumption.
|
|
99
|
+
return { workchain, hash, testOnly: false };
|
|
100
|
+
};
|
|
101
|
+
const parseFriendlyAddress = (value) => {
|
|
102
|
+
const bytes = decodeBase64Url(value);
|
|
103
|
+
if (bytes === null || bytes.length !== friendlyAddressByteLength) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
const body = bytes.subarray(0, friendlyAddressByteLength - 2);
|
|
107
|
+
// Every index below is in range: the length was checked to be exactly
|
|
108
|
+
// `friendlyAddressByteLength` above, so the non-null assertions are sound.
|
|
109
|
+
const stated = (bytes[friendlyAddressByteLength - 2] << 8) | bytes[friendlyAddressByteLength - 1];
|
|
110
|
+
if (checksum(body) !== stated) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
const tag = bytes[0];
|
|
114
|
+
const withoutTestFlag = tag & ~testnetTagFlag;
|
|
115
|
+
if (withoutTestFlag !== bounceableTag && withoutTestFlag !== nonBounceableTag) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
const workchainByte = bytes[1];
|
|
119
|
+
return {
|
|
120
|
+
// The masterchain is workchain -1, which travels as the byte 0xff.
|
|
121
|
+
workchain: workchainByte === 0xff ? -1 : workchainByte,
|
|
122
|
+
hash: bytes.slice(2, 2 + accountHashByteLength),
|
|
123
|
+
testOnly: (tag & testnetTagFlag) !== 0,
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Parses either address form The Open Network uses.
|
|
128
|
+
*
|
|
129
|
+
* @param value - a friendly (`EQ…`/`UQ…`) or raw (`0:…`) address
|
|
130
|
+
* @returns the address parts, or null when it is neither form or fails its checksum
|
|
131
|
+
*/
|
|
132
|
+
export const parseTonAddress = (value) => value.includes(':') ? parseRawAddress(value) : parseFriendlyAddress(value);
|
|
133
|
+
/**
|
|
134
|
+
* Re-encodes an address into the friendly form matching a bounce setting.
|
|
135
|
+
*
|
|
136
|
+
* This is the whole point of the module: the bounce flag has nowhere else to
|
|
137
|
+
* live in a TON Connect request, so it has to be carried by the address itself.
|
|
138
|
+
*
|
|
139
|
+
* @param address - the address in either form
|
|
140
|
+
* @param options.bounceable - whether the message may bounce back to the sender
|
|
141
|
+
* @returns the friendly address, or null when the input cannot be parsed
|
|
142
|
+
*/
|
|
143
|
+
export const encodeTonAddress = (address, { bounceable }) => {
|
|
144
|
+
const parsed = parseTonAddress(address);
|
|
145
|
+
if (parsed === null) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
const bytes = new Uint8Array(friendlyAddressByteLength);
|
|
149
|
+
bytes[0] =
|
|
150
|
+
(bounceable ? bounceableTag : nonBounceableTag) | (parsed.testOnly ? testnetTagFlag : 0);
|
|
151
|
+
bytes[1] = parsed.workchain < 0 ? 0xff : parsed.workchain;
|
|
152
|
+
bytes.set(parsed.hash, 2);
|
|
153
|
+
const stated = checksum(bytes.subarray(0, friendlyAddressByteLength - 2));
|
|
154
|
+
bytes[friendlyAddressByteLength - 2] = (stated >> 8) & 0xff;
|
|
155
|
+
bytes[friendlyAddressByteLength - 1] = stated & 0xff;
|
|
156
|
+
return encodeBase64Url(bytes);
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Builds the TON Connect request for a deposit.
|
|
160
|
+
*
|
|
161
|
+
* @param deposit - the parsed origin deposit
|
|
162
|
+
* @param options.validUntil - unix seconds after which the request expires.
|
|
163
|
+
* Supplied by the caller rather than read from a clock here so the transform
|
|
164
|
+
* stays pure; {@link tonConnectDefaultValidForSeconds} is the usual offset.
|
|
165
|
+
* @returns the request, or null when the deposit carries no messages or any
|
|
166
|
+
* destination address cannot be re-encoded — never a request whose bounce
|
|
167
|
+
* setting has been silently dropped
|
|
168
|
+
*/
|
|
169
|
+
export const buildTonConnectRequest = (deposit, { validUntil }) => {
|
|
170
|
+
if (deposit.messages.length === 0) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
const messages = [];
|
|
174
|
+
for (const message of deposit.messages) {
|
|
175
|
+
const address = encodeTonAddress(message.to, { bounceable: message.bounce });
|
|
176
|
+
// Refuse rather than fall back to the unencoded address: the fallback is
|
|
177
|
+
// exactly the bounce-losing behaviour this module exists to prevent.
|
|
178
|
+
if (address === null) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
messages.push({
|
|
182
|
+
address,
|
|
183
|
+
amount: message.value.toString(),
|
|
184
|
+
...(message.body ? { payload: message.body } : {}),
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
// `messages[0]` is present: the empty case returned null above.
|
|
188
|
+
const from = deposit.messages[0].from;
|
|
189
|
+
return {
|
|
190
|
+
validUntil,
|
|
191
|
+
network: tonConnectMainnetNetwork,
|
|
192
|
+
...(from === null ? {} : { from }),
|
|
193
|
+
messages,
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* Derives the transaction identifier from a wallet's `sendTransaction` response.
|
|
198
|
+
*
|
|
199
|
+
* A wallet answers with a `boc`: the base64 bag-of-cells of the signed
|
|
200
|
+
* external-in message it broadcast — NOT the resulting transaction, and NOT a
|
|
201
|
+
* hash. The identifier explorers and indexers (toncenter, TonAPI) resolve a
|
|
202
|
+
* transaction by is the hash of that external message, but it must be the
|
|
203
|
+
* NORMALIZED hash of TEP-467, not the hash of the bytes the wallet happened to
|
|
204
|
+
* send.
|
|
205
|
+
*
|
|
206
|
+
* The distinction is the whole point, and getting it wrong loses a resume record
|
|
207
|
+
* for real funds. An external-in message can be serialized several equivalent
|
|
208
|
+
* ways — a wallet may or may not include its own `src`, may set a non-zero
|
|
209
|
+
* `import_fee`, may attach a `stateInit`, may store the body inline or as a
|
|
210
|
+
* reference. The raw cell hash changes with each of those wallet-specific
|
|
211
|
+
* choices, so it frequently does NOT match what an indexer keyed the same
|
|
212
|
+
* transaction under. TEP-467 rebuilds the message with those variable fields
|
|
213
|
+
* stripped (`src` to none, `import_fee` to zero, `init` dropped, body forced to a
|
|
214
|
+
* reference) so the hash is stable across wallets and matches the indexers.
|
|
215
|
+
*
|
|
216
|
+
* @param boc - the wallet's base64 `boc` field from `sendTransaction`
|
|
217
|
+
* @returns the 64-character hex normalized hash, or null when the input is not a
|
|
218
|
+
* parseable external-in message
|
|
219
|
+
*/
|
|
220
|
+
export const tonExternalMessageHash = (boc) => {
|
|
221
|
+
try {
|
|
222
|
+
const message = loadMessage(Cell.fromBase64(boc).beginParse());
|
|
223
|
+
// Only an external-in message has a normalized form; an internal message or
|
|
224
|
+
// a bare cell is not the wallet's broadcast envelope and cannot be keyed.
|
|
225
|
+
if (message.info.type !== 'external-in') {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
const normalized = {
|
|
229
|
+
...message,
|
|
230
|
+
// Dropped by TEP-467 so a wallet-attached stateInit does not move the hash.
|
|
231
|
+
init: null,
|
|
232
|
+
// `src` to addr_none (undefined) and the fee to zero: both are the sender's
|
|
233
|
+
// to choose and neither is part of what identifies the message.
|
|
234
|
+
info: { ...message.info, src: undefined, importFee: 0n },
|
|
235
|
+
};
|
|
236
|
+
// `forceRef` stores the body as a reference, as the normalized form requires.
|
|
237
|
+
return beginCell()
|
|
238
|
+
.store(storeMessage(normalized, { forceRef: true }))
|
|
239
|
+
.endCell()
|
|
240
|
+
.hash()
|
|
241
|
+
.toString('hex');
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
// A malformed base64 payload or a cell tree that is not a message at all.
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type { TronDeposit } from './origin-deposit.js';
|
|
2
|
+
/**
|
|
3
|
+
* Executing a deposit on Tron, as pure data.
|
|
4
|
+
*
|
|
5
|
+
* Relay does not return a broadcastable Tron transaction. It returns the
|
|
6
|
+
* *parameters* of a `TriggerSmartContract` call — owner, contract, call value
|
|
7
|
+
* and calldata — and nothing else. A Tron transaction additionally needs a
|
|
8
|
+
* reference block, an expiration and a fee limit, none of which Relay supplies.
|
|
9
|
+
* Those are produced by asking a full node to build the transaction, which is
|
|
10
|
+
* what {@link buildTriggerSmartContractRequest} constructs the body for. The
|
|
11
|
+
* node answers with an UNSIGNED transaction; the wallet signs it and a second
|
|
12
|
+
* call broadcasts it.
|
|
13
|
+
*
|
|
14
|
+
* Three details of that exchange lose money or mislead if handled naively:
|
|
15
|
+
*
|
|
16
|
+
* **The `visible` flag defaults to true.** A node reads addresses as base58
|
|
17
|
+
* unless told otherwise, but Relay returns them as hex with Tron's `41` prefix.
|
|
18
|
+
* Omitting the flag therefore hands base58-parsing a hex string. The request
|
|
19
|
+
* built here always states `visible: false` rather than relying on a default
|
|
20
|
+
* that is the opposite of what the payload contains.
|
|
21
|
+
*
|
|
22
|
+
* **`fee_limit` is required and Relay omits it.** It caps the energy the caller
|
|
23
|
+
* will pay for. Set too low, the call runs out of energy, reverts, and the
|
|
24
|
+
* energy already burned is NOT refunded — the deposit fails and costs the user
|
|
25
|
+
* anyway.
|
|
26
|
+
*
|
|
27
|
+
* **A rejected call still answers HTTP 200**, with the reason hex-encoded in
|
|
28
|
+
* `result.message`. Reading the status code alone reports a failed deposit as a
|
|
29
|
+
* success, and printing the message unmodified shows the user a wall of hex.
|
|
30
|
+
* {@link tronErrorMessage} decodes it.
|
|
31
|
+
*
|
|
32
|
+
* Kept free of transport and of clock reads so it is fully testable: nothing
|
|
33
|
+
* here signs, fetches, or calls `Date.now`. The caller performs the requests.
|
|
34
|
+
*/
|
|
35
|
+
/** Path of the full-node endpoint that builds an unsigned contract call. */
|
|
36
|
+
export declare const tronTriggerSmartContractPath = "/wallet/triggersmartcontract";
|
|
37
|
+
/**
|
|
38
|
+
* The fee limit used when a caller states none, in sun (1 TRX = 1,000,000 sun).
|
|
39
|
+
*
|
|
40
|
+
* Thirty TRX, matching Relay's own wallet adapter, which is the only reference
|
|
41
|
+
* implementation of this deposit path. It is an upper bound rather than a
|
|
42
|
+
* charge: unused energy is not taken, so the cost of setting it generously is
|
|
43
|
+
* only that the wallet shows a larger worst case.
|
|
44
|
+
*/
|
|
45
|
+
export declare const defaultTronFeeLimitSun = 30000000;
|
|
46
|
+
/**
|
|
47
|
+
* The largest fee limit a node accepts, in sun — 15,000 TRX. Documented as a
|
|
48
|
+
* hard protocol cap, so a request above it is rejected outright rather than
|
|
49
|
+
* clamped.
|
|
50
|
+
*/
|
|
51
|
+
export declare const maximumTronFeeLimitSun = 15000000000;
|
|
52
|
+
/**
|
|
53
|
+
* The body of a `/wallet/triggersmartcontract` request.
|
|
54
|
+
*
|
|
55
|
+
* Field names are the node's own snake_case, deliberately: this object is
|
|
56
|
+
* serialized and posted verbatim, so renaming to the codebase's conventions
|
|
57
|
+
* would only invite a translation step that can drift.
|
|
58
|
+
*/
|
|
59
|
+
export type TriggerSmartContractRequest = {
|
|
60
|
+
owner_address: string;
|
|
61
|
+
contract_address: string;
|
|
62
|
+
/** Calldata as bare hex, with no `0x` prefix. */
|
|
63
|
+
data: string;
|
|
64
|
+
/** Native TRX sent with the call, in sun. */
|
|
65
|
+
call_value: number;
|
|
66
|
+
/** Energy spending cap, in sun. */
|
|
67
|
+
fee_limit: number;
|
|
68
|
+
/** Always false — the addresses above are hex, not base58. See the module note. */
|
|
69
|
+
visible: false;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Normalizes a Tron hex address for comparison.
|
|
73
|
+
*
|
|
74
|
+
* Wallets and quotes disagree on casing, and Relay's payload is not guaranteed
|
|
75
|
+
* to match what a wallet reports even when both name the same account.
|
|
76
|
+
*
|
|
77
|
+
* @param value - an address in Tron's `41`-prefixed hex form, in any casing,
|
|
78
|
+
* optionally written with a `0x` prefix
|
|
79
|
+
* @returns the lowercase `41` form, or null when the value is not that form
|
|
80
|
+
*/
|
|
81
|
+
export declare const normalizeTronHexAddress: (value: string) => string | null;
|
|
82
|
+
/**
|
|
83
|
+
* Whether two Tron hex addresses name the same account.
|
|
84
|
+
*
|
|
85
|
+
* Used to check a deposit's owner against the connected wallet before signing.
|
|
86
|
+
* A mismatch means the quote was built for a different account, and signing it
|
|
87
|
+
* would spend from an account the user did not choose — or simply fail after
|
|
88
|
+
* the user has already approved.
|
|
89
|
+
*
|
|
90
|
+
* @param left - an address in Tron's `41`-prefixed hex form
|
|
91
|
+
* @param right - an address in the same form
|
|
92
|
+
* @returns true only when both parse and name the same account
|
|
93
|
+
*/
|
|
94
|
+
export declare const isSameTronAccount: (left: string, right: string) => boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Builds the body that asks a full node to construct the unsigned deposit.
|
|
97
|
+
*
|
|
98
|
+
* @param deposit - the parsed origin deposit
|
|
99
|
+
* @param options.feeLimitSun - the energy spending cap in sun; defaults to
|
|
100
|
+
* {@link defaultTronFeeLimitSun}
|
|
101
|
+
* @returns the request body, or null when the deposit names an address that is
|
|
102
|
+
* not Tron hex, carries a call value that cannot be represented exactly, or
|
|
103
|
+
* the fee limit is outside what a node accepts — never a body that would be
|
|
104
|
+
* built against a default `visible` the payload contradicts
|
|
105
|
+
*/
|
|
106
|
+
export declare const buildTriggerSmartContractRequest: (deposit: TronDeposit, { feeLimitSun }?: {
|
|
107
|
+
feeLimitSun?: number;
|
|
108
|
+
}) => TriggerSmartContractRequest | null;
|
|
109
|
+
/**
|
|
110
|
+
* Extracts a node's failure reason from either endpoint's response.
|
|
111
|
+
*
|
|
112
|
+
* @param payload - the parsed response body
|
|
113
|
+
* @returns the decoded message, or null when the response reports no failure
|
|
114
|
+
*/
|
|
115
|
+
export declare const tronErrorMessage: (payload: unknown) => string | null;
|
|
116
|
+
/**
|
|
117
|
+
* Reads the unsigned transaction out of a build response.
|
|
118
|
+
*
|
|
119
|
+
* The response carries the transaction at the top level alongside a `result`
|
|
120
|
+
* object stating whether the build succeeded, so the presence of a `txID` is
|
|
121
|
+
* not on its own evidence of success.
|
|
122
|
+
*
|
|
123
|
+
* @param payload - the parsed `/wallet/triggersmartcontract` response
|
|
124
|
+
* @returns the unsigned transaction to hand a wallet, or null when the node
|
|
125
|
+
* refused the call
|
|
126
|
+
*/
|
|
127
|
+
export declare const readTriggerSmartContractResponse: (payload: unknown) => Record<string, unknown> | null;
|
|
128
|
+
/**
|
|
129
|
+
* Reads the transaction identifier out of a broadcast response.
|
|
130
|
+
*
|
|
131
|
+
* GOTCHA: a rejected broadcast answers HTTP 200, and `result` is OMITTED rather
|
|
132
|
+
* than set to false. Testing `result === false` therefore never fires, and
|
|
133
|
+
* trusting the status code records a deposit that never entered a block. Only
|
|
134
|
+
* an explicit `result: true` is accepted here.
|
|
135
|
+
*
|
|
136
|
+
* The identifier is read from `txid` where a node reports it, falling back to
|
|
137
|
+
* the echoed transaction's `txID`. The two spellings differ by casing and by
|
|
138
|
+
* position: a node answers `txid`, while a wallet library that echoes the
|
|
139
|
+
* transaction it broadcast carries `txID` inside it. Accepting only the first
|
|
140
|
+
* would report an accepted broadcast as a failure whenever the caller is one of
|
|
141
|
+
* those libraries.
|
|
142
|
+
*
|
|
143
|
+
* @param payload - the parsed `/wallet/broadcasttransaction` response
|
|
144
|
+
* @returns the transaction identifier, or null when the broadcast was refused
|
|
145
|
+
*/
|
|
146
|
+
export declare const readBroadcastResponse: (payload: unknown) => string | null;
|