/
prover.go
58 lines (49 loc) · 1.33 KB
/
prover.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
package keeper
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"github.com/sei-protocol/sei-chain/x/nitro/types"
)
const (
RightSibling = 1
LeftSibling = -1
)
// this function goes over all merkle proof hashes, and check if the merkle root generated is the same as the provided root
func (k *Keeper) Validate(root []byte, proof *types.MerkleProof) error {
if len(proof.Hash) != len(proof.Direction) {
return types.ErrValidateMerkleProof
}
i := 0
newHash := []byte(proof.Commitment)
for i < len(proof.Hash) {
if proof.Direction[i] == RightSibling {
newHash = Hash(newHash, []byte(proof.Hash[i]))
} else {
newHash = Hash([]byte(proof.Hash[i]), newHash)
}
i++
}
if !bytes.Equal(root, newHash) {
return types.ErrInvalidMerkleProof
}
return nil
}
func Hash(val1, val2 []byte) []byte {
sum := sha256.Sum256(append(val1, val2...))
return sum[:]
}
func AccountToValue(account types.Account) ([]byte, error) {
value := [sha256.Size]byte{}
lamportbz := make([]byte, 8)
binary.BigEndian.PutUint64(lamportbz, account.Lamports)
rentepochbz := make([]byte, 8)
binary.BigEndian.PutUint64(rentepochbz, account.RentEpoch)
databz, err := hex.DecodeString(account.Data)
if err != nil {
return nil, err
}
value = sha256.Sum256(append(append(lamportbz, rentepochbz...), databz...))
return value[:], nil
}