Documentation
¶
Overview ¶
ElectrumX API接口实现
Package electrumx 提供ElectrumX JSON-RPC客户端 ¶
ElectrumX 原生数据结构定义
Index ¶
- type BalanceDTO
- type BlockHeaderDTO
- type Client
- func (c *Client) AddressGetBalance(ctx context.Context, addr string) (int64, int64, error)
- func (c *Client) AddressGetHistory(ctx context.Context, addr string) ([]HistoryDTO, error)
- func (c *Client) AddressGetMempool(ctx context.Context, addr string) ([]MempoolDTO, error)
- func (c *Client) AddressGetUTXOs(ctx context.Context, addr string) ([]UTXODTO, error)
- func (c *Client) AddressSubscribe(ctx context.Context, addr string) (string, error)
- func (c *Client) BatchGetBalances(ctx context.Context, addresses []string, concurrent int) ([]types.AddressBalanceInfo, error)
- func (c *Client) BlockchainGetBlockHeader(ctx context.Context, height int64, cpHeight int64) (string, error)
- func (c *Client) BlockchainGetBlockHeaders(ctx context.Context, startHeight int64, count int64, cpHeight int64) (interface{}, error)
- func (c *Client) EstimateFee(ctx context.Context, blocks int) (float64, error)
- func (c *Client) FilterAddressesWithBalance(ctx context.Context, addresses []string, minBalance int64, concurrent int) ([]types.AddressBalanceInfo, error)
- func (c *Client) GetBalancesByPrivateKey(ctx context.Context, privateKeyWIF string) (*types.AddressBalanceInfo, error)
- func (c *Client) GetBalancesByXPRV(ctx context.Context, xprv string, scanCount uint32) ([]types.AddressBalanceInfo, error)
- func (c *Client) GetBlockchainTip(ctx context.Context) (int64, error)
- func (c *Client) RelayFee(ctx context.Context) (float64, error)
- func (c *Client) ServerBanner(ctx context.Context) (string, error)
- func (c *Client) ServerFeatures(ctx context.Context) (*ServerFeaturesDTO, error)
- func (c *Client) ServerPing(ctx context.Context) error
- func (c *Client) ServerVersion(ctx context.Context, clientName string, protocolVersion string) (*ServerVersionDTO, error)
- func (c *Client) TransactionBroadcast(ctx context.Context, rawTxHex string) (string, error)
- func (c *Client) TransactionGet(ctx context.Context, txid string, verbose bool) (interface{}, error)
- func (c *Client) TransactionGetMerkle(ctx context.Context, txid string, height int64) (interface{}, error)
- func (c *Client) TransactionGetRaw(ctx context.Context, txid string) (string, error)
- func (c *Client) TransactionIDFromPos(ctx context.Context, height int64, txPos int64, merkle bool) (interface{}, error)
- type FeeEstimateDTO
- type HistoryDTO
- type MempoolDTO
- type ScriptPubKey
- type ScriptSig
- type ServerFeaturesDTO
- type ServerVersionDTO
- type TransactionDTO
- type UTXODTO
- type Vin
- type Vout
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BalanceDTO ¶
type BalanceDTO struct {
Confirmed int64 `json:"confirmed"` // 已确认余额(单位:聪)
Unconfirmed int64 `json:"unconfirmed"` // 未确认余额(单位:聪)
}
BalanceDTO 地址余额数据结构
type BlockHeaderDTO ¶
type BlockHeaderDTO struct {
Version int32 `json:"version"` // 版本
PrevBlockHash string `json:"prev_block_hash"` // 上一个区块哈希
MerkleRoot string `json:"merkle_root"` // 默克尔根
Timestamp int64 `json:"timestamp"` // 时间戳
Bits uint32 `json:"bits"` // 难度目标
Nonce uint32 `json:"nonce"` // 随机数
BlockHeight int64 `json:"block_height"` // 区块高度
}
BlockHeaderDTO 区块头数据结构
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func New ¶
New 创建ElectrumX客户端实例 baseURL: ElectrumX服务器地址,例如 "http://localhost:50001" timeout: 请求超时时间(秒)
func (*Client) AddressGetBalance ¶
AddressGetBalance 获取地址余额 参数: addr - 比特币地址 返回: 已确认余额(聪)、未确认余额(聪)、错误
func (*Client) AddressGetHistory ¶
AddressGetHistory 获取地址交易历史 参数: addr - 比特币地址 返回: 交易历史列表、错误
func (*Client) AddressGetMempool ¶
AddressGetMempool 获取地址在内存池中的交易 参数: addr - 比特币地址 返回: 内存池交易列表、错误
func (*Client) AddressGetUTXOs ¶
AddressGetUTXOs 获取地址UTXO列表 参数: addr - 比特币地址 返回: UTXO列表、错误
func (*Client) AddressSubscribe ¶
AddressSubscribe 订阅地址变更通知 参数: addr - 比特币地址 返回: 当前状态哈希、错误 注意: 此方法需要WebSocket连接支持,HTTP连接仅返回当前状态
func (*Client) BatchGetBalances ¶ added in v0.2.1
func (c *Client) BatchGetBalances(ctx context.Context, addresses []string, concurrent int) ([]types.AddressBalanceInfo, error)
BatchGetBalances 批量查询地址余额(返回所有地址的余额,包括0余额) 参数:
ctx - 上下文 addresses - 地址列表 concurrent - 并发查询数
返回: 所有地址的余额信息列表、错误
func (*Client) BlockchainGetBlockHeader ¶
func (c *Client) BlockchainGetBlockHeader(ctx context.Context, height int64, cpHeight int64) (string, error)
BlockchainGetBlockHeader 获取区块头信息 参数: height - 区块高度, cpHeight - checkpoint高度(可选) 返回: 区块头十六进制数据、错误
func (*Client) BlockchainGetBlockHeaders ¶
func (c *Client) BlockchainGetBlockHeaders(ctx context.Context, startHeight int64, count int64, cpHeight int64) (interface{}, error)
BlockchainGetBlockHeaders 批量获取区块头信息 参数: startHeight - 起始区块高度, count - 获取数量, cpHeight - checkpoint高度(可选) 返回: 区块头数据、错误
func (*Client) EstimateFee ¶
EstimateFee 估算交易手续费 参数: blocks - 目标确认区块数 返回: 手续费率(BTC/KB)、错误
func (*Client) FilterAddressesWithBalance ¶ added in v0.2.1
func (c *Client) FilterAddressesWithBalance(ctx context.Context, addresses []string, minBalance int64, concurrent int) ([]types.AddressBalanceInfo, error)
FilterAddressesWithBalance 过滤出余额大于0的地址 参数:
ctx - 上下文 addresses - 地址列表 minBalance - 最小余额(聪),默认为0表示只要有余额即可 concurrent - 并发查询数,建议设置为10-50
返回: 余额大于指定值的地址列表、错误
func (*Client) GetBalancesByPrivateKey ¶ added in v0.2.1
func (c *Client) GetBalancesByPrivateKey(ctx context.Context, privateKeyWIF string) (*types.AddressBalanceInfo, error)
GetBalancesByPrivateKey 通过单个私钥查询对应地址的余额 参数:
ctx - 上下文 privateKeyWIF - WIF格式的私钥
返回: 地址余额信息、错误
func (*Client) GetBalancesByXPRV ¶ added in v0.2.1
func (c *Client) GetBalancesByXPRV(ctx context.Context, xprv string, scanCount uint32) ([]types.AddressBalanceInfo, error)
GetBalancesByXPRV 通过扩展私钥查询所有派生地址的余额 参数:
ctx - 上下文
xprv - 扩展私钥(HD钱包主密钥)
derivationPaths - 派生路径列表,例如 [][]uint32{{44', 0', 0', 0, 0}, {44', 0', 0', 0, 1}}
scanCount - 每种类型地址扫描的数量(如果为0,默认扫描20个)
返回: 地址余额信息列表、错误
func (*Client) GetBlockchainTip ¶
GetBlockchainTip 获取当前区块链最新高度 返回: 最新区块高度、错误
func (*Client) ServerBanner ¶
ServerBanner 获取服务器横幅信息 返回: 横幅文本、错误
func (*Client) ServerFeatures ¶
func (c *Client) ServerFeatures(ctx context.Context) (*ServerFeaturesDTO, error)
ServerFeatures 获取服务器功能信息 返回: 服务器功能信息、错误
func (*Client) ServerPing ¶
ServerPing 心跳检测 返回: 错误
func (*Client) ServerVersion ¶
func (c *Client) ServerVersion(ctx context.Context, clientName string, protocolVersion string) (*ServerVersionDTO, error)
ServerVersion 获取服务器版本信息 参数: clientName - 客户端名称, protocolVersion - 协议版本 返回: 服务器版本信息、错误
func (*Client) TransactionBroadcast ¶
TransactionBroadcast 广播交易 参数: rawTxHex - 交易原始十六进制数据 返回: 交易ID、错误
func (*Client) TransactionGet ¶
func (c *Client) TransactionGet(ctx context.Context, txid string, verbose bool) (interface{}, error)
TransactionGet 获取交易详情 参数: txid - 交易ID, verbose - 是否返回详细信息 返回: 交易原始十六进制或详细信息、错误
func (*Client) TransactionGetMerkle ¶
func (c *Client) TransactionGetMerkle(ctx context.Context, txid string, height int64) (interface{}, error)
TransactionGetMerkle 获取交易的Merkle证明 参数: txid - 交易ID, height - 区块高度 返回: Merkle证明数据、错误
func (*Client) TransactionGetRaw ¶
TransactionGetRaw 获取交易原始十六进制数据 参数: txid - 交易ID 返回: 交易原始十六进制、错误
type FeeEstimateDTO ¶
type FeeEstimateDTO struct {
FeeRate float64 `json:"feerate"` // 手续费率(单位:BTC/KB)
}
FeeEstimateDTO 手续费估算数据结构
type HistoryDTO ¶
type HistoryDTO struct {
TxHash string `json:"tx_hash"` // 交易哈希
Height int64 `json:"height"` // 区块高度(0表示未确认,-1表示未广播)
Fee int64 `json:"fee"` // 交易费用(单位:聪)
}
HistoryDTO 交易历史数据结构
type MempoolDTO ¶
type MempoolDTO struct {
TxHash string `json:"tx_hash"` // 交易哈希
Height int64 `json:"height"` // 高度(-1表示在内存池中)
Fee int64 `json:"fee"` // 交易费用
}
MempoolDTO 内存池交易数据结构
type ScriptPubKey ¶
type ScriptPubKey struct {
Asm string `json:"asm"` // 汇编格式
Hex string `json:"hex"` // 十六进制格式
ReqSigs int `json:"reqSigs"` // 需要的签名数
Type string `json:"type"` // 脚本类型
Addresses []string `json:"addresses"` // 地址列表
}
ScriptPubKey 锁定脚本数据结构
type ServerFeaturesDTO ¶
type ServerFeaturesDTO struct {
GenesisHash string `json:"genesis_hash"` // 创世区块哈希
Hosts map[string]interface{} `json:"hosts"` // 服务器主机信息
ProtocolMax string `json:"protocol_max"` // 最大协议版本
ProtocolMin string `json:"protocol_min"` // 最小协议版本
Pruning interface{} `json:"pruning"` // 修剪信息
ServerVersion string `json:"server_version"` // 服务器版本
HashFunction string `json:"hash_function"` // 哈希函数
}
ServerFeaturesDTO 服务器功能信息
type ServerVersionDTO ¶
type ServerVersionDTO struct {
ServerVersion string `json:"server_version"` // 服务器版本
ProtocolVersion string `json:"protocol_version"` // 协议版本
}
ServerVersionDTO 服务器版本信息
type TransactionDTO ¶
type TransactionDTO struct {
Txid string `json:"txid"` // 交易ID
Hash string `json:"hash"` // 交易哈希(包含见证数据)
Version int32 `json:"version"` // 交易版本
Size int64 `json:"size"` // 交易大小(字节)
Vsize int64 `json:"vsize"` // 虚拟大小(vbytes)
Weight int64 `json:"weight"` // 权重
Locktime uint32 `json:"locktime"` // 锁定时间
Hex string `json:"hex"` // 原始交易十六进制
Confirmations int64 `json:"confirmations"` // 确认数
BlockHash string `json:"blockhash"` // 区块哈希
BlockTime int64 `json:"blocktime"` // 区块时间
Time int64 `json:"time"` // 交易时间
Vin []Vin `json:"vin"` // 输入列表
Vout []Vout `json:"vout"` // 输出列表
}
TransactionDTO 交易详情数据结构
type UTXODTO ¶
type UTXODTO struct {
TxHash string `json:"tx_hash"` // 交易哈希
TxPos uint32 `json:"tx_pos"` // 交易输出索引
Value int64 `json:"value"` // 金额(单位:聪)
Height int64 `json:"height"` // 区块高度(0表示未确认)
}
UTXODTO UTXO数据结构
type Vin ¶
type Vin struct {
Txid string `json:"txid"` // 引用的交易ID
Vout uint32 `json:"vout"` // 引用的输出索引
ScriptSig ScriptSig `json:"scriptSig"` // 签名脚本
Sequence uint32 `json:"sequence"` // 序列号
Witness []string `json:"txinwitness,omitempty"` // 见证数据
}
Vin 交易输入数据结构
type Vout ¶
type Vout struct {
Value float64 `json:"value"` // 金额(单位:BTC)
N uint32 `json:"n"` // 输出索引
ScriptPubKey ScriptPubKey `json:"scriptPubKey"` // 锁定脚本
}
Vout 交易输出数据结构