util

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2022 License: Unlicense, ISC Imports: 24 Imported by: 0

README

btcutil

ISC License GoDoc

Package btcutil provides bitcoin-specific convenience functions and types.

A comprehensive suite of tests is provided to ensure proper functionality. See test_coverage.txt for the gocov coverage report. Alternatively, if you are running a POSIX OS, you can run the cov_report.sh script for a real-time report.

This package was developed for pod, an alternative full-node implementation of bitcoin which is under active development by Conformal. Although it was primarily written for pod, this package has intentionally been designed so it can be used as a standalone package for any projects needing the functionality provided.

Installation and Updating

$ go get -u github.com/cybriq/p9/btcutil

License

Package btcutil is licensed under the copyfree ISC License.

Documentation

Overview

Package util provides bitcoin-specific convenience functions and types.

Block Overview

A Block defines a bitcoin block that provides easier and more efficient manipulation of raw wire protocol blocks. It also memoizes hashes for the block and its transactions on their first access so subsequent accesses don't have to repeat the relatively expensive hashing operations.

Tx Overview

A Tx defines a bitcoin transaction that provides more efficient manipulation of raw wire protocol transactions. It memoizes the hash for the transaction on its first access so subsequent accesses don't have to repeat the relatively expensive hashing operations.

Address Overview

The Address interface provides an abstraction for a Bitcoin address. While the most common type is a pay-to-pubkey-hash, Bitcoin already supports others and may well support more in the future. This package currently provides implementations for the pay-to-pubkey, pay-to-pubkey-hash, and pay-to-script-hash address types. To decode/encode an address:

	// NOTE: The default network is only used for address types which do not already contain that information.  At this
    // time, that is only pay-to-pubkey addresses.
	addrString := "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962" +
		"e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d57" +
		"8a4c702b6bf11d5f"
	defaultNet := &chaincfg.MainNetParams
	addr, e := util.DecodeAddress(addrString, defaultNet)
	if e != nil  {
		fmt.Println(e)
		return
	}
	fmt.Println(addr.EncodeAddress())

Index

Constants

View Source
const TxIndexUnknown = -1

TxIndexUnknown is the value returned for a transaction index that is unknown. This is typically because the transaction has not been inserted into a block yet.

Variables

View Source
var ErrMalformedPrivateKey = errors.New("malformed private key")

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.

View Source
var F, E, W, I, D, T log.LevelPrinter = log.GetLogPrinterSet(subsystem)

Functions

func GetActualPort

func GetActualPort(listener string) uint16

func GetSecondsAhead

func GetSecondsAhead(first, second time.Time) int

GetSecondsAhead returns the difference in time, of the second ahead of the first

func NewTLSCertPair

func NewTLSCertPair(organization string, validUntil time.Time, extraHosts []string) (cert, key []byte, e error)

NewTLSCertPair returns a new PEM-encoded x.509 certificate pair based on a 521-bit ECDSA private key. The machine's local interface addresses and all variants of IPv4 and IPv6 localhost are included as valid IP addresses.

Types

type Tx

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

Tx defines a bitcoin transaction that provides easier and more efficient manipulation of raw transactions. It also memorizes the hash for the transaction on its first access so subsequent accesses don't have to repeat the relatively expensive hashing operations.

func NewTx

func NewTx(msgTx *wire.MsgTx) *Tx

NewTx returns a new instance of a bitcoin transaction given an underlying wire.MsgTx. See Tx.

func NewTxFromBytes

func NewTxFromBytes(serializedTx []byte) (*Tx, error)

NewTxFromBytes returns a new instance of a bitcoin transaction given the serialized bytes. See Tx.

func NewTxFromReader

func NewTxFromReader(r io.Reader) (*Tx, error)

NewTxFromReader returns a new instance of a bitcoin transaction given a Reader to deserialize the transaction. See Tx.

func (*Tx) Hash

func (t *Tx) Hash() *chainhash.Hash

Hash returns the hash of the transaction. This is equivalent to calling TxHash on the underlying wire.MsgTx, however it caches the result so subsequent calls are more efficient.

func (*Tx) Index

func (t *Tx) Index() int

Index returns the saved index of the transaction within a block. This value will be TxIndexUnknown if it hasn't already explicitly been set.

func (*Tx) MsgTx

func (t *Tx) MsgTx() *wire.MsgTx

MsgTx returns the underlying wire.MsgTx for the transaction.

func (*Tx) SetIndex

func (t *Tx) SetIndex(index int)

SetIndex sets the index of the transaction in within a block.

type WIF

type WIF struct {
	// PrivKey is the private key being imported or exported.
	PrivKey *ec.PrivateKey
	// CompressPubKey specifies whether the address controlled by the imported or exported private key was created by
	// hashing a compressed (33-byte) serialized 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 serialized 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 *ec.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 serializing 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) SerializePubKey

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

SerializePubKey serializes the associated public key of the imported or exported private key in either a compressed or uncompressed format. The serialization 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.

Directories

Path Synopsis
Package hdkeychain provides an API for bitcoin hierarchical deterministic extended keys (BIP0032).
Package hdkeychain provides an API for bitcoin hierarchical deterministic extended keys (BIP0032).
Package helpers provides convenience functions to simplify wallet code.
Package helpers provides convenience functions to simplify wallet code.
legacy
Package treap implements a treap data structure that is used to hold ordered key/value pairs using a combination of binary search tree and heap semantics.
Package treap implements a treap data structure that is used to hold ordered key/value pairs using a combination of binary search tree and heap semantics.
Package zero Copyright (c) 2015 The btcsuite developers
Package zero Copyright (c) 2015 The btcsuite developers

Jump to

Keyboard shortcuts

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