-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtest.js
644 lines (603 loc) · 22.6 KB
/
test.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
require('dotenv').config()
const assert = require('assert');
const bitcoin = require('bsv')
const datapay = require('../index');
// Private Key for Demo Purpose Only
const privKey = process.env.privKey
var utxoSize;
describe('datapay', function() {
beforeEach(function(done) {
const address = new bitcoin.PrivateKey(privKey).toAddress()
datapay.connect().getUnspentUtxos(address, function(err, utxos) {
if (err) {
console.log("Error: ", err)
} else {
utxoSize = utxos.length
done()
}
})
})
describe('build', function() {
describe('safe as default', function() {
it('safe as default', function(done) {
const options = {
data: [{op: 78}, "hello world"]
}
datapay.build(options, function(err, tx) {
let generated = tx.toObject()
let s = new bitcoin.Script(generated.outputs[0].script).toString()
assert(s.startsWith("OP_0 OP_RETURN OP_PUSHDATA4 1818585099"))
done()
});
})
it('set safe true', function(done) {
const options = {
safe: true,
data: [{op: 78}, "hello world"]
}
datapay.build(options, function(err, tx) {
let generated = tx.toObject()
let s = new bitcoin.Script(generated.outputs[0].script).toString()
assert(s.startsWith("OP_0 OP_RETURN OP_PUSHDATA4 1818585099"))
done()
});
})
it('set safe false', function(done) {
const options = {
safe: false,
data: [{op: 78}, "hello world"]
}
datapay.build(options, function(err, tx) {
let generated = tx.toObject()
let s = new bitcoin.Script(generated.outputs[0].script).toString()
assert(s.startsWith("OP_RETURN OP_PUSHDATA4 1818585099"))
done()
});
})
})
describe('data only', function() {
it('opcode', function(done) {
const options = {
data: [{op: 78}, "hello world"]
}
datapay.build(options, function(err, tx) {
let generated = tx.toObject()
let s = new bitcoin.Script(generated.outputs[0].script).toString()
assert(s.startsWith("OP_RETURN OP_PUSHDATA4 1818585099"))
done()
});
})
it('opcode 2', function(done) {
const options = {
data: ["0x6d02", "hello world", {op: 78}, "blah blah blah * 10^100"]
}
datapay.build(options, function(err, tx) {
let generated = tx.toObject()
let s = new bitcoin.Script(generated.outputs[0].script).toString()
assert(s.startsWith("OP_RETURN 2 0x6d02 11 0x68656c6c6f20776f726c64 OP_PUSHDATA4 1634492951"))
done()
});
})
it('push data array', function(done) {
const options = {
data: ["0x6d02", "hello world"]
}
datapay.build(options, function(err, tx) {
let generated = tx.toObject();
// no input (since no one has signed yet)
assert.equal(generated.inputs.length, 0)
// output has one item (only OP_RETURN)
assert.equal(generated.outputs.length, 1)
// the only existing output is a script
assert(generated.outputs[0].script);
// uses the default fee of 400
assert(generated.fee <= 400)
done()
});
})
it('hex string that represents script', function(done) {
const options = {
data: "6a04366430320b68656c6c6f20776f726c64"
}
datapay.build(options, function(err, tx) {
let generated = tx.toObject();
// no input (since no one has signed yet)
assert.equal(generated.inputs.length, 0)
// output has one item (only OP_RETURN)
assert.equal(generated.outputs.length, 1)
// the only existing output is a script
assert(generated.outputs[0].script);
// uses the default fee of 400
assert(generated.fee <= 400)
done()
});
})
it('Buffer', function(done) {
const options = {
data: [Buffer.from("abc"), "hello world"]
}
datapay.build(options, function(err, tx) {
let generated = tx.toObject();
// no input (since no one has signed yet)
assert.equal(generated.inputs.length, 0)
// output has one item (only OP_RETURN)
assert.equal(generated.outputs.length, 1)
// the only existing output is a script
assert(generated.outputs[0].script);
// uses the default fee of 400
assert(generated.fee <= 400)
done()
});
})
})
describe('pay only', function() {
it('to', function(done) {
const address = new bitcoin.PrivateKey(privKey).toAddress()
const options = {
pay: {
key: privKey,
to: [{ address: address, value: 100 }]
}
}
datapay.build(options, function(err, tx) {
// If only 'key' is included, it will use the default values for
// rest of the pay attributes
// and make a transaction that sends money to oneself
// (since no receiver is specified)
let generated = tx.toObject();
done()
})
})
it('pay.key only', function(done) {
const options = {
pay: {
key: privKey
}
}
datapay.build(options, function(err, tx) {
// If only 'key' is included, it will use the default values for
// rest of the pay attributes
// and make a transaction that sends money to oneself
// (since no receiver is specified)
let generated = tx.toObject();
// input length utxoSize => from the user specifiec by the private key
assert.equal(generated.inputs.length, utxoSize)
// contains a 'changeScript'
assert(generated.changeScript)
// output length 1 => the output points to the sender by default
assert.equal(generated.outputs.length, 1)
// script is a pubkeyhashout
let s = new bitcoin.Script(generated.outputs[0].script)
assert(s.isPublicKeyHashOut())
// script sends the money to the same address as the sender
// specified by the private key
const address = new bitcoin.PrivateKey(privKey).toAddress()
assert.equal(address.toString(), s.toAddress().toString())
done()
});
})
it('pay.fee only', function(done) {
const options = {
pay: {
fee: 100
}
}
datapay.build(options, function(err, tx) {
// if no key is included,
// empty input (since no sender)
// empty output (since nothing else is specified)
let generated = tx.toObject();
assert.equal(generated.inputs.length, 0)
assert.equal(generated.outputs.length, 0)
assert.equal(generated.fee, 100)
done()
})
})
it('pay.key and pay.fee', function(done) {
const options = {
pay: {
key: privKey,
fee: 100
}
}
datapay.build(options, function(err, tx) {
let generated = tx.toObject();
assert.equal(generated.fee, 100)
done()
})
})
/**
pay.rpc TBD
**/
})
describe('data and pay', function() {
it('both data and pay', function(done) {
const options = {
data: ["0x6d02", "hello world"],
pay: {
key: privKey
}
}
datapay.build(options, function(err, tx) {
let generated = tx.toObject();
// input length 1 => from the user specifiec by the private key
assert.equal(generated.inputs.length, utxoSize)
// contains a 'changeScript'
assert(generated.changeScript)
// must have two outputs
assert.equal(generated.outputs.length, 2)
let s1 = new bitcoin.Script(generated.outputs[0].script)
// the first output is OP_RETURN
assert(s1.chunks[0].opcodenum, bitcoin.Opcode.OP_RETURN)
// the second script is a pubkeyhashout (change address)
let s2 = new bitcoin.Script(generated.outputs[1].script)
assert(s2.isPublicKeyHashOut())
// script sends the money to the same address as the sender
// specified by the private key
const address = new bitcoin.PrivateKey(privKey).toAddress()
assert.equal(address.toString(), s2.toAddress().toString())
done()
})
})
/*
it('pay.filter', function(done) {
const options = {
data: ["0x6d02", "hello world"],
pay: {
key: privKey,
filter: {
v: 3,
q: {
find: {
}
}
}
}
}
datapay.build(options, function(err, tx) {
let generated = tx.toObject();
// input length 1 => from the user specifiec by the private key
assert.equal(generated.inputs.length, 5)
// contains a 'changeScript'
assert(generated.changeScript)
// must have two outputs
assert.equal(generated.outputs.length, 2)
let s1 = new bitcoin.Script(generated.outputs[0].script)
// the first output is OP_RETURN
assert(s1.chunks[0].opcodenum, bitcoin.Opcode.OP_RETURN)
// the second script is a pubkeyhashout (change address)
let s2 = new bitcoin.Script(generated.outputs[1].script)
assert(s2.isPublicKeyHashOut())
// script sends the money to the same address as the sender
// specified by the private key
const address = new bitcoin.PrivateKey(privKey).toAddress()
assert.equal(address.toString(), s2.toAddress().toString())
done()
})
})
*/
})
describe('attach coins to data', function() {
it('paying tip to 1 user', function(done) {
// send to myself
const receiver = new bitcoin.PrivateKey(privKey).toAddress()
const options = {
data: ["0x6d02", "hello world"],
pay: {
key: privKey,
to: [{
address: receiver,
value: 1000
}]
}
}
datapay.build(options, function(err, tx) {
// output has 3 items
assert.equal(tx.outputs.length, 3)
// 1. OP_RETURN
let s1 = new bitcoin.Script(tx.outputs[0].script)
assert(s1.chunks[0].opcodenum, bitcoin.Opcode.OP_RETURN)
// 2. Manual transaction output
// the second script is a pubkeyhashout (change address)
let s2 = new bitcoin.Script(tx.outputs[1].script)
assert(s2.isPublicKeyHashOut())
// the value sent is 1000
assert.equal(tx.outputs[1].satoshis, 1000)
// the receiver address is the address specified in pay.to
assert.equal(s2.toAddress().toString(), bitcoin.Address(receiver).toString())
// 3. Change address transaction output
let s3 = new bitcoin.Script(tx.outputs[2].script)
assert(s3.isPublicKeyHashOut())
done()
})
})
it('paying tip to 2 users', function(done) {
// send to myself
const receiver = new bitcoin.PrivateKey(privKey).toAddress()
const options = {
data: ["0x6d02", "hello world"],
pay: {
key: privKey,
to: [{
address: receiver,
value: 1000
}, {
address: receiver,
value: 2000
}]
}
}
datapay.build(options, function(err, tx) {
// output has 4 items
assert.equal(tx.outputs.length, 4)
// 1. OP_RETURN
let s1 = new bitcoin.Script(tx.outputs[0].script)
assert(s1.chunks[0].opcodenum, bitcoin.Opcode.OP_RETURN)
// 2. Manual transaction output
// the second script is a pubkeyhashout (change address)
let s2 = new bitcoin.Script(tx.outputs[1].script)
assert(s2.isPublicKeyHashOut())
// the value sent is 1000
assert.equal(tx.outputs[1].satoshis, 1000)
// the receiver address is the address specified in pay.to
assert.equal(s2.toAddress().toString(), bitcoin.Address(receiver).toString())
// 3. Manual transaction output
// the third script is a pubkeyhashout (change address)
let s3 = new bitcoin.Script(tx.outputs[2].script)
assert(s3.isPublicKeyHashOut())
// the value sent is 1000
assert.equal(tx.outputs[2].satoshis, 2000)
// the receiver address is the address specified in pay.to
assert.equal(s3.toAddress().toString(), bitcoin.Address(receiver).toString())
// 3. Change address transaction output
let s4 = new bitcoin.Script(tx.outputs[3].script)
assert(s4.isPublicKeyHashOut())
done()
})
})
})
describe('tx', function() {
describe('importing unsigned tx', function() {
it('tx only', function(done) {
// 1. build
const options = {
data: ["0x6d02", "hello world"]
}
datapay.build(options, function(err, original_tx) {
// 2. export
let exportedTx = original_tx.toString()
// exported transaction is string
assert.equal(typeof exportedTx, "string")
// 3. re-import
datapay.build({
tx: exportedTx
}, function(err, imported_tx) {
// the imported transaction should equal the original transaction
assert.equal(imported_tx.toString(), original_tx.toString())
done()
})
})
})
it('tx + data', function(done) {
// if there's a 'tx' attribute, it should ignore 'data' to avoid confusion
const options1 = {
data: ["0x6d02", "hello world"]
}
// 1. build initial transaction
datapay.build(options1, function(err, tx1) {
let exported_tx1 = tx1.toString();
// 2. build a new transaction using the exported transaction + new data
let options2 = {
tx: exported_tx1,
data: ["0x6d02", "bye world"]
}
datapay.build(options2, function(err, tx2) {
assert.equal(tx1.toString(), tx2.toString())
done()
})
})
})
it('tx + pay', function(done) {
// tx1 is an unsigned transaction
// and we create a signed version by adding the 'pay' attribute
const options1 = {
data: ["0x6d02", "hello world"]
}
// 1. build initial transaction
datapay.build(options1, function(err, tx1) {
let exported_tx1 = tx1.toString();
// 2. build a new transaction using the exported transaction + new data
let options2 = {
tx: exported_tx1,
pay: {
key: privKey
}
}
datapay.build(options2, function(err, tx2) {
// tx1's input should be empty
assert.equal(tx1.inputs.length, 0)
// tx2's input should now have as many as the utxoSize
assert.equal(tx2.inputs.length, utxoSize)
// tx1's output should have one item
assert.equal(tx1.outputs.length, 1)
// and it should be an OP_RETURN
let script1 = new bitcoin.Script(tx1.outputs[0].script)
assert(script1.chunks[0].opcodenum, bitcoin.Opcode.OP_RETURN)
// tx2's output should have two items
assert.equal(tx2.outputs.length, 2)
let script2 = [
new bitcoin.Script(tx2.outputs[0].script),
new bitcoin.Script(tx2.outputs[1].script)
]
// the first should be OP_RETURN
assert(script2[0].chunks[0].opcodenum, bitcoin.Opcode.OP_RETURN)
// the second script is a pubkeyhashout (change address)
assert(script2[1].isPublicKeyHashOut())
done()
})
})
})
it('tx + pay + data', function(done) {
// tx1 is an unsigned transaction
// and we create a signed version by adding the 'pay' attribute
// but this time we also try to sneak in 'data'
// the 'data' should be ignored
const options1 = {
data: ["0x6d02", "hello world"]
}
// 1. build initial transaction
datapay.build(options1, function(err, tx1) {
let exported_tx1 = tx1.toString();
// 2. build a new transaction using the exported transaction + new data
let options2 = {
tx: exported_tx1,
data: ["0x6d02", "bye world"], // trying to sneak in 'data'
pay: {
key: privKey
}
}
datapay.build(options2, function(err, tx2) {
// tx2's input should now have as many as the utxoSize
assert.equal(tx2.inputs.length, utxoSize)
// tx2's output should have two items
assert.equal(tx2.outputs.length, 2)
let script2 = [
new bitcoin.Script(tx2.outputs[0].script),
new bitcoin.Script(tx2.outputs[1].script)
]
// the first should be OP_RETURN
assert(script2[0].chunks[0].opcodenum, bitcoin.Opcode.OP_RETURN)
// the second script is a pubkeyhashout (change address)
assert(script2[1].isPublicKeyHashOut())
// the script for the original OP_RETURN
// should match the new OP_RETURN script
// because the 'data' attribute was ignored
let script1 = new bitcoin.Script(tx1.outputs[0].script)
assert.equal(script1.toString(), script2[0].toString())
done()
})
})
})
})
describe('importing signed tx', function() {
it('tx only', function(done) {
const options1 = {
data: ["0x6d02", "hello world"],
pay: { key: privKey }
}
// 1. build initial transaction
datapay.build(options1, function(err, tx1) {
let exported_tx1 = tx1.toString();
// 2. import transaction
datapay.build({ tx: exported_tx1 }, function(err, tx2) {
// the imported transaction should have as many as the utxoSize
assert.equal(tx2.inputs.length, utxoSize)
// the input should have 'script' property
assert(tx2.inputs[0].script)
// the script should be public key hash in
let script = new bitcoin.Script(tx2.inputs[0].script)
assert(script.isPublicKeyHashIn())
// the imported transaction's input script address should match
// the address corresponding to the originally imported private key
const address = new bitcoin.PrivateKey(privKey).toAddress()
assert.equal(address.toString(), script.toAddress().toString())
done()
})
})
})
it('tx + data', function(done) {
// the transaction has already been signed
// the data should be ignored
// Better yet, this shouldn't be used
const options1 = {
data: ["0x6d02", "hello world"],
pay: { key: privKey }
}
// 1. build initial transaction
datapay.build(options1, function(err, tx1) {
let exported_tx1 = tx1.toString();
// 2. import transaction
datapay.build({
tx: exported_tx1,
data: ["0x6d02", "bye world"]
}, function(err, tx2) {
assert(err.toString(), "the transaction is already signed and cannot be modified")
assert.equal(tx2, undefined)
done()
})
})
})
it('tx+ pay', function(done) {
// the transaction has already been signed
// the pay attribute should be ignored
// and throw and error
const options1 = {
data: ["0x6d02", "hello world"],
pay: { key: privKey }
}
// 1. build initial transaction
datapay.build(options1, function(err, tx1) {
let exported_tx1 = tx1.toString();
// 2. import transaction
// But this time, we're updating the key attribute.
// This should re-sign the transaction
datapay.build({
tx: exported_tx1,
pay: {
key: privKey
}
}, function(err, tx2) {
assert(err.toString(), "the transaction is already signed and cannot be modified")
assert.equal(tx2, undefined)
done()
})
})
})
it('tx + pay + data', function(done) {
const options1 = {
data: ["0x6d02", "hello world"],
pay: { key: privKey }
}
// 1. build initial transaction
datapay.build(options1, function(err, tx1) {
let exported_tx1 = tx1.toString();
// 2. import transaction
// But this time, we're updating the key attribute.
// This should re-sign the transaction
datapay.build({
tx: exported_tx1,
data: ["0x6d02", "bye world"],
pay: {
key: privKey
}
}, function(err, tx2) {
assert(err.toString(), "the transaction is already signed and cannot be modified")
assert.equal(tx2, undefined)
done()
})
})
})
})
})
})
describe('advanced', function() {
describe('bitcoin', function() {
it('exposes bitcoin', function() {
assert(datapay.bsv.Networks)
assert(datapay.bsv.Opcode)
})
})
describe('connect', function() {
it('default', function() {
var insight = datapay.connect();
assert.equal(insight.constructor.name, "Insight")
assert.equal(insight.url, 'https://api.mattercloud.net')
})
it('connect with url', function() {
var insight = datapay.connect('https://api.mattercloud.net');
assert.equal(insight.constructor.name, "Insight")
assert.equal(insight.url, 'https://api.mattercloud.net')
})
})
})
})