forked from l0rdicon/blkparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx.go
More file actions
170 lines (140 loc) · 4.05 KB
/
tx.go
File metadata and controls
170 lines (140 loc) · 4.05 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package blkparser
import (
"encoding/binary"
"fmt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"strings"
)
// ClamMainNet Indicates the CLAM network.
const ClamMainNet wire.BitcoinNet = 0x15352203
// ClamParams contains CLAM network parameters.
var ClamParams = chaincfg.Params{
Name: "clammainnet",
Net: ClamMainNet,
DefaultPort: "31174",
PubKeyHashAddrID: 0x89,
ScriptHashAddrID: 0x0d,
PrivateKeyID: 0x85,
}
// Tx models the data in a transaction.
type Tx struct {
Hash string
Size uint32
LockTime uint32
Version uint32
Time uint32
Comment string
TxInCnt uint32
TxOutCnt uint32
TxIns []*TxIn
TxOuts []*TxOut
}
// TxIn models the data in a transaction input.
type TxIn struct {
InputHash string
InputVout uint32
ScriptSig []byte
Sequence uint32
}
// TxOut models the data in a transaction output.
type TxOut struct {
Addr string
Value uint64
Pkscript []byte
}
func (tx *Tx) IsCoinStake() bool {
if len(tx.TxIns) > 0 &&
!(tx.TxIns[0].InputHash == fmt.Sprintf("%064x", 0) && tx.TxIns[0].InputVout == ^uint32(0)) &&
len(tx.TxOuts) >= 2 &&
(tx.TxOuts[0].Value == 0 && tx.TxOuts[0].Addr == "") {
return true
}
return false
}
// ParseTxs deserializes a byte slice into a Tx slice.
func ParseTxs(txsraw []byte) (txs []*Tx, offset int, err error) {
offset = int(0)
txcnt, txcnt_size := DecodeVariableLengthInteger(txsraw[offset:])
offset += txcnt_size
txs = make([]*Tx, txcnt)
txoffset := int(0)
for i := range txs {
txs[i], txoffset = NewTx(txsraw[offset:])
txs[i].Hash = GetShaString(txsraw[offset : offset+txoffset])
txs[i].Size = uint32(txoffset)
offset += txoffset
}
return
}
// NewTx deserializes a byte slice into a Tx.
func NewTx(rawtx []byte) (tx *Tx, offset int) {
tx = new(Tx)
tx.Version = binary.LittleEndian.Uint32(rawtx[0:4])
tx.Time = binary.LittleEndian.Uint32(rawtx[4:8])
offset = 8
txincnt, txincntsize := DecodeVariableLengthInteger(rawtx[offset:])
offset += txincntsize
tx.TxInCnt = uint32(txincnt)
tx.TxIns = make([]*TxIn, txincnt)
txoffset := int(0)
for i := range tx.TxIns {
tx.TxIns[i], txoffset = NewTxIn(rawtx[offset:])
offset += txoffset
}
txoutcnt, txoutcntsize := DecodeVariableLengthInteger(rawtx[offset:])
offset += txoutcntsize
tx.TxOutCnt = uint32(txoutcnt)
tx.TxOuts = make([]*TxOut, txoutcnt)
for i := range tx.TxOuts {
tx.TxOuts[i], txoffset = NewTxOut(rawtx[offset:])
offset += txoffset
}
tx.LockTime = binary.LittleEndian.Uint32(rawtx[offset : offset+4])
offset += 4
if tx.Version > 1 {
comment, commentsize := DecodeVariableLengthInteger(rawtx[offset:])
offset += commentsize
commentstr := fmt.Sprintf("%+q", rawtx[offset:offset+comment])
// Trim once on each end of the comment in case the actual speech has quotation marks.
commentstr = strings.TrimSuffix(strings.TrimPrefix(commentstr, "\""), "\"")
tx.Comment = commentstr
offset += comment
}
return
}
// NewTxIn deserializes a byte slice into a TxIn.
func NewTxIn(txinraw []byte) (txin *TxIn, offset int) {
txin = new(TxIn)
txin.InputHash = HashString(txinraw[0:32])
txin.InputVout = binary.LittleEndian.Uint32(txinraw[32:36])
offset = 36
scriptsig, scriptsigsize := DecodeVariableLengthInteger(txinraw[offset:])
offset += scriptsigsize
txin.ScriptSig = txinraw[offset : offset+scriptsig]
offset += scriptsig
txin.Sequence = binary.LittleEndian.Uint32(txinraw[offset : offset+4])
offset += 4
return
}
// NewTxOut deserializes a byte slice into a TxOut.
func NewTxOut(txoutraw []byte) (txout *TxOut, offset int) {
txout = new(TxOut)
txout.Value = binary.LittleEndian.Uint64(txoutraw[0:8])
offset = 8
pkscript, pkscriptsize := DecodeVariableLengthInteger(txoutraw[offset:])
offset += pkscriptsize
txout.Pkscript = txoutraw[offset : offset+pkscript]
offset += pkscript
_, addrhash, _, err := txscript.ExtractPkScriptAddrs(txout.Pkscript, &ClamParams)
if err != nil {
return
}
if len(addrhash) != 0 {
txout.Addr = addrhash[0].EncodeAddress()
} else {
txout.Addr = ""
}
return
}