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