UNPKG

4.38 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.isRawNode = exports.decodeNode = exports.decodeRawNode = exports.LeafNode = exports.ExtensionNode = exports.BranchNode = void 0;
4const ethereumjs_util_1 = require("ethereumjs-util");
5const nibbles_1 = require("./util/nibbles");
6const hex_1 = require("./util/hex");
7class BranchNode {
8 constructor() {
9 this._branches = new Array(16).fill(null);
10 this._value = null;
11 }
12 static fromArray(arr) {
13 const node = new BranchNode();
14 node._branches = arr.slice(0, 16);
15 node._value = arr[16];
16 return node;
17 }
18 get value() {
19 return this._value && this._value.length > 0 ? this._value : null;
20 }
21 set value(v) {
22 this._value = v;
23 }
24 setBranch(i, v) {
25 this._branches[i] = v;
26 }
27 raw() {
28 return [...this._branches, this._value];
29 }
30 serialize() {
31 return ethereumjs_util_1.rlp.encode(this.raw());
32 }
33 hash() {
34 return (0, ethereumjs_util_1.keccak256)(this.serialize());
35 }
36 getBranch(i) {
37 const b = this._branches[i];
38 if (b !== null && b.length > 0) {
39 return b;
40 }
41 else {
42 return null;
43 }
44 }
45 getChildren() {
46 const children = [];
47 for (let i = 0; i < 16; i++) {
48 const b = this._branches[i];
49 if (b !== null && b.length > 0) {
50 children.push([i, b]);
51 }
52 }
53 return children;
54 }
55}
56exports.BranchNode = BranchNode;
57class ExtensionNode {
58 constructor(nibbles, value) {
59 this._nibbles = nibbles;
60 this._value = value;
61 }
62 static encodeKey(key) {
63 return (0, hex_1.addHexPrefix)(key, false);
64 }
65 static decodeKey(key) {
66 return (0, hex_1.removeHexPrefix)(key);
67 }
68 get key() {
69 return this._nibbles.slice(0);
70 }
71 set key(k) {
72 this._nibbles = k;
73 }
74 get keyLength() {
75 return this._nibbles.length;
76 }
77 get value() {
78 return this._value;
79 }
80 set value(v) {
81 this._value = v;
82 }
83 encodedKey() {
84 return ExtensionNode.encodeKey(this._nibbles.slice(0));
85 }
86 raw() {
87 return [(0, nibbles_1.nibblesToBuffer)(this.encodedKey()), this._value];
88 }
89 serialize() {
90 return ethereumjs_util_1.rlp.encode(this.raw());
91 }
92 hash() {
93 return (0, ethereumjs_util_1.keccak256)(this.serialize());
94 }
95}
96exports.ExtensionNode = ExtensionNode;
97class LeafNode {
98 constructor(nibbles, value) {
99 this._nibbles = nibbles;
100 this._value = value;
101 }
102 static encodeKey(key) {
103 return (0, hex_1.addHexPrefix)(key, true);
104 }
105 static decodeKey(encodedKey) {
106 return (0, hex_1.removeHexPrefix)(encodedKey);
107 }
108 get key() {
109 return this._nibbles.slice(0);
110 }
111 set key(k) {
112 this._nibbles = k;
113 }
114 get keyLength() {
115 return this._nibbles.length;
116 }
117 get value() {
118 return this._value;
119 }
120 set value(v) {
121 this._value = v;
122 }
123 encodedKey() {
124 return LeafNode.encodeKey(this._nibbles.slice(0));
125 }
126 raw() {
127 return [(0, nibbles_1.nibblesToBuffer)(this.encodedKey()), this._value];
128 }
129 serialize() {
130 return ethereumjs_util_1.rlp.encode(this.raw());
131 }
132 hash() {
133 return (0, ethereumjs_util_1.keccak256)(this.serialize());
134 }
135}
136exports.LeafNode = LeafNode;
137function decodeRawNode(raw) {
138 if (raw.length === 17) {
139 return BranchNode.fromArray(raw);
140 }
141 else if (raw.length === 2) {
142 const nibbles = (0, nibbles_1.bufferToNibbles)(raw[0]);
143 if ((0, hex_1.isTerminator)(nibbles)) {
144 return new LeafNode(LeafNode.decodeKey(nibbles), raw[1]);
145 }
146 return new ExtensionNode(ExtensionNode.decodeKey(nibbles), raw[1]);
147 }
148 else {
149 throw new Error('Invalid node');
150 }
151}
152exports.decodeRawNode = decodeRawNode;
153function decodeNode(raw) {
154 const des = ethereumjs_util_1.rlp.decode(raw);
155 if (!Array.isArray(des)) {
156 throw new Error('Invalid node');
157 }
158 return decodeRawNode(des);
159}
160exports.decodeNode = decodeNode;
161function isRawNode(n) {
162 return Array.isArray(n) && !Buffer.isBuffer(n);
163}
164exports.isRawNode = isRawNode;
165//# sourceMappingURL=trieNode.js.map
\No newline at end of file