common

package
v1.75.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: BSD-3-Clause Imports: 20 Imported by: 20 modules, 54 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package common 实现钱包基础功能包

Index

Constants

View Source
const (
	// KdfIterations pbkdf2 迭代次数, 参考 OWASP 对 PBKDF2-SHA256 的建议值
	KdfIterations = 210000
	// KdfSaltLen 随机盐长度
	KdfSaltLen = 16
	// KdfKeyLen 派生出的 AES-256 密钥长度
	KdfKeyLen = 32
	// KdfVersion 新格式版本号, 后续调整 KDF 参数时递增
	KdfVersion = byte(1)
)

钱包加密密钥派生相关常量。

历史格式直接把用户口令零填充为 32 字节当作 AES 密钥,没有任何拉伸, 拿到 wallet.db 即可以「一次 AES 运算」的代价离线爆破口令。 新格式改用 pbkdf2-sha256 + 随机盐派生密钥,并在密文前写入魔数标识, 使解密侧能够区分新旧格式,从而在不破坏历史数据的前提下升级安全强度。

Variables

View Source
var (
	// MagicPrivKey 新版私钥密文魔数
	MagicPrivKey = []byte("C33K")
	// MagicSeed 新版 seed 密文魔数
	MagicSeed = []byte("C33S")
)
View Source
var (
	// QueryData 查询接口数据全局对象
	QueryData = types.NewQueryData("On_")
	// PolicyContainer 钱包业务容器
	PolicyContainer = make(map[string]WalletBizPolicy)
)

Functions

func CBCDecrypterPrivkey

func CBCDecrypterPrivkey(password []byte, privkey []byte) []byte

CBCDecrypterPrivkey 使用钱包的password对私钥进行aes cbc解密,返回解密后的privkey。

依次尝试三种格式, 保证历史钱包数据可以继续读取:

  1. 新格式 MagicPrivKey + version + salt + iv + ciphertext (pbkdf2 派生密钥)
  2. 旧格式 iv(16) + ciphertext (口令零填充为密钥, 随机 iv)
  3. 最初格式 ciphertext, iv = key[:16] (口令零填充为密钥, 固定 iv)

func CBCEncrypterPrivkey

func CBCEncrypterPrivkey(password []byte, privkey []byte) []byte

CBCEncrypterPrivkey 使用钱包的password对私钥进行aes cbc加密,返回加密后的privkey。

新格式: MagicPrivKey(4) + version(1) + salt(16) + iv(16) + ciphertext, 密钥由 pbkdf2 派生, 不再直接使用口令明文。旧数据仍可由 CBCDecrypterPrivkey 读取。

func CalcAccountKey

func CalcAccountKey(timestamp string, addr string) []byte

CalcAccountKey 用于所有Account账户的输出list,需要安装时间排序

func CalcAddrKey

func CalcAddrKey(addr string) []byte

CalcAddrKey 通过addr地址查询Account账户信息

func CalcAirDropIndex

func CalcAirDropIndex() []byte

CalcAirDropIndex 通过钱包Seed指定index生成的空投地址

func CalcEncryptionFlag

func CalcEncryptionFlag() []byte

CalcEncryptionFlag 加密标志Key

func CalcLabelKey

func CalcLabelKey(label string) []byte

CalcLabelKey 通过label查询Account账户信息

func CalcPasswordHash

func CalcPasswordHash() []byte

CalcPasswordHash 密码hash的Key

func CalcTxKey

func CalcTxKey(key string) []byte

CalcTxKey 通过height*100000+index 查询Tx交易信息 key:Tx:height*100000+index

func CalcWalletSeed

func CalcWalletSeed() []byte

CalcWalletSeed 钱包Seed的Key

func CalckeyEncryptionCompFlag

func CalckeyEncryptionCompFlag() []byte

CalckeyEncryptionCompFlag 加密比较标志Key

func DeriveKey added in v1.70.0

func DeriveKey(password, salt []byte) []byte

DeriveKey 由口令与盐派生 AES 密钥, 结果带缓存。

func HasSeedMagic added in v1.70.0

func HasSeedMagic(data []byte) bool

HasSeedMagic 判断 seed 密文是否为新版 KDF 格式

func Init

func Init(wallet WalletOperate, sub map[string][]byte)

Init 初始化所有已经注册的钱包业务

func LegacyKey added in v1.70.0

func LegacyKey(password []byte) []byte

LegacyKey 复现历史的「口令零填充」密钥派生方式, 仅用于解密旧数据。

func NewSalt added in v1.70.0

func NewSalt() ([]byte, error)

NewSalt 生成随机盐

func RegisterPolicy

func RegisterPolicy(key string, policy WalletBizPolicy)

RegisterPolicy 注册钱包业务策略接口

Types

type AddrInfo

type AddrInfo struct {
	Index  uint32 `json:"index,omitempty"`
	Addr   string `json:"addr,omitempty"`
	Pubkey string `json:"pubkey,omitempty"`
}

AddrInfo 通过seed指定index创建的账户信息,目前主要用于空投地址

type MineStatusReport

type MineStatusReport interface {
	IsAutoMining() bool
	// 返回挖矿买票锁的状态
	IsTicketLocked() bool
	PolicyName() string
}

MineStatusReport 挖矿操作状态

type Store

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

Store 钱包通用数据库存储类,实现对钱包账户数据库操作的基本实现

func NewStore

func NewStore(db db.DB) *Store

NewStore 新建存储对象

func (*Store) Close

func (store *Store) Close()

Close 关闭数据库

func (*Store) DelAccountByLabel

func (store *Store) DelAccountByLabel(label string)

DelAccountByLabel 根据标签名称,删除对应的账号信息

func (*Store) FreeBlockBatch added in v1.65.0

func (store *Store) FreeBlockBatch()

FreeBlockBatch free

func (*Store) Get

func (store *Store) Get(key []byte) ([]byte, error)

Get 取值

func (*Store) GetAccountByAddr

func (store *Store) GetAccountByAddr(addr string) (*types.WalletAccountStore, error)

GetAccountByAddr 根据地址获取账号信息

func (*Store) GetAccountByLabel

func (store *Store) GetAccountByLabel(label string) (*types.WalletAccountStore, error)

GetAccountByLabel 根据标签获取账号信息

func (*Store) GetAccountByPrefix

func (store *Store) GetAccountByPrefix(addr string) ([]*types.WalletAccountStore, error)

GetAccountByPrefix 根据前缀获取账号信息列表

func (*Store) GetAccountByte

func (store *Store) GetAccountByte(update bool, addr string, account *types.WalletAccountStore) ([]byte, error)

GetAccountByte 获取账号byte类型

func (*Store) GetAirDropIndex

func (store *Store) GetAirDropIndex() (string, error)

GetAirDropIndex 获取指定index的空投地址

func (*Store) GetBlockBatch added in v1.65.0

func (store *Store) GetBlockBatch(sync bool) db.Batch

GetBlockBatch 新建批处理操作对象接口

func (*Store) GetDB

func (store *Store) GetDB() db.DB

GetDB 获取数据库操作接口

func (*Store) GetEncryptionFlag

func (store *Store) GetEncryptionFlag() int64

GetEncryptionFlag 获取加密方式

func (*Store) GetTxDetailByIter

func (store *Store) GetTxDetailByIter(TxList *types.ReqWalletTransactionList) (*types.WalletTxDetails, error)

GetTxDetailByIter 迭代获取从指定key:height*100000+index 开始向前或者向后查找指定count的交易

func (*Store) GetWalletVersion

func (store *Store) GetWalletVersion() int64

GetWalletVersion 获取wallet数据库的版本号

func (*Store) HasSeed

func (store *Store) HasSeed() (bool, error)

HasSeed 判断钱包是否已经保存seed

func (*Store) NewBatch

func (store *Store) NewBatch(sync bool) db.Batch

NewBatch 新建批处理操作对象接口

func (*Store) NewListHelper

func (store *Store) NewListHelper() *db.ListHelper

NewListHelper 新建列表复制操作对象

func (*Store) Set

func (store *Store) Set(key []byte, value []byte) (err error)

Set 设置值

func (*Store) SetAirDropIndex

func (store *Store) SetAirDropIndex(airDropIndex *AddrInfo) error

SetAirDropIndex 存储指定index的空投地址信息

func (*Store) SetEncryptionFlag

func (store *Store) SetEncryptionFlag(batch db.Batch) error

SetEncryptionFlag 设置加密方式标志

func (*Store) SetPasswordHash

func (store *Store) SetPasswordHash(password string, batch db.Batch) error

SetPasswordHash 保存密码哈希

func (*Store) SetWalletAccount

func (store *Store) SetWalletAccount(update bool, addr string, account *types.WalletAccountStore) error

SetWalletAccount 保存钱包账户信息

func (*Store) SetWalletAccountInBatch

func (store *Store) SetWalletAccountInBatch(update bool, addr string, account *types.WalletAccountStore, newbatch db.Batch) error

SetWalletAccountInBatch 保存钱包账号信息

func (*Store) SetWalletVersion

func (store *Store) SetWalletVersion(ver int64) error

SetWalletVersion 升级数据库的版本号

func (*Store) VerifyPasswordHash

func (store *Store) VerifyPasswordHash(password string) bool

VerifyPasswordHash 检查密码有效性

type WalletBizPolicy

type WalletBizPolicy interface {
	// Init 初始化钱包业务策略,在使用前调用
	Init(walletBiz WalletOperate, sub []byte)
	// OnAddBlockTx 当区块被增加确认时调用本函数
	// block: 被增加的区块详细信息
	// tx: 区块增加的交易信息
	// index: 交易信息在区块上的索引为止,从0开始计数
	// dbbatch: 数据库批量操作接口
	// 返回值为提供给外部快速检索的钱包详细信息,如果内部已经处理,不需要外部处理的情况下,可以返回nil
	OnAddBlockTx(block *types.BlockDetail, tx *types.Transaction, index int32, dbbatch db.Batch) *types.WalletTxDetail
	// OnDeleteBlockTx 当区块被回退删除时调用本函数
	// block: 被回退的区块详细信息
	// tx: 区块回退的交易信息
	// index: 交易信息在区块上的索引为止,从0开始计数
	// dbbatch: 数据库批量操作接口
	// 返回值为提供给外部快速检索的钱包详细信息,如果内部已经处理,不需要外部处理的情况下,可以返回nil
	OnDeleteBlockTx(block *types.BlockDetail, tx *types.Transaction, index int32, dbbatch db.Batch) *types.WalletTxDetail
	// SignTransaction 针对特殊的交易,按照新的签名方式签名
	// key 签名的私钥信息
	// req 需要签名交易流信息
	// needSysSign 表示是否需要继续走系统流程的签名,true表示继续,false表示已经完成签名,不需要系统处理
	// signtx 签名成功后,保存签名成功的交易字符串
	// err 错误信息
	SignTransaction(key crypto.PrivKey, req *types.ReqSignRawTx) (needSysSign bool, signtx string, err error)
	// OnCreateNewAccount 当用户在钱包中新建账户时,将调用本接口函数
	OnCreateNewAccount(acc *types.Account)
	// OnImportPrivateKey 当用户导入私钥时,将调用本函数
	OnImportPrivateKey(acc *types.Account)
	OnWalletLocked()
	OnWalletUnlocked(WalletUnLock *types.WalletUnLock)
	OnAddBlockFinish(block *types.BlockDetail)
	OnDeleteBlockFinish(block *types.BlockDetail)
	OnClose()
	OnSetQueueClient()
	Call(funName string, in types.Message) (ret types.Message, err error)
}

WalletBizPolicy 细分钱包业务逻辑的街口

type WalletOperate

type WalletOperate interface {
	RegisterMineStatusReporter(reporter MineStatusReport) error

	GetAPI() client.QueueProtocolAPI
	GetDBStore() db.DB
	GetSignType() int
	GetCoinType() uint32
	GetPassword() string
	GetBlockHeight() int64
	GetRandom() *rand.Rand
	GetWalletDone() chan struct{}
	GetLastHeader() *types.Header
	GetTxDetailByHashs(ReqHashes *types.ReqHashes)
	GetWaitGroup() *sync.WaitGroup
	GetAllPrivKeys() ([]crypto.PrivKey, error)
	GetWalletAccounts() ([]*types.WalletAccountStore, error)
	GetPrivKeyByAddr(addr string) (crypto.PrivKey, error)
	GetConfig() *types.Wallet
	GetBalance(addr string, execer string) (*types.Account, error)

	IsWalletLocked() bool
	IsClose() bool
	IsCaughtUp() bool
	AddrInWallet(addr string) bool

	CheckWalletStatus() (bool, error)
	Nonce() int64

	WaitTx(hash []byte) *types.TransactionDetail
	WaitTxs(hashes [][]byte) (ret []*types.TransactionDetail)
	SendTransaction(payload types.Message, execer []byte, priv crypto.PrivKey, addressID int32, to string) (hash []byte, err error)
	SendToAddress(priv crypto.PrivKey, addressID int32, addrto string, amount int64, note string, Istoken bool, tokenSymbol string) (*types.ReplyHash, error)
}

WalletOperate 钱包对业务插件提供服务的操作接口

Jump to

Keyboard shortcuts

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