1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | var __read = (this && this.__read) || function (o, n) {
|
22 | var m = typeof Symbol === "function" && o[Symbol.iterator];
|
23 | if (!m) return o;
|
24 | var i = m.call(o), r, ar = [], e;
|
25 | try {
|
26 | while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
27 | }
|
28 | catch (error) { e = { error: error }; }
|
29 | finally {
|
30 | try {
|
31 | if (r && !r.done && (m = i["return"])) m.call(i);
|
32 | }
|
33 | finally { if (e) throw e.error; }
|
34 | }
|
35 | return ar;
|
36 | };
|
37 | var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
38 | if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
39 | if (ar || !(i in from)) {
|
40 | if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
41 | ar[i] = from[i];
|
42 | }
|
43 | }
|
44 | return to.concat(ar || Array.prototype.slice.call(from));
|
45 | };
|
46 | Object.defineProperty(exports, "__esModule", { value: true });
|
47 | exports.isRawNode = exports.decodeNode = exports.decodeRawNode = exports.LeafNode = exports.ExtensionNode = exports.BranchNode = void 0;
|
48 | var rlp = __importStar(require("rlp"));
|
49 | var ethereumjs_util_1 = require("ethereumjs-util");
|
50 | var nibbles_1 = require("./util/nibbles");
|
51 | var hex_1 = require("./util/hex");
|
52 | var BranchNode = (function () {
|
53 | function BranchNode() {
|
54 | this._branches = new Array(16).fill(null);
|
55 | this._value = null;
|
56 | }
|
57 | BranchNode.fromArray = function (arr) {
|
58 | var node = new BranchNode();
|
59 | node._branches = arr.slice(0, 16);
|
60 | node._value = arr[16];
|
61 | return node;
|
62 | };
|
63 | Object.defineProperty(BranchNode.prototype, "value", {
|
64 | get: function () {
|
65 | return this._value && this._value.length > 0 ? this._value : null;
|
66 | },
|
67 | set: function (v) {
|
68 | this._value = v;
|
69 | },
|
70 | enumerable: false,
|
71 | configurable: true
|
72 | });
|
73 | BranchNode.prototype.setBranch = function (i, v) {
|
74 | this._branches[i] = v;
|
75 | };
|
76 | BranchNode.prototype.raw = function () {
|
77 | return __spreadArray(__spreadArray([], __read(this._branches), false), [this._value], false);
|
78 | };
|
79 | BranchNode.prototype.serialize = function () {
|
80 | return rlp.encode(this.raw());
|
81 | };
|
82 | BranchNode.prototype.hash = function () {
|
83 | return (0, ethereumjs_util_1.keccak256)(this.serialize());
|
84 | };
|
85 | BranchNode.prototype.getBranch = function (i) {
|
86 | var b = this._branches[i];
|
87 | if (b !== null && b.length > 0) {
|
88 | return b;
|
89 | }
|
90 | else {
|
91 | return null;
|
92 | }
|
93 | };
|
94 | BranchNode.prototype.getChildren = function () {
|
95 | var children = [];
|
96 | for (var i = 0; i < 16; i++) {
|
97 | var b = this._branches[i];
|
98 | if (b !== null && b.length > 0) {
|
99 | children.push([i, b]);
|
100 | }
|
101 | }
|
102 | return children;
|
103 | };
|
104 | return BranchNode;
|
105 | }());
|
106 | exports.BranchNode = BranchNode;
|
107 | var ExtensionNode = (function () {
|
108 | function ExtensionNode(nibbles, value) {
|
109 | this._nibbles = nibbles;
|
110 | this._value = value;
|
111 | }
|
112 | ExtensionNode.encodeKey = function (key) {
|
113 | return (0, hex_1.addHexPrefix)(key, false);
|
114 | };
|
115 | ExtensionNode.decodeKey = function (key) {
|
116 | return (0, hex_1.removeHexPrefix)(key);
|
117 | };
|
118 | Object.defineProperty(ExtensionNode.prototype, "key", {
|
119 | get: function () {
|
120 | return this._nibbles.slice(0);
|
121 | },
|
122 | set: function (k) {
|
123 | this._nibbles = k;
|
124 | },
|
125 | enumerable: false,
|
126 | configurable: true
|
127 | });
|
128 | Object.defineProperty(ExtensionNode.prototype, "value", {
|
129 | get: function () {
|
130 | return this._value;
|
131 | },
|
132 | set: function (v) {
|
133 | this._value = v;
|
134 | },
|
135 | enumerable: false,
|
136 | configurable: true
|
137 | });
|
138 | ExtensionNode.prototype.encodedKey = function () {
|
139 | return ExtensionNode.encodeKey(this._nibbles.slice(0));
|
140 | };
|
141 | ExtensionNode.prototype.raw = function () {
|
142 | return [(0, nibbles_1.nibblesToBuffer)(this.encodedKey()), this._value];
|
143 | };
|
144 | ExtensionNode.prototype.serialize = function () {
|
145 | return rlp.encode(this.raw());
|
146 | };
|
147 | ExtensionNode.prototype.hash = function () {
|
148 | return (0, ethereumjs_util_1.keccak256)(this.serialize());
|
149 | };
|
150 | return ExtensionNode;
|
151 | }());
|
152 | exports.ExtensionNode = ExtensionNode;
|
153 | var LeafNode = (function () {
|
154 | function LeafNode(nibbles, value) {
|
155 | this._nibbles = nibbles;
|
156 | this._value = value;
|
157 | }
|
158 | LeafNode.encodeKey = function (key) {
|
159 | return (0, hex_1.addHexPrefix)(key, true);
|
160 | };
|
161 | LeafNode.decodeKey = function (encodedKey) {
|
162 | return (0, hex_1.removeHexPrefix)(encodedKey);
|
163 | };
|
164 | Object.defineProperty(LeafNode.prototype, "key", {
|
165 | get: function () {
|
166 | return this._nibbles.slice(0);
|
167 | },
|
168 | set: function (k) {
|
169 | this._nibbles = k;
|
170 | },
|
171 | enumerable: false,
|
172 | configurable: true
|
173 | });
|
174 | Object.defineProperty(LeafNode.prototype, "value", {
|
175 | get: function () {
|
176 | return this._value;
|
177 | },
|
178 | set: function (v) {
|
179 | this._value = v;
|
180 | },
|
181 | enumerable: false,
|
182 | configurable: true
|
183 | });
|
184 | LeafNode.prototype.encodedKey = function () {
|
185 | return LeafNode.encodeKey(this._nibbles.slice(0));
|
186 | };
|
187 | LeafNode.prototype.raw = function () {
|
188 | return [(0, nibbles_1.nibblesToBuffer)(this.encodedKey()), this._value];
|
189 | };
|
190 | LeafNode.prototype.serialize = function () {
|
191 | return rlp.encode(this.raw());
|
192 | };
|
193 | LeafNode.prototype.hash = function () {
|
194 | return (0, ethereumjs_util_1.keccak256)(this.serialize());
|
195 | };
|
196 | return LeafNode;
|
197 | }());
|
198 | exports.LeafNode = LeafNode;
|
199 | function decodeRawNode(raw) {
|
200 | if (raw.length === 17) {
|
201 | return BranchNode.fromArray(raw);
|
202 | }
|
203 | else if (raw.length === 2) {
|
204 | var nibbles = (0, nibbles_1.bufferToNibbles)(raw[0]);
|
205 | if ((0, hex_1.isTerminator)(nibbles)) {
|
206 | return new LeafNode(LeafNode.decodeKey(nibbles), raw[1]);
|
207 | }
|
208 | return new ExtensionNode(ExtensionNode.decodeKey(nibbles), raw[1]);
|
209 | }
|
210 | else {
|
211 | throw new Error('Invalid node');
|
212 | }
|
213 | }
|
214 | exports.decodeRawNode = decodeRawNode;
|
215 | function decodeNode(raw) {
|
216 | var des = rlp.decode(raw);
|
217 | if (!Array.isArray(des)) {
|
218 | throw new Error('Invalid node');
|
219 | }
|
220 | return decodeRawNode(des);
|
221 | }
|
222 | exports.decodeNode = decodeNode;
|
223 | function isRawNode(n) {
|
224 | return Array.isArray(n) && !Buffer.isBuffer(n);
|
225 | }
|
226 | exports.isRawNode = isRawNode;
|
227 |
|
\ | No newline at end of file |