@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
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MessageHandler = exports.AbstractMessageHandler = void 0;
4
+ /**
5
+ * Base implementation of protocol message handler
6
+ *
7
+ * @export
8
+ * @abstract
9
+ * @class AbstractMessageHandler
10
+ * @implements {IProtocolMessageHandler}
11
+ */
12
+ class AbstractMessageHandler {
13
+ setNext(messageHandler) {
14
+ this.nextMessageHandler = messageHandler;
15
+ return messageHandler;
16
+ }
17
+ async handle(message, context) {
18
+ if (this.nextMessageHandler)
19
+ return this.nextMessageHandler.handle(message, context);
20
+ return Promise.reject('Message handler not provided or message not supported');
21
+ }
22
+ }
23
+ exports.AbstractMessageHandler = AbstractMessageHandler;
24
+ /**
25
+ * Protocol message handler entry point
26
+ */
27
+ class MessageHandler {
28
+ /**
29
+ * Creates an instance of MessageHandler.
30
+ * @param {{
31
+ * messageHandlers: AbstractMessageHandler[];
32
+ * packageManager: IPackageManager;
33
+ * }} _params
34
+ * @memberof MessageHandler
35
+ */
36
+ constructor(_params) {
37
+ this._params = _params;
38
+ this.registerHandlers(_params.messageHandlers);
39
+ }
40
+ /**
41
+ * Registers a list of message handlers and sets up the chain of responsibility.
42
+ *
43
+ * This method takes an array of `AbstractMessageHandler` instances and sets up a chain of responsibility
44
+ * where each handler is linked to the next one in the array. The first handler in the array becomes the
45
+ * main message handler for the `MessageHandler` class.
46
+ *
47
+ * @param {AbstractMessageHandler[]} handlersList - An array of `AbstractMessageHandler` instances to be registered.
48
+ * @returns {void}
49
+ */
50
+ registerHandlers(handlersList) {
51
+ if (!handlersList.length)
52
+ return;
53
+ const [firstMessageHandler, ...restHandlersList] = handlersList;
54
+ const tempHandler = firstMessageHandler;
55
+ for (const currentHandler of restHandlersList) {
56
+ let lastHandler = tempHandler;
57
+ while (lastHandler.nextMessageHandler) {
58
+ lastHandler = lastHandler.nextMessageHandler;
59
+ }
60
+ lastHandler.setNext(currentHandler);
61
+ }
62
+ if (!this.messageHandler) {
63
+ this.messageHandler = firstMessageHandler;
64
+ }
65
+ else {
66
+ this.messageHandler.setNext(firstMessageHandler);
67
+ }
68
+ }
69
+ /**
70
+ * Handles a message by unpacking it, passing it to the registered message handler, and packing the response.
71
+ *
72
+ * This method takes a Uint8Array of message bytes and a context object that contains information specific to the
73
+ * type of message being handled (e.g. AuthMessageHandlerOptions, ContractMessageHandlerOptions, etc.).
74
+ *
75
+ * The method first unpacks the message using the provided package manager, then passes the unpacked message and
76
+ * context to the registered message handler. If the message handler returns a response, the method packs the
77
+ * response using the package manager and returns it. If the message handler does not return a response, the
78
+ * method returns null.
79
+ *
80
+ * @param bytes - A Uint8Array of message bytes to be handled.
81
+ * @param context - An object containing information specific to the type of message being handled.
82
+ * @returns A Promise that resolves to a Uint8Array of the packed response, or null if no response was generated.
83
+ */
84
+ async handleMessage(bytes, context) {
85
+ const { unpackedMediaType, unpackedMessage: message } = await this._params.packageManager.unpack(bytes);
86
+ if (!this.messageHandler) {
87
+ return Promise.reject(new Error('Message handler not provided'));
88
+ }
89
+ const response = await this.messageHandler.handle(message, context);
90
+ if (response) {
91
+ return this._params.packageManager.packMessage(unpackedMediaType, response, {});
92
+ }
93
+ return null;
94
+ }
95
+ }
96
+ exports.MessageHandler = MessageHandler;
@@ -30,6 +30,7 @@ const js_iden3_core_1 = require("@iden3/js-iden3-core");
30
30
  const uuid = __importStar(require("uuid"));
31
31
  const utils_1 = require("../../utils");
32
32
  const js_jwz_1 = require("@iden3/js-jwz");
33
+ const message_handler_1 = require("./message-handler");
33
34
  /**
34
35
  *
35
36
  * Allows to process RevocationStatusRequest protocol message
@@ -38,7 +39,7 @@ const js_jwz_1 = require("@iden3/js-jwz");
38
39
  * @class RevocationStatusHandler
39
40
  * @implements implements IRevocationStatusHandler interface
40
41
  */
41
- class RevocationStatusHandler {
42
+ class RevocationStatusHandler extends message_handler_1.AbstractMessageHandler {
42
43
  /**
43
44
  * Creates an instance of RevocationStatusHandler.
44
45
  * @param {IPackageManager} _packerMgr - package manager to unpack message envelope
@@ -46,33 +47,25 @@ class RevocationStatusHandler {
46
47
  *
47
48
  */
48
49
  constructor(_packerMgr, _identityWallet) {
50
+ super();
49
51
  this._packerMgr = _packerMgr;
50
52
  this._identityWallet = _identityWallet;
51
53
  }
52
- /**
53
- * @inheritdoc IRevocationStatusHandler#parseRevocationStatusRequest
54
- */
55
- async parseRevocationStatusRequest(request) {
56
- const { unpackedMessage: message } = await this._packerMgr.unpack(request);
57
- const ciRequest = message;
58
- if (message.type !== constants_1.PROTOCOL_MESSAGE_TYPE.REVOCATION_STATUS_REQUEST_MESSAGE_TYPE) {
59
- throw new Error('Invalid media type');
54
+ handle(message, context) {
55
+ if (!context.did) {
56
+ throw new Error('DID is required');
60
57
  }
61
- return ciRequest;
62
- }
63
- /**
64
- * @inheritdoc IRevocationStatusHandler#handleRevocationStatusRequest
65
- */
66
- async handleRevocationStatusRequest(did, request, opts) {
67
- if (!opts) {
68
- opts = {
69
- mediaType: constants_2.MediaType.PlainMessage
70
- };
58
+ if (!context.mediaType) {
59
+ throw new Error('mediaType is required');
71
60
  }
72
- if (opts.mediaType === constants_2.MediaType.SignedMessage && !opts.packerOptions) {
73
- throw new Error(`jws packer options are required for ${constants_2.MediaType.SignedMessage}`);
61
+ switch (message.type) {
62
+ case constants_1.PROTOCOL_MESSAGE_TYPE.REVOCATION_STATUS_REQUEST_MESSAGE_TYPE:
63
+ return this.handleRevocationStatusRequestMessage(message, context);
64
+ default:
65
+ return super.handle(message, context);
74
66
  }
75
- const rsRequest = await this.parseRevocationStatusRequest(request);
67
+ }
68
+ async handleRevocationStatusRequestMessage(rsRequest, context) {
76
69
  if (!rsRequest.to) {
77
70
  throw new Error(`failed request. empty 'to' field`);
78
71
  }
@@ -83,7 +76,7 @@ class RevocationStatusHandler {
83
76
  throw new Error(`failed request. empty 'revocation_nonce' field`);
84
77
  }
85
78
  const issuerDID = js_iden3_core_1.DID.parse(rsRequest.to);
86
- const mtpWithTreeState = await this._identityWallet.generateNonRevocationMtpWithNonce(issuerDID, BigInt(rsRequest.body.revocation_nonce), opts?.treeState);
79
+ const mtpWithTreeState = await this._identityWallet.generateNonRevocationMtpWithNonce(issuerDID, BigInt(rsRequest.body.revocation_nonce), context.treeState);
87
80
  const treeState = mtpWithTreeState.treeState;
88
81
  const revStatus = {
89
82
  issuer: {
@@ -94,12 +87,6 @@ class RevocationStatusHandler {
94
87
  },
95
88
  mtp: mtpWithTreeState.proof
96
89
  };
97
- const packerOpts = opts.mediaType === constants_2.MediaType.SignedMessage
98
- ? opts.packerOptions
99
- : {
100
- provingMethodAlg: js_jwz_1.proving.provingMethodGroth16AuthV2Instance.methodAlg
101
- };
102
- const senderDID = js_iden3_core_1.DID.parse(rsRequest.to);
103
90
  const guid = uuid.v4();
104
91
  const response = {
105
92
  id: guid,
@@ -107,9 +94,50 @@ class RevocationStatusHandler {
107
94
  type: constants_1.PROTOCOL_MESSAGE_TYPE.REVOCATION_STATUS_RESPONSE_MESSAGE_TYPE,
108
95
  thid: rsRequest.thid ?? guid,
109
96
  body: revStatus,
110
- from: did.string(),
97
+ from: context.did.string(),
111
98
  to: rsRequest.from
112
99
  };
100
+ return response;
101
+ }
102
+ /**
103
+ * @inheritdoc IRevocationStatusHandler#parseRevocationStatusRequest
104
+ */
105
+ async parseRevocationStatusRequest(request) {
106
+ const { unpackedMessage: message } = await this._packerMgr.unpack(request);
107
+ const ciRequest = message;
108
+ if (message.type !== constants_1.PROTOCOL_MESSAGE_TYPE.REVOCATION_STATUS_REQUEST_MESSAGE_TYPE) {
109
+ throw new Error('Invalid media type');
110
+ }
111
+ return ciRequest;
112
+ }
113
+ /**
114
+ * @inheritdoc IRevocationStatusHandler#handleRevocationStatusRequest
115
+ */
116
+ async handleRevocationStatusRequest(did, request, opts) {
117
+ if (!opts) {
118
+ opts = {
119
+ mediaType: constants_2.MediaType.PlainMessage
120
+ };
121
+ }
122
+ if (opts.mediaType === constants_2.MediaType.SignedMessage && !opts.packerOptions) {
123
+ throw new Error(`jws packer options are required for ${constants_2.MediaType.SignedMessage}`);
124
+ }
125
+ const rsRequest = await this.parseRevocationStatusRequest(request);
126
+ const response = await this.handleRevocationStatusRequestMessage(rsRequest, {
127
+ did,
128
+ mediaType: opts.mediaType,
129
+ packerOptions: opts.packerOptions,
130
+ treeState: opts.treeState
131
+ });
132
+ const packerOpts = opts.mediaType === constants_2.MediaType.SignedMessage
133
+ ? opts.packerOptions
134
+ : {
135
+ provingMethodAlg: js_jwz_1.proving.provingMethodGroth16AuthV2Instance.methodAlg
136
+ };
137
+ if (!rsRequest.to) {
138
+ throw new Error(`failed request. empty 'to' field`);
139
+ }
140
+ const senderDID = js_iden3_core_1.DID.parse(rsRequest.to);
113
141
  return this._packerMgr.pack(opts.mediaType, utils_1.byteEncoder.encode(JSON.stringify(response)), {
114
142
  senderDID,
115
143
  ...packerOpts
@@ -32,6 +32,22 @@ class PackageManager {
32
32
  }
33
33
  return await p.pack(payload, params);
34
34
  }
35
+ /**
36
+ * Packs a protocol message using the specified media type and packer parameters.
37
+ *
38
+ * @param mediaType - The media type to use for packing the message.
39
+ * @param protocolMessage - The protocol message to pack.
40
+ * @param params - The packer parameters.
41
+ * @returns A promise that resolves to the packed message as a Uint8Array.
42
+ * @throws An error if the packer for the specified media type is not found.
43
+ */
44
+ packMessage(mediaType, protocolMessage, params) {
45
+ const p = this.packers.get(mediaType);
46
+ if (!p) {
47
+ throw new Error(`packer for media type ${mediaType} not found`);
48
+ }
49
+ return p.packMessage(protocolMessage, params);
50
+ }
35
51
  /** {@inheritDoc IPackageManager.unpack} */
36
52
  async unpack(envelope) {
37
53
  const decodedStr = utils_1.byteDecoder.decode(envelope);
@@ -6,6 +6,7 @@ const did_1 = require("../utils/did");
6
6
  const kms_1 = require("../../kms/");
7
7
  const did_jwt_1 = require("did-jwt");
8
8
  const did_resolver_1 = require("did-resolver");
9
+ const encoding_1 = require("../../utils/encoding");
9
10
  const utils_1 = require("../../utils");
10
11
  /**
11
12
  * Packer that can pack message to JWZ token,
@@ -26,6 +27,16 @@ class JWSPacker {
26
27
  this._kms = _kms;
27
28
  this._documentResolver = _documentResolver;
28
29
  }
30
+ /**
31
+ * Packs the given payload and returns a promise that resolves to the packed data.
32
+ *
33
+ * @param {Uint8Array} payload - The payload to be packed.
34
+ * @param {PackerParams} param - The packing parameters.
35
+ * @returns `Promise<Uint8Array>`
36
+ */
37
+ packMessage(msg, param) {
38
+ return this.packInternal(msg, param);
39
+ }
29
40
  /**
30
41
  * creates JSON Web Signature token
31
42
  *
@@ -34,56 +45,8 @@ class JWSPacker {
34
45
  * @returns `Promise<Uint8Array>`
35
46
  */
36
47
  async pack(payload, params) {
37
- if (!params.alg) {
38
- throw new Error('Missing algorithm');
39
- }
40
48
  const message = JSON.parse(utils_1.byteDecoder.decode(payload));
41
- const from = message.from ?? '';
42
- if (!from) {
43
- throw new Error('Missing sender DID');
44
- }
45
- const vmTypes = constants_1.SUPPORTED_PUBLIC_KEY_TYPES[params.alg];
46
- if (!vmTypes?.length) {
47
- throw new Error(`No supported verification methods for algorithm ${params.alg}`);
48
- }
49
- const didDocument = params.didDocument ?? (await this.resolveDidDoc(from));
50
- const vms = (0, did_1.resolveVerificationMethods)(didDocument);
51
- if (!vms.length) {
52
- throw new Error(`No verification methods defined in the DID document of ${didDocument.id}`);
53
- }
54
- // try to find a managed signing key that matches keyRef
55
- const vm = params.kid ? vms.find((vm) => vm.id === params.kid) : vms[0];
56
- if (!vm) {
57
- throw new Error(`No key found with id ${params.kid} in DID document of ${didDocument.id}`);
58
- }
59
- const { publicKeyBytes, kmsKeyType } = (0, did_1.extractPublicKeyBytes)(vm);
60
- if (!publicKeyBytes && !kmsKeyType) {
61
- if ((vm.blockchainAccountId || vm.ethereumAddress) && !params.signer) {
62
- throw new Error(`No signer provided for ${vm.blockchainAccountId || vm.ethereumAddress}`);
63
- }
64
- }
65
- const kid = vm.id;
66
- const headerObj = { alg: params.alg, kid, typ: constants_1.MediaType.SignedMessage };
67
- const header = (0, utils_1.encodeBase64url)(JSON.stringify(headerObj));
68
- const msg = (0, utils_1.encodeBase64url)(JSON.stringify(message));
69
- const signingInput = `${header}.${msg}`;
70
- const signingInputBytes = utils_1.byteEncoder.encode(signingInput);
71
- let signatureBase64;
72
- if (params.signer) {
73
- const signerFn = params.signer(vm, signingInputBytes);
74
- signatureBase64 = (await signerFn(signingInput)).toString();
75
- }
76
- else {
77
- if (!publicKeyBytes) {
78
- throw new Error('No public key found');
79
- }
80
- if (!kmsKeyType) {
81
- throw new Error('No KMS key type found');
82
- }
83
- const signatureBytes = await this._kms.sign({ type: kmsKeyType, id: (0, kms_1.keyPath)(kmsKeyType, (0, utils_1.bytesToHex)(publicKeyBytes)) }, signingInputBytes);
84
- signatureBase64 = utils_1.byteDecoder.decode(signatureBytes);
85
- }
86
- return utils_1.byteEncoder.encode(`${signingInput}.${signatureBase64}`);
49
+ return this.packInternal(message, params);
87
50
  }
88
51
  /**
89
52
  * validate envelope which is jwz token
@@ -137,5 +100,56 @@ class JWSPacker {
137
100
  }
138
101
  return didDocument;
139
102
  }
103
+ async packInternal(message, params) {
104
+ if (!params.alg) {
105
+ throw new Error('Missing algorithm');
106
+ }
107
+ const from = message.from ?? '';
108
+ if (!from) {
109
+ throw new Error('Missing sender DID');
110
+ }
111
+ const vmTypes = constants_1.SUPPORTED_PUBLIC_KEY_TYPES[params.alg];
112
+ if (!vmTypes?.length) {
113
+ throw new Error(`No supported verification methods for algorithm ${params.alg}`);
114
+ }
115
+ const didDocument = params.didDocument ?? (await this.resolveDidDoc(from));
116
+ const vms = (0, did_1.resolveVerificationMethods)(didDocument);
117
+ if (!vms.length) {
118
+ throw new Error(`No verification methods defined in the DID document of ${didDocument.id}`);
119
+ }
120
+ // try to find a managed signing key that matches keyRef
121
+ const vm = params.kid ? vms.find((vm) => vm.id === params.kid) : vms[0];
122
+ if (!vm) {
123
+ throw new Error(`No key found with id ${params.kid} in DID document of ${didDocument.id}`);
124
+ }
125
+ const { publicKeyBytes, kmsKeyType } = (0, did_1.extractPublicKeyBytes)(vm);
126
+ if (!publicKeyBytes && !kmsKeyType) {
127
+ if ((vm.blockchainAccountId || vm.ethereumAddress) && !params.signer) {
128
+ throw new Error(`No signer provided for ${vm.blockchainAccountId || vm.ethereumAddress}`);
129
+ }
130
+ }
131
+ const kid = vm.id;
132
+ const headerObj = { alg: params.alg, kid, typ: constants_1.MediaType.SignedMessage };
133
+ const header = (0, utils_1.encodeBase64url)(JSON.stringify(headerObj));
134
+ const msg = (0, utils_1.encodeBase64url)(JSON.stringify(message));
135
+ const signingInput = `${header}.${msg}`;
136
+ const signingInputBytes = utils_1.byteEncoder.encode(signingInput);
137
+ let signatureBase64;
138
+ if (params.signer) {
139
+ const signature = await params.signer(vm, signingInputBytes);
140
+ signatureBase64 = (0, encoding_1.bytesToBase64url)(signature);
141
+ }
142
+ else {
143
+ if (!publicKeyBytes) {
144
+ throw new Error('No public key found');
145
+ }
146
+ if (!kmsKeyType) {
147
+ throw new Error('No KMS key type found');
148
+ }
149
+ const signatureBytes = await this._kms.sign({ type: kmsKeyType, id: (0, kms_1.keyPath)(kmsKeyType, (0, utils_1.bytesToHex)(publicKeyBytes)) }, signingInputBytes, { alg: params.alg });
150
+ signatureBase64 = (0, encoding_1.bytesToBase64url)(signatureBytes);
151
+ }
152
+ return utils_1.byteEncoder.encode(`${signingInput}.${signatureBase64}`);
153
+ }
140
154
  }
141
155
  exports.JWSPacker = JWSPacker;
@@ -11,6 +11,18 @@ const utils_1 = require("../../utils");
11
11
  * @implements implements IPacker interface
12
12
  */
13
13
  class PlainPacker {
14
+ /**
15
+ * Packs a basic message using the specified parameters.
16
+ *
17
+ * @param msg - The basic message to pack.
18
+ * @param param - The packer parameters.
19
+ * @returns A promise that resolves to a Uint8Array representing the packed message.
20
+ * @throws An error if the method is not implemented.
21
+ */
22
+ packMessage(msg) {
23
+ msg.typ = constants_1.MediaType.PlainMessage;
24
+ return Promise.resolve(utils_1.byteEncoder.encode(JSON.stringify(msg)));
25
+ }
14
26
  /**
15
27
  * Pack returns packed message to transport envelope
16
28
  *
@@ -18,9 +30,7 @@ class PlainPacker {
18
30
  * @param {PlainPackerParams} _params - not used here
19
31
  * @returns `Promise<Uint8Array>`
20
32
  */
21
- //
22
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
- async pack(payload, _params) {
33
+ async pack(payload) {
24
34
  const msg = JSON.parse(utils_1.byteDecoder.decode(payload));
25
35
  msg.typ = constants_1.MediaType.PlainMessage;
26
36
  return Promise.resolve(utils_1.byteEncoder.encode(JSON.stringify(msg)));
@@ -79,6 +79,15 @@ class ZKPPacker {
79
79
  this.provingParamsMap = provingParamsMap;
80
80
  this.verificationParamsMap = verificationParamsMap;
81
81
  }
82
+ /**
83
+ * Packs a basic message using the specified parameters.
84
+ * @param msg - The basic message to pack.
85
+ * @param param - The parameters for the ZKPPacker.
86
+ * @returns A promise that resolves to a Uint8Array representing the packed message.
87
+ */
88
+ packMessage(msg, param) {
89
+ return this.pack(utils_1.byteEncoder.encode(JSON.stringify(msg)), param);
90
+ }
82
91
  /**
83
92
  * creates JSON Web Zeroknowledge token
84
93
  *
@@ -1,11 +1,8 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.extractPublicKeyBytes = exports.resolveVerificationMethods = void 0;
7
4
  const constants_1 = require("../constants");
8
- const elliptic_1 = __importDefault(require("elliptic"));
5
+ const secp256k1_1 = require("@noble/curves/secp256k1");
9
6
  const kms_1 = require("../../kms");
10
7
  const utils_1 = require("../../utils");
11
8
  const DIDAuthenticationSection = 'authentication';
@@ -30,7 +27,6 @@ const resolveVerificationMethods = (didDocument) => {
30
27
  return sortedVerificationMethods;
31
28
  };
32
29
  exports.resolveVerificationMethods = resolveVerificationMethods;
33
- const secp256k1 = new elliptic_1.default.ec('secp256k1');
34
30
  const extractPublicKeyBytes = (vm) => {
35
31
  const isSupportedVmType = Object.keys(constants_1.SUPPORTED_PUBLIC_KEY_TYPES).some((key) => constants_1.SUPPORTED_PUBLIC_KEY_TYPES[key].includes(vm.type));
36
32
  if (vm.publicKeyBase58 && isSupportedVmType) {
@@ -49,13 +45,17 @@ const extractPublicKeyBytes = (vm) => {
49
45
  vm.publicKeyJwk.crv === 'secp256k1' &&
50
46
  vm.publicKeyJwk.x &&
51
47
  vm.publicKeyJwk.y) {
48
+ const [xHex, yHex] = [
49
+ (0, utils_1.base64UrlToBytes)(vm.publicKeyJwk.x),
50
+ (0, utils_1.base64UrlToBytes)(vm.publicKeyJwk.y)
51
+ ].map(utils_1.bytesToHex);
52
+ const x = xHex.includes('0x') ? BigInt(xHex) : BigInt(`0x${xHex}`);
53
+ const y = yHex.includes('0x') ? BigInt(yHex) : BigInt(`0x${yHex}`);
52
54
  return {
53
- publicKeyBytes: (0, utils_1.hexToBytes)(secp256k1
54
- .keyFromPublic({
55
- x: (0, utils_1.bytesToHex)((0, utils_1.base64UrlToBytes)(vm.publicKeyJwk.x)),
56
- y: (0, utils_1.bytesToHex)((0, utils_1.base64UrlToBytes)(vm.publicKeyJwk.y))
57
- })
58
- .getPublic('hex')),
55
+ publicKeyBytes: secp256k1_1.secp256k1.ProjectivePoint.fromAffine({
56
+ x,
57
+ y
58
+ }).toRawBytes(false),
59
59
  kmsKeyType: kms_1.KmsKeyType.Secp256k1
60
60
  };
61
61
  }