-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (58 loc) · 2.25 KB
/
index.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
const neon = require('@cityofzion/neon-js');
const debug = true;
const random = function () {
const date = new Date();
const random = neon.u.sha256(neon.u.str2hexstring(date.toISOString() + Math.random()));
return random;
}
const mineTransactionHash = async function (account, transaction, mask, length, count) {
// Create a random remark to salt the transaction
const remark = random();
// Create a transaction
const tx = new neon.tx.Transaction(transaction).addAttribute(32, '6913a4d3f4e0ffb3aad8e9f919e73b6098f4aa22').addRemark(remark);
const serialized = neon.tx.serializeTransaction(tx);
const hash = neon.tx.getTransactionHash(tx);
const bits = parseInt(neon.u.reverseHex(hash.substr(0, length)), 16).toString(2);
// Show the progress of searching through random transaction hashes
if (debug) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`${hash} (${bits}) (${count})`);
}
// The last bits of the reversed hash should be equal to the zeroes in the difficulty mask
return (bits.endsWith(mask) || bits == 0) ? tx.sign(account.privateKey) : false
}
const createTransaction = async function (difficulty) {
const mask = (Math.pow(2, difficulty)).toString(2).substr(1);
const hex = (Math.pow(2, difficulty) - 1).toString(16);
const length = (hex.length % 2) ? `0${hex}`.length : `${hex}`.length
console.log(`Mining hash for transaction with difficulty [${difficulty}] mask [${mask}] hex [${hex}] length [${length}]`);
const account = new neon.wallet.Account('93f734cc7cd911b7eca6439943cb7a03f34b6c41801ee07355d531ea6c39164a');
const invoke = neon.sc.createScript({
scriptHash: '6913a4d3f4e0ffb3aad8e9f919e73b6098f4aa22',
operation: 'neo-tx-hash-mining',
args: [
'6913a4d3f4e0ffb3aad8e9f919e73b6098f4aa22'
]
});
const transaction = {
type: 209,
version: 1,
script: invoke,
gas: 0
};
let tx = false, count = 0;
while (!tx) {
tx = await mineTransactionHash(account, transaction, mask, length, count);
count++;
};
const hash = neon.tx.getTransactionHash(tx);
console.log();
}
const init = async function () {
console.time('neo-tx-hash-mining');
// Create a transaction and mine for a hash with difficulty level 16
await createTransaction(16);
console.timeEnd('neo-tx-hash-mining');
}
init();