-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx_input.go
50 lines (44 loc) · 1.33 KB
/
tx_input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package blc
import (
"crypto/ecdsa"
"github.com/pkg/errors"
"github.com/treeforest/easyblc/script"
)
type TxInput struct {
TxId [32]byte // 引用的上一笔交易的交易哈希,创币交易初始化全为0
Vout uint32 // 引用的上一笔交易的输出索引,创币交易初始化为0xFFFFFFFF
ScriptSig []byte // 解锁脚本
CoinbaseDataSize int // 创币交易长度
CoinbaseData []byte // 创币交易(用户可以在这里写下任何东西,可辅助挖矿)
}
func NewTxInput(txId [32]byte, vout uint32) (*TxInput, error) {
txInput := &TxInput{
TxId: txId,
Vout: vout,
ScriptSig: nil,
CoinbaseDataSize: 0,
CoinbaseData: nil,
}
return txInput, nil
}
func NewCoinbaseTxInput(coinbaseData []byte) *TxInput {
return &TxInput{
TxId: [32]byte{},
Vout: 0xFFFFFFFF,
ScriptSig: nil,
CoinbaseDataSize: len(coinbaseData),
CoinbaseData: coinbaseData,
}
}
func (input *TxInput) IsCoinbase() bool {
return input.Vout == 0xFFFFFFFF
}
// SetScriptSig 设置输入脚本
func (input *TxInput) SetScriptSig(hash [32]byte, key *ecdsa.PrivateKey) error {
scriptSig, err := script.GenerateScriptSig(hash, key)
if err != nil {
return errors.WithStack(err)
}
input.ScriptSig = scriptSig
return nil
}