Gathering detailed insights and metrics for @scure/base
Gathering detailed insights and metrics for @scure/base
Gathering detailed insights and metrics for @scure/base
Gathering detailed insights and metrics for @scure/base
Secure, audited & 0-deps implementation of bech32, base64, base32, base16 & base58
npm install @scure/base
99.9
Supply Chain
100
Quality
89.3
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
120 Stars
157 Commits
13 Forks
4 Watching
3 Branches
10 Contributors
Updated on 23 Nov 2024
Minified
Minified + Gzipped
JavaScript (69.01%)
TypeScript (26.13%)
Rust (4.86%)
Cumulative downloads
Total Downloads
Last day
-3.4%
208,786
Compared to previous day
Last week
13.3%
1,303,326
Compared to previous week
Last month
2.4%
5,020,156
Compared to previous month
Last year
112.4%
44,638,699
Compared to previous year
5
Audited & minimal implementation of bech32, base64, base58, base32 & base16.
Check out Projects using scure-base.
scure â audited micro-libraries.
npm install @scure/base
deno add @scure/base
We support all major platforms and runtimes. The library is hybrid ESM / Common.js package.
1import { base16, base32, base64, base58 } from '@scure/base'; 2// Flavors 3import { 4 base58xmr, 5 base58xrp, 6 base32nopad, 7 base32hex, 8 base32hexnopad, 9 base32crockford, 10 base64nopad, 11 base64url, 12 base64urlnopad, 13} from '@scure/base'; 14 15const data = Uint8Array.from([1, 2, 3]); 16base64.decode(base64.encode(data)); 17 18// Convert utf8 string to Uint8Array 19const data2 = new TextEncoder().encode('hello'); 20base58.encode(data2); 21 22// Everything has the same API except for bech32 and base58check 23base32.encode(data); 24base16.encode(data); 25base32hex.encode(data);
base58check is a special case: you need to pass sha256()
function:
1import { createBase58check } from '@scure/base'; 2createBase58check(sha256).encode(data);
Alternative API:
1import { str, bytes } from '@scure/base'; 2const encoded = str('base64', data); 3const data = bytes('base64', encoded);
We provide low-level bech32 operations. If you need high-level methods for BTC (addresses, and others), use scure-btc-signer instead.
Bitcoin addresses use both 5-bit words and bytes representations.
They can't be parsed using bech32.decodeToBytes
. Instead, do something this:
1const decoded = bech32.decode(address); 2// NOTE: words in bitcoin addresses contain version as first element, 3// with actual witness program words in rest 4// BIP-141: The value of the first push is called the "version byte". 5// The following byte vector pushed is called the "witness program". 6const [version, ...dataW] = decoded.words; 7const program = bech32.fromWords(dataW); // actual witness program
Same applies to Lightning Invoice Protocol
BOLT-11.
We have many tests in ./test/bip173.test.js
that serve as minimal examples of
Bitcoin address and Lightning Invoice Protocol parsers.
Keep in mind that you'll need to verify the examples before using them in your code.
The code may feel unnecessarily complicated; but actually it's much easier to reason about. Any encoding library consists of two functions:
encode(A) -> B
decode(B) -> A
where X = decode(encode(X))
# encode(decode(X)) can be !== X!
# because decoding can normalize input
e.g.
base58checksum = {
encode(): {
// checksum
// radix conversion
// alphabet
},
decode(): {
// alphabet
// radix conversion
// checksum
}
}
But instead of creating two big functions for each specific case, we create them from tiny composable building blocks:
base58checksum = chain(checksum(), radix(), alphabet())
Which is the same as chain/pipe/sequence function in Functional Programming, but significantly more useful since it enforces same order of execution of encode/decode. Basically you only define encode (in declarative way) and get correct decode for free. So, instead of reasoning about two big functions you need only reason about primitives and encode chain. The design revealed obvious bug in older version of the lib, where xmr version of base58 had errors in decode's block processing.
Besides base-encodings, we can reuse the same approach with any encode/decode function
(bytes2number
, bytes2u32
, etc).
For example, you can easily encode entropy to mnemonic (BIP-39):
1export function getCoder(wordlist: string[]) {
2 if (!Array.isArray(wordlist) || wordlist.length !== 2 ** 11 || typeof wordlist[0] !== 'string') {
3 throw new Error('Wordlist: expected array of 2048 strings');
4 }
5 return mbc.chain(mbu.checksum(1, checksum), mbu.radix2(11, true), mbu.alphabet(wordlist));
6}
Uint8Array
is represented as big-endian number:
[1, 2, 3, 4, 5] -> 1*(256**4) + 2*(256**3) 3*(256**2) + 4*(256**1) + 5*(256**0)
where 256 = 2**8 (8 bits per byte)
which is then converted to a number in another radix/base (16/32/58/64, etc).
However, generic conversion between bases has quadratic O(n^2) time complexity.
Which means base58 has quadratic time complexity too. Use base58 only when you have small constant sized input, because variable length sized input from user can cause DoS.
On the other hand, if both bases are power of same number (like 2**8 <-> 2**64
),
there is linear algorithm. For now we have implementation for power-of-two bases only (radix2).
The library has been independently audited:
The library was initially developed for js-ethereum-cryptography.
At commit ae00e6d7,
it was extracted to a separate package called micro-base
.
After the audit we've decided to use @scure
NPM namespace for security.
MIT (c) Paul Miller (https://paulmillr.com), see LICENSE file.
No vulnerabilities found.
No security vulnerabilities found.