@0xpolygonid/js-sdk 1.17.1 → 1.17.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,43 +7,43 @@ const IDEN3_PROTOCOL = 'https://iden3-communication.io/';
7
7
  */
8
8
  exports.PROTOCOL_MESSAGE_TYPE = Object.freeze({
9
9
  // AuthorizationV2RequestMessageType defines auth request type of the communication protocol
10
- AUTHORIZATION_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'authorization/1.0/request',
10
+ AUTHORIZATION_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}authorization/1.0/request`,
11
11
  // AuthorizationResponseMessageType defines auth response type of the communication protocol
12
- AUTHORIZATION_RESPONSE_MESSAGE_TYPE: IDEN3_PROTOCOL + 'authorization/1.0/response',
12
+ AUTHORIZATION_RESPONSE_MESSAGE_TYPE: `${IDEN3_PROTOCOL}authorization/1.0/response`,
13
13
  // CredentialIssuanceRequestMessageType accepts request for credential creation
14
- CREDENTIAL_ISSUANCE_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/1.0/issuance-request',
14
+ CREDENTIAL_ISSUANCE_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/1.0/issuance-request`,
15
15
  // CredentialFetchRequestMessageType is type for request of credential generation
16
- CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/1.0/fetch-request',
16
+ CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/1.0/fetch-request`,
17
17
  // CredentialOfferMessageType is type of message with credential offering
18
- CREDENTIAL_OFFER_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/1.0/offer',
18
+ CREDENTIAL_OFFER_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/1.0/offer`,
19
19
  // CredentialIssuanceResponseMessageType is type for message with a credential issuance
20
- CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/1.0/issuance-response',
20
+ CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/1.0/issuance-response`,
21
21
  // CredentialRefreshMessageType is type for message with a credential issuance
22
- CREDENTIAL_REFRESH_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/1.0/refresh',
22
+ CREDENTIAL_REFRESH_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/1.0/refresh`,
23
23
  // DeviceRegistrationRequestMessageType defines device registration request type of the communication protocol
24
- DEVICE_REGISTRATION_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'devices/1.0/registration',
24
+ DEVICE_REGISTRATION_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}devices/1.0/registration`,
25
25
  // MessageFetMessageFetchRequestMessageType defines message fetch request type of the communication protocol.
26
- MESSAGE_FETCH_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'messages/1.0/fetch',
26
+ MESSAGE_FETCH_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}messages/1.0/fetch`,
27
27
  // ProofGenerationRequestMessageType is type for request of proof generation
28
- PROOF_GENERATION_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'proofs/1.0/request',
28
+ PROOF_GENERATION_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}proofs/1.0/request`,
29
29
  // ProofGenerationResponseMessageType is type for response of proof generation
30
- PROOF_GENERATION_RESPONSE_MESSAGE_TYPE: IDEN3_PROTOCOL + 'proofs/1.0/response',
30
+ PROOF_GENERATION_RESPONSE_MESSAGE_TYPE: `${IDEN3_PROTOCOL}proofs/1.0/response`,
31
31
  // RevocationStatusRequestMessageType is type for request of revocation status
32
- REVOCATION_STATUS_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'revocation/1.0/request-status',
32
+ REVOCATION_STATUS_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}revocation/1.0/request-status`,
33
33
  // RevocationStatusResponseMessageType is type for response with a revocation status
34
- REVOCATION_STATUS_RESPONSE_MESSAGE_TYPE: IDEN3_PROTOCOL + 'revocation/1.0/status',
34
+ REVOCATION_STATUS_RESPONSE_MESSAGE_TYPE: `${IDEN3_PROTOCOL}revocation/1.0/status`,
35
35
  // ContractInvokeRequestMessageType is type for request of contract invoke request
36
- CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'proofs/1.0/contract-invoke-request',
36
+ CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}proofs/1.0/contract-invoke-request`,
37
37
  // CredentialOnchainOfferMessageType is type of message with credential onchain offering
38
- CREDENTIAL_ONCHAIN_OFFER_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/1.0/onchain-offer',
38
+ CREDENTIAL_ONCHAIN_OFFER_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/1.0/onchain-offer`,
39
39
  // ProposalRequestMessageType is type for proposal-request message
40
- PROPOSAL_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal-request',
40
+ PROPOSAL_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/0.1/proposal-request`,
41
41
  // ProposalMessageType is type for proposal message
42
- PROPOSAL_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal',
42
+ PROPOSAL_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/0.1/proposal`,
43
43
  // PaymentRequestMessageType is type for payment-request message
44
- PAYMENT_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/payment-request',
44
+ PAYMENT_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/0.1/payment-request`,
45
45
  // PaymentMessageType is type for payment message
46
- PAYMENT_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/payment'
46
+ PAYMENT_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/0.1/payment`
47
47
  });
48
48
  /**
49
49
  * Media types for iden3 comm communication protocol
@@ -685,16 +685,8 @@ class IdentityWallet {
685
685
  await this.updateIdentityState(did, true, newTreeState);
686
686
  return txId;
687
687
  }
688
- /** {@inheritdoc IIdentityWallet.addBJJAuthCredential} */
689
- async addBJJAuthCredential(did, oldTreeState, isOldStateGenesis, ethSigner, opts, prover // it will be needed in case of non ethereum identities
690
- ) {
691
- opts.seed = opts.seed ?? (0, js_crypto_1.getRandomBytes)(32);
692
- opts.revocationOpts.nonce =
693
- opts.revocationOpts.nonce ??
694
- (isOldStateGenesis
695
- ? 0
696
- : opts.revocationOpts.nonce ?? new DataView((0, js_crypto_1.getRandomBytes)(12).buffer).getUint32(0));
697
- const { authClaim, pubKey } = await this.createAuthCoreClaim(opts.revocationOpts.nonce ?? 0, opts.seed);
688
+ async getAuthBJJCredential(did, oldTreeState, { nonce, seed, id, type }) {
689
+ const { authClaim, pubKey } = await this.createAuthCoreClaim(nonce, seed);
698
690
  const { hi, hv } = authClaim.hiHv();
699
691
  await this._storage.mt.addToMerkleTree(did.string(), storage_1.MerkleTreeType.Claims, hi, hv);
700
692
  // Calculate current state after adding credential to merkle tree
@@ -704,34 +696,68 @@ class IdentityWallet {
704
696
  oldTreeState.revocationRoot.bigInt(),
705
697
  oldTreeState.rootOfRoots.bigInt()
706
698
  ]);
707
- let credential = await this.createAuthBJJCredential(did, pubKey, authClaim, currentState, opts.revocationOpts);
708
- const txId = await this.transitState(did, oldTreeState, isOldStateGenesis, ethSigner, prover);
709
- const { receipt, block } = await this._transactionService.getTransactionReceiptAndBlock(txId);
710
- const credsWithIden3MTPProof = await this.generateIden3SparseMerkleTreeProof(did, [credential], txId, receipt?.blockNumber, block?.timestamp, undefined, {
711
- revNonce: Number(authClaim.getRevocationNonce()),
712
- subjectPosition: verifiable_1.SubjectPosition.None,
713
- merklizedRootPosition: verifiable_1.MerklizedRootPosition.None,
714
- updatable: false,
715
- version: 0,
716
- merklizeOpts: { documentLoader: (0, schema_processor_1.cacheLoader)() }
699
+ return this.createAuthBJJCredential(did, pubKey, authClaim, currentState, {
700
+ id,
701
+ type
717
702
  });
718
- await this._credentialWallet.saveAll(credsWithIden3MTPProof);
719
- await this._storage.identity.saveIdentity({
720
- did: did.string(),
721
- state: currentState,
722
- isStatePublished: true,
723
- isStateGenesis: false
703
+ }
704
+ /** {@inheritdoc IIdentityWallet.addBJJAuthCredential} */
705
+ async addBJJAuthCredential(did, oldTreeState, isOldStateGenesis, ethSigner, opts, prover // it will be needed in case of non ethereum identities
706
+ ) {
707
+ opts.seed = opts.seed ?? (0, js_crypto_1.getRandomBytes)(32);
708
+ opts.revocationOpts.nonce =
709
+ opts.revocationOpts.nonce ??
710
+ (isOldStateGenesis
711
+ ? 0
712
+ : opts.revocationOpts.nonce ?? new DataView((0, js_crypto_1.getRandomBytes)(12).buffer).getUint32(0));
713
+ const credential = await this.getAuthBJJCredential(did, oldTreeState, {
714
+ nonce: opts.revocationOpts.nonce,
715
+ seed: opts.seed,
716
+ id: opts.revocationOpts.id,
717
+ type: opts.revocationOpts.type
724
718
  });
725
- const credRefreshed = await this._credentialWallet.findById(credential.id);
726
- if (!credRefreshed) {
727
- throw new Error('Credential not found in credential wallet');
719
+ const addMtpToCredAndPublishRevState = async () => {
720
+ const { receipt, block } = await this._transactionService.getTransactionReceiptAndBlock(txId);
721
+ const credsWithIden3MTPProof = await this.generateIden3SparseMerkleTreeProof(did, [credential], txId, receipt?.blockNumber, block?.timestamp, undefined, {
722
+ revNonce: opts.revocationOpts.nonce ?? 0,
723
+ subjectPosition: verifiable_1.SubjectPosition.None,
724
+ merklizedRootPosition: verifiable_1.MerklizedRootPosition.None,
725
+ updatable: false,
726
+ version: 0,
727
+ merklizeOpts: { documentLoader: (0, schema_processor_1.cacheLoader)() }
728
+ });
729
+ await this._credentialWallet.saveAll(credsWithIden3MTPProof);
730
+ await this.publishRevocationInfoByCredentialStatusType(did, opts.revocationOpts.type, {
731
+ rhsUrl: opts.revocationOpts.id,
732
+ onChain: opts.revocationOpts.onChain
733
+ });
734
+ return credsWithIden3MTPProof[0];
735
+ };
736
+ let txId = '';
737
+ let attempt = 2;
738
+ do {
739
+ try {
740
+ txId = await this.transitState(did, oldTreeState, isOldStateGenesis, ethSigner, prover);
741
+ break;
742
+ }
743
+ catch (err) {
744
+ console.warn(`Error while transiting state, retrying state transition, attempt: ${attempt}`, err);
745
+ }
746
+ } while (--attempt);
747
+ if (!txId) {
748
+ const oldTransitStateInfoJson = JSON.stringify({
749
+ claimsRoot: oldTreeState.claimsRoot.hex(),
750
+ revocationRoot: oldTreeState.revocationRoot.hex(),
751
+ rootOfRoots: oldTreeState.rootOfRoots.hex(),
752
+ state: oldTreeState.state.hex(),
753
+ isOldStateGenesis,
754
+ credentialId: credential.id,
755
+ did: did.string()
756
+ }, null, 2);
757
+ await this._credentialWallet.save(credential);
758
+ throw new Error(`Error publishing state, info to publish: ${oldTransitStateInfoJson}`);
728
759
  }
729
- credential = credRefreshed;
730
- await this.publishRevocationInfoByCredentialStatusType(did, opts.revocationOpts.type, {
731
- rhsUrl: opts.revocationOpts.id,
732
- onChain: opts.revocationOpts.onChain
733
- });
734
- return credential;
760
+ return addMtpToCredAndPublishRevState();
735
761
  }
736
762
  }
737
763
  exports.IdentityWallet = IdentityWallet;
@@ -185,7 +185,7 @@ class EthStateStorage {
185
185
  const chainId = (0, js_iden3_core_1.getChainId)(js_iden3_core_1.DID.blockchainFromId(idTyped), js_iden3_core_1.DID.networkIdFromId(idTyped));
186
186
  const config = this.networkByChainId(chainId);
187
187
  const provider = new ethers_1.JsonRpcProvider(config.url);
188
- const stateContract = new ethers_1.Contract(config.contractAddress, State_json_1.default, this.provider);
188
+ const stateContract = new ethers_1.Contract(config.contractAddress, State_json_1.default, provider);
189
189
  return { stateContract, provider };
190
190
  }
191
191
  networkByChainId(chainId) {
@@ -4,43 +4,43 @@ const IDEN3_PROTOCOL = 'https://iden3-communication.io/';
4
4
  */
5
5
  export const PROTOCOL_MESSAGE_TYPE = Object.freeze({
6
6
  // AuthorizationV2RequestMessageType defines auth request type of the communication protocol
7
- AUTHORIZATION_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'authorization/1.0/request',
7
+ AUTHORIZATION_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}authorization/1.0/request`,
8
8
  // AuthorizationResponseMessageType defines auth response type of the communication protocol
9
- AUTHORIZATION_RESPONSE_MESSAGE_TYPE: IDEN3_PROTOCOL + 'authorization/1.0/response',
9
+ AUTHORIZATION_RESPONSE_MESSAGE_TYPE: `${IDEN3_PROTOCOL}authorization/1.0/response`,
10
10
  // CredentialIssuanceRequestMessageType accepts request for credential creation
11
- CREDENTIAL_ISSUANCE_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/1.0/issuance-request',
11
+ CREDENTIAL_ISSUANCE_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/1.0/issuance-request`,
12
12
  // CredentialFetchRequestMessageType is type for request of credential generation
13
- CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/1.0/fetch-request',
13
+ CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/1.0/fetch-request`,
14
14
  // CredentialOfferMessageType is type of message with credential offering
15
- CREDENTIAL_OFFER_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/1.0/offer',
15
+ CREDENTIAL_OFFER_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/1.0/offer`,
16
16
  // CredentialIssuanceResponseMessageType is type for message with a credential issuance
17
- CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/1.0/issuance-response',
17
+ CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/1.0/issuance-response`,
18
18
  // CredentialRefreshMessageType is type for message with a credential issuance
19
- CREDENTIAL_REFRESH_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/1.0/refresh',
19
+ CREDENTIAL_REFRESH_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/1.0/refresh`,
20
20
  // DeviceRegistrationRequestMessageType defines device registration request type of the communication protocol
21
- DEVICE_REGISTRATION_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'devices/1.0/registration',
21
+ DEVICE_REGISTRATION_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}devices/1.0/registration`,
22
22
  // MessageFetMessageFetchRequestMessageType defines message fetch request type of the communication protocol.
23
- MESSAGE_FETCH_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'messages/1.0/fetch',
23
+ MESSAGE_FETCH_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}messages/1.0/fetch`,
24
24
  // ProofGenerationRequestMessageType is type for request of proof generation
25
- PROOF_GENERATION_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'proofs/1.0/request',
25
+ PROOF_GENERATION_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}proofs/1.0/request`,
26
26
  // ProofGenerationResponseMessageType is type for response of proof generation
27
- PROOF_GENERATION_RESPONSE_MESSAGE_TYPE: IDEN3_PROTOCOL + 'proofs/1.0/response',
27
+ PROOF_GENERATION_RESPONSE_MESSAGE_TYPE: `${IDEN3_PROTOCOL}proofs/1.0/response`,
28
28
  // RevocationStatusRequestMessageType is type for request of revocation status
29
- REVOCATION_STATUS_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'revocation/1.0/request-status',
29
+ REVOCATION_STATUS_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}revocation/1.0/request-status`,
30
30
  // RevocationStatusResponseMessageType is type for response with a revocation status
31
- REVOCATION_STATUS_RESPONSE_MESSAGE_TYPE: IDEN3_PROTOCOL + 'revocation/1.0/status',
31
+ REVOCATION_STATUS_RESPONSE_MESSAGE_TYPE: `${IDEN3_PROTOCOL}revocation/1.0/status`,
32
32
  // ContractInvokeRequestMessageType is type for request of contract invoke request
33
- CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'proofs/1.0/contract-invoke-request',
33
+ CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}proofs/1.0/contract-invoke-request`,
34
34
  // CredentialOnchainOfferMessageType is type of message with credential onchain offering
35
- CREDENTIAL_ONCHAIN_OFFER_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/1.0/onchain-offer',
35
+ CREDENTIAL_ONCHAIN_OFFER_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/1.0/onchain-offer`,
36
36
  // ProposalRequestMessageType is type for proposal-request message
37
- PROPOSAL_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal-request',
37
+ PROPOSAL_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/0.1/proposal-request`,
38
38
  // ProposalMessageType is type for proposal message
39
- PROPOSAL_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal',
39
+ PROPOSAL_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/0.1/proposal`,
40
40
  // PaymentRequestMessageType is type for payment-request message
41
- PAYMENT_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/payment-request',
41
+ PAYMENT_REQUEST_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/0.1/payment-request`,
42
42
  // PaymentMessageType is type for payment message
43
- PAYMENT_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/payment'
43
+ PAYMENT_MESSAGE_TYPE: `${IDEN3_PROTOCOL}credentials/0.1/payment`
44
44
  });
45
45
  /**
46
46
  * Media types for iden3 comm communication protocol
@@ -659,16 +659,8 @@ export class IdentityWallet {
659
659
  await this.updateIdentityState(did, true, newTreeState);
660
660
  return txId;
661
661
  }
662
- /** {@inheritdoc IIdentityWallet.addBJJAuthCredential} */
663
- async addBJJAuthCredential(did, oldTreeState, isOldStateGenesis, ethSigner, opts, prover // it will be needed in case of non ethereum identities
664
- ) {
665
- opts.seed = opts.seed ?? getRandomBytes(32);
666
- opts.revocationOpts.nonce =
667
- opts.revocationOpts.nonce ??
668
- (isOldStateGenesis
669
- ? 0
670
- : opts.revocationOpts.nonce ?? new DataView(getRandomBytes(12).buffer).getUint32(0));
671
- const { authClaim, pubKey } = await this.createAuthCoreClaim(opts.revocationOpts.nonce ?? 0, opts.seed);
662
+ async getAuthBJJCredential(did, oldTreeState, { nonce, seed, id, type }) {
663
+ const { authClaim, pubKey } = await this.createAuthCoreClaim(nonce, seed);
672
664
  const { hi, hv } = authClaim.hiHv();
673
665
  await this._storage.mt.addToMerkleTree(did.string(), MerkleTreeType.Claims, hi, hv);
674
666
  // Calculate current state after adding credential to merkle tree
@@ -678,33 +670,67 @@ export class IdentityWallet {
678
670
  oldTreeState.revocationRoot.bigInt(),
679
671
  oldTreeState.rootOfRoots.bigInt()
680
672
  ]);
681
- let credential = await this.createAuthBJJCredential(did, pubKey, authClaim, currentState, opts.revocationOpts);
682
- const txId = await this.transitState(did, oldTreeState, isOldStateGenesis, ethSigner, prover);
683
- const { receipt, block } = await this._transactionService.getTransactionReceiptAndBlock(txId);
684
- const credsWithIden3MTPProof = await this.generateIden3SparseMerkleTreeProof(did, [credential], txId, receipt?.blockNumber, block?.timestamp, undefined, {
685
- revNonce: Number(authClaim.getRevocationNonce()),
686
- subjectPosition: SubjectPosition.None,
687
- merklizedRootPosition: MerklizedRootPosition.None,
688
- updatable: false,
689
- version: 0,
690
- merklizeOpts: { documentLoader: cacheLoader() }
673
+ return this.createAuthBJJCredential(did, pubKey, authClaim, currentState, {
674
+ id,
675
+ type
691
676
  });
692
- await this._credentialWallet.saveAll(credsWithIden3MTPProof);
693
- await this._storage.identity.saveIdentity({
694
- did: did.string(),
695
- state: currentState,
696
- isStatePublished: true,
697
- isStateGenesis: false
677
+ }
678
+ /** {@inheritdoc IIdentityWallet.addBJJAuthCredential} */
679
+ async addBJJAuthCredential(did, oldTreeState, isOldStateGenesis, ethSigner, opts, prover // it will be needed in case of non ethereum identities
680
+ ) {
681
+ opts.seed = opts.seed ?? getRandomBytes(32);
682
+ opts.revocationOpts.nonce =
683
+ opts.revocationOpts.nonce ??
684
+ (isOldStateGenesis
685
+ ? 0
686
+ : opts.revocationOpts.nonce ?? new DataView(getRandomBytes(12).buffer).getUint32(0));
687
+ const credential = await this.getAuthBJJCredential(did, oldTreeState, {
688
+ nonce: opts.revocationOpts.nonce,
689
+ seed: opts.seed,
690
+ id: opts.revocationOpts.id,
691
+ type: opts.revocationOpts.type
698
692
  });
699
- const credRefreshed = await this._credentialWallet.findById(credential.id);
700
- if (!credRefreshed) {
701
- throw new Error('Credential not found in credential wallet');
693
+ const addMtpToCredAndPublishRevState = async () => {
694
+ const { receipt, block } = await this._transactionService.getTransactionReceiptAndBlock(txId);
695
+ const credsWithIden3MTPProof = await this.generateIden3SparseMerkleTreeProof(did, [credential], txId, receipt?.blockNumber, block?.timestamp, undefined, {
696
+ revNonce: opts.revocationOpts.nonce ?? 0,
697
+ subjectPosition: SubjectPosition.None,
698
+ merklizedRootPosition: MerklizedRootPosition.None,
699
+ updatable: false,
700
+ version: 0,
701
+ merklizeOpts: { documentLoader: cacheLoader() }
702
+ });
703
+ await this._credentialWallet.saveAll(credsWithIden3MTPProof);
704
+ await this.publishRevocationInfoByCredentialStatusType(did, opts.revocationOpts.type, {
705
+ rhsUrl: opts.revocationOpts.id,
706
+ onChain: opts.revocationOpts.onChain
707
+ });
708
+ return credsWithIden3MTPProof[0];
709
+ };
710
+ let txId = '';
711
+ let attempt = 2;
712
+ do {
713
+ try {
714
+ txId = await this.transitState(did, oldTreeState, isOldStateGenesis, ethSigner, prover);
715
+ break;
716
+ }
717
+ catch (err) {
718
+ console.warn(`Error while transiting state, retrying state transition, attempt: ${attempt}`, err);
719
+ }
720
+ } while (--attempt);
721
+ if (!txId) {
722
+ const oldTransitStateInfoJson = JSON.stringify({
723
+ claimsRoot: oldTreeState.claimsRoot.hex(),
724
+ revocationRoot: oldTreeState.revocationRoot.hex(),
725
+ rootOfRoots: oldTreeState.rootOfRoots.hex(),
726
+ state: oldTreeState.state.hex(),
727
+ isOldStateGenesis,
728
+ credentialId: credential.id,
729
+ did: did.string()
730
+ }, null, 2);
731
+ await this._credentialWallet.save(credential);
732
+ throw new Error(`Error publishing state, info to publish: ${oldTransitStateInfoJson}`);
702
733
  }
703
- credential = credRefreshed;
704
- await this.publishRevocationInfoByCredentialStatusType(did, opts.revocationOpts.type, {
705
- rhsUrl: opts.revocationOpts.id,
706
- onChain: opts.revocationOpts.onChain
707
- });
708
- return credential;
734
+ return addMtpToCredAndPublishRevState();
709
735
  }
710
736
  }
@@ -179,7 +179,7 @@ export class EthStateStorage {
179
179
  const chainId = getChainId(DID.blockchainFromId(idTyped), DID.networkIdFromId(idTyped));
180
180
  const config = this.networkByChainId(chainId);
181
181
  const provider = new JsonRpcProvider(config.url);
182
- const stateContract = new Contract(config.contractAddress, abi, this.provider);
182
+ const stateContract = new Contract(config.contractAddress, abi, provider);
183
183
  return { stateContract, provider };
184
184
  }
185
185
  networkByChainId(chainId) {
@@ -2,25 +2,25 @@
2
2
  * Constants for Iden3 protocol
3
3
  */
4
4
  export declare const PROTOCOL_MESSAGE_TYPE: Readonly<{
5
- AUTHORIZATION_REQUEST_MESSAGE_TYPE: string;
6
- AUTHORIZATION_RESPONSE_MESSAGE_TYPE: string;
7
- CREDENTIAL_ISSUANCE_REQUEST_MESSAGE_TYPE: string;
8
- CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE: string;
9
- CREDENTIAL_OFFER_MESSAGE_TYPE: string;
10
- CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE: string;
11
- CREDENTIAL_REFRESH_MESSAGE_TYPE: string;
12
- DEVICE_REGISTRATION_REQUEST_MESSAGE_TYPE: string;
13
- MESSAGE_FETCH_REQUEST_MESSAGE_TYPE: string;
14
- PROOF_GENERATION_REQUEST_MESSAGE_TYPE: string;
15
- PROOF_GENERATION_RESPONSE_MESSAGE_TYPE: string;
16
- REVOCATION_STATUS_REQUEST_MESSAGE_TYPE: string;
17
- REVOCATION_STATUS_RESPONSE_MESSAGE_TYPE: string;
18
- CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE: string;
19
- CREDENTIAL_ONCHAIN_OFFER_MESSAGE_TYPE: string;
20
- PROPOSAL_REQUEST_MESSAGE_TYPE: string;
21
- PROPOSAL_MESSAGE_TYPE: string;
22
- PAYMENT_REQUEST_MESSAGE_TYPE: string;
23
- PAYMENT_MESSAGE_TYPE: string;
5
+ AUTHORIZATION_REQUEST_MESSAGE_TYPE: "https://iden3-communication.io/authorization/1.0/request";
6
+ AUTHORIZATION_RESPONSE_MESSAGE_TYPE: "https://iden3-communication.io/authorization/1.0/response";
7
+ CREDENTIAL_ISSUANCE_REQUEST_MESSAGE_TYPE: "https://iden3-communication.io/credentials/1.0/issuance-request";
8
+ CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE: "https://iden3-communication.io/credentials/1.0/fetch-request";
9
+ CREDENTIAL_OFFER_MESSAGE_TYPE: "https://iden3-communication.io/credentials/1.0/offer";
10
+ CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE: "https://iden3-communication.io/credentials/1.0/issuance-response";
11
+ CREDENTIAL_REFRESH_MESSAGE_TYPE: "https://iden3-communication.io/credentials/1.0/refresh";
12
+ DEVICE_REGISTRATION_REQUEST_MESSAGE_TYPE: "https://iden3-communication.io/devices/1.0/registration";
13
+ MESSAGE_FETCH_REQUEST_MESSAGE_TYPE: "https://iden3-communication.io/messages/1.0/fetch";
14
+ PROOF_GENERATION_REQUEST_MESSAGE_TYPE: "https://iden3-communication.io/proofs/1.0/request";
15
+ PROOF_GENERATION_RESPONSE_MESSAGE_TYPE: "https://iden3-communication.io/proofs/1.0/response";
16
+ REVOCATION_STATUS_REQUEST_MESSAGE_TYPE: "https://iden3-communication.io/revocation/1.0/request-status";
17
+ REVOCATION_STATUS_RESPONSE_MESSAGE_TYPE: "https://iden3-communication.io/revocation/1.0/status";
18
+ CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE: "https://iden3-communication.io/proofs/1.0/contract-invoke-request";
19
+ CREDENTIAL_ONCHAIN_OFFER_MESSAGE_TYPE: "https://iden3-communication.io/credentials/1.0/onchain-offer";
20
+ PROPOSAL_REQUEST_MESSAGE_TYPE: "https://iden3-communication.io/credentials/0.1/proposal-request";
21
+ PROPOSAL_MESSAGE_TYPE: "https://iden3-communication.io/credentials/0.1/proposal";
22
+ PAYMENT_REQUEST_MESSAGE_TYPE: "https://iden3-communication.io/credentials/0.1/payment-request";
23
+ PAYMENT_MESSAGE_TYPE: "https://iden3-communication.io/credentials/0.1/payment";
24
24
  }>;
25
25
  /**
26
26
  * Media types for iden3 comm communication protocol
@@ -2,13 +2,13 @@ import { DID } from '@iden3/js-iden3-core';
2
2
  import { DataPrepareHandlerFunc, VerificationHandlerFunc } from '../packers';
3
3
  import { ProvingMethodAlg } from '@iden3/js-jwz';
4
4
  import { CircuitId } from '../../circuits';
5
- import { MediaType } from '../constants';
5
+ import { MediaType, PROTOCOL_MESSAGE_TYPE } from '../constants';
6
6
  import { DIDDocument, VerificationMethod } from 'did-resolver';
7
7
  import { StateVerificationOpts } from './models';
8
8
  /**
9
9
  * Protocol message type
10
10
  */
11
- export type ProtocolMessage = string;
11
+ export type ProtocolMessage = (typeof PROTOCOL_MESSAGE_TYPE)[keyof typeof PROTOCOL_MESSAGE_TYPE];
12
12
  /**
13
13
  * JSONValue
14
14
  */
@@ -1,10 +1,12 @@
1
1
  import { ZKProof } from '@iden3/js-jwz';
2
2
  import { BasicMessage, JSONObject } from '../packer';
3
+ import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
3
4
  /** AuthorizationResponseMessage is struct the represents iden3message authorization response */
4
5
  export type AuthorizationResponseMessage = BasicMessage & {
5
6
  body: AuthorizationMessageResponseBody;
6
7
  from: string;
7
8
  to: string;
9
+ type: typeof PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_RESPONSE_MESSAGE_TYPE;
8
10
  };
9
11
  /** AuthorizationMessageResponseBody is struct the represents authorization response data */
10
12
  export type AuthorizationMessageResponseBody = {
@@ -16,6 +18,7 @@ export type AuthorizationMessageResponseBody = {
16
18
  export type AuthorizationRequestMessage = BasicMessage & {
17
19
  body: AuthorizationRequestMessageBody;
18
20
  from: string;
21
+ type: typeof PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_REQUEST_MESSAGE_TYPE;
19
22
  };
20
23
  /** AuthorizationRequestMessageBody is body for authorization request */
21
24
  export type AuthorizationRequestMessageBody = {
@@ -1,8 +1,10 @@
1
+ import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
1
2
  import { BasicMessage } from '../packer';
2
3
  import { ZeroKnowledgeProofRequest } from './auth';
3
4
  /** ContractInvokeRequest represents structure of contract invoke request object */
4
5
  export type ContractInvokeRequest = BasicMessage & {
5
6
  body: ContractInvokeRequestBody;
7
+ type: typeof PROTOCOL_MESSAGE_TYPE.CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE;
6
8
  };
7
9
  /** ContractInvokeRequestBody represents structure of contract invoke request body object */
8
10
  export type ContractInvokeRequestBody = {
@@ -1,4 +1,5 @@
1
1
  import { W3CCredential } from '../../../verifiable';
2
+ import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
2
3
  import { BasicMessage, JSONObject } from '../packer';
3
4
  import { ContractInvokeTransactionData } from './contract-request';
4
5
  /** CredentialIssuanceRequestMessageBody represents data for credential issuance request */
@@ -10,19 +11,22 @@ export type CredentialIssuanceRequestMessageBody = {
10
11
  /** CredentialIssuanceRequestMessage represent Iden3message for credential request */
11
12
  export type CredentialIssuanceRequestMessage = Required<BasicMessage> & {
12
13
  body: CredentialIssuanceRequestMessageBody;
14
+ type: typeof PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ISSUANCE_REQUEST_MESSAGE_TYPE;
13
15
  };
14
16
  /** CredentialsOfferMessage represent Iden3message for credential offer */
15
17
  export type CredentialsOfferMessage = Required<BasicMessage> & {
16
18
  body: CredentialsOfferMessageBody;
19
+ type: typeof PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE;
17
20
  };
18
21
  /** CredentialsOfferMessageBody is struct the represents offer message */
19
22
  export type CredentialsOfferMessageBody = {
20
23
  url: string;
21
24
  credentials: CredentialOffer[];
22
25
  };
23
- /** CredentialsOnchainOfferMessage represent Iden3message for credential onhcain offer message */
26
+ /** CredentialsOnchainOfferMessage represent Iden3message for credential onchain offer message */
24
27
  export type CredentialsOnchainOfferMessage = Required<BasicMessage> & {
25
28
  body: CredentialsOnchainOfferMessageBody;
29
+ type: typeof PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ONCHAIN_OFFER_MESSAGE_TYPE;
26
30
  };
27
31
  /** CredentialsOnchainOfferMessageBody is struct the represents onchain offer message body */
28
32
  export type CredentialsOnchainOfferMessageBody = {
@@ -44,6 +48,7 @@ export type CredentialOffer = {
44
48
  /** CredentialIssuanceMessage represent Iden3message for credential issuance */
45
49
  export type CredentialIssuanceMessage = Required<BasicMessage> & {
46
50
  body: IssuanceMessageBody;
51
+ type: typeof PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE;
47
52
  };
48
53
  /** IssuanceMessageBody is struct the represents message when credential is issued */
49
54
  export type IssuanceMessageBody = {
@@ -52,6 +57,7 @@ export type IssuanceMessageBody = {
52
57
  /** CredentialFetchRequestMessage represent Iden3message for credential fetch request */
53
58
  export type CredentialFetchRequestMessage = BasicMessage & {
54
59
  body?: CredentialFetchRequestMessageBody;
60
+ type: typeof PROTOCOL_MESSAGE_TYPE.CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE;
55
61
  };
56
62
  /** CredentialFetchRequestMessageBody is msg body for fetch request */
57
63
  export type CredentialFetchRequestMessageBody = {
@@ -66,6 +72,7 @@ export type Schema = {
66
72
  /** CredentialRefreshMessage represent Iden3message for credential refresh request */
67
73
  export type CredentialRefreshMessage = Required<BasicMessage> & {
68
74
  body?: CredentialRefreshMessageBody;
75
+ type: typeof PROTOCOL_MESSAGE_TYPE.CREDENTIAL_REFRESH_MESSAGE_TYPE;
69
76
  };
70
77
  /** CredentialRefreshMessageBody is msg body for refresh request */
71
78
  export type CredentialRefreshMessageBody = {
@@ -1,7 +1,9 @@
1
1
  import { BasicMessage } from '../';
2
+ import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
2
3
  /** MessageFetchRequestMessage represent Iden3message for message fetch request. */
3
4
  export type MessageFetchRequestMessage = Required<BasicMessage> & {
4
5
  body?: MessageFetchRequestMessageBody;
6
+ type: typeof PROTOCOL_MESSAGE_TYPE.CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE;
5
7
  };
6
8
  /** MessageFetchRequestMessageBody is struct the represents body for message fetch request. */
7
9
  export type MessageFetchRequestMessageBody = {
@@ -1,8 +1,10 @@
1
1
  import { BasicMessage } from '../';
2
2
  import { PaymentRequestDataType, PaymentRequestType, PaymentType } from '../../../verifiable';
3
+ import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
3
4
  /** @beta PaymentRequestMessage is struct the represents payment-request message */
4
5
  export type PaymentRequestMessage = BasicMessage & {
5
6
  body: PaymentRequestMessageBody;
7
+ type: typeof PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE;
6
8
  };
7
9
  /** @beta PaymentRequestMessageBody is struct the represents body for payment-request */
8
10
  export type PaymentRequestMessageBody = {
@@ -33,6 +35,7 @@ export type PaymentRequestDataInfo = {
33
35
  /** @beta PaymentMessage is struct the represents payment message */
34
36
  export type PaymentMessage = BasicMessage & {
35
37
  body: PaymentMessageBody;
38
+ type: typeof PROTOCOL_MESSAGE_TYPE.PAYMENT_MESSAGE_TYPE;
36
39
  };
37
40
  /** @beta PaymentMessageBody is struct the represents body for payment message */
38
41
  export type PaymentMessageBody = {
@@ -1,8 +1,10 @@
1
1
  import { BasicMessage } from '../';
2
+ import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
2
3
  import { ZeroKnowledgeProofRequest, ZeroKnowledgeProofResponse } from './auth';
3
4
  /** ProofGenerationRequestMessage is struct the represents body for proof generation request */
4
5
  export type ProofGenerationRequestMessage = Required<BasicMessage> & {
5
6
  body?: ProofGenerationRequestMessageBody;
7
+ type: typeof PROTOCOL_MESSAGE_TYPE.PROOF_GENERATION_REQUEST_MESSAGE_TYPE;
6
8
  };
7
9
  /** ProofGenerationRequestMessageBody is struct the represents body for proof generation request */
8
10
  export type ProofGenerationRequestMessageBody = {
@@ -11,6 +13,7 @@ export type ProofGenerationRequestMessageBody = {
11
13
  /** ProofGenerationResponseMessage is struct the represents body for proof generation request */
12
14
  export type ProofGenerationResponseMessage = Required<BasicMessage> & {
13
15
  body?: ResponseMessageBody;
16
+ type: typeof PROTOCOL_MESSAGE_TYPE.PROOF_GENERATION_RESPONSE_MESSAGE_TYPE;
14
17
  };
15
18
  /** ResponseMessageBody is struct the represents request for revocation status */
16
19
  export type ResponseMessageBody = {
@@ -1,7 +1,9 @@
1
1
  import { BasicMessage, JSONObject } from '../';
2
+ import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
2
3
  /** @beta ProposalRequestMessage is struct the represents proposal-request message */
3
4
  export type ProposalRequestMessage = BasicMessage & {
4
5
  body?: ProposalRequestMessageBody;
6
+ type: typeof PROTOCOL_MESSAGE_TYPE.PROPOSAL_REQUEST_MESSAGE_TYPE;
5
7
  };
6
8
  /** @beta ProposalRequestMessageBody is struct the represents body for proposal-request */
7
9
  export type ProposalRequestMessageBody = {
@@ -15,6 +17,7 @@ export type ProposalRequestMessageBody = {
15
17
  /** @beta ProposalMessage is struct the represents proposal message */
16
18
  export type ProposalMessage = BasicMessage & {
17
19
  body?: ProposalMessageBody;
20
+ type: typeof PROTOCOL_MESSAGE_TYPE.PROPOSAL_MESSAGE_TYPE;
18
21
  };
19
22
  /** @beta ProposalMessageBody is struct the represents body for proposal message */
20
23
  export type ProposalMessageBody = {
@@ -1,8 +1,10 @@
1
1
  import { BasicMessage } from '../';
2
2
  import { RevocationStatus } from '../../../verifiable';
3
+ import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
3
4
  /** RevocationStatusRequestMessage is struct the represents body for proof generation request */
4
5
  export type RevocationStatusRequestMessage = BasicMessage & {
5
6
  body?: RevocationStatusRequestMessageBody;
7
+ type: typeof PROTOCOL_MESSAGE_TYPE.REVOCATION_STATUS_REQUEST_MESSAGE_TYPE;
6
8
  };
7
9
  /** RevocationStatusRequestMessageBody is struct the represents request for revocation status */
8
10
  export type RevocationStatusRequestMessageBody = {
@@ -11,6 +13,7 @@ export type RevocationStatusRequestMessageBody = {
11
13
  /** RevocationStatusResponseMessage is struct the represents body for proof generation request */
12
14
  export type RevocationStatusResponseMessage = Required<BasicMessage> & {
13
15
  body?: RevocationStatusResponseMessageBody;
16
+ type: typeof PROTOCOL_MESSAGE_TYPE.REVOCATION_STATUS_RESPONSE_MESSAGE_TYPE;
14
17
  };
15
18
  /** RevocationStatusResponseMessageBody is struct the represents request for revocation status */
16
19
  export type RevocationStatusResponseMessageBody = RevocationStatus;
@@ -444,6 +444,7 @@ export declare class IdentityWallet implements IIdentityWallet {
444
444
  updateIdentityState(issuerDID: DID, published: boolean, treeState?: TreeState): Promise<void>;
445
445
  /** {@inheritdoc IIdentityWallet.transitState} */
446
446
  transitState(did: DID, oldTreeState: TreeState, isOldStateGenesis: boolean, ethSigner: Signer, prover?: IZKProver): Promise<string>;
447
+ private getAuthBJJCredential;
447
448
  /** {@inheritdoc IIdentityWallet.addBJJAuthCredential} */
448
449
  addBJJAuthCredential(did: DID, oldTreeState: TreeState, isOldStateGenesis: boolean, ethSigner: Signer, opts: AuthBJJCredentialCreationOptions, prover?: IZKProver): Promise<W3CCredential>;
449
450
  }