@zkpassport/sdk 0.5.6 → 0.5.7-beta.10

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.
@@ -1,6 +0,0 @@
1
- import type { JsonRpcRequest, JsonRpcResponse } from "@zkpassport/utils";
2
- import { WebSocketClient } from "./websocket";
3
- export declare function createJsonRpcRequest(method: string, params: any): JsonRpcRequest;
4
- export declare function createEncryptedJsonRpcRequest(method: string, params: any, sharedSecret: Uint8Array, topic: string): Promise<JsonRpcRequest>;
5
- export declare function sendEncryptedJsonRpcRequest(method: string, params: any, sharedSecret: Uint8Array, topic: string, wsClient: WebSocketClient): Promise<boolean>;
6
- export declare function createJsonRpcResponse(id: string, result: any): JsonRpcResponse;
@@ -1,47 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createJsonRpcRequest = createJsonRpcRequest;
4
- exports.createEncryptedJsonRpcRequest = createEncryptedJsonRpcRequest;
5
- exports.sendEncryptedJsonRpcRequest = sendEncryptedJsonRpcRequest;
6
- exports.createJsonRpcResponse = createJsonRpcResponse;
7
- const crypto_1 = require("crypto");
8
- const encryption_1 = require("./encryption");
9
- const logger_1 = require("./logger");
10
- function createJsonRpcRequest(method, params) {
11
- return {
12
- jsonrpc: "2.0",
13
- id: (0, crypto_1.randomBytes)(16).toString("hex"),
14
- method,
15
- params,
16
- };
17
- }
18
- async function createEncryptedJsonRpcRequest(method, params, sharedSecret, topic) {
19
- const encryptedMessage = await (0, encryption_1.encrypt)(JSON.stringify({ method, params: params || {} }), sharedSecret, topic);
20
- return createJsonRpcRequest("encryptedMessage", {
21
- payload: Buffer.from(encryptedMessage).toString("base64"),
22
- });
23
- }
24
- async function sendEncryptedJsonRpcRequest(method, params, sharedSecret, topic, wsClient) {
25
- try {
26
- const message = { method, params: params || {} };
27
- const encryptedMessage = await (0, encryption_1.encrypt)(JSON.stringify(message), sharedSecret, topic);
28
- const request = createJsonRpcRequest("encryptedMessage", {
29
- payload: Buffer.from(encryptedMessage).toString("base64"),
30
- });
31
- logger_1.noLogger.debug("Sending encrypted message (original):", message);
32
- logger_1.noLogger.debug("Sending encrypted message (encrypted):", request);
33
- wsClient.send(JSON.stringify(request));
34
- return true;
35
- }
36
- catch (error) {
37
- logger_1.noLogger.error("Error sending encrypted message:", error);
38
- return false;
39
- }
40
- }
41
- function createJsonRpcResponse(id, result) {
42
- return {
43
- jsonrpc: "2.0",
44
- id,
45
- result,
46
- };
47
- }
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noLogger = exports.customLogger = void 0;
4
- exports.customLogger = {
5
- debug: (message, ...args) => console.debug(message, ...args),
6
- info: (message, ...args) => console.info(message, ...args),
7
- warn: (message, ...args) => console.warn(message, ...args),
8
- error: (message, ...args) => console.error(message, ...args),
9
- };
10
- exports.noLogger = {
11
- debug: (..._) => { },
12
- info: (..._) => { },
13
- warn: (..._) => { },
14
- error: (..._) => { },
15
- };
@@ -1,39 +0,0 @@
1
- export declare class ZkPassportProver {
2
- private domain?;
3
- private topicToKeyPair;
4
- private topicToWebSocketClient;
5
- private topicToRemoteDomainVerified;
6
- private topicToSharedSecret;
7
- private topicToRemotePublicKey;
8
- private onDomainVerifiedCallbacks;
9
- private onBridgeConnectCallbacks;
10
- private onWebsiteDomainVerifyFailureCallbacks;
11
- constructor();
12
- /**
13
- * @notice Handle an encrypted message.
14
- * @param request The request.
15
- * @param outerRequest The outer request.
16
- */
17
- private handleEncryptedMessage;
18
- /**
19
- * @notice Scan a credentirequest QR code.
20
- * @returns
21
- */
22
- scan(url: string, { keyPairOverride, }?: {
23
- keyPairOverride?: {
24
- privateKey: Uint8Array;
25
- publicKey: Uint8Array;
26
- };
27
- }): Promise<{
28
- domain: string;
29
- requestId: string;
30
- isBridgeConnected: () => boolean;
31
- isDomainVerified: () => boolean;
32
- onDomainVerified: (callback: () => void) => number;
33
- onBridgeConnect: (callback: () => void) => number;
34
- notifyReject: () => Promise<void>;
35
- notifyAccept: () => Promise<void>;
36
- notifyDone: (proof: any) => Promise<void>;
37
- notifyError: (error: string) => Promise<void>;
38
- }>;
39
- }
@@ -1,130 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ZkPassportProver = void 0;
4
- const utils_1 = require("@noble/ciphers/utils");
5
- const websocket_1 = require("./websocket");
6
- const json_rpc_1 = require("./json-rpc");
7
- const encryption_1 = require("./encryption");
8
- const logger_1 = require("./logger");
9
- class ZkPassportProver {
10
- constructor() {
11
- this.topicToKeyPair = {};
12
- this.topicToWebSocketClient = {};
13
- this.topicToRemoteDomainVerified = {};
14
- this.topicToSharedSecret = {};
15
- this.topicToRemotePublicKey = {};
16
- this.onDomainVerifiedCallbacks = {};
17
- this.onBridgeConnectCallbacks = {};
18
- this.onWebsiteDomainVerifyFailureCallbacks = {};
19
- }
20
- /**
21
- * @notice Handle an encrypted message.
22
- * @param request The request.
23
- * @param outerRequest The outer request.
24
- */
25
- async handleEncryptedMessage(topic, request, outerRequest) {
26
- logger_1.noLogger.debug("Received encrypted message:", request);
27
- if (request.method === "hello") {
28
- logger_1.noLogger.info(`Successfully verified origin domain name: ${outerRequest.origin}`);
29
- this.topicToRemoteDomainVerified[topic] = true;
30
- await Promise.all(this.onDomainVerifiedCallbacks[topic].map((callback) => callback()));
31
- }
32
- else if (request.method === "closed_page") {
33
- // TODO: Implement
34
- }
35
- }
36
- /**
37
- * @notice Scan a credentirequest QR code.
38
- * @returns
39
- */
40
- async scan(url, { keyPairOverride, } = {}) {
41
- const parsedUrl = new URL(url);
42
- const domain = parsedUrl.searchParams.get("d");
43
- const topic = parsedUrl.searchParams.get("t");
44
- const pubkeyHex = parsedUrl.searchParams.get("p");
45
- if (!domain || !topic || !pubkeyHex) {
46
- throw new Error("Invalid URL: missing required parameters");
47
- }
48
- const pubkey = new Uint8Array(Buffer.from(pubkeyHex, "hex"));
49
- this.domain = domain;
50
- const keyPair = keyPairOverride || (await (0, encryption_1.generateECDHKeyPair)());
51
- this.topicToKeyPair[topic] = {
52
- privateKey: keyPair.privateKey,
53
- publicKey: keyPair.publicKey,
54
- };
55
- this.topicToRemotePublicKey[topic] = pubkey;
56
- this.topicToSharedSecret[topic] = await (0, encryption_1.getSharedSecret)((0, utils_1.bytesToHex)(keyPair.privateKey), (0, utils_1.bytesToHex)(pubkey));
57
- this.topicToRemoteDomainVerified[topic] = false;
58
- this.onDomainVerifiedCallbacks[topic] = [];
59
- this.onBridgeConnectCallbacks[topic] = [];
60
- // Set up WebSocket connection
61
- const wsClient = (0, websocket_1.getWebSocketClient)(`wss://bridge.zkpassport.id?topic=${topic}&pubkey=${(0, utils_1.bytesToHex)(keyPair.publicKey)}`);
62
- this.topicToWebSocketClient[topic] = wsClient;
63
- wsClient.onopen = async () => {
64
- logger_1.noLogger.info("[mobile] WebSocket connection established");
65
- await Promise.all(this.onBridgeConnectCallbacks[topic].map((callback) => callback()));
66
- // Server sends handshake automatically (when it sees a pubkey in websocket URI)
67
- // wsClient.send(
68
- // JSON.stringify(
69
- // createJsonRpcRequest('handshake', {
70
- // pubkey: bytesToHex(keyPair.publicKey),
71
- // }),
72
- // ),
73
- // )
74
- };
75
- wsClient.addEventListener("message", async (event) => {
76
- logger_1.noLogger.info("[mobile] Received message:", event.data);
77
- try {
78
- const data = JSON.parse(event.data);
79
- const originDomain = data.origin ? new URL(data.origin).hostname : undefined;
80
- // Origin domain must match domain in QR code
81
- if (originDomain !== this.domain) {
82
- logger_1.noLogger.warn(`[mobile] Origin does not match domain in QR code. Expected ${this.domain} but got ${originDomain}`);
83
- return;
84
- }
85
- if (data.method === "encryptedMessage") {
86
- // Decode the payload from base64 to Uint8Array
87
- const payload = new Uint8Array(atob(data.params.payload)
88
- .split("")
89
- .map((c) => c.charCodeAt(0)));
90
- try {
91
- // Decrypt the payload using the shared secret
92
- const decrypted = await (0, encryption_1.decrypt)(payload, this.topicToSharedSecret[topic], topic);
93
- const decryptedJson = JSON.parse(decrypted);
94
- await this.handleEncryptedMessage(topic, decryptedJson, data);
95
- }
96
- catch (error) {
97
- logger_1.noLogger.error("[mobile] Error decrypting message:", error);
98
- }
99
- }
100
- }
101
- catch (error) {
102
- logger_1.noLogger.error("[mobile] Error:", error);
103
- }
104
- });
105
- wsClient.onerror = (error) => {
106
- logger_1.noLogger.error("[mobile] WebSocket error:", error);
107
- };
108
- return {
109
- domain: this.domain,
110
- requestId: topic,
111
- isBridgeConnected: () => this.topicToWebSocketClient[topic].readyState === WebSocket.OPEN,
112
- isDomainVerified: () => this.topicToRemoteDomainVerified[topic] === true,
113
- onDomainVerified: (callback) => this.onDomainVerifiedCallbacks[topic].push(callback),
114
- onBridgeConnect: (callback) => this.onBridgeConnectCallbacks[topic].push(callback),
115
- notifyReject: async () => {
116
- await (0, json_rpc_1.sendEncryptedJsonRpcRequest)("reject", null, this.topicToSharedSecret[topic], topic, this.topicToWebSocketClient[topic]);
117
- },
118
- notifyAccept: async () => {
119
- await (0, json_rpc_1.sendEncryptedJsonRpcRequest)("accept", null, this.topicToSharedSecret[topic], topic, this.topicToWebSocketClient[topic]);
120
- },
121
- notifyDone: async (proof) => {
122
- await (0, json_rpc_1.sendEncryptedJsonRpcRequest)("done", { proof }, this.topicToSharedSecret[topic], topic, this.topicToWebSocketClient[topic]);
123
- },
124
- notifyError: async (error) => {
125
- await (0, json_rpc_1.sendEncryptedJsonRpcRequest)("error", { error }, this.topicToSharedSecret[topic], topic, this.topicToWebSocketClient[topic]);
126
- },
127
- };
128
- }
129
- }
130
- exports.ZkPassportProver = ZkPassportProver;
@@ -1,2 +0,0 @@
1
- export declare function getWebSocketClient(url: string, origin?: string): WebSocket | import("ws").WebSocket;
2
- export type WebSocketClient = ReturnType<typeof getWebSocketClient>;
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getWebSocketClient = getWebSocketClient;
4
- function getWebSocketClient(url, origin) {
5
- if (typeof window !== "undefined" && window.WebSocket) {
6
- // Browser environment
7
- return new WebSocket(url);
8
- }
9
- else {
10
- // Node.js environment
11
- const WebSocket = require("ws");
12
- return new WebSocket(url, {
13
- headers: {
14
- Origin: origin || "nodejs",
15
- },
16
- });
17
- }
18
- }