Base58
Base58
No 0, O, I or l. With check, the Bitcoin address you know.
- Overhead
- +37% on random bytes
- Checksum
- with check
- Standard
- Bitcoin Core base58.cpp, Monero common/base58.cpp, Flickr short URLs, XRP Ledger addresses
- Used by
- Bitcoin, Solana, IPFS CIDv0, XRP Ledger
Sample
- Out
- 2yGEbwRFyhPZZckKA
- Back
- "hello world!"
Options
Access
- Import
import { base58 } from "@agntn/encodings/base58" - CLI
encodings encode base58 'hello world!' - Tryplayground with the sample above
Satoshi dropped 0, O, I and l from base62 and got 58 characters nobody can misread. The catch? Base58 doesn't work in blocks. The whole input is one big number, divided by 58 over and over.
encode("base58", "hello world"); // "StV1DL6CwTryKyV"
base58.encode(hex.decode("0000287fb4cd")); // "11233QC4"
Leading zero bytes don't survive arithmetic, so each one becomes a leading 1. That's why every Bitcoin P2PKH address starts with 1. Version byte zero.
It's quadratic, by the way. Fine for addresses and keys, silly for a megabyte. Solana keys and IPFS CIDv0 hashes are plain base58. Bitcoin adds a checksum.
Base58Check
Base58 with a seatbelt. The payload, then the first four bytes of its double SHA-256. check: true writes it and checks it.
hex.encode(decode("base58", "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", { check: true }).bytes);
// "0062e907b15cbf27d5425399ebf6f0fb50ebb88f18"
The genesis address: version 0x00, then the HASH160 of Satoshi's key. Change one character and decode throws ChecksumError. The double SHA-256 comes from @agntn/hashes, the one dependency this package has.
The version stays in the payload. Is 0x80 a WIF key and 0x05 a P2SH address? Sure, but that's network knowledge, and it lives in @agntn/keys. check verifies the checksum and hands you the bytes.
The flickr alphabet
Same 58 characters, different order. Flickr put lowercase before uppercase for its flic.kr short links, and that one swap makes every string look different.
encode("base58", "hello world"); // "StV1DL6CwTryKyV"
encode("base58", "hello world", { alphabet: "flickr" }); // "rTu1dk6cWsRYjYu"
Notice the cases flipped and the digits stayed? That's the whole difference. Decode a Flickr string with the Bitcoin alphabet and you get valid, wrong bytes. identify tries both and shows both. Know your source.
The ripple alphabet
The XRP Ledger shuffled the alphabet so that r is the zero digit. Version byte zero, address starts with r. Cute.
const xrp = { alphabet: "ripple", check: true } as const;
hex.encode(decode("base58", "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", xrp).bytes);
// "00b5f762798a53d543a014caf8b297cff8f2f937e8"
decode("base58", "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTi", xrp);
// throws "base58: checksum does not match"
That's the ledger's genesis account, then the same account with its last character bumped. Version 0x00, then a 20-byte account ID. The checksum is Bitcoin's scheme, so check reads it in this alphabet too. Leave check out and a typo decodes without complaint.
Monero's blocks
Remember the catch up top? Monero fixed it the blunt way. Cut the bytes into blocks of eight, write each one as exactly eleven characters, pad with 1. A short last block gets a fixed width too.
Every block is its own small number, so nothing goes quadratic. And the length of the text follows from the byte count alone. A 69-byte address is 95 characters, always.
const fund = "44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A";
decode("base58", fund, { blocks: true }).bytes.length; // 69
decode("base58", fund).bytes.length; // 70
That's the Monero General Fund. Network byte 0x12, a spend key, a view key, four bytes of Keccak. Read it without blocks and you get 70 bytes of confident nonsense. Same alphabet as Bitcoin, completely different numbers.
The checksum isn't check's double SHA-256, so blocks leaves it in the bytes, the way check leaves the version. Eleven digits also hold a bit more than 64 bits. So jpXCZedGfVR, one past eight ff bytes, has nowhere to go, and decode throws. A last block of 1, 4 or 8 characters throws too. No byte count writes one.
Another hash
Decred keeps the format and ends its addresses with double BLAKE-256. So check turns down every one of them. Correctly, in a way. createBase58check takes the hash and builds the codec:
import { blake256 } from "@agntn/hashes/blake256";
import { createBase58check } from "@agntn/encodings/base58";
const decred = createBase58check(blake256);
hex.encode(decred.decode("DsUZxxoHJSty8DCfwfartwTYbuhmVct7tJu"));
// "073f2789d58cfa0957d206f025c2af056fc8a77cebb0"
Pass the hash once. The codec runs it twice and keeps four bytes, like Bitcoin does with SHA-256. And the version is two bytes here, 073f. A second argument takes an alphabet of 58 distinct characters. Anything else is InvalidOptionError, right at the call. The hash stays your import. This package doesn't grow a BLAKE-256 of its own.
Just the digits
base58 takes check, and check needs SHA-256. Your bundler can't know you'll never pass it. So a Solana key decoder ships a hash it never calls, and that hash is more than half the bundle. Want it out? createBase58 gives you one alphabet's plain codec. No options, no hash:
import { createBase58 } from "@agntn/encodings/base58";
createBase58("ripple").encode(hex.decode("0000287fb4cd")); // "rrpssQUh"
hex.encode(createBase58().decode("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"));
// "0062e907b15cbf27d5425399ebf6f0fb50ebb88f18c29b7d93"
Same digits as base58, same errors. It just can't check anything. So the genesis address comes back with its four checksum bytes still attached. Fair trade.
Both createBase58 and createBase58check come from @agntn/encodings/base58 only.