wif

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2021 License: ISC Imports: 6 Imported by: 26

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrChecksumMismatch describes an error where decoding failed due
	// to a bad checksum.
	ErrChecksumMismatch = errors.New("checksum mismatch")

	// ErrMalformedPrivateKey describes an error where a WIF-encoded private
	// key cannot be decoded due to being improperly formatted.  This may occur
	// if the byte length is incorrect or an unexpected magic number was
	// encountered.
	ErrMalformedPrivateKey = errors.New("malformed private key")
)

Functions

This section is empty.

Types

type WIF

type WIF struct {
	// PrivKey is the private key being imported or exported.
	PrivKey *bec.PrivateKey

	// CompressPubKey specifies whether the address controlled by the
	// imported or exported private key was created by hashing a
	// compressed (33-byte) serialised public key, rather than an
	// uncompressed (65-byte) one.
	CompressPubKey bool
	// contains filtered or unexported fields
}

WIF contains the individual components described by the Wallet Import Format (WIF). A WIF string is typically used to represent a private key and its associated address in a way that may be easily copied and imported into or exported from wallet software. WIF strings may be decoded into this structure by calling DecodeWIF or created with a user-provided private key by calling NewWIF.

func DecodeWIF

func DecodeWIF(wif string) (*WIF, error)

DecodeWIF creates a new WIF structure by decoding the string encoding of the import format.

The WIF string must be a base58-encoded string of the following byte sequence:

  • 1 byte to identify the network, must be 0x80 for mainnet or 0xef for either testnet3 or the regression test network
  • 32 bytes of a binary-encoded, big-endian, zero-padded private key
  • Optional 1 byte (equal to 0x01) if the address being imported or exported was created by taking the RIPEMD160 after SHA256 hash of a serialised compressed (33-byte) public key
  • 4 bytes of checksum, must equal the first four bytes of the double SHA256 of every byte before the checksum in this sequence

If the base58-decoded byte sequence does not match this, DecodeWIF will return a non-nil error. ErrMalformedPrivateKey is returned when the WIF is of an impossible length or the expected compressed pubkey magic number does not equal the expected value of 0x01. ErrChecksumMismatch is returned if the expected WIF checksum does not match the calculated checksum.

func NewWIF

func NewWIF(privKey *bec.PrivateKey, net *chaincfg.Params, compress bool) (*WIF, error)

NewWIF creates a new WIF structure to export an address and its private key as a string encoded in the Wallet Import Format. The compress argument specifies whether the address intended to be imported or exported was created by serialising the public key compressed rather than uncompressed.

func (*WIF) IsForNet

func (w *WIF) IsForNet(net *chaincfg.Params) bool

IsForNet returns whether or not the decoded WIF structure is associated with the passed bitcoin network.

func (*WIF) SerialisePubKey

func (w *WIF) SerialisePubKey() []byte

SerialisePubKey serialises the associated public key of the imported or exported private key in either a compressed or uncompressed format. The serialisation format chosen depends on the value of w.CompressPubKey.

func (*WIF) String

func (w *WIF) String() string

String creates the Wallet Import Format string encoding of a WIF structure. See DecodeWIF for a detailed breakdown of the format and requirements of a valid WIF string.

Jump to

Keyboard shortcuts

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