@d9-network/spec 1.0.2 → 1.2.2

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,3 @@
1
+ *
2
+ !.gitignore
3
+ !package.json
@@ -0,0 +1,24 @@
1
+ {
2
+ "version": "0.1.0-autogenerated.7179878986362617971",
3
+ "name": "@polkadot-api/descriptors",
4
+ "files": [
5
+ "dist"
6
+ ],
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "module": "./dist/index.mjs",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "main": "./dist/index.js",
17
+ "module": "./dist/index.mjs",
18
+ "browser": "./dist/index.mjs",
19
+ "types": "./dist/index.d.ts",
20
+ "sideEffects": false,
21
+ "peerDependencies": {
22
+ "polkadot-api": ">=1.21.0"
23
+ }
24
+ }
package/README.md CHANGED
@@ -63,6 +63,18 @@ if (result.success) {
63
63
  | `parseAmount` | Parse balance string to bigint |
64
64
  | `isValidAddress` | Validate SS58 address |
65
65
 
66
+ ### Byte Utilities
67
+
68
+ | Export | Description |
69
+ | ------------------- | ------------------------------------- |
70
+ | `hexToBytes` | Convert hex string to Uint8Array |
71
+ | `bytesToHex` | Convert Uint8Array to hex string |
72
+ | `concatBytes` | Concatenate multiple Uint8Arrays |
73
+ | `bytesEqual` | Compare two Uint8Arrays |
74
+ | `extractSelector` | Extract 4-byte selector from calldata |
75
+ | `extractSelectorHex`| Extract selector as hex string |
76
+ | `normalizeHex` | Normalize hex string (0x prefix, lowercase) |
77
+
66
78
  ### Constants
67
79
 
68
80
  | Export | Description |
package/dist/index.cjs CHANGED
@@ -10629,6 +10629,112 @@ function formatBlockDuration(blocks, options = {}) {
10629
10629
  return parts.slice(0, maxUnits).join(short ? " " : ", ");
10630
10630
  }
10631
10631
 
10632
+ //#endregion
10633
+ //#region src/utils/bytes.ts
10634
+ /**
10635
+ * Concatenate multiple Uint8Arrays into one
10636
+ *
10637
+ * @param arrays - Arrays to concatenate
10638
+ * @returns Combined Uint8Array
10639
+ *
10640
+ * @example
10641
+ * ```ts
10642
+ * const selector = hexToBytes("0xdb20f9f5");
10643
+ * const args = encodeArgs(...);
10644
+ * const callData = concatBytes(selector, args);
10645
+ * ```
10646
+ */
10647
+ function concatBytes(...arrays) {
10648
+ const totalLength = arrays.reduce((sum, arr$1) => sum + arr$1.length, 0);
10649
+ const result = new Uint8Array(totalLength);
10650
+ let offset = 0;
10651
+ for (const arr$1 of arrays) {
10652
+ result.set(arr$1, offset);
10653
+ offset += arr$1.length;
10654
+ }
10655
+ return result;
10656
+ }
10657
+ /**
10658
+ * Compare two Uint8Arrays for equality
10659
+ *
10660
+ * @param a - First array
10661
+ * @param b - Second array
10662
+ * @returns True if arrays are equal
10663
+ */
10664
+ function bytesEqual(a, b) {
10665
+ if (a.length !== b.length) return false;
10666
+ for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
10667
+ return true;
10668
+ }
10669
+ /**
10670
+ * Extract a slice from a Uint8Array with bounds checking
10671
+ *
10672
+ * @param data - Source array
10673
+ * @param start - Start index
10674
+ * @param length - Optional length (defaults to rest of array)
10675
+ * @returns Sliced array
10676
+ * @throws If indices are out of bounds
10677
+ */
10678
+ function safeSlice(data, start, length) {
10679
+ if (start < 0 || start > data.length) throw new RangeError(`Start index ${start} out of bounds (0-${data.length})`);
10680
+ const end = length !== void 0 ? start + length : data.length;
10681
+ if (end > data.length) throw new RangeError(`End index ${end} exceeds array length ${data.length}`);
10682
+ return data.slice(start, end);
10683
+ }
10684
+ /**
10685
+ * Extract selector (first 4 bytes) from call data
10686
+ *
10687
+ * @param callData - The call data (Uint8Array or hex string)
10688
+ * @returns 4-byte selector
10689
+ * @throws If data is less than 4 bytes
10690
+ *
10691
+ * @example
10692
+ * ```ts
10693
+ * const selector = extractSelector(callData);
10694
+ * const selectorHex = bytesToHex(selector); // "0xdb20f9f5"
10695
+ * ```
10696
+ */
10697
+ function extractSelector(callData) {
10698
+ const bytes = callData instanceof Uint8Array ? callData : hexToBytes(callData);
10699
+ if (bytes.length < 4) throw new Error(`Call data too short (${bytes.length} bytes), need at least 4 bytes for selector`);
10700
+ return bytes.slice(0, 4);
10701
+ }
10702
+ /**
10703
+ * Extract selector as hex string from call data
10704
+ *
10705
+ * @param callData - The call data (Uint8Array or hex string)
10706
+ * @returns Hex string without 0x prefix (e.g., "db20f9f5")
10707
+ *
10708
+ * @example
10709
+ * ```ts
10710
+ * const selectorHex = extractSelectorHex(callData);
10711
+ * if (selectorHex === "db20f9f5") {
10712
+ * // PSP22::transfer
10713
+ * }
10714
+ * ```
10715
+ */
10716
+ function extractSelectorHex(callData) {
10717
+ return bytesToHex(extractSelector(callData)).slice(2);
10718
+ }
10719
+ /**
10720
+ * Normalize a hex string (ensure 0x prefix, lowercase)
10721
+ *
10722
+ * @param hex - Hex string with or without 0x prefix
10723
+ * @returns Normalized hex string with 0x prefix
10724
+ */
10725
+ function normalizeHex(hex$1) {
10726
+ return `0x${(hex$1.startsWith("0x") ? hex$1.slice(2) : hex$1).toLowerCase()}`;
10727
+ }
10728
+ /**
10729
+ * Convert a selector string (with or without 0x) to Uint8Array
10730
+ *
10731
+ * @param selector - Selector as hex string
10732
+ * @returns 4-byte Uint8Array
10733
+ */
10734
+ function selectorToBytes(selector) {
10735
+ return hexToBytes(normalizeHex(selector));
10736
+ }
10737
+
10632
10738
  //#endregion
10633
10739
  exports.AbortedError = AbortedError;
10634
10740
  exports.ArithmeticError = ArithmeticError;
@@ -10670,7 +10776,9 @@ exports.USDT_TOKEN = USDT_TOKEN;
10670
10776
  exports.addressesEqual = addressesEqual;
10671
10777
  exports.blocksToMs = blocksToMs;
10672
10778
  exports.blocksUntil = blocksUntil;
10779
+ exports.bytesEqual = bytesEqual;
10673
10780
  exports.bytesToHex = bytesToHex;
10781
+ exports.concatBytes = concatBytes;
10674
10782
  exports.contracts = contracts_exports;
10675
10783
  exports.createAccountFromMnemonic = createAccountFromMnemonic;
10676
10784
  exports.createAccountFromPrivateKey = createAccountFromPrivateKey;
@@ -10678,6 +10786,8 @@ exports.createSr25519SignerFromKeypair = createSr25519SignerFromKeypair;
10678
10786
  exports.d9 = d9_default;
10679
10787
  exports.estimateBlockAtTime = estimateBlockAtTime;
10680
10788
  exports.estimateTimeAtBlock = estimateTimeAtBlock;
10789
+ exports.extractSelector = extractSelector;
10790
+ exports.extractSelectorHex = extractSelectorHex;
10681
10791
  exports.formatBalance = formatBalance;
10682
10792
  exports.formatBlockDuration = formatBlockDuration;
10683
10793
  exports.formatTokenAmount = formatTokenAmount;
@@ -10693,7 +10803,10 @@ exports.isValidAddressForNetwork = isValidAddressForNetwork;
10693
10803
  exports.isValidD9Address = isValidD9Address;
10694
10804
  exports.mnemonicToMiniSecret = mnemonicToMiniSecret;
10695
10805
  exports.msToBlocks = msToBlocks;
10806
+ exports.normalizeHex = normalizeHex;
10696
10807
  exports.parseAmount = parseAmount;
10808
+ exports.safeSlice = safeSlice;
10809
+ exports.selectorToBytes = selectorToBytes;
10697
10810
  exports.sr25519AddressFromKeypair = sr25519AddressFromKeypair;
10698
10811
  exports.sr25519AddressFromSecretKeyHex = sr25519AddressFromSecretKeyHex;
10699
10812
  exports.sr25519DeriveFromMiniSecret = sr25519DeriveFromMiniSecret;