hdwallet

package module
v0.0.0-...-36670e3 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: MIT Imports: 12 Imported by: 0

README

hdwallet

from https://github.com/foxnut/go-hdwallet

Supports several address derivation for Bitcoin

  1. legacy address
  2. taproot address
  3. nested segwit address
  4. native segwit address

example

package hdwallet

import (
	"github.com/stretchr/testify/assert"
	"testing"
)

func GetMnemonic() string {
	return ""
}

func TestBtc_Address(t *testing.T) {
	masterKey, err := NewMasterKey(MnemonicFunc(GetMnemonic()))
	assert.NoError(t, err)
	nativeSegwitWallet := NewBtcNativeSegwitFromMasterKey(masterKey)
	t.Log("native segwit:		", nativeSegwitWallet.Address())
	nestedSegwitWallet := NewBtcNestedSegwitFromMasterKey(masterKey)
	t.Log("netsted segwit:		", nestedSegwitWallet.Address())
	taprootWallet := NewBtcTaprootFromMasterKey(masterKey)
	t.Log("taproot:				", taprootWallet.Address())
	legacyWallet := NewBtcLegacyFromMasterKey(masterKey)
	t.Log("legacy:				", legacyWallet.Address())
}

Documentation

Index

Constants

View Source
const (
	English            = "english"
	ChineseSimplified  = "chinese_simplified"
	ChineseTraditional = "chinese_traditional"
)
View Source
const (
	Zero      uint32 = 0
	ZeroQuote uint32 = 0x80000000
	BTCToken  uint32 = 0x10000000
	ETHToken  uint32 = 0x20000000
)
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
)

wallet type from bip44

Variables

View Source
var (
	DefaultParams       = &BTCParams
	DefaultPassword     = ""
	DefaultLanguage     = English
	DefaultPurpose      = ZeroQuote + 44
	DefaultCoinType     = BTC
	DefaultAccount      = ZeroQuote
	DefaultChange       = Zero
	DefaultAddressIndex = Zero
)
View Source
var (
	BTCParams        = chaincfg.MainNetParams
	BTCTestnetParams = chaincfg.TestNet3Params
	LTCParams        = chaincfg.MainNetParams
	DOGEParams       = chaincfg.MainNetParams
	DASHParams       = chaincfg.MainNetParams
	BCHParams        = chaincfg.MainNetParams
)
View Source
var (
	BTCLegacy       = AddressType(0)
	BTCTaproot      = AddressType(1)
	BTCNativeSegwit = AddressType(2)
	BTCNestedSegwit = AddressType(3)
)

Functions

func NewSeed

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

func PathNumber

func PathNumber(str string) uint32

PathNumber 44' => 0x80000000 + 44

Types

type AddressType

type AddressType uint32

func (AddressType) EqualTo

func (a AddressType) EqualTo(t AddressType) bool

type Btc

type Btc struct {
	AddressType AddressType
	// contains filtered or unexported fields
}

func (*Btc) Address

func (b *Btc) Address() string

func (*Btc) AddressWithType

func (b *Btc) AddressWithType(addressType AddressType) string

func (*Btc) Name

func (b *Btc) Name() string

func (*Btc) Symbol

func (b *Btc) Symbol() string

func (*Btc) Type

func (b *Btc) Type() uint32

type Key

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

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

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

func NewMasterKey

func NewMasterKey(opts ...KeyOptionFunc) (*Key, error)

func (*Key) AddressBTC

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

func (*Key) AddressP2WPKH

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

func (*Key) AddressP2WPKHInP2SH

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

func (*Key) GetChildrenKey

func (k *Key) GetChildrenKey(opts ...KeyOptionFunc) (*Key, error)

func (*Key) PublicHash

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

type KeyOption

type KeyOption struct {
	Params *chaincfg.Params

	Mnemonic string
	Password string
	Language string
	Seed     []byte

	// bip44
	Purpose      uint32
	CoinType     uint32
	Account      uint32
	Change       uint32
	AddressIndex uint32
}

func (*KeyOption) GetPathString

func (o *KeyOption) GetPathString() string

type KeyOptionFunc

type KeyOptionFunc func(option *KeyOption)

func AccountFunc

func AccountFunc(a uint32) KeyOptionFunc

func AddressIndexFunc

func AddressIndexFunc(a uint32) KeyOptionFunc

func ChangeFunc

func ChangeFunc(c uint32) KeyOptionFunc

func CoinTypeFunc

func CoinTypeFunc(c uint32) KeyOptionFunc

func LanguageFunc

func LanguageFunc(l string) KeyOptionFunc

func MnemonicFunc

func MnemonicFunc(m string) KeyOptionFunc

func ParamsFunc

func ParamsFunc(p *chaincfg.Params) KeyOptionFunc

func PasswordFunc

func PasswordFunc(p string) KeyOptionFunc

func PathFunc

func PathFunc(path string) KeyOptionFunc

func PurposeFunc

func PurposeFunc(p uint32) KeyOptionFunc

func SeedFunc

func SeedFunc(s []byte) KeyOptionFunc

type Wallet

type Wallet interface {
	Type() uint32
	Name() string
	Symbol() string
	Address() string
	// contains filtered or unexported methods
}

func NewBTCFromKey

func NewBTCFromKey(key *Key, addressType AddressType) Wallet

func NewBtcLegacyFromMasterKey

func NewBtcLegacyFromMasterKey(master *Key) Wallet

func NewBtcNativeSegwitFromMasterKey

func NewBtcNativeSegwitFromMasterKey(master *Key) Wallet

func NewBtcNestedSegwitFromMasterKey

func NewBtcNestedSegwitFromMasterKey(master *Key) Wallet

func NewBtcTaprootFromMasterKey

func NewBtcTaprootFromMasterKey(master *Key) Wallet

Jump to

Keyboard shortcuts

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