btc

package
v0.0.0-...-aec784a Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package btc provides ability to send and get anchors, that brings an abstraction layer between the Gateway (package gw) and Bitcoin implementations.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDryRun               = errors.New("")
	ErrFailedToExec         = errors.New("ErrFailedToExec")
	ErrFailedToDecode       = errors.New("ErrFailedToDecode")
	ErrInvalidOpReturn      = errors.New("ErrInvalidOpReturn")
	ErrInconsistentBTCNet   = errors.New("ErrInconsistentBTCNet")
	ErrUnsupportedVersion   = errors.New("ErrUnsupportedVersion")
	ErrUnexpectedExitCode   = errors.New("ErrUnexpectedExitCode")
	ErrExitCode1            = errors.New("ErrExitCode1")
	ErrPingFailed           = errors.New("ErrPingFailed")
	ErrInvalidTransactionID = errors.New("ErrInvalidTransactionID")
	ErrWalletNotLoaded      = errors.New("ErrWalletNotLoaded")
	ErrInvalidFee           = errors.New("ErrInvalidFee")
	ErrFailedToSign         = errors.New("ErrFailedToSign")
	ErrTxDecodeFailed       = errors.New("ErrTxDecodeFailed")
	ErrTxAlreadySpent       = errors.New("ErrTxAlreadySpent")
	ErrTxAlreadyExists      = errors.New("ErrTxAlreadyExists")
	ErrNotEnoughBalance     = errors.New("ErrNotEnoughBalance")
	ErrNotEnoughConfirm     = errors.New("ErrNotEnoughConfirm")
)

Errors

View Source
var (
	ErrCouldNotOpenWalletStore  = errors.New("ErrCouldNotOpenWalletStore")
	ErrCouldNotCloseWalletStore = errors.New("ErrCouldNotCloseWalletStore")
	ErrCouldNotLoadWallet       = errors.New("ErrCouldNotLoadWallet")
	ErrCouldNotSaveWallet       = errors.New("ErrCouldNotSaveWallet")
	ErrCouldNotGetNextUTXO      = errors.New("ErrCouldNotGetNextUTXO")
	ErrCouldNotAddUTXO          = errors.New("ErrCouldNotAddUTXO")
)

Errors

Functions

This section is empty.

Types

type BTC

type BTC interface {
	// PutAnchor anchors the given Anchor by sending a Bitcoin transaction and returns its ID.
	PutAnchor(ctx context.Context, a *model.Anchor) ([]byte, error)
	// GetAnchor returns an AnchorRecord by searching the given Bitcoin transaction ID and parsing its data.
	GetAnchor(ctx context.Context, btctx []byte) (*model.AnchorRecord, error)

	io.Closer
}

BTC provides features to send and get anchors. This interface is not responsible for which Bitcoin wallet is used.

type BitcoinCLI

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

BitcoinCLI contains parameters for bitcoin-cli. Parameters are read only so this struct does not have state.

Excepts: xBTCAddr and xTransactionID are mutable. This is under consideration.

func MustNewBitcoinCLIWithCmdProxy

func MustNewBitcoinCLIWithCmdProxy(binPath string, btcNet model.BTCNet, rpcAddr, rpcPort, rpcUser, rpcPassword string, cmdproxyURL, cmdproxySecret string) *BitcoinCLI

MustNewBitcoinCLIWithCmdProxy initializes a BitcoinCLI with remote bitcoin-cli via cmdproxy. Runs date command on initialize and panics if failed. Also see: NewBitcoinCLI

func NewBitcoinCLI

func NewBitcoinCLI(binPath string, btcNet model.BTCNet, rpcAddr, rpcPort, rpcUser, rpcPassword string) *BitcoinCLI

NewBitcoinCLI initializes a BitcoinCLI.

Parameters:

  • binPath sets path to bitcoin-cli. Both relative and absolute are acceptable.
  • btcNet sets target Bitcoin network. A valid value must be set.
  • rpcAddr sets IP address to which bitcoin-cli connects. If "" is set, default value will be used.
  • rpcPort sets TCP port to which bitcoin-cli connects. If "" is set, default value will be used.
  • rpcUser sets RPC username used by bitcoin-cli. If "" is set, default value will be used.
  • rpcPassword sets RPC password used by bitcoin-cli. If "" is set, default value will be used.

This function does NOT check the status of the target bitcoind. If necessary, call b.Ping after calling this function.

func (*BitcoinCLI) Close

func (b *BitcoinCLI) Close() error

Close does not required to be called on exit.

func (*BitcoinCLI) CreateRawTransactionForAnchor

func (b *BitcoinCLI) CreateRawTransactionForAnchor(ctx context.Context, fromTxid []byte, vout int, balance string, toAddr string, fee uint, data []byte) ([]byte, error)

CreateRawTransactionForAnchor creates a raw transaction with one vout and one OP_RETURN.

Parameters:

  • fromTxid sets UTXO.
  • balance sets balance of fromTxid.
  • toAddr sets destination Bitcoin address.
  • fee sets transaction fee in Satoshi.
  • Info: BTC 0.0002 = Satoshi 20,000
  • data sets OP_RETURN data. Up to 80 bytes.

Possible errors: ErrInvalidFee|ErrExitCode1|ErrUnexpectedExitCode|ErrFailedToExec

func (*BitcoinCLI) DecodeRawTransaction

func (b *BitcoinCLI) DecodeRawTransaction(ctx context.Context, txdata []byte) (*bytes.Buffer, error)

DecodeRawTransaction returns a raw transaction in JSON.

Possible errors: ErrTxDecodeFailed|ErrWalletNotLoaded|ErrUnexpectedExitCode|ErrFailedToExec

func (*BitcoinCLI) GetAnchor

func (b *BitcoinCLI) GetAnchor(ctx context.Context, btctx []byte) (*model.AnchorRecord, error)

GetAnchor returns an AnchorRecord by searching the given Bitcoin transaction ID and parsing its data.

func (*BitcoinCLI) GetBalance

func (b *BitcoinCLI) GetBalance(ctx context.Context) (string, error)

GetBalance returns balance of the default wallet.

Possible errors: ErrWalletNotLoaded|ErrUnexpectedExitCode|ErrFailedToExec

func (*BitcoinCLI) GetTransaction

func (b *BitcoinCLI) GetTransaction(ctx context.Context, txid []byte) (*bytes.Buffer, error)

GetTransaction returns a transaction in JSON.

Possible errors: ErrInvalidTransactionID|ErrWalletNotLoaded|ErrUnexpectedExitCode|ErrFailedToExec

func (*BitcoinCLI) ParseRawTransactionOpReturn

func (*BitcoinCLI) ParseRawTransactionOpReturn(rawTxJSON *bytes.Buffer) ([]byte, error)

ParseRawTransactionOpReturn returns OP_RETURN value of the given raw transaction.

func (*BitcoinCLI) ParseSignRawTransactionWithWallet

func (*BitcoinCLI) ParseSignRawTransactionWithWallet(stdout io.Reader) ([]byte, error)

ParseSignRawTransactionWithWallet parses the response from b.SignRawTransactionWithWallet.

func (*BitcoinCLI) ParseTransactionConfirmations

func (*BitcoinCLI) ParseTransactionConfirmations(txJSON *bytes.Buffer) (uint, error)

ParseTransactionConfirmations returns confirmations of the given transaction.

func (*BitcoinCLI) ParseTransactionRawHex

func (*BitcoinCLI) ParseTransactionRawHex(txJSON *bytes.Buffer) ([]byte, error)

ParseTransactionRawHex returns raw data of the given transaction.

func (*BitcoinCLI) ParseTransactionReceived

func (*BitcoinCLI) ParseTransactionReceived(txJSON *bytes.Buffer, recvAddr string) (int, string, error)

ParseTransactionReceived returns vout number and received amount of the given Bitcoin address. Only counts the first received amount of the given address. Returns error if no received.

func (*BitcoinCLI) ParseTransactionTime

func (*BitcoinCLI) ParseTransactionTime(txJSON *bytes.Buffer) (time.Time, error)

ParseTransactionTime returns time of the given transaction.

func (*BitcoinCLI) Ping

func (b *BitcoinCLI) Ping(ctx context.Context) error

Ping checks bitcoin-cli version and pings bitcoind.

Possible errors: ErrUnsupportedVersion|ErrPingFailed|ErrUnexpectedExitCode|ErrFailedToExec

func (*BitcoinCLI) PutAnchor

func (b *BitcoinCLI) PutAnchor(ctx context.Context, a *model.Anchor) ([]byte, error)

PutAnchor anchors the given Anchor by sending a Bitcoin transaction and returns its transaction ID.

func (*BitcoinCLI) SendRawTransaction

func (b *BitcoinCLI) SendRawTransaction(ctx context.Context, signedRawTx []byte) ([]byte, error)

SendRawTransaction sends the given signed raw transaction and returns transaction ID.

Possible errors: ErrTxAlreadySpent|ErrTxAlreadyExists|ErrUnexpectedExitCode|ErrFailedToExec

func (*BitcoinCLI) SignRawTransactionWithWallet

func (b *BitcoinCLI) SignRawTransactionWithWallet(ctx context.Context, rawTx []byte) (*bytes.Buffer, error)

SignRawTransactionWithWallet signs the given transaction with the default wallet in bitcoin-cli and returns JSON.

Possible errors: ErrWalletNotLoaded|ErrUnexpectedExitCode|ErrFailedToExec

func (*BitcoinCLI) XGetUTXO

func (b *BitcoinCLI) XGetUTXO() (txid []byte, btcAddr string)

XGetUTXO returns b.xTransactionID and b.xBTCAddr. Please see XPutUTXO.

func (*BitcoinCLI) XSetUTXO

func (b *BitcoinCLI) XSetUTXO(txid []byte, btcAddr string)

XSetUTXO sets b.xTransactionID and b.xBTCAddr. As b.PutAnchor does not update b.xTransactionID after sending the transaction, this method should be called in cases of:

  • Every time before calling PutAnchor, the caller should also call this method to set UTXO to use.
  • Every time after calling PutAnchor, b call this method to set the next UTXO.

type DocstoreWallet

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

func MustNewDocstoreWallet

func MustNewDocstoreWallet(conn string, addr string) *DocstoreWallet

MustNewDocstoreWallet initializes a DocstoreWallet, panics if failed to access datastore.

Parameters:

  • conn sets connection string.
  • addr sets document name (uses Bitcoin addresses).

DocstoreWallet puts all UTXOs in a single document specified by name. TODO: put all UTXOs on every save is too heavy...

func (*DocstoreWallet) AddUTXO

func (w *DocstoreWallet) AddUTXO(txid []byte, addr string) error

func (*DocstoreWallet) Close

func (w *DocstoreWallet) Close() error

func (*DocstoreWallet) NextUTXO

func (w *DocstoreWallet) NextUTXO() (txid []byte, addr string, err error)

func (*DocstoreWallet) PeekNextUTXO

func (w *DocstoreWallet) PeekNextUTXO() (txid []byte, addr string, err error)

type Wallet

type Wallet interface {
	NextUTXO() (txid []byte, addr string, err error)
	PeekNextUTXO() (txid []byte, addr string, err error)
	AddUTXO(txid []byte, addr string) error
	io.Closer
}

Jump to

Keyboard shortcuts

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