-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-nonce.js
57 lines (50 loc) · 1.57 KB
/
find-nonce.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
/*jslint node: true */
"use strict";
const fs = require('fs');
const objectHash = require("ocore/object_hash.js");
const { parse } = require("ocore/formula/parse_ojson.js");
process.on('unhandledRejection', up => {
console.error('unhandledRejection event', up, up.stack);
throw up;
});
const file = process.argv[2];
const prefix = process.argv[3];
if (!prefix) {
console.error(`Usage: node find-nonce.js oscriptfile prefix`);
console.error(`E.g.: node find-nonce.js city.oscript CITY`);
process.exit();
}
const re = /\/\* nonce: \w+ \*\//;
function findNonce(definition) {
// const prefixRe = new RegExp('^' + prefix);
let nonce = 0;
console.error(`searching for nonce matching prefix ${prefix} ...`);
const start_ts = Date.now();
const printProgress = () => {
const elapsed = Date.now() - start_ts;
console.error(`trying ${nonce}, ${nonce / elapsed * 1000} nonces/sec`);
};
const interval = setInterval(printProgress, 10 * 1000);
let address;
do {
nonce++;
definition[1].getters = definition[1].getters.replace(re, `/* nonce: ${nonce} */`);
address = objectHash.getChash160(definition);
if (nonce % 100000 === 0)
printProgress();
}
while (!address.startsWith(prefix));
clearInterval(interval);
console.error(`found AA ${address} with nonce ${nonce}, search took ${(Date.now() - start_ts)/1000} seconds`);
}
function start() {
const strInitialDefinition = fs.readFileSync(file, 'utf8');
parse(strInitialDefinition, (err, definition) => {
if (err)
throw Error(err);
// console.log(definition[1].getters);
findNonce(definition);
process.exit();
});
}
start();