wallet

package
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2021 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Mainnet/Testnet/Regnet
	AddrMain = 0
	AddrTest = 1
	AddrReg  = 2

	// Address usage
	AddrP2PKH        = 0
	AddrP2SH         = 1
	AddrP2WPKH       = 2
	AddrP2WSH        = 3
	AddrP2WPKHinP2SH = 4
	AddrP2WSHinP2SH  = 5
)

Address constants

View Source
const (
	XpubVersion = 0x0488b21e
	XprvVersion = 0x0488ade4

	UpubVersion = 0x043587cf
	UprvVersion = 0x04358394

	YpubVersion = 0x049d7cb2

	DrkpVersion = 0x02fe52cc

	DgubVersion = 0x02facafd

	MtubVersion = 0x01b26ef6
)

extended data version codes

Variables

View Source
var (
	ErrHDVersion = errors.New("Version mismatch")
	ErrHDPath    = errors.New("Invalid HD path")
	ErrHDKey     = errors.New("Invalid HD key")
)

Error codes

View Source
var (
	// AddrList for selected coins
	// (see page source for "https://iancoleman.io/bip39/")
	AddrList = []*AddrSpec{

		{0, []*AddrFormat{

			{"bc", 0x80, []*AddrVersion{
				{0x00, 0x0488ade4, 0x0488b21e},
				{0x05, 0x0488ade4, 0x0488b21e},
				{0x00, 0x04b24746, 0x04b2430c},
				{0x05, 0x02aa7ed3, 0x02aa7a99},
				{0x05, 0x049d7cb2, 0x049d7878},
				{0x05, 0x0295b43f, 0x0295b005},
			}},

			{"tb", 0xef, []*AddrVersion{
				{0x6f, 0x0488ade4, 0x0488b21e},
				{0xc4, 0x0488ade4, 0x0488b21e},
				{0x6f, 0x045f1cf6, 0x045f18bc},
				{0xc4, 0x02575483, 0x02575048},
				{0xc4, 0x044a5262, 0x044a4e28},
				{0xc4, 0x024289ef, 0x024285b5},
			}},

			{"bcrt", 0xef, []*AddrVersion{
				{0x6f, 0x043587cf, 0x04358394},
				{0xc4, 0x043587cf, 0x04358394},
				{0x6f, 0x045f1cf6, 0x045f18bc},
				{0xc4, 0x02575483, 0x02575048},
				{0xc4, 0x044a5262, 0x044a4e28},
				{0xc4, 0x024289ef, 0x024285b5},
			}},
		}, nil},

		{2, []*AddrFormat{

			{"ltc", 0xb0, []*AddrVersion{
				{0x30, 0x0488ade4, 0x0488b21e},
				{0x32, 0x0488ade4, 0x0488b21e},
				{0x30, 0x04b24746, 0x04b2430c},
				nil,
				{0x32, 0x01b26ef6, 0x01b26792},
				nil,
			}},

			{"litecointestnet", 0xef, []*AddrVersion{
				{0x6f, 0x043587cf, 0x04358394},
				{0xc4, 0x043587cf, 0x04358394},
				{0x6f, 0x043587cf, 0x04358394},
				nil,
				{0xc4, 0x043587cf, 0x04358394},
				nil,
			}},

			nil,
		}, nil},

		{3, []*AddrFormat{

			{"", 0x9e, []*AddrVersion{
				{0x1e, 0x02facafd, 0x02fac398},
				{0x16, 0x02facafd, 0x02fac398},
				nil,
				nil,
				nil,
				nil,
			}},

			{"dogecointestnet", 0xf1, []*AddrVersion{
				{0x71, 0x043587cf, 0x04358394},
				{0xc4, 0x043587cf, 0x04358394},
				{0x71, 0x043587cf, 0x04358394},
				nil,
				{0xc4, 0x043587cf, 0x04358394},
				nil,
			}},

			nil,
		}, nil},

		{5, []*AddrFormat{

			{"", 0xcc, []*AddrVersion{
				{0x4c, 0x0488ade4, 0x0488b21e},
				{0x10, 0x0488ade4, 0x0488b21e},
				nil,
				nil,
				nil,
				nil,
			}},

			{"", 0xef, []*AddrVersion{
				{0x8c, 0x043587cf, 0x04358394},
				{0x13, 0x043587cf, 0x04358394},
				nil,
				nil,
				nil,
				nil,
			}},

			nil,
		}, nil},

		{7, []*AddrFormat{

			{"", 0xb4, []*AddrVersion{
				{0x34, 0x0488ade4, 0x0488b21e},
				{0x0d, 0x0488ade4, 0x0488b21e},
				nil,
				nil,
				nil,
				nil,
			}},

			nil,

			nil,
		}, nil},

		{20, []*AddrFormat{

			{"dgb", 0x80, []*AddrVersion{
				{0x1e, 0x0488ade4, 0x0488b21e},
				{0x05, 0x0488ade4, 0x0488b21e},
				{0x1e, 0x04b24746, 0x04b2430c},
				nil,
				{0x3f, 0x049d7cb2, 0x049d7878},
				nil,
			}},

			nil,

			nil,
		}, nil},

		{133, []*AddrFormat{

			{"", 0x80, []*AddrVersion{
				{0x1cb8, 0x0488ade4, 0x0488b21e},
				{0x1cbd, 0x0488ade4, 0x0488b21e},
				nil,
				nil,
				nil,
				nil,
			}},

			nil,

			nil,
		}, nil},

		{145, []*AddrFormat{

			{"", 0x80, []*AddrVersion{
				{0x00, 0x0488ade4, 0x0488b21e},
				{0x05, 0x0488ade4, 0x0488b21e},
				{0x00, 0x04b24746, 0x04b2430c},
				{0x05, 0x02aa7ed3, 0x02aa7a99},
				{0x05, 0x049d7cb2, 0x049d7878},
				{0x05, 0x0295b43f, 0x0295b005},
			}},

			{"", 0xef, []*AddrVersion{
				{0x6f, 0x0488ade4, 0x0488b21e},
				{0xc4, 0x0488ade4, 0x0488b21e},
				{0x6f, 0x045f1cf6, 0x045f18bc},
				{0xc4, 0x02575483, 0x02575048},
				{0xc4, 0x044a5262, 0x044a4e28},
				{0xc4, 0x024289ef, 0x024285b5},
			}},

			{"", 0xef, []*AddrVersion{
				{0x6f, 0x043587cf, 0x04358394},
				{0xc4, 0x043587cf, 0x04358394},
				{0x6f, 0x045f1cf6, 0x045f18bc},
				{0xc4, 0x02575483, 0x02575048},
				{0xc4, 0x044a5262, 0x044a4e28},
				{0xc4, 0x024289ef, 0x024285b5},
			}},
		}, func(data []byte) string {

			bit5 := func(data []byte) []byte {
				size := len(data) * 8
				v := new(big.Int).SetBytes(data)
				pad := size % 5
				if pad != 0 {
					v = new(big.Int).Lsh(v, uint(5-pad))
				}
				num := (size + 4) / 5
				res := make([]byte, num)
				for i := num - 1; i >= 0; i-- {
					res[i] = byte(v.Int64() & 31)
					v = new(big.Int).Rsh(v, 5)
				}
				return res
			}

			polymod := func(values []byte) uint64 {
				var c uint64 = 1
				for _, d := range values {
					c0 := c >> 35
					c = ((c & 0x07ffffffff) << 5) ^ uint64(d)
					if c0&0x01 != 0 {
						c ^= 0x98f2bc8e61
					}
					if c0&0x02 != 0 {
						c ^= 0x79b76d99e2
					}
					if c0&0x04 != 0 {
						c ^= 0xf33e5fb3c4
					}
					if c0&0x08 != 0 {
						c ^= 0xae2eabe2a8
					}
					if c0&0x10 != 0 {
						c ^= 0x1e4f43e470
					}
				}
				return c ^ 1
			}

			b32 := base32.NewEncoding("qpzry9x8gf2tvdw0s3jn54khce6mua7l")
			addr := strings.Trim("bitcoincash:"+b32.EncodeToString(data), "=")
			values := make([]byte, 54)
			copy(values, []byte{2, 9, 20, 3, 15, 9, 14, 3, 1, 19, 8, 0})
			copy(values[12:], bit5(data))
			crc := polymod(values)
			buf := new(bytes.Buffer)
			binary.Write(buf, binary.BigEndian, crc)
			return addr + strings.Trim(b32.EncodeToString(buf.Bytes()[3:]), "=")
		}},

		{156, []*AddrFormat{

			{"btg", 0x80, []*AddrVersion{
				{0x26, 0x0488ade4, 0x0488b21e},
				{0x17, 0x0488ade4, 0x0488b21e},
				{0x26, 0x04b24746, 0x04b2430c},
				nil,
				{0x17, 0x049d7cb2, 0x049d7878},
				nil,
			}},

			nil,

			nil,
		}, nil},
	}
)
View Source
var (
	// CoinList of all all available BIP44 coin types
	CoinList = []*CoinSpec{}/* 815 elements not displayed */

)
View Source
var (
	ErrInvalidEntropy = fmt.Errorf("Invalid entropy data")
)

Error codes

Functions

func CheckVersion added in v1.2.5

func CheckVersion(version uint32) int

CheckVersion returns a status code:

-1 if extended data refers to a public key
 1 if extended data refers to a private key
 0 if version is unknown

func EntropyToWords

func EntropyToWords(ent []byte) (words []string, err error)

EntropyToWords converts an entropy into a sequence of words

func MakeAddress added in v1.2.6

func MakeAddress(key *bitcoin.PublicKey, coin, version, network int) string

MakeAddress computes an address from public key for the "real" Bitcoin network

func WordsToEntropy

func WordsToEntropy(words []string) ([]byte, string)

WordsToEntropy converts a sequence of words into an entropy. Returns the entropy OR the words where the conversion failed. If the number of words are incorrect, "#" is returned.

func WordsToSeed

func WordsToSeed(words []string, password string) ([]byte, string)

WordsToSeed computes a seed value for a given word list

Types

type AddrFormat added in v1.2.6

type AddrFormat struct {
	Bech32     string
	WifVersion byte
	Versions   []*AddrVersion
}

AddrFormat defines formatting information for addresses

type AddrSpec added in v1.2.6

type AddrSpec struct {
	CoinID  int
	Formats []*AddrFormat
	Conv    func([]byte) string
}

AddrSpec defines a coin address format.

type AddrVersion added in v1.2.6

type AddrVersion struct {
	Version    uint16 // version byte (address prefix)
	PubVersion uint32 // BIP32 key version (public)
	PrvVersion uint32 // BIP32 key version (private)
}

AddrVersion defines address version constants

type CoinSpec

type CoinSpec struct {
	ID     int
	Symbol string
	Name   string
}

CoinSpec defines a BIP44 registered coin. see: https://github.com/satoshilabs/slips/blob/master/slip-0044.md

https://github.com/libbitcoin/libbitcoin-system/wiki/Altcoin-Version-Mappings

type ExtendedData

type ExtendedData struct {
	Version   uint32 `order:"big"`
	Depth     uint8
	ParentFP  uint32 `order:"big"`
	Child     uint32 `order:"big"`
	Chaincode []byte `size:"32"`
	Keydata   []byte `size:"33"`
}

ExtendedData is the data structure representing ExtendedKeys (both public and private) for exchange purposes.

func NewExtendedData

func NewExtendedData() *ExtendedData

NewExtendedData allocates a new extended data object

func ParseExtended

func ParseExtended(s string) (*ExtendedData, error)

ParseExtended returns a new data object for a given extended key string

func (*ExtendedData) String

func (d *ExtendedData) String() string

String converts an extended data object into a human-readable representation.

type ExtendedPrivateKey

type ExtendedPrivateKey struct {
	Data *ExtendedData
	Key  *math.Int
}

ExtendedPrivateKey represents a private key in a HD tree

func CKDprv

func CKDprv(k *ExtendedPrivateKey, i uint32) (ki *ExtendedPrivateKey)

CKDprv is a key derivation function for private keys

func ParseExtendedPrivateKey

func ParseExtendedPrivateKey(s string) (k *ExtendedPrivateKey, err error)

ParseExtendedPrivateKey converts a xprv string to a private key

func (*ExtendedPrivateKey) Public

Public returns the associated public key

func (*ExtendedPrivateKey) String

func (k *ExtendedPrivateKey) String() string

String returns the string representation of an ExtendedPrivateKey

type ExtendedPublicKey

type ExtendedPublicKey struct {
	Data *ExtendedData
	Key  *bitcoin.Point
}

ExtendedPublicKey represents a public key in a HD tree

func CKDpub

func CKDpub(k *ExtendedPublicKey, i uint32) (ki *ExtendedPublicKey)

CKDpub is a key derivation function for public keys

func ParseExtendedPublicKey

func ParseExtendedPublicKey(s string) (k *ExtendedPublicKey, err error)

ParseExtendedPublicKey converts a xpub string to a public key

func (*ExtendedPublicKey) Clone

Clone returns a deep copy of a public key

func (*ExtendedPublicKey) Fingerprint

func (e *ExtendedPublicKey) Fingerprint() (i uint32)

Fingerprint returns the fingerprint of an ExtendedPublicKey

func (*ExtendedPublicKey) String

func (e *ExtendedPublicKey) String() string

String returns the string representation of an ExtendedPublicKey

type HD

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

HD represents a hierarchically deterministic key space.

func NewHD

func NewHD(seed []byte) *HD

NewHD initializes a new HD from a seed value.

func (*HD) MasterPrivate added in v1.2.5

func (hd *HD) MasterPrivate() *ExtendedPrivateKey

MasterPrivate returns the master private key.

func (*HD) MasterPublic added in v1.2.5

func (hd *HD) MasterPublic() *ExtendedPublicKey

MasterPublic returns the master public key.

func (*HD) Private

func (hd *HD) Private(path string) (prv *ExtendedPrivateKey, err error)

Private returns an extended private key for a given path (BIP32,BIP44)

func (*HD) Public

func (hd *HD) Public(path string) (pub *ExtendedPublicKey, err error)

Public returns an extended public key for a given path (BIP32,BIP44)

type HDPublic

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

HDPublic represents a public branch in a hierarchically deterministic key space.

func NewHDPublic

func NewHDPublic(key *ExtendedPublicKey, path string) *HDPublic

NewHDPublic initializes a new HDPublic from an extended public key with a given path.

func (*HDPublic) Public

func (hd *HDPublic) Public(path string) (pub *ExtendedPublicKey, err error)

Public returns an extended public key for a given path. The path MUST NOT contain hardened elements and must start with the path of the public key in HDPublic!

Jump to

Keyboard shortcuts

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