-
Notifications
You must be signed in to change notification settings - Fork 4
/
fire.js
79 lines (59 loc) · 2.71 KB
/
fire.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
import config from "./config"
import { sendtx } from "./send"
import { utxos as getutxos } from "./utxos"
import bsv from "bsv";
const bitindex = require('bitindex-sdk').instance();
async function fireForUTXO(privateKey, utxo, changeAddress, satoshis, target) {
if (!privateKey) { throw new Error(`shooter requires a privateKey`) }
if (!utxo) { throw new Error(`shooter requires a utxo`) }
if (!target) { throw new Error(`shooter requires a target`) }
if (!changeAddress) { throw new Error(`shooter requires a change address`) }
if (!Number.isInteger(satoshis)) { throw new Error(`shooter requires an amount`) }
const tx = bsv.Transaction()
.from([utxo])
.to(target, satoshis)
.change(changeAddress);
tx.sign(privateKey);
if (tx.verify() !== true) {
console.log("error while verifying tx for utxo", utxo);
throw new Error(`error while verifying tx`);
}
const txhash = tx.serialize();
//const result = await bitindex.tx.send(txhash);
const result = await sendtx(txhash);
if (result.error) {
console.log("error while sending tx for utxo", utxo);
throw new Error(`error while sending tx ${result.error}`);
}
return result.result;
}
export async function fire(wif, num, satoshis, target, backend) {
console.log("starting transaction shooter");
if (!wif) { throw new Error(`shooter requires a wif`) }
if (!target) { throw new Error(`shooter requires a target`) }
if (!Number.isInteger(num)) { throw new Error(`shooter requires a num`) }
if (!Number.isInteger(satoshis)) { throw new Error(`shooter requires an amount`) }
const privateKey = bsv.PrivateKey(wif);
const address = bsv.Address.fromPrivateKey(privateKey).toString();
console.log(`loading private key that owns address ${address}`);
const utxos = await getutxos(address);
if (utxos.length < num) { throw new Error(`don't have enough utxos, need to split them`) }
const sats = utxos.map(utxo => { return utxo.satoshis }).reduce((a, b) => a + b, 0);
console.log(`found ${utxos.length} utxos worth ${sats} satoshis`);
const expectedSpend = (num * satoshis);
if (expectedSpend > sats) { throw new Error(`don't have enough money to send ${expectedSpend} satoshis to ${target}`) }
let curr = 0;
console.log("aim");
for (const utxo of utxos) {
const txid = await fireForUTXO(privateKey, utxo, address, satoshis, target);
if (!txid) { throw new Error(`error firing tx to target`) }
backend.add(txid);
curr += 1;
console.log(`🔫 FIRE ${txid}`);
if (curr >= num) {
console.log(`FIRED ${curr}/${num} targets`);
break;
}
}
backend.wait();
}