@xylabs/hex 2.13.11 → 2.13.12
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/hex.d.cts.map +1 -1
- package/dist/browser/hex.d.mts.map +1 -1
- package/dist/browser/hex.d.ts.map +1 -1
- package/dist/browser/index.cjs +2 -1
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +2 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/node/hex.d.cts.map +1 -1
- package/dist/node/hex.d.mts.map +1 -1
- package/dist/node/hex.d.ts.map +1 -1
- package/dist/node/index.cjs +2 -1
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +2 -1
- package/dist/node/index.js.map +1 -1
- package/package.json +4 -1
- package/src/hex.ts +3 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AAEpD,eAAO,MAAM,QAAQ,QAAiB,CAAA;AACtC,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AAExB,eAAO,MAAM,OAAO,WAAY,WAAW,WAE1C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAI5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,YAEzC,CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,GAAG,GAAG,SAAS,CAAA;AACtD,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,CAAA;AAC1E,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,GAAG,CAAA;AAChE,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,CAAC,EAAE,YAAY,GAAG,GAAG,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AAEpD,eAAO,MAAM,QAAQ,QAAiB,CAAA;AACtC,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AAExB,eAAO,MAAM,OAAO,WAAY,WAAW,WAE1C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAI5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,YAEzC,CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,GAAG,GAAG,SAAS,CAAA;AACtD,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,CAAA;AAC1E,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,GAAG,CAAA;AAChE,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,CAAC,EAAE,YAAY,GAAG,GAAG,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AAEpD,eAAO,MAAM,QAAQ,QAAiB,CAAA;AACtC,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AAExB,eAAO,MAAM,OAAO,WAAY,WAAW,WAE1C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAI5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,YAEzC,CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,GAAG,GAAG,SAAS,CAAA;AACtD,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,CAAA;AAC1E,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,GAAG,CAAA;AAChE,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,CAAC,EAAE,YAAY,GAAG,GAAG,CAAA"}
|
package/dist/browser/index.cjs
CHANGED
|
@@ -50,6 +50,7 @@ var assertError = (value, assert, defaultMessage) => {
|
|
|
50
50
|
};
|
|
51
51
|
|
|
52
52
|
// src/hex.ts
|
|
53
|
+
var import_arraybuffer = require("@xylabs/arraybuffer");
|
|
53
54
|
var hexRegex = /^[0-9a-f]+$/i;
|
|
54
55
|
var hexRegexWithPrefix = /0x[0-9a-f]+$/i;
|
|
55
56
|
var hexFrom = (buffer) => {
|
|
@@ -93,7 +94,7 @@ function asHex(value, assertOrBitLength, assertOnly) {
|
|
|
93
94
|
stringValue = value.toString(16);
|
|
94
95
|
break;
|
|
95
96
|
case "object":
|
|
96
|
-
if (
|
|
97
|
+
if ((0, import_arraybuffer.isArrayBuffer)(value)) {
|
|
97
98
|
stringValue = hexFrom(value);
|
|
98
99
|
}
|
|
99
100
|
break;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/assert.ts","../../src/hex.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 { AssertConfig, assertError } from './assert'\n\nexport const hexRegex = /^[0-9a-f]+$/i\nexport const hexRegexWithPrefix = /0x[0-9a-f]+$/i\n\nexport type Hex = string\n\nexport const hexFrom = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n//determine the number of octets for a given number of bits\nexport const bitsToOctets = (value: number): number => {\n const octets = value >> 2\n if (value !== octets << 2) throw Error('Bits for octets must multiple of 8')\n return octets\n}\n\n//determine the number of octets for a given number of bits\nexport const octetsToBits = (value: number): number => {\n return value << 2\n}\n\n//are the number of bit a round octet (factor of 8)?\nexport const isRoundOctet = (value: number) => {\n return (value >> 2) << 2 === value\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 !== bitsToOctets(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, bitLength?: number): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex\nexport function asHex(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Hex | undefined {\n const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : undefined\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n //remove the leading 0x if it is there\n stringValue = hexRegexWithPrefix.test(value) ? value.substring(2) : value\n break\n case 'number':\n if (value === Math.floor(value)) {\n stringValue = value.toString(16)\n } else {\n return assertError(value, assert, 'Numbers must be whole')\n }\n break\n case 'bigint':\n stringValue = value.toString(16)\n break\n case 'object':\n if (value
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/assert.ts","../../src/hex.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 { AssertConfig, assertError } from './assert'\n\nexport const hexRegex = /^[0-9a-f]+$/i\nexport const hexRegexWithPrefix = /0x[0-9a-f]+$/i\n\nexport type Hex = string\n\nexport const hexFrom = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n//determine the number of octets for a given number of bits\nexport const bitsToOctets = (value: number): number => {\n const octets = value >> 2\n if (value !== octets << 2) throw Error('Bits for octets must multiple of 8')\n return octets\n}\n\n//determine the number of octets for a given number of bits\nexport const octetsToBits = (value: number): number => {\n return value << 2\n}\n\n//are the number of bit a round octet (factor of 8)?\nexport const isRoundOctet = (value: number) => {\n return (value >> 2) << 2 === value\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 !== bitsToOctets(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, bitLength?: number): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex\nexport function asHex(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Hex | undefined {\n const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : undefined\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n //remove the leading 0x if it is there\n stringValue = hexRegexWithPrefix.test(value) ? value.substring(2) : value\n break\n case 'number':\n if (value === Math.floor(value)) {\n stringValue = value.toString(16)\n } else {\n return assertError(value, assert, 'Numbers must be whole')\n }\n break\n case 'bigint':\n stringValue = value.toString(16)\n break\n case 'object':\n if (isArrayBuffer(value)) {\n stringValue = hexFrom(value)\n }\n break\n default:\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n\n //make it conform to the bit length if shorter\n if (stringValue && bitLength) {\n stringValue = stringValue.padStart(bitsToOctets(bitLength), '0')\n }\n\n return isHex(stringValue, bitLength) ? stringValue.toLowerCase() : assertError(value, assert, 'Unable to convert to Hash')\n}\n","import { AssertConfig, assertError } from './assert'\nimport { asHex, bitsToOctets, isHex } from './hex'\n\nexport const addressRegex = /0x[0-9a-f]+/i\n\nexport type Address = string\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 !== bitsToOctets(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, bitLength: number): Address | undefined\nexport function asAddress(value: unknown, bitLength: number, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Address | undefined {\n const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : 160\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n const result = `0x${asHex(value, bitLength, assert)}`\n return isAddress(result, bitLength) ? result : assertError(value, assert, 'Resulting value is not an Address')\n}\n","import { AssertConfig, assertError } from './assert'\nimport { asHex, Hex, isHex, octetsToBits } 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 if (!isHex(value, bitLength)) return false\n\n const hex = asHex(value, bitLength)\n if (!hex) return false\n\n if (!isHashBitLength(octetsToBits(hex.length))) return false\n\n return true\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, bitLength?: HashBitLength): Hash | undefined\nexport function asHash(value: unknown, bitLength: HashBitLength | undefined, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assertOrBitLength?: AssertConfig | HashBitLength, assertOnly?: AssertConfig): Hash | undefined {\n const bitLength: HashBitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : 256\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n const result = asHex(value, bitLength, assert)\n return isHash(result, bitLength) ? result : assertError(value, assert, 'Resulting value is not a Hash')\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;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,iBAAiB,OAAO,OAAO,cAAc;AACtI,QAAI,cAAc;AAChB,YAAM,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACnE;AAAA,EACF;AACA,SAAO;AACT;;;ACZA,yBAA8B;AAIvB,IAAM,WAAW;AACjB,IAAM,qBAAqB;AAI3B,IAAM,UAAU,CAAC,WAAwB;AAC9C,SAAO,CAAC,GAAG,IAAI,WAAW,MAAM,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE,KAAK,EAAE;AACxF;AAGO,IAAM,eAAe,CAAC,UAA0B;AACrD,QAAM,SAAS,SAAS;AACxB,MAAI,UAAU,UAAU;AAAG,UAAM,MAAM,oCAAoC;AAC3E,SAAO;AACT;AAGO,IAAM,eAAe,CAAC,UAA0B;AACrD,SAAO,SAAS;AAClB;AAGO,IAAM,eAAe,CAAC,UAAkB;AAC7C,SAAQ,SAA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lC,MAAI,cAAc,UAAa,SAAS,WAAW,aAAa,SAAS;AAAG,WAAO;AAEnF,SAAO,MAAM,UAAU,SAAS;AAClC;AAMO,SAAS,UAAU,OAAgB,mBAA2C,YAAgD;AACnI,QAAM,YAAY,OAAO,sBAAsB,WAAW,oBAAoB;AAC9E,QAAM,SAAS,OAAO,sBAAsB,WAAW,oBAAoB;AAE3E,QAAM,SAAS,KAAK,MAAM,OAAO,WAAW,MAAM,CAAC;AACnD,SAAO,UAAU,QAAQ,SAAS,IAAI,SAAS,YAAY,OAAO,QAAQ,mCAAmC;AAC/G;;;AC5BO,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,MAAI,CAAC,MAAM,OAAO,SAAS;AAAG,WAAO;AAErC,QAAM,MAAM,MAAM,OAAO,SAAS;AAClC,MAAI,CAAC;AAAK,WAAO;AAEjB,MAAI,CAAC,gBAAgB,aAAa,IAAI,MAAM,CAAC;AAAG,WAAO;AAEvD,SAAO;AACT;AAMO,SAAS,OAAO,OAAgB,mBAAkD,YAA6C;AACpI,QAAM,YAA2B,OAAO,sBAAsB,WAAW,oBAAoB;AAC7F,QAAM,SAAS,OAAO,sBAAsB,WAAW,oBAAoB;AAE3E,QAAM,SAAS,MAAM,OAAO,WAAW,MAAM;AAC7C,SAAO,OAAO,QAAQ,SAAS,IAAI,SAAS,YAAY,OAAO,QAAQ,+BAA+B;AACxG;","names":[]}
|
package/dist/browser/index.js
CHANGED
|
@@ -10,6 +10,7 @@ var assertError = (value, assert, defaultMessage) => {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
// src/hex.ts
|
|
13
|
+
import { isArrayBuffer } from "@xylabs/arraybuffer";
|
|
13
14
|
var hexRegex = /^[0-9a-f]+$/i;
|
|
14
15
|
var hexRegexWithPrefix = /0x[0-9a-f]+$/i;
|
|
15
16
|
var hexFrom = (buffer) => {
|
|
@@ -53,7 +54,7 @@ function asHex(value, assertOrBitLength, assertOnly) {
|
|
|
53
54
|
stringValue = value.toString(16);
|
|
54
55
|
break;
|
|
55
56
|
case "object":
|
|
56
|
-
if (value
|
|
57
|
+
if (isArrayBuffer(value)) {
|
|
57
58
|
stringValue = hexFrom(value);
|
|
58
59
|
}
|
|
59
60
|
break;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/assert.ts","../../src/hex.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 { AssertConfig, assertError } from './assert'\n\nexport const hexRegex = /^[0-9a-f]+$/i\nexport const hexRegexWithPrefix = /0x[0-9a-f]+$/i\n\nexport type Hex = string\n\nexport const hexFrom = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n//determine the number of octets for a given number of bits\nexport const bitsToOctets = (value: number): number => {\n const octets = value >> 2\n if (value !== octets << 2) throw Error('Bits for octets must multiple of 8')\n return octets\n}\n\n//determine the number of octets for a given number of bits\nexport const octetsToBits = (value: number): number => {\n return value << 2\n}\n\n//are the number of bit a round octet (factor of 8)?\nexport const isRoundOctet = (value: number) => {\n return (value >> 2) << 2 === value\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 !== bitsToOctets(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, bitLength?: number): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex\nexport function asHex(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Hex | undefined {\n const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : undefined\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n //remove the leading 0x if it is there\n stringValue = hexRegexWithPrefix.test(value) ? value.substring(2) : value\n break\n case 'number':\n if (value === Math.floor(value)) {\n stringValue = value.toString(16)\n } else {\n return assertError(value, assert, 'Numbers must be whole')\n }\n break\n case 'bigint':\n stringValue = value.toString(16)\n break\n case 'object':\n if (value
|
|
1
|
+
{"version":3,"sources":["../../src/assert.ts","../../src/hex.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 { AssertConfig, assertError } from './assert'\n\nexport const hexRegex = /^[0-9a-f]+$/i\nexport const hexRegexWithPrefix = /0x[0-9a-f]+$/i\n\nexport type Hex = string\n\nexport const hexFrom = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n//determine the number of octets for a given number of bits\nexport const bitsToOctets = (value: number): number => {\n const octets = value >> 2\n if (value !== octets << 2) throw Error('Bits for octets must multiple of 8')\n return octets\n}\n\n//determine the number of octets for a given number of bits\nexport const octetsToBits = (value: number): number => {\n return value << 2\n}\n\n//are the number of bit a round octet (factor of 8)?\nexport const isRoundOctet = (value: number) => {\n return (value >> 2) << 2 === value\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 !== bitsToOctets(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, bitLength?: number): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex\nexport function asHex(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Hex | undefined {\n const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : undefined\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n //remove the leading 0x if it is there\n stringValue = hexRegexWithPrefix.test(value) ? value.substring(2) : value\n break\n case 'number':\n if (value === Math.floor(value)) {\n stringValue = value.toString(16)\n } else {\n return assertError(value, assert, 'Numbers must be whole')\n }\n break\n case 'bigint':\n stringValue = value.toString(16)\n break\n case 'object':\n if (isArrayBuffer(value)) {\n stringValue = hexFrom(value)\n }\n break\n default:\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n\n //make it conform to the bit length if shorter\n if (stringValue && bitLength) {\n stringValue = stringValue.padStart(bitsToOctets(bitLength), '0')\n }\n\n return isHex(stringValue, bitLength) ? stringValue.toLowerCase() : assertError(value, assert, 'Unable to convert to Hash')\n}\n","import { AssertConfig, assertError } from './assert'\nimport { asHex, bitsToOctets, isHex } from './hex'\n\nexport const addressRegex = /0x[0-9a-f]+/i\n\nexport type Address = string\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 !== bitsToOctets(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, bitLength: number): Address | undefined\nexport function asAddress(value: unknown, bitLength: number, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Address | undefined {\n const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : 160\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n const result = `0x${asHex(value, bitLength, assert)}`\n return isAddress(result, bitLength) ? result : assertError(value, assert, 'Resulting value is not an Address')\n}\n","import { AssertConfig, assertError } from './assert'\nimport { asHex, Hex, isHex, octetsToBits } 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 if (!isHex(value, bitLength)) return false\n\n const hex = asHex(value, bitLength)\n if (!hex) return false\n\n if (!isHashBitLength(octetsToBits(hex.length))) return false\n\n return true\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, bitLength?: HashBitLength): Hash | undefined\nexport function asHash(value: unknown, bitLength: HashBitLength | undefined, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assertOrBitLength?: AssertConfig | HashBitLength, assertOnly?: AssertConfig): Hash | undefined {\n const bitLength: HashBitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : 256\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n const result = asHex(value, bitLength, assert)\n return isHash(result, bitLength) ? result : assertError(value, assert, 'Resulting value is not a Hash')\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,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACnE;AAAA,EACF;AACA,SAAO;AACT;;;ACZA,SAAS,qBAAqB;AAIvB,IAAM,WAAW;AACjB,IAAM,qBAAqB;AAI3B,IAAM,UAAU,CAAC,WAAwB;AAC9C,SAAO,CAAC,GAAG,IAAI,WAAW,MAAM,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE,KAAK,EAAE;AACxF;AAGO,IAAM,eAAe,CAAC,UAA0B;AACrD,QAAM,SAAS,SAAS;AACxB,MAAI,UAAU,UAAU;AAAG,UAAM,MAAM,oCAAoC;AAC3E,SAAO;AACT;AAGO,IAAM,eAAe,CAAC,UAA0B;AACrD,SAAO,SAAS;AAClB;AAGO,IAAM,eAAe,CAAC,UAAkB;AAC7C,SAAQ,SAAS,KAAM,MAAM;AAC/B;AAEO,IAAM,QAAQ,CAAC,OAAgB,cAAqC;AAEzE,MAAI,OAAO,UAAU;AAAU,WAAO;AAGtC,MAAI,cAAc,UAAa,MAAM,WAAW,aAAa,SAAS;AAAG,WAAO;AAGhF,SAAO,SAAS,KAAK,KAAK;AAC5B;AAMO,SAAS,MAAM,OAAgB,mBAA2C,YAA4C;AAC3H,QAAM,YAAY,OAAO,sBAA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AW,MAAM,CAAC;AACnD,SAAO,UAAU,QAAQ,SAAS,IAAI,SAAS,YAAY,OAAO,QAAQ,mCAAmC;AAC/G;;;AC5BO,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,MAAI,CAAC,MAAM,OAAO,SAAS;AAAG,WAAO;AAErC,QAAM,MAAM,MAAM,OAAO,SAAS;AAClC,MAAI,CAAC;AAAK,WAAO;AAEjB,MAAI,CAAC,gBAAgB,aAAa,IAAI,MAAM,CAAC;AAAG,WAAO;AAEvD,SAAO;AACT;AAMO,SAAS,OAAO,OAAgB,mBAAkD,YAA6C;AACpI,QAAM,YAA2B,OAAO,sBAAsB,WAAW,oBAAoB;AAC7F,QAAM,SAAS,OAAO,sBAAsB,WAAW,oBAAoB;AAE3E,QAAM,SAAS,MAAM,OAAO,WAAW,MAAM;AAC7C,SAAO,OAAO,QAAQ,SAAS,IAAI,SAAS,YAAY,OAAO,QAAQ,+BAA+B;AACxG;","names":[]}
|
package/dist/node/hex.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AAEpD,eAAO,MAAM,QAAQ,QAAiB,CAAA;AACtC,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AAExB,eAAO,MAAM,OAAO,WAAY,WAAW,WAE1C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAI5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,YAEzC,CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,GAAG,GAAG,SAAS,CAAA;AACtD,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,CAAA;AAC1E,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,GAAG,CAAA;AAChE,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,CAAC,EAAE,YAAY,GAAG,GAAG,CAAA"}
|
package/dist/node/hex.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AAEpD,eAAO,MAAM,QAAQ,QAAiB,CAAA;AACtC,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AAExB,eAAO,MAAM,OAAO,WAAY,WAAW,WAE1C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAI5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,YAEzC,CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,GAAG,GAAG,SAAS,CAAA;AACtD,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,CAAA;AAC1E,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,GAAG,CAAA;AAChE,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,CAAC,EAAE,YAAY,GAAG,GAAG,CAAA"}
|
package/dist/node/hex.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AAEpD,eAAO,MAAM,QAAQ,QAAiB,CAAA;AACtC,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AAExB,eAAO,MAAM,OAAO,WAAY,WAAW,WAE1C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAI5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,YAEzC,CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,GAAG,GAAG,SAAS,CAAA;AACtD,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,CAAA;AAC1E,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,GAAG,CAAA;AAChE,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,CAAC,EAAE,YAAY,GAAG,GAAG,CAAA"}
|
package/dist/node/index.cjs
CHANGED
|
@@ -50,6 +50,7 @@ var assertError = (value, assert, defaultMessage) => {
|
|
|
50
50
|
};
|
|
51
51
|
|
|
52
52
|
// src/hex.ts
|
|
53
|
+
var import_arraybuffer = require("@xylabs/arraybuffer");
|
|
53
54
|
var hexRegex = /^[0-9a-f]+$/i;
|
|
54
55
|
var hexRegexWithPrefix = /0x[0-9a-f]+$/i;
|
|
55
56
|
var hexFrom = (buffer) => {
|
|
@@ -93,7 +94,7 @@ function asHex(value, assertOrBitLength, assertOnly) {
|
|
|
93
94
|
stringValue = value.toString(16);
|
|
94
95
|
break;
|
|
95
96
|
case "object":
|
|
96
|
-
if (
|
|
97
|
+
if ((0, import_arraybuffer.isArrayBuffer)(value)) {
|
|
97
98
|
stringValue = hexFrom(value);
|
|
98
99
|
}
|
|
99
100
|
break;
|
package/dist/node/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/assert.ts","../../src/hex.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 { AssertConfig, assertError } from './assert'\n\nexport const hexRegex = /^[0-9a-f]+$/i\nexport const hexRegexWithPrefix = /0x[0-9a-f]+$/i\n\nexport type Hex = string\n\nexport const hexFrom = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n//determine the number of octets for a given number of bits\nexport const bitsToOctets = (value: number): number => {\n const octets = value >> 2\n if (value !== octets << 2) throw Error('Bits for octets must multiple of 8')\n return octets\n}\n\n//determine the number of octets for a given number of bits\nexport const octetsToBits = (value: number): number => {\n return value << 2\n}\n\n//are the number of bit a round octet (factor of 8)?\nexport const isRoundOctet = (value: number) => {\n return (value >> 2) << 2 === value\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 !== bitsToOctets(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, bitLength?: number): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex\nexport function asHex(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Hex | undefined {\n const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : undefined\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n //remove the leading 0x if it is there\n stringValue = hexRegexWithPrefix.test(value) ? value.substring(2) : value\n break\n case 'number':\n if (value === Math.floor(value)) {\n stringValue = value.toString(16)\n } else {\n return assertError(value, assert, 'Numbers must be whole')\n }\n break\n case 'bigint':\n stringValue = value.toString(16)\n break\n case 'object':\n if (value
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/assert.ts","../../src/hex.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 { AssertConfig, assertError } from './assert'\n\nexport const hexRegex = /^[0-9a-f]+$/i\nexport const hexRegexWithPrefix = /0x[0-9a-f]+$/i\n\nexport type Hex = string\n\nexport const hexFrom = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n//determine the number of octets for a given number of bits\nexport const bitsToOctets = (value: number): number => {\n const octets = value >> 2\n if (value !== octets << 2) throw Error('Bits for octets must multiple of 8')\n return octets\n}\n\n//determine the number of octets for a given number of bits\nexport const octetsToBits = (value: number): number => {\n return value << 2\n}\n\n//are the number of bit a round octet (factor of 8)?\nexport const isRoundOctet = (value: number) => {\n return (value >> 2) << 2 === value\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 !== bitsToOctets(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, bitLength?: number): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex\nexport function asHex(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Hex | undefined {\n const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : undefined\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n //remove the leading 0x if it is there\n stringValue = hexRegexWithPrefix.test(value) ? value.substring(2) : value\n break\n case 'number':\n if (value === Math.floor(value)) {\n stringValue = value.toString(16)\n } else {\n return assertError(value, assert, 'Numbers must be whole')\n }\n break\n case 'bigint':\n stringValue = value.toString(16)\n break\n case 'object':\n if (isArrayBuffer(value)) {\n stringValue = hexFrom(value)\n }\n break\n default:\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n\n //make it conform to the bit length if shorter\n if (stringValue && bitLength) {\n stringValue = stringValue.padStart(bitsToOctets(bitLength), '0')\n }\n\n return isHex(stringValue, bitLength) ? stringValue.toLowerCase() : assertError(value, assert, 'Unable to convert to Hash')\n}\n","import { AssertConfig, assertError } from './assert'\nimport { asHex, bitsToOctets, isHex } from './hex'\n\nexport const addressRegex = /0x[0-9a-f]+/i\n\nexport type Address = string\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 !== bitsToOctets(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, bitLength: number): Address | undefined\nexport function asAddress(value: unknown, bitLength: number, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Address | undefined {\n const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : 160\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n const result = `0x${asHex(value, bitLength, assert)}`\n return isAddress(result, bitLength) ? result : assertError(value, assert, 'Resulting value is not an Address')\n}\n","import { AssertConfig, assertError } from './assert'\nimport { asHex, Hex, isHex, octetsToBits } 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 if (!isHex(value, bitLength)) return false\n\n const hex = asHex(value, bitLength)\n if (!hex) return false\n\n if (!isHashBitLength(octetsToBits(hex.length))) return false\n\n return true\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, bitLength?: HashBitLength): Hash | undefined\nexport function asHash(value: unknown, bitLength: HashBitLength | undefined, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assertOrBitLength?: AssertConfig | HashBitLength, assertOnly?: AssertConfig): Hash | undefined {\n const bitLength: HashBitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : 256\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n const result = asHex(value, bitLength, assert)\n return isHash(result, bitLength) ? result : assertError(value, assert, 'Resulting value is not a Hash')\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;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,iBAAiB,OAAO,OAAO,cAAc;AACtI,QAAI,cAAc;AAChB,YAAM,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACnE;AAAA,EACF;AACA,SAAO;AACT;;;ACZA,yBAA8B;AAIvB,IAAM,WAAW;AACjB,IAAM,qBAAqB;AAI3B,IAAM,UAAU,CAAC,WA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W,oBAAoB;AAC7F,QAAM,SAAS,OAAO,sBAAsB,WAAW,oBAAoB;AAE3E,QAAM,SAAS,MAAM,OAAO,WAAW,MAAM;AAC7C,SAAO,OAAO,QAAQ,SAAS,IAAI,SAAS,YAAY,OAAO,QAAQ,+BAA+B;AACxG;","names":[]}
|
package/dist/node/index.js
CHANGED
|
@@ -10,6 +10,7 @@ var assertError = (value, assert, defaultMessage) => {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
// src/hex.ts
|
|
13
|
+
import { isArrayBuffer } from "@xylabs/arraybuffer";
|
|
13
14
|
var hexRegex = /^[0-9a-f]+$/i;
|
|
14
15
|
var hexRegexWithPrefix = /0x[0-9a-f]+$/i;
|
|
15
16
|
var hexFrom = (buffer) => {
|
|
@@ -53,7 +54,7 @@ function asHex(value, assertOrBitLength, assertOnly) {
|
|
|
53
54
|
stringValue = value.toString(16);
|
|
54
55
|
break;
|
|
55
56
|
case "object":
|
|
56
|
-
if (value
|
|
57
|
+
if (isArrayBuffer(value)) {
|
|
57
58
|
stringValue = hexFrom(value);
|
|
58
59
|
}
|
|
59
60
|
break;
|
package/dist/node/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/assert.ts","../../src/hex.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 { AssertConfig, assertError } from './assert'\n\nexport const hexRegex = /^[0-9a-f]+$/i\nexport const hexRegexWithPrefix = /0x[0-9a-f]+$/i\n\nexport type Hex = string\n\nexport const hexFrom = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n//determine the number of octets for a given number of bits\nexport const bitsToOctets = (value: number): number => {\n const octets = value >> 2\n if (value !== octets << 2) throw Error('Bits for octets must multiple of 8')\n return octets\n}\n\n//determine the number of octets for a given number of bits\nexport const octetsToBits = (value: number): number => {\n return value << 2\n}\n\n//are the number of bit a round octet (factor of 8)?\nexport const isRoundOctet = (value: number) => {\n return (value >> 2) << 2 === value\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 !== bitsToOctets(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, bitLength?: number): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex\nexport function asHex(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Hex | undefined {\n const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : undefined\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n //remove the leading 0x if it is there\n stringValue = hexRegexWithPrefix.test(value) ? value.substring(2) : value\n break\n case 'number':\n if (value === Math.floor(value)) {\n stringValue = value.toString(16)\n } else {\n return assertError(value, assert, 'Numbers must be whole')\n }\n break\n case 'bigint':\n stringValue = value.toString(16)\n break\n case 'object':\n if (value
|
|
1
|
+
{"version":3,"sources":["../../src/assert.ts","../../src/hex.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 { AssertConfig, assertError } from './assert'\n\nexport const hexRegex = /^[0-9a-f]+$/i\nexport const hexRegexWithPrefix = /0x[0-9a-f]+$/i\n\nexport type Hex = string\n\nexport const hexFrom = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n//determine the number of octets for a given number of bits\nexport const bitsToOctets = (value: number): number => {\n const octets = value >> 2\n if (value !== octets << 2) throw Error('Bits for octets must multiple of 8')\n return octets\n}\n\n//determine the number of octets for a given number of bits\nexport const octetsToBits = (value: number): number => {\n return value << 2\n}\n\n//are the number of bit a round octet (factor of 8)?\nexport const isRoundOctet = (value: number) => {\n return (value >> 2) << 2 === value\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 !== bitsToOctets(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, bitLength?: number): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex\nexport function asHex(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Hex | undefined {\n const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : undefined\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n //remove the leading 0x if it is there\n stringValue = hexRegexWithPrefix.test(value) ? value.substring(2) : value\n break\n case 'number':\n if (value === Math.floor(value)) {\n stringValue = value.toString(16)\n } else {\n return assertError(value, assert, 'Numbers must be whole')\n }\n break\n case 'bigint':\n stringValue = value.toString(16)\n break\n case 'object':\n if (isArrayBuffer(value)) {\n stringValue = hexFrom(value)\n }\n break\n default:\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n\n //make it conform to the bit length if shorter\n if (stringValue && bitLength) {\n stringValue = stringValue.padStart(bitsToOctets(bitLength), '0')\n }\n\n return isHex(stringValue, bitLength) ? stringValue.toLowerCase() : assertError(value, assert, 'Unable to convert to Hash')\n}\n","import { AssertConfig, assertError } from './assert'\nimport { asHex, bitsToOctets, isHex } from './hex'\n\nexport const addressRegex = /0x[0-9a-f]+/i\n\nexport type Address = string\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 !== bitsToOctets(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, bitLength: number): Address | undefined\nexport function asAddress(value: unknown, bitLength: number, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Address | undefined {\n const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : 160\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n const result = `0x${asHex(value, bitLength, assert)}`\n return isAddress(result, bitLength) ? result : assertError(value, assert, 'Resulting value is not an Address')\n}\n","import { AssertConfig, assertError } from './assert'\nimport { asHex, Hex, isHex, octetsToBits } 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 if (!isHex(value, bitLength)) return false\n\n const hex = asHex(value, bitLength)\n if (!hex) return false\n\n if (!isHashBitLength(octetsToBits(hex.length))) return false\n\n return true\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, bitLength?: HashBitLength): Hash | undefined\nexport function asHash(value: unknown, bitLength: HashBitLength | undefined, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assertOrBitLength?: AssertConfig | HashBitLength, assertOnly?: AssertConfig): Hash | undefined {\n const bitLength: HashBitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : 256\n const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly\n\n const result = asHex(value, bitLength, assert)\n return isHash(result, bitLength) ? result : assertError(value, assert, 'Resulting value is not a Hash')\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,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACnE;AAAA,EACF;AACA,SAAO;AACT;;;ACZA,SAAS,qBAAqB;AAIvB,IAAM,WAAW;AACjB,IAAM,qBAAqB;AAI3B,IAAM,UAAU,CAAC,WAAwB;AAC9C,SAAO,CAAC,GAAG,IAAI,WAAW,MAAM,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE,KAAK,EAAE;AACxF;AAGO,IAAM,eAAe,CAAC,UAA0B;AACrD,QAAM,SAAS,SAAS;AACxB,MAAI,UAAU,UAAU;AAAG,UAAM,MAAM,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|
|
38
38
|
"esm"
|
|
39
39
|
],
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@xylabs/arraybuffer": "~2.13.12"
|
|
42
|
+
},
|
|
40
43
|
"devDependencies": {
|
|
41
44
|
"@xylabs/ts-scripts-yarn3": "^3.2.10",
|
|
42
45
|
"@xylabs/tsconfig": "^3.2.10",
|
|
@@ -50,7 +53,7 @@
|
|
|
50
53
|
"url": "https://github.com/xylabs/sdk-js.git"
|
|
51
54
|
},
|
|
52
55
|
"sideEffects": false,
|
|
53
|
-
"version": "2.13.
|
|
56
|
+
"version": "2.13.12",
|
|
54
57
|
"packageManager": "yarn@3.3.1",
|
|
55
58
|
"type": "module"
|
|
56
59
|
}
|
package/src/hex.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isArrayBuffer } from '@xylabs/arraybuffer'
|
|
2
|
+
|
|
1
3
|
import { AssertConfig, assertError } from './assert'
|
|
2
4
|
|
|
3
5
|
export const hexRegex = /^[0-9a-f]+$/i
|
|
@@ -63,7 +65,7 @@ export function asHex(value: unknown, assertOrBitLength?: AssertConfig | number,
|
|
|
63
65
|
stringValue = value.toString(16)
|
|
64
66
|
break
|
|
65
67
|
case 'object':
|
|
66
|
-
if (value
|
|
68
|
+
if (isArrayBuffer(value)) {
|
|
67
69
|
stringValue = hexFrom(value)
|
|
68
70
|
}
|
|
69
71
|
break
|