scure-base
Audited & minimal implementation of bech32, base64, base58, base32 & base16.
🔒 Audited by an independent security firm
🔻 Tree-shakeable: unused code is excluded from your builds
📦 ESM and common.js
✍️ Written in functional style, easily composable
💼 Matches specs
RFC 4648 (aka RFC 3548) for Base16, Base32, Base32Hex, Base64, Base64Url
🪶 4KB gzipped
Check out Projects using scure-base.
This library belongs to scure
scure — audited micro-libraries.
Zero or minimal dependencies
Highly readable TypeScript / JS code
PGP-signed releases and transparent NPM builds
Usage
npm install @scure/base
deno add jsr:@scure/base
deno doc jsr:@scure/base# command-line documentation
We support all major platforms and runtimes. The library is hybrid ESM / Common.js package.
base58check is a special case: you need to pass sha256() function:
Bech32, Bech32m and Bitcoin
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.
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.
Do something like this:
Design rationale
The code may feel unnecessarily complicated; but actually it's much easier to reason about. Any encoding library consists of two functions:
But instead of creating two big functions for each specific case, we create them from tiny composable building blocks:
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):
base58 is O(n^2) and radixes
Uint8Array is represented as big-endian number:
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).
Security
The library has been independently audited:
at version 1.0.0, in Jan 2022, by cure53
The audit has been funded by Ethereum Foundation with help of Nomic Labs
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.
Supply chain security
Commits are signed with PGP keys, to prevent forgery. Make sure to verify commit signatures
Releases are transparent and built on GitHub CI. Make sure to verify provenance logs
Use GitHub CLI to verify single-file builds:
gh attestation verify --owner paulmillr scure-base.js
Rare releasing is followed to ensure less re-audit need for end-users
Dependencies are minimized and locked-down: any dependency could get hacked and users will be downloading malware with every install.
We make sure to use as few dependencies as possible
Automatic dep updates are prevented by locking-down version ranges; diffs are checked with
npm-diff
Dev Dependencies are disabled for end-users; they are only used to develop / build the source code
For this package, there are 0 dependencies; and a few dev dependencies:
micro-bmark, micro-should and jsbt are used for benchmarking / testing / build tooling and developed by the same author
prettier, fast-check and typescript are used for code quality / test generation / ts compilation. It's hard to audit their source code thoroughly and fully because of their size
Contributing & testing
npm install && npm run build && npm testwill build the code and run tests.npm run lint/npm run formatwill run linter / fix linter issues.npm run build:releasewill build single file
Projects using scure-base
License
MIT (c) Paul Miller (https://paulmillr.com), see LICENSE file.
Last updated