forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This reverts commit 1a37cc2.
- Loading branch information
Showing
9 changed files
with
164 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,3 @@ replace ( | |
) | ||
|
||
retract v0.43.0 | ||
|
||
// DEV | ||
replace github.com/lazyledger/smt => ../smt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package smt | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/gob" | ||
"hash" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/lazyledger/smt" | ||
"github.com/tendermint/tendermint/crypto/merkle" | ||
tmmerkle "github.com/tendermint/tendermint/proto/tendermint/crypto" | ||
) | ||
|
||
type HasherType byte | ||
|
||
const ( | ||
SHA256 HasherType = iota | ||
) | ||
|
||
const ( | ||
ProofType = "smt" | ||
) | ||
|
||
type ProofOp struct { | ||
Root []byte | ||
Key []byte | ||
Hasher HasherType | ||
Proof smt.SparseMerkleProof | ||
} | ||
|
||
var _ merkle.ProofOperator = (*ProofOp)(nil) | ||
|
||
// NewProofOp returns a ProofOp for a SparseMerkleProof. | ||
func NewProofOp(root, key []byte, hasher HasherType, proof smt.SparseMerkleProof) *ProofOp { | ||
return &ProofOp{ | ||
Root: root, | ||
Key: key, | ||
Hasher: hasher, | ||
Proof: proof, | ||
} | ||
} | ||
|
||
func (p *ProofOp) Run(args [][]byte) ([][]byte, error) { | ||
switch len(args) { | ||
case 0: // non-membership proof | ||
if !smt.VerifyProof(p.Proof, p.Root, p.Key, []byte{}, getHasher(p.Hasher)) { | ||
return nil, sdkerrors.Wrapf(types.ErrInvalidProof, "proof did not verify absence of key: %s", p.Key) | ||
} | ||
case 1: // membership proof | ||
if !smt.VerifyProof(p.Proof, p.Root, p.Key, args[0], getHasher(p.Hasher)) { | ||
return nil, sdkerrors.Wrapf(types.ErrInvalidProof, "proof did not verify existence of key %s with given value %x", p.Key, args[0]) | ||
} | ||
default: | ||
return nil, sdkerrors.Wrapf(types.ErrInvalidProof, "args must be length 0 or 1, got: %d", len(args)) | ||
} | ||
return [][]byte{p.Root}, nil | ||
} | ||
|
||
func (p *ProofOp) GetKey() []byte { | ||
return p.Key | ||
} | ||
|
||
func (p *ProofOp) ProofOp() tmmerkle.ProofOp { | ||
var data bytes.Buffer | ||
enc := gob.NewEncoder(&data) | ||
enc.Encode(p) | ||
return tmmerkle.ProofOp{ | ||
Type: "smt", | ||
Key: p.Key, | ||
Data: data.Bytes(), | ||
} | ||
} | ||
|
||
func ProofDecoder(pop tmmerkle.ProofOp) (merkle.ProofOperator, error) { | ||
dec := gob.NewDecoder(bytes.NewBuffer(pop.Data)) | ||
var proof ProofOp | ||
err := dec.Decode(&proof) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &proof, nil | ||
} | ||
|
||
func getHasher(hasher HasherType) hash.Hash { | ||
switch hasher { | ||
case SHA256: | ||
return sha256.New() | ||
default: | ||
return nil | ||
} | ||
} |
Oops, something went wrong.