• Stars
    star
    312
  • Rank 134,101 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Encode/decode any base

base-x

NPM Package Build Status

js-standard-style

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('base-x')(BASE58)

var decoded = bs58.decode('5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr')

console.log(decoded)
// => Uint8Array(33) [
//   128, 237, 219, 220,  17, 104, 241, 218,
//   234, 219, 211, 228,  76,  30,  63, 143,
//    90,  40,  76,  32,  41, 247, 138, 210,
//   106, 249, 133, 131, 164, 153, 222,  91,
//    25
// ]

console.log(bs58.encode(decoded))
// => 5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr

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+/
67 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.

More Repositories

1

secp256k1-node

Node.js binding for an Optimized C library for EC operations on curve secp256k1
JavaScript
341
star
2

bs58

Base58 encoding/decoding for Bitcoin
JavaScript
212
star
3

hdkey

JavaScript component for Bitcoin hierarchical deterministic keys (BIP32)
JavaScript
201
star
4

coinkey

JavaScript component for private keys, public keys, and addresess for crypto currencies such as Bitcoin, Litecoin, and Dogecoin
JavaScript
149
star
5

cryptocoin

JavaScript library for crypto currencies like Bitcoin and Litecoin.
JavaScript
140
star
6

coininfo

JavaScript component for crypto currency specific information.
JavaScript
116
star
7

keccak

Keccak sponge function family
C
86
star
8

p2p-node

Manage peer-to-peer communications for cryptocurrency implementations
JavaScript
61
star
9

ecurve

JavaScript component for Eliptic Curve Cryptography
JavaScript
58
star
10

awesome-cryptocoinjs

Useful crypto coins JavaScript libraries
58
star
11

bigi

JavaScript Big Integer library based upon Tom Wu's work.
JavaScript
49
star
12

sha256

DEPRECATED - JavaScript component to compute the SHA256 of strings or bytes.
JavaScript
47
star
13

scryptsy

Scrypt KDF is used for BIP38 (encryption of private keys) and proof of work for some crypto currencies.
JavaScript
45
star
14

stealth

Stealth addresses for Bitcoin and other crypto currencies.
JavaScript
42
star
15

ecdsa

DEPRECATED - JavaScript component to Eliptical Curve Cryptography signing and verify.
JavaScript
38
star
16

qr-encode

JavaScript component to encode strings into QR codes.
JavaScript
25
star
17

pbkdf2-sha256

DEPRECATED - Key derivation function primarily used for Scrypt
JavaScript
22
star
18

aes

A JavaScript component for the Advanced Encryption Standard (AES). Important for BIP38
JavaScript
21
star
19

eckey

A JavaScript component for Eliptical curve cryptography for crypto currencies such as Litecoin and Bitcoin
JavaScript
17
star
20

coinstring

DEPRECATED - Create and parse crypto currency addresses and wallet import formats.
JavaScript
16
star
21

scrypt

Scrypt for node and browser
C
14
star
22

binstring

DEPRECATED - Convert binary data to and from various string/integer representations
JavaScript
12
star
23

blake-hash

SHA-3 proposal Blake
C
12
star
24

btc-transaction

Create, parse, serialize Bitcoin transactions.
JavaScript
11
star
25

btc-address

JavaScript component to handle Bitcoin addresses.
JavaScript
10
star
26

crypto-hashing

JavaScript hashing libraries wrapped up into one module. Compatible with Node and the browser.
JavaScript
10
star
27

sha512

DEPRECATED - SHA 512 secure hashing algorithm
JavaScript
9
star
28

cryptocoinjs.com

CryptoCoinJS Docs
HTML
9
star
29

drbg.js

Deterministic Random Bit Generators from NIST SP 800-90A
JavaScript
8
star
30

btc-p2p

Bitcoin peer network manager
JavaScript
8
star
31

btc-blockchain

Bitcoin blockchain management
JavaScript
7
star
32

btc-script

Script support for Bitcoin
JavaScript
7
star
33

eip55

An EIP55 compatible address encoding library
JavaScript
7
star
34

coinmsg

Message signing for Bitcoin and other cryptocurrencies.
JavaScript
7
star
35

btc-scriptinterpreter

JavaScript
6
star
36

crypto-binary

Assemble and disassemble binary messages used in cryptocoin applications
JavaScript
5
star
37

p2p-manager

Manage peer connections to a P2P network
JavaScript
5
star
38

btc-opcode

Opcode support for Bitcoin.
JavaScript
5
star