@chainflip/utils 2.2.0-alpha.1 → 2.2.0-alpha.3

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/dist/tron.cjs CHANGED
@@ -1,5 +1,10 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
2
3
  const require_base58 = require('./base58.cjs');
4
+ let _noble_hashes_sha2_js = require("@noble/hashes/sha2.js");
5
+ let _noble_hashes_utils_js = require("@noble/hashes/utils.js");
6
+ let assert = require("assert");
7
+ assert = require_runtime.__toESM(assert, 1);
3
8
 
4
9
  //#region src/tron.ts
5
10
  const isValidTronAddress = (address) => {
@@ -10,6 +15,25 @@ const isValidTronAddress = (address) => {
10
15
  return false;
11
16
  }
12
17
  };
18
+ const tronAddressToHex = (address) => {
19
+ const bytes = require_base58.decode(address);
20
+ (0, assert.default)(bytes.length === 25, "Invalid tron address length");
21
+ return (0, _noble_hashes_utils_js.bytesToHex)(bytes.slice(1, 21));
22
+ };
23
+ const hexToTronAddress = (hex) => {
24
+ const bytes = (0, _noble_hashes_utils_js.hexToBytes)(hex.startsWith("0x") ? hex.slice(2) : hex);
25
+ (0, assert.default)(bytes.length === 20, "Invalid hex length");
26
+ const payload = new Uint8Array(21);
27
+ payload[0] = 65;
28
+ payload.set(bytes, 1);
29
+ const checksum = (0, _noble_hashes_sha2_js.sha256)((0, _noble_hashes_sha2_js.sha256)(payload)).slice(0, 4);
30
+ const addressBytes = new Uint8Array(25);
31
+ addressBytes.set(payload);
32
+ addressBytes.set(checksum, 21);
33
+ return require_base58.encode(addressBytes);
34
+ };
13
35
 
14
36
  //#endregion
15
- exports.isValidTronAddress = isValidTronAddress;
37
+ exports.hexToTronAddress = hexToTronAddress;
38
+ exports.isValidTronAddress = isValidTronAddress;
39
+ exports.tronAddressToHex = tronAddressToHex;
package/dist/tron.d.cts CHANGED
@@ -1,4 +1,6 @@
1
1
  //#region src/tron.d.ts
2
2
  declare const isValidTronAddress: (address: string) => boolean;
3
+ declare const tronAddressToHex: (address: string) => string;
4
+ declare const hexToTronAddress: (hex: string) => string;
3
5
  //#endregion
4
- export { isValidTronAddress };
6
+ export { hexToTronAddress, isValidTronAddress, tronAddressToHex };
package/dist/tron.d.mts CHANGED
@@ -1,4 +1,6 @@
1
1
  //#region src/tron.d.ts
2
2
  declare const isValidTronAddress: (address: string) => boolean;
3
+ declare const tronAddressToHex: (address: string) => string;
4
+ declare const hexToTronAddress: (hex: string) => string;
3
5
  //#endregion
4
- export { isValidTronAddress };
6
+ export { hexToTronAddress, isValidTronAddress, tronAddressToHex };
package/dist/tron.mjs CHANGED
@@ -1,4 +1,7 @@
1
- import { decode } from "./base58.mjs";
1
+ import { decode, encode } from "./base58.mjs";
2
+ import { sha256 } from "@noble/hashes/sha2.js";
3
+ import { bytesToHex, hexToBytes } from "@noble/hashes/utils.js";
4
+ import assert from "assert";
2
5
 
3
6
  //#region src/tron.ts
4
7
  const isValidTronAddress = (address) => {
@@ -9,6 +12,23 @@ const isValidTronAddress = (address) => {
9
12
  return false;
10
13
  }
11
14
  };
15
+ const tronAddressToHex = (address) => {
16
+ const bytes = decode(address);
17
+ assert(bytes.length === 25, "Invalid tron address length");
18
+ return bytesToHex(bytes.slice(1, 21));
19
+ };
20
+ const hexToTronAddress = (hex) => {
21
+ const bytes = hexToBytes(hex.startsWith("0x") ? hex.slice(2) : hex);
22
+ assert(bytes.length === 20, "Invalid hex length");
23
+ const payload = new Uint8Array(21);
24
+ payload[0] = 65;
25
+ payload.set(bytes, 1);
26
+ const checksum = sha256(sha256(payload)).slice(0, 4);
27
+ const addressBytes = new Uint8Array(25);
28
+ addressBytes.set(payload);
29
+ addressBytes.set(checksum, 21);
30
+ return encode(addressBytes);
31
+ };
12
32
 
13
33
  //#endregion
14
- export { isValidTronAddress };
34
+ export { hexToTronAddress, isValidTronAddress, tronAddressToHex };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainflip/utils",
3
- "version": "2.2.0-alpha.1",
3
+ "version": "2.2.0-alpha.3",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",