btcapis

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2025 License: MIT Imports: 15 Imported by: 0

README

BTCAPIs

Go Version Documentation

一个功能全面的 Go 语言比特币 API 库,提供地址处理、交易操作、PSBT 管理、脚本解析等核心功能。支持多种数据源,包括 Bitcoin Core RPC 和 Mempool.space API。

🚀 特性

核心功能
  • 地址处理 - 支持所有比特币地址类型(P2PKH、P2SH、P2WPKH、P2WSH、P2TR)
  • 交易操作 - 创建、签名、广播比特币交易
  • PSBT 支持 - 完整的 PSBT(部分签名比特币交易)工厂实现
  • 脚本解析 - 比特币脚本编码/解码与分析
  • UTXO 管理 - 查询和管理未花费交易输出
  • 费率估算 - 智能费率计算和优化
数据源支持
  • Bitcoin Core RPC - 直接连接比特币核心节点
  • Mempool.space API - 支持主网、测试网、Signet
  • 多后端架构 - 可扩展的提供商系统
网络支持
  • 主网 (Mainnet)
  • 测试网 (Testnet)
  • Signet

📦 安装

go get github.com/crazycloudcc/btcapis

🔧 快速开始

基础初始化
package main

import (
    "context"
    "log"
    "time"

    "github.com/crazycloudcc/btcapis"
)

func main() {
    // 创建客户端连接
    client := btcapis.New(
        "testnet",                    // 网络: mainnet, testnet, signet
        "http://localhost:18332",     // Bitcoin Core RPC URL
        "rpcuser",                    // RPC 用户名
        "rpcpassword",               // RPC 密码
        30,                          // 超时时间(秒)
    )

    ctx := context.Background()

    // 检查连接
    blockCount, err := client.GetBlockCount(ctx)
    if err != nil {
        log.Fatal("连接失败:", err)
    }

    log.Printf("当前区块高度: %d", blockCount)
}
地址操作
// 查询地址余额
confirmed, mempool, err := client.GetAddressBalance(ctx, "tb1q...")
if err != nil {
    log.Fatal(err)
}
log.Printf("确认余额: %d satoshi, 未确认: %d satoshi", confirmed, mempool)

// 获取地址 UTXO
utxos, err := client.GetAddressUTXOs(ctx, "tb1q...")
if err != nil {
    log.Fatal(err)
}
log.Printf("UTXO 数量: %d", len(utxos))

// 地址类型解析
addrType, err := client.DecodeAddressToType("tb1q...")
if err != nil {
    log.Fatal(err)
}
log.Printf("地址类型: %s", addrType)
交易操作
// 查询交易信息
tx, err := client.GetTx(ctx, "交易ID")
if err != nil {
    log.Fatal(err)
}
log.Printf("交易版本: %d, 输入数: %d, 输出数: %d",
    tx.Version, len(tx.TxIn), len(tx.TxOut))

// 获取原始交易数据
rawTx, err := client.GetTxRaw(ctx, "交易ID")
if err != nil {
    log.Fatal(err)
}
log.Printf("原始交易大小: %d bytes", len(rawTx))
PSBT 交易创建
import "github.com/crazycloudcc/btcapis/types"

// 构建交易参数
txParams := &types.TxInputParams{
    FromAddress: []string{"tb1p..."},          // 发送地址
    ToAddress:   []string{"tb1q..."},          // 接收地址
    AmountBTC:   []float64{0.001},             // 金额 (BTC)
    FeeRate:     1.0,                          // 费率 (sat/vB)
    Locktime:    0,                            // 锁定时间
    Replaceable: true,                         // 支持 RBF
    Data:        "Hello Bitcoin",              // 可选数据 (OP_RETURN)
    PublicKey:   "公钥十六进制",                // 公钥
    ChangeAddress: "tb1p...",                  // 找零地址
}

// 创建 PSBT
psbtBase64, err := client.CreatePSBT(ctx, txParams)
if err != nil {
    log.Fatal(err)
}
log.Printf("PSBT: %s", psbtBase64)

// 完成签名并广播 (需要外部签名)
signedPSBT := "..." // 签名后的 PSBT
txid, err := client.FinalizePSBTAndBroadcast(ctx, signedPSBT)
if err != nil {
    log.Fatal(err)
}
log.Printf("交易已广播: %s", txid)
链信息查询
// 费率估算
fastRate, economyRate, err := client.EstimateFeeRate(ctx, 6)
if err != nil {
    log.Fatal(err)
}
log.Printf("快速费率: %.2f sat/vB, 经济费率: %.2f sat/vB", fastRate, economyRate)

// 获取最新区块哈希
bestHash, err := client.GetBestBlockHash(ctx)
if err != nil {
    log.Fatal(err)
}
log.Printf("最新区块: %s", bestHash)

// 查询区块信息
block, err := client.GetBlock(ctx, bestHash)
if err != nil {
    log.Fatal(err)
}
log.Printf("区块高度: %d, 交易数: %d", block.Height, len(block.Tx))

📋 完整功能列表

🏠 地址模块 (Address)
功能 方法 描述
余额查询 GetAddressBalance() 查询地址的确认/未确认余额
UTXO 查询 GetAddressUTXOs() 获取地址的未花费输出
地址解析 DecodeAddressToScriptInfo() 解析地址的详细脚本信息
脚本转换 DecodeAddressToPkScript() 地址转锁定脚本
类型识别 DecodeAddressToType() 识别地址类型
脚本解析 DecodePkScriptToAddressInfo() 脚本转地址信息
💸 交易模块 (Transaction)
功能 方法 描述
交易查询 GetTx() 获取交易详细信息
原始数据 GetTxRaw() 获取交易原始字节数据
PSBT 创建 CreatePSBT() 创建部分签名比特币交易
PSBT 完成 FinalizePSBTAndBroadcast() 完成签名并广播
交易广播 BroadcastRawTx() 广播原始交易
地址导入 ImportAddressAndPublickey() 导入地址和公钥
⛓️ 区块链模块 (Chain)
功能 方法 描述
费率估算 EstimateFeeRate() 估算交易费率
UTXO 查询 GetUTXO() 查询特定 UTXO 状态
区块统计 GetBlockCount() 获取区块链高度
最新区块 GetBestBlockHash() 获取最新区块哈希
区块哈希 GetBlockHash() 根据高度获取区块哈希
区块头 GetBlockHeader() 获取区块头信息
区块数据 GetBlock() 获取完整区块信息
🔧 脚本模块 (Script)
功能 方法 描述
脚本解析 DecodeScriptToOpcodes() 解析脚本为操作码
ASM 转换 DecodeScriptToASM() 脚本转汇编格式
类型检测 DecodePKScriptToType() 检测脚本类型

🏗️ 架构设计

BTCAPIs 采用三层架构模式,提供清晰的关注点分离:

┌─────────────────────────────────────────────────────────────┐
│                    门面层 (Facade)                          │
│              btcapis.go, *_facade.go                        │
├─────────────────────────────────────────────────────────────┤
│                    端口层 (Ports)                           │
│                chain/backend.go                             │
├─────────────────────────────────────────────────────────────┤
│                   适配器层 (Adapters)                       │
│         providers/bitcoindrpc, mempoolspace                │
└─────────────────────────────────────────────────────────────┘
核心模块
  • types/ - 核心数据类型定义 (地址、交易、UTXO 等)
  • internal/adapters/ - 数据源适配器 (Bitcoin Core, Mempool.space)
  • internal/address/ - 地址处理逻辑
  • internal/tx/ - 交易构建和管理
  • internal/chain/ - 区块链交互
  • internal/decoders/ - 编码解码工具

🌐 数据源配置

Bitcoin Core RPC
client := btcapis.New(
    "mainnet",
    "http://localhost:8332",  // RPC 地址
    "rpcuser",                // 用户名
    "rpcpassword",           // 密码
    30,                      // 超时秒数
)
Mempool.space API

自动根据网络配置:

  • 主网: https://mempool.space
  • 测试网: https://mempool.space/testnet
  • Signet: https://mempool.space/signet

📖 地址类型支持

地址类型 前缀 示例 支持状态
P2PKH (Legacy) 1... 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa ✅ 完整支持
P2SH (Script Hash) 3... 3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy ✅ 完整支持
P2WPKH (SegWit v0) bc1q... bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4 ✅ 完整支持
P2WSH (SegWit v0) bc1q... bc1qrp33g0q2c70qkn... ✅ 完整支持
P2TR (Taproot) bc1p... bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297 ✅ 完整支持

🔐 PSBT 工作流程

1. 创建未签名交易
psbtBase64, err := client.CreatePSBT(ctx, &types.TxInputParams{
    FromAddress:   []string{"发送地址"},
    ToAddress:     []string{"接收地址"},
    AmountBTC:     []float64{0.001},
    FeeRate:       1.0,
    ChangeAddress: "找零地址",
})
2. 外部签名 (如硬件钱包)
// 使用外部钱包签名 PSBT
signedPSBT := signWithExternalWallet(psbtBase64)
3. 完成并广播
txid, err := client.FinalizePSBTAndBroadcast(ctx, signedPSBT)

⚙️ 高级配置

自定义网络参数
import "github.com/crazycloudcc/btcapis/types"

// 设置当前网络
types.SetCurrentNetwork("testnet")
费率策略
// 获取推荐费率
fastRate, economyRate, err := client.EstimateFeeRate(ctx, 6)

// 使用自定义费率
txParams.FeeRate = 2.5  // sat/vB

🧪 测试示例

查看 examples/ 目录获取完整示例:

cd examples
go run main.go

主要测试场景:

  • 连接测试 - 验证 Bitcoin Core 和 Mempool.space 连接
  • 地址操作 - 余额查询、UTXO 管理
  • 交易创建 - PSBT 工作流程
  • 脚本解析 - 地址和脚本转换

📚 文档

详细文档位于 docs/ 目录:

🤝 兼容性

Go 版本支持
  • 最低要求: Go 1.23+
  • 推荐版本: Go 1.24+
依赖库
  • btcsuite/btcd - 比特币协议实现
  • 标准库 - 无其他外部依赖
btcd 生态集成

内部使用 btcd 库进行:

  • 交易编码/解码 (wire)
  • 脚本处理 (txscript)
  • 哈希计算 (chainhash)
  • 地址工具 (btcutil)

公共 API 使用自定义类型,确保向前兼容。

🛠️ 开发指南

本地开发
# 克隆项目
git clone https://github.com/crazycloudcc/btcapis.git
cd btcapis

# 安装依赖
go mod tidy

# 运行测试
go test ./...

# 运行示例
cd examples && go run main.go
代码质量
# 代码检查
golangci-lint run

# 格式化代码
gofmt -w .

# 模块整理
go mod tidy

📄 许可证

本项目采用 MIT 许可证

🌟 贡献

欢迎贡献代码!请参考:

  1. Fork 项目
  2. 创建功能分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送分支 (git push origin feature/AmazingFeature)
  5. 打开 Pull Request

📞 支持

🏷️ 版本历史

当前版本基于 Go modules go 1.23.0,支持:

  • ✅ 完整的地址类型支持 (P2PKH, P2SH, P2WPKH, P2WSH, P2TR)
  • ✅ PSBT v0 工作流程
  • ✅ 多数据源架构 (Bitcoin Core + Mempool.space)
  • ✅ RBF (Replace-By-Fee) 支持
  • ✅ OP_RETURN 数据嵌入
  • ✅ 智能费率估算

BTCAPIs - 构建现代比特币应用的可靠基础 🚀

Documentation

Overview

校验数据的基本格式.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func New

func New(cfg *Config) *Client

func NewWithElectrumX added in v0.2.0

func NewWithElectrumX(network string, rpc_url, rpc_user, rpc_pass string, electrumx_url string, timeout int) *Client

NewWithElectrumX 创建包含ElectrumX支持的客户端

func (*Client) BatchGetBalancesWithElectrumX added in v0.2.1

func (c *Client) BatchGetBalancesWithElectrumX(ctx context.Context, addresses []string, concurrent int) ([]types.AddressBalanceInfo, error)

批量查询所有地址余额

func (*Client) BroadcastRawTx added in v0.1.0

func (c *Client) BroadcastRawTx(ctx context.Context, rawtx []byte) (string, error)

广播签名

func (*Client) CheckFormatAddress added in v0.1.2

func (c *Client) CheckFormatAddress(s string) error

校验地址是否合法

func (*Client) CheckFormatBase64 added in v0.1.2

func (c *Client) CheckFormatBase64(s string) error

helper 校验base64字符串是否合法

func (*Client) CheckFormatHex added in v0.1.2

func (c *Client) CheckFormatHex(s string) error

helper 校验hex字符串是否合法

func (*Client) CheckFormatTxid added in v0.1.2

func (c *Client) CheckFormatTxid(s string) error

校验交易id是否合法

func (*Client) CreateNewWallet added in v0.2.0

func (c *Client) CreateNewWallet(ctx context.Context) (*types.WalletInfo, error)

创建新钱包

func (*Client) CreatePSBT

func (c *Client) CreatePSBT(ctx context.Context, inputParams *types.TxInputParams) (string, error)

创建PSBT预览交易数据(钱包未签名状态)

func (*Client) DecodeAddressToPkScript added in v0.1.0

func (c *Client) DecodeAddressToPkScript(addr string) ([]byte, error)

通过地址获取锁定脚本

func (*Client) DecodeAddressToScriptInfo added in v0.1.0

func (c *Client) DecodeAddressToScriptInfo(addr string) (*types.AddressScriptInfo, error)

通过地址获取地址的详细信息

func (*Client) DecodeAddressToType added in v0.1.0

func (c *Client) DecodeAddressToType(addr string) (types.AddressType, error)

通过地址获取类型

func (*Client) DecodePKScriptToType added in v0.1.0

func (c *Client) DecodePKScriptToType(pkScript []byte) (types.AddressType, error)

通过脚本获取类型

func (*Client) DecodePkScriptToAddressInfo added in v0.1.0

func (c *Client) DecodePkScriptToAddressInfo(pkScript []byte) (*types.AddressInfo, error)

通过脚本获取地址信息

func (*Client) DecodePkScriptToAsmString added in v0.1.0

func (c *Client) DecodePkScriptToAsmString(pkScript []byte) (ops []types.ScriptOp, asm string, err error)

解析脚本为操作码

func (*Client) DecodeRawTx

func (c *Client) DecodeRawTx(rawtx []byte) (*types.Tx, error)

解析一笔交易元数据 => 适用于外部直接输入交易元数据解析结构

func (*Client) DecodeRawTxString added in v0.1.1

func (c *Client) DecodeRawTxString(rawHex string) (*types.Tx, error)

解析一笔交易元数据 => 适用于外部直接输入交易元数据解析结构(十六进制字符串)

func (*Client) EstimateFeeRate

func (c *Client) EstimateFeeRate(ctx context.Context, targetBlocks int) (float64, float64, error)

EstimateFeeRate 估计手续费.

func (*Client) FilterAddressesWithBalanceWithElectrumX added in v0.2.1

func (c *Client) FilterAddressesWithBalanceWithElectrumX(ctx context.Context, addresses []string, concurrent int) ([]types.AddressBalanceInfo, error)

批量查询并过滤有余额的地址

func (*Client) FinalizePSBTAndBroadcast added in v0.1.0

func (c *Client) FinalizePSBTAndBroadcast(ctx context.Context, psbt string) (string, error)

上传经过钱包签名的PSBT数据并进行广播;

func (*Client) GetAddressBalance added in v0.1.0

func (c *Client) GetAddressBalance(ctx context.Context, addr string) (confirmed float64, mempool float64, err error)

GetAddressBalance 返回地址的确认余额和未确认余额.

func (*Client) GetAddressBalanceWithElectrumX added in v0.2.1

func (c *Client) GetAddressBalanceWithElectrumX(ctx context.Context, addr string) (confirmed float64, mempool float64, err error)

GetAddressBalanceWithElectrumX 返回地址的确认余额和未确认余额.

func (*Client) GetAddressBalanceWithElectrumXByPrivateKey added in v0.2.1

func (c *Client) GetAddressBalanceWithElectrumXByPrivateKey(ctx context.Context, privateKeyWIF string) (*types.AddressBalanceInfo, error)

通过私钥查询余额

func (*Client) GetAddressBalanceWithElectrumXByXPRV added in v0.2.1

func (c *Client) GetAddressBalanceWithElectrumXByXPRV(ctx context.Context, xprv string, numAddresses uint32) ([]types.AddressBalanceInfo, error)

通过扩展私钥查询余额

func (*Client) GetAddressUTXOs

func (c *Client) GetAddressUTXOs(ctx context.Context, addr string) ([]types.TxUTXO, error)

GetAddressUTXOs 返回地址拥有的UTXO.

func (*Client) GetTx

func (c *Client) GetTx(ctx context.Context, txid string) (*types.Tx, error)

查询交易信息 => 适用于通过txid查询详细交易信息

func (*Client) GetTxRaw

func (c *Client) GetTxRaw(ctx context.Context, txid string) ([]byte, error)

查询交易元数据

func (*Client) ImportAddressAndPublickey

func (c *Client) ImportAddressAndPublickey(ctx context.Context, address string, publickey string) error

上传钱包+publickey, 用于后续组装PSBT等数据, 后续需要在postgres创建映射;

func (*Client) TransferAllToNewAddress added in v0.2.2

func (c *Client) TransferAllToNewAddress(ctx context.Context, toAddress string, privateKeyWIF string, fromAddress string, feeRate float64) (string, error)

transfer all to new address

func (*Client) ValidateSignedPsbtBase64 added in v0.1.1

func (c *Client) ValidateSignedPsbtBase64(ctx context.Context, psbtBase64 string) (string, error)

校验已签名psbt的base64串

func (*Client) ValidateUnsignedPsbtBase64 added in v0.1.1

func (c *Client) ValidateUnsignedPsbtBase64(ctx context.Context, psbtBase64 string) error

校验psbt base64串是否合法

type Config added in v0.2.0

type Config struct {
	Network         string // 网络类型: mainnet, testnet, signet
	Timeout         int    // 超时时间(秒)
	RPCUrl          string // Bitcoin Core RPC服务器地址
	RPCUser         string // Bitcoin Core RPC用户名
	RPCPass         string // Bitcoin Core RPC密码
	MempoolSpaceUrl string // mempool.space API地址
	ElectrumXUrl    string // ElectrumX服务器地址
}

type TestClient

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

func NewTestClient

func NewTestClient(client *Client) *TestClient

func (*TestClient) GetBlockChainInfo added in v0.1.0

func (c *TestClient) GetBlockChainInfo(ctx context.Context) (*bitcoindrpc.ChainInfoDTO, error)

获取链信息

func (*TestClient) GetBlockCount added in v0.1.1

func (c *TestClient) GetBlockCount(ctx context.Context) (int64, error)

GetBlockCount

func (*TestClient) GetBlockStats added in v0.1.0

func (c *TestClient) GetBlockStats(ctx context.Context, height int64) (*bitcoindrpc.BlockStatsDTO, error)

获取区块统计信息

func (*TestClient) GetChainTips added in v0.1.0

func (c *TestClient) GetChainTips(ctx context.Context) ([]bitcoindrpc.ChainTipDTO, error)

获取链顶信息

func (*TestClient) GetNetworkInfo

func (c *TestClient) GetNetworkInfo(ctx context.Context) (*bitcoindrpc.NetworkInfoDTO, error)

获取节点网络信息

Directories

Path Synopsis
internal
adapters/bitcoindrpc
Package bitcoindrpc 提供Bitcoin Core JSON-RPC客户端
Package bitcoindrpc 提供Bitcoin Core JSON-RPC客户端
adapters/electrumx
ElectrumX API接口实现
ElectrumX API接口实现
adapters/mempoolapis
Package mempoolspace 提供mempool.space REST API客户端
Package mempoolspace 提供mempool.space REST API客户端
decoders
提供一些通用的解析函数
提供一些通用的解析函数
tx
pkg
示例代码备用参考: 锁定脚本/解锁脚本/自定义逻辑脚等
示例代码备用参考: 锁定脚本/解锁脚本/自定义逻辑脚等

Jump to

Keyboard shortcuts

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