Code Monkey home page Code Monkey logo

libbech32's Introduction

libbech32

This is a C++ implementation of "Bech32:" a checksummed base32 data encoding format. It is primarily used as a new bitcoin address format specified by BIP 0173.

Building libbech32

To build libbech32, you will need:

  • cmake
  • g++, clang or Visual Studio (community edition)

libbech32 uses a pretty standard cmake build system:

mkdir build
cd build
cmake ..
make

You can also run all the tests:

make test

Installing prerequisites

If the above doesn't work, you probably need to install some prerequisites. For example, on a fresh Debian 10 ("buster") system:

sudo apt-get update
sudo apt-get install make gcc g++

It is worth getting the latest cmake (check cmake.org for the latest release), so install that the hard way:

sudo apt-get install libssl-dev # recent cmake needs ssl
wget https://github.com/Kitware/CMake/releases/download/v3.20.1/cmake-3.20.1.tar.gz
wget https://github.com/Kitware/CMake/releases/download/v3.20.1/cmake-3.20.1-SHA-256.txt
shasum -a 256 --ignore-missing -c cmake-3.20.1-SHA-256.txt # make sure this says "OK"
tar xzf cmake-3.20.1.tar.gz
cd cmake-3.20.1
./configure
make 
sudo make install

Now you can again try to build libbech32.

Usage Examples

C++ Encoding Example

#include "libbech32.h"
#include <iostream>

int main() {
    // simple human readable part with some data
    std::string hrp = "hello";
    std::vector<unsigned char> data = {14, 15, 3, 31, 13};

    // encode
    std::string bstr = bech32::encode(hrp, data);

    std::cout << bstr << std::endl;
    // prints "hello1w0rldjn365x"
    // ... "hello" + Bech32.separator ("1") + encoded data ("w0rld") + 6 char checksum ("jn365x")
}

C++ Decoding Example

#include "libbech32.h"

int main() {
    bech32::DecodedResult decodedResult = bech32::decode("hello1w0rldjn365x");

    // decodedResult.hrp == "hello"
    // decodedResult.dp[0] == 14
    // decodedResult.encoding == bech32::Encoding::Bech32m
}

For more C++ examples, see examples/cpp_other_examples.cpp

C Encoding Example

#include "libbech32.h"
#include <string.h>
#include <stdio.h>

int main() {
    // simple human readable part with some data
    char hrp[] = "hello";
    unsigned char dp[] = {14, 15, 3, 31, 13};

    // create storage for bech32 string
    bech32_bstring *bstring = bech32_create_bstring(strlen(hrp), sizeof(dp));
    if(!bstring) {
        printf("bech32 string can not be created");
        return E_BECH32_NO_MEMORY;
    }

    // encode
    bech32_error err = bech32_encode(bstring, hrp, dp, sizeof(dp));
    if(err != E_BECH32_SUCCESS) {
        printf("%s\n", bech32_strerror(err));
        bech32_free_bstring(bstring);
        return err;
    }

    printf("bech32 encoding of human-readable part \'hello\' and data part \'[14, 15, 3, 31, 13]\' is:\n");
    printf("%s\n", bstring->string);
    // prints "hello1w0rldjn365x"
    // ... "hello" + Bech32.separator ("1") + encoded data ("w0rld") + 6 char checksum ("jn365x")

    // free memory
    bech32_free_bstring(bstring);

    return E_BECH32_SUCCESS;
}

C Decoding Example

#include "libbech32.h"
#include <string.h>
#include <stdio.h>

int main() {

    char str[] = "hello1w0rldjn365x";

    // create storage for decoded bech32 data
    bech32_DecodedResult * decodedResult = bech32_create_DecodedResult(str);
    if(!decodedResult) {
        printf("bech32 DecodedResult can not be created");
        return E_BECH32_NO_MEMORY;
    }

    // decode
    bech32_error err = bech32_decode(decodedResult, str);
    if(err != E_BECH32_SUCCESS) {
        printf("%s\n", bech32_strerror(err));
        bech32_free_DecodedResult(decodedResult);
        return err;
    }

    // decodedResult->hrp == "hello"
    // decodedResult->dp[0] == 14
    // decodedResult->encoding == ENCODING_BECH32M

    // free memory
    bech32_free_DecodedResult(decodedResult);
}

For more C examples, see examples/c_other_examples.c

Regarding bech32 checksums

The Bech32 data encoding format was first proposed by Pieter Wuille in early 2017 in BIP 0173. Later, in November 2019, Pieter published some research that a constant used in the bech32 checksum algorithm (value = 1) may not be optimal for the error detecting properties of bech32. In February 2021, Pieter published BIP 0350 reporting that "exhaustive analysis" showed the best possible constant value is 0x2bc830a3. This improved variant of Bech32 is called "Bech32m".

When decoding a possible bech32 encoded string, libbech32 returns an enum value showing whether bech32m or bech32 was used to encode. This can be seen in the examples above.

When encoding data, libbech32 defaults to using the new constant value of 0x2bc830a3. If the original constant value of 1 is desired, then the following functions may be used:

C++ Usage Example

    /// ... as above ...

    // encode
    std::string bstr = bech32::encodeUsingOriginalConstant(hrp, data);

    /// ... as above ...

C Usage Example

    /// ... as above ...

    // encode
    bech32_error err = bech32_encode_using_original_constant(bstring, hrp, dp, sizeof(dp));

    /// ... as above ...

libbech32's People

Contributors

danpape avatar dcdpr avatar rxgrant avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.