@anzerr/base.util

base encoding / decoding of any given alphabet

Usage no npm install needed!

<script type="module">
  import anzerrBaseUtil from 'https://cdn.skypack.dev/@anzerr/base.util';
</script>

README

Intro

GitHub Actions status | linter GitHub Actions status | publish GitHub Actions status | test

base encoding / decoding of any given alphabet for bitcoin style leading zero compression and rfc4648

Install

npm install --save git+https://github.com/anzerr/base.util.git
npm install --save @anzerr/base.util

Example

const {bc58, b32, Compress, Base} = require('base.util');

const address = '003c176e659bea0f29a3e9bf7880c112b1b31b4dc826268187';

console.log(bc58.encode(Buffer.from(address, 'hex'), true)) // 16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS

console.log(b32.encode(Buffer.from('foobar'), true)); // MZXW6YTBOI======
console.log(b32.encode(Buffer.from('foobar'))); // <Buffer 0c 19 17 16 1e 18 13 01 0e 08>

const base32 = (data, toString = true) => {
    let handle = new Base('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', 40); // alphabet, pad size
    return handle.encode(Buffer.from(data), toString);
}

console.log(base32('fooba')) // MZXW6YTB

let bitcoin58 = new Compress('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz');

console.log(bc58.encode(Buffer.from(address, 'hex'), true)) // 16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS