1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.SecureTrie = void 0;
|
4 | const ethereumjs_util_1 = require("ethereumjs-util");
|
5 | const checkpointTrie_1 = require("./checkpointTrie");
|
6 | /**
|
7 | * You can create a secure Trie where the keys are automatically hashed
|
8 | * using **keccak256** by using `import { SecureTrie as Trie } from 'merkle-patricia-tree'`.
|
9 | * It has the same methods and constructor as `Trie`.
|
10 | * @class SecureTrie
|
11 | * @extends Trie
|
12 | * @public
|
13 | */
|
14 | class SecureTrie extends checkpointTrie_1.CheckpointTrie {
|
15 | constructor(...args) {
|
16 | super(...args);
|
17 | }
|
18 | /**
|
19 | * Gets a value given a `key`
|
20 | * @param key - the key to search for
|
21 | * @returns A Promise that resolves to `Buffer` if a value was found or `null` if no value was found.
|
22 | */
|
23 | async get(key) {
|
24 | const hash = (0, ethereumjs_util_1.keccak256)(key);
|
25 | const value = await super.get(hash);
|
26 | return value;
|
27 | }
|
28 | /**
|
29 | * Stores a given `value` at the given `key`.
|
30 | * For a falsey value, use the original key to avoid double hashing the key.
|
31 | * @param key
|
32 | * @param value
|
33 | */
|
34 | async put(key, val) {
|
35 | if (!val || val.toString() === '') {
|
36 | await this.del(key);
|
37 | }
|
38 | else {
|
39 | const hash = (0, ethereumjs_util_1.keccak256)(key);
|
40 | await super.put(hash, val);
|
41 | }
|
42 | }
|
43 | /**
|
44 | * Deletes a value given a `key`.
|
45 | * @param key
|
46 | */
|
47 | async del(key) {
|
48 | const hash = (0, ethereumjs_util_1.keccak256)(key);
|
49 | await super.del(hash);
|
50 | }
|
51 | /**
|
52 | * prove has been renamed to {@link SecureTrie.createProof}.
|
53 | * @deprecated
|
54 | * @param trie
|
55 | * @param key
|
56 | */
|
57 | static async prove(trie, key) {
|
58 | return this.createProof(trie, key);
|
59 | }
|
60 | /**
|
61 | * Creates a proof that can be verified using {@link SecureTrie.verifyProof}.
|
62 | * @param trie
|
63 | * @param key
|
64 | */
|
65 | static createProof(trie, key) {
|
66 | const hash = (0, ethereumjs_util_1.keccak256)(key);
|
67 | return super.createProof(trie, hash);
|
68 | }
|
69 | /**
|
70 | * Verifies a proof.
|
71 | * @param rootHash
|
72 | * @param key
|
73 | * @param proof
|
74 | * @throws If proof is found to be invalid.
|
75 | * @returns The value from the key.
|
76 | */
|
77 | static async verifyProof(rootHash, key, proof) {
|
78 | const hash = (0, ethereumjs_util_1.keccak256)(key);
|
79 | return super.verifyProof(rootHash, hash, proof);
|
80 | }
|
81 | /**
|
82 | * Returns a copy of the underlying trie with the interface of SecureTrie.
|
83 | * @param includeCheckpoints - If true and during a checkpoint, the copy will contain the checkpointing metadata and will use the same scratch as underlying db.
|
84 | */
|
85 | copy(includeCheckpoints = true) {
|
86 | const db = this.db.copy();
|
87 | const secureTrie = new SecureTrie(db._leveldb, this.root);
|
88 | if (includeCheckpoints && this.isCheckpoint) {
|
89 | secureTrie.db.checkpoints = [...this.db.checkpoints];
|
90 | }
|
91 | return secureTrie;
|
92 | }
|
93 | }
|
94 | exports.SecureTrie = SecureTrie;
|
95 | //# sourceMappingURL=secure.js.map |
\ | No newline at end of file |