forked from tsileo/blkparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx.go
137 lines (108 loc) · 2.86 KB
/
tx.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
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
package blkparser
import (
"encoding/binary"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
)
const MazaMainNet wire.BitcoinNet = 0xdf03b5f8
var MazaParams = chaincfg.Params{
Name: "mazamainnet",
Net: MazaMainNet,
DefaultPort: "12835",
PubKeyHashAddrID: 0x32,
ScriptHashAddrID: 0x05,
PrivateKeyID: 0xe0,
}
type Tx struct {
Hash string
Size uint32
LockTime uint32
Version uint32
TxInCnt uint32
TxOutCnt uint32
TxIns []*TxIn
TxOuts []*TxOut
}
type TxIn struct {
InputHash string
InputVout uint32
ScriptSig []byte
Sequence uint32
}
type TxOut struct {
Addr string
Value uint64
Pkscript []byte
}
func ParseTxs(txsraw []byte) (txs []*Tx, 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
}
func NewTx(rawtx []byte) (tx *Tx, offset int) {
tx = new(Tx)
tx.Version = binary.LittleEndian.Uint32(rawtx[0:4])
offset = 4
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
return
}
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
}
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, &MazaParams)
if err != nil {
return
}
if len(addrhash) != 0 {
txout.Addr = addrhash[0].EncodeAddress()
} else {
txout.Addr = ""
}
return
}