hdwallet

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2019 License: Unlicense Imports: 12 Imported by: 7

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

This section is empty.

Variables

View Source
var (
	//MainNet
	Public  []byte
	Private []byte
	//TestNet
	TestPublic  []byte
	TestPrivate []byte
)

Functions

func ByteCheck

func ByteCheck(dbin []byte) error

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.

func StringAddress

func StringAddress(data string) (string, error)

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

func StringCheck

func StringCheck(key string) error

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

func StringChild

func StringChild(data string, i uint32) (string, error)

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

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
}

HDWallet defines the components of a hierarchical deterministic wallet

func MasterKey

func MasterKey(seed []byte) *HDWallet

MasterKey returns a new wallet given a random seed.

func StringWallet

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

StringWallet returns a wallet given a base58-encoded extended key

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.

Jump to

Keyboard shortcuts

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