btc

package
v0.0.0-...-7df67f7 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 30 Imported by: 2

Documentation

Index

Constants

View Source
const (
	HEADER_SIZE           = 80
	HEADER_FILE_NAME      = "blockchain_headers"
	ELECTRUM_MAGIC_NUMHDR = 2016
)
View Source
const (
	RpcDefaultIP   = "127.0.0.1"
	RpcDefaultPort = 8888
)

///////////////////////////////////////////// RPC Server ///////////

Variables

View Source
var ErrNoNode error = errors.New("no node")
View Source
var ErrNoSubscriptionFoundInDb = errors.New("no subscription found in db")
View Source
var ErrNoWallet error = errors.New("no wallet")

Functions

func NewBtcElectrumClient

func NewBtcElectrumClient(cfg *client.ClientConfig) client.ElectrumClient

func RPCServe

func RPCServe(electrumClient client.ElectrumClient) error

Types

type BtcElectrumClient

type BtcElectrumClient struct {
	ClientConfig *client.ClientConfig
	Wallet       wallet.ElectrumWallet
	Node         electrumx.ElectrumXNode
	// contains filtered or unexported fields
}

BtcElectrumClient

func (*BtcElectrumClient) Balance

func (ec *BtcElectrumClient) Balance() (int64, int64, int64, error)

Balance returns the confirmed and unconfirmed balances of this wallet. This is a simple wallet and once a transaction has been mined it is considered confirmed.

func (*BtcElectrumClient) Broadcast

func (ec *BtcElectrumClient) Broadcast(ctx context.Context, rawTx []byte) (string, error)

Broadcast sends a transaction to the ElectrumX server for broadcast on the bitcoin network. It may also set up address status change notifications with ElectrumX in the wallet db for addresses such as change address belonging to the wallet.

func (*BtcElectrumClient) ChangeAddress

func (ec *BtcElectrumClient) ChangeAddress(ctx context.Context) (string, error)

ChangeAddress gets a new unused wallet change address and subscribes for ElectrumX address status notify events on the returned address.

func (*BtcElectrumClient) CloseWallet

func (ec *BtcElectrumClient) CloseWallet()

func (*BtcElectrumClient) CreateWallet

func (ec *BtcElectrumClient) CreateWallet(pw string) error

CreateWallet makes a new wallet with a new seed. The password is to encrypt stored xpub, xprv and other sensitive data.

func (*BtcElectrumClient) FeeRate

func (ec *BtcElectrumClient) FeeRate(ctx context.Context, confTarget int64) (int64, error)

func (*BtcElectrumClient) FreezeUTXO

func (ec *BtcElectrumClient) FreezeUTXO(txid string, out uint32) error

func (*BtcElectrumClient) GetAddressHistory

func (ec *BtcElectrumClient) GetAddressHistory(ctx context.Context, addr string) (electrumx.HistoryResult, error)

Return the transaction history of any address.

func (*BtcElectrumClient) GetAddressHistoryFromNode

func (ec *BtcElectrumClient) GetAddressHistoryFromNode(ctx context.Context, subscription *wallet.Subscription) (electrumx.HistoryResult, error)

GetAddressHistoryFromNode requests address history from ElectrumX for a subscribed address.

func (*BtcElectrumClient) GetAddressUnspent

func (ec *BtcElectrumClient) GetAddressUnspent(ctx context.Context, addr string) (electrumx.ListUnspentResult, error)

func (*BtcElectrumClient) GetBlockHeader

func (ec *BtcElectrumClient) GetBlockHeader(height int64) *wire.BlockHeader

GetBlockHeader returns the client's block header for height. If out of range will return nil.

func (*BtcElectrumClient) GetBlockHeaders

func (ec *BtcElectrumClient) GetBlockHeaders(startHeight, count int64) ([]*wire.BlockHeader, error)

GetBlockHeaders returns the client's block headers for the requested range. If startHeight < startPoint or startHeight > tip or startHeight+count > tip will return error.

func (*BtcElectrumClient) GetConfig

func (ec *BtcElectrumClient) GetConfig() *client.ClientConfig

func (*BtcElectrumClient) GetHeaderForBlockHash

func (ec *BtcElectrumClient) GetHeaderForBlockHash(blkHash *chainhash.Hash) *wire.BlockHeader

func (*BtcElectrumClient) GetNode

func (*BtcElectrumClient) GetPrivKeyForAddress

func (ec *BtcElectrumClient) GetPrivKeyForAddress(pw, addr string) (string, error)

GetPrivKeyForAddress

func (*BtcElectrumClient) GetRawTransaction

func (ec *BtcElectrumClient) GetRawTransaction(ctx context.Context, txid string) ([]byte, error)

Return the raw transaction bytes for a txid.

func (*BtcElectrumClient) GetRawTransactionFromNode

func (ec *BtcElectrumClient) GetRawTransactionFromNode(ctx context.Context, txid string) (*wire.MsgTx, time.Time, error)

GetRawTransactionFromNode requests a raw hex transaction for a subscribed address from ElectrumX keyed on a txid. This txid is usually taken from an ElectrumX history list.

func (*BtcElectrumClient) GetTransaction

func (ec *BtcElectrumClient) GetTransaction(ctx context.Context, txid string) (*electrumx.GetTransactionResult, error)

Return the transaction info for a txid.

func (*BtcElectrumClient) GetWallet

func (ec *BtcElectrumClient) GetWallet() wallet.ElectrumWallet

func (*BtcElectrumClient) GetWalletSpents

func (ec *BtcElectrumClient) GetWalletSpents() ([]wallet.Stxo, error)

func (*BtcElectrumClient) GetWalletTx

func (ec *BtcElectrumClient) GetWalletTx(txid string) (int, bool, []byte, error)

GetWalletTx gets a tx from this wallet if it exists. If it does not esist then we return error. Edge cases:

  • If the tx in the db is unmined we return it with no error. But if it has been mined then we compare the height it was mined with 'maybeTip' and return an error if the wallet tip is obviously behind electrumx; and we also return a flag indicating that. Software calling this API can then choose to directly ask electrumx for tx information using the GetTransaction API.

  • if wallet tx height is less than or equal to 'maybeTip' we return the tx with no error even though we may be behind electrumx.

func (*BtcElectrumClient) ImportAndSweep

func (ec *BtcElectrumClient) ImportAndSweep(ctx context.Context, importedKeyPairs []string) error

ImportAndSweep imports privkeys from other wallets and builds a transaction that sweeps their funds into our wallet.

func (*BtcElectrumClient) ListConfirmedUnspent

func (ec *BtcElectrumClient) ListConfirmedUnspent() ([]wallet.Utxo, error)

ListConfirmedUnspent returns a list of all utxos in the wallet db with height > 0.

func (*BtcElectrumClient) ListFrozenUnspent

func (ec *BtcElectrumClient) ListFrozenUnspent() ([]wallet.Utxo, error)

ListFrozenUnspent returns a list of all utxos in the wallet db that are temporarily frozen.

func (*BtcElectrumClient) ListUnspent

func (ec *BtcElectrumClient) ListUnspent() ([]wallet.Utxo, error)

ListUnspent returns a list of all utxos in the wallet db.

func (*BtcElectrumClient) LoadWallet

func (ec *BtcElectrumClient) LoadWallet(pw string) error

LoadWallet loads an existing wallet. The password is required to decrypt the stored xpub, xprv and other sensitive data

func (*BtcElectrumClient) RecreateWallet

func (ec *BtcElectrumClient) RecreateWallet(ctx context.Context, pw, mnenomic string) error

RecreateWallet recreates a wallet from an existing mnemonic seed. The password is to encrypt the stored xpub, xprv and other sensitive data and can be different from the original wallet's password.

func (*BtcElectrumClient) RegisterTipChangeNotify

func (ec *BtcElectrumClient) RegisterTipChangeNotify() (<-chan int64, error)

func (*BtcElectrumClient) RescanWallet

func (ec *BtcElectrumClient) RescanWallet(ctx context.Context) error

RescanWallet asks ElectrumX for info for our wallet keys back to latest checkpoint height. We need to do this for a recreated wallet.

func (*BtcElectrumClient) RpcBroadcast

func (ec *BtcElectrumClient) RpcBroadcast(ctx context.Context, tx string) (string, error)

RpcBroadcast sends a transaction to the server for broadcast on the bitcoin network. It is a test rpc server endpoint and it is thus not part of the ElectrumClient interface.

func (*BtcElectrumClient) SignTx

func (ec *BtcElectrumClient) SignTx(pw string, txBytes []byte) ([]byte, error)

func (*BtcElectrumClient) Spend

func (ec *BtcElectrumClient) Spend(
	pw string,
	amount int64,
	toAddress string,
	feeLevel wallet.FeeLevel) (int, string, string, error)

Spend tries to create a new transaction to pay an amount from the wallet to toAddress. It returns Tx & Txid as hex strings and the index of any change output or -1 if none. The wallet password is required in order to sign the tx.

func (*BtcElectrumClient) Start

func (ec *BtcElectrumClient) Start(ctx context.Context) error

func (*BtcElectrumClient) Stop

func (ec *BtcElectrumClient) Stop()

func (*BtcElectrumClient) SubscribeAddressNotify

func (ec *BtcElectrumClient) SubscribeAddressNotify(ctx context.Context, newSub *wallet.Subscription) (string, error)

SubscribeAddressNotify subscribes to notifications from ElectrumX for a public key script address. It also adds the new subscription to the wallet database. It returns a subscribe status which is the hash of all address history known to the electrumX server and can be a zero length string if the subscription is new and has no history yet.

func (*BtcElectrumClient) SyncWallet

func (ec *BtcElectrumClient) SyncWallet(ctx context.Context) error

SyncWallet sets up address notifications for subscribed addresses in the wallet db. This will update txns, utxos, stxos wallet db tables with any new address status history since the wallet was last open.

func (*BtcElectrumClient) Tip

func (ec *BtcElectrumClient) Tip() (int64, bool)

Tip returns the (local) block headers tip height and client headers sync status.

func (*BtcElectrumClient) UnfreezeUTXO

func (ec *BtcElectrumClient) UnfreezeUTXO(txid string, out uint32) error

func (*BtcElectrumClient) UnregisterTipChangeNotify

func (ec *BtcElectrumClient) UnregisterTipChangeNotify()

func (*BtcElectrumClient) UnsubscribeAddressNotify

func (ec *BtcElectrumClient) UnsubscribeAddressNotify(ctx context.Context, pkScript string)

UnsubscribeAddressNotify both unsubscribes from notifications for an address _and_ removes the subscription details from the wallet database.

func (*BtcElectrumClient) UnusedAddress

func (ec *BtcElectrumClient) UnusedAddress(ctx context.Context) (string, error)

UnusedAddress gets a new unused wallet receive address and subscribes for ElectrumX address status notify events on the returned address.

func (*BtcElectrumClient) ValidateAddress

func (ec *BtcElectrumClient) ValidateAddress(addr string) (bool, bool, error)

ValidateAddress returns if the address is valid and if it does or does not belong to this wallet

type Ec

type Ec struct {
	EleClient *BtcElectrumClient
}

RPC Service methods

func (*Ec) ListUnspent

func (e *Ec) ListUnspent() (string, error)

List unspent outputs in the wallet including frozen utxos

func (*Ec) RPCBalance

func (e *Ec) RPCBalance(request map[string]string, response *map[string]string) error

func (*Ec) RPCBroadcast

func (e *Ec) RPCBroadcast(request map[string]string, response *map[string]string) error

func (*Ec) RPCChangeAddress

func (e *Ec) RPCChangeAddress(request map[string]string, response *map[string]string) error

Get a new unused wallet change address

func (*Ec) RPCEcho

func (e *Ec) RPCEcho(request map[string]string, response *map[string]string) error

Simple echo back to client method

func (*Ec) RPCListUnspent

func (e *Ec) RPCListUnspent(request map[string]string, response *map[string]string) error

func (*Ec) RPCSpend

func (e *Ec) RPCSpend(request map[string]string, response *map[string]string) error

func (*Ec) RPCTip

func (e *Ec) RPCTip(request map[string]string, response *map[string]string) error

func (*Ec) RPCUnusedAddress

func (e *Ec) RPCUnusedAddress(request map[string]string, response *map[string]string) error

Get a new unused wallet receive address

func (*Ec) Tip

func (e *Ec) Tip() (int64, bool)

Get the blockchain tip and sync status

type Headers

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

func NewHeaders

func NewHeaders(cfg *client.ClientConfig) *Headers

func (*Headers) AppendHeaders

func (h *Headers) AppendHeaders(rawHdrs []byte) (int64, error)

AppendHeaders appends headers from server 'blockchain.block.header(s)' calls to 'blockchain_headers' file. Also appends headers received from the 'blockchain.headers.subscribe' events. Returns the number of headers written.

func (*Headers) BytesToNumHdrs

func (h *Headers) BytesToNumHdrs(numBytes int64) (int64, error)

func (*Headers) ClearMaps

func (h *Headers) ClearMaps()

Only for TEST in headers_test.go

func (*Headers) DumpAll

func (h *Headers) DumpAll()

func (*Headers) DumpAt

func (h *Headers) DumpAt(height int64)

func (*Headers) ReadAllBytesFromFile

func (h *Headers) ReadAllBytesFromFile() ([]byte, error)

func (*Headers) ReadHeaders

func (h *Headers) ReadHeaders(num, height int64) (int32, error)

Read num headers from offset in 'blockchain_headers' file

func (*Headers) StatFileSize

func (h *Headers) StatFileSize() (int64, error)

Get the 'blockchain_headers' file size. Error is returned unexamined as we assume the file exists and ENOENT will not be valid.

func (*Headers) Store

func (h *Headers) Store(b []byte, startHeight int64) error

store 'numHdrs' headers starting at height 'height' in 'hdrs' map 'b' should have exactly 'numHdrs' x 'HEADER_SIZE' bytes.

func (*Headers) VerifyAll

func (h *Headers) VerifyAll() error

func (*Headers) VerifyFromTip

func (h *Headers) VerifyFromTip(depth int64, all bool) error

Verify headers prev hash back from tip. If all is true depth is ignored and the whole chain is verified

Jump to

Keyboard shortcuts

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