@zkpassport/sdk 0.1.2 → 0.2.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.
- package/README.md +80 -25
- package/dist/{encryption.d.ts → cjs/encryption.d.ts} +4 -4
- package/dist/cjs/encryption.js +79 -0
- package/dist/cjs/index.d.ts +191 -0
- package/dist/cjs/index.js +857 -0
- package/dist/{json-rpc.d.ts → cjs/json-rpc.d.ts} +1 -1
- package/dist/cjs/json-rpc.js +48 -0
- package/dist/{logger.js → cjs/logger.js} +5 -38
- package/dist/cjs/mobile.js +131 -0
- package/dist/{websocket.js → cjs/websocket.js} +2 -2
- package/dist/esm/encryption.d.ts +7 -0
- package/dist/esm/encryption.js +40 -0
- package/dist/esm/index.d.ts +191 -0
- package/dist/esm/index.js +846 -0
- package/dist/esm/json-rpc.d.ts +6 -0
- package/dist/esm/json-rpc.js +41 -0
- package/dist/esm/logger.d.ts +7 -0
- package/dist/esm/logger.js +37 -0
- package/dist/esm/mobile.d.ts +39 -0
- package/dist/esm/mobile.js +126 -0
- package/dist/esm/websocket.d.ts +2 -0
- package/dist/esm/websocket.js +15 -0
- package/package.json +16 -8
- package/src/index.ts +974 -63
- package/src/json-rpc.ts +6 -2
- package/src/mobile.ts +14 -5
- package/tsconfig.json +14 -9
- package/dist/constants/index.d.ts +0 -13
- package/dist/constants/index.js +0 -52
- package/dist/encryption.js +0 -126
- package/dist/index.d.ts +0 -79
- package/dist/index.js +0 -384
- package/dist/json-rpc.js +0 -105
- package/dist/mobile.js +0 -253
- package/dist/types/countries.d.ts +0 -1
- package/dist/types/countries.js +0 -2
- package/dist/types/credentials.d.ts +0 -17
- package/dist/types/credentials.js +0 -2
- package/dist/types/index.d.ts +0 -4
- package/dist/types/index.js +0 -2
- package/dist/types/json-rpc.d.ts +0 -12
- package/dist/types/json-rpc.js +0 -2
- package/dist/types/query-result.d.ts +0 -46
- package/dist/types/query-result.js +0 -2
- package/src/circuits/proof_age.json +0 -1
- package/src/constants/index.ts +0 -54
- package/src/types/countries.ts +0 -278
- package/src/types/credentials.ts +0 -40
- package/src/types/index.ts +0 -13
- package/src/types/json-rpc.ts +0 -13
- package/src/types/query-result.ts +0 -49
- /package/dist/{logger.d.ts → cjs/logger.d.ts} +0 -0
- /package/dist/{mobile.d.ts → cjs/mobile.d.ts} +0 -0
- /package/dist/{websocket.d.ts → cjs/websocket.d.ts} +0 -0
package/dist/index.js
DELETED
|
@@ -1,384 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
-
if (k2 === undefined) k2 = k;
|
|
15
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
16
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
17
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
18
|
-
}
|
|
19
|
-
Object.defineProperty(o, k2, desc);
|
|
20
|
-
}) : (function(o, m, k, k2) {
|
|
21
|
-
if (k2 === undefined) k2 = k;
|
|
22
|
-
o[k2] = m[k];
|
|
23
|
-
}));
|
|
24
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
25
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
26
|
-
};
|
|
27
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
28
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
29
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
30
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
31
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
32
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
33
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
34
|
-
});
|
|
35
|
-
};
|
|
36
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
37
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
38
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
39
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
40
|
-
function step(op) {
|
|
41
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
42
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
43
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
44
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
45
|
-
switch (op[0]) {
|
|
46
|
-
case 0: case 1: t = op; break;
|
|
47
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
48
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
49
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
50
|
-
default:
|
|
51
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
52
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
53
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
54
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
55
|
-
if (t[2]) _.ops.pop();
|
|
56
|
-
_.trys.pop(); continue;
|
|
57
|
-
}
|
|
58
|
-
op = body.call(thisArg, _);
|
|
59
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
60
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
64
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
65
|
-
};
|
|
66
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
67
|
-
exports.ZkPassport = void 0;
|
|
68
|
-
var crypto_1 = require("crypto");
|
|
69
|
-
var i18n_iso_countries_1 = require("i18n-iso-countries");
|
|
70
|
-
//import { UltraHonkBackend, ProofData, CompiledCircuit } from '@noir-lang/backend_barretenberg'
|
|
71
|
-
var utils_1 = require("@noble/ciphers/utils");
|
|
72
|
-
var websocket_1 = require("./websocket");
|
|
73
|
-
var json_rpc_1 = require("./json-rpc");
|
|
74
|
-
var encryption_1 = require("./encryption");
|
|
75
|
-
var logger_1 = __importDefault(require("./logger"));
|
|
76
|
-
(0, i18n_iso_countries_1.registerLocale)(require('i18n-iso-countries/langs/en.json'));
|
|
77
|
-
function normalizeCountry(country) {
|
|
78
|
-
var normalizedCountry;
|
|
79
|
-
var alpha3 = (0, i18n_iso_countries_1.getAlpha3Code)(country, 'en');
|
|
80
|
-
normalizedCountry = alpha3 || country;
|
|
81
|
-
return normalizedCountry;
|
|
82
|
-
}
|
|
83
|
-
function numericalCompare(fnName, key, value, requestId, requestIdToConfig) {
|
|
84
|
-
var _a;
|
|
85
|
-
requestIdToConfig[requestId][key] = __assign(__assign({}, requestIdToConfig[requestId][key]), (_a = {}, _a[fnName] = value, _a));
|
|
86
|
-
}
|
|
87
|
-
function rangeCompare(key, value, requestId, requestIdToConfig) {
|
|
88
|
-
requestIdToConfig[requestId][key] = __assign(__assign({}, requestIdToConfig[requestId][key]), { range: value });
|
|
89
|
-
}
|
|
90
|
-
function generalCompare(fnName, key, value, requestId, requestIdToConfig) {
|
|
91
|
-
var _a;
|
|
92
|
-
requestIdToConfig[requestId][key] = __assign(__assign({}, requestIdToConfig[requestId][key]), (_a = {}, _a[fnName] = value, _a));
|
|
93
|
-
}
|
|
94
|
-
__exportStar(require("./constants"), exports);
|
|
95
|
-
__exportStar(require("./types"), exports);
|
|
96
|
-
var ZkPassport = /** @class */ (function () {
|
|
97
|
-
function ZkPassport(_domain) {
|
|
98
|
-
this.topicToConfig = {};
|
|
99
|
-
this.topicToKeyPair = {};
|
|
100
|
-
this.topicToWebSocketClient = {};
|
|
101
|
-
this.topicToSharedSecret = {};
|
|
102
|
-
this.topicToQRCodeScanned = {};
|
|
103
|
-
this.onQRCodeScannedCallbacks = {};
|
|
104
|
-
this.onGeneratingProofCallbacks = {};
|
|
105
|
-
this.onBridgeConnectCallbacks = {};
|
|
106
|
-
this.onProofGeneratedCallbacks = {};
|
|
107
|
-
this.onRejectCallbacks = {};
|
|
108
|
-
this.onErrorCallbacks = {};
|
|
109
|
-
this.topicToService = {};
|
|
110
|
-
if (!_domain && typeof window === 'undefined') {
|
|
111
|
-
throw new Error('Domain argument is required in Node.js environment');
|
|
112
|
-
}
|
|
113
|
-
this.domain = _domain || window.location.hostname;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* @notice Handle an encrypted message.
|
|
117
|
-
* @param request The request.
|
|
118
|
-
* @param outerRequest The outer request.
|
|
119
|
-
*/
|
|
120
|
-
ZkPassport.prototype.handleEncryptedMessage = function (topic, request, outerRequest) {
|
|
121
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
122
|
-
return __generator(this, function (_a) {
|
|
123
|
-
switch (_a.label) {
|
|
124
|
-
case 0:
|
|
125
|
-
logger_1.default.debug('Received encrypted message:', request);
|
|
126
|
-
if (!(request.method === 'accept')) return [3 /*break*/, 2];
|
|
127
|
-
logger_1.default.debug("User accepted the request and is generating a proof");
|
|
128
|
-
return [4 /*yield*/, Promise.all(this.onGeneratingProofCallbacks[topic].map(function (callback) { return callback(topic); }))];
|
|
129
|
-
case 1:
|
|
130
|
-
_a.sent();
|
|
131
|
-
return [3 /*break*/, 8];
|
|
132
|
-
case 2:
|
|
133
|
-
if (!(request.method === 'reject')) return [3 /*break*/, 4];
|
|
134
|
-
logger_1.default.debug("User rejected the request");
|
|
135
|
-
return [4 /*yield*/, Promise.all(this.onRejectCallbacks[topic].map(function (callback) { return callback(); }))];
|
|
136
|
-
case 3:
|
|
137
|
-
_a.sent();
|
|
138
|
-
return [3 /*break*/, 8];
|
|
139
|
-
case 4:
|
|
140
|
-
if (!(request.method === 'done')) return [3 /*break*/, 6];
|
|
141
|
-
logger_1.default.debug("User generated proof");
|
|
142
|
-
return [4 /*yield*/, Promise.all(this.onProofGeneratedCallbacks[topic].map(function (callback) { return callback(request.params.result); }))];
|
|
143
|
-
case 5:
|
|
144
|
-
_a.sent();
|
|
145
|
-
return [3 /*break*/, 8];
|
|
146
|
-
case 6:
|
|
147
|
-
if (!(request.method === 'error')) return [3 /*break*/, 8];
|
|
148
|
-
return [4 /*yield*/, Promise.all(this.onErrorCallbacks[topic].map(function (callback) { return callback(request.params.error); }))];
|
|
149
|
-
case 7:
|
|
150
|
-
_a.sent();
|
|
151
|
-
_a.label = 8;
|
|
152
|
-
case 8: return [2 /*return*/];
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
};
|
|
157
|
-
ZkPassport.prototype.getZkPassportRequest = function (topic) {
|
|
158
|
-
var _this = this;
|
|
159
|
-
return {
|
|
160
|
-
eq: function (key, value) {
|
|
161
|
-
if (key === 'issuing_country' || key === 'nationality') {
|
|
162
|
-
value = normalizeCountry(value);
|
|
163
|
-
}
|
|
164
|
-
generalCompare('eq', key, value, topic, _this.topicToConfig);
|
|
165
|
-
return _this.getZkPassportRequest(topic);
|
|
166
|
-
},
|
|
167
|
-
gte: function (key, value) {
|
|
168
|
-
numericalCompare('gte', key, value, topic, _this.topicToConfig);
|
|
169
|
-
return _this.getZkPassportRequest(topic);
|
|
170
|
-
},
|
|
171
|
-
gt: function (key, value) {
|
|
172
|
-
numericalCompare('gt', key, value, topic, _this.topicToConfig);
|
|
173
|
-
return _this.getZkPassportRequest(topic);
|
|
174
|
-
},
|
|
175
|
-
lte: function (key, value) {
|
|
176
|
-
numericalCompare('lte', key, value, topic, _this.topicToConfig);
|
|
177
|
-
return _this.getZkPassportRequest(topic);
|
|
178
|
-
},
|
|
179
|
-
lt: function (key, value) {
|
|
180
|
-
numericalCompare('lt', key, value, topic, _this.topicToConfig);
|
|
181
|
-
return _this.getZkPassportRequest(topic);
|
|
182
|
-
},
|
|
183
|
-
range: function (key, start, end) {
|
|
184
|
-
rangeCompare(key, [start, end], topic, _this.topicToConfig);
|
|
185
|
-
return _this.getZkPassportRequest(topic);
|
|
186
|
-
},
|
|
187
|
-
in: function (key, value) {
|
|
188
|
-
if (key === 'issuing_country' || key === 'nationality') {
|
|
189
|
-
value = value.map(function (v) { return normalizeCountry(v); });
|
|
190
|
-
}
|
|
191
|
-
generalCompare('in', key, value, topic, _this.topicToConfig);
|
|
192
|
-
return _this.getZkPassportRequest(topic);
|
|
193
|
-
},
|
|
194
|
-
out: function (key, value) {
|
|
195
|
-
if (key === 'issuing_country' || key === 'nationality') {
|
|
196
|
-
value = value.map(function (v) { return normalizeCountry(v); });
|
|
197
|
-
}
|
|
198
|
-
generalCompare('out', key, value, topic, _this.topicToConfig);
|
|
199
|
-
return _this.getZkPassportRequest(topic);
|
|
200
|
-
},
|
|
201
|
-
disclose: function (key) {
|
|
202
|
-
_this.topicToConfig[topic][key] = __assign(__assign({}, _this.topicToConfig[topic][key]), { disclose: true });
|
|
203
|
-
return _this.getZkPassportRequest(topic);
|
|
204
|
-
},
|
|
205
|
-
/*checkAML: (country?: CountryName | Alpha2Code | Alpha3Code) => {
|
|
206
|
-
return this.getZkPassportRequest(topic)
|
|
207
|
-
},*/
|
|
208
|
-
done: function () {
|
|
209
|
-
var base64Config = Buffer.from(JSON.stringify(_this.topicToConfig[topic])).toString('base64');
|
|
210
|
-
var base64Service = Buffer.from(JSON.stringify(_this.topicToService[topic])).toString('base64');
|
|
211
|
-
var pubkey = (0, utils_1.bytesToHex)(_this.topicToKeyPair[topic].publicKey);
|
|
212
|
-
return {
|
|
213
|
-
url: "https://zkpassport.id/r?d=".concat(_this.domain, "&t=").concat(topic, "&c=").concat(base64Config, "&s=").concat(base64Service, "&p=").concat(pubkey),
|
|
214
|
-
requestId: topic,
|
|
215
|
-
onQRCodeScanned: function (callback) { return _this.onQRCodeScannedCallbacks[topic].push(callback); },
|
|
216
|
-
onGeneratingProof: function (callback) { return _this.onGeneratingProofCallbacks[topic].push(callback); },
|
|
217
|
-
onBridgeConnect: function (callback) { return _this.onBridgeConnectCallbacks[topic].push(callback); },
|
|
218
|
-
onProofGenerated: function (callback) {
|
|
219
|
-
return _this.onProofGeneratedCallbacks[topic].push(callback);
|
|
220
|
-
},
|
|
221
|
-
onReject: function (callback) { return _this.onRejectCallbacks[topic].push(callback); },
|
|
222
|
-
onError: function (callback) { return _this.onErrorCallbacks[topic].push(callback); },
|
|
223
|
-
isBridgeConnected: function () { return _this.topicToWebSocketClient[topic].readyState === WebSocket.OPEN; },
|
|
224
|
-
isQRCodeScanned: function () { return _this.topicToQRCodeScanned[topic] === true; },
|
|
225
|
-
};
|
|
226
|
-
},
|
|
227
|
-
};
|
|
228
|
-
};
|
|
229
|
-
/**
|
|
230
|
-
* @notice Create a new request.
|
|
231
|
-
* @returns The query builder object.
|
|
232
|
-
*/
|
|
233
|
-
ZkPassport.prototype.request = function (_a) {
|
|
234
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
235
|
-
var topic, keyPair, _c, wsClient;
|
|
236
|
-
var _this = this;
|
|
237
|
-
var name = _b.name, logo = _b.logo, purpose = _b.purpose, topicOverride = _b.topicOverride, keyPairOverride = _b.keyPairOverride;
|
|
238
|
-
return __generator(this, function (_d) {
|
|
239
|
-
switch (_d.label) {
|
|
240
|
-
case 0:
|
|
241
|
-
topic = topicOverride || (0, crypto_1.randomBytes)(16).toString('hex');
|
|
242
|
-
_c = keyPairOverride;
|
|
243
|
-
if (_c) return [3 /*break*/, 2];
|
|
244
|
-
return [4 /*yield*/, (0, encryption_1.generateECDHKeyPair)()];
|
|
245
|
-
case 1:
|
|
246
|
-
_c = (_d.sent());
|
|
247
|
-
_d.label = 2;
|
|
248
|
-
case 2:
|
|
249
|
-
keyPair = _c;
|
|
250
|
-
this.topicToKeyPair[topic] = {
|
|
251
|
-
privateKey: keyPair.privateKey,
|
|
252
|
-
publicKey: keyPair.publicKey,
|
|
253
|
-
};
|
|
254
|
-
this.topicToConfig[topic] = {};
|
|
255
|
-
this.topicToService[topic] = { name: name, logo: logo, purpose: purpose };
|
|
256
|
-
this.onQRCodeScannedCallbacks[topic] = [];
|
|
257
|
-
this.onGeneratingProofCallbacks[topic] = [];
|
|
258
|
-
this.onBridgeConnectCallbacks[topic] = [];
|
|
259
|
-
this.onProofGeneratedCallbacks[topic] = [];
|
|
260
|
-
this.onRejectCallbacks[topic] = [];
|
|
261
|
-
this.onErrorCallbacks[topic] = [];
|
|
262
|
-
wsClient = (0, websocket_1.getWebSocketClient)("wss://bridge.zkpassport.id?topic=".concat(topic), this.domain);
|
|
263
|
-
this.topicToWebSocketClient[topic] = wsClient;
|
|
264
|
-
wsClient.onopen = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
265
|
-
return __generator(this, function (_a) {
|
|
266
|
-
switch (_a.label) {
|
|
267
|
-
case 0:
|
|
268
|
-
logger_1.default.info('[frontend] WebSocket connection established');
|
|
269
|
-
return [4 /*yield*/, Promise.all(this.onBridgeConnectCallbacks[topic].map(function (callback) { return callback(); }))];
|
|
270
|
-
case 1:
|
|
271
|
-
_a.sent();
|
|
272
|
-
return [2 /*return*/];
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
}); };
|
|
276
|
-
wsClient.addEventListener('message', function (event) { return __awaiter(_this, void 0, void 0, function () {
|
|
277
|
-
var data, _a, _b, encryptedMessage, payload, decrypted, decryptedJson, error_1, error_2;
|
|
278
|
-
return __generator(this, function (_c) {
|
|
279
|
-
switch (_c.label) {
|
|
280
|
-
case 0:
|
|
281
|
-
logger_1.default.debug('[frontend] Received message:', event.data);
|
|
282
|
-
_c.label = 1;
|
|
283
|
-
case 1:
|
|
284
|
-
_c.trys.push([1, 11, , 12]);
|
|
285
|
-
data = JSON.parse(event.data);
|
|
286
|
-
if (!(data.method === 'handshake')) return [3 /*break*/, 5];
|
|
287
|
-
logger_1.default.debug('[frontend] Received handshake:', event.data);
|
|
288
|
-
this.topicToQRCodeScanned[topic] = true;
|
|
289
|
-
_a = this.topicToSharedSecret;
|
|
290
|
-
_b = topic;
|
|
291
|
-
return [4 /*yield*/, (0, encryption_1.getSharedSecret)((0, utils_1.bytesToHex)(keyPair.privateKey), data.params.pubkey)];
|
|
292
|
-
case 2:
|
|
293
|
-
_a[_b] = _c.sent();
|
|
294
|
-
logger_1.default.debug('[frontend] Shared secret:', Buffer.from(this.topicToSharedSecret[topic]).toString('hex'));
|
|
295
|
-
return [4 /*yield*/, (0, json_rpc_1.createEncryptedJsonRpcRequest)('hello', null, this.topicToSharedSecret[topic], topic)];
|
|
296
|
-
case 3:
|
|
297
|
-
encryptedMessage = _c.sent();
|
|
298
|
-
logger_1.default.debug('[frontend] Sending encrypted message:', encryptedMessage);
|
|
299
|
-
wsClient.send(JSON.stringify(encryptedMessage));
|
|
300
|
-
return [4 /*yield*/, Promise.all(this.onQRCodeScannedCallbacks[topic].map(function (callback) { return callback(); }))];
|
|
301
|
-
case 4:
|
|
302
|
-
_c.sent();
|
|
303
|
-
return [2 /*return*/];
|
|
304
|
-
case 5:
|
|
305
|
-
if (!(data.method === 'encryptedMessage')) return [3 /*break*/, 10];
|
|
306
|
-
payload = new Uint8Array(atob(data.params.payload)
|
|
307
|
-
.split('')
|
|
308
|
-
.map(function (c) { return c.charCodeAt(0); }));
|
|
309
|
-
_c.label = 6;
|
|
310
|
-
case 6:
|
|
311
|
-
_c.trys.push([6, 8, , 9]);
|
|
312
|
-
return [4 /*yield*/, (0, encryption_1.decrypt)(payload, this.topicToSharedSecret[topic], topic)];
|
|
313
|
-
case 7:
|
|
314
|
-
decrypted = _c.sent();
|
|
315
|
-
decryptedJson = JSON.parse(decrypted);
|
|
316
|
-
this.handleEncryptedMessage(topic, decryptedJson, data);
|
|
317
|
-
return [3 /*break*/, 9];
|
|
318
|
-
case 8:
|
|
319
|
-
error_1 = _c.sent();
|
|
320
|
-
logger_1.default.error('[frontend] Error decrypting message:', error_1);
|
|
321
|
-
return [3 /*break*/, 9];
|
|
322
|
-
case 9: return [2 /*return*/];
|
|
323
|
-
case 10: return [3 /*break*/, 12];
|
|
324
|
-
case 11:
|
|
325
|
-
error_2 = _c.sent();
|
|
326
|
-
logger_1.default.error('[frontend] Error:', error_2);
|
|
327
|
-
return [3 /*break*/, 12];
|
|
328
|
-
case 12: return [2 /*return*/];
|
|
329
|
-
}
|
|
330
|
-
});
|
|
331
|
-
}); });
|
|
332
|
-
wsClient.onerror = function (error) {
|
|
333
|
-
logger_1.default.error('[frontend] WebSocket error:', error);
|
|
334
|
-
};
|
|
335
|
-
return [2 /*return*/, this.getZkPassportRequest(topic)];
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
});
|
|
339
|
-
};
|
|
340
|
-
/**
|
|
341
|
-
* @notice Verifies a proof.
|
|
342
|
-
* @param proof The proof to verify.
|
|
343
|
-
* @returns True if the proof is valid, false otherwise.
|
|
344
|
-
*/
|
|
345
|
-
/*public verify(result: ProofResult) {
|
|
346
|
-
const backend = new UltraHonkBackend(proofOfAgeCircuit as CompiledCircuit)
|
|
347
|
-
const proofData: ProofData = {
|
|
348
|
-
proof: Buffer.from(result.proof as string, 'hex'),
|
|
349
|
-
// TODO: extract the public inputs from the proof
|
|
350
|
-
publicInputs: [],
|
|
351
|
-
}
|
|
352
|
-
return backend.verifyProof(proofData)
|
|
353
|
-
}*/
|
|
354
|
-
/**
|
|
355
|
-
* @notice Returns the URL of the request.
|
|
356
|
-
* @param requestId The request ID.
|
|
357
|
-
* @returns The URL of the request.
|
|
358
|
-
*/
|
|
359
|
-
ZkPassport.prototype.getUrl = function (requestId) {
|
|
360
|
-
var pubkey = (0, utils_1.bytesToHex)(this.topicToKeyPair[requestId].publicKey);
|
|
361
|
-
var base64Config = Buffer.from(JSON.stringify(this.topicToConfig[requestId])).toString('base64');
|
|
362
|
-
var base64Service = Buffer.from(JSON.stringify(this.topicToService[requestId])).toString('base64');
|
|
363
|
-
return "https://zkpassport.id/r?d=".concat(this.domain, "&t=").concat(requestId, "&c=").concat(base64Config, "&s=").concat(base64Service, "&p=").concat(pubkey);
|
|
364
|
-
};
|
|
365
|
-
/**
|
|
366
|
-
* @notice Cancels a request by closing the WebSocket connection and deleting the associated data.
|
|
367
|
-
* @param requestId The request ID.
|
|
368
|
-
*/
|
|
369
|
-
ZkPassport.prototype.cancelRequest = function (requestId) {
|
|
370
|
-
this.topicToWebSocketClient[requestId].close();
|
|
371
|
-
delete this.topicToWebSocketClient[requestId];
|
|
372
|
-
delete this.topicToKeyPair[requestId];
|
|
373
|
-
delete this.topicToConfig[requestId];
|
|
374
|
-
delete this.topicToSharedSecret[requestId];
|
|
375
|
-
this.onQRCodeScannedCallbacks[requestId] = [];
|
|
376
|
-
this.onGeneratingProofCallbacks[requestId] = [];
|
|
377
|
-
this.onBridgeConnectCallbacks[requestId] = [];
|
|
378
|
-
this.onProofGeneratedCallbacks[requestId] = [];
|
|
379
|
-
this.onRejectCallbacks[requestId] = [];
|
|
380
|
-
this.onErrorCallbacks[requestId] = [];
|
|
381
|
-
};
|
|
382
|
-
return ZkPassport;
|
|
383
|
-
}());
|
|
384
|
-
exports.ZkPassport = ZkPassport;
|
package/dist/json-rpc.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
-
};
|
|
41
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
exports.createJsonRpcRequest = createJsonRpcRequest;
|
|
43
|
-
exports.createEncryptedJsonRpcRequest = createEncryptedJsonRpcRequest;
|
|
44
|
-
exports.sendEncryptedJsonRpcRequest = sendEncryptedJsonRpcRequest;
|
|
45
|
-
exports.createJsonRpcResponse = createJsonRpcResponse;
|
|
46
|
-
var crypto_1 = require("crypto");
|
|
47
|
-
var encryption_1 = require("./encryption");
|
|
48
|
-
var logger_1 = __importDefault(require("./logger"));
|
|
49
|
-
function createJsonRpcRequest(method, params) {
|
|
50
|
-
return {
|
|
51
|
-
jsonrpc: '2.0',
|
|
52
|
-
id: (0, crypto_1.randomBytes)(16).toString('hex'),
|
|
53
|
-
method: method,
|
|
54
|
-
params: params,
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
function createEncryptedJsonRpcRequest(method, params, sharedSecret, topic) {
|
|
58
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
59
|
-
var encryptedMessage;
|
|
60
|
-
return __generator(this, function (_a) {
|
|
61
|
-
switch (_a.label) {
|
|
62
|
-
case 0: return [4 /*yield*/, (0, encryption_1.encrypt)(JSON.stringify({ method: method, params: params || {} }), sharedSecret, topic)];
|
|
63
|
-
case 1:
|
|
64
|
-
encryptedMessage = _a.sent();
|
|
65
|
-
return [2 /*return*/, createJsonRpcRequest('encryptedMessage', {
|
|
66
|
-
payload: Buffer.from(encryptedMessage).toString('base64'),
|
|
67
|
-
})];
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
function sendEncryptedJsonRpcRequest(method, params, sharedSecret, topic, wsClient) {
|
|
73
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
74
|
-
var message, encryptedMessage, request, error_1;
|
|
75
|
-
return __generator(this, function (_a) {
|
|
76
|
-
switch (_a.label) {
|
|
77
|
-
case 0:
|
|
78
|
-
_a.trys.push([0, 2, , 3]);
|
|
79
|
-
message = { method: method, params: params || {} };
|
|
80
|
-
return [4 /*yield*/, (0, encryption_1.encrypt)(JSON.stringify(message), sharedSecret, topic)];
|
|
81
|
-
case 1:
|
|
82
|
-
encryptedMessage = _a.sent();
|
|
83
|
-
request = createJsonRpcRequest('encryptedMessage', {
|
|
84
|
-
payload: Buffer.from(encryptedMessage).toString('base64'),
|
|
85
|
-
});
|
|
86
|
-
logger_1.default.debug('Sending encrypted message (original):', message);
|
|
87
|
-
logger_1.default.debug('Sending encrypted message (encrypted):', request);
|
|
88
|
-
wsClient.send(JSON.stringify(request));
|
|
89
|
-
return [2 /*return*/, true];
|
|
90
|
-
case 2:
|
|
91
|
-
error_1 = _a.sent();
|
|
92
|
-
logger_1.default.error('Error sending encrypted message:', error_1);
|
|
93
|
-
return [2 /*return*/, false];
|
|
94
|
-
case 3: return [2 /*return*/];
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
function createJsonRpcResponse(id, result) {
|
|
100
|
-
return {
|
|
101
|
-
jsonrpc: '2.0',
|
|
102
|
-
id: id,
|
|
103
|
-
result: result,
|
|
104
|
-
};
|
|
105
|
-
}
|