-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibcFunctions.js
508 lines (395 loc) · 15.5 KB
/
ibcFunctions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
const { SerialBuffer, createInitialTypes } = require("eosjs/dist/eosjs-serialize");
const types = createInitialTypes();
const crypto = require("crypto");
const hex64 = require('hex64');
const axios = require("axios");
const historyProvider = process.env.HISTORY_PROVIDER;
const eosjsTypes = {
name: types.get("name"),
bytes: types.get("bytes"),
uint8: types.get("uint8"),
uint16: types.get("uint16"),
uint32: types.get("uint32"),
uint64: types.get("uint64"),
varuint32: types.get("varuint32"),
checksum256: types.get("checksum256")
}
const { name, uint8, uint64,varuint32, checksum256, bytes } = eosjsTypes;
const nameToUint64 = (s) => {
let n = 0n;
let i = 0;
for (; i < 12 && s[i]; i++) n |= BigInt(char_to_symbol(s.charCodeAt(i)) & 0x1f) << BigInt(64 - 5 * (i + 1));
if (i == 12) n |= BigInt(char_to_symbol(s.charCodeAt(i)) & 0x0f);
return n.toString();
};
const char_to_symbol = c => {
if (typeof c == 'string') c = c.charCodeAt(0);
if (c >= 'a'.charCodeAt(0) && c <= 'z'.charCodeAt(0)) return c - 'a'.charCodeAt(0) + 6;
if (c >= '1'.charCodeAt(0) && c <= '5'.charCodeAt(0)) return c - '1'.charCodeAt(0) + 1;
return 0;
};
function getActionProof(block_to_prove, requested_action_receipt_digest){
let { action_receipt_digests, action_return_value } = getReceiptDigests(block_to_prove, requested_action_receipt_digest);
if (action_receipt_digests.length>1) action_receipt_digests = getProof(createTree(action_receipt_digests), requested_action_receipt_digest);
return {
amproofpath: action_receipt_digests,
returnvalue: action_return_value && action_return_value.length ? action_return_value : ""
};
}
function getReceiptDigests(block_to_prove, action_receipt_digest){
if (historyProvider === 'firehose') return getFirehoseReceiptDigests(block_to_prove, action_receipt_digest);
else if (historyProvider === 'ship') return getShipReceiptDigests(block_to_prove, action_receipt_digest);
else if (historyProvider === 'greymass') return getNodeosReceiptDigests(block_to_prove, action_receipt_digest);
}
function getShipReceiptDigests(block_to_prove, action_receipt_digest){
let action_return_value;
var action_receipt_digests = [];
var transactions = block_to_prove.transactions;
for (traces of transactions){
for (trace of traces.action_traces.sort((a,b)=> a.receipt.global_sequence > b.receipt.global_sequence? 1 :-1)){
if (!trace || !trace.receipt) continue;
var receipt_digest = getReceiptDigest(trace.receipt);
//if this is the trace of the action we are trying to prove, assign the action_return_value from trace result
if (receipt_digest === action_receipt_digest && trace.return_value) action_return_value = trace.return_value.toString()
action_receipt_digests.push(receipt_digest);
}
}
return { action_receipt_digests, action_return_value };
}
function getNodeosReceiptDigests(block_to_prove, action_receipt_digest){
let action_return_value;
var action_receipt_digests = [];
for (traces of block_to_prove.transactions){
for (trace of traces.sort((a,b)=> a.receipt.global_sequence > b.receipt.global_sequence? 1 :-1)){
//if this is the trace of the action we are trying to prove, assign the action_return_value from trace result
if (trace.action_receipt_digest === action_receipt_digest && trace.return_value) action_return_value = trace.return_value.toString()
action_receipt_digests.push(trace.action_receipt_digest );
}
}
return { action_receipt_digests, action_return_value };
}
function getFirehoseReceiptDigests(block_to_prove, action_receipt_digest){
let action_return_value;
var action_receipt_digests = [];
var transactions = block_to_prove.unfilteredTransactionTraces.map(item => item.actionTraces);
for (traces of transactions){
for (trace of traces){
if (!trace || !trace.receipt) continue;
trace.action.rawData = hex64.toHex(trace.action.rawData);
var receipt_digest = getReceiptDigest(trace.receipt);
//if this is the trace of the action we are trying to prove, assign the action_return_value from trace result
if (receipt_digest === action_receipt_digest && trace.returnValue) action_return_value = hex64.toHex(trace.returnValue)
action_receipt_digests.push(receipt_digest);
}
}
return { action_receipt_digests, action_return_value };
}
function getBmProof(block_to_prove, last_proven_block ){
return new Promise(async resolve=>{
const blocksTofetch = [];
let proofPath = getProofPath(block_to_prove-1, last_proven_block-1);
for (var i = 0 ; i < proofPath.length; i++){
var multiplier = Math.pow(2, i) ;
var block_num = (proofPath[i].pairIndex + 1) * multiplier;
if (proofPath[i].pairIndex % 2 == 0 ) block_num+=1;
block_num = Math.min(block_num, last_proven_block-1);
blocksTofetch.push(block_num);
}
const uniqueList = [];
for (var num of blocksTofetch) if (!uniqueList.includes(num)) uniqueList.push(num);
let result = (await axios(`${process.env.LIGHTPROOF_API}?blocks=${uniqueList.join(',')}`)).data;
let bmproof = [];
for (var i = 0 ; i < blocksTofetch.length;i++){
const block = result.find(r=>r.num === blocksTofetch[i]);
if(!block){
console.log("Error, block not found!", blocksTofetch[i]);
process.exit();
}
const path = i == 0 && (block_to_prove - 1) % 2 == 0 ? 1 :
i == 0 && (block_to_prove - 1) % 2 != 0 ? 2 :
proofPath[i].pairIndex % 2 == 0 ? 3 :
4;
let hash;
if ( path == 1 ) hash = block.id;
else if ( path == 2 || path == 3 ) hash = block.nodes[0];
else hash = append(block.id, block.nodes, block.num - 1, i).root;
hash = applyMask(hash, proofPath[i].isLeft).toString("hex");
bmproof.push(hash);
}
resolve(bmproof)
})
}
function append(digest, _active_nodes, node_count, stop_at_depth = -1) {
var partial = false;
var max_depth = calculate_max_depth(node_count + 1);
// var implied_count = next_power_of_2(node_count);
var current_depth = stop_at_depth == -1 ? max_depth - 1 : stop_at_depth;
var index = node_count;
var top = digest;
var count = 0;
var updated_active_nodes = [];
while (current_depth > 0) {
if (!(index & 0x1)) {
if (!partial) updated_active_nodes.push(top);
top = hashPair(make_canonical_pair(top, top));
partial = true;
}
else {
var left_value = _active_nodes[count];
count++;
if (partial) updated_active_nodes.push(left_value);
top = hashPair(make_canonical_pair(left_value, top));
}
current_depth--;
index = index >> 1;
}
updated_active_nodes.push(top);
return { nodes: updated_active_nodes, root: top };
}
function merkle(ids) {
if( 0 == ids.length ) return "";
while( ids.length > 1 ) {
if( ids.length % 2 ) ids.push(ids[ids.length-1]);
for (var i = 0; i < ids.length / 2; i++) {
var p = make_canonical_pair(ids[2 * i], ids[(2 * i) + 1]);
var buffLeft = Buffer.from(p.l, "hex")
var buffRight = Buffer.from(p.r, "hex")
var buffFinal = Buffer.concat([buffLeft, buffRight]);
var finalHash = crypto.createHash("sha256").update(buffFinal).digest("hex");
ids[i] = finalHash;
}
ids = ids.slice(0, ids.length / 2);
}
return ids[0];
}
function createTree(leaves, mask = true){
var n_leaves = JSON.parse(JSON.stringify(leaves));
var tree = { leaves, layers : [] }
var layer_index = 0 ;
while (n_leaves.length>1){
if (n_leaves.length % 2) n_leaves.push(n_leaves[n_leaves.length-1]);
tree.layers.push([]);
for (var i = 0; i<n_leaves.length / 2; i++){
var leftLeaf;
var rightLeaf;
if (mask){
leftLeaf = maskLeft(n_leaves[2*i]);
rightLeaf = maskRight(n_leaves[2*i+1]);
}
else {
leftLeaf = n_leaves[2*i];
rightLeaf = n_leaves[2*i+1];
}
tree.layers[layer_index].push(leftLeaf.toString("hex"));
tree.layers[layer_index].push(rightLeaf.toString("hex"));
var hash = crypto.createHash('sha256')
.update(Buffer.concat([Buffer.from(leftLeaf, "hex"), Buffer.from(rightLeaf, "hex")]))
.digest("hex");
n_leaves[i] = hash;
}
n_leaves = n_leaves.slice(0, n_leaves.length / 2)
layer_index++;
}
tree.root = n_leaves[0];
return tree;
}
function verify (proof, targetNode, root, mask = true){
var hash_count = 0;
var hash = targetNode;
//console.log("target : ", hash, "\n");
for (let i = 0; i < proof.length; i++) {
var node = proof[i];
//console.log("node : ", node, "\n");
var isLeft = isLeftNode(proof[i]);
if (mask){
node = isLeft ? maskLeft(node) : maskRight(node);
hash = isLeft ? maskRight(hash) : maskLeft(hash);
}
//console.log("canonical node : ", node );
//console.log("canonical hash : ", hash );
var buffers = [];
buffers.push(Buffer.from(hash, "hex"));
buffers[ isLeft ? 'unshift' : 'push' ](Buffer.from(node, "hex"));
var hash_hex = crypto.createHash('sha256').update(Buffer.concat(buffers)).digest("hex");
//console.log("hash : ", hash_hex, "\n");
hash_count++;
hash = hash_hex;
}
return Buffer.compare(Buffer.from(hash, "hex"), Buffer.from(root, "hex")) === 0;
}
function getProof (tree, leaf) {
var proof = [];
var index = tree.leaves.findIndex(item => item == leaf);
if (index <= -1) {
console.log("Couldn't find leaf in tree");
return []
}
for (var i = 0; i < tree.layers.length; i++) {
const layer = tree.layers[i];
const isLeft = index % 2;
const pairIndex = isLeft ? index - 1 :
index === layer.length - 1 && i < tree.layers.length - 1 ? index :
index + 1;
if (pairIndex < layer.length) proof.push( applyMask( layer[pairIndex], isLeft ) );
index = (index / 2) | 0
}
return proof;
}
function getProofPath (index, nodes_count) {
var proof = [];
var layers_depth = calculate_max_depth(nodes_count) -1;
var c_nodes_count = nodes_count;
for (var i = 0; i < layers_depth; i++) {
if (c_nodes_count%2) c_nodes_count+=1;
const isLeft = index % 2;
var pairIndex = isLeft ? index - 1 :
index === nodes_count - 1 && i < layers_depth - 1 ? index :
index + 1;
c_nodes_count/=2;
if (pairIndex < nodes_count) proof.push({layer : i, pairIndex, isLeft});
index = (index / 2) | 0
}
return proof;
}
//Digests
function getReceiptDigest(receipt){
const buffer = new SerialBuffer({ TextEncoder, TextDecoder });
//handle different formats of receipt for dfuse (camelCase) / nodeos / SHIP
//if receipt is in nodeos format, convert to dfuse format
if (receipt.act_digest && !receipt.digest){
let authSequence = [];
for (var auth of receipt.auth_sequence) {
if(auth[1]) authSequence.push({ accountName: auth[0], sequence: auth[1] })
else authSequence.push({ accountName: auth.account, sequence: auth.sequence }) //handle SHIP
}
if (authSequence.length>1) authSequence = authSequence.sort((a,b)=> a.accountName > b.accountName? 1 : a.accountName < b.accountName ? -1 : 0);
receipt = {
receiver: receipt.receiver,
digest: receipt.act_digest,
globalSequence: receipt.global_sequence,
recvSequence: receipt.recv_sequence,
authSequence,
codeSequence: receipt.code_sequence,
abiSequence: receipt.abi_sequence,
}
}
name.serialize(buffer, receipt.receiver);
checksum256.serialize(buffer, receipt.digest);
uint64.serialize(buffer, receipt.globalSequence);
uint64.serialize(buffer, receipt.recvSequence);
if (receipt.authSequence) {
varuint32.serialize(buffer, receipt.authSequence.length);
for (var auth of receipt.authSequence){
name.serialize(buffer, auth.accountName);
uint64.serialize(buffer, auth.sequence);
}
}
else varuint32.serialize(buffer, 0);
if (receipt.codeSequence) varuint32.serialize(buffer, receipt.codeSequence);
else varuint32.serialize(buffer, 0);
if (receipt.abiSequence) varuint32.serialize(buffer, receipt.abiSequence);
else varuint32.serialize(buffer, 0);
return crypto.createHash("sha256").update(buffer.asUint8Array()).digest("hex");
}
function getBaseActionDigest(a){
const buff = new SerialBuffer({ TextEncoder, TextDecoder });
uint64.serialize(buff, nameToUint64(a.account));
uint64.serialize(buff, nameToUint64(a.name));
varuint32.serialize(buff, a.authorization.length);
for (var i = 0 ; i < a.authorization.length;i++){
uint64.serialize(buff, nameToUint64(a.authorization[i].actor));
uint64.serialize(buff, nameToUint64(a.authorization[i].permission));
}
return crypto.createHash("sha256").update(buff.asUint8Array()).digest("hex");
}
function getDataDigest(act, returnValue){
const buff = new SerialBuffer({ TextEncoder, TextDecoder });
bytes.serialize(buff, act.data);
bytes.serialize(buff, returnValue);
return crypto.createHash("sha256").update(buff.asUint8Array()).digest("hex");
}
function compressProof(obj){
const newObj = JSON.parse(JSON.stringify(obj));
const hashes = [];
let totalHashes = 0;
for (var i = 0 ; i < newObj.proof.blockproof.blocktoprove.active_nodes.length; i++) {
var node = newObj.proof.blockproof.blocktoprove.active_nodes[i];
hashes.push(node);
var node_index = hashes.length-1;
newObj.proof.blockproof.blocktoprove.active_nodes[i] = node_index;
totalHashes++;
}
for (var i = 0 ; i < newObj.proof.blockproof.bftproof.length; i++) {
for (var j = 0 ; j < newObj.proof.blockproof.bftproof[i].bmproofpath.length; j++){
var node = newObj.proof.blockproof.bftproof[i].bmproofpath[j];
var node_index = hashes.indexOf(node);
if (node_index==-1){
hashes.push(node);
node_index = hashes.length-1;
}
newObj.proof.blockproof.bftproof[i].bmproofpath[j] = node_index;
totalHashes++;
}
}
newObj.proof.blockproof.hashes = hashes;
var newSize = (hashes.length*32) + (totalHashes*2);
var oldSize = totalHashes * 32;
var compressionRatio = 1 - (newSize / oldSize);
console.log("Compression ratio : ", compressionRatio);
return newObj;
}
const applyMask = (node, isLeft) => isLeft ? maskLeft(node).toString("hex") : maskRight(node).toString("hex");
const make_canonical_pair = (l,r) => ({ l: maskLeft(l), r: maskRight(r) });
const isLeftNode = n => (Buffer.from(n, "hex"))[0] < 128;
function maskLeft(n){
var nn = Buffer.from(n, "hex");
nn[0] &= 0x7f;
if (nn[0] < 0) nn[0] += (1 << 30) * 4;
return nn;
}
function maskRight(n){
var nn = Buffer.from(n, "hex");
nn[0] |= 0x80;
if (nn[0] < 0) nn[0] += (1 << 30) * 4;
return nn;
}
function next_power_of_2( value) {
value -= 1;
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
value |= value >> 32;
value += 1;
return value;
}
function clz_power_2( value) {
var count = 1;
for (var i = 0; i < 30; i++){
count*=2;
if (value == count) return i+1;
}
return 0;
}
function calculate_max_depth( node_count) {
if (node_count == 0) return 0;
var implied_count = next_power_of_2(node_count);
return clz_power_2(implied_count) + 1;
}
function hashPair(p){
var buffLeft = Buffer.from(p.l, "hex")
var buffRight = Buffer.from(p.r, "hex")
var buffFinal = Buffer.concat([buffLeft, buffRight]);
var finalHash = crypto.createHash("sha256").update(buffFinal).digest("hex");
return finalHash;
}
module.exports = {
getActionProof,
getBmProof,
verify,
compressProof,
getReceiptDigest,
getBaseActionDigest,
getDataDigest
}