neoTransaction

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2019 License: GPL-3.0 Imports: 9 Imported by: 0

README

Bitcoin Transaction Driver

        transaction_test.go测试案例说明
        Test_case1    : 花费单个P2PKH-UTXO
        Test_case2    : 花费多个P2PKH-UTXO
        Test_case3    : 花费单个P2WPKH-UTXO
        Test_case4    : 花费多个P2WPKH-UTXO
        Test_case5    : 花费P2PKH和P2WPKH混合的UTXO
        Test_case6    : 花费BECH32-UTXO
        Test_case7    : 花费BECH32-UTXO
        Test_case8    : 花费2-of-3多重签名UTXO,带隔离见证
        Test_case9    : 花费2-of-3多重签名UTXO,带隔离见证
        Test_case10   : 花费2-of-3多重签名UTXO,无隔离见证
        Test_case11   : 花费2-of-3多重签名UTXO,无隔离见证

其他类比特币验证须知

        需要修改的数据在txProfile.go里面
        可针对不同平台进行调整

当前支持

        交易单构建
        交易单签名
        交易单合并
        交易单验签

        现已支持全系地址,任意类型、数量、顺序的混合

TODO

        signSingal,anyoneCanPay...

用法:

创建空交易单 CreateEmptyRawTransaction
        前置条件:
                获取需要发送的utxo
                获取接收地址和找零地址
                确定手续费以及找零数额
        步骤:
                使用utxo的交易哈希(txid)和输出索引(vout)填充TxIn结构体的Prevout字段
                使用接收地址和找零地址以及对应数额填充TxOut结构体的Address字段和Amount字段
                确认交易单版本号
                确认交易单锁定时间
                确认交易是否可以追加手续费(replaceable)
                获取空交易单
        调用方式:
                CreateRawTransaction([]TxIn, []TxOut, transactionVersion, lockTime, replaceable)
        Tips:
                txid使用的是小端模式,即查询交易时的端序
                交易单版本号为目前的默认版本号02
创建用于签名的交易单哈希 CreateRawTransactionHashForSign
        前置条件:
                获取需要发送的utxo
                获取接收地址和找零地址
                确定手续费以及找零数额
        步骤:
                使用utxo的交易哈希(txid)和输出索引(vout)填充TxIn结构体的Prevout字段
                使用接收地址和找零地址以及对应数额填充TxOut结构体的Address字段和Amount字段
                确认交易单版本号
                确认交易单锁定时间
                确认交易是否可以追加手续费(replaceable)
                获取前置交易的锁定脚本
                确认签名类型
                获取交易单
        调用方式:
                CreateRawTransactionHashForSign([]TxIn []TxOut, transactionVersion, lockTime, replaceable, signType)
        Tips:
                txid使用的是小端模式,即查询交易时的端序
                交易单版本号为目前的默认版本号02
                签名类型一般为signAll
本地交易单签名 SignEmptyRawTransaction
        前置条件:
                获取空交易单emptyTrans
                获取utxo的锁定脚本以及脚本对应的私钥
        步骤:
                使用锁定脚本与私钥填充TxUnlock结构体
                确定签名类型
        调用方式:
                SignRawTransaction(emptyTrans, []TxUnlock, sigType)
        Tips:
                TxUnlock结构体数组的顺序应该与空交易单的utxo的txid顺序保持一致
                签名类型一般为signAll
客户端交易单哈希签名 SignRawTransactionHash
        前置条件:
                获得用于签名的交易单哈希
        步骤:
                获取前置交易的锁定脚本对应的私钥,填充TxUnlock结构体
                获取签名
        调用方式:
                SignRawTransactionHash(transForSig, []TxUnlock)
        Tips:
                TxUnlock结构体数组的顺序应该与空交易单的utxo的txid顺序保持一致
合并交易单 InsertSignaturesToEmptyRawTransaction
        前置条件:
                获得空交易单
                获得签名
        步骤:
                合并
        调用方式:
                InsertSignaturesToEmptyRawTransaction(emptyTrans, []SignaturePubkkey)
        Tips:
                签名数据结构体数组的顺序应该与utxo的txid顺序保持一致
交易单验签 VerifyRawTransaction
        前置条件:    
                获取签名后的交易单signedTrans
                获取utxo的锁定脚本
        步骤:
                使用锁定脚本填充TxUnlock结构体
        调用方式:
                VerifyRawTransaction(signedTrans, []TxUnlock)
        Tips:
                TxUnlock结构体数组的顺序应该与交易单的utxo的txid顺序保持一致

Documentation

Index

Constants

View Source
const (
	Attribute_Usage_ContractHash   = 0   // 合约脚本哈希 length 32
	Attribute_Usage_ECDH02         = 2   // 用于ECDH密钥交换的公钥 length 32
	Attribute_Usage_ECDH03         = 3   // 用于ECDH密钥交换的公钥 length 32
	Attribute_Usage_Script         = 32  // 交易额外的验证 length 20
	Attribute_Usage_Vote           = 48  // 投票payload length 需要指定 最大255个字节
	Attribute_Usage_DescriptionUrl = 129 // 描述说明的URL length 需要指定 最大255个字节
	Attribute_Usage_Description    = 144 // 说明 length 需要指定 最大255个字节

)
View Source
const (
	PublicKeySize        = 33
	DefaultTxVersion     = uint32(0)
	MaxScriptElementSize = 520
)
View Source
const (
	NeoAssetId    = "c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b" // NEO 的 asset id
	NeoGasAssetId = "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7" // NEOGAS 的 asset id
)
View Source
const (
	SequenceFinal        = uint32(0xFFFFFFFF)
	SequenceMaxBip125RBF = uint32(0xFFFFFFFD)
)
View Source
const (
	OpPushBytes64 = byte(0x40)
	OpPushBytes33 = byte(0x21)
	OpCheckSig    = byte(0xac)

	OpPush2         = byte(0x60)
	OpCheckMultiSig = byte(0xae)
)

Variables

View Source
var (
	ErrorInvalidAddress = errors.New("Invalid address!")

	BTCBech32Alphabet = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"

	CHARSET_REV = []int8{}/* 128 elements not displayed */

)
View Source
var (
	MinerTransaction        = TransactionType{"MinerTransaction", 0x00, 0}
	IssueTransaction        = TransactionType{"IssueTransaction", 0x02, 0}
	ClaimTransaction        = TransactionType{"ClaimTransaction", 0x03, 0}
	DataFile                = TransactionType{"DataFile", 0x12, 0}
	EnrollmentTransaction   = TransactionType{"EnrollmentTransaction", 0x20, 0}
	RegisterTransaction     = TransactionType{"RegisterTransaction", 0x40, 0}
	ContractTransaction     = TransactionType{"ContractTransaction", 0x80, 0}
	RecordTransaction       = TransactionType{"RecordTransaction", 0x81, 0}
	StateTransaction        = TransactionType{"StateTransaction", 0x90, 0}
	StateUpdateTransaction  = TransactionType{"StateUpdateTransaction", 0x90, 0}
	StateUpdaterTransaction = TransactionType{"StateUpdaterTransaction", 0x91, 0}
	DestroyTransaction      = TransactionType{"DestroyTransaction", 0x18, 0}
	PublishTransaction      = TransactionType{"PublishTransaction", 0xd0, 0}
	InvocationTransaction   = TransactionType{"InvocationTransaction", 0xd1, 1}
)
View Source
var (
	AttrContractHash   = AttributeType{"ContractHash", 0x00, 32, 32}
	AttrECDH02         = AttributeType{"ECDH02", 0x02, 32, 32}
	AttrECDH03         = AttributeType{"ECDH03", 0x03, 32, 32}
	AttrScript         = AttributeType{"Script", 0x20, 20, 20}
	AttrVote           = AttributeType{"Vote", 0x30, 0, 0}
	AttrDescriptionUrl = AttributeType{"DescriptionUrl", 0x81, 255, 0}
	AttrDescription    = AttributeType{"Description", 0x90, 65535, 0}

	AttrHash1  = AttributeType{"Hash1", 0xa1, 32, 32}
	AttrHash2  = AttributeType{"Hash2", 0xa2, 32, 32}
	AttrHash3  = AttributeType{"Hash3", 0xa3, 32, 32}
	AttrHash4  = AttributeType{"Hash4", 0xa4, 32, 32}
	AttrHash5  = AttributeType{"Hash5", 0xa5, 32, 32}
	AttrHash6  = AttributeType{"Hash6", 0xa6, 32, 32}
	AttrHash7  = AttributeType{"Hash7", 0xa7, 32, 32}
	AttrHash8  = AttributeType{"Hash8", 0xa8, 32, 32}
	AttrHash9  = AttributeType{"Hash9", 0xa9, 32, 32}
	AttrHash10 = AttributeType{"Hash10", 0xaa, 32, 32}
	AttrHash11 = AttributeType{"Hash11", 0xab, 32, 32}
	AttrHash12 = AttributeType{"Hash12", 0xac, 32, 32}
	AttrHash13 = AttributeType{"Hash13", 0xad, 32, 32}
	AttrHash14 = AttributeType{"Hash14", 0xae, 32, 32}
	AttrHash15 = AttributeType{"Hash15", 0xaf, 32, 32}

	AttrRemark   = AttributeType{"Remark", 0xf0, 65535, 0}
	AttrRemark1  = AttributeType{"Remark1", 0xf1, 65535, 0}
	AttrRemark2  = AttributeType{"Remark2", 0xf2, 65535, 0}
	AttrRemark3  = AttributeType{"Remark3", 0xf3, 65535, 0}
	AttrRemark4  = AttributeType{"Remark4", 0xf4, 65535, 0}
	AttrRemark5  = AttributeType{"Remark5", 0xf5, 65535, 0}
	AttrRemark6  = AttributeType{"Remark6", 0xf6, 65535, 0}
	AttrRemark7  = AttributeType{"Remark7", 0xf7, 65535, 0}
	AttrRemark8  = AttributeType{"Remark8", 0xf8, 65535, 0}
	AttrRemark9  = AttributeType{"Remark9", 0xf9, 65535, 0}
	AttrRemark10 = AttributeType{"Remark10", 0xfa, 65535, 0}
	AttrRemark11 = AttributeType{"Remark11", 0xfb, 65535, 0}
	AttrRemark12 = AttributeType{"Remark12", 0xfc, 65535, 0}
	AttrRemark13 = AttributeType{"Remark13", 0xfd, 65535, 0}
	AttrRemark14 = AttributeType{"Remark14", 0xfe, 65535, 0}
	AttrRemark15 = AttributeType{"Remark15", 0xff, 65535, 0}
)
View Source
var (
	CurveOrder     = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B, 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x41}
	HalfCurveOrder = []byte{0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x5D, 0x57, 0x6E, 0x73, 0x57, 0xA4, 0x50, 0x1D, 0xDF, 0xE9, 0x2F, 0x46, 0x68, 0x1B, 0x20, 0xA0}
)
View Source
var (
	ErrorInvalidBase58String = errors.New("invalid base58 string")
)

Errors

View Source
var (
	NeocoinAlphabet = NewAlphabet("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
)

Alphabet: copy from https://en.wikipedia.org/wiki/Base58

Functions

func Bech32Decode

func Bech32Decode(address string) ([]byte, error)

func Bech32Encode

func Bech32Encode(prefix, alphabet string, payload []byte) string

func BuildInvocation

func BuildInvocation(signBytes []byte) []byte

构建参数脚本 PushBytes64(0x40) + 签名,多签重复添加 0x40+签名 signBytes : 签名内容

func BuildVerification

func BuildVerification(pubKey string) ([]byte, error)

构建验证脚本 = PushByte33(0x21) + 签名对应的公钥 + CheckSig(0xac) pubKey : 签名对应的公钥

func CreateEmptyRawTransaction

func CreateEmptyRawTransaction(txType TransactionType, vins []Vin, vouts []Vout, attrs []Attribute) (string, error)

创建未签名的空交易 txType : 交易类型 vins : 交易输入 vouts : 交易输出 attrs : 交易附加属性

func CreateMultiSig

func CreateMultiSig(required byte, pubkeys [][]byte, SegwitON bool, addressPrefix AddressPrefix) (string, string, error)

func Decode

func Decode(input string, alphabet *Alphabet) ([]byte, error)

Decode docode with custom alphabet

func DecodeCheck

func DecodeCheck(address string) ([]byte, []byte, error)

return prefix + hash + error

func Encode

func Encode(input []byte, alphabet *Alphabet) string

Encode encode with custom alphabet

func EncodeCheck

func EncodeCheck(prefix []byte, hash []byte) string

func InsertSignatureIntoEmptyTransaction

func InsertSignatureIntoEmptyTransaction(rawTx string, txHashes []TxHash) ([]byte, error)

合并签名数据到空交易 rawTx : 原始空交易 txHashes : 签名信息

func VerifyRawTransaction

func VerifyRawTransaction(signedRawTx string) bool

验证交易签名 signedRawTx : 添加签名信息的原始交易

Types

type AddressPrefix

type AddressPrefix struct {
	P2PKHPrefix  []byte
	P2WPKHPrefix []byte
	P2SHPrefix   []byte
	Bech32Prefix string
}

type Alphabet

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

Alphabet The base58 alphabet object.

func NewAlphabet

func NewAlphabet(alphabet string) *Alphabet

NewAlphabet create a custom alphabet from 58-length string. Note: len(rune(alphabet)) must be 58.

func (Alphabet) String

func (alphabet Alphabet) String() string

Alphabet's string representation

type Attribute

type Attribute struct {
	Attr AttributeType
	Data string
}

func (*Attribute) String

func (attr *Attribute) String() string

type AttributeType

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

交易附加参数类型

type MultiTx

type MultiTx struct {
	Pubkey  string
	SigType byte
	SigPub  SignaturePubkey
}

type NormalTx

type NormalTx struct {
	Address string
	SigType byte
	SigPub  SignaturePubkey
}

type Script

type Script struct {
	Invocation   string
	Verification string
}

func (*Script) String

func (s *Script) String() string

type SignaturePubkey

type SignaturePubkey struct {
	Signature []byte
	Pubkey    []byte
}

func SignRawTransaction

func SignRawTransaction(rawTx string, prikey []byte) (*SignaturePubkey, error)

签名原始交易 rawTx : 组装获得的原始交易 priKey : 签名的私钥

type Transaction

type Transaction struct {
	Type       byte
	Version    byte
	Attributes []TxAttribute
	Vouts      []TxOut
	Vins       []TxIn
	Scripts    []TxScript
}

func DecodeRawTransaction

func DecodeRawTransaction(txBytes []byte) (*Transaction, error)

交易反序列化

func (*Transaction) String

func (t *Transaction) String() string

type TransactionType

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

交易类型

type TxAttribute

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

func (*TxAttribute) String

func (tx *TxAttribute) String() string

type TxHash

type TxHash struct {
	Hash      string
	NRequired byte
	Normal    *NormalTx
	Multi     []MultiTx
}

func CreateRawTransactionHashForSig

func CreateRawTransactionHashForSig(txHex string) ([]TxHash, error)

func (TxHash) GetMultiTxPubkeys

func (tx TxHash) GetMultiTxPubkeys() []string

func (TxHash) GetNormalTxAddress

func (tx TxHash) GetNormalTxAddress() string

func (TxHash) GetTxHashHex

func (tx TxHash) GetTxHashHex() string

func (TxHash) IsMultisig

func (tx TxHash) IsMultisig() bool

type TxIn

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

func (TxIn) GetTxID

func (in TxIn) GetTxID() string

获取交易ID

func (TxIn) GetVout

func (in TxIn) GetVout() uint16

获取对应索引

func (*TxIn) String

func (ti *TxIn) String() string

type TxOut

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

func (*TxOut) String

func (to *TxOut) String() string

type TxScript

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

交易脚本

func NewEmptyTxScript

func NewEmptyTxScript(invocation, verification []byte) *TxScript

func (*TxScript) GetPubKeyByVerificationScript

func (ts *TxScript) GetPubKeyByVerificationScript() ([]byte, error)

获取验证脚本中的公钥

func (*TxScript) GetSignatureByInvocationScript

func (ts *TxScript) GetSignatureByInvocationScript() ([]byte, error)

获取调用参数中的签名内容

func (*TxScript) String

func (ts *TxScript) String() string

type Vin

type Vin struct {
	TxID string
	Vout uint16
}

func (*Vin) String

func (vin *Vin) String() string

type Vout

type Vout struct {
	Asset   string
	Address string
	Value   uint64
}

func (*Vout) String

func (vout *Vout) String() string

Jump to

Keyboard shortcuts

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