@xylabs/hex 2.13.13 → 2.13.15

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.
@@ -1 +1 @@
1
- {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAS,GAAG,EAAuB,MAAM,OAAO,CAAA;AAEvD,MAAM,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAA;AAC1E,eAAO,MAAM,aAAa,EAAE,aAAa,EAA8C,CAAA;AAEvF,eAAO,MAAM,eAAe,UAAW,OAAO,2BAE7C,CAAA;AAED,MAAM,MAAM,IAAI,GAAG,GAAG,CAAA;AACtB,eAAO,MAAM,MAAM,UAAW,OAAO,cAAa,aAAa,oBAS9D,CAAA;AAED,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,CAAA;AACxD,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA;AAClE,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,SAAS,CAAA;AACnF,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,SAAS,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA"}
1
+ {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAS,GAAG,EAAwB,MAAM,OAAO,CAAA;AAExD,MAAM,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG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1 +1 @@
1
- {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAS,GAAG,EAAuB,MAAM,OAAO,CAAA;AAEvD,MAAM,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAA;AAC1E,eAAO,MAAM,aAAa,EAAE,aAAa,EAA8C,CAAA;AAEvF,eAAO,MAAM,eAAe,UAAW,OAAO,2BAE7C,CAAA;AAED,MAAM,MAAM,IAAI,GAAG,GAAG,CAAA;AACtB,eAAO,MAAM,MAAM,UAAW,OAAO,cAAa,aAAa,oBAS9D,CAAA;AAED,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,CAAA;AACxD,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA;AAClE,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,SAAS,CAAA;AACnF,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,SAAS,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA"}
1
+ {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAS,GAAG,EAAwB,MAAM,OAAO,CAAA;AAExD,MAAM,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAA;AAC1E,eAAO,MAAM,aAAa,EAAE,aAAa,EAA8C,CAAA;AAEvF,eAAO,MAAM,eAAe,UAAW,OAAO,2BAE7C,CAAA;AAED,MAAM,MAAM,IAAI,GAAG,GAAG,CAAA;AACtB,eAAO,MAAM,MAAM,UAAW,OAAO,cAAa,aAAa,oBAS9D,CAAA;AAED,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,CAAA;AACxD,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA;AAClE,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,SAAS,CAAA;AACnF,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,SAAS,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA"}
@@ -3,10 +3,11 @@ export declare const hexRegex: RegExp;
3
3
  export declare const hexRegexWithPrefix: RegExp;
4
4
  export type Hex = string;
5
5
  export declare const hexFrom: (buffer: ArrayBuffer) => string;
6
- export declare const bitsToOctets: (value: number) => number;
7
- export declare const octetsToBits: (value: number) => number;
8
- export declare const isRoundOctet: (value: number) => boolean;
6
+ export declare const bitsToNibbles: (value: number) => number;
7
+ export declare const nibblesToBits: (value: number) => number;
8
+ export declare const isRoundNibble: (value: number) => boolean;
9
9
  export declare const isHex: (value: unknown, bitLength?: number) => value is string;
10
+ export declare const toHex: (buffer: ArrayBuffer) => string;
10
11
  export declare function asHex(value: unknown): Hex | undefined;
11
12
  export declare function asHex(value: unknown, bitLength?: number): Hex | undefined;
12
13
  export declare function asHex(value: unknown, assert: AssertConfig): Hex;
@@ -1 +1 @@
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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c,MAAM,oBASvD,CAAA;AAED,eAAO,MAAM,KAAK,WAAY,WAAW,WAExC,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"}
@@ -3,10 +3,11 @@ export declare const hexRegex: RegExp;
3
3
  export declare const hexRegexWithPrefix: RegExp;
4
4
  export type Hex = string;
5
5
  export declare const hexFrom: (buffer: ArrayBuffer) => string;
6
- export declare const bitsToOctets: (value: number) => number;
7
- export declare const octetsToBits: (value: number) => number;
8
- export declare const isRoundOctet: (value: number) => boolean;
6
+ export declare const bitsToNibbles: (value: number) => number;
7
+ export declare const nibblesToBits: (value: number) => number;
8
+ export declare const isRoundNibble: (value: number) => boolean;
9
9
  export declare const isHex: (value: unknown, bitLength?: number) => value is string;
10
+ export declare const toHex: (buffer: ArrayBuffer) => string;
10
11
  export declare function asHex(value: unknown): Hex | undefined;
11
12
  export declare function asHex(value: unknown, bitLength?: number): Hex | undefined;
12
13
  export declare function asHex(value: unknown, assert: AssertConfig): Hex;
@@ -1 +1 @@
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,eAA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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"}
@@ -3,10 +3,11 @@ export declare const hexRegex: RegExp;
3
3
  export declare const hexRegexWithPrefix: RegExp;
4
4
  export type Hex = string;
5
5
  export declare const hexFrom: (buffer: ArrayBuffer) => string;
6
- export declare const bitsToOctets: (value: number) => number;
7
- export declare const octetsToBits: (value: number) => number;
8
- export declare const isRoundOctet: (value: number) => boolean;
6
+ export declare const bitsToNibbles: (value: number) => number;
7
+ export declare const nibblesToBits: (value: number) => number;
8
+ export declare const isRoundNibble: (value: number) => boolean;
9
9
  export declare const isHex: (value: unknown, bitLength?: number) => value is string;
10
+ export declare const toHex: (buffer: ArrayBuffer) => string;
10
11
  export declare function asHex(value: unknown): Hex | undefined;
11
12
  export declare function asHex(value: unknown, bitLength?: number): Hex | undefined;
12
13
  export declare function asHex(value: unknown, assert: AssertConfig): Hex;
@@ -1 +1 @@
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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AAE,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"}
@@ -25,7 +25,7 @@ __export(src_exports, {
25
25
  asAddress: () => asAddress,
26
26
  asHash: () => asHash,
27
27
  asHex: () => asHex,
28
- bitsToOctets: () => bitsToOctets,
28
+ bitsToNibbles: () => bitsToNibbles,
29
29
  hexFrom: () => hexFrom,
30
30
  hexRegex: () => hexRegex,
31
31
  hexRegexWithPrefix: () => hexRegexWithPrefix,
@@ -33,8 +33,9 @@ __export(src_exports, {
33
33
  isHash: () => isHash,
34
34
  isHashBitLength: () => isHashBitLength,
35
35
  isHex: () => isHex,
36
- isRoundOctet: () => isRoundOctet,
37
- octetsToBits: () => octetsToBits
36
+ isRoundNibble: () => isRoundNibble,
37
+ nibblesToBits: () => nibblesToBits,
38
+ toHex: () => toHex
38
39
  });
39
40
  module.exports = __toCommonJS(src_exports);
40
41
 
@@ -56,25 +57,28 @@ var hexRegexWithPrefix = /0x[0-9a-f]+$/i;
56
57
  var hexFrom = (buffer) => {
57
58
  return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
58
59
  };
59
- var bitsToOctets = (value) => {
60
- const octets = value >> 2;
61
- if (value !== octets << 2)
62
- throw Error("Bits for octets must multiple of 8");
63
- return octets;
60
+ var bitsToNibbles = (value) => {
61
+ const nibbles = value >> 2;
62
+ if (value !== nibbles << 2)
63
+ throw Error("Bits for nibbles must multiple of 4");
64
+ return nibbles;
64
65
  };
65
- var octetsToBits = (value) => {
66
+ var nibblesToBits = (value) => {
66
67
  return value << 2;
67
68
  };
68
- var isRoundOctet = (value) => {
69
+ var isRoundNibble = (value) => {
69
70
  return value >> 2 << 2 === value;
70
71
  };
71
72
  var isHex = (value, bitLength) => {
72
73
  if (typeof value !== "string")
73
74
  return false;
74
- if (bitLength !== void 0 && value.length !== bitsToOctets(bitLength))
75
+ if (bitLength !== void 0 && value.length !== bitsToNibbles(bitLength))
75
76
  return false;
76
77
  return hexRegex.test(value);
77
78
  };
79
+ var toHex = (buffer) => {
80
+ return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
81
+ };
78
82
  function asHex(value, assertOrBitLength, assertOnly) {
79
83
  const bitLength = typeof assertOrBitLength === "number" ? assertOrBitLength : void 0;
80
84
  const assert = typeof assertOrBitLength !== "number" ? assertOrBitLength : assertOnly;
@@ -104,7 +108,7 @@ function asHex(value, assertOrBitLength, assertOnly) {
104
108
  return assertError(value, assert, `Unsupported type [${typeof value}]`);
105
109
  }
106
110
  if (stringValue && bitLength) {
107
- stringValue = stringValue.padStart(bitsToOctets(bitLength), "0");
111
+ stringValue = stringValue.padStart(bitsToNibbles(bitLength), "0");
108
112
  }
109
113
  return isHex(stringValue, bitLength) ? stringValue.toLowerCase() : assertError(value, assert, "Unable to convert to Hash");
110
114
  }
@@ -117,7 +121,7 @@ var isAddress = (value, bitLength = 160) => {
117
121
  if (!addressRegex.test(value))
118
122
  return false;
119
123
  const valueHex = value.substring(2);
120
- if (bitLength !== void 0 && valueHex.length !== bitsToOctets(bitLength))
124
+ if (bitLength !== void 0 && valueHex.length !== bitsToNibbles(bitLength))
121
125
  return false;
122
126
  return isHex(valueHex, bitLength);
123
127
  };
@@ -139,7 +143,7 @@ var isHash = (value, bitLength = 256) => {
139
143
  const hex = asHex(value, bitLength);
140
144
  if (!hex)
141
145
  return false;
142
- if (!isHashBitLength(octetsToBits(hex.length)))
146
+ if (!isHashBitLength(nibblesToBits(hex.length)))
143
147
  return false;
144
148
  return true;
145
149
  };
@@ -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 { 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 } else {\n return assertError(value, assert, 'Unsupported object')\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,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O,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":[]}
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 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\n//are the number of bit a round nibble (factor of 4)?\nexport const isRoundNibble = (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 !== bitsToNibbles(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n\nexport const toHex = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\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 } else {\n return assertError(value, assert, 'Unsupported object')\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(bitsToNibbles(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, bitsToNibbles, 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 !== 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, 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, nibblesToBits } 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(nibblesToBits(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;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,gBAAgB,CAAC,UAA0B;AACtD,QAAM,UAAU,SAAS;AACzB,MAAI,UAAU,WAAW;AAAG,UAAM,MAAM,qCAAqC;AAC7E,SAAO;AACT;AAGO,IAAM,gBAAgB,CAAC,UAA0B;AACtD,SAAO,SAAS;AAClB;AAGO,IAAM,gBAAgB,CAAC,UAAkB;AAC9C,SAAQ,SAAS,KAAM,MAAM;AAC/B;AAEO,IAAM,QAAQ,CAAC,OAAgB,cAAqC;AAEzE,MAAI,OAAO,UAAU;AAAU,WAAO;AAGtC,MAAI,cAAc,UAAa,MAAM,WAAW,cAAc,SAAS;AAAG,WAA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String(16).padStart(2, "0")).join("");
18
18
  };
19
- var bitsToOctets = (value) => {
20
- const octets = value >> 2;
21
- if (value !== octets << 2)
22
- throw Error("Bits for octets must multiple of 8");
23
- return octets;
19
+ var bitsToNibbles = (value) => {
20
+ const nibbles = value >> 2;
21
+ if (value !== nibbles << 2)
22
+ throw Error("Bits for nibbles must multiple of 4");
23
+ return nibbles;
24
24
  };
25
- var octetsToBits = (value) => {
25
+ var nibblesToBits = (value) => {
26
26
  return value << 2;
27
27
  };
28
- var isRoundOctet = (value) => {
28
+ var isRoundNibble = (value) => {
29
29
  return value >> 2 << 2 === value;
30
30
  };
31
31
  var isHex = (value, bitLength) => {
32
32
  if (typeof value !== "string")
33
33
  return false;
34
- if (bitLength !== void 0 && value.length !== bitsToOctets(bitLength))
34
+ if (bitLength !== void 0 && value.length !== bitsToNibbles(bitLength))
35
35
  return false;
36
36
  return hexRegex.test(value);
37
37
  };
38
+ var toHex = (buffer) => {
39
+ return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
40
+ };
38
41
  function asHex(value, assertOrBitLength, assertOnly) {
39
42
  const bitLength = typeof assertOrBitLength === "number" ? assertOrBitLength : void 0;
40
43
  const assert = typeof assertOrBitLength !== "number" ? assertOrBitLength : assertOnly;
@@ -64,7 +67,7 @@ function asHex(value, assertOrBitLength, assertOnly) {
64
67
  return assertError(value, assert, `Unsupported type [${typeof value}]`);
65
68
  }
66
69
  if (stringValue && bitLength) {
67
- stringValue = stringValue.padStart(bitsToOctets(bitLength), "0");
70
+ stringValue = stringValue.padStart(bitsToNibbles(bitLength), "0");
68
71
  }
69
72
  return isHex(stringValue, bitLength) ? stringValue.toLowerCase() : assertError(value, assert, "Unable to convert to Hash");
70
73
  }
@@ -77,7 +80,7 @@ var isAddress = (value, bitLength = 160) => {
77
80
  if (!addressRegex.test(value))
78
81
  return false;
79
82
  const valueHex = value.substring(2);
80
- if (bitLength !== void 0 && valueHex.length !== bitsToOctets(bitLength))
83
+ if (bitLength !== void 0 && valueHex.length !== bitsToNibbles(bitLength))
81
84
  return false;
82
85
  return isHex(valueHex, bitLength);
83
86
  };
@@ -99,7 +102,7 @@ var isHash = (value, bitLength = 256) => {
99
102
  const hex = asHex(value, bitLength);
100
103
  if (!hex)
101
104
  return false;
102
- if (!isHashBitLength(octetsToBits(hex.length)))
105
+ if (!isHashBitLength(nibblesToBits(hex.length)))
103
106
  return false;
104
107
  return true;
105
108
  };
@@ -115,7 +118,7 @@ export {
115
118
  asAddress,
116
119
  asHash,
117
120
  asHex,
118
- bitsToOctets,
121
+ bitsToNibbles,
119
122
  hexFrom,
120
123
  hexRegex,
121
124
  hexRegexWithPrefix,
@@ -123,7 +126,8 @@ export {
123
126
  isHash,
124
127
  isHashBitLength,
125
128
  isHex,
126
- isRoundOctet,
127
- octetsToBits
129
+ isRoundNibble,
130
+ nibblesToBits,
131
+ toHex
128
132
  };
129
133
  //# sourceMappingURL=index.js.map
@@ -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 { 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 } else {\n return assertError(value, assert, 'Unsupported object')\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,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AS,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":[]}
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 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\n//are the number of bit a round nibble (factor of 4)?\nexport const isRoundNibble = (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 !== bitsToNibbles(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n\nexport const toHex = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\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 } else {\n return assertError(value, assert, 'Unsupported object')\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(bitsToNibbles(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, bitsToNibbles, 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 !== 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, 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, nibblesToBits } 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(nibblesToBits(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,gBAAgB,CAAC,UAA0B;AACtD,QAAM,UAAU,SAAS;AACzB,MAAI,UAAU,WAAW;AAAG,UAAM,MAAM,qCAAqC;AAC7E,SAAO;A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O,sBAAsB,WAAW,oBAAoB;AAE3E,QAAM,SAAS,MAAM,OAAO,WAAW,MAAM;AAC7C,SAAO,OAAO,QAAQ,SAAS,IAAI,SAAS,YAAY,OAAO,QAAQ,+BAA+B;AACxG;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAS,GAAG,EAAuB,MAAM,OAAO,CAAA;AAEvD,MAAM,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAA;AAC1E,eAAO,MAAM,aAAa,EAAE,aAAa,EAA8C,CAAA;AAEvF,eAAO,MAAM,eAAe,UAAW,OAAO,2BAE7C,CAAA;AAED,MAAM,MAAM,IAAI,GAAG,GAAG,CAAA;AACtB,eAAO,MAAM,MAAM,UAAW,OAAO,cAAa,aAAa,oBAS9D,CAAA;AAED,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,CAAA;AACxD,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA;AAClE,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,SAAS,CAAA;AACnF,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,SAAS,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA"}
1
+ {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAS,GAAG,EAAwB,MAAM,OAAO,CAAA;AAExD,MA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,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAS,GAAG,EAAuB,MAAM,OAAO,CAAA;AAEvD,MAAM,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAA;AAC1E,eAAO,MAAM,aAAa,EAAE,aAAa,EAA8C,CAAA;AAEvF,eAAO,MAAM,eAAe,UAAW,OAAO,2BAE7C,CAAA;AAED,MAAM,MAAM,IAAI,GAAG,GAAG,CAAA;AACtB,eAAO,MAAM,MAAM,UAAW,OAAO,cAAa,aAAa,oBAS9D,CAAA;AAED,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,CAAA;AACxD,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA;AAClE,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,SAAS,CAAA;AACnF,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,SAAS,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA"}
1
+ {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAS,GAAG,EAAwB,MAAM,OAAO,CAAA;AAExD,MAAM,MAAM,aAAa,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAA;AAC1E,eAAO,MAAM,aAAa,EAAE,aAAa,EAA8C,CAAA;AAEvF,eAAO,MAAM,eAAe,UAAW,OAAO,2BAE7C,CAAA;AAED,MAAM,MAAM,IAAI,GAAG,GAAG,CAAA;AACtB,eAAO,MAAM,MAAM,UAAW,OAAO,cAAa,aAAa,oBAS9D,CAAA;AAED,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,CAAA;AACxD,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA;AAClE,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,SAAS,CAAA;AACnF,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,GAAG,SAAS,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAA"}
@@ -3,10 +3,11 @@ export declare const hexRegex: RegExp;
3
3
  export declare const hexRegexWithPrefix: RegExp;
4
4
  export type Hex = string;
5
5
  export declare const hexFrom: (buffer: ArrayBuffer) => string;
6
- export declare const bitsToOctets: (value: number) => number;
7
- export declare const octetsToBits: (value: number) => number;
8
- export declare const isRoundOctet: (value: number) => boolean;
6
+ export declare const bitsToNibbles: (value: number) => number;
7
+ export declare const nibblesToBits: (value: number) => number;
8
+ export declare const isRoundNibble: (value: number) => boolean;
9
9
  export declare const isHex: (value: unknown, bitLength?: number) => value is string;
10
+ export declare const toHex: (buffer: ArrayBuffer) => string;
10
11
  export declare function asHex(value: unknown): Hex | undefined;
11
12
  export declare function asHex(value: unknown, bitLength?: number): Hex | undefined;
12
13
  export declare function asHex(value: unknown, assert: AssertConfig): Hex;
@@ -1 +1 @@
1
- {"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;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CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,eAAO,MAAM,KAAK,WAAY,WAAW,WAExC,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"}
@@ -3,10 +3,11 @@ export declare const hexRegex: RegExp;
3
3
  export declare const hexRegexWithPrefix: RegExp;
4
4
  export type Hex = string;
5
5
  export declare const hexFrom: (buffer: ArrayBuffer) => string;
6
- export declare const bitsToOctets: (value: number) => number;
7
- export declare const octetsToBits: (value: number) => number;
8
- export declare const isRoundOctet: (value: number) => boolean;
6
+ export declare const bitsToNibbles: (value: number) => number;
7
+ export declare const nibblesToBits: (value: number) => number;
8
+ export declare const isRoundNibble: (value: number) => boolean;
9
9
  export declare const isHex: (value: unknown, bitLength?: number) => value is string;
10
+ export declare const toHex: (buffer: ArrayBuffer) => string;
10
11
  export declare function asHex(value: unknown): Hex | undefined;
11
12
  export declare function asHex(value: unknown, bitLength?: number): Hex | undefined;
12
13
  export declare function asHex(value: unknown, assert: AssertConfig): Hex;
@@ -1 +1 @@
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,M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,WAAY,WAAW,WAExC,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"}
@@ -3,10 +3,11 @@ export declare const hexRegex: RegExp;
3
3
  export declare const hexRegexWithPrefix: RegExp;
4
4
  export type Hex = string;
5
5
  export declare const hexFrom: (buffer: ArrayBuffer) => string;
6
- export declare const bitsToOctets: (value: number) => number;
7
- export declare const octetsToBits: (value: number) => number;
8
- export declare const isRoundOctet: (value: number) => boolean;
6
+ export declare const bitsToNibbles: (value: number) => number;
7
+ export declare const nibblesToBits: (value: number) => number;
8
+ export declare const isRoundNibble: (value: number) => boolean;
9
9
  export declare const isHex: (value: unknown, bitLength?: number) => value is string;
10
+ export declare const toHex: (buffer: ArrayBuffer) => string;
10
11
  export declare function asHex(value: unknown): Hex | undefined;
11
12
  export declare function asHex(value: unknown, bitLength?: number): Hex | undefined;
12
13
  export declare function asHex(value: unknown, assert: AssertConfig): Hex;
@@ -1 +1 @@
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;AAG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AAS,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"}
@@ -25,7 +25,7 @@ __export(src_exports, {
25
25
  asAddress: () => asAddress,
26
26
  asHash: () => asHash,
27
27
  asHex: () => asHex,
28
- bitsToOctets: () => bitsToOctets,
28
+ bitsToNibbles: () => bitsToNibbles,
29
29
  hexFrom: () => hexFrom,
30
30
  hexRegex: () => hexRegex,
31
31
  hexRegexWithPrefix: () => hexRegexWithPrefix,
@@ -33,8 +33,9 @@ __export(src_exports, {
33
33
  isHash: () => isHash,
34
34
  isHashBitLength: () => isHashBitLength,
35
35
  isHex: () => isHex,
36
- isRoundOctet: () => isRoundOctet,
37
- octetsToBits: () => octetsToBits
36
+ isRoundNibble: () => isRoundNibble,
37
+ nibblesToBits: () => nibblesToBits,
38
+ toHex: () => toHex
38
39
  });
39
40
  module.exports = __toCommonJS(src_exports);
40
41
 
@@ -56,25 +57,28 @@ var hexRegexWithPrefix = /0x[0-9a-f]+$/i;
56
57
  var hexFrom = (buffer) => {
57
58
  return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
58
59
  };
59
- var bitsToOctets = (value) => {
60
- const octets = value >> 2;
61
- if (value !== octets << 2)
62
- throw Error("Bits for octets must multiple of 8");
63
- return octets;
60
+ var bitsToNibbles = (value) => {
61
+ const nibbles = value >> 2;
62
+ if (value !== nibbles << 2)
63
+ throw Error("Bits for nibbles must multiple of 4");
64
+ return nibbles;
64
65
  };
65
- var octetsToBits = (value) => {
66
+ var nibblesToBits = (value) => {
66
67
  return value << 2;
67
68
  };
68
- var isRoundOctet = (value) => {
69
+ var isRoundNibble = (value) => {
69
70
  return value >> 2 << 2 === value;
70
71
  };
71
72
  var isHex = (value, bitLength) => {
72
73
  if (typeof value !== "string")
73
74
  return false;
74
- if (bitLength !== void 0 && value.length !== bitsToOctets(bitLength))
75
+ if (bitLength !== void 0 && value.length !== bitsToNibbles(bitLength))
75
76
  return false;
76
77
  return hexRegex.test(value);
77
78
  };
79
+ var toHex = (buffer) => {
80
+ return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
81
+ };
78
82
  function asHex(value, assertOrBitLength, assertOnly) {
79
83
  const bitLength = typeof assertOrBitLength === "number" ? assertOrBitLength : void 0;
80
84
  const assert = typeof assertOrBitLength !== "number" ? assertOrBitLength : assertOnly;
@@ -104,7 +108,7 @@ function asHex(value, assertOrBitLength, assertOnly) {
104
108
  return assertError(value, assert, `Unsupported type [${typeof value}]`);
105
109
  }
106
110
  if (stringValue && bitLength) {
107
- stringValue = stringValue.padStart(bitsToOctets(bitLength), "0");
111
+ stringValue = stringValue.padStart(bitsToNibbles(bitLength), "0");
108
112
  }
109
113
  return isHex(stringValue, bitLength) ? stringValue.toLowerCase() : assertError(value, assert, "Unable to convert to Hash");
110
114
  }
@@ -117,7 +121,7 @@ var isAddress = (value, bitLength = 160) => {
117
121
  if (!addressRegex.test(value))
118
122
  return false;
119
123
  const valueHex = value.substring(2);
120
- if (bitLength !== void 0 && valueHex.length !== bitsToOctets(bitLength))
124
+ if (bitLength !== void 0 && valueHex.length !== bitsToNibbles(bitLength))
121
125
  return false;
122
126
  return isHex(valueHex, bitLength);
123
127
  };
@@ -139,7 +143,7 @@ var isHash = (value, bitLength = 256) => {
139
143
  const hex = asHex(value, bitLength);
140
144
  if (!hex)
141
145
  return false;
142
- if (!isHashBitLength(octetsToBits(hex.length)))
146
+ if (!isHashBitLength(nibblesToBits(hex.length)))
143
147
  return false;
144
148
  return true;
145
149
  };
@@ -156,7 +160,7 @@ function asHash(value, assertOrBitLength, assertOnly) {
156
160
  asAddress,
157
161
  asHash,
158
162
  asHex,
159
- bitsToOctets,
163
+ bitsToNibbles,
160
164
  hexFrom,
161
165
  hexRegex,
162
166
  hexRegexWithPrefix,
@@ -164,7 +168,8 @@ function asHash(value, assertOrBitLength, assertOnly) {
164
168
  isHash,
165
169
  isHashBitLength,
166
170
  isHex,
167
- isRoundOctet,
168
- octetsToBits
171
+ isRoundNibble,
172
+ nibblesToBits,
173
+ toHex
169
174
  });
170
175
  //# sourceMappingURL=index.cjs.map
@@ -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 { 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 } else {\n return assertError(value, assert, 'Unsupported object')\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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,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":[]}
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 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\n//are the number of bit a round nibble (factor of 4)?\nexport const isRoundNibble = (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 !== bitsToNibbles(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n\nexport const toHex = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\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 } else {\n return assertError(value, assert, 'Unsupported object')\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(bitsToNibbles(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, bitsToNibbles, 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 !== 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, 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, nibblesToBits } 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(nibblesToBits(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;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,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,WAAO;AAExD,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":[]}
@@ -16,25 +16,28 @@ var hexRegexWithPrefix = /0x[0-9a-f]+$/i;
16
16
  var hexFrom = (buffer) => {
17
17
  return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
18
18
  };
19
- var bitsToOctets = (value) => {
20
- const octets = value >> 2;
21
- if (value !== octets << 2)
22
- throw Error("Bits for octets must multiple of 8");
23
- return octets;
19
+ var bitsToNibbles = (value) => {
20
+ const nibbles = value >> 2;
21
+ if (value !== nibbles << 2)
22
+ throw Error("Bits for nibbles must multiple of 4");
23
+ return nibbles;
24
24
  };
25
- var octetsToBits = (value) => {
25
+ var nibblesToBits = (value) => {
26
26
  return value << 2;
27
27
  };
28
- var isRoundOctet = (value) => {
28
+ var isRoundNibble = (value) => {
29
29
  return value >> 2 << 2 === value;
30
30
  };
31
31
  var isHex = (value, bitLength) => {
32
32
  if (typeof value !== "string")
33
33
  return false;
34
- if (bitLength !== void 0 && value.length !== bitsToOctets(bitLength))
34
+ if (bitLength !== void 0 && value.length !== bitsToNibbles(bitLength))
35
35
  return false;
36
36
  return hexRegex.test(value);
37
37
  };
38
+ var toHex = (buffer) => {
39
+ return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join("");
40
+ };
38
41
  function asHex(value, assertOrBitLength, assertOnly) {
39
42
  const bitLength = typeof assertOrBitLength === "number" ? assertOrBitLength : void 0;
40
43
  const assert = typeof assertOrBitLength !== "number" ? assertOrBitLength : assertOnly;
@@ -64,7 +67,7 @@ function asHex(value, assertOrBitLength, assertOnly) {
64
67
  return assertError(value, assert, `Unsupported type [${typeof value}]`);
65
68
  }
66
69
  if (stringValue && bitLength) {
67
- stringValue = stringValue.padStart(bitsToOctets(bitLength), "0");
70
+ stringValue = stringValue.padStart(bitsToNibbles(bitLength), "0");
68
71
  }
69
72
  return isHex(stringValue, bitLength) ? stringValue.toLowerCase() : assertError(value, assert, "Unable to convert to Hash");
70
73
  }
@@ -77,7 +80,7 @@ var isAddress = (value, bitLength = 160) => {
77
80
  if (!addressRegex.test(value))
78
81
  return false;
79
82
  const valueHex = value.substring(2);
80
- if (bitLength !== void 0 && valueHex.length !== bitsToOctets(bitLength))
83
+ if (bitLength !== void 0 && valueHex.length !== bitsToNibbles(bitLength))
81
84
  return false;
82
85
  return isHex(valueHex, bitLength);
83
86
  };
@@ -99,7 +102,7 @@ var isHash = (value, bitLength = 256) => {
99
102
  const hex = asHex(value, bitLength);
100
103
  if (!hex)
101
104
  return false;
102
- if (!isHashBitLength(octetsToBits(hex.length)))
105
+ if (!isHashBitLength(nibblesToBits(hex.length)))
103
106
  return false;
104
107
  return true;
105
108
  };
@@ -115,7 +118,7 @@ export {
115
118
  asAddress,
116
119
  asHash,
117
120
  asHex,
118
- bitsToOctets,
121
+ bitsToNibbles,
119
122
  hexFrom,
120
123
  hexRegex,
121
124
  hexRegexWithPrefix,
@@ -123,7 +126,8 @@ export {
123
126
  isHash,
124
127
  isHashBitLength,
125
128
  isHex,
126
- isRoundOctet,
127
- octetsToBits
129
+ isRoundNibble,
130
+ nibblesToBits,
131
+ toHex
128
132
  };
129
133
  //# sourceMappingURL=index.js.map
@@ -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 { 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 } else {\n return assertError(value, assert, 'Unsupported object')\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,UA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B;AACxG;","names":[]}
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 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\n//are the number of bit a round nibble (factor of 4)?\nexport const isRoundNibble = (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 !== bitsToNibbles(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n\nexport const toHex = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\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 } else {\n return assertError(value, assert, 'Unsupported object')\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(bitsToNibbles(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, bitsToNibbles, 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 !== 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, 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, nibblesToBits } 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(nibblesToBits(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,YA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zE,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,cAAc,IAAI,MAAM,CAAC;AAAG,WAAO;AAExD,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/package.json CHANGED
@@ -38,7 +38,7 @@
38
38
  "esm"
39
39
  ],
40
40
  "dependencies": {
41
- "@xylabs/arraybuffer": "~2.13.13"
41
+ "@xylabs/arraybuffer": "~2.13.15"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@xylabs/ts-scripts-yarn3": "^3.2.10",
@@ -53,7 +53,7 @@
53
53
  "url": "https://github.com/xylabs/sdk-js.git"
54
54
  },
55
55
  "sideEffects": false,
56
- "version": "2.13.13",
56
+ "version": "2.13.15",
57
57
  "packageManager": "yarn@3.3.1",
58
58
  "type": "module"
59
59
  }
package/src/address.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AssertConfig, assertError } from './assert'
2
- import { asHex, bitsToOctets, isHex } from './hex'
2
+ import { asHex, bitsToNibbles, isHex } from './hex'
3
3
 
4
4
  export const addressRegex = /0x[0-9a-f]+/i
5
5
 
@@ -15,7 +15,7 @@ export const isAddress = (value: unknown, bitLength = 160): value is Address =>
15
15
  const valueHex = value.substring(2)
16
16
 
17
17
  //If a bitLength specified, does it conform?
18
- if (bitLength !== undefined && valueHex.length !== bitsToOctets(bitLength)) return false
18
+ if (bitLength !== undefined && valueHex.length !== bitsToNibbles(bitLength)) return false
19
19
 
20
20
  return isHex(valueHex, bitLength)
21
21
  }
package/src/hash.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AssertConfig, assertError } from './assert'
2
- import { asHex, Hex, isHex, octetsToBits } from './hex'
2
+ import { asHex, Hex, isHex, nibblesToBits } from './hex'
3
3
 
4
4
  export type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096
5
5
  export const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]
@@ -15,7 +15,7 @@ export const isHash = (value: unknown, bitLength: HashBitLength = 256): value is
15
15
  const hex = asHex(value, bitLength)
16
16
  if (!hex) return false
17
17
 
18
- if (!isHashBitLength(octetsToBits(hex.length))) return false
18
+ if (!isHashBitLength(nibblesToBits(hex.length))) return false
19
19
 
20
20
  return true
21
21
  }
package/src/hex.ts CHANGED
@@ -11,20 +11,20 @@ export const hexFrom = (buffer: ArrayBuffer) => {
11
11
  return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')
12
12
  }
13
13
 
14
- //determine the number of octets for a given number of bits
15
- export const bitsToOctets = (value: number): number => {
16
- const octets = value >> 2
17
- if (value !== octets << 2) throw Error('Bits for octets must multiple of 8')
18
- return octets
14
+ //determine the number of nibbles for a given number of bits
15
+ export const bitsToNibbles = (value: number): number => {
16
+ const nibbles = value >> 2
17
+ if (value !== nibbles << 2) throw Error('Bits for nibbles must multiple of 4')
18
+ return nibbles
19
19
  }
20
20
 
21
- //determine the number of octets for a given number of bits
22
- export const octetsToBits = (value: number): number => {
21
+ //determine the number of nibbles for a given number of bits
22
+ export const nibblesToBits = (value: number): number => {
23
23
  return value << 2
24
24
  }
25
25
 
26
- //are the number of bit a round octet (factor of 8)?
27
- export const isRoundOctet = (value: number) => {
26
+ //are the number of bit a round nibble (factor of 4)?
27
+ export const isRoundNibble = (value: number) => {
28
28
  return (value >> 2) << 2 === value
29
29
  }
30
30
 
@@ -33,12 +33,16 @@ export const isHex = (value: unknown, bitLength?: number): value is Hex => {
33
33
  if (typeof value !== 'string') return false
34
34
 
35
35
  //If a bitLength specified, does it conform?
36
- if (bitLength !== undefined && value.length !== bitsToOctets(bitLength)) return false
36
+ if (bitLength !== undefined && value.length !== bitsToNibbles(bitLength)) return false
37
37
 
38
38
  //Does it only has hex values?
39
39
  return hexRegex.test(value)
40
40
  }
41
41
 
42
+ export const toHex = (buffer: ArrayBuffer) => {
43
+ return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')
44
+ }
45
+
42
46
  export function asHex(value: unknown): Hex | undefined
43
47
  export function asHex(value: unknown, bitLength?: number): Hex | undefined
44
48
  export function asHex(value: unknown, assert: AssertConfig): Hex
@@ -77,7 +81,7 @@ export function asHex(value: unknown, assertOrBitLength?: AssertConfig | number,
77
81
 
78
82
  //make it conform to the bit length if shorter
79
83
  if (stringValue && bitLength) {
80
- stringValue = stringValue.padStart(bitsToOctets(bitLength), '0')
84
+ stringValue = stringValue.padStart(bitsToNibbles(bitLength), '0')
81
85
  }
82
86
 
83
87
  return isHex(stringValue, bitLength) ? stringValue.toLowerCase() : assertError(value, assert, 'Unable to convert to Hash')