@0xpolygonid/js-sdk 1.10.4 → 1.11.1

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.
Files changed (105) hide show
  1. package/README.md +11 -0
  2. package/dist/browser/esm/index.js +8360 -17398
  3. package/dist/browser/esm/index.js.map +1 -1
  4. package/dist/browser/umd/index.js +8383 -17416
  5. package/dist/browser/umd/index.js.map +1 -1
  6. package/dist/node/cjs/blockchain/index.js +17 -0
  7. package/dist/node/cjs/blockchain/transaction-service.js +55 -0
  8. package/dist/node/cjs/credentials/credential-wallet.js +1 -1
  9. package/dist/node/cjs/credentials/status/reverse-sparse-merkle-tree.js +5 -1
  10. package/dist/node/cjs/iden3comm/handlers/auth.js +62 -27
  11. package/dist/node/cjs/iden3comm/handlers/common.js +4 -4
  12. package/dist/node/cjs/iden3comm/handlers/contract-request.js +34 -15
  13. package/dist/node/cjs/iden3comm/handlers/credential-proposal.js +33 -14
  14. package/dist/node/cjs/iden3comm/handlers/fetch.js +65 -34
  15. package/dist/node/cjs/iden3comm/handlers/index.js +1 -0
  16. package/dist/node/cjs/iden3comm/handlers/message-handler.js +96 -0
  17. package/dist/node/cjs/iden3comm/handlers/revocation-status.js +58 -30
  18. package/dist/node/cjs/iden3comm/packageManager.js +16 -0
  19. package/dist/node/cjs/iden3comm/packers/jws.js +63 -49
  20. package/dist/node/cjs/iden3comm/packers/plain.js +13 -3
  21. package/dist/node/cjs/iden3comm/packers/zkp.js +9 -0
  22. package/dist/node/cjs/iden3comm/utils/did.js +11 -11
  23. package/dist/node/cjs/identity/identity-wallet.js +228 -43
  24. package/dist/node/cjs/index.js +1 -0
  25. package/dist/node/cjs/kms/key-providers/bjj-provider.js +9 -0
  26. package/dist/node/cjs/kms/key-providers/ed25519-provider.js +14 -3
  27. package/dist/node/cjs/kms/key-providers/index.js +1 -1
  28. package/dist/node/cjs/kms/key-providers/{sec256k1-provider.js → secp256k1-provider.js} +37 -26
  29. package/dist/node/cjs/kms/kms.js +15 -0
  30. package/dist/node/cjs/proof/proof-service.js +3 -33
  31. package/dist/node/cjs/proof/provers/inputs-generator.js +2 -7
  32. package/dist/node/cjs/storage/blockchain/onchain-revocation.js +4 -12
  33. package/dist/node/cjs/storage/blockchain/onchain-zkp-verifier.js +3 -10
  34. package/dist/node/cjs/storage/blockchain/state.js +39 -10
  35. package/dist/node/cjs/utils/did-helper.js +36 -2
  36. package/dist/node/cjs/verifiable/constants.js +2 -1
  37. package/dist/node/esm/blockchain/index.js +1 -0
  38. package/dist/node/esm/blockchain/transaction-service.js +51 -0
  39. package/dist/node/esm/credentials/credential-wallet.js +1 -1
  40. package/dist/node/esm/credentials/status/reverse-sparse-merkle-tree.js +6 -2
  41. package/dist/node/esm/iden3comm/handlers/auth.js +62 -27
  42. package/dist/node/esm/iden3comm/handlers/common.js +4 -4
  43. package/dist/node/esm/iden3comm/handlers/contract-request.js +34 -15
  44. package/dist/node/esm/iden3comm/handlers/credential-proposal.js +33 -14
  45. package/dist/node/esm/iden3comm/handlers/fetch.js +65 -34
  46. package/dist/node/esm/iden3comm/handlers/index.js +1 -0
  47. package/dist/node/esm/iden3comm/handlers/message-handler.js +91 -0
  48. package/dist/node/esm/iden3comm/handlers/revocation-status.js +58 -30
  49. package/dist/node/esm/iden3comm/packageManager.js +16 -0
  50. package/dist/node/esm/iden3comm/packers/jws.js +63 -49
  51. package/dist/node/esm/iden3comm/packers/plain.js +13 -3
  52. package/dist/node/esm/iden3comm/packers/zkp.js +9 -0
  53. package/dist/node/esm/iden3comm/utils/did.js +11 -8
  54. package/dist/node/esm/identity/identity-wallet.js +230 -45
  55. package/dist/node/esm/index.js +1 -0
  56. package/dist/node/esm/kms/key-providers/bjj-provider.js +10 -1
  57. package/dist/node/esm/kms/key-providers/ed25519-provider.js +14 -3
  58. package/dist/node/esm/kms/key-providers/index.js +1 -1
  59. package/dist/node/esm/kms/key-providers/secp256k1-provider.js +85 -0
  60. package/dist/node/esm/kms/kms.js +15 -0
  61. package/dist/node/esm/proof/proof-service.js +4 -34
  62. package/dist/node/esm/proof/provers/inputs-generator.js +3 -8
  63. package/dist/node/esm/storage/blockchain/onchain-revocation.js +4 -12
  64. package/dist/node/esm/storage/blockchain/onchain-zkp-verifier.js +3 -10
  65. package/dist/node/esm/storage/blockchain/state.js +39 -10
  66. package/dist/node/esm/utils/did-helper.js +33 -1
  67. package/dist/node/esm/verifiable/constants.js +2 -1
  68. package/dist/types/blockchain/index.d.ts +1 -0
  69. package/dist/types/blockchain/transaction-service.d.ts +80 -0
  70. package/dist/types/iden3comm/handlers/auth.d.ts +17 -2
  71. package/dist/types/iden3comm/handlers/common.d.ts +2 -2
  72. package/dist/types/iden3comm/handlers/contract-request.d.ts +10 -2
  73. package/dist/types/iden3comm/handlers/credential-proposal.d.ts +5 -2
  74. package/dist/types/iden3comm/handlers/fetch.d.ts +15 -6
  75. package/dist/types/iden3comm/handlers/index.d.ts +1 -0
  76. package/dist/types/iden3comm/handlers/message-handler.d.ts +83 -0
  77. package/dist/types/iden3comm/handlers/revocation-status.d.ts +18 -2
  78. package/dist/types/iden3comm/packageManager.d.ts +10 -0
  79. package/dist/types/iden3comm/packers/jws.d.ts +9 -0
  80. package/dist/types/iden3comm/packers/plain.d.ts +10 -2
  81. package/dist/types/iden3comm/packers/zkp.d.ts +7 -0
  82. package/dist/types/iden3comm/types/packageManager.d.ts +10 -0
  83. package/dist/types/iden3comm/types/packer.d.ts +24 -2
  84. package/dist/types/iden3comm/types/protocol/auth.d.ts +5 -15
  85. package/dist/types/iden3comm/types/protocol/contract-request.d.ts +2 -6
  86. package/dist/types/iden3comm/types/protocol/credentials.d.ts +7 -44
  87. package/dist/types/iden3comm/types/protocol/messages.d.ts +2 -9
  88. package/dist/types/iden3comm/types/protocol/proof.d.ts +3 -16
  89. package/dist/types/iden3comm/types/protocol/revocation.d.ts +3 -16
  90. package/dist/types/identity/identity-wallet.d.ts +80 -7
  91. package/dist/types/index.d.ts +1 -0
  92. package/dist/types/kms/key-providers/bjj-provider.d.ts +1 -0
  93. package/dist/types/kms/key-providers/ed25519-provider.d.ts +10 -2
  94. package/dist/types/kms/key-providers/index.d.ts +1 -1
  95. package/dist/types/kms/key-providers/{sec256k1-provider.d.ts → secp256k1-provider.d.ts} +15 -9
  96. package/dist/types/kms/kms.d.ts +18 -0
  97. package/dist/types/proof/proof-service.d.ts +2 -2
  98. package/dist/types/storage/blockchain/onchain-revocation.d.ts +1 -0
  99. package/dist/types/storage/blockchain/state.d.ts +7 -2
  100. package/dist/types/storage/interfaces/state.d.ts +26 -2
  101. package/dist/types/utils/did-helper.d.ts +9 -1
  102. package/dist/types/verifiable/constants.d.ts +1 -0
  103. package/package.json +10 -12
  104. package/LICENSE +0 -691
  105. package/dist/node/esm/kms/key-providers/sec256k1-provider.js +0 -71
@@ -1,6 +1,7 @@
1
1
  import { Contract, JsonRpcProvider } from 'ethers';
2
2
  import { Proof, Hash } from '@iden3/js-merkletree';
3
3
  import abi from '../blockchain/abi/CredentialStatusResolver.json';
4
+ import { TransactionService } from '../../blockchain';
4
5
  /**
5
6
  * OnChainRevocationStore is a class that allows to interact with the onchain contract
6
7
  * and build the revocation status.
@@ -26,6 +27,7 @@ export class OnChainRevocationStorage {
26
27
  contract = contract.connect(this._signer);
27
28
  }
28
29
  this._contract = contract;
30
+ this._transactionService = new TransactionService(this._provider);
29
31
  }
30
32
  /**
31
33
  * Get revocation status by issuerId, issuerState and nonce from the onchain.
@@ -75,18 +77,8 @@ export class OnChainRevocationStorage {
75
77
  maxFeePerGas,
76
78
  maxPriorityFeePerGas
77
79
  };
78
- const tx = await this._signer.sendTransaction(request);
79
- return tx.wait().then((txReceipt) => {
80
- if (!txReceipt) {
81
- throw new Error(`transaction: ${tx.hash} failed to mine`);
82
- }
83
- const status = txReceipt.status;
84
- const txnHash = txReceipt.hash;
85
- if (!status) {
86
- throw new Error(`transaction: ${txnHash} failed to mine`);
87
- }
88
- return txReceipt;
89
- });
80
+ const { txnReceipt } = await this._transactionService.sendTransactionRequest(this._signer, request);
81
+ return txnReceipt;
90
82
  }
91
83
  static convertIssuerInfo(issuer) {
92
84
  const [state, claimsTreeRoot, revocationTreeRoot, rootOfRoots] = issuer.map((i) => Hash.fromBigInt(i).hex());
@@ -1,5 +1,6 @@
1
1
  import { JsonRpcProvider, Contract } from 'ethers';
2
2
  import abi from './abi/ZkpVerifier.json';
3
+ import { TransactionService } from '../../blockchain';
3
4
  /**
4
5
  * OnChainZKPVerifier is a class that allows to interact with the OnChainZKPVerifier contract
5
6
  * and submitZKPResponse.
@@ -73,16 +74,8 @@ export class OnChainZKPVerifier {
73
74
  maxFeePerGas,
74
75
  maxPriorityFeePerGas
75
76
  };
76
- const tx = await ethSigner.sendTransaction(request);
77
- const txnReceipt = await tx.wait();
78
- if (!txnReceipt) {
79
- throw new Error(`transaction: ${tx.hash} failed to mined`);
80
- }
81
- const status = txnReceipt.status;
82
- const txnHash = txnReceipt.hash;
83
- if (!status) {
84
- throw new Error(`transaction: ${txnHash} failed to mined`);
85
- }
77
+ const transactionService = new TransactionService(provider);
78
+ const { txnHash } = await transactionService.sendTransactionRequest(ethSigner, request);
86
79
  response.set(txnHash, zkProof);
87
80
  }
88
81
  return response;
@@ -3,6 +3,7 @@ import { StateTransitionPubSignals } from '../../circuits';
3
3
  import { byteEncoder } from '../../utils';
4
4
  import abi from './abi/State.json';
5
5
  import { DID, getChainId, Id } from '@iden3/js-iden3-core';
6
+ import { TransactionService } from '../../blockchain';
6
7
  export /** @type {EthConnectionConfig} - default configuration for EthConnectionConfig */ const defaultEthConnectionConfig = {
7
8
  url: 'http://localhost:8545',
8
9
  defaultGasLimit: 600000,
@@ -33,6 +34,7 @@ export class EthStateStorage {
33
34
  const config = Array.isArray(ethConfig) ? ethConfig[0] : ethConfig;
34
35
  this.provider = new JsonRpcProvider(config.url);
35
36
  this.stateContract = new Contract(config.contractAddress, abi, this.provider);
37
+ this._transactionService = new TransactionService(this.getRpcProvider());
36
38
  }
37
39
  /** {@inheritdoc IStateStorage.getLatestStateById} */
38
40
  async getLatestStateById(id) {
@@ -99,16 +101,39 @@ export class EthStateStorage {
99
101
  maxFeePerGas,
100
102
  maxPriorityFeePerGas
101
103
  };
102
- const tx = await signer.sendTransaction(request);
103
- const txnReceipt = await tx.wait();
104
- if (!txnReceipt) {
105
- throw new Error(`transaction: ${tx.hash} failed to mined`);
106
- }
107
- const status = txnReceipt.status;
108
- const txnHash = txnReceipt.hash;
109
- if (!status) {
110
- throw new Error(`transaction: ${txnHash} failed to mined`);
111
- }
104
+ const { txnHash } = await this._transactionService.sendTransactionRequest(signer, request);
105
+ return txnHash;
106
+ }
107
+ /** {@inheritdoc IStateStorage.publishStateGeneric} */
108
+ async publishStateGeneric(signer, userStateTransitionInfo) {
109
+ const { userId, oldUserState, newUserState, isOldStateGenesis, methodId, methodParams } = userStateTransitionInfo;
110
+ const { stateContract, provider } = this.getStateContractAndProviderForId(userId.bigInt());
111
+ const contract = stateContract.connect(signer);
112
+ const feeData = await provider.getFeeData();
113
+ const maxFeePerGas = defaultEthConnectionConfig.maxFeePerGas
114
+ ? BigInt(defaultEthConnectionConfig.maxFeePerGas)
115
+ : feeData.maxFeePerGas;
116
+ const maxPriorityFeePerGas = defaultEthConnectionConfig.maxPriorityFeePerGas
117
+ ? BigInt(defaultEthConnectionConfig.maxPriorityFeePerGas)
118
+ : feeData.maxPriorityFeePerGas;
119
+ const payload = [
120
+ userId.bigInt().toString(),
121
+ oldUserState.bigInt().toString(),
122
+ newUserState.bigInt().toString(),
123
+ isOldStateGenesis,
124
+ methodId,
125
+ methodParams //'0x'
126
+ ];
127
+ const gasLimit = await contract.transitStateGeneric.estimateGas(...payload);
128
+ const txData = await contract.transitStateGeneric.populateTransaction(...payload);
129
+ const request = {
130
+ to: txData.to,
131
+ data: txData.data,
132
+ gasLimit,
133
+ maxFeePerGas,
134
+ maxPriorityFeePerGas
135
+ };
136
+ const { txnHash } = await this._transactionService.sendTransactionRequest(signer, request);
112
137
  return txnHash;
113
138
  }
114
139
  /** {@inheritdoc IStateStorage.getGISTProof} */
@@ -139,6 +164,10 @@ export class EthStateStorage {
139
164
  replacedAtBlock: BigInt(data.replacedAtBlock.toString())
140
165
  };
141
166
  }
167
+ /** {@inheritdoc IStateStorage.getRpcProvider} */
168
+ getRpcProvider() {
169
+ return this.provider;
170
+ }
142
171
  getStateContractAndProviderForId(id) {
143
172
  const idTyped = Id.fromBigInt(id);
144
173
  if (!Array.isArray(this.ethConfig)) {
@@ -1,10 +1,12 @@
1
1
  import { Hex } from '@iden3/js-crypto';
2
2
  import { Id, buildDIDType, genesisFromEthAddress, DID } from '@iden3/js-iden3-core';
3
3
  import { Hash } from '@iden3/js-merkletree';
4
+ import { keccak256 } from 'js-sha3';
5
+ import { hexToBytes } from './encoding';
4
6
  /**
5
7
  * Checks if state is genesis state
6
8
  *
7
- * @param {string} did - did
9
+ * @param {DID} did - did
8
10
  * @param {bigint|string} state - hash on bigInt or hex string format
9
11
  * @returns boolean
10
12
  */
@@ -18,6 +20,24 @@ export function isGenesisState(did, state) {
18
20
  const idFromState = Id.idGenesisFromIdenState(type, state);
19
21
  return id.bigInt().toString() === idFromState.bigInt().toString();
20
22
  }
23
+ /**
24
+ * Checks if DID is an ethereum identity
25
+ *
26
+ * @param {DID} did - did
27
+ * @returns boolean
28
+ */
29
+ export function isEthereumIdentity(did) {
30
+ const issuerId = DID.idFromDID(did);
31
+ try {
32
+ Id.ethAddressFromId(issuerId);
33
+ // is an ethereum identity
34
+ return true;
35
+ }
36
+ catch {
37
+ // not an ethereum identity (BabyJubJub or other)
38
+ return false;
39
+ }
40
+ }
21
41
  export const buildVerifierId = (address, info) => {
22
42
  address = address.replace('0x', '');
23
43
  const ethAddrBytes = Hex.decodeString(address);
@@ -46,3 +66,15 @@ export const resolveDIDDocumentAuth = async (did, resolveURL, state) => {
46
66
  const didResolutionRes = (await resp.json());
47
67
  return didResolutionRes.didDocument?.verificationMethod?.find((i) => i.type === 'Iden3StateInfo2023');
48
68
  };
69
+ export const buildDIDFromEthPubKey = (didType, pubKeyEth) => {
70
+ // Use Keccak-256 hash function to get public key hash
71
+ const hashOfPublicKey = keccak256(hexToBytes(pubKeyEth));
72
+ // Convert hash to buffer
73
+ const ethAddressBuffer = hexToBytes(hashOfPublicKey);
74
+ // Ethereum Address is '0x' concatenated with last 20 bytes
75
+ // of the public key hash
76
+ const ethAddr = ethAddressBuffer.slice(-20);
77
+ const genesis = genesisFromEthAddress(ethAddr);
78
+ const identifier = new Id(didType, genesis);
79
+ return DID.parseFromId(identifier);
80
+ };
@@ -4,7 +4,8 @@ export const VerifiableConstants = Object.freeze({
4
4
  FiELD_IS_EMPTY: 'fieldPath is empty',
5
5
  CONTEXT_TYPE_IS_EMPTY: 'ctxType is empty',
6
6
  // ErrStateNotFound issuer state is genesis state.
7
- IDENTITY_DOES_NOT_EXIST: 'Identity does not exist'
7
+ IDENTITY_DOES_NOT_EXIST: 'Identity does not exist',
8
+ NO_AUTH_CRED_FOUND: 'no auth credentials found'
8
9
  },
9
10
  CREDENTIAL_TYPE: {
10
11
  // VerifiableCredential is a W3C verifiable credential type
@@ -0,0 +1 @@
1
+ export * from './transaction-service';
@@ -0,0 +1,80 @@
1
+ import { Block, JsonRpcProvider, Signer, TransactionReceipt, TransactionRequest } from 'ethers';
2
+ /**
3
+ * Resend transaction options
4
+ * @type ResendTxnOptions
5
+ */
6
+ export type ResendTxnOptions = {
7
+ increasedFeesPercentage?: number;
8
+ };
9
+ /**
10
+ * Interface for TransactionService
11
+ * @public
12
+ */
13
+ export interface ITransactionService {
14
+ /**
15
+ * Returns transaction receipt and block by transaction hash
16
+ *
17
+ * @param {string} transactionHash - transaction hash.
18
+ * @returns `Promise<{receipt?: TransactionReceipt , block?: Block}>` - returns transaction receipt and block
19
+ * @public
20
+ */
21
+ getTransactionReceiptAndBlock(transactionHash: string): Promise<{
22
+ receipt?: TransactionReceipt;
23
+ block?: Block;
24
+ }>;
25
+ /**
26
+ * Send transaction.
27
+ *
28
+ * @param {Signer} signer - transaction signer.
29
+ * @param {TransactionRequest} request - transaction request.
30
+ * @returns `Promise<txnHash: string, txnReceipt: TransactionReceipt` - returns txn hash and txn receipt.
31
+ * @public
32
+ */
33
+ sendTransactionRequest(signer: Signer, request: TransactionRequest): Promise<{
34
+ txnHash: string;
35
+ txnReceipt: TransactionReceipt;
36
+ }>;
37
+ /**
38
+ * Resend transaction with options. Useful when `transaction underpriced` error thrown on transaction.
39
+ *
40
+ * @param {Signer} signer - transaction signer.
41
+ * @param {TransactionRequest} request - transaction request.
42
+ * @param {ResendTxnOptions} opts - resend transaction options.
43
+ * @returns `Promise<{ txnHash: string; txnReceipt: TransactionReceipt }>` -returns txn hash and txn receipt.
44
+ * @public
45
+ */
46
+ resendTransaction(signer: Signer, request: TransactionRequest, opts?: ResendTxnOptions): Promise<{
47
+ txnHash: string;
48
+ txnReceipt: TransactionReceipt;
49
+ }>;
50
+ }
51
+ /**
52
+ * Transaction service to provide interaction with blockchain transactions.
53
+ * allows to: get tx receipt by tx id, send and resend transaction with new fees.
54
+ * @class TransactionService
55
+ * @public
56
+ * @implements ITransactionService interface
57
+ */
58
+ export declare class TransactionService implements ITransactionService {
59
+ private readonly _provider;
60
+ /**
61
+ * Creates an instance of TransactionService.
62
+ * @param {JsonRpcProvider} - RPC provider
63
+ */
64
+ constructor(_provider: JsonRpcProvider);
65
+ /** {@inheritDoc ITransactionService.getTransactionReceiptAndBlock} */
66
+ getTransactionReceiptAndBlock(txnHash: string): Promise<{
67
+ receipt?: TransactionReceipt;
68
+ block?: Block;
69
+ }>;
70
+ /** {@inheritDoc ITransactionService.sendTransactionRequest} */
71
+ sendTransactionRequest(signer: Signer, request: TransactionRequest): Promise<{
72
+ txnHash: string;
73
+ txnReceipt: TransactionReceipt;
74
+ }>;
75
+ /** {@inheritDoc ITransactionService.resendTransaction} */
76
+ resendTransaction(signer: Signer, request: TransactionRequest, opts?: ResendTxnOptions): Promise<{
77
+ txnHash: string;
78
+ txnReceipt: TransactionReceipt;
79
+ }>;
80
+ }
@@ -1,7 +1,8 @@
1
1
  import { MediaType } from '../constants';
2
2
  import { IProofService } from '../../proof/proof-service';
3
- import { AuthorizationRequestMessage, AuthorizationResponseMessage, IPackageManager, JWSPackerParams } from '../types';
3
+ import { AuthorizationRequestMessage, AuthorizationResponseMessage, BasicMessage, IPackageManager, JWSPackerParams } from '../types';
4
4
  import { DID } from '@iden3/js-iden3-core';
5
+ import { AbstractMessageHandler, IProtocolMessageHandler } from './message-handler';
5
6
  /**
6
7
  * createAuthorizationRequest is a function to create protocol authorization request
7
8
  * @param {string} reason - reason to request proof
@@ -76,6 +77,16 @@ export interface IAuthHandler {
76
77
  response: AuthorizationResponseMessage;
77
78
  }>;
78
79
  }
80
+ type AuthReqOptions = {
81
+ senderDid: DID;
82
+ mediaType?: MediaType;
83
+ };
84
+ type AuthRespOptions = {
85
+ request: AuthorizationRequestMessage;
86
+ acceptedStateTransitionDelay?: number;
87
+ acceptedProofGenerationDelay?: number;
88
+ };
89
+ export type AuthMessageHandlerOptions = AuthReqOptions | AuthRespOptions;
79
90
  /**
80
91
  *
81
92
  * Options to pass to auth handler
@@ -96,7 +107,7 @@ export interface AuthHandlerOptions {
96
107
  * @class AuthHandler
97
108
  * @implements implements IAuthHandler interface
98
109
  */
99
- export declare class AuthHandler implements IAuthHandler {
110
+ export declare class AuthHandler extends AbstractMessageHandler implements IAuthHandler, IProtocolMessageHandler {
100
111
  private readonly _packerMgr;
101
112
  private readonly _proofService;
102
113
  private readonly _supportedCircuits;
@@ -107,10 +118,12 @@ export declare class AuthHandler implements IAuthHandler {
107
118
  *
108
119
  */
109
120
  constructor(_packerMgr: IPackageManager, _proofService: IProofService);
121
+ handle(message: BasicMessage, ctx: AuthMessageHandlerOptions): Promise<BasicMessage | null>;
110
122
  /**
111
123
  * @inheritdoc IAuthHandler#parseAuthorizationRequest
112
124
  */
113
125
  parseAuthorizationRequest(request: Uint8Array): Promise<AuthorizationRequestMessage>;
126
+ private handleAuthRequest;
114
127
  /**
115
128
  * @inheritdoc IAuthHandler#handleAuthorizationRequest
116
129
  */
@@ -119,6 +132,7 @@ export declare class AuthHandler implements IAuthHandler {
119
132
  authRequest: AuthorizationRequestMessage;
120
133
  authResponse: AuthorizationResponseMessage;
121
134
  }>;
135
+ private handleAuthResponse;
122
136
  /**
123
137
  * @inheritdoc IAuthHandler#handleAuthorizationResponse
124
138
  */
@@ -128,3 +142,4 @@ export declare class AuthHandler implements IAuthHandler {
128
142
  }>;
129
143
  private verifyAuthRequest;
130
144
  }
145
+ export {};
@@ -7,14 +7,14 @@ import { Signer } from 'ethers';
7
7
  /**
8
8
  * Processes zero knowledge proof requests.
9
9
  *
10
- * @param senderIdentifier - The identifier of the sender.
10
+ * @param to - The identifier of the recipient.
11
11
  * @param requests - An array of zero knowledge proof requests.
12
12
  * @param from - The identifier of the sender.
13
13
  * @param proofService - The proof service.
14
14
  * @param opts - Additional options for processing the requests.
15
15
  * @returns A promise that resolves to an array of zero knowledge proof responses.
16
16
  */
17
- export declare const processZeroKnowledgeProofRequests: (senderIdentifier: DID, requests: ZeroKnowledgeProofRequest[] | undefined, from: DID | undefined, proofService: IProofService, opts: {
17
+ export declare const processZeroKnowledgeProofRequests: (to: DID, requests: ZeroKnowledgeProofRequest[] | undefined, from: DID | undefined, proofService: IProofService, opts: {
18
18
  mediaType?: MediaType;
19
19
  packerOptions?: JWSPackerParams;
20
20
  supportedCircuits: CircuitId[];
@@ -1,9 +1,10 @@
1
1
  import { IProofService } from '../../proof/proof-service';
2
- import { IPackageManager, ZeroKnowledgeProofResponse } from '../types';
2
+ import { BasicMessage, IPackageManager, ZeroKnowledgeProofResponse } from '../types';
3
3
  import { ContractInvokeRequest } from '../types/protocol/contract-request';
4
4
  import { DID } from '@iden3/js-iden3-core';
5
5
  import { IOnChainZKPVerifier } from '../../storage';
6
6
  import { Signer } from 'ethers';
7
+ import { AbstractMessageHandler, IProtocolMessageHandler } from './message-handler';
7
8
  /**
8
9
  * Interface that allows the processing of the contract request
9
10
  *
@@ -33,6 +34,11 @@ export type ContractInvokeHandlerOptions = {
33
34
  ethSigner: Signer;
34
35
  challenge?: bigint;
35
36
  };
37
+ export type ContractMessageHandlerOptions = {
38
+ senderDid: DID;
39
+ ethSigner: Signer;
40
+ challenge?: bigint;
41
+ };
36
42
  /**
37
43
  *
38
44
  * Allows to process ContractInvokeRequest protocol message
@@ -42,7 +48,7 @@ export type ContractInvokeHandlerOptions = {
42
48
  * @class ContractRequestHandler
43
49
  * @implements implements IContractRequestHandler interface
44
50
  */
45
- export declare class ContractRequestHandler implements IContractRequestHandler {
51
+ export declare class ContractRequestHandler extends AbstractMessageHandler implements IContractRequestHandler, IProtocolMessageHandler {
46
52
  private readonly _packerMgr;
47
53
  private readonly _proofService;
48
54
  private readonly _zkpVerifier;
@@ -55,6 +61,8 @@ export declare class ContractRequestHandler implements IContractRequestHandler {
55
61
  *
56
62
  */
57
63
  constructor(_packerMgr: IPackageManager, _proofService: IProofService, _zkpVerifier: IOnChainZKPVerifier);
64
+ handle(message: BasicMessage, ctx: ContractMessageHandlerOptions): Promise<BasicMessage | null>;
65
+ private handleContractInvoke;
58
66
  /**
59
67
  * unpacks contract-invoke request
60
68
  * @beta
@@ -1,7 +1,8 @@
1
- import { IPackageManager, JSONObject, PackerParams } from '../types';
1
+ import { BasicMessage, IPackageManager, JSONObject, PackerParams } from '../types';
2
2
  import { DID } from '@iden3/js-iden3-core';
3
3
  import { Proposal, ProposalRequestCredential, ProposalRequestMessage, ProposalMessage } from '../types/protocol/proposal-request';
4
4
  import { IIdentityWallet } from '../../identity';
5
+ import { AbstractMessageHandler, IProtocolMessageHandler } from './message-handler';
5
6
  /** @beta ProposalRequestCreationOptions represents proposal-request creation options */
6
7
  export type ProposalRequestCreationOptions = {
7
8
  credentials: ProposalRequestCredential[];
@@ -83,7 +84,7 @@ export type CredentialProposalHandlerParams = {
83
84
  * @class CredentialProposalHandler
84
85
  * @implements implements ICredentialProposalHandler interface
85
86
  */
86
- export declare class CredentialProposalHandler implements ICredentialProposalHandler {
87
+ export declare class CredentialProposalHandler extends AbstractMessageHandler implements ICredentialProposalHandler, IProtocolMessageHandler {
87
88
  private readonly _packerMgr;
88
89
  private readonly _identityWallet;
89
90
  private readonly _params;
@@ -95,10 +96,12 @@ export declare class CredentialProposalHandler implements ICredentialProposalHan
95
96
  *
96
97
  */
97
98
  constructor(_packerMgr: IPackageManager, _identityWallet: IIdentityWallet, _params: CredentialProposalHandlerParams);
99
+ handle(message: BasicMessage, context: ProposalRequestHandlerOptions): Promise<BasicMessage | null>;
98
100
  /**
99
101
  * @inheritdoc ICredentialProposalHandler#parseProposalRequest
100
102
  */
101
103
  parseProposalRequest(request: Uint8Array): Promise<ProposalRequestMessage>;
104
+ private handleProposalRequestMessage;
102
105
  /**
103
106
  * @inheritdoc ICredentialProposalHandler#handleProposalRequest
104
107
  */
@@ -1,7 +1,8 @@
1
1
  import { MediaType } from '../constants';
2
- import { IPackageManager, JWSPackerParams } from '../types';
2
+ import { BasicMessage, IPackageManager, JWSPackerParams } from '../types';
3
3
  import { W3CCredential } from '../../verifiable';
4
4
  import { ICredentialWallet } from '../../credentials';
5
+ import { AbstractMessageHandler, IProtocolMessageHandler } from './message-handler';
5
6
  /**
6
7
  *
7
8
  * Options to pass to fetch handler
@@ -9,13 +10,14 @@ import { ICredentialWallet } from '../../credentials';
9
10
  * @public
10
11
  * @interface FetchHandlerOptions
11
12
  */
12
- export interface FetchHandlerOptions {
13
+ export type FetchHandlerOptions = {
13
14
  mediaType: MediaType;
14
15
  packerOptions?: JWSPackerParams;
15
16
  headers?: {
16
17
  [key: string]: string;
17
18
  };
18
- }
19
+ };
20
+ export type FetchMessageHandlerOptions = FetchHandlerOptions;
19
21
  /**
20
22
  * Interface that allows the processing of the credential offer in the raw format for given identifier
21
23
  *
@@ -61,16 +63,21 @@ export interface IFetchHandler {
61
63
  * @class FetchHandler
62
64
  * @implements implements IFetchHandler interface
63
65
  */
64
- export declare class FetchHandler implements IFetchHandler {
66
+ export declare class FetchHandler extends AbstractMessageHandler implements IFetchHandler, IProtocolMessageHandler {
65
67
  private readonly _packerMgr;
66
68
  private readonly opts?;
67
69
  /**
68
- * Creates an instance of FetchHandler.
69
- * @param {IPackageManager} _packerMgr - package manager to unpack message envelope
70
+ * Constructs a new instance of the FetchHandler class.
71
+ *
72
+ * @param _packerMgr The package manager used for packing and unpacking data.
73
+ * @param opts Optional configuration options for the FetchHandler.
74
+ * @param opts.credentialWallet The credential wallet used for managing credentials.
70
75
  */
71
76
  constructor(_packerMgr: IPackageManager, opts?: {
72
77
  credentialWallet: ICredentialWallet;
73
78
  } | undefined);
79
+ handle(message: BasicMessage, ctx: FetchMessageHandlerOptions): Promise<BasicMessage | null>;
80
+ private handleOfferMessage;
74
81
  /**
75
82
  * Handles only messages with credentials/1.0/offer type
76
83
  *
@@ -81,10 +88,12 @@ export declare class FetchHandler implements IFetchHandler {
81
88
  * @returns `Promise<W3CCredential[]>`
82
89
  */
83
90
  handleCredentialOffer(offer: Uint8Array, opts?: FetchHandlerOptions): Promise<W3CCredential[]>;
91
+ private handleFetchRequest;
84
92
  /**
85
93
  * @inheritdoc IFetchHandler#handleCredentialFetchRequest
86
94
  */
87
95
  handleCredentialFetchRequest(envelope: Uint8Array): Promise<Uint8Array>;
96
+ private handleIssuanceResponseMsg;
88
97
  /**
89
98
  * @inheritdoc IFetchHandler#handleIssuanceResponseMessage
90
99
  */
@@ -5,3 +5,4 @@ export * from './refresh';
5
5
  export * from './revocation-status';
6
6
  export * from './common';
7
7
  export * from './credential-proposal';
8
+ export * from './message-handler';
@@ -0,0 +1,83 @@
1
+ import { BasicMessage, IPackageManager } from '../types';
2
+ import { AuthMessageHandlerOptions } from './auth';
3
+ import { RevocationStatusMessageHandlerOptions } from './revocation-status';
4
+ import { ContractMessageHandlerOptions } from './contract-request';
5
+ /**
6
+ * iden3 Protocol message handler interface
7
+ */
8
+ export interface IProtocolMessageHandler {
9
+ /**
10
+ * Handel message implementation
11
+ *
12
+ * @param {BasicMessage} message
13
+ * @param {{ [key: string]: unknown }} ctx context
14
+ * @returns {(Promise<BasicMessage | null>)}
15
+ * @memberof IProtocolMessageHandler
16
+ */
17
+ handle(message: BasicMessage, ctx: {
18
+ [key: string]: unknown;
19
+ }): Promise<BasicMessage | null>;
20
+ }
21
+ /**
22
+ * Base implementation of protocol message handler
23
+ *
24
+ * @export
25
+ * @abstract
26
+ * @class AbstractMessageHandler
27
+ * @implements {IProtocolMessageHandler}
28
+ */
29
+ export declare abstract class AbstractMessageHandler implements IProtocolMessageHandler {
30
+ nextMessageHandler?: AbstractMessageHandler;
31
+ setNext(messageHandler: AbstractMessageHandler): AbstractMessageHandler;
32
+ handle(message: BasicMessage, context: {
33
+ [key: string]: unknown;
34
+ }): Promise<BasicMessage | null>;
35
+ }
36
+ /**
37
+ * Protocol message handler entry point
38
+ */
39
+ export declare class MessageHandler {
40
+ private readonly _params;
41
+ private messageHandler?;
42
+ /**
43
+ * Creates an instance of MessageHandler.
44
+ * @param {{
45
+ * messageHandlers: AbstractMessageHandler[];
46
+ * packageManager: IPackageManager;
47
+ * }} _params
48
+ * @memberof MessageHandler
49
+ */
50
+ constructor(_params: {
51
+ messageHandlers: AbstractMessageHandler[];
52
+ packageManager: IPackageManager;
53
+ });
54
+ /**
55
+ * Registers a list of message handlers and sets up the chain of responsibility.
56
+ *
57
+ * This method takes an array of `AbstractMessageHandler` instances and sets up a chain of responsibility
58
+ * where each handler is linked to the next one in the array. The first handler in the array becomes the
59
+ * main message handler for the `MessageHandler` class.
60
+ *
61
+ * @param {AbstractMessageHandler[]} handlersList - An array of `AbstractMessageHandler` instances to be registered.
62
+ * @returns {void}
63
+ */
64
+ registerHandlers(handlersList: AbstractMessageHandler[]): void;
65
+ /**
66
+ * Handles a message by unpacking it, passing it to the registered message handler, and packing the response.
67
+ *
68
+ * This method takes a Uint8Array of message bytes and a context object that contains information specific to the
69
+ * type of message being handled (e.g. AuthMessageHandlerOptions, ContractMessageHandlerOptions, etc.).
70
+ *
71
+ * The method first unpacks the message using the provided package manager, then passes the unpacked message and
72
+ * context to the registered message handler. If the message handler returns a response, the method packs the
73
+ * response using the package manager and returns it. If the message handler does not return a response, the
74
+ * method returns null.
75
+ *
76
+ * @param bytes - A Uint8Array of message bytes to be handled.
77
+ * @param context - An object containing information specific to the type of message being handled.
78
+ * @returns A Promise that resolves to a Uint8Array of the packed response, or null if no response was generated.
79
+ */
80
+ handleMessage(bytes: Uint8Array, context: AuthMessageHandlerOptions | ContractMessageHandlerOptions | RevocationStatusMessageHandlerOptions | ContractMessageHandlerOptions | {
81
+ [key: string]: unknown;
82
+ }): Promise<Uint8Array | null>;
83
+ }
@@ -1,8 +1,22 @@
1
1
  import { MediaType } from '../constants';
2
- import { IPackageManager, JWSPackerParams, RevocationStatusRequestMessage } from '../types';
2
+ import { BasicMessage, IPackageManager, JWSPackerParams, RevocationStatusRequestMessage } from '../types';
3
3
  import { DID } from '@iden3/js-iden3-core';
4
4
  import { TreeState } from '../../circuits';
5
5
  import { IIdentityWallet } from '../../identity';
6
+ import { AbstractMessageHandler, IProtocolMessageHandler } from './message-handler';
7
+ /**
8
+ * Defines the options for a RevocationStatusMessageHandler.
9
+ * @property did - The DID (Decentralized Identifier) to be used.
10
+ * @property mediaType - The media type to be used.
11
+ * @property packerOptions - Optional parameters for the JWS packer.
12
+ * @property treeState - Optional tree state to be used.
13
+ */
14
+ export type RevocationStatusMessageHandlerOptions = {
15
+ did: DID;
16
+ mediaType: MediaType;
17
+ packerOptions?: JWSPackerParams;
18
+ treeState?: TreeState;
19
+ };
6
20
  /**
7
21
  * Interface that allows the processing of the revocation status
8
22
  *
@@ -38,7 +52,7 @@ export type RevocationStatusHandlerOptions = {
38
52
  * @class RevocationStatusHandler
39
53
  * @implements implements IRevocationStatusHandler interface
40
54
  */
41
- export declare class RevocationStatusHandler implements IRevocationStatusHandler {
55
+ export declare class RevocationStatusHandler extends AbstractMessageHandler implements IRevocationStatusHandler, IProtocolMessageHandler {
42
56
  private readonly _packerMgr;
43
57
  private readonly _identityWallet;
44
58
  /**
@@ -48,6 +62,8 @@ export declare class RevocationStatusHandler implements IRevocationStatusHandler
48
62
  *
49
63
  */
50
64
  constructor(_packerMgr: IPackageManager, _identityWallet: IIdentityWallet);
65
+ handle(message: BasicMessage, context: RevocationStatusMessageHandlerOptions): Promise<BasicMessage | null>;
66
+ private handleRevocationStatusRequestMessage;
51
67
  /**
52
68
  * @inheritdoc IRevocationStatusHandler#parseRevocationStatusRequest
53
69
  */
@@ -17,6 +17,16 @@ export declare class PackageManager implements IPackageManager {
17
17
  registerPackers(packers: Array<IPacker>): void;
18
18
  /** {@inheritDoc IPackageManager.pack} */
19
19
  pack(mediaType: MediaType, payload: Uint8Array, params: PackerParams): Promise<Uint8Array>;
20
+ /**
21
+ * Packs a protocol message using the specified media type and packer parameters.
22
+ *
23
+ * @param mediaType - The media type to use for packing the message.
24
+ * @param protocolMessage - The protocol message to pack.
25
+ * @param params - The packer parameters.
26
+ * @returns A promise that resolves to the packed message as a Uint8Array.
27
+ * @throws An error if the packer for the specified media type is not found.
28
+ */
29
+ packMessage(mediaType: MediaType, protocolMessage: BasicMessage, params: PackerParams): Promise<Uint8Array>;
20
30
  /** {@inheritDoc IPackageManager.unpack} */
21
31
  unpack(envelope: Uint8Array): Promise<{
22
32
  unpackedMessage: BasicMessage;