JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 33478
  • Score
    100M100P100Q147905F
  • License Apache-2.0

Bech32 encoding for byte buffers

Package Exports

  • bech32-buffer

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (bech32-buffer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Bech32 Encoding for Arbitrary Buffers

Build status Code coverage Code style

Bech32 is a new proposed Bitcoin address format specified in BIP 173. Among its advantages are: better adaptability to QR codes and in voice conversations, and improved error detection. This library generalizes Bech32 to encode any (reasonably short) byte buffers.

Usage

Encoding data

function encode(prefix, data)

Encodes binary data with the specified human-readable prefix into a Bech32 string.

Arguments

  • prefix: string
    Human-readable prefix to hint what kind of data Bech32 encodes. Must contain ASCII chars in the range 33-126
  • data: Uint8Array
    Binary data to encode

Return value

String containing:

  1. prefix
  2. '1' separator char
  3. data encoded with the variant of base32 encoding used by Bech32, and
  4. 6-char checksum calculated based on prefix and data

Example

var bech32 = require('bech32-buffer');
var data = new Uint8Array(20);
var encoded = bech32.encode('test', data);
// 'test1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqql6aptf'

Decoding data

function decode(data)

Extracts human-readable prefix and binary data from the Bech32-encoded string.

Arguments

  • data: string
    String to decode

Return value

An object with the following fields:

  • prefix: string
    Human-readable prefix
  • data: Uint8Array
    Binary data encoded into the input string

The encoding may fail for a variety of reasons (e.g., invalid checksum, or Invalid chars in the input). In this case, decode() throws an exception with a descriptive message.

Example

var bech32 = require('bech32-buffer');
var data = 'lost1qsyq7yqh9gk0szs5';
var decoded = bech32.decode(data);
// {
//   prefix: 'lost',
//   data: Uint8Array([ 4, 8, 15, 16, 23, 42 ])
// }

Acknowledgements

BIP 173 is authored by Pieter Wuille and Greg Maxwell and is licensed under the 2-clause BSD license.

There are at least 2 existing implementations of Bech32 for JavaScript:

Both implementations are Bitcoin-specific, and the reference implementation is also not in the Npm / yarn package manager.

License

(c) 2017 Alex Ostrovski

bech32-buffer is available under Apache-2.0 license.