hdwallet

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2019 License: Unlicense Imports: 12 Imported by: 0

README

Go HD Wallet tools Build Status

Get this library

    go get github.com/wemeetagain/go-hdwallet

Example

    // Generate a random 256 bit seed
    seed, err := hdwallet.GenSeed(256)
    
    // Create a master private key
    masterprv := hdwallet.MasterKey(seed)
    
    // Convert a private key to public key
    masterpub := masterprv.Pub()
    
    // Generate new child key based on private or public key
    childprv, err := masterprv.Child(0)
    childpub, err := masterpub.Child(0)
    
    // Create bitcoin address from public key
    address := childpub.Address()

    // Convenience string -> string Child and ToAddress functions
    walletstring := childpub.String()
    childstring, err := hdwallet.StringChild(walletstring, 0)
    childaddress, err := hdwallet.StringAddress(childstring)

Dependencies

    go get golang.org/x/crypto/ripemd160
    go get github.com/btcsuite/btcutil/base58
    go get github.com/btcsuite/btcd/btcec

License

Unlicense

Documentation

Overview

Package hdwallet implements heirarchical deterministic Bitcoin wallets, as defined in BIP 32.

BIP 32 - https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki

This package provides utilities for generating hierarchical deterministic Bitcoin wallets.

Examples

// Generate a random 256 bit seed
seed, err := hdwallet.GenSeed(256)

// Create a master private key
masterprv := hdwallet.MasterKey(seed)

// Convert a private key to public key
masterpub := masterprv.Pub()

// Generate new child key based on private or public key
childprv, err := masterprv.Child(0)
childpub, err := masterpub.Child(0)

// Create bitcoin address from public key
address := childpub.Address()

// Convenience string -> string Child and Address functions
walletstring := childpub.String()
childstring, err := hdwallet.StringChild(walletstring,0)
childaddress, err := hdwallet.StringAddress(childstring)

Extended Keys

Hierarchical deterministic wallets are simply deserialized extended keys. Extended Keys can be imported and exported as base58-encoded strings. Here are two examples:

public key:   "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"
private key:  "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi"

Index

Constants

View Source
const (
	// main net
	BtcPublicPrefix  = "0488B21E" //same for Bch
	BtcPrivatePrefix = "0488ADE4" //same for Bch
	BtcPubkeyHash    = "00"

	// test net
	TestBtcPublicPrefix  = "043587CF"
	TestBtcPrivatePrefix = "04358394"
	TestBtcPubkeyHash    = "6F"

	// main net
	LtcPublicPrefix  = "019DA462"
	LtcPrivatePrefix = "019D9CFE"
	LtcPubkeyHash    = "30"

	// test net
	TestLtcPublicPrefix  = "0436F6E1"
	TestLtcPrivatePrefix = "0436EF7D"
	TestLtcPubkeyHash    = "6F"

	DefaultBtcKey = "Bitcoin seed"
	DefaultLtcKey = "Litecoin seed"
)

Variables

This section is empty.

Functions

func GenSeed

func GenSeed(length int) ([]byte, error)

GenSeed returns a random seed with a length measured in bytes. The length must be at least 128.

Types

type HDWallet

type HDWallet struct {
	Vbytes      []byte //4 bytes
	Depth       uint16 //1 byte
	Fingerprint []byte //4 bytes
	I           []byte //4 bytes
	Chaincode   []byte //32 bytes
	Key         []byte //33 bytes
	// contains filtered or unexported fields
}

HDWallet defines the components of a hierarchical deterministic wallet

func (*HDWallet) Address

func (w *HDWallet) Address() string

Address returns bitcoin address represented by wallet w.

func (*HDWallet) Child

func (w *HDWallet) Child(i uint32) (*HDWallet, error)

Child returns the ith child of wallet w. Values of i >= 2^31 signify private key derivation. Attempting private key derivation with a public key will throw an error.

func (*HDWallet) Pub

func (w *HDWallet) Pub() *HDWallet

Pub returns a new wallet which is the public key version of w. If w is a public key, Pub returns a copy of w

func (*HDWallet) Serialize

func (w *HDWallet) Serialize() []byte

Serialize returns the serialized form of the wallet.

func (*HDWallet) String

func (w *HDWallet) String() string

String returns the base58-encoded string form of the wallet.

type WalletGenPrefixes

type WalletGenPrefixes struct {
	PubkeyHash    string
	PublicPrefix  string
	PrivatePrefix string
	Key           string
}

type WalletGenerator

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

func NewDefaultBchWalletGenerator

func NewDefaultBchWalletGenerator(test bool) (*WalletGenerator, error)

func NewDefaultBtcWalletGenerator

func NewDefaultBtcWalletGenerator(test bool) (*WalletGenerator, error)

func NewDefaultLtcWalletGenerator

func NewDefaultLtcWalletGenerator(test bool) (*WalletGenerator, error)

func NewWalletGenerator

func NewWalletGenerator(prefixes WalletGenPrefixes) (*WalletGenerator, error)

func (*WalletGenerator) ByteCheck

func (w *WalletGenerator) ByteCheck(dbin []byte) error

func (*WalletGenerator) MasterKey

func (w *WalletGenerator) MasterKey(seed []byte) (*HDWallet, error)

MasterKey returns a new wallet given a random seed.

func (*WalletGenerator) StringAddress

func (w *WalletGenerator) StringAddress(data string) (string, error)

StringAddress returns the Bitcoin address of a base58-encoded extended key.

func (*WalletGenerator) StringCheck

func (w *WalletGenerator) StringCheck(key string) error

StringCheck is a validation check of a base58-encoded extended key.

func (*WalletGenerator) StringChild

func (w *WalletGenerator) StringChild(data string, i uint32) (string, error)

StringChild returns the ith base58-encoded extended key of a base58-encoded extended key.

func (*WalletGenerator) StringWallet

func (w *WalletGenerator) StringWallet(data string) (*HDWallet, error)

StringWallet returns a wallet given a base58-encoded extended key

Jump to

Keyboard shortcuts

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