/
proofs.go
117 lines (94 loc) · 3.98 KB
/
proofs.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
package utils
import (
"errors"
"fmt"
"net/url"
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v5/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v5/modules/core/23-commitment/types"
ibckeeper "github.com/cosmos/ibc-go/v5/modules/core/keeper"
tmclienttypes "github.com/cosmos/ibc-go/v5/modules/light-clients/07-tendermint/types"
"github.com/tendermint/tendermint/proto/tendermint/crypto"
claimsmanagerkeeper "github.com/nephirim/blackfury/x/claimsmanager/keeper"
)
type ProofOpsFn func(ctx sdk.Context, ibcKeeper *ibckeeper.Keeper, connectionID, chainID string, height int64, module string, key []byte, data []byte, proofOps *crypto.ProofOps) error
type SelfProofOpsFn func(ctx sdk.Context, claimsKeeper claimsmanagerkeeper.Keeper, consensusStateKey, module string, key []byte, data []byte, proofOps *crypto.ProofOps) error
func ValidateProofOps(
ctx sdk.Context,
ibcKeeper *ibckeeper.Keeper,
connectionID,
chainID string,
height int64,
module string,
key,
data []byte,
proofOps *crypto.ProofOps,
) error {
if proofOps == nil {
return errors.New("unable to validate proof. No proof submitted")
}
connection, _ := ibcKeeper.ConnectionKeeper.GetConnection(ctx, connectionID)
csHeight := clienttypes.NewHeight(clienttypes.ParseChainID(chainID), uint64(height)+1)
consensusState, found := ibcKeeper.ClientKeeper.GetClientConsensusState(ctx, connection.ClientId, csHeight)
if !found {
return errors.New("unable to fetch consensus state")
}
clientState, found := ibcKeeper.ClientKeeper.GetClientState(ctx, connection.ClientId)
if !found {
return errors.New("unable to fetch client state")
}
path := commitmenttypes.NewMerklePath([]string{module, url.PathEscape(string(key))}...)
merkleProof, err := commitmenttypes.ConvertProofs(proofOps)
if err != nil {
return errors.New("error converting proofs")
}
tmClientState, ok := clientState.(*tmclienttypes.ClientState)
if !ok {
return errors.New("error unmarshaling client state")
}
if len(data) != 0 {
// if we got a non-nil response, verify inclusion proof.
if err := merkleProof.VerifyMembership(tmClientState.ProofSpecs, consensusState.GetRoot(), path, data); err != nil {
return fmt.Errorf("unable to verify inclusion proof: %w", err)
}
return nil
}
// if we got a nil response, verify non inclusion proof.
if err := merkleProof.VerifyNonMembership(tmClientState.ProofSpecs, consensusState.GetRoot(), path); err != nil {
return fmt.Errorf("unable to verify non-inclusion proof: %w", err)
}
return nil
}
func ValidateSelfProofOps(ctx sdk.Context, claimsKeeper claimsmanagerkeeper.Keeper, consensusStateKey, module string, key, data []byte, proofOps *crypto.ProofOps) error {
if proofOps == nil {
return errors.New("unable to validate proof. No proof submitted")
}
consensusState, found := claimsKeeper.GetSelfConsensusState(ctx, consensusStateKey)
if !found {
return errors.New("unable to lookup self-consensus state")
}
proofSpecs := commitmenttypes.GetSDKSpecs()
path := commitmenttypes.NewMerklePath([]string{module, url.PathEscape(string(key))}...)
merkleProof, err := commitmenttypes.ConvertProofs(proofOps)
if err != nil {
return errors.New("error converting proofs")
}
if len(data) != 0 {
// if we got a non-nil response, verify inclusion proof.
if err := merkleProof.VerifyMembership(proofSpecs, consensusState.GetRoot(), path, data); err != nil {
return fmt.Errorf("unable to verify inclusion proof: %w", err)
}
return nil
}
// if we got a nil response, verify non inclusion proof.
if err := merkleProof.VerifyNonMembership(proofSpecs, consensusState.GetRoot(), path); err != nil {
return fmt.Errorf("unable to verify non-inclusion proof: %w", err)
}
return nil
}
func MockSelfProofOps(_ sdk.Context, _ claimsmanagerkeeper.Keeper, _, _ string, _, _ []byte, _ *crypto.ProofOps) error {
return nil
}
func MockProofOps(_ sdk.Context, _ *ibckeeper.Keeper, _, _ string, _ int64, _ string, _, _ []byte, _ *crypto.ProofOps) error {
return nil
}