Skip to content

Commit

Permalink
Merge pull request #11 from dongrie/fix-verify-membership
Browse files Browse the repository at this point in the history
Fix VerifyMembership proof
  • Loading branch information
siburu authored Jan 31, 2024
2 parents 2391112 + 331c660 commit b37f2fc
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion modules/light-clients/xx-mock/types/client_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package types
import (
"bytes"
"crypto/sha256"
"encoding/binary"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
)

Expand Down Expand Up @@ -113,7 +115,35 @@ func (cs ClientState) VerifyMembership(
return sdkerrors.Wrap(clienttypes.ErrConsensusStateNotFound, "please ensure the proof was constructed against a height that exists on the client")
}

h := sha256.Sum256(value)
// sha256(abi.encodePacked(height.toUint128(), sha256(prefix), sha256(path), sha256(value)))
revisionNumber := height.GetRevisionNumber()
revisionHeight := height.GetRevisionHeight()

heightBuf := make([]byte, 16)
binary.BigEndian.PutUint64(heightBuf[:8], revisionNumber)
binary.BigEndian.PutUint64(heightBuf[8:], revisionHeight)

merklePath := path.(commitmenttypes.MerklePath)
mPrefix, err := merklePath.GetKey(0)
if err != nil {
return sdkerrors.Wrapf(err, "invalid merkle path key at index 0")
}
mPath, err := merklePath.GetKey(1)
if err != nil {
return sdkerrors.Wrapf(err, "invalid merkle path key at index 1")
}

hashPrefix := sha256.Sum256([]byte(mPrefix))
hashPath := sha256.Sum256([]byte(mPath))
hashValue := sha256.Sum256([]byte(value))

var combined []byte
combined = append(combined, heightBuf...)
combined = append(combined, hashPrefix[:]...)
combined = append(combined, hashPath[:]...)
combined = append(combined, hashValue[:]...)
h := sha256.Sum256(combined)

if !bytes.Equal(proof, h[:]) {
return sdkerrors.Wrapf(ErrInvalidProof, "expected the proof '%X', actually got '%X'", h, proof)
}
Expand Down

0 comments on commit b37f2fc

Please sign in to comment.