@scure/base
Advanced tools
Comparing version 1.1.3 to 1.1.4
41
index.ts
@@ -20,2 +20,9 @@ /*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
function isBytes(a: unknown): a is Uint8Array { | ||
return ( | ||
a instanceof Uint8Array || | ||
(a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array') | ||
); | ||
} | ||
// TODO: some recusive type inference so it would check correct order of input/output inside rest? | ||
@@ -41,13 +48,9 @@ // like <string, number>, <number, bytes>, <bytes, float> | ||
function chain<T extends Chain & AsChain<T>>(...args: T): Coder<Input<First<T>>, Output<Last<T>>> { | ||
const id = (a: any) => a; | ||
// Wrap call in closure so JIT can inline calls | ||
const wrap = (a: any, b: any) => (c: any) => a(b(c)); | ||
// Construct chain of args[-1].encode(args[-2].encode([...])) | ||
const encode = Array.from(args) | ||
.reverse() | ||
.reduce((acc, i: any) => (acc ? wrap(acc, i.encode) : i.encode), undefined) as any; | ||
const encode = args.map((x) => x.encode).reduceRight(wrap, id); | ||
// Construct chain of args[0].decode(args[1].decode(...)) | ||
const decode = args.reduce( | ||
(acc, i: any) => (acc ? wrap(acc, i.decode) : i.decode), | ||
undefined | ||
) as any; | ||
const decode = args.map((x) => x.decode).reduce(wrap, id); | ||
return { encode, decode }; | ||
@@ -238,4 +241,3 @@ } | ||
encode: (bytes: Uint8Array) => { | ||
if (!(bytes instanceof Uint8Array)) | ||
throw new Error('radix.encode input should be Uint8Array'); | ||
if (!isBytes(bytes)) throw new Error('radix.encode input should be Uint8Array'); | ||
return convertRadix(Array.from(bytes), 2 ** 8, num); | ||
@@ -245,3 +247,3 @@ }, | ||
if (!Array.isArray(digits) || (digits.length && typeof digits[0] !== 'number')) | ||
throw new Error('radix.decode input should be array of strings'); | ||
throw new Error('radix.decode input should be array of numbers'); | ||
return Uint8Array.from(convertRadix(digits, num, 2 ** 8)); | ||
@@ -264,4 +266,3 @@ }, | ||
encode: (bytes: Uint8Array) => { | ||
if (!(bytes instanceof Uint8Array)) | ||
throw new Error('radix2.encode input should be Uint8Array'); | ||
if (!isBytes(bytes)) throw new Error('radix2.encode input should be Uint8Array'); | ||
return convertRadix2(Array.from(bytes), 8, bits, !revPadding); | ||
@@ -271,3 +272,3 @@ }, | ||
if (!Array.isArray(digits) || (digits.length && typeof digits[0] !== 'number')) | ||
throw new Error('radix2.decode input should be array of strings'); | ||
throw new Error('radix2.decode input should be array of numbers'); | ||
return Uint8Array.from(convertRadix2(digits, bits, 8, revPadding)); | ||
@@ -302,4 +303,3 @@ }, | ||
encode(data: Uint8Array) { | ||
if (!(data instanceof Uint8Array)) | ||
throw new Error('checksum.encode: input should be Uint8Array'); | ||
if (!isBytes(data)) throw new Error('checksum.encode: input should be Uint8Array'); | ||
const checksum = fn(data).slice(0, len); | ||
@@ -312,4 +312,3 @@ const res = new Uint8Array(data.length + len); | ||
decode(data: Uint8Array) { | ||
if (!(data instanceof Uint8Array)) | ||
throw new Error('checksum.decode: input should be Uint8Array'); | ||
if (!isBytes(data)) throw new Error('checksum.decode: input should be Uint8Array'); | ||
const payload = data.slice(0, -len); | ||
@@ -412,3 +411,5 @@ const newChecksum = fn(payload).slice(0, len); | ||
export const base58check = /* @__PURE__ */ (sha256: (data: Uint8Array) => Uint8Array): BytesCoder => | ||
export const createBase58check = /* @__PURE__ */ ( | ||
sha256: (data: Uint8Array) => Uint8Array | ||
): BytesCoder => | ||
chain( | ||
@@ -418,2 +419,4 @@ checksum(4, (data) => sha256(sha256(data))), | ||
); | ||
// legacy export, bad name | ||
export const base58check = createBase58check; | ||
@@ -565,3 +568,3 @@ // Bech32 code | ||
if (typeof type !== 'string' || !CODERS.hasOwnProperty(type)) throw new TypeError(coderTypeError); | ||
if (!(bytes instanceof Uint8Array)) throw new TypeError('bytesToString() expects Uint8Array'); | ||
if (!isBytes(bytes)) throw new TypeError('bytesToString() expects Uint8Array'); | ||
return CODERS[type].encode(bytes); | ||
@@ -568,0 +571,0 @@ }; |
@@ -10,2 +10,6 @@ /*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
} | ||
function isBytes(a) { | ||
return (a instanceof Uint8Array || | ||
(a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array')); | ||
} | ||
/** | ||
@@ -15,10 +19,9 @@ * @__NO_SIDE_EFFECTS__ | ||
function chain(...args) { | ||
const id = (a) => a; | ||
// Wrap call in closure so JIT can inline calls | ||
const wrap = (a, b) => (c) => a(b(c)); | ||
// Construct chain of args[-1].encode(args[-2].encode([...])) | ||
const encode = Array.from(args) | ||
.reverse() | ||
.reduce((acc, i) => (acc ? wrap(acc, i.encode) : i.encode), undefined); | ||
const encode = args.map((x) => x.encode).reduceRight(wrap, id); | ||
// Construct chain of args[0].decode(args[1].decode(...)) | ||
const decode = args.reduce((acc, i) => (acc ? wrap(acc, i.decode) : i.decode), undefined); | ||
const decode = args.map((x) => x.decode).reduce(wrap, id); | ||
return { encode, decode }; | ||
@@ -223,3 +226,3 @@ } | ||
encode: (bytes) => { | ||
if (!(bytes instanceof Uint8Array)) | ||
if (!isBytes(bytes)) | ||
throw new Error('radix.encode input should be Uint8Array'); | ||
@@ -230,3 +233,3 @@ return convertRadix(Array.from(bytes), 2 ** 8, num); | ||
if (!Array.isArray(digits) || (digits.length && typeof digits[0] !== 'number')) | ||
throw new Error('radix.decode input should be array of strings'); | ||
throw new Error('radix.decode input should be array of numbers'); | ||
return Uint8Array.from(convertRadix(digits, num, 2 ** 8)); | ||
@@ -249,3 +252,3 @@ }, | ||
encode: (bytes) => { | ||
if (!(bytes instanceof Uint8Array)) | ||
if (!isBytes(bytes)) | ||
throw new Error('radix2.encode input should be Uint8Array'); | ||
@@ -256,3 +259,3 @@ return convertRadix2(Array.from(bytes), 8, bits, !revPadding); | ||
if (!Array.isArray(digits) || (digits.length && typeof digits[0] !== 'number')) | ||
throw new Error('radix2.decode input should be array of strings'); | ||
throw new Error('radix2.decode input should be array of numbers'); | ||
return Uint8Array.from(convertRadix2(digits, bits, 8, revPadding)); | ||
@@ -284,3 +287,3 @@ }, | ||
encode(data) { | ||
if (!(data instanceof Uint8Array)) | ||
if (!isBytes(data)) | ||
throw new Error('checksum.encode: input should be Uint8Array'); | ||
@@ -294,3 +297,3 @@ const checksum = fn(data).slice(0, len); | ||
decode(data) { | ||
if (!(data instanceof Uint8Array)) | ||
if (!isBytes(data)) | ||
throw new Error('checksum.decode: input should be Uint8Array'); | ||
@@ -351,3 +354,5 @@ const payload = data.slice(0, -len); | ||
}; | ||
export const base58check = /* @__PURE__ */ (sha256) => chain(checksum(4, (data) => sha256(sha256(data))), base58); | ||
export const createBase58check = /* @__PURE__ */ (sha256) => chain(checksum(4, (data) => sha256(sha256(data))), base58); | ||
// legacy export, bad name | ||
export const base58check = createBase58check; | ||
const BECH_ALPHABET = /* @__PURE__ */ chain(alphabet('qpzry9x8gf2tvdw0s3jn54khce6mua7l'), join('')); | ||
@@ -459,3 +464,3 @@ const POLYMOD_GENERATORS = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]; | ||
throw new TypeError(coderTypeError); | ||
if (!(bytes instanceof Uint8Array)) | ||
if (!isBytes(bytes)) | ||
throw new TypeError('bytesToString() expects Uint8Array'); | ||
@@ -462,0 +467,0 @@ return CODERS[type].encode(bytes); |
@@ -76,2 +76,3 @@ /*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
export declare const base58xmr: BytesCoder; | ||
export declare const createBase58check: (sha256: (data: Uint8Array) => Uint8Array) => BytesCoder; | ||
export declare const base58check: (sha256: (data: Uint8Array) => Uint8Array) => BytesCoder; | ||
@@ -78,0 +79,0 @@ export interface Bech32Decoded<Prefix extends string = string> { |
"use strict"; | ||
/*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.bytes = exports.stringToBytes = exports.str = exports.bytesToString = exports.hex = exports.utf8 = exports.bech32m = exports.bech32 = exports.base58check = exports.base58xmr = exports.base58xrp = exports.base58flickr = exports.base58 = exports.base64urlnopad = exports.base64url = exports.base64 = exports.base32crockford = exports.base32hex = exports.base32 = exports.base16 = exports.utils = exports.assertNumber = void 0; | ||
exports.bytes = exports.stringToBytes = exports.str = exports.bytesToString = exports.hex = exports.utf8 = exports.bech32m = exports.bech32 = exports.base58check = exports.createBase58check = exports.base58xmr = exports.base58xrp = exports.base58flickr = exports.base58 = exports.base64urlnopad = exports.base64url = exports.base64 = exports.base32crockford = exports.base32hex = exports.base32 = exports.base16 = exports.utils = exports.assertNumber = void 0; | ||
// Utilities | ||
@@ -14,2 +14,6 @@ /** | ||
exports.assertNumber = assertNumber; | ||
function isBytes(a) { | ||
return (a instanceof Uint8Array || | ||
(a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array')); | ||
} | ||
/** | ||
@@ -19,10 +23,9 @@ * @__NO_SIDE_EFFECTS__ | ||
function chain(...args) { | ||
const id = (a) => a; | ||
// Wrap call in closure so JIT can inline calls | ||
const wrap = (a, b) => (c) => a(b(c)); | ||
// Construct chain of args[-1].encode(args[-2].encode([...])) | ||
const encode = Array.from(args) | ||
.reverse() | ||
.reduce((acc, i) => (acc ? wrap(acc, i.encode) : i.encode), undefined); | ||
const encode = args.map((x) => x.encode).reduceRight(wrap, id); | ||
// Construct chain of args[0].decode(args[1].decode(...)) | ||
const decode = args.reduce((acc, i) => (acc ? wrap(acc, i.decode) : i.decode), undefined); | ||
const decode = args.map((x) => x.decode).reduce(wrap, id); | ||
return { encode, decode }; | ||
@@ -227,3 +230,3 @@ } | ||
encode: (bytes) => { | ||
if (!(bytes instanceof Uint8Array)) | ||
if (!isBytes(bytes)) | ||
throw new Error('radix.encode input should be Uint8Array'); | ||
@@ -234,3 +237,3 @@ return convertRadix(Array.from(bytes), 2 ** 8, num); | ||
if (!Array.isArray(digits) || (digits.length && typeof digits[0] !== 'number')) | ||
throw new Error('radix.decode input should be array of strings'); | ||
throw new Error('radix.decode input should be array of numbers'); | ||
return Uint8Array.from(convertRadix(digits, num, 2 ** 8)); | ||
@@ -253,3 +256,3 @@ }, | ||
encode: (bytes) => { | ||
if (!(bytes instanceof Uint8Array)) | ||
if (!isBytes(bytes)) | ||
throw new Error('radix2.encode input should be Uint8Array'); | ||
@@ -260,3 +263,3 @@ return convertRadix2(Array.from(bytes), 8, bits, !revPadding); | ||
if (!Array.isArray(digits) || (digits.length && typeof digits[0] !== 'number')) | ||
throw new Error('radix2.decode input should be array of strings'); | ||
throw new Error('radix2.decode input should be array of numbers'); | ||
return Uint8Array.from(convertRadix2(digits, bits, 8, revPadding)); | ||
@@ -288,3 +291,3 @@ }, | ||
encode(data) { | ||
if (!(data instanceof Uint8Array)) | ||
if (!isBytes(data)) | ||
throw new Error('checksum.encode: input should be Uint8Array'); | ||
@@ -298,3 +301,3 @@ const checksum = fn(data).slice(0, len); | ||
decode(data) { | ||
if (!(data instanceof Uint8Array)) | ||
if (!isBytes(data)) | ||
throw new Error('checksum.decode: input should be Uint8Array'); | ||
@@ -355,4 +358,6 @@ const payload = data.slice(0, -len); | ||
}; | ||
const base58check = (sha256) => chain(checksum(4, (data) => sha256(sha256(data))), exports.base58); | ||
exports.base58check = base58check; | ||
const createBase58check = (sha256) => chain(checksum(4, (data) => sha256(sha256(data))), exports.base58); | ||
exports.createBase58check = createBase58check; | ||
// legacy export, bad name | ||
exports.base58check = exports.createBase58check; | ||
const BECH_ALPHABET = /* @__PURE__ */ chain(alphabet('qpzry9x8gf2tvdw0s3jn54khce6mua7l'), join('')); | ||
@@ -464,3 +469,3 @@ const POLYMOD_GENERATORS = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]; | ||
throw new TypeError(coderTypeError); | ||
if (!(bytes instanceof Uint8Array)) | ||
if (!isBytes(bytes)) | ||
throw new TypeError('bytesToString() expects Uint8Array'); | ||
@@ -467,0 +472,0 @@ return CODERS[type].encode(bytes); |
{ | ||
"name": "@scure/base", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "Secure, audited & 0-dep implementation of base64, bech32, base58, base32 & base16", | ||
@@ -41,4 +41,4 @@ "files": [ | ||
"micro-should": "0.4.0", | ||
"prettier": "2.8.4", | ||
"typescript": "5.0.2" | ||
"prettier": "3.1.1", | ||
"typescript": "5.3.2" | ||
}, | ||
@@ -45,0 +45,0 @@ "keywords": [ |
@@ -20,7 +20,8 @@ # scure-base | ||
> **scure** — secure, independently audited packages for every use case. | ||
> **scure** — audited micro-libraries. | ||
- Minimal or zero dependencies | ||
- Releases are signed with PGP keys and built transparently with NPM provenance | ||
- Check out all libraries: | ||
- Zero or minimal dependencies | ||
- Highly readable TypeScript / JS code | ||
- PGP-signed releases and transparent NPM builds | ||
- Check out [homepage](https://paulmillr.com/noble/#scure) & all libraries: | ||
[base](https://github.com/paulmillr/scure-base), | ||
@@ -66,4 +67,4 @@ [bip32](https://github.com/paulmillr/scure-bip32), | ||
```js | ||
import { base58check } from '@scure/base'; | ||
base58check(sha256).encode(data); | ||
import { createBase58check } from '@scure/base'; | ||
createBase58check(sha256).encode(data); | ||
``` | ||
@@ -70,0 +71,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
78206
1598
203