bitcoincash

package
v0.2.15 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2020 License: GPL-3.0 Imports: 21 Imported by: 4

Documentation

Index

Constants

View Source
const SighashForkID = txscript.SigHashType(0x40)

SighashForkID used to distinguish between Bitcoin Cash and Bitcoin transactions by masking hash types.

View Source
const SighashMask = txscript.SigHashType(0x1F)

SighashMask used to mask hash types.

View Source
const Version int32 = 1

Version of Bitcoin Cash transactions supported by the multichain.

Variables

View Source
var (
	// Alphabet used by Bitcoin Cash to encode addresses.
	Alphabet = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
	// AlphabetReverseLookup used by Bitcoin Cash to decode addresses.
	AlphabetReverseLookup = func() map[rune]byte {
		lookup := map[rune]byte{}
		for i, char := range Alphabet {
			lookup[char] = byte(i)
		}
		return lookup
	}()
)
View Source
var NewClient = bitcoin.NewClient

NewClient returns a new Client.

Functions

func AddressPrefix

func AddressPrefix(params *chaincfg.Params) string

AddressPrefix returns the string representations of an address prefix based on the network parameters: "bitcoincash" (for mainnet), "bchtest" (for testnet), and "bchreg" (for regtest). This function panics if the network parameters are not recognised.

func AppendChecksum

func AppendChecksum(prefix string, payload []byte) []byte

AppendChecksum to the data payload.

https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/cashaddr.md#checksum

func CalculateBip143Sighash

func CalculateBip143Sighash(subScript []byte, sigHashes *txscript.TxSigHashes, hashType txscript.SigHashType, tx *wire.MsgTx, idx int, amt int64) []byte

CalculateBip143Sighash computes the sighash digest of a transaction's input using the new, optimized digest calculation algorithm defined in BIP0143. This function makes use of pre-calculated sighash fragments stored within the passed HashCache to eliminate duplicate hashing computations when calculating the final digest, reducing the complexity from O(N^2) to O(N). Additionally, signatures now cover the input value of the referenced unspent output. This allows offline, or hardware wallets to compute the exact amount being spent, in addition to the final transaction fee. In the case the wallet if fed an invalid input amount, the real sighash will differ causing the produced signature to be invalid.

https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki

func DecodeString

func DecodeString(address string) []byte

DecodeString using Bitcoin Cash address encoding.

func EncodePrefix

func EncodePrefix(prefixString string) []byte

EncodePrefix string into bytes.

https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/cashaddr.md#checksum

func EncodeToString

func EncodeToString(data []byte) string

EncodeToString using Bitcoin Cash address encoding, assuming that the data has a prefix and checksum.

func NewTxBuilder

func NewTxBuilder(params *chaincfg.Params) utxo.TxBuilder

NewTxBuilder returns an implementation of the transaction builder interface from the Bitcoin Compat API, and exposes the functionality to build simple Bitcoin Cash transactions.

func PolyMod

func PolyMod(v []byte) uint64

PolyMod is used to calculate the checksum for Bitcoin Cash addresses.

uint64_t PolyMod(const data &v) {
    uint64_t c = 1;
    for (uint8_t d : v) {
        uint8_t c0 = c >> 35;
        c = ((c & 0x07ffffffff) << 5) ^ d;
        if (c0 & 0x01) c ^= 0x98f2bc8e61;
        if (c0 & 0x02) c ^= 0x79b76d99e2;
        if (c0 & 0x04) c ^= 0xf33e5fb3c4;
        if (c0 & 0x08) c ^= 0xae2eabe2a8;
        if (c0 & 0x10) c ^= 0x1e4f43e470;
    }
    return c ^ 1;
}

https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/cashaddr.md

func VerifyChecksum added in v0.2.0

func VerifyChecksum(prefix string, payload []byte) bool

VerifyChecksum verifies whether the given payload is well-formed.

https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/cashaddr.md#checksum

Types

type Address added in v0.2.0

type Address interface {
	btcutil.Address
	BitcoinAddress() btcutil.Address
}

An Address represents a Bitcoin Cash address.

type AddressDecoder added in v0.2.8

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

AddressDecoder encapsulates the chain specific configurations and implements the address.Decoder interface

func NewAddressDecoder added in v0.2.8

func NewAddressDecoder(params *chaincfg.Params) AddressDecoder

NewAddressDecoder constructs a new AddressDecoder with the chain specific configurations

func (AddressDecoder) DecodeAddress added in v0.2.8

func (decoder AddressDecoder) DecodeAddress(addr address.Address) (address.RawAddress, error)

DecodeAddress implements the address.Decoder interface

type AddressEncodeDecoder added in v0.2.8

type AddressEncodeDecoder struct {
	AddressEncoder
	AddressDecoder
}

AddressEncodeDecoder implements the address.EncodeDecoder interface

func NewAddressEncodeDecoder added in v0.2.8

func NewAddressEncodeDecoder(params *chaincfg.Params) AddressEncodeDecoder

NewAddressEncodeDecoder constructs a new AddressEncodeDecoder with the chain specific configurations

type AddressEncoder added in v0.2.8

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

AddressEncoder encapsulates the chain specific configurations and implements the address.Encoder interface

func NewAddressEncoder added in v0.2.8

func NewAddressEncoder(params *chaincfg.Params) AddressEncoder

NewAddressEncoder constructs a new AddressEncoder with the chain specific configurations

func (AddressEncoder) EncodeAddress added in v0.2.8

func (encoder AddressEncoder) EncodeAddress(rawAddr address.RawAddress) (address.Address, error)

EncodeAddress implements the address.Encoder interface

type AddressLegacy added in v0.2.0

type AddressLegacy struct {
	btcutil.Address
}

AddressLegacy represents a legacy Bitcoin address.

func (AddressLegacy) BitcoinAddress added in v0.2.0

func (addr AddressLegacy) BitcoinAddress() btcutil.Address

BitcoinAddress returns the address as if it was a Bitcoin address.

type AddressPubKeyHash

type AddressPubKeyHash struct {
	*btcutil.AddressPubKeyHash
	// contains filtered or unexported fields
}

AddressPubKeyHash represents an address for P2PKH transactions for Bitcoin Cash that is compatible with the Bitcoin-compat API.

func NewAddressPubKey

func NewAddressPubKey(pk []byte, params *chaincfg.Params) (AddressPubKeyHash, error)

NewAddressPubKey returns a new AddressPubKey that is compatible with the Bitcoin-compat API.

func NewAddressPubKeyHash

func NewAddressPubKeyHash(pkh []byte, params *chaincfg.Params) (AddressPubKeyHash, error)

NewAddressPubKeyHash returns a new AddressPubKeyHash that is compatible with the Bitcoin-compat API.

func (AddressPubKeyHash) BitcoinAddress added in v0.2.0

func (addr AddressPubKeyHash) BitcoinAddress() btcutil.Address

BitcoinAddress returns the address as if it was a Bitcoin address.

func (AddressPubKeyHash) EncodeAddress

func (addr AddressPubKeyHash) EncodeAddress() string

EncodeAddress returns the string encoding of the payment address associated with the Address value. See the comment on String for how this method differs from String.

func (AddressPubKeyHash) IsForNet

func (addr AddressPubKeyHash) IsForNet(params *chaincfg.Params) bool

IsForNet returns whether or not the address is associated with the passed bitcoin network.

func (AddressPubKeyHash) ScriptAddress

func (addr AddressPubKeyHash) ScriptAddress() []byte

ScriptAddress returns the raw bytes of the address to be used when inserting the address into a txout's script.

func (AddressPubKeyHash) String

func (addr AddressPubKeyHash) String() string

String returns the string encoding of the transaction output destination.

Please note that String differs subtly from EncodeAddress: String will return the value as a string without any conversion, while EncodeAddress may convert destination types (for example, converting pubkeys to P2PKH addresses) before encoding as a payment address string.

type AddressScriptHash added in v0.2.0

type AddressScriptHash struct {
	*btcutil.AddressScriptHash
	// contains filtered or unexported fields
}

AddressScriptHash represents an address for P2SH transactions for Bitcoin Cash that is compatible with the Bitcoin-compat API.

func NewAddressScriptHash added in v0.2.0

func NewAddressScriptHash(script []byte, params *chaincfg.Params) (AddressScriptHash, error)

NewAddressScriptHash returns a new AddressScriptHash that is compatible with the Bitcoin-compat API.

func NewAddressScriptHashFromHash added in v0.2.0

func NewAddressScriptHashFromHash(scriptHash []byte, params *chaincfg.Params) (AddressScriptHash, error)

NewAddressScriptHashFromHash returns a new AddressScriptHash that is compatible with the Bitcoin-compat API.

func (AddressScriptHash) BitcoinAddress added in v0.2.0

func (addr AddressScriptHash) BitcoinAddress() btcutil.Address

BitcoinAddress returns the address as if it was a Bitcoin address.

func (AddressScriptHash) EncodeAddress added in v0.2.0

func (addr AddressScriptHash) EncodeAddress() string

EncodeAddress returns the string encoding of the payment address associated with the Address value. See the comment on String for how this method differs from String.

func (AddressScriptHash) IsForNet added in v0.2.0

func (addr AddressScriptHash) IsForNet(params *chaincfg.Params) bool

IsForNet returns whether or not the address is associated with the passed bitcoin network.

func (AddressScriptHash) ScriptAddress added in v0.2.0

func (addr AddressScriptHash) ScriptAddress() []byte

ScriptAddress returns the raw bytes of the address to be used when inserting the address into a txout's script.

func (AddressScriptHash) String added in v0.2.0

func (addr AddressScriptHash) String() string

String returns the string encoding of the transaction output destination.

Please note that String differs subtly from EncodeAddress: String will return the value as a string without any conversion, while EncodeAddress may convert destination types (for example, converting pubkeys to P2PKH addresses) before encoding as a payment address string.

type Client added in v0.2.0

type Client = bitcoin.Client

A Client interacts with an instance of the Bitcoin network using the RPC interface exposed by a Bitcoin node.

type ClientOptions added in v0.2.0

type ClientOptions = bitcoin.ClientOptions

ClientOptions are used to parameterise the behaviour of the Client.

func DefaultClientOptions added in v0.2.0

func DefaultClientOptions() ClientOptions

DefaultClientOptions returns ClientOptions with the default settings. These settings are valid for use with the default local deployment of the multichain. In production, the host, user, and password should be changed.

type GasEstimator added in v0.2.0

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

A GasEstimator returns the SATs-per-byte that is needed in order to confirm transactions with an estimated maximum delay of one block. In distributed networks that collectively build, sign, and submit transactions, it is important that all nodes in the network have reached consensus on the SATs-per-byte.

func NewGasEstimator added in v0.2.0

func NewGasEstimator(client Client, fallbackGas pack.U256) GasEstimator

NewGasEstimator returns a simple gas estimator that always returns the given number of SATs-per-byte.

func (GasEstimator) EstimateGas added in v0.2.13

func (gasEstimator GasEstimator) EstimateGas(ctx context.Context) (pack.U256, pack.U256, error)

EstimateGas returns the number of SATs-per-byte (for both price and cap) that is needed in order to confirm transactions with a minimal delay. It is the responsibility of the caller to know the number of bytes in their transaction. This method calls the `estimatefee` RPC call to the node, which based on a conservative (considering longer history) strategy returns the estimated BCH per kilobyte of data in the transaction. An error will be returned if the node hasn't observed enough blocks to make an estimate.

type Tx

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

Tx represents a simple Bitcoin Cash transaction that implements the Bitcoin Compat API.

func (*Tx) Hash

func (tx *Tx) Hash() (pack.Bytes, error)

Hash returns the transaction hash of the given underlying transaction. It implements the multichain.UTXOTx interface

func (*Tx) Inputs added in v0.2.0

func (tx *Tx) Inputs() ([]utxo.Input, error)

Inputs returns the UTXO inputs in the underlying transaction. It implements the multichain.UTXOTx interface

func (*Tx) Outputs added in v0.2.0

func (tx *Tx) Outputs() ([]utxo.Output, error)

Outputs returns the UTXO outputs in the underlying transaction. It implements the multichain.UTXOTx interface

func (*Tx) Serialize

func (tx *Tx) Serialize() (pack.Bytes, error)

Serialize serializes the UTXO transaction to bytes

func (*Tx) Sighashes

func (tx *Tx) Sighashes() ([]pack.Bytes32, error)

Sighashes returns the digests that must be signed before the transaction can be submitted by the client.

func (*Tx) Sign

func (tx *Tx) Sign(signatures []pack.Bytes65, pubKey pack.Bytes) error

Sign consumes a list of signatures, and adds them to the list of UTXOs in the underlying transactions. It implements the multichain.UTXOTx interface

type TxBuilder added in v0.2.0

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

The TxBuilder is an implementation of a UTXO-compatible transaction builder for Bitcoin.

func (TxBuilder) BuildTx added in v0.2.0

func (txBuilder TxBuilder) BuildTx(inputs []utxo.Input, recipients []utxo.Recipient) (utxo.Tx, error)

BuildTx returns a simple Bitcoin Cash transaction that consumes the funds from the given outputs, and sends the to the given recipients. The difference in the sum value of the inputs and the sum value of the recipients is paid as a fee to the Bitcoin Cash network.

It is assumed that the required signature scripts require the SIGHASH_ALL signatures and the serialized public key:

builder := txscript.NewScriptBuilder()
builder.AddData(append(signature.Serialize(), byte(txscript.SigHashAll|SighashForkID)))
builder.AddData(serializedPubKey)

Outputs produced for recipients will use P2PKH, or P2SH scripts as the pubkey script, based on the format of the recipient address.

Jump to

Keyboard shortcuts

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