-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathwallet-http-test.js
2791 lines (2173 loc) · 76.7 KB
/
wallet-http-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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const Network = require('../lib/protocol/network');
const MTX = require('../lib/primitives/mtx');
const {isSignatureEncoding, isKeyEncoding} = require('../lib/script/common');
const {Resource} = require('../lib/dns/resource');
const Address = require('../lib/primitives/address');
const Output = require('../lib/primitives/output');
const HD = require('../lib/hd/hd');
const Mnemonic = require('../lib/hd/mnemonic');
const rules = require('../lib/covenants/rules');
const {types} = rules;
const secp256k1 = require('bcrypto/lib/secp256k1');
const network = Network.get('regtest');
const assert = require('bsert');
const {BufferSet} = require('buffer-map');
const common = require('./util/common');
const Outpoint = require('../lib/primitives/outpoint');
const consensus = require('../lib/protocol/consensus');
const NodeContext = require('./util/node-context');
const {forEvent, sleep} = require('./util/common');
const {generateInitialBlocks} = require('./util/pagination');
const {
treeInterval,
biddingPeriod,
revealPeriod,
transferLockup
} = network.names;
describe('Wallet HTTP', function() {
this.timeout(20000);
/** @type {NodeContext} */
let nodeCtx;
let wclient, nclient;
// primary wallet client.
let wallet, cbAddress;
const beforeAll = async () => {
nodeCtx = new NodeContext({
apiKey: 'foo',
network: 'regtest',
walletAuth: true,
wallet: true
});
await nodeCtx.open();
wclient = nodeCtx.wclient;
nclient = nodeCtx.nclient;
wallet = nodeCtx.wclient.wallet('primary');
cbAddress = (await wallet.createAddress('default')).address;
};
const afterAll = async () => {
await nodeCtx.close();
};
describe('Create wallet', function() {
before(beforeAll);
after(afterAll);
it('should create wallet', async () => {
const info = await wclient.createWallet('test');
assert.strictEqual(info.id, 'test');
const wallet = wclient.wallet('test', info.token);
await wallet.open();
});
it('should create wallet with spanish mnemonic', async () => {
await wclient.createWallet(
'cartera1',
{language: 'spanish'}
);
const master = await wclient.getMaster('cartera1');
const phrase = master.mnemonic.phrase;
for (const word of phrase.split(' ')) {
const language = Mnemonic.getLanguage(word);
assert.strictEqual(language, 'spanish');
// Comprobar la cordura:
assert.notStrictEqual(language, 'english');
}
// Verificar
await wclient.createWallet(
'cartera2',
{mnemonic: phrase}
);
assert.deepStrictEqual(
await wclient.getAccount('cartera1', 'default'),
await wclient.getAccount('cartera2', 'default')
);
});
});
describe('Lookahead', function() {
before(beforeAll);
after(afterAll);
it('should create wallet with default account 1000 lookahead', async () => {
const wname = 'lookahead';
await wclient.createWallet(wname, {
lookahead: 1000
});
const defAccount = await wclient.getAccount(wname, 'default');
assert.strictEqual(defAccount.lookahead, 1000);
const newAccount = await wclient.createAccount(wname, 'newaccount', {
lookahead: 1001
});
assert.strictEqual(newAccount.lookahead, 1001);
const getNewAccount = await wclient.getAccount(wname, 'newaccount', {
lookahead: 1001
});
assert.strictEqual(getNewAccount.lookahead, 1001);
});
it('should modify account lookahead to 1000', async () => {
const wname = 'lookahead2';
await wclient.createWallet(wname);
const defAccount = await wclient.getAccount(wname, 'default');
assert.strictEqual(defAccount.lookahead, 200);
const modified = await wclient.modifyAccount(wname, 'default', {
lookahead: 1000
});
assert.strictEqual(modified.lookahead, 1000);
});
});
describe('Wallet info', function() {
let wallet;
before(async () => {
await beforeAll();
await wclient.createWallet('test');
wallet = wclient.wallet('test');
});
after(afterAll);
it('should get wallet info', async () => {
const info = await wallet.getInfo();
assert.strictEqual(info.id, 'test');
const acct = await wallet.getAccount('default');
const str = acct.receiveAddress;
assert(typeof str === 'string');
});
});
describe('Key/Address', function() {
before(beforeAll);
after(afterAll);
it('should get key by address from watch-only', async () => {
const phrase = 'abandon abandon abandon abandon abandon abandon '
+ 'abandon abandon abandon abandon abandon about';
const master = HD.HDPrivateKey.fromPhrase(phrase);
const xprv = master.deriveAccount(44, 5355, 5);
const xpub = xprv.toPublic();
const pubkey = xpub.derive(0).derive(0);
const addr = Address.fromPubkey(pubkey.publicKey);
const wallet = wclient.wallet('watchonly');
await wclient.createWallet('watchonly', {
watchOnly: true,
accountKey: xpub.xpubkey('regtest')
});
const key = await wallet.getKey(addr.toString('regtest'));
assert.equal(xpub.childIndex ^ HD.common.HARDENED, key.account);
assert.equal(0, key.branch);
assert.equal(0, key.index);
});
});
describe('Mine/Fund', function() {
before(beforeAll);
after(afterAll);
it('should mine to the primary/default wallet', async () => {
const height = 20;
await nodeCtx.mineBlocks(height, cbAddress);
const info = await nclient.getInfo();
assert.equal(info.chain.height, height);
const accountInfo = await wallet.getAccount('default');
// each coinbase output was indexed
assert.equal(accountInfo.balance.coin, height);
const coins = await wallet.getCoins();
// the wallet has no previous history besides
// what it has mined
assert.ok(coins.every(coin => coin.coinbase === true));
});
});
describe('Events', function() {
before(beforeAll);
after(afterAll);
it('balance address and tx events', async () => {
await wclient.createWallet('test');
const testWallet = wclient.wallet('test');
await testWallet.open();
const {address} = await testWallet.createAddress('default');
const mtx = new MTX();
mtx.addOutpoint(new Outpoint(consensus.ZERO_HASH, 0));
mtx.addOutput(address, 50460);
mtx.addOutput(address, 50460);
mtx.addOutput(address, 50460);
mtx.addOutput(address, 50460);
const tx = mtx.toTX();
let balance = null;
testWallet.once('balance', (b) => {
balance = b;
});
let receive = null;
testWallet.once('address', (r) => {
receive = r[0];
});
let details = null;
testWallet.once('tx', (d) => {
details = d;
});
await nodeCtx.wdb.addTX(tx);
await new Promise(r => setTimeout(r, 300));
assert(receive);
assert.strictEqual(receive.name, 'default');
assert.strictEqual(receive.branch, 0);
assert(balance);
assert.strictEqual(balance.confirmed, 0);
assert.strictEqual(balance.unconfirmed, 201840);
assert(details);
assert.strictEqual(details.hash, tx.txid());
});
});
describe('Create/Send transaction', function() {
let wallet2;
before(async () => {
await beforeAll();
await nodeCtx.mineBlocks(20, cbAddress);
await wclient.createWallet('secondary');
wallet2 = wclient.wallet('secondary');
});
after(afterAll);
it('should create a transaction', async () => {
const tx = await wallet.createTX({
outputs: [{ address: cbAddress, value: 1e4 }]
});
assert.ok(tx);
assert.equal(tx.outputs.length, 1 + 1); // send + change
assert.equal(tx.locktime, 0);
});
it('should create self-send transaction with HD paths', async () => {
const tx = await wallet.createTX({
paths: true,
outputs: [{ address: cbAddress, value: 1e4 }]
});
assert.ok(tx);
assert.ok(tx.inputs);
for (let i = 0; i < tx.inputs.length; i++) {
const path = tx.inputs[i].path;
assert.ok(typeof path.name === 'string');
assert.ok(typeof path.account === 'number');
assert.ok(typeof path.change === 'boolean');
assert.ok(typeof path.derivation === 'string');
}
// cbAddress is a self-send
// so all output paths including change should be known
for (let i = 0; i < tx.outputs.length; i++) {
const path = tx.outputs[i].path;
assert.ok(typeof path.name === 'string');
assert.ok(typeof path.account === 'number');
assert.ok(typeof path.change === 'boolean');
assert.ok(typeof path.derivation === 'string');
}
});
it('should create a transaction with HD paths', async () => {
const tx = await wallet.createTX({
paths: true,
outputs: [{
address: 'rs1qlf5se77y0xlg5940slyf00djvveskcsvj9sdrd',
value: 1e4
}]
});
assert.ok(tx);
assert.ok(tx.inputs);
for (let i = 0; i < tx.inputs.length; i++) {
const path = tx.inputs[i].path;
assert.ok(typeof path.name === 'string');
assert.ok(typeof path.account === 'number');
assert.ok(typeof path.change === 'boolean');
assert.ok(typeof path.derivation === 'string');
}
{
const path = tx.outputs[1].path; // change
assert.ok(typeof path.name === 'string');
assert.ok(typeof path.account === 'number');
assert.ok(typeof path.change === 'boolean');
assert.ok(typeof path.derivation === 'string');
}
{
const path = tx.outputs[0].path; // receiver
assert(!path);
}
});
it('should create a transaction with a locktime', async () => {
const locktime = 8e6;
const tx = await wallet.createTX({
locktime: locktime,
outputs: [{ address: cbAddress, value: 1e4 }]
});
assert.equal(tx.locktime, locktime);
});
it('should create a transaction that is not bip 69 sorted', async () => {
// create a list of outputs that descend in value
// bip 69 sorts in ascending order based on the value
const outputs = [];
for (let i = 0; i < 5; i++) {
const addr = await wallet.createAddress('default');
outputs.push({ address: addr.address, value: (5 - i) * 1e5 });
}
const tx = await wallet.createTX({
outputs: outputs,
sort: false
});
// assert outputs in the same order that they were sent from the client
for (const [i, output] of outputs.entries()) {
assert.equal(tx.outputs[i].value, output.value);
assert.equal(tx.outputs[i].address.toString(network), output.address);
}
const mtx = MTX.fromJSON(tx);
mtx.sortMembers();
// the order changes after sorting
assert.ok(tx.outputs[0].value !== mtx.outputs[0].value);
});
it('should create a transaction that is bip 69 sorted', async () => {
const outputs = [];
for (let i = 0; i < 5; i++) {
const addr = await wallet.createAddress('default');
outputs.push({ address: addr.address, value: (5 - i) * 1e5 });
}
const tx = await wallet.createTX({
outputs: outputs
});
const mtx = MTX.fromJSON(tx);
mtx.sortMembers();
// assert the ordering of the outputs is the
// same after sorting the response client side
for (const [i, output] of tx.outputs.entries()) {
assert.equal(output.value, mtx.outputs[i].value);
assert.equal(output.address, mtx.outputs[i].address.toString(network));
}
});
it('should mine to the secondary/default wallet', async () => {
const height = 5;
const {address} = await wallet2.createAddress('default');
await nodeCtx.mineBlocks(height, address);
const accountInfo = await wallet2.getAccount('default');
assert.equal(accountInfo.balance.coin, height);
});
});
describe('Get balance', function() {
before(async () => {
await beforeAll();
await nodeCtx.mineBlocks(20, cbAddress);
});
after(afterAll);
it('should get balance', async () => {
const balance = await wallet.getBalance();
assert.equal(balance.tx, 20);
assert.equal(balance.coin, 20);
});
});
describe('Get TX', function() {
let hash;
before(async () => {
await beforeAll();
await nodeCtx.mineBlocks(10, cbAddress);
const {address} = await wallet.createAddress('default');
const tx = await wallet.send({outputs: [{address, value: 1e4}]});
hash = tx.hash;
});
after(afterAll);
it('should fail to get TX that does not exist', async () => {
const hash = consensus.ZERO_HASH;
const tx = await wallet.getTX(hash.toString('hex'));
assert.strictEqual(tx, null);
});
it('should get TX', async () => {
const tx = await wallet.getTX(hash.toString('hex'));
assert(tx);
assert.strictEqual(tx.hash, hash);
});
});
describe('Zap TXs', function() {
const TEST_WALLET = 'test';
const DEFAULT = 'default';
const ALT = 'alt';
let testWallet;
const resetPending = async () => {
await wallet.zap(null, 0);
nodeCtx.mempool.reset();
};
before(async () => {
await beforeAll();
await wclient.createWallet(TEST_WALLET);
testWallet = wclient.wallet(TEST_WALLET);
await testWallet.createAccount(ALT);
await nodeCtx.mineBlocks(10, cbAddress);
});
afterEach(resetPending);
after(afterAll);
it('should zap all txs (wallet)', async () => {
for (const account of [DEFAULT, ALT]) {
const {address} = await testWallet.createAddress(account);
for (let i = 0; i < 3; i++)
await wallet.send({outputs: [{address, value: 1e4}]});
}
const result = await testWallet.zap(null, 0);
assert.strictEqual(result.zapped, 6);
});
it('should zap all txs (account)', async () => {
for (const account of [DEFAULT, ALT]) {
const {address} = await testWallet.createAddress(account);
for (let i = 0; i < 3; i++)
await wallet.send({outputs: [{address, value: 1e4}]});
}
const resultDefault = await testWallet.zap(DEFAULT, 0);
assert.strictEqual(resultDefault.zapped, 3);
const resultAlt = await testWallet.zap(ALT, 0);
assert.strictEqual(resultAlt.zapped, 3);
});
});
describe('Create account (Integration)', function() {
before(beforeAll);
after(afterAll);
it('should create an account', async () => {
const info = await wallet.createAccount('foo');
assert(info);
assert(info.initialized);
assert.strictEqual(info.name, 'foo');
assert.strictEqual(info.accountIndex, 1);
assert.strictEqual(info.m, 1);
assert.strictEqual(info.n, 1);
});
it('should create account', async () => {
const info = await wallet.createAccount('foo1');
assert(info);
assert(info.initialized);
assert.strictEqual(info.name, 'foo1');
assert.strictEqual(info.accountIndex, 2);
assert.strictEqual(info.m, 1);
assert.strictEqual(info.n, 1);
});
it('should create account', async () => {
const info = await wallet.createAccount('foo2', {
type: 'multisig',
m: 1,
n: 2
});
assert(info);
assert(!info.initialized);
assert.strictEqual(info.name, 'foo2');
assert.strictEqual(info.accountIndex, 3);
assert.strictEqual(info.m, 1);
assert.strictEqual(info.n, 2);
});
});
describe('Wallet auction (Integration)', function() {
const accountTwo = 'foobar';
let name, wallet2;
const ownedNames = [];
const allNames = [];
before(async () => {
await beforeAll();
await nodeCtx.mineBlocks(20, cbAddress);
await wallet.createAccount(accountTwo);
await wclient.createWallet('secondary');
wallet2 = wclient.wallet('secondary');
const saddr = (await wallet2.createAddress('default')).address;
await nodeCtx.mineBlocks(5, saddr);
});
after(afterAll);
beforeEach(async () => {
name = await nclient.execute('grindname', [5]);
});
afterEach(async () => {
await nodeCtx.mempool.reset();
});
it('should have no name state indexed initially', async () => {
const names = await wallet.getNames();
assert.strictEqual(names.length, 0);
});
it('should allow covenants with create tx', async () => {
const {address} = await wallet.createChange('default');
const output = openOutput(name, address, network);
const tx = await wallet.createTX({
outputs: [output.getJSON(network)]
});
assert.equal(tx.outputs[0].covenant.type, types.OPEN);
});
it('should allow covenants with send tx', async () => {
const {address} = await wallet.createChange('default');
const output = openOutput(name, address, network);
const tx = await wallet.send({
outputs: [output.getJSON(network)]
});
assert.equal(tx.outputs[0].covenant.type, types.OPEN);
});
it('should create an open and broadcast the tx', async () => {
let emitted = 0;
const handler = () => emitted++;
nodeCtx.mempool.on('tx', handler);
const mempoolTXEvent = common.forEvent(nodeCtx.mempool, 'tx');
const json = await wallet.createOpen({
name: name
});
await mempoolTXEvent;
const mempool = await nodeCtx.nclient.getMempool();
assert.ok(mempool.includes(json.hash));
const opens = json.outputs.filter(output => output.covenant.type === types.OPEN);
assert.equal(opens.length, 1);
assert.equal(emitted, 1);
// reset for next test
nodeCtx.mempool.removeListener('tx', handler);
});
it('should create an open and not broadcast the transaction', async () => {
let entered = false;
const handler = () => entered = true;
nodeCtx.mempool.on('tx', handler);
const json = await wallet.createOpen({
name: name,
broadcast: false
});
await sleep(200);
// tx is not in the mempool
assert.equal(entered, false);
const mempool = await nclient.getMempool();
assert.ok(!mempool.includes(json.hash));
const mtx = MTX.fromJSON(json);
assert.ok(mtx.hasWitness());
// the signature and pubkey are templated correctly
const sig = mtx.inputs[0].witness.get(0);
assert.ok(isSignatureEncoding(sig));
const pubkey = mtx.inputs[0].witness.get(1);
assert.ok(isKeyEncoding(pubkey));
assert.ok(secp256k1.publicKeyVerify(pubkey));
// transaction is valid
assert.ok(mtx.verify());
const opens = mtx.outputs.filter(output => output.covenant.type === types.OPEN);
assert.equal(opens.length, 1);
// reset for next test
nodeCtx.mempool.removeListener('tx', handler);
});
it('should create an open and not sign the transaction', async () => {
let entered = false;
const handler = () => entered = true;
nodeCtx.mempool.on('tx', handler);
const json = await wallet.createOpen({
name: name,
broadcast: false,
sign: false
});
await sleep(200);
// tx is not in the mempool
assert.equal(entered, false);
const mempool = await nclient.getMempool();
assert.ok(!mempool.includes(json.hash));
// the signature is templated as an
// empty buffer
const mtx = MTX.fromJSON(json);
const sig = mtx.inputs[0].witness.get(0);
assert.bufferEqual(Buffer.from(''), sig);
assert.ok(!isSignatureEncoding(sig));
// the pubkey is properly templated
const pubkey = mtx.inputs[0].witness.get(1);
assert.ok(isKeyEncoding(pubkey));
assert.ok(secp256k1.publicKeyVerify(pubkey));
// transaction not valid
assert.equal(mtx.verify(), false);
// reset for next test
nodeCtx.mempool.removeListener('tx', handler);
});
it('should throw error with incompatible broadcast and sign options', async () => {
const fn = async () => await (wallet.createOpen({
name: name,
broadcast: true,
sign: false
}));
await assert.rejects(fn, {message: 'Must sign when broadcasting.'});
});
it('should fail to create open for account with no monies', async () => {
const info = await wallet.getAccount(accountTwo);
assert.equal(info.balance.tx, 0);
assert.equal(info.balance.coin, 0);
const fn = async () => (await wallet.createOpen({
name: name,
account: accountTwo
}));
await assert.rejects(fn, {message: /Not enough funds./});
});
it('should mine to the account with no monies', async () => {
const height = 5;
const {receiveAddress} = await wallet.getAccount(accountTwo);
await nodeCtx.mineBlocks(height, receiveAddress);
const info = await wallet.getAccount(accountTwo);
assert.equal(info.balance.tx, height);
assert.equal(info.balance.coin, height);
});
it('should create open for specific account', async () => {
const json = await wallet.createOpen({
name: name,
account: accountTwo
});
const info = await wallet.getAccount(accountTwo);
// assert that each of the inputs belongs to the account
for (const {address} of json.inputs) {
const keyInfo = await wallet.getKey(address);
assert.equal(keyInfo.name, info.name);
}
});
it('should open an auction', async () => {
await wallet.createOpen({
name: name
});
// save chain height for later comparison
const info = await nclient.getInfo();
await nodeCtx.mineBlocks(treeInterval + 1, cbAddress);
// Confirmed OPEN adds name to wallet's namemap
allNames.push(name);
const json = await wallet.createBid({
name: name,
bid: 1000,
lockup: 2000
});
const bids = json.outputs.filter(output => output.covenant.type === types.BID);
assert.equal(bids.length, 1);
const [bid] = bids;
assert.equal(bid.covenant.items.length, 4);
const [nameHash, start, rawName, blind] = bid.covenant.items;
assert.equal(nameHash, rules.hashName(name).toString('hex'));
// initially opened in the first block mined, so chain.height + 1
const hex = Buffer.from(start, 'hex').reverse().toString('hex');
assert.equal(parseInt(hex, 16), info.chain.height + 1);
assert.equal(rawName, Buffer.from(name, 'ascii').toString('hex'));
// blind is type string, so 32 * 2
assert.equal(blind.length, 32 * 2);
});
it('should be able to get nonce', async () => {
const bid = 100;
const response = await wallet.getNonce(name, {
address: cbAddress,
bid: bid
});
const address = Address.fromString(cbAddress, network.type);
const nameHash = rules.hashName(name);
const primary = nodeCtx.wdb.primary;
const nonces = await primary.generateNonces(nameHash, address, bid);
const blinds = nonces.map(nonce => rules.blind(bid, nonce));
assert.deepStrictEqual(response, {
address: address.toString(network.type),
blinds: blinds.map(blind => blind.toString('hex')),
nonces: nonces.map(nonce => nonce.toString('hex')),
bid: bid,
name: name,
nameHash: nameHash.toString('hex')
});
});
it('should be able to get nonce for bid=0', async () => {
const bid = 0;
const response = await wallet.getNonce(name, {
address: cbAddress,
bid: bid
});
const address = Address.fromString(cbAddress, network.type);
const nameHash = rules.hashName(name);
const primary = nodeCtx.wdb.primary;
const nonces = await primary.generateNonces(nameHash, address, bid);
const blinds = nonces.map(nonce => rules.blind(bid, nonce));
assert.deepStrictEqual(response, {
address: address.toString(network.type),
blinds: blinds.map(blind => blind.toString('hex')),
nonces: nonces.map(nonce => nonce.toString('hex')),
bid: bid,
name: name,
nameHash: nameHash.toString('hex')
});
});
it('should get name info', async () => {
const names = await wallet.getNames();
assert.strictEqual(allNames.length, names.length);
assert(names.length > 0);
const [ns] = names;
const nameInfo = await wallet.getName(ns.name);
assert.deepEqual(ns, nameInfo);
});
it('should fail to open a bid without a bid value', async () => {
const fn = async () => (await wallet.createBid({
name: name
}));
await assert.rejects(fn, {message: 'Bid is required.'});
});
it('should fail to open a bid without a lockup value', async () => {
const fn = async () => (await wallet.createBid({
name: name,
bid: 1000
}));
await assert.rejects(fn, {message: 'Lockup is required.'});
});
it('should send bid with 0 value and non-dust lockup', async () => {
await wallet.createOpen({
name: name
});
await nodeCtx.mineBlocks(treeInterval + 1, cbAddress);
// Confirmed OPEN adds name to wallet's namemap
allNames.push(name);
await wallet.createBid({
name: name,
bid: 0,
lockup: 1000
});
});
it('should fail to send bid with 0 value and 0 lockup', async () => {
await wallet.createOpen({
name: name
});
await nodeCtx.mineBlocks(treeInterval + 1, cbAddress);
// Confirmed OPEN adds name to wallet's namemap
allNames.push(name);
const fn = async () => await wallet.createBid({
name: name,
bid: 0,
lockup: 0
});
await assert.rejects(fn, {message: 'Output is dust.'});
});
it('should get all bids (single player)', async () => {
await wallet.createOpen({
name: name
});
await nodeCtx.mineBlocks(treeInterval + 1, cbAddress);
// Confirmed OPEN adds name to wallet's namemap
allNames.push(name);
const tx1 = await wallet.createBid({
name: name,
bid: 1000,
lockup: 2000
});
const tx2 = await wallet.createBid({
name: name,
bid: 2000,
lockup: 3000
});
const tx3 = await wallet.createBid({
name: name,
bid: 4000,
lockup: 5000
});
await nodeCtx.mineBlocks(1, cbAddress);
// this method gets all bids for all names
const bids = await wallet.getBids();
// this depends on this it block creating
// the first bids of this test suite
assert.equal(bids.length, 3);
assert.ok(bids.every(bid => bid.name === name));
assert.ok(bids.every(bid => bid.height === nodeCtx.height));
// tx1
assert.ok(bids.find(bid =>
(bid.value === 1000
&& bid.lockup === 2000
&& bid.prevout.hash === tx1.hash)
));
// tx2
assert.ok(bids.find(bid =>
(bid.value === 2000
&& bid.lockup === 3000
&& bid.prevout.hash === tx2.hash)
));
// tx3
assert.ok(bids.find(bid =>
(bid.value === 4000
&& bid.lockup === 5000
&& bid.prevout.hash === tx3.hash)
));
});
it('should get all bids (two players)', async () => {
await wallet.createOpen({
name: name
});
await nodeCtx.mineBlocks(treeInterval + 1, cbAddress);
// Confirmed OPEN adds name to wallet's namemap
allNames.push(name);
const tx1 = await wallet.createBid({
name: name,
bid: 1000,
lockup: 2000
});
const tx2 = await wallet2.createBid({
name: name,
bid: 2000,
lockup: 3000
});
await nodeCtx.mineBlocks(1, cbAddress);
{
await sleep(100);
// fetch all bids for the name
const bids = await wallet.getBidsByName(name);
assert.equal(bids.length, 2);
assert.ok(bids.every(bid => bid.height === nodeCtx.height));
// there is no value property on bids
// from other wallets
assert.ok(bids.find(bid =>