@layerzerolabs/address-utils-ton 0.2.66

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,19 @@
1
+  WARN  Issue while reading "/home/runner/work/monorepo-internal/monorepo-internal/.npmrc". Failed to replace env in config: ${NPM_TOKEN}
2
+
3
+ > @layerzerolabs/address-utils-ton@0.0.0 build /home/runner/work/monorepo-internal/monorepo-internal/packages/vms/ton/address-utils-ton
4
+ > tsup
5
+
6
+ CLI Building entry: src/index.ts
7
+ CLI Using tsconfig: tsconfig.json
8
+ CLI tsup v8.5.1
9
+ CLI Using tsup config: /home/runner/work/monorepo-internal/monorepo-internal/packages/vms/ton/address-utils-ton/tsup.config.ts
10
+ CLI Target: ES2023
11
+ CLI Cleaning output folder
12
+ CJS Build start
13
+ ESM Build start
14
+ CJS dist/index.cjs 3.42 KB
15
+ CJS dist/index.cjs.map 7.22 KB
16
+ CJS ⚡️ Build success in 160ms
17
+ ESM dist/index.js 3.00 KB
18
+ ESM dist/index.js.map 7.21 KB
19
+ ESM ⚡️ Build success in 161ms
@@ -0,0 +1,8 @@
1
+
2
+ > @layerzerolabs/address-utils-ton@0.0.0 lint /home/runner/work/monorepo-internal/monorepo-internal/packages/vms/ton/address-utils-ton
3
+ > eslint . --max-warnings 0 || (eslint . --fix --max-warnings 0 && false)
4
+
5
+ (node:126312) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/monorepo-internal/monorepo-internal/eslint.config.js?mtime=1775694520701 is not specified and it doesn't parse as CommonJS.
6
+ Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
7
+ To eliminate this warning, add "type": "module" to /home/runner/work/monorepo-internal/monorepo-internal/package.json.
8
+ (Use `node --trace-warnings ...` to show where the warning was created)
@@ -0,0 +1,16 @@
1
+
2
+ > @layerzerolabs/address-utils-ton@0.0.0 test /home/runner/work/monorepo-internal/monorepo-internal/packages/vms/ton/address-utils-ton
3
+ > vitest --run --pass-with-no-tests --typecheck
4
+
5
+ Testing types with tsc and vue-tsc is an experimental feature.
6
+ Breaking changes might not follow SemVer, please pin Vitest's version when using it.
7
+
8
+  RUN  v3.2.4 /home/runner/work/monorepo-internal/monorepo-internal/packages/vms/ton/address-utils-ton
9
+
10
+ No test files found, exiting with code 0
11
+
12
+ include: **/*.{test,spec}.?(c|m)[jt]s?(x)
13
+ exclude: **/node_modules/**, **/dist/**, **/cypress/**, **/.{idea,git,cache,output,temp}/**, **/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build,eslint,prettier}.config.*
14
+ typecheck include: **/*.{test,spec}-d.?(c|m)[jt]s?(x)
15
+ typecheck exclude: **/node_modules/**, **/dist/**, **/cypress/**, **/.{idea,git,cache,output,temp}/**, **/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build,eslint,prettier}.config.*
16
+
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2026 - LayerZero Labs Ltd.
2
+
3
+ Permission is hereby granted, free of charge, to any
4
+ person obtaining a copy of this software and associated
5
+ documentation files (the "Software"), to deal in the
6
+ Software without restriction, including without
7
+ limitation the rights to use, copy, modify, merge,
8
+ publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software
10
+ is furnished to do so, subject to the following
11
+ conditions:
12
+ The above copyright notice and this permission notice
13
+ shall be included in all copies or substantial portions
14
+ of the Software.
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17
+ TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
18
+ PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
19
+ SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
22
+ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
+ DEALINGS IN THE SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ var ton = require('@ton/ton');
4
+ var base58 = require('bs58');
5
+ var commonEncodingUtils = require('@layerzerolabs/common-encoding-utils');
6
+ var tonSdkTools = require('@layerzerolabs/ton-sdk-tools');
7
+
8
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
+
10
+ var base58__default = /*#__PURE__*/_interopDefault(base58);
11
+
12
+ var __defProp = Object.defineProperty;
13
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
14
+ var to32ByteBuffer = /* @__PURE__ */ __name((value, maxIntermediateBufferSize = 66) => {
15
+ if (typeof value === "string") {
16
+ if (!commonEncodingUtils.isHexString(value)) {
17
+ throw new Error("only hex string is supported");
18
+ }
19
+ let hex = commonEncodingUtils.trim0x(value);
20
+ if (hex.length % 2 !== 0) {
21
+ hex = "0" + hex;
22
+ }
23
+ value = tonSdkTools.bufferToBigInt(Buffer.from(hex, "hex"));
24
+ }
25
+ if (value instanceof Uint8Array) {
26
+ value = tonSdkTools.bufferToBigInt(Buffer.from(value));
27
+ }
28
+ const bf = tonSdkTools.bigintToSizedBuffer(BigInt(value), maxIntermediateBufferSize);
29
+ return bf.subarray(-32);
30
+ }, "to32ByteBuffer");
31
+ var bigintToAddress = /* @__PURE__ */ __name((value) => {
32
+ const buf = to32ByteBuffer(value);
33
+ return ton.Address.parse(`0:${buf.toString("hex")}`);
34
+ }, "bigintToAddress");
35
+ var parseTonAddress = /* @__PURE__ */ __name((address) => {
36
+ if (address instanceof ton.Address) {
37
+ return address;
38
+ }
39
+ try {
40
+ if (typeof address === "bigint" || typeof address === "number") {
41
+ return bigintToAddress(BigInt(address));
42
+ }
43
+ if (address.startsWith("0x")) {
44
+ return bigintToAddress(BigInt(address));
45
+ }
46
+ try {
47
+ return ton.Address.parse(address);
48
+ } catch {
49
+ const _isBase58 = commonEncodingUtils.isBase58(address);
50
+ const _isHex = commonEncodingUtils.isHexString(address);
51
+ if (_isHex) {
52
+ return bigintToAddress(BigInt(`0x${address}`));
53
+ }
54
+ if (_isBase58) {
55
+ return bigintToAddress(BigInt(`0x${Buffer.from(base58__default.default.decode(address)).toString("hex")}`));
56
+ }
57
+ throw new Error(`The address ${address} cannot be decoded as TON, hex, or base58`);
58
+ }
59
+ } catch (err) {
60
+ console.error("Error parsing address", {
61
+ address,
62
+ error: err?.message ?? err
63
+ });
64
+ throw err;
65
+ }
66
+ }, "parseTonAddress");
67
+ var _addressToNotPaddedHex = /* @__PURE__ */ __name((address) => {
68
+ return `0x${parseTonAddress(address).hash.toString("hex")}`;
69
+ }, "_addressToNotPaddedHex");
70
+ var addressToHex = /* @__PURE__ */ __name((address) => {
71
+ return `0x${to32ByteBuffer(_addressToNotPaddedHex(address)).toString("hex").toLowerCase()}`;
72
+ }, "addressToHex");
73
+ var addressToBigInt = /* @__PURE__ */ __name((address) => {
74
+ return BigInt(_addressToNotPaddedHex(address));
75
+ }, "addressToBigInt");
76
+ var compareAddresses = /* @__PURE__ */ __name((addressA, addressB) => {
77
+ return parseTonAddress(addressA).equals(parseTonAddress(addressB));
78
+ }, "compareAddresses");
79
+ var numberOfLeadingZeroBitsInAddress = /* @__PURE__ */ __name((address) => {
80
+ return 32 * 8 - addressToBigInt(address).toString(2).length;
81
+ }, "numberOfLeadingZeroBitsInAddress");
82
+
83
+ exports.addressToBigInt = addressToBigInt;
84
+ exports.addressToHex = addressToHex;
85
+ exports.bigintToAddress = bigintToAddress;
86
+ exports.compareAddresses = compareAddresses;
87
+ exports.numberOfLeadingZeroBitsInAddress = numberOfLeadingZeroBitsInAddress;
88
+ exports.parseTonAddress = parseTonAddress;
89
+ //# sourceMappingURL=index.cjs.map
90
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["to32ByteBuffer","value","maxIntermediateBufferSize","isHexString","Error","hex","trim0x","length","bufferToBigInt","Buffer","from","Uint8Array","bf","bigintToSizedBuffer","BigInt","subarray","bigintToAddress","buf","Address","parse","toString","parseTonAddress","address","startsWith","_isBase58","isBase58","_isHex","base58","decode","err","console","error","message","_addressToNotPaddedHex","hash","addressToHex","toLowerCase","addressToBigInt","compareAddresses","addressA","addressB","equals","numberOfLeadingZeroBitsInAddress"],"mappings":";;;;;;;;;;;;;AASA,IAAMA,cAAAA,mBAAiB,MAAA,CAAA,CACnBC,KAAAA,EACAC,yBAAAA,GAA4B,EAAA,KAAE;AAE9B,EAAA,IAAI,OAAOD,UAAU,QAAA,EAAU;AAC3B,IAAA,IAAI,CAACE,+BAAAA,CAAYF,KAAAA,CAAAA,EAAQ;AACrB,MAAA,MAAM,IAAIG,MAAM,8BAAA,CAAA;AACpB,IAAA;AACA,IAAA,IAAIC,GAAAA,GAAMC,2BAAOL,KAAAA,CAAAA;AACjB,IAAA,IAAII,GAAAA,CAAIE,MAAAA,GAAS,CAAA,KAAM,CAAA,EAAG;AACtBF,MAAAA,GAAAA,GAAM,GAAA,GAAMA,GAAAA;AAChB,IAAA;AACAJ,IAAAA,KAAAA,GAAQO,0BAAAA,CAAeC,MAAAA,CAAOC,IAAAA,CAAKL,GAAAA,EAAK,KAAA,CAAA,CAAA;AAC5C,EAAA;AACA,EAAA,IAAIJ,iBAAiBU,UAAAA,EAAY;AAC7BV,IAAAA,KAAAA,GAAQO,0BAAAA,CAAeC,MAAAA,CAAOC,IAAAA,CAAKT,KAAAA,CAAAA,CAAAA;AACvC,EAAA;AACA,EAAA,MAAMW,EAAAA,GAAKC,+BAAAA,CAAoBC,MAAAA,CAAOb,KAAAA,GAAQC,yBAAAA,CAAAA;AAE9C,EAAA,OAAOU,EAAAA,CAAGG,SAAS,GAAC,CAAA;AACxB,CAAA,EApBuB,gBAAA,CAAA;AAsBhB,IAAMC,eAAAA,2BAAmBf,KAAAA,KAAAA;AA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A,CAAAA,CAASF,QAAAA,CAAS,CAAA,CAAA,CAAGb,MAAAA;AACzD,CAAA,EAFgD,kCAAA","file":"index.cjs","sourcesContent":["import { Address } from '@ton/ton';\nimport base58 from 'bs58';\n\nimport type { HexString } from '@layerzerolabs/common-chain-model';\nimport { isBase58, isHexString, trim0x } from '@layerzerolabs/common-encoding-utils';\nimport { bigintToSizedBuffer, bufferToBigInt } from '@layerzerolabs/ton-sdk-tools';\n\nexport type AddressTypeLike = Address | string | bigint;\n\nconst to32ByteBuffer = (\n value: bigint | number | string | Uint8Array,\n maxIntermediateBufferSize = 66,\n): Buffer => {\n if (typeof value === 'string') {\n if (!isHexString(value)) {\n throw new Error('only hex string is supported');\n }\n let hex = trim0x(value);\n if (hex.length % 2 !== 0) {\n hex = '0' + hex;\n }\n value = bufferToBigInt(Buffer.from(hex, 'hex'));\n }\n if (value instanceof Uint8Array) {\n value = bufferToBigInt(Buffer.from(value));\n }\n const bf = bigintToSizedBuffer(BigInt(value), maxIntermediateBufferSize);\n // trim from the left, keep the right 32 bytes\n return bf.subarray(-32);\n};\n\nexport const bigintToAddress = (value: bigint): Address => {\n const buf = to32ByteBuffer(value);\n return Address.parse(`0:${buf.toString('hex')}`);\n};\n\nexport const parseTonAddress = (address: AddressTypeLike): Address => {\n if (address instanceof Address) {\n return address;\n }\n\n try {\n if (typeof address === 'bigint' || typeof address === 'number') {\n return bigintToAddress(BigInt(address));\n }\n\n if (address.startsWith('0x')) {\n // If it's a hex address format it to ton format\n return bigintToAddress(BigInt(address));\n }\n\n try {\n return Address.parse(address);\n } catch {\n //at this point, our address is either hex without a '0x', or base58\n const _isBase58 = isBase58(address);\n const _isHex = isHexString(address);\n\n //this includes the case where the address is both base58 and hex\n //but nearly every address generated this way is hex (only 1/(~5.5e12) are b58)\n if (_isHex) {\n return bigintToAddress(BigInt(`0x${address}`));\n }\n\n if (_isBase58) {\n return bigintToAddress(\n BigInt(`0x${Buffer.from(base58.decode(address)).toString('hex')}`),\n );\n }\n\n //this is the case where the address is invalid in any encoding\n throw new Error(`The address ${address} cannot be decoded as TON, hex, or base58`);\n }\n } catch (err: any) {\n console.error('Error parsing address', {\n address,\n error: err?.message ?? err,\n });\n throw err;\n }\n};\n\nconst _addressToNotPaddedHex = (address: AddressTypeLike) => {\n return `0x${parseTonAddress(address).hash.toString('hex')}`;\n};\n\nexport const addressToHex = (address: AddressTypeLike): HexString => {\n return `0x${to32ByteBuffer(_addressToNotPaddedHex(address)).toString('hex').toLowerCase()}`;\n};\n\nexport const addressToBigInt = (address: AddressTypeLike) => {\n return BigInt(_addressToNotPaddedHex(address));\n};\n\nexport const compareAddresses = (addressA: AddressTypeLike, addressB: AddressTypeLike) => {\n return parseTonAddress(addressA).equals(parseTonAddress(addressB));\n};\n\n/**\n * Since toString(2) does not include leading zeros, we can calculate the leading zeros by subtracting\n * the total length of address in binary from the total length of addresses in TON in bits (32 bytes * 8)\n */\nexport const numberOfLeadingZeroBitsInAddress = (address: AddressTypeLike) => {\n return 32 * 8 - addressToBigInt(address).toString(2).length;\n};\n"]}
@@ -0,0 +1,14 @@
1
+ import { Address } from '@ton/ton';
2
+ import type { HexString } from '@layerzerolabs/common-chain-model';
3
+ export type AddressTypeLike = Address | string | bigint;
4
+ export declare const bigintToAddress: (value: bigint) => Address;
5
+ export declare const parseTonAddress: (address: AddressTypeLike) => Address;
6
+ export declare const addressToHex: (address: AddressTypeLike) => HexString;
7
+ export declare const addressToBigInt: (address: AddressTypeLike) => bigint;
8
+ export declare const compareAddresses: (addressA: AddressTypeLike, addressB: AddressTypeLike) => boolean;
9
+ /**
10
+ * Since toString(2) does not include leading zeros, we can calculate the leading zeros by subtracting
11
+ * the total length of address in binary from the total length of addresses in TON in bits (32 bytes * 8)
12
+ */
13
+ export declare const numberOfLeadingZeroBitsInAddress: (address: AddressTypeLike) => number;
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAGnC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AAInE,MAAM,MAAM,eAAe,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,CAAC;AAwBxD,eAAO,MAAM,eAAe,GAAI,OAAO,MAAM,KAAG,OAG/C,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,SAAS,eAAe,KAAG,OA4C1D,CAAC;AAMF,eAAO,MAAM,YAAY,GAAI,SAAS,eAAe,KAAG,SAEvD,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,SAAS,eAAe,WAEvD,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,UAAU,eAAe,EAAE,UAAU,eAAe,YAEpF,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,gCAAgC,GAAI,SAAS,eAAe,WAExE,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,79 @@
1
+ import { Address } from '@ton/ton';
2
+ import base58 from 'bs58';
3
+ import { isBase58, isHexString, trim0x } from '@layerzerolabs/common-encoding-utils';
4
+ import { bufferToBigInt, bigintToSizedBuffer } from '@layerzerolabs/ton-sdk-tools';
5
+
6
+ var __defProp = Object.defineProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var to32ByteBuffer = /* @__PURE__ */ __name((value, maxIntermediateBufferSize = 66) => {
9
+ if (typeof value === "string") {
10
+ if (!isHexString(value)) {
11
+ throw new Error("only hex string is supported");
12
+ }
13
+ let hex = trim0x(value);
14
+ if (hex.length % 2 !== 0) {
15
+ hex = "0" + hex;
16
+ }
17
+ value = bufferToBigInt(Buffer.from(hex, "hex"));
18
+ }
19
+ if (value instanceof Uint8Array) {
20
+ value = bufferToBigInt(Buffer.from(value));
21
+ }
22
+ const bf = bigintToSizedBuffer(BigInt(value), maxIntermediateBufferSize);
23
+ return bf.subarray(-32);
24
+ }, "to32ByteBuffer");
25
+ var bigintToAddress = /* @__PURE__ */ __name((value) => {
26
+ const buf = to32ByteBuffer(value);
27
+ return Address.parse(`0:${buf.toString("hex")}`);
28
+ }, "bigintToAddress");
29
+ var parseTonAddress = /* @__PURE__ */ __name((address) => {
30
+ if (address instanceof Address) {
31
+ return address;
32
+ }
33
+ try {
34
+ if (typeof address === "bigint" || typeof address === "number") {
35
+ return bigintToAddress(BigInt(address));
36
+ }
37
+ if (address.startsWith("0x")) {
38
+ return bigintToAddress(BigInt(address));
39
+ }
40
+ try {
41
+ return Address.parse(address);
42
+ } catch {
43
+ const _isBase58 = isBase58(address);
44
+ const _isHex = isHexString(address);
45
+ if (_isHex) {
46
+ return bigintToAddress(BigInt(`0x${address}`));
47
+ }
48
+ if (_isBase58) {
49
+ return bigintToAddress(BigInt(`0x${Buffer.from(base58.decode(address)).toString("hex")}`));
50
+ }
51
+ throw new Error(`The address ${address} cannot be decoded as TON, hex, or base58`);
52
+ }
53
+ } catch (err) {
54
+ console.error("Error parsing address", {
55
+ address,
56
+ error: err?.message ?? err
57
+ });
58
+ throw err;
59
+ }
60
+ }, "parseTonAddress");
61
+ var _addressToNotPaddedHex = /* @__PURE__ */ __name((address) => {
62
+ return `0x${parseTonAddress(address).hash.toString("hex")}`;
63
+ }, "_addressToNotPaddedHex");
64
+ var addressToHex = /* @__PURE__ */ __name((address) => {
65
+ return `0x${to32ByteBuffer(_addressToNotPaddedHex(address)).toString("hex").toLowerCase()}`;
66
+ }, "addressToHex");
67
+ var addressToBigInt = /* @__PURE__ */ __name((address) => {
68
+ return BigInt(_addressToNotPaddedHex(address));
69
+ }, "addressToBigInt");
70
+ var compareAddresses = /* @__PURE__ */ __name((addressA, addressB) => {
71
+ return parseTonAddress(addressA).equals(parseTonAddress(addressB));
72
+ }, "compareAddresses");
73
+ var numberOfLeadingZeroBitsInAddress = /* @__PURE__ */ __name((address) => {
74
+ return 32 * 8 - addressToBigInt(address).toString(2).length;
75
+ }, "numberOfLeadingZeroBitsInAddress");
76
+
77
+ export { addressToBigInt, addressToHex, bigintToAddress, compareAddresses, numberOfLeadingZeroBitsInAddress, parseTonAddress };
78
+ //# sourceMappingURL=index.js.map
79
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["to32ByteBuffer","value","maxIntermediateBufferSize","isHexString","Error","hex","trim0x","length","bufferToBigInt","Buffer","from","Uint8Array","bf","bigintToSizedBuffer","BigInt","subarray","bigintToAddress","buf","Address","parse","toString","parseTonAddress","address","startsWith","_isBase58","isBase58","_isHex","base58","decode","err","console","error","message","_addressToNotPaddedHex","hash","addressToHex","toLowerCase","addressToBigInt","compareAddresses","addressA","addressB","equals","numberOfLeadingZeroBitsInAddress"],"mappings":";;;;;;;AASA,IAAMA,cAAAA,mBAAiB,MAAA,CAAA,CACnBC,KAAAA,EACAC,yBAAAA,GAA4B,EAAA,KAAE;AAE9B,EAAA,IAAI,OAAOD,UAAU,QAAA,EAAU;AAC3B,IAAA,IAAI,CAACE,WAAAA,CAAYF,KAAAA,CAAAA,EAAQ;AACrB,MAAA,MAAM,IAAIG,MAAM,8BAAA,CAAA;AACpB,IAAA;AACA,IAAA,IAAIC,GAAAA,GAAMC,OAAOL,KAAAA,CAAAA;AACjB,IAAA,IAAII,GAAAA,CAAIE,MAAAA,GAAS,CAAA,KAAM,CAAA,EAAG;AACtBF,MAAAA,GAAAA,GAAM,GAAA,GAAMA,GAAAA;AAChB,IAAA;AACAJ,IAAAA,KAAAA,GAAQO,cAAAA,CAAeC,MAAAA,CAAOC,IAAAA,CAAKL,GAAAA,EAAK,KAAA,CAAA,CAAA;AAC5C,EAAA;AACA,EAAA,IAAIJ,iBAAiBU,UAAAA,EAAY;AAC7BV,IAAAA,KAAAA,GAAQO,cAAAA,CAAeC,MAAAA,CAAOC,IAAAA,CAAKT,KAAAA,CAAAA,CAAAA;AACvC,EAAA;AACA,EAAA,MAAMW,EAAAA,GAAKC,mBAAAA,CAAoBC,MAAAA,CAA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CAAgBmB,QAAAA,CAAAA,CAAAA;AAC5D,CAAA,EAFgC,kBAAA;AAQzB,IAAME,gCAAAA,2BAAoCpB,OAAAA,KAAAA;AAC7C,EAAA,OAAO,KAAK,CAAA,GAAIe,eAAAA,CAAgBf,OAAAA,CAAAA,CAASF,QAAAA,CAAS,CAAA,CAAA,CAAGb,MAAAA;AACzD,CAAA,EAFgD,kCAAA","file":"index.js","sourcesContent":["import { Address } from '@ton/ton';\nimport base58 from 'bs58';\n\nimport type { HexString } from '@layerzerolabs/common-chain-model';\nimport { isBase58, isHexString, trim0x } from '@layerzerolabs/common-encoding-utils';\nimport { bigintToSizedBuffer, bufferToBigInt } from '@layerzerolabs/ton-sdk-tools';\n\nexport type AddressTypeLike = Address | string | bigint;\n\nconst to32ByteBuffer = (\n value: bigint | number | string | Uint8Array,\n maxIntermediateBufferSize = 66,\n): Buffer => {\n if (typeof value === 'string') {\n if (!isHexString(value)) {\n throw new Error('only hex string is supported');\n }\n let hex = trim0x(value);\n if (hex.length % 2 !== 0) {\n hex = '0' + hex;\n }\n value = bufferToBigInt(Buffer.from(hex, 'hex'));\n }\n if (value instanceof Uint8Array) {\n value = bufferToBigInt(Buffer.from(value));\n }\n const bf = bigintToSizedBuffer(BigInt(value), maxIntermediateBufferSize);\n // trim from the left, keep the right 32 bytes\n return bf.subarray(-32);\n};\n\nexport const bigintToAddress = (value: bigint): Address => {\n const buf = to32ByteBuffer(value);\n return Address.parse(`0:${buf.toString('hex')}`);\n};\n\nexport const parseTonAddress = (address: AddressTypeLike): Address => {\n if (address instanceof Address) {\n return address;\n }\n\n try {\n if (typeof address === 'bigint' || typeof address === 'number') {\n return bigintToAddress(BigInt(address));\n }\n\n if (address.startsWith('0x')) {\n // If it's a hex address format it to ton format\n return bigintToAddress(BigInt(address));\n }\n\n try {\n return Address.parse(address);\n } catch {\n //at this point, our address is either hex without a '0x', or base58\n const _isBase58 = isBase58(address);\n const _isHex = isHexString(address);\n\n //this includes the case where the address is both base58 and hex\n //but nearly every address generated this way is hex (only 1/(~5.5e12) are b58)\n if (_isHex) {\n return bigintToAddress(BigInt(`0x${address}`));\n }\n\n if (_isBase58) {\n return bigintToAddress(\n BigInt(`0x${Buffer.from(base58.decode(address)).toString('hex')}`),\n );\n }\n\n //this is the case where the address is invalid in any encoding\n throw new Error(`The address ${address} cannot be decoded as TON, hex, or base58`);\n }\n } catch (err: any) {\n console.error('Error parsing address', {\n address,\n error: err?.message ?? err,\n });\n throw err;\n }\n};\n\nconst _addressToNotPaddedHex = (address: AddressTypeLike) => {\n return `0x${parseTonAddress(address).hash.toString('hex')}`;\n};\n\nexport const addressToHex = (address: AddressTypeLike): HexString => {\n return `0x${to32ByteBuffer(_addressToNotPaddedHex(address)).toString('hex').toLowerCase()}`;\n};\n\nexport const addressToBigInt = (address: AddressTypeLike) => {\n return BigInt(_addressToNotPaddedHex(address));\n};\n\nexport const compareAddresses = (addressA: AddressTypeLike, addressB: AddressTypeLike) => {\n return parseTonAddress(addressA).equals(parseTonAddress(addressB));\n};\n\n/**\n * Since toString(2) does not include leading zeros, we can calculate the leading zeros by subtracting\n * the total length of address in binary from the total length of addresses in TON in bits (32 bytes * 8)\n */\nexport const numberOfLeadingZeroBitsInAddress = (address: AddressTypeLike) => {\n return 32 * 8 - addressToBigInt(address).toString(2).length;\n};\n"]}
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@layerzerolabs/address-utils-ton",
3
+ "version": "0.2.66",
4
+ "private": false,
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ "types": "./dist/index.d.ts",
9
+ "require": "./dist/index.cjs",
10
+ "import": "./dist/index.js",
11
+ "default": "./dist/index.cjs"
12
+ },
13
+ "main": "./dist/index.cjs",
14
+ "module": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "dependencies": {
17
+ "@layerzerolabs/ton-sdk-tools": "3.0.167",
18
+ "@ton/ton": "npm:@layerzerolabs/ton@15.1.0-rc.9",
19
+ "bs58": "^5.0.0",
20
+ "@layerzerolabs/common-encoding-utils": "0.2.66",
21
+ "@layerzerolabs/common-chain-model": "0.2.66"
22
+ },
23
+ "devDependencies": {
24
+ "tsup": "^8.4.0",
25
+ "vitest": "^3.2.3",
26
+ "@layerzerolabs/typescript-configuration": "0.2.66",
27
+ "@layerzerolabs/tsup-configuration": "0.2.66"
28
+ },
29
+ "publishConfig": {
30
+ "access": "restricted",
31
+ "registry": "https://registry.npmjs.org/"
32
+ },
33
+ "scripts": {
34
+ "build": "tsup",
35
+ "lint": "eslint . --max-warnings 0 || (eslint . --fix --max-warnings 0 && false)",
36
+ "test": "vitest --run --pass-with-no-tests --typecheck"
37
+ }
38
+ }
package/src/index.ts ADDED
@@ -0,0 +1,105 @@
1
+ import { Address } from '@ton/ton';
2
+ import base58 from 'bs58';
3
+
4
+ import type { HexString } from '@layerzerolabs/common-chain-model';
5
+ import { isBase58, isHexString, trim0x } from '@layerzerolabs/common-encoding-utils';
6
+ import { bigintToSizedBuffer, bufferToBigInt } from '@layerzerolabs/ton-sdk-tools';
7
+
8
+ export type AddressTypeLike = Address | string | bigint;
9
+
10
+ const to32ByteBuffer = (
11
+ value: bigint | number | string | Uint8Array,
12
+ maxIntermediateBufferSize = 66,
13
+ ): Buffer => {
14
+ if (typeof value === 'string') {
15
+ if (!isHexString(value)) {
16
+ throw new Error('only hex string is supported');
17
+ }
18
+ let hex = trim0x(value);
19
+ if (hex.length % 2 !== 0) {
20
+ hex = '0' + hex;
21
+ }
22
+ value = bufferToBigInt(Buffer.from(hex, 'hex'));
23
+ }
24
+ if (value instanceof Uint8Array) {
25
+ value = bufferToBigInt(Buffer.from(value));
26
+ }
27
+ const bf = bigintToSizedBuffer(BigInt(value), maxIntermediateBufferSize);
28
+ // trim from the left, keep the right 32 bytes
29
+ return bf.subarray(-32);
30
+ };
31
+
32
+ export const bigintToAddress = (value: bigint): Address => {
33
+ const buf = to32ByteBuffer(value);
34
+ return Address.parse(`0:${buf.toString('hex')}`);
35
+ };
36
+
37
+ export const parseTonAddress = (address: AddressTypeLike): Address => {
38
+ if (address instanceof Address) {
39
+ return address;
40
+ }
41
+
42
+ try {
43
+ if (typeof address === 'bigint' || typeof address === 'number') {
44
+ return bigintToAddress(BigInt(address));
45
+ }
46
+
47
+ if (address.startsWith('0x')) {
48
+ // If it's a hex address format it to ton format
49
+ return bigintToAddress(BigInt(address));
50
+ }
51
+
52
+ try {
53
+ return Address.parse(address);
54
+ } catch {
55
+ //at this point, our address is either hex without a '0x', or base58
56
+ const _isBase58 = isBase58(address);
57
+ const _isHex = isHexString(address);
58
+
59
+ //this includes the case where the address is both base58 and hex
60
+ //but nearly every address generated this way is hex (only 1/(~5.5e12) are b58)
61
+ if (_isHex) {
62
+ return bigintToAddress(BigInt(`0x${address}`));
63
+ }
64
+
65
+ if (_isBase58) {
66
+ return bigintToAddress(
67
+ BigInt(`0x${Buffer.from(base58.decode(address)).toString('hex')}`),
68
+ );
69
+ }
70
+
71
+ //this is the case where the address is invalid in any encoding
72
+ throw new Error(`The address ${address} cannot be decoded as TON, hex, or base58`);
73
+ }
74
+ } catch (err: any) {
75
+ console.error('Error parsing address', {
76
+ address,
77
+ error: err?.message ?? err,
78
+ });
79
+ throw err;
80
+ }
81
+ };
82
+
83
+ const _addressToNotPaddedHex = (address: AddressTypeLike) => {
84
+ return `0x${parseTonAddress(address).hash.toString('hex')}`;
85
+ };
86
+
87
+ export const addressToHex = (address: AddressTypeLike): HexString => {
88
+ return `0x${to32ByteBuffer(_addressToNotPaddedHex(address)).toString('hex').toLowerCase()}`;
89
+ };
90
+
91
+ export const addressToBigInt = (address: AddressTypeLike) => {
92
+ return BigInt(_addressToNotPaddedHex(address));
93
+ };
94
+
95
+ export const compareAddresses = (addressA: AddressTypeLike, addressB: AddressTypeLike) => {
96
+ return parseTonAddress(addressA).equals(parseTonAddress(addressB));
97
+ };
98
+
99
+ /**
100
+ * Since toString(2) does not include leading zeros, we can calculate the leading zeros by subtracting
101
+ * the total length of address in binary from the total length of addresses in TON in bits (32 bytes * 8)
102
+ */
103
+ export const numberOfLeadingZeroBitsInAddress = (address: AddressTypeLike) => {
104
+ return 32 * 8 - addressToBigInt(address).toString(2).length;
105
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "extends": "@layerzerolabs/typescript-configuration/tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "./dist",
6
+ "strictPropertyInitialization": false,
7
+ "noUnusedLocals": false,
8
+ "noUnusedParameters": false,
9
+ "jsx": "react-jsx"
10
+ },
11
+ "exclude": [
12
+ "node_modules",
13
+ "**/__mocks__/*",
14
+ "**/__tests__/*",
15
+ "**/*.spec.ts",
16
+ "**/*.test.ts",
17
+ "dist"
18
+ ],
19
+ "include": ["src/**/*"]
20
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ import { createPackageTsupConfig } from '@layerzerolabs/tsup-configuration';
4
+
5
+ export default defineConfig(({ watch }) => ({
6
+ ...createPackageTsupConfig(),
7
+ clean: !watch,
8
+ }));