1
- import { Cl } from "@stacks/transactions" ;
1
+ import { Cl , ClarityType } from "@stacks/transactions" ;
2
2
import { beforeEach , describe , expect , it } from "vitest" ;
3
3
import { createHash } from "node:crypto" ;
4
+ import { project } from '../clarigen-types' ; // where your [types.output] was specified
5
+ import { projectFactory } from '@clarigen/core' ;
6
+ import { rovOk , mapGet } from '@clarigen/test' ;
7
+
8
+ const contracts = projectFactory ( project , 'simnet' ) ;
9
+ const contract = contracts . bns ;
4
10
5
11
const accounts = simnet . getAccounts ( ) ;
6
12
const alice = accounts . get ( "wallet_1" ) ! ;
7
13
const bob = accounts . get ( "wallet_2" ) ! ;
8
14
const charlie = accounts . get ( "wallet_3" ) ! ;
9
15
16
+ function utf8ToBytes ( str : string ) {
17
+ return new TextEncoder ( ) . encode ( str ) ;
18
+ }
19
+
10
20
const cases = [
11
21
{
12
22
namespace : "blockstack" ,
@@ -120,7 +130,7 @@ describe("preorder namespace", () => {
120
130
[ Cl . buffer ( ripemd160 ) , Cl . uint ( cases [ 1 ] . value ) ] ,
121
131
cases [ 1 ] . namespaceOwner
122
132
) ;
123
- expect ( result ) . toBeOk ( Cl . uint ( 144 + simnet . blockHeight ) ) ;
133
+ expect ( result ) . toBeOk ( Cl . uint ( 143 + simnet . blockHeight ) ) ;
124
134
} ) ;
125
135
} ) ;
126
136
@@ -136,7 +146,7 @@ describe("namespace reveal workflow", () => {
136
146
[ Cl . buffer ( ripemd160 ) , Cl . uint ( cases [ 1 ] . value ) ] ,
137
147
cases [ 1 ] . namespaceOwner
138
148
) ;
139
- expect ( result ) . toBeOk ( Cl . uint ( 144 + simnet . blockHeight ) ) ;
149
+ expect ( result ) . toBeOk ( Cl . uint ( 143 + simnet . blockHeight ) ) ;
140
150
} ) ;
141
151
142
152
it ( "should reveal a namespace" , ( ) => {
@@ -180,7 +190,7 @@ describe("namespace reveal workflow", () => {
180
190
[ Cl . buffer ( ripemd160 ) , Cl . uint ( 100 ) ] ,
181
191
bob
182
192
) ;
183
- expect ( preorder . result ) . toBeOk ( Cl . uint ( 144 + simnet . blockHeight ) ) ;
193
+ expect ( preorder . result ) . toBeOk ( Cl . uint ( 141 + simnet . blockHeight ) ) ;
184
194
185
195
const register = simnet . callPublicFn (
186
196
"bns" ,
@@ -286,6 +296,7 @@ describe("name revealing workflow", () => {
286
296
const merged = new TextEncoder ( ) . encode ( `${ name } .${ cases [ 0 ] . namespace } ${ cases [ 0 ] . salt } ` ) ;
287
297
const sha256 = createHash ( "sha256" ) . update ( merged ) . digest ( ) ;
288
298
const ripemd160 = createHash ( "ripemd160" ) . update ( sha256 ) . digest ( ) ;
299
+ simnet . mineEmptyBlock ( ) ;
289
300
simnet . callPublicFn ( "bns" , "name-preorder" , [ Cl . buffer ( ripemd160 ) , Cl . uint ( 2559999 ) ] , bob ) ;
290
301
291
302
const { result } = simnet . callPublicFn (
@@ -328,6 +339,7 @@ describe("name revealing workflow", () => {
328
339
const merged = new TextEncoder ( ) . encode ( `${ name } .${ cases [ 0 ] . namespace } ${ cases [ 0 ] . salt } ` ) ;
329
340
const sha256 = createHash ( "sha256" ) . update ( merged ) . digest ( ) ;
330
341
const ripemd160 = createHash ( "ripemd160" ) . update ( sha256 ) . digest ( ) ;
342
+ simnet . mineEmptyBlock ( ) ;
331
343
simnet . callPublicFn ( "bns" , "name-preorder" , [ Cl . buffer ( ripemd160 ) , Cl . uint ( 2560000 ) ] , bob ) ;
332
344
333
345
const register = simnet . callPublicFn (
@@ -366,8 +378,8 @@ describe("name revealing workflow", () => {
366
378
Cl . tuple ( {
367
379
owner : Cl . standardPrincipal ( bob ) ,
368
380
[ "zonefile-hash" ] : Cl . bufferFromUtf8 ( cases [ 0 ] . zonefile ) ,
369
- [ "lease-ending-at" ] : Cl . some ( Cl . uint ( 16 ) ) ,
370
- [ "lease-started-at" ] : Cl . uint ( 6 ) ,
381
+ [ "lease-ending-at" ] : Cl . some ( Cl . uint ( 14 ) ) ,
382
+ [ "lease-started-at" ] : Cl . uint ( 4 ) ,
371
383
} )
372
384
) ;
373
385
} ) ;
@@ -377,6 +389,7 @@ describe("name revealing workflow", () => {
377
389
const merged = new TextEncoder ( ) . encode ( `${ name } .${ cases [ 0 ] . namespace } ${ cases [ 0 ] . salt } ` ) ;
378
390
const sha256 = createHash ( "sha256" ) . update ( merged ) . digest ( ) ;
379
391
const ripemd160 = createHash ( "ripemd160" ) . update ( sha256 ) . digest ( ) ;
392
+ simnet . mineEmptyBlock ( ) ;
380
393
simnet . callPublicFn ( "bns" , "name-preorder" , [ Cl . buffer ( ripemd160 ) , Cl . uint ( 2560000 ) ] , bob ) ;
381
394
simnet . callPublicFn (
382
395
"bns" ,
@@ -415,11 +428,11 @@ describe("register a name again before and after expiration", () => {
415
428
simnet . callPublicFn (
416
429
"bns" ,
417
430
"namespace-preorder" ,
418
- [ Cl . buffer ( ripemd160NS ) , Cl . uint ( cases [ 0 ] . value ) ] ,
431
+ [ Cl . buffer ( Uint8Array . from ( ripemd160NS ) ) , Cl . uint ( cases [ 0 ] . value ) ] ,
419
432
cases [ 0 ] . namespaceOwner
420
433
) ;
421
434
422
- simnet . callPublicFn (
435
+ const revealResult = simnet . callPublicFn (
423
436
"bns" ,
424
437
"namespace-reveal" ,
425
438
[
@@ -431,22 +444,34 @@ describe("register a name again before and after expiration", () => {
431
444
] ,
432
445
cases [ 0 ] . namespaceOwner
433
446
) ;
447
+ expect ( revealResult . result ) . toBeOk ( Cl . bool ( true ) ) ;
434
448
435
- simnet . callPublicFn (
449
+ const readyResult = simnet . callPublicFn (
436
450
"bns" ,
437
451
"namespace-ready" ,
438
452
[ Cl . bufferFromUtf8 ( cases [ 0 ] . namespace ) ] ,
439
453
cases [ 0 ] . namespaceOwner
440
454
) ;
441
-
455
+ expect ( readyResult . result ) . toBeOk ( Cl . bool ( true ) ) ;
442
456
// register bob.blockstack
443
457
const name = "bob" ;
444
458
const merged = new TextEncoder ( ) . encode ( `${ name } .${ cases [ 0 ] . namespace } ${ cases [ 0 ] . salt } ` ) ;
445
459
const sha256 = createHash ( "sha256" ) . update ( merged ) . digest ( ) ;
446
460
const ripemd160 = createHash ( "ripemd160" ) . update ( sha256 ) . digest ( ) ;
447
- simnet . callPublicFn ( "bns" , "name-preorder" , [ Cl . buffer ( ripemd160 ) , Cl . uint ( 2560000 ) ] , bob ) ;
461
+ simnet . mineEmptyBlocks ( 1 ) ;
462
+ const preorderResult = simnet . callPublicFn ( "bns" , "name-preorder" , [ Cl . buffer ( Uint8Array . from ( ripemd160 ) ) , Cl . uint ( 2560000 ) ] , bob ) ;
463
+ expect ( preorderResult . result . type ) . toBe ( ClarityType . ResponseOk ) ;
448
464
449
- simnet . callPublicFn (
465
+ const namespace = rovOk ( contract . getNamespaceProperties ( utf8ToBytes ( cases [ 0 ] . namespace ) ) )
466
+ expect ( namespace ) . toBeTruthy ( ) ;
467
+
468
+ const preorder = mapGet ( contract . identifier , contract . maps . namePreorders , {
469
+ buyer : bob ,
470
+ hashedSaltedFqn : Uint8Array . from ( ripemd160 ) ,
471
+ } )
472
+ expect ( preorder ) . toBeTruthy ( ) ;
473
+
474
+ const registerResult = simnet . callPublicFn (
450
475
"bns" ,
451
476
"name-register" ,
452
477
[
@@ -457,6 +482,7 @@ describe("register a name again before and after expiration", () => {
457
482
] ,
458
483
bob
459
484
) ;
485
+ expect ( registerResult . result ) . toBeOk ( Cl . bool ( true ) ) ;
460
486
} ) ;
461
487
462
488
it ( "fails if someone else tries to register it" , ( ) => {
@@ -465,13 +491,15 @@ describe("register a name again before and after expiration", () => {
465
491
const merged = new TextEncoder ( ) . encode ( `${ name } .${ cases [ 0 ] . namespace } ${ salt } ` ) ;
466
492
const sha256 = createHash ( "sha256" ) . update ( merged ) . digest ( ) ;
467
493
const ripemd160 = createHash ( "ripemd160" ) . update ( sha256 ) . digest ( ) ;
494
+ let blockHeight = simnet . blockHeight ;
495
+ console . log ( "blockHeight" , blockHeight ) ;
468
496
const preorder = simnet . callPublicFn (
469
497
"bns" ,
470
498
"name-preorder" ,
471
- [ Cl . buffer ( ripemd160 ) , Cl . uint ( 2560000 ) ] ,
499
+ [ Cl . buffer ( Uint8Array . from ( ripemd160 ) ) , Cl . uint ( 2560000 ) ] ,
472
500
charlie
473
501
) ;
474
- expect ( preorder . result ) . toBeOk ( Cl . uint ( 144 + simnet . blockHeight ) ) ;
502
+ expect ( preorder . result ) . toBeOk ( Cl . uint ( 138 + simnet . blockHeight ) ) ;
475
503
476
504
const register = simnet . callPublicFn (
477
505
"bns" ,
@@ -484,6 +512,7 @@ describe("register a name again before and after expiration", () => {
484
512
] ,
485
513
charlie
486
514
) ;
515
+ console . log ( simnet . blockHeight ) ;
487
516
expect ( register . result ) . toBeErr ( Cl . int ( 2004 ) ) ;
488
517
} ) ;
489
518
@@ -541,7 +570,7 @@ describe("register a name again before and after expiration", () => {
541
570
[ Cl . buffer ( ripemd160 ) , Cl . uint ( 2560000 ) ] ,
542
571
bob
543
572
) ;
544
- expect ( preorder . result ) . toBeOk ( Cl . uint ( 144 + simnet . blockHeight ) ) ;
573
+ expect ( preorder . result ) . toBeOk ( Cl . uint ( 138 + simnet . blockHeight ) ) ;
545
574
546
575
const register = simnet . callPublicFn (
547
576
"bns" ,
@@ -589,8 +618,8 @@ describe("register a name again before and after expiration", () => {
589
618
Cl . tuple ( {
590
619
owner : Cl . standardPrincipal ( charlie ) ,
591
620
[ "zonefile-hash" ] : Cl . bufferFromAscii ( "CHARLIE" ) ,
592
- [ "lease-ending-at" ] : Cl . some ( Cl . uint ( 5029 ) ) ,
593
- [ "lease-started-at" ] : Cl . uint ( 5019 ) ,
621
+ [ "lease-ending-at" ] : Cl . some ( Cl . uint ( 5025 ) ) ,
622
+ [ "lease-started-at" ] : Cl . uint ( 5015 ) ,
594
623
} )
595
624
) ;
596
625
} ) ;
0 commit comments