gobbc

package module
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: May 28, 2020 License: BSD-3-Clause Imports: 21 Imported by: 0

README

BBC 签名库 in go

修改自: https://github.com/bigbangcore/OffLineSignLib

多签部分参考core代码实现

TODO

  • 增加更多的测试用例
  • 增加持续集成测试
  • 代码审计

To 开发人员

Bigbang 的release notes不够详细,不能依靠release notes判断改动,在升级新包的时候务必执行完整的集成测试

  • 当前目录下 go test
  • qa 目录下 go test

增加新的特性也务必确保测试用例的覆盖率,至少需要确保核心功能可以正常使用

测试:make test 集成测试:make qaTest all: make all

Features

  • 生成密钥对、地址
  • 交易序列化和解析
  • 使用私钥签名
  • 多签地址交易签名

Missing features

  • 部分模版地址签名支持

其他

开发过程中的一些小插曲参考 doc 目录

Documentation

Index

Constants

View Source
const (
	PubkeyHexLen     = 32*2 + 1
	PrivkeyHexLen    = 32*2 + 1
	PubkeyAddressLen = 57 + 1
	Uint256HexLen    = 65
)

some len const

View Source
const Precision = 1000000
View Source
const TemplateDataSpliter = ","

TemplateDataSpliter 使用,分隔多个template data

Variables

This section is empty.

Functions

func Base32Encode

func Base32Encode(md32 []uint8) string

Base32Encode copied from c++

func ConvertAddress2pubk added in v1.0.7

func ConvertAddress2pubk(address string) (string, error)

ConvertAddress2pubk .

func CopyReverse

func CopyReverse(bs []byte) []byte

CopyReverse copy and reverse []byte

func CopyReverseThenEncodeHex

func CopyReverseThenEncodeHex(bs []byte) string

CopyReverseThenEncodeHex 复制[]byte,反转后hex.EncodeToString

func CryptoMultiSign

func CryptoMultiSign(pubks [][]byte, privk ed25519.PrivateKey, msg []byte, currentSig []byte) ([]byte, error)

CryptoMultiSign pubks 公钥数组,privk 私钥, msg 待签名数据, sig 已有签名

func GetPubKeyAddress

func GetPubKeyAddress(pubk string) (string, error)

GetPubKeyAddress Get Address hex string from public key hex string

func JSONIndent

func JSONIndent(v interface{}) string

JSONIndent pretty json

func ParsePrivkHex

func ParsePrivkHex(privkHex string) (ed25519.PrivateKey, error)

ParsePrivkHex BBC 私钥解析为ed25519.PrivateKey

func ParsePublicKeyHex

func ParsePublicKeyHex(hexedPubK string) ([]byte, error)

ParsePublicKeyHex 解析私钥为实际使用的seed

func PrivateKeyHex2Seed

func PrivateKeyHex2Seed(hexedPrivk string) ([]byte, error)

PrivateKeyHex2Seed 解析私钥为实际使用的seed

func Seed2pubk

func Seed2pubk(seed []byte) ([]byte, error)

Seed2pubk .

func Seed2pubkString

func Seed2pubkString(seed []byte) (string, error)

Seed2pubkString .

func Seed2string

func Seed2string(seed []byte) string

Seed2string 私钥字符串

func UntilError

func UntilError(fns ...func() error) error

UntilError execute all func until error returned

func UtilDataEncoding added in v1.0.7

func UtilDataEncoding(data string) string

UtilDataEncoding 将tx data 进行编码

Types

type AddrKeyPair

type AddrKeyPair struct {
	Addr  string
	Privk string
	Pubk  string
}

AddrKeyPair 地址、私钥、公钥

func MakeKeyPair

func MakeKeyPair() (AddrKeyPair, error)

MakeKeyPair .

type DataDetail added in v1.0.7

type DataDetail struct {
	UUID     string
	UnixTime uint32
	Data     string
}

DataDetail .

func UtilDataDecoding added in v1.0.7

func UtilDataDecoding(data string) (DataDetail, error)

UtilDataDecoding .

type MultisigInfo

type MultisigInfo struct {
	Hex     string
	M, N    uint8 //m-n签名,N名成员需要至少M个签名
	Members []MultisigMember
}

MultisigInfo 多签信息

func ParseMultisigTemplateHex

func ParseMultisigTemplateHex(hexData string) (*MultisigInfo, error)

ParseMultisigTemplateHex 解析多签地址hex, hex decode 前2byte代表类型,通常是0200 (不确定是0200或者0020) 接下来1+8 byte为M,N 接下来有N个33 (32为公钥,1为weight) ref to: https://github.com/bigbangcore/BigBang/wiki/%E5%A4%9A%E9%87%8D%E7%AD%BE%E5%90%8D |---2---|---1---|---8---|---33*n---| | type | M | N | keys... |

func (MultisigInfo) Pubks

func (mi MultisigInfo) Pubks() [][]byte

Pubks 参与签名的公钥列表

func (MultisigInfo) SignTemplatePart

func (mi MultisigInfo) SignTemplatePart() []byte

SignTemplatePart 签名时签名的前半部分

type MultisigMember

type MultisigMember struct {
	Pub    []byte
	Weight uint8
}

MultisigMember .

type RawTransaction

type RawTransaction struct {
	Version         uint16
	Typ             uint16 //type > typ
	Timestamp       uint32
	LockUntil       uint32
	HashAnchorBytes [32]byte `json:"-"` // fork id
	SizeIn          uint64   //input 数量
	Input           []byte   `json:"-"`
	Prefix          uint8    //addr prefix
	AddressBytes    [32]byte `json:"-"` // binary data (caller do not care about this field, you just care hex field)
	Amount          int64
	TxFee           int64
	SizeOut         uint64
	VchData         []byte `json:"-"` // binary (caller do not care about this field, you just care hex field)
	SizeSign        uint64 // binary sign data size, ref: https://github.com/bigbangcore/BigBang/wiki/IO-Stream#stdvector-stdmap-stdstring
	SignBytes       []byte `json:"-"` // [template data]sig
}

RawTransaction 实际的序列话数据结构 注意:数据类型不要更改(序列化时对类型有依赖)

func (*RawTransaction) Encode

func (rtx *RawTransaction) Encode(encodeSignData bool) (string, error)

Encode .

func (*RawTransaction) EncodeBytes

func (rtx *RawTransaction) EncodeBytes(encodeSignData bool) ([]byte, error)

EncodeBytes .

func (*RawTransaction) SignWithPrivateKey added in v1.0.3

func (rtx *RawTransaction) SignWithPrivateKey(templateDataList, privkHex string) error

SignWithPrivateKey 用私钥签名 templateDataList: 使用[,]分隔的模版数据列表, - 对于不需要模版数据的交易传入空字符串即可, - 如果传入了模版数据签名后会将模版数据按照顺序放在签名前面, - 如果传入的模版数据检测到多重签名则在签名时使用多重签名机制

通常,在from为模版地址时需要传入from的模版数据,可以通过rpc validateaddress 获取(data.addressdata.templatedata.hex) 当to地址为vote类型模版地址时需要传入to地址模版数据 特别的,只有1种情况需要传入2个模版地址:delegate类型模版的owner为多签地址,从该地址转出时需要传入:delegate模版数据,多签模版数据 (基于上面一种情况,如果转出地址为vote template可能还需要提供vote template data, 一共3个😂,这个未经测试、验证)

下面列出常见的场景: 一般公钥地址转出(到非vote template)->(不需要模版数据) 一般公钥地址投票时->投票模版数据 一般公钥投票赎回时->投票模版数据 多签地址签名(转账到一般地址)->多签模版数据 从dpos委托模版地址转出->委托模版数据 从dpos委托模版(owner为多签)地址转出->委托模版数据+多签模版数据 从pow挖矿模版地址转出->pow挖矿模版地址

注意:签名逻辑不对模版数据进行严格合理的校验,因为离线环境下无法感知模版数据的有效性,调用方需自行确保参数正确

func (RawTransaction) ToTransaction added in v1.0.7

func (rtx RawTransaction) ToTransaction(includeSignData bool) Transaction

ToTransaction .

func (*RawTransaction) Txid

func (rtx *RawTransaction) Txid() ([32]byte, error)

Txid 计算txid

type TW

type TW struct {
	*testing.T
	// contains filtered or unexported fields
}

TW testing.T wrap

func (*TW) Continue

func (tw *TW) Continue(_continue bool) *TW

Continue 断言失败时是否继续测试(不执行FailNow)

func (*TW) Copy

func (tw *TW) Copy() *TW

Copy .

func (*TW) Equal

func (tw *TW) Equal(expected, actual interface{}, args ...interface{}) *TW

Equal reflect.DeepEqual

func (*TW) IsZero

func (tw *TW) IsZero(v interface{}, args ...interface{}) *TW

IsZero if flag == false, fatal

func (*TW) Nil

func (tw *TW) Nil(x interface{}, args ...interface{}) *TW

Nil if x not nil, fatal

func (*TW) NotZero

func (tw *TW) NotZero(v interface{}, args ...interface{}) *TW

NotZero if flag == false, fatal

func (*TW) True

func (tw *TW) True(flag bool, args ...interface{}) *TW

True if flag == false, fatal

type TXBuilder added in v1.0.7

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

TXBuilder .

func NewTXBuilder added in v1.0.7

func NewTXBuilder() *TXBuilder

func (*TXBuilder) AddInput added in v1.0.7

func (b *TXBuilder) AddInput(txid string, vout uint8) *TXBuilder

AddInput 参考listunspent,确保输入金额满足amount

func (*TXBuilder) Build added in v1.0.7

func (b *TXBuilder) Build() (*RawTransaction, error)

Build .

func (*TXBuilder) SetAddress added in v1.0.7

func (b *TXBuilder) SetAddress(add string) *TXBuilder

SetAddress 转账地址,目前只支持公钥地址

func (*TXBuilder) SetAmount added in v1.0.7

func (b *TXBuilder) SetAmount(amount float64) *TXBuilder

SetAmount 转账金额

func (*TXBuilder) SetAnchor added in v1.0.7

func (b *TXBuilder) SetAnchor(anchor string) *TXBuilder

SetAnchor 锚定分支id

func (*TXBuilder) SetData added in v1.0.7

func (b *TXBuilder) SetData(data []byte) *TXBuilder

SetData 原始data设置,参考 UtilDataEncoding

func (*TXBuilder) SetDataWithUUID added in v1.0.7

func (b *TXBuilder) SetDataWithUUID(_uuid string, timestamp int64, data string) *TXBuilder

SetDataWithUUID 指定uuid,timestamp,data

func (*TXBuilder) SetFee added in v1.0.7

func (b *TXBuilder) SetFee(fee float64) *TXBuilder

SetFee 手续费,目前0.01,如果带data则0.03, 额外需咨询BBC

func (*TXBuilder) SetLockUntil added in v1.0.7

func (b *TXBuilder) SetLockUntil(lockUntil int) *TXBuilder

SetLockUntil lock until

func (*TXBuilder) SetStringData added in v1.0.7

func (b *TXBuilder) SetStringData(data string) *TXBuilder

SetStringData 自动编码数据,自动生成uuid和时间戳

func (*TXBuilder) SetTimestamp added in v1.0.7

func (b *TXBuilder) SetTimestamp(timestamp int) *TXBuilder

SetTimestamp 当前时间戳

func (*TXBuilder) SetVersion added in v1.0.7

func (b *TXBuilder) SetVersion(v int) *TXBuilder

SetVersion 当前版本 1

type TXData added in v1.0.5

type TXData struct {
	TplHex string `json:"tpl_hex,omitempty"` //成员信息,通过rpc validateaddress (多签模版地址) 取到的值的ret.Addressdata.Templatedata.Hex
	TxHex  string `json:"tx_hex,omitempty"`  //encoded tx data
}

TXData 包含了原始交易数据和需要的模版数据,模版数据使用,(英文逗号)分隔

func (*TXData) DecodeString added in v1.0.8

func (data *TXData) DecodeString(jsonHex string) error

DecodeString parse jsonHex set value to data

func (*TXData) EncodeString added in v1.0.8

func (data *TXData) EncodeString() (string, error)

EncodeString json marshal + hex encode

type TemplateType added in v1.0.3

type TemplateType int16

TemplateType 模版类型

const (
	TemplateTypeMin TemplateType = iota
	TemplateTypeWeighted
	TemplateTypeMultisig //多重签名
	TemplateTypeFork
	TemplateTypeProof    //pow
	TemplateTypeDelegate //dpos
	TemplateTypeExchange
	TemplateTypeVote //dpos投票
	TemplateTypePayment
	TemplateTypeMax
)

模版类型

func GetTemplateType added in v1.0.3

func GetTemplateType(templateData string) TemplateType

GetTemplateType 如果解析失败则返回TemplateTypeMin(0)

func (TemplateType) String added in v1.0.5

func (typ TemplateType) String() string

type Transaction

type Transaction struct {
	RawTransaction
	HashAnchor string // hex string([65]byte)
	Address    string // hex string ([64 + 1]byte)
	Sign       string // hex string
	Vin        []Vin
	Data       string
}

Transaction . TODO 增加vout

func DecodeRawTransaction

func DecodeRawTransaction(txData string, decodeSignData bool) (*Transaction, error)

DecodeRawTransaction hexed tx parse

type Vin added in v1.0.7

type Vin struct {
	Txid string
	Vout int
}

Jump to

Keyboard shortcuts

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