bitcoin

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2023 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const SighashForkID = txscript.SigHashType(0x40)
View Source
const SighashMask = txscript.SigHashType(0x1F)
View Source
const TxVersion int32 = 2

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 (
	// default timeout for client
	DefaultClientTimeout = time.Minute
	// default retry period for failed connections
	DefaultClientTimeoutRetry = 10 * time.Second
	// default host to connect to rpc node
	DefaultClientHost = "http://0.0.0.0:18443"
	// default user for rpc connection
	DefaultClientUser = "user"
	// default password for rpc connection
	DefaultClientPassword = "password"
	// default auth header for rpc connection
	DefaultClientAuthHeader = ""
)

Functions

func AddressPrefix

func AddressPrefix(params *chaincfg.Params) string

The bch prefix is different for each network type

func AppendBchChecksum

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

Add bch checksum to a payload https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/cashaddr.md#checksum

func BchAddressFromBytes

func BchAddressFromBytes(addrBytes []byte, params *chaincfg.Params) (btcutil.Address, error)

func CalculateBchBip143Sighash

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

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

func CheckError

func CheckError(err error) xc.ClientError

func DecodeBchAddress

func DecodeBchAddress(addr string, params *chaincfg.Params) ([]byte, error)

DecodeAddress implements the address.Decoder interface

func DecodeBchString

func DecodeBchString(address string) []byte

DecodeString using Bitcoin Cash address encoding.

func EncodeToBchString

func EncodeToBchString(data []byte) string

White bch data as a bch encoded string

func GetParams

func GetParams(cfg *xc.AssetConfig) (*chaincfg.Params, error)

func NewAddressBuilder

func NewAddressBuilder(asset xc.ITask) (xc.AddressBuilder, error)

NewAddressBuilder creates a new Bitcoin AddressBuilder

func NewBchAddressPubKey

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

Create a new bch, btc compatible address pkh

func NewSigner

func NewSigner(asset xc.ITask) (xc.Signer, error)

NewSigner creates a new Bitcoin Signer

func NewTxBuilder

func NewTxBuilder(cfgI xc.ITask) (xc.TxBuilder, error)

NewTxBuilder creates a new Bitcoin TxBuilder

func PolyMod

func PolyMod(v []byte) uint64

PolyMod is the checksum alg for BCH https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/cashaddr.md

func VerifyChecksum

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

Verify if a bch payload is well formed

Types

type AddressBuilder

type AddressBuilder struct {
	UseLegacy     bool
	UseScriptHash bool
	// contains filtered or unexported fields
}

AddressBuilder for Bitcoin

func (AddressBuilder) GetAddressFromPublicKey

func (ab AddressBuilder) GetAddressFromPublicKey(publicKeyBytes []byte) (xc.Address, error)

GetAddressFromPublicKey returns an Address given a public key

func (AddressBuilder) GetAllPossibleAddressesFromPublicKey

func (ab AddressBuilder) GetAllPossibleAddressesFromPublicKey(publicKeyBytes []byte) ([]xc.PossibleAddress, error)

GetAllPossibleAddressesFromPublicKey returns all PossubleAddress(es) given a public key

type BlockchairClient

type BlockchairClient struct {
	Asset           *xc.AssetConfig
	EstimateGasFunc xc.EstimateGasFunc
	// contains filtered or unexported fields
}

Client for Bitcoin

func NewBlockchairClient

func NewBlockchairClient(cfgI xc.ITask) (*BlockchairClient, error)

NewClient returns a new Bitcoin Client

func (*BlockchairClient) EstimateGas

func (client *BlockchairClient) EstimateGas(ctx context.Context) (xc.AmountBlockchain, error)

func (*BlockchairClient) EstimateGasFee

func (client *BlockchairClient) EstimateGasFee(ctx context.Context, numBlocks int64) (float64, error)

func (*BlockchairClient) FetchBalance

func (client *BlockchairClient) FetchBalance(ctx context.Context, address xc.Address) (xc.AmountBlockchain, error)

func (*BlockchairClient) FetchNativeBalance

func (client *BlockchairClient) FetchNativeBalance(ctx context.Context, address xc.Address) (xc.AmountBlockchain, error)

func (*BlockchairClient) FetchTxInfo

func (client *BlockchairClient) FetchTxInfo(ctx context.Context, txHash xc.TxHash) (xc.TxInfo, error)

func (*BlockchairClient) FetchTxInput

func (client *BlockchairClient) FetchTxInput(ctx context.Context, from xc.Address, to xc.Address) (xc.TxInput, error)

FetchTxInput returns tx input for a Bitcoin tx

func (*BlockchairClient) LatestBlock

func (client *BlockchairClient) LatestBlock(ctx context.Context) (uint64, error)

func (*BlockchairClient) RegisterEstimateGasCallback

func (client *BlockchairClient) RegisterEstimateGasCallback(estimateGas xc.EstimateGasFunc)

EstimateGas(ctx context.Context) (AmountBlockchain, error)

func (*BlockchairClient) SubmitTx

func (client *BlockchairClient) SubmitTx(ctx context.Context, tx xc.Tx) error

func (*BlockchairClient) UnspentOutputs

func (client *BlockchairClient) UnspentOutputs(ctx context.Context, minConf, maxConf int64, addr xc.Address) ([]Output, error)

type BlockchairContext

type BlockchairContext struct {
	Code  int32  `json:"code"` // 200 = ok
	Error string `json:"error,omitempty"`
	State int64  `json:"state"` // to count confirmations
}

type ClientOptions

type ClientOptions struct {
	Timeout         time.Duration
	TimeoutRetry    time.Duration
	Host            string
	User            string
	Password        string
	AuthHeader      string
	AuthHeaderValue string
	Chaincfg        *chaincfg.Params
}

ClientOptions are used to parameterise the behaviour of the Client.

func DefaultClientOptions

func DefaultClientOptions() ClientOptions

type Input

type Input struct {
	Output    `json:"output"`
	SigScript []byte     `json:"sigScript,omitempty"`
	Address   xc.Address `json:"address,omitempty"`
}

type NativeClient

type NativeClient struct {
	Asset           *xc.AssetConfig
	EstimateGasFunc xc.EstimateGasFunc
	// contains filtered or unexported fields
}

Client for Bitcoin

func NewNativeClient

func NewNativeClient(cfgI xc.ITask) (*NativeClient, error)

NewClient returns a new Bitcoin Client

func (*NativeClient) EstimateFeeLegacy

func (client *NativeClient) EstimateFeeLegacy(ctx context.Context, numBlocks int64) (float64, error)

Older version of estimating fee for some forks of BTC (e.g. BCH).

func (*NativeClient) EstimateGas

func (client *NativeClient) EstimateGas(ctx context.Context) (xc.AmountBlockchain, error)

func (*NativeClient) EstimateSmartFee

func (client *NativeClient) EstimateSmartFee(ctx context.Context, numBlocks int64) (float64, error)

Latest way to estimate fees on BTC

func (*NativeClient) FetchBalance

func (client *NativeClient) FetchBalance(ctx context.Context, address xc.Address) (xc.AmountBlockchain, error)

func (*NativeClient) FetchNativeBalance

func (client *NativeClient) FetchNativeBalance(ctx context.Context, address xc.Address) (xc.AmountBlockchain, error)

func (*NativeClient) FetchTxInfo

func (client *NativeClient) FetchTxInfo(ctx context.Context, txHash xc.TxHash) (xc.TxInfo, error)

FetchTxInfo returns tx info for a Bitcoin tx

func (*NativeClient) FetchTxInput

func (client *NativeClient) FetchTxInput(ctx context.Context, from xc.Address, to xc.Address) (xc.TxInput, error)

FetchTxInput returns tx input for a Bitcoin tx

func (*NativeClient) GetWalletInfo

func (client *NativeClient) GetWalletInfo(ctx context.Context) (float64, error)

func (*NativeClient) ImportAddress

func (client *NativeClient) ImportAddress(ctx context.Context, addr address.Address, label string, rescan bool) error

Import an address into the RPC node to be tracked.

func (*NativeClient) LatestBlock

func (client *NativeClient) LatestBlock(ctx context.Context) (uint64, error)

LatestBlock returns the height of the longest blockchain.

func (*NativeClient) Output

func (client *NativeClient) Output(ctx context.Context, outpoint Outpoint) (Output, uint64, error)

Output associated with an outpoint, and its number of confirmations.

func (*NativeClient) RegisterEstimateGasCallback

func (client *NativeClient) RegisterEstimateGasCallback(estimateGas xc.EstimateGasFunc)

EstimateGas(ctx context.Context) (AmountBlockchain, error)

func (*NativeClient) SubmitTx

func (client *NativeClient) SubmitTx(ctx context.Context, txInput xc.Tx) error

SubmitTx submits a Bitcoin tx

func (*NativeClient) UnspentOutputs

func (client *NativeClient) UnspentOutputs(ctx context.Context, minConf, maxConf int64, addr xc.Address) ([]Output, error)

UnspentOutputs spendable by the given address.

type Network

type Network string

UTXO chains have mainnet, testnet, and regtest/devnet network types built in.

const Mainnet Network = "mainnet"
const Regtest Network = "regtest"
const Testnet Network = "testnet"

type NetworkTriple

type NetworkTriple struct {
	Mainnet *chaincfg.Params
	Testnet *chaincfg.Params
	Regtest *chaincfg.Params
}
var DogeNetworks *NetworkTriple = &NetworkTriple{
	Mainnet: &chaincfg.Params{
		Name: "mainnet",
		Net:  0xc0c0c0c0,

		PubKeyHashAddrID: 30,
		ScriptHashAddrID: 22,
		PrivateKeyID:     158,

		HDPrivateKeyID: [4]byte{0x02, 0xfa, 0xc3, 0x98},
		HDPublicKeyID:  [4]byte{0x02, 0xfa, 0xca, 0xfd},

		Bech32HRPSegwit: "doge",
	},
	Testnet: &chaincfg.Params{
		Name: "testnet",
		Net:  0xfcc1b7dc,

		PubKeyHashAddrID: 113,
		ScriptHashAddrID: 196,
		PrivateKeyID:     241,

		HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94},
		HDPublicKeyID:  [4]byte{0x04, 0x35, 0x87, 0xcf},

		Bech32HRPSegwit: "doget",
	},
	Regtest: &chaincfg.Params{
		Name: "regtest",

		Net: 0xfabfb5da,

		PubKeyHashAddrID: 111,
		ScriptHashAddrID: 196,
		PrivateKeyID:     239,

		HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94},
		HDPublicKeyID:  [4]byte{0x04, 0x35, 0x87, 0xcf},

		Bech32HRPSegwit: "dogert",
	},
}
var LtcNetworks *NetworkTriple = &NetworkTriple{
	Mainnet: &chaincfg.Params{
		Name: "mainnet",
		Net:  0xfbc0b6db,

		PubKeyHashAddrID: 48,
		ScriptHashAddrID: 50,
		PrivateKeyID:     176,

		HDPrivateKeyID: [4]byte{0x04, 0x88, 0xAD, 0xE4},
		HDPublicKeyID:  [4]byte{0x04, 0x88, 0xB2, 0x1E},

		Bech32HRPSegwit: "ltc",
	},
	Testnet: &chaincfg.Params{
		Name: "testnet",
		Net:  0xfdd2c8f1,

		PubKeyHashAddrID: 111,
		ScriptHashAddrID: 196,
		PrivateKeyID:     239,

		HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94},
		HDPublicKeyID:  [4]byte{0x04, 0x35, 0x87, 0xCF},

		Bech32HRPSegwit: "tltc",
	},
	Regtest: &chaincfg.Params{
		Name: "regtest",

		Net: 0xfabfb5da,

		PubKeyHashAddrID: 111,
		ScriptHashAddrID: 196,
		PrivateKeyID:     239,

		HDPrivateKeyID: [4]byte{0x04, 0x35, 0x83, 0x94},
		HDPublicKeyID:  [4]byte{0x04, 0x35, 0x87, 0xcf},

		Bech32HRPSegwit: "rltc",
	},
}

func (*NetworkTriple) GetParams

func (n *NetworkTriple) GetParams(network string) *chaincfg.Params

type Outpoint

type Outpoint struct {
	Hash  []byte `json:"hash"`
	Index uint32 `json:"index"`
}

A specific output from a transaction

type Output

type Output struct {
	Outpoint     `json:"outpoint"`
	Value        xc.AmountBlockchain `json:"value"`
	PubKeyScript []byte              `json:"pubKeyScript"`
}

type Recipient

type Recipient struct {
	To    xc.Address          `json:"to"`
	Value xc.AmountBlockchain `json:"value"`
}

type Signer

type Signer struct {
}

Signer for Bitcoin

func (*Signer) ImportPrivateKey

func (signer *Signer) ImportPrivateKey(privateKey string) (xc.PrivateKey, error)

ImportPrivateKey imports a Bitcoin private key This private key should be either:

  • a 32 byte hex string representing a k256 private key
  • a WIF string representing a k256 private key

func (*Signer) Sign

func (signer *Signer) Sign(privateKeyBytes xc.PrivateKey, data xc.TxDataToSign) (xc.TxSignature, error)

Sign a Bitcoin tx

type Tx

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

Tx for Bitcoin

func (*Tx) AddSignatures

func (tx *Tx) AddSignatures(signatures ...xc.TxSignature) error

AddSignatures adds a signature to Tx

func (*Tx) DetectFrom

func (tx *Tx) DetectFrom() (string, xc.AmountBlockchain)

Heuristic to determine the sender of a transaction by using the largest utxo input and taking it's spender.

func (*Tx) DetectToAndAmount

func (tx *Tx) DetectToAndAmount(from string, expectedTo string) (string, xc.AmountBlockchain, xc.AmountBlockchain)

func (*Tx) Hash

func (tx *Tx) Hash() xc.TxHash

Hash returns the tx hash or id

func (*Tx) IsBch

func (tx *Tx) IsBch() bool

func (*Tx) Outputs

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

Outputs returns the UTXO outputs in the underlying transaction.

func (*Tx) Serialize

func (tx *Tx) Serialize() ([]byte, error)

func (*Tx) Sighashes

func (tx *Tx) Sighashes() ([]xc.TxDataToSign, error)

Sighashes returns the tx payload to sign, aka sighash

type TxBuilder

type TxBuilder struct {
	Asset  *xc.AssetConfig
	Params *chaincfg.Params
	// contains filtered or unexported fields
}

TxBuilder for Bitcoin

func (TxBuilder) NewNativeTransfer

func (txBuilder TxBuilder) NewNativeTransfer(from xc.Address, to xc.Address, amount xc.AmountBlockchain, input xc.TxInput) (xc.Tx, error)

NewNativeTransfer creates a new transfer for a native asset

func (TxBuilder) NewTokenTransfer

func (txBuilder TxBuilder) NewTokenTransfer(from xc.Address, to xc.Address, amount xc.AmountBlockchain, input xc.TxInput) (xc.Tx, error)

NewTokenTransfer creates a new transfer for a token asset

func (TxBuilder) NewTransfer

func (txBuilder TxBuilder) NewTransfer(from xc.Address, to xc.Address, amount xc.AmountBlockchain, input xc.TxInput) (xc.Tx, error)

NewTransfer creates a new transfer for an Asset, either native or token

type TxInput

type TxInput struct {
	xc.TxInputEnvelope
	UnspentOutputs  []Output            `json:"unspent_outputs"`
	Inputs          []Input             `json:"input"`
	FromPublicKey   []byte              `json:"from_public_key"`
	GasPricePerByte xc.AmountBlockchain `json:"gas_price_per_byte"`
}

TxInput for Bitcoin

func NewTxInput

func NewTxInput() *TxInput

NewTxInput returns a new Bitcoin TxInput

func (*TxInput) GetGetPricePerByte

func (txInput *TxInput) GetGetPricePerByte() xc.AmountBlockchain

func (*TxInput) SetPublicKey

func (txInput *TxInput) SetPublicKey(publicKeyBytes xc.PublicKey) error

func (*TxInput) SetPublicKeyFromStr

func (txInput *TxInput) SetPublicKeyFromStr(publicKeyStr string) error

Jump to

Keyboard shortcuts

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