@@ -70,6 +70,14 @@ export type IdentityCreationOptions = {
70
70
method ?: string ;
71
71
blockchain ?: string ;
72
72
networkId ?: string ;
73
+ } & AuthBJJCredentialCreationOptions ;
74
+
75
+ /**
76
+ * Options for creating Auth BJJ credential
77
+ * seed - seed to generate BJJ key pair
78
+ * revocationOpts -
79
+ */
80
+ export type AuthBJJCredentialCreationOptions = {
73
81
revocationOpts : {
74
82
id : string ;
75
83
type : CredentialStatusType ;
@@ -82,12 +90,16 @@ export type IdentityCreationOptions = {
82
90
seed ?: Uint8Array ;
83
91
} ;
84
92
93
+ /**
94
+ * Options for creating Ethereum based identity
95
+ */
85
96
export type EthereumBasedIdentityCreationOptions = IdentityCreationOptions & {
86
97
ethereumBasedIdentityOpts ?: {
87
98
ethSigner ?: Signer ;
88
99
createBjjCredential ?: boolean ;
89
100
} ;
90
101
} ;
102
+
91
103
/**
92
104
* Options for RevocationInfoOptions.
93
105
*/
@@ -420,12 +432,14 @@ export interface IIdentityWallet {
420
432
*
421
433
* @param {DID } did - identifier of the user
422
434
* @param {TreeState } oldTreeState - old tree state of the user
435
+ * @param {boolean } isOldTreeState - if the old state is genesis
423
436
* @param {Signer } ethSigner - signer to sign the transaction
424
437
* @param {object } opts - additional options
425
438
*/
426
439
addBJJAuthCredential (
427
440
did : DID ,
428
441
oldTreeState : TreeState ,
442
+ isOldTreeStateGenesis : boolean ,
429
443
ethSigner : Signer ,
430
444
opts ?: object
431
445
) : Promise < W3CCredential > ;
@@ -510,14 +524,22 @@ export class IdentityWallet implements IIdentityWallet {
510
524
did : DID ,
511
525
pubKey : PublicKey ,
512
526
authClaim : Claim ,
513
- currentState : Hash ,
527
+ oldTreeState : TreeState ,
514
528
revocationOpts : { id : string ; type : CredentialStatusType }
515
529
) : Promise < W3CCredential > {
516
530
const claimsTree = await this . _storage . mt . getMerkleTreeByIdentifierAndType (
517
531
did . string ( ) ,
518
532
MerkleTreeType . Claims
519
533
) ;
520
534
535
+ const ctr = await claimsTree . root ( ) ;
536
+
537
+ const currentState = hashElems ( [
538
+ ctr . bigInt ( ) ,
539
+ oldTreeState . revocationRoot . bigInt ( ) ,
540
+ oldTreeState . rootOfRoots . bigInt ( )
541
+ ] ) ;
542
+
521
543
const authData = authClaim . getExpirationDate ( ) ;
522
544
const expiration = authData ? getUnixTimestamp ( authData ) : 0 ;
523
545
@@ -539,35 +561,40 @@ export class IdentityWallet implements IIdentityWallet {
539
561
}
540
562
} ;
541
563
542
- const schema = JSON . parse ( VerifiableConstants . AUTH . AUTH_BJJ_CREDENTIAL_SCHEMA_JSON ) ;
543
- let credential : W3CCredential = new W3CCredential ( ) ;
544
- try {
545
- credential = this . _credentialWallet . createCredential ( did , request , schema ) ;
546
- } catch ( e ) {
547
- throw new Error ( `Error create w3c credential ${ ( e as Error ) . message } ` ) ;
548
- }
549
-
550
- const index = authClaim . hIndex ( ) ;
551
- const ctr = await claimsTree . root ( ) ;
564
+ // Check if has already an auth credential
565
+ const authCredentials = await this . _credentialWallet . getAllAuthBJJCredentials ( did ) ;
552
566
553
- const { proof } = await claimsTree . generateProof ( index , ctr ) ;
567
+ let credential : W3CCredential = new W3CCredential ( ) ;
568
+ if ( authCredentials . length === 0 ) {
569
+ const schema = JSON . parse ( VerifiableConstants . AUTH . AUTH_BJJ_CREDENTIAL_SCHEMA_JSON ) ;
570
+ try {
571
+ credential = this . _credentialWallet . createCredential ( did , request , schema ) ;
572
+ } catch ( e ) {
573
+ throw new Error ( `Error create w3c credential ${ ( e as Error ) . message } ` ) ;
574
+ }
554
575
555
- const mtpProof : Iden3SparseMerkleTreeProof = new Iden3SparseMerkleTreeProof ( {
556
- mtp : proof ,
557
- issuerData : {
558
- id : did ,
559
- state : {
560
- rootOfRoots : ZERO_HASH ,
561
- revocationTreeRoot : ZERO_HASH ,
562
- claimsTreeRoot : ctr ,
563
- value : currentState
564
- }
565
- } ,
566
- coreClaim : authClaim
567
- } ) ;
576
+ const index = authClaim . hIndex ( ) ;
577
+ const { proof } = await claimsTree . generateProof ( index , ctr ) ;
568
578
569
- credential . proof = [ mtpProof ] ;
579
+ const mtpProof : Iden3SparseMerkleTreeProof = new Iden3SparseMerkleTreeProof ( {
580
+ mtp : proof ,
581
+ issuerData : {
582
+ id : did ,
583
+ state : {
584
+ rootOfRoots : oldTreeState . rootOfRoots ,
585
+ revocationTreeRoot : oldTreeState . revocationRoot ,
586
+ claimsTreeRoot : ctr ,
587
+ value : currentState
588
+ }
589
+ } ,
590
+ coreClaim : authClaim
591
+ } ) ;
570
592
593
+ credential . proof = [ mtpProof ] ;
594
+ } else {
595
+ // credential with sigProof signed with previous auth bjj credential
596
+ credential = await this . issueCredential ( did , request ) ;
597
+ }
571
598
return credential ;
572
599
}
573
600
@@ -594,11 +621,9 @@ export class IdentityWallet implements IIdentityWallet {
594
621
MerkleTreeType . Claims
595
622
) ;
596
623
597
- const currentState = hashElems ( [
598
- ( await claimsTree . root ( ) ) . bigInt ( ) ,
599
- ZERO_HASH . bigInt ( ) ,
600
- ZERO_HASH . bigInt ( )
601
- ] ) ;
624
+ const ctr = await claimsTree . root ( ) ;
625
+
626
+ const currentState = hashElems ( [ ctr . bigInt ( ) , ZERO_HASH . bigInt ( ) , ZERO_HASH . bigInt ( ) ] ) ;
602
627
603
628
const didType = buildDIDType (
604
629
opts . method || DidMethod . Iden3 ,
@@ -614,7 +639,12 @@ export class IdentityWallet implements IIdentityWallet {
614
639
did ,
615
640
pubKey ,
616
641
authClaim ,
617
- currentState ,
642
+ {
643
+ revocationRoot : ZERO_HASH ,
644
+ claimsRoot : ctr ,
645
+ state : currentState ,
646
+ rootOfRoots : ZERO_HASH
647
+ } ,
618
648
opts . revocationOpts
619
649
) ;
620
650
@@ -689,7 +719,7 @@ export class IdentityWallet implements IIdentityWallet {
689
719
rootOfRoots : ZERO_HASH
690
720
} ;
691
721
692
- credential = await this . addBJJAuthCredential ( did , oldTreeState , ethSigner , opts ) ;
722
+ credential = await this . addBJJAuthCredential ( did , oldTreeState , true , ethSigner , opts ) ;
693
723
}
694
724
695
725
return {
@@ -1351,6 +1381,7 @@ export class IdentityWallet implements IIdentityWallet {
1351
1381
async addBJJAuthCredential (
1352
1382
did : DID ,
1353
1383
oldTreeState : TreeState ,
1384
+ isOldStateGenesis : boolean ,
1354
1385
ethSigner : Signer ,
1355
1386
opts : IdentityCreationOptions ,
1356
1387
prover ?: IZKProver // it will be needed in case of non ethereum identities
@@ -1365,33 +1396,33 @@ export class IdentityWallet implements IIdentityWallet {
1365
1396
const { hi, hv } = authClaim . hiHv ( ) ;
1366
1397
await this . _storage . mt . addToMerkleTree ( did . string ( ) , MerkleTreeType . Claims , hi , hv ) ;
1367
1398
1368
- const claimsTree = await this . _storage . mt . getMerkleTreeByIdentifierAndType (
1369
- did . string ( ) ,
1370
- MerkleTreeType . Claims
1371
- ) ;
1372
-
1373
- const stateAuthClaim = hashElems ( [
1374
- ( await claimsTree . root ( ) ) . bigInt ( ) ,
1375
- oldTreeState . revocationRoot . bigInt ( ) ,
1376
- oldTreeState . rootOfRoots . bigInt ( )
1377
- ] ) ;
1378
-
1379
- const credential = await this . createAuthBJJCredential (
1399
+ let credential = await this . createAuthBJJCredential (
1380
1400
did ,
1381
1401
pubKey ,
1382
1402
authClaim ,
1383
- stateAuthClaim ,
1403
+ oldTreeState ,
1384
1404
opts . revocationOpts
1385
1405
) ;
1386
1406
1387
- await this . transitState ( did , oldTreeState , true , ethSigner , prover ) ;
1407
+ const txId = await this . transitState ( did , oldTreeState , isOldStateGenesis , ethSigner , prover ) ;
1408
+ const credsWithIden3MTPProof = await this . generateIden3SparseMerkleTreeProof (
1409
+ did ,
1410
+ [ credential ] ,
1411
+ txId
1412
+ ) ;
1413
+ await this . _credentialWallet . saveAll ( credsWithIden3MTPProof ) ;
1414
+
1415
+ const credRefreshed = await this . _credentialWallet . findById ( credential . id ) ;
1416
+ if ( ! credRefreshed ) {
1417
+ throw new Error ( 'Credential not found in credential wallet' ) ;
1418
+ }
1419
+ credential = credRefreshed ;
1388
1420
1389
1421
await this . publishRevocationInfoByCredentialStatusType ( did , opts . revocationOpts . type , {
1390
1422
rhsUrl : opts . revocationOpts . id ,
1391
1423
onChain : opts . revocationOpts . onChain
1392
1424
} ) ;
1393
1425
1394
- await this . _credentialWallet . save ( credential ) ;
1395
1426
return credential ;
1396
1427
}
1397
1428
}
0 commit comments