Documentation
¶
Overview ¶
示例代码备用参考: 锁定脚本/解锁脚本/自定义逻辑脚等
Package types 网络相关类型定义 包含比特币网络节点信息、连接状态、网络统计等网络层相关的数据结构
Index ¶
- Variables
- func OpcodeName(op byte) string
- func SetCurrentNetwork(net string)
- type AddressInfo
- type AddressScriptInfo
- type AddressType
- type BRC20Action
- type Hash32
- type Network
- type OrdinalsEnvelope
- type OrdinalsRecord
- type Runestone
- type ScriptOp
- type TapControlBlock
- type Tx
- type TxIn
- type TxInputParams
- type TxOut
- type TxOutPoint
- type TxUTXO
- type TxWitness
- type VarInt
Constants ¶
This section is empty.
Variables ¶
var CurrentNetworkParams *chaincfg.Params = &chaincfg.MainNetParams // 当前网络参数
Functions ¶
Types ¶
type AddressInfo ¶
type AddressInfo struct {
PKScript []byte // 原始脚本
Typ AddressType // 地址类型
Cls txscript.ScriptClass // 脚本类型
ReqSigs int // 需要签名数(多签时有意义)
Addresses []string // 可能为 0/1/N
}
AddressInfo 结构体:存储地址解析后的信息
type AddressScriptInfo ¶
type AddressScriptInfo struct {
Address string // 地址
Typ AddressType // 地址类型
Cls txscript.ScriptClass // 脚本类型
ScriptPubKeyHex []byte // 脚本哈希 => PKScript
ScriptAsm string // 脚本汇编
// “哈希/程序”层面(地址能直接给出的)
PubKeyHashHex []byte // P2PKH: 20 bytes
RedeemScriptHashHex []byte // P2SH: 20 bytes
// SegWit
IsWitness bool // 是否为见证地址
WitnessVersion int // 见证版本:0(SegWit v0)、1(Taproot)、-1(非SegWit)
WitnessProgramHex []byte // v0: 20/32 bytes; v1+: 32 bytes
WitnessProgramLen int // 见证数据长度:20字节或32字节
BechEncoding string // bech32 / bech32m
// Taproot
TaprootOutputKeyHex []byte // 等同于 witness program (v=1, 32B x-only pubkey)
}
AddressScriptInfo 结构体:存储地址解析后的脚本信息 包含脚本类型、各种哈希值、见证版本等关键信息
type AddressType ¶
type AddressType string
AddressType 标注常见地址/脚本族群。
const ( AddrP2PK AddressType = "p2pk" AddrP2PKH AddressType = "p2pkh" AddrP2SH AddressType = "p2sh" AddrP2WPKH AddressType = "p2wpkh" AddrP2WSH AddressType = "p2wsh" AddrP2TR AddressType = "p2tr" AddrUnknown AddressType = "unknown" )
type BRC20Action ¶
type BRC20Action struct {
Op string `json:"op"` // 操作类型
Tick string `json:"tick"` // 代币符号
Amt string `json:"amt,omitempty"` // 数量
Max string `json:"max,omitempty"` // 最大数量
Lim string `json:"lim,omitempty"` // 限制
}
BRC20Action 结构体:存储 BRC-20 动作
type Hash32 ¶
type Hash32 [32]byte
Hash32 是 32 字节哈希(双 SHA256 结果)。 约定:内部按“比特币内部顺序”存(通常等同于小端存放), JSON 显示时按“大端十六进制字符串”(常见 txid 书写)输出。
func (*Hash32) UnmarshalJSON ¶
type OrdinalsEnvelope ¶
type OrdinalsEnvelope struct {
ContentType string `json:"content_type,omitempty"` // 若 key=0x01 存在,ASCII 解码
BodyHex string `json:"body_hex,omitempty"` // key=0x00 的所有分片合并后的十六进制
Records []OrdinalsRecord `json:"records"` // 完整 TLV(有序)
}
OrdinalsEnvelope 为还原后的 envelope 数据
type OrdinalsRecord ¶
OrdinalsRecord 表示一条 TLV 记录(key/value 都用 hex 表示;方便外部再做自定义解析)
type Runestone ¶
type Runestone struct {
BodyHex string `json:"body_hex"` // 十六进制编码的 payload
}
Runestone 结构体:存储 Runestone 数据
type ScriptOp ¶
type ScriptOp struct {
Op string `json:"op"` // 操作码
DataHex string `json:"data_hex,omitempty"` // 数据十六进制
DataLen int `json:"data_len,omitempty"` // 数据长度,单位:字节
}
ScriptOp 表示一条脚本指令;若为数据推送,DataHex/DataLen 会被填充。
type TapControlBlock ¶
type TapControlBlock struct {
Header byte `json:"header"` // 原始头字节
LeafVersion byte `json:"leaf_version"` // header & 0xfe
Parity int `json:"parity"` // (header >> 7) & 1
InternalKey string `json:"internal_key"` // 32B x-only pubkey (hex)
MerkleHashes []string `json:"merkle_hashes"` // 0或多段32B(hex)
}
TapControlBlock 解析后的控制块信息(P2TR 脚本路径花费使用)
type Tx ¶
type Tx struct {
Version int32 // 交易版本
LockTime uint32 // 交易锁定时间
TxIn []TxIn // 交易输入
TxOut []TxOut // 交易输出
}
Tx:完整的交易对象(不显式包含 marker/flag 字段;是否为 segwit 由 TxIn[].Witness 是否存在决定) 序列化顺序(无见证):Version | vinCount | vin[...] | voutCount | vout[...] | LockTime 序列化顺序(有见证):Version | 0x00 | 0x01 | vinCount | vin[...] | voutCount | vout[...] | witnesses(for each vin) | LockTime
type TxIn ¶
type TxIn struct {
PreviousOutPoint TxOutPoint // 上一笔交易的输出点
Sequence uint32 // 交易序列号
ScriptSig []byte // scriptSig
Witness TxWitness // 若任一输入 Witness 非空,序列化需写入 marker/flag,并在所有 TxOut 之后写入全部 Witness
}
TxIn:交易输入 - PreviousOutPoint:被花费的 UTXO 引用 - ScriptSig:非隔离见证路径下的解锁脚本(如 P2PKH 的 <sig><pubkey> 等) - Sequence:nSequence;影响 RBF(<0xffffffff-1)与 CSV;默认 0xffffffff - Witness:隔离见证路径下的见证栈(P2WPKH/P2WSH/P2TR 等)
func (TxIn) MarshalJSON ¶
type TxInputParams ¶
type TxInputParams struct {
FromAddress []string `json:"from_address"` // 来源地址数组-可以是多个, 但是目前版本只支持1个地址
ToAddress []string `json:"to_address"` // 目标地址数组-可以是多个, 但是要和Amount一一对应
AmountBTC []float64 `json:"amount"` // 金额-单位BTC
FeeRate float64 `json:"fee_rate"` // 费用率(sat/vB)
Locktime int64 `json:"locktime"` // 锁定时间(秒)
Replaceable bool `json:"replaceable"` // 是否可替换RBF
Data string `json:"data"` // 可选 交付附加数据
PublicKey string `json:"public_key"` // 公钥 => 从OKX获取, 后续要删除, 改用其他方式录入钱包
ChangeAddress string `json:"change_address"` // 找零地址
}
通用的转账交易输入参数
type TxOut ¶
type TxOut struct {
Value int64 // satoshi
PkScript []byte // scriptPubKey
ScriptType string // 可选, 用于显示 解析出的脚本类别
Address string // 可选, 用于显示 解析得到的人类可读地址
}
TxOut:交易输出 - Value:satoshi 数(int64,允许负数编码但务必在业务层校验非负) - PkScript:锁定脚本(scriptPubKey),如 P2PKH 的 OP_DUP OP_HASH160 <20b> OP_EQUALVERIFY OP_CHECKSIG
func (TxOut) MarshalJSON ¶
type TxOutPoint ¶
type TxOutPoint struct {
Hash Hash32 // 上一笔交易的 txid(逻辑上大端显示;写入时小端序)
Index uint32 // vout 索引,从 0 开始
}
OutPoint 唯一标识一个已存在的交易输出:<txid, vout>