/
zcoinparser.go
270 lines (221 loc) · 6.02 KB
/
zcoinparser.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package xzc
import (
"blockbook/bchain"
"blockbook/bchain/coins/btc"
"bytes"
"encoding/binary"
"encoding/json"
"io"
"github.com/martinboehm/btcd/chaincfg/chainhash"
"github.com/martinboehm/btcd/wire"
"github.com/martinboehm/btcutil/chaincfg"
)
const (
OpZeroCoinMint = 0xc1
OpZeroCoinSpend = 0xc2
OpSigmaMint = 0xc3
OpSigmaSpend = 0xc4
MainnetMagic wire.BitcoinNet = 0xe3d9fef1
TestnetMagic wire.BitcoinNet = 0xcffcbeea
RegtestMagic wire.BitcoinNet = 0xfabfb5da
GenesisBlockTime = 1414776286
SwitchToMTPBlockHeader = 1544443200
MTPL = 64
SpendTxID = "0000000000000000000000000000000000000000000000000000000000000000"
)
var (
MainNetParams chaincfg.Params
TestNetParams chaincfg.Params
RegtestParams chaincfg.Params
)
func init() {
// mainnet
MainNetParams = chaincfg.MainNetParams
MainNetParams.Net = MainnetMagic
MainNetParams.AddressMagicLen = 1
MainNetParams.PubKeyHashAddrID = []byte{0x52}
MainNetParams.ScriptHashAddrID = []byte{0x07}
// testnet
TestNetParams = chaincfg.TestNet3Params
TestNetParams.Net = TestnetMagic
TestNetParams.AddressMagicLen = 1
TestNetParams.PubKeyHashAddrID = []byte{0x41}
TestNetParams.ScriptHashAddrID = []byte{0xb2}
// regtest
RegtestParams = chaincfg.RegressionNetParams
RegtestParams.Net = RegtestMagic
}
// ZcoinParser handle
type ZcoinParser struct {
*btc.BitcoinParser
}
// NewZcoinParser returns new ZcoinParser instance
func NewZcoinParser(params *chaincfg.Params, c *btc.Configuration) *ZcoinParser {
return &ZcoinParser{
BitcoinParser: btc.NewBitcoinParser(params, c),
}
}
// GetChainParams contains network parameters for the main Zcoin network,
// the regression test Zcoin network, the test Zcoin network and
// the simulation test Zcoin network, in this order
func GetChainParams(chain string) *chaincfg.Params {
if !chaincfg.IsRegistered(&MainNetParams) {
err := chaincfg.Register(&MainNetParams)
if err == nil {
err = chaincfg.Register(&TestNetParams)
}
if err == nil {
err = chaincfg.Register(&RegtestParams)
}
if err != nil {
panic(err)
}
}
switch chain {
case "test":
return &TestNetParams
case "regtest":
return &RegtestParams
default:
return &MainNetParams
}
}
// GetAddressesFromAddrDesc returns addresses for given address descriptor with flag if the addresses are searchable
func (p *ZcoinParser) GetAddressesFromAddrDesc(addrDesc bchain.AddressDescriptor) ([]string, bool, error) {
if len(addrDesc) > 0 {
switch addrDesc[0] {
case OpZeroCoinMint:
return []string{"Zeromint"}, false, nil
case OpZeroCoinSpend:
return []string{"Zerospend"}, false, nil
case OpSigmaMint:
return []string{"Sigmamint"}, false, nil
case OpSigmaSpend:
return []string{"Sigmaspend"}, false, nil
}
}
return p.OutputScriptToAddressesFunc(addrDesc)
}
// PackTx packs transaction to byte array using protobuf
func (p *ZcoinParser) PackTx(tx *bchain.Tx, height uint32, blockTime int64) ([]byte, error) {
return p.BaseParser.PackTx(tx, height, blockTime)
}
// UnpackTx unpacks transaction from protobuf byte array
func (p *ZcoinParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) {
return p.BaseParser.UnpackTx(buf)
}
// ParseBlock parses raw block to our Block struct
func (p *ZcoinParser) ParseBlock(b []byte) (*bchain.Block, error) {
reader := bytes.NewReader(b)
// parse standard block header first
header, err := parseBlockHeader(reader)
if err != nil {
return nil, err
}
// then MTP header
if isMTP(header) {
mtpHeader := MTPBlockHeader{}
mtpHashData := MTPHashData{}
// header
err = binary.Read(reader, binary.LittleEndian, &mtpHeader)
if err != nil {
return nil, err
}
// hash data
err = binary.Read(reader, binary.LittleEndian, &mtpHashData)
if err != nil {
return nil, err
}
// proof
for i := 0; i < MTPL*3; i++ {
var numberProofBlocks uint8
err = binary.Read(reader, binary.LittleEndian, &numberProofBlocks)
if err != nil {
return nil, err
}
for j := uint8(0); j < numberProofBlocks; j++ {
var mtpData [16]uint8
err = binary.Read(reader, binary.LittleEndian, mtpData[:])
if err != nil {
return nil, err
}
}
}
}
// parse txs
ntx, err := wire.ReadVarInt(reader, 0)
if err != nil {
return nil, err
}
txs := make([]bchain.Tx, ntx)
for i := uint64(0); i < ntx; i++ {
tx := wire.MsgTx{}
err := tx.BtcDecode(reader, 0, wire.WitnessEncoding)
if err != nil {
return nil, err
}
btx := p.TxFromMsgTx(&tx, false)
p.parseZcoinTx(&btx)
txs[i] = btx
}
return &bchain.Block{
BlockHeader: bchain.BlockHeader{
Size: len(b),
Time: header.Timestamp.Unix(),
},
Txs: txs,
}, nil
}
// ParseTxFromJson parses JSON message containing transaction and returns Tx struct
func (p *ZcoinParser) ParseTxFromJson(msg json.RawMessage) (*bchain.Tx, error) {
var tx bchain.Tx
err := json.Unmarshal(msg, &tx)
if err != nil {
return nil, err
}
for i := range tx.Vout {
vout := &tx.Vout[i]
// convert vout.JsonValue to big.Int and clear it, it is only temporary value used for unmarshal
vout.ValueSat, err = p.AmountToBigInt(vout.JsonValue)
if err != nil {
return nil, err
}
vout.JsonValue = ""
}
p.parseZcoinTx(&tx)
return &tx, nil
}
func (p *ZcoinParser) parseZcoinTx(tx *bchain.Tx) error {
for i := range tx.Vin {
vin := &tx.Vin[i]
// FIXME: right now we treat zerocoin spend vin as coinbase
// change this after blockbook support special type of vin
if vin.Txid == SpendTxID {
vin.Coinbase = vin.Txid
vin.Txid = ""
vin.Sequence = 0
vin.Vout = 0
}
}
return nil
}
func parseBlockHeader(r io.Reader) (*wire.BlockHeader, error) {
h := &wire.BlockHeader{}
err := h.Deserialize(r)
return h, err
}
func isMTP(h *wire.BlockHeader) bool {
epoch := h.Timestamp.Unix()
// the genesis block never be MTP block
return epoch > GenesisBlockTime && epoch >= SwitchToMTPBlockHeader
}
type MTPHashData struct {
HashRootMTP [16]uint8
BlockMTP [128][128]uint64
}
type MTPBlockHeader struct {
VersionMTP int32
MTPHashValue chainhash.Hash
Reserved1 chainhash.Hash
Reserved2 chainhash.Hash
}