This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
proof.go
536 lines (466 loc) · 15.6 KB
/
proof.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package rpc
import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"net/http"
"sort"
"strconv"
"strings"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/gorilla/mux"
"github.com/tendermint/iavl"
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/tmhash"
cmn "github.com/tendermint/tendermint/libs/common"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"github.com/tendermint/tendermint/types"
"github.com/bandprotocol/d3n/chain/x/zoracle"
)
var (
relayArguments abi.Arguments
verifyArguments abi.Arguments
)
func init() {
err := json.Unmarshal(relayFormat, &relayArguments)
if err != nil {
panic(err)
}
err = json.Unmarshal(verifyFormat, &verifyArguments)
if err != nil {
panic(err)
}
}
type IAVLMerklePath struct {
IsDataOnRight bool `json:"isDataOnRight"`
SubtreeHeight uint8 `json:"subtreeHeight"`
SubtreeSize uint64 `json:"subtreeSize"`
SubtreeVersion uint64 `json:"subtreeVersion"`
SiblingHash cmn.HexBytes `json:"siblingHash"`
}
type IAVLMerklePathEthereum struct {
IsDataOnRight bool
SubtreeHeight uint8
SubtreeSize *big.Int
SubtreeVersion *big.Int
SiblingHash common.Hash
}
func (merklePath *IAVLMerklePath) encodeToEthFormat() IAVLMerklePathEthereum {
return IAVLMerklePathEthereum{
merklePath.IsDataOnRight,
merklePath.SubtreeHeight,
big.NewInt(int64(merklePath.SubtreeSize)),
big.NewInt(int64(merklePath.SubtreeVersion)),
common.BytesToHash(merklePath.SiblingHash),
}
}
type BlockHeaderMerkleParts struct {
VersionAndChainIdHash cmn.HexBytes `json:"versionAndChainIdHash"`
TimeHash cmn.HexBytes `json:"timeHash"`
TxCountAndLastBlockInfoHash cmn.HexBytes `json:"txCountAndLastBlockInfoHash"`
ConsensusDataHash cmn.HexBytes `json:"consensusDataHash"`
LastResultsHash cmn.HexBytes `json:"lastResultsHash"`
EvidenceAndProposerHash cmn.HexBytes `json:"evidenceAndProposerHash"`
}
type BlockHeaderMerklePartsEthereum struct {
VersionAndChainIdHash common.Hash
TimeHash common.Hash
TxCountAndLastBlockInfoHash common.Hash
ConsensusDataHash common.Hash
LastResultsHash common.Hash
EvidenceAndProposerHash common.Hash
}
func (blockHeader *BlockHeaderMerkleParts) encodeToEthFormat() BlockHeaderMerklePartsEthereum {
return BlockHeaderMerklePartsEthereum{
common.BytesToHash(blockHeader.VersionAndChainIdHash),
common.BytesToHash(blockHeader.TimeHash),
common.BytesToHash(blockHeader.TxCountAndLastBlockInfoHash),
common.BytesToHash(blockHeader.ConsensusDataHash),
common.BytesToHash(blockHeader.LastResultsHash),
common.BytesToHash(blockHeader.EvidenceAndProposerHash),
}
}
type BlockRelayProof struct {
OracleIAVLStateHash cmn.HexBytes `json:"oracleIAVLStateHash"`
OtherStoresMerkleHash cmn.HexBytes `json:"otherStoresMerkleHash"`
SupplyStoresMerkleHash cmn.HexBytes `json:"supplyStoresMerkleHash"`
BlockHeaderMerkleParts BlockHeaderMerkleParts `json:"blockHeaderMerkleParts"`
SignedDataPrefix cmn.HexBytes `json:"signedDataPrefix"`
Signatures []TMSignature `json:"signatures"`
}
func (blockRelay *BlockRelayProof) encodeToEthData(blockHeight uint64) ([]byte, error) {
parseSignatures := make([]TMSignatureEthereum, len(blockRelay.Signatures))
for i, sig := range blockRelay.Signatures {
parseSignatures[i] = sig.encodeToEthFormat()
}
return relayArguments.Pack(
big.NewInt(int64(blockHeight)),
common.BytesToHash(blockRelay.OracleIAVLStateHash),
common.BytesToHash(blockRelay.OtherStoresMerkleHash),
common.BytesToHash(blockRelay.SupplyStoresMerkleHash),
blockRelay.BlockHeaderMerkleParts.encodeToEthFormat(),
blockRelay.SignedDataPrefix,
parseSignatures,
)
}
type OracleDataProof struct {
Version uint64 `json:"version"`
RequestID zoracle.RequestID `json:"requestID"`
OracleScriptID zoracle.OracleScriptID `json:"oracleScriptID"`
Calldata cmn.HexBytes `json:"calldata"`
Data cmn.HexBytes `json:"data"`
MerklePaths []IAVLMerklePath `json:"merklePaths"`
}
func (oracleData *OracleDataProof) encodeToEthData(blockHeight uint64) ([]byte, error) {
parsePaths := make([]IAVLMerklePathEthereum, len(oracleData.MerklePaths))
for i, path := range oracleData.MerklePaths {
parsePaths[i] = path.encodeToEthFormat()
}
return verifyArguments.Pack(
big.NewInt(int64(blockHeight)),
oracleData.Data,
uint64(oracleData.RequestID),
uint64(oracleData.OracleScriptID),
oracleData.Calldata,
big.NewInt(int64(oracleData.Version)),
parsePaths,
)
}
type TMSignature struct {
R cmn.HexBytes `json:"r"`
S cmn.HexBytes `json:"s"`
V uint8 `json:"v"`
SignedDataSuffix cmn.HexBytes `json:"signedDataSuffix"`
}
type TMSignatureEthereum struct {
R common.Hash
S common.Hash
V uint8
SignedDataSuffix []byte
}
func (signature *TMSignature) encodeToEthFormat() TMSignatureEthereum {
return TMSignatureEthereum{
common.BytesToHash(signature.R),
common.BytesToHash(signature.S),
signature.V,
signature.SignedDataSuffix,
}
}
type Proof struct {
JsonProof JsonProof `json:"jsonProof"`
EVMProofBytes cmn.HexBytes `json:"evmProofBytes"`
}
type JsonProof struct {
BlockHeight uint64 `json:"blockHeight"`
OracleDataProof OracleDataProof `json:"oracleDataProof"`
BlockRelayProof BlockRelayProof `json:"blockRelayProof"`
}
func cdcEncode(cdc *codec.Codec, item interface{}) []byte {
if item != nil && !cmn.IsTypedNil(item) && !cmn.IsEmpty(item) {
return cdc.MustMarshalBinaryBare(item)
}
return nil
}
func RecoverETHAddress(msg, sig, signer []byte) ([]byte, uint8, error) {
for i := uint8(0); i < 2; i++ {
pubuc, err := crypto.SigToPub(tmhash.Sum(msg), append(sig, byte(i)))
if err != nil {
return nil, 0, err
}
pub := crypto.CompressPubkey(pubuc)
var tmp [33]byte
copy(tmp[:], pub)
if string(signer) == string(secp256k1.PubKeySecp256k1(tmp).Address()) {
return crypto.PubkeyToAddress(*pubuc).Bytes(), 27 + i, nil
}
}
return nil, 0, fmt.Errorf("No match address found")
}
func GetBlockRelayProof(cliCtx context.CLIContext, blockId uint64) (BlockRelayProof, error) {
bp := BlockRelayProof{}
_blockId := int64(blockId)
rc, err := cliCtx.Client.Commit(&_blockId)
if err != nil {
return BlockRelayProof{}, err
}
sh := rc.SignedHeader
block := *sh.Header
commit := *sh.Commit
bhmp := BlockHeaderMerkleParts{}
bhmp.VersionAndChainIdHash = merkle.SimpleHashFromByteSlices([][]byte{
cdcEncode(cliCtx.Codec, block.Version),
cdcEncode(cliCtx.Codec, block.ChainID),
})
bhmp.TimeHash = merkle.SimpleHashFromByteSlices([][]byte{
cdcEncode(cliCtx.Codec, block.Time),
})
bhmp.TxCountAndLastBlockInfoHash = merkle.SimpleHashFromByteSlices([][]byte{
cdcEncode(cliCtx.Codec, block.NumTxs),
cdcEncode(cliCtx.Codec, block.TotalTxs),
cdcEncode(cliCtx.Codec, block.LastBlockID),
cdcEncode(cliCtx.Codec, block.LastCommitHash),
})
bhmp.ConsensusDataHash = merkle.SimpleHashFromByteSlices([][]byte{
cdcEncode(cliCtx.Codec, block.DataHash),
cdcEncode(cliCtx.Codec, block.ValidatorsHash),
cdcEncode(cliCtx.Codec, block.NextValidatorsHash),
cdcEncode(cliCtx.Codec, block.ConsensusHash),
})
bhmp.LastResultsHash = merkle.SimpleHashFromByteSlices([][]byte{
cdcEncode(cliCtx.Codec, block.LastResultsHash),
})
bhmp.EvidenceAndProposerHash = merkle.SimpleHashFromByteSlices([][]byte{
cdcEncode(cliCtx.Codec, block.EvidenceHash),
cdcEncode(cliCtx.Codec, block.ProposerAddress),
})
bp.BlockHeaderMerkleParts = bhmp
leftMsg := ""
bp.Signatures = []TMSignature{}
addrs := []string{}
mapAddrs := map[string]TMSignature{}
for i := 0; i < len(commit.Precommits); i++ {
if commit.Precommits[i] == nil {
continue
}
vote := types.Vote(*commit.Precommits[i])
msg := vote.SignBytes("bandchain")
lr := strings.Split(hex.EncodeToString(msg), hex.EncodeToString(block.Hash()))
if leftMsg != "" && leftMsg != lr[0] {
return BlockRelayProof{}, fmt.Errorf("Inconsistent prefix signature bytes")
}
leftMsg = lr[0]
lr1, err := hex.DecodeString(lr[1])
if err != nil {
continue
}
addr, v, err := RecoverETHAddress(msg, vote.Signature, vote.ValidatorAddress)
if err != nil {
continue
}
addrs = append(addrs, string(addr))
mapAddrs[string(addr)] = TMSignature{
vote.Signature[:32],
vote.Signature[32:],
v,
lr1,
}
}
if len(addrs) == 0 {
return BlockRelayProof{}, fmt.Errorf("Too many invalid precommits")
}
sort.Strings(addrs)
for _, addr := range addrs {
bp.Signatures = append(bp.Signatures, mapAddrs[addr])
}
kb, err := hex.DecodeString(leftMsg)
if err != nil {
return BlockRelayProof{}, err
}
bp.SignedDataPrefix = kb
if err != nil {
return BlockRelayProof{}, err
}
return bp, nil
}
func MakeOtherStoresMerkleHash(mspo rootmulti.MultiStoreProofOp) (cmn.HexBytes, cmn.HexBytes, cmn.HexBytes) {
var zoracleHash []byte
m := map[string][]byte{}
for _, si := range mspo.Proof.StoreInfos {
m[si.Name] = tmhash.Sum(tmhash.Sum(si.Core.CommitID.Hash))
if si.Name == "zoracle" {
zoracleHash = si.Core.CommitID.Hash
}
}
keys := []string{}
for k := range m {
if k != "zoracle" {
keys = append(keys, k)
}
}
sort.Strings(keys)
bs := [][]byte{}
for _, k := range keys {
bs = append(bs, m[k])
}
h1 := tmhash.Sum(
append(
[]byte{1},
append(
tmhash.Sum(append([]byte{0}, append(append([]byte{3}, []byte("acc")...), append([]byte{32}, m["acc"]...)...)...)),
tmhash.Sum(append([]byte{0}, append(append([]byte{12}, []byte("distribution")...), append([]byte{32}, m["distribution"]...)...)...))...,
)...,
),
)
h2 := tmhash.Sum(
append(
[]byte{1},
append(
tmhash.Sum(append([]byte{0}, append(append([]byte{3}, []byte("gov")...), append([]byte{32}, m["gov"]...)...)...)),
tmhash.Sum(append([]byte{0}, append(append([]byte{4}, []byte("main")...), append([]byte{32}, m["main"]...)...)...))...,
)...,
),
)
h3 := tmhash.Sum(
append(
[]byte{1},
append(
tmhash.Sum(append([]byte{0}, append(append([]byte{4}, []byte("mint")...), append([]byte{32}, m["mint"]...)...)...)),
tmhash.Sum(append([]byte{0}, append(append([]byte{6}, []byte("params")...), append([]byte{32}, m["params"]...)...)...))...,
)...,
),
)
h4 := tmhash.Sum(
append(
[]byte{1},
append(
tmhash.Sum(append([]byte{0}, append(append([]byte{8}, []byte("slashing")...), append([]byte{32}, m["slashing"]...)...)...)),
tmhash.Sum(append([]byte{0}, append(append([]byte{7}, []byte("staking")...), append([]byte{32}, m["staking"]...)...)...))...,
)...,
),
)
h5 := tmhash.Sum(append([]byte{0}, append(append([]byte{6}, []byte("supply")...), append([]byte{32}, m["supply"]...)...)...))
h6 := tmhash.Sum(append([]byte{1}, append(h1, h2...)...))
h7 := tmhash.Sum(append([]byte{1}, append(h3, h4...)...))
h8 := tmhash.Sum(append([]byte{1}, append(h6, h7...)...))
return h5, h8, zoracleHash
}
func GetProofHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
intRequestID, err := strconv.ParseUint(vars[requestIDTag], 10, 64)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
requestID := zoracle.RequestID(intRequestID)
rc, err := cliCtx.Client.Commit(nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
brp, err := GetBlockRelayProof(cliCtx, uint64(rc.Height))
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
var queryRequest zoracle.RequestQuerierInfo
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/zoracle/request/%d", requestID), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
err = cliCtx.Codec.UnmarshalJSON(res, &queryRequest)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
key := zoracle.ResultStoreKey(requestID, queryRequest.Request.OracleScriptID, queryRequest.Request.Calldata)
resp, err := cliCtx.Client.ABCIQueryWithOptions(
"/store/zoracle/key",
key,
rpcclient.ABCIQueryOptions{Height: rc.Height - 1, Prove: true},
)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
proof := resp.Response.GetProof()
if proof == nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, "proof not found")
return
}
ops := proof.GetOps()
if ops == nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, "proof ops not found")
return
}
var iavlOpData []byte
var multistoreOpData []byte
for _, op := range ops {
opType := op.GetType()
if opType == "iavl:v" {
iavlOpData = op.GetData()
} else if opType == "multistore" {
multistoreOpData = op.GetData()
}
}
if iavlOpData == nil || multistoreOpData == nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, "proof was corrupted")
return
}
var opiavl iavl.IAVLValueOp
err = cliCtx.Codec.UnmarshalBinaryLengthPrefixed(iavlOpData, &opiavl)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
odp := OracleDataProof{}
odp.RequestID = requestID
odp.Data = resp.Response.GetValue()
odp.OracleScriptID = queryRequest.Request.OracleScriptID
odp.Calldata = queryRequest.Request.Calldata
odp.Version = uint64(opiavl.Proof.Leaves[0].Version)
for i := len(opiavl.Proof.LeftPath) - 1; i >= 0; i-- {
path := opiavl.Proof.LeftPath[i]
imp := IAVLMerklePath{}
imp.SubtreeHeight = uint8(path.Height)
imp.SubtreeSize = uint64(path.Size)
imp.SubtreeVersion = uint64(path.Version)
if len(path.Right) == 0 {
imp.SiblingHash = path.Left
imp.IsDataOnRight = true
} else {
imp.SiblingHash = path.Right
imp.IsDataOnRight = false
}
odp.MerklePaths = append(odp.MerklePaths, imp)
}
var opms rootmulti.MultiStoreProofOp
err = cliCtx.Codec.UnmarshalBinaryLengthPrefixed(multistoreOpData, &opms)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
ssmh, osmh, oiavlsh := MakeOtherStoresMerkleHash(opms)
brp.OtherStoresMerkleHash = osmh
brp.SupplyStoresMerkleHash = ssmh
brp.OracleIAVLStateHash = oiavlsh
// Calculate byte for proofbytes
var relayAndVerifyArguments abi.Arguments
format := `[{"type":"bytes"},{"type":"bytes"}]`
err = json.Unmarshal([]byte(format), &relayAndVerifyArguments)
if err != nil {
panic(err)
}
blockRelayBytes, err := brp.encodeToEthData(uint64(rc.Height))
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
oracleDataBytes, err := odp.encodeToEthData(uint64(rc.Height))
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
evmProofBytes, err := relayAndVerifyArguments.Pack(blockRelayBytes, oracleDataBytes)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, Proof{
JsonProof: JsonProof{
BlockHeight: uint64(rc.Height),
OracleDataProof: odp,
BlockRelayProof: brp},
EVMProofBytes: evmProofBytes,
})
}
}