-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollect-plutus-scripts.mjs
79 lines (65 loc) · 2.24 KB
/
collect-plutus-scripts.mjs
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
import blake2b from 'blake2b';
import WebSocket from 'ws';
import { UNTIL, SINCE, OGMIOS_HOST, NATIVE_SCRIPTS } from './config.mjs';
const client = new WebSocket(OGMIOS_HOST);
// Strawman JSON-RPC 2.0 client
client.rpc = function rpc(method, params = {}) {
this.send(JSON.stringify({
jsonrpc: '2.0',
method,
params,
}));
};
client.on('open', () => {
client.rpc('findIntersection', { points: [SINCE] });
});
client.once('message', (data) => {
let store = new Set();
let n = null;
const tip = (UNTIL || JSON.parse(data).result.tip).slot;
client.on('message', (data) => {
const result = JSON.parse(data).result;
if (result.direction === 'forward') {
n += 1;
result.block.transactions.forEach(tx => {
// Gather scripts from outputs & witnesses
const scripts = (tx.outputs ?? []).reduce((acc, out) => {
if (out.script && out.script.language !== 'native') {
const tag = Number.parseInt(out.script.language.slice(-1), 10);
const msg = Buffer.concat([
Buffer.from([tag]),
Buffer.from(out.script.cbor, 'hex'),
]);
const digest = blake2b(28).update(msg).digest('hex');
acc[digest] = out.script;
} else if (out.script && out.script.language === 'native') {
const msg = Buffer.concat([
Buffer.from([0]),
Buffer.from(out.script.cbor, 'hex'),
]);
const digest = blake2b(28).update(msg).digest('hex');
acc[digest] = out.script;
}
return acc;
}, tx.scripts || {});
// Print out possible candidates, with their digest.
Object.keys(scripts).forEach(digest => {
if (store.has(digest)) { return; }
store.add(digest);
const { language, cbor } = scripts[digest];
if (language !== 'native') {
console.log(`${digest},${cbor}`);
} else {
console.error(`, "${digest}"`);
}
});
});
if (result.block.slot >= tip) {
process.exit(0);
}
client.rpc('nextBlock');
}
});
// Fill in the initial queue to leverage pipelining.
for (let i = 0; i < 100; i += 1) { client.rpc('nextBlock'); }
});