@xylabs/hex 2.13.14 → 2.13.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/dist/browser/address.d.cts +2 -2
  2. package/dist/browser/address.d.cts.map +1 -1
  3. package/dist/browser/address.d.mts +2 -2
  4. package/dist/browser/address.d.mts.map +1 -1
  5. package/dist/browser/address.d.ts +2 -2
  6. package/dist/browser/address.d.ts.map +1 -1
  7. package/dist/browser/hash.d.cts +0 -2
  8. package/dist/browser/hash.d.cts.map +1 -1
  9. package/dist/browser/hash.d.mts +0 -2
  10. package/dist/browser/hash.d.mts.map +1 -1
  11. package/dist/browser/hash.d.ts +0 -2
  12. package/dist/browser/hash.d.ts.map +1 -1
  13. package/dist/browser/hex/as.d.cts +5 -0
  14. package/dist/browser/hex/as.d.cts.map +1 -0
  15. package/dist/browser/hex/as.d.mts +5 -0
  16. package/dist/browser/hex/as.d.mts.map +1 -0
  17. package/dist/browser/hex/as.d.ts +5 -0
  18. package/dist/browser/hex/as.d.ts.map +1 -0
  19. package/dist/browser/hex/from.d.cts +7 -0
  20. package/dist/browser/hex/from.d.cts.map +1 -0
  21. package/dist/browser/hex/from.d.mts +7 -0
  22. package/dist/browser/hex/from.d.mts.map +1 -0
  23. package/dist/browser/hex/from.d.ts +7 -0
  24. package/dist/browser/hex/from.d.ts.map +1 -0
  25. package/dist/browser/hex/index.d.cts +8 -0
  26. package/dist/browser/hex/index.d.cts.map +1 -0
  27. package/dist/browser/hex/index.d.mts +8 -0
  28. package/dist/browser/hex/index.d.mts.map +1 -0
  29. package/dist/browser/hex/index.d.ts +8 -0
  30. package/dist/browser/hex/index.d.ts.map +1 -0
  31. package/dist/browser/hex/is.d.cts +3 -0
  32. package/dist/browser/hex/is.d.cts.map +1 -0
  33. package/dist/browser/hex/is.d.mts +3 -0
  34. package/dist/browser/hex/is.d.mts.map +1 -0
  35. package/dist/browser/hex/is.d.ts +3 -0
  36. package/dist/browser/hex/is.d.ts.map +1 -0
  37. package/dist/browser/hex/legacy.d.cts +4 -0
  38. package/dist/browser/hex/legacy.d.cts.map +1 -0
  39. package/dist/browser/hex/legacy.d.mts +4 -0
  40. package/dist/browser/hex/legacy.d.mts.map +1 -0
  41. package/dist/browser/hex/legacy.d.ts +4 -0
  42. package/dist/browser/hex/legacy.d.ts.map +1 -0
  43. package/dist/browser/hex/model.d.cts +7 -0
  44. package/dist/browser/hex/model.d.cts.map +1 -0
  45. package/dist/browser/hex/model.d.mts +7 -0
  46. package/dist/browser/hex/model.d.mts.map +1 -0
  47. package/dist/browser/hex/model.d.ts +7 -0
  48. package/dist/browser/hex/model.d.ts.map +1 -0
  49. package/dist/browser/hex/nibble.d.cts +3 -0
  50. package/dist/browser/hex/nibble.d.cts.map +1 -0
  51. package/dist/browser/hex/nibble.d.mts +3 -0
  52. package/dist/browser/hex/nibble.d.mts.map +1 -0
  53. package/dist/browser/hex/nibble.d.ts +3 -0
  54. package/dist/browser/hex/nibble.d.ts.map +1 -0
  55. package/dist/browser/hex/regex.d.cts +3 -0
  56. package/dist/browser/hex/regex.d.cts.map +1 -0
  57. package/dist/browser/hex/regex.d.mts +3 -0
  58. package/dist/browser/hex/regex.d.mts.map +1 -0
  59. package/dist/browser/hex/regex.d.ts +3 -0
  60. package/dist/browser/hex/regex.d.ts.map +1 -0
  61. package/dist/browser/index.cjs +107 -61
  62. package/dist/browser/index.cjs.map +1 -1
  63. package/dist/browser/index.js +107 -61
  64. package/dist/browser/index.js.map +1 -1
  65. package/dist/node/address.d.cts +2 -2
  66. package/dist/node/address.d.cts.map +1 -1
  67. package/dist/node/address.d.mts +2 -2
  68. package/dist/node/address.d.mts.map +1 -1
  69. package/dist/node/address.d.ts +2 -2
  70. package/dist/node/address.d.ts.map +1 -1
  71. package/dist/node/hash.d.cts +0 -2
  72. package/dist/node/hash.d.cts.map +1 -1
  73. package/dist/node/hash.d.mts +0 -2
  74. package/dist/node/hash.d.mts.map +1 -1
  75. package/dist/node/hash.d.ts +0 -2
  76. package/dist/node/hash.d.ts.map +1 -1
  77. package/dist/node/hex/as.d.cts +5 -0
  78. package/dist/node/hex/as.d.cts.map +1 -0
  79. package/dist/node/hex/as.d.mts +5 -0
  80. package/dist/node/hex/as.d.mts.map +1 -0
  81. package/dist/node/hex/as.d.ts +5 -0
  82. package/dist/node/hex/as.d.ts.map +1 -0
  83. package/dist/node/hex/from.d.cts +7 -0
  84. package/dist/node/hex/from.d.cts.map +1 -0
  85. package/dist/node/hex/from.d.mts +7 -0
  86. package/dist/node/hex/from.d.mts.map +1 -0
  87. package/dist/node/hex/from.d.ts +7 -0
  88. package/dist/node/hex/from.d.ts.map +1 -0
  89. package/dist/node/hex/index.d.cts +8 -0
  90. package/dist/node/hex/index.d.cts.map +1 -0
  91. package/dist/node/hex/index.d.mts +8 -0
  92. package/dist/node/hex/index.d.mts.map +1 -0
  93. package/dist/node/hex/index.d.ts +8 -0
  94. package/dist/node/hex/index.d.ts.map +1 -0
  95. package/dist/node/hex/is.d.cts +3 -0
  96. package/dist/node/hex/is.d.cts.map +1 -0
  97. package/dist/node/hex/is.d.mts +3 -0
  98. package/dist/node/hex/is.d.mts.map +1 -0
  99. package/dist/node/hex/is.d.ts +3 -0
  100. package/dist/node/hex/is.d.ts.map +1 -0
  101. package/dist/node/hex/legacy.d.cts +4 -0
  102. package/dist/node/hex/legacy.d.cts.map +1 -0
  103. package/dist/node/hex/legacy.d.mts +4 -0
  104. package/dist/node/hex/legacy.d.mts.map +1 -0
  105. package/dist/node/hex/legacy.d.ts +4 -0
  106. package/dist/node/hex/legacy.d.ts.map +1 -0
  107. package/dist/node/hex/model.d.cts +7 -0
  108. package/dist/node/hex/model.d.cts.map +1 -0
  109. package/dist/node/hex/model.d.mts +7 -0
  110. package/dist/node/hex/model.d.mts.map +1 -0
  111. package/dist/node/hex/model.d.ts +7 -0
  112. package/dist/node/hex/model.d.ts.map +1 -0
  113. package/dist/node/hex/nibble.d.cts +3 -0
  114. package/dist/node/hex/nibble.d.cts.map +1 -0
  115. package/dist/node/hex/nibble.d.mts +3 -0
  116. package/dist/node/hex/nibble.d.mts.map +1 -0
  117. package/dist/node/hex/nibble.d.ts +3 -0
  118. package/dist/node/hex/nibble.d.ts.map +1 -0
  119. package/dist/node/hex/regex.d.cts +3 -0
  120. package/dist/node/hex/regex.d.cts.map +1 -0
  121. package/dist/node/hex/regex.d.mts +3 -0
  122. package/dist/node/hex/regex.d.mts.map +1 -0
  123. package/dist/node/hex/regex.d.ts +3 -0
  124. package/dist/node/hex/regex.d.ts.map +1 -0
  125. package/dist/node/index.cjs +117 -65
  126. package/dist/node/index.cjs.map +1 -1
  127. package/dist/node/index.js +107 -61
  128. package/dist/node/index.js.map +1 -1
  129. package/package.json +2 -2
  130. package/src/address.ts +18 -10
  131. package/src/hash.ts +13 -17
  132. package/src/hex/as.ts +20 -0
  133. package/src/hex/from.ts +52 -0
  134. package/src/hex/index.ts +7 -0
  135. package/src/hex/is.ts +21 -0
  136. package/src/hex/legacy.ts +8 -0
  137. package/src/hex/model.ts +7 -0
  138. package/src/hex/nibble.ts +11 -0
  139. package/src/hex/regex.ts +2 -0
  140. package/dist/browser/hex.d.cts +0 -15
  141. package/dist/browser/hex.d.cts.map +0 -1
  142. package/dist/browser/hex.d.mts +0 -15
  143. package/dist/browser/hex.d.mts.map +0 -1
  144. package/dist/browser/hex.d.ts +0 -15
  145. package/dist/browser/hex.d.ts.map +0 -1
  146. package/dist/node/hex.d.cts +0 -15
  147. package/dist/node/hex.d.cts.map +0 -1
  148. package/dist/node/hex.d.mts +0 -15
  149. package/dist/node/hex.d.mts.map +0 -1
  150. package/dist/node/hex.d.ts +0 -15
  151. package/dist/node/hex.d.ts.map +0 -1
  152. package/src/hex.ts +0 -88
@@ -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 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(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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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/assert.ts","../../src/hex/from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/as.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString = typeof assert === 'string' ? assert : typeof assert === 'boolean' ? defaultMessage : assert(value, defaultMessage)\n if (assertString) {\n throw Error(assertString === true ? defaultMessage : assertString)\n }\n }\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { isHex } from './is'\nimport { HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): string => {\n const unPadded = [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n return hexFromHexString(unPadded, config)\n}\n\nexport const hexFromBigInt = (value: bigint, config: HexConfig = {}): string => {\n const { bitLength } = config\n const unPadded = value.toString(16)\n const padded = bitLength === undefined ? unPadded : unPadded.padStart(bitsToNibbles(bitLength), '0')\n return hexFromHexString(padded, config)\n}\n\nexport const hexFromNumber = (value: number, config?: HexConfig): string => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): string => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.substring(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return prefix ? `0x${padded}` : padded\n } else {\n throw Error('Received string is not a value hex')\n }\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): string => {\n switch (typeof value) {\n case 'string':\n return hexFromHexString(value, config)\n case 'bigint':\n return hexFromBigInt(value, config)\n case 'number':\n return hexFromNumber(value, config)\n case 'object':\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw Error('Invalid type: object !== ArrayBuffer')\n }\n default:\n throw Error(`Invalid type: ${typeof value}`)\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[0-9a-f]+$/i\nexport const hexRegexWithPrefix = /0x[0-9a-f]+$/i\n","import { hexFromHexString } from './from'\nimport { Hex } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex } from './regex'\n\nexport const isHexZero = (value: string) => {\n return BigInt(hexFromHexString(value, { prefix: true })) === 0n\n}\n\nexport const isHex = (value: unknown, bitLength?: number): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n console.log(`BitLength: ${value.length << 2}|${bitLength}`)\n\n //If a bitLength specified, does it conform?\n if (bitLength !== undefined && value.length !== bitsToNibbles(bitLength)) return false\n\n //Does it only has hex values?\n return hexRegex.test(value)\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n stringValue = hexFromHexString(value)\n break\n default:\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { bitsToNibbles, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport const addressRegex = /0x[0-9a-f]+/i\n\nexport type Address = string\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = true } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, bitLength = 160): value is Address => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n //Does it only has hex values and leading 0x?\n if (!addressRegex.test(value)) return false\n\n const valueHex = value.substring(2)\n\n //If a bitLength specified, does it conform?\n if (bitLength !== undefined && valueHex.length !== bitsToNibbles(bitLength)) return false\n\n return isHex(valueHex, bitLength)\n}\n\nexport function asAddress(value: unknown): Address | undefined\nexport function asAddress(value: unknown, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n stringValue = hexFromHexString(value, { prefix: true })\n break\n default:\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Hex\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, bitLength)\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string':\n stringValue = hexFromHexString(value)\n break\n default:\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n"],"mappings":";AAIO,IAAM,cAAc,CAAC,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;;;ACCvB,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;;;ACVO,IAAM,WAAW;AACjB,IAAM,qBAAqB;;;ACI3B,IAAM,YAAY,CAAC,UAAkB;AAC1C,SAAO,OAAO,iBAAiB,OAAO,EAAE,QAAQ,KAAK,CAAC,CAAC,MAAM;AAC/D;AAEO,IAAM,QAAQ,CAAC,OAAg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AAM,UAAU,CAAC;AAGlC,MAAI,cAAc,UAAa,SAAS,WAAW,cAAc,SAAS;AAAG,WAAO;AAEpF,SAAO,MAAM,UAAU,SAAS;AAClC;AAIO,SAAS,UAAU,OAAgB,QAA4C;AACpF,MAAI,cAAkC;AAEtC,UAAQ,OAAO,OAAO;AAAA,IACpB,KAAK;AACH,oBAAc,iBAAiB,OAAO,EAAE,QAAQ,KAAK,CAAC;AACtD;AAAA,IACF;AACE,aAAO,SAAS,YAAY,OAAO,QAAQ,qBAAqB,OAAO,KAAK,GAAG,IAAI;AAAA,EACvF;AACA,SAAO,UAAU,WAAW,IAAI,cAAc,YAAY,OAAO,QAAQ,4BAA4B,KAAK,GAAG;AAC/G;;;ACpCO,IAAM,gBAAiC,CAAC,IAAI,IAAI,KAAK,KAAK,KAAK,MAAM,MAAM,IAAI;AAE/E,IAAM,kBAAkB,CAAC,UAA2C;AACzE,SAAO,OAAO,UAAU,YAAY,cAAc,SAAS,KAAsB;AACnF;AAGO,IAAM,SAAS,CAAC,OAAgB,YAA2B,QAAuB;AACvF,SAAO,MAAM,OAAO,SAAS;AAC/B;AAIO,SAAS,OAAO,OAAgB,QAAyC;AAC9E,MAAI,cAAkC;AAEtC,UAAQ,OAAO,OAAO;AAAA,IACpB,KAAK;AACH,oBAAc,iBAAiB,KAAK;AACpC;AAAA,IACF;AACE,aAAO,SAAS,YAAY,OAAO,QAAQ,qBAAqB,OAAO,KAAK,GAAG,IAAI;AAAA,EACvF;AACA,SAAO,OAAO,WAAW,IAAI,cAAc,YAAY,OAAO,QAAQ,wBAAwB,KAAK,GAAG;AACxG;","names":[]}
package/package.json CHANGED
@@ -38,7 +38,7 @@
38
38
  "esm"
39
39
  ],
40
40
  "dependencies": {
41
- "@xylabs/arraybuffer": "~2.13.14"
41
+ "@xylabs/arraybuffer": "~2.13.16"
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.14",
56
+ "version": "2.13.16",
57
57
  "packageManager": "yarn@3.3.1",
58
58
  "type": "module"
59
59
  }
package/src/address.ts CHANGED
@@ -1,10 +1,15 @@
1
1
  import { AssertConfig, assertError } from './assert'
2
- import { asHex, bitsToOctets, isHex } from './hex'
2
+ import { bitsToNibbles, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'
3
3
 
4
4
  export const addressRegex = /0x[0-9a-f]+/i
5
5
 
6
6
  export type Address = string
7
7
 
8
+ export const toAddress = (value: unknown, config: HexConfig = {}) => {
9
+ const { bitLength = 160, prefix = true } = config
10
+ return hexFrom(value, { bitLength, prefix, ...config })
11
+ }
12
+
8
13
  export const isAddress = (value: unknown, bitLength = 160): value is Address => {
9
14
  //Is it a string?
10
15
  if (typeof value !== 'string') return false
@@ -15,19 +20,22 @@ export const isAddress = (value: unknown, bitLength = 160): value is Address =>
15
20
  const valueHex = value.substring(2)
16
21
 
17
22
  //If a bitLength specified, does it conform?
18
- if (bitLength !== undefined && valueHex.length !== bitsToOctets(bitLength)) return false
23
+ if (bitLength !== undefined && valueHex.length !== bitsToNibbles(bitLength)) return false
19
24
 
20
25
  return isHex(valueHex, bitLength)
21
26
  }
22
27
 
23
28
  export function asAddress(value: unknown): Address | undefined
24
29
  export function asAddress(value: unknown, assert: AssertConfig): Address
25
- export function asAddress(value: unknown, bitLength: number): Address | undefined
26
- export function asAddress(value: unknown, bitLength: number, assert: AssertConfig): Address
27
- export function asAddress(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Address | undefined {
28
- const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : 160
29
- const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly
30
-
31
- const result = `0x${asHex(value, bitLength, assert)}`
32
- return isAddress(result, bitLength) ? result : assertError(value, assert, 'Resulting value is not an Address')
30
+ export function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {
31
+ let stringValue: string | undefined = undefined
32
+
33
+ switch (typeof value) {
34
+ case 'string':
35
+ stringValue = hexFromHexString(value, { prefix: true })
36
+ break
37
+ default:
38
+ return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined
39
+ }
40
+ return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)
33
41
  }
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 { Hex, hexFromHexString, isHex } 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]
@@ -10,24 +10,20 @@ export const isHashBitLength = (value: unknown): value is HashBitLength => {
10
10
 
11
11
  export type Hash = Hex
12
12
  export const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {
13
- if (!isHex(value, bitLength)) return false
14
-
15
- const hex = asHex(value, bitLength)
16
- if (!hex) return false
17
-
18
- if (!isHashBitLength(octetsToBits(hex.length))) return false
19
-
20
- return true
13
+ return isHex(value, bitLength)
21
14
  }
22
15
 
23
16
  export function asHash(value: unknown): Hash | undefined
24
17
  export function asHash(value: unknown, assert: AssertConfig): Hash
25
- export function asHash(value: unknown, bitLength?: HashBitLength): Hash | undefined
26
- export function asHash(value: unknown, bitLength: HashBitLength | undefined, assert: AssertConfig): Hash
27
- export function asHash(value: unknown, assertOrBitLength?: AssertConfig | HashBitLength, assertOnly?: AssertConfig): Hash | undefined {
28
- const bitLength: HashBitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : 256
29
- const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly
30
-
31
- const result = asHex(value, bitLength, assert)
32
- return isHash(result, bitLength) ? result : assertError(value, assert, 'Resulting value is not a Hash')
18
+ export function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {
19
+ let stringValue: string | undefined = undefined
20
+
21
+ switch (typeof value) {
22
+ case 'string':
23
+ stringValue = hexFromHexString(value)
24
+ break
25
+ default:
26
+ return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined
27
+ }
28
+ return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)
33
29
  }
package/src/hex/as.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { AssertConfig, assertError } from '../assert'
2
+ import { hexFromHexString } from './from'
3
+ import { isHex } from './is'
4
+ import { Hex } from './model'
5
+
6
+ export function asHex(value: unknown): Hex | undefined
7
+ export function asHex(value: unknown, assert: AssertConfig): Hex
8
+ export function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {
9
+ let stringValue: string | undefined = undefined
10
+
11
+ switch (typeof value) {
12
+ case 'string':
13
+ stringValue = hexFromHexString(value)
14
+ break
15
+ default:
16
+ return assertError(value, assert, `Unsupported type [${typeof value}]`)
17
+ }
18
+
19
+ return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)
20
+ }
@@ -0,0 +1,52 @@
1
+ import { isArrayBuffer } from '@xylabs/arraybuffer'
2
+
3
+ import { isHex } from './is'
4
+ import { HexConfig } from './model'
5
+ import { bitsToNibbles } from './nibble'
6
+
7
+ export const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): string => {
8
+ const unPadded = [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')
9
+ return hexFromHexString(unPadded, config)
10
+ }
11
+
12
+ export const hexFromBigInt = (value: bigint, config: HexConfig = {}): string => {
13
+ const { bitLength } = config
14
+ const unPadded = value.toString(16)
15
+ const padded = bitLength === undefined ? unPadded : unPadded.padStart(bitsToNibbles(bitLength), '0')
16
+ return hexFromHexString(padded, config)
17
+ }
18
+
19
+ export const hexFromNumber = (value: number, config?: HexConfig): string => {
20
+ return hexFromBigInt(BigInt(value), config)
21
+ }
22
+
23
+ export const hexFromHexString = (value: string, config: HexConfig = {}): string => {
24
+ const { prefix = false, byteSize = 8 } = config
25
+ const nibbleBoundary = bitsToNibbles(byteSize)
26
+ const unPadded = (value.startsWith('0x') ? value.substring(2) : value).toLowerCase()
27
+ if (isHex(unPadded)) {
28
+ const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')
29
+ return prefix ? `0x${padded}` : padded
30
+ } else {
31
+ throw Error('Received string is not a value hex')
32
+ }
33
+ }
34
+
35
+ export const hexFrom = (value: unknown, config?: HexConfig): string => {
36
+ switch (typeof value) {
37
+ case 'string':
38
+ return hexFromHexString(value, config)
39
+ case 'bigint':
40
+ return hexFromBigInt(value, config)
41
+ case 'number':
42
+ return hexFromNumber(value, config)
43
+ case 'object':
44
+ if (isArrayBuffer(value)) {
45
+ return hexFromArrayBuffer(value, config)
46
+ } else {
47
+ throw Error('Invalid type: object !== ArrayBuffer')
48
+ }
49
+ default:
50
+ throw Error(`Invalid type: ${typeof value}`)
51
+ }
52
+ }
@@ -0,0 +1,7 @@
1
+ export * from './as'
2
+ export * from './from'
3
+ export * from './is'
4
+ export * from './legacy'
5
+ export * from './model'
6
+ export * from './nibble'
7
+ export * from './regex'
package/src/hex/is.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { hexFromHexString } from './from'
2
+ import { Hex } from './model'
3
+ import { bitsToNibbles } from './nibble'
4
+ import { hexRegex } from './regex'
5
+
6
+ export const isHexZero = (value: string) => {
7
+ return BigInt(hexFromHexString(value, { prefix: true })) === 0n
8
+ }
9
+
10
+ export const isHex = (value: unknown, bitLength?: number): value is Hex => {
11
+ //Is it a string?
12
+ if (typeof value !== 'string') return false
13
+
14
+ console.log(`BitLength: ${value.length << 2}|${bitLength}`)
15
+
16
+ //If a bitLength specified, does it conform?
17
+ if (bitLength !== undefined && value.length !== bitsToNibbles(bitLength)) return false
18
+
19
+ //Does it only has hex values?
20
+ return hexRegex.test(value)
21
+ }
@@ -0,0 +1,8 @@
1
+ export const toHexLegacy = (buffer: ArrayBuffer) => {
2
+ return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')
3
+ }
4
+
5
+ /** @deprecated use toHexLegacy instead */
6
+ export const toHex = (buffer: ArrayBuffer) => {
7
+ return toHexLegacy(buffer)
8
+ }
@@ -0,0 +1,7 @@
1
+ export type Hex = string
2
+
3
+ export interface HexConfig {
4
+ bitLength?: number
5
+ byteSize?: number
6
+ prefix?: boolean
7
+ }
@@ -0,0 +1,11 @@
1
+ //determine the number of nibbles for a given number of bits
2
+ export const bitsToNibbles = (value: number): number => {
3
+ const nibbles = value >> 2
4
+ if (value !== nibbles << 2) throw Error('Bits for nibbles must multiple of 4')
5
+ return nibbles
6
+ }
7
+
8
+ //determine the number of nibbles for a given number of bits
9
+ export const nibblesToBits = (value: number): number => {
10
+ return value << 2
11
+ }
@@ -0,0 +1,2 @@
1
+ export const hexRegex = /^[0-9a-f]+$/i
2
+ export const hexRegexWithPrefix = /0x[0-9a-f]+$/i
@@ -1,15 +0,0 @@
1
- import { AssertConfig } from './assert';
2
- export declare const hexRegex: RegExp;
3
- export declare const hexRegexWithPrefix: RegExp;
4
- export type Hex = string;
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;
9
- export declare const isHex: (value: unknown, bitLength?: number) => value is string;
10
- export declare const toHex: (buffer: ArrayBuffer) => string;
11
- export declare function asHex(value: unknown): Hex | undefined;
12
- export declare function asHex(value: unknown, bitLength?: number): Hex | undefined;
13
- export declare function asHex(value: unknown, assert: AssertConfig): Hex;
14
- export declare function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex;
15
- //# sourceMappingURL=hex.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AAEpD,eAAO,MAAM,QAAQ,QAAiB,CAAA;AACtC,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AAExB,eAAO,MAAM,OAAO,WAAY,WAAW,WAE1C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAI5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,YAEzC,CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,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"}
@@ -1,15 +0,0 @@
1
- import { AssertConfig } from './assert';
2
- export declare const hexRegex: RegExp;
3
- export declare const hexRegexWithPrefix: RegExp;
4
- export type Hex = string;
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;
9
- export declare const isHex: (value: unknown, bitLength?: number) => value is string;
10
- export declare const toHex: (buffer: ArrayBuffer) => string;
11
- export declare function asHex(value: unknown): Hex | undefined;
12
- export declare function asHex(value: unknown, bitLength?: number): Hex | undefined;
13
- export declare function asHex(value: unknown, assert: AssertConfig): Hex;
14
- export declare function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex;
15
- //# sourceMappingURL=hex.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AAEpD,eAAO,MAAM,QAAQ,QAAiB,CAAA;AACtC,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AAExB,eAAO,MAAM,OAAO,WAAY,WAAW,WAE1C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAI5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,YAEzC,CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,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"}
@@ -1,15 +0,0 @@
1
- import { AssertConfig } from './assert';
2
- export declare const hexRegex: RegExp;
3
- export declare const hexRegexWithPrefix: RegExp;
4
- export type Hex = string;
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;
9
- export declare const isHex: (value: unknown, bitLength?: number) => value is string;
10
- export declare const toHex: (buffer: ArrayBuffer) => string;
11
- export declare function asHex(value: unknown): Hex | undefined;
12
- export declare function asHex(value: unknown, bitLength?: number): Hex | undefined;
13
- export declare function asHex(value: unknown, assert: AssertConfig): Hex;
14
- export declare function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex;
15
- //# sourceMappingURL=hex.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AAEpD,eAAO,MAAM,QAAQ,QAAiB,CAAA;AACtC,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AAExB,eAAO,MAAM,OAAO,WAAY,WAAW,WAE1C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAI5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,YAEzC,CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,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"}
@@ -1,15 +0,0 @@
1
- import { AssertConfig } from './assert';
2
- export declare const hexRegex: RegExp;
3
- export declare const hexRegexWithPrefix: RegExp;
4
- export type Hex = string;
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;
9
- export declare const isHex: (value: unknown, bitLength?: number) => value is string;
10
- export declare const toHex: (buffer: ArrayBuffer) => string;
11
- export declare function asHex(value: unknown): Hex | undefined;
12
- export declare function asHex(value: unknown, bitLength?: number): Hex | undefined;
13
- export declare function asHex(value: unknown, assert: AssertConfig): Hex;
14
- export declare function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex;
15
- //# sourceMappingURL=hex.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AAEpD,eAAO,MAAM,QAAQ,QAAiB,CAAA;AACtC,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AAExB,eAAO,MAAM,OAAO,WAAY,WAAW,WAE1C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAI5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,YAEzC,CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,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"}
@@ -1,15 +0,0 @@
1
- import { AssertConfig } from './assert';
2
- export declare const hexRegex: RegExp;
3
- export declare const hexRegexWithPrefix: RegExp;
4
- export type Hex = string;
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;
9
- export declare const isHex: (value: unknown, bitLength?: number) => value is string;
10
- export declare const toHex: (buffer: ArrayBuffer) => string;
11
- export declare function asHex(value: unknown): Hex | undefined;
12
- export declare function asHex(value: unknown, bitLength?: number): Hex | undefined;
13
- export declare function asHex(value: unknown, assert: AssertConfig): Hex;
14
- export declare function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex;
15
- //# sourceMappingURL=hex.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AAEpD,eAAO,MAAM,QAAQ,QAAiB,CAAA;AACtC,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AAExB,eAAO,MAAM,OAAO,WAAY,WAAW,WAE1C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAI5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,YAEzC,CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,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"}
@@ -1,15 +0,0 @@
1
- import { AssertConfig } from './assert';
2
- export declare const hexRegex: RegExp;
3
- export declare const hexRegexWithPrefix: RegExp;
4
- export type Hex = string;
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;
9
- export declare const isHex: (value: unknown, bitLength?: number) => value is string;
10
- export declare const toHex: (buffer: ArrayBuffer) => string;
11
- export declare function asHex(value: unknown): Hex | undefined;
12
- export declare function asHex(value: unknown, bitLength?: number): Hex | undefined;
13
- export declare function asHex(value: unknown, assert: AssertConfig): Hex;
14
- export declare function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex;
15
- //# sourceMappingURL=hex.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/hex.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AAEpD,eAAO,MAAM,QAAQ,QAAiB,CAAA;AACtC,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,MAAM,MAAM,GAAG,GAAG,MAAM,CAAA;AAExB,eAAO,MAAM,OAAO,WAAY,WAAW,WAE1C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAI5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAA;AAGD,eAAO,MAAM,YAAY,UAAW,MAAM,YAEzC,CAAA;AAED,eAAO,MAAM,KAAK,UAAW,OAAO,cAAc,MAAM,oBASvD,CAAA;AAED,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"}
package/src/hex.ts DELETED
@@ -1,88 +0,0 @@
1
- import { isArrayBuffer } from '@xylabs/arraybuffer'
2
-
3
- import { AssertConfig, assertError } from './assert'
4
-
5
- export const hexRegex = /^[0-9a-f]+$/i
6
- export const hexRegexWithPrefix = /0x[0-9a-f]+$/i
7
-
8
- export type Hex = string
9
-
10
- export const hexFrom = (buffer: ArrayBuffer) => {
11
- return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')
12
- }
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
19
- }
20
-
21
- //determine the number of octets for a given number of bits
22
- export const octetsToBits = (value: number): number => {
23
- return value << 2
24
- }
25
-
26
- //are the number of bit a round octet (factor of 8)?
27
- export const isRoundOctet = (value: number) => {
28
- return (value >> 2) << 2 === value
29
- }
30
-
31
- export const isHex = (value: unknown, bitLength?: number): value is Hex => {
32
- //Is it a string?
33
- if (typeof value !== 'string') return false
34
-
35
- //If a bitLength specified, does it conform?
36
- if (bitLength !== undefined && value.length !== bitsToOctets(bitLength)) return false
37
-
38
- //Does it only has hex values?
39
- return hexRegex.test(value)
40
- }
41
-
42
- export const toHex = (buffer: ArrayBuffer) => {
43
- return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')
44
- }
45
-
46
- export function asHex(value: unknown): Hex | undefined
47
- export function asHex(value: unknown, bitLength?: number): Hex | undefined
48
- export function asHex(value: unknown, assert: AssertConfig): Hex
49
- export function asHex(value: unknown, bitLength: number | undefined, assert?: AssertConfig): Hex
50
- export function asHex(value: unknown, assertOrBitLength?: AssertConfig | number, assertOnly?: AssertConfig): Hex | undefined {
51
- const bitLength = typeof assertOrBitLength === 'number' ? assertOrBitLength : undefined
52
- const assert = typeof assertOrBitLength !== 'number' ? assertOrBitLength : assertOnly
53
-
54
- let stringValue: string | undefined = undefined
55
-
56
- switch (typeof value) {
57
- case 'string':
58
- //remove the leading 0x if it is there
59
- stringValue = hexRegexWithPrefix.test(value) ? value.substring(2) : value
60
- break
61
- case 'number':
62
- if (value === Math.floor(value)) {
63
- stringValue = value.toString(16)
64
- } else {
65
- return assertError(value, assert, 'Numbers must be whole')
66
- }
67
- break
68
- case 'bigint':
69
- stringValue = value.toString(16)
70
- break
71
- case 'object':
72
- if (isArrayBuffer(value)) {
73
- stringValue = hexFrom(value)
74
- } else {
75
- return assertError(value, assert, 'Unsupported object')
76
- }
77
- break
78
- default:
79
- return assertError(value, assert, `Unsupported type [${typeof value}]`)
80
- }
81
-
82
- //make it conform to the bit length if shorter
83
- if (stringValue && bitLength) {
84
- stringValue = stringValue.padStart(bitsToOctets(bitLength), '0')
85
- }
86
-
87
- return isHex(stringValue, bitLength) ? stringValue.toLowerCase() : assertError(value, assert, 'Unable to convert to Hash')
88
- }