|
5 | 5 | */ |
6 | 6 | 'use strict'; |
7 | 7 |
|
8 | | -const assert = require('bsert'); |
9 | | -const {proofTypes} = require('./common'); |
| 8 | +const BLAKE2b = require('./blake2b'); |
| 9 | +const Proof = require('urkel/lib/proof'); |
10 | 10 |
|
11 | | -const HASH_SIZE = 32; |
12 | 11 | const HASH_BITS = 256; |
13 | | -const EMPTY_PROOF = Buffer.alloc(4, 0x00); |
14 | 12 |
|
15 | | -class EncodingError extends Error { |
16 | | - /** |
17 | | - * Create an encoding error. |
18 | | - * @constructor |
19 | | - * @param {Number} offset |
20 | | - * @param {String} reason |
21 | | - */ |
22 | | - |
23 | | - constructor(offset, reason, start) { |
24 | | - super(); |
25 | | - |
26 | | - this.type = 'EncodingError'; |
27 | | - this.name = 'EncodingError'; |
28 | | - this.code = 'ERR_ENCODING'; |
29 | | - this.message = `${reason} (offset=${offset}).`; |
30 | | - |
31 | | - if (Error.captureStackTrace) |
32 | | - Error.captureStackTrace(this, start || EncodingError); |
33 | | - } |
34 | | -} |
35 | | - |
36 | | -class Proof { |
| 13 | +class WrappedProof { |
37 | 14 | constructor() { |
38 | | - this.type = proofTypes.TYPE_UNKNOWN; |
39 | | - this.depth = 0; |
40 | | - this.nodesLen = 0; |
41 | | - this.raw = EMPTY_PROOF; |
| 15 | + this._proof = new Proof(); |
| 16 | + this._raw = null; |
| 17 | + this._size = null; |
42 | 18 | } |
43 | 19 |
|
44 | | - getSize() { |
45 | | - return this.raw.length; |
| 20 | + get type() { |
| 21 | + return this._proof.type; |
46 | 22 | } |
47 | 23 |
|
48 | | - write(data, off) { |
49 | | - checkWrite(off + this.raw.length <= data.length, off); |
50 | | - this.raw.copy(data, off); |
51 | | - return off + this.raw.length; |
52 | | - } |
53 | | - |
54 | | - read(data, off) { |
55 | | - assert(Buffer.isBuffer(data)); |
56 | | - assert((off >>> 0) === off); |
57 | | - |
58 | | - let pos = off; |
59 | | - |
60 | | - const field = readU16(data, pos); |
61 | | - pos += 2; |
62 | | - this.type = field >>> 14; |
63 | | - this.depth = field & ~(3 << 14); |
64 | | - |
65 | | - if (this.depth > HASH_BITS) |
66 | | - throw new EncodingError(pos, 'Invalid depth'); |
67 | | - |
68 | | - checkRead(pos + 2 <= data.length, pos); |
69 | | - |
70 | | - const count = readU16(data, pos); |
71 | | - const bsize = (count + 7) >>> 3; |
72 | | - pos += 2; |
| 24 | + /** |
| 25 | + * @returns {Number} |
| 26 | + */ |
73 | 27 |
|
74 | | - if (count > HASH_BITS) |
75 | | - throw new EncodingError(pos, 'Proof too large'); |
| 28 | + getSize() { |
| 29 | + if (!this._size) |
| 30 | + this._size = this._proof.getSize(BLAKE2b, HASH_BITS); |
76 | 31 |
|
77 | | - checkRead(pos + bsize <= data.length, pos); |
78 | | - pos += bsize; |
| 32 | + return this._size; |
| 33 | + } |
79 | 34 |
|
80 | | - for (let i = 0; i < count; i++) { |
81 | | - checkRead(pos + 2 <= data.length, pos); |
| 35 | + /** |
| 36 | + * @param {Buffer} data |
| 37 | + * @param {Number} off |
| 38 | + * @returns {Number} |
| 39 | + */ |
82 | 40 |
|
83 | | - if (hasBit(data, (off + 4) * 8 + i)) |
84 | | - pos = skipReadBits(data, pos); |
| 41 | + write(data, off) { |
| 42 | + return this._proof.write(data, off, BLAKE2b, HASH_BITS); |
| 43 | + } |
85 | 44 |
|
86 | | - pos += HASH_SIZE; |
87 | | - } |
| 45 | + /** |
| 46 | + * @param {bio.BufferWriter|bio.StaticWriter} bw |
| 47 | + * @returns {bio.BufferWriter|bio.StaticWriter} |
| 48 | + */ |
88 | 49 |
|
89 | | - this.nodesLen = count; |
| 50 | + writeBW(bw) { |
| 51 | + return this._proof.writeBW(bw, BLAKE2b, HASH_BITS); |
| 52 | + } |
90 | 53 |
|
91 | | - switch (this.type) { |
92 | | - case proofTypes.TYPE_DEADEND: { |
93 | | - break; |
94 | | - } |
| 54 | + /** |
| 55 | + * @param {Buffer} data |
| 56 | + * @param {Number} off |
| 57 | + * @returns {Number} |
| 58 | + */ |
95 | 59 |
|
96 | | - case proofTypes.TYPE_SHORT: { |
97 | | - pos = skipReadBits(data, pos); |
98 | | - pos += HASH_SIZE; |
99 | | - pos += HASH_SIZE; |
100 | | - break; |
101 | | - } |
| 60 | + read(data, off) { |
| 61 | + this._proof.read(data, off, BLAKE2b, HASH_BITS); |
| 62 | + return this; |
| 63 | + } |
102 | 64 |
|
103 | | - case proofTypes.TYPE_COLLISION: { |
104 | | - pos += HASH_BITS >>> 3; |
105 | | - pos += HASH_SIZE; |
106 | | - break; |
107 | | - } |
| 65 | + /** |
| 66 | + * @param {BufferReader} br |
| 67 | + * @returns {this} |
| 68 | + */ |
108 | 69 |
|
109 | | - case proofTypes.TYPE_EXISTS: { |
110 | | - checkRead(pos + 2 <= data.length, pos); |
111 | | - const size = readU16(data, pos); |
112 | | - pos += 2; |
113 | | - pos += size; |
| 70 | + readBR(br) { |
| 71 | + this._proof.readBR(br, BLAKE2b, HASH_BITS); |
| 72 | + return this; |
| 73 | + } |
114 | 74 |
|
115 | | - break; |
116 | | - } |
| 75 | + /** |
| 76 | + * @returns {Buffer} |
| 77 | + */ |
117 | 78 |
|
118 | | - default: { |
119 | | - throw new Error('Invalid type.'); |
120 | | - } |
121 | | - } |
| 79 | + encode() { |
| 80 | + if (!this._raw) |
| 81 | + this._raw = this._proof.encode(BLAKE2b, HASH_BITS); |
122 | 82 |
|
123 | | - this.raw = data.slice(off, pos); |
124 | | - return pos; |
| 83 | + return this._raw; |
125 | 84 | } |
126 | 85 |
|
127 | | - encode() { |
128 | | - return this.raw; |
129 | | - } |
| 86 | + /** |
| 87 | + * @param {Buffer} data |
| 88 | + * @returns {this} |
| 89 | + */ |
130 | 90 |
|
131 | 91 | decode(data) { |
| 92 | + this._raw = data; |
132 | 93 | this.read(data, 0); |
133 | 94 | return this; |
134 | 95 | } |
135 | 96 |
|
136 | | - readBR(br) { |
137 | | - assert(br && typeof br.readU8 === 'function'); |
138 | | - br.offset = this.read(br.data, br.offset); |
139 | | - return this; |
140 | | - } |
| 97 | + /** |
| 98 | + * Verify |
| 99 | + * @param {Buffer} root |
| 100 | + * @param {Buffer} key |
| 101 | + * @returns {Array} |
| 102 | + */ |
141 | 103 |
|
142 | | - writeBW(bw, hash, bits) { |
143 | | - assert(bw && typeof bw.writeU8 === 'function'); |
144 | | - if (bw.data) |
145 | | - bw.offset = this.write(bw.data, bw.offset, hash, bits); |
146 | | - else |
147 | | - bw.writeBytes(this.encode(hash, bits)); |
148 | | - return bw; |
| 104 | + verify(root, key) { |
| 105 | + return this._proof.verify(root, key, BLAKE2b, HASH_BITS); |
149 | 106 | } |
150 | 107 |
|
| 108 | + /** |
| 109 | + * @param {Buffer} data |
| 110 | + * @param {Number} off |
| 111 | + * @returns {WrappedProof} |
| 112 | + */ |
| 113 | + |
151 | 114 | static read(data, off) { |
152 | 115 | return new this().read(data, off); |
153 | 116 | } |
154 | 117 |
|
| 118 | + /** |
| 119 | + * @param {BufferReader} br |
| 120 | + * @returns {WrappedProof} |
| 121 | + */ |
| 122 | + |
155 | 123 | static readBR(br) { |
156 | 124 | return new this().readBR(br); |
157 | 125 | } |
158 | 126 |
|
| 127 | + /** |
| 128 | + * @param {Buffer} data |
| 129 | + * @returns {WrappedProof} |
| 130 | + */ |
| 131 | + |
159 | 132 | static decode(data) { |
160 | 133 | return new this().decode(data); |
161 | 134 | } |
162 | 135 |
|
| 136 | + /** |
| 137 | + * @param {*} obj |
| 138 | + * @returns {Boolean} |
| 139 | + */ |
| 140 | + |
163 | 141 | static isProof(obj) { |
164 | 142 | return obj instanceof this; |
165 | 143 | } |
166 | 144 | } |
167 | 145 |
|
168 | | -function skipReadBits(data, off) { |
169 | | - checkRead(off + 2 <= data.length, off); |
170 | | - |
171 | | - let size = data[off]; |
172 | | - off += 1; |
173 | | - |
174 | | - if (size & 0x80) { |
175 | | - size -= 0x80; |
176 | | - size *= 0x100; |
177 | | - size += data[off]; |
178 | | - off += 1; |
179 | | - } |
180 | | - |
181 | | - const bytes = (size + 7) >>> 3; |
182 | | - |
183 | | - checkRead(off + bytes <= data.length, off); |
184 | | - |
185 | | - return off + bytes; |
186 | | -} |
187 | | - |
188 | | -function hasBit(key, index) { |
189 | | - const oct = index >>> 3; |
190 | | - const bit = index & 7; |
191 | | - return (key[oct] >>> (7 - bit)) & 1; |
192 | | -} |
193 | | - |
194 | | -function readU16(data, off) { |
195 | | - return data[off++] + data[off] * 0x100; |
196 | | -} |
197 | | - |
198 | | -function checkWrite(ok, offset, start) { |
199 | | - if (!ok) { |
200 | | - throw new EncodingError(offset, |
201 | | - 'Out of bounds write', |
202 | | - start || checkWrite); |
203 | | - } |
204 | | -} |
205 | | - |
206 | | -function checkRead(ok, offset, start) { |
207 | | - if (!ok) { |
208 | | - throw new EncodingError(offset, |
209 | | - 'Out of bounds read', |
210 | | - start || checkRead); |
211 | | - } |
212 | | -} |
213 | | - |
214 | | -module.exports = Proof; |
| 146 | +module.exports = WrappedProof; |
0 commit comments