secure_trie.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package trie
  17. import "github.com/ethereum/go-ethereum/crypto"
  18. var keyPrefix = []byte("secure-key-")
  19. type SecureTrie struct {
  20. *Trie
  21. }
  22. func NewSecure(root []byte, backend Backend) *SecureTrie {
  23. return &SecureTrie{New(root, backend)}
  24. }
  25. func (self *SecureTrie) Update(key, value []byte) Node {
  26. shaKey := crypto.Sha3(key)
  27. self.Trie.cache.Put(append(keyPrefix, shaKey...), key)
  28. return self.Trie.Update(shaKey, value)
  29. }
  30. func (self *SecureTrie) UpdateString(key, value string) Node {
  31. return self.Update([]byte(key), []byte(value))
  32. }
  33. func (self *SecureTrie) Get(key []byte) []byte {
  34. return self.Trie.Get(crypto.Sha3(key))
  35. }
  36. func (self *SecureTrie) GetString(key string) []byte {
  37. return self.Get([]byte(key))
  38. }
  39. func (self *SecureTrie) Delete(key []byte) Node {
  40. return self.Trie.Delete(crypto.Sha3(key))
  41. }
  42. func (self *SecureTrie) DeleteString(key string) Node {
  43. return self.Delete([]byte(key))
  44. }
  45. func (self *SecureTrie) Copy() *SecureTrie {
  46. return &SecureTrie{self.Trie.Copy()}
  47. }
  48. func (self *SecureTrie) GetKey(shaKey []byte) []byte {
  49. return self.Trie.cache.Get(append(keyPrefix, shaKey...))
  50. }