package main
import (
"bytes"
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/trie"
)
func updateString(trie *trie.Trie, k, v string) {
trie.Update([]byte(k), []byte(v))
}
type proofList [][]byte
func (n *proofList) Put(key []byte, value []byte) error {
*n = append(*n, value)
return nil
}
func (n *proofList) Delete(key []byte) error {
panic("not supported")
}
func (n proofList) Has(key []byte) (bool, error) {
for _, value := range n {
hash := crypto.Keccak256(value)
if bytes.Equal(key, hash) {
return true, nil
}
}
return false, nil
}
func (n proofList) Get(key []byte) ([]byte, error) {
for _, value := range n {
hash := crypto.Keccak256(value)
if bytes.Equal(key, hash) {
return value, nil
}
}
return []byte{}, nil
}
func main() {
t := new(trie.Trie)
updateString(t, "doe", "reindeer")
updateString(t, "dog", "puppy")
updateString(t, "dogglesworth", "cat")
rootHash := t.Hash()
spew.Dump(rootHash)
proofKey := []byte("dogglesworth")
var proof proofList
err := t.Prove(proofKey, 0, &proof)
// proof := memorydb.New()
// err := t.Prove([]byte("111"), 0, proof)
spew.Dump(err)
spew.Dump(proof)
value, nodes, err := trie.VerifyProof(rootHash, proofKey, proof)
spew.Dump(value, nodes, err)
}