What is @multiformats/base-x?
@multiformats/base-x is a JavaScript library for encoding and decoding data using custom base alphabets. It is commonly used for encoding binary data into a textual representation that is more human-readable and easier to handle in text-based systems.
What are @multiformats/base-x's main functionalities?
Custom Base Encoding
This feature allows you to encode data using a custom base alphabet. In this example, the data 'hello world' is encoded using the Base58 alphabet.
const baseX = require('@multiformats/base-x');
const BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
const base58 = baseX(BASE58);
const encoded = base58.encode(Buffer.from('hello world'));
console.log(encoded); // prints 'StV1DL6CwTryKyV'
Custom Base Decoding
This feature allows you to decode data that was encoded using a custom base alphabet. In this example, the encoded string 'StV1DL6CwTryKyV' is decoded back to 'hello world'.
const baseX = require('@multiformats/base-x');
const BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
const base58 = baseX(BASE58);
const decoded = base58.decode('StV1DL6CwTryKyV');
console.log(decoded.toString()); // prints 'hello world'
Other packages similar to @multiformats/base-x
base-x
The 'base-x' package is a similar library that provides encoding and decoding functionalities for custom base alphabets. It is a lightweight and straightforward library, making it a good alternative to @multiformats/base-x.
bs58
The 'bs58' package is specifically designed for Base58 encoding and decoding. It is optimized for performance and is widely used in cryptocurrency applications. Unlike @multiformats/base-x, it does not support custom base alphabets.
base64-js
The 'base64-js' package provides Base64 encoding and decoding functionalities. It is a simple and efficient library for handling Base64 operations but does not support custom base alphabets like @multiformats/base-x.
base-x
(Hopefully temporary) fork of base-x library which removed node Buffer dependecy.
Fast base encoding / decoding of any given alphabet using bitcoin style leading
zero compression.
WARNING: This module is NOT RFC3548 compliant, it cannot be used for base16 (hex), base32, or base64 encoding in a standards compliant manner.
Example
Base58
var BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
var bs58 = require('@multiformats/base-x')(BASE58)
var decoded = bs58.decode('5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr')
console.log(decoded)
console.log(bs58.encode(decoded))
Alphabets
See below for a list of commonly recognized alphabets, and their respective base.
Base | Alphabet |
---|
2 | 01 |
8 | 01234567 |
11 | 0123456789a |
16 | 0123456789abcdef |
32 | 0123456789ABCDEFGHJKMNPQRSTVWXYZ |
32 | ybndrfg8ejkmcpqxot1uwisza345h769 (z-base-32) |
36 | 0123456789abcdefghijklmnopqrstuvwxyz |
58 | 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz |
62 | 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ |
64 | ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ |
66 | ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.!~ |
How it works
It encodes octet arrays by doing long divisions on all significant digits in the
array, creating a representation of that number in the new base. Then for every
leading zero in the input (not significant as a number) it will encode as a
single leader character. This is the first in the alphabet and will decode as 8
bits. The other characters depend upon the base. For example, a base58 alphabet
packs roughly 5.858 bits per character.
This means the encoded string 000f (using a base16, 0-f alphabet) will actually decode
to 4 bytes unlike a canonical hex encoding which uniformly packs 4 bits into each
character.
While unusual, this does mean that no padding is required and it works for bases
like 43.
LICENSE MIT
A direct derivation of the base58 implementation from bitcoin/bitcoin
, generalized for variable length alphabets.