@mysten/enoki 0.0.0-experimental-20231117210618

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.
Files changed (65) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/LICENSE +201 -0
  3. package/README.md +1 -0
  4. package/dist/cjs/EnokiClient/index.d.ts +18 -0
  5. package/dist/cjs/EnokiClient/index.js +114 -0
  6. package/dist/cjs/EnokiClient/index.js.map +7 -0
  7. package/dist/cjs/EnokiClient/type.d.ts +36 -0
  8. package/dist/cjs/EnokiClient/type.js +17 -0
  9. package/dist/cjs/EnokiClient/type.js.map +7 -0
  10. package/dist/cjs/EnokiFlow.d.ts +50 -0
  11. package/dist/cjs/EnokiFlow.js +256 -0
  12. package/dist/cjs/EnokiFlow.js.map +7 -0
  13. package/dist/cjs/EnokiKeypair.d.ts +31 -0
  14. package/dist/cjs/EnokiKeypair.js +119 -0
  15. package/dist/cjs/EnokiKeypair.js.map +7 -0
  16. package/dist/cjs/encryption.d.ts +15 -0
  17. package/dist/cjs/encryption.js +47 -0
  18. package/dist/cjs/encryption.js.map +7 -0
  19. package/dist/cjs/index.d.ts +5 -0
  20. package/dist/cjs/index.js +36 -0
  21. package/dist/cjs/index.js.map +7 -0
  22. package/dist/cjs/package.json +4 -0
  23. package/dist/cjs/react.d.ts +9 -0
  24. package/dist/cjs/react.js +46 -0
  25. package/dist/cjs/react.js.map +7 -0
  26. package/dist/cjs/stores.d.ts +21 -0
  27. package/dist/cjs/stores.js +67 -0
  28. package/dist/cjs/stores.js.map +7 -0
  29. package/dist/esm/EnokiClient/index.d.ts +18 -0
  30. package/dist/esm/EnokiClient/index.js +94 -0
  31. package/dist/esm/EnokiClient/index.js.map +7 -0
  32. package/dist/esm/EnokiClient/type.d.ts +36 -0
  33. package/dist/esm/EnokiClient/type.js +1 -0
  34. package/dist/esm/EnokiClient/type.js.map +7 -0
  35. package/dist/esm/EnokiFlow.d.ts +50 -0
  36. package/dist/esm/EnokiFlow.js +236 -0
  37. package/dist/esm/EnokiFlow.js.map +7 -0
  38. package/dist/esm/EnokiKeypair.d.ts +31 -0
  39. package/dist/esm/EnokiKeypair.js +99 -0
  40. package/dist/esm/EnokiKeypair.js.map +7 -0
  41. package/dist/esm/encryption.d.ts +15 -0
  42. package/dist/esm/encryption.js +30 -0
  43. package/dist/esm/encryption.js.map +7 -0
  44. package/dist/esm/index.d.ts +5 -0
  45. package/dist/esm/index.js +20 -0
  46. package/dist/esm/index.js.map +7 -0
  47. package/dist/esm/package.json +4 -0
  48. package/dist/esm/react.d.ts +9 -0
  49. package/dist/esm/react.js +26 -0
  50. package/dist/esm/react.js.map +7 -0
  51. package/dist/esm/stores.d.ts +21 -0
  52. package/dist/esm/stores.js +47 -0
  53. package/dist/esm/stores.js.map +7 -0
  54. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  55. package/dist/tsconfig.tsbuildinfo +1 -0
  56. package/package.json +73 -0
  57. package/react/package.json +5 -0
  58. package/src/EnokiClient/index.ts +97 -0
  59. package/src/EnokiClient/type.ts +42 -0
  60. package/src/EnokiFlow.ts +308 -0
  61. package/src/EnokiKeypair.ts +98 -0
  62. package/src/encryption.ts +44 -0
  63. package/src/index.ts +13 -0
  64. package/src/react.tsx +33 -0
  65. package/src/stores.ts +68 -0
@@ -0,0 +1,256 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var __accessCheck = (obj, member, msg) => {
20
+ if (!member.has(obj))
21
+ throw TypeError("Cannot " + msg);
22
+ };
23
+ var __privateGet = (obj, member, getter) => {
24
+ __accessCheck(obj, member, "read from private field");
25
+ return getter ? getter.call(obj) : member.get(obj);
26
+ };
27
+ var __privateAdd = (obj, member, value) => {
28
+ if (member.has(obj))
29
+ throw TypeError("Cannot add the same private member more than once");
30
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
31
+ };
32
+ var __privateSet = (obj, member, value, setter) => {
33
+ __accessCheck(obj, member, "write to private field");
34
+ setter ? setter.call(obj, value) : member.set(obj, value);
35
+ return value;
36
+ };
37
+ var __privateMethod = (obj, member, method) => {
38
+ __accessCheck(obj, member, "access private method");
39
+ return method;
40
+ };
41
+ var EnokiFlow_exports = {};
42
+ __export(EnokiFlow_exports, {
43
+ EnokiFlow: () => EnokiFlow
44
+ });
45
+ module.exports = __toCommonJS(EnokiFlow_exports);
46
+ var import_ed25519 = require("@mysten/sui.js/keypairs/ed25519");
47
+ var import_utils = require("@mysten/sui.js/utils");
48
+ var import_jose = require("jose");
49
+ var import_nanostores = require("nanostores");
50
+ var import_encryption = require("./encryption.js");
51
+ var import_EnokiClient = require("./EnokiClient/index.js");
52
+ var import_EnokiKeypair = require("./EnokiKeypair.js");
53
+ var import_stores = require("./stores.js");
54
+ var _enokiClient, _encryption, _encryptionKey, _store, _zkLoginSessionInitialized, _zkLoginSession, _setSession, setSession_fn;
55
+ const STORAGE_KEYS = {
56
+ STATE: "@enoki/flow/state",
57
+ SESSION: "@enoki/flow/session"
58
+ };
59
+ class EnokiFlow {
60
+ constructor(config) {
61
+ __privateAdd(this, _setSession);
62
+ __privateAdd(this, _enokiClient, void 0);
63
+ __privateAdd(this, _encryption, void 0);
64
+ __privateAdd(this, _encryptionKey, void 0);
65
+ __privateAdd(this, _store, void 0);
66
+ __privateAdd(this, _zkLoginSessionInitialized, void 0);
67
+ __privateAdd(this, _zkLoginSession, void 0);
68
+ __privateSet(this, _enokiClient, new import_EnokiClient.EnokiClient({
69
+ apiKey: config.apiKey,
70
+ apiUrl: config.apiUrl
71
+ }));
72
+ __privateSet(this, _encryptionKey, config.apiKey);
73
+ __privateSet(this, _encryption, config.encryption ?? (0, import_encryption.createDefaultEncryption)());
74
+ __privateSet(this, _store, config.store ?? (0, import_stores.createSessionStorage)());
75
+ let storedState = null;
76
+ try {
77
+ const rawStoredValue = __privateGet(this, _store).get(STORAGE_KEYS.STATE);
78
+ if (rawStoredValue) {
79
+ storedState = JSON.parse(rawStoredValue);
80
+ }
81
+ } catch {
82
+ }
83
+ this.$zkLoginState = (0, import_nanostores.atom)(storedState || {});
84
+ __privateSet(this, _zkLoginSessionInitialized, false);
85
+ __privateSet(this, _zkLoginSession, null);
86
+ (0, import_nanostores.onSet)(this.$zkLoginState, ({ newValue }) => {
87
+ __privateGet(this, _store).set(STORAGE_KEYS.STATE, JSON.stringify(newValue));
88
+ });
89
+ }
90
+ get enokiClient() {
91
+ return __privateGet(this, _enokiClient);
92
+ }
93
+ async createAuthorizationURL(input) {
94
+ const ephemeralKeyPair = new import_ed25519.Ed25519Keypair();
95
+ const { nonce, randomness, maxEpoch, estimatedExpiration } = await __privateGet(this, _enokiClient).createZkLoginNonce({
96
+ ephemeralPublicKey: ephemeralKeyPair.getPublicKey()
97
+ });
98
+ const params = new URLSearchParams({
99
+ ...input.extraParams,
100
+ nonce,
101
+ client_id: input.clientId,
102
+ redirect_uri: input.redirectUrl,
103
+ response_type: "id_token",
104
+ // TODO: Eventually fetch the scopes for this client ID from the Enoki service:
105
+ scope: [
106
+ "openid",
107
+ // Merge the requested scopes in with the required openid scopes:
108
+ ...input.extraParams && "scope" in input.extraParams ? input.extraParams.scope : []
109
+ ].filter(Boolean).join(" ")
110
+ });
111
+ let oauthUrl;
112
+ switch (input.provider) {
113
+ case "google": {
114
+ oauthUrl = `https://accounts.google.com/o/oauth2/v2/auth?${params}`;
115
+ break;
116
+ }
117
+ case "facebook": {
118
+ oauthUrl = `https://www.facebook.com/v17.0/dialog/oauth?${params}`;
119
+ break;
120
+ }
121
+ case "twitch": {
122
+ params.set("force_verify", "true");
123
+ oauthUrl = `https://id.twitch.tv/oauth2/authorize?${params}`;
124
+ break;
125
+ }
126
+ default:
127
+ throw new Error(`Invalid provider: ${input.provider}`);
128
+ }
129
+ this.$zkLoginState.set({ provider: input.provider });
130
+ await __privateMethod(this, _setSession, setSession_fn).call(this, {
131
+ expiresAt: estimatedExpiration,
132
+ maxEpoch,
133
+ randomness,
134
+ ephemeralKeyPair: ephemeralKeyPair.export().privateKey
135
+ });
136
+ return oauthUrl;
137
+ }
138
+ // TODO: Should our SDK manage this automatically in addition to exposing a method?
139
+ async handleAuthCallback(hash = window.location.hash) {
140
+ const params = new URLSearchParams(hash.startsWith("#") ? hash.slice(1) : hash);
141
+ const zkp = await this.getSession();
142
+ if (!zkp || !zkp.ephemeralKeyPair || !zkp.maxEpoch || !zkp.randomness) {
143
+ throw new Error(
144
+ "Start of sign-in flow could not be found. Ensure you have started the sign-in flow before calling this."
145
+ );
146
+ }
147
+ const jwt = params.get("id_token");
148
+ if (!jwt) {
149
+ throw new Error("Missing ID Token");
150
+ }
151
+ const decodedJwt = (0, import_jose.decodeJwt)(jwt);
152
+ if (!decodedJwt.sub || !decodedJwt.aud || typeof decodedJwt.aud !== "string") {
153
+ throw new Error("Missing JWT data");
154
+ }
155
+ const { address, salt } = await __privateGet(this, _enokiClient).getZkLogin({ jwt });
156
+ this.$zkLoginState.set({
157
+ ...this.$zkLoginState.get(),
158
+ salt,
159
+ address
160
+ });
161
+ await __privateMethod(this, _setSession, setSession_fn).call(this, {
162
+ ...zkp,
163
+ jwt
164
+ });
165
+ return params.get("state");
166
+ }
167
+ async getSession() {
168
+ if (__privateGet(this, _zkLoginSessionInitialized)) {
169
+ return __privateGet(this, _zkLoginSession);
170
+ }
171
+ try {
172
+ const storedValue = __privateGet(this, _store).get(STORAGE_KEYS.SESSION);
173
+ if (!storedValue)
174
+ return null;
175
+ const state = JSON.parse(
176
+ await __privateGet(this, _encryption).decrypt(__privateGet(this, _encryptionKey), storedValue)
177
+ );
178
+ if (state?.expiresAt && Date.now() > state.expiresAt) {
179
+ await this.logout();
180
+ } else {
181
+ __privateSet(this, _zkLoginSession, state);
182
+ }
183
+ return __privateGet(this, _zkLoginSession);
184
+ } finally {
185
+ __privateSet(this, _zkLoginSessionInitialized, true);
186
+ }
187
+ }
188
+ async logout() {
189
+ this.$zkLoginState.set({});
190
+ __privateGet(this, _store).delete(STORAGE_KEYS.STATE);
191
+ await __privateMethod(this, _setSession, setSession_fn).call(this, null);
192
+ }
193
+ // TODO: Should this return the proof if it already exists?
194
+ async getProof() {
195
+ const zkp = await this.getSession();
196
+ const { salt } = this.$zkLoginState.get();
197
+ if (zkp?.proof) {
198
+ if (zkp.expiresAt && Date.now() > zkp.expiresAt) {
199
+ throw new Error("Stored proof is expired.");
200
+ }
201
+ return zkp.proof;
202
+ }
203
+ if (!salt || !zkp || !zkp.jwt) {
204
+ throw new Error("Missing required parameters for proof generation");
205
+ }
206
+ const ephemeralKeyPair = import_ed25519.Ed25519Keypair.fromSecretKey((0, import_utils.fromB64)(zkp.ephemeralKeyPair));
207
+ const proof = await __privateGet(this, _enokiClient).createZkLoginZkp({
208
+ jwt: zkp.jwt,
209
+ maxEpoch: zkp.maxEpoch,
210
+ randomness: zkp.randomness,
211
+ ephemeralPublicKey: ephemeralKeyPair.getPublicKey()
212
+ });
213
+ await __privateMethod(this, _setSession, setSession_fn).call(this, {
214
+ ...zkp,
215
+ proof
216
+ });
217
+ return proof;
218
+ }
219
+ async getKeypair() {
220
+ const zkp = await this.getSession();
221
+ await this.getProof();
222
+ const { address } = this.$zkLoginState.get();
223
+ if (!address || !zkp || !zkp.proof) {
224
+ throw new Error("Missing required data for keypair generation.");
225
+ }
226
+ if (Date.now() > zkp.expiresAt) {
227
+ throw new Error("Stored proof is expired.");
228
+ }
229
+ return new import_EnokiKeypair.EnokiKeypair({
230
+ address,
231
+ maxEpoch: zkp.maxEpoch,
232
+ proof: zkp.proof,
233
+ ephemeralKeypair: import_ed25519.Ed25519Keypair.fromSecretKey((0, import_utils.fromB64)(zkp.ephemeralKeyPair))
234
+ });
235
+ }
236
+ }
237
+ _enokiClient = new WeakMap();
238
+ _encryption = new WeakMap();
239
+ _encryptionKey = new WeakMap();
240
+ _store = new WeakMap();
241
+ _zkLoginSessionInitialized = new WeakMap();
242
+ _zkLoginSession = new WeakMap();
243
+ _setSession = new WeakSet();
244
+ setSession_fn = async function(newValue) {
245
+ if (newValue) {
246
+ const storedValue = await __privateGet(this, _encryption).encrypt(
247
+ __privateGet(this, _encryptionKey),
248
+ JSON.stringify(newValue)
249
+ );
250
+ __privateGet(this, _store).set(STORAGE_KEYS.SESSION, storedValue);
251
+ } else {
252
+ __privateGet(this, _store).delete(STORAGE_KEYS.SESSION);
253
+ }
254
+ __privateSet(this, _zkLoginSession, newValue);
255
+ };
256
+ //# sourceMappingURL=EnokiFlow.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/EnokiFlow.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519';\nimport { fromB64 } from '@mysten/sui.js/utils';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui.js/zklogin';\nimport { decodeJwt } from 'jose';\nimport type { WritableAtom } from 'nanostores';\nimport { atom, onSet } from 'nanostores';\n\nimport type { Encryption } from './encryption.js';\nimport { createDefaultEncryption } from './encryption.js';\nimport type { EnokiClientConfig } from './EnokiClient/index.js';\nimport { EnokiClient } from './EnokiClient/index.js';\nimport { EnokiKeypair } from './EnokiKeypair.js';\nimport type { SyncStore } from './stores.js';\nimport { createSessionStorage } from './stores.js';\n\nexport interface EnokiFlowConfig extends EnokiClientConfig {\n\t/**\n\t * The storage interface to persist Enoki data locally.\n\t * If not provided, it will use a sessionStorage-backed store.\n\t */\n\tstore?: SyncStore;\n\t/**\n\t * The encryption interface that will be used to encrypt data before storing it locally.\n\t * If not provided, it will use a default encryption interface.\n\t */\n\tencryption?: Encryption;\n}\n\n// State that is not bound to a session, and is encrypted.\nexport interface ZkLoginState {\n\tprovider?: AuthProvider;\n\taddress?: string;\n\tsalt?: string;\n}\n\n// State that session-bound, and is encrypted in storage.\nexport interface ZkLoginSession {\n\tephemeralKeyPair: string;\n\tmaxEpoch: number;\n\trandomness: string;\n\texpiresAt: number;\n\n\tjwt?: string;\n\tproof?: ZkLoginSignatureInputs;\n}\n\nexport type AuthProvider = 'google' | 'facebook' | 'twitch';\n\nconst STORAGE_KEYS = {\n\tSTATE: '@enoki/flow/state',\n\tSESSION: '@enoki/flow/session',\n};\n\nexport class EnokiFlow {\n\t#enokiClient: EnokiClient;\n\t#encryption: Encryption;\n\t#encryptionKey: string;\n\t#store: SyncStore;\n\n\t#zkLoginSessionInitialized: boolean;\n\t#zkLoginSession: ZkLoginSession | null;\n\n\t$zkLoginState: WritableAtom<ZkLoginState>;\n\n\tconstructor(config: EnokiFlowConfig) {\n\t\tthis.#enokiClient = new EnokiClient({\n\t\t\tapiKey: config.apiKey,\n\t\t\tapiUrl: config.apiUrl,\n\t\t});\n\t\tthis.#encryptionKey = config.apiKey;\n\t\tthis.#encryption = config.encryption ?? createDefaultEncryption();\n\t\tthis.#store = config.store ?? createSessionStorage();\n\n\t\tlet storedState = null;\n\t\ttry {\n\t\t\tconst rawStoredValue = this.#store.get(STORAGE_KEYS.STATE);\n\t\t\tif (rawStoredValue) {\n\t\t\t\tstoredState = JSON.parse(rawStoredValue);\n\t\t\t}\n\t\t} catch {\n\t\t\t// Ignore errors\n\t\t}\n\n\t\tthis.$zkLoginState = atom(storedState || {});\n\n\t\tthis.#zkLoginSessionInitialized = false;\n\t\tthis.#zkLoginSession = null;\n\n\t\tonSet(this.$zkLoginState, ({ newValue }) => {\n\t\t\tthis.#store.set(STORAGE_KEYS.STATE, JSON.stringify(newValue));\n\t\t});\n\t}\n\n\tget enokiClient() {\n\t\treturn this.#enokiClient;\n\t}\n\n\tasync createAuthorizationURL(input: {\n\t\tprovider: AuthProvider;\n\t\tclientId: string;\n\t\tredirectUrl: string;\n\t\textraParams?: Record<string, unknown>;\n\t}) {\n\t\tconst ephemeralKeyPair = new Ed25519Keypair();\n\t\tconst { nonce, randomness, maxEpoch, estimatedExpiration } =\n\t\t\tawait this.#enokiClient.createZkLoginNonce({\n\t\t\t\tephemeralPublicKey: ephemeralKeyPair.getPublicKey(),\n\t\t\t});\n\n\t\tconst params = new URLSearchParams({\n\t\t\t...input.extraParams,\n\t\t\tnonce,\n\t\t\tclient_id: input.clientId,\n\t\t\tredirect_uri: input.redirectUrl,\n\t\t\tresponse_type: 'id_token',\n\t\t\t// TODO: Eventually fetch the scopes for this client ID from the Enoki service:\n\t\t\tscope: [\n\t\t\t\t'openid',\n\t\t\t\t// Merge the requested scopes in with the required openid scopes:\n\t\t\t\t...(input.extraParams && 'scope' in input.extraParams\n\t\t\t\t\t? (input.extraParams.scope as string[])\n\t\t\t\t\t: []),\n\t\t\t]\n\t\t\t\t.filter(Boolean)\n\t\t\t\t.join(' '),\n\t\t});\n\n\t\tlet oauthUrl: string;\n\t\tswitch (input.provider) {\n\t\t\tcase 'google': {\n\t\t\t\toauthUrl = `https://accounts.google.com/o/oauth2/v2/auth?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase 'facebook': {\n\t\t\t\toauthUrl = `https://www.facebook.com/v17.0/dialog/oauth?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase 'twitch': {\n\t\t\t\tparams.set('force_verify', 'true');\n\t\t\t\toauthUrl = `https://id.twitch.tv/oauth2/authorize?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Invalid provider: ${input.provider}`);\n\t\t}\n\n\t\tthis.$zkLoginState.set({ provider: input.provider });\n\t\tawait this.#setSession({\n\t\t\texpiresAt: estimatedExpiration,\n\t\t\tmaxEpoch,\n\t\t\trandomness,\n\t\t\tephemeralKeyPair: ephemeralKeyPair.export().privateKey,\n\t\t});\n\n\t\treturn oauthUrl;\n\t}\n\n\t// TODO: Should our SDK manage this automatically in addition to exposing a method?\n\tasync handleAuthCallback(hash: string = window.location.hash) {\n\t\tconst params = new URLSearchParams(hash.startsWith('#') ? hash.slice(1) : hash);\n\n\t\t// Before we handle the auth redirect and get the state, we need to restore it:\n\t\tconst zkp = await this.getSession();\n\n\t\tif (!zkp || !zkp.ephemeralKeyPair || !zkp.maxEpoch || !zkp.randomness) {\n\t\t\tthrow new Error(\n\t\t\t\t'Start of sign-in flow could not be found. Ensure you have started the sign-in flow before calling this.',\n\t\t\t);\n\t\t}\n\n\t\tconst jwt = params.get('id_token');\n\t\tif (!jwt) {\n\t\t\tthrow new Error('Missing ID Token');\n\t\t}\n\n\t\tconst decodedJwt = decodeJwt(jwt);\n\t\tif (!decodedJwt.sub || !decodedJwt.aud || typeof decodedJwt.aud !== 'string') {\n\t\t\tthrow new Error('Missing JWT data');\n\t\t}\n\n\t\tconst { address, salt } = await this.#enokiClient.getZkLogin({ jwt });\n\n\t\tthis.$zkLoginState.set({\n\t\t\t...this.$zkLoginState.get(),\n\t\t\tsalt,\n\t\t\taddress,\n\t\t});\n\t\tawait this.#setSession({\n\t\t\t...zkp,\n\t\t\tjwt,\n\t\t});\n\n\t\treturn params.get('state');\n\t}\n\n\tasync #setSession(newValue: ZkLoginSession | null) {\n\t\tif (newValue) {\n\t\t\tconst storedValue = await this.#encryption.encrypt(\n\t\t\t\tthis.#encryptionKey,\n\t\t\t\tJSON.stringify(newValue),\n\t\t\t);\n\n\t\t\tthis.#store.set(STORAGE_KEYS.SESSION, storedValue);\n\t\t} else {\n\t\t\tthis.#store.delete(STORAGE_KEYS.SESSION);\n\t\t}\n\n\t\tthis.#zkLoginSession = newValue;\n\t}\n\n\tasync getSession() {\n\t\tif (this.#zkLoginSessionInitialized) {\n\t\t\treturn this.#zkLoginSession;\n\t\t}\n\n\t\ttry {\n\t\t\tconst storedValue = this.#store.get(STORAGE_KEYS.SESSION);\n\t\t\tif (!storedValue) return null;\n\n\t\t\tconst state: ZkLoginSession = JSON.parse(\n\t\t\t\tawait this.#encryption.decrypt(this.#encryptionKey, storedValue),\n\t\t\t);\n\n\t\t\t// TODO: Rather than having expiration act as a logout, we should keep the state that still is relevant,\n\t\t\t// and just clear out the expired session, but keep the other zkLogin state.\n\t\t\tif (state?.expiresAt && Date.now() > state.expiresAt) {\n\t\t\t\tawait this.logout();\n\t\t\t} else {\n\t\t\t\tthis.#zkLoginSession = state;\n\t\t\t}\n\n\t\t\treturn this.#zkLoginSession;\n\t\t} finally {\n\t\t\tthis.#zkLoginSessionInitialized = true;\n\t\t}\n\t}\n\n\tasync logout() {\n\t\tthis.$zkLoginState.set({});\n\t\tthis.#store.delete(STORAGE_KEYS.STATE);\n\n\t\tawait this.#setSession(null);\n\t}\n\n\t// TODO: Should this return the proof if it already exists?\n\tasync getProof() {\n\t\tconst zkp = await this.getSession();\n\t\tconst { salt } = this.$zkLoginState.get();\n\n\t\tif (zkp?.proof) {\n\t\t\tif (zkp.expiresAt && Date.now() > zkp.expiresAt) {\n\t\t\t\tthrow new Error('Stored proof is expired.');\n\t\t\t}\n\n\t\t\treturn zkp.proof;\n\t\t}\n\n\t\tif (!salt || !zkp || !zkp.jwt) {\n\t\t\tthrow new Error('Missing required parameters for proof generation');\n\t\t}\n\n\t\tconst ephemeralKeyPair = Ed25519Keypair.fromSecretKey(fromB64(zkp.ephemeralKeyPair));\n\n\t\tconst proof = await this.#enokiClient.createZkLoginZkp({\n\t\t\tjwt: zkp.jwt!,\n\t\t\tmaxEpoch: zkp.maxEpoch!,\n\t\t\trandomness: zkp.randomness!,\n\t\t\tephemeralPublicKey: ephemeralKeyPair.getPublicKey(),\n\t\t});\n\n\t\tawait this.#setSession({\n\t\t\t...zkp,\n\t\t\tproof,\n\t\t});\n\n\t\treturn proof;\n\t}\n\n\tasync getKeypair() {\n\t\tconst zkp = await this.getSession();\n\n\t\t// Get the proof, so that we ensure it exists in state:\n\t\tawait this.getProof();\n\n\t\t// Check to see if we have the essentials for a keypair:\n\t\tconst { address } = this.$zkLoginState.get();\n\t\tif (!address || !zkp || !zkp.proof) {\n\t\t\tthrow new Error('Missing required data for keypair generation.');\n\t\t}\n\n\t\tif (Date.now() > zkp.expiresAt) {\n\t\t\tthrow new Error('Stored proof is expired.');\n\t\t}\n\n\t\treturn new EnokiKeypair({\n\t\t\taddress,\n\t\t\tmaxEpoch: zkp.maxEpoch,\n\t\t\tproof: zkp.proof,\n\t\t\tephemeralKeypair: Ed25519Keypair.fromSecretKey(fromB64(zkp.ephemeralKeyPair)),\n\t\t});\n\t}\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAA+B;AAC/B,mBAAwB;AAExB,kBAA0B;AAE1B,wBAA4B;AAG5B,wBAAwC;AAExC,yBAA4B;AAC5B,0BAA6B;AAE7B,oBAAqC;AAhBrC;AAmDA,MAAM,eAAe;AAAA,EACpB,OAAO;AAAA,EACP,SAAS;AACV;AAEO,MAAM,UAAU;AAAA,EAWtB,YAAY,QAAyB;AAsIrC,uBAAM;AAhJN;AACA;AACA;AACA;AAEA;AACA;AAKC,uBAAK,cAAe,IAAI,+BAAY;AAAA,MACnC,QAAQ,OAAO;AAAA,MACf,QAAQ,OAAO;AAAA,IAChB,CAAC;AACD,uBAAK,gBAAiB,OAAO;AAC7B,uBAAK,aAAc,OAAO,kBAAc,2CAAwB;AAChE,uBAAK,QAAS,OAAO,aAAS,oCAAqB;AAEnD,QAAI,cAAc;AAClB,QAAI;AACH,YAAM,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AW;AAGlC,UAAM,KAAK,SAAS;AAGpB,UAAM,EAAE,QAAQ,IAAI,KAAK,cAAc,IAAI;AAC3C,QAAI,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,OAAO;AACnC,YAAM,IAAI,MAAM,+CAA+C;AAAA,IAChE;AAEA,QAAI,KAAK,IAAI,IAAI,IAAI,WAAW;AAC/B,YAAM,IAAI,MAAM,0BAA0B;AAAA,IAC3C;AAEA,WAAO,IAAI,iCAAa;AAAA,MACvB;AAAA,MACA,UAAU,IAAI;AAAA,MACd,OAAO,IAAI;AAAA,MACX,kBAAkB,8BAAe,kBAAc,sBAAQ,IAAI,gBAAgB,CAAC;AAAA,IAC7E,CAAC;AAAA,EACF;AACD;AA1PC;AACA;AACA;AACA;AAEA;AACA;AA0IM;AAAA,gBAAW,eAAC,UAAiC;AAClD,MAAI,UAAU;AACb,UAAM,cAAc,MAAM,mBAAK,aAAY;AAAA,MAC1C,mBAAK;AAAA,MACL,KAAK,UAAU,QAAQ;AAAA,IACxB;AAEA,uBAAK,QAAO,IAAI,aAAa,SAAS,WAAW;AAAA,EAClD,OAAO;AACN,uBAAK,QAAO,OAAO,aAAa,OAAO;AAAA,EACxC;AAEA,qBAAK,iBAAkB;AACxB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,31 @@
1
+ import type { SignatureWithBytes } from '@mysten/sui.js/cryptography';
2
+ import { Keypair, PublicKey } from '@mysten/sui.js/cryptography';
3
+ import type { Ed25519Keypair, Ed25519PublicKey } from '@mysten/sui.js/keypairs/ed25519';
4
+ import type { ZkLoginSignatureInputs } from '@mysten/sui.js/zklogin';
5
+ export declare class EnokiPublicKey extends PublicKey {
6
+ #private;
7
+ constructor(input: {
8
+ address: string;
9
+ ephemeralPublicKey: Ed25519PublicKey;
10
+ });
11
+ flag(): number;
12
+ toSuiAddress(): string;
13
+ verify(): never;
14
+ toRawBytes(): Uint8Array;
15
+ }
16
+ export declare class EnokiKeypair extends Keypair {
17
+ #private;
18
+ constructor(input: {
19
+ address: string;
20
+ maxEpoch: number;
21
+ proof: ZkLoginSignatureInputs;
22
+ ephemeralKeypair: Ed25519Keypair;
23
+ });
24
+ signData(data: Uint8Array): Uint8Array;
25
+ sign(data: Uint8Array): Promise<Uint8Array>;
26
+ signPersonalMessage(): never;
27
+ signTransactionBlock(bytes: Uint8Array): Promise<SignatureWithBytes>;
28
+ getKeyScheme(): import("@mysten/sui.js/cryptography").SignatureScheme;
29
+ getPublicKey(): EnokiPublicKey;
30
+ export(): never;
31
+ }
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var __accessCheck = (obj, member, msg) => {
20
+ if (!member.has(obj))
21
+ throw TypeError("Cannot " + msg);
22
+ };
23
+ var __privateGet = (obj, member, getter) => {
24
+ __accessCheck(obj, member, "read from private field");
25
+ return getter ? getter.call(obj) : member.get(obj);
26
+ };
27
+ var __privateAdd = (obj, member, value) => {
28
+ if (member.has(obj))
29
+ throw TypeError("Cannot add the same private member more than once");
30
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
31
+ };
32
+ var __privateSet = (obj, member, value, setter) => {
33
+ __accessCheck(obj, member, "write to private field");
34
+ setter ? setter.call(obj, value) : member.set(obj, value);
35
+ return value;
36
+ };
37
+ var EnokiKeypair_exports = {};
38
+ __export(EnokiKeypair_exports, {
39
+ EnokiKeypair: () => EnokiKeypair,
40
+ EnokiPublicKey: () => EnokiPublicKey
41
+ });
42
+ module.exports = __toCommonJS(EnokiKeypair_exports);
43
+ var import_cryptography = require("@mysten/sui.js/cryptography");
44
+ var import_zklogin = require("@mysten/zklogin");
45
+ var _address, _ephemeralPublicKey, _proof, _maxEpoch, _ephemeralKeypair, _publicKey;
46
+ class EnokiPublicKey extends import_cryptography.PublicKey {
47
+ constructor(input) {
48
+ super();
49
+ __privateAdd(this, _address, void 0);
50
+ __privateAdd(this, _ephemeralPublicKey, void 0);
51
+ __privateSet(this, _address, input.address);
52
+ __privateSet(this, _ephemeralPublicKey, input.ephemeralPublicKey);
53
+ }
54
+ flag() {
55
+ return import_cryptography.SIGNATURE_SCHEME_TO_FLAG["ZkLogin"];
56
+ }
57
+ toSuiAddress() {
58
+ return __privateGet(this, _address);
59
+ }
60
+ verify() {
61
+ throw new Error("Verification for EnokiPublicKey is not supported");
62
+ }
63
+ toRawBytes() {
64
+ return __privateGet(this, _ephemeralPublicKey).toRawBytes();
65
+ }
66
+ }
67
+ _address = new WeakMap();
68
+ _ephemeralPublicKey = new WeakMap();
69
+ class EnokiKeypair extends import_cryptography.Keypair {
70
+ constructor(input) {
71
+ super();
72
+ __privateAdd(this, _proof, void 0);
73
+ __privateAdd(this, _maxEpoch, void 0);
74
+ __privateAdd(this, _ephemeralKeypair, void 0);
75
+ __privateAdd(this, _publicKey, void 0);
76
+ __privateSet(this, _proof, input.proof);
77
+ __privateSet(this, _maxEpoch, input.maxEpoch);
78
+ __privateSet(this, _ephemeralKeypair, input.ephemeralKeypair);
79
+ __privateSet(this, _publicKey, new EnokiPublicKey({
80
+ address: input.address,
81
+ ephemeralPublicKey: input.ephemeralKeypair.getPublicKey()
82
+ }));
83
+ }
84
+ signData(data) {
85
+ return __privateGet(this, _ephemeralKeypair).signData(data);
86
+ }
87
+ async sign(data) {
88
+ return __privateGet(this, _ephemeralKeypair).sign(data);
89
+ }
90
+ signPersonalMessage() {
91
+ throw new Error("Signing personal messages is not supported");
92
+ }
93
+ async signTransactionBlock(bytes) {
94
+ const { bytes: signedBytes, signature: userSignature } = await __privateGet(this, _ephemeralKeypair).signTransactionBlock(bytes);
95
+ const zkSignature = (0, import_zklogin.getZkLoginSignature)({
96
+ inputs: __privateGet(this, _proof),
97
+ maxEpoch: __privateGet(this, _maxEpoch),
98
+ userSignature
99
+ });
100
+ return {
101
+ bytes: signedBytes,
102
+ signature: zkSignature
103
+ };
104
+ }
105
+ getKeyScheme() {
106
+ return __privateGet(this, _ephemeralKeypair).getKeyScheme();
107
+ }
108
+ getPublicKey() {
109
+ return __privateGet(this, _publicKey);
110
+ }
111
+ export() {
112
+ throw new Error("EnokiKeypair does not support exporting");
113
+ }
114
+ }
115
+ _proof = new WeakMap();
116
+ _maxEpoch = new WeakMap();
117
+ _ephemeralKeypair = new WeakMap();
118
+ _publicKey = new WeakMap();
119
+ //# sourceMappingURL=EnokiKeypair.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/EnokiKeypair.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SignatureWithBytes } from '@mysten/sui.js/cryptography';\nimport { Keypair, PublicKey, SIGNATURE_SCHEME_TO_FLAG } from '@mysten/sui.js/cryptography';\nimport type { Ed25519Keypair, Ed25519PublicKey } from '@mysten/sui.js/keypairs/ed25519';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui.js/zklogin';\nimport { getZkLoginSignature } from '@mysten/zklogin';\n\nexport class EnokiPublicKey extends PublicKey {\n\t#address: string;\n\t#ephemeralPublicKey: Ed25519PublicKey;\n\n\tconstructor(input: { address: string; ephemeralPublicKey: Ed25519PublicKey }) {\n\t\tsuper();\n\t\tthis.#address = input.address;\n\t\tthis.#ephemeralPublicKey = input.ephemeralPublicKey;\n\t}\n\n\tflag(): number {\n\t\treturn SIGNATURE_SCHEME_TO_FLAG['ZkLogin'];\n\t}\n\n\ttoSuiAddress(): string {\n\t\treturn this.#address;\n\t}\n\n\tverify(): never {\n\t\tthrow new Error('Verification for EnokiPublicKey is not supported');\n\t}\n\n\ttoRawBytes(): Uint8Array {\n\t\treturn this.#ephemeralPublicKey.toRawBytes();\n\t}\n}\n\nexport class EnokiKeypair extends Keypair {\n\t#proof: ZkLoginSignatureInputs;\n\t#maxEpoch: number;\n\t#ephemeralKeypair: Ed25519Keypair;\n\t#publicKey: EnokiPublicKey;\n\n\tconstructor(input: {\n\t\taddress: string;\n\t\tmaxEpoch: number;\n\t\tproof: ZkLoginSignatureInputs;\n\t\tephemeralKeypair: Ed25519Keypair;\n\t}) {\n\t\tsuper();\n\t\tthis.#proof = input.proof;\n\t\tthis.#maxEpoch = input.maxEpoch;\n\t\tthis.#ephemeralKeypair = input.ephemeralKeypair;\n\t\tthis.#publicKey = new EnokiPublicKey({\n\t\t\taddress: input.address,\n\t\t\tephemeralPublicKey: input.ephemeralKeypair.getPublicKey(),\n\t\t});\n\t}\n\n\tsignData(data: Uint8Array) {\n\t\treturn this.#ephemeralKeypair.signData(data);\n\t}\n\n\tasync sign(data: Uint8Array) {\n\t\treturn this.#ephemeralKeypair.sign(data);\n\t}\n\n\tsignPersonalMessage(): never {\n\t\tthrow new Error('Signing personal messages is not supported');\n\t}\n\n\tasync signTransactionBlock(bytes: Uint8Array): Promise<SignatureWithBytes> {\n\t\tconst { bytes: signedBytes, signature: userSignature } =\n\t\t\tawait this.#ephemeralKeypair.signTransactionBlock(bytes);\n\n\t\tconst zkSignature = getZkLoginSignature({\n\t\t\tinputs: this.#proof,\n\t\t\tmaxEpoch: this.#maxEpoch,\n\t\t\tuserSignature,\n\t\t});\n\n\t\treturn {\n\t\t\tbytes: signedBytes,\n\t\t\tsignature: zkSignature,\n\t\t};\n\t}\n\n\tgetKeyScheme() {\n\t\treturn this.#ephemeralKeypair.getKeyScheme();\n\t}\n\n\tgetPublicKey() {\n\t\treturn this.#publicKey;\n\t}\n\n\texport(): never {\n\t\tthrow new Error('EnokiKeypair does not support exporting');\n\t}\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,0BAA6D;AAG7D,qBAAoC;AAPpC;AASO,MAAM,uBAAuB,8BAAU;AAAA,EAI7C,YAAY,OAAkE;AAC7E,UAAM;AAJP;AACA;AAIC,uBAAK,UAAW,MAAM;AACtB,uBAAK,qBAAsB,MAAM;AAAA,EAClC;AAAA,EAEA,OAAe;AACd,WAAO,6CAAyB,SAAS;AAAA,EAC1C;AAAA,EAEA,eAAuB;AACtB,WAAO,mBAAK;AAAA,EACb;AAAA,EAEA,SAAgB;AACf,UAAM,IAAI,MAAM,kDAAkD;AAAA,EACnE;AAAA,EAEA,aAAyB;AACxB,WAAO,mBAAK,qBAAoB,WAAW;AAAA,EAC5C;AACD;AAxBC;AACA;AAyBM,MAAM,qBAAqB,4BAAQ;AAAA,EAMzC,YAAY,OAKT;AACF,UAAM;AAXP;AACA;AACA;AACA;AASC,uBAAK,QAAS,MAAM;AACpB,uBAAK,WAAY,MAAM;AACvB,uBAAK,mBAAoB,MAAM;AAC/B,uBAAK,YAAa,IAAI,eAAe;AAAA,MACpC,SAAS,MAAM;AAAA,MACf,oBAAoB,MAAM,iBAAiB,aAAa;AAAA,IACzD,CAAC;AAAA,EACF;AAAA,EAEA,SAAS,MAAkB;AAC1B,WAAO,mBAAK,mBAAkB,SAAS,IAAI;AAAA,EAC5C;AAAA,EAEA,MAAM,KAAK,MAAkB;AAC5B,WAAO,mBAAK,mBAAkB,KAAK,IAAI;AAAA,EACxC;AAAA,EAEA,sBAA6B;AAC5B,UAAM,IAAI,MAAM,4CAA4C;AAAA,EAC7D;AAAA,EAEA,MAAM,qBAAqB,OAAgD;AAC1E,UAAM,EAAE,OAAO,aAAa,WAAW,cAAc,IACpD,MAAM,mBAAK,mBAAkB,qBAAqB,KAAK;AAExD,UAAM,kBAAc,oCAAoB;AAAA,MACvC,QAAQ,mBAAK;AAAA,MACb,UAAU,mBAAK;AAAA,MACf;AAAA,IACD,CAAC;AAED,WAAO;AAAA,MACN,OAAO;AAAA,MACP,WAAW;AAAA,IACZ;AAAA,EACD;AAAA,EAEA,eAAe;AACd,WAAO,mBAAK,mBAAkB,aAAa;AAAA,EAC5C;AAAA,EAEA,eAAe;AACd,WAAO,mBAAK;AAAA,EACb;AAAA,EAEA,SAAgB;AACf,UAAM,IAAI,MAAM,yCAAyC;AAAA,EAC1D;AACD;AA5DC;AACA;AACA;AACA;",
6
+ "names": []
7
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * An interface
3
+ */
4
+ export interface Encryption {
5
+ encrypt(password: string, data: string): Promise<string>;
6
+ decrypt(password: string, data: string): Promise<string>;
7
+ }
8
+ /**
9
+ * Create the default encryption interface, which uses the browsers built-in crypto primitives.
10
+ */
11
+ export declare function createDefaultEncryption(): Encryption;
12
+ /**
13
+ * Create a passthrough encryption interface, which does not encrypt or decrypt data.
14
+ */
15
+ export declare function createPassthroughEncryption(): Encryption;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var encryption_exports = {};
20
+ __export(encryption_exports, {
21
+ createDefaultEncryption: () => createDefaultEncryption,
22
+ createPassthroughEncryption: () => createPassthroughEncryption
23
+ });
24
+ module.exports = __toCommonJS(encryption_exports);
25
+ var import_browser_passworder = require("@metamask/browser-passworder");
26
+ function createDefaultEncryption() {
27
+ return {
28
+ async encrypt(password, data) {
29
+ return (0, import_browser_passworder.encrypt)(password, data);
30
+ },
31
+ async decrypt(password, data) {
32
+ const decrypted = await (0, import_browser_passworder.decrypt)(password, data);
33
+ return decrypted;
34
+ }
35
+ };
36
+ }
37
+ function createPassthroughEncryption() {
38
+ return {
39
+ async encrypt(_password, data) {
40
+ return data;
41
+ },
42
+ async decrypt(_password, data) {
43
+ return data;
44
+ }
45
+ };
46
+ }
47
+ //# sourceMappingURL=encryption.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/encryption.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport {\n\tdecrypt as metamaskDecrypt,\n\tencrypt as metamaskEncrypt,\n} from '@metamask/browser-passworder';\n\n/**\n * An interface\n */\nexport interface Encryption {\n\tencrypt(password: string, data: string): Promise<string>;\n\tdecrypt(password: string, data: string): Promise<string>;\n}\n\n/**\n * Create the default encryption interface, which uses the browsers built-in crypto primitives.\n */\nexport function createDefaultEncryption(): Encryption {\n\treturn {\n\t\tasync encrypt(password, data) {\n\t\t\treturn metamaskEncrypt(password, data);\n\t\t},\n\t\tasync decrypt(password, data) {\n\t\t\tconst decrypted = await metamaskDecrypt(password, data);\n\t\t\treturn decrypted as string;\n\t\t},\n\t};\n}\n\n/**\n * Create a passthrough encryption interface, which does not encrypt or decrypt data.\n */\nexport function createPassthroughEncryption(): Encryption {\n\treturn {\n\t\tasync encrypt(_password, data) {\n\t\t\treturn data;\n\t\t},\n\t\tasync decrypt(_password, data) {\n\t\t\treturn data;\n\t\t},\n\t};\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,gCAGO;AAaA,SAAS,0BAAsC;AACrD,SAAO;AAAA,IACN,MAAM,QAAQ,UAAU,MAAM;AAC7B,iBAAO,0BAAAA,SAAgB,UAAU,IAAI;AAAA,IACtC;AAAA,IACA,MAAM,QAAQ,UAAU,MAAM;AAC7B,YAAM,YAAY,UAAM,0BAAAC,SAAgB,UAAU,IAAI;AACtD,aAAO;AAAA,IACR;AAAA,EACD;AACD;AAKO,SAAS,8BAA0C;AACzD,SAAO;AAAA,IACN,MAAM,QAAQ,WAAW,MAAM;AAC9B,aAAO;AAAA,IACR;AAAA,IACA,MAAM,QAAQ,WAAW,MAAM;AAC9B,aAAO;AAAA,IACR;AAAA,EACD;AACD;",
6
+ "names": ["metamaskEncrypt", "metamaskDecrypt"]
7
+ }
@@ -0,0 +1,5 @@
1
+ export { EnokiClient, type EnokiClientConfig } from './EnokiClient/index.js';
2
+ export { EnokiFlow, type AuthProvider, type EnokiFlowConfig } from './EnokiFlow.js';
3
+ export { createLocalStorage, createSessionStorage, createInMemoryStorage, type SyncStore, } from './stores.js';
4
+ export { createDefaultEncryption, type Encryption } from './encryption.js';
5
+ export { EnokiKeypair, EnokiPublicKey } from './EnokiKeypair.js';
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var src_exports = {};
20
+ __export(src_exports, {
21
+ EnokiClient: () => import_EnokiClient.EnokiClient,
22
+ EnokiFlow: () => import_EnokiFlow.EnokiFlow,
23
+ EnokiKeypair: () => import_EnokiKeypair.EnokiKeypair,
24
+ EnokiPublicKey: () => import_EnokiKeypair.EnokiPublicKey,
25
+ createDefaultEncryption: () => import_encryption.createDefaultEncryption,
26
+ createInMemoryStorage: () => import_stores.createInMemoryStorage,
27
+ createLocalStorage: () => import_stores.createLocalStorage,
28
+ createSessionStorage: () => import_stores.createSessionStorage
29
+ });
30
+ module.exports = __toCommonJS(src_exports);
31
+ var import_EnokiClient = require("./EnokiClient/index.js");
32
+ var import_EnokiFlow = require("./EnokiFlow.js");
33
+ var import_stores = require("./stores.js");
34
+ var import_encryption = require("./encryption.js");
35
+ var import_EnokiKeypair = require("./EnokiKeypair.js");
36
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport { EnokiClient, type EnokiClientConfig } from './EnokiClient/index.js';\nexport { EnokiFlow, type AuthProvider, type EnokiFlowConfig } from './EnokiFlow.js';\nexport {\n\tcreateLocalStorage,\n\tcreateSessionStorage,\n\tcreateInMemoryStorage,\n\ttype SyncStore,\n} from './stores.js';\nexport { createDefaultEncryption, type Encryption } from './encryption.js';\nexport { EnokiKeypair, EnokiPublicKey } from './EnokiKeypair.js';\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,yBAAoD;AACpD,uBAAmE;AACnE,oBAKO;AACP,wBAAyD;AACzD,0BAA6C;",
6
+ "names": []
7
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "private": true,
3
+ "type": "commonjs"
4
+ }
@@ -0,0 +1,9 @@
1
+ import type { ReactNode } from 'react';
2
+ import type { EnokiFlowConfig } from './EnokiFlow.js';
3
+ import { EnokiFlow } from './EnokiFlow.js';
4
+ export interface EnokiFlowProviderProps extends EnokiFlowConfig {
5
+ children: ReactNode;
6
+ }
7
+ export declare function EnokiFlowProvider({ children, ...config }: EnokiFlowProviderProps): import("react/jsx-runtime.js").JSX.Element;
8
+ export declare function useEnokiFlow(): EnokiFlow;
9
+ export declare function useZkLogin(): import("./EnokiFlow.js").ZkLoginState;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var react_exports = {};
20
+ __export(react_exports, {
21
+ EnokiFlowProvider: () => EnokiFlowProvider,
22
+ useEnokiFlow: () => useEnokiFlow,
23
+ useZkLogin: () => useZkLogin
24
+ });
25
+ module.exports = __toCommonJS(react_exports);
26
+ var import_jsx_runtime = require("react/jsx-runtime");
27
+ var import_react = require("@nanostores/react");
28
+ var import_react2 = require("react");
29
+ var import_EnokiFlow = require("./EnokiFlow.js");
30
+ const EnokiFlowContext = (0, import_react2.createContext)(null);
31
+ function EnokiFlowProvider({ children, ...config }) {
32
+ const [enokiFlow] = (0, import_react2.useState)(() => new import_EnokiFlow.EnokiFlow(config));
33
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(EnokiFlowContext.Provider, { value: enokiFlow, children });
34
+ }
35
+ function useEnokiFlow() {
36
+ const context = (0, import_react2.useContext)(EnokiFlowContext);
37
+ if (!context) {
38
+ throw new Error("Missing `EnokiFlowContext` provider");
39
+ }
40
+ return context;
41
+ }
42
+ function useZkLogin() {
43
+ const flow = useEnokiFlow();
44
+ return (0, import_react.useStore)(flow.$zkLoginState);
45
+ }
46
+ //# sourceMappingURL=react.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/react.tsx"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { useStore } from '@nanostores/react';\nimport type { ReactNode } from 'react';\nimport { createContext, useContext, useState } from 'react';\n\nimport type { EnokiFlowConfig } from './EnokiFlow.js';\nimport { EnokiFlow } from './EnokiFlow.js';\n\nconst EnokiFlowContext = createContext<EnokiFlow | null>(null);\n\nexport interface EnokiFlowProviderProps extends EnokiFlowConfig {\n\tchildren: ReactNode;\n}\n\nexport function EnokiFlowProvider({ children, ...config }: EnokiFlowProviderProps) {\n\tconst [enokiFlow] = useState(() => new EnokiFlow(config));\n\treturn <EnokiFlowContext.Provider value={enokiFlow}>{children}</EnokiFlowContext.Provider>;\n}\n\nexport function useEnokiFlow() {\n\tconst context = useContext(EnokiFlowContext);\n\tif (!context) {\n\t\tthrow new Error('Missing `EnokiFlowContext` provider');\n\t}\n\treturn context;\n}\n\nexport function useZkLogin() {\n\tconst flow = useEnokiFlow();\n\treturn useStore(flow.$zkLoginState);\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBQ;AAfR,mBAAyB;AAEzB,IAAAA,gBAAoD;AAGpD,uBAA0B;AAE1B,MAAM,uBAAmB,6BAAgC,IAAI;AAMtD,SAAS,kBAAkB,EAAE,UAAU,GAAG,OAAO,GAA2B;AAClF,QAAM,CAAC,SAAS,QAAI,wBAAS,MAAM,IAAI,2BAAU,MAAM,CAAC;AACxD,SAAO,4CAAC,iBAAiB,UAAjB,EAA0B,OAAO,WAAY,UAAS;AAC/D;AAEO,SAAS,eAAe;AAC9B,QAAM,cAAU,0BAAW,gBAAgB;AAC3C,MAAI,CAAC,SAAS;AACb,UAAM,IAAI,MAAM,qCAAqC;AAAA,EACtD;AACA,SAAO;AACR;AAEO,SAAS,aAAa;AAC5B,QAAM,OAAO,aAAa;AAC1B,aAAO,uBAAS,KAAK,aAAa;AACnC;",
6
+ "names": ["import_react"]
7
+ }