hdwallet

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2022 License: MIT Imports: 24 Imported by: 0

README

go-hdwallet

Golang实现的多币种HD钱包

支持的币种

  • BTC
  • LTC
  • DOGE
  • DASH
  • ETH
  • ETC
  • BCH
  • QTUM
  • USDT
  • IOST
  • USDC
  • TRX
  • BNB(Binance Chain)
  • FIL

安装

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

示例

更多示例请参考example

package main

import (
    "fmt"

    "github.com/tyler-smith/go-bip39"
    "github.com/xxx20220101/go-hdwallet"
)

func main() {

    // 128: 12 phrases
    // 256: 24 phrases
    mnemonic, _ := hdwallet.NewMnemonic(12, "")
       
    master, err := hdwallet.NewKey(
        hdwallet.Mnemonic(mnemonic),
    )
    if err != nil {
        panic(err)
    }
    fmt.Println("助记词:", mnemonic)

    wallet, _ := master.GetWallet(hdwallet.Purpose(hdwallet.ZeroQuote+44), hdwallet.CoinType(hdwallet.BTC), hdwallet.AddressIndex(0))
    address, _ := wallet.GetAddress()  // 1AwEPfoojHnKrhgt1vfuZAhrvPrmz7Rh44
    addressP2WPKH, _ := wallet.GetKey().AddressP2WPKH() // bc1qdnavt2xqvmc58ktff7rhvtn9s62zylp5lh5sry
    addressP2WPKHInP2SH, _ := wallet.GetKey().AddressP2WPKHInP2SH() // 39vtu9kWfGigXTKMMyc8tds7q36JBCTEHg 
    // addressP2WPKHInP2SH的特别说明:这个隔离见证的地址,是属于当前wif私钥的(默认bip44)。 
    // 假设你是用生成的助记词导入到imtoken中,对应的隔离见证地址不是这个。
    // 若想和imtoken一致,请在 master.GetWallet 时传入 hdwallet.ZeroQuote+49 (即bip49)得到的隔离见证地址和对应私钥即可
    btcwif, err := wallet.GetKey().PrivateWIF(true)
    if err != nil {
        panic(err)
    }
    fmt.Println("BTC私钥:", btcwif)
    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私钥:", wallet.GetKey().PrivateHex())
    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
	BTCTestnet = ZeroQuote + 1
	LTC        = ZeroQuote + 2
	DOGE       = ZeroQuote + 3
	DASH       = ZeroQuote + 5
	ETH        = ZeroQuote + 60
	ETC        = ZeroQuote + 61
	BCH        = ZeroQuote + 145
	QTUM       = ZeroQuote + 2301
	TRX        = ZeroQuote + 195
	BNB        = ZeroQuote + 714
	FIL        = ZeroQuote + 461
	SOL        = ZeroQuote + 501
	BSC        = ZeroQuote + 9006 //币安智能链

	// btc token
	USDT = BTCToken + 1

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

wallet type from bip44

View Source
const (
	MAINNET = "mainnet"
	TESTNET = "testnet"
)

network

Variables

View Source
var (
	DefaultParams = &BTCParams

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

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

default options

View Source
var (
	BTCParams        = chaincfg.MainNetParams
	BTCTestnetParams = chaincfg.TestNet3Params
	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

func PathNumber

func PathNumber(str string) uint32

PathNumber 44' => 0x80000000 + 44

Types

type Key

type Key struct {
	Opt      *Options
	Extended *hdkeychain.ExtendedKey

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

	// for eth
	PrivateECDSA *ecdsa.PrivateKey
	PublicECDSA  *ecdsa.PublicKey

	// for solana
	PublicKey  common.PublicKey
	PrivateKey ed25519.PrivateKey
}

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) AddressBNB

func (k *Key) AddressBNB(network string) (string, error)

AddressBNB 生成bnb地址 network mainnet主网地址 testnet测试网地址

func (*Key) AddressBTC

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

AddressBTC generate public key to btc style address

func (*Key) AddressFIL

func (k *Key) AddressFIL(network string) (string, error)

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) PrivateHexSOL

func (k *Key) PrivateHexSOL() string

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 Path

func Path(path string) Option

Path set to options example: m/44'/0'/0'/0/0 example: m/Purpose'/CoinType'/Account'/Change/AddressIndex

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)
	GetPrivateKey() (string, error)
}

Wallet interface

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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