bchutil

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2022 License: MIT Imports: 14 Imported by: 5

README

bchutil

Bitcoin Cash Utilities

Contains the bitcoin cash signing algorithm and some protocol configuration. This package removes the need to use a fork of btcd when working with Bitcoin Cash.

Documentation

Index

Constants

View Source
const (
	// MainNet represents the main bitcoin network.
	MainnetMagic wire.BitcoinNet = 0xe8f3e1e3

	// Testnet represents the test network (version 3).
	TestnetMagic wire.BitcoinNet = 0xf4f3e5f4

	// Regtest represents the regression test network.
	Regtestmagic wire.BitcoinNet = 0xfabfb5da
)
View Source
const BlockHeightUnknown = int32(-1)

BlockHeightUnknown is the value returned for a block height that is unknown. This is typically because the block has not been inserted into the main chain yet.

View Source
const CHARSET string = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"

*

  • The cashaddr character set for encoding.
View Source
const SFNodeBitcoinCash wire.ServiceFlag = 1 << 5

Node_BitcoinCash service bit

View Source
const (
	SigHashForkID txscript.SigHashType = 0x40
)
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 (
	// ErrChecksumMismatch describes an error where decoding failed due
	// to a bad checksum.
	ErrChecksumMismatch = errors.New("checksum mismatch")

	// ErrUnknownAddressType describes an error where an address can not
	// decoded as a specific address type due to the string encoding
	// begining with an identifier byte unknown to any standard or
	// registered (via chaincfg.Register) network.
	ErrUnknownAddressType = errors.New("unknown address type")

	// ErrAddressCollision describes an error where an address can not
	// be uniquely determined as either a pay-to-pubkey-hash or
	// pay-to-script-hash address since the leading identifier is used for
	// describing both address kinds, but for different networks.  Rather
	// than assuming or defaulting to one or the other, this error is
	// returned and the caller must decide how to decode the address.
	ErrAddressCollision = errors.New("address collision")

	// ErrInvalidFormat describes an error where decoding failed due to invalid version
	ErrInvalidFormat = errors.New("invalid format: version and/or checksum bytes missing")

	Prefixes map[string]string
)
View Source
var CHARSET_REV = [128]int8{}/* 128 elements not displayed */

*

  • The cashaddr character set for decoding.
View Source
var MainnetDNSSeeds = []chaincfg.DNSSeed{
	{"seed.bitcoinabc.org", true},
	{"seed-abc.bitcoinforks.org", true},
	{"seed.bitcoinunlimited.info", true},
	{"seed.bitprim.org", true},
	{"seed.deadalnix.me", true},
}
View Source
var SlpPrefixes = map[string]string{
	chaincfg.MainNetParams.Name:  "simpleledger",
	chaincfg.TestNet3Params.Name: "slptest",
}
View Source
var TestnetDNSSeeds = []chaincfg.DNSSeed{
	{"testnet-seed.bitcoinabc.org", true},
	{"testnet-seed-abc.bitcoinforks.org", true},
	{"testnet-seed.bitcoinunlimited.info", true},
	{"testnet-seed.bitprim.org", true},
	{"testnet-seed.deadalnix.me", true},
}

Functions

func Cat

func Cat(x, y data) data

*

  • Concatenate two byte arrays.

func CheckEncodeCashAddress

func CheckEncodeCashAddress(input []byte, prefix string, t AddressType) string

func CheckEncodeSlpAddress

func CheckEncodeSlpAddress(input []byte, prefix string, t AddressType) string

func CreateChecksum

func CreateChecksum(prefix string, payload data) data

*

  • Create a checksum.

func DecodeAddress

func DecodeAddress(addr string, defaultNet *chaincfg.Params) (btcutil.Address, error)

DecodeAddress decodes the string encoding of an address and returns the Address if addr is a valid encoding for a known address type.

The bitcoin cash network the address is associated with is extracted if possible.

func DecodeBitpay

func DecodeBitpay(addr string, defaultNet *chaincfg.Params) (btcutil.Address, error)

DecodeAddress decodes the string encoding of an address and returns the Address if addr is a valid encoding for a known address type.

The bitcoin network the address is associated with is extracted if possible. When the address does not encode the network, such as in the case of a raw public key, the address will be associated with the passed defaultNet.

func DecodeCashAddress

func DecodeCashAddress(str string) (string, data, error)

*

  • Decode a cashaddr string.

func Encode

func Encode(prefix string, payload data) string

*

  • Encode a cashaddr string.

func ExpandPrefix

func ExpandPrefix(prefix string) data

*

  • Expand the address prefix for the checksum computation.

func ExtractPkScriptAddrs

func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (btcutil.Address, error)

ExtractPkScriptAddrs returns the type of script, addresses and required signatures associated with the passed PkScript. Note that it only works for 'standard' transaction script types. Any data such as public keys which are invalid are omitted from the results.

func GetDNSSeed

func GetDNSSeed(params *chaincfg.Params) []chaincfg.DNSSeed

func LowerCase

func LowerCase(c byte) byte

*

  • Convert to lower case. *
  • Assume the input is a character.

func PayToAddrScript

func PayToAddrScript(addr btcutil.Address) ([]byte, error)

func PolyMod

func PolyMod(v data) uint64

*

  • This function will compute what 8 5-bit values to XOR into the last 8 input
  • values, in order to make the checksum 0. These 8 values are packed together
  • in a single 40-bit integer. The higher bits correspond to earlier values.

func RawTxInSignature

func RawTxInSignature(tx *wire.MsgTx, idx int, subScript []byte,
	hashType txscript.SigHashType, key *btcec.PrivateKey, amt int64) ([]byte, error)

RawTxInSignature returns the serialized ECDSA signature for the input idx of the given transaction, with hashType appended to it.

func SignTxOutput

func SignTxOutput(chainParams *chaincfg.Params, tx *wire.MsgTx, idx int,
	pkScript []byte, hashType txscript.SigHashType, kdb txscript.KeyDB, sdb txscript.ScriptDB,
	previousScript []byte, amt int64) ([]byte, error)

func SignatureScript

func SignatureScript(tx *wire.MsgTx, idx int, subscript []byte, hashType txscript.SigHashType, privKey *btcec.PrivateKey, compress bool, amt int64) ([]byte, error)

func VerifyChecksum

func VerifyChecksum(prefix string, payload data) bool

*

  • Verify a checksum.

Types

type AddressType

type AddressType int
const (
	P2PKH AddressType = 0
	P2SH  AddressType = 1
)

func CheckDecodeCashAddress

func CheckDecodeCashAddress(input string) (result []byte, prefix string, t AddressType, err error)

CheckDecode decodes a string that was encoded with CheckEncode and verifies the checksum.

type BitpayAddressPubKeyHash

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

AddressPubKeyHash is an Address for a pay-to-pubkey-hash (P2PKH) transaction.

func NewBitpayAddressPubKeyHash

func NewBitpayAddressPubKeyHash(pkHash []byte, net *chaincfg.Params) (*BitpayAddressPubKeyHash, error)

NewAddressPubKeyHash returns a new AddressPubKeyHash. pkHash mustbe 20 bytes.

func (*BitpayAddressPubKeyHash) EncodeAddress

func (a *BitpayAddressPubKeyHash) EncodeAddress() string

EncodeAddress returns the string encoding of a pay-to-pubkey-hash address. Part of the Address interface.

func (*BitpayAddressPubKeyHash) Hash160

func (a *BitpayAddressPubKeyHash) Hash160() *[ripemd160.Size]byte

Hash160 returns the underlying array of the pubkey hash. This can be useful when an array is more appropiate than a slice (for example, when used as map keys).

func (*BitpayAddressPubKeyHash) IsForNet

func (a *BitpayAddressPubKeyHash) IsForNet(net *chaincfg.Params) bool

IsForNet returns whether or not the pay-to-pubkey-hash address is associated with the passed bitcoin network.

func (*BitpayAddressPubKeyHash) ScriptAddress

func (a *BitpayAddressPubKeyHash) ScriptAddress() []byte

ScriptAddress returns the bytes to be included in a txout script to pay to a pubkey hash. Part of the Address interface.

func (*BitpayAddressPubKeyHash) String

func (a *BitpayAddressPubKeyHash) String() string

String returns a human-readable string for the pay-to-pubkey-hash address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.

type BitpayAddressScriptHash

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

AddressScriptHash is an Address for a pay-to-script-hash (P2SH) transaction.

func NewBitpayAddressScriptHash

func NewBitpayAddressScriptHash(serializedScript []byte, net *chaincfg.Params) (*BitpayAddressScriptHash, error)

NewAddressScriptHash returns a new AddressScriptHash.

func NewBitpayAddressScriptHashFromHash

func NewBitpayAddressScriptHashFromHash(scriptHash []byte, net *chaincfg.Params) (*BitpayAddressScriptHash, error)

NewAddressScriptHashFromHash returns a new AddressScriptHash. scriptHash must be 20 bytes.

func (*BitpayAddressScriptHash) EncodeAddress

func (a *BitpayAddressScriptHash) EncodeAddress() string

EncodeAddress returns the string encoding of a pay-to-script-hash address. Part of the Address interface.

func (*BitpayAddressScriptHash) Hash160

func (a *BitpayAddressScriptHash) Hash160() *[ripemd160.Size]byte

Hash160 returns the underlying array of the script hash. This can be useful when an array is more appropiate than a slice (for example, when used as map keys).

func (*BitpayAddressScriptHash) IsForNet

func (a *BitpayAddressScriptHash) IsForNet(net *chaincfg.Params) bool

IsForNet returns whether or not the pay-to-script-hash address is associated with the passed bitcoin network.

func (*BitpayAddressScriptHash) ScriptAddress

func (a *BitpayAddressScriptHash) ScriptAddress() []byte

ScriptAddress returns the bytes to be included in a txout script to pay to a script hash. Part of the Address interface.

func (*BitpayAddressScriptHash) String

func (a *BitpayAddressScriptHash) String() string

String returns a human-readable string for the pay-to-script-hash address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.

type Block

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

Block defines a bitcoin block that provides easier and more efficient manipulation of raw 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.

func NewBlock

func NewBlock(msgBlock *wire.MsgBlock) *Block

NewBlock returns a new instance of a bitcoin block given an underlying wire.MsgBlock. See Block.

func NewBlockFromBlockAndBytes

func NewBlockFromBlockAndBytes(msgBlock *wire.MsgBlock, serializedBlock []byte) *Block

NewBlockFromBlockAndBytes returns a new instance of a bitcoin block given an underlying wire.MsgBlock and the serialized bytes for it. See Block.

func NewBlockFromBytes

func NewBlockFromBytes(serializedBlock []byte) (*Block, error)

NewBlockFromBytes returns a new instance of a bitcoin block given the serialized bytes. See Block.

func NewBlockFromReader

func NewBlockFromReader(r io.Reader) (*Block, error)

NewBlockFromReader returns a new instance of a bitcoin block given a Reader to deserialize the block. See Block.

func (*Block) Bytes

func (b *Block) Bytes() ([]byte, error)

Bytes returns the serialized bytes for the Block. This is equivalent to calling Serialize on the underlying wire.MsgBlock, however it caches the result so subsequent calls are more efficient.

func (*Block) Hash

func (b *Block) Hash() *chainhash.Hash

Hash returns the block identifier hash for the Block. This is equivalent to calling BlockHash on the underlying wire.MsgBlock, however it caches the result so subsequent calls are more efficient.

func (*Block) Height

func (b *Block) Height() int32

Height returns the saved height of the block in the block chain. This value will be BlockHeightUnknown if it hasn't already explicitly been set.

func (*Block) MsgBlock

func (b *Block) MsgBlock() *wire.MsgBlock

MsgBlock returns the underlying wire.MsgBlock for the Block.

func (*Block) SetHeight

func (b *Block) SetHeight(height int32)

SetHeight sets the height of the block in the block chain.

func (*Block) Transactions

func (b *Block) Transactions() []*Tx

Transactions returns a slice of wrapped transactions (btcutil.Tx) for all transactions in the Block. This is nearly equivalent to accessing the raw transactions (wire.MsgTx) in the underlying wire.MsgBlock, however it instead provides easy access to wrapped versions (btcutil.Tx) of them.

func (*Block) Tx

func (b *Block) Tx(txNum int) (*Tx, error)

Tx returns a wrapped transaction (btcutil.Tx) for the transaction at the specified index in the Block. The supplied index is 0 based. That is to say, the first transaction in the block is txNum 0. This is nearly equivalent to accessing the raw transaction (wire.MsgTx) from the underlying wire.MsgBlock, however the wrapped transaction has some helpful properties such as caching the hash so subsequent calls are more efficient.

func (*Block) TxHash

func (b *Block) TxHash(txNum int) (*chainhash.Hash, error)

TxHash returns the hash for the requested transaction number in the Block. The supplied index is 0 based. That is to say, the first transaction in the block is txNum 0. This is equivalent to calling TxHash on the underlying wire.MsgTx, however it caches the result so subsequent calls are more efficient.

func (*Block) TxLoc

func (b *Block) TxLoc() ([]wire.TxLoc, error)

TxLoc returns the offsets and lengths of each transaction in a raw block. It is used to allow fast indexing into transactions within the raw byte stream.

type CashAddressPubKeyHash

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

AddressPubKeyHash is an Address for a pay-to-pubkey-hash (P2PKH) transaction.

func NewCashAddressPubKeyHash

func NewCashAddressPubKeyHash(pkHash []byte, net *chaincfg.Params) (*CashAddressPubKeyHash, error)

NewAddressPubKeyHash returns a new AddressPubKeyHash. pkHash mustbe 20 bytes.

func (*CashAddressPubKeyHash) EncodeAddress

func (a *CashAddressPubKeyHash) EncodeAddress() string

EncodeAddress returns the string encoding of a pay-to-pubkey-hash address. Part of the Address interface.

func (*CashAddressPubKeyHash) Hash160

func (a *CashAddressPubKeyHash) Hash160() *[ripemd160.Size]byte

Hash160 returns the underlying array of the pubkey hash. This can be useful when an array is more appropiate than a slice (for example, when used as map keys).

func (*CashAddressPubKeyHash) IsForNet

func (a *CashAddressPubKeyHash) IsForNet(net *chaincfg.Params) bool

IsForNet returns whether or not the pay-to-pubkey-hash address is associated with the passed bitcoin cash network.

func (*CashAddressPubKeyHash) ScriptAddress

func (a *CashAddressPubKeyHash) ScriptAddress() []byte

ScriptAddress returns the bytes to be included in a txout script to pay to a pubkey hash. Part of the Address interface.

func (*CashAddressPubKeyHash) String

func (a *CashAddressPubKeyHash) String() string

String returns a human-readable string for the pay-to-pubkey-hash address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.

type CashAddressScriptHash

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

AddressScriptHash is an Address for a pay-to-script-hash (P2SH) transaction.

func NewCashAddressScriptHash

func NewCashAddressScriptHash(serializedScript []byte, net *chaincfg.Params) (*CashAddressScriptHash, error)

NewAddressScriptHash returns a new AddressScriptHash.

func NewCashAddressScriptHashFromHash

func NewCashAddressScriptHashFromHash(scriptHash []byte, net *chaincfg.Params) (*CashAddressScriptHash, error)

NewAddressScriptHashFromHash returns a new AddressScriptHash. scriptHash must be 20 bytes.

func (*CashAddressScriptHash) EncodeAddress

func (a *CashAddressScriptHash) EncodeAddress() string

EncodeAddress returns the string encoding of a pay-to-script-hash address. Part of the Address interface.

func (*CashAddressScriptHash) Hash160

func (a *CashAddressScriptHash) Hash160() *[ripemd160.Size]byte

Hash160 returns the underlying array of the script hash. This can be useful when an array is more appropiate than a slice (for example, when used as map keys).

func (*CashAddressScriptHash) IsForNet

func (a *CashAddressScriptHash) IsForNet(net *chaincfg.Params) bool

IsForNet returns whether or not the pay-to-script-hash address is associated with the passed bitcoin cash network.

func (*CashAddressScriptHash) ScriptAddress

func (a *CashAddressScriptHash) ScriptAddress() []byte

ScriptAddress returns the bytes to be included in a txout script to pay to a script hash. Part of the Address interface.

func (*CashAddressScriptHash) String

func (a *CashAddressScriptHash) String() string

String returns a human-readable string for the pay-to-script-hash address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.

type OutOfRangeError

type OutOfRangeError string

OutOfRangeError describes an error due to accessing an element that is out of range.

func (OutOfRangeError) Error

func (e OutOfRangeError) Error() string

Error satisfies the error interface and prints human-readable errors.

type SlpAddressPubKeyHash

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

AddressPubKeyHash is an Address for a pay-to-pubkey-hash (P2PKH) transaction.

func NewSlpAddressPubKeyHash

func NewSlpAddressPubKeyHash(pkHash []byte, net *chaincfg.Params) (*SlpAddressPubKeyHash, error)

NewAddressPubKeyHash returns a new AddressPubKeyHash. pkHash mustbe 20 bytes.

func (*SlpAddressPubKeyHash) EncodeAddress

func (a *SlpAddressPubKeyHash) EncodeAddress() string

EncodeAddress returns the string encoding of a pay-to-pubkey-hash address. Part of the Address interface.

func (*SlpAddressPubKeyHash) Hash160

func (a *SlpAddressPubKeyHash) Hash160() *[ripemd160.Size]byte

Hash160 returns the underlying array of the pubkey hash. This can be useful when an array is more appropiate than a slice (for example, when used as map keys).

func (*SlpAddressPubKeyHash) IsForNet

func (a *SlpAddressPubKeyHash) IsForNet(net *chaincfg.Params) bool

IsForNet returns whether or not the pay-to-pubkey-hash address is associated with the passed bitcoin cash network.

func (*SlpAddressPubKeyHash) ScriptAddress

func (a *SlpAddressPubKeyHash) ScriptAddress() []byte

ScriptAddress returns the bytes to be included in a txout script to pay to a pubkey hash. Part of the Address interface.

func (*SlpAddressPubKeyHash) String

func (a *SlpAddressPubKeyHash) String() string

String returns a human-readable string for the pay-to-pubkey-hash address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.

type SlpAddressScriptHash

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

AddressScriptHash is an Address for a pay-to-script-hash (P2SH) transaction.

func NewSlpAddressScriptHash

func NewSlpAddressScriptHash(serializedScript []byte, net *chaincfg.Params) (*SlpAddressScriptHash, error)

NewAddressScriptHash returns a new AddressScriptHash.

func NewSlpAddressScriptHashFromHash

func NewSlpAddressScriptHashFromHash(scriptHash []byte, net *chaincfg.Params) (*SlpAddressScriptHash, error)

NewAddressScriptHashFromHash returns a new AddressScriptHash. scriptHash must be 20 bytes.

func (*SlpAddressScriptHash) EncodeAddress

func (a *SlpAddressScriptHash) EncodeAddress() string

EncodeAddress returns the string encoding of a pay-to-script-hash address. Part of the Address interface.

func (*SlpAddressScriptHash) Hash160

func (a *SlpAddressScriptHash) Hash160() *[ripemd160.Size]byte

Hash160 returns the underlying array of the script hash. This can be useful when an array is more appropiate than a slice (for example, when used as map keys).

func (*SlpAddressScriptHash) IsForNet

func (a *SlpAddressScriptHash) IsForNet(net *chaincfg.Params) bool

IsForNet returns whether or not the pay-to-script-hash address is associated with the passed bitcoin cash network.

func (*SlpAddressScriptHash) ScriptAddress

func (a *SlpAddressScriptHash) ScriptAddress() []byte

ScriptAddress returns the bytes to be included in a txout script to pay to a script hash. Part of the Address interface.

func (*SlpAddressScriptHash) String

func (a *SlpAddressScriptHash) String() string

String returns a human-readable string for the pay-to-script-hash address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.

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 memoizes 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 UnsupportedWitnessProgLenError

type UnsupportedWitnessProgLenError int

UnsupportedWitnessProgLenError describes an error where a segwit address being decoded has an unsupported witness program length.

func (UnsupportedWitnessProgLenError) Error

type UnsupportedWitnessVerError

type UnsupportedWitnessVerError byte

UnsupportedWitnessVerError describes an error where a segwit address being decoded has an unsupported witness version.

func (UnsupportedWitnessVerError) Error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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