@mysten/enoki 0.3.9 → 0.3.11
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/CHANGELOG.md +28 -2
- package/dist/cjs/EnokiClient/index.js +20 -34
- package/dist/cjs/EnokiClient/index.js.map +2 -2
- package/dist/cjs/EnokiFlow.js +21 -36
- package/dist/cjs/EnokiFlow.js.map +1 -1
- package/dist/cjs/EnokiKeypair.js +13 -24
- package/dist/cjs/EnokiKeypair.js.map +1 -1
- package/dist/cjs/react.js +1 -2
- package/dist/cjs/react.js.map +1 -1
- package/dist/esm/EnokiClient/index.js +19 -33
- package/dist/esm/EnokiClient/index.js.map +2 -2
- package/dist/esm/EnokiFlow.js +20 -35
- package/dist/esm/EnokiFlow.js.map +1 -1
- package/dist/esm/EnokiKeypair.js +12 -23
- package/dist/esm/EnokiKeypair.js.map +1 -1
- package/dist/esm/react.js +1 -2
- package/dist/esm/react.js.map +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +14 -14
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/EnokiClient/index.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type {\n\tCreateSponsoredTransactionApiInput,\n\tCreateSponsoredTransactionApiResponse,\n\tCreateZkLoginNonceApiInput,\n\tCreateZkLoginNonceApiResponse,\n\tCreateZkLoginZkpApiInput,\n\tCreateZkLoginZkpApiResponse,\n\tExecuteSponsoredTransactionApiInput,\n\tExecuteSponsoredTransactionApiResponse,\n\tGetAppApiInput,\n\tGetAppApiResponse,\n\tGetZkLoginApiInput,\n\tGetZkLoginApiResponse,\n} from './type.js';\n\nconst DEFAULT_API_URL = 'https://api.enoki.mystenlabs.com';\nconst ZKLOGIN_HEADER = 'zklogin-jwt';\n\nexport interface EnokiClientConfig {\n\t/** The API key for the Enoki app, available in the Enoki Portal. */\n\tapiKey: string;\n\n\t/** The API URL for Enoki. In most cases, this should not be set. */\n\tapiUrl?: string;\n}\n\nexport class EnokiClientError extends Error {\n\terrors: { code: string; message: string; data: unknown }[] = [];\n\n\tconstructor(status: number, response: string) {\n\t\tlet errors;\n\t\ttry {\n\t\t\tconst parsedResponse = JSON.parse(response) as {\n\t\t\t\terrors: { code: string; message: string; data: unknown }[];\n\t\t\t};\n\t\t\terrors = parsedResponse.errors;\n\t\t} catch (e) {\n\t\t\t// Ignore\n\t\t}\n\t\tconst cause = errors?.[0] ? new Error(errors[0].message) : undefined;\n\t\tsuper(`Request to Enoki API failed (status: ${status})`, {\n\t\t\tcause,\n\t\t});\n\t\tthis.errors = errors ?? [];\n\t\tthis.name = 'EnokiClientError';\n\t}\n}\n\n/**\n * A low-level client for interacting with the Enoki API.\n */\nexport class EnokiClient {\n\t#version: string;\n\t#apiUrl: string;\n\t#apiKey: string;\n\n\tconstructor(config: EnokiClientConfig) {\n\t\tthis.#version = 'v1';\n\t\tthis.#apiUrl = config.apiUrl ?? DEFAULT_API_URL;\n\t\tthis.#apiKey = config.apiKey;\n\t}\n\n\tgetApp(_input?: GetAppApiInput) {\n\t\treturn this.#fetch<GetAppApiResponse>('app', {\n\t\t\tmethod: 'GET',\n\t\t});\n\t}\n\n\tgetZkLogin(input: GetZkLoginApiInput) {\n\t\treturn this.#fetch<GetZkLoginApiResponse>('zklogin', {\n\t\t\tmethod: 'GET',\n\t\t\theaders: {\n\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t},\n\t\t});\n\t}\n\n\tcreateZkLoginNonce(input: CreateZkLoginNonceApiInput) {\n\t\treturn this.#fetch<CreateZkLoginNonceApiResponse>('zklogin/nonce', {\n\t\t\tmethod: 'POST',\n\t\t\tbody: JSON.stringify({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),\n\t\t\t\tadditionalEpochs: input.additionalEpochs,\n\t\t\t}),\n\t\t});\n\t}\n\n\tcreateZkLoginZkp(input: CreateZkLoginZkpApiInput) {\n\t\treturn this.#fetch<CreateZkLoginZkpApiResponse>('zklogin/zkp', {\n\t\t\tmethod: 'POST',\n\t\t\theaders: {\n\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),\n\t\t\t\tmaxEpoch: input.maxEpoch,\n\t\t\t\trandomness: input.randomness,\n\t\t\t}),\n\t\t});\n\t}\n\n\tcreateSponsoredTransaction(input: CreateSponsoredTransactionApiInput) {\n\t\treturn this.#fetch<CreateSponsoredTransactionApiResponse>('transaction-blocks/sponsor', {\n\t\t\tmethod: 'POST',\n\t\t\theaders: input.jwt\n\t\t\t\t? {\n\t\t\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t\t
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type {\n\tCreateSponsoredTransactionApiInput,\n\tCreateSponsoredTransactionApiResponse,\n\tCreateZkLoginNonceApiInput,\n\tCreateZkLoginNonceApiResponse,\n\tCreateZkLoginZkpApiInput,\n\tCreateZkLoginZkpApiResponse,\n\tExecuteSponsoredTransactionApiInput,\n\tExecuteSponsoredTransactionApiResponse,\n\tGetAppApiInput,\n\tGetAppApiResponse,\n\tGetZkLoginApiInput,\n\tGetZkLoginApiResponse,\n} from './type.js';\n\nconst DEFAULT_API_URL = 'https://api.enoki.mystenlabs.com';\nconst ZKLOGIN_HEADER = 'zklogin-jwt';\n\nexport interface EnokiClientConfig {\n\t/** The API key for the Enoki app, available in the Enoki Portal. */\n\tapiKey: string;\n\n\t/** The API URL for Enoki. In most cases, this should not be set. */\n\tapiUrl?: string;\n}\n\nexport class EnokiClientError extends Error {\n\terrors: { code: string; message: string; data: unknown }[] = [];\n\n\tconstructor(status: number, response: string) {\n\t\tlet errors;\n\t\ttry {\n\t\t\tconst parsedResponse = JSON.parse(response) as {\n\t\t\t\terrors: { code: string; message: string; data: unknown }[];\n\t\t\t};\n\t\t\terrors = parsedResponse.errors;\n\t\t} catch (e) {\n\t\t\t// Ignore\n\t\t}\n\t\tconst cause = errors?.[0] ? new Error(errors[0].message) : undefined;\n\t\tsuper(`Request to Enoki API failed (status: ${status})`, {\n\t\t\tcause,\n\t\t});\n\t\tthis.errors = errors ?? [];\n\t\tthis.name = 'EnokiClientError';\n\t}\n}\n\n/**\n * A low-level client for interacting with the Enoki API.\n */\nexport class EnokiClient {\n\t#version: string;\n\t#apiUrl: string;\n\t#apiKey: string;\n\n\tconstructor(config: EnokiClientConfig) {\n\t\tthis.#version = 'v1';\n\t\tthis.#apiUrl = config.apiUrl ?? DEFAULT_API_URL;\n\t\tthis.#apiKey = config.apiKey;\n\t}\n\n\tgetApp(_input?: GetAppApiInput) {\n\t\treturn this.#fetch<GetAppApiResponse>('app', {\n\t\t\tmethod: 'GET',\n\t\t});\n\t}\n\n\tgetZkLogin(input: GetZkLoginApiInput) {\n\t\treturn this.#fetch<GetZkLoginApiResponse>('zklogin', {\n\t\t\tmethod: 'GET',\n\t\t\theaders: {\n\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t},\n\t\t});\n\t}\n\n\tcreateZkLoginNonce(input: CreateZkLoginNonceApiInput) {\n\t\treturn this.#fetch<CreateZkLoginNonceApiResponse>('zklogin/nonce', {\n\t\t\tmethod: 'POST',\n\t\t\tbody: JSON.stringify({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),\n\t\t\t\tadditionalEpochs: input.additionalEpochs,\n\t\t\t}),\n\t\t});\n\t}\n\n\tcreateZkLoginZkp(input: CreateZkLoginZkpApiInput) {\n\t\treturn this.#fetch<CreateZkLoginZkpApiResponse>('zklogin/zkp', {\n\t\t\tmethod: 'POST',\n\t\t\theaders: {\n\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),\n\t\t\t\tmaxEpoch: input.maxEpoch,\n\t\t\t\trandomness: input.randomness,\n\t\t\t}),\n\t\t});\n\t}\n\n\tcreateSponsoredTransaction(input: CreateSponsoredTransactionApiInput) {\n\t\treturn this.#fetch<CreateSponsoredTransactionApiResponse>('transaction-blocks/sponsor', {\n\t\t\tmethod: 'POST',\n\t\t\theaders: input.jwt\n\t\t\t\t? {\n\t\t\t\t\t\t[ZKLOGIN_HEADER]: input.jwt,\n\t\t\t\t\t}\n\t\t\t\t: {},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tsender: input.sender,\n\t\t\t\tnetwork: input.network,\n\t\t\t\ttransactionBlockKindBytes: input.transactionKindBytes,\n\t\t\t\tallowedAddresses: input.allowedAddresses,\n\t\t\t\tallowedMoveCallTargets: input.allowedMoveCallTargets,\n\t\t\t}),\n\t\t});\n\t}\n\n\texecuteSponsoredTransaction(input: ExecuteSponsoredTransactionApiInput) {\n\t\treturn this.#fetch<ExecuteSponsoredTransactionApiResponse>(\n\t\t\t`transaction-blocks/sponsor/${input.digest}`,\n\t\t\t{\n\t\t\t\tmethod: 'POST',\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tsignature: input.signature,\n\t\t\t\t}),\n\t\t\t},\n\t\t);\n\t}\n\n\tasync #fetch<T = unknown>(path: string, init: RequestInit): Promise<T> {\n\t\tconst res = await fetch(`${this.#apiUrl}/${this.#version}/${path}`, {\n\t\t\t...init,\n\t\t\theaders: {\n\t\t\t\t...init.headers,\n\t\t\t\tAuthorization: `Bearer ${this.#apiKey}`,\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t'Request-Id': crypto.randomUUID(),\n\t\t\t},\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tthrow new EnokiClientError(res.status, await res.text());\n\t\t}\n\n\t\tconst { data } = await res.json();\n\n\t\treturn data as T;\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;AAAA;AAkBA,MAAM,kBAAkB;AACxB,MAAM,iBAAiB;AAUhB,MAAM,yBAAyB,MAAM;AAAA,EAG3C,YAAY,QAAgB,UAAkB;AAC7C,QAAI;AACJ,QAAI;AACH,YAAM,iBAAiB,KAAK,MAAM,QAAQ;AAG1C,eAAS,eAAe;AAAA,IACzB,SAAS,GAAG;AAAA,IAEZ;AACA,UAAM,QAAQ,SAAS,CAAC,IAAI,IAAI,MAAM,OAAO,CAAC,EAAE,OAAO,IAAI;AAC3D,UAAM,wCAAwC,MAAM,KAAK;AAAA,MACxD;AAAA,IACD,CAAC;AAfF,kBAA6D,CAAC;AAgB7D,SAAK,SAAS,UAAU,CAAC;AACzB,SAAK,OAAO;AAAA,EACb;AACD;AAKO,MAAM,YAAY;AAAA,EAKxB,YAAY,QAA2B;AALjC;AACN;AACA;AACA;AAGC,uBAAK,UAAW;AAChB,uBAAK,SAAU,OAAO,UAAU;AAChC,uBAAK,SAAU,OAAO;AAAA,EACvB;AAAA,EAEA,OAAO,QAAyB;AAC/B,WAAO,sBAAK,kCAAL,WAA+B,OAAO;AAAA,MAC5C,QAAQ;AAAA,IACT;AAAA,EACD;AAAA,EAEA,WAAW,OAA2B;AACrC,WAAO,sBAAK,kCAAL,WAAmC,WAAW;AAAA,MACpD,QAAQ;AAAA,MACR,SAAS;AAAA,QACR,CAAC,cAAc,GAAG,MAAM;AAAA,MACzB;AAAA,IACD;AAAA,EACD;AAAA,EAEA,mBAAmB,OAAmC;AACrD,WAAO,sBAAK,kCAAL,WAA2C,iBAAiB;AAAA,MAClE,QAAQ;AAAA,MACR,MAAM,KAAK,UAAU;AAAA,QACpB,SAAS,MAA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,eAAe,UAAU,mBAAK,QAAO;AAAA,MACrC,gBAAgB;AAAA,MAChB,cAAc,OAAO,WAAW;AAAA,IACjC;AAAA,EACD,CAAC;AAED,MAAI,CAAC,IAAI,IAAI;AACZ,UAAM,IAAI,iBAAiB,IAAI,QAAQ,MAAM,IAAI,KAAK,CAAC;AAAA,EACxD;AAEA,QAAM,EAAE,KAAK,IAAI,MAAM,IAAI,KAAK;AAEhC,SAAO;AACR;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/EnokiFlow.js
CHANGED
|
@@ -1,26 +1,12 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
throw TypeError("Cannot " + msg);
|
|
1
|
+
var __typeError = (msg) => {
|
|
2
|
+
throw TypeError(msg);
|
|
4
3
|
};
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
12
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
13
|
-
};
|
|
14
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
15
|
-
__accessCheck(obj, member, "write to private field");
|
|
16
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
17
|
-
return value;
|
|
18
|
-
};
|
|
19
|
-
var __privateMethod = (obj, member, method) => {
|
|
20
|
-
__accessCheck(obj, member, "access private method");
|
|
21
|
-
return method;
|
|
22
|
-
};
|
|
23
|
-
var _storageKeys, _enokiClient, _encryption, _encryptionKey, _store, _setSession, setSession_fn;
|
|
4
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
5
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
6
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
7
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
8
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
9
|
+
var _storageKeys, _enokiClient, _encryption, _encryptionKey, _store, _EnokiFlow_instances, setSession_fn;
|
|
24
10
|
import { decodeSuiPrivateKey } from "@mysten/sui/cryptography";
|
|
25
11
|
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
|
|
26
12
|
import { fromB64, toB64 } from "@mysten/sui/utils";
|
|
@@ -36,12 +22,12 @@ const createStorageKeys = (apiKey) => ({
|
|
|
36
22
|
});
|
|
37
23
|
class EnokiFlow {
|
|
38
24
|
constructor(config) {
|
|
39
|
-
__privateAdd(this,
|
|
40
|
-
__privateAdd(this, _storageKeys
|
|
41
|
-
__privateAdd(this, _enokiClient
|
|
42
|
-
__privateAdd(this, _encryption
|
|
43
|
-
__privateAdd(this, _encryptionKey
|
|
44
|
-
__privateAdd(this, _store
|
|
25
|
+
__privateAdd(this, _EnokiFlow_instances);
|
|
26
|
+
__privateAdd(this, _storageKeys);
|
|
27
|
+
__privateAdd(this, _enokiClient);
|
|
28
|
+
__privateAdd(this, _encryption);
|
|
29
|
+
__privateAdd(this, _encryptionKey);
|
|
30
|
+
__privateAdd(this, _store);
|
|
45
31
|
__privateSet(this, _enokiClient, new EnokiClient({
|
|
46
32
|
apiKey: config.apiKey,
|
|
47
33
|
apiUrl: config.apiUrl
|
|
@@ -108,7 +94,7 @@ class EnokiFlow {
|
|
|
108
94
|
throw new Error(`Invalid provider: ${input.provider}`);
|
|
109
95
|
}
|
|
110
96
|
this.$zkLoginState.set({ provider: input.provider });
|
|
111
|
-
await __privateMethod(this,
|
|
97
|
+
await __privateMethod(this, _EnokiFlow_instances, setSession_fn).call(this, {
|
|
112
98
|
expiresAt: estimatedExpiration,
|
|
113
99
|
maxEpoch,
|
|
114
100
|
randomness,
|
|
@@ -139,7 +125,7 @@ class EnokiFlow {
|
|
|
139
125
|
salt,
|
|
140
126
|
address
|
|
141
127
|
});
|
|
142
|
-
await __privateMethod(this,
|
|
128
|
+
await __privateMethod(this, _EnokiFlow_instances, setSession_fn).call(this, {
|
|
143
129
|
...zkp,
|
|
144
130
|
jwt
|
|
145
131
|
});
|
|
@@ -151,8 +137,7 @@ class EnokiFlow {
|
|
|
151
137
|
}
|
|
152
138
|
try {
|
|
153
139
|
const storedValue = __privateGet(this, _store).get(__privateGet(this, _storageKeys).SESSION);
|
|
154
|
-
if (!storedValue)
|
|
155
|
-
return null;
|
|
140
|
+
if (!storedValue) return null;
|
|
156
141
|
const state = JSON.parse(
|
|
157
142
|
await __privateGet(this, _encryption).decrypt(__privateGet(this, _encryptionKey), storedValue)
|
|
158
143
|
);
|
|
@@ -169,7 +154,7 @@ class EnokiFlow {
|
|
|
169
154
|
async logout() {
|
|
170
155
|
this.$zkLoginState.set({});
|
|
171
156
|
__privateGet(this, _store).delete(__privateGet(this, _storageKeys).STATE);
|
|
172
|
-
await __privateMethod(this,
|
|
157
|
+
await __privateMethod(this, _EnokiFlow_instances, setSession_fn).call(this, null);
|
|
173
158
|
}
|
|
174
159
|
// TODO: Should this return the proof if it already exists?
|
|
175
160
|
async getProof({ network } = {}) {
|
|
@@ -192,7 +177,7 @@ class EnokiFlow {
|
|
|
192
177
|
randomness: zkp.randomness,
|
|
193
178
|
ephemeralPublicKey: ephemeralKeyPair.getPublicKey()
|
|
194
179
|
});
|
|
195
|
-
await __privateMethod(this,
|
|
180
|
+
await __privateMethod(this, _EnokiFlow_instances, setSession_fn).call(this, {
|
|
196
181
|
...zkp,
|
|
197
182
|
proof
|
|
198
183
|
});
|
|
@@ -267,7 +252,7 @@ _enokiClient = new WeakMap();
|
|
|
267
252
|
_encryption = new WeakMap();
|
|
268
253
|
_encryptionKey = new WeakMap();
|
|
269
254
|
_store = new WeakMap();
|
|
270
|
-
|
|
255
|
+
_EnokiFlow_instances = new WeakSet();
|
|
271
256
|
setSession_fn = async function(newValue) {
|
|
272
257
|
if (newValue) {
|
|
273
258
|
const storedValue = await __privateGet(this, _encryption).encrypt(
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/EnokiFlow.ts"],
|
|
4
4
|
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SuiClient } from '@mysten/sui/client';\nimport { decodeSuiPrivateKey } from '@mysten/sui/cryptography';\nimport { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';\nimport type { Transaction } from '@mysten/sui/transactions';\nimport { fromB64, toB64 } from '@mysten/sui/utils';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';\nimport { decodeJwt } from 'jose';\nimport type { WritableAtom } from 'nanostores';\nimport { atom, onMount, 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 createStorageKeys = (apiKey: string) => ({\n\tSTATE: `@enoki/flow/state/${apiKey}`,\n\tSESSION: `@enoki/flow/session/${apiKey}`,\n});\n\nexport class EnokiFlow {\n\t#storageKeys: { STATE: string; SESSION: string };\n\t#enokiClient: EnokiClient;\n\t#encryption: Encryption;\n\t#encryptionKey: string;\n\t#store: SyncStore;\n\n\t$zkLoginSession: WritableAtom<{ initialized: boolean; value: ZkLoginSession | null }>;\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\t\tthis.#storageKeys = createStorageKeys(config.apiKey);\n\n\t\tlet storedState = null;\n\t\ttry {\n\t\t\tconst rawStoredValue = this.#store.get(this.#storageKeys.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\t\tthis.$zkLoginSession = atom({ initialized: false, value: null });\n\n\t\t// Hydrate the session on mount:\n\t\tonMount(this.$zkLoginSession, () => {\n\t\t\tthis.getSession();\n\t\t});\n\n\t\tonSet(this.$zkLoginState, ({ newValue }) => {\n\t\t\tthis.#store.set(this.#storageKeys.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\tnetwork?: 'mainnet' | 'testnet' | 'devnet';\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\tnetwork: input.network,\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: toB64(decodeSuiPrivateKey(ephemeralKeyPair.getSecretKey()).secretKey),\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(this.#storageKeys.SESSION, storedValue);\n\t\t} else {\n\t\t\tthis.#store.delete(this.#storageKeys.SESSION);\n\t\t}\n\n\t\tthis.$zkLoginSession.set({ initialized: true, value: newValue });\n\t}\n\n\tasync getSession() {\n\t\tif (this.$zkLoginSession.get().initialized) {\n\t\t\treturn this.$zkLoginSession.get().value;\n\t\t}\n\n\t\ttry {\n\t\t\tconst storedValue = this.#store.get(this.#storageKeys.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.set({ initialized: true, value: state });\n\t\t\t}\n\t\t} catch {\n\t\t\tthis.$zkLoginSession.set({ initialized: true, value: null });\n\t\t}\n\n\t\treturn this.$zkLoginSession.get().value;\n\t}\n\n\tasync logout() {\n\t\tthis.$zkLoginState.set({});\n\t\tthis.#store.delete(this.#storageKeys.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({ network }: { network?: 'mainnet' | 'testnet' } = {}) {\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\tnetwork,\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({ network }: { network?: 'mainnet' | 'testnet' } = {}) {\n\t\t// Get the proof, so that we ensure it exists in state:\n\t\tawait this.getProof({ network });\n\n\t\tconst zkp = await this.getSession();\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\tasync sponsorTransaction({\n\t\tnetwork,\n\t\ttransaction,\n\t\tclient,\n\t}: {\n\t\tnetwork?: 'mainnet' | 'testnet';\n\t\ttransaction: Transaction;\n\t\tclient: SuiClient;\n\t}) {\n\t\tconst session = await this.getSession();\n\n\t\tif (!session || !session.jwt) {\n\t\t\tthrow new Error('Missing required data for sponsorship.');\n\t\t}\n\n\t\tconst transactionKindBytes = await transaction.build({\n\t\t\tonlyTransactionKind: true,\n\t\t\tclient,\n\t\t});\n\n\t\treturn await this.#enokiClient.createSponsoredTransaction({\n\t\t\tjwt: session.jwt,\n\t\t\tnetwork,\n\t\t\ttransactionKindBytes: toB64(transactionKindBytes),\n\t\t});\n\t}\n\n\tasync executeTransaction({\n\t\tnetwork,\n\t\tbytes,\n\t\tdigest,\n\t\tclient,\n\t}: {\n\t\tnetwork?: 'mainnet' | 'testnet';\n\t\tbytes: string;\n\t\tdigest: string;\n\t\tclient: SuiClient;\n\t}) {\n\t\tconst keypair = await this.getKeypair({ network });\n\t\tconst userSignature = await keypair.signTransaction(fromB64(bytes));\n\n\t\tawait this.#enokiClient.executeSponsoredTransaction({\n\t\t\tdigest,\n\t\t\tsignature: userSignature.signature,\n\t\t});\n\n\t\t// TODO: Should the parent just do this?\n\t\tawait client.waitForTransaction({ digest });\n\n\t\treturn { digest };\n\t}\n\n\tasync sponsorAndExecuteTransaction({\n\t\tnetwork,\n\t\ttransaction,\n\t\tclient,\n\t}: {\n\t\tnetwork?: 'mainnet' | 'testnet';\n\t\ttransaction: Transaction;\n\t\tclient: SuiClient;\n\t}) {\n\t\tconst { bytes, digest } = await this.sponsorTransaction({\n\t\t\tnetwork,\n\t\t\ttransaction,\n\t\t\tclient,\n\t\t});\n\t\treturn await this.executeTransaction({ network, bytes, digest, client });\n\t}\n}\n"],
|
|
5
|
-
"mappings": "
|
|
5
|
+
"mappings": ";;;;;;;;AAAA;AAIA,SAAS,2BAA2B;AACpC,SAAS,sBAAsB;AAE/B,SAAS,SAAS,aAAa;AAE/B,SAAS,iBAAiB;AAE1B,SAAS,MAAM,SAAS,aAAa;AAGrC,SAAS,+BAA+B;AAExC,SAAS,mBAAmB;AAC5B,SAAS,oBAAoB;AAE7B,SAAS,4BAA4B;AAmCrC,MAAM,oBAAoB,CAAC,YAAoB;AAAA,EAC9C,OAAO,qBAAqB,MAAM;AAAA,EAClC,SAAS,uBAAuB,MAAM;AACvC;AAEO,MAAM,UAAU;AAAA,EAUtB,YAAY,QAAyB;AAV/B;AACN;AACA;AACA;AACA;AACA;AAMC,uBAAK,cAAe,IAAI,YAAY;AAAA,MACnC,QAAQ,OAAO;AAAA,MACf,QAAQ,OAAO;AAAA,IAChB,CAAC;AACD,uBAAK,gBAAiB,OAAO;AAC7B,uBAAK,aAAc,OAAO,cAAc,wBAAwB;AAChE,uBAAK,QAAS,OAAO,SAAS,qBAAqB;AACnD,uBAAK,cAAe,kBAAkB,OAAO,MAAM;AAEnD,QAAI,cAAc;AAClB,QAAI;AACH,YAAM,iBAAiB,mBAAK,QAAO,IAAI,mBAAK,cAAa,KAAK;AAC9D,UAAI,gBAAgB;AACnB,sBAAc,KAAK,MAAM,cAAc;AAAA,MACxC;AAAA,IACD,QAAQ;AAAA,IAER;AAEA,SAAK,gBAAgB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,MAAM,KAAK,WAAW,EAAE,QAAQ,CAAC;AACjD,UAAM,gBAAgB,MAAM,QAAQ,gBAAgB,QAAQ,KAAK,CAAC;AAElE,UAAM,mBAAK,cAAa,4BAA4B;AAAA,MACnD;AAAA,MACA,WAAW,cAAc;AAAA,IAC1B,CAAC;AAGD,UAAM,OAAO,mBAAmB,EAAE,OAAO,CAAC;AAE1C,WAAO,EAAE,OAAO;AAAA,EACjB;AAAA,EAEA,MAAM,6BAA6B;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,EACD,GAIG;AACF,UAAM,EAAE,OAAO,OAAO,IAAI,MAAM,KAAK,mBAAmB;AAAA,MACvD;AAAA,MACA;AAAA,MACA;AAAA,IACD,CAAC;AACD,WAAO,MAAM,KAAK,mBAAmB,EAAE,SAAS,OAAO,QAAQ,OAAO,CAAC;AAAA,EACxE;AACD;AArUC;AACA;AACA;AACA;AACA;AALM;AAsJA,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,mBAAK,cAAa,SAAS,WAAW;AAAA,EACvD,OAAO;AACN,uBAAK,QAAO,OAAO,mBAAK,cAAa,OAAO;AAAA,EAC7C;AAEA,OAAK,gBAAgB,IAAI,EAAE,aAAa,MAAM,OAAO,SAAS,CAAC;AAChE;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/EnokiKeypair.js
CHANGED
|
@@ -1,29 +1,18 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
throw TypeError("Cannot " + msg);
|
|
4
|
-
};
|
|
5
|
-
var __privateGet = (obj, member, getter) => {
|
|
6
|
-
__accessCheck(obj, member, "read from private field");
|
|
7
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
8
|
-
};
|
|
9
|
-
var __privateAdd = (obj, member, value) => {
|
|
10
|
-
if (member.has(obj))
|
|
11
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
12
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
13
|
-
};
|
|
14
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
15
|
-
__accessCheck(obj, member, "write to private field");
|
|
16
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
17
|
-
return value;
|
|
1
|
+
var __typeError = (msg) => {
|
|
2
|
+
throw TypeError(msg);
|
|
18
3
|
};
|
|
4
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
5
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
6
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
7
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
19
8
|
var _address, _ephemeralPublicKey, _proof, _maxEpoch, _ephemeralKeypair, _publicKey;
|
|
20
9
|
import { Keypair, PublicKey, SIGNATURE_SCHEME_TO_FLAG } from "@mysten/sui/cryptography";
|
|
21
10
|
import { getZkLoginSignature } from "@mysten/zklogin";
|
|
22
11
|
class EnokiPublicKey extends PublicKey {
|
|
23
12
|
constructor(input) {
|
|
24
13
|
super();
|
|
25
|
-
__privateAdd(this, _address
|
|
26
|
-
__privateAdd(this, _ephemeralPublicKey
|
|
14
|
+
__privateAdd(this, _address);
|
|
15
|
+
__privateAdd(this, _ephemeralPublicKey);
|
|
27
16
|
__privateSet(this, _address, input.address);
|
|
28
17
|
__privateSet(this, _ephemeralPublicKey, input.ephemeralPublicKey);
|
|
29
18
|
}
|
|
@@ -45,10 +34,10 @@ _ephemeralPublicKey = new WeakMap();
|
|
|
45
34
|
class EnokiKeypair extends Keypair {
|
|
46
35
|
constructor(input) {
|
|
47
36
|
super();
|
|
48
|
-
__privateAdd(this, _proof
|
|
49
|
-
__privateAdd(this, _maxEpoch
|
|
50
|
-
__privateAdd(this, _ephemeralKeypair
|
|
51
|
-
__privateAdd(this, _publicKey
|
|
37
|
+
__privateAdd(this, _proof);
|
|
38
|
+
__privateAdd(this, _maxEpoch);
|
|
39
|
+
__privateAdd(this, _ephemeralKeypair);
|
|
40
|
+
__privateAdd(this, _publicKey);
|
|
52
41
|
__privateSet(this, _proof, input.proof);
|
|
53
42
|
__privateSet(this, _maxEpoch, input.maxEpoch);
|
|
54
43
|
__privateSet(this, _ephemeralKeypair, input.ephemeralKeypair);
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/EnokiKeypair.ts"],
|
|
4
4
|
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SignatureWithBytes } from '@mysten/sui/cryptography';\nimport { Keypair, PublicKey, SIGNATURE_SCHEME_TO_FLAG } from '@mysten/sui/cryptography';\nimport type { Ed25519Keypair, Ed25519PublicKey } from '@mysten/sui/keypairs/ed25519';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui/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\tasync sign(data: Uint8Array) {\n\t\treturn this.#ephemeralKeypair.sign(data);\n\t}\n\n\tasync signPersonalMessage(bytes: Uint8Array): Promise<SignatureWithBytes> {\n\t\tconst { bytes: signedBytes, signature: userSignature } =\n\t\t\tawait this.#ephemeralKeypair.signPersonalMessage(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\tasync signTransaction(bytes: Uint8Array): Promise<SignatureWithBytes> {\n\t\tconst { bytes: signedBytes, signature: userSignature } =\n\t\t\tawait this.#ephemeralKeypair.signTransaction(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\tgetSecretKey(): never {\n\t\tthrow new Error('EnokiKeypair does not support getting the secret key');\n\t}\n}\n"],
|
|
5
|
-
"mappings": "
|
|
5
|
+
"mappings": ";;;;;;;AAAA;AAIA,SAAS,SAAS,WAAW,gCAAgC;AAG7D,SAAS,2BAA2B;AAE7B,MAAM,uBAAuB,UAAU;AAAA,EAI7C,YAAY,OAAkE;AAC7E,UAAM;AAJP;AACA;AAIC,uBAAK,UAAW,MAAM;AACtB,uBAAK,qBAAsB,MAAM;AAAA,EAClC;AAAA,EAEA,OAAe;AACd,WAAO,yBAAyB,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,QAAQ;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,MAAM,KAAK,MAAkB;AAC5B,WAAO,mBAAK,mBAAkB,KAAK,IAAI;AAAA,EACxC;AAAA,EAEA,MAAM,oBAAoB,OAAgD;AACzE,UAAM,EAAE,OAAO,aAAa,WAAW,cAAc,IACpD,MAAM,mBAAK,mBAAkB,oBAAoB,KAAK;AAEvD,UAAM,cAAc,oBAAoB;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,MAAM,gBAAgB,OAAgD;AACrE,UAAM,EAAE,OAAO,aAAa,WAAW,cAAc,IACpD,MAAM,mBAAK,mBAAkB,gBAAgB,KAAK;AAEnD,UAAM,cAAc,oBAAoB;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;AAAA,EAEA,eAAsB;AACrB,UAAM,IAAI,MAAM,sDAAsD;AAAA,EACvE;AACD;AAxEC;AACA;AACA;AACA;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/react.js
CHANGED
package/dist/esm/react.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/react.tsx"],
|
|
4
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, useEffect, 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\nexport function useZkLoginSession() {\n\tconst flow = useEnokiFlow();\n\treturn useStore(flow.$zkLoginSession).value;\n}\n\nexport function useAuthCallback() {\n\tconst flow = useEnokiFlow();\n\tconst [state, setState] = useState<string | null>(null);\n\tconst [handled, setHandled] = useState(false);\n\tconst [hash, setHash] = useState<string | null>(null);\n\n\tuseEffect(() => {\n\t\tconst listener = () => setHash(window.location.hash.slice(1).trim());\n\t\tlistener();\n\n\t\twindow.addEventListener('hashchange', listener);\n\t\treturn () => window.removeEventListener('hashchange', listener);\n\t}, []);\n\n\tuseEffect(() => {\n\t\tif (!hash) return;\n\n\t\t(async () => {\n\t\t\ttry {\n\t\t\t\tsetState(await flow.handleAuthCallback(hash));\n\n\t\t\t\twindow.location.hash = '';\n\t\t\t} finally {\n\t\t\t\tsetHandled(true);\n\t\t\t}\n\t\t})();\n\t}, [hash, flow]);\n\n\treturn { handled, state };\n}\n"],
|
|
5
|
-
"mappings": "AAkBQ;AAfR,SAAS,gBAAgB;AAEzB,SAAS,eAAe,YAAY,WAAW,gBAAgB;AAG/D,SAAS,iBAAiB;AAE1B,MAAM,mBAAmB,cAAgC,IAAI;AAMtD,SAAS,kBAAkB,EAAE,UAAU,GAAG,OAAO,GAA2B;AAClF,QAAM,CAAC,SAAS,IAAI,SAAS,MAAM,IAAI,UAAU,MAAM,CAAC;AACxD,SAAO,oBAAC,iBAAiB,UAAjB,EAA0B,OAAO,WAAY,UAAS;AAC/D;AAEO,SAAS,eAAe;AAC9B,QAAM,UAAU,WAAW,gBAAgB;AAC3C,MAAI,CAAC,SAAS;AACb,UAAM,IAAI,MAAM,qCAAqC;AAAA,EACtD;AACA,S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