@xylabs/hex 2.13.19 → 2.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/dist/browser/address.d.cts +0 -1
- package/dist/browser/address.d.cts.map +1 -1
- package/dist/browser/address.d.mts +0 -1
- package/dist/browser/address.d.mts.map +1 -1
- package/dist/browser/address.d.ts +0 -1
- package/dist/browser/address.d.ts.map +1 -1
- package/dist/browser/assert.d.cts.map +1 -1
- package/dist/browser/assert.d.mts.map +1 -1
- package/dist/browser/assert.d.ts.map +1 -1
- package/dist/browser/hex/{from.d.cts → from/from.d.cts} +1 -2
- package/dist/browser/hex/from/from.d.cts.map +1 -0
- package/dist/{node/hex → browser/hex/from}/from.d.mts +1 -2
- package/dist/browser/hex/from/from.d.mts.map +1 -0
- package/dist/{node/hex → browser/hex/from}/from.d.ts +1 -2
- package/dist/browser/hex/from/from.d.ts.map +1 -0
- package/dist/browser/hex/from/fromHexString.d.cts +3 -0
- package/dist/browser/hex/from/fromHexString.d.cts.map +1 -0
- package/dist/browser/hex/from/fromHexString.d.mts +3 -0
- package/dist/browser/hex/from/fromHexString.d.mts.map +1 -0
- package/dist/browser/hex/from/fromHexString.d.ts +3 -0
- package/dist/browser/hex/from/fromHexString.d.ts.map +1 -0
- package/dist/browser/hex/from/index.d.cts +3 -0
- package/dist/browser/hex/from/index.d.cts.map +1 -0
- package/dist/browser/hex/from/index.d.mts +3 -0
- package/dist/browser/hex/from/index.d.mts.map +1 -0
- package/dist/browser/hex/from/index.d.ts +3 -0
- package/dist/browser/hex/from/index.d.ts.map +1 -0
- package/dist/browser/hex/index.d.cts +1 -0
- package/dist/browser/hex/index.d.cts.map +1 -1
- package/dist/browser/hex/index.d.mts +1 -0
- package/dist/browser/hex/index.d.mts.map +1 -1
- package/dist/browser/hex/index.d.ts +1 -0
- package/dist/browser/hex/index.d.ts.map +1 -1
- package/dist/browser/hex/is.d.cts +2 -2
- package/dist/browser/hex/is.d.cts.map +1 -1
- package/dist/browser/hex/is.d.mts +2 -2
- package/dist/browser/hex/is.d.mts.map +1 -1
- package/dist/browser/hex/is.d.ts +2 -2
- package/dist/browser/hex/is.d.ts.map +1 -1
- package/dist/browser/hex/isHexZero.d.cts +2 -0
- package/dist/browser/hex/isHexZero.d.cts.map +1 -0
- package/dist/browser/hex/isHexZero.d.mts +2 -0
- package/dist/browser/hex/isHexZero.d.mts.map +1 -0
- package/dist/browser/hex/isHexZero.d.ts +2 -0
- package/dist/browser/hex/isHexZero.d.ts.map +1 -0
- package/dist/browser/hex/regex.d.cts.map +1 -1
- package/dist/browser/hex/regex.d.mts.map +1 -1
- package/dist/browser/hex/regex.d.ts.map +1 -1
- package/dist/browser/index.cjs +54 -47
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +54 -47
- package/dist/browser/index.js.map +1 -1
- package/dist/node/address.d.cts +0 -1
- package/dist/node/address.d.cts.map +1 -1
- package/dist/node/address.d.mts +0 -1
- package/dist/node/address.d.mts.map +1 -1
- package/dist/node/address.d.ts +0 -1
- package/dist/node/address.d.ts.map +1 -1
- package/dist/node/assert.d.cts.map +1 -1
- package/dist/node/assert.d.mts.map +1 -1
- package/dist/node/assert.d.ts.map +1 -1
- package/dist/node/hex/{from.d.cts → from/from.d.cts} +1 -2
- package/dist/node/hex/from/from.d.cts.map +1 -0
- package/dist/{browser/hex → node/hex/from}/from.d.mts +1 -2
- package/dist/node/hex/from/from.d.mts.map +1 -0
- package/dist/{browser/hex → node/hex/from}/from.d.ts +1 -2
- package/dist/node/hex/from/from.d.ts.map +1 -0
- package/dist/node/hex/from/fromHexString.d.cts +3 -0
- package/dist/node/hex/from/fromHexString.d.cts.map +1 -0
- package/dist/node/hex/from/fromHexString.d.mts +3 -0
- package/dist/node/hex/from/fromHexString.d.mts.map +1 -0
- package/dist/node/hex/from/fromHexString.d.ts +3 -0
- package/dist/node/hex/from/fromHexString.d.ts.map +1 -0
- package/dist/node/hex/from/index.d.cts +3 -0
- package/dist/node/hex/from/index.d.cts.map +1 -0
- package/dist/node/hex/from/index.d.mts +3 -0
- package/dist/node/hex/from/index.d.mts.map +1 -0
- package/dist/node/hex/from/index.d.ts +3 -0
- package/dist/node/hex/from/index.d.ts.map +1 -0
- package/dist/node/hex/index.d.cts +1 -0
- package/dist/node/hex/index.d.cts.map +1 -1
- package/dist/node/hex/index.d.mts +1 -0
- package/dist/node/hex/index.d.mts.map +1 -1
- package/dist/node/hex/index.d.ts +1 -0
- package/dist/node/hex/index.d.ts.map +1 -1
- package/dist/node/hex/is.d.cts +2 -2
- package/dist/node/hex/is.d.cts.map +1 -1
- package/dist/node/hex/is.d.mts +2 -2
- package/dist/node/hex/is.d.mts.map +1 -1
- package/dist/node/hex/is.d.ts +2 -2
- package/dist/node/hex/is.d.ts.map +1 -1
- package/dist/node/hex/isHexZero.d.cts +2 -0
- package/dist/node/hex/isHexZero.d.cts.map +1 -0
- package/dist/node/hex/isHexZero.d.mts +2 -0
- package/dist/node/hex/isHexZero.d.mts.map +1 -0
- package/dist/node/hex/isHexZero.d.ts +2 -0
- package/dist/node/hex/isHexZero.d.ts.map +1 -0
- package/dist/node/hex/regex.d.cts.map +1 -1
- package/dist/node/hex/regex.d.mts.map +1 -1
- package/dist/node/hex/regex.d.ts.map +1 -1
- package/dist/node/index.cjs +54 -48
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +54 -47
- package/dist/node/index.js.map +1 -1
- package/package.json +9 -6
- package/src/address.ts +6 -17
- package/src/assert.ts +2 -1
- package/src/hash.ts +5 -3
- package/src/hex/as.ts +4 -2
- package/src/hex/{from.ts → from/from.ts} +15 -22
- package/src/hex/from/fromHexString.ts +15 -0
- package/src/hex/from/index.ts +2 -0
- package/src/hex/index.ts +1 -0
- package/src/hex/is.ts +7 -10
- package/src/hex/isHexZero.ts +5 -0
- package/src/hex/nibble.ts +1 -1
- package/src/hex/regex.ts +2 -2
- package/dist/browser/hex/from.d.cts.map +0 -1
- package/dist/browser/hex/from.d.mts.map +0 -1
- package/dist/browser/hex/from.d.ts.map +0 -1
- package/dist/node/hex/from.d.cts.map +0 -1
- package/dist/node/hex/from.d.mts.map +0 -1
- package/dist/node/hex/from.d.ts.map +0 -1
package/dist/node/index.cjs
CHANGED
|
@@ -21,7 +21,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
HashBitLength: () => HashBitLength,
|
|
24
|
-
addressRegex: () => addressRegex,
|
|
25
24
|
asAddress: () => asAddress,
|
|
26
25
|
asHash: () => asHash,
|
|
27
26
|
asHex: () => asHex,
|
|
@@ -50,20 +49,20 @@ var assertError = (value, assert, defaultMessage) => {
|
|
|
50
49
|
if (assert) {
|
|
51
50
|
const assertString = typeof assert === "string" ? assert : typeof assert === "boolean" ? defaultMessage : assert(value, defaultMessage);
|
|
52
51
|
if (assertString) {
|
|
53
|
-
throw Error(assertString === true ? defaultMessage : assertString);
|
|
52
|
+
throw new Error(assertString === true ? defaultMessage : assertString);
|
|
54
53
|
}
|
|
55
54
|
}
|
|
56
55
|
return void 0;
|
|
57
56
|
};
|
|
58
57
|
|
|
59
|
-
// src/hex/from.ts
|
|
58
|
+
// src/hex/from/from.ts
|
|
60
59
|
var import_arraybuffer = require("@xylabs/arraybuffer");
|
|
61
60
|
|
|
62
61
|
// src/hex/nibble.ts
|
|
63
62
|
var bitsToNibbles = (value) => {
|
|
64
63
|
const nibbles = value >> 2;
|
|
65
64
|
if (value !== nibbles << 2)
|
|
66
|
-
throw Error("Bits for nibbles must multiple of 4");
|
|
65
|
+
throw new Error("Bits for nibbles must multiple of 4");
|
|
67
66
|
return nibbles;
|
|
68
67
|
};
|
|
69
68
|
var nibblesToBits = (value) => {
|
|
@@ -71,22 +70,33 @@ var nibblesToBits = (value) => {
|
|
|
71
70
|
};
|
|
72
71
|
|
|
73
72
|
// src/hex/regex.ts
|
|
74
|
-
var hexRegex = /^[
|
|
75
|
-
var hexRegexWithPrefix = /0x[
|
|
73
|
+
var hexRegex = /^[\da-f]+$/i;
|
|
74
|
+
var hexRegexWithPrefix = /0x[\da-f]+$/i;
|
|
76
75
|
|
|
77
76
|
// src/hex/is.ts
|
|
78
|
-
var
|
|
79
|
-
return BigInt(hexFromHexString(value, { prefix: true })) === 0n;
|
|
80
|
-
};
|
|
81
|
-
var isHex = (value, bitLength) => {
|
|
77
|
+
var isHex = (value, config) => {
|
|
82
78
|
if (typeof value !== "string")
|
|
83
79
|
return false;
|
|
84
|
-
|
|
80
|
+
const valueCharLength = (config == null ? void 0 : config.prefix) ? value.length - 2 : value.length;
|
|
81
|
+
if ((config == null ? void 0 : config.bitLength) !== void 0 && valueCharLength !== bitsToNibbles(config == null ? void 0 : config.bitLength))
|
|
85
82
|
return false;
|
|
86
|
-
return hexRegex.test(value);
|
|
83
|
+
return (config == null ? void 0 : config.prefix) ? hexRegexWithPrefix.test(value) : hexRegex.test(value);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// src/hex/from/fromHexString.ts
|
|
87
|
+
var hexFromHexString = (value, config = {}) => {
|
|
88
|
+
const { prefix = false, byteSize = 8 } = config;
|
|
89
|
+
const nibbleBoundary = bitsToNibbles(byteSize);
|
|
90
|
+
const unPadded = (value.startsWith("0x") ? value.slice(2) : value).toLowerCase();
|
|
91
|
+
if (isHex(unPadded)) {
|
|
92
|
+
const padded = unPadded.padStart(unPadded.length + unPadded.length % nibbleBoundary, "0");
|
|
93
|
+
return prefix ? `0x${padded}` : padded;
|
|
94
|
+
} else {
|
|
95
|
+
throw new Error("Received string is not a value hex");
|
|
96
|
+
}
|
|
87
97
|
};
|
|
88
98
|
|
|
89
|
-
// src/hex/from.ts
|
|
99
|
+
// src/hex/from/from.ts
|
|
90
100
|
var hexFromArrayBuffer = (buffer, config) => {
|
|
91
101
|
const unPadded = [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
92
102
|
return hexFromHexString(unPadded, config);
|
|
@@ -100,33 +110,27 @@ var hexFromBigInt = (value, config = {}) => {
|
|
|
100
110
|
var hexFromNumber = (value, config) => {
|
|
101
111
|
return hexFromBigInt(BigInt(value), config);
|
|
102
112
|
};
|
|
103
|
-
var hexFromHexString = (value, config = {}) => {
|
|
104
|
-
const { prefix = false, byteSize = 8 } = config;
|
|
105
|
-
const nibbleBoundary = bitsToNibbles(byteSize);
|
|
106
|
-
const unPadded = (value.startsWith("0x") ? value.substring(2) : value).toLowerCase();
|
|
107
|
-
if (isHex(unPadded)) {
|
|
108
|
-
const padded = unPadded.padStart(unPadded.length + unPadded.length % nibbleBoundary, "0");
|
|
109
|
-
return prefix ? `0x${padded}` : padded;
|
|
110
|
-
} else {
|
|
111
|
-
throw Error("Received string is not a value hex");
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
113
|
var hexFrom = (value, config) => {
|
|
115
114
|
switch (typeof value) {
|
|
116
|
-
case "string":
|
|
115
|
+
case "string": {
|
|
117
116
|
return hexFromHexString(value, config);
|
|
118
|
-
|
|
117
|
+
}
|
|
118
|
+
case "bigint": {
|
|
119
119
|
return hexFromBigInt(value, config);
|
|
120
|
-
|
|
120
|
+
}
|
|
121
|
+
case "number": {
|
|
121
122
|
return hexFromNumber(value, config);
|
|
122
|
-
|
|
123
|
+
}
|
|
124
|
+
case "object": {
|
|
123
125
|
if ((0, import_arraybuffer.isArrayBuffer)(value)) {
|
|
124
126
|
return hexFromArrayBuffer(value, config);
|
|
125
127
|
} else {
|
|
126
|
-
throw Error("Invalid type: object !== ArrayBuffer");
|
|
128
|
+
throw new Error("Invalid type: object !== ArrayBuffer");
|
|
127
129
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
+
}
|
|
131
|
+
default: {
|
|
132
|
+
throw new Error(`Invalid type: ${typeof value}`);
|
|
133
|
+
}
|
|
130
134
|
}
|
|
131
135
|
};
|
|
132
136
|
|
|
@@ -134,15 +138,22 @@ var hexFrom = (value, config) => {
|
|
|
134
138
|
function asHex(value, assert) {
|
|
135
139
|
let stringValue = void 0;
|
|
136
140
|
switch (typeof value) {
|
|
137
|
-
case "string":
|
|
141
|
+
case "string": {
|
|
138
142
|
stringValue = hexFromHexString(value);
|
|
139
143
|
break;
|
|
140
|
-
|
|
144
|
+
}
|
|
145
|
+
default: {
|
|
141
146
|
return assertError(value, assert, `Unsupported type [${typeof value}]`);
|
|
147
|
+
}
|
|
142
148
|
}
|
|
143
149
|
return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`);
|
|
144
150
|
}
|
|
145
151
|
|
|
152
|
+
// src/hex/isHexZero.ts
|
|
153
|
+
var isHexZero = (value) => {
|
|
154
|
+
return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : void 0;
|
|
155
|
+
};
|
|
156
|
+
|
|
146
157
|
// src/hex/legacy.ts
|
|
147
158
|
var toHexLegacy = (buffer) => {
|
|
148
159
|
return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
@@ -152,29 +163,23 @@ var toHex = (buffer) => {
|
|
|
152
163
|
};
|
|
153
164
|
|
|
154
165
|
// src/address.ts
|
|
155
|
-
var addressRegex = /0x[0-9a-f]+/i;
|
|
156
166
|
var toAddress = (value, config = {}) => {
|
|
157
167
|
const { bitLength = 160, prefix = true } = config;
|
|
158
168
|
return hexFrom(value, { bitLength, prefix, ...config });
|
|
159
169
|
};
|
|
160
170
|
var isAddress = (value, bitLength = 160) => {
|
|
161
|
-
|
|
162
|
-
return false;
|
|
163
|
-
if (!addressRegex.test(value))
|
|
164
|
-
return false;
|
|
165
|
-
const valueHex = value.substring(2);
|
|
166
|
-
if (bitLength !== void 0 && valueHex.length !== bitsToNibbles(bitLength))
|
|
167
|
-
return false;
|
|
168
|
-
return isHex(valueHex, bitLength);
|
|
171
|
+
return isHex(value, { bitLength, prefix: true });
|
|
169
172
|
};
|
|
170
173
|
function asAddress(value, assert) {
|
|
171
174
|
let stringValue = void 0;
|
|
172
175
|
switch (typeof value) {
|
|
173
|
-
case "string":
|
|
176
|
+
case "string": {
|
|
174
177
|
stringValue = hexFromHexString(value, { prefix: true });
|
|
175
178
|
break;
|
|
176
|
-
|
|
179
|
+
}
|
|
180
|
+
default: {
|
|
177
181
|
return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : void 0;
|
|
182
|
+
}
|
|
178
183
|
}
|
|
179
184
|
return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`);
|
|
180
185
|
}
|
|
@@ -185,23 +190,24 @@ var isHashBitLength = (value) => {
|
|
|
185
190
|
return typeof value === "number" && HashBitLength.includes(value);
|
|
186
191
|
};
|
|
187
192
|
var isHash = (value, bitLength = 256) => {
|
|
188
|
-
return isHex(value, bitLength);
|
|
193
|
+
return isHex(value, { bitLength });
|
|
189
194
|
};
|
|
190
195
|
function asHash(value, assert) {
|
|
191
196
|
let stringValue = void 0;
|
|
192
197
|
switch (typeof value) {
|
|
193
|
-
case "string":
|
|
198
|
+
case "string": {
|
|
194
199
|
stringValue = hexFromHexString(value);
|
|
195
200
|
break;
|
|
196
|
-
|
|
201
|
+
}
|
|
202
|
+
default: {
|
|
197
203
|
return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : void 0;
|
|
204
|
+
}
|
|
198
205
|
}
|
|
199
206
|
return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`);
|
|
200
207
|
}
|
|
201
208
|
// Annotate the CommonJS export names for ESM import in node:
|
|
202
209
|
0 && (module.exports = {
|
|
203
210
|
HashBitLength,
|
|
204
|
-
addressRegex,
|
|
205
211
|
asAddress,
|
|
206
212
|
asHash,
|
|
207
213
|
asHex,
|
package/dist/node/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/assert.ts","../../src/hex/from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/as.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export * from './address'\nexport * from './hash'\nexport * from './hex'\n","export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString = typeof assert === 'string' ? assert : typeof assert === 'boolean' ? defaultMessage : assert(value, defaultMessage)\n if (assertString) {\n throw Error(assertString === true ? defaultMessage : assertString)\n }\n }\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { isHex } from './is'\nimport { HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): string => {\n const unPadded = [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n return hexFromHexString(unPadded, config)\n}\n\nexport const hexFromBigInt = (value: bigint, config: HexConfig = {}): string => {\n const { bitLength } = config\n const unPadded = value.toString(16)\n const padded = bitLength === undefined ? unPadded : unPadded.padStart(bitsToNibbles(bitLength), '0')\n return hexFromHexString(padded, config)\n}\n\nexport const hexFromNumber = (value: number, config?: HexConfig): string => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): string => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.substring(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return prefix ? `0x${padded}` : padded\n } else {\n throw Error('Received string is not a value hex')\n }\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): string => {\n switch (typeof value) {\n case 'string':\n return hexFromHexString(value, config)\n case 'bigint':\n return hexFromBigInt(value, config)\n case 'number':\n return hexFromNumber(value, config)\n case 'object':\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw Error('Invalid type: object !== ArrayBuffer')\n }\n default:\n throw Error(`Invalid type: ${typeof value}`)\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[0-9a-f]+$/i\nexport const hexRegexWithPrefix = /0x[0-9a-f]+$/i\n","import { hexFromHexString } from './from'\nimport { Hex } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex } from './regex'\n\nexport const isHexZero = (value: string) => {\n return BigInt(hexFromHexString(value, { prefix: true })) === 0n\n}\n\nexport const isHex = (value: unknown, bitLength?: number): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n //If a bitLength specified, does it conform?\n if (bitLength !== undefined && value.length !== bitsToNibbles(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n stringValue = hexFromHexString(value)\n break\n default:\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { bitsToNibbles, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport const addressRegex = /0x[0-9a-f]+/i\n\nexport type Address = string\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = true } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, bitLength = 160): value is Address => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n //Does it only has hex values and leading 0x?\n if (!addressRegex.test(value)) return false\n\n const valueHex = value.substring(2)\n\n //If a bitLength specified, does it conform?\n if (bitLength !== undefined && valueHex.length !== bitsToNibbles(bitLength)) return false\n\n return isHex(valueHex, bitLength)\n}\n\nexport function asAddress(value: unknown): Address | undefined\nexport function asAddress(value: unknown, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n stringValue = hexFromHexString(value, { prefix: true })\n break\n default:\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Hex\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, bitLength)\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n stringValue = hexFromHexString(value)\n break\n default:\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) :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Ac,YAAY,OAAO,QAAQ,4BAA4B,KAAK,GAAG;AAC/G;;;ACpCO,IAAM,gBAAiC,CAAC,IAAI,IAAI,KAAK,KAAK,KAAK,MAAM,MAAM,IAAI;AAE/E,IAAM,kBAAkB,CAAC,UAA2C;AACzE,SAAO,OAAO,UAAU,YAAY,cAAc,SAAS,KAAsB;AACnF;AAGO,IAAM,SAAS,CAAC,OAAgB,YAA2B,QAAuB;AACvF,SAAO,MAAM,OAAO,SAAS;AAC/B;AAIO,SAAS,OAAO,OAAgB,QAAyC;AAC9E,MAAI,cAAkC;AAEtC,UAAQ,OAAO,OAAO;AAAA,IACpB,KAAK;AACH,oBAAc,iBAAiB,KAAK;AACpC;AAAA,IACF;AACE,aAAO,SAAS,YAAY,OAAO,QAAQ,qBAAqB,OAAO,KAAK,GAAG,IAAI;AAAA,EACvF;AACA,SAAO,OAAO,WAAW,IAAI,cAAc,YAAY,OAAO,QAAQ,wBAAwB,KAAK,GAAG;AACxG;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/assert.ts","../../src/hex/from/from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/from/fromHexString.ts","../../src/hex/as.ts","../../src/hex/isHexZero.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export * from './address'\nexport * from './hash'\nexport * from './hex'\n","export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString = typeof assert === 'string' ? assert : typeof assert === 'boolean' ? defaultMessage : assert(value, defaultMessage)\n if (assertString) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\nimport { hexFromHexString } from './fromHexString'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): string => {\n const unPadded = [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n return hexFromHexString(unPadded, config)\n}\n\nexport const hexFromBigInt = (value: bigint, config: HexConfig = {}): string => {\n const { bitLength } = config\n const unPadded = value.toString(16)\n const padded = bitLength === undefined ? unPadded : unPadded.padStart(bitsToNibbles(bitLength), '0')\n return hexFromHexString(padded, config)\n}\n\nexport const hexFromNumber = (value: number, config?: HexConfig): string => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): string => {\n switch (typeof value) {\n case 'string': {\n return hexFromHexString(value, config)\n }\n case 'bigint': {\n return hexFromBigInt(value, config)\n }\n case 'number': {\n return hexFromNumber(value, config)\n }\n case 'object': {\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw new Error('Invalid type: object !== ArrayBuffer')\n }\n }\n default: {\n throw new Error(`Invalid type: ${typeof value}`)\n }\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[\\da-f]+$/i\nexport const hexRegexWithPrefix = /0x[\\da-f]+$/i\n","import { Hex, HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex, hexRegexWithPrefix } from './regex'\n\nexport const isHex = (value: unknown, config?: HexConfig): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n const valueCharLength = config?.prefix ? value.length - 2 : value.length\n\n //If a bitLength specified, does it conform?\n if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false\n\n //Does it only has hex values?\n return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value)\n}\n","import { isHex } from '../is'\nimport { HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): string => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.slice(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return prefix ? `0x${padded}` : padded\n } else {\n throw new Error('Received string is not a value hex')\n }\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","import { hexFromHexString } from './from'\n\nexport const isHexZero = (value?: string) => {\n return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : undefined\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport type Address = string\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = true } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, bitLength = 160): value is Address => {\n return isHex(value, { bitLength, prefix: true })\n}\n\nexport function asAddress(value: unknown): Address | undefined\nexport function asAddress(value: unknown, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value, { prefix: true })\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Hex\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACIO,IAAM,cAAc,CAAC,OAAgB,QAAkC,mBAA2B;AACvG,MAAI,QAAQ;AACV,UAAM,eAAe,OAAO,WAAW,WAAW,SAAS,OAAO,WAAW,YAAY,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,YAA2B,QAAuB;AACvF,SAAO,MAAM,OAAO,EAAE,UAAU,CAAC;AACnC;AAIO,SAAS,OAAO,OAAgB,QAAyC;AAC9E,MAAI,cAAkC;AAEtC,UAAQ,OAAO,OAAO;AAAA,IACpB,KAAK,UAAU;AACb,oBAAc,iBAAiB,KAAK;AACpC;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,SAAS,YAAY,OAAO,QAAQ,qBAAqB,OAAO,KAAK,GAAG,IAAI;AAAA,IACrF;AAAA,EACF;AACA,SAAO,OAAO,WAAW,IAAI,cAAc,YAAY,OAAO,QAAQ,wBAAwB,KAAK,GAAG;AACxG;","names":[]}
|
package/dist/node/index.js
CHANGED
|
@@ -3,20 +3,20 @@ var assertError = (value, assert, defaultMessage) => {
|
|
|
3
3
|
if (assert) {
|
|
4
4
|
const assertString = typeof assert === "string" ? assert : typeof assert === "boolean" ? defaultMessage : assert(value, defaultMessage);
|
|
5
5
|
if (assertString) {
|
|
6
|
-
throw Error(assertString === true ? defaultMessage : assertString);
|
|
6
|
+
throw new Error(assertString === true ? defaultMessage : assertString);
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
return void 0;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
// src/hex/from.ts
|
|
12
|
+
// src/hex/from/from.ts
|
|
13
13
|
import { isArrayBuffer } from "@xylabs/arraybuffer";
|
|
14
14
|
|
|
15
15
|
// src/hex/nibble.ts
|
|
16
16
|
var bitsToNibbles = (value) => {
|
|
17
17
|
const nibbles = value >> 2;
|
|
18
18
|
if (value !== nibbles << 2)
|
|
19
|
-
throw Error("Bits for nibbles must multiple of 4");
|
|
19
|
+
throw new Error("Bits for nibbles must multiple of 4");
|
|
20
20
|
return nibbles;
|
|
21
21
|
};
|
|
22
22
|
var nibblesToBits = (value) => {
|
|
@@ -24,22 +24,33 @@ var nibblesToBits = (value) => {
|
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
// src/hex/regex.ts
|
|
27
|
-
var hexRegex = /^[
|
|
28
|
-
var hexRegexWithPrefix = /0x[
|
|
27
|
+
var hexRegex = /^[\da-f]+$/i;
|
|
28
|
+
var hexRegexWithPrefix = /0x[\da-f]+$/i;
|
|
29
29
|
|
|
30
30
|
// src/hex/is.ts
|
|
31
|
-
var
|
|
32
|
-
return BigInt(hexFromHexString(value, { prefix: true })) === 0n;
|
|
33
|
-
};
|
|
34
|
-
var isHex = (value, bitLength) => {
|
|
31
|
+
var isHex = (value, config) => {
|
|
35
32
|
if (typeof value !== "string")
|
|
36
33
|
return false;
|
|
37
|
-
|
|
34
|
+
const valueCharLength = (config == null ? void 0 : config.prefix) ? value.length - 2 : value.length;
|
|
35
|
+
if ((config == null ? void 0 : config.bitLength) !== void 0 && valueCharLength !== bitsToNibbles(config == null ? void 0 : config.bitLength))
|
|
38
36
|
return false;
|
|
39
|
-
return hexRegex.test(value);
|
|
37
|
+
return (config == null ? void 0 : config.prefix) ? hexRegexWithPrefix.test(value) : hexRegex.test(value);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/hex/from/fromHexString.ts
|
|
41
|
+
var hexFromHexString = (value, config = {}) => {
|
|
42
|
+
const { prefix = false, byteSize = 8 } = config;
|
|
43
|
+
const nibbleBoundary = bitsToNibbles(byteSize);
|
|
44
|
+
const unPadded = (value.startsWith("0x") ? value.slice(2) : value).toLowerCase();
|
|
45
|
+
if (isHex(unPadded)) {
|
|
46
|
+
const padded = unPadded.padStart(unPadded.length + unPadded.length % nibbleBoundary, "0");
|
|
47
|
+
return prefix ? `0x${padded}` : padded;
|
|
48
|
+
} else {
|
|
49
|
+
throw new Error("Received string is not a value hex");
|
|
50
|
+
}
|
|
40
51
|
};
|
|
41
52
|
|
|
42
|
-
// src/hex/from.ts
|
|
53
|
+
// src/hex/from/from.ts
|
|
43
54
|
var hexFromArrayBuffer = (buffer, config) => {
|
|
44
55
|
const unPadded = [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
45
56
|
return hexFromHexString(unPadded, config);
|
|
@@ -53,33 +64,27 @@ var hexFromBigInt = (value, config = {}) => {
|
|
|
53
64
|
var hexFromNumber = (value, config) => {
|
|
54
65
|
return hexFromBigInt(BigInt(value), config);
|
|
55
66
|
};
|
|
56
|
-
var hexFromHexString = (value, config = {}) => {
|
|
57
|
-
const { prefix = false, byteSize = 8 } = config;
|
|
58
|
-
const nibbleBoundary = bitsToNibbles(byteSize);
|
|
59
|
-
const unPadded = (value.startsWith("0x") ? value.substring(2) : value).toLowerCase();
|
|
60
|
-
if (isHex(unPadded)) {
|
|
61
|
-
const padded = unPadded.padStart(unPadded.length + unPadded.length % nibbleBoundary, "0");
|
|
62
|
-
return prefix ? `0x${padded}` : padded;
|
|
63
|
-
} else {
|
|
64
|
-
throw Error("Received string is not a value hex");
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
67
|
var hexFrom = (value, config) => {
|
|
68
68
|
switch (typeof value) {
|
|
69
|
-
case "string":
|
|
69
|
+
case "string": {
|
|
70
70
|
return hexFromHexString(value, config);
|
|
71
|
-
|
|
71
|
+
}
|
|
72
|
+
case "bigint": {
|
|
72
73
|
return hexFromBigInt(value, config);
|
|
73
|
-
|
|
74
|
+
}
|
|
75
|
+
case "number": {
|
|
74
76
|
return hexFromNumber(value, config);
|
|
75
|
-
|
|
77
|
+
}
|
|
78
|
+
case "object": {
|
|
76
79
|
if (isArrayBuffer(value)) {
|
|
77
80
|
return hexFromArrayBuffer(value, config);
|
|
78
81
|
} else {
|
|
79
|
-
throw Error("Invalid type: object !== ArrayBuffer");
|
|
82
|
+
throw new Error("Invalid type: object !== ArrayBuffer");
|
|
80
83
|
}
|
|
81
|
-
|
|
82
|
-
|
|
84
|
+
}
|
|
85
|
+
default: {
|
|
86
|
+
throw new Error(`Invalid type: ${typeof value}`);
|
|
87
|
+
}
|
|
83
88
|
}
|
|
84
89
|
};
|
|
85
90
|
|
|
@@ -87,15 +92,22 @@ var hexFrom = (value, config) => {
|
|
|
87
92
|
function asHex(value, assert) {
|
|
88
93
|
let stringValue = void 0;
|
|
89
94
|
switch (typeof value) {
|
|
90
|
-
case "string":
|
|
95
|
+
case "string": {
|
|
91
96
|
stringValue = hexFromHexString(value);
|
|
92
97
|
break;
|
|
93
|
-
|
|
98
|
+
}
|
|
99
|
+
default: {
|
|
94
100
|
return assertError(value, assert, `Unsupported type [${typeof value}]`);
|
|
101
|
+
}
|
|
95
102
|
}
|
|
96
103
|
return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`);
|
|
97
104
|
}
|
|
98
105
|
|
|
106
|
+
// src/hex/isHexZero.ts
|
|
107
|
+
var isHexZero = (value) => {
|
|
108
|
+
return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : void 0;
|
|
109
|
+
};
|
|
110
|
+
|
|
99
111
|
// src/hex/legacy.ts
|
|
100
112
|
var toHexLegacy = (buffer) => {
|
|
101
113
|
return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
@@ -105,29 +117,23 @@ var toHex = (buffer) => {
|
|
|
105
117
|
};
|
|
106
118
|
|
|
107
119
|
// src/address.ts
|
|
108
|
-
var addressRegex = /0x[0-9a-f]+/i;
|
|
109
120
|
var toAddress = (value, config = {}) => {
|
|
110
121
|
const { bitLength = 160, prefix = true } = config;
|
|
111
122
|
return hexFrom(value, { bitLength, prefix, ...config });
|
|
112
123
|
};
|
|
113
124
|
var isAddress = (value, bitLength = 160) => {
|
|
114
|
-
|
|
115
|
-
return false;
|
|
116
|
-
if (!addressRegex.test(value))
|
|
117
|
-
return false;
|
|
118
|
-
const valueHex = value.substring(2);
|
|
119
|
-
if (bitLength !== void 0 && valueHex.length !== bitsToNibbles(bitLength))
|
|
120
|
-
return false;
|
|
121
|
-
return isHex(valueHex, bitLength);
|
|
125
|
+
return isHex(value, { bitLength, prefix: true });
|
|
122
126
|
};
|
|
123
127
|
function asAddress(value, assert) {
|
|
124
128
|
let stringValue = void 0;
|
|
125
129
|
switch (typeof value) {
|
|
126
|
-
case "string":
|
|
130
|
+
case "string": {
|
|
127
131
|
stringValue = hexFromHexString(value, { prefix: true });
|
|
128
132
|
break;
|
|
129
|
-
|
|
133
|
+
}
|
|
134
|
+
default: {
|
|
130
135
|
return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : void 0;
|
|
136
|
+
}
|
|
131
137
|
}
|
|
132
138
|
return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`);
|
|
133
139
|
}
|
|
@@ -138,22 +144,23 @@ var isHashBitLength = (value) => {
|
|
|
138
144
|
return typeof value === "number" && HashBitLength.includes(value);
|
|
139
145
|
};
|
|
140
146
|
var isHash = (value, bitLength = 256) => {
|
|
141
|
-
return isHex(value, bitLength);
|
|
147
|
+
return isHex(value, { bitLength });
|
|
142
148
|
};
|
|
143
149
|
function asHash(value, assert) {
|
|
144
150
|
let stringValue = void 0;
|
|
145
151
|
switch (typeof value) {
|
|
146
|
-
case "string":
|
|
152
|
+
case "string": {
|
|
147
153
|
stringValue = hexFromHexString(value);
|
|
148
154
|
break;
|
|
149
|
-
|
|
155
|
+
}
|
|
156
|
+
default: {
|
|
150
157
|
return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : void 0;
|
|
158
|
+
}
|
|
151
159
|
}
|
|
152
160
|
return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`);
|
|
153
161
|
}
|
|
154
162
|
export {
|
|
155
163
|
HashBitLength,
|
|
156
|
-
addressRegex,
|
|
157
164
|
asAddress,
|
|
158
165
|
asHash,
|
|
159
166
|
asHex,
|
package/dist/node/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/assert.ts","../../src/hex/from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/as.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString = typeof assert === 'string' ? assert : typeof assert === 'boolean' ? defaultMessage : assert(value, defaultMessage)\n if (assertString) {\n throw Error(assertString === true ? defaultMessage : assertString)\n }\n }\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { isHex } from './is'\nimport { HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): string => {\n const unPadded = [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n return hexFromHexString(unPadded, config)\n}\n\nexport const hexFromBigInt = (value: bigint, config: HexConfig = {}): string => {\n const { bitLength } = config\n const unPadded = value.toString(16)\n const padded = bitLength === undefined ? unPadded : unPadded.padStart(bitsToNibbles(bitLength), '0')\n return hexFromHexString(padded, config)\n}\n\nexport const hexFromNumber = (value: number, config?: HexConfig): string => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): string => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.substring(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return prefix ? `0x${padded}` : padded\n } else {\n throw Error('Received string is not a value hex')\n }\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): string => {\n switch (typeof value) {\n case 'string':\n return hexFromHexString(value, config)\n case 'bigint':\n return hexFromBigInt(value, config)\n case 'number':\n return hexFromNumber(value, config)\n case 'object':\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw Error('Invalid type: object !== ArrayBuffer')\n }\n default:\n throw Error(`Invalid type: ${typeof value}`)\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[0-9a-f]+$/i\nexport const hexRegexWithPrefix = /0x[0-9a-f]+$/i\n","import { hexFromHexString } from './from'\nimport { Hex } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex } from './regex'\n\nexport const isHexZero = (value: string) => {\n return BigInt(hexFromHexString(value, { prefix: true })) === 0n\n}\n\nexport const isHex = (value: unknown, bitLength?: number): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n //If a bitLength specified, does it conform?\n if (bitLength !== undefined && value.length !== bitsToNibbles(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n stringValue = hexFromHexString(value)\n break\n default:\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { bitsToNibbles, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport const addressRegex = /0x[0-9a-f]+/i\n\nexport type Address = string\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = true } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, bitLength = 160): value is Address => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n //Does it only has hex values and leading 0x?\n if (!addressRegex.test(value)) return false\n\n const valueHex = value.substring(2)\n\n //If a bitLength specified, does it conform?\n if (bitLength !== undefined && valueHex.length !== bitsToNibbles(bitLength)) return false\n\n return isHex(valueHex, bitLength)\n}\n\nexport function asAddress(value: unknown): Address | undefined\nexport function asAddress(value: unknown, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n stringValue = hexFromHexString(value, { prefix: true })\n break\n default:\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Hex\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, bitLength)\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n stringValue = hexFromHexString(value)\n break\n default:\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n"],"mappings":";AAIO,IAAM,cAAc,CAAC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,MAAM,OAAO,SAAS;AAC/B;AAIO,SAAS,OAAO,OAAgB,QAAyC;AAC9E,MAAI,cAAkC;AAEtC,UAAQ,OAAO,OAAO;AAAA,IACpB,KAAK;AACH,oBAAc,iBAAiB,KAAK;AACpC;AAAA,IACF;AACE,aAAO,SAAS,YAAY,OAAO,QAAQ,qBAAqB,OAAO,KAAK,GAAG,IAAI;AAAA,EACvF;AACA,SAAO,OAAO,WAAW,IAAI,cAAc,YAAY,OAAO,QAAQ,wBAAwB,KAAK,GAAG;AACxG;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/assert.ts","../../src/hex/from/from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/from/fromHexString.ts","../../src/hex/as.ts","../../src/hex/isHexZero.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString = typeof assert === 'string' ? assert : typeof assert === 'boolean' ? defaultMessage : assert(value, defaultMessage)\n if (assertString) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\nimport { hexFromHexString } from './fromHexString'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): string => {\n const unPadded = [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n return hexFromHexString(unPadded, config)\n}\n\nexport const hexFromBigInt = (value: bigint, config: HexConfig = {}): string => {\n const { bitLength } = config\n const unPadded = value.toString(16)\n const padded = bitLength === undefined ? unPadded : unPadded.padStart(bitsToNibbles(bitLength), '0')\n return hexFromHexString(padded, config)\n}\n\nexport const hexFromNumber = (value: number, config?: HexConfig): string => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): string => {\n switch (typeof value) {\n case 'string': {\n return hexFromHexString(value, config)\n }\n case 'bigint': {\n return hexFromBigInt(value, config)\n }\n case 'number': {\n return hexFromNumber(value, config)\n }\n case 'object': {\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw new Error('Invalid type: object !== ArrayBuffer')\n }\n }\n default: {\n throw new Error(`Invalid type: ${typeof value}`)\n }\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[\\da-f]+$/i\nexport const hexRegexWithPrefix = /0x[\\da-f]+$/i\n","import { Hex, HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex, hexRegexWithPrefix } from './regex'\n\nexport const isHex = (value: unknown, config?: HexConfig): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n const valueCharLength = config?.prefix ? value.length - 2 : value.length\n\n //If a bitLength specified, does it conform?\n if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false\n\n //Does it only has hex values?\n return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value)\n}\n","import { isHex } from '../is'\nimport { HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): string => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.slice(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return prefix ? `0x${padded}` : padded\n } else {\n throw new Error('Received string is not a value hex')\n }\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","import { hexFromHexString } from './from'\n\nexport const isHexZero = (value?: string) => {\n return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : undefined\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport type Address = string\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = true } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, bitLength = 160): value is Address => {\n return isHex(value, { bitLength, prefix: true })\n}\n\nexport function asAddress(value: unknown): Address | undefined\nexport function asAddress(value: unknown, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value, { prefix: true })\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Hex\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n"],"mappings":";AAIO,IAAM,cAAc,CAAC,OAAgB,QAAkC,mBAA2B;AACvG,MAAI,QAAQ;AACV,UAAM,eAAe,OAAO,WAAW,WAAW,SAAS,OAAO,WAAW,YAAY,iBAAiB,OAAO,OAAO,cAAc;AACtI,QAAI,cAAc;AAChB,YAAM,IAAI,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACvE;AAAA,EACF;AAEA,SAAO;AACT;;;ACbA,SAAS,qBAAqB;;;ACCvB,IAAM,gBAAgB,CAAC,UAA0B;AACtD,QAAM,UAAU,SAAS;AACzB,MAAI,UAAU,WAAW;AAAG,UAAM,IAAI,MAAM,qCAAqC;AACjF,SAAO;AACT;AAGO,IAAM,gBAAgB,CAAC,UAA0B;AACtD,SAAO,SAAS;AAClB;;;ACVO,IAAM,WAAW;AACjB,IAAM,qBAAqB;;;ACG3B,IAAM,QAAQ,CAAC,OAAgB,WAAqC;AAEzE,MAAI,OAAO,UAAU;AAAU,WAAO;AAEtC,QAAM,mBAAkB,i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homepage": "https://xylabs.com",
|
|
34
34
|
"keywords": [
|
|
35
|
+
"hex",
|
|
35
36
|
"xylabs",
|
|
36
37
|
"utility",
|
|
37
38
|
"typescript",
|
|
38
39
|
"esm"
|
|
39
40
|
],
|
|
40
41
|
"dependencies": {
|
|
41
|
-
"@xylabs/arraybuffer": "~2.13.
|
|
42
|
+
"@xylabs/arraybuffer": "~2.13.21"
|
|
42
43
|
},
|
|
43
44
|
"devDependencies": {
|
|
44
|
-
"@xylabs/ts-scripts-yarn3": "^3.2.
|
|
45
|
-
"@xylabs/tsconfig": "^3.2.
|
|
46
|
-
"typescript": "^5.3.
|
|
45
|
+
"@xylabs/ts-scripts-yarn3": "^3.2.25",
|
|
46
|
+
"@xylabs/tsconfig": "^3.2.25",
|
|
47
|
+
"typescript": "^5.3.3"
|
|
48
|
+
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=18"
|
|
47
51
|
},
|
|
48
52
|
"publishConfig": {
|
|
49
53
|
"access": "public"
|
|
@@ -53,7 +57,6 @@
|
|
|
53
57
|
"url": "https://github.com/xylabs/sdk-js.git"
|
|
54
58
|
},
|
|
55
59
|
"sideEffects": false,
|
|
56
|
-
"version": "2.13.
|
|
57
|
-
"packageManager": "yarn@3.3.1",
|
|
60
|
+
"version": "2.13.21",
|
|
58
61
|
"type": "module"
|
|
59
62
|
}
|
package/src/address.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { AssertConfig, assertError } from './assert'
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export const addressRegex = /0x[0-9a-f]+/i
|
|
2
|
+
import { HexConfig, hexFrom, hexFromHexString, isHex } from './hex'
|
|
5
3
|
|
|
6
4
|
export type Address = string
|
|
7
5
|
|
|
@@ -11,18 +9,7 @@ export const toAddress = (value: unknown, config: HexConfig = {}) => {
|
|
|
11
9
|
}
|
|
12
10
|
|
|
13
11
|
export const isAddress = (value: unknown, bitLength = 160): value is Address => {
|
|
14
|
-
|
|
15
|
-
if (typeof value !== 'string') return false
|
|
16
|
-
|
|
17
|
-
//Does it only has hex values and leading 0x?
|
|
18
|
-
if (!addressRegex.test(value)) return false
|
|
19
|
-
|
|
20
|
-
const valueHex = value.substring(2)
|
|
21
|
-
|
|
22
|
-
//If a bitLength specified, does it conform?
|
|
23
|
-
if (bitLength !== undefined && valueHex.length !== bitsToNibbles(bitLength)) return false
|
|
24
|
-
|
|
25
|
-
return isHex(valueHex, bitLength)
|
|
12
|
+
return isHex(value, { bitLength, prefix: true })
|
|
26
13
|
}
|
|
27
14
|
|
|
28
15
|
export function asAddress(value: unknown): Address | undefined
|
|
@@ -31,11 +18,13 @@ export function asAddress(value: unknown, assert?: AssertConfig): Address | unde
|
|
|
31
18
|
let stringValue: string | undefined = undefined
|
|
32
19
|
|
|
33
20
|
switch (typeof value) {
|
|
34
|
-
case 'string':
|
|
21
|
+
case 'string': {
|
|
35
22
|
stringValue = hexFromHexString(value, { prefix: true })
|
|
36
23
|
break
|
|
37
|
-
|
|
24
|
+
}
|
|
25
|
+
default: {
|
|
38
26
|
return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined
|
|
27
|
+
}
|
|
39
28
|
}
|
|
40
29
|
return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)
|
|
41
30
|
}
|
package/src/assert.ts
CHANGED
|
@@ -6,8 +6,9 @@ export const assertError = (value: unknown, assert: AssertConfig | undefined, de
|
|
|
6
6
|
if (assert) {
|
|
7
7
|
const assertString = typeof assert === 'string' ? assert : typeof assert === 'boolean' ? defaultMessage : assert(value, defaultMessage)
|
|
8
8
|
if (assertString) {
|
|
9
|
-
throw Error(assertString === true ? defaultMessage : assertString)
|
|
9
|
+
throw new Error(assertString === true ? defaultMessage : assertString)
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
+
// eslint-disable-next-line unicorn/no-useless-undefined
|
|
12
13
|
return undefined
|
|
13
14
|
}
|
package/src/hash.ts
CHANGED
|
@@ -10,7 +10,7 @@ export const isHashBitLength = (value: unknown): value is HashBitLength => {
|
|
|
10
10
|
|
|
11
11
|
export type Hash = Hex
|
|
12
12
|
export const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {
|
|
13
|
-
return isHex(value, bitLength)
|
|
13
|
+
return isHex(value, { bitLength })
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export function asHash(value: unknown): Hash | undefined
|
|
@@ -19,11 +19,13 @@ export function asHash(value: unknown, assert?: AssertConfig): Hash | undefined
|
|
|
19
19
|
let stringValue: string | undefined = undefined
|
|
20
20
|
|
|
21
21
|
switch (typeof value) {
|
|
22
|
-
case 'string':
|
|
22
|
+
case 'string': {
|
|
23
23
|
stringValue = hexFromHexString(value)
|
|
24
24
|
break
|
|
25
|
-
|
|
25
|
+
}
|
|
26
|
+
default: {
|
|
26
27
|
return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined
|
|
28
|
+
}
|
|
27
29
|
}
|
|
28
30
|
return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)
|
|
29
31
|
}
|
package/src/hex/as.ts
CHANGED
|
@@ -9,11 +9,13 @@ export function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {
|
|
|
9
9
|
let stringValue: string | undefined = undefined
|
|
10
10
|
|
|
11
11
|
switch (typeof value) {
|
|
12
|
-
case 'string':
|
|
12
|
+
case 'string': {
|
|
13
13
|
stringValue = hexFromHexString(value)
|
|
14
14
|
break
|
|
15
|
-
|
|
15
|
+
}
|
|
16
|
+
default: {
|
|
16
17
|
return assertError(value, assert, `Unsupported type [${typeof value}]`)
|
|
18
|
+
}
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)
|