tokenengine

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2020 License: GPL-3.0, GPL-3.0 Imports: 14 Imported by: 2

README

Token Engine介绍

TokenEngine来自于btcd的txscript模块,该模块实现了比特币的堆栈引擎。对于txscript项目做了如下修改:

  1. 屏蔽了隔离见证相关的代码。
  2. 修改了Tx结构,采用了Tx->Message->Input/Output这样的三层结构
  3. 增加了OPCODE:OP_JURY_REDEEM_EQUAL = 0xc8 // 200 用于从状态数据库获得一个合约的陪审团赎回代码并判断是否相等。
  4. 增加了SigHashRaw SigHashType = 0x4 //直接对构造好的不包含任何签名信息的Tx签名
  5. 增加了付款到合约和从合约付款出去的支持。

Documentation

Overview

Package tokenengine is a generated GoMock package.

Index

Constants

View Source
const (
	//SigHashOld          uint32 = 0x0
	SigHashAll          uint32 = 0x1
	SigHashNone         uint32 = 0x2
	SigHashSingle       uint32 = 0x3
	SigHashRaw          uint32 = 0x4
	SigHashOneMessage   uint32 = 0x40
	SigHashAnyOneCanPay uint32 = 0x80
)

Variables

This section is empty.

Functions

func GenerateP2PKHUnlockScript

func GenerateP2PKHUnlockScript(sign []byte, pubKey []byte) []byte

根据签名和公钥信息生成解锁脚本 Use signature and public key to generate a P2PKH unlock script

func IsUnspendable

func IsUnspendable(script []byte) bool

Types

type AddressGetPubKey

type AddressGetPubKey func(common.Address) ([]byte, error)

根据地址获得对应的公钥

type AddressGetSign

type AddressGetSign func(common.Address, []byte) ([]byte, error)

用指定地址对应的私钥对消息进行签名,并返回签名结果

type ITokenEngine added in v1.0.2

type ITokenEngine interface {
	//根据公钥Hash,生成锁定脚本
	GenerateP2PKHLockScript(pubKeyHash []byte) []byte
	//根据赎回脚本Hash,生成锁定脚本
	GenerateP2SHLockScript(redeemScriptHash []byte) []byte
	//根据合约ID,生成锁定脚本
	GenerateP2CHLockScript(contractId common.Address) []byte
	//根据地址生成锁定脚本
	GenerateLockScript(address common.Address) []byte
	//根据锁定脚本,得出对应的地址
	GetAddressFromScript(lockScript []byte) (common.Address, error)
	//根据解锁脚本,计算出地址
	GetAddressFromUnlockScript(unlockScript []byte) (common.Address, error)
	//根据公钥列表和需要的签名数,获得赎回脚本
	GenerateRedeemScript(needed byte, pubKeys [][]byte) []byte
	//将一个脚本二进制解析为字符串形式
	DisasmString(script []byte) (string, error)
	//计算要对一个Tx的msgIdx和inputInx位置进行签名,对应的Hash
	CalcSignatureHash(tx *modules.Transaction, hashType uint32,
		msgIdx, inputIdx int, lockOrRedeemScript []byte) ([]byte, error)
	//根据tx,msgIdx和inputIdx,获得多签的解锁脚本,然后计算获得解锁脚本是哪些地址进行的该签名,主要用于计算手续费分摊
	GetScriptSigners(tx *modules.Transaction, msgIdx, inputIndex int) ([]common.Address, error)
	//对一个未签名的tx进行签名,将所有input的解锁脚本填充完毕
	SignTxAllPaymentInput(tx *modules.Transaction, hashType uint32, utxoLockScripts map[modules.OutPoint][]byte,
		redeemScript []byte, pubKeyFn AddressGetPubKey, signFunc AddressGetSign) ([]common.SignatureError, error)
	//对一个未签名交易的某条Message进行签名,如果msgIdx==-1表示对所有的Message进行签名
	SignTx1MsgPaymentInput(tx *modules.Transaction, msgIdx int, hashType uint32, utxoLockScripts map[modules.OutPoint][]byte,
		redeemScript []byte, pubKeyFn AddressGetPubKey, signFunc AddressGetSign) ([]common.SignatureError, error)
	//对tx的某个多签input进行签名,如果已经有别人签名,则合并
	MultiSignOnePaymentInput(tx *modules.Transaction,
		hashType uint32, msgIdx, id int,
		utxoLockScript []byte, redeemScript []byte,
		pubKeyFn AddressGetPubKey, signFunc AddressGetSign, previousScript []byte) ([]byte, error)
	//当用户合约需要解锁Token时,收集到了jury签名列表和赎回脚本,组合成合约的解锁脚本
	MergeContractUnlockScript(signs [][]byte, redeemScript []byte) []byte
	//验证一个tx的指定input的解锁脚本是否正确
	ScriptValidate(utxoLockScript []byte,
		pickupJuryRedeemScript PickupJuryRedeemScript,
		tx *modules.Transaction,
		msgIdx, inputIndex int) error
	//验证tx中的某个Payment message的所有input的解锁脚本是否正确
	ScriptValidate1Msg(utxoLockScripts map[string][]byte,
		pickupJuryRedeemScript PickupJuryRedeemScript,
		tx *modules.Transaction, msgIdx int) error
}
var Instance ITokenEngine = &TokenEngine{signCache: txscript.NewSigCache(20000)}

type MockITokenEngine added in v1.0.2

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

MockITokenEngine is a mock of ITokenEngine interface

func NewMockITokenEngine added in v1.0.2

func NewMockITokenEngine(ctrl *gomock.Controller) *MockITokenEngine

NewMockITokenEngine creates a new mock instance

func (*MockITokenEngine) CalcSignatureHash added in v1.0.2

func (m *MockITokenEngine) CalcSignatureHash(tx *modules.Transaction, hashType uint32, msgIdx, inputIdx int, lockOrRedeemScript []byte) ([]byte, error)

CalcSignatureHash mocks base method

func (*MockITokenEngine) DisasmString added in v1.0.2

func (m *MockITokenEngine) DisasmString(script []byte) (string, error)

DisasmString mocks base method

func (*MockITokenEngine) EXPECT added in v1.0.2

EXPECT returns an object that allows the caller to indicate expected use

func (*MockITokenEngine) GenerateLockScript added in v1.0.2

func (m *MockITokenEngine) GenerateLockScript(address common.Address) []byte

GenerateLockScript mocks base method

func (*MockITokenEngine) GenerateP2CHLockScript added in v1.0.2

func (m *MockITokenEngine) GenerateP2CHLockScript(contractId common.Address) []byte

GenerateP2CHLockScript mocks base method

func (*MockITokenEngine) GenerateP2PKHLockScript added in v1.0.2

func (m *MockITokenEngine) GenerateP2PKHLockScript(pubKeyHash []byte) []byte

GenerateP2PKHLockScript mocks base method

func (*MockITokenEngine) GenerateP2SHLockScript added in v1.0.2

func (m *MockITokenEngine) GenerateP2SHLockScript(redeemScriptHash []byte) []byte

GenerateP2SHLockScript mocks base method

func (*MockITokenEngine) GenerateRedeemScript added in v1.0.2

func (m *MockITokenEngine) GenerateRedeemScript(needed byte, pubKeys [][]byte) []byte

GenerateRedeemScript mocks base method

func (*MockITokenEngine) GetAddressFromScript added in v1.0.2

func (m *MockITokenEngine) GetAddressFromScript(lockScript []byte) (common.Address, error)

GetAddressFromScript mocks base method

func (*MockITokenEngine) GetAddressFromUnlockScript added in v1.0.5

func (m *MockITokenEngine) GetAddressFromUnlockScript(unlockScript []byte) (common.Address, error)

GetAddressFromUnlockScript mocks base method

func (*MockITokenEngine) GetScriptSigners added in v1.0.2

func (m *MockITokenEngine) GetScriptSigners(tx *modules.Transaction, msgIdx, inputIndex int) ([]common.Address, error)

GetScriptSigners mocks base method

func (*MockITokenEngine) MergeContractUnlockScript added in v1.0.2

func (m *MockITokenEngine) MergeContractUnlockScript(signs [][]byte, redeemScript []byte) []byte

MergeContractUnlockScript mocks base method

func (*MockITokenEngine) MultiSignOnePaymentInput added in v1.0.2

func (m *MockITokenEngine) MultiSignOnePaymentInput(tx *modules.Transaction, hashType uint32, msgIdx, id int, utxoLockScript, redeemScript []byte, pubKeyFn AddressGetPubKey, signFunc AddressGetSign, previousScript []byte) ([]byte, error)

MultiSignOnePaymentInput mocks base method

func (*MockITokenEngine) ScriptValidate added in v1.0.2

func (m *MockITokenEngine) ScriptValidate(utxoLockScript []byte, pickupJuryRedeemScript PickupJuryRedeemScript, tx *modules.Transaction, msgIdx, inputIndex int) error

ScriptValidate mocks base method

func (*MockITokenEngine) ScriptValidate1Msg added in v1.0.2

func (m *MockITokenEngine) ScriptValidate1Msg(utxoLockScripts map[string][]byte, pickupJuryRedeemScript PickupJuryRedeemScript, tx *modules.Transaction, msgIdx int) error

ScriptValidate1Msg mocks base method

func (*MockITokenEngine) SignTx1MsgPaymentInput added in v1.0.5

func (m *MockITokenEngine) SignTx1MsgPaymentInput(tx *modules.Transaction, msgIdx int, hashType uint32, utxoLockScripts map[modules.OutPoint][]byte, redeemScript []byte, pubKeyFn AddressGetPubKey, signFunc AddressGetSign) ([]common.SignatureError, error)

SignTx1MsgPaymentInput mocks base method

func (*MockITokenEngine) SignTxAllPaymentInput added in v1.0.2

func (m *MockITokenEngine) SignTxAllPaymentInput(tx *modules.Transaction, hashType uint32, utxoLockScripts map[modules.OutPoint][]byte, redeemScript []byte, pubKeyFn AddressGetPubKey, signFunc AddressGetSign) ([]common.SignatureError, error)

SignTxAllPaymentInput mocks base method

type MockITokenEngineMockRecorder added in v1.0.2

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

MockITokenEngineMockRecorder is the mock recorder for MockITokenEngine

func (*MockITokenEngineMockRecorder) CalcSignatureHash added in v1.0.2

func (mr *MockITokenEngineMockRecorder) CalcSignatureHash(tx, hashType, msgIdx, inputIdx, lockOrRedeemScript interface{}) *gomock.Call

CalcSignatureHash indicates an expected call of CalcSignatureHash

func (*MockITokenEngineMockRecorder) DisasmString added in v1.0.2

func (mr *MockITokenEngineMockRecorder) DisasmString(script interface{}) *gomock.Call

DisasmString indicates an expected call of DisasmString

func (*MockITokenEngineMockRecorder) GenerateLockScript added in v1.0.2

func (mr *MockITokenEngineMockRecorder) GenerateLockScript(address interface{}) *gomock.Call

GenerateLockScript indicates an expected call of GenerateLockScript

func (*MockITokenEngineMockRecorder) GenerateP2CHLockScript added in v1.0.2

func (mr *MockITokenEngineMockRecorder) GenerateP2CHLockScript(contractId interface{}) *gomock.Call

GenerateP2CHLockScript indicates an expected call of GenerateP2CHLockScript

func (*MockITokenEngineMockRecorder) GenerateP2PKHLockScript added in v1.0.2

func (mr *MockITokenEngineMockRecorder) GenerateP2PKHLockScript(pubKeyHash interface{}) *gomock.Call

GenerateP2PKHLockScript indicates an expected call of GenerateP2PKHLockScript

func (*MockITokenEngineMockRecorder) GenerateP2SHLockScript added in v1.0.2

func (mr *MockITokenEngineMockRecorder) GenerateP2SHLockScript(redeemScriptHash interface{}) *gomock.Call

GenerateP2SHLockScript indicates an expected call of GenerateP2SHLockScript

func (*MockITokenEngineMockRecorder) GenerateRedeemScript added in v1.0.2

func (mr *MockITokenEngineMockRecorder) GenerateRedeemScript(needed, pubKeys interface{}) *gomock.Call

GenerateRedeemScript indicates an expected call of GenerateRedeemScript

func (*MockITokenEngineMockRecorder) GetAddressFromScript added in v1.0.2

func (mr *MockITokenEngineMockRecorder) GetAddressFromScript(lockScript interface{}) *gomock.Call

GetAddressFromScript indicates an expected call of GetAddressFromScript

func (*MockITokenEngineMockRecorder) GetAddressFromUnlockScript added in v1.0.5

func (mr *MockITokenEngineMockRecorder) GetAddressFromUnlockScript(unlockScript interface{}) *gomock.Call

GetAddressFromUnlockScript indicates an expected call of GetAddressFromUnlockScript

func (*MockITokenEngineMockRecorder) GetScriptSigners added in v1.0.2

func (mr *MockITokenEngineMockRecorder) GetScriptSigners(tx, msgIdx, inputIndex interface{}) *gomock.Call

GetScriptSigners indicates an expected call of GetScriptSigners

func (*MockITokenEngineMockRecorder) MergeContractUnlockScript added in v1.0.2

func (mr *MockITokenEngineMockRecorder) MergeContractUnlockScript(signs, redeemScript interface{}) *gomock.Call

MergeContractUnlockScript indicates an expected call of MergeContractUnlockScript

func (*MockITokenEngineMockRecorder) MultiSignOnePaymentInput added in v1.0.2

func (mr *MockITokenEngineMockRecorder) MultiSignOnePaymentInput(tx, hashType, msgIdx, id, utxoLockScript, redeemScript, pubKeyFn, signFunc, previousScript interface{}) *gomock.Call

MultiSignOnePaymentInput indicates an expected call of MultiSignOnePaymentInput

func (*MockITokenEngineMockRecorder) ScriptValidate added in v1.0.2

func (mr *MockITokenEngineMockRecorder) ScriptValidate(utxoLockScript, pickupJuryRedeemScript, tx, msgIdx, inputIndex interface{}) *gomock.Call

ScriptValidate indicates an expected call of ScriptValidate

func (*MockITokenEngineMockRecorder) ScriptValidate1Msg added in v1.0.2

func (mr *MockITokenEngineMockRecorder) ScriptValidate1Msg(utxoLockScripts, pickupJuryRedeemScript, tx, msgIdx interface{}) *gomock.Call

ScriptValidate1Msg indicates an expected call of ScriptValidate1Msg

func (*MockITokenEngineMockRecorder) SignTx1MsgPaymentInput added in v1.0.5

func (mr *MockITokenEngineMockRecorder) SignTx1MsgPaymentInput(tx, msgIdx, hashType, utxoLockScripts, redeemScript, pubKeyFn, signFunc interface{}) *gomock.Call

SignTx1MsgPaymentInput indicates an expected call of SignTx1MsgPaymentInput

func (*MockITokenEngineMockRecorder) SignTxAllPaymentInput added in v1.0.2

func (mr *MockITokenEngineMockRecorder) SignTxAllPaymentInput(tx, hashType, utxoLockScripts, redeemScript, pubKeyFn, signFunc interface{}) *gomock.Call

SignTxAllPaymentInput indicates an expected call of SignTxAllPaymentInput

type PickupJuryRedeemScript added in v1.0.2

type PickupJuryRedeemScript func(common.Address) ([]byte, error)

根据合约地址,获得该合约对应的陪审团赎回脚本

type PubKey4Sort

type PubKey4Sort [][]byte

func (PubKey4Sort) Len

func (c PubKey4Sort) Len() int

func (PubKey4Sort) Less

func (c PubKey4Sort) Less(i, j int) bool

func (PubKey4Sort) Swap

func (c PubKey4Sort) Swap(i, j int)

type TokenEngine added in v1.0.2

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

func (*TokenEngine) CalcSignatureHash added in v1.0.2

func (engine *TokenEngine) CalcSignatureHash(tx *modules.Transaction, hashType uint32,
	msgIdx, inputIdx int, lockOrRedeemScript []byte) ([]byte, error)

为钱包计算要签名某个Input对应的Hash

func (*TokenEngine) DisasmString added in v1.0.2

func (engine *TokenEngine) DisasmString(script []byte) (string, error)

传入一个脚本二进制,解析为可读的文本形式

func (*TokenEngine) GenerateLockScript added in v1.0.2

func (engine *TokenEngine) GenerateLockScript(address common.Address) []byte

根据地址产生对应的锁定脚本

func (*TokenEngine) GenerateP2CHLockScript added in v1.0.2

func (engine *TokenEngine) GenerateP2CHLockScript(contractId common.Address) []byte

func (*TokenEngine) GenerateP2PKHLockScript added in v1.0.2

func (engine *TokenEngine) GenerateP2PKHLockScript(pubKeyHash []byte) []byte

Generate a P2PKH lock script, just only need input 20bytes public key hash. You can use Address.Bytes() to get address hash.

func (*TokenEngine) GenerateP2SHLockScript added in v1.0.2

func (engine *TokenEngine) GenerateP2SHLockScript(redeemScriptHash []byte) []byte

Give redeem script hash 160 result, generate a P2SH lock script. If you have built your redeem script, please use crypto.Hash160() to gnerate hash

func (*TokenEngine) GenerateRedeemScript added in v1.0.2

func (engine *TokenEngine) GenerateRedeemScript(needed byte, pubKeys [][]byte) []byte

生成多签用的赎回脚本 Generate redeem script

func (*TokenEngine) GetAddressFromScript added in v1.0.2

func (engine *TokenEngine) GetAddressFromScript(lockScript []byte) (common.Address, error)

根据锁定脚本获得对应的地址

func (*TokenEngine) GetAddressFromUnlockScript added in v1.0.5

func (engine *TokenEngine) GetAddressFromUnlockScript(unlockScript []byte) (common.Address, error)

func (*TokenEngine) GetScriptSigners added in v1.0.2

func (engine *TokenEngine) GetScriptSigners(tx *modules.Transaction, msgIdx, inputIndex int) ([]common.Address, error)

对于一个多签或者合约解锁脚本,获得到底哪些用户参与了签名

func (*TokenEngine) MergeContractUnlockScript added in v1.0.2

func (engine *TokenEngine) MergeContractUnlockScript(signs [][]byte, redeemScript []byte) []byte

func (*TokenEngine) MultiSignOnePaymentInput added in v1.0.2

func (engine *TokenEngine) MultiSignOnePaymentInput(tx *modules.Transaction,
	hashType uint32, msgIdx, id int,
	utxoLockScript []byte, redeemScript []byte,
	pubKeyFn AddressGetPubKey, hashFn AddressGetSign, previousScript []byte) ([]byte, error)

对交易中的Payment类型中的某个Input生成解锁脚本 func SignOnePaymentInput(tx *modules.Transaction, msgIdx, id int,

	utxoLockScript []byte, privKey *ecdsa.PrivateKey, juryVersion int) ([]byte, error) {
	lookupKey := func(a common.Address) (*ecdsa.PrivateKey, bool, error) {
		return privKey, true, nil
	}
	sigScript, err := txscript.SignTxOutput(tx, msgIdx, id, utxoLockScript, txscript.SigHashAll,
		txscript.KeyClosure(lookupKey), nil, nil, juryVersion)
	if err != nil {
		return []byte{}, err
	}
	return sigScript, nil
}

func (*TokenEngine) ScriptValidate added in v1.0.2

func (engine *TokenEngine) ScriptValidate(utxoLockScript []byte,
	pickupJuryRedeemScript PickupJuryRedeemScript,
	tx *modules.Transaction,
	msgIdx, inputIndex int) error

validate this transaction and input index script can unlock the utxo.

func (*TokenEngine) ScriptValidate1Msg added in v1.0.2

func (engine *TokenEngine) ScriptValidate1Msg(utxoLockScripts map[string][]byte,
	pickupJuryRedeemScript PickupJuryRedeemScript,
	tx *modules.Transaction, msgIdx int) error

验证一个PaymentMessage的所有Input解锁脚本是否正确

func (*TokenEngine) SignTx1MsgPaymentInput added in v1.0.5

func (engine *TokenEngine) SignTx1MsgPaymentInput(tx *modules.Transaction, msgIdx int, hashType uint32, utxoLockScripts map[modules.OutPoint][]byte,
	redeemScript []byte, pubKeyFn AddressGetPubKey, signFn AddressGetSign) ([]common.SignatureError, error)

Sign a message of transaction

func (*TokenEngine) SignTxAllPaymentInput added in v1.0.2

func (engine *TokenEngine) SignTxAllPaymentInput(tx *modules.Transaction, hashType uint32, utxoLockScripts map[modules.OutPoint][]byte,
	redeemScript []byte, pubKeyFn AddressGetPubKey, signFn AddressGetSign) ([]common.SignatureError, error)

Sign a full transaction

Directories

Path Synopsis
internal
txscript
Package txscript implements the bitcoin transaction script language.
Package txscript implements the bitcoin transaction script language.

Jump to

Keyboard shortcuts

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