base58

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2020 License: MIT Imports: 5 Imported by: 4

README

Base58 package

Build Status codecov

Introduction

This simple package implements the Base58 encoding/decoding, with and without checksum.

Installation

The package can be installed by simply running:

go get -u github.com/ebellocchia/go-base58

Usage

First of all, a base58 object shall be created using the New function, by specifying the alphabet to be used.
The possible alphabets are:

  • Bitcoin: base58.AlphabetBitcoin
  • Ripple: base58.AlphabetRipple
  • Flickr: base58.AlphabetFlickr

If the object is created without using the New function, the Bitcoin alphabet will be used by default.
There are 4 APIs that can be used:

  • Encode([]byte) string: encode bytes into string
  • CheckEncode([]byte) string: encode bytes into string with checksum
  • Decode(string) ([]byte, error): decode string back into bytes, return error if format is not valid
  • CheckDecode(string) ([]byte, error): decode string with checksum back into bytes, return error if format or checksum is not valid

Example

package main

import (
  "github.com/ebellocchia/go-base58"
  "fmt"
  "encoding/hex"
)

func main() {
    // Create base58 object using Bitcoin alphabet
    base58Btc := base58.New(base58.AlphabetBitcoin)

    // Some bytes to encode
    data_bytes, _ := hex.DecodeString("bf4f89001e670274dd")

    // Encode
    // It returns an empty string if the alphabet is not valid
    enc := base58Btc.Encode(data_bytes)
    fmt.Printf("Encode: %s\n", enc)
    // CheckEncode
    // It returns an empty string if the alphabet is not valid
    check_enc := base58Btc.CheckEncode(data_bytes)
    fmt.Printf("Checksum encode: %s\n", check_enc)

    // Decode
    // It returns error if the alphabet or the string format is not valid
    dec, err := base58Btc.Decode(enc)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Decode: %s\n", hex.EncodeToString(dec))
    // CheckDecode
    // It returns error if the alphabet, the string format or the checksum is not valid
    check_dec, err := base58Btc.CheckDecode(check_enc)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Checksum decode: %s\n", hex.EncodeToString(check_dec))
}

License

This software is available under the MIT license.

Documentation

Index

Constants

View Source
const (
	// Supported alphabets
	AlphabetBitcoin = 0
	AlphabetRipple  = 1
	AlphabetFlickr  = 2
)

Constants

Variables

View Source
var (
	// ErrInvalidFormat is returned when trying to decode a string in invalid format
	ErrInvalidFormat = errors.New("The specified string is not a valid Base58 format")
	// ErrInvalidChecksum is returned when trying to decode a string with invalid checksum
	ErrInvalidChecksum = errors.New("The checksum of the specified string is not valid")
)

Variables

View Source
var (

	// ErrInvalidAlphabet is returned when trying to get a not-existent alphabet
	ErrInvalidAlphabet = errors.New("The specified alphabet is not existent")
)

Variables

Functions

This section is empty.

Types

type Base58Obj

type Base58Obj struct {
	AlphIdx int
}

Base58 structure. It basically holds the alphabet index to be used. The default value (0) is the Bitcoin alphabet.

func New

func New(alphIdx int) *Base58Obj

Helper function for creating Base58Obj structuer from the alphabet index.

func (*Base58Obj) CheckDecode

func (obj *Base58Obj) CheckDecode(input string) ([]byte, error)

Decode the specified string in Base58 format to bytes, by removing and verifying the checksum.

func (*Base58Obj) CheckEncode

func (obj *Base58Obj) CheckEncode(input []byte) string

Encode the specified bytes to Base58 format, by adding the checksum.

func (*Base58Obj) Decode

func (obj *Base58Obj) Decode(input string) ([]byte, error)

Decode the specified string in Base58 format to bytes.

func (*Base58Obj) Encode

func (obj *Base58Obj) Encode(input []byte) string

Encode the specified bytes to Base58 format

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL