thirdweb 5.67.1-nightly-9584885f42ba6d7fec7dd4e1b3d7e0b677cf6afc-20241112000327 → 5.67.1-nightly-040e478ad6cf630dedf666eac7abeb668d323666-20241113000339

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.
Files changed (95) hide show
  1. package/dist/cjs/auth/verify-hash.js +1 -1
  2. package/dist/cjs/auth/verify-hash.js.map +1 -1
  3. package/dist/cjs/react/core/utils/walletIcon.js +10 -1
  4. package/dist/cjs/react/core/utils/walletIcon.js.map +1 -1
  5. package/dist/cjs/utils/encoding/from-bytes.js +5 -27
  6. package/dist/cjs/utils/encoding/from-bytes.js.map +1 -1
  7. package/dist/cjs/utils/encoding/helpers/byte-size.js +2 -2
  8. package/dist/cjs/utils/encoding/helpers/byte-size.js.map +1 -1
  9. package/dist/cjs/utils/encoding/helpers/concat-hex.js +2 -1
  10. package/dist/cjs/utils/encoding/helpers/concat-hex.js.map +1 -1
  11. package/dist/cjs/utils/encoding/helpers/is-hex.js +2 -9
  12. package/dist/cjs/utils/encoding/helpers/is-hex.js.map +1 -1
  13. package/dist/cjs/utils/encoding/hex.js +13 -134
  14. package/dist/cjs/utils/encoding/hex.js.map +1 -1
  15. package/dist/cjs/utils/encoding/to-bytes.js +5 -59
  16. package/dist/cjs/utils/encoding/to-bytes.js.map +1 -1
  17. package/dist/cjs/version.js +1 -1
  18. package/dist/cjs/wallets/in-app/core/wallet/enclave-wallet.js +11 -7
  19. package/dist/cjs/wallets/in-app/core/wallet/enclave-wallet.js.map +1 -1
  20. package/dist/esm/auth/verify-hash.js +1 -1
  21. package/dist/esm/auth/verify-hash.js.map +1 -1
  22. package/dist/esm/react/core/utils/walletIcon.js +10 -1
  23. package/dist/esm/react/core/utils/walletIcon.js.map +1 -1
  24. package/dist/esm/utils/encoding/from-bytes.js +6 -28
  25. package/dist/esm/utils/encoding/from-bytes.js.map +1 -1
  26. package/dist/esm/utils/encoding/helpers/byte-size.js +1 -1
  27. package/dist/esm/utils/encoding/helpers/byte-size.js.map +1 -1
  28. package/dist/esm/utils/encoding/helpers/concat-hex.js +2 -1
  29. package/dist/esm/utils/encoding/helpers/concat-hex.js.map +1 -1
  30. package/dist/esm/utils/encoding/helpers/is-hex.js +2 -9
  31. package/dist/esm/utils/encoding/helpers/is-hex.js.map +1 -1
  32. package/dist/esm/utils/encoding/hex.js +13 -134
  33. package/dist/esm/utils/encoding/hex.js.map +1 -1
  34. package/dist/esm/utils/encoding/to-bytes.js +5 -58
  35. package/dist/esm/utils/encoding/to-bytes.js.map +1 -1
  36. package/dist/esm/version.js +1 -1
  37. package/dist/esm/wallets/in-app/core/wallet/enclave-wallet.js +11 -7
  38. package/dist/esm/wallets/in-app/core/wallet/enclave-wallet.js.map +1 -1
  39. package/dist/types/react/core/utils/walletIcon.d.ts +11 -1
  40. package/dist/types/react/core/utils/walletIcon.d.ts.map +1 -1
  41. package/dist/types/utils/encoding/from-bytes.d.ts +4 -9
  42. package/dist/types/utils/encoding/from-bytes.d.ts.map +1 -1
  43. package/dist/types/utils/encoding/helpers/byte-size.d.ts +1 -1
  44. package/dist/types/utils/encoding/helpers/byte-size.d.ts.map +1 -1
  45. package/dist/types/utils/encoding/helpers/concat-hex.d.ts +1 -1
  46. package/dist/types/utils/encoding/helpers/concat-hex.d.ts.map +1 -1
  47. package/dist/types/utils/encoding/helpers/is-hex.d.ts +3 -5
  48. package/dist/types/utils/encoding/helpers/is-hex.d.ts.map +1 -1
  49. package/dist/types/utils/encoding/hex.d.ts +11 -42
  50. package/dist/types/utils/encoding/hex.d.ts.map +1 -1
  51. package/dist/types/utils/encoding/to-bytes.d.ts +5 -19
  52. package/dist/types/utils/encoding/to-bytes.d.ts.map +1 -1
  53. package/dist/types/version.d.ts +1 -1
  54. package/dist/types/wallets/in-app/core/wallet/enclave-wallet.d.ts.map +1 -1
  55. package/package.json +4 -2
  56. package/src/auth/verify-hash.ts +1 -1
  57. package/src/extensions/prebuilts/deploy-published.test.ts +0 -36
  58. package/src/extensions/unstoppable-domains/read/resolveAddress.test.ts +25 -22
  59. package/src/react/core/utils/walletIcon.ts +11 -2
  60. package/src/react/web/ui/SiteEmbed.test.tsx +3 -18
  61. package/src/utils/encoding/from-bytes.ts +8 -38
  62. package/src/utils/encoding/helpers/byte-size.ts +1 -1
  63. package/src/utils/encoding/helpers/concat-hex.ts +3 -5
  64. package/src/utils/encoding/helpers/is-hex.ts +4 -14
  65. package/src/utils/encoding/hex.ts +23 -214
  66. package/src/utils/encoding/to-bytes.test.ts +3 -51
  67. package/src/utils/encoding/to-bytes.ts +10 -82
  68. package/src/version.ts +1 -1
  69. package/src/wallets/in-app/core/wallet/enclave-wallet.ts +23 -18
  70. package/src/wallets/smart/smart-wallet-integration-v07.test.ts +15 -15
  71. package/src/wallets/smart/smart-wallet-integration.test.ts +3 -8
  72. package/dist/cjs/utils/encoding/helpers/assert-size.js +0 -23
  73. package/dist/cjs/utils/encoding/helpers/assert-size.js.map +0 -1
  74. package/dist/cjs/utils/encoding/helpers/charcode-to-base-16.js +0 -27
  75. package/dist/cjs/utils/encoding/helpers/charcode-to-base-16.js.map +0 -1
  76. package/dist/cjs/utils/encoding/helpers/trim.js +0 -36
  77. package/dist/cjs/utils/encoding/helpers/trim.js.map +0 -1
  78. package/dist/esm/utils/encoding/helpers/assert-size.js +0 -20
  79. package/dist/esm/utils/encoding/helpers/assert-size.js.map +0 -1
  80. package/dist/esm/utils/encoding/helpers/charcode-to-base-16.js +0 -24
  81. package/dist/esm/utils/encoding/helpers/charcode-to-base-16.js.map +0 -1
  82. package/dist/esm/utils/encoding/helpers/trim.js +0 -33
  83. package/dist/esm/utils/encoding/helpers/trim.js.map +0 -1
  84. package/dist/types/utils/encoding/helpers/assert-size.d.ts +0 -17
  85. package/dist/types/utils/encoding/helpers/assert-size.d.ts.map +0 -1
  86. package/dist/types/utils/encoding/helpers/charcode-to-base-16.d.ts +0 -5
  87. package/dist/types/utils/encoding/helpers/charcode-to-base-16.d.ts.map +0 -1
  88. package/dist/types/utils/encoding/helpers/trim.d.ts +0 -16
  89. package/dist/types/utils/encoding/helpers/trim.d.ts.map +0 -1
  90. package/src/utils/encoding/helpers/assert-size.test.ts +0 -47
  91. package/src/utils/encoding/helpers/assert-size.ts +0 -24
  92. package/src/utils/encoding/helpers/charcode-to-base-16.test.ts +0 -39
  93. package/src/utils/encoding/helpers/charcode-to-base-16.ts +0 -24
  94. package/src/utils/encoding/helpers/trim.ts +0 -48
  95. /package/src/utils/encoding/helpers/{is-hext.test.ts → is-hex.test.ts} +0 -0
@@ -1,7 +1,5 @@
1
- import { assertSize } from "./helpers/assert-size.js";
2
- import type { Hex } from "./helpers/is-hex.js";
3
- import { trim } from "./helpers/trim.js";
4
- import { hexToBigInt, hexToNumber, uint8ArrayToHex } from "./hex.js";
1
+ import * as ox__Bytes from "ox/Bytes";
2
+ import { type Hex, uint8ArrayToHex } from "./hex.js";
5
3
 
6
4
  export type FromBytesParameters<
7
5
  TTo extends "string" | "hex" | "bigint" | "number" | "boolean",
@@ -86,17 +84,10 @@ export function bytesToBigInt(
86
84
  bytes: Uint8Array,
87
85
  opts: BytesToBigIntOpts = {},
88
86
  ): bigint {
89
- if (typeof opts.size !== "undefined") {
90
- assertSize(bytes, { size: opts.size });
91
- }
92
- const hex = uint8ArrayToHex(bytes, opts);
93
- return hexToBigInt(hex, opts);
87
+ return ox__Bytes.toBigInt(bytes, opts);
94
88
  }
95
89
 
96
- export type BytesToBoolOpts = {
97
- /** Size of the bytes. */
98
- size?: number;
99
- };
90
+ export type BytesToBoolOpts = ox__Bytes.toBoolean.Options;
100
91
 
101
92
  /**
102
93
  * Converts a byte array to a boolean value.
@@ -117,16 +108,7 @@ export function bytesToBool(
117
108
  bytes_: Uint8Array,
118
109
  opts: BytesToBoolOpts = {},
119
110
  ): boolean {
120
- let bytes = bytes_;
121
- if (typeof opts.size !== "undefined") {
122
- assertSize(bytes, { size: opts.size });
123
- bytes = trim(bytes);
124
- }
125
-
126
- if (bytes.length > 1 || (bytes[0] && bytes[0] > 1)) {
127
- throw new Error(`Invalid boolean representation: ${bytes}`);
128
- }
129
- return Boolean(bytes[0]);
111
+ return ox__Bytes.toBoolean(bytes_, opts);
130
112
  }
131
113
 
132
114
  export type BytesToNumberOpts = BytesToBigIntOpts;
@@ -149,17 +131,10 @@ export function bytesToNumber(
149
131
  bytes: Uint8Array,
150
132
  opts: BytesToNumberOpts = {},
151
133
  ): number {
152
- if (typeof opts.size !== "undefined") {
153
- assertSize(bytes, { size: opts.size });
154
- }
155
- const hex = uint8ArrayToHex(bytes, opts);
156
- return hexToNumber(hex, opts);
134
+ return ox__Bytes.toNumber(bytes, opts);
157
135
  }
158
136
 
159
- export type BytesToStringOpts = {
160
- /** Size of the bytes. */
161
- size?: number;
162
- };
137
+ export type BytesToStringOpts = ox__Bytes.toString.Options;
163
138
 
164
139
  /**
165
140
  * Converts an array of bytes to a string using UTF-8 encoding.
@@ -179,10 +154,5 @@ export function bytesToString(
179
154
  bytes_: Uint8Array,
180
155
  opts: BytesToStringOpts = {},
181
156
  ): string {
182
- let bytes = bytes_;
183
- if (typeof opts.size !== "undefined") {
184
- assertSize(bytes, { size: opts.size });
185
- bytes = trim(bytes, { dir: "right" });
186
- }
187
- return new TextDecoder().decode(bytes);
157
+ return ox__Bytes.toString(bytes_, opts);
188
158
  }
@@ -1,4 +1,4 @@
1
- import { type Hex, isHex } from "./is-hex.js";
1
+ import { type Hex, isHex } from "../hex.js";
2
2
 
3
3
  /**
4
4
  * Calculates the byte size of a Hex string or Uint8Array.
@@ -1,4 +1,5 @@
1
- import type { Hex } from "./is-hex.js";
1
+ import * as ox__Hex from "ox/Hex";
2
+ import type { Hex } from "../hex.js";
2
3
 
3
4
  /**
4
5
  * Concatenates an array of hexadecimal values into a single hexadecimal value.
@@ -8,8 +9,5 @@ import type { Hex } from "./is-hex.js";
8
9
  * @utils
9
10
  */
10
11
  export function concatHex(values: readonly Hex[]): Hex {
11
- return `0x${(values as Hex[]).reduce(
12
- (acc, x) => acc + x.replace("0x", ""),
13
- "",
14
- )}`;
12
+ return ox__Hex.concat(...values);
15
13
  }
@@ -1,9 +1,7 @@
1
- export type Hex = `0x${string}`;
1
+ import * as ox__Hex from "ox/Hex";
2
+ import type { Hex } from "../hex.js";
2
3
 
3
- export type IsHexOptions = {
4
- /** If set to true, the value must start with "0x" and only contain hexadecimal characters. If set to false, the value can start with "0x" or not. Default is true. */
5
- strict?: boolean;
6
- };
4
+ export type IsHexOptions = ox__Hex.assert.Options;
7
5
 
8
6
  /**
9
7
  * Checks if a value is a valid hexadecimal string.
@@ -22,13 +20,5 @@ export function isHex(
22
20
  value: unknown,
23
21
  options: IsHexOptions = {},
24
22
  ): value is Hex {
25
- if (!value) {
26
- return false;
27
- }
28
- if (typeof value !== "string") {
29
- return false;
30
- }
31
- return options.strict
32
- ? /^0x[0-9a-fA-F]*$/.test(value)
33
- : value.startsWith("0x");
23
+ return ox__Hex.validate(value, options);
34
24
  }
@@ -1,51 +1,7 @@
1
- import { cachedTextDecoder } from "../text-decoder.js";
2
- import { cachedTextEncoder } from "../text-encoder.js";
3
- // slightly tweaked re-exports from viem for the moment
4
- import { assertSize } from "./helpers/assert-size.js";
5
- import { charCodeToBase16 } from "./helpers/charcode-to-base-16.js";
6
- import type { Hex } from "./helpers/is-hex.js";
1
+ import * as ox__Hex from "ox/Hex";
2
+ export { isHex, type IsHexOptions } from "./helpers/is-hex.js";
7
3
 
8
- export { type Hex, isHex, type IsHexOptions } from "./helpers/is-hex.js";
9
-
10
- type TrimOptions = {
11
- dir?: "left" | "right";
12
- };
13
- type TrimReturnType<TValue extends Uint8Array | Hex> = TValue extends Hex
14
- ? Hex
15
- : Uint8Array;
16
-
17
- function trim<TValue extends Uint8Array | Hex>(
18
- hexOrBytes: TValue,
19
- options: TrimOptions = {},
20
- ): TrimReturnType<TValue> {
21
- const dir = options.dir || "left";
22
- // biome-ignore lint/suspicious/noExplicitAny: TODO: fix any
23
- let data: any =
24
- typeof hexOrBytes === "string" ? hexOrBytes.replace("0x", "") : hexOrBytes;
25
-
26
- let sliceLength = 0;
27
- for (let i = 0; i < data.length - 1; i++) {
28
- if (data[dir === "left" ? i : data.length - i - 1].toString() === "0") {
29
- sliceLength++;
30
- } else {
31
- break;
32
- }
33
- }
34
- data =
35
- dir === "left"
36
- ? data.slice(sliceLength)
37
- : data.slice(0, data.length - sliceLength);
38
-
39
- if (typeof hexOrBytes === "string") {
40
- if (data.length === 1 && dir === "right") {
41
- data = `${data}0`;
42
- }
43
- return `0x${
44
- data.length % 2 === 1 ? `0${data}` : data
45
- }` as TrimReturnType<TValue>;
46
- }
47
- return data as TrimReturnType<TValue>;
48
- }
4
+ export type Hex = ox__Hex.Hex;
49
5
 
50
6
  type PadOptions = {
51
7
  dir?: "left" | "right";
@@ -71,25 +27,17 @@ export function padHex(hex_: Hex, options: PadOptions = {}) {
71
27
  if (size === null) {
72
28
  return hex_;
73
29
  }
74
- const hex = hex_.replace("0x", "");
75
- if (hex.length > size * 2) {
76
- throw new Error(`Size overflow: ${Math.ceil(hex.length / 2)} > ${size}`);
30
+ if (dir === "right") {
31
+ return ox__Hex.padRight(hex_, size);
77
32
  }
78
-
79
- return `0x${hex[dir === "right" ? "padEnd" : "padStart"](
80
- size * 2,
81
- "0",
82
- )}` as Hex;
33
+ return ox__Hex.padLeft(hex_, size);
83
34
  }
84
35
 
85
36
  //--------------------------------------------------------------------------
86
37
  // FROM HEX
87
38
  //--------------------------------------------------------------------------
88
39
 
89
- export type HexToStringOpts = {
90
- /** Size (in bytes) of the hex value. */
91
- size?: number;
92
- };
40
+ export type HexToStringOpts = ox__Hex.toString.Options;
93
41
 
94
42
  /**
95
43
  * Converts a hexadecimal string to a UTF-8 string.
@@ -105,20 +53,10 @@ export type HexToStringOpts = {
105
53
  * @utils
106
54
  */
107
55
  export function hexToString(hex: Hex, opts: HexToStringOpts = {}): string {
108
- let bytes = hexToUint8Array(hex);
109
- if (opts.size) {
110
- assertSize(bytes, { size: opts.size });
111
- bytes = trim(bytes, { dir: "right" });
112
- }
113
- return cachedTextDecoder().decode(bytes);
56
+ return ox__Hex.toString(hex, opts);
114
57
  }
115
58
 
116
- export type HexToBigIntOpts = {
117
- /** Whether or not the number of a signed representation. */
118
- signed?: boolean;
119
- /** Size (in bytes) of the hex value. */
120
- size?: number;
121
- };
59
+ export type HexToBigIntOpts = ox__Hex.toBigInt.Options;
122
60
 
123
61
  /**
124
62
  * Converts a hexadecimal string to a BigInt.
@@ -134,24 +72,7 @@ export type HexToBigIntOpts = {
134
72
  * @utils
135
73
  */
136
74
  export function hexToBigInt(hex: Hex, opts: HexToBigIntOpts = {}): bigint {
137
- const { signed } = opts;
138
-
139
- if (opts.size) {
140
- assertSize(hex, { size: opts.size });
141
- }
142
-
143
- const value = BigInt(hex);
144
- if (!signed) {
145
- return value;
146
- }
147
-
148
- const size = (hex.length - 2) / 2;
149
- const max = (1n << (BigInt(size) * 8n - 1n)) - 1n;
150
- if (value <= max) {
151
- return value;
152
- }
153
-
154
- return value - BigInt(`0x${"f".padStart(size * 2, "f")}`) - 1n;
75
+ return ox__Hex.toBigInt(hex, opts);
155
76
  }
156
77
 
157
78
  export type HexToNumberOpts = HexToBigIntOpts;
@@ -169,13 +90,10 @@ export type HexToNumberOpts = HexToBigIntOpts;
169
90
  * @utils
170
91
  */
171
92
  export function hexToNumber(hex: Hex, opts: HexToNumberOpts = {}): number {
172
- return Number(hexToBigInt(hex, opts));
93
+ return ox__Hex.toNumber(hex, opts);
173
94
  }
174
95
 
175
- export type HexToBoolOpts = {
176
- /** Size (in bytes) of the hex value. */
177
- size?: number;
178
- };
96
+ export type HexToBoolOpts = ox__Hex.toBoolean.Options;
179
97
 
180
98
  /**
181
99
  * Converts a hexadecimal string to a boolean value.
@@ -192,24 +110,10 @@ export type HexToBoolOpts = {
192
110
  * @utils
193
111
  */
194
112
  export function hexToBool(hex: Hex, opts: HexToBoolOpts = {}): boolean {
195
- if (opts.size) {
196
- assertSize(hex, { size: opts.size });
197
- // biome-ignore lint/style/noParameterAssign: for perf
198
- hex = trim(hex);
199
- }
200
- if (trim(hex) === "0x00") {
201
- return false;
202
- }
203
- if (trim(hex) === "0x01") {
204
- return true;
205
- }
206
- throw new Error(`Invalid hex boolean: ${hex}`);
113
+ return ox__Hex.toBoolean(hex, opts);
207
114
  }
208
115
 
209
- export type HexToUint8ArrayOpts = {
210
- /** Size of the output bytes. */
211
- size?: number;
212
- };
116
+ export type HexToUint8ArrayOpts = ox__Hex.toBytes.Options;
213
117
 
214
118
  /**
215
119
  * Converts a hexadecimal string to a Uint8Array.
@@ -228,28 +132,7 @@ export function hexToUint8Array(
228
132
  hex: Hex,
229
133
  opts: HexToUint8ArrayOpts = {},
230
134
  ): Uint8Array {
231
- if (opts.size) {
232
- assertSize(hex, { size: opts.size });
233
- // biome-ignore lint/style/noParameterAssign: for perf
234
- hex = padHex(hex, { dir: "right", size: opts.size });
235
- }
236
-
237
- let hexString = hex.slice(2) as string;
238
- if (hexString.length % 2) {
239
- hexString = `0${hexString}`;
240
- }
241
-
242
- const length = hexString.length / 2;
243
- const bytes = new Uint8Array(length);
244
- for (let index = 0, j = 0; index < length; index++) {
245
- const nibbleLeft = charCodeToBase16(hexString.charCodeAt(j++));
246
- const nibbleRight = charCodeToBase16(hexString.charCodeAt(j++));
247
- if (nibbleLeft === undefined || nibbleRight === undefined) {
248
- throw new Error(`Invalid hex character: ${hexString}`);
249
- }
250
- bytes[index] = nibbleLeft * 16 + nibbleRight;
251
- }
252
- return bytes;
135
+ return ox__Hex.toBytes(hex, opts);
253
136
  }
254
137
 
255
138
  export type FromHexParameters<
@@ -311,13 +194,7 @@ export function fromHex<
311
194
  // TO HEX
312
195
  //--------------------------------------------------------------------------
313
196
 
314
- const hexes = /* @__PURE__ */ (() =>
315
- Array.from({ length: 256 }, (_v, i) => i.toString(16).padStart(2, "0")))();
316
-
317
- export type BoolToHexOpts = {
318
- /** The size (in bytes) of the output hex value. */
319
- size?: number;
320
- };
197
+ export type BoolToHexOpts = ox__Hex.fromBoolean.Options;
321
198
 
322
199
  /**
323
200
  * Converts a boolean value to a hexadecimal string representation.
@@ -333,18 +210,10 @@ export type BoolToHexOpts = {
333
210
  * @utils
334
211
  */
335
212
  export function boolToHex(value: boolean, opts: BoolToHexOpts = {}): Hex {
336
- const hex = `0x${Number(value)}` as const;
337
- if (typeof opts.size === "number") {
338
- assertSize(hex, { size: opts.size });
339
- return padHex(hex, { size: opts.size });
340
- }
341
- return hex;
213
+ return ox__Hex.fromBoolean(value, opts);
342
214
  }
343
215
 
344
- export type Uint8ArrayToHexOpts = {
345
- /** The size (in bytes) of the output hex value. */
346
- size?: number;
347
- };
216
+ export type Uint8ArrayToHexOpts = ox__Hex.fromBoolean.Options;
348
217
 
349
218
  /**
350
219
  * Converts an array of bytes to a hexadecimal string.
@@ -363,32 +232,10 @@ export function uint8ArrayToHex(
363
232
  value: Uint8Array,
364
233
  opts: Uint8ArrayToHexOpts = {},
365
234
  ): Hex {
366
- let string = "";
367
- for (let i = 0; i < value.length; i++) {
368
- // biome-ignore lint/style/noNonNullAssertion: we know this is defined
369
- string += hexes[value[i]!];
370
- }
371
- const hex = `0x${string}` as const;
372
-
373
- if (typeof opts.size === "number") {
374
- assertSize(hex, { size: opts.size });
375
- return padHex(hex, { dir: "right", size: opts.size });
376
- }
377
- return hex;
235
+ return ox__Hex.fromBytes(value, opts);
378
236
  }
379
237
 
380
- export type NumberToHexOpts =
381
- | {
382
- /** Whether or not the number of a signed representation. */
383
- signed?: boolean;
384
- /** The size (in bytes) of the output hex value. */
385
- size: number;
386
- }
387
- | {
388
- signed?: never;
389
- /** The size (in bytes) of the output hex value. */
390
- size?: number;
391
- };
238
+ export type NumberToHexOpts = ox__Hex.fromNumber.Options;
392
239
 
393
240
  /**
394
241
  * Converts a number or bigint to a hexadecimal string.
@@ -408,47 +255,10 @@ export function numberToHex(
408
255
  value_: number | bigint,
409
256
  opts: NumberToHexOpts = {},
410
257
  ): Hex {
411
- const { signed, size } = opts;
412
-
413
- const value = BigInt(value_);
414
-
415
- let maxValue: bigint | number | undefined;
416
- if (size) {
417
- if (signed) {
418
- maxValue = (1n << (BigInt(size) * 8n - 1n)) - 1n;
419
- } else {
420
- maxValue = 2n ** (BigInt(size) * 8n) - 1n;
421
- }
422
- } else if (typeof value_ === "number") {
423
- maxValue = BigInt(Number.MAX_SAFE_INTEGER);
424
- }
425
-
426
- const minValue = typeof maxValue === "bigint" && signed ? -maxValue - 1n : 0;
427
-
428
- if ((maxValue && value > maxValue) || value < minValue) {
429
- const suffix = typeof value_ === "bigint" ? "n" : "";
430
- throw new Error(
431
- `Number "${value_}${suffix}" is not in safe ${
432
- size ? `${size * 8}-bit ${signed ? "signed" : "unsigned"} ` : ""
433
- }integer range ${
434
- maxValue ? `(${minValue} to ${maxValue})` : `(above ${minValue})`
435
- }`,
436
- );
437
- }
438
-
439
- const hex = `0x${(
440
- signed && value < 0 ? (1n << BigInt(size * 8)) + BigInt(value) : value
441
- ).toString(16)}` as Hex;
442
- if (size) {
443
- return padHex(hex, { size }) as Hex;
444
- }
445
- return hex;
258
+ return ox__Hex.fromNumber(value_, opts);
446
259
  }
447
260
 
448
- export type StringToHexOpts = {
449
- /** The size (in bytes) of the output hex value. */
450
- size?: number;
451
- };
261
+ export type StringToHexOpts = ox__Hex.fromString.Options;
452
262
 
453
263
  /**
454
264
  * Converts a string to its hexadecimal representation.
@@ -464,8 +274,7 @@ export type StringToHexOpts = {
464
274
  * @utils
465
275
  */
466
276
  export function stringToHex(value_: string, opts: StringToHexOpts = {}): Hex {
467
- const value = cachedTextEncoder().encode(value_);
468
- return uint8ArrayToHex(value, opts);
277
+ return ox__Hex.fromString(value_, opts);
469
278
  }
470
279
 
471
280
  export type ToHexParameters = {
@@ -4,59 +4,11 @@ import {
4
4
  boolToBytes,
5
5
  hexToBytes,
6
6
  numberToBytes,
7
- padBytes,
8
7
  stringToBytes,
9
8
  toBytes,
10
9
  } from "./to-bytes.js";
11
10
 
12
11
  describe("to-bytes.js", () => {
13
- describe("padBytes", () => {
14
- it("should pad bytes to the left by default to the specified size", () => {
15
- const result = padBytes(new Uint8Array([1, 2, 3]), { size: 5 });
16
- expect(result).toEqual(new Uint8Array([0, 0, 1, 2, 3]));
17
- });
18
-
19
- it('should pad bytes to the right if dir is "right"', () => {
20
- const result = padBytes(new Uint8Array([1, 2, 3]), {
21
- dir: "right",
22
- size: 5,
23
- });
24
- expect(result).toEqual(new Uint8Array([1, 2, 3, 0, 0]));
25
- });
26
-
27
- it("should not pad if the byte array is already the specified size", () => {
28
- const result = padBytes(new Uint8Array([1, 2, 3, 4, 5]), { size: 5 });
29
- expect(result).toEqual(new Uint8Array([1, 2, 3, 4, 5]));
30
- });
31
-
32
- it("should throw an error if the byte array exceeds the specified size", () => {
33
- expect(() =>
34
- padBytes(new Uint8Array([1, 2, 3, 4, 5, 6]), { size: 5 }),
35
- ).toThrow("Size overflow: 6 > 5");
36
- });
37
-
38
- it("should return the same byte array if size is null", () => {
39
- const bytes = new Uint8Array([1, 2, 3]);
40
- const result = padBytes(bytes, { size: null });
41
- expect(result).toEqual(bytes);
42
- });
43
-
44
- it("should pad to the default size of 32 if size is not specified", () => {
45
- const result = padBytes(new Uint8Array([1, 2, 3]));
46
- expect(result).toStrictEqual(
47
- new Uint8Array([
48
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49
- 0, 0, 0, 0, 0, 0, 1, 2, 3,
50
- ]),
51
- );
52
- });
53
-
54
- it("should handle an empty byte array and pad to the specified size", () => {
55
- const result = padBytes(new Uint8Array([]), { size: 3 });
56
- expect(result).toEqual(new Uint8Array([0, 0, 0]));
57
- });
58
- });
59
-
60
12
  describe("toBytes", () => {
61
13
  it("should convert a number to bytes", () => {
62
14
  expect(toBytes(123)).toEqual(new Uint8Array([123]));
@@ -109,7 +61,7 @@ describe("to-bytes.js", () => {
109
61
 
110
62
  it("should throw an error if the specified size is less than the byte size", () => {
111
63
  expect(() => boolToBytes(true, { size: 0 })).toThrow(
112
- "Size overflow: 1 > 0",
64
+ "Size cannot exceed `0` bytes. Given size: `1` bytes.",
113
65
  );
114
66
  });
115
67
 
@@ -151,7 +103,7 @@ describe("to-bytes.js", () => {
151
103
 
152
104
  it("should throw an error if the hex string exceeds the specified size", () => {
153
105
  expect(() => hexToBytes("0x123456", { size: 2 })).toThrow(
154
- "Size overflow",
106
+ "Size cannot exceed `2` bytes. Given size: `3` bytes.",
155
107
  );
156
108
  });
157
109
 
@@ -211,7 +163,7 @@ describe("to-bytes.js", () => {
211
163
 
212
164
  it("should throw an error if the byte array exceeds the specified size", () => {
213
165
  expect(() => stringToBytes("Hello", { size: 3 })).toThrow(
214
- "Size overflow: 5 > 3",
166
+ "Size cannot exceed `3` bytes. Given size: `5` bytes.",
215
167
  );
216
168
  });
217
169
 
@@ -1,36 +1,7 @@
1
- import { cachedTextEncoder } from "../text-encoder.js";
2
- import { assertSize } from "./helpers/assert-size.js";
3
- import { charCodeToBase16 } from "./helpers/charcode-to-base-16.js";
4
- import { type Hex, isHex } from "./helpers/is-hex.js";
5
- import { type NumberToHexOpts, numberToHex, padHex } from "./hex.js";
6
-
7
- type PadOptions = {
8
- dir?: "left" | "right";
9
- size?: number | null;
10
- };
11
-
12
- /**
13
- * @internal Exported for test
14
- */
15
- export function padBytes(
16
- bytes: Uint8Array,
17
- { dir, size = 32 }: PadOptions = {},
18
- ) {
19
- if (size === null) {
20
- return bytes;
21
- }
22
- if (bytes.length > size) {
23
- throw new Error(`Size overflow: ${bytes.length} > ${size}`);
24
- }
25
- const paddedBytes = new Uint8Array(size);
26
- for (let i = 0; i < size; i++) {
27
- const padEnd = dir === "right";
28
- paddedBytes[padEnd ? i : size - i - 1] =
29
- // biome-ignore lint/style/noNonNullAssertion: we know its there
30
- bytes[padEnd ? i : bytes.length - i - 1]!;
31
- }
32
- return paddedBytes;
33
- }
1
+ import * as ox__Bytes from "ox/Bytes";
2
+ import { isHex } from "./helpers/is-hex.js";
3
+ import type { Hex } from "./hex.js";
4
+ import type { NumberToHexOpts } from "./hex.js";
34
5
 
35
6
  export type ToBytesParameters = {
36
7
  /** Size of the output bytes. */
@@ -68,10 +39,7 @@ export function toBytes(
68
39
  }
69
40
  }
70
41
 
71
- export type BoolToBytesOpts = {
72
- /** Size of the output bytes. */
73
- size?: number;
74
- };
42
+ export type BoolToBytesOpts = ox__Bytes.fromBoolean.Options;
75
43
 
76
44
  /**
77
45
  * Converts a boolean value to a Uint8Array of bytes.
@@ -87,19 +55,10 @@ export type BoolToBytesOpts = {
87
55
  * @utils
88
56
  */
89
57
  export function boolToBytes(value: boolean, opts: BoolToBytesOpts = {}) {
90
- const bytes = new Uint8Array(1);
91
- bytes[0] = Number(value);
92
- if (typeof opts.size === "number") {
93
- assertSize(bytes, { size: opts.size });
94
- return padBytes(bytes, { size: opts.size });
95
- }
96
- return bytes;
58
+ return ox__Bytes.fromBoolean(value, opts);
97
59
  }
98
60
 
99
- export type HexToBytesOpts = {
100
- /** Size of the output bytes. */
101
- size?: number;
102
- };
61
+ export type HexToBytesOpts = ox__Bytes.fromHex.Options;
103
62
 
104
63
  /**
105
64
  * Converts a hexadecimal string to a Uint8Array of bytes.
@@ -116,32 +75,7 @@ export type HexToBytesOpts = {
116
75
  * @utils
117
76
  */
118
77
  export function hexToBytes(hex_: Hex, opts: HexToBytesOpts = {}): Uint8Array {
119
- let hex = hex_;
120
- if (opts.size) {
121
- assertSize(hex, { size: opts.size });
122
- hex = padHex(hex, { dir: "right", size: opts.size });
123
- }
124
-
125
- let hexString = hex.slice(2) as string;
126
- if (hexString.length % 2) {
127
- hexString = `0${hexString}`;
128
- }
129
-
130
- const length = hexString.length / 2;
131
- const bytes = new Uint8Array(length);
132
- for (let index = 0, j = 0; index < length; index++) {
133
- const nibbleLeft = charCodeToBase16(hexString.charCodeAt(j++));
134
- const nibbleRight = charCodeToBase16(hexString.charCodeAt(j++));
135
- if (nibbleLeft === undefined || nibbleRight === undefined) {
136
- throw new Error(
137
- `Invalid byte sequence ("${hexString[j - 2]}${
138
- hexString[j - 1]
139
- }" in "${hexString}").`,
140
- );
141
- }
142
- bytes[index] = nibbleLeft * 16 + nibbleRight;
143
- }
144
- return bytes;
78
+ return ox__Bytes.fromHex(hex_, opts);
145
79
  }
146
80
 
147
81
  /**
@@ -158,8 +92,7 @@ export function hexToBytes(hex_: Hex, opts: HexToBytesOpts = {}): Uint8Array {
158
92
  * @utils
159
93
  */
160
94
  export function numberToBytes(value: bigint | number, opts?: NumberToHexOpts) {
161
- const hex = numberToHex(value, opts);
162
- return hexToBytes(hex);
95
+ return ox__Bytes.fromNumber(value, opts);
163
96
  }
164
97
 
165
98
  export type StringToBytesOpts = {
@@ -184,10 +117,5 @@ export function stringToBytes(
184
117
  value: string,
185
118
  opts: StringToBytesOpts = {},
186
119
  ): Uint8Array {
187
- const bytes = cachedTextEncoder().encode(value);
188
- if (typeof opts.size === "number") {
189
- assertSize(bytes, { size: opts.size });
190
- return padBytes(bytes, { dir: "right", size: opts.size });
191
- }
192
- return bytes;
120
+ return ox__Bytes.fromString(value, opts);
193
121
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = "5.67.1-nightly-9584885f42ba6d7fec7dd4e1b3d7e0b677cf6afc-20241112000327";
1
+ export const version = "5.67.1-nightly-040e478ad6cf630dedf666eac7abeb668d323666-20241113000339";