-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathbns-integration.test.ts
More file actions
676 lines (623 loc) · 26.3 KB
/
bns-integration.test.ts
File metadata and controls
676 lines (623 loc) · 26.3 KB
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
import { ApiServer, startApiServer } from '../../src/api/init';
import * as supertest from 'supertest';
import { createHash } from 'crypto';
import { AnchorMode, ChainID, PostConditionMode, someCV } from '@stacks/transactions';
import { StacksMocknet } from '@stacks/network';
import {
broadcastTransaction,
bufferCV,
ClarityAbi,
FungibleConditionCode,
getAddressFromPrivateKey,
makeContractCall,
makeStandardSTXPostCondition,
standardPrincipalCV,
uintCV,
SignedContractCallOptions,
noneCV,
StacksTransaction,
TransactionVersion,
} from '@stacks/transactions';
import { PgWriteStore } from '../../src/datastore/pg-write-store';
import { standByForTx as standByForTxShared } from '../utils/test-helpers';
import { FAUCET_TESTNET_KEYS } from '../../src/api/routes/faucets';
import { logger } from '@stacks/api-toolkit';
import { ENV } from '../../src/env';
function hash160(bfr: Buffer): Buffer {
const hash160 = createHash('ripemd160')
.update(createHash('sha256').update(bfr).digest())
.digest('hex');
return Buffer.from(hash160, 'hex');
}
const network = new StacksMocknet({
url: `http://${ENV.STACKS_BLOCKCHAIN_API_HOST}:${ENV.STACKS_BLOCKCHAIN_API_PORT}`,
});
const deployedTo = 'ST000000000000000000002AMW42H';
const deployedName = 'bns';
const salt = Buffer.from('60104ad42ed976f5b8cfd6341496476aa72d1101', 'hex'); // salt and pepper
type TestnetKey = {
pkey: string;
address: string;
};
describe('BNS integration tests', () => {
let db: PgWriteStore;
let api: ApiServer;
let bnsContractAbi: ClarityAbi | undefined;
const standByForTx = (expectedTxId: string) => standByForTxShared(expectedTxId, api);
async function getBnsContractAbi(): Promise<ClarityAbi> {
if (bnsContractAbi) return bnsContractAbi;
const contractId = `${deployedTo}.${deployedName}`;
const contractResp = await supertest(api.server).get(`/extended/v1/contract/${contractId}`);
if (contractResp.status === 200 && contractResp.body?.abi) {
const apiAbi =
typeof contractResp.body.abi === 'string'
? JSON.parse(contractResp.body.abi)
: contractResp.body.abi;
if (apiAbi?.functions) {
bnsContractAbi = apiAbi as ClarityAbi;
return bnsContractAbi;
}
}
const abiUrl = network.getAbiApiUrl(deployedTo, deployedName);
const response = await fetch(abiUrl);
if (!response.ok) {
throw new Error(
`Failed to fetch BNS ABI from API and ${abiUrl}: ${response.status} ${response.statusText}`
);
}
const payload = JSON.parse(await response.text());
const abiCandidate = payload?.functions ?? payload?.abi ?? payload?.contract_interface;
if (!abiCandidate?.functions) {
const debugShape = JSON.stringify(Object.keys(payload ?? {}));
throw new Error(
`Unexpected ABI response shape from ${abiUrl}. Top-level keys: ${debugShape}`
);
}
bnsContractAbi = abiCandidate as ClarityAbi;
return bnsContractAbi;
}
async function makeBnsContractCall(
txOptions: SignedContractCallOptions
): Promise<StacksTransaction> {
const abi = await getBnsContractAbi();
const senderAddress = getAddressFromPrivateKey(txOptions.senderKey, TransactionVersion.Testnet);
const nonces = await db.getAddressNonces({ stxAddress: senderAddress });
const options = {
...txOptions,
validateWithAbi: abi,
nonce: txOptions.nonce ?? BigInt(nonces.possibleNextNonce),
};
return await makeContractCall(options);
}
async function standbyBnsName(expectedTxId: string): Promise<string> {
const broadcastTx = new Promise<string>(resolve => {
const listener: (txId: string) => void = txId => {
if (txId === expectedTxId) {
api.datastore.eventEmitter.removeListener('nameUpdate', listener);
resolve(txId);
}
};
api.datastore.eventEmitter.addListener('nameUpdate', listener);
});
const txid = await broadcastTx;
return txid;
}
async function getContractTransaction(txOptions: SignedContractCallOptions, zonefile?: string) {
const transaction = await makeBnsContractCall(txOptions);
const body: { tx: string; attachment?: string } = {
tx: Buffer.from(transaction.serialize()).toString('hex'),
};
if (zonefile) body.attachment = Buffer.from(zonefile).toString('hex');
const apiResult = await fetch(network.getBroadcastApiUrl(), {
method: 'post',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
});
await apiResult.json();
const expectedTxId = '0x' + transaction.txid();
const standByNamePromise = standbyBnsName(expectedTxId);
const result = await standByForTx(expectedTxId);
if (result.status != 1) throw new Error('result status error');
await standByNamePromise;
return transaction;
}
async function namespacePreorder(namespaceHash: Buffer, testnetKey: TestnetKey) {
const txOptions: SignedContractCallOptions = {
contractAddress: deployedTo,
contractName: deployedName,
functionName: 'namespace-preorder',
functionArgs: [bufferCV(namespaceHash), uintCV(64000000000)],
senderKey: testnetKey.pkey,
validateWithAbi: true,
postConditions: [
makeStandardSTXPostCondition(testnetKey.address, FungibleConditionCode.GreaterEqual, 1),
],
network,
anchorMode: AnchorMode.Any,
fee: 100000,
};
const transaction = await makeBnsContractCall(txOptions);
await broadcastTransaction(transaction, network);
const preorder = await standByForTx('0x' + transaction.txid());
if (preorder.status != 1) logger.error('Namespace preorder error');
return transaction;
}
async function namespaceReveal(
namespace: string,
salt: Buffer,
testnetKey: TestnetKey,
expiration: number
) {
const revealTxOptions: SignedContractCallOptions = {
contractAddress: deployedTo,
contractName: deployedName,
functionName: 'namespace-reveal',
functionArgs: [
bufferCV(Buffer.from(namespace)),
bufferCV(salt),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(1),
uintCV(expiration), //this number is set to expire the name before calling name-revewal
standardPrincipalCV(testnetKey.address),
],
senderKey: testnetKey.pkey,
validateWithAbi: true,
network,
anchorMode: AnchorMode.Any,
fee: 100000,
};
const revealTransaction = await makeBnsContractCall(revealTxOptions);
await broadcastTransaction(revealTransaction, network);
const reveal = await standByForTx('0x' + revealTransaction.txid());
if (reveal.status != 1) logger.error('Namespace Reveal Error');
return revealTransaction;
}
async function initiateNamespaceNetwork(
namespace: string,
salt: Buffer,
namespaceHash: Buffer,
testnetKey: TestnetKey,
expiration: number
) {
await namespacePreorder(namespaceHash, testnetKey);
await namespaceReveal(namespace, salt, testnetKey, expiration);
}
async function namespaceReady(namespace: string, pkey: string) {
const txOptions = {
contractAddress: deployedTo,
contractName: deployedName,
functionName: 'namespace-ready',
functionArgs: [bufferCV(Buffer.from(namespace))],
senderKey: pkey,
validateWithAbi: true,
network,
anchorMode: AnchorMode.Any,
fee: 100000,
};
const transaction = await makeBnsContractCall(txOptions);
await broadcastTransaction(transaction, network);
const readyResult = await standByForTx('0x' + transaction.txid());
if (readyResult.status != 1) logger.error('namespace-ready error');
return transaction;
}
async function nameImport(
namespace: string,
zonefile: string,
name: string,
testnetKey: TestnetKey
) {
const txOptions = {
contractAddress: deployedTo,
contractName: deployedName,
functionName: 'name-import',
functionArgs: [
bufferCV(Buffer.from(namespace)),
bufferCV(Buffer.from(name)),
standardPrincipalCV(testnetKey.address),
bufferCV(hash160(Buffer.from(zonefile))),
],
senderKey: testnetKey.pkey,
validateWithAbi: true,
network,
anchorMode: AnchorMode.Any,
fee: 100000,
};
return await getContractTransaction(txOptions, zonefile);
}
async function nameUpdate(namespace: string, zonefile: string, name: string, pkey: string) {
const txOptions = {
contractAddress: deployedTo,
contractName: deployedName,
functionName: 'name-update',
functionArgs: [
bufferCV(Buffer.from(namespace)),
bufferCV(Buffer.from(name)),
bufferCV(hash160(Buffer.from(zonefile))),
],
senderKey: pkey,
validateWithAbi: true,
network,
anchorMode: AnchorMode.Any,
fee: 100000,
};
return await getContractTransaction(txOptions, zonefile);
}
async function namePreorder(
namespace: string,
saltName: string,
testnetKey: TestnetKey,
name: string
) {
const postConditions = [
makeStandardSTXPostCondition(testnetKey.address, FungibleConditionCode.GreaterEqual, 1),
];
const fqn = `${name}.${namespace}${saltName}`;
const nameSaltedHash = hash160(Buffer.from(fqn));
const preOrderTxOptions: SignedContractCallOptions = {
contractAddress: deployedTo,
contractName: deployedName,
functionName: 'name-preorder',
functionArgs: [bufferCV(nameSaltedHash), uintCV(64000000000)],
senderKey: testnetKey.pkey,
validateWithAbi: true,
postConditions: postConditions,
network,
anchorMode: AnchorMode.Any,
fee: 100000,
};
const preOrderTransaction = await makeBnsContractCall(preOrderTxOptions);
await broadcastTransaction(preOrderTransaction, network);
const preorderResult = await standByForTx('0x' + preOrderTransaction.txid());
return preOrderTransaction;
}
async function nameRegister(
namespace: string,
saltName: string,
zonefile: string,
testnetKey: TestnetKey,
name: string
) {
await namePreorder(namespace, saltName, testnetKey, name);
const txOptions = {
contractAddress: deployedTo,
contractName: deployedName,
functionName: 'name-register',
functionArgs: [
bufferCV(Buffer.from(namespace)),
bufferCV(Buffer.from(name)),
bufferCV(Buffer.from(saltName)),
bufferCV(hash160(Buffer.from(zonefile))),
],
senderKey: testnetKey.pkey,
validateWithAbi: true,
network,
anchorMode: AnchorMode.Any,
fee: 100000,
};
return await getContractTransaction(txOptions, zonefile);
}
async function nameTransfer(namespace: string, name: string, testnetKey: TestnetKey) {
const txOptions: SignedContractCallOptions = {
contractAddress: deployedTo,
contractName: deployedName,
functionName: 'name-transfer',
functionArgs: [
bufferCV(Buffer.from(namespace)),
bufferCV(Buffer.from(name)),
standardPrincipalCV(testnetKey.address),
noneCV(),
],
senderKey: testnetKey.pkey,
validateWithAbi: true,
postConditionMode: PostConditionMode.Allow,
anchorMode: AnchorMode.Any,
network,
fee: 100000,
};
return await getContractTransaction(txOptions);
}
async function nameRevoke(namespace: string, name: string, pkey: string) {
const txOptions: SignedContractCallOptions = {
contractAddress: deployedTo,
contractName: deployedName,
functionName: 'name-revoke',
functionArgs: [bufferCV(Buffer.from(namespace)), bufferCV(Buffer.from(name))],
senderKey: pkey,
validateWithAbi: true,
network,
anchorMode: AnchorMode.Any,
fee: 100000,
};
return await getContractTransaction(txOptions);
}
async function nameRenewal(namespace: string, zonefile: string, pkey: string, name: string) {
const txOptions: SignedContractCallOptions = {
contractAddress: deployedTo,
contractName: deployedName,
functionName: 'name-renewal',
functionArgs: [
bufferCV(Buffer.from(namespace)),
bufferCV(Buffer.from(name)),
uintCV(2560000),
noneCV(),
someCV(bufferCV(hash160(Buffer.from(zonefile)))),
],
senderKey: pkey,
validateWithAbi: true,
network,
anchorMode: AnchorMode.Any,
fee: 100000,
};
return await getContractTransaction(txOptions);
}
beforeAll(async () => {
ENV.PG_DATABASE = 'postgres';
db = await PgWriteStore.connect({ usageName: 'tests', skipMigrations: true });
api = await startApiServer({ datastore: db, chainId: ChainID.Testnet });
});
afterAll(async () => {
await api.terminate();
await db?.close();
});
test('name-import/ready/update contract call', async () => {
const namespace = 'name-import';
const name = 'alice';
const importZonefile = `$ORIGIN ${name}.${namespace}\n$TTL 3600\n_http._tcp IN URI 10 1 "https://blockstack.s3.amazonaws.com/${name}.${namespace}"\n`;
const namespaceHash = hash160(Buffer.concat([Buffer.from(namespace), salt]));
const testnetKey = {
pkey: FAUCET_TESTNET_KEYS[0].secretKey,
address: FAUCET_TESTNET_KEYS[0].stacksAddress,
};
// initalizing namespace network - preorder and reveal
await initiateNamespaceNetwork(namespace, salt, namespaceHash, testnetKey, 12);
// testing name import
await nameImport(namespace, importZonefile, name, testnetKey);
const importQuery = await db.getName({
name: `${name}.${namespace}`,
includeUnanchored: false,
});
const importQuery1 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(importQuery1.status).toBe(200);
expect(importQuery1.type).toBe('application/json');
expect(importQuery.found).toBe(true);
if (importQuery.found) {
expect(importQuery.result.zonefile).toBe(importZonefile);
}
// testing namespace ready
await namespaceReady(namespace, testnetKey.pkey);
const readyQuery1 = await supertest(api.server).get('/v1/namespaces');
const readyResult = JSON.parse(readyQuery1.text);
expect(readyResult.namespaces.includes(namespace)).toBe(true);
});
test('name-update contract call', async () => {
const namespace = 'name-update';
const name = 'update';
const importZonefile = `$ORIGIN ${name}.${namespace}\n$TTL 3600\n_http._tcp IN URI 10 1 "https://blockstack.s3.amazonaws.com/${name}.${namespace}"\n`;
const namespaceHash = hash160(Buffer.concat([Buffer.from(namespace), salt]));
const testnetKey = {
pkey: FAUCET_TESTNET_KEYS[1].secretKey,
address: FAUCET_TESTNET_KEYS[1].stacksAddress,
};
// initalizing namespace network - preorder and reveal
await initiateNamespaceNetwork(namespace, salt, namespaceHash, testnetKey, 12);
// testing name import
await nameImport(namespace, importZonefile, name, testnetKey);
await namespaceReady(namespace, testnetKey.pkey);
// testing name update 1
let zonefile = `$TTL 3600
1yeardaily TXT "owner=1MwPD6dH4fE3gQ9mCov81L1DEQWT7E85qH" "seqn=0" "parts=1" "zf0=JE9SSUdJTiAxeWVhcmRhaWx5CiRUVEwgMzYwMApfaHR0cC5fdGNwIFVSSSAxMCAxICJodHRwczovL3BoLmRvdHBvZGNhc3QuY28vMXllYXJkYWlseS9oZWFkLmpzb24iCg=="
_http._tcp URI 10 1 "https://dotpodcast.co/"`;
try {
// testing name update
await nameUpdate(namespace, zonefile, name, testnetKey.pkey);
const query1 = await supertest(api.server).get(`/v1/names/1yeardaily.${name}.${namespace}`);
expect(query1.status).toBe(200);
expect(query1.type).toBe('application/json');
const query2 = await db.getSubdomain({
subdomain: `1yeardaily.${name}.${namespace}`,
includeUnanchored: false,
chainId: ChainID.Testnet,
});
expect(query2.found).toBe(true);
if (query2.result) expect(query2.result.resolver).toBe('');
const query3 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(query3.status).toBe(200);
expect(query3.type).toBe('application/json');
expect(query3.body.zonefile).toBe(zonefile);
} catch (err: any) {
throw new Error('Error post transaction: ' + err.message);
}
// testing name update 2
zonefile = `$TTL 3600
1yeardaily TXT "owner=1MwPD6dH4fE3gQ9mCov81L1DEQWT7E85qH" "seqn=0" "parts=1" "zf0=JE9SSUdJTiAxeWVhcmRhaWx5CiRUVEwgMzYwMApfaHR0cC5fdGNwIFVSSSAxMCAxICJodHRwczovL3BoLmRvdHBvZGNhc3QuY28vMXllYXJkYWlseS9oZWFkLmpzb24iCg=="Í
2dopequeens TXT "owner=1MwPD6dH4fE3gQ9mCov81L1DEQWT7E85qH" "seqn=0" "parts=1" "zf0=JE9SSUdJTiAyZG9wZXF1ZWVucwokVFRMIDM2MDAKX2h0dHAuX3RjcCBVUkkgMTAgMSAiaHR0cHM6Ly9waC5kb3Rwb2RjYXN0LmNvLzJkb3BlcXVlZW5zL2hlYWQuanNvbiIK"
10happier TXT "owner=1MwPD6dH4fE3gQ9mCov81L1DEQWT7E85qH" "seqn=0" "parts=1" "zf0=JE9SSUdJTiAxMGhhcHBpZXIKJFRUTCAzNjAwCl9odHRwLl90Y3AgVVJJIDEwIDEgImh0dHBzOi8vcGguZG90cG9kY2FzdC5jby8xMGhhcHBpZXIvaGVhZC5qc29uIgo="
31thoughts TXT "owner=1MwPD6dH4fE3gQ9mCov81L1DEQWT7E85qH" "seqn=0" "parts=1" "zf0=JE9SSUdJTiAzMXRob3VnaHRzCiRUVEwgMzYwMApfaHR0cC5fdGNwIFVSSSAxMCAxICJodHRwczovL3BoLmRvdHBvZGNhc3QuY28vMzF0aG91Z2h0cy9oZWFkLmpzb24iCg=="
359 TXT "owner=1MwPD6dH4fE3gQ9mCov81L1DEQWT7E85qH" "seqn=0" "parts=1" "zf0=JE9SSUdJTiAzNTkKJFRUTCAzNjAwCl9odHRwLl90Y3AgVVJJIDEwIDEgImh0dHBzOi8vcGguZG90cG9kY2FzdC5jby8zNTkvaGVhZC5qc29uIgo="
30for30 TXT "owner=1MwPD6dH4fE3gQ9mCov81L1DEQWT7E85qH" "seqn=0" "parts=1" "zf0=JE9SSUdJTiAzMGZvcjMwCiRUVEwgMzYwMApfaHR0cC5fdGNwIFVSSSAxMCAxICJodHRwczovL3BoLmRvdHBvZGNhc3QuY28vMzBmb3IzMC9oZWFkLmpzb24iCg=="
excluded TXT "subdomain should not include"
10minuteteacher TXT "owner=1MwPD6dH4fE3gQ9mCov81L1DEQWT7E85qH" "seqn=0" "parts=1" "zf0=JE9SSUdJTiAxMG1pbnV0ZXRlYWNoZXIKJFRUTCAzNjAwCl9odHRwLl90Y3AgVVJJIDEwIDEgImh0dHBzOi8vcGguZG90cG9kY2FzdC5jby8xMG1pbnV0ZXRlYWNoZXIvaGVhZC5qc29uIgo="
36questionsthepodcastmusical TXT "owner=1MwPD6dH4fE3gQ9mCov81L1DEQWT7E85qH" "seqn=0" "parts=1" "zf0=JE9SSUdJTiAzNnF1ZXN0aW9uc3RoZXBvZGNhc3RtdXNpY2FsCiRUVEwgMzYwMApfaHR0cC5fdGNwIFVSSSAxMCAxICJodHRwczovL3BoLmRvdHBvZGNhc3QuY28vMzZxdWVzdGlvbnN0aGVwb2RjYXN0bXVzaWNhbC9oZWFkLmpzb24iCg=="
_http._tcp URI 10 1 "https://dotpodcast.co/"`;
await nameUpdate(namespace, zonefile, name, testnetKey.pkey);
const query1 = await supertest(api.server).get(`/v1/names/2dopequeens.${name}.${namespace}`);
expect(query1.status).toBe(200);
expect(query1.type).toBe('application/json');
const query2 = await db.getSubdomainsList({ page: 0, includeUnanchored: false });
expect(
query2.results.filter(function (value) {
return value === `1yeardaily.${name}.${namespace}`;
}).length
).toBe(1);
const query3 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(query3.status).toBe(200);
expect(query3.type).toBe('application/json');
expect(query3.body.zonefile).toBe(zonefile); //zone file updated of same name
const query4 = await supertest(api.server).get(
`/v1/names/36questionsthepodcastmusical.${name}.${namespace}`
);
expect(query4.status).toBe(200);
const query5 = await supertest(api.server).get(`/v1/names/excluded.${name}.${namespace}`);
expect(query5.status).toBe(404);
expect(query5.type).toBe('application/json');
// testing nameupdate 3
zonefile = `$TTL 3600
_http._tcp URI 10 1 "https://dotpodcast.co/"`;
await nameUpdate(namespace, zonefile, name, testnetKey.pkey);
const query6 = await supertest(api.server).get(`/v1/names/2dopequeens.${name}.${namespace}`); //check if previous sobdomains are still there
expect(query6.status).toBe(200);
expect(query6.type).toBe('application/json');
const query7 = await db.getSubdomainsList({ page: 0, includeUnanchored: false });
expect(query7.results).toContain(`1yeardaily.${name}.${namespace}`);
const query8 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(query8.status).toBe(200);
expect(query8.type).toBe('application/json');
expect(query8.body.zonefile).toBe(zonefile);
});
test('name-register/transfer contract call', async () => {
const saltName = '0000';
const name = 'bob';
const namespace = 'name-register';
const namespaceHash = hash160(Buffer.concat([Buffer.from(namespace), salt]));
const zonefile = `$ORIGIN ${name}.${namespace}\n$TTL 3600\n_http._tcp IN URI 10 1 "https://blockstack.s3.amazonaws.com/${name}.${namespace}"\n`;
const importZonefile = `$ORIGIN ${name}.${namespace}\n$TTL 3600\n_http._tcp IN URI 10 1 "https://blockstack.s3.amazonaws.com/${name}.${namespace}"\n`;
const testnetKey = {
pkey: FAUCET_TESTNET_KEYS[2].secretKey,
address: FAUCET_TESTNET_KEYS[2].stacksAddress,
};
// initializing namespace network
await initiateNamespaceNetwork(namespace, salt, namespaceHash, testnetKey, 12);
await namespaceReady(namespace, testnetKey.pkey);
// testing name register
await nameRegister(namespace, saltName, zonefile, testnetKey, name);
const query1 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(query1.status).toBe(200);
expect(query1.type).toBe('application/json');
const query = await db.getName({
name: `${name}.${namespace}`,
includeUnanchored: false,
});
expect(query.found).toBe(true);
if (query.found) {
expect(query.result.zonefile).toBe(zonefile);
}
// testing name transfer
const transferTestnetKey = {
pkey: FAUCET_TESTNET_KEYS[2].secretKey,
address: FAUCET_TESTNET_KEYS[3].stacksAddress,
};
await nameTransfer(namespace, name, transferTestnetKey);
const query2 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(query2.status).toBe(200);
expect(query2.type).toBe('application/json');
expect(query2.body.zonefile).toBe('');
expect(query2.body.status).toBe('name-transfer');
});
test('name-revoke contract call', async () => {
//name revoke
const namespace = 'name-revoke';
const name = 'foo';
const namespaceHash = hash160(Buffer.concat([Buffer.from(namespace), salt]));
const testnetKey = {
pkey: FAUCET_TESTNET_KEYS[4].secretKey,
address: FAUCET_TESTNET_KEYS[4].stacksAddress,
};
const zonefile = `$ORIGIN ${name}.${namespace}\n$TTL 3600\n_http._tcp IN URI 10 1 "https://blockstack.s3.amazonaws.com/${name}.${namespace}"\n`;
// initializing namespace network
await initiateNamespaceNetwork(namespace, salt, namespaceHash, testnetKey, 12);
await nameImport(namespace, zonefile, name, testnetKey);
await namespaceReady(namespace, testnetKey.pkey);
// testing name revoke
await nameRevoke(namespace, name, testnetKey.pkey);
const query1 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(query1.status).toBe(404);
expect(query1.type).toBe('application/json');
});
test('name-import/name-renewal contract call', async () => {
const zonefile = `new zone file`;
const namespace = 'name-renewal';
const name = 'renewal';
const namespaceHash = hash160(Buffer.concat([Buffer.from(namespace), salt]));
const testnetKey = {
pkey: FAUCET_TESTNET_KEYS[5].secretKey,
address: FAUCET_TESTNET_KEYS[5].stacksAddress,
};
// initializing namespace network
await initiateNamespaceNetwork(namespace, salt, namespaceHash, testnetKey, 1);
await nameImport(namespace, zonefile, name, testnetKey);
await namespaceReady(namespace, testnetKey.pkey);
// check expiration block
const query0 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(query0.status).toBe(200);
expect(query0.type).toBe('application/json');
expect(query0.body.expire_block).toBe(0); // Imported names don't know about their namespaces
// name renewal
await nameRenewal(namespace, zonefile, testnetKey.pkey, name);
const query1 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(query1.status).toBe(200);
expect(query1.type).toBe('application/json');
expect(query1.body.zonefile).toBe(zonefile);
expect(query1.body.status).toBe('name-renewal');
// Name should appear only once in namespace list
const query2 = await supertest(api.server).get(`/v1/namespaces/${namespace}/names`);
expect(query2.status).toBe(200);
expect(query2.type).toBe('application/json');
expect(query2.body).toStrictEqual(['renewal.name-renewal']);
// check new expiration block, should not be 0
const query3 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(query3.status).toBe(200);
expect(query3.type).toBe('application/json');
expect(query3.body.expire_block).not.toBe(0);
});
test('name-register/name-renewal contract call', async () => {
const saltName = '0000';
const zonefile = `new zone file`;
const namespace = 'name-renewal2';
const name = 'renewal2';
const namespaceHash = hash160(Buffer.concat([Buffer.from(namespace), salt]));
const testnetKey = {
pkey: FAUCET_TESTNET_KEYS[5].secretKey,
address: FAUCET_TESTNET_KEYS[5].stacksAddress,
};
// initializing namespace network
await initiateNamespaceNetwork(namespace, salt, namespaceHash, testnetKey, 1);
await namespaceReady(namespace, testnetKey.pkey);
await nameRegister(namespace, saltName, zonefile, testnetKey, name);
// check expiration block, should not be 0
const query0 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(query0.status).toBe(200);
expect(query0.type).toBe('application/json');
expect(query0.body.expire_block).not.toBe(0);
const prevExpiration = query0.body.expire_block;
// name renewal
await nameRenewal(namespace, zonefile, testnetKey.pkey, name);
const query1 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(query1.status).toBe(200);
expect(query1.type).toBe('application/json');
expect(query1.body.zonefile).toBe(zonefile);
expect(query1.body.status).toBe('name-renewal');
// check new expiration block, should be greater than the previous one
const query3 = await supertest(api.server).get(`/v1/names/${name}.${namespace}`);
expect(query3.status).toBe(200);
expect(query3.type).toBe('application/json');
expect(query3.body.expire_block > prevExpiration).toBe(true);
});
});