@xylabs/hex 4.13.19 → 4.13.21
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/README.md +686 -3
- package/package.json +10 -7
- package/src/spec/address.spec.ts +25 -0
- package/src/spec/hash.spec.ts +23 -0
- package/src/spec/hex.spec.ts +24 -0
- package/xy.config.ts +0 -10
package/README.md
CHANGED
|
@@ -12,13 +12,696 @@
|
|
|
12
12
|
[![snyk-badge][]][snyk-link]
|
|
13
13
|
[![socket-badge][]][socket-link]
|
|
14
14
|
|
|
15
|
-
Version: 4.13.15
|
|
16
15
|
|
|
17
16
|
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
18
17
|
|
|
19
|
-
## Documentation
|
|
18
|
+
## API Documentation
|
|
19
|
+
|
|
20
|
+
**@xylabs/hex**
|
|
21
|
+
|
|
22
|
+
***
|
|
23
|
+
|
|
24
|
+
## Interfaces
|
|
25
|
+
|
|
26
|
+
- [HexConfig](#interfaces/HexConfig)
|
|
27
|
+
|
|
28
|
+
## Type Aliases
|
|
29
|
+
|
|
30
|
+
- [Address](#type-aliases/Address)
|
|
31
|
+
- [HashBitLength](#type-aliases/HashBitLength)
|
|
32
|
+
- [Hash](#type-aliases/Hash)
|
|
33
|
+
- [Hex](#type-aliases/Hex)
|
|
34
|
+
|
|
35
|
+
## Variables
|
|
36
|
+
|
|
37
|
+
- [ZERO\_ADDRESS](#variables/ZERO_ADDRESS)
|
|
38
|
+
- [ZERO\_HASH](#variables/ZERO_HASH)
|
|
39
|
+
- [HashBitLength](#variables/HashBitLength)
|
|
40
|
+
- [hexRegex](#variables/hexRegex)
|
|
41
|
+
- [hexRegexWithPrefix](#variables/hexRegexWithPrefix)
|
|
42
|
+
|
|
43
|
+
## Functions
|
|
44
|
+
|
|
45
|
+
- [toAddress](#functions/toAddress)
|
|
46
|
+
- [isAddress](#functions/isAddress)
|
|
47
|
+
- [asAddress](#functions/asAddress)
|
|
48
|
+
- [isHashBitLength](#functions/isHashBitLength)
|
|
49
|
+
- [isHash](#functions/isHash)
|
|
50
|
+
- [asHash](#functions/asHash)
|
|
51
|
+
- [asHex](#functions/asHex)
|
|
52
|
+
- [hexFrom](#functions/hexFrom)
|
|
53
|
+
- [hexFromArrayBuffer](#functions/hexFromArrayBuffer)
|
|
54
|
+
- [hexFromBigInt](#functions/hexFromBigInt)
|
|
55
|
+
- [hexFromHexString](#functions/hexFromHexString)
|
|
56
|
+
- [hexFromNumber](#functions/hexFromNumber)
|
|
57
|
+
- [isHex](#functions/isHex)
|
|
58
|
+
- [isHexZero](#functions/isHexZero)
|
|
59
|
+
- [toHexLegacy](#functions/toHexLegacy)
|
|
60
|
+
- [bitsToNibbles](#functions/bitsToNibbles)
|
|
61
|
+
- [nibblesToBits](#functions/nibblesToBits)
|
|
62
|
+
- [toHex](#functions/toHex)
|
|
63
|
+
- [hexToBigInt](#functions/hexToBigInt)
|
|
64
|
+
|
|
65
|
+
### functions
|
|
66
|
+
|
|
67
|
+
### <a id="asAddress"></a>asAddress
|
|
68
|
+
|
|
69
|
+
[**@xylabs/hex**](#../README)
|
|
70
|
+
|
|
71
|
+
***
|
|
72
|
+
|
|
73
|
+
## Call Signature
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
function asAddress(value): undefined | Lowercase<string>;
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Parameters
|
|
80
|
+
|
|
81
|
+
### value
|
|
82
|
+
|
|
83
|
+
`unknown`
|
|
84
|
+
|
|
85
|
+
### Returns
|
|
86
|
+
|
|
87
|
+
`undefined` \| `Lowercase`\<`string`\>
|
|
88
|
+
|
|
89
|
+
## Call Signature
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
function asAddress(value, assert): Lowercase<string>;
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Parameters
|
|
96
|
+
|
|
97
|
+
### value
|
|
98
|
+
|
|
99
|
+
`unknown`
|
|
100
|
+
|
|
101
|
+
### assert
|
|
102
|
+
|
|
103
|
+
`AssertConfig`
|
|
104
|
+
|
|
105
|
+
### Returns
|
|
106
|
+
|
|
107
|
+
`Lowercase`\<`string`\>
|
|
108
|
+
|
|
109
|
+
### <a id="asHash"></a>asHash
|
|
110
|
+
|
|
111
|
+
[**@xylabs/hex**](#../README)
|
|
112
|
+
|
|
113
|
+
***
|
|
114
|
+
|
|
115
|
+
## Call Signature
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
function asHash(value): undefined | Lowercase<string>;
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Parameters
|
|
122
|
+
|
|
123
|
+
### value
|
|
124
|
+
|
|
125
|
+
`unknown`
|
|
126
|
+
|
|
127
|
+
### Returns
|
|
128
|
+
|
|
129
|
+
`undefined` \| `Lowercase`\<`string`\>
|
|
130
|
+
|
|
131
|
+
## Call Signature
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
function asHash(value, assert): Lowercase<string>;
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Parameters
|
|
138
|
+
|
|
139
|
+
### value
|
|
140
|
+
|
|
141
|
+
`unknown`
|
|
142
|
+
|
|
143
|
+
### assert
|
|
144
|
+
|
|
145
|
+
`AssertConfig`
|
|
146
|
+
|
|
147
|
+
### Returns
|
|
148
|
+
|
|
149
|
+
`Lowercase`\<`string`\>
|
|
150
|
+
|
|
151
|
+
### <a id="asHex"></a>asHex
|
|
152
|
+
|
|
153
|
+
[**@xylabs/hex**](#../README)
|
|
154
|
+
|
|
155
|
+
***
|
|
156
|
+
|
|
157
|
+
## Call Signature
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
function asHex(value): undefined | Lowercase<string>;
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Parameters
|
|
164
|
+
|
|
165
|
+
### value
|
|
166
|
+
|
|
167
|
+
`unknown`
|
|
168
|
+
|
|
169
|
+
### Returns
|
|
170
|
+
|
|
171
|
+
`undefined` \| `Lowercase`\<`string`\>
|
|
172
|
+
|
|
173
|
+
## Call Signature
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
function asHex(value, assert): Lowercase<string>;
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Parameters
|
|
180
|
+
|
|
181
|
+
### value
|
|
182
|
+
|
|
183
|
+
`unknown`
|
|
184
|
+
|
|
185
|
+
### assert
|
|
186
|
+
|
|
187
|
+
`AssertConfig`
|
|
188
|
+
|
|
189
|
+
### Returns
|
|
190
|
+
|
|
191
|
+
`Lowercase`\<`string`\>
|
|
192
|
+
|
|
193
|
+
### <a id="bitsToNibbles"></a>bitsToNibbles
|
|
194
|
+
|
|
195
|
+
[**@xylabs/hex**](#../README)
|
|
196
|
+
|
|
197
|
+
***
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
function bitsToNibbles(value): number;
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Parameters
|
|
204
|
+
|
|
205
|
+
### value
|
|
206
|
+
|
|
207
|
+
`number`
|
|
208
|
+
|
|
209
|
+
## Returns
|
|
210
|
+
|
|
211
|
+
`number`
|
|
212
|
+
|
|
213
|
+
### <a id="hexFrom"></a>hexFrom
|
|
214
|
+
|
|
215
|
+
[**@xylabs/hex**](#../README)
|
|
216
|
+
|
|
217
|
+
***
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
function hexFrom(value, config?): Lowercase<string>;
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
Takes unknown value and tries our best to convert it to a hex string
|
|
224
|
+
|
|
225
|
+
## Parameters
|
|
226
|
+
|
|
227
|
+
### value
|
|
228
|
+
|
|
229
|
+
Supported types are string, number, bigint, and ArrayBuffer
|
|
230
|
+
|
|
231
|
+
`string` | `number` | `bigint` | `ArrayBufferLike`
|
|
232
|
+
|
|
233
|
+
### config?
|
|
234
|
+
|
|
235
|
+
[`HexConfig`](#../interfaces/HexConfig)
|
|
236
|
+
|
|
237
|
+
Configuration of output format and validation
|
|
238
|
+
|
|
239
|
+
## Returns
|
|
240
|
+
|
|
241
|
+
`Lowercase`\<`string`\>
|
|
242
|
+
|
|
243
|
+
### <a id="hexFromArrayBuffer"></a>hexFromArrayBuffer
|
|
244
|
+
|
|
245
|
+
[**@xylabs/hex**](#../README)
|
|
246
|
+
|
|
247
|
+
***
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
function hexFromArrayBuffer(buffer, config?): Lowercase<string>;
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Convert an ArrayBuffer to a hex string
|
|
254
|
+
|
|
255
|
+
## Parameters
|
|
256
|
+
|
|
257
|
+
### buffer
|
|
258
|
+
|
|
259
|
+
`ArrayBufferLike`
|
|
260
|
+
|
|
261
|
+
The buffer to be converted
|
|
262
|
+
|
|
263
|
+
### config?
|
|
264
|
+
|
|
265
|
+
[`HexConfig`](#../interfaces/HexConfig)
|
|
266
|
+
|
|
267
|
+
Configuration of output format and validation
|
|
268
|
+
|
|
269
|
+
## Returns
|
|
270
|
+
|
|
271
|
+
`Lowercase`\<`string`\>
|
|
272
|
+
|
|
273
|
+
### <a id="hexFromBigInt"></a>hexFromBigInt
|
|
274
|
+
|
|
275
|
+
[**@xylabs/hex**](#../README)
|
|
276
|
+
|
|
277
|
+
***
|
|
278
|
+
|
|
279
|
+
```ts
|
|
280
|
+
function hexFromBigInt(value, config): Lowercase<string>;
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
Convert a bigint to a hex string
|
|
284
|
+
|
|
285
|
+
## Parameters
|
|
286
|
+
|
|
287
|
+
### value
|
|
288
|
+
|
|
289
|
+
`bigint`
|
|
290
|
+
|
|
291
|
+
The bigint to be converted
|
|
292
|
+
|
|
293
|
+
### config
|
|
294
|
+
|
|
295
|
+
[`HexConfig`](#../interfaces/HexConfig) = `{}`
|
|
296
|
+
|
|
297
|
+
Configuration of output format and validation
|
|
298
|
+
|
|
299
|
+
## Returns
|
|
300
|
+
|
|
301
|
+
`Lowercase`\<`string`\>
|
|
302
|
+
|
|
303
|
+
### <a id="hexFromHexString"></a>hexFromHexString
|
|
304
|
+
|
|
305
|
+
[**@xylabs/hex**](#../README)
|
|
306
|
+
|
|
307
|
+
***
|
|
308
|
+
|
|
309
|
+
```ts
|
|
310
|
+
function hexFromHexString(value, config): Lowercase<string>;
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
## Parameters
|
|
314
|
+
|
|
315
|
+
### value
|
|
316
|
+
|
|
317
|
+
`string`
|
|
318
|
+
|
|
319
|
+
### config
|
|
320
|
+
|
|
321
|
+
[`HexConfig`](#../interfaces/HexConfig) = `{}`
|
|
322
|
+
|
|
323
|
+
## Returns
|
|
324
|
+
|
|
325
|
+
`Lowercase`\<`string`\>
|
|
326
|
+
|
|
327
|
+
### <a id="hexFromNumber"></a>hexFromNumber
|
|
328
|
+
|
|
329
|
+
[**@xylabs/hex**](#../README)
|
|
330
|
+
|
|
331
|
+
***
|
|
332
|
+
|
|
333
|
+
```ts
|
|
334
|
+
function hexFromNumber(value, config?): Lowercase<string>;
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
## Parameters
|
|
338
|
+
|
|
339
|
+
### value
|
|
340
|
+
|
|
341
|
+
`number`
|
|
342
|
+
|
|
343
|
+
### config?
|
|
344
|
+
|
|
345
|
+
[`HexConfig`](#../interfaces/HexConfig)
|
|
346
|
+
|
|
347
|
+
## Returns
|
|
348
|
+
|
|
349
|
+
`Lowercase`\<`string`\>
|
|
350
|
+
|
|
351
|
+
### <a id="hexToBigInt"></a>hexToBigInt
|
|
352
|
+
|
|
353
|
+
[**@xylabs/hex**](#../README)
|
|
354
|
+
|
|
355
|
+
***
|
|
356
|
+
|
|
357
|
+
```ts
|
|
358
|
+
function hexToBigInt(hex): bigint;
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
## Parameters
|
|
362
|
+
|
|
363
|
+
### hex
|
|
364
|
+
|
|
365
|
+
`Lowercase`\<`string`\>
|
|
366
|
+
|
|
367
|
+
## Returns
|
|
368
|
+
|
|
369
|
+
`bigint`
|
|
370
|
+
|
|
371
|
+
### <a id="isAddress"></a>isAddress
|
|
372
|
+
|
|
373
|
+
[**@xylabs/hex**](#../README)
|
|
374
|
+
|
|
375
|
+
***
|
|
376
|
+
|
|
377
|
+
```ts
|
|
378
|
+
function isAddress(value, config): value is Lowercase<string>;
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
## Parameters
|
|
382
|
+
|
|
383
|
+
### value
|
|
384
|
+
|
|
385
|
+
`unknown`
|
|
386
|
+
|
|
387
|
+
### config
|
|
388
|
+
|
|
389
|
+
[`HexConfig`](#../interfaces/HexConfig) = `{}`
|
|
390
|
+
|
|
391
|
+
## Returns
|
|
392
|
+
|
|
393
|
+
`value is Lowercase<string>`
|
|
394
|
+
|
|
395
|
+
### <a id="isHash"></a>isHash
|
|
396
|
+
|
|
397
|
+
[**@xylabs/hex**](#../README)
|
|
398
|
+
|
|
399
|
+
***
|
|
400
|
+
|
|
401
|
+
```ts
|
|
402
|
+
function isHash(value, bitLength): value is Lowercase<string>;
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
## Parameters
|
|
406
|
+
|
|
407
|
+
### value
|
|
408
|
+
|
|
409
|
+
`unknown`
|
|
410
|
+
|
|
411
|
+
### bitLength
|
|
412
|
+
|
|
413
|
+
[`HashBitLength`](#../type-aliases/HashBitLength) = `256`
|
|
414
|
+
|
|
415
|
+
## Returns
|
|
416
|
+
|
|
417
|
+
`value is Lowercase<string>`
|
|
418
|
+
|
|
419
|
+
### <a id="isHashBitLength"></a>isHashBitLength
|
|
420
|
+
|
|
421
|
+
[**@xylabs/hex**](#../README)
|
|
422
|
+
|
|
423
|
+
***
|
|
424
|
+
|
|
425
|
+
```ts
|
|
426
|
+
function isHashBitLength(value): value is HashBitLength;
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
## Parameters
|
|
430
|
+
|
|
431
|
+
### value
|
|
432
|
+
|
|
433
|
+
`unknown`
|
|
434
|
+
|
|
435
|
+
## Returns
|
|
436
|
+
|
|
437
|
+
`value is HashBitLength`
|
|
438
|
+
|
|
439
|
+
### <a id="isHex"></a>isHex
|
|
440
|
+
|
|
441
|
+
[**@xylabs/hex**](#../README)
|
|
442
|
+
|
|
443
|
+
***
|
|
444
|
+
|
|
445
|
+
```ts
|
|
446
|
+
function isHex(value, config?): value is Lowercase<string>;
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
## Parameters
|
|
450
|
+
|
|
451
|
+
### value
|
|
452
|
+
|
|
453
|
+
`unknown`
|
|
454
|
+
|
|
455
|
+
### config?
|
|
456
|
+
|
|
457
|
+
[`HexConfig`](#../interfaces/HexConfig)
|
|
458
|
+
|
|
459
|
+
## Returns
|
|
460
|
+
|
|
461
|
+
`value is Lowercase<string>`
|
|
462
|
+
|
|
463
|
+
### <a id="isHexZero"></a>isHexZero
|
|
464
|
+
|
|
465
|
+
[**@xylabs/hex**](#../README)
|
|
466
|
+
|
|
467
|
+
***
|
|
468
|
+
|
|
469
|
+
```ts
|
|
470
|
+
function isHexZero(value?): undefined | boolean;
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
## Parameters
|
|
474
|
+
|
|
475
|
+
### value?
|
|
476
|
+
|
|
477
|
+
`string`
|
|
478
|
+
|
|
479
|
+
## Returns
|
|
480
|
+
|
|
481
|
+
`undefined` \| `boolean`
|
|
482
|
+
|
|
483
|
+
### <a id="nibblesToBits"></a>nibblesToBits
|
|
484
|
+
|
|
485
|
+
[**@xylabs/hex**](#../README)
|
|
486
|
+
|
|
487
|
+
***
|
|
488
|
+
|
|
489
|
+
```ts
|
|
490
|
+
function nibblesToBits(value): number;
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
## Parameters
|
|
494
|
+
|
|
495
|
+
### value
|
|
496
|
+
|
|
497
|
+
`number`
|
|
498
|
+
|
|
499
|
+
## Returns
|
|
500
|
+
|
|
501
|
+
`number`
|
|
502
|
+
|
|
503
|
+
### <a id="toAddress"></a>toAddress
|
|
504
|
+
|
|
505
|
+
[**@xylabs/hex**](#../README)
|
|
506
|
+
|
|
507
|
+
***
|
|
508
|
+
|
|
509
|
+
```ts
|
|
510
|
+
function toAddress(value, config): Lowercase<string>;
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
## Parameters
|
|
514
|
+
|
|
515
|
+
### value
|
|
516
|
+
|
|
517
|
+
`string` | `number` | `bigint` | `ArrayBufferLike`
|
|
518
|
+
|
|
519
|
+
### config
|
|
520
|
+
|
|
521
|
+
[`HexConfig`](#../interfaces/HexConfig) = `{}`
|
|
522
|
+
|
|
523
|
+
## Returns
|
|
524
|
+
|
|
525
|
+
`Lowercase`\<`string`\>
|
|
526
|
+
|
|
527
|
+
### <a id="toHex"></a>toHex
|
|
528
|
+
|
|
529
|
+
[**@xylabs/hex**](#../README)
|
|
530
|
+
|
|
531
|
+
***
|
|
532
|
+
|
|
533
|
+
```ts
|
|
534
|
+
function toHex(value, config): Lowercase<string>;
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
takes any value and tries our best to convert it to a hex string
|
|
538
|
+
|
|
539
|
+
## Parameters
|
|
540
|
+
|
|
541
|
+
### value
|
|
542
|
+
|
|
543
|
+
Supported types are string, number, bigint, and ArrayBuffer
|
|
544
|
+
|
|
545
|
+
`string` | `number` | `bigint` | `ArrayBufferLike`
|
|
546
|
+
|
|
547
|
+
### config
|
|
548
|
+
|
|
549
|
+
[`HexConfig`](#../interfaces/HexConfig) = `{}`
|
|
550
|
+
|
|
551
|
+
Configuration of output format and validation
|
|
552
|
+
|
|
553
|
+
## Returns
|
|
554
|
+
|
|
555
|
+
`Lowercase`\<`string`\>
|
|
556
|
+
|
|
557
|
+
### <a id="toHexLegacy"></a>toHexLegacy
|
|
558
|
+
|
|
559
|
+
[**@xylabs/hex**](#../README)
|
|
560
|
+
|
|
561
|
+
***
|
|
562
|
+
|
|
563
|
+
```ts
|
|
564
|
+
function toHexLegacy(buffer): string;
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
## Parameters
|
|
568
|
+
|
|
569
|
+
### buffer
|
|
570
|
+
|
|
571
|
+
`ArrayBuffer`
|
|
572
|
+
|
|
573
|
+
## Returns
|
|
574
|
+
|
|
575
|
+
`string`
|
|
576
|
+
|
|
577
|
+
### interfaces
|
|
578
|
+
|
|
579
|
+
### <a id="HexConfig"></a>HexConfig
|
|
580
|
+
|
|
581
|
+
[**@xylabs/hex**](#../README)
|
|
582
|
+
|
|
583
|
+
***
|
|
584
|
+
|
|
585
|
+
Configuration of validation and output format
|
|
586
|
+
|
|
587
|
+
## Properties
|
|
588
|
+
|
|
589
|
+
### bitLength?
|
|
590
|
+
|
|
591
|
+
```ts
|
|
592
|
+
optional bitLength: number;
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
***
|
|
596
|
+
|
|
597
|
+
### byteSize?
|
|
598
|
+
|
|
599
|
+
```ts
|
|
600
|
+
optional byteSize: number;
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
***
|
|
604
|
+
|
|
605
|
+
### prefix?
|
|
606
|
+
|
|
607
|
+
```ts
|
|
608
|
+
optional prefix: boolean;
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
### type-aliases
|
|
612
|
+
|
|
613
|
+
### <a id="Address"></a>Address
|
|
614
|
+
|
|
615
|
+
[**@xylabs/hex**](#../README)
|
|
616
|
+
|
|
617
|
+
***
|
|
618
|
+
|
|
619
|
+
```ts
|
|
620
|
+
type Address = Exclude<Hex, "reserved-address-value">;
|
|
621
|
+
```
|
|
622
|
+
|
|
623
|
+
### <a id="Hash"></a>Hash
|
|
624
|
+
|
|
625
|
+
[**@xylabs/hex**](#../README)
|
|
626
|
+
|
|
627
|
+
***
|
|
628
|
+
|
|
629
|
+
```ts
|
|
630
|
+
type Hash = Exclude<Hex, "reserved-hash-value">;
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
### <a id="HashBitLength"></a>HashBitLength
|
|
634
|
+
|
|
635
|
+
[**@xylabs/hex**](#../README)
|
|
636
|
+
|
|
637
|
+
***
|
|
638
|
+
|
|
639
|
+
```ts
|
|
640
|
+
type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096;
|
|
641
|
+
```
|
|
642
|
+
|
|
643
|
+
### <a id="Hex"></a>Hex
|
|
644
|
+
|
|
645
|
+
[**@xylabs/hex**](#../README)
|
|
646
|
+
|
|
647
|
+
***
|
|
648
|
+
|
|
649
|
+
```ts
|
|
650
|
+
type Hex = Exclude<Lowercase<string>, "reserved-hex-value">;
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
### variables
|
|
654
|
+
|
|
655
|
+
### <a id="HashBitLength"></a>HashBitLength
|
|
656
|
+
|
|
657
|
+
[**@xylabs/hex**](#../README)
|
|
658
|
+
|
|
659
|
+
***
|
|
660
|
+
|
|
661
|
+
```ts
|
|
662
|
+
HashBitLength: HashBitLength[];
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
### <a id="ZERO_ADDRESS"></a>ZERO_ADDRESS
|
|
666
|
+
|
|
667
|
+
[**@xylabs/hex**](#../README)
|
|
668
|
+
|
|
669
|
+
***
|
|
670
|
+
|
|
671
|
+
```ts
|
|
672
|
+
const ZERO_ADDRESS: Address;
|
|
673
|
+
```
|
|
674
|
+
|
|
675
|
+
### <a id="ZERO_HASH"></a>ZERO_HASH
|
|
676
|
+
|
|
677
|
+
[**@xylabs/hex**](#../README)
|
|
678
|
+
|
|
679
|
+
***
|
|
680
|
+
|
|
681
|
+
```ts
|
|
682
|
+
const ZERO_HASH: Hash;
|
|
683
|
+
```
|
|
684
|
+
|
|
685
|
+
### <a id="hexRegex"></a>hexRegex
|
|
686
|
+
|
|
687
|
+
[**@xylabs/hex**](#../README)
|
|
688
|
+
|
|
689
|
+
***
|
|
690
|
+
|
|
691
|
+
```ts
|
|
692
|
+
const hexRegex: RegExp;
|
|
693
|
+
```
|
|
694
|
+
|
|
695
|
+
### <a id="hexRegexWithPrefix"></a>hexRegexWithPrefix
|
|
696
|
+
|
|
697
|
+
[**@xylabs/hex**](#../README)
|
|
698
|
+
|
|
699
|
+
***
|
|
700
|
+
|
|
701
|
+
```ts
|
|
702
|
+
const hexRegexWithPrefix: RegExp;
|
|
703
|
+
```
|
|
20
704
|
|
|
21
|
-
Coming Soon!
|
|
22
705
|
|
|
23
706
|
Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
|
|
24
707
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/hex",
|
|
3
|
-
"version": "4.13.
|
|
3
|
+
"version": "4.13.21",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hex",
|
|
@@ -29,22 +29,25 @@
|
|
|
29
29
|
"exports": {
|
|
30
30
|
".": {
|
|
31
31
|
"types": "./dist/neutral/index.d.ts",
|
|
32
|
+
"source": "./src/index.ts",
|
|
32
33
|
"default": "./dist/neutral/index.mjs"
|
|
33
34
|
},
|
|
34
35
|
"./package.json": "./package.json"
|
|
35
36
|
},
|
|
36
37
|
"module": "./dist/neutral/index.mjs",
|
|
38
|
+
"source": "./src/index.ts",
|
|
37
39
|
"types": "./dist/neutral/index.d.ts",
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"src"
|
|
40
43
|
],
|
|
41
44
|
"dependencies": {
|
|
42
|
-
"@xylabs/typeof": "^4.13.
|
|
45
|
+
"@xylabs/typeof": "^4.13.21"
|
|
43
46
|
},
|
|
44
47
|
"devDependencies": {
|
|
45
|
-
"@xylabs/ts-scripts-yarn3": "^7.0.0
|
|
46
|
-
"@xylabs/tsconfig": "^7.0.0
|
|
47
|
-
"@xylabs/vitest-extended": "^4.13.
|
|
48
|
+
"@xylabs/ts-scripts-yarn3": "^7.0.0",
|
|
49
|
+
"@xylabs/tsconfig": "^7.0.0",
|
|
50
|
+
"@xylabs/vitest-extended": "^4.13.21",
|
|
48
51
|
"typescript": "^5.8.3",
|
|
49
52
|
"vitest": "^3.2.4"
|
|
50
53
|
},
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import '@xylabs/vitest-extended'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
describe, expect, test,
|
|
5
|
+
} from 'vitest'
|
|
6
|
+
|
|
7
|
+
import { asAddress, isAddress } from '../address.ts'
|
|
8
|
+
|
|
9
|
+
describe('address', () => {
|
|
10
|
+
test('isAddress', () => {
|
|
11
|
+
expect(isAddress(true)).toBeFalse()
|
|
12
|
+
expect(isAddress({})).toBeFalse()
|
|
13
|
+
expect(isAddress('NotHex')).toBeFalse()
|
|
14
|
+
expect(isAddress('deadbeef')).toBeFalse()
|
|
15
|
+
expect(isAddress('0xdeadbeef')).toBeFalse()
|
|
16
|
+
expect(isAddress('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', { prefix: true })).toBeTrue()
|
|
17
|
+
expect(isAddress('0xdeadbeefdeadbeefdeadxxxxdeadbeefdeadbeef')).toBeFalse()
|
|
18
|
+
})
|
|
19
|
+
test('asAddress', () => {
|
|
20
|
+
expect(asAddress('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef')).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
|
|
21
|
+
expect(asAddress('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
|
|
22
|
+
expect(asAddress('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', true)).toBe('deadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
|
|
23
|
+
expect(asAddress('000000000000000000000000000000000000000a', true)).toBe('000000000000000000000000000000000000000a')
|
|
24
|
+
})
|
|
25
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import '@xylabs/vitest-extended'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
describe, expect, test,
|
|
5
|
+
} from 'vitest'
|
|
6
|
+
|
|
7
|
+
import { isHash } from '../hash.ts'
|
|
8
|
+
import { hexFromNumber } from '../hex/index.ts'
|
|
9
|
+
|
|
10
|
+
describe('hash', () => {
|
|
11
|
+
test('isHash', () => {
|
|
12
|
+
expect(isHash(true)).toBeFalse()
|
|
13
|
+
expect(isHash({})).toBeFalse()
|
|
14
|
+
expect(isHash('NotHex')).toBeFalse()
|
|
15
|
+
expect(isHash('deadbeef')).toBeFalse()
|
|
16
|
+
expect(isHash('0xdeadbeef')).toBeFalse()
|
|
17
|
+
})
|
|
18
|
+
test('hexFromNumber', () => {
|
|
19
|
+
expect(hexFromNumber(10)).toBe('0a')
|
|
20
|
+
expect(hexFromNumber(10, { bitLength: 256 })).toBe('000000000000000000000000000000000000000000000000000000000000000a')
|
|
21
|
+
expect(hexFromNumber(10, { bitLength: 128 })).toBe('0000000000000000000000000000000a')
|
|
22
|
+
})
|
|
23
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import '@xylabs/vitest-extended'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
describe, expect, test,
|
|
5
|
+
} from 'vitest'
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
hexFromArrayBuffer, hexFromNumber, isHex,
|
|
9
|
+
} from '../hex/index.ts'
|
|
10
|
+
|
|
11
|
+
describe('hex', () => {
|
|
12
|
+
test('isHex', () => {
|
|
13
|
+
expect(isHex(true)).toBeFalse()
|
|
14
|
+
expect(isHex({})).toBeFalse()
|
|
15
|
+
expect(isHex('NotHex')).toBeFalse()
|
|
16
|
+
expect(isHex('deadbeef')).toBeTrue()
|
|
17
|
+
expect(isHex('0xdeadbeef')).toBeFalse()
|
|
18
|
+
})
|
|
19
|
+
test('asHex', () => {
|
|
20
|
+
expect(hexFromArrayBuffer(new ArrayBuffer(2))).toBe('0000')
|
|
21
|
+
expect(hexFromNumber(10)).toBe('0a')
|
|
22
|
+
expect(hexFromNumber(10, { bitLength: 256 })).toBe('000000000000000000000000000000000000000000000000000000000000000a')
|
|
23
|
+
})
|
|
24
|
+
})
|