adapter

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 6 Imported by: 0

README

wallet-adapter

github.com/godaddy-x/wallet-adapter — 多主链钱包适配器基础库,提供交易单构建、广播与区块扫描等能力。同系列子类库如 github.com/godaddy-x/wallet-adapter-eth 等可依赖本库实现各链适配。

能力概览

  • 统一交易类型RawTransactionPendingSignTx(待签名交易单)、TransactionSummaryRawTransaction
  • 核心流程:入口为 flow(adapter.BuildTransaction 创建待签名交易单 PendingSignTx、adapter.SendTransaction 验证+广播);decoder 只负责构建/验签/提交 rawTx,签名由外部 MPC 完成
  • 区块扫描BlockScanner 接口与 BlockScannerBase,支持按高度扫描区块、持续扫块循环、补扫单高度、提取交易与回执
  • 链抽象ChainAdapterTransactionDecoderBlockScannerAddressDecoder;可选 WalletDAI 回调查询钱包/账户/地址等
  • 链配置config 包提供 Configer 接口与 JSON 解析(KVFromJSONFile/KVFromJSONContentMapConfig),供 AssetsConfig.LoadAssetsConfig 等复用
  • 智能合约(可选):SmartContractDecoderABIDAI:代币余额、ABI 调用/创建/广播、合约元数据;GetSmartContractDecoder(symbol) 按链获取
  • 多链注册:按 symbol 注册/查询:RegAdapterGetAdapterGetTransactionDecoderGetBlockScannerGetAddressDecoderGetSmartContractDecoder

项目结构(按 package 划分)

wallet-adapter/
├── go.mod
├── README.md
├── doc.go                    # 根包 adapter 说明
├── export.go                 # 类型与函数统一导出
├── types/                    # 数据类型与错误码
│   ├── types.go              # 交易、账户、地址等核心类型
│   ├── errors.go             # 错误码与 AdapterError
│   ├── symbol.go             # 链/币种信息 SymbolInfo
│   ├── contract.go           # 智能合约相关 TokenBalance、SmartContractRawTransaction、SmartContractCallResult、ABIInfo
│   └── block.go              # 扫块相关 BlockHeader、TxExtractData、ExtractDataItem、ContractReceiptItem、Balance、UnscanRecord、SmartContractReceipt 等
├── wallet/                   # 钱包数据访问接口(与 types 解耦)
│   └── wallet.go             # Wallet、WalletDAI、WalletDAIBase
├── decoder/                  # 解码器(交易 + 地址 + 智能合约)
│   ├── transaction.go        # TransactionDecoder、TransactionDecoderBase
│   ├── address.go            # AddressDecoder、AddressDecoderBase
│   └── contract.go           # SmartContractDecoder、SmartContractDecoderBase、ABIDAI
├── config/                   # 链配置通用接口与 JSON 解析
│   ├── configer.go           # Configer、MapConfig(供 LoadAssetsConfig 使用)
│   └── json.go               # KVFromJSONFile、KVFromJSONContent
├── chain/                    # 链适配器与注册表
│   ├── adapter.go            # ChainAdapter、ChainAdapterBase
│   ├── config.go             # AssetsConfig、AssetsConfigBase
│   └── registry.go           # RegAdapter、GetAdapter、GetTransactionDecoder 等
├── flow/                     # 构建与广播流程(入口:BuildTransaction/BuildSummaryTransaction/SendTransaction)
│   └── flow.go               # 调 decoder 构建 rawTx,再调 wrapper.SignPendingTxData 得 PendingSignTx;广播前校验 DataSign/TradeSign
├── scanner/                  # 区块扫描器
│   ├── scanner.go            # BlockScanner 接口与 Base(按高度扫块、持续循环、插队扫描、地址余额查询)
│   └── SCANNER.md            # 扫块器详细设计文档
  • 推荐import "github.com/godaddy-x/wallet-adapter" 后使用 adapter.BuildTransactionadapter.RawTransactionadapter.RegAdapter 等。
  • 按需引用子包:如 import "github.com/godaddy-x/wallet-adapter/types"import "github.com/godaddy-x/wallet-adapter/wallet"import "github.com/godaddy-x/wallet-adapter/decoder"import "github.com/godaddy-x/wallet-adapter/config"import "github.com/godaddy-x/wallet-adapter/chain" 等。

接入新链步骤

  1. 实现 TransactionDecoder

    • CreateRawTransaction:根据 RawTransaction 构建 rawHex/fees/sigParts 等(只负责构建 rawTx)
    • CreateSummaryRawTransactionWithError:汇总场景生成多笔 RawTransactionWithError
    • VerifyRawTransaction:校验 rawTx(合并 SignerList 后)签名
    • SubmitRawTransaction:提交已签名 rawTx 到链上节点并返回 Transaction
    • 可选:SignRawTransaction(本地签名时实现)、GetRawTransactionFeeRateEstimateRawTransactionFee
  2. 实现 ChainAdapter

    • 实现 SymbolInfo(Symbol、Decimal 等)
    • GetTransactionDecoder() 返回上述 decoder
    • (可选)GetAddressDecoder() 返回地址解析器;GetBlockScanner() 返回扫块器;GetSmartContractDecoder() 返回智能合约解析器
  3. 注册

    • init() 或启动时:adapter.RegAdapter("SYMBOL", yourAdapter)
  4. (可选)实现 BlockScanner

    • 嵌入 scanner.Base,实现 ScanBlockWithResult(按高度扫块并返回结果)、ScanBlockOnce(单高度补扫)、RunScanLoop(持续扫块循环)、ScanBlockPrioritize(插队扫描)、ResetScanHeight(重置游标)、GetBalanceByAddress(地址余额查询)等。
    • 通过 SetBlockScanTargetFunc 设置扫描目标查询,SetTokenMetadataFunc 注入合约元数据查询,供扫块时补充合约信息。
    • GetBalanceByAddress 可使用 QueryBalancesConcurrent 辅助函数实现并发查询。
  5. (可选)实现 AddressDecoder

    • 嵌入 decoder.AddressDecoderBase,按需实现:PublicKeyToAddressAddressVerifyAddressDecodeAddressEncode、WIF、多签、CustomCreateAddress 等;未实现的方法由 Base 返回“未实现”。
  6. (可选)实现 SmartContractDecoder(见 decoder/contract.go

    • 嵌入 decoder.SmartContractDecoderBase,按需实现:GetTokenBalanceByAddressCallSmartContractABICreateSmartContractRawTransactionSubmitSmartContractRawTransactionGetABIInfoSetABIInfoGetTokenMetadata;链不支持合约则 GetSmartContractDecoder() 返回 nil。

使用示例

import "github.com/godaddy-x/wallet-adapter"

// 1. 获取某链的 TransactionDecoder(需已 RegAdapter)
decoder, err := adapter.GetTransactionDecoder("BTC")
if err != nil {
    return err
}

// 2. 构造原始交易单
rawTx := &adapter.RawTransaction{
    Coin:    adapter.Coin{Symbol: "BTC"},
    Account: account,           // *adapter.AssetsAccount
    To:      map[string]string{toAddress: amount},
    FeeRate: feeRate,
    Required: 1,
}

// 3. 调用 flow 构建待签名交易单(decoder 构建 rawTx → wrapper.SignPendingTxData 填 DataSign/TradeSign → 返回 PendingSignTx)
//    wrapper 实现 adapter.WalletDAI,BuildTransaction/SendTransaction 时不可为 nil
pendingTx, err := adapter.BuildTransaction(decoder, wrapper, rawTx)
if err != nil {
    return err
}
// ... 调用 MPC 签名,填充 pendingTx.SignerList ...

// 4. 广播(内部会复算 DataSign/TradeSign 校验 Data 未被篡改,再验签并提交)
tx, err := adapter.SendTransaction(decoder, wrapper, pendingTx)
if err != nil {
    return err
}
// tx.TxID, tx.Status 等

本库为 github.com/godaddy-x 下的基础适配器模块,以币类转账为主(交易构建/广播、区块扫描、地址解析),并可选扩展智能合约decoder/contract.goSmartContractDecodertypes/contract.go 的合约相关类型)。链实现(如 github.com/godaddy-x/wallet-adapter-eth)、MPC 签名库等可依赖本库;不包含 HD 钱包、具体链实现等。

License

见项目根目录 LICENSE。

Documentation

Overview

Package adapter 多主链适配器基础框架,提供交易构建、广播与区块扫描等核心能力。

子包划分:

  • types — 数据类型与错误码(含 BlockHeader、TxExtractData、Balance 等扫块类型)
  • wallet — 钱包数据访问:Wallet、WalletDAI、WalletDAIBase(供 flow/decoder 回调查询钱包/账户/地址等)
  • decoder — 解码器:TransactionDecoder(transaction.go)、AddressDecoder(address.go)、SmartContractDecoder(contract.go,可选),各带 Base 基类;签名由外部 MPC 提供
  • config — 链配置通用接口 Configer、MapConfig 与 JSON 解析(KVFromJSONFile、KVFromJSONContent),供 AssetsConfig.LoadAssetsConfig 复用
  • chain — 链适配器 ChainAdapter 与注册表 RegAdapter/GetAdapter/GetTransactionDecoder/GetBlockScanner/GetAddressDecoder/GetSmartContractDecoder;AssetsConfig、SmartContractDecoder 可选
  • flow — 构建与广播流程 BuildTransaction、BuildSummaryTransaction、SendTransaction(可传入 WalletDAI 回调查询)
  • scanner — 区块扫描器 BlockScanner 与 Base(按高度扫块、持续循环、插队扫描、地址余额查询)

本包对上述子包做统一导出,便于调用方 import "github.com/godaddy-x/wallet-adapter" 使用。

使用示例:

decoder, _ := adapter.GetTransactionDecoder("BTC")
rawTx := &adapter.RawTransaction{ Coin: adapter.Coin{Symbol: "BTC"}, Account: account, To: map[string]string{toAddr: amount}, Required: 1 }
pendingTx, _ := adapter.BuildTransaction(decoder, wrapper, rawTx) // 入口为 flow,返回 PendingSignTx;wrapper 不可为 nil
tx, _ := adapter.SendTransaction(decoder, wrapper, pendingTx)

统一导出:types/wallet/decoder/config/chain/flow/scanner 的类型与函数,便于 import "github.com/godaddy-x/wallet-adapter" 一站式使用。 config 包(Configer、JSON 解析)需单独 import "github.com/godaddy-x/wallet-adapter/config" 使用,供 LoadAssetsConfig 等复用。

Index

Constants

View Source
const (
	TxStatusSuccess               = types.TxStatusSuccess
	TxStatusFail                  = types.TxStatusFail
	BalanceModelTypeAddress       = types.BalanceModelTypeAddress
	BalanceModelTypeAccount       = types.BalanceModelTypeAccount
	ScanTargetTypeAccountAddress  = types.ScanTargetTypeAccountAddress
	ScanTargetTypeAccountAlias    = types.ScanTargetTypeAccountAlias
	ScanTargetTypeContractAddress = types.ScanTargetTypeContractAddress
	ScanTargetTypeContractAlias   = types.ScanTargetTypeContractAlias
	ScanTargetTypeAddressPubKey   = types.ScanTargetTypeAddressPubKey
	ScanTargetTypeAddressMemo     = types.ScanTargetTypeAddressMemo
	// 智能合约 Raw 类型
	TxRawTypeHex                         = types.TxRawTypeHex
	TxRawTypeJSON                        = types.TxRawTypeJSON
	TxRawTypeBase64                      = types.TxRawTypeBase64
	SmartContractCallResultStatusFail    = types.SmartContractCallResultStatusFail
	SmartContractCallResultStatusSuccess = types.SmartContractCallResultStatusSuccess
)
View Source
const (
	ErrInsufficientBalanceOfAccount      = types.ErrInsufficientBalanceOfAccount
	ErrInsufficientBalanceOfAddress      = types.ErrInsufficientBalanceOfAddress
	ErrInsufficientFees                  = types.ErrInsufficientFees
	ErrDustLimit                         = types.ErrDustLimit
	ErrCreateRawTransactionFailed        = types.ErrCreateRawTransactionFailed
	ErrSignRawTransactionFailed          = types.ErrSignRawTransactionFailed
	ErrVerifyRawTransactionFailed        = types.ErrVerifyRawTransactionFailed
	ErrSubmitRawTransactionFailed        = types.ErrSubmitRawTransactionFailed
	ErrInsufficientTokenBalanceOfAddress = types.ErrInsufficientTokenBalanceOfAddress
	ErrAccountNotFound                   = types.ErrAccountNotFound
	ErrAddressNotFound                   = types.ErrAddressNotFound
	ErrContractNotFound                  = types.ErrContractNotFound
	ErrAddressEncodeFailed               = types.ErrAddressEncodeFailed
	ErrAddressDecodeFailed               = types.ErrAddressDecodeFailed
	ErrNonceInvalid                      = types.ErrNonceInvalid
	ErrCallFullNodeAPIFailed             = types.ErrCallFullNodeAPIFailed
	ErrNetworkRequestFailed              = types.ErrNetworkRequestFailed
	ErrUnknownException                  = types.ErrUnknownException
	ErrSystemException                   = types.ErrSystemException
)

错误码

Variables

View Source
var NewBlockScannerBase = scanner.NewBlockScannerBase

Functions

func GetRandomSecure

func GetRandomSecure(l int) ([]byte, error)

func ListSymbols

func ListSymbols() []string

func RegAdapter

func RegAdapter(symbol string, a ChainAdapter)

Types

type ABIDAI

type ABIDAI = decoder.ABIDAI

type ABIInfo

type ABIInfo = types.ABIInfo

----- types 导出 -----

type AdapterError

type AdapterError = types.AdapterError

----- types 导出 -----

func ConvertError

func ConvertError(err error) *AdapterError

func Errorf

func Errorf(code uint64, format string, a ...interface{}) *AdapterError

func NewError

func NewError(code uint64, text string) *AdapterError

type Address

type Address = types.Address

----- types 导出 -----

type AddressDecoder

type AddressDecoder = decoder.AddressDecoder

func GetAddressDecoder

func GetAddressDecoder(symbol string) (AddressDecoder, error)

type AddressDecoderBase

type AddressDecoderBase = decoder.AddressDecoderBase

type AssetsAccount

type AssetsAccount = types.AssetsAccount

----- types 导出 -----

type AssetsConfig

type AssetsConfig = chain.AssetsConfig

type AssetsConfigBase

type AssetsConfigBase = chain.AssetsConfigBase

type Balance

type Balance = types.Balance

----- types 导出 -----

type BalanceModelType

type BalanceModelType = types.BalanceModelType

----- types 导出 -----

type BalanceQueryFunc

type BalanceQueryFunc = scanner.BalanceQueryFunc

type BlockHeader

type BlockHeader = types.BlockHeader

扫块相关

type BlockScanTargetFunc

type BlockScanTargetFunc = scanner.BlockScanTargetFunc

type BlockScanner

type BlockScanner = scanner.BlockScanner

----- scanner 导出 -----

func GetBlockScanner

func GetBlockScanner(symbol string) (BlockScanner, error)

type BlockScannerBase

type BlockScannerBase = scanner.Base

type BlockchainSyncStatus

type BlockchainSyncStatus = types.BlockchainSyncStatus

----- types 导出 -----

type ChainAdapter

type ChainAdapter = chain.ChainAdapter

----- chain 导出 -----

func GetAdapter

func GetAdapter(symbol string) (ChainAdapter, error)

type ChainAdapterBase

type ChainAdapterBase = chain.ChainAdapterBase

type Coin

type Coin = types.Coin

----- types 导出 -----

type ContractReceiptItem

type ContractReceiptItem = types.ContractReceiptItem

----- types 导出 -----

type ExtractDataItem

type ExtractDataItem = types.ExtractDataItem

----- types 导出 -----

type FeesSupportAccount

type FeesSupportAccount = types.FeesSupportAccount

----- types 导出 -----

type KeySignature

type KeySignature = types.KeySignature

----- types 导出 -----

type PendingSignTx

type PendingSignTx = types.PendingSignTx

----- types 导出 -----

func BuildSummaryTransaction

func BuildSummaryTransaction(d TransactionDecoder, wrapper WalletDAI, sumRawTx *SummaryRawTransaction) ([]*PendingSignTx, error)

func BuildTransaction

func BuildTransaction(d TransactionDecoder, wrapper WalletDAI, rawTx *RawTransaction) (*PendingSignTx, error)

----- flow 导出 -----

type RawTransaction

type RawTransaction = types.RawTransaction

----- types 导出 -----

type RawTransactionWithError

type RawTransactionWithError = types.RawTransactionWithError

----- types 导出 -----

type Recharge

type Recharge = types.Recharge

----- types 导出 -----

type ScanTarget

type ScanTarget = types.ScanTarget

----- types 导出 -----

type ScanTargetParam

type ScanTargetParam = types.ScanTargetParam

----- types 导出 -----

func NewScanTargetParamForAddress

func NewScanTargetParamForAddress(symbol, address string) ScanTargetParam

func NewScanTargetParamForAlias

func NewScanTargetParamForAlias(symbol, alias string) ScanTargetParam

type ScanTargetResult

type ScanTargetResult = types.ScanTargetResult

----- types 导出 -----

type SmartContract

type SmartContract = types.SmartContract

----- types 导出 -----

type SmartContractCallResult

type SmartContractCallResult = types.SmartContractCallResult

----- types 导出 -----

type SmartContractDecoder

type SmartContractDecoder = decoder.SmartContractDecoder

func GetSmartContractDecoder

func GetSmartContractDecoder(symbol string) (SmartContractDecoder, error)

type SmartContractDecoderBase

type SmartContractDecoderBase = decoder.SmartContractDecoderBase

type SmartContractEvent

type SmartContractEvent = types.SmartContractEvent

----- types 导出 -----

type SmartContractRawTransaction

type SmartContractRawTransaction = types.SmartContractRawTransaction

----- types 导出 -----

type SmartContractReceipt

type SmartContractReceipt = types.SmartContractReceipt

----- types 导出 -----

type SummaryRawTransaction

type SummaryRawTransaction = types.SummaryRawTransaction

----- types 导出 -----

type SymbolInfo

type SymbolInfo = types.SymbolInfo

----- types 导出 -----

type SymbolInfoBase

type SymbolInfoBase = types.SymbolInfoBase

----- types 导出 -----

type TokenBalance

type TokenBalance = types.TokenBalance

智能合约解析相关

type Transaction

type Transaction = types.Transaction

----- types 导出 -----

func SendTransaction

func SendTransaction(d TransactionDecoder, wrapper WalletDAI, pendingTx *PendingSignTx) (*Transaction, error)

type TransactionDecoder

type TransactionDecoder = decoder.TransactionDecoder

----- decoder 导出 -----

func GetTransactionDecoder

func GetTransactionDecoder(symbol string) (TransactionDecoder, error)

type TransactionDecoderBase

type TransactionDecoderBase = decoder.TransactionDecoderBase

type TxExtractData

type TxExtractData = types.TxExtractData

----- types 导出 -----

func NewTxExtractData

func NewTxExtractData() *TxExtractData

type TxInput

type TxInput = types.TxInput

----- types 导出 -----

type TxOutPut

type TxOutPut = types.TxOutPut

----- types 导出 -----

type UnscanRecord

type UnscanRecord = types.UnscanRecord

----- types 导出 -----

func NewUnscanRecord

func NewUnscanRecord(height uint64, txID, reason, symbol string) *UnscanRecord

type WalletDAI

type WalletDAI = wallet.WalletDAI

----- types 导出 -----

type WalletDAIBase

type WalletDAIBase = wallet.WalletDAIBase

----- types 导出 -----

Directories

Path Synopsis
Package chain 链适配器接口与注册表,提供 ChainAdapter 及按 symbol 的 RegAdapter/GetAdapter/GetTransactionDecoder/GetBlockScanner/GetAddressDecoder/GetSmartContractDecoder/ListSymbols。
Package chain 链适配器接口与注册表,提供 ChainAdapter 及按 symbol 的 RegAdapter/GetAdapter/GetTransactionDecoder/GetBlockScanner/GetAddressDecoder/GetSmartContractDecoder/ListSymbols。
Package config 提供链配置的通用接口与 JSON 解析,供各子类适配器(如 github.com/godaddy-x/wallet-adapter-eth)复用。
Package config 提供链配置的通用接口与 JSON 解析,供各子类适配器(如 github.com/godaddy-x/wallet-adapter-eth)复用。
地址解析器:编解码、校验、WIF、多签、自定义创建地址。
地址解析器:编解码、校验、WIF、多签、自定义创建地址。
Package flow 交易构建与广播流程;签名由外部 MPC 提供,本包负责构建待签交易单与提交广播。
Package flow 交易构建与广播流程;签名由外部 MPC 提供,本包负责构建待签交易单与提交广播。
Package scanner 区块扫描器接口与基类,负责按高度扫描区块、提取交易/回执并返回结果。
Package scanner 区块扫描器接口与基类,负责按高度扫描区块、提取交易/回执并返回结果。
Package types 区块扫描相关数据类型
Package types 区块扫描相关数据类型
Package wallet 钱包数据访问接口(WalletDAI),供 TransactionDecoder 在构建/验证时回调查询,与 types 解耦。
Package wallet 钱包数据访问接口(WalletDAI),供 TransactionDecoder 在构建/验证时回调查询,与 types 解耦。

Jump to

Keyboard shortcuts

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