@zkpassport/sdk 0.2.1 → 0.2.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/cjs/encryption.d.ts +4 -4
- package/dist/cjs/encryption.js +10 -20
- package/dist/cjs/index.d.ts +7 -7
- package/dist/cjs/index.js +150 -124
- package/dist/cjs/json-rpc.d.ts +2 -2
- package/dist/cjs/json-rpc.js +11 -12
- package/dist/cjs/logger.d.ts +7 -2
- package/dist/cjs/logger.js +8 -32
- package/dist/cjs/mobile.js +23 -24
- package/dist/cjs/websocket.js +3 -3
- package/dist/esm/encryption.d.ts +4 -4
- package/dist/esm/encryption.js +5 -5
- package/dist/esm/index.d.ts +7 -7
- package/dist/esm/index.js +132 -128
- package/dist/esm/json-rpc.d.ts +2 -2
- package/dist/esm/json-rpc.js +13 -13
- package/dist/esm/logger.d.ts +7 -2
- package/dist/esm/logger.js +7 -32
- package/dist/esm/mobile.js +25 -25
- package/dist/esm/websocket.js +3 -3
- package/package.json +17 -7
- package/src/encryption.ts +5 -5
- package/src/index.ts +144 -140
- package/src/json-rpc.ts +15 -15
- package/src/logger.ts +7 -38
- package/src/mobile.ts +26 -26
- package/src/websocket.ts +4 -4
package/src/json-rpc.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { randomBytes } from
|
|
2
|
-
import type { JsonRpcRequest, JsonRpcResponse } from
|
|
3
|
-
import { encrypt } from
|
|
4
|
-
import { WebSocketClient } from
|
|
5
|
-
import logger from
|
|
1
|
+
import { randomBytes } from "crypto"
|
|
2
|
+
import type { JsonRpcRequest, JsonRpcResponse } from "@zkpassport/utils"
|
|
3
|
+
import { encrypt } from "./encryption"
|
|
4
|
+
import { WebSocketClient } from "./websocket"
|
|
5
|
+
import { noLogger as logger } from "./logger"
|
|
6
6
|
|
|
7
7
|
export function createJsonRpcRequest(method: string, params: any): JsonRpcRequest {
|
|
8
8
|
return {
|
|
9
|
-
jsonrpc:
|
|
10
|
-
id: randomBytes(16).toString(
|
|
9
|
+
jsonrpc: "2.0",
|
|
10
|
+
id: randomBytes(16).toString("hex"),
|
|
11
11
|
method,
|
|
12
12
|
params,
|
|
13
13
|
}
|
|
@@ -24,8 +24,8 @@ export async function createEncryptedJsonRpcRequest(
|
|
|
24
24
|
sharedSecret,
|
|
25
25
|
topic,
|
|
26
26
|
)
|
|
27
|
-
return createJsonRpcRequest(
|
|
28
|
-
payload: Buffer.from(encryptedMessage).toString(
|
|
27
|
+
return createJsonRpcRequest("encryptedMessage", {
|
|
28
|
+
payload: Buffer.from(encryptedMessage).toString("base64"),
|
|
29
29
|
})
|
|
30
30
|
}
|
|
31
31
|
|
|
@@ -39,22 +39,22 @@ export async function sendEncryptedJsonRpcRequest(
|
|
|
39
39
|
try {
|
|
40
40
|
const message = { method, params: params || {} }
|
|
41
41
|
const encryptedMessage = await encrypt(JSON.stringify(message), sharedSecret, topic)
|
|
42
|
-
const request = createJsonRpcRequest(
|
|
43
|
-
payload: Buffer.from(encryptedMessage).toString(
|
|
42
|
+
const request = createJsonRpcRequest("encryptedMessage", {
|
|
43
|
+
payload: Buffer.from(encryptedMessage).toString("base64"),
|
|
44
44
|
})
|
|
45
|
-
logger.debug(
|
|
46
|
-
logger.debug(
|
|
45
|
+
logger.debug("Sending encrypted message (original):", message)
|
|
46
|
+
logger.debug("Sending encrypted message (encrypted):", request)
|
|
47
47
|
wsClient.send(JSON.stringify(request))
|
|
48
48
|
return true
|
|
49
49
|
} catch (error) {
|
|
50
|
-
logger.error(
|
|
50
|
+
logger.error("Error sending encrypted message:", error)
|
|
51
51
|
return false
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
export function createJsonRpcResponse(id: string, result: any): JsonRpcResponse {
|
|
56
56
|
return {
|
|
57
|
-
jsonrpc:
|
|
57
|
+
jsonrpc: "2.0",
|
|
58
58
|
id,
|
|
59
59
|
result,
|
|
60
60
|
}
|
package/src/logger.ts
CHANGED
|
@@ -1,44 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
// import colors from 'colors/safe'
|
|
3
|
-
// import util from 'util'
|
|
4
|
-
|
|
5
|
-
// const logger = createLogger({
|
|
6
|
-
// level: 'debug',
|
|
7
|
-
// format: format.combine(
|
|
8
|
-
// format.timestamp({ format: 'HH:mm' }),
|
|
9
|
-
// format.printf(({ timestamp, level, message, additionalInfo }) => {
|
|
10
|
-
// const colorMap = {
|
|
11
|
-
// debug: colors.cyan,
|
|
12
|
-
// info: colors.green,
|
|
13
|
-
// warn: colors.yellow,
|
|
14
|
-
// error: colors.red,
|
|
15
|
-
// } as const
|
|
16
|
-
// const coloredLevel = (colorMap[level as keyof typeof colorMap] || colors.white)(level.toUpperCase())
|
|
17
|
-
|
|
18
|
-
// let logMessage = `${timestamp} [${coloredLevel}] ${message}`
|
|
19
|
-
|
|
20
|
-
// if (additionalInfo && additionalInfo.length > 0) {
|
|
21
|
-
// logMessage += ' ' + util.inspect(additionalInfo, { depth: null, colors: true })
|
|
22
|
-
// }
|
|
23
|
-
|
|
24
|
-
// return logMessage
|
|
25
|
-
// }),
|
|
26
|
-
// ),
|
|
27
|
-
// transports: [new transports.Console()],
|
|
28
|
-
// })
|
|
29
|
-
|
|
30
|
-
// const customLogger = {
|
|
31
|
-
// debug: (message: string, ...args: any[]) => logger.debug(message, { additionalInfo: args }),
|
|
32
|
-
// info: (message: string, ...args: any[]) => logger.info(message, { additionalInfo: args }),
|
|
33
|
-
// warn: (message: string, ...args: any[]) => logger.warn(message, { additionalInfo: args }),
|
|
34
|
-
// error: (message: string, ...args: any[]) => logger.error(message, { additionalInfo: args }),
|
|
35
|
-
// }
|
|
36
|
-
|
|
37
|
-
const customLogger = {
|
|
1
|
+
export const customLogger = {
|
|
38
2
|
debug: (message: string, ...args: any[]) => console.debug(message, ...args),
|
|
39
3
|
info: (message: string, ...args: any[]) => console.info(message, ...args),
|
|
40
4
|
warn: (message: string, ...args: any[]) => console.warn(message, ...args),
|
|
41
5
|
error: (message: string, ...args: any[]) => console.error(message, ...args),
|
|
42
6
|
}
|
|
43
7
|
|
|
44
|
-
export
|
|
8
|
+
export const noLogger = {
|
|
9
|
+
debug: (..._: any[]) => {},
|
|
10
|
+
info: (..._: any[]) => {},
|
|
11
|
+
warn: (..._: any[]) => {},
|
|
12
|
+
error: (..._: any[]) => {},
|
|
13
|
+
}
|
package/src/mobile.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { bytesToHex } from
|
|
2
|
-
import { getWebSocketClient, WebSocketClient } from
|
|
3
|
-
import { sendEncryptedJsonRpcRequest } from
|
|
4
|
-
import { decrypt, generateECDHKeyPair, getSharedSecret } from
|
|
5
|
-
import type { JsonRpcRequest } from
|
|
6
|
-
import logger from
|
|
1
|
+
import { bytesToHex } from "@noble/ciphers/utils"
|
|
2
|
+
import { getWebSocketClient, WebSocketClient } from "./websocket"
|
|
3
|
+
import { sendEncryptedJsonRpcRequest } from "./json-rpc"
|
|
4
|
+
import { decrypt, generateECDHKeyPair, getSharedSecret } from "./encryption"
|
|
5
|
+
import type { JsonRpcRequest } from "@zkpassport/utils"
|
|
6
|
+
import { noLogger as logger } from "./logger"
|
|
7
7
|
|
|
8
8
|
export class ZkPassportProver {
|
|
9
9
|
private domain?: string
|
|
@@ -29,12 +29,12 @@ export class ZkPassportProver {
|
|
|
29
29
|
request: JsonRpcRequest,
|
|
30
30
|
outerRequest: JsonRpcRequest,
|
|
31
31
|
) {
|
|
32
|
-
logger.debug(
|
|
33
|
-
if (request.method ===
|
|
32
|
+
logger.debug("Received encrypted message:", request)
|
|
33
|
+
if (request.method === "hello") {
|
|
34
34
|
logger.info(`Successfully verified origin domain name: ${outerRequest.origin}`)
|
|
35
35
|
this.topicToRemoteDomainVerified[topic] = true
|
|
36
36
|
await Promise.all(this.onDomainVerifiedCallbacks[topic].map((callback) => callback()))
|
|
37
|
-
} else if (request.method ===
|
|
37
|
+
} else if (request.method === "closed_page") {
|
|
38
38
|
// TODO: Implement
|
|
39
39
|
}
|
|
40
40
|
}
|
|
@@ -52,15 +52,15 @@ export class ZkPassportProver {
|
|
|
52
52
|
} = {},
|
|
53
53
|
) {
|
|
54
54
|
const parsedUrl = new URL(url)
|
|
55
|
-
const domain = parsedUrl.searchParams.get(
|
|
56
|
-
const topic = parsedUrl.searchParams.get(
|
|
57
|
-
const pubkeyHex = parsedUrl.searchParams.get(
|
|
55
|
+
const domain = parsedUrl.searchParams.get("d")
|
|
56
|
+
const topic = parsedUrl.searchParams.get("t")
|
|
57
|
+
const pubkeyHex = parsedUrl.searchParams.get("p")
|
|
58
58
|
|
|
59
59
|
if (!domain || !topic || !pubkeyHex) {
|
|
60
|
-
throw new Error(
|
|
60
|
+
throw new Error("Invalid URL: missing required parameters")
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
const pubkey = new Uint8Array(Buffer.from(pubkeyHex,
|
|
63
|
+
const pubkey = new Uint8Array(Buffer.from(pubkeyHex, "hex"))
|
|
64
64
|
|
|
65
65
|
this.domain = domain
|
|
66
66
|
const keyPair = keyPairOverride || (await generateECDHKeyPair())
|
|
@@ -85,7 +85,7 @@ export class ZkPassportProver {
|
|
|
85
85
|
this.topicToWebSocketClient[topic] = wsClient
|
|
86
86
|
|
|
87
87
|
wsClient.onopen = async () => {
|
|
88
|
-
logger.info(
|
|
88
|
+
logger.info("[mobile] WebSocket connection established")
|
|
89
89
|
await Promise.all(this.onBridgeConnectCallbacks[topic].map((callback) => callback()))
|
|
90
90
|
// Server sends handshake automatically (when it sees a pubkey in websocket URI)
|
|
91
91
|
// wsClient.send(
|
|
@@ -97,8 +97,8 @@ export class ZkPassportProver {
|
|
|
97
97
|
// )
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
wsClient.addEventListener(
|
|
101
|
-
logger.info(
|
|
100
|
+
wsClient.addEventListener("message", async (event: any) => {
|
|
101
|
+
logger.info("[mobile] Received message:", event.data)
|
|
102
102
|
|
|
103
103
|
try {
|
|
104
104
|
const data: JsonRpcRequest = JSON.parse(event.data)
|
|
@@ -111,11 +111,11 @@ export class ZkPassportProver {
|
|
|
111
111
|
return
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
-
if (data.method ===
|
|
114
|
+
if (data.method === "encryptedMessage") {
|
|
115
115
|
// Decode the payload from base64 to Uint8Array
|
|
116
116
|
const payload = new Uint8Array(
|
|
117
117
|
atob(data.params.payload)
|
|
118
|
-
.split(
|
|
118
|
+
.split("")
|
|
119
119
|
.map((c) => c.charCodeAt(0)),
|
|
120
120
|
)
|
|
121
121
|
try {
|
|
@@ -124,16 +124,16 @@ export class ZkPassportProver {
|
|
|
124
124
|
const decryptedJson: JsonRpcRequest = JSON.parse(decrypted)
|
|
125
125
|
await this.handleEncryptedMessage(topic, decryptedJson, data)
|
|
126
126
|
} catch (error) {
|
|
127
|
-
logger.error(
|
|
127
|
+
logger.error("[mobile] Error decrypting message:", error)
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
} catch (error) {
|
|
131
|
-
logger.error(
|
|
131
|
+
logger.error("[mobile] Error:", error)
|
|
132
132
|
}
|
|
133
133
|
})
|
|
134
134
|
|
|
135
135
|
wsClient.onerror = (error: Event) => {
|
|
136
|
-
logger.error(
|
|
136
|
+
logger.error("[mobile] WebSocket error:", error)
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
return {
|
|
@@ -147,7 +147,7 @@ export class ZkPassportProver {
|
|
|
147
147
|
this.onBridgeConnectCallbacks[topic].push(callback),
|
|
148
148
|
notifyReject: async () => {
|
|
149
149
|
await sendEncryptedJsonRpcRequest(
|
|
150
|
-
|
|
150
|
+
"reject",
|
|
151
151
|
null,
|
|
152
152
|
this.topicToSharedSecret[topic],
|
|
153
153
|
topic,
|
|
@@ -156,7 +156,7 @@ export class ZkPassportProver {
|
|
|
156
156
|
},
|
|
157
157
|
notifyAccept: async () => {
|
|
158
158
|
await sendEncryptedJsonRpcRequest(
|
|
159
|
-
|
|
159
|
+
"accept",
|
|
160
160
|
null,
|
|
161
161
|
this.topicToSharedSecret[topic],
|
|
162
162
|
topic,
|
|
@@ -165,7 +165,7 @@ export class ZkPassportProver {
|
|
|
165
165
|
},
|
|
166
166
|
notifyDone: async (proof: any) => {
|
|
167
167
|
await sendEncryptedJsonRpcRequest(
|
|
168
|
-
|
|
168
|
+
"done",
|
|
169
169
|
{ proof },
|
|
170
170
|
this.topicToSharedSecret[topic],
|
|
171
171
|
topic,
|
|
@@ -174,7 +174,7 @@ export class ZkPassportProver {
|
|
|
174
174
|
},
|
|
175
175
|
notifyError: async (error: string) => {
|
|
176
176
|
await sendEncryptedJsonRpcRequest(
|
|
177
|
-
|
|
177
|
+
"error",
|
|
178
178
|
{ error },
|
|
179
179
|
this.topicToSharedSecret[topic],
|
|
180
180
|
topic,
|
package/src/websocket.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
export function getWebSocketClient(url: string, origin?: string) {
|
|
2
|
-
if (typeof window !==
|
|
2
|
+
if (typeof window !== "undefined" && window.WebSocket) {
|
|
3
3
|
// Browser environment
|
|
4
4
|
return new WebSocket(url)
|
|
5
5
|
} else {
|
|
6
6
|
// Node.js environment
|
|
7
|
-
const WebSocket = require(
|
|
7
|
+
const WebSocket = require("ws")
|
|
8
8
|
return new WebSocket(url, {
|
|
9
9
|
headers: {
|
|
10
|
-
Origin: origin ||
|
|
10
|
+
Origin: origin || "nodejs",
|
|
11
11
|
},
|
|
12
|
-
}) as import(
|
|
12
|
+
}) as import("ws").WebSocket
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
|