@zkpassport/sdk 0.2.7 → 0.2.8
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/cjs/index.d.ts +9 -2
- package/dist/cjs/index.js +21 -9
- package/dist/esm/index.d.ts +9 -2
- package/dist/esm/index.js +21 -9
- package/package.json +1 -1
- package/src/index.ts +33 -10
package/dist/cjs/index.d.ts
CHANGED
|
@@ -128,6 +128,7 @@ export type QueryBuilder = {
|
|
|
128
128
|
export declare class ZKPassport {
|
|
129
129
|
private domain;
|
|
130
130
|
private topicToConfig;
|
|
131
|
+
private topicToLocalConfig;
|
|
131
132
|
private topicToKeyPair;
|
|
132
133
|
private topicToWebSocketClient;
|
|
133
134
|
private topicToSharedSecret;
|
|
@@ -154,14 +155,20 @@ export declare class ZKPassport {
|
|
|
154
155
|
private handleEncryptedMessage;
|
|
155
156
|
private getZkPassportRequest;
|
|
156
157
|
/**
|
|
157
|
-
* @notice Create a new request
|
|
158
|
+
* @notice Create a new request
|
|
159
|
+
* @param name Your service name
|
|
160
|
+
* @param logo The logo of your service
|
|
161
|
+
* @param purpose To explain what you want to do with the user's data
|
|
162
|
+
* @param scope Scope this request to a specific use case
|
|
163
|
+
* @param validity How many days ago should have the ID been last scanned by the user?
|
|
158
164
|
* @returns The query builder object.
|
|
159
165
|
*/
|
|
160
|
-
request({ name, logo, purpose, scope, topicOverride, keyPairOverride, }: {
|
|
166
|
+
request({ name, logo, purpose, scope, validity, topicOverride, keyPairOverride, }: {
|
|
161
167
|
name: string;
|
|
162
168
|
logo: string;
|
|
163
169
|
purpose: string;
|
|
164
170
|
scope?: string;
|
|
171
|
+
validity?: number;
|
|
165
172
|
topicOverride?: string;
|
|
166
173
|
keyPairOverride?: {
|
|
167
174
|
privateKey: Uint8Array;
|
package/dist/cjs/index.js
CHANGED
|
@@ -72,6 +72,7 @@ class ZKPassport {
|
|
|
72
72
|
//private wasmVerifierInit: boolean = false
|
|
73
73
|
constructor(_domain) {
|
|
74
74
|
this.topicToConfig = {};
|
|
75
|
+
this.topicToLocalConfig = {};
|
|
75
76
|
this.topicToKeyPair = {};
|
|
76
77
|
this.topicToWebSocketClient = {};
|
|
77
78
|
this.topicToSharedSecret = {};
|
|
@@ -305,10 +306,15 @@ class ZKPassport {
|
|
|
305
306
|
};
|
|
306
307
|
}
|
|
307
308
|
/**
|
|
308
|
-
* @notice Create a new request
|
|
309
|
+
* @notice Create a new request
|
|
310
|
+
* @param name Your service name
|
|
311
|
+
* @param logo The logo of your service
|
|
312
|
+
* @param purpose To explain what you want to do with the user's data
|
|
313
|
+
* @param scope Scope this request to a specific use case
|
|
314
|
+
* @param validity How many days ago should have the ID been last scanned by the user?
|
|
309
315
|
* @returns The query builder object.
|
|
310
316
|
*/
|
|
311
|
-
async request({ name, logo, purpose, scope, topicOverride, keyPairOverride, }) {
|
|
317
|
+
async request({ name, logo, purpose, scope, validity, topicOverride, keyPairOverride, }) {
|
|
312
318
|
const topic = topicOverride || (0, crypto_1.randomBytes)(16).toString("hex");
|
|
313
319
|
const keyPair = keyPairOverride || (await (0, encryption_1.generateECDHKeyPair)());
|
|
314
320
|
this.topicToKeyPair[topic] = {
|
|
@@ -319,6 +325,10 @@ class ZKPassport {
|
|
|
319
325
|
this.topicToService[topic] = { name, logo, purpose, scope };
|
|
320
326
|
this.topicToProofs[topic] = [];
|
|
321
327
|
this.topicToExpectedProofCount[topic] = 0;
|
|
328
|
+
this.topicToLocalConfig[topic] = {
|
|
329
|
+
// Default to 6 months
|
|
330
|
+
validity: validity || 6 * 30,
|
|
331
|
+
};
|
|
322
332
|
this.onRequestReceivedCallbacks[topic] = [];
|
|
323
333
|
this.onGeneratingProofCallbacks[topic] = [];
|
|
324
334
|
this.onBridgeConnectCallbacks[topic] = [];
|
|
@@ -375,7 +385,7 @@ class ZKPassport {
|
|
|
375
385
|
};
|
|
376
386
|
return this.getZkPassportRequest(topic);
|
|
377
387
|
}
|
|
378
|
-
async checkPublicInputs(proofs, queryResult) {
|
|
388
|
+
async checkPublicInputs(proofs, queryResult, topic) {
|
|
379
389
|
let commitmentIn;
|
|
380
390
|
let commitmentOut;
|
|
381
391
|
let isCorrect = true;
|
|
@@ -433,11 +443,12 @@ class ZKPassport {
|
|
|
433
443
|
}
|
|
434
444
|
commitmentOut = (0, utils_1.getCommitmentOutFromIntegrityProof)(proofData);
|
|
435
445
|
const currentDate = (0, utils_1.getCurrentDateFromIntegrityProof)(proofData);
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
446
|
+
const todayToCurrentDate = today.getTime() - currentDate.getTime();
|
|
447
|
+
const expectedDifference = this.topicToLocalConfig[topic]?.validity * 86400000;
|
|
448
|
+
const actualDifference = today.getTime() - (today.getTime() - expectedDifference);
|
|
449
|
+
// The ID should not expire within the next 6 months (or whatever the custom value is)
|
|
450
|
+
if (todayToCurrentDate >= actualDifference) {
|
|
451
|
+
console.warn(`The date used to check the validity of the ID is older than ${this.topicToLocalConfig[topic]?.validity} days. You can ask the user to rescan their ID or ask them to disclose their expiry date`);
|
|
441
452
|
isCorrect = false;
|
|
442
453
|
break;
|
|
443
454
|
}
|
|
@@ -927,7 +938,7 @@ class ZKPassport {
|
|
|
927
938
|
let verified = true;
|
|
928
939
|
let uniqueIdentifier;
|
|
929
940
|
if (queryResult) {
|
|
930
|
-
const { isCorrect, uniqueIdentifier: uniqueIdentifierFromPublicInputs } = await this.checkPublicInputs(proofsToVerify, queryResult);
|
|
941
|
+
const { isCorrect, uniqueIdentifier: uniqueIdentifierFromPublicInputs } = await this.checkPublicInputs(proofsToVerify, queryResult, requestId);
|
|
931
942
|
uniqueIdentifier = uniqueIdentifierFromPublicInputs;
|
|
932
943
|
verified = isCorrect;
|
|
933
944
|
}
|
|
@@ -974,6 +985,7 @@ class ZKPassport {
|
|
|
974
985
|
delete this.topicToWebSocketClient[requestId];
|
|
975
986
|
delete this.topicToKeyPair[requestId];
|
|
976
987
|
delete this.topicToConfig[requestId];
|
|
988
|
+
delete this.topicToLocalConfig[requestId];
|
|
977
989
|
delete this.topicToSharedSecret[requestId];
|
|
978
990
|
delete this.topicToProofs[requestId];
|
|
979
991
|
delete this.topicToExpectedProofCount[requestId];
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -128,6 +128,7 @@ export type QueryBuilder = {
|
|
|
128
128
|
export declare class ZKPassport {
|
|
129
129
|
private domain;
|
|
130
130
|
private topicToConfig;
|
|
131
|
+
private topicToLocalConfig;
|
|
131
132
|
private topicToKeyPair;
|
|
132
133
|
private topicToWebSocketClient;
|
|
133
134
|
private topicToSharedSecret;
|
|
@@ -154,14 +155,20 @@ export declare class ZKPassport {
|
|
|
154
155
|
private handleEncryptedMessage;
|
|
155
156
|
private getZkPassportRequest;
|
|
156
157
|
/**
|
|
157
|
-
* @notice Create a new request
|
|
158
|
+
* @notice Create a new request
|
|
159
|
+
* @param name Your service name
|
|
160
|
+
* @param logo The logo of your service
|
|
161
|
+
* @param purpose To explain what you want to do with the user's data
|
|
162
|
+
* @param scope Scope this request to a specific use case
|
|
163
|
+
* @param validity How many days ago should have the ID been last scanned by the user?
|
|
158
164
|
* @returns The query builder object.
|
|
159
165
|
*/
|
|
160
|
-
request({ name, logo, purpose, scope, topicOverride, keyPairOverride, }: {
|
|
166
|
+
request({ name, logo, purpose, scope, validity, topicOverride, keyPairOverride, }: {
|
|
161
167
|
name: string;
|
|
162
168
|
logo: string;
|
|
163
169
|
purpose: string;
|
|
164
170
|
scope?: string;
|
|
171
|
+
validity?: number;
|
|
165
172
|
topicOverride?: string;
|
|
166
173
|
keyPairOverride?: {
|
|
167
174
|
privateKey: Uint8Array;
|
package/dist/esm/index.js
CHANGED
|
@@ -62,6 +62,7 @@ export class ZKPassport {
|
|
|
62
62
|
//private wasmVerifierInit: boolean = false
|
|
63
63
|
constructor(_domain) {
|
|
64
64
|
this.topicToConfig = {};
|
|
65
|
+
this.topicToLocalConfig = {};
|
|
65
66
|
this.topicToKeyPair = {};
|
|
66
67
|
this.topicToWebSocketClient = {};
|
|
67
68
|
this.topicToSharedSecret = {};
|
|
@@ -295,10 +296,15 @@ export class ZKPassport {
|
|
|
295
296
|
};
|
|
296
297
|
}
|
|
297
298
|
/**
|
|
298
|
-
* @notice Create a new request
|
|
299
|
+
* @notice Create a new request
|
|
300
|
+
* @param name Your service name
|
|
301
|
+
* @param logo The logo of your service
|
|
302
|
+
* @param purpose To explain what you want to do with the user's data
|
|
303
|
+
* @param scope Scope this request to a specific use case
|
|
304
|
+
* @param validity How many days ago should have the ID been last scanned by the user?
|
|
299
305
|
* @returns The query builder object.
|
|
300
306
|
*/
|
|
301
|
-
async request({ name, logo, purpose, scope, topicOverride, keyPairOverride, }) {
|
|
307
|
+
async request({ name, logo, purpose, scope, validity, topicOverride, keyPairOverride, }) {
|
|
302
308
|
const topic = topicOverride || randomBytes(16).toString("hex");
|
|
303
309
|
const keyPair = keyPairOverride || (await generateECDHKeyPair());
|
|
304
310
|
this.topicToKeyPair[topic] = {
|
|
@@ -309,6 +315,10 @@ export class ZKPassport {
|
|
|
309
315
|
this.topicToService[topic] = { name, logo, purpose, scope };
|
|
310
316
|
this.topicToProofs[topic] = [];
|
|
311
317
|
this.topicToExpectedProofCount[topic] = 0;
|
|
318
|
+
this.topicToLocalConfig[topic] = {
|
|
319
|
+
// Default to 6 months
|
|
320
|
+
validity: validity || 6 * 30,
|
|
321
|
+
};
|
|
312
322
|
this.onRequestReceivedCallbacks[topic] = [];
|
|
313
323
|
this.onGeneratingProofCallbacks[topic] = [];
|
|
314
324
|
this.onBridgeConnectCallbacks[topic] = [];
|
|
@@ -365,7 +375,7 @@ export class ZKPassport {
|
|
|
365
375
|
};
|
|
366
376
|
return this.getZkPassportRequest(topic);
|
|
367
377
|
}
|
|
368
|
-
async checkPublicInputs(proofs, queryResult) {
|
|
378
|
+
async checkPublicInputs(proofs, queryResult, topic) {
|
|
369
379
|
let commitmentIn;
|
|
370
380
|
let commitmentOut;
|
|
371
381
|
let isCorrect = true;
|
|
@@ -423,11 +433,12 @@ export class ZKPassport {
|
|
|
423
433
|
}
|
|
424
434
|
commitmentOut = getCommitmentOutFromIntegrityProof(proofData);
|
|
425
435
|
const currentDate = getCurrentDateFromIntegrityProof(proofData);
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
436
|
+
const todayToCurrentDate = today.getTime() - currentDate.getTime();
|
|
437
|
+
const expectedDifference = this.topicToLocalConfig[topic]?.validity * 86400000;
|
|
438
|
+
const actualDifference = today.getTime() - (today.getTime() - expectedDifference);
|
|
439
|
+
// The ID should not expire within the next 6 months (or whatever the custom value is)
|
|
440
|
+
if (todayToCurrentDate >= actualDifference) {
|
|
441
|
+
console.warn(`The date used to check the validity of the ID is older than ${this.topicToLocalConfig[topic]?.validity} days. You can ask the user to rescan their ID or ask them to disclose their expiry date`);
|
|
431
442
|
isCorrect = false;
|
|
432
443
|
break;
|
|
433
444
|
}
|
|
@@ -917,7 +928,7 @@ export class ZKPassport {
|
|
|
917
928
|
let verified = true;
|
|
918
929
|
let uniqueIdentifier;
|
|
919
930
|
if (queryResult) {
|
|
920
|
-
const { isCorrect, uniqueIdentifier: uniqueIdentifierFromPublicInputs } = await this.checkPublicInputs(proofsToVerify, queryResult);
|
|
931
|
+
const { isCorrect, uniqueIdentifier: uniqueIdentifierFromPublicInputs } = await this.checkPublicInputs(proofsToVerify, queryResult, requestId);
|
|
921
932
|
uniqueIdentifier = uniqueIdentifierFromPublicInputs;
|
|
922
933
|
verified = isCorrect;
|
|
923
934
|
}
|
|
@@ -964,6 +975,7 @@ export class ZKPassport {
|
|
|
964
975
|
delete this.topicToWebSocketClient[requestId];
|
|
965
976
|
delete this.topicToKeyPair[requestId];
|
|
966
977
|
delete this.topicToConfig[requestId];
|
|
978
|
+
delete this.topicToLocalConfig[requestId];
|
|
967
979
|
delete this.topicToSharedSecret[requestId];
|
|
968
980
|
delete this.topicToProofs[requestId];
|
|
969
981
|
delete this.topicToExpectedProofCount[requestId];
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -260,6 +260,12 @@ export type QueryBuilder = {
|
|
|
260
260
|
export class ZKPassport {
|
|
261
261
|
private domain: string
|
|
262
262
|
private topicToConfig: Record<string, Record<string, IDCredentialConfig>> = {}
|
|
263
|
+
private topicToLocalConfig: Record<
|
|
264
|
+
string,
|
|
265
|
+
{
|
|
266
|
+
validity: number
|
|
267
|
+
}
|
|
268
|
+
> = {}
|
|
263
269
|
private topicToKeyPair: Record<string, { privateKey: Uint8Array; publicKey: Uint8Array }> = {}
|
|
264
270
|
private topicToWebSocketClient: Record<string, WebSocketClient> = {}
|
|
265
271
|
private topicToSharedSecret: Record<string, Uint8Array> = {}
|
|
@@ -546,7 +552,12 @@ export class ZKPassport {
|
|
|
546
552
|
}
|
|
547
553
|
|
|
548
554
|
/**
|
|
549
|
-
* @notice Create a new request
|
|
555
|
+
* @notice Create a new request
|
|
556
|
+
* @param name Your service name
|
|
557
|
+
* @param logo The logo of your service
|
|
558
|
+
* @param purpose To explain what you want to do with the user's data
|
|
559
|
+
* @param scope Scope this request to a specific use case
|
|
560
|
+
* @param validity How many days ago should have the ID been last scanned by the user?
|
|
550
561
|
* @returns The query builder object.
|
|
551
562
|
*/
|
|
552
563
|
public async request({
|
|
@@ -554,6 +565,7 @@ export class ZKPassport {
|
|
|
554
565
|
logo,
|
|
555
566
|
purpose,
|
|
556
567
|
scope,
|
|
568
|
+
validity,
|
|
557
569
|
topicOverride,
|
|
558
570
|
keyPairOverride,
|
|
559
571
|
}: {
|
|
@@ -561,6 +573,7 @@ export class ZKPassport {
|
|
|
561
573
|
logo: string
|
|
562
574
|
purpose: string
|
|
563
575
|
scope?: string
|
|
576
|
+
validity?: number
|
|
564
577
|
topicOverride?: string
|
|
565
578
|
keyPairOverride?: { privateKey: Uint8Array; publicKey: Uint8Array }
|
|
566
579
|
}): Promise<QueryBuilder> {
|
|
@@ -576,6 +589,10 @@ export class ZKPassport {
|
|
|
576
589
|
this.topicToService[topic] = { name, logo, purpose, scope }
|
|
577
590
|
this.topicToProofs[topic] = []
|
|
578
591
|
this.topicToExpectedProofCount[topic] = 0
|
|
592
|
+
this.topicToLocalConfig[topic] = {
|
|
593
|
+
// Default to 6 months
|
|
594
|
+
validity: validity || 6 * 30,
|
|
595
|
+
}
|
|
579
596
|
|
|
580
597
|
this.onRequestReceivedCallbacks[topic] = []
|
|
581
598
|
this.onGeneratingProofCallbacks[topic] = []
|
|
@@ -650,7 +667,11 @@ export class ZKPassport {
|
|
|
650
667
|
return this.getZkPassportRequest(topic)
|
|
651
668
|
}
|
|
652
669
|
|
|
653
|
-
private async checkPublicInputs(
|
|
670
|
+
private async checkPublicInputs(
|
|
671
|
+
proofs: Array<ProofResult>,
|
|
672
|
+
queryResult: QueryResult,
|
|
673
|
+
topic: string,
|
|
674
|
+
) {
|
|
654
675
|
let commitmentIn: bigint | undefined
|
|
655
676
|
let commitmentOut: bigint | undefined
|
|
656
677
|
let isCorrect = true
|
|
@@ -720,13 +741,14 @@ export class ZKPassport {
|
|
|
720
741
|
}
|
|
721
742
|
commitmentOut = getCommitmentOutFromIntegrityProof(proofData)
|
|
722
743
|
const currentDate = getCurrentDateFromIntegrityProof(proofData)
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
744
|
+
const todayToCurrentDate = today.getTime() - currentDate.getTime()
|
|
745
|
+
const expectedDifference = this.topicToLocalConfig[topic]?.validity * 86400000
|
|
746
|
+
const actualDifference = today.getTime() - (today.getTime() - expectedDifference)
|
|
747
|
+
// The ID should not expire within the next 6 months (or whatever the custom value is)
|
|
748
|
+
if (todayToCurrentDate >= actualDifference) {
|
|
749
|
+
console.warn(
|
|
750
|
+
`The date used to check the validity of the ID is older than ${this.topicToLocalConfig[topic]?.validity} days. You can ask the user to rescan their ID or ask them to disclose their expiry date`,
|
|
751
|
+
)
|
|
730
752
|
isCorrect = false
|
|
731
753
|
break
|
|
732
754
|
}
|
|
@@ -1309,7 +1331,7 @@ export class ZKPassport {
|
|
|
1309
1331
|
let uniqueIdentifier: string | undefined
|
|
1310
1332
|
if (queryResult) {
|
|
1311
1333
|
const { isCorrect, uniqueIdentifier: uniqueIdentifierFromPublicInputs } =
|
|
1312
|
-
await this.checkPublicInputs(proofsToVerify!, queryResult
|
|
1334
|
+
await this.checkPublicInputs(proofsToVerify!, queryResult!, requestId)
|
|
1313
1335
|
uniqueIdentifier = uniqueIdentifierFromPublicInputs
|
|
1314
1336
|
verified = isCorrect
|
|
1315
1337
|
}
|
|
@@ -1364,6 +1386,7 @@ export class ZKPassport {
|
|
|
1364
1386
|
delete this.topicToWebSocketClient[requestId]
|
|
1365
1387
|
delete this.topicToKeyPair[requestId]
|
|
1366
1388
|
delete this.topicToConfig[requestId]
|
|
1389
|
+
delete this.topicToLocalConfig[requestId]
|
|
1367
1390
|
delete this.topicToSharedSecret[requestId]
|
|
1368
1391
|
delete this.topicToProofs[requestId]
|
|
1369
1392
|
delete this.topicToExpectedProofCount[requestId]
|