@getpara/core-sdk 2.0.0-dev.3 → 2.0.0-dev.7

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.
@@ -24,6 +24,8 @@ __export(constants_exports, {
24
24
  LOCAL_STORAGE_CURRENT_WALLET_IDS: () => LOCAL_STORAGE_CURRENT_WALLET_IDS,
25
25
  LOCAL_STORAGE_ED25519_WALLETS: () => LOCAL_STORAGE_ED25519_WALLETS,
26
26
  LOCAL_STORAGE_EMAIL: () => LOCAL_STORAGE_EMAIL,
27
+ LOCAL_STORAGE_ENCLAVE_JWT: () => LOCAL_STORAGE_ENCLAVE_JWT,
28
+ LOCAL_STORAGE_ENCLAVE_REFRESH_JWT: () => LOCAL_STORAGE_ENCLAVE_REFRESH_JWT,
27
29
  LOCAL_STORAGE_EXTERNAL_WALLETS: () => LOCAL_STORAGE_EXTERNAL_WALLETS,
28
30
  LOCAL_STORAGE_EXTERNAL_WALLET_USER_ID: () => LOCAL_STORAGE_EXTERNAL_WALLET_USER_ID,
29
31
  LOCAL_STORAGE_FARCASTER_USERNAME: () => LOCAL_STORAGE_FARCASTER_USERNAME,
@@ -33,6 +35,7 @@ __export(constants_exports, {
33
35
  LOCAL_STORAGE_USER_ID: () => LOCAL_STORAGE_USER_ID,
34
36
  LOCAL_STORAGE_WALLETS: () => LOCAL_STORAGE_WALLETS,
35
37
  PARA_CORE_VERSION: () => PARA_CORE_VERSION,
38
+ PARA_PREFIX: () => PARA_PREFIX,
36
39
  POLLING_INTERVAL_MS: () => POLLING_INTERVAL_MS,
37
40
  POLLING_TIMEOUT_MS: () => POLLING_TIMEOUT_MS,
38
41
  PREFIX: () => PREFIX,
@@ -40,8 +43,9 @@ __export(constants_exports, {
40
43
  SHORT_POLLING_INTERVAL_MS: () => SHORT_POLLING_INTERVAL_MS
41
44
  });
42
45
  module.exports = __toCommonJS(constants_exports);
43
- const PARA_CORE_VERSION = "2.0.0-dev.0";
46
+ const PARA_CORE_VERSION = "2.0.0-alpha.50";
44
47
  const PREFIX = "@CAPSULE/";
48
+ const PARA_PREFIX = "@PARA/";
45
49
  const LOCAL_STORAGE_AUTH_INFO = `${PREFIX}authInfo`;
46
50
  const LOCAL_STORAGE_EMAIL = `${PREFIX}e-mail`;
47
51
  const LOCAL_STORAGE_PHONE = `${PREFIX}phone`;
@@ -55,6 +59,8 @@ const LOCAL_STORAGE_WALLETS = `${PREFIX}wallets`;
55
59
  const LOCAL_STORAGE_EXTERNAL_WALLETS = `${PREFIX}externalWallets`;
56
60
  const LOCAL_STORAGE_CURRENT_WALLET_IDS = `${PREFIX}currentWalletIds`;
57
61
  const LOCAL_STORAGE_SESSION_COOKIE = `${PREFIX}sessionCookie`;
62
+ const LOCAL_STORAGE_ENCLAVE_JWT = `${PREFIX}enclaveJwt`;
63
+ const LOCAL_STORAGE_ENCLAVE_REFRESH_JWT = `${PREFIX}enclaveRefreshJwt`;
58
64
  const SESSION_STORAGE_LOGIN_ENCRYPTION_KEY_PAIR = `${PREFIX}loginEncryptionKeyPair`;
59
65
  const POLLING_INTERVAL_MS = 2e3;
60
66
  const SHORT_POLLING_INTERVAL_MS = 1e3;
@@ -70,6 +76,8 @@ const ACCOUNT_LINK_CONFLICT = "Account already linked";
70
76
  LOCAL_STORAGE_CURRENT_WALLET_IDS,
71
77
  LOCAL_STORAGE_ED25519_WALLETS,
72
78
  LOCAL_STORAGE_EMAIL,
79
+ LOCAL_STORAGE_ENCLAVE_JWT,
80
+ LOCAL_STORAGE_ENCLAVE_REFRESH_JWT,
73
81
  LOCAL_STORAGE_EXTERNAL_WALLETS,
74
82
  LOCAL_STORAGE_EXTERNAL_WALLET_USER_ID,
75
83
  LOCAL_STORAGE_FARCASTER_USERNAME,
@@ -79,6 +87,7 @@ const ACCOUNT_LINK_CONFLICT = "Account already linked";
79
87
  LOCAL_STORAGE_USER_ID,
80
88
  LOCAL_STORAGE_WALLETS,
81
89
  PARA_CORE_VERSION,
90
+ PARA_PREFIX,
82
91
  POLLING_INTERVAL_MS,
83
92
  POLLING_TIMEOUT_MS,
84
93
  PREFIX,
package/dist/cjs/index.js CHANGED
@@ -45,6 +45,7 @@ __export(src_exports, {
45
45
  OnRampProvider: () => import_user_management_client.OnRampProvider,
46
46
  OnRampPurchaseStatus: () => import_user_management_client.OnRampPurchaseStatus,
47
47
  OnRampPurchaseType: () => import_user_management_client.OnRampPurchaseType,
48
+ PARA_STORAGE_PREFIX: () => import_constants.PARA_PREFIX,
48
49
  PREGEN_IDENTIFIER_TYPES: () => import_user_management_client.PREGEN_IDENTIFIER_TYPES,
49
50
  PopupType: () => import_types.PopupType,
50
51
  PregenIdentifierType: () => import_types.PregenIdentifierType,
@@ -133,6 +134,7 @@ var src_default = import_ParaCore.ParaCore;
133
134
  OnRampProvider,
134
135
  OnRampPurchaseStatus,
135
136
  OnRampPurchaseType,
137
+ PARA_STORAGE_PREFIX,
136
138
  PREGEN_IDENTIFIER_TYPES,
137
139
  PopupType,
138
140
  PregenIdentifierType,
@@ -0,0 +1,274 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var __async = (__this, __arguments, generator) => {
19
+ return new Promise((resolve, reject) => {
20
+ var fulfilled = (value) => {
21
+ try {
22
+ step(generator.next(value));
23
+ } catch (e) {
24
+ reject(e);
25
+ }
26
+ };
27
+ var rejected = (value) => {
28
+ try {
29
+ step(generator.throw(value));
30
+ } catch (e) {
31
+ reject(e);
32
+ }
33
+ };
34
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
35
+ step((generator = generator.apply(__this, __arguments)).next());
36
+ });
37
+ };
38
+ var enclave_exports = {};
39
+ __export(enclave_exports, {
40
+ EnclaveClient: () => EnclaveClient
41
+ });
42
+ module.exports = __toCommonJS(enclave_exports);
43
+ class EnclaveClient {
44
+ constructor({
45
+ userManagementClient,
46
+ retrieveJwt,
47
+ persistJwt,
48
+ retrieveRefreshJwt,
49
+ persistRefreshJwt
50
+ }) {
51
+ this.enclavePublicKey = null;
52
+ this.frontendKeyPair = null;
53
+ this.userManagementClient = userManagementClient;
54
+ this.retrieveJwt = retrieveJwt;
55
+ this.persistJwt = persistJwt;
56
+ this.retrieveRefreshJwt = retrieveRefreshJwt;
57
+ this.persistRefreshJwt = persistRefreshJwt;
58
+ }
59
+ refreshJwt() {
60
+ return __async(this, null, function* () {
61
+ const frontendKeyPair = yield this.generateFrontendKeyPair();
62
+ const responsePublicKeyPEM = yield this.exportPublicKeyToPEM(frontendKeyPair.publicKey);
63
+ const payload = {
64
+ refreshJwt: this.retrieveRefreshJwt(),
65
+ responsePublicKey: responsePublicKeyPEM
66
+ };
67
+ const encryptedPayload = yield this.encryptForEnclave(JSON.stringify(payload));
68
+ const response = yield this.userManagementClient.refreshEnclaveJwt(JSON.stringify(encryptedPayload));
69
+ const decryptedResponse = yield this.decryptForFrontend(JSON.parse(response.payload));
70
+ this.persistJwt(decryptedResponse.jwt);
71
+ this.persistRefreshJwt(decryptedResponse.refreshJwt);
72
+ });
73
+ }
74
+ withJwtRefreshRetry(fn) {
75
+ return __async(this, null, function* () {
76
+ try {
77
+ return yield fn();
78
+ } catch (error) {
79
+ yield this.refreshJwt();
80
+ return yield fn();
81
+ }
82
+ });
83
+ }
84
+ issueEnclaveJwt() {
85
+ return __async(this, null, function* () {
86
+ const frontendKeyPair = yield this.generateFrontendKeyPair();
87
+ const responsePublicKeyPEM = yield this.exportPublicKeyToPEM(frontendKeyPair.publicKey);
88
+ const payload = {
89
+ responsePublicKey: responsePublicKeyPEM
90
+ };
91
+ const encryptedPayload = yield this.encryptForEnclave(JSON.stringify(payload));
92
+ const response = yield this.userManagementClient.issueEnclaveJwt(JSON.stringify(encryptedPayload));
93
+ console.log(JSON.parse(response));
94
+ const decryptedResponse = yield this.decryptForFrontend(JSON.parse(response));
95
+ console.log("decryptedResponse", decryptedResponse);
96
+ this.persistJwt(decryptedResponse.jwt);
97
+ });
98
+ }
99
+ /**
100
+ * Generate a P-256 keypair for the frontend to receive encrypted responses
101
+ */
102
+ generateFrontendKeyPair() {
103
+ return __async(this, null, function* () {
104
+ if (this.frontendKeyPair) {
105
+ return this.frontendKeyPair;
106
+ }
107
+ this.frontendKeyPair = yield crypto.subtle.generateKey({ name: "ECDH", namedCurve: "P-256" }, true, ["deriveBits"]);
108
+ return this.frontendKeyPair;
109
+ });
110
+ }
111
+ /**
112
+ * Get the enclave's public key from the user-management service
113
+ */
114
+ getEnclavePublicKey() {
115
+ return __async(this, null, function* () {
116
+ if (this.enclavePublicKey) {
117
+ return this.enclavePublicKey;
118
+ }
119
+ const response = yield this.userManagementClient.getEnclavePublicKey();
120
+ this.enclavePublicKey = response.publicKey;
121
+ return this.enclavePublicKey;
122
+ });
123
+ }
124
+ /**
125
+ * Import a PEM-formatted public key for use with Web Crypto API
126
+ */
127
+ importPublicKeyFromPEM(pemString) {
128
+ return __async(this, null, function* () {
129
+ const pemContents = pemString.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "").replace(/\s/g, "");
130
+ const keyData = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0));
131
+ return yield crypto.subtle.importKey("spki", keyData, { name: "ECDH", namedCurve: "P-256" }, false, []);
132
+ });
133
+ }
134
+ /**
135
+ * Export a public key to PEM format
136
+ */
137
+ exportPublicKeyToPEM(publicKey) {
138
+ return __async(this, null, function* () {
139
+ const exported = yield crypto.subtle.exportKey("spki", publicKey);
140
+ const exportedAsBase64 = btoa(String.fromCharCode(...new Uint8Array(exported)));
141
+ return `-----BEGIN PUBLIC KEY-----
142
+ ${exportedAsBase64}
143
+ -----END PUBLIC KEY-----`;
144
+ });
145
+ }
146
+ /**
147
+ * Encrypt data using P-256 ECIES for the enclave
148
+ */
149
+ encryptForEnclave(plaintext) {
150
+ return __async(this, null, function* () {
151
+ const enclavePublicKeyPEM = yield this.getEnclavePublicKey();
152
+ const enclavePublicKey = yield this.importPublicKeyFromPEM(enclavePublicKeyPEM);
153
+ const ephemeralKeyPair = yield crypto.subtle.generateKey({ name: "ECDH", namedCurve: "P-256" }, true, ["deriveBits"]);
154
+ const sharedSecretBits = yield crypto.subtle.deriveBits(
155
+ { name: "ECDH", public: enclavePublicKey },
156
+ ephemeralKeyPair.privateKey,
157
+ 256
158
+ // 32 bytes = 256 bits
159
+ );
160
+ const encryptionKeyBuffer = yield crypto.subtle.digest("SHA-256", sharedSecretBits);
161
+ const encryptionKey = yield crypto.subtle.importKey("raw", encryptionKeyBuffer, { name: "AES-GCM" }, false, ["encrypt"]);
162
+ const iv = crypto.getRandomValues(new Uint8Array(12));
163
+ const encrypted = yield crypto.subtle.encrypt(
164
+ { name: "AES-GCM", iv },
165
+ encryptionKey,
166
+ new TextEncoder().encode(plaintext)
167
+ );
168
+ const encryptedArray = new Uint8Array(encrypted);
169
+ const combined = new Uint8Array(iv.length + encryptedArray.length);
170
+ combined.set(iv);
171
+ combined.set(encryptedArray, iv.length);
172
+ const ephemeralPublicKeyBuffer = yield crypto.subtle.exportKey("spki", ephemeralKeyPair.publicKey);
173
+ return {
174
+ encryptedData: btoa(String.fromCharCode(...combined)),
175
+ keyId: "",
176
+ // Will be set by the enclave
177
+ algorithm: "ECIES-P256-AES256-SHA256",
178
+ ephemeral: btoa(String.fromCharCode(...new Uint8Array(ephemeralPublicKeyBuffer)))
179
+ };
180
+ });
181
+ }
182
+ /**
183
+ * Decrypt response encrypted for the frontend
184
+ */
185
+ decryptForFrontend(encryptedPayload) {
186
+ return __async(this, null, function* () {
187
+ if (!this.frontendKeyPair) {
188
+ throw new Error("Frontend keypair not available");
189
+ }
190
+ const encryptedData = Uint8Array.from(atob(encryptedPayload.encryptedData), (c) => c.charCodeAt(0));
191
+ const ephemeralPublicKeyData = Uint8Array.from(atob(encryptedPayload.ephemeral), (c) => c.charCodeAt(0));
192
+ const ephemeralPublicKey = yield crypto.subtle.importKey(
193
+ "spki",
194
+ ephemeralPublicKeyData,
195
+ { name: "ECDH", namedCurve: "P-256" },
196
+ false,
197
+ []
198
+ );
199
+ const sharedSecretBits = yield crypto.subtle.deriveBits(
200
+ { name: "ECDH", public: ephemeralPublicKey },
201
+ this.frontendKeyPair.privateKey,
202
+ 256
203
+ );
204
+ const encryptionKeyBuffer = yield crypto.subtle.digest("SHA-256", sharedSecretBits);
205
+ const encryptionKey = yield crypto.subtle.importKey("raw", encryptionKeyBuffer, { name: "AES-GCM" }, false, ["decrypt"]);
206
+ const iv = encryptedData.slice(0, 12);
207
+ const ciphertext = encryptedData.slice(12);
208
+ const decrypted = yield crypto.subtle.decrypt({ name: "AES-GCM", iv }, encryptionKey, ciphertext);
209
+ console.log("decryptForFrontend decrypted", decrypted);
210
+ return JSON.parse(new TextDecoder().decode(decrypted));
211
+ });
212
+ }
213
+ /**
214
+ * Persist key shares to the enclave
215
+ * @param shares Array of share data to persist
216
+ */
217
+ persistShares(shares) {
218
+ return __async(this, null, function* () {
219
+ console.log("persistShares about to call encryptForEnclave");
220
+ console.log("shares", shares);
221
+ const payload = {
222
+ shares,
223
+ jwt: this.retrieveJwt()
224
+ };
225
+ const encryptedPayload = yield this.encryptForEnclave(JSON.stringify(payload));
226
+ const encryptedPayloadStr = JSON.stringify(encryptedPayload);
227
+ return yield this.userManagementClient.persistEnclaveShares(encryptedPayloadStr);
228
+ });
229
+ }
230
+ /**
231
+ * Retrieve key shares from the enclave
232
+ * @param query Query parameters for finding shares (single query or array of queries)
233
+ */
234
+ retrieveShares(query) {
235
+ return __async(this, null, function* () {
236
+ yield this.issueEnclaveJwt();
237
+ const frontendKeyPair = yield this.generateFrontendKeyPair();
238
+ const responsePublicKeyPEM = yield this.exportPublicKeyToPEM(frontendKeyPair.publicKey);
239
+ const fullQuery = query.map((q) => ({
240
+ userId: q.userId
241
+ }));
242
+ const payload = {
243
+ query: fullQuery,
244
+ responsePublicKey: responsePublicKeyPEM,
245
+ jwt: this.retrieveJwt()
246
+ };
247
+ const encryptedPayload = yield this.encryptForEnclave(JSON.stringify(payload));
248
+ const encryptedPayloadStr = JSON.stringify(encryptedPayload);
249
+ const response = yield this.userManagementClient.retrieveEnclaveShares(encryptedPayloadStr);
250
+ const encryptedResponse = JSON.parse(response.payload);
251
+ const decryptedData = yield this.decryptForFrontend(encryptedResponse);
252
+ console.log("retrieveShares decryptedData", decryptedData);
253
+ return decryptedData;
254
+ });
255
+ }
256
+ retrieveSharesWithRetry(query) {
257
+ return __async(this, null, function* () {
258
+ return yield this.withJwtRefreshRetry(() => __async(this, null, function* () {
259
+ return this.retrieveShares(query);
260
+ }));
261
+ });
262
+ }
263
+ persistSharesWithRetry(shares) {
264
+ return __async(this, null, function* () {
265
+ return yield this.withJwtRefreshRetry(() => __async(this, null, function* () {
266
+ return this.persistShares(shares);
267
+ }));
268
+ });
269
+ }
270
+ }
271
+ // Annotate the CommonJS export names for ESM import in node:
272
+ 0 && (module.exports = {
273
+ EnclaveClient
274
+ });
@@ -52,8 +52,23 @@ function distributeNewShare(_0) {
52
52
  ignoreRedistributingBackupEncryptedShare = false,
53
53
  emailProps = {},
54
54
  partnerId,
55
- protocolId
55
+ protocolId,
56
+ isEnclaveUser,
57
+ walletScheme
56
58
  }) {
59
+ if (isEnclaveUser) {
60
+ yield ctx.enclaveClient.persistSharesWithRetry([
61
+ {
62
+ userId,
63
+ walletId,
64
+ walletScheme,
65
+ signer: userShare,
66
+ partnerId,
67
+ protocolId
68
+ }
69
+ ]);
70
+ return "";
71
+ }
57
72
  const publicKeysRes = yield ctx.client.getSessionPublicKeys(userId);
58
73
  const biometricEncryptedShares = publicKeysRes.data.keys.map((key) => {
59
74
  if (!key.publicKey) {
@@ -80,7 +80,9 @@ const PARA_INTERNAL_METHODS = [
80
80
  "verifyFarcasterLink",
81
81
  "verifyTelegramLink",
82
82
  "verifyExternalWalletLink",
83
- "accountLinkInProgress"
83
+ "accountLinkInProgress",
84
+ "prepareLogin",
85
+ "sendLoginCode"
84
86
  ];
85
87
  // Annotate the CommonJS export names for ESM import in node:
86
88
  0 && (module.exports = {