npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@scure/base

v1.1.6

Published

Secure, audited & 0-dep implementation of base64, bech32, base58, base32 & base16

Downloads

3,488,337

Readme

scure-base

Audited & minimal implementation of bech32, base64, base58, base32 & base16.

Check out Projects using scure-base.

This library belongs to scure

scure — audited micro-libraries.

Usage

npm install @scure/base

We support all major platforms and runtimes. The library is hybrid ESM / Common.js package.

import { base16, base32, base64, base58 } from '@scure/base';
// Flavors
import {
  base58xmr,
  base58xrp,
  base32hex,
  base32crockford,
  base64nopad,
  base64url,
  base64urlnopad,
} from '@scure/base';

const data = Uint8Array.from([1, 2, 3]);
base64.decode(base64.encode(data));

// Convert utf8 string to Uint8Array
const data2 = new TextEncoder().encode('hello');
base58.encode(data2);

// Everything has the same API except for bech32 and base58check
base32.encode(data);
base16.encode(data);
base32hex.encode(data);

base58check is a special case: you need to pass sha256() function:

import { createBase58check } from '@scure/base';
createBase58check(sha256).encode(data);

Alternative API:

import { str, bytes } from '@scure/base';
const encoded = str('base64', data);
const data = bytes('base64', encoded);

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. Instead, do something this:

const decoded = bech32.decode(address);
// NOTE: words in bitcoin addresses contain version as first element,
// with actual witnes program words in rest
// BIP-141: The value of the first push is called the "version byte".
// The following byte vector pushed is called the "witness program".
const [version, ...dataW] = decoded.words;
const 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.

Design rationale

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 composamble 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):

export function getCoder(wordlist: string[]) {
  if (!Array.isArray(wordlist) || wordlist.length !== 2 ** 11 || typeof wordlist[0] !== 'string') {
    throw new Error('Worlist: expected array of 2048 strings');
  }
  return mbc.chain(mbu.checksum(1, checksum), mbu.radix2(11, true), mbu.alphabet(wordlist));
}

base58 is O(n^2) and radixes

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).

Security

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.

Resources

Projects using scure-base

License

MIT (c) Paul Miller (https://paulmillr.com), see LICENSE file.