Skip to content

trustwallet/ens-coincodec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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