@0xpolygonid/js-sdk 1.7.2 → 1.7.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.
- package/dist/browser/esm/index.js +69 -6
- package/dist/browser/esm/index.js.map +1 -1
- package/dist/browser/umd/index.js +69 -6
- package/dist/browser/umd/index.js.map +1 -1
- package/dist/node/cjs/iden3comm/handlers/fetch.js +68 -7
- package/dist/node/cjs/verifiable/credential.js +3 -0
- package/dist/node/esm/iden3comm/handlers/fetch.js +68 -7
- package/dist/node/esm/verifiable/credential.js +3 -0
- package/dist/types/iden3comm/handlers/fetch.d.ts +33 -1
- package/package.json +1 -1
|
@@ -26,11 +26,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.FetchHandler = void 0;
|
|
27
27
|
const constants_1 = require("../constants");
|
|
28
28
|
const constants_2 = require("../constants");
|
|
29
|
-
const uuid = __importStar(require("uuid"));
|
|
30
29
|
const verifiable_1 = require("../../verifiable");
|
|
30
|
+
const credentials_1 = require("../../credentials");
|
|
31
31
|
const utils_1 = require("../../utils");
|
|
32
32
|
const js_jwz_1 = require("@iden3/js-jwz");
|
|
33
33
|
const js_iden3_core_1 = require("@iden3/js-iden3-core");
|
|
34
|
+
const uuid = __importStar(require("uuid"));
|
|
34
35
|
/**
|
|
35
36
|
*
|
|
36
37
|
* Allows to handle Credential offer protocol message and return fetched credential
|
|
@@ -45,8 +46,9 @@ class FetchHandler {
|
|
|
45
46
|
* Creates an instance of FetchHandler.
|
|
46
47
|
* @param {IPackageManager} _packerMgr - package manager to unpack message envelope
|
|
47
48
|
*/
|
|
48
|
-
constructor(_packerMgr) {
|
|
49
|
+
constructor(_packerMgr, opts) {
|
|
49
50
|
this._packerMgr = _packerMgr;
|
|
51
|
+
this.opts = opts;
|
|
50
52
|
}
|
|
51
53
|
/**
|
|
52
54
|
* Handles only messages with credentials/1.0/offer type
|
|
@@ -66,11 +68,7 @@ class FetchHandler {
|
|
|
66
68
|
if (opts.mediaType === constants_1.MediaType.SignedMessage && !opts.packerOptions) {
|
|
67
69
|
throw new Error(`jws packer options are required for ${constants_1.MediaType.SignedMessage}`);
|
|
68
70
|
}
|
|
69
|
-
const
|
|
70
|
-
const offerMessage = message;
|
|
71
|
-
if (message.type !== constants_2.PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE) {
|
|
72
|
-
throw new Error('Invalid media type');
|
|
73
|
-
}
|
|
71
|
+
const offerMessage = await FetchHandler.unpackMessage(this._packerMgr, offer, constants_2.PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE);
|
|
74
72
|
const credentials = [];
|
|
75
73
|
for (let index = 0; index < offerMessage.body.credentials.length; index++) {
|
|
76
74
|
const credentialInfo = offerMessage.body.credentials[index];
|
|
@@ -122,5 +120,68 @@ class FetchHandler {
|
|
|
122
120
|
}
|
|
123
121
|
return credentials;
|
|
124
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* @inheritdoc IFetchHandler#handleCredentialFetchRequest
|
|
125
|
+
*/
|
|
126
|
+
async handleCredentialFetchRequest(envelope) {
|
|
127
|
+
const msgRequest = await FetchHandler.unpackMessage(this._packerMgr, envelope, constants_2.PROTOCOL_MESSAGE_TYPE.CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE);
|
|
128
|
+
if (!msgRequest.to) {
|
|
129
|
+
throw new Error("failed request. empty 'to' field");
|
|
130
|
+
}
|
|
131
|
+
if (!msgRequest.from) {
|
|
132
|
+
throw new Error("failed request. empty 'from' field");
|
|
133
|
+
}
|
|
134
|
+
const issuerDID = js_iden3_core_1.DID.parse(msgRequest.to);
|
|
135
|
+
const userDID = js_iden3_core_1.DID.parse(msgRequest.from);
|
|
136
|
+
const credId = msgRequest.body?.id;
|
|
137
|
+
if (!credId) {
|
|
138
|
+
throw new Error('nvalid credential id in fetch request body');
|
|
139
|
+
}
|
|
140
|
+
if (!this.opts?.credentialWallet) {
|
|
141
|
+
throw new Error('please, provide credential wallet in options');
|
|
142
|
+
}
|
|
143
|
+
const cred = await this.opts.credentialWallet.findById(credId);
|
|
144
|
+
if (!cred) {
|
|
145
|
+
throw new Error('credential not found');
|
|
146
|
+
}
|
|
147
|
+
const userToVerifyDID = (0, credentials_1.getUserDIDFromCredential)(issuerDID, cred);
|
|
148
|
+
if (userToVerifyDID.string() !== userDID.string()) {
|
|
149
|
+
throw new Error('credential subject is not a sender DID');
|
|
150
|
+
}
|
|
151
|
+
return this._packerMgr.pack(constants_1.MediaType.PlainMessage, utils_1.byteEncoder.encode(JSON.stringify({
|
|
152
|
+
id: uuid.v4(),
|
|
153
|
+
type: constants_2.PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE,
|
|
154
|
+
typ: msgRequest.typ ?? constants_1.MediaType.PlainMessage,
|
|
155
|
+
thid: msgRequest.thid ?? uuid.v4(),
|
|
156
|
+
body: { credential: cred },
|
|
157
|
+
from: msgRequest.to,
|
|
158
|
+
to: msgRequest.from
|
|
159
|
+
})), {});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* @inheritdoc IFetchHandler#handleIssuanceResponseMessage
|
|
163
|
+
*/
|
|
164
|
+
async handleIssuanceResponseMessage(envelop) {
|
|
165
|
+
const issuanceMsg = await FetchHandler.unpackMessage(this._packerMgr, envelop, constants_2.PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE);
|
|
166
|
+
if (!this.opts?.credentialWallet) {
|
|
167
|
+
throw new Error('please provide credential wallet in options');
|
|
168
|
+
}
|
|
169
|
+
if (!issuanceMsg.body?.credential) {
|
|
170
|
+
throw new Error('credential is missing in issuance response message');
|
|
171
|
+
}
|
|
172
|
+
await this.opts.credentialWallet.save(verifiable_1.W3CCredential.fromJSON(issuanceMsg.body.credential));
|
|
173
|
+
return Promise.resolve(Uint8Array.from([]));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* @inheritdoc IFetchHandler#unpackMessage
|
|
177
|
+
*/
|
|
178
|
+
static async unpackMessage(packerMgr, envelope, messageType) {
|
|
179
|
+
const { unpackedMessage: message } = await packerMgr.unpack(envelope);
|
|
180
|
+
const msgRequest = message;
|
|
181
|
+
if (message.type !== messageType) {
|
|
182
|
+
throw new Error('Invalid message type');
|
|
183
|
+
}
|
|
184
|
+
return msgRequest;
|
|
185
|
+
}
|
|
125
186
|
}
|
|
126
187
|
exports.FetchHandler = FetchHandler;
|
|
@@ -171,6 +171,9 @@ class W3CCredential {
|
|
|
171
171
|
throw new Error('signature proof: invalid tree state of the issuer while checking credential status of singing key');
|
|
172
172
|
}
|
|
173
173
|
const revocationNonce = BigInt(proof.issuerData.credentialStatus.revocationNonce || 0);
|
|
174
|
+
if (revocationNonce !== proof.issuerData.authCoreClaim.getRevocationNonce()) {
|
|
175
|
+
throw new Error(`revocation nonce mismatch: revocation nonce from core representation of auth credential is not the same as in its credential`);
|
|
176
|
+
}
|
|
174
177
|
const proofValid = await (0, js_merkletree_1.verifyProof)(js_merkletree_1.Hash.fromHex(credStatus.issuer.revocationTreeRoot), credStatus.mtp, revocationNonce, BigInt(0));
|
|
175
178
|
if (!proofValid) {
|
|
176
179
|
throw new Error(`proof validation failed. revNonce=${revocationNonce}`);
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { MediaType } from '../constants';
|
|
2
2
|
import { PROTOCOL_MESSAGE_TYPE } from '../constants';
|
|
3
|
-
import * as uuid from 'uuid';
|
|
4
3
|
import { W3CCredential } from '../../verifiable';
|
|
4
|
+
import { getUserDIDFromCredential } from '../../credentials';
|
|
5
5
|
import { byteDecoder, byteEncoder } from '../../utils';
|
|
6
6
|
import { proving } from '@iden3/js-jwz';
|
|
7
7
|
import { DID } from '@iden3/js-iden3-core';
|
|
8
|
+
import * as uuid from 'uuid';
|
|
8
9
|
/**
|
|
9
10
|
*
|
|
10
11
|
* Allows to handle Credential offer protocol message and return fetched credential
|
|
@@ -19,8 +20,9 @@ export class FetchHandler {
|
|
|
19
20
|
* Creates an instance of FetchHandler.
|
|
20
21
|
* @param {IPackageManager} _packerMgr - package manager to unpack message envelope
|
|
21
22
|
*/
|
|
22
|
-
constructor(_packerMgr) {
|
|
23
|
+
constructor(_packerMgr, opts) {
|
|
23
24
|
this._packerMgr = _packerMgr;
|
|
25
|
+
this.opts = opts;
|
|
24
26
|
}
|
|
25
27
|
/**
|
|
26
28
|
* Handles only messages with credentials/1.0/offer type
|
|
@@ -40,11 +42,7 @@ export class FetchHandler {
|
|
|
40
42
|
if (opts.mediaType === MediaType.SignedMessage && !opts.packerOptions) {
|
|
41
43
|
throw new Error(`jws packer options are required for ${MediaType.SignedMessage}`);
|
|
42
44
|
}
|
|
43
|
-
const
|
|
44
|
-
const offerMessage = message;
|
|
45
|
-
if (message.type !== PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE) {
|
|
46
|
-
throw new Error('Invalid media type');
|
|
47
|
-
}
|
|
45
|
+
const offerMessage = await FetchHandler.unpackMessage(this._packerMgr, offer, PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE);
|
|
48
46
|
const credentials = [];
|
|
49
47
|
for (let index = 0; index < offerMessage.body.credentials.length; index++) {
|
|
50
48
|
const credentialInfo = offerMessage.body.credentials[index];
|
|
@@ -96,4 +94,67 @@ export class FetchHandler {
|
|
|
96
94
|
}
|
|
97
95
|
return credentials;
|
|
98
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* @inheritdoc IFetchHandler#handleCredentialFetchRequest
|
|
99
|
+
*/
|
|
100
|
+
async handleCredentialFetchRequest(envelope) {
|
|
101
|
+
const msgRequest = await FetchHandler.unpackMessage(this._packerMgr, envelope, PROTOCOL_MESSAGE_TYPE.CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE);
|
|
102
|
+
if (!msgRequest.to) {
|
|
103
|
+
throw new Error("failed request. empty 'to' field");
|
|
104
|
+
}
|
|
105
|
+
if (!msgRequest.from) {
|
|
106
|
+
throw new Error("failed request. empty 'from' field");
|
|
107
|
+
}
|
|
108
|
+
const issuerDID = DID.parse(msgRequest.to);
|
|
109
|
+
const userDID = DID.parse(msgRequest.from);
|
|
110
|
+
const credId = msgRequest.body?.id;
|
|
111
|
+
if (!credId) {
|
|
112
|
+
throw new Error('nvalid credential id in fetch request body');
|
|
113
|
+
}
|
|
114
|
+
if (!this.opts?.credentialWallet) {
|
|
115
|
+
throw new Error('please, provide credential wallet in options');
|
|
116
|
+
}
|
|
117
|
+
const cred = await this.opts.credentialWallet.findById(credId);
|
|
118
|
+
if (!cred) {
|
|
119
|
+
throw new Error('credential not found');
|
|
120
|
+
}
|
|
121
|
+
const userToVerifyDID = getUserDIDFromCredential(issuerDID, cred);
|
|
122
|
+
if (userToVerifyDID.string() !== userDID.string()) {
|
|
123
|
+
throw new Error('credential subject is not a sender DID');
|
|
124
|
+
}
|
|
125
|
+
return this._packerMgr.pack(MediaType.PlainMessage, byteEncoder.encode(JSON.stringify({
|
|
126
|
+
id: uuid.v4(),
|
|
127
|
+
type: PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE,
|
|
128
|
+
typ: msgRequest.typ ?? MediaType.PlainMessage,
|
|
129
|
+
thid: msgRequest.thid ?? uuid.v4(),
|
|
130
|
+
body: { credential: cred },
|
|
131
|
+
from: msgRequest.to,
|
|
132
|
+
to: msgRequest.from
|
|
133
|
+
})), {});
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* @inheritdoc IFetchHandler#handleIssuanceResponseMessage
|
|
137
|
+
*/
|
|
138
|
+
async handleIssuanceResponseMessage(envelop) {
|
|
139
|
+
const issuanceMsg = await FetchHandler.unpackMessage(this._packerMgr, envelop, PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE);
|
|
140
|
+
if (!this.opts?.credentialWallet) {
|
|
141
|
+
throw new Error('please provide credential wallet in options');
|
|
142
|
+
}
|
|
143
|
+
if (!issuanceMsg.body?.credential) {
|
|
144
|
+
throw new Error('credential is missing in issuance response message');
|
|
145
|
+
}
|
|
146
|
+
await this.opts.credentialWallet.save(W3CCredential.fromJSON(issuanceMsg.body.credential));
|
|
147
|
+
return Promise.resolve(Uint8Array.from([]));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* @inheritdoc IFetchHandler#unpackMessage
|
|
151
|
+
*/
|
|
152
|
+
static async unpackMessage(packerMgr, envelope, messageType) {
|
|
153
|
+
const { unpackedMessage: message } = await packerMgr.unpack(envelope);
|
|
154
|
+
const msgRequest = message;
|
|
155
|
+
if (message.type !== messageType) {
|
|
156
|
+
throw new Error('Invalid message type');
|
|
157
|
+
}
|
|
158
|
+
return msgRequest;
|
|
159
|
+
}
|
|
99
160
|
}
|
|
@@ -168,6 +168,9 @@ export class W3CCredential {
|
|
|
168
168
|
throw new Error('signature proof: invalid tree state of the issuer while checking credential status of singing key');
|
|
169
169
|
}
|
|
170
170
|
const revocationNonce = BigInt(proof.issuerData.credentialStatus.revocationNonce || 0);
|
|
171
|
+
if (revocationNonce !== proof.issuerData.authCoreClaim.getRevocationNonce()) {
|
|
172
|
+
throw new Error(`revocation nonce mismatch: revocation nonce from core representation of auth credential is not the same as in its credential`);
|
|
173
|
+
}
|
|
171
174
|
const proofValid = await verifyProof(Hash.fromHex(credStatus.issuer.revocationTreeRoot), credStatus.mtp, revocationNonce, BigInt(0));
|
|
172
175
|
if (!proofValid) {
|
|
173
176
|
throw new Error(`proof validation failed. revNonce=${revocationNonce}`);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { MediaType } from '../constants';
|
|
2
2
|
import { IPackageManager, JWSPackerParams } from '../types';
|
|
3
3
|
import { W3CCredential } from '../../verifiable';
|
|
4
|
+
import { ICredentialWallet } from '../../credentials';
|
|
4
5
|
/**
|
|
5
6
|
*
|
|
6
7
|
* Options to pass to fetch handler
|
|
@@ -34,6 +35,22 @@ export interface IFetchHandler {
|
|
|
34
35
|
}>`
|
|
35
36
|
*/
|
|
36
37
|
handleCredentialOffer(offer: Uint8Array, opts?: FetchHandlerOptions): Promise<W3CCredential[]>;
|
|
38
|
+
/**
|
|
39
|
+
* Handles a credential fetch request.
|
|
40
|
+
*
|
|
41
|
+
* @param basicMessage - The basic message containing the fetch request.
|
|
42
|
+
* @returns A promise that resolves to the response message.
|
|
43
|
+
* @throws An error if the request is invalid or if the credential is not found.
|
|
44
|
+
*/
|
|
45
|
+
handleCredentialFetchRequest(basicMessage: Uint8Array): Promise<Uint8Array>;
|
|
46
|
+
/**
|
|
47
|
+
* Handles the issuance response message.
|
|
48
|
+
*
|
|
49
|
+
* @param basicMessage - The basic message containing the issuance response.
|
|
50
|
+
* @returns A promise that resolves to a Uint8Array.
|
|
51
|
+
* @throws An error if the credential wallet is not provided in the options or if the credential is missing in the issuance response message.
|
|
52
|
+
*/
|
|
53
|
+
handleIssuanceResponseMessage(basicMessage: Uint8Array): Promise<Uint8Array>;
|
|
37
54
|
}
|
|
38
55
|
/**
|
|
39
56
|
*
|
|
@@ -46,11 +63,14 @@ export interface IFetchHandler {
|
|
|
46
63
|
*/
|
|
47
64
|
export declare class FetchHandler implements IFetchHandler {
|
|
48
65
|
private readonly _packerMgr;
|
|
66
|
+
private readonly opts?;
|
|
49
67
|
/**
|
|
50
68
|
* Creates an instance of FetchHandler.
|
|
51
69
|
* @param {IPackageManager} _packerMgr - package manager to unpack message envelope
|
|
52
70
|
*/
|
|
53
|
-
constructor(_packerMgr: IPackageManager
|
|
71
|
+
constructor(_packerMgr: IPackageManager, opts?: {
|
|
72
|
+
credentialWallet: ICredentialWallet;
|
|
73
|
+
} | undefined);
|
|
54
74
|
/**
|
|
55
75
|
* Handles only messages with credentials/1.0/offer type
|
|
56
76
|
*
|
|
@@ -61,4 +81,16 @@ export declare class FetchHandler implements IFetchHandler {
|
|
|
61
81
|
* @returns `Promise<W3CCredential[]>`
|
|
62
82
|
*/
|
|
63
83
|
handleCredentialOffer(offer: Uint8Array, opts?: FetchHandlerOptions): Promise<W3CCredential[]>;
|
|
84
|
+
/**
|
|
85
|
+
* @inheritdoc IFetchHandler#handleCredentialFetchRequest
|
|
86
|
+
*/
|
|
87
|
+
handleCredentialFetchRequest(envelope: Uint8Array): Promise<Uint8Array>;
|
|
88
|
+
/**
|
|
89
|
+
* @inheritdoc IFetchHandler#handleIssuanceResponseMessage
|
|
90
|
+
*/
|
|
91
|
+
handleIssuanceResponseMessage(envelop: Uint8Array): Promise<Uint8Array>;
|
|
92
|
+
/**
|
|
93
|
+
* @inheritdoc IFetchHandler#unpackMessage
|
|
94
|
+
*/
|
|
95
|
+
static unpackMessage<T>(packerMgr: IPackageManager, envelope: Uint8Array, messageType: string): Promise<T>;
|
|
64
96
|
}
|