hdwallet

package module
v0.0.0-...-1a6e2ac Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2018 License: MIT Imports: 12 Imported by: 0

README

go-hdwallet

A multi-cryptocurrency HD wallet implementated by golang.

supported coins

  • BTC
  • LTC
  • DOGE
  • DASH
  • ETH
  • ETC
  • BCH
  • QTUM
  • USDT
  • IOST
  • USDC

install

go get -v -u github.com/foxnut/go-hdwallet

example

package main

import (
    "fmt"

    "github.com/foxnut/go-hdwallet"
)

var (
    mnemonic = "range sheriff try enroll deer over ten level bring display stamp recycle"
)

func main() {
    master, err := hdwallet.NewKey(
        hdwallet.Mnemonic(mnemonic),
    )
    if err != nil {
        panic(err)
    }

    // BTC: 1AwEPfoojHnKrhgt1vfuZAhrvPrmz7Rh4
    wallet, _ := master.GetWallet(hdwallet.CoinType(hdwallet.BTC), hdwallet.AddressIndex(1))
    address, _ := wallet.GetAddress()
    addressP2WPKH, _ := wallet.GetKey().AddressP2WPKH()
    addressP2WPKHInP2SH, _ := wallet.GetKey().AddressP2WPKHInP2SH()
    fmt.Println("BTC: ", address, addressP2WPKH, addressP2WPKHInP2SH)

    // BCH: 1CSBT18sjcCwLCpmnnyN5iqLc46Qx7CC91
    wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.BCH))
    address, _ = wallet.GetAddress()
    addressBCH, _ := wallet.GetKey().AddressBCH()
    fmt.Println("BCH: ", address, addressBCH)

    // LTC: LLCaMFT8AKjDTvz1Ju8JoyYXxuug4PZZmS
    wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.LTC))
    address, _ = wallet.GetAddress()
    fmt.Println("LTC: ", address)

    // DOGE: DHLA3rJcCjG2tQwvnmoJzD5Ej7dBTQqhHK
    wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.DOGE))
    address, _ = wallet.GetAddress()
    fmt.Println("DOGE:", address)

    // ETH: 0x37039021cBA199663cBCb8e86bB63576991A28C1
    wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.ETH))
    address, _ = wallet.GetAddress()
    fmt.Println("ETH: ", address)

    // ETC: 0x480C69E014C7f018dAbF17A98273e90f0b0680cf
    wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.ETC))
    address, _ = wallet.GetAddress()
    fmt.Println("ETC: ", address)
}

Documentation

Index

Constants

View Source
const (
	English            = "english"
	ChineseSimplified  = "chinese_simplified"
	ChineseTraditional = "chinese_traditional"
)

mnemonic language

View Source
const (
	Zero      uint32 = 0
	ZeroQuote uint32 = 0x80000000
	BTCToken  uint32 = 0x10000000
	ETHToken  uint32 = 0x20000000
)

zero is deafult of uint32

View Source
const (
	// https://github.com/satoshilabs/slips/blob/master/slip-0044.md#registered-coin-types
	BTC  = ZeroQuote + 0
	LTC  = ZeroQuote + 2
	DOGE = ZeroQuote + 3
	DASH = ZeroQuote + 5
	ETH  = ZeroQuote + 60
	ETC  = ZeroQuote + 61
	BCH  = ZeroQuote + 145
	QTUM = ZeroQuote + 2301

	// btc token
	USDT = BTCToken + 1

	// eth token
	IOST = ETHToken + 1
	USDC = ETHToken + 2
)

wallet type from bip44

Variables

View Source
var (
	DefaultParams = &chaincfg.MainNetParams

	// master key options
	DefaultPassword = ""
	DefaultLanguage = ""

	// child key options
	DefaultPurpose      = ZeroQuote + 44
	DefaultCoinType     = BTC
	DefaultAccount      = ZeroQuote
	DefaultChange       = Zero
	DefaultAddressIndex = Zero
)

default options

View Source
var (
	BTCParams  = chaincfg.MainNetParams
	LTCParams  = chaincfg.MainNetParams
	DOGEParams = chaincfg.MainNetParams
	DASHParams = chaincfg.MainNetParams
	BCHParams  = chaincfg.MainNetParams
	QTUMParams = chaincfg.MainNetParams
	USDTParams = chaincfg.MainNetParams
)

init net params

View Source
var (
	ErrCoinTypeUnknow = errors.New("unknow coin type")
)

errors

Functions

func NewMnemonic

func NewMnemonic(length int, language string) (string, error)

NewMnemonic creates a random mnemonic

func NewSeed

func NewSeed(mnemonic, password, language string) ([]byte, error)

NewSeed creates a hashed seed

Types

type Key

type Key struct {
	Extended *hdkeychain.ExtendedKey

	// for btc
	Private *btcec.PrivateKey
	Public  *btcec.PublicKey

	// for eth
	PrivateECDSA *ecdsa.PrivateKey
	PublicECDSA  *ecdsa.PublicKey
	// contains filtered or unexported fields
}

Key struct

func NewKey

func NewKey(opts ...Option) (*Key, error)

NewKey creates a master key params: Mnemonic, Password, Language, Seed

func (*Key) AddressBCH

func (k *Key) AddressBCH() (string, error)

AddressBCH generate public key to bch style address

func (*Key) AddressBTC

func (k *Key) AddressBTC() (string, error)

AddressBTC generate public key to btc style address

func (*Key) AddressP2WPKH

func (k *Key) AddressP2WPKH() (string, error)

AddressP2WPKH generate public key to p2wpkh style address

func (*Key) AddressP2WPKHInP2SH

func (k *Key) AddressP2WPKHInP2SH() (string, error)

AddressP2WPKHInP2SH generate public key to p2wpkh nested within p2sh style address

func (*Key) GetChildKey

func (k *Key) GetChildKey(opts ...Option) (*Key, error)

GetChildKey return a key from master key params: Purpose, CoinType, Account, Change, AddressIndex, [Path]

func (*Key) GetWallet

func (k *Key) GetWallet(opts ...Option) (Wallet, error)

GetWallet return wallet from master key params: Purpose, CoinType, Account, Change, AddressIndex, [Path]

func (*Key) PrivateHex

func (k *Key) PrivateHex() string

PrivateHex generate private key to string by hex

func (*Key) PrivateWIF

func (k *Key) PrivateWIF(compress bool) (string, error)

PrivateWIF generate private key to string by wif

func (*Key) PublicHash

func (k *Key) PublicHash() ([]byte, error)

PublicHash generate public key by hash160

func (*Key) PublicHex

func (k *Key) PublicHex(compress bool) string

PublicHex generate public key to string by hex

type Option

type Option func(*Options)

Option of key

func Account

func Account(a uint32) Option

Account set to options

func AddressIndex

func AddressIndex(a uint32) Option

AddressIndex set to options

func Change

func Change(c uint32) Option

Change set to options

func CoinType

func CoinType(c uint32) Option

CoinType set to options

func Language

func Language(l string) Option

Language set to options

func Mnemonic

func Mnemonic(m string) Option

Mnemonic set to options

func Params

func Params(p *chaincfg.Params) Option

Params set to options

func Password

func Password(p string) Option

Password set to options

func Purpose

func Purpose(p uint32) Option

Purpose set to options

func Seed

func Seed(s []byte) Option

Seed set to options

type Options

type Options struct {
	Params *chaincfg.Params

	// master key options
	Mnemonic string
	Password string
	Language string
	Seed     []byte

	// child key options
	Purpose      uint32
	CoinType     uint32
	Account      uint32
	Change       uint32
	AddressIndex uint32
}

Options of key

func (*Options) GetPath

func (o *Options) GetPath() []uint32

GetPath return path in bip44 style

type Wallet

type Wallet interface {
	GetType() uint32
	GetName() string
	GetSymbol() string
	GetKey() *Key
	GetAddress() (string, error)
}

Wallet interface

Jump to

Keyboard shortcuts

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