btcLikeTxDriver

package
v1.1.10 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2019 License: GPL-3.0 Imports: 11 Imported by: 0

README

Bitcoin Transaction Driver

        transaction_test.go测试案例说明
        Test_case1    : from 公钥哈希地址 to  公钥哈希地址
        Test_case2    : from 脚本哈希地址 to  公钥哈希地址
        Test_case3    : from 混合地址    to  公钥哈希地址 
        Test_case4    : from bech32地址  to  公钥哈希地址 
        Test_case5    : from multisig地址  to  公钥哈希地址 

其他类比特币验证须知

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

当前支持

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

        支持时间锁定和追加交易费的交易单构建
        支持P2PKH类型目标地址
        支持P2SH类型目标地址
        支持P2PKH和P2SH混合类型目标地址
        支持Bech32新型地址
        默认启用隔离认证
        从multisig地址进行支付

TODO

        stuff

用法:

创建空交易单 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 (
	SequenceFinal        = uint32(0xFFFFFFFF)
	SequenceMaxBip125RBF = uint32(0xFFFFFFFD)
)
View Source
const (
	DefaultTxVersion = uint32(2)
	DefaultHashType  = uint32(1)
)
View Source
const (
	TypeP2PKH  = 0
	TypeP2SH   = 1
	TypeBech32 = 2
)
View Source
const (
	Bech32Prefix = "tb1"
)

Variables

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

	BTCBech32Alphabet = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"

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

)
View Source
var (
	SegWitSymbol  = byte(0)
	SegWitVersion = byte(1)
	SigHashAll    = byte(1)
)
View Source
var (
	OpCodeHash160     = byte(0xA9)
	OpCodeEqual       = byte(0x87)
	OpCodeEqualVerify = byte(0x88)
	OpCodeCheckSig    = byte(0xAC)
	OpCodeDup         = byte(0x76)
	OpCode_1          = byte(0x51)
	OpCheckMultiSig   = byte(0xAE)
)
View Source
var (
	MaxScriptElementSize = 520
	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 (
	BitcoinAlphabet = NewAlphabet("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
)

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

View Source
var (
	ErrorInvalidBase58String = errors.New("invalid base58 string")
)

Errors

Functions

func Bech32Decode

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

func Bech32Encode

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

func CreateEmptyRawTransaction

func CreateEmptyRawTransaction(vins []Vin, vouts []Vout, lockTime uint32, replaceable bool, isTestNet bool) (string, error)

func CreateMultiSig

func CreateMultiSig(required byte, pubkeys [][]byte, isTestNet bool) (string, string, error)

func CreateQRC20TokenEmptyRawTransaction

func CreateQRC20TokenEmptyRawTransaction(vins []Vin, contract Vcontract, vout []Vout, lockTime uint32, replaceable bool, isTestNet bool) (string, error)

func CreateRawTransactionHashForSig

func CreateRawTransactionHashForSig(txHex string, unlockData []TxUnlock) ([]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(txHex string, sigPub []SignaturePubkey, unlockData []TxUnlock) (string, error)

func SignEmptyRawTransaction

func SignEmptyRawTransaction(txHex string, unlockData []TxUnlock) (string, error)

func VerifyRawTransaction

func VerifyRawTransaction(txHex string, unlockData []TxUnlock) bool

Types

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 Contract

type Contract struct {
	Version   []byte
	Vins      []TxIn
	Vcontract TxContract
	Vouts     []TxOut
	Witness   []TxWitness
	LockTime  []byte
}

type SignaturePubkey

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

func SignRawTransactionHash

func SignRawTransactionHash(txHash []string, unlockData []TxUnlock) ([]SignaturePubkey, error)

type Transaction

type Transaction struct {
	Version  []byte
	Vins     []TxIn
	Vouts    []TxOut
	Witness  []TxWitness
	LockTime []byte
}

func DecodeRawTransaction

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

type TxContract

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

type TxIn

type TxIn struct {
	TxID                  []byte
	Vout                  []byte
	ScriptPubkeySignature []byte
	Sequence              []byte
}

func (TxIn) GetScriptPubkey

func (in TxIn) GetScriptPubkey() string

func (TxIn) GetSequence

func (in TxIn) GetSequence() uint32

func (TxIn) GetTxID

func (in TxIn) GetTxID() string

func (TxIn) GetVout

func (in TxIn) GetVout() uint32

type TxOut

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

type TxUnlock

type TxUnlock struct {
	PrivateKey   []byte
	LockScript   string
	RedeemScript string
	Amount       uint64
	Address      string
}

type TxWitness

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

type Vcontract

type Vcontract struct {
	ContractAddr string
	To           string
	SendAmount   decimal.Decimal
	GasLimit     string
	GasPrice     string
	Amount       uint32
}

type Vin

type Vin struct {
	TxID string
	Vout uint32
}

type Vout

type Vout struct {
	Address string
	Amount  uint64
}

Jump to

Keyboard shortcuts

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