@mysten/enoki 0.5.2 → 0.6.0
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 +24 -0
- package/dist/cjs/EnokiClient/type.d.ts +1 -1
- package/dist/cjs/EnokiClient/type.js.map +1 -1
- package/dist/cjs/EnokiFlow.d.ts +19 -27
- package/dist/cjs/EnokiFlow.js +41 -52
- package/dist/cjs/EnokiFlow.js.map +2 -2
- package/dist/cjs/EnokiKeypair.d.ts +1 -2
- package/dist/cjs/EnokiKeypair.js.map +2 -2
- package/dist/cjs/encryption.d.ts +1 -1
- package/dist/cjs/encryption.js.map +1 -1
- package/dist/cjs/index.d.ts +5 -1
- package/dist/cjs/index.js +6 -1
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/react.d.ts +8 -2
- package/dist/cjs/react.js.map +2 -2
- package/dist/cjs/utils.d.ts +2 -0
- package/dist/cjs/utils.js +27 -0
- package/dist/cjs/utils.js.map +7 -0
- package/dist/cjs/wallet/index.d.ts +34 -0
- package/dist/cjs/wallet/index.js +330 -0
- package/dist/cjs/wallet/index.js.map +7 -0
- package/dist/cjs/wallet/providers.d.ts +7 -0
- package/dist/cjs/wallet/providers.js +41 -0
- package/dist/cjs/wallet/providers.js.map +7 -0
- package/dist/cjs/wallet/state.d.ts +43 -0
- package/dist/cjs/wallet/state.js +263 -0
- package/dist/cjs/wallet/state.js.map +7 -0
- package/dist/cjs/wallet/types.d.ts +40 -0
- package/dist/cjs/wallet/types.js +17 -0
- package/dist/cjs/wallet/types.js.map +7 -0
- package/dist/esm/EnokiClient/type.d.ts +1 -1
- package/dist/esm/EnokiFlow.d.ts +19 -27
- package/dist/esm/EnokiFlow.js +41 -52
- package/dist/esm/EnokiFlow.js.map +2 -2
- package/dist/esm/EnokiKeypair.d.ts +1 -2
- package/dist/esm/EnokiKeypair.js.map +2 -2
- package/dist/esm/encryption.d.ts +1 -1
- package/dist/esm/encryption.js.map +1 -1
- package/dist/esm/index.d.ts +5 -1
- package/dist/esm/index.js +6 -1
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/react.d.ts +8 -2
- package/dist/esm/react.js.map +2 -2
- package/dist/esm/utils.d.ts +2 -0
- package/dist/esm/utils.js +7 -0
- package/dist/esm/utils.js.map +7 -0
- package/dist/esm/wallet/index.d.ts +34 -0
- package/dist/esm/wallet/index.js +300 -0
- package/dist/esm/wallet/index.js.map +7 -0
- package/dist/esm/wallet/providers.d.ts +7 -0
- package/dist/esm/wallet/providers.js +21 -0
- package/dist/esm/wallet/providers.js.map +7 -0
- package/dist/esm/wallet/state.d.ts +43 -0
- package/dist/esm/wallet/state.js +243 -0
- package/dist/esm/wallet/state.js.map +7 -0
- package/dist/esm/wallet/types.d.ts +40 -0
- package/dist/esm/wallet/types.js +1 -0
- package/dist/esm/wallet/types.js.map +7 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -3
|
@@ -0,0 +1,263 @@
|
|
|
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 __typeError = (msg) => {
|
|
7
|
+
throw TypeError(msg);
|
|
8
|
+
};
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
23
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
24
|
+
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);
|
|
25
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
26
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
27
|
+
var state_exports = {};
|
|
28
|
+
__export(state_exports, {
|
|
29
|
+
INTERNAL_ONLY_EnokiFlow: () => INTERNAL_ONLY_EnokiFlow
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(state_exports);
|
|
32
|
+
var import_webcrypto = require("@mysten/signers/webcrypto");
|
|
33
|
+
var import_zklogin = require("@mysten/sui/zklogin");
|
|
34
|
+
var import_idb_keyval = require("idb-keyval");
|
|
35
|
+
var import_nanostores = require("nanostores");
|
|
36
|
+
var import_encryption = require("../encryption.js");
|
|
37
|
+
var import_EnokiClient = require("../EnokiClient/index.js");
|
|
38
|
+
var import_EnokiKeypair = require("../EnokiKeypair.js");
|
|
39
|
+
var import_stores = require("../stores.js");
|
|
40
|
+
var _storageKeys, _enokiClient, _network, _encryption, _encryptionKey, _store, _idbStore, _INTERNAL_ONLY_EnokiFlow_instances, setSession_fn;
|
|
41
|
+
const createStorageKeys = (apiKey, network) => ({
|
|
42
|
+
STATE: `@enoki/flow/state/${apiKey}/${network}`,
|
|
43
|
+
SESSION: `@enoki/flow/session/${apiKey}/${network}`
|
|
44
|
+
});
|
|
45
|
+
class INTERNAL_ONLY_EnokiFlow {
|
|
46
|
+
constructor(config) {
|
|
47
|
+
__privateAdd(this, _INTERNAL_ONLY_EnokiFlow_instances);
|
|
48
|
+
__privateAdd(this, _storageKeys);
|
|
49
|
+
__privateAdd(this, _enokiClient);
|
|
50
|
+
__privateAdd(this, _network);
|
|
51
|
+
__privateAdd(this, _encryption);
|
|
52
|
+
__privateAdd(this, _encryptionKey);
|
|
53
|
+
__privateAdd(this, _store);
|
|
54
|
+
__privateAdd(this, _idbStore);
|
|
55
|
+
__privateSet(this, _enokiClient, new import_EnokiClient.EnokiClient({
|
|
56
|
+
apiKey: config.apiKey,
|
|
57
|
+
apiUrl: config.apiUrl
|
|
58
|
+
}));
|
|
59
|
+
__privateSet(this, _network, config.network);
|
|
60
|
+
__privateSet(this, _encryptionKey, config.apiKey);
|
|
61
|
+
__privateSet(this, _encryption, (0, import_encryption.createDefaultEncryption)());
|
|
62
|
+
__privateSet(this, _store, (0, import_stores.createSessionStorage)());
|
|
63
|
+
__privateSet(this, _storageKeys, createStorageKeys(config.apiKey, __privateGet(this, _network)));
|
|
64
|
+
__privateSet(this, _idbStore, (0, import_idb_keyval.createStore)("enoki", `${config.apiKey}_${__privateGet(this, _network)}`));
|
|
65
|
+
let storedState = null;
|
|
66
|
+
try {
|
|
67
|
+
const rawStoredValue = __privateGet(this, _store).get(__privateGet(this, _storageKeys).STATE);
|
|
68
|
+
if (rawStoredValue) {
|
|
69
|
+
storedState = JSON.parse(rawStoredValue);
|
|
70
|
+
}
|
|
71
|
+
} catch {
|
|
72
|
+
}
|
|
73
|
+
this.$zkLoginState = (0, import_nanostores.atom)(storedState || {});
|
|
74
|
+
this.$zkLoginSession = (0, import_nanostores.atom)({ initialized: false, value: null });
|
|
75
|
+
(0, import_nanostores.onMount)(this.$zkLoginSession, () => {
|
|
76
|
+
this.getSession();
|
|
77
|
+
});
|
|
78
|
+
(0, import_nanostores.onSet)(this.$zkLoginState, ({ newValue }) => {
|
|
79
|
+
__privateGet(this, _store).set(__privateGet(this, _storageKeys).STATE, JSON.stringify(newValue));
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
get network() {
|
|
83
|
+
return __privateGet(this, _network);
|
|
84
|
+
}
|
|
85
|
+
async createAuthorizationURL(input) {
|
|
86
|
+
const ephemeralKeyPair = await import_webcrypto.WebCryptoSigner.generate();
|
|
87
|
+
const { nonce, randomness, maxEpoch, estimatedExpiration } = await __privateGet(this, _enokiClient).createZkLoginNonce({
|
|
88
|
+
network: __privateGet(this, _network),
|
|
89
|
+
ephemeralPublicKey: ephemeralKeyPair.getPublicKey()
|
|
90
|
+
});
|
|
91
|
+
const params = new URLSearchParams({
|
|
92
|
+
...input.extraParams,
|
|
93
|
+
nonce,
|
|
94
|
+
client_id: input.clientId,
|
|
95
|
+
redirect_uri: input.redirectUrl,
|
|
96
|
+
response_type: "id_token",
|
|
97
|
+
// TODO: Eventually fetch the scopes for this client ID from the Enoki service:
|
|
98
|
+
scope: [
|
|
99
|
+
"openid",
|
|
100
|
+
// Merge the requested scopes in with the required openid scopes:
|
|
101
|
+
...input.extraParams && "scope" in input.extraParams ? input.extraParams.scope : []
|
|
102
|
+
].filter(Boolean).join(" ")
|
|
103
|
+
});
|
|
104
|
+
let oauthUrl;
|
|
105
|
+
switch (input.provider) {
|
|
106
|
+
case "google": {
|
|
107
|
+
oauthUrl = `https://accounts.google.com/o/oauth2/v2/auth?${params}`;
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case "facebook": {
|
|
111
|
+
oauthUrl = `https://www.facebook.com/v17.0/dialog/oauth?${params}`;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case "twitch": {
|
|
115
|
+
params.set("force_verify", "true");
|
|
116
|
+
oauthUrl = `https://id.twitch.tv/oauth2/authorize?${params}`;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
default:
|
|
120
|
+
throw new Error(`Invalid provider: ${input.provider}`);
|
|
121
|
+
}
|
|
122
|
+
this.$zkLoginState.set({ provider: input.provider });
|
|
123
|
+
await (0, import_idb_keyval.set)("ephemeralKeyPair", ephemeralKeyPair, __privateGet(this, _idbStore));
|
|
124
|
+
await __privateMethod(this, _INTERNAL_ONLY_EnokiFlow_instances, setSession_fn).call(this, {
|
|
125
|
+
expiresAt: estimatedExpiration,
|
|
126
|
+
maxEpoch,
|
|
127
|
+
randomness
|
|
128
|
+
});
|
|
129
|
+
return oauthUrl;
|
|
130
|
+
}
|
|
131
|
+
async handleAuthCallback(hash = window.location.hash) {
|
|
132
|
+
const params = new URLSearchParams(hash.startsWith("#") ? hash.slice(1) : hash);
|
|
133
|
+
const zkp = await this.getSession();
|
|
134
|
+
if (!zkp || !zkp.maxEpoch || !zkp.randomness) {
|
|
135
|
+
throw new Error(
|
|
136
|
+
"Start of sign-in flow could not be found. Ensure you have started the sign-in flow before calling this."
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
const jwt = params.get("id_token");
|
|
140
|
+
if (!jwt) {
|
|
141
|
+
throw new Error("Missing ID Token");
|
|
142
|
+
}
|
|
143
|
+
(0, import_zklogin.decodeJwt)(jwt);
|
|
144
|
+
const { address, salt, publicKey } = await __privateGet(this, _enokiClient).getZkLogin({ jwt });
|
|
145
|
+
this.$zkLoginState.set({
|
|
146
|
+
...this.$zkLoginState.get(),
|
|
147
|
+
salt,
|
|
148
|
+
address,
|
|
149
|
+
publicKey
|
|
150
|
+
});
|
|
151
|
+
await __privateMethod(this, _INTERNAL_ONLY_EnokiFlow_instances, setSession_fn).call(this, {
|
|
152
|
+
...zkp,
|
|
153
|
+
jwt
|
|
154
|
+
});
|
|
155
|
+
return params.get("state");
|
|
156
|
+
}
|
|
157
|
+
async getSession() {
|
|
158
|
+
if (this.$zkLoginSession.get().initialized) {
|
|
159
|
+
return this.$zkLoginSession.get().value;
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
const storedValue = __privateGet(this, _store).get(__privateGet(this, _storageKeys).SESSION);
|
|
163
|
+
if (!storedValue) return null;
|
|
164
|
+
const state = JSON.parse(
|
|
165
|
+
await __privateGet(this, _encryption).decrypt(__privateGet(this, _encryptionKey), storedValue)
|
|
166
|
+
);
|
|
167
|
+
if (state?.expiresAt && Date.now() > state.expiresAt) {
|
|
168
|
+
await this.logout();
|
|
169
|
+
} else {
|
|
170
|
+
this.$zkLoginSession.set({ initialized: true, value: state });
|
|
171
|
+
}
|
|
172
|
+
} catch {
|
|
173
|
+
this.$zkLoginSession.set({ initialized: true, value: null });
|
|
174
|
+
}
|
|
175
|
+
return this.$zkLoginSession.get().value;
|
|
176
|
+
}
|
|
177
|
+
async logout() {
|
|
178
|
+
this.$zkLoginState.set({});
|
|
179
|
+
__privateGet(this, _store).delete(__privateGet(this, _storageKeys).STATE);
|
|
180
|
+
await (0, import_idb_keyval.clear)(__privateGet(this, _idbStore));
|
|
181
|
+
await __privateMethod(this, _INTERNAL_ONLY_EnokiFlow_instances, setSession_fn).call(this, null);
|
|
182
|
+
}
|
|
183
|
+
// TODO: Should this return the proof if it already exists?
|
|
184
|
+
async getProof() {
|
|
185
|
+
const zkp = await this.getSession();
|
|
186
|
+
const { salt } = this.$zkLoginState.get();
|
|
187
|
+
if (zkp?.proof) {
|
|
188
|
+
if (zkp.expiresAt && Date.now() > zkp.expiresAt) {
|
|
189
|
+
throw new Error("Stored proof is expired.");
|
|
190
|
+
}
|
|
191
|
+
return zkp.proof;
|
|
192
|
+
}
|
|
193
|
+
if (!salt || !zkp || !zkp.jwt) {
|
|
194
|
+
throw new Error("Missing required parameters for proof generation");
|
|
195
|
+
}
|
|
196
|
+
const storedNativeSigner = await (0, import_idb_keyval.get)(
|
|
197
|
+
"ephemeralKeyPair",
|
|
198
|
+
__privateGet(this, _idbStore)
|
|
199
|
+
);
|
|
200
|
+
if (!storedNativeSigner) {
|
|
201
|
+
throw new Error("Native signer not found in store.");
|
|
202
|
+
}
|
|
203
|
+
const ephemeralKeyPair = import_webcrypto.WebCryptoSigner.import(storedNativeSigner);
|
|
204
|
+
const proof = await __privateGet(this, _enokiClient).createZkLoginZkp({
|
|
205
|
+
network: __privateGet(this, _network),
|
|
206
|
+
jwt: zkp.jwt,
|
|
207
|
+
maxEpoch: zkp.maxEpoch,
|
|
208
|
+
randomness: zkp.randomness,
|
|
209
|
+
ephemeralPublicKey: ephemeralKeyPair.getPublicKey()
|
|
210
|
+
});
|
|
211
|
+
await __privateMethod(this, _INTERNAL_ONLY_EnokiFlow_instances, setSession_fn).call(this, {
|
|
212
|
+
...zkp,
|
|
213
|
+
proof
|
|
214
|
+
});
|
|
215
|
+
return proof;
|
|
216
|
+
}
|
|
217
|
+
async getKeypair() {
|
|
218
|
+
await this.getProof();
|
|
219
|
+
const zkp = await this.getSession();
|
|
220
|
+
const { address } = this.$zkLoginState.get();
|
|
221
|
+
if (!address || !zkp || !zkp.proof) {
|
|
222
|
+
throw new Error("Missing required data for keypair generation.");
|
|
223
|
+
}
|
|
224
|
+
if (Date.now() > zkp.expiresAt) {
|
|
225
|
+
throw new Error("Stored proof is expired.");
|
|
226
|
+
}
|
|
227
|
+
const storedNativeSigner = await (0, import_idb_keyval.get)(
|
|
228
|
+
"ephemeralKeyPair",
|
|
229
|
+
__privateGet(this, _idbStore)
|
|
230
|
+
);
|
|
231
|
+
if (!storedNativeSigner) {
|
|
232
|
+
throw new Error("Native signer not found in store.");
|
|
233
|
+
}
|
|
234
|
+
const ephemeralKeypair = import_webcrypto.WebCryptoSigner.import(storedNativeSigner);
|
|
235
|
+
return new import_EnokiKeypair.EnokiKeypair({
|
|
236
|
+
address,
|
|
237
|
+
ephemeralKeypair,
|
|
238
|
+
maxEpoch: zkp.maxEpoch,
|
|
239
|
+
proof: zkp.proof
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
_storageKeys = new WeakMap();
|
|
244
|
+
_enokiClient = new WeakMap();
|
|
245
|
+
_network = new WeakMap();
|
|
246
|
+
_encryption = new WeakMap();
|
|
247
|
+
_encryptionKey = new WeakMap();
|
|
248
|
+
_store = new WeakMap();
|
|
249
|
+
_idbStore = new WeakMap();
|
|
250
|
+
_INTERNAL_ONLY_EnokiFlow_instances = new WeakSet();
|
|
251
|
+
setSession_fn = async function(newValue) {
|
|
252
|
+
if (newValue) {
|
|
253
|
+
const storedValue = await __privateGet(this, _encryption).encrypt(
|
|
254
|
+
__privateGet(this, _encryptionKey),
|
|
255
|
+
JSON.stringify(newValue)
|
|
256
|
+
);
|
|
257
|
+
__privateGet(this, _store).set(__privateGet(this, _storageKeys).SESSION, storedValue);
|
|
258
|
+
} else {
|
|
259
|
+
__privateGet(this, _store).delete(__privateGet(this, _storageKeys).SESSION);
|
|
260
|
+
}
|
|
261
|
+
this.$zkLoginSession.set({ initialized: true, value: newValue });
|
|
262
|
+
};
|
|
263
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/wallet/state.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { ExportedWebCryptoKeypair } from '@mysten/signers/webcrypto';\nimport { WebCryptoSigner } from '@mysten/signers/webcrypto';\nimport { decodeJwt } from '@mysten/sui/zklogin';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';\nimport type { UseStore } from 'idb-keyval';\nimport { clear, createStore, get, set } from 'idb-keyval';\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 type { AuthProvider, EnokiNetwork } from '../EnokiClient/type.js';\nimport { EnokiKeypair } from '../EnokiKeypair.js';\nimport type { SyncStore } from '../stores.js';\nimport { createSessionStorage } from '../stores.js';\n\ntype EnokiFlowConfig = EnokiClientConfig & { network: EnokiNetwork };\n\n// State that is not bound to a session, and is encrypted.\ninterface ZkLoginState {\n\tprovider?: AuthProvider;\n\taddress?: string;\n\tsalt?: string;\n\tpublicKey?: string;\n}\n\n// State that session-bound, and is encrypted in storage.\ninterface ZkLoginSession {\n\tmaxEpoch: number;\n\trandomness: string;\n\texpiresAt: number;\n\n\tjwt?: string;\n\tproof?: ZkLoginSignatureInputs;\n}\n\nconst createStorageKeys = (apiKey: string, network: EnokiNetwork) => ({\n\tSTATE: `@enoki/flow/state/${apiKey}/${network}`,\n\tSESSION: `@enoki/flow/session/${apiKey}/${network}`,\n});\n\nexport class INTERNAL_ONLY_EnokiFlow {\n\t#storageKeys: { STATE: string; SESSION: string };\n\t#enokiClient: EnokiClient;\n\t#network: EnokiNetwork;\n\t#encryption: Encryption;\n\t#encryptionKey: string;\n\t#store: SyncStore;\n\t#idbStore: UseStore;\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.#network = config.network;\n\t\tthis.#encryptionKey = config.apiKey;\n\t\tthis.#encryption = createDefaultEncryption();\n\t\tthis.#store = createSessionStorage();\n\t\tthis.#storageKeys = createStorageKeys(config.apiKey, this.#network);\n\t\tthis.#idbStore = createStore('enoki', `${config.apiKey}_${this.#network}`);\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 network() {\n\t\treturn this.#network;\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 = await WebCryptoSigner.generate();\n\t\tconst { nonce, randomness, maxEpoch, estimatedExpiration } =\n\t\t\tawait this.#enokiClient.createZkLoginNonce({\n\t\t\t\tnetwork: this.#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\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\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\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\n\t\tawait set('ephemeralKeyPair', ephemeralKeyPair, this.#idbStore);\n\t\tawait this.#setSession({\n\t\t\texpiresAt: estimatedExpiration,\n\t\t\tmaxEpoch,\n\t\t\trandomness,\n\t\t});\n\n\t\treturn oauthUrl;\n\t}\n\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.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\tdecodeJwt(jwt);\n\n\t\tconst { address, salt, publicKey } = 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\tpublicKey,\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 clear(this.#idbStore);\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 storedNativeSigner = await get<ExportedWebCryptoKeypair>(\n\t\t\t'ephemeralKeyPair',\n\t\t\tthis.#idbStore,\n\t\t);\n\t\tif (!storedNativeSigner) {\n\t\t\tthrow new Error('Native signer not found in store.');\n\t\t}\n\n\t\tconst ephemeralKeyPair = WebCryptoSigner.import(storedNativeSigner);\n\n\t\tconst proof = await this.#enokiClient.createZkLoginZkp({\n\t\t\tnetwork: this.#network,\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\t// Get the proof, so that we ensure it exists in state:\n\t\tawait this.getProof();\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\tconst storedNativeSigner = await get<ExportedWebCryptoKeypair>(\n\t\t\t'ephemeralKeyPair',\n\t\t\tthis.#idbStore,\n\t\t);\n\n\t\tif (!storedNativeSigner) {\n\t\t\tthrow new Error('Native signer not found in store.');\n\t\t}\n\n\t\tconst ephemeralKeypair = WebCryptoSigner.import(storedNativeSigner);\n\n\t\treturn new EnokiKeypair({\n\t\t\taddress,\n\t\t\tephemeralKeypair,\n\t\t\tmaxEpoch: zkp.maxEpoch,\n\t\t\tproof: zkp.proof,\n\t\t});\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,uBAAgC;AAChC,qBAA0B;AAG1B,wBAA6C;AAE7C,wBAAqC;AAGrC,wBAAwC;AAExC,yBAA4B;AAE5B,0BAA6B;AAE7B,oBAAqC;AAnBrC;AAyCA,MAAM,oBAAoB,CAAC,QAAgB,aAA2B;AAAA,EACrE,OAAO,qBAAqB,MAAM,IAAI,OAAO;AAAA,EAC7C,SAAS,uBAAuB,MAAM,IAA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mCAAmC;AAAA,IACpD;AAEA,UAAM,mBAAmB,iCAAgB,OAAO,kBAAkB;AAElE,WAAO,IAAI,iCAAa;AAAA,MACvB;AAAA,MACA;AAAA,MACA,UAAU,IAAI;AAAA,MACd,OAAO,IAAI;AAAA,IACZ,CAAC;AAAA,EACF;AACD;AAlRC;AACA;AACA;AACA;AACA;AACA;AACA;AAPM;AAoJA,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
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { SuiClient } from '@mysten/sui/client';
|
|
2
|
+
import type { StandardEventsListeners } from '@mysten/wallet-standard';
|
|
3
|
+
import type { EnokiClientConfig } from '../EnokiClient/index.js';
|
|
4
|
+
import type { AuthProvider, EnokiNetwork } from '../EnokiClient/type.js';
|
|
5
|
+
export type WalletEventsMap = {
|
|
6
|
+
[E in keyof StandardEventsListeners]: Parameters<StandardEventsListeners[E]>[0];
|
|
7
|
+
};
|
|
8
|
+
export interface RegisterEnokiWalletsOptions extends EnokiClientConfig {
|
|
9
|
+
/**
|
|
10
|
+
* Configuration for each OAuth provider.
|
|
11
|
+
*/
|
|
12
|
+
providers: Partial<Record<AuthProvider, {
|
|
13
|
+
/**
|
|
14
|
+
* The OAuth client ID.
|
|
15
|
+
*/
|
|
16
|
+
clientId: string;
|
|
17
|
+
/**
|
|
18
|
+
* The URL to redirect to after authorization.
|
|
19
|
+
*/
|
|
20
|
+
redirectUrl?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Extra parameters to include in the authorization URL.
|
|
23
|
+
*/
|
|
24
|
+
extraParams?: Record<string, string>;
|
|
25
|
+
}>>;
|
|
26
|
+
/**
|
|
27
|
+
* The SuiClient instance to use when building and executing transactions.
|
|
28
|
+
*/
|
|
29
|
+
client: SuiClient;
|
|
30
|
+
/**
|
|
31
|
+
* The network to use when building and executing transactions.
|
|
32
|
+
* @default 'mainnet'
|
|
33
|
+
*/
|
|
34
|
+
network?: EnokiNetwork;
|
|
35
|
+
/**
|
|
36
|
+
* The window features to use when opening the authorization popup.
|
|
37
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Window/open#windowfeatures
|
|
38
|
+
*/
|
|
39
|
+
windowFeatures?: string | (() => string);
|
|
40
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var types_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(types_exports);
|
|
17
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/wallet/types.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SuiClient } from '@mysten/sui/client';\nimport type { StandardEventsListeners } from '@mysten/wallet-standard';\n\nimport type { EnokiClientConfig } from '../EnokiClient/index.js';\nimport type { AuthProvider, EnokiNetwork } from '../EnokiClient/type.js';\n\nexport type WalletEventsMap = {\n\t[E in keyof StandardEventsListeners]: Parameters<StandardEventsListeners[E]>[0];\n};\n\nexport interface RegisterEnokiWalletsOptions extends EnokiClientConfig {\n\t/**\n\t * Configuration for each OAuth provider.\n\t */\n\tproviders: Partial<\n\t\tRecord<\n\t\t\tAuthProvider,\n\t\t\t{\n\t\t\t\t/**\n\t\t\t\t * The OAuth client ID.\n\t\t\t\t */\n\t\t\t\tclientId: string;\n\n\t\t\t\t/**\n\t\t\t\t * The URL to redirect to after authorization.\n\t\t\t\t */\n\t\t\t\tredirectUrl?: string;\n\n\t\t\t\t/**\n\t\t\t\t * Extra parameters to include in the authorization URL.\n\t\t\t\t */\n\t\t\t\textraParams?: Record<string, string>;\n\t\t\t}\n\t\t>\n\t>;\n\n\t/**\n\t * The SuiClient instance to use when building and executing transactions.\n\t */\n\tclient: SuiClient;\n\n\t/**\n\t * The network to use when building and executing transactions.\n\t * @default 'mainnet'\n\t */\n\tnetwork?: EnokiNetwork;\n\n\t/**\n\t * The window features to use when opening the authorization popup.\n\t * https://developer.mozilla.org/en-US/docs/Web/API/Window/open#windowfeatures\n\t */\n\twindowFeatures?: string | (() => string);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PublicKey } from '@mysten/sui/cryptography';
|
|
2
2
|
import type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';
|
|
3
|
-
|
|
3
|
+
export type AuthProvider = 'google' | 'facebook' | 'twitch';
|
|
4
4
|
export type EnokiNetwork = 'mainnet' | 'testnet' | 'devnet';
|
|
5
5
|
export type EnokiDomainNetwork = 'mainnet' | 'testnet';
|
|
6
6
|
export type EnokiSubanameStatus = 'PENDING' | 'ACTIVE';
|
package/dist/esm/EnokiFlow.d.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import type { SuiClient } from '@mysten/sui/client';
|
|
2
|
-
import type { Transaction } from '@mysten/sui/transactions';
|
|
3
1
|
import type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';
|
|
4
2
|
import type { WritableAtom } from 'nanostores';
|
|
5
3
|
import type { Encryption } from './encryption.js';
|
|
6
4
|
import type { EnokiClientConfig } from './EnokiClient/index.js';
|
|
7
5
|
import { EnokiClient } from './EnokiClient/index.js';
|
|
6
|
+
import type { AuthProvider, EnokiNetwork } from './EnokiClient/type.js';
|
|
8
7
|
import { EnokiKeypair } from './EnokiKeypair.js';
|
|
9
8
|
import type { SyncStore } from './stores.js';
|
|
10
|
-
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated Use `RegisterEnokiWalletsOptions` instead
|
|
11
|
+
*/
|
|
12
|
+
export type EnokiFlowConfig = EnokiClientConfig & ({
|
|
13
|
+
experimental_nativeCryptoSigner?: unknown;
|
|
11
14
|
/**
|
|
12
15
|
* The storage interface to persist Enoki data locally.
|
|
13
16
|
* If not provided, it will use a sessionStorage-backed store.
|
|
@@ -18,7 +21,14 @@ export interface EnokiFlowConfig extends EnokiClientConfig {
|
|
|
18
21
|
* If not provided, it will use a default encryption interface.
|
|
19
22
|
*/
|
|
20
23
|
encryption?: Encryption;
|
|
21
|
-
}
|
|
24
|
+
} | {
|
|
25
|
+
store?: never;
|
|
26
|
+
encryption?: never;
|
|
27
|
+
/**
|
|
28
|
+
* Enables the new native crypto signer for the EnokiFlow, which is more secure.
|
|
29
|
+
*/
|
|
30
|
+
experimental_nativeCryptoSigner: true;
|
|
31
|
+
});
|
|
22
32
|
export interface ZkLoginState {
|
|
23
33
|
provider?: AuthProvider;
|
|
24
34
|
address?: string;
|
|
@@ -33,7 +43,9 @@ export interface ZkLoginSession {
|
|
|
33
43
|
jwt?: string;
|
|
34
44
|
proof?: ZkLoginSignatureInputs;
|
|
35
45
|
}
|
|
36
|
-
|
|
46
|
+
/**
|
|
47
|
+
* @deprecated Use `registerEnokiWallets` instead
|
|
48
|
+
*/
|
|
37
49
|
export declare class EnokiFlow {
|
|
38
50
|
#private;
|
|
39
51
|
$zkLoginSession: WritableAtom<{
|
|
@@ -54,29 +66,9 @@ export declare class EnokiFlow {
|
|
|
54
66
|
getSession(): Promise<ZkLoginSession | null>;
|
|
55
67
|
logout(): Promise<void>;
|
|
56
68
|
getProof({ network }?: {
|
|
57
|
-
network?:
|
|
69
|
+
network?: EnokiNetwork;
|
|
58
70
|
}): Promise<import("./EnokiClient/type.js").CreateZkLoginZkpApiResponse>;
|
|
59
71
|
getKeypair({ network }?: {
|
|
60
|
-
network?:
|
|
72
|
+
network?: EnokiNetwork;
|
|
61
73
|
}): Promise<EnokiKeypair>;
|
|
62
|
-
sponsorTransaction({ network, transaction, client, }: {
|
|
63
|
-
network?: 'mainnet' | 'testnet';
|
|
64
|
-
transaction: Transaction;
|
|
65
|
-
client: SuiClient;
|
|
66
|
-
}): Promise<import("./EnokiClient/type.js").CreateSponsoredTransactionApiResponse>;
|
|
67
|
-
executeTransaction({ network, bytes, digest, client, }: {
|
|
68
|
-
network?: 'mainnet' | 'testnet';
|
|
69
|
-
bytes: string;
|
|
70
|
-
digest: string;
|
|
71
|
-
client: SuiClient;
|
|
72
|
-
}): Promise<{
|
|
73
|
-
digest: string;
|
|
74
|
-
}>;
|
|
75
|
-
sponsorAndExecuteTransaction({ network, transaction, client, }: {
|
|
76
|
-
network?: 'mainnet' | 'testnet';
|
|
77
|
-
transaction: Transaction;
|
|
78
|
-
client: SuiClient;
|
|
79
|
-
}): Promise<{
|
|
80
|
-
digest: string;
|
|
81
|
-
}>;
|
|
82
74
|
}
|
package/dist/esm/EnokiFlow.js
CHANGED
|
@@ -6,11 +6,13 @@ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read fr
|
|
|
6
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
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
8
|
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
9
|
-
var _storageKeys, _enokiClient, _encryption, _encryptionKey, _store, _EnokiFlow_instances, setSession_fn;
|
|
9
|
+
var _storageKeys, _enokiClient, _encryption, _encryptionKey, _store, _useNativeCryptoSigner, _idbStore, _EnokiFlow_instances, setSession_fn;
|
|
10
|
+
import { WebCryptoSigner } from "@mysten/signers/webcrypto";
|
|
10
11
|
import { decodeSuiPrivateKey } from "@mysten/sui/cryptography";
|
|
11
12
|
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
|
|
12
13
|
import { fromBase64, toBase64 } from "@mysten/sui/utils";
|
|
13
14
|
import { decodeJwt } from "@mysten/sui/zklogin";
|
|
15
|
+
import { clear, createStore, get, set } from "idb-keyval";
|
|
14
16
|
import { atom, onMount, onSet } from "nanostores";
|
|
15
17
|
import { createDefaultEncryption } from "./encryption.js";
|
|
16
18
|
import { EnokiClient } from "./EnokiClient/index.js";
|
|
@@ -28,11 +30,19 @@ class EnokiFlow {
|
|
|
28
30
|
__privateAdd(this, _encryption);
|
|
29
31
|
__privateAdd(this, _encryptionKey);
|
|
30
32
|
__privateAdd(this, _store);
|
|
33
|
+
__privateAdd(this, _useNativeCryptoSigner);
|
|
34
|
+
__privateAdd(this, _idbStore);
|
|
31
35
|
__privateSet(this, _enokiClient, new EnokiClient({
|
|
32
36
|
apiKey: config.apiKey,
|
|
33
37
|
apiUrl: config.apiUrl
|
|
34
38
|
}));
|
|
35
39
|
__privateSet(this, _encryptionKey, config.apiKey);
|
|
40
|
+
if (config.experimental_nativeCryptoSigner) {
|
|
41
|
+
__privateSet(this, _useNativeCryptoSigner, true);
|
|
42
|
+
__privateSet(this, _idbStore, createStore("enoki", config.apiKey));
|
|
43
|
+
} else {
|
|
44
|
+
__privateSet(this, _useNativeCryptoSigner, false);
|
|
45
|
+
}
|
|
36
46
|
__privateSet(this, _encryption, config.encryption ?? createDefaultEncryption());
|
|
37
47
|
__privateSet(this, _store, config.store ?? createSessionStorage());
|
|
38
48
|
__privateSet(this, _storageKeys, createStorageKeys(config.apiKey));
|
|
@@ -57,7 +67,7 @@ class EnokiFlow {
|
|
|
57
67
|
return __privateGet(this, _enokiClient);
|
|
58
68
|
}
|
|
59
69
|
async createAuthorizationURL(input) {
|
|
60
|
-
const ephemeralKeyPair = new Ed25519Keypair();
|
|
70
|
+
const ephemeralKeyPair = __privateGet(this, _useNativeCryptoSigner) ? await WebCryptoSigner.generate() : new Ed25519Keypair();
|
|
61
71
|
const { nonce, randomness, maxEpoch, estimatedExpiration } = await __privateGet(this, _enokiClient).createZkLoginNonce({
|
|
62
72
|
network: input.network,
|
|
63
73
|
ephemeralPublicKey: ephemeralKeyPair.getPublicKey()
|
|
@@ -94,11 +104,16 @@ class EnokiFlow {
|
|
|
94
104
|
throw new Error(`Invalid provider: ${input.provider}`);
|
|
95
105
|
}
|
|
96
106
|
this.$zkLoginState.set({ provider: input.provider });
|
|
107
|
+
if (__privateGet(this, _useNativeCryptoSigner)) {
|
|
108
|
+
await set("ephemeralKeyPair", ephemeralKeyPair, __privateGet(this, _idbStore));
|
|
109
|
+
}
|
|
97
110
|
await __privateMethod(this, _EnokiFlow_instances, setSession_fn).call(this, {
|
|
98
111
|
expiresAt: estimatedExpiration,
|
|
99
112
|
maxEpoch,
|
|
100
113
|
randomness,
|
|
101
|
-
ephemeralKeyPair: toBase64(
|
|
114
|
+
ephemeralKeyPair: __privateGet(this, _useNativeCryptoSigner) ? "@@native" : toBase64(
|
|
115
|
+
decodeSuiPrivateKey(ephemeralKeyPair.getSecretKey()).secretKey
|
|
116
|
+
)
|
|
102
117
|
});
|
|
103
118
|
return oauthUrl;
|
|
104
119
|
}
|
|
@@ -152,6 +167,9 @@ class EnokiFlow {
|
|
|
152
167
|
async logout() {
|
|
153
168
|
this.$zkLoginState.set({});
|
|
154
169
|
__privateGet(this, _store).delete(__privateGet(this, _storageKeys).STATE);
|
|
170
|
+
if (__privateGet(this, _useNativeCryptoSigner)) {
|
|
171
|
+
await clear(__privateGet(this, _idbStore));
|
|
172
|
+
}
|
|
155
173
|
await __privateMethod(this, _EnokiFlow_instances, setSession_fn).call(this, null);
|
|
156
174
|
}
|
|
157
175
|
// TODO: Should this return the proof if it already exists?
|
|
@@ -167,7 +185,14 @@ class EnokiFlow {
|
|
|
167
185
|
if (!salt || !zkp || !zkp.jwt) {
|
|
168
186
|
throw new Error("Missing required parameters for proof generation");
|
|
169
187
|
}
|
|
170
|
-
|
|
188
|
+
let storedNativeSigner = void 0;
|
|
189
|
+
if (__privateGet(this, _useNativeCryptoSigner) && zkp.ephemeralKeyPair === "@@native") {
|
|
190
|
+
storedNativeSigner = await get("ephemeralKeyPair", __privateGet(this, _idbStore));
|
|
191
|
+
if (!storedNativeSigner) {
|
|
192
|
+
throw new Error("Native signer not found in store.");
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const ephemeralKeyPair = zkp.ephemeralKeyPair === "@@native" ? WebCryptoSigner.import(storedNativeSigner) : Ed25519Keypair.fromSecretKey(fromBase64(zkp.ephemeralKeyPair));
|
|
171
196
|
const proof = await __privateGet(this, _enokiClient).createZkLoginZkp({
|
|
172
197
|
network,
|
|
173
198
|
jwt: zkp.jwt,
|
|
@@ -191,58 +216,20 @@ class EnokiFlow {
|
|
|
191
216
|
if (Date.now() > zkp.expiresAt) {
|
|
192
217
|
throw new Error("Stored proof is expired.");
|
|
193
218
|
}
|
|
219
|
+
let storedNativeSigner = void 0;
|
|
220
|
+
if (__privateGet(this, _useNativeCryptoSigner) && zkp.ephemeralKeyPair === "@@native") {
|
|
221
|
+
storedNativeSigner = await get("ephemeralKeyPair", __privateGet(this, _idbStore));
|
|
222
|
+
if (!storedNativeSigner) {
|
|
223
|
+
throw new Error("Native signer not found in store.");
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
const ephemeralKeypair = zkp.ephemeralKeyPair === "@@native" ? WebCryptoSigner.import(storedNativeSigner) : Ed25519Keypair.fromSecretKey(fromBase64(zkp.ephemeralKeyPair));
|
|
194
227
|
return new EnokiKeypair({
|
|
195
228
|
address,
|
|
229
|
+
ephemeralKeypair,
|
|
196
230
|
maxEpoch: zkp.maxEpoch,
|
|
197
|
-
proof: zkp.proof
|
|
198
|
-
ephemeralKeypair: Ed25519Keypair.fromSecretKey(fromBase64(zkp.ephemeralKeyPair))
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
async sponsorTransaction({
|
|
202
|
-
network,
|
|
203
|
-
transaction,
|
|
204
|
-
client
|
|
205
|
-
}) {
|
|
206
|
-
const session = await this.getSession();
|
|
207
|
-
if (!session || !session.jwt) {
|
|
208
|
-
throw new Error("Missing required data for sponsorship.");
|
|
209
|
-
}
|
|
210
|
-
const transactionKindBytes = await transaction.build({
|
|
211
|
-
onlyTransactionKind: true,
|
|
212
|
-
client
|
|
213
|
-
});
|
|
214
|
-
return await __privateGet(this, _enokiClient).createSponsoredTransaction({
|
|
215
|
-
jwt: session.jwt,
|
|
216
|
-
network,
|
|
217
|
-
transactionKindBytes: toBase64(transactionKindBytes)
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
async executeTransaction({
|
|
221
|
-
network,
|
|
222
|
-
bytes,
|
|
223
|
-
digest,
|
|
224
|
-
client
|
|
225
|
-
}) {
|
|
226
|
-
const keypair = await this.getKeypair({ network });
|
|
227
|
-
const userSignature = await keypair.signTransaction(fromBase64(bytes));
|
|
228
|
-
await __privateGet(this, _enokiClient).executeSponsoredTransaction({
|
|
229
|
-
digest,
|
|
230
|
-
signature: userSignature.signature
|
|
231
|
-
});
|
|
232
|
-
await client.waitForTransaction({ digest });
|
|
233
|
-
return { digest };
|
|
234
|
-
}
|
|
235
|
-
async sponsorAndExecuteTransaction({
|
|
236
|
-
network,
|
|
237
|
-
transaction,
|
|
238
|
-
client
|
|
239
|
-
}) {
|
|
240
|
-
const { bytes, digest } = await this.sponsorTransaction({
|
|
241
|
-
network,
|
|
242
|
-
transaction,
|
|
243
|
-
client
|
|
231
|
+
proof: zkp.proof
|
|
244
232
|
});
|
|
245
|
-
return await this.executeTransaction({ network, bytes, digest, client });
|
|
246
233
|
}
|
|
247
234
|
}
|
|
248
235
|
_storageKeys = new WeakMap();
|
|
@@ -250,6 +237,8 @@ _enokiClient = new WeakMap();
|
|
|
250
237
|
_encryption = new WeakMap();
|
|
251
238
|
_encryptionKey = new WeakMap();
|
|
252
239
|
_store = new WeakMap();
|
|
240
|
+
_useNativeCryptoSigner = new WeakMap();
|
|
241
|
+
_idbStore = new WeakMap();
|
|
253
242
|
_EnokiFlow_instances = new WeakSet();
|
|
254
243
|
setSession_fn = async function(newValue) {
|
|
255
244
|
if (newValue) {
|