Documentation ¶
Index ¶
- Constants
- Variables
- func NewBtcElectrumClient(cfg *client.ClientConfig) client.ElectrumClient
- func RPCServe(electrumClient client.ElectrumClient) error
- type BtcElectrumClient
- func (ec *BtcElectrumClient) Balance() (int64, int64, int64, error)
- func (ec *BtcElectrumClient) Broadcast(ctx context.Context, rawTx []byte) (string, error)
- func (ec *BtcElectrumClient) ChangeAddress(ctx context.Context) (string, error)
- func (ec *BtcElectrumClient) CloseWallet()
- func (ec *BtcElectrumClient) CreateWallet(pw string) error
- func (ec *BtcElectrumClient) FeeRate(ctx context.Context, confTarget int64) (int64, error)
- func (ec *BtcElectrumClient) FreezeUTXO(txid string, out uint32) error
- func (ec *BtcElectrumClient) GetAddressHistory(ctx context.Context, addr string) (electrumx.HistoryResult, error)
- func (ec *BtcElectrumClient) GetAddressHistoryFromNode(ctx context.Context, subscription *wallet.Subscription) (electrumx.HistoryResult, error)
- func (ec *BtcElectrumClient) GetAddressUnspent(ctx context.Context, addr string) (electrumx.ListUnspentResult, error)
- func (ec *BtcElectrumClient) GetBlockHeader(height int64) *wire.BlockHeader
- func (ec *BtcElectrumClient) GetBlockHeaders(startHeight, count int64) ([]*wire.BlockHeader, error)
- func (ec *BtcElectrumClient) GetConfig() *client.ClientConfig
- func (ec *BtcElectrumClient) GetHeaderForBlockHash(blkHash *chainhash.Hash) *wire.BlockHeader
- func (ec *BtcElectrumClient) GetNode() electrumx.ElectrumXNode
- func (ec *BtcElectrumClient) GetPrivKeyForAddress(pw, addr string) (string, error)
- func (ec *BtcElectrumClient) GetRawTransaction(ctx context.Context, txid string) ([]byte, error)
- func (ec *BtcElectrumClient) GetRawTransactionFromNode(ctx context.Context, txid string) (*wire.MsgTx, time.Time, error)
- func (ec *BtcElectrumClient) GetTransaction(ctx context.Context, txid string) (*electrumx.GetTransactionResult, error)
- func (ec *BtcElectrumClient) GetWallet() wallet.ElectrumWallet
- func (ec *BtcElectrumClient) GetWalletSpents() ([]wallet.Stxo, error)
- func (ec *BtcElectrumClient) GetWalletTx(txid string) (int, bool, []byte, error)
- func (ec *BtcElectrumClient) ImportAndSweep(ctx context.Context, importedKeyPairs []string) error
- func (ec *BtcElectrumClient) ListConfirmedUnspent() ([]wallet.Utxo, error)
- func (ec *BtcElectrumClient) ListFrozenUnspent() ([]wallet.Utxo, error)
- func (ec *BtcElectrumClient) ListUnspent() ([]wallet.Utxo, error)
- func (ec *BtcElectrumClient) LoadWallet(pw string) error
- func (ec *BtcElectrumClient) RecreateWallet(ctx context.Context, pw, mnenomic string) error
- func (ec *BtcElectrumClient) RegisterTipChangeNotify() (<-chan int64, error)
- func (ec *BtcElectrumClient) RescanWallet(ctx context.Context) error
- func (ec *BtcElectrumClient) RpcBroadcast(ctx context.Context, tx string) (string, error)
- func (ec *BtcElectrumClient) SignTx(pw string, txBytes []byte) ([]byte, error)
- func (ec *BtcElectrumClient) Spend(pw string, amount int64, toAddress string, feeLevel wallet.FeeLevel) (int, string, string, error)
- func (ec *BtcElectrumClient) Start(ctx context.Context) error
- func (ec *BtcElectrumClient) Stop()
- func (ec *BtcElectrumClient) SubscribeAddressNotify(ctx context.Context, newSub *wallet.Subscription) (string, error)
- func (ec *BtcElectrumClient) SyncWallet(ctx context.Context) error
- func (ec *BtcElectrumClient) Tip() (int64, bool)
- func (ec *BtcElectrumClient) UnfreezeUTXO(txid string, out uint32) error
- func (ec *BtcElectrumClient) UnregisterTipChangeNotify()
- func (ec *BtcElectrumClient) UnsubscribeAddressNotify(ctx context.Context, pkScript string)
- func (ec *BtcElectrumClient) UnusedAddress(ctx context.Context) (string, error)
- func (ec *BtcElectrumClient) ValidateAddress(addr string) (bool, bool, error)
- type Ec
- func (e *Ec) ListUnspent() (string, error)
- func (e *Ec) RPCBalance(request map[string]string, response *map[string]string) error
- func (e *Ec) RPCBroadcast(request map[string]string, response *map[string]string) error
- func (e *Ec) RPCChangeAddress(request map[string]string, response *map[string]string) error
- func (e *Ec) RPCEcho(request map[string]string, response *map[string]string) error
- func (e *Ec) RPCListUnspent(request map[string]string, response *map[string]string) error
- func (e *Ec) RPCSpend(request map[string]string, response *map[string]string) error
- func (e *Ec) RPCTip(request map[string]string, response *map[string]string) error
- func (e *Ec) RPCUnusedAddress(request map[string]string, response *map[string]string) error
- func (e *Ec) Tip() (int64, bool)
- type Headers
- func (h *Headers) AppendHeaders(rawHdrs []byte) (int64, error)
- func (h *Headers) BytesToNumHdrs(numBytes int64) (int64, error)
- func (h *Headers) ClearMaps()
- func (h *Headers) DumpAll()
- func (h *Headers) DumpAt(height int64)
- func (h *Headers) ReadAllBytesFromFile() ([]byte, error)
- func (h *Headers) ReadHeaders(num, height int64) (int32, error)
- func (h *Headers) StatFileSize() (int64, error)
- func (h *Headers) Store(b []byte, startHeight int64) error
- func (h *Headers) VerifyAll() error
- func (h *Headers) VerifyFromTip(depth int64, all bool) error
Constants ¶
const ( HEADER_SIZE = 80 HEADER_FILE_NAME = "blockchain_headers" ELECTRUM_MAGIC_NUMHDR = 2016 )
const ( RpcDefaultIP = "127.0.0.1" RpcDefaultPort = 8888 )
///////////////////////////////////////////// RPC Server ///////////
Variables ¶
var ErrNoNode error = errors.New("no node")
var ErrNoSubscriptionFoundInDb = errors.New("no subscription found in db")
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 ¶
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) 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 (ec *BtcElectrumClient) GetNode() electrumx.ElectrumXNode
func (*BtcElectrumClient) GetPrivKeyForAddress ¶
func (ec *BtcElectrumClient) GetPrivKeyForAddress(pw, addr string) (string, error)
GetPrivKeyForAddress
func (*BtcElectrumClient) GetRawTransaction ¶
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 ¶
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 ¶
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) 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 ¶
List unspent outputs in the wallet including frozen utxos
func (*Ec) RPCBalance ¶
func (*Ec) RPCBroadcast ¶
func (*Ec) RPCChangeAddress ¶
Get a new unused wallet change address
func (*Ec) RPCListUnspent ¶
func (*Ec) RPCUnusedAddress ¶
Get a new unused wallet receive address
type Headers ¶
type Headers struct {
// contains filtered or unexported fields
}
func NewHeaders ¶
func NewHeaders(cfg *client.ClientConfig) *Headers
func (*Headers) AppendHeaders ¶
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) ReadAllBytesFromFile ¶
func (*Headers) ReadHeaders ¶
Read num headers from offset in 'blockchain_headers' file
func (*Headers) StatFileSize ¶
Get the 'blockchain_headers' file size. Error is returned unexamined as we assume the file exists and ENOENT will not be valid.