@layerzerolabs/common-encoding-utils 0.2.64 → 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,3 @@
1
+ export { ByteCodec } from './2BTCXNB3.js';
2
+ //# sourceMappingURL=byte-codec.js.map
3
+ //# sourceMappingURL=byte-codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"byte-codec.js"}
package/dist/index.cjs CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var __defProp = Object.defineProperty;
4
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ var DF2CBFKF_cjs = require('./DF2CBFKF.cjs');
5
4
 
6
5
  // src/index.ts
7
6
  var base58regex = /^[A-HJ-NP-Za-km-z1-9]*$/;
@@ -10,11 +9,11 @@ var hexStringRegex = /^(0x)?[0-9A-Fa-f]*$/;
10
9
  function isBase58(address) {
11
10
  return base58regex.test(address);
12
11
  }
13
- __name(isBase58, "isBase58");
12
+ DF2CBFKF_cjs.__name(isBase58, "isBase58");
14
13
  function isBase64(address) {
15
14
  return base64regex.test(address);
16
15
  }
17
- __name(isBase64, "isBase64");
16
+ DF2CBFKF_cjs.__name(isBase64, "isBase64");
18
17
  function isHexString(value, length) {
19
18
  if (typeof value !== "string" || !value.match(hexStringRegex)) {
20
19
  return false;
@@ -24,7 +23,7 @@ function isHexString(value, length) {
24
23
  }
25
24
  return true;
26
25
  }
27
- __name(isHexString, "isHexString");
26
+ DF2CBFKF_cjs.__name(isHexString, "isHexString");
28
27
  function hexZeroPad(value, length) {
29
28
  if (!isHexString(value)) {
30
29
  throw new Error(`invalid hex string: ${value}`);
@@ -34,25 +33,25 @@ function hexZeroPad(value, length) {
34
33
  }
35
34
  return `0x${trim0x(value).padStart(2 * length, "0")}`;
36
35
  }
37
- __name(hexZeroPad, "hexZeroPad");
36
+ DF2CBFKF_cjs.__name(hexZeroPad, "hexZeroPad");
38
37
  function trim0x(str) {
39
38
  return str.replace(/^0x/, "");
40
39
  }
41
- __name(trim0x, "trim0x");
40
+ DF2CBFKF_cjs.__name(trim0x, "trim0x");
42
41
  function ensure0xPrefixed(str) {
43
42
  return `0x${trim0x(str)}`;
44
43
  }
45
- __name(ensure0xPrefixed, "ensure0xPrefixed");
44
+ DF2CBFKF_cjs.__name(ensure0xPrefixed, "ensure0xPrefixed");
46
45
  function trimLeadingZeros(hexString) {
47
46
  let withoutPrefix = trim0x(hexString);
48
47
  withoutPrefix = withoutPrefix.replace(/^0+/, "");
49
48
  return ensure0xPrefixed(withoutPrefix);
50
49
  }
51
- __name(trimLeadingZeros, "trimLeadingZeros");
50
+ DF2CBFKF_cjs.__name(trimLeadingZeros, "trimLeadingZeros");
52
51
  function hexToUint8Array(hexString) {
53
52
  return Uint8Array.from(Buffer.from(trim0x(hexString), "hex"));
54
53
  }
55
- __name(hexToUint8Array, "hexToUint8Array");
54
+ DF2CBFKF_cjs.__name(hexToUint8Array, "hexToUint8Array");
56
55
  function hexToAscii(hex) {
57
56
  if (!isHexString(hex)) {
58
57
  throw new Error(`Invalid hex string ${hex}`);
@@ -69,31 +68,35 @@ function hexToAscii(hex) {
69
68
  }
70
69
  return str;
71
70
  }
72
- __name(hexToAscii, "hexToAscii");
71
+ DF2CBFKF_cjs.__name(hexToAscii, "hexToAscii");
73
72
  function stringToUint8Array(str) {
74
73
  const value = str.replace(/^0x/i, "");
75
74
  const len = value.length + 1 - (value.length + 1) % 2;
76
75
  return Uint8Array.from(Buffer.from(value.padStart(len, "0"), "hex"));
77
76
  }
78
- __name(stringToUint8Array, "stringToUint8Array");
77
+ DF2CBFKF_cjs.__name(stringToUint8Array, "stringToUint8Array");
79
78
  function hexToBytes(hex) {
80
79
  const value = hex.replace(/^0x/i, "");
81
80
  const len = value.length + 1 - (value.length + 1) % 2;
82
81
  return Uint8Array.from(Buffer.from(value.padStart(len, "0"), "hex"));
83
82
  }
84
- __name(hexToBytes, "hexToBytes");
83
+ DF2CBFKF_cjs.__name(hexToBytes, "hexToBytes");
85
84
  function bytesToHex(bytes) {
86
- return Buffer.from(bytes).toString("hex");
85
+ return Buffer.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).toString("hex");
87
86
  }
88
- __name(bytesToHex, "bytesToHex");
87
+ DF2CBFKF_cjs.__name(bytesToHex, "bytesToHex");
89
88
  function bytesToHexPrefixed(bytes) {
90
89
  return ensure0xPrefixed(bytesToHex(bytes));
91
90
  }
92
- __name(bytesToHexPrefixed, "bytesToHexPrefixed");
91
+ DF2CBFKF_cjs.__name(bytesToHexPrefixed, "bytesToHexPrefixed");
93
92
  function bytesToBase64(bytes) {
94
- return Buffer.from(bytes).toString("base64");
93
+ return Buffer.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).toString("base64");
95
94
  }
96
- __name(bytesToBase64, "bytesToBase64");
95
+ DF2CBFKF_cjs.__name(bytesToBase64, "bytesToBase64");
96
+ function base64ToBytes(base64) {
97
+ return Uint8Array.from(Buffer.from(base64, "base64"));
98
+ }
99
+ DF2CBFKF_cjs.__name(base64ToBytes, "base64ToBytes");
97
100
  function hexToBase64(hexString) {
98
101
  const hex = trim0x(hexString);
99
102
  if (!/^[0-9a-fA-F]+$/.test(hex)) {
@@ -109,14 +112,14 @@ function hexToBase64(hexString) {
109
112
  const base64String = Buffer.from(bytes).toString("base64");
110
113
  return base64String;
111
114
  }
112
- __name(hexToBase64, "hexToBase64");
115
+ DF2CBFKF_cjs.__name(hexToBase64, "hexToBase64");
113
116
  function base64ToHex(base64String) {
114
117
  if (!/^[A-Za-z0-9+/=]*$/.test(base64String)) {
115
118
  throw new Error(`Invalid base64 string: ${base64String}`);
116
119
  }
117
120
  return `0x${Buffer.from(base64String, "base64").toString("hex")}`;
118
121
  }
119
- __name(base64ToHex, "base64ToHex");
122
+ DF2CBFKF_cjs.__name(base64ToHex, "base64ToHex");
120
123
  function padString(str, length, left, padding = "0") {
121
124
  const diff = length - str.length;
122
125
  let result = str;
@@ -126,23 +129,28 @@ function padString(str, length, left, padding = "0") {
126
129
  }
127
130
  return result;
128
131
  }
129
- __name(padString, "padString");
132
+ DF2CBFKF_cjs.__name(padString, "padString");
130
133
  function padLeft(str, length, padding = "0") {
131
134
  return padString(str, length, true, padding);
132
135
  }
133
- __name(padLeft, "padLeft");
136
+ DF2CBFKF_cjs.__name(padLeft, "padLeft");
134
137
  function calcByteLength(str, byteSize = 8) {
135
138
  const { length } = str;
136
139
  const remainder = length % byteSize;
137
140
  return remainder ? (length - remainder) / byteSize * byteSize + byteSize : length;
138
141
  }
139
- __name(calcByteLength, "calcByteLength");
142
+ DF2CBFKF_cjs.__name(calcByteLength, "calcByteLength");
140
143
  function padAlignHex(str, byteSize = 8, padding = "0") {
141
144
  const trimmed = trim0x(str);
142
145
  return padLeft(trimmed, calcByteLength(trimmed, byteSize), padding);
143
146
  }
144
- __name(padAlignHex, "padAlignHex");
147
+ DF2CBFKF_cjs.__name(padAlignHex, "padAlignHex");
145
148
 
149
+ Object.defineProperty(exports, "ByteCodec", {
150
+ enumerable: true,
151
+ get: function () { return DF2CBFKF_cjs.ByteCodec; }
152
+ });
153
+ exports.base64ToBytes = base64ToBytes;
146
154
  exports.base64ToHex = base64ToHex;
147
155
  exports.bytesToBase64 = bytesToBase64;
148
156
  exports.bytesToHex = bytesToHex;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":["base58regex","base64regex","hexStringRegex","isBase58","address","test","isBase64","isHexString","value","length","match","hexZeroPad","Error","trim0x","padStart","str","replace","ensure0xPrefixed","trimLeadingZeros","hexString","withoutPrefix","hexToUint8Array","Uint8Array","from","Buffer","hexToAscii","hex","i","l","startsWith","code","parseInt","slice","String","fromCharCode","stringToUint8Array","len","hexToBytes","bytesToHex","bytes","toString","bytesToHexPrefixed","bytesToBase64","hexToBase64","substr","base64String","base64ToHex","padString","left","padding","diff","result","pad","repeat","padLeft","calcByteLength","byteSize","remainder","pad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D,MAAAA;AACX;AARSJ,MAAAA,CAAAA,SAAAA,EAAAA,WAAAA,CAAAA;AAUT,SAASO,OAAAA,CAAQvC,GAAAA,EAAaN,MAAAA,EAAgBwC,OAAAA,GAAU,GAAA,EAAG;AACvD,EAAA,OAAOF,SAAAA,CAAUhC,GAAAA,EAAKN,MAAAA,EAAQ,IAAA,EAAMwC,OAAAA,CAAAA;AACxC;AAFSK,MAAAA,CAAAA,OAAAA,EAAAA,SAAAA,CAAAA;AAGT,SAASC,cAAAA,CAAexC,GAAAA,EAAayC,QAAAA,GAAW,CAAA,EAAC;AAC7C,EAAA,MAAM,EAAE/C,QAAM,GAAKM,GAAAA;AACnB,EAAA,MAAM0C,YAAYhD,MAAAA,GAAS+C,QAAAA;AAC3B,EAAA,OAAOC,SAAAA,GAAAA,CAAchD,MAAAA,GAASgD,SAAAA,IAAaD,QAAAA,GAAYA,WAAWA,QAAAA,GAAW/C,MAAAA;AACjF;AAJS8C,MAAAA,CAAAA,cAAAA,EAAAA,gBAAAA,CAAAA;AAcF,SAASG,WAAAA,CAAY3C,GAAAA,EAAayC,QAAAA,GAAW,CAAA,EAAGP,UAAU,GAAA,EAAG;AAChE,EAAA,MAAMU,OAAAA,GAAU9C,OAAOE,GAAAA,CAAAA;AACvB,EAAA,OAAOuC,QAAQK,OAAAA,EAASJ,cAAAA,CAAeI,OAAAA,EAASH,QAAAA,GAAWP,OAAAA,CAAAA;AAC/D;AAHgBS,MAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA","file":"index.cjs","sourcesContent":["export type HexString = `0x${string}`;\n\nconst base58regex = /^[A-HJ-NP-Za-km-z1-9]*$/;\nconst base64regex = /^[A-Za-z0-9+/]*={0,2}$/;\nconst hexStringRegex = /^(0x)?[0-9A-Fa-f]*$/;\n\nexport function isBase58(address: string): boolean {\n return base58regex.test(address);\n}\n\nexport function isBase64(address: string): boolean {\n return base64regex.test(address);\n}\n\nexport function isHexString(value: any, length?: number): value is HexString {\n if (typeof value !== 'string' || !value.match(hexStringRegex)) {\n return false;\n }\n if (length && value.length !== 2 + 2 * length) {\n return false;\n }\n return true;\n}\n\nexport function hexZeroPad(value: string, length: number): HexString {\n if (!isHexString(value)) {\n throw new Error(`invalid hex string: ${value}`);\n }\n\n if (value.length > 2 * length + 2) {\n throw new Error('value out of range');\n }\n\n return `0x${trim0x(value).padStart(2 * length, '0')}`;\n}\n\nexport function trim0x(str: string): string {\n return str.replace(/^0x/, '');\n}\n\nexport function ensure0xPrefixed(str: string): HexString {\n return `0x${trim0x(str)}`;\n}\n\nexport function trimLeadingZeros(hexString: string): HexString {\n // Remove the '0x' prefix\n let withoutPrefix = trim0x(hexString);\n\n // Trim leading zeros\n withoutPrefix = withoutPrefix.replace(/^0+/, '');\n\n // Add back the '0x' prefix\n return ensure0xPrefixed(withoutPrefix);\n}\n\nexport function hexToUint8Array(hexString: string): Uint8Array {\n return Uint8Array.from(Buffer.from(trim0x(hexString), 'hex'));\n}\n\nexport function hexToAscii(hex: string): string {\n if (!isHexString(hex)) {\n throw new Error(`Invalid hex string ${hex}`);\n }\n\n let str = '';\n let i = 0;\n const l = hex.length;\n if (hex.startsWith('0x')) {\n i = 2;\n }\n for (; i < l; i += 2) {\n const code = parseInt(hex.slice(i, i + 2), 16);\n str += String.fromCharCode(code);\n }\n\n return str;\n}\n\nexport function stringToUint8Array(str: string): Uint8Array {\n const value = str.replace(/^0x/i, '');\n const len = value.length + 1 - ((value.length + 1) % 2);\n return Uint8Array.from(Buffer.from(value.padStart(len, '0'), 'hex'));\n}\n\n/**\n * A function to convert hex string to Uint8Array (not prefixed)\n * @param hex hex string\n * @returns Uint8Array\n */\nexport function hexToBytes(hex: string): Uint8Array {\n const value = hex.replace(/^0x/i, '');\n const len = value.length + 1 - ((value.length + 1) % 2);\n return Uint8Array.from(Buffer.from(value.padStart(len, '0'), 'hex'));\n}\n\nexport function bytesToHex(bytes: Uint8Array | ArrayBuffer): string {\n return Buffer.from(bytes).toString('hex');\n}\n\n/**\n * 0x prefixed hex string from Uint8Array\n * @param bytes\n */\nexport function bytesToHexPrefixed(bytes: Uint8Array | ArrayBuffer): HexString {\n return ensure0xPrefixed(bytesToHex(bytes));\n}\n\nexport function bytesToBase64(bytes: Uint8Array | ArrayBuffer): string {\n return Buffer.from(bytes).toString('base64');\n}\n\nexport function hexToBase64(hexString: string): string {\n const hex = trim0x(hexString);\n // Validate that the input is a valid hex string\n if (!/^[0-9a-fA-F]+$/.test(hex)) {\n throw new Error(`Invalid hex string: ${hexString}`);\n }\n\n // Ensure the hex string has an even length\n if (hex.length % 2 !== 0) {\n throw new Error('Hex string must have an even length');\n }\n\n // Convert hex string to a byte array\n const bytes = new Uint8Array(hex.length / 2);\n for (let i = 0; i < hex.length; i += 2) {\n bytes[i / 2] = parseInt(hex.substr(i, 2), 16);\n }\n\n // Convert the byte array to a base64 string\n const base64String = Buffer.from(bytes).toString('base64');\n\n return base64String;\n}\n\nexport function base64ToHex(base64String: string): HexString {\n if (!/^[A-Za-z0-9+/=]*$/.test(base64String)) {\n throw new Error(`Invalid base64 string: ${base64String}`);\n }\n return `0x${Buffer.from(base64String, 'base64').toString('hex')}`;\n}\n\nfunction padString(str: string, length: number, left: boolean, padding = '0') {\n const diff = length - str.length;\n let result = str;\n if (diff > 0) {\n const pad = padding.repeat(diff);\n result = left ? pad + str : str + pad;\n }\n return result;\n}\n\nfunction padLeft(str: string, length: number, padding = '0') {\n return padString(str, length, true, padding);\n}\nfunction calcByteLength(str: string, byteSize = 8) {\n const { length } = str;\n const remainder = length % byteSize;\n return remainder ? ((length - remainder) / byteSize) * byteSize + byteSize : length;\n}\n\n/**\n * Pads a hex string on the left so its length is a multiple of `byteSize`.\n * Used to align hex strings.\n * @param str Hex string.\n * @param byteSize Group size to align to, in hex chars (default: 8).\n * @param padding Left-pad character (default: '0').\n * @returns Hex string left-padded to a multiple of `byteSize`.\n */\nexport function padAlignHex(str: string, byteSize = 8, padding = '0') {\n const trimmed = trim0x(str);\n return padLeft(trimmed, calcByteLength(trimmed, byteSize), padding);\n}\n"]}
1
+ {"version":3,"sources":["../src/index.ts"],"names":["base58regex","base64regex","hexStringRegex","isBase58","address","test","isBase64","isHexString","value","length","match","hexZeroPad","Error","trim0x","padStart","str","replace","ensure0xPrefixed","trimLeadingZeros","hexString","withoutPrefix","hexToUint8Array","Uint8Array","from","Buffer","hexToAscii","hex","i","l","startsWith","code","parseInt","slice","String","fromCharCode","stringToUint8Array","len","hexToBytes","bytesToHex","bytes","ArrayBuffer","toString","bytesToHexPrefixed","bytesToBase64","base64ToBytes","base64","hexToBase64","substr","base64String","base64ToHex","padString","left","padding","diff","result","pad","repeat","padLeft","calcByteLength","byteSize","remainder","padAlignHex","trimmed"],"mappings":";;;;;AAIA,IAAMA,WAAAA,GAAc,yBAAA;AACpB,IAAMC,WAAAA,GAAc,wBAAA;AACpB,IAAMC,cAAAA,GAAiB,qBAAA;AAEhB,SAASC,SAASC,OAAAA,EAAe;AACpC,EAAA,OAAOJ,WAAAA,CAAYK,KAAKD,OAAAA,CAAAA;AAC5B;AAFgBD,mBAAAA,CAAAA,QAAAA,EAAAA,UAAAA,CAAAA;AAIT,SAASG,SAASF,OAAAA,EAAe;AACpC,EAAA,OAAOH,WAAAA,CAAYI,KAAKD,OAAAA,CAAAA;AAC5B;AAFgBE,mBAAAA,CAAAA,QAAAA,EAAAA,UAAAA,CAAAA;AAIT,SAASC,WAAAA,CAAYC,OAAYC,MAAAA,EAAe;AACnD,EAAA,IAAI,OAAOD,KAAAA,KAAU,QAAA,IAAY,CAACA,KAAAA,CAAME,KAAAA,CAAMR,cAAAA,CAAAA,EAAiB;AAC3D,IAAA,OAAO,KAAA;AACX,EAAA;AACA,EAAA,IAAIO,MAAAA,IAAUD,KAAAA,CAAMC,MAAAA,KAAW,CAAA,GAAI,IAAIA,MAAAA,EAAQ;AAC3C,IAAA,OAAO,KAAA;AACX,EAAA;AACA,EAAA,OAAO,IAAA;AACX;AARgBF,mBAAAA,CAAAA,WAAAA,EAA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AAA,MAAM6C,YAAYnD,MAAAA,GAASkD,QAAAA;AAC3B,EAAA,OAAOC,SAAAA,GAAAA,CAAcnD,MAAAA,GAASmD,SAAAA,IAAaD,QAAAA,GAAYA,WAAWA,QAAAA,GAAWlD,MAAAA;AACjF;AAJSiD,mBAAAA,CAAAA,cAAAA,EAAAA,gBAAAA,CAAAA;AAcF,SAASG,WAAAA,CAAY9C,GAAAA,EAAa4C,QAAAA,GAAW,CAAA,EAAGP,UAAU,GAAA,EAAG;AAChE,EAAA,MAAMU,OAAAA,GAAUjD,OAAOE,GAAAA,CAAAA;AACvB,EAAA,OAAO0C,QAAQK,OAAAA,EAASJ,cAAAA,CAAeI,OAAAA,EAASH,QAAAA,GAAWP,OAAAA,CAAAA;AAC/D;AAHgBS,mBAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA","file":"index.cjs","sourcesContent":["export * from './byte-codec';\n\nexport type HexString = `0x${string}`;\n\nconst base58regex = /^[A-HJ-NP-Za-km-z1-9]*$/;\nconst base64regex = /^[A-Za-z0-9+/]*={0,2}$/;\nconst hexStringRegex = /^(0x)?[0-9A-Fa-f]*$/;\n\nexport function isBase58(address: string): boolean {\n return base58regex.test(address);\n}\n\nexport function isBase64(address: string): boolean {\n return base64regex.test(address);\n}\n\nexport function isHexString(value: any, length?: number): value is HexString {\n if (typeof value !== 'string' || !value.match(hexStringRegex)) {\n return false;\n }\n if (length && value.length !== 2 + 2 * length) {\n return false;\n }\n return true;\n}\n\nexport function hexZeroPad(value: string, length: number): HexString {\n if (!isHexString(value)) {\n throw new Error(`invalid hex string: ${value}`);\n }\n\n if (value.length > 2 * length + 2) {\n throw new Error('value out of range');\n }\n\n return `0x${trim0x(value).padStart(2 * length, '0')}`;\n}\n\nexport function trim0x(str: string): string {\n return str.replace(/^0x/, '');\n}\n\nexport function ensure0xPrefixed(str: string): HexString {\n return `0x${trim0x(str)}`;\n}\n\nexport function trimLeadingZeros(hexString: string): HexString {\n // Remove the '0x' prefix\n let withoutPrefix = trim0x(hexString);\n\n // Trim leading zeros\n withoutPrefix = withoutPrefix.replace(/^0+/, '');\n\n // Add back the '0x' prefix\n return ensure0xPrefixed(withoutPrefix);\n}\n\nexport function hexToUint8Array(hexString: string): Uint8Array {\n return Uint8Array.from(Buffer.from(trim0x(hexString), 'hex'));\n}\n\nexport function hexToAscii(hex: string): string {\n if (!isHexString(hex)) {\n throw new Error(`Invalid hex string ${hex}`);\n }\n\n let str = '';\n let i = 0;\n const l = hex.length;\n if (hex.startsWith('0x')) {\n i = 2;\n }\n for (; i < l; i += 2) {\n const code = parseInt(hex.slice(i, i + 2), 16);\n str += String.fromCharCode(code);\n }\n\n return str;\n}\n\nexport function stringToUint8Array(str: string): Uint8Array {\n const value = str.replace(/^0x/i, '');\n const len = value.length + 1 - ((value.length + 1) % 2);\n return Uint8Array.from(Buffer.from(value.padStart(len, '0'), 'hex'));\n}\n\n/**\n * A function to convert hex string to Uint8Array (not prefixed)\n * @param hex hex string\n * @returns Uint8Array\n */\nexport function hexToBytes(hex: string): Uint8Array {\n const value = hex.replace(/^0x/i, '');\n const len = value.length + 1 - ((value.length + 1) % 2);\n return Uint8Array.from(Buffer.from(value.padStart(len, '0'), 'hex'));\n}\n\nexport function bytesToHex(bytes: Uint8Array | ArrayBuffer): string {\n return Buffer.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).toString(\n 'hex',\n );\n}\n\n/**\n * 0x prefixed hex string from Uint8Array\n * @param bytes\n */\nexport function bytesToHexPrefixed(bytes: Uint8Array | ArrayBuffer): HexString {\n return ensure0xPrefixed(bytesToHex(bytes));\n}\n\nexport function bytesToBase64(bytes: Uint8Array | ArrayBuffer): string {\n return Buffer.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).toString(\n 'base64',\n );\n}\n\nexport function base64ToBytes(base64: string): Uint8Array {\n return Uint8Array.from(Buffer.from(base64, 'base64'));\n}\n\nexport function hexToBase64(hexString: string): string {\n const hex = trim0x(hexString);\n // Validate that the input is a valid hex string\n if (!/^[0-9a-fA-F]+$/.test(hex)) {\n throw new Error(`Invalid hex string: ${hexString}`);\n }\n\n // Ensure the hex string has an even length\n if (hex.length % 2 !== 0) {\n throw new Error('Hex string must have an even length');\n }\n\n // Convert hex string to a byte array\n const bytes = new Uint8Array(hex.length / 2);\n for (let i = 0; i < hex.length; i += 2) {\n bytes[i / 2] = parseInt(hex.substr(i, 2), 16);\n }\n\n // Convert the byte array to a base64 string\n const base64String = Buffer.from(bytes).toString('base64');\n\n return base64String;\n}\n\nexport function base64ToHex(base64String: string): HexString {\n if (!/^[A-Za-z0-9+/=]*$/.test(base64String)) {\n throw new Error(`Invalid base64 string: ${base64String}`);\n }\n return `0x${Buffer.from(base64String, 'base64').toString('hex')}`;\n}\n\nfunction padString(str: string, length: number, left: boolean, padding = '0') {\n const diff = length - str.length;\n let result = str;\n if (diff > 0) {\n const pad = padding.repeat(diff);\n result = left ? pad + str : str + pad;\n }\n return result;\n}\n\nfunction padLeft(str: string, length: number, padding = '0') {\n return padString(str, length, true, padding);\n}\nfunction calcByteLength(str: string, byteSize = 8) {\n const { length } = str;\n const remainder = length % byteSize;\n return remainder ? ((length - remainder) / byteSize) * byteSize + byteSize : length;\n}\n\n/**\n * Pads a hex string on the left so its length is a multiple of `byteSize`.\n * Used to align hex strings.\n * @param str Hex string.\n * @param byteSize Group size to align to, in hex chars (default: 8).\n * @param padding Left-pad character (default: '0').\n * @returns Hex string left-padded to a multiple of `byteSize`.\n */\nexport function padAlignHex(str: string, byteSize = 8, padding = '0') {\n const trimmed = trim0x(str);\n return padLeft(trimmed, calcByteLength(trimmed, byteSize), padding);\n}\n"]}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './byte-codec';
1
2
  export type HexString = `0x${string}`;
2
3
  export declare function isBase58(address: string): boolean;
3
4
  export declare function isBase64(address: string): boolean;
@@ -22,6 +23,7 @@ export declare function bytesToHex(bytes: Uint8Array | ArrayBuffer): string;
22
23
  */
23
24
  export declare function bytesToHexPrefixed(bytes: Uint8Array | ArrayBuffer): HexString;
24
25
  export declare function bytesToBase64(bytes: Uint8Array | ArrayBuffer): string;
26
+ export declare function base64ToBytes(base64: string): Uint8Array;
25
27
  export declare function hexToBase64(hexString: string): string;
26
28
  export declare function base64ToHex(base64String: string): HexString;
27
29
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,KAAK,MAAM,EAAE,CAAC;AAMtC,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAEjD;AAED,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAEjD;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,KAAK,IAAI,SAAS,CAQ3E;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS,CAUnE;AAED,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE1C;AAED,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAEvD;AAED,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,CAS7D;AAED,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,UAAU,CAE7D;AAED,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAiB9C;AAED,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAI1D;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAIlD;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,GAAG,MAAM,CAElE;AAED;;;GAGG;AACH,wBAAgB,kBA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IG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAIlD;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,GAAG,MAAM,CAIlE;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,GAAG,SAAS,CAE7E;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,GAAG,MAAM,CAIrE;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAExD;AAED,wBAAgB,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAsBrD;AAED,wBAAgB,WAAW,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,CAK3D;AAqBD;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,SAAI,EAAE,OAAO,SAAM,UAGnE"}
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
1
+ import { __name } from './2BTCXNB3.js';
2
+ export { ByteCodec } from './2BTCXNB3.js';
3
3
 
4
4
  // src/index.ts
5
5
  var base58regex = /^[A-HJ-NP-Za-km-z1-9]*$/;
@@ -81,7 +81,7 @@ function hexToBytes(hex) {
81
81
  }
82
82
  __name(hexToBytes, "hexToBytes");
83
83
  function bytesToHex(bytes) {
84
- return Buffer.from(bytes).toString("hex");
84
+ return Buffer.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).toString("hex");
85
85
  }
86
86
  __name(bytesToHex, "bytesToHex");
87
87
  function bytesToHexPrefixed(bytes) {
@@ -89,9 +89,13 @@ function bytesToHexPrefixed(bytes) {
89
89
  }
90
90
  __name(bytesToHexPrefixed, "bytesToHexPrefixed");
91
91
  function bytesToBase64(bytes) {
92
- return Buffer.from(bytes).toString("base64");
92
+ return Buffer.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).toString("base64");
93
93
  }
94
94
  __name(bytesToBase64, "bytesToBase64");
95
+ function base64ToBytes(base64) {
96
+ return Uint8Array.from(Buffer.from(base64, "base64"));
97
+ }
98
+ __name(base64ToBytes, "base64ToBytes");
95
99
  function hexToBase64(hexString) {
96
100
  const hex = trim0x(hexString);
97
101
  if (!/^[0-9a-fA-F]+$/.test(hex)) {
@@ -141,6 +145,6 @@ function padAlignHex(str, byteSize = 8, padding = "0") {
141
145
  }
142
146
  __name(padAlignHex, "padAlignHex");
143
147
 
144
- export { base64ToHex, bytesToBase64, bytesToHex, bytesToHexPrefixed, ensure0xPrefixed, hexToAscii, hexToBase64, hexToBytes, hexToUint8Array, hexZeroPad, isBase58, isBase64, isHexString, padAlignHex, stringToUint8Array, trim0x, trimLeadingZeros };
148
+ export { base64ToBytes, base64ToHex, bytesToBase64, bytesToHex, bytesToHexPrefixed, ensure0xPrefixed, hexToAscii, hexToBase64, hexToBytes, hexToUint8Array, hexZeroPad, isBase58, isBase64, isHexString, padAlignHex, stringToUint8Array, trim0x, trimLeadingZeros };
145
149
  //# sourceMappingURL=index.js.map
146
150
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":["base58regex","base64regex","hexStringRegex","isBase58","address","test","isBase64","isHexString","value","length","match","hexZeroPad","Error","trim0x","padStart","str","replace","ensure0xPrefixed","trimLeadingZeros","hexString","withoutPrefix","hexToUint8Array","Uint8Array","from","Buffer","hexToAscii","hex","i","l","startsWith","code","parseInt","slice","String","fromCharCode","stringToUint8Array","len","hexToBytes","bytesToHex","bytes","toString","bytesToHexPrefixed","bytesToBase64","hexToBase64","substr","base64String","base64ToHex","padString","left","padding","diff","result","pad","repeat","padLeft","calcByteLength","byteSize","remainder","padAlignHex","trimmed"],"mappings":";;;;AAEA,IAAMA,WAAAA,GAAc,yBAAA;AACpB,IAAMC,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O,OAAAA,CAAQvC,GAAAA,EAAaN,MAAAA,EAAgBwC,OAAAA,GAAU,GAAA,EAAG;AACvD,EAAA,OAAOF,SAAAA,CAAUhC,GAAAA,EAAKN,MAAAA,EAAQ,IAAA,EAAMwC,OAAAA,CAAAA;AACxC;AAFSK,MAAAA,CAAAA,OAAAA,EAAAA,SAAAA,CAAAA;AAGT,SAASC,cAAAA,CAAexC,GAAAA,EAAayC,QAAAA,GAAW,CAAA,EAAC;AAC7C,EAAA,MAAM,EAAE/C,QAAM,GAAKM,GAAAA;AACnB,EAAA,MAAM0C,YAAYhD,MAAAA,GAAS+C,QAAAA;AAC3B,EAAA,OAAOC,SAAAA,GAAAA,CAAchD,MAAAA,GAASgD,SAAAA,IAAaD,QAAAA,GAAYA,WAAWA,QAAAA,GAAW/C,MAAAA;AACjF;AAJS8C,MAAAA,CAAAA,cAAAA,EAAAA,gBAAAA,CAAAA;AAcF,SAASG,WAAAA,CAAY3C,GAAAA,EAAayC,QAAAA,GAAW,CAAA,EAAGP,UAAU,GAAA,EAAG;AAChE,EAAA,MAAMU,OAAAA,GAAU9C,OAAOE,GAAAA,CAAAA;AACvB,EAAA,OAAOuC,QAAQK,OAAAA,EAASJ,cAAAA,CAAeI,OAAAA,EAASH,QAAAA,GAAWP,OAAAA,CAAAA;AAC/D;AAHgBS,MAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA","file":"index.js","sourcesContent":["export type HexString = `0x${string}`;\n\nconst base58regex = /^[A-HJ-NP-Za-km-z1-9]*$/;\nconst base64regex = /^[A-Za-z0-9+/]*={0,2}$/;\nconst hexStringRegex = /^(0x)?[0-9A-Fa-f]*$/;\n\nexport function isBase58(address: string): boolean {\n return base58regex.test(address);\n}\n\nexport function isBase64(address: string): boolean {\n return base64regex.test(address);\n}\n\nexport function isHexString(value: any, length?: number): value is HexString {\n if (typeof value !== 'string' || !value.match(hexStringRegex)) {\n return false;\n }\n if (length && value.length !== 2 + 2 * length) {\n return false;\n }\n return true;\n}\n\nexport function hexZeroPad(value: string, length: number): HexString {\n if (!isHexString(value)) {\n throw new Error(`invalid hex string: ${value}`);\n }\n\n if (value.length > 2 * length + 2) {\n throw new Error('value out of range');\n }\n\n return `0x${trim0x(value).padStart(2 * length, '0')}`;\n}\n\nexport function trim0x(str: string): string {\n return str.replace(/^0x/, '');\n}\n\nexport function ensure0xPrefixed(str: string): HexString {\n return `0x${trim0x(str)}`;\n}\n\nexport function trimLeadingZeros(hexString: string): HexString {\n // Remove the '0x' prefix\n let withoutPrefix = trim0x(hexString);\n\n // Trim leading zeros\n withoutPrefix = withoutPrefix.replace(/^0+/, '');\n\n // Add back the '0x' prefix\n return ensure0xPrefixed(withoutPrefix);\n}\n\nexport function hexToUint8Array(hexString: string): Uint8Array {\n return Uint8Array.from(Buffer.from(trim0x(hexString), 'hex'));\n}\n\nexport function hexToAscii(hex: string): string {\n if (!isHexString(hex)) {\n throw new Error(`Invalid hex string ${hex}`);\n }\n\n let str = '';\n let i = 0;\n const l = hex.length;\n if (hex.startsWith('0x')) {\n i = 2;\n }\n for (; i < l; i += 2) {\n const code = parseInt(hex.slice(i, i + 2), 16);\n str += String.fromCharCode(code);\n }\n\n return str;\n}\n\nexport function stringToUint8Array(str: string): Uint8Array {\n const value = str.replace(/^0x/i, '');\n const len = value.length + 1 - ((value.length + 1) % 2);\n return Uint8Array.from(Buffer.from(value.padStart(len, '0'), 'hex'));\n}\n\n/**\n * A function to convert hex string to Uint8Array (not prefixed)\n * @param hex hex string\n * @returns Uint8Array\n */\nexport function hexToBytes(hex: string): Uint8Array {\n const value = hex.replace(/^0x/i, '');\n const len = value.length + 1 - ((value.length + 1) % 2);\n return Uint8Array.from(Buffer.from(value.padStart(len, '0'), 'hex'));\n}\n\nexport function bytesToHex(bytes: Uint8Array | ArrayBuffer): string {\n return Buffer.from(bytes).toString('hex');\n}\n\n/**\n * 0x prefixed hex string from Uint8Array\n * @param bytes\n */\nexport function bytesToHexPrefixed(bytes: Uint8Array | ArrayBuffer): HexString {\n return ensure0xPrefixed(bytesToHex(bytes));\n}\n\nexport function bytesToBase64(bytes: Uint8Array | ArrayBuffer): string {\n return Buffer.from(bytes).toString('base64');\n}\n\nexport function hexToBase64(hexString: string): string {\n const hex = trim0x(hexString);\n // Validate that the input is a valid hex string\n if (!/^[0-9a-fA-F]+$/.test(hex)) {\n throw new Error(`Invalid hex string: ${hexString}`);\n }\n\n // Ensure the hex string has an even length\n if (hex.length % 2 !== 0) {\n throw new Error('Hex string must have an even length');\n }\n\n // Convert hex string to a byte array\n const bytes = new Uint8Array(hex.length / 2);\n for (let i = 0; i < hex.length; i += 2) {\n bytes[i / 2] = parseInt(hex.substr(i, 2), 16);\n }\n\n // Convert the byte array to a base64 string\n const base64String = Buffer.from(bytes).toString('base64');\n\n return base64String;\n}\n\nexport function base64ToHex(base64String: string): HexString {\n if (!/^[A-Za-z0-9+/=]*$/.test(base64String)) {\n throw new Error(`Invalid base64 string: ${base64String}`);\n }\n return `0x${Buffer.from(base64String, 'base64').toString('hex')}`;\n}\n\nfunction padString(str: string, length: number, left: boolean, padding = '0') {\n const diff = length - str.length;\n let result = str;\n if (diff > 0) {\n const pad = padding.repeat(diff);\n result = left ? pad + str : str + pad;\n }\n return result;\n}\n\nfunction padLeft(str: string, length: number, padding = '0') {\n return padString(str, length, true, padding);\n}\nfunction calcByteLength(str: string, byteSize = 8) {\n const { length } = str;\n const remainder = length % byteSize;\n return remainder ? ((length - remainder) / byteSize) * byteSize + byteSize : length;\n}\n\n/**\n * Pads a hex string on the left so its length is a multiple of `byteSize`.\n * Used to align hex strings.\n * @param str Hex string.\n * @param byteSize Group size to align to, in hex chars (default: 8).\n * @param padding Left-pad character (default: '0').\n * @returns Hex string left-padded to a multiple of `byteSize`.\n */\nexport function padAlignHex(str: string, byteSize = 8, padding = '0') {\n const trimmed = trim0x(str);\n return padLeft(trimmed, calcByteLength(trimmed, byteSize), padding);\n}\n"]}
1
+ {"version":3,"sources":["../src/index.ts"],"names":["base58regex","base64regex","hexStringRegex","isBase58","address","test","isBase64","isHexString","value","length","match","hexZeroPad","Error","trim0x","padStart","str","replace","ensure0xPrefixed","trimLeadingZeros","hexString","withoutPrefix","hexToUint8Array","Uint8Array","from","Buffer","hexToAscii","hex","i","l","startsWith","code","parseInt","slice","String","fromCharCode","stringToUint8Array","len","hexToBytes","bytesToHex","bytes","ArrayBuffer","toString","bytesToHexPrefixed","bytesToBase64","base64ToBytes","base64","hexToBase64","substr","base64String","base64ToHex","padString","left","padding","diff","result","pad","repeat","padLeft","calcByteLength","byteSize","remainder","padAlignHex","trimmed"],"mappings":";;;;AAIA,IAAMA,WAAAA,GAAc,yBAAA;AACpB,IAAMC,WAAAA,GAAc,wBAAA;AACpB,IAAMC,cAAAA,GAAiB,qBAAA;AAEhB,SAASC,SAASC,OAAAA,EAAe;AACpC,EAAA,OAAOJ,WAAAA,CAAYK,KAAKD,OAAAA,CAAAA;AAC5B;AAFgBD,MAAAA,CAAAA,QAAAA,EAAAA,UAAAA,CAAAA;AAIT,SAASG,SAASF,OAAAA,EAAe;AACpC,EAAA,OAAOH,WAAAA,CAAYI,KAAKD,OAAAA,CAAAA;AAC5B;AAFgBE,MAAAA,CAAAA,QAAAA,EAAAA,UAAAA,CAAAA;AAIT,SAASC,WAAAA,CAAYC,OAAYC,MAAAA,EAAe;AACnD,EAAA,IAAI,OAAOD,KAAAA,KAAU,QAAA,IAAY,CAACA,KAAAA,CAAME,KAAAA,CAAMR,cAAAA,CAAAA,EAAiB;AAC3D,IAAA,OAAO,KAAA;AACX,EAAA;AACA,EAAA,IAAIO,MAAAA,IAAUD,KAAAA,CAAMC,MAAAA,KAAW,CAAA,GAAI,IAAIA,MAAAA,EAAQ;AAC3C,IAAA,OAAO,KAAA;AACX,EAAA;AACA,EAAA,OAAO,IAAA;AACX;AARgBF,MAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA;AAUT,SAASI,UAAAA,CAAWH,OAAeC,MAAAA,EAAc;AACpD,EAAA,IAAI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SAAAA,IAAaD,QAAAA,GAAYA,WAAWA,QAAAA,GAAWlD,MAAAA;AACjF;AAJSiD,MAAAA,CAAAA,cAAAA,EAAAA,gBAAAA,CAAAA;AAcF,SAASG,WAAAA,CAAY9C,GAAAA,EAAa4C,QAAAA,GAAW,CAAA,EAAGP,UAAU,GAAA,EAAG;AAChE,EAAA,MAAMU,OAAAA,GAAUjD,OAAOE,GAAAA,CAAAA;AACvB,EAAA,OAAO0C,QAAQK,OAAAA,EAASJ,cAAAA,CAAeI,OAAAA,EAASH,QAAAA,GAAWP,OAAAA,CAAAA;AAC/D;AAHgBS,MAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA","file":"index.js","sourcesContent":["export * from './byte-codec';\n\nexport type HexString = `0x${string}`;\n\nconst base58regex = /^[A-HJ-NP-Za-km-z1-9]*$/;\nconst base64regex = /^[A-Za-z0-9+/]*={0,2}$/;\nconst hexStringRegex = /^(0x)?[0-9A-Fa-f]*$/;\n\nexport function isBase58(address: string): boolean {\n return base58regex.test(address);\n}\n\nexport function isBase64(address: string): boolean {\n return base64regex.test(address);\n}\n\nexport function isHexString(value: any, length?: number): value is HexString {\n if (typeof value !== 'string' || !value.match(hexStringRegex)) {\n return false;\n }\n if (length && value.length !== 2 + 2 * length) {\n return false;\n }\n return true;\n}\n\nexport function hexZeroPad(value: string, length: number): HexString {\n if (!isHexString(value)) {\n throw new Error(`invalid hex string: ${value}`);\n }\n\n if (value.length > 2 * length + 2) {\n throw new Error('value out of range');\n }\n\n return `0x${trim0x(value).padStart(2 * length, '0')}`;\n}\n\nexport function trim0x(str: string): string {\n return str.replace(/^0x/, '');\n}\n\nexport function ensure0xPrefixed(str: string): HexString {\n return `0x${trim0x(str)}`;\n}\n\nexport function trimLeadingZeros(hexString: string): HexString {\n // Remove the '0x' prefix\n let withoutPrefix = trim0x(hexString);\n\n // Trim leading zeros\n withoutPrefix = withoutPrefix.replace(/^0+/, '');\n\n // Add back the '0x' prefix\n return ensure0xPrefixed(withoutPrefix);\n}\n\nexport function hexToUint8Array(hexString: string): Uint8Array {\n return Uint8Array.from(Buffer.from(trim0x(hexString), 'hex'));\n}\n\nexport function hexToAscii(hex: string): string {\n if (!isHexString(hex)) {\n throw new Error(`Invalid hex string ${hex}`);\n }\n\n let str = '';\n let i = 0;\n const l = hex.length;\n if (hex.startsWith('0x')) {\n i = 2;\n }\n for (; i < l; i += 2) {\n const code = parseInt(hex.slice(i, i + 2), 16);\n str += String.fromCharCode(code);\n }\n\n return str;\n}\n\nexport function stringToUint8Array(str: string): Uint8Array {\n const value = str.replace(/^0x/i, '');\n const len = value.length + 1 - ((value.length + 1) % 2);\n return Uint8Array.from(Buffer.from(value.padStart(len, '0'), 'hex'));\n}\n\n/**\n * A function to convert hex string to Uint8Array (not prefixed)\n * @param hex hex string\n * @returns Uint8Array\n */\nexport function hexToBytes(hex: string): Uint8Array {\n const value = hex.replace(/^0x/i, '');\n const len = value.length + 1 - ((value.length + 1) % 2);\n return Uint8Array.from(Buffer.from(value.padStart(len, '0'), 'hex'));\n}\n\nexport function bytesToHex(bytes: Uint8Array | ArrayBuffer): string {\n return Buffer.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).toString(\n 'hex',\n );\n}\n\n/**\n * 0x prefixed hex string from Uint8Array\n * @param bytes\n */\nexport function bytesToHexPrefixed(bytes: Uint8Array | ArrayBuffer): HexString {\n return ensure0xPrefixed(bytesToHex(bytes));\n}\n\nexport function bytesToBase64(bytes: Uint8Array | ArrayBuffer): string {\n return Buffer.from(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes).toString(\n 'base64',\n );\n}\n\nexport function base64ToBytes(base64: string): Uint8Array {\n return Uint8Array.from(Buffer.from(base64, 'base64'));\n}\n\nexport function hexToBase64(hexString: string): string {\n const hex = trim0x(hexString);\n // Validate that the input is a valid hex string\n if (!/^[0-9a-fA-F]+$/.test(hex)) {\n throw new Error(`Invalid hex string: ${hexString}`);\n }\n\n // Ensure the hex string has an even length\n if (hex.length % 2 !== 0) {\n throw new Error('Hex string must have an even length');\n }\n\n // Convert hex string to a byte array\n const bytes = new Uint8Array(hex.length / 2);\n for (let i = 0; i < hex.length; i += 2) {\n bytes[i / 2] = parseInt(hex.substr(i, 2), 16);\n }\n\n // Convert the byte array to a base64 string\n const base64String = Buffer.from(bytes).toString('base64');\n\n return base64String;\n}\n\nexport function base64ToHex(base64String: string): HexString {\n if (!/^[A-Za-z0-9+/=]*$/.test(base64String)) {\n throw new Error(`Invalid base64 string: ${base64String}`);\n }\n return `0x${Buffer.from(base64String, 'base64').toString('hex')}`;\n}\n\nfunction padString(str: string, length: number, left: boolean, padding = '0') {\n const diff = length - str.length;\n let result = str;\n if (diff > 0) {\n const pad = padding.repeat(diff);\n result = left ? pad + str : str + pad;\n }\n return result;\n}\n\nfunction padLeft(str: string, length: number, padding = '0') {\n return padString(str, length, true, padding);\n}\nfunction calcByteLength(str: string, byteSize = 8) {\n const { length } = str;\n const remainder = length % byteSize;\n return remainder ? ((length - remainder) / byteSize) * byteSize + byteSize : length;\n}\n\n/**\n * Pads a hex string on the left so its length is a multiple of `byteSize`.\n * Used to align hex strings.\n * @param str Hex string.\n * @param byteSize Group size to align to, in hex chars (default: 8).\n * @param padding Left-pad character (default: '0').\n * @returns Hex string left-padded to a multiple of `byteSize`.\n */\nexport function padAlignHex(str: string, byteSize = 8, padding = '0') {\n const trimmed = trim0x(str);\n return padLeft(trimmed, calcByteLength(trimmed, byteSize), padding);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@layerzerolabs/common-encoding-utils",
3
- "version": "0.2.64",
3
+ "version": "0.2.66",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,18 +16,13 @@
16
16
  "devDependencies": {
17
17
  "tsup": "^8.4.0",
18
18
  "vitest": "^3.2.3",
19
- "@layerzerolabs/tsup-configuration": "0.2.64",
20
- "@layerzerolabs/typescript-configuration": "0.2.64"
19
+ "@layerzerolabs/tsup-configuration": "0.2.66",
20
+ "@layerzerolabs/typescript-configuration": "0.2.66"
21
21
  },
22
22
  "publishConfig": {
23
23
  "access": "restricted",
24
24
  "registry": "https://registry.npmjs.org/"
25
25
  },
26
- "externalRepoConfig": {
27
- "targets": [
28
- "audit-external"
29
- ]
30
- },
31
26
  "scripts": {
32
27
  "build": "tsup",
33
28
  "lint": "eslint . --max-warnings 0 || (eslint . --fix --max-warnings 0 && false)",
@@ -0,0 +1,265 @@
1
+ /**
2
+ * Growable byte codec backed by `ArrayBuffer` + `DataView`.
3
+ *
4
+ * Low-level building block for constructing byte payloads deterministically.
5
+ * Higher-level concerns (e.g. hashing, ABI encoding, function signatures) should
6
+ * live in wrappers/subclasses.
7
+ */
8
+
9
+ function uMax(bits: number): bigint {
10
+ if (!Number.isInteger(bits) || bits < 0) {
11
+ throw new RangeError(`ByteCodec: invalid bit width: ${bits}`);
12
+ }
13
+ // (2^bits) - 1, as bigint
14
+ return bits === 0 ? 0n : (1n << BigInt(bits)) - 1n;
15
+ }
16
+
17
+ // TODO: add tests for this class — currently has zero test coverage
18
+ export class ByteCodec {
19
+ // Start with a small-ish initial capacity to avoid frequent reallocations for
20
+ // common short payloads (e.g. function selector + a few fixed-width fields),
21
+ // while still staying tiny in memory terms. Buffer grows by doubling as needed.
22
+ #buf = new ArrayBuffer(128);
23
+ #view = new DataView(this.#buf);
24
+ #cursor = 0;
25
+
26
+ static readonly #ZERO = 0n;
27
+ static readonly #U8_MAX = uMax(8);
28
+ static readonly #U16_MAX = uMax(16);
29
+ static readonly #U32_MAX = uMax(32);
30
+ static readonly #U64_MAX = uMax(64);
31
+ static readonly #U128_MAX = uMax(128);
32
+ static readonly #U256_MAX = uMax(256);
33
+
34
+ static #outOfRange(targetLength: number, value: bigint): RangeError {
35
+ return new RangeError(`ByteCodec: value out of range for u${targetLength * 8}: ${value}`);
36
+ }
37
+
38
+ /**
39
+ * Read helpers (big-endian). These are intentionally small/low-level so other
40
+ * packages can share a single implementation for parsing encoded payloads.
41
+ */
42
+ static readU16be(buf: Uint8Array, offset: number): number {
43
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
44
+ return view.getUint16(offset, false);
45
+ }
46
+
47
+ static readU8(buf: Uint8Array, offset: number): number {
48
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
49
+ return view.getUint8(offset);
50
+ }
51
+
52
+ static readU32be(buf: Uint8Array, offset: number): number {
53
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
54
+ return view.getUint32(offset, false);
55
+ }
56
+
57
+ static readU128be(buf: Uint8Array, offset: number): bigint {
58
+ return ByteCodec.readUNbe(buf, offset, 16);
59
+ }
60
+
61
+ static readUNbe(buf: Uint8Array, offset: number, length: number): bigint {
62
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
63
+
64
+ let out = 0n;
65
+ const end = offset + length;
66
+
67
+ // Fast path: read 8 bytes at a time
68
+ let cursor = offset;
69
+ while (cursor + 8 <= end) {
70
+ out = (out << 64n) | view.getBigUint64(cursor, false);
71
+ cursor += 8;
72
+ }
73
+
74
+ // Tail: 0..7 bytes
75
+ while (cursor < end) {
76
+ out = (out << 8n) | BigInt(view.getUint8(cursor));
77
+ cursor += 1;
78
+ }
79
+
80
+ return out;
81
+ }
82
+
83
+ static readBytes32(buf: Uint8Array, offset: number): Uint8Array {
84
+ if (offset + 32 > buf.length) throw new RangeError(`ByteCodec: out of bounds at ${offset}`);
85
+ return buf.slice(offset, offset + 32);
86
+ }
87
+
88
+ /**
89
+ * Left-pad `bytes` to `targetLength` using `padByte` (default 0x00).
90
+ *
91
+ * Commonly used to mimic Solidity's `bytes32(bytesN)`-style left zero padding.
92
+ */
93
+ static leftPad(bytes: Uint8Array, targetLength: number, padByte = 0): Uint8Array {
94
+ if (!Number.isInteger(targetLength) || targetLength < 0) {
95
+ throw new RangeError(`ByteCodec: invalid length: target=${targetLength}`);
96
+ }
97
+ if (!Number.isInteger(padByte) || padByte < 0 || padByte > 255) {
98
+ throw new RangeError(`ByteCodec: invalid pad byte: ${padByte}`);
99
+ }
100
+ if (bytes.length > targetLength) {
101
+ throw new RangeError(
102
+ `ByteCodec: bytes length ${bytes.length} exceeds target ${targetLength}`,
103
+ );
104
+ }
105
+
106
+ const out = new Uint8Array(targetLength);
107
+ if (padByte !== 0) out.fill(padByte);
108
+ out.set(bytes, targetLength - bytes.length);
109
+ return out;
110
+ }
111
+
112
+ /**
113
+ * Cast an unsigned big-endian integer in `bytes` into a fixed-width uint (by bytes),
114
+ * reverting on overflow.
115
+ *
116
+ * This mirrors Solidity-style safe casts like `SafeCast.toUint128(uint256)`:
117
+ * - if `bytes.length > targetLength`, the high (bytes.length - targetLength) bytes must be all zero
118
+ * - returns the low `targetLength` bytes as the result
119
+ *
120
+ * Examples:
121
+ * - cast bytes32 -> u128: castUNbe(bytes32, 16)
122
+ * - cast bytesN -> u64: castUNbe(bytesN, 8)
123
+ */
124
+ static castUNbe(bytes: Uint8Array, targetLength: number): bigint {
125
+ if (!Number.isInteger(targetLength) || targetLength < 0) {
126
+ throw new RangeError(`ByteCodec: invalid length: target=${targetLength}`);
127
+ }
128
+
129
+ const len = bytes.length;
130
+ if (len <= targetLength) return ByteCodec.readUNbe(bytes, 0, len);
131
+
132
+ // Overflow check: any excess high bytes must be 0x00.
133
+ const excess = len - targetLength;
134
+ for (let i = 0; i < excess; i++) {
135
+ if (bytes[i] !== 0)
136
+ throw ByteCodec.#outOfRange(targetLength, ByteCodec.readUNbe(bytes, 0, len));
137
+ }
138
+
139
+ return ByteCodec.readUNbe(bytes, excess, targetLength);
140
+ }
141
+
142
+ static castU8be(bytes: Uint8Array): bigint {
143
+ return ByteCodec.castUNbe(bytes, 1);
144
+ }
145
+
146
+ static castU16be(bytes: Uint8Array): bigint {
147
+ return ByteCodec.castUNbe(bytes, 2);
148
+ }
149
+
150
+ static castU32be(bytes: Uint8Array): bigint {
151
+ return ByteCodec.castUNbe(bytes, 4);
152
+ }
153
+
154
+ static castU64be(bytes: Uint8Array): bigint {
155
+ return ByteCodec.castUNbe(bytes, 8);
156
+ }
157
+
158
+ static castU128be(bytes: Uint8Array): bigint {
159
+ return ByteCodec.castUNbe(bytes, 16);
160
+ }
161
+
162
+ protected ensureCapacity(additionalBytes: number): void {
163
+ const needed = this.#cursor + additionalBytes;
164
+ if (needed <= this.#buf.byteLength) return;
165
+
166
+ let nextCap = this.#buf.byteLength;
167
+ while (nextCap < needed) nextCap *= 2;
168
+
169
+ // Grow by allocating a new ArrayBuffer and copying the written prefix
170
+ // [0..cursor) into it. We then swap the backing buffer+view.
171
+ const next = new ArrayBuffer(nextCap);
172
+ new Uint8Array(next).set(new Uint8Array(this.#buf, 0, this.#cursor));
173
+ this.#buf = next;
174
+ this.#view = new DataView(this.#buf);
175
+ }
176
+
177
+ bytes(b: Uint8Array): this {
178
+ this.ensureCapacity(b.length);
179
+ new Uint8Array(this.#buf, this.#cursor, b.length).set(b);
180
+ this.#cursor += b.length;
181
+ return this;
182
+ }
183
+
184
+ bytes32(b: Uint8Array): this {
185
+ return this.bytes(ByteCodec.leftPad(b, 32));
186
+ }
187
+
188
+ bool(b: boolean): this {
189
+ return this.u8(b ? 1n : ByteCodec.#ZERO);
190
+ }
191
+
192
+ u8(v: bigint): this {
193
+ if (v < ByteCodec.#ZERO || v > ByteCodec.#U8_MAX)
194
+ throw new RangeError(`ByteCodec: value out of range for u8: ${v}`);
195
+ this.ensureCapacity(1);
196
+ this.#view.setUint8(this.#cursor, Number(v));
197
+ this.#cursor += 1;
198
+ return this;
199
+ }
200
+
201
+ u16be(v: bigint): this {
202
+ if (v < ByteCodec.#ZERO || v > ByteCodec.#U16_MAX)
203
+ throw new RangeError(`ByteCodec: value out of range for u16: ${v}`);
204
+ this.ensureCapacity(2);
205
+ this.#view.setUint16(this.#cursor, Number(v), false);
206
+ this.#cursor += 2;
207
+ return this;
208
+ }
209
+
210
+ u32be(v: bigint): this {
211
+ if (v < ByteCodec.#ZERO || v > ByteCodec.#U32_MAX) {
212
+ throw new RangeError(`ByteCodec: value out of range for u32: ${v}`);
213
+ }
214
+ this.ensureCapacity(4);
215
+ this.#view.setUint32(this.#cursor, Number(v), false);
216
+ this.#cursor += 4;
217
+ return this;
218
+ }
219
+
220
+ u64be(v: bigint): this {
221
+ if (v < ByteCodec.#ZERO || v > ByteCodec.#U64_MAX) {
222
+ throw new RangeError(`ByteCodec: value out of range for u64: ${v}`);
223
+ }
224
+ this.ensureCapacity(8);
225
+ this.#view.setBigUint64(this.#cursor, v, false);
226
+ this.#cursor += 8;
227
+ return this;
228
+ }
229
+
230
+ u128be(v: bigint): this {
231
+ if (v < ByteCodec.#ZERO || v > ByteCodec.#U128_MAX) {
232
+ throw new RangeError(`ByteCodec: value out of range for u128: ${v}`);
233
+ }
234
+ this.ensureCapacity(16);
235
+ const hi = (v >> 64n) & ByteCodec.#U64_MAX;
236
+ const lo = v & ByteCodec.#U64_MAX;
237
+ this.#view.setBigUint64(this.#cursor, hi, false);
238
+ this.#view.setBigUint64(this.#cursor + 8, lo, false);
239
+ this.#cursor += 16;
240
+ return this;
241
+ }
242
+
243
+ u256be(v: bigint): this {
244
+ if (v < ByteCodec.#ZERO || v > ByteCodec.#U256_MAX) {
245
+ throw new RangeError(`ByteCodec: value out of range for u256: ${v}`);
246
+ }
247
+ this.ensureCapacity(32);
248
+ const w0 = (v >> 192n) & ByteCodec.#U64_MAX;
249
+ const w1 = (v >> 128n) & ByteCodec.#U64_MAX;
250
+ const w2 = (v >> 64n) & ByteCodec.#U64_MAX;
251
+ const w3 = v & ByteCodec.#U64_MAX;
252
+ this.#view.setBigUint64(this.#cursor, w0, false);
253
+ this.#view.setBigUint64(this.#cursor + 8, w1, false);
254
+ this.#view.setBigUint64(this.#cursor + 16, w2, false);
255
+ this.#view.setBigUint64(this.#cursor + 24, w3, false);
256
+ this.#cursor += 32;
257
+ return this;
258
+ }
259
+ /**
260
+ * Returns a copy of the accumulated bytes (no shared backing buffer).
261
+ */
262
+ toBytes(): Uint8Array {
263
+ return new Uint8Array(this.#buf, 0, this.#cursor).slice();
264
+ }
265
+ }