-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestArc.js
590 lines (501 loc) · 14.7 KB
/
testArc.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
const bsv = require("bsv");
const axios = require("axios");
require("bitcoin-ef/bsv");
require("dotenv").config();
const ArcClient = require("@bitcoin-a/arc-client").ArcClient;
const arcURLTestnet = process.env.TESTNET_URL;
const arcURLMainnet = process.env.MAINNET_URL;
const arcURLMainnet2 = process.env.MAINNET_URL_2;
const apikeyTestnet = process.env.APIKEY_TESTNET;
const apikeyMainnet = process.env.APIKEY_MAINNET;
const privkey = process.env.PRIV_KEY;
const wocURLTestnet = "https://api.whatsonchain.com/v1/bsv/test";
const wocURLMainnet = "https://api.whatsonchain.com/v1/bsv/main";
var wocURL = wocURLTestnet;
var apikey = apikeyTestnet;
var arcURL = arcURLTestnet;
var arcURL2 = arcURLTestnet;
var network = "testnet";
var print = false;
var extended = false;
class TestArc {
constructor() {
this.privateKey = bsv.PrivateKey.fromString(privkey);
this.address = bsv.Address.fromPrivateKey(
this.privateKey,
network
).toString();
this.p2pkhOut = bsv.Script.buildPublicKeyHashOut(this.address).toHex();
this.pkh = bsv.Address.fromString(this.address).toJSON().hash;
this.utxos = [];
}
async getAddressUnspentUtxosWoC(address) {
const res = await axios({
method: "get",
url: `${wocURL}/address/${address}/unspent`,
});
return res.data;
}
async getAddressBalanceWoC(address) {
const res = await axios({
method: "get",
url: `${wocURL}/address/${address}/balance`,
});
return res.data;
}
async printAddress() {
console.log(this.address);
}
async getAddressHistoryWoC() {
const address = this.address;
const res = await axios({
method: "get",
url: `${wocURL}/address/${address}/history`,
});
return res.data;
}
async getTransactionStatusWoC(txid) {
const res = await axios({
method: "get",
url: `${wocURL}/tx/hash/${txid}`,
});
return res.data;
}
async getTransactionsStatusWoC(transactionIDs) {
const json = JSON.stringify({ txids: transactionIDs });
const res = await axios.post(`${wocURL}/txs/status`, json);
return res.data;
}
async getAddressUtxosWoC() {
const utxos = await this.getAddressUnspentUtxosWoC(this.address);
const formatedUtxos = [];
for (const utxo of utxos) {
if (utxo.value <= 1) {
continue;
}
const formatedUtxo = {
txid: utxo.tx_hash,
vout: utxo.tx_pos,
satoshis: utxo.value,
script: this.p2pkhOut,
};
formatedUtxos.push(formatedUtxo);
}
return formatedUtxos;
}
async splitUtxo(targetnumber) {
const utxo = this.utxos.pop();
const tx = bsv.Transaction();
tx.from(utxo);
let splitSatoshis = Math.floor(utxo.satoshis / targetnumber);
for (let i = 1; i < targetnumber; i++) {
tx.to(this.address, splitSatoshis);
}
tx.to(this.address, splitSatoshis - 2.5 * targetnumber); // leave 10 sats for fees - 1 input 1 output
tx.sign(this.privateKey);
if (extended) {
return tx.toExtended("hex");
}
return tx;
}
async build2ConflictingTx(ext) {
const newRandomAddress = bsv.Address.fromPrivateKey(
bsv.PrivateKey.fromRandom(),
network
).toString();
// utxo to spend
const utxo = this.utxos.shift();
// tx 1 sends it to the same address
const tx1 = bsv.Transaction();
tx1.from(utxo);
tx1.to(this.address, utxo.satoshis - 1);
tx1.sign(this.privateKey);
// tx 2 sends it to a new address
const tx2 = bsv.Transaction();
tx2.from(utxo);
tx2.to(newRandomAddress, utxo.satoshis - 1);
tx2.sign(this.privateKey);
if (ext) {
return [tx1.toExtended("hex"), tx2.toExtended("hex")];
}
return [tx1, tx2];
}
async buildTx(address, utxo, fee) {
const tx = bsv.Transaction();
if (utxo === undefined) {
throw new Error("address ", address, " has no utxos to spend");
}
tx.from(utxo);
tx.to(address, utxo.satoshis - fee); // leave 1 sat for fees - 1 input 1 output
tx.sign(this.privateKey);
return tx;
}
async buildChainedTxCpp() {
let utxo = this.utxos.shift();
if (utxo === undefined) {
throw new Error("address ", address, " has no utxos to spend");
}
const numOfTx = 3;
const allTxs = [];
for (let i = 0; i < numOfTx; i++) {
let fee = 0;
if (i == numOfTx - 1) {
fee = 3;
}
const tx = await this.buildTx(this.address, utxo, fee);
utxo = {
txid: tx.id,
vout: 0,
satoshis: utxo.satoshis - fee,
script: this.p2pkhOut,
};
if (extended) {
allTxs.push(tx.toExtended("hex"));
continue;
}
allTxs.push(tx.toString("hex"));
}
return allTxs;
}
async buildChainedTx() {
let utxo = this.utxos.shift();
if (utxo === undefined) {
throw new Error("address ", address, " has no utxos to spend");
}
const numOfTx = 10;
const allTxs = [];
for (let i = 0; i < numOfTx; i++) {
const fee = 1;
const tx = await this.buildTx(this.address, utxo, fee);
utxo = {
txid: tx.id,
vout: 0,
satoshis: utxo.satoshis - fee,
script: this.p2pkhOut,
};
if (extended) {
allTxs.push(tx.toExtended("hex"));
continue;
}
allTxs.push(tx.toString("hex"));
}
return allTxs;
}
async buildMultiTx() {
let numOfTx = 1;
if (this.utxos.length < 10) {
numOfTx = this.utxos.length;
} else {
numOfTx = 10;
}
const allTxs = [];
for (let i = 0; i < numOfTx; i++) {
const utxo = this.utxos.shift();
const tx = await this.buildTx(this.address, utxo, 1);
if (extended) {
allTxs.push(tx.toExtended("hex"));
continue;
}
allTxs.push(tx.toString("hex"));
}
return allTxs;
}
}
const splitUtxo = async () => {
const test = new TestArc();
test.utxos = await test.getAddressUtxosWoC();
const arcClient = new ArcClient(arcURL);
arcClient.setAuthorization(apikey);
const splitTx = await test.splitUtxo(10);
if (print) {
console.log(splitTx.toString());
return;
}
try {
const txRes = await arcClient.postTransaction(splitTx);
console.log("Transaction Response: ", txRes);
} catch (err) {
console.log("error: ", err);
}
};
// run 1 single test
const submit1Tx = async () => {
const test = new TestArc();
test.utxos = await test.getAddressUtxosWoC();
const arcClient = new ArcClient(arcURL);
arcClient.setAuthorization(apikey);
const utxo = test.utxos.shift();
const tx = await test.buildTx(test.address, utxo, 1);
let rawTx;
if (extended) {
rawTx = tx.toExtended("hex");
} else {
rawTx = tx.toString("hex");
}
if (print) {
console.log(rawTx);
return;
}
try {
const txRes = await arcClient.postTransaction(rawTx);
console.log("Transaction Response: ", txRes);
} catch (err) {
console.log("error: ", err);
}
};
function delay(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
const submit2ConflictingTx = async () => {
const test = new TestArc();
test.utxos = await test.getAddressUtxosWoC();
const arcClient = new ArcClient(arcURL);
const arcClient2 = new ArcClient(arcURL2);
arcClient.setAuthorization(apikey);
const [tx1, tx2] = await test.build2ConflictingTx(extended);
if (print) {
console.log("tx1: ", tx1.toString(), "\n\n");
console.log("tx2: ", tx2.toString());
return;
}
try {
const txRes = await arcClient.postTransaction(tx1);
console.log("Transaction Response: ", txRes);
} catch (err) {
console.log("error: ", err);
}
await delay(1000);
try {
const txRes = await arcClient2.postTransaction(tx2);
console.log("Transaction Response: ", txRes);
} catch (err) {
console.log("error: ", err);
}
};
const submitChainedTx = async () => {
const test = new TestArc();
test.utxos = await test.getAddressUtxosWoC();
const txs = await test.buildChainedTx();
const arcClient = new ArcClient(arcURL);
arcClient.setAuthorization(apikey);
if (print) {
txsJson = txs.map((tx) => {
return { rawTx: tx };
});
var dictstring = JSON.stringify(txsJson);
console.log(dictstring);
return;
}
try {
const txRes = await arcClient.postTransactions(txs);
console.log("Transaction Response: ", txRes);
} catch (err) {
console.log("error: ", err);
}
};
const submitChainedTxCpp = async () => {
const test = new TestArc();
test.utxos = await test.getAddressUtxosWoC();
const txs = await test.buildChainedTxCpp();
const arcClient = new ArcClient(arcURL);
arcClient.setAuthorization(apikey);
if (print) {
txsJson = txs.map((tx) => {
return { rawTx: tx };
});
var dictstring = JSON.stringify(txsJson);
console.log(dictstring);
return;
}
try {
const txRes = await arcClient.postTransactions(txs);
console.log("Transaction Response: ", txRes);
} catch (err) {
console.log("error: ", err);
}
};
// run multi broadcast test
const submitMultipleTx = async () => {
const test = new TestArc();
test.utxos = await test.getAddressUtxosWoC();
const txs = await test.buildMultiTx();
const arcClient = new ArcClient(arcURL);
arcClient.setAuthorization(apikey);
if (print) {
txsJson = txs.map((tx) => {
return { rawTx: tx };
});
var dictstring = JSON.stringify(txsJson);
console.log(dictstring);
return;
}
try {
const txRes = await arcClient.postTransactions(txs);
console.log("Transaction Response: ", txRes);
} catch (err) {
console.log("error: ", err);
}
};
const getAddressUtxos = async () => {
const test = new TestArc();
const arcClient = new ArcClient(arcURL);
arcClient.setAuthorization(apikey);
test.utxos = await test.getAddressUtxosWoC();
const rows = [];
for (let i = 0; i < test.utxos.length; i++) {
const utxo = test.utxos[i];
rows.push({ txid: utxo.txid, vout: utxo.vout, satoshis: utxo.satoshis });
}
console.table(rows);
};
const getAddressBalanceWoC = async () => {
const test = new TestArc();
const arcClient = new ArcClient(arcURL);
arcClient.setAuthorization(apikey);
balance = await test.getAddressBalanceWoC(test.address);
console.table(balance);
};
const getTxStatus = async () => {
const test = new TestArc();
const arcClient = new ArcClient(arcURL);
arcClient.setAuthorization(apikey);
test.utxos = await test.getAddressUtxosWoC();
const distinctUtxos = [...new Set(test.utxos.map((item) => item.txid))];
const wocRes = await test.getTransactionsStatusWoC(distinctUtxos);
const rows = [];
for (let i = 0; i < distinctUtxos.length; i++) {
const txid = distinctUtxos[i];
const arcRes = await arcClient.getTransactionStatus(txid);
const wocResRow = wocRes.find(function (post, index) {
if (post.txid == txid) return true;
});
const blockHashWoc = wocResRow.blockhash;
rows.push({ txid: txid, arc: arcRes.blockHash, woc: blockHashWoc });
}
console.table(rows);
};
const printNewPrivateKey = async () => {
const newPrivateKey = bsv.PrivateKey.fromRandom();
console.log("new private key:\t", newPrivateKey.toString());
console.log(
"testnet address:\t",
bsv.Address.fromPrivateKey(newPrivateKey, "testnet").toString()
);
console.log(
"mainnet address:\t",
bsv.Address.fromPrivateKey(newPrivateKey, "mainnet").toString()
);
};
// print the address from the specified private key
const printAddress = async () => {
const test = new TestArc();
const arcClient = new ArcClient(arcURL);
test.printAddress();
};
// print status of the 20 last transactions
const getTxStatusHistory = async () => {
const test = new TestArc();
const arcClient = new ArcClient(arcURL);
arcClient.setAuthorization(apikey);
const wocRes = await test.getAddressHistoryWoC();
let wocResLast20 = wocRes
.sort((w1, w2) =>
w1.height < w2.height ? 1 : w1.height > w2.height ? -1 : 0
)
.slice(0, 20);
const distinctUtxos = [...new Set(wocResLast20.map((item) => item.tx_hash))];
const rows = [];
for (let i = 0; i < distinctUtxos.length; i++) {
const txid = distinctUtxos[i];
let blockHeightArc;
let txStatusArc;
try {
const arcRes = await arcClient.getTransactionStatus(txid);
blockHeightArc = arcRes.blockHeight;
txStatusArc = arcRes.txStatus;
} catch (error) {
blockHeightArc = 0;
txStatusArc = "not found";
}
const wocResRow = wocResLast20.find(function (post, index) {
if (post.tx_hash == txid) return true;
});
const blockHeightWoc = wocResRow.height;
const txidString = txid;
rows.push({
txid: txidString,
blockHeightArc: blockHeightArc,
txStatusArc: txStatusArc,
blockHeightWoc: blockHeightWoc,
});
}
console.log("block height");
console.table(rows);
};
for (let index = 0; index < process.argv.length; index++) {
const element = process.argv[index];
switch (element) {
case "--main":
wocURL = wocURLMainnet;
apikey = apikeyMainnet;
arcURL = arcURLMainnet;
arcURL2 = arcURLMainnet2;
network = "mainnet";
break;
case "--extended":
extended = true;
break;
case "--print":
print = true;
break;
default:
break;
}
}
const command = process.argv[process.argv.length - 1];
switch (command) {
case "submit1Tx":
submit1Tx();
break;
case "submitMultipleTx":
submitMultipleTx();
break;
case "submitChainedTx":
submitChainedTx();
break;
case "submitChainedTxCpp":
submitChainedTxCpp();
break;
case "submit2ConflictingTx":
submit2ConflictingTx();
break;
case "splitUtxo":
splitUtxo();
break;
case "printAddress":
printAddress();
break;
case "printNewPrivateKey":
printNewPrivateKey();
break;
case "getAddressUtxos":
getAddressUtxos();
break;
case "getTxStatus":
getTxStatus();
break;
case "getTxStatusHistory":
getTxStatusHistory();
break;
case "getAddressBalanceWoC":
getAddressBalanceWoC();
break;
case "toWif":
const test = new TestArc();
console.log(test.privateKey.toWIF());
break;
default:
console.log("error: {} not a valid command", command);
break;
}
module.exports = TestArc;