coincodec

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2020 License: Apache-2.0 Imports: 17 Imported by: 0

README

ens-coincodec

Tag License GoDoc CI codecov.io Go Report

Go utility library to provide movement between string and binary representation of multpile different cryptocurrency coin formats, mainly for ENS, please checkout EIP-2304 for details.

Table of Contents

Coins

Install

ens-coincodec is a standard Go module which can be installed with:

go get github.com/trustwallet/ens-coincodec

Usage

Example
import (
	"fmt"

	cc "github.com/trustwallet/ens-coincodec"
	slip44 "github.com/wealdtech/go-slip44"
)

func main() {
	// Ethereum
	bytes, err := cc.ToBytes("0x314159265dD8dbb310642f98f50C066173C1259b", slip44.ETHER)
	// hex: 314159265dd8dbb310642f98f50c066173c1259b
	if err != nil {
		panic(err)
	}
	str, err := cc.ToString(bytes, slip44.ETHER)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Ethereum: %s\n", str)

	// Bitcoin
	bytes, err = cc.ToBytes("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", slip44.BITCOIN)
	// script hash: 0014751e76e8199196d454941c45d1b3a323f1433bd6
	if err != nil {
		panic(err)
	}
	str, err = cc.ToString(bytes, slip44.BITCOIN)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Bitcoin: %s\n", str)

	// BNB
	bytes, err = cc.ToBytes("bnb1grpf0955h0ykzq3ar5nmum7y6gdfl6lxfn46h2", slip44.BINANCE)
	// public key hash: 40c2979694bbc961023d1d27be6fc4d21a9febe6
	if err != nil {
		panic(err)
	}
	str, err = cc.ToString(bytes, slip44.BINANCE)
	if err != nil {
		panic(err)
	}

	fmt.Printf("BNB: %s\n", str)
}

Contribute

Contributions welcome. Please check out the issues.

If you are adding a new coin type please try to follow the following rules:

  • use the existing ethereum.go and ethereum_test.go as templates
  • ensure you have 100% code coverage with your tests
  • try not to import large amounts of code; consider copying the relevant code rather than bringing in an entire project to use the address conversion functions

License

Apache-2.0 © 2019 Weald Technology Trading Ltd / Trust Wallet

Documentation

Index

Constants

View Source
const (
	OP_DUP         = 0x76
	OP_HASH160     = 0xa9
	OP_EQUALVERIFY = 0x88
	OP_CHECKSIG    = 0xac
	OP_EQUAL       = 0x87
)
View Source
const (
	ZCashStaticPrefixByte = 0x1C // 28
	ZCashPrefixByteP2pkh  = 0xB8 // 184
	ZCashPrefixByteP2sh   = 0xBD // 189
)
View Source
const (
	PREFIX byte = 'f'

	BASE32_ALPHABET_FILECOIN string = "abcdefghijklmnopqrstuvwxyz234567"
)
View Source
const (
	TypeID = iota
	TypeSecp256k1
	TypeActor
	TypeBls
	TypeInvalid
)
View Source
const (
	OntologyAddressLength = 21
	OntologyVersion       = 0x17
)
View Source
const (
	TronAddressLength = 21
	TronPrefix        = 0x41
)
View Source
const (
	// Default alphabet, used e.g. by Bitcoin
	Base58DefaultAlphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
)
View Source
const (
	Bech32DefaultKeyhashLength = 20
)
View Source
const (
	ElrondKeyhashLength = 32
)
View Source
const (
	PolkadotNetwork = 0
)
View Source
const (
	SolanaAddressLength = 32
)
View Source
const (
	TezosAddressLength = 23
)

Variables

View Source
var (
	P2PKH_SCRIPT_PREFIX = []byte{OP_DUP, OP_HASH160, btcKeyHashLenght}
	P2SH_SCRIPT_PREFIX  = []byte{OP_HASH160, btcKeyHashLenght}
)

Functions

func BNBDecodeToBytes

func BNBDecodeToBytes(input string) ([]byte, error)

BNBDecodeToBytes converts the input string to a byte array

func BNBEncodeToString

func BNBEncodeToString(bytes []byte) (string, error)

BNBEncodeToString converts the input byte array to a string representation of the BNB address.

func Base58AddressDecodeToBytesPrefix added in v1.0.8

func Base58AddressDecodeToBytesPrefix(input string, decodedSize int, validPrefixes [][]byte) ([]byte, error)

func Base58AddressEncodeToStringPrefix added in v1.0.8

func Base58AddressEncodeToStringPrefix(data []byte, decodedSize int, validPrefixes [][]byte) (string, error)

func Base58ChecksumDecode added in v1.0.6

func Base58ChecksumDecode(b string, alphabet string) ([]byte, error)

Base58Decode decodes a modified base58 string to a byte slice and checks checksum.

func Base58ChecksumEncode added in v1.0.6

func Base58ChecksumEncode(b []byte, alphabet string) string

Base58Encode encodes a byte slice to a modified base58 string.

func Base58Decode

func Base58Decode(b string, alphabet string) ([]byte, error)

Base58Decode decodes a modified base58 string to a byte slice.

func Base58Encode

func Base58Encode(b []byte, alphabet string) string

Base58Encode encodes a byte slice to a modified base58 string.

func Bech32AddressDecodeToBytes

func Bech32AddressDecodeToBytes(hrp string, input string, keyhashLength int) ([]byte, error)

Bech32AddressDecodeToBytes converts the input string to a byte array

func Bech32AddressEncodeToString

func Bech32AddressEncodeToString(hrp string, bytes []byte, keyhashLength int) (string, error)

Bech32AddressEncodeToString converts the input byte array to a string representation of the bech32 address.

func Bech32DecodeToBytes

func Bech32DecodeToBytes(input string, hrp string) ([]byte, error)

func Bech32EncodeToString

func Bech32EncodeToString(bytes []byte, hrp string) (string, error)

func BitcoinCashDecodeToBytes

func BitcoinCashDecodeToBytes(input string) ([]byte, error)

BitcoinCashDecodeToBytes converts the input string to a byte array

func BitcoinCashEncodeToString

func BitcoinCashEncodeToString(input []byte) (string, error)

BitcoinCashEncodeToString converts the input byte array to a string representation of the Bitcoin address.

func BitcoinDecodeToBytes

func BitcoinDecodeToBytes(input string) ([]byte, error)

BitcoinDecodeToBytes converts the input string to a byte array

func BitcoinEncodeToString

func BitcoinEncodeToString(input []byte) (string, error)

BitcoinEncodeToString converts the input byte array to a string representation of the Bitcoin address.

func CosmosDecodeToBytes

func CosmosDecodeToBytes(input string) ([]byte, error)

CosmosDecodeToBytes converts the input string to a byte array

func CosmosEncodeToString

func CosmosEncodeToString(bytes []byte) (string, error)

CosmosEncodeToString converts the input byte array to a string representation of the Cosmos address.

func ElrondDecodeToBytes added in v1.0.9

func ElrondDecodeToBytes(input string) ([]byte, error)

ElrondDecodeToBytes converts the input string to a byte array

func ElrondEncodeToString added in v1.0.9

func ElrondEncodeToString(bytes []byte) (string, error)

ElrondEncodeToString converts the input byte array to a string representation of the Elrond address.

func EtherToBytes

func EtherToBytes(input string) ([]byte, error)

EtherToBytes converts the input string to a byte array.

func EtherToString

func EtherToString(input []byte) (string, error)

EtherToString converts the input byte array to a string representation of the Ethereum address.

func FilecoinDecodeToBytes added in v1.0.9

func FilecoinDecodeToBytes(input string) ([]byte, error)

FilecoinDecodeToBytes converts the input string to a byte array

func FilecoinEncodeToString added in v1.0.9

func FilecoinEncodeToString(data []byte) (string, error)

FilecoinEncodeToString converts the input byte array to a string representation of the Solana address.

func IoTexDecodeToBytes

func IoTexDecodeToBytes(input string) ([]byte, error)

IoTexDecodeToBytes converts the input string to a byte array

func IoTexEncodeToString

func IoTexEncodeToString(bytes []byte) (string, error)

IoTexEncodeToString converts the input byte array to a string representation of the Cosmos address.

func MakeBitcoinDecodeToBytes

func MakeBitcoinDecodeToBytes(config *CoinConfig) func(string) ([]byte, error)

MakeBitcoinDecodeToBytes takes a CoinConfig and returns a func to decode string to bytes

func MakeBitcoinEncodeToString

func MakeBitcoinEncodeToString(config *CoinConfig) func([]byte) (string, error)

MakeBitcoinEncodeToString takes a CoinConfig and returns a func to encode bytes to string

func NimiqDecodeToBytes added in v1.0.6

func NimiqDecodeToBytes(input string) ([]byte, error)

NimiqDecodeToBytes converts the "user-friendly Nimiq address" to its raw byte representation. The input must be uppercase, spaces are ignored.

func NimiqEncodeToString added in v1.0.6

func NimiqEncodeToString(buf []byte) (string, error)

NimiqEncodeToString a raw address to its user-friendly representation. If len(buf) != 20, encoding fails.

func OntologyDecodeToBytes added in v1.0.6

func OntologyDecodeToBytes(input string) ([]byte, error)

OntologyDecodeToBytes converts the input string to a byte array

func OntologyEncodeToString added in v1.0.6

func OntologyEncodeToString(bytes []byte) (string, error)

OntologyEncodeToString converts the input byte array to a string representation of the Ontology address.

func PolkadotDecodeToBytes added in v1.0.7

func PolkadotDecodeToBytes(input string) ([]byte, error)

PolkadotDecodeToBytes converts the input string to a byte array

func PolkadotEncodeToString added in v1.0.7

func PolkadotEncodeToString(data []byte) (string, error)

PolkadotEncodeToString converts the input byte array to a string representation of the Polkadot address.

func RunTestDecode added in v1.0.6

func RunTestDecode(coinType uint32, tt TestcaseDecode) error

func RunTestEncode added in v1.0.6

func RunTestEncode(coinType uint32, tt TestcaseEncode) error

func RunTestsDecode added in v1.0.6

func RunTestsDecode(t *testing.T, coinType uint32, tests []TestcaseDecode)

func RunTestsEncode added in v1.0.6

func RunTestsEncode(t *testing.T, coinType uint32, tests []TestcaseEncode)

func SS58AddressDecodeToBytes added in v1.0.7

func SS58AddressDecodeToBytes(input string, network byte) ([]byte, error)

SS58AddressDecodeToBytes converts the input string to a byte array

func SS58AddressEncodeToString added in v1.0.7

func SS58AddressEncodeToString(data []byte, network byte) (string, error)

SS58AddressEncodeToString converts the input byte array to a string representation of the address.

func SolanaDecodeToBytes added in v1.0.8

func SolanaDecodeToBytes(input string) ([]byte, error)

SolanaDecodeToBytes converts the input string to a byte array

func SolanaEncodeToString added in v1.0.8

func SolanaEncodeToString(data []byte) (string, error)

SolanaEncodeToString converts the input byte array to a string representation of the Solana address.

func StellarDecodeToBytes

func StellarDecodeToBytes(input string) ([]byte, error)

StellarDecodeToBytes converts the input string to a byte array

func StellarEncodeToString

func StellarEncodeToString(bytes []byte) (string, error)

StellarEncodeToString converts the input byte array to a string representation of the Cosmos address.

func TezosDecodeToBytes added in v1.0.6

func TezosDecodeToBytes(input string) ([]byte, error)

TezosDecodeToBytes converts the input string to a byte array

func TezosEncodeToString added in v1.0.6

func TezosEncodeToString(bytes []byte) (string, error)

TezosEncodeToString converts the input byte array to a string representation of the Tezos address.

func ToBytes

func ToBytes(input string, coinType uint32) ([]byte, error)

ToBytes converts the input string to a byte array for the given coin type.

func ToString

func ToString(input []byte, coinType uint32) (string, error)

ToString converts the input byte array to a string representation of the given coin type.

func TronDecodeToBytes added in v1.0.6

func TronDecodeToBytes(input string) ([]byte, error)

TronDecodeToBytes converts the input string to a byte array

func TronEncodeToString added in v1.0.6

func TronEncodeToString(bytes []byte) (string, error)

TronEncodeToString converts the input byte array to a string representation of the Tron address.

func XRPDecodeToBytes

func XRPDecodeToBytes(input string) ([]byte, error)

XRPDecodeToBytes converts the input string to a byte array

func XRPEncodeToString

func XRPEncodeToString(bytes []byte) (string, error)

XRPEncodeToString converts the input byte array to a string representation of the XRP address.

func ZilliqaDecodeToBytes

func ZilliqaDecodeToBytes(input string) ([]byte, error)

ZilliqaDecodeToBytes converts the input string to a byte array

func ZilliqaEncodeToString

func ZilliqaEncodeToString(bytes []byte) (string, error)

ZilliqaEncodeToString converts the input byte array to a string representation of the Zilliqa address.

Types

type CoinConfig

type CoinConfig struct {
	P2PKHPrefix []byte
	P2SHPPrefix []byte
	HRP         string
}

CoinConfig for Bitcoin and its forks

type TestcaseDecode added in v1.0.6

type TestcaseDecode struct {
	// contains filtered or unexported fields
}

type TestcaseEncode added in v1.0.6

type TestcaseEncode struct {
	// contains filtered or unexported fields
}

type Type added in v1.0.9

type Type byte

Jump to

Keyboard shortcuts

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