-
Notifications
You must be signed in to change notification settings - Fork 3
/
txinput.go
59 lines (51 loc) · 1.2 KB
/
txinput.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
51
52
53
54
55
56
57
58
package simpleBlockchain
import (
"bytes"
"encoding/json"
)
var (
coinbasePrevTxHash = []byte{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
}
coinbasePrevTxOutIndex uint = 0
)
type TxIn struct {
PrevTxHash Hashes `json:"prevtxhash"`
PrevTxOutIndex uint `json:"prevtxoutindex"`
ScriptSig Hashes `json:"scriptsig"`
}
func CreateCoinbaseTxIn(data string) *TxIn {
coinbaseTxIn := &TxIn{
PrevTxHash: coinbasePrevTxHash,
PrevTxOutIndex: coinbasePrevTxOutIndex,
ScriptSig: []byte(data),
}
return coinbaseTxIn
}
func (txIn *TxIn) recoverCoinbaseScriptsig() string{
return string(txIn.ScriptSig)
}
func (txIn *TxIn) isCoinbaseTxIn() bool {
if bytes.Compare(txIn.PrevTxHash, coinbasePrevTxHash) == 0 && txIn.PrevTxOutIndex == coinbasePrevTxOutIndex {
return true
}
return false
}
func (txIn *TxIn) Serialize() ([]byte, error) {
res, err := json.Marshal(txIn)
if err != nil {
return nil, err
}
return res, nil
}
func DeserializeTxIn(data []byte) (*TxIn, error) {
var txIn TxIn
err := json.Unmarshal(data, &txIn)
if err != nil {
return nil, err
}
return &txIn, nil
}