@xylabs/hex 3.0.4 → 3.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/address.d.cts +1 -1
- package/dist/browser/address.d.cts.map +1 -1
- package/dist/browser/address.d.mts +1 -1
- package/dist/browser/address.d.mts.map +1 -1
- package/dist/browser/address.d.ts +1 -1
- package/dist/browser/address.d.ts.map +1 -1
- package/dist/browser/index.cjs +5 -4
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.js +5 -4
- package/dist/browser/index.js.map +1 -1
- package/dist/node/address.d.cts +1 -1
- package/dist/node/address.d.cts.map +1 -1
- package/dist/node/address.d.mts +1 -1
- package/dist/node/address.d.mts.map +1 -1
- package/dist/node/address.d.ts +1 -1
- package/dist/node/address.d.ts.map +1 -1
- package/dist/node/index.cjs +5 -4
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +5 -4
- package/dist/node/index.js.map +1 -1
- package/package.json +2 -2
- package/src/address.ts +5 -4
|
@@ -2,7 +2,7 @@ import { AssertConfig } from './assert';
|
|
|
2
2
|
import { Hex, HexConfig } from './hex';
|
|
3
3
|
export type Address = Exclude<Hex, 'reserved-address-value'>;
|
|
4
4
|
export declare const toAddress: (value: unknown, config?: HexConfig) => Lowercase<string>;
|
|
5
|
-
export declare const isAddress: (value: unknown,
|
|
5
|
+
export declare const isAddress: (value: unknown, config?: HexConfig) => value is Lowercase<string>;
|
|
6
6
|
export declare function asAddress(value: unknown): Address | undefined;
|
|
7
7
|
export declare function asAddress(value: unknown, assert: AssertConfig): Address;
|
|
8
8
|
//# sourceMappingURL=address.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAoC,MAAM,OAAO,CAAA;AAExE,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAA;AAE5D,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,sBAG1D,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,OAAO,
|
|
1
|
+
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAoC,MAAM,OAAO,CAAA;AAExE,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAA;AAE5D,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,sBAG1D,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,+BAG1D,CAAA;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,GAAG,SAAS,CAAA;AAC9D,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAA"}
|
|
@@ -2,7 +2,7 @@ import { AssertConfig } from './assert';
|
|
|
2
2
|
import { Hex, HexConfig } from './hex';
|
|
3
3
|
export type Address = Exclude<Hex, 'reserved-address-value'>;
|
|
4
4
|
export declare const toAddress: (value: unknown, config?: HexConfig) => Lowercase<string>;
|
|
5
|
-
export declare const isAddress: (value: unknown,
|
|
5
|
+
export declare const isAddress: (value: unknown, config?: HexConfig) => value is Lowercase<string>;
|
|
6
6
|
export declare function asAddress(value: unknown): Address | undefined;
|
|
7
7
|
export declare function asAddress(value: unknown, assert: AssertConfig): Address;
|
|
8
8
|
//# sourceMappingURL=address.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAoC,MAAM,OAAO,CAAA;AAExE,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAA;AAE5D,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,sBAG1D,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,OAAO,
|
|
1
|
+
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAoC,MAAM,OAAO,CAAA;AAExE,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAA;AAE5D,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,sBAG1D,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,+BAG1D,CAAA;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,GAAG,SAAS,CAAA;AAC9D,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAA"}
|
|
@@ -2,7 +2,7 @@ import { AssertConfig } from './assert';
|
|
|
2
2
|
import { Hex, HexConfig } from './hex';
|
|
3
3
|
export type Address = Exclude<Hex, 'reserved-address-value'>;
|
|
4
4
|
export declare const toAddress: (value: unknown, config?: HexConfig) => Lowercase<string>;
|
|
5
|
-
export declare const isAddress: (value: unknown,
|
|
5
|
+
export declare const isAddress: (value: unknown, config?: HexConfig) => value is Lowercase<string>;
|
|
6
6
|
export declare function asAddress(value: unknown): Address | undefined;
|
|
7
7
|
export declare function asAddress(value: unknown, assert: AssertConfig): Address;
|
|
8
8
|
//# sourceMappingURL=address.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAoC,MAAM,OAAO,CAAA;AAExE,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAA;AAE5D,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,sBAG1D,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,OAAO,
|
|
1
|
+
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAoC,MAAM,OAAO,CAAA;AAExE,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAA;AAE5D,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,sBAG1D,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,+BAG1D,CAAA;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,GAAG,SAAS,CAAA;AAC9D,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAA"}
|
package/dist/browser/index.cjs
CHANGED
|
@@ -164,17 +164,18 @@ var toHex = (buffer) => {
|
|
|
164
164
|
|
|
165
165
|
// src/address.ts
|
|
166
166
|
var toAddress = (value, config = {}) => {
|
|
167
|
-
const { bitLength = 160, prefix =
|
|
167
|
+
const { bitLength = 160, prefix = false } = config;
|
|
168
168
|
return hexFrom(value, { bitLength, prefix, ...config });
|
|
169
169
|
};
|
|
170
|
-
var isAddress = (value,
|
|
171
|
-
|
|
170
|
+
var isAddress = (value, config = {}) => {
|
|
171
|
+
const { bitLength = 160, prefix = false } = config;
|
|
172
|
+
return isHex(value, { bitLength, prefix });
|
|
172
173
|
};
|
|
173
174
|
function asAddress(value, assert) {
|
|
174
175
|
let stringValue = void 0;
|
|
175
176
|
switch (typeof value) {
|
|
176
177
|
case "string": {
|
|
177
|
-
stringValue = hexFromHexString(value, { prefix:
|
|
178
|
+
stringValue = hexFromHexString(value, { prefix: false });
|
|
178
179
|
break;
|
|
179
180
|
}
|
|
180
181
|
default: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/assert.ts","../../src/hex/from/from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/from/fromHexString.ts","../../src/hex/as.ts","../../src/hex/isHexZero.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export * from './address'\nexport * from './hash'\nexport * from './hex'\n","export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString =\n typeof assert === 'string' ? assert\n : typeof assert === 'boolean' ? defaultMessage\n : assert(value, defaultMessage)\n if (assertString) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\nimport { hexFromHexString } from './fromHexString'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): Hex => {\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 = {}): Hex => {\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): Hex => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): Hex => {\n switch (typeof value) {\n case 'string': {\n return hexFromHexString(value, config)\n }\n case 'bigint': {\n return hexFromBigInt(value, config)\n }\n case 'number': {\n return hexFromNumber(value, config)\n }\n case 'object': {\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw new Error('Invalid type: object !== ArrayBuffer')\n }\n }\n default: {\n throw new Error(`Invalid type: ${typeof value}`)\n }\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[\\da-f]+$/i\nexport const hexRegexWithPrefix = /0x[\\da-f]+$/i\n","import { Hex, HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex, hexRegexWithPrefix } from './regex'\n\nexport const isHex = (value: unknown, config?: HexConfig): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n const valueCharLength = config?.prefix ? value.length - 2 : value.length\n\n //If a bitLength specified, does it conform?\n if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false\n\n //Does it only has hex values?\n return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value)\n}\n","import { isHex } from '../is'\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): Hex => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.slice(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return (prefix ? `0x${padded}` : padded).toLowerCase() as Hex\n } else {\n throw new Error('Received string is not a value hex')\n }\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","import { hexFromHexString } from './from'\n\nexport const isHexZero = (value?: string) => {\n return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : undefined\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport type Address = Exclude<Hex, 'reserved-address-value'>\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = true } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, bitLength = 160): value is Address => {\n return isHex(value, { bitLength, prefix: true })\n}\n\nexport function asAddress(value: unknown): Address | undefined\nexport function asAddress(value: unknown, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value, { prefix: true })\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Exclude<Hex, 'reserved-hash-value'>\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACIO,IAAM,cAAc,CAAC,OAAgB,QAAkC,mBAA2B;AACvG,MAAI,QAAQ;AACV,UAAM,eACJ,OAAO,WAAW,WAAW,SAC3B,OAAO,WAAW,YAAY,iBAC9B,OAAO,OAAO,cAAc;AAChC,QAAI,cAAc;AAChB,YAAM,IAAI,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACvE;AAAA,EACF;AAEA,SAAO;AACT;;;AChBA,yBAA8B;;;ACCvB,IAAM,gBAAgB,CAAC,UAA0B;AACtD,QAAM,UAAU,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AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,SAAS,YAAY,OAAO,QAAQ,qBAAqB,OAAO,KAAK,GAAG,IAAI;AAAA,IACrF;AAAA,EACF;AACA,SAAO,OAAO,WAAW,IAAI,cAAc,YAAY,OAAO,QAAQ,wBAAwB,KAAK,GAAG;AACxG;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/assert.ts","../../src/hex/from/from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/from/fromHexString.ts","../../src/hex/as.ts","../../src/hex/isHexZero.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export * from './address'\nexport * from './hash'\nexport * from './hex'\n","export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString =\n typeof assert === 'string' ? assert\n : typeof assert === 'boolean' ? defaultMessage\n : assert(value, defaultMessage)\n if (assertString) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\nimport { hexFromHexString } from './fromHexString'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): Hex => {\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 = {}): Hex => {\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): Hex => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): Hex => {\n switch (typeof value) {\n case 'string': {\n return hexFromHexString(value, config)\n }\n case 'bigint': {\n return hexFromBigInt(value, config)\n }\n case 'number': {\n return hexFromNumber(value, config)\n }\n case 'object': {\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw new Error('Invalid type: object !== ArrayBuffer')\n }\n }\n default: {\n throw new Error(`Invalid type: ${typeof value}`)\n }\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[\\da-f]+$/i\nexport const hexRegexWithPrefix = /0x[\\da-f]+$/i\n","import { Hex, HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex, hexRegexWithPrefix } from './regex'\n\nexport const isHex = (value: unknown, config?: HexConfig): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n const valueCharLength = config?.prefix ? value.length - 2 : value.length\n\n //If a bitLength specified, does it conform?\n if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false\n\n //Does it only has hex values?\n return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value)\n}\n","import { isHex } from '../is'\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): Hex => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.slice(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return (prefix ? `0x${padded}` : padded).toLowerCase() as Hex\n } else {\n throw new Error('Received string is not a value hex')\n }\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","import { hexFromHexString } from './from'\n\nexport const isHexZero = (value?: string) => {\n return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : undefined\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport type Address = Exclude<Hex, 'reserved-address-value'>\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = false } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, config: HexConfig = {}): value is Address => {\n const { bitLength = 160, prefix = false } = config\n return isHex(value, { bitLength, prefix })\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: false })\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Exclude<Hex, 'reserved-hash-value'>\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACIO,IAAM,cAAc,CAAC,OAAgB,QAAkC,mBAA2B;AACvG,MAAI,QAAQ;AACV,UAAM,eACJ,OAAO,WAAW,WAAW,SAC3B,OAAO,WAAW,YAAY,iBAC9B,OAAO,OAAO,cAAc;AAChC,QAAI,cAAc;AAChB,YAAM,IAAI,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACvE;AAAA,EACF;AAEA,SAAO;AACT;;;AChBA,yBAA8B;;;ACCvB,IAAM,gBAAgB,CAAC,UAA0B;AACtD,QAAM,UAAU,SAAS;AACzB,MAAI,UAAU,WAAW;AAAG,UAAM,IAAI,MAAM,qCAAqC;AACjF,SAAO;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S;AACP,aAAO,SAAS,YAAY,OAAO,QAAQ,qBAAqB,OAAO,KAAK,GAAG,IAAI;AAAA,IACrF;AAAA,EACF;AACA,SAAO,OAAO,WAAW,IAAI,cAAc,YAAY,OAAO,QAAQ,wBAAwB,KAAK,GAAG;AACxG;","names":[]}
|
package/dist/browser/index.js
CHANGED
|
@@ -118,17 +118,18 @@ var toHex = (buffer) => {
|
|
|
118
118
|
|
|
119
119
|
// src/address.ts
|
|
120
120
|
var toAddress = (value, config = {}) => {
|
|
121
|
-
const { bitLength = 160, prefix =
|
|
121
|
+
const { bitLength = 160, prefix = false } = config;
|
|
122
122
|
return hexFrom(value, { bitLength, prefix, ...config });
|
|
123
123
|
};
|
|
124
|
-
var isAddress = (value,
|
|
125
|
-
|
|
124
|
+
var isAddress = (value, config = {}) => {
|
|
125
|
+
const { bitLength = 160, prefix = false } = config;
|
|
126
|
+
return isHex(value, { bitLength, prefix });
|
|
126
127
|
};
|
|
127
128
|
function asAddress(value, assert) {
|
|
128
129
|
let stringValue = void 0;
|
|
129
130
|
switch (typeof value) {
|
|
130
131
|
case "string": {
|
|
131
|
-
stringValue = hexFromHexString(value, { prefix:
|
|
132
|
+
stringValue = hexFromHexString(value, { prefix: false });
|
|
132
133
|
break;
|
|
133
134
|
}
|
|
134
135
|
default: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/assert.ts","../../src/hex/from/from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/from/fromHexString.ts","../../src/hex/as.ts","../../src/hex/isHexZero.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString =\n typeof assert === 'string' ? assert\n : typeof assert === 'boolean' ? defaultMessage\n : assert(value, defaultMessage)\n if (assertString) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\nimport { hexFromHexString } from './fromHexString'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): Hex => {\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 = {}): Hex => {\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): Hex => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): Hex => {\n switch (typeof value) {\n case 'string': {\n return hexFromHexString(value, config)\n }\n case 'bigint': {\n return hexFromBigInt(value, config)\n }\n case 'number': {\n return hexFromNumber(value, config)\n }\n case 'object': {\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw new Error('Invalid type: object !== ArrayBuffer')\n }\n }\n default: {\n throw new Error(`Invalid type: ${typeof value}`)\n }\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[\\da-f]+$/i\nexport const hexRegexWithPrefix = /0x[\\da-f]+$/i\n","import { Hex, HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex, hexRegexWithPrefix } from './regex'\n\nexport const isHex = (value: unknown, config?: HexConfig): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n const valueCharLength = config?.prefix ? value.length - 2 : value.length\n\n //If a bitLength specified, does it conform?\n if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false\n\n //Does it only has hex values?\n return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value)\n}\n","import { isHex } from '../is'\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): Hex => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.slice(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return (prefix ? `0x${padded}` : padded).toLowerCase() as Hex\n } else {\n throw new Error('Received string is not a value hex')\n }\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","import { hexFromHexString } from './from'\n\nexport const isHexZero = (value?: string) => {\n return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : undefined\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport type Address = Exclude<Hex, 'reserved-address-value'>\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = true } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, bitLength = 160): value is Address => {\n return isHex(value, { bitLength, prefix: true })\n}\n\nexport function asAddress(value: unknown): Address | undefined\nexport function asAddress(value: unknown, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value, { prefix: true })\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Exclude<Hex, 'reserved-hash-value'>\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n"],"mappings":";AAIO,IAAM,cAAc,CAAC,OAAgB,QAAk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AAkB,CAAC,UAA2C;AACzE,SAAO,OAAO,UAAU,YAAY,cAAc,SAAS,KAAsB;AACnF;AAGO,IAAM,SAAS,CAAC,OAAgB,YAA2B,QAAuB;AACvF,SAAO,MAAM,OAAO,EAAE,UAAU,CAAC;AACnC;AAIO,SAAS,OAAO,OAAgB,QAAyC;AAC9E,MAAI,cAAkC;AAEtC,UAAQ,OAAO,OAAO;AAAA,IACpB,KAAK,UAAU;AACb,oBAAc,iBAAiB,KAAK;AACpC;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,SAAS,YAAY,OAAO,QAAQ,qBAAqB,OAAO,KAAK,GAAG,IAAI;AAAA,IACrF;AAAA,EACF;AACA,SAAO,OAAO,WAAW,IAAI,cAAc,YAAY,OAAO,QAAQ,wBAAwB,KAAK,GAAG;AACxG;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/assert.ts","../../src/hex/from/from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/from/fromHexString.ts","../../src/hex/as.ts","../../src/hex/isHexZero.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString =\n typeof assert === 'string' ? assert\n : typeof assert === 'boolean' ? defaultMessage\n : assert(value, defaultMessage)\n if (assertString) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\nimport { hexFromHexString } from './fromHexString'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): Hex => {\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 = {}): Hex => {\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): Hex => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): Hex => {\n switch (typeof value) {\n case 'string': {\n return hexFromHexString(value, config)\n }\n case 'bigint': {\n return hexFromBigInt(value, config)\n }\n case 'number': {\n return hexFromNumber(value, config)\n }\n case 'object': {\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw new Error('Invalid type: object !== ArrayBuffer')\n }\n }\n default: {\n throw new Error(`Invalid type: ${typeof value}`)\n }\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[\\da-f]+$/i\nexport const hexRegexWithPrefix = /0x[\\da-f]+$/i\n","import { Hex, HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex, hexRegexWithPrefix } from './regex'\n\nexport const isHex = (value: unknown, config?: HexConfig): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n const valueCharLength = config?.prefix ? value.length - 2 : value.length\n\n //If a bitLength specified, does it conform?\n if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false\n\n //Does it only has hex values?\n return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value)\n}\n","import { isHex } from '../is'\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): Hex => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.slice(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return (prefix ? `0x${padded}` : padded).toLowerCase() as Hex\n } else {\n throw new Error('Received string is not a value hex')\n }\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","import { hexFromHexString } from './from'\n\nexport const isHexZero = (value?: string) => {\n return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : undefined\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport type Address = Exclude<Hex, 'reserved-address-value'>\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = false } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, config: HexConfig = {}): value is Address => {\n const { bitLength = 160, prefix = false } = config\n return isHex(value, { bitLength, prefix })\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: false })\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Exclude<Hex, 'reserved-hash-value'>\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n"],"mappings":";AAIO,IAAM,cAAc,CAAC,OAAgB,QAAkC,mBAA2B;AACvG,MAAI,QAAQ;AACV,UAAM,eACJ,OAAO,WAAW,WAAW,SAC3B,OAAO,WAAW,YAAY,iBAC9B,OAAO,OAAO,cAAc;AAChC,QAAI,cAAc;AAChB,YAAM,IAAI,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACvE;AAAA,EACF;AAEA,SAAO;AACT;;;AChBA,SAAS,qBAAqB;;;ACCvB,IAAM,gBAAgB,CAAC,UAA0B;AACt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OAAO;AAAA,IACpB,KAAK,UAAU;AACb,oBAAc,iBAAiB,KAAK;AACpC;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,SAAS,YAAY,OAAO,QAAQ,qBAAqB,OAAO,KAAK,GAAG,IAAI;AAAA,IACrF;AAAA,EACF;AACA,SAAO,OAAO,WAAW,IAAI,cAAc,YAAY,OAAO,QAAQ,wBAAwB,KAAK,GAAG;AACxG;","names":[]}
|
package/dist/node/address.d.cts
CHANGED
|
@@ -2,7 +2,7 @@ import { AssertConfig } from './assert';
|
|
|
2
2
|
import { Hex, HexConfig } from './hex';
|
|
3
3
|
export type Address = Exclude<Hex, 'reserved-address-value'>;
|
|
4
4
|
export declare const toAddress: (value: unknown, config?: HexConfig) => Lowercase<string>;
|
|
5
|
-
export declare const isAddress: (value: unknown,
|
|
5
|
+
export declare const isAddress: (value: unknown, config?: HexConfig) => value is Lowercase<string>;
|
|
6
6
|
export declare function asAddress(value: unknown): Address | undefined;
|
|
7
7
|
export declare function asAddress(value: unknown, assert: AssertConfig): Address;
|
|
8
8
|
//# sourceMappingURL=address.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAoC,MAAM,OAAO,CAAA;AAExE,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAA;AAE5D,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,sBAG1D,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,OAAO,
|
|
1
|
+
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAoC,MAAM,OAAO,CAAA;AAExE,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAA;AAE5D,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,sBAG1D,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,+BAG1D,CAAA;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,GAAG,SAAS,CAAA;AAC9D,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAA"}
|
package/dist/node/address.d.mts
CHANGED
|
@@ -2,7 +2,7 @@ import { AssertConfig } from './assert';
|
|
|
2
2
|
import { Hex, HexConfig } from './hex';
|
|
3
3
|
export type Address = Exclude<Hex, 'reserved-address-value'>;
|
|
4
4
|
export declare const toAddress: (value: unknown, config?: HexConfig) => Lowercase<string>;
|
|
5
|
-
export declare const isAddress: (value: unknown,
|
|
5
|
+
export declare const isAddress: (value: unknown, config?: HexConfig) => value is Lowercase<string>;
|
|
6
6
|
export declare function asAddress(value: unknown): Address | undefined;
|
|
7
7
|
export declare function asAddress(value: unknown, assert: AssertConfig): Address;
|
|
8
8
|
//# sourceMappingURL=address.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAoC,MAAM,OAAO,CAAA;AAExE,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAA;AAE5D,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,sBAG1D,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,OAAO,
|
|
1
|
+
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAoC,MAAM,OAAO,CAAA;AAExE,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAA;AAE5D,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,sBAG1D,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,+BAG1D,CAAA;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,GAAG,SAAS,CAAA;AAC9D,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAA"}
|
package/dist/node/address.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { AssertConfig } from './assert';
|
|
|
2
2
|
import { Hex, HexConfig } from './hex';
|
|
3
3
|
export type Address = Exclude<Hex, 'reserved-address-value'>;
|
|
4
4
|
export declare const toAddress: (value: unknown, config?: HexConfig) => Lowercase<string>;
|
|
5
|
-
export declare const isAddress: (value: unknown,
|
|
5
|
+
export declare const isAddress: (value: unknown, config?: HexConfig) => value is Lowercase<string>;
|
|
6
6
|
export declare function asAddress(value: unknown): Address | undefined;
|
|
7
7
|
export declare function asAddress(value: unknown, assert: AssertConfig): Address;
|
|
8
8
|
//# sourceMappingURL=address.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAoC,MAAM,OAAO,CAAA;AAExE,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAA;AAE5D,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,sBAG1D,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,OAAO,
|
|
1
|
+
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAA;AACpD,OAAO,EAAE,GAAG,EAAE,SAAS,EAAoC,MAAM,OAAO,CAAA;AAExE,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,wBAAwB,CAAC,CAAA;AAE5D,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,sBAG1D,CAAA;AAED,eAAO,MAAM,SAAS,UAAW,OAAO,WAAU,SAAS,+BAG1D,CAAA;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,GAAG,SAAS,CAAA;AAC9D,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAA"}
|
package/dist/node/index.cjs
CHANGED
|
@@ -164,17 +164,18 @@ var toHex = (buffer) => {
|
|
|
164
164
|
|
|
165
165
|
// src/address.ts
|
|
166
166
|
var toAddress = (value, config = {}) => {
|
|
167
|
-
const { bitLength = 160, prefix =
|
|
167
|
+
const { bitLength = 160, prefix = false } = config;
|
|
168
168
|
return hexFrom(value, { bitLength, prefix, ...config });
|
|
169
169
|
};
|
|
170
|
-
var isAddress = (value,
|
|
171
|
-
|
|
170
|
+
var isAddress = (value, config = {}) => {
|
|
171
|
+
const { bitLength = 160, prefix = false } = config;
|
|
172
|
+
return isHex(value, { bitLength, prefix });
|
|
172
173
|
};
|
|
173
174
|
function asAddress(value, assert) {
|
|
174
175
|
let stringValue = void 0;
|
|
175
176
|
switch (typeof value) {
|
|
176
177
|
case "string": {
|
|
177
|
-
stringValue = hexFromHexString(value, { prefix:
|
|
178
|
+
stringValue = hexFromHexString(value, { prefix: false });
|
|
178
179
|
break;
|
|
179
180
|
}
|
|
180
181
|
default: {
|
package/dist/node/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/assert.ts","../../src/hex/from/from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/from/fromHexString.ts","../../src/hex/as.ts","../../src/hex/isHexZero.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export * from './address'\nexport * from './hash'\nexport * from './hex'\n","export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString =\n typeof assert === 'string' ? assert\n : typeof assert === 'boolean' ? defaultMessage\n : assert(value, defaultMessage)\n if (assertString) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\nimport { hexFromHexString } from './fromHexString'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): Hex => {\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 = {}): Hex => {\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): Hex => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): Hex => {\n switch (typeof value) {\n case 'string': {\n return hexFromHexString(value, config)\n }\n case 'bigint': {\n return hexFromBigInt(value, config)\n }\n case 'number': {\n return hexFromNumber(value, config)\n }\n case 'object': {\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw new Error('Invalid type: object !== ArrayBuffer')\n }\n }\n default: {\n throw new Error(`Invalid type: ${typeof value}`)\n }\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[\\da-f]+$/i\nexport const hexRegexWithPrefix = /0x[\\da-f]+$/i\n","import { Hex, HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex, hexRegexWithPrefix } from './regex'\n\nexport const isHex = (value: unknown, config?: HexConfig): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n const valueCharLength = config?.prefix ? value.length - 2 : value.length\n\n //If a bitLength specified, does it conform?\n if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false\n\n //Does it only has hex values?\n return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value)\n}\n","import { isHex } from '../is'\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): Hex => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.slice(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return (prefix ? `0x${padded}` : padded).toLowerCase() as Hex\n } else {\n throw new Error('Received string is not a value hex')\n }\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","import { hexFromHexString } from './from'\n\nexport const isHexZero = (value?: string) => {\n return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : undefined\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport type Address = Exclude<Hex, 'reserved-address-value'>\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = true } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, bitLength = 160): value is Address => {\n return isHex(value, { bitLength, prefix: true })\n}\n\nexport function asAddress(value: unknown): Address | undefined\nexport function asAddress(value: unknown, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value, { prefix: true })\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Exclude<Hex, 'reserved-hash-value'>\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACIO,IAAM,cAAc,CAAC,OAAgB,QAAkC,mBAA2B;AACvG,MAAI,QAAQ;AACV,UAAM,eACJ,OAAO,WAAW,WAAW,SAC3B,OAAO,WAAW,YAAY,iBAC9B,OAAO,OAAO,cAAc;AAChC,QAAI,cAAc;AAChB,YAAM,IAAI,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACvE;AAAA,EACF;AAEA,SAAO;AACT;;;AChBA,yBAA8B;;;ACCvB,IAAM,gBAAgB,CAAC,UAA0B;AACtD,QAAM,UAAU,SAAS;AACzB,MAAI,UAAU,WAAW;AAAG,UAAM,IAAI,MAAM,qCAAqC;AACjF,SAAO;AACT;AAGO,IAAM,gBAAgB,CAAC,UAA0B;AACtD,SAAO,SAAS;AAClB;;;ACVO,IAAM,WAAW;AACjB,IAAM,qBAAqB;;;ACG3B,IAAM,QAAQ,CAAC,OAAgB,WAAqC;AAEzE,MAAI,OAAO,UAAU;AAAU,WAAO;AAEtC,QAAM,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from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/from/fromHexString.ts","../../src/hex/as.ts","../../src/hex/isHexZero.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export * from './address'\nexport * from './hash'\nexport * from './hex'\n","export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString =\n typeof assert === 'string' ? assert\n : typeof assert === 'boolean' ? defaultMessage\n : assert(value, defaultMessage)\n if (assertString) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\nimport { hexFromHexString } from './fromHexString'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): Hex => {\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 = {}): Hex => {\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): Hex => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): Hex => {\n switch (typeof value) {\n case 'string': {\n return hexFromHexString(value, config)\n }\n case 'bigint': {\n return hexFromBigInt(value, config)\n }\n case 'number': {\n return hexFromNumber(value, config)\n }\n case 'object': {\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw new Error('Invalid type: object !== ArrayBuffer')\n }\n }\n default: {\n throw new Error(`Invalid type: ${typeof value}`)\n }\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[\\da-f]+$/i\nexport const hexRegexWithPrefix = /0x[\\da-f]+$/i\n","import { Hex, HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex, hexRegexWithPrefix } from './regex'\n\nexport const isHex = (value: unknown, config?: HexConfig): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n const valueCharLength = config?.prefix ? value.length - 2 : value.length\n\n //If a bitLength specified, does it conform?\n if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false\n\n //Does it only has hex values?\n return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value)\n}\n","import { isHex } from '../is'\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): Hex => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.slice(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return (prefix ? `0x${padded}` : padded).toLowerCase() as Hex\n } else {\n throw new Error('Received string is not a value hex')\n }\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","import { hexFromHexString } from './from'\n\nexport const isHexZero = (value?: string) => {\n return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : undefined\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport type Address = Exclude<Hex, 'reserved-address-value'>\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = false } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, config: HexConfig = {}): value is Address => {\n const { bitLength = 160, prefix = false } = config\n return isHex(value, { bitLength, prefix })\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: false })\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Exclude<Hex, 'reserved-hash-value'>\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACIO,IAAM,cAAc,CAAC,OAAgB,QAAkC,mBAA2B;AACvG,MAAI,QAAQ;AACV,UAAM,eACJ,OAAO,WAAW,WAAW,SAC3B,OAAO,WAAW,YAAY,iBAC9B,OAAO,OAAO,cAAc;AAChC,QAAI,cAAc;AAChB,YAAM,IAAI,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACvE;AAAA,EACF;AAEA,SAAO;AACT;;;AChBA,yBAA8B;;;ACCvB,IAAM,gBAAgB,CAAC,UAA0B;AACtD,QAAM,UAAU,SAAS;AACzB,MAAI,UAAU,WAAW;AAAG,UAAM,IAAI,MAAM,qCAAqC;AACjF,SAAO;AACT;AAGO,IAAM,gBAAgB,CAAC,UAA0B;AACtD,SAAO,SAAS;AAClB;;;ACVO,IAAM,WAAW;AACjB,IAAM,qBAAqB;;;ACG3B,IAAM,QAAQ,CAAC,OAAgB,WAAqC;AAEzE,MAAI,OAAO,UAAU;AAAU,WAAO;AAEtC,QAAM,mBAAkB,iCAAQ,UAAS,MAAM,SAAS,IAAI,MAAM;AAGlE,OAAI,iCAAQ,e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119
119
|
// src/address.ts
|
|
120
120
|
var toAddress = (value, config = {}) => {
|
|
121
|
-
const { bitLength = 160, prefix =
|
|
121
|
+
const { bitLength = 160, prefix = false } = config;
|
|
122
122
|
return hexFrom(value, { bitLength, prefix, ...config });
|
|
123
123
|
};
|
|
124
|
-
var isAddress = (value,
|
|
125
|
-
|
|
124
|
+
var isAddress = (value, config = {}) => {
|
|
125
|
+
const { bitLength = 160, prefix = false } = config;
|
|
126
|
+
return isHex(value, { bitLength, prefix });
|
|
126
127
|
};
|
|
127
128
|
function asAddress(value, assert) {
|
|
128
129
|
let stringValue = void 0;
|
|
129
130
|
switch (typeof value) {
|
|
130
131
|
case "string": {
|
|
131
|
-
stringValue = hexFromHexString(value, { prefix:
|
|
132
|
+
stringValue = hexFromHexString(value, { prefix: false });
|
|
132
133
|
break;
|
|
133
134
|
}
|
|
134
135
|
default: {
|
package/dist/node/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/assert.ts","../../src/hex/from/from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/from/fromHexString.ts","../../src/hex/as.ts","../../src/hex/isHexZero.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString =\n typeof assert === 'string' ? assert\n : typeof assert === 'boolean' ? defaultMessage\n : assert(value, defaultMessage)\n if (assertString) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\nimport { hexFromHexString } from './fromHexString'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): Hex => {\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 = {}): Hex => {\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): Hex => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): Hex => {\n switch (typeof value) {\n case 'string': {\n return hexFromHexString(value, config)\n }\n case 'bigint': {\n return hexFromBigInt(value, config)\n }\n case 'number': {\n return hexFromNumber(value, config)\n }\n case 'object': {\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw new Error('Invalid type: object !== ArrayBuffer')\n }\n }\n default: {\n throw new Error(`Invalid type: ${typeof value}`)\n }\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[\\da-f]+$/i\nexport const hexRegexWithPrefix = /0x[\\da-f]+$/i\n","import { Hex, HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex, hexRegexWithPrefix } from './regex'\n\nexport const isHex = (value: unknown, config?: HexConfig): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n const valueCharLength = config?.prefix ? value.length - 2 : value.length\n\n //If a bitLength specified, does it conform?\n if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false\n\n //Does it only has hex values?\n return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value)\n}\n","import { isHex } from '../is'\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): Hex => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.slice(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return (prefix ? `0x${padded}` : padded).toLowerCase() as Hex\n } else {\n throw new Error('Received string is not a value hex')\n }\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","import { hexFromHexString } from './from'\n\nexport const isHexZero = (value?: string) => {\n return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : undefined\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport type Address = Exclude<Hex, 'reserved-address-value'>\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = true } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, bitLength = 160): value is Address => {\n return isHex(value, { bitLength, prefix: true })\n}\n\nexport function asAddress(value: unknown): Address | undefined\nexport function asAddress(value: unknown, assert: AssertConfig): Address\nexport function asAddress(value: unknown, assert?: AssertConfig): Address | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value, { prefix: true })\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Exclude<Hex, 'reserved-hash-value'>\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n"],"mappings":";AAIO,IAAM,cAAc,CAAC,OAAgB,QAAkC,mBAA2B;AACvG,MAAI,QAAQ;AACV,UAAM,eACJ,OAAO,WAAW,WAAW,SAC3B,OAAO,WAAW,YAAY,iBAC9B,OAAO,OAAO,cAAc;AAChC,QAAI,cAAc;AAChB,YAAM,IAAI,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACvE;AAAA,EACF;AAEA,SAAO;AACT;;;AChBA,SAAS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OAAO,OAAO;AAAA,IACpB,KAAK,UAAU;AACb,oBAAc,iBAAiB,KAAK;AACpC;AAAA,IACF;AAAA,IACA,SAAS;AACP,aAAO,SAAS,YAAY,OAAO,QAAQ,qBAAqB,OAAO,KAAK,GAAG,IAAI;AAAA,IACrF;AAAA,EACF;AACA,SAAO,OAAO,WAAW,IAAI,cAAc,YAAY,OAAO,QAAQ,wBAAwB,KAAK,GAAG;AACxG;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/assert.ts","../../src/hex/from/from.ts","../../src/hex/nibble.ts","../../src/hex/regex.ts","../../src/hex/is.ts","../../src/hex/from/fromHexString.ts","../../src/hex/as.ts","../../src/hex/isHexZero.ts","../../src/hex/legacy.ts","../../src/address.ts","../../src/hash.ts"],"sourcesContent":["export type AssertCallback = (value: unknown, message: string) => string | boolean\n\nexport type AssertConfig = string | AssertCallback | boolean\n\nexport const assertError = (value: unknown, assert: AssertConfig | undefined, defaultMessage: string) => {\n if (assert) {\n const assertString =\n typeof assert === 'string' ? assert\n : typeof assert === 'boolean' ? defaultMessage\n : assert(value, defaultMessage)\n if (assertString) {\n throw new Error(assertString === true ? defaultMessage : assertString)\n }\n }\n // eslint-disable-next-line unicorn/no-useless-undefined\n return undefined\n}\n","import { isArrayBuffer } from '@xylabs/arraybuffer'\n\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\nimport { hexFromHexString } from './fromHexString'\n\nexport const hexFromArrayBuffer = (buffer: ArrayBuffer, config?: HexConfig): Hex => {\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 = {}): Hex => {\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): Hex => {\n return hexFromBigInt(BigInt(value), config)\n}\n\nexport const hexFrom = (value: unknown, config?: HexConfig): Hex => {\n switch (typeof value) {\n case 'string': {\n return hexFromHexString(value, config)\n }\n case 'bigint': {\n return hexFromBigInt(value, config)\n }\n case 'number': {\n return hexFromNumber(value, config)\n }\n case 'object': {\n if (isArrayBuffer(value)) {\n return hexFromArrayBuffer(value, config)\n } else {\n throw new Error('Invalid type: object !== ArrayBuffer')\n }\n }\n default: {\n throw new Error(`Invalid type: ${typeof value}`)\n }\n }\n}\n","//determine the number of nibbles for a given number of bits\nexport const bitsToNibbles = (value: number): number => {\n const nibbles = value >> 2\n if (value !== nibbles << 2) throw new Error('Bits for nibbles must multiple of 4')\n return nibbles\n}\n\n//determine the number of nibbles for a given number of bits\nexport const nibblesToBits = (value: number): number => {\n return value << 2\n}\n","export const hexRegex = /^[\\da-f]+$/i\nexport const hexRegexWithPrefix = /0x[\\da-f]+$/i\n","import { Hex, HexConfig } from './model'\nimport { bitsToNibbles } from './nibble'\nimport { hexRegex, hexRegexWithPrefix } from './regex'\n\nexport const isHex = (value: unknown, config?: HexConfig): value is Hex => {\n //Is it a string?\n if (typeof value !== 'string') return false\n\n const valueCharLength = config?.prefix ? value.length - 2 : value.length\n\n //If a bitLength specified, does it conform?\n if (config?.bitLength !== undefined && valueCharLength !== bitsToNibbles(config?.bitLength)) return false\n\n //Does it only has hex values?\n return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value)\n}\n","import { isHex } from '../is'\nimport { Hex, HexConfig } from '../model'\nimport { bitsToNibbles } from '../nibble'\n\nexport const hexFromHexString = (value: string, config: HexConfig = {}): Hex => {\n const { prefix = false, byteSize = 8 } = config\n const nibbleBoundary = bitsToNibbles(byteSize)\n const unPadded = (value.startsWith('0x') ? value.slice(2) : value).toLowerCase()\n if (isHex(unPadded)) {\n const padded = unPadded.padStart(unPadded.length + (unPadded.length % nibbleBoundary), '0')\n return (prefix ? `0x${padded}` : padded).toLowerCase() as Hex\n } else {\n throw new Error('Received string is not a value hex')\n }\n}\n","import { AssertConfig, assertError } from '../assert'\nimport { hexFromHexString } from './from'\nimport { isHex } from './is'\nimport { Hex } from './model'\n\nexport function asHex(value: unknown): Hex | undefined\nexport function asHex(value: unknown, assert: AssertConfig): Hex\nexport function asHex(value: unknown, assert?: AssertConfig): Hex | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assertError(value, assert, `Unsupported type [${typeof value}]`)\n }\n }\n\n return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`)\n}\n","import { hexFromHexString } from './from'\n\nexport const isHexZero = (value?: string) => {\n return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : undefined\n}\n","export const toHexLegacy = (buffer: ArrayBuffer) => {\n return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('')\n}\n\n/** @deprecated use toHexLegacy instead */\nexport const toHex = (buffer: ArrayBuffer) => {\n return toHexLegacy(buffer)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'\n\nexport type Address = Exclude<Hex, 'reserved-address-value'>\n\nexport const toAddress = (value: unknown, config: HexConfig = {}) => {\n const { bitLength = 160, prefix = false } = config\n return hexFrom(value, { bitLength, prefix, ...config })\n}\n\nexport const isAddress = (value: unknown, config: HexConfig = {}): value is Address => {\n const { bitLength = 160, prefix = false } = config\n return isHex(value, { bitLength, prefix })\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: false })\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`)\n}\n","import { AssertConfig, assertError } from './assert'\nimport { Hex, hexFromHexString, isHex } from './hex'\n\nexport type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096\nexport const HashBitLength: HashBitLength[] = [32, 64, 128, 256, 512, 1024, 2048, 4096]\n\nexport const isHashBitLength = (value: unknown): value is HashBitLength => {\n return typeof value === 'number' && HashBitLength.includes(value as HashBitLength)\n}\n\nexport type Hash = Exclude<Hex, 'reserved-hash-value'>\nexport const isHash = (value: unknown, bitLength: HashBitLength = 256): value is Hash => {\n return isHex(value, { bitLength })\n}\n\nexport function asHash(value: unknown): Hash | undefined\nexport function asHash(value: unknown, assert: AssertConfig): Hash\nexport function asHash(value: unknown, assert?: AssertConfig): Hash | undefined {\n let stringValue: string | undefined = undefined\n\n switch (typeof value) {\n case 'string': {\n stringValue = hexFromHexString(value)\n break\n }\n default: {\n return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : undefined\n }\n }\n return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`)\n}\n"],"mappings":";AAIO,IAAM,cAAc,CAAC,OAAgB,QAAkC,mBAA2B;AACvG,MAAI,QAAQ;AACV,UAAM,eACJ,OAAO,WAAW,WAAW,SAC3B,OAAO,WAAW,YAAY,iBAC9B,OAAO,OAAO,cAAc;AAChC,QAAI,cAAc;AAChB,YAAM,IAAI,MAAM,iBAAiB,OAAO,iBAAiB,YAAY;AAAA,IACvE;AAAA,EACF;AAEA,SAAO;AACT;;;AChBA,SAAS,qBAAqB;;;ACCvB,IAAM,gBAAgB,CAAC,UAA0B;AACtD,QAAM,UAAU,SAAS;AACzB,MAAI,UAAU,WAAW;AAAG,UAAM,IAAI,MAAM,qCAAqC;AACjF,SAAO;AACT;AAGO,IAAM,gBAAgB,CAAC,UAA0B;AACtD,SAAO,SAAS;AAClB;;;ACVO,IAAM,WAAW;AACjB,IAAM,qBAAqB;;;ACG3B,IAAM,QAAQ,CAAC,OAAgB,WAAqC;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AQ,wBAAwB,KAAK,GAAG;AACxG;","names":[]}
|
package/package.json
CHANGED
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"esm"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@xylabs/arraybuffer": "~3.0.
|
|
42
|
+
"@xylabs/arraybuffer": "~3.0.6"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@xylabs/ts-scripts-yarn3": "^3.4.1",
|
|
@@ -57,6 +57,6 @@
|
|
|
57
57
|
"url": "https://github.com/xylabs/sdk-js.git"
|
|
58
58
|
},
|
|
59
59
|
"sideEffects": false,
|
|
60
|
-
"version": "3.0.
|
|
60
|
+
"version": "3.0.6",
|
|
61
61
|
"type": "module"
|
|
62
62
|
}
|
package/src/address.ts
CHANGED
|
@@ -4,12 +4,13 @@ import { Hex, HexConfig, hexFrom, hexFromHexString, isHex } from './hex'
|
|
|
4
4
|
export type Address = Exclude<Hex, 'reserved-address-value'>
|
|
5
5
|
|
|
6
6
|
export const toAddress = (value: unknown, config: HexConfig = {}) => {
|
|
7
|
-
const { bitLength = 160, prefix =
|
|
7
|
+
const { bitLength = 160, prefix = false } = config
|
|
8
8
|
return hexFrom(value, { bitLength, prefix, ...config })
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export const isAddress = (value: unknown,
|
|
12
|
-
|
|
11
|
+
export const isAddress = (value: unknown, config: HexConfig = {}): value is Address => {
|
|
12
|
+
const { bitLength = 160, prefix = false } = config
|
|
13
|
+
return isHex(value, { bitLength, prefix })
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
export function asAddress(value: unknown): Address | undefined
|
|
@@ -19,7 +20,7 @@ export function asAddress(value: unknown, assert?: AssertConfig): Address | unde
|
|
|
19
20
|
|
|
20
21
|
switch (typeof value) {
|
|
21
22
|
case 'string': {
|
|
22
|
-
stringValue = hexFromHexString(value, { prefix:
|
|
23
|
+
stringValue = hexFromHexString(value, { prefix: false })
|
|
23
24
|
break
|
|
24
25
|
}
|
|
25
26
|
default: {
|