@zing-protocol/zing-sdk 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/package.json +37 -0
- package/src/_generated/index.js +6 -0
- package/src/_generated/utils/index.ts +181 -0
- package/src/_generated/zing_framework/coin_utils.ts +41 -0
- package/src/_generated/zing_framework/deps/sui/object.ts +16 -0
- package/src/_generated/zing_framework/derived_object_bag.ts +400 -0
- package/src/_generated/zing_framework/derived_table.ts +366 -0
- package/src/_generated/zing_governance/admin.ts +47 -0
- package/src/_generated/zing_governance/deps/sui/object.ts +16 -0
- package/src/_generated/zing_governance/treasury.ts +134 -0
- package/src/_generated/zing_identity/config.ts +191 -0
- package/src/_generated/zing_identity/deps/std/type_name.ts +25 -0
- package/src/_generated/zing_identity/deps/sui/object.ts +16 -0
- package/src/_generated/zing_identity/deps/sui/object_table.ts +26 -0
- package/src/_generated/zing_identity/deps/sui/table.ts +38 -0
- package/src/_generated/zing_identity/deps/sui/vec_set.ts +28 -0
- package/src/_generated/zing_identity/ecdsa.ts +166 -0
- package/src/_generated/zing_identity/identity.ts +661 -0
- package/src/_generated/zing_identity/reclaim.ts +803 -0
- package/src/_generated/zing_studio/app.ts +855 -0
- package/src/_generated/zing_studio/article.ts +598 -0
- package/src/_generated/zing_studio/config.ts +475 -0
- package/src/_generated/zing_studio/deps/std/type_name.ts +25 -0
- package/src/_generated/zing_studio/deps/sui/bag.ts +43 -0
- package/src/_generated/zing_studio/deps/sui/balance.ts +20 -0
- package/src/_generated/zing_studio/deps/sui/coin.ts +21 -0
- package/src/_generated/zing_studio/deps/sui/dynamic_field.ts +272 -0
- package/src/_generated/zing_studio/deps/sui/object.ts +16 -0
- package/src/_generated/zing_studio/deps/sui/table.ts +38 -0
- package/src/_generated/zing_studio/deps/sui/vec_map.ts +39 -0
- package/src/_generated/zing_studio/deps/sui/vec_set.ts +28 -0
- package/src/_generated/zing_studio/deps/walrus/blob.ts +22 -0
- package/src/_generated/zing_studio/deps/walrus/events.ts +124 -0
- package/src/_generated/zing_studio/deps/walrus/metadata.ts +137 -0
- package/src/_generated/zing_studio/deps/walrus/storage_resource.ts +17 -0
- package/src/_generated/zing_studio/deps/zing_framework/derived_object_bag.ts +17 -0
- package/src/_generated/zing_studio/deps/zing_framework/derived_table.ts +17 -0
- package/src/_generated/zing_studio/donation.ts +18 -0
- package/src/_generated/zing_studio/enclave.ts +466 -0
- package/src/_generated/zing_studio/nitro_attestation.ts +308 -0
- package/src/_generated/zing_studio/referral.ts +447 -0
- package/src/_generated/zing_studio/storage.ts +664 -0
- package/src/_generated/zing_studio/studio.ts +682 -0
- package/src/bcs.ts +139 -0
- package/src/client/index.ts +557 -0
- package/src/client/types.ts +28 -0
- package/src/components/index.ts +3 -0
- package/src/components/wallet-provider.tsx +53 -0
- package/src/components/walrus-provider.tsx +212 -0
- package/src/components/zing-provider.tsx +54 -0
- package/src/config/common.ts +82 -0
- package/src/config/index.ts +45 -0
- package/src/config/mainnet.ts +109 -0
- package/src/config/testnet.ts +111 -0
- package/src/config/types.ts +69 -0
- package/src/const.ts +32 -0
- package/src/getters.ts +523 -0
- package/src/hooks/index.ts +14 -0
- package/src/hooks/useGetStudio.ts +22 -0
- package/src/hooks/useGetTierPlan.ts +115 -0
- package/src/hooks/useReclaim.ts +165 -0
- package/src/hooks/useSignAndExecuteTransaction.ts +57 -0
- package/src/hooks/useSignPersonalMessage.ts +28 -0
- package/src/hooks/useZingInfiniteQueries.ts +92 -0
- package/src/hooks/useZingMutation.ts +66 -0
- package/src/hooks/useZingQueries.ts +107 -0
- package/src/hooks/useZingQuery.ts +88 -0
- package/src/index.ts +15 -0
- package/src/lib/utils/colorful-logger.ts +27 -0
- package/src/lib/utils/const.ts +25 -0
- package/src/lib/utils/helpers.ts +78 -0
- package/src/lib/utils/index.ts +5 -0
- package/src/lib/utils/init-app-with-shadow.ts +42 -0
- package/src/lib/utils/quilt.ts +317 -0
- package/src/lib/utils/reclaim.ts +148 -0
- package/src/lib/utils/types.ts +14 -0
- package/src/mutations/index.ts +3 -0
- package/src/mutations/seal.ts +78 -0
- package/src/mutations/signer.ts +241 -0
- package/src/mutations/walrus.ts +862 -0
- package/src/stores/index.ts +3 -0
- package/src/stores/walletStore.ts +360 -0
- package/src/stores/walrusStore.ts +118 -0
- package/src/stores/zkloginStore.ts +53 -0
- package/src/types.ts +120 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { zkloginStorage } from "./zkloginStore.js";
|
|
2
|
+
import { createZkLoginProof } from "../mutations/signer.js";
|
|
3
|
+
import { decodeSuiPrivateKey } from "@mysten/sui/cryptography";
|
|
4
|
+
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
|
|
5
|
+
import { fromBase64, toBase64 } from "@mysten/sui/utils";
|
|
6
|
+
import {
|
|
7
|
+
generateNonce,
|
|
8
|
+
decodeJwt,
|
|
9
|
+
getExtendedEphemeralPublicKey,
|
|
10
|
+
generateRandomness,
|
|
11
|
+
} from "@mysten/sui/zklogin";
|
|
12
|
+
import { createStore } from "zustand";
|
|
13
|
+
import type { AuthProvider } from "../config/common.js";
|
|
14
|
+
import type { PartialZkLoginSignature, ZKLoginSignerState } from "../types.js";
|
|
15
|
+
import type { SuiClient } from "@mysten/sui/client";
|
|
16
|
+
import type { StoreApi, StateCreator } from "zustand";
|
|
17
|
+
|
|
18
|
+
export interface WalletAccount {
|
|
19
|
+
jwt: ReturnType<typeof decodeJwt> & {
|
|
20
|
+
email: string;
|
|
21
|
+
email_verified: boolean;
|
|
22
|
+
family_name?: string;
|
|
23
|
+
given_name?: string;
|
|
24
|
+
name?: string;
|
|
25
|
+
picture?: string;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface WalletState {
|
|
30
|
+
suiClient: SuiClient;
|
|
31
|
+
isConnected: boolean;
|
|
32
|
+
isLoading: boolean;
|
|
33
|
+
account: WalletAccount | null;
|
|
34
|
+
error: string | null;
|
|
35
|
+
network: string;
|
|
36
|
+
// zk/login state (kept in memory; secrets persisted in zkloginStorage only)
|
|
37
|
+
jwt: string | null;
|
|
38
|
+
decodedJwt: ReturnType<typeof decodeJwt> | null;
|
|
39
|
+
authProvider: AuthProvider | null;
|
|
40
|
+
ephemeralKeyPair: Ed25519Keypair | null;
|
|
41
|
+
suiAddress: string | null;
|
|
42
|
+
zkProof: PartialZkLoginSignature | null;
|
|
43
|
+
maxEpoch?: number | null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type WalletActions = {
|
|
47
|
+
// main flows
|
|
48
|
+
login: (provider: AuthProvider) => Promise<void>;
|
|
49
|
+
disconnect: () => Promise<void>;
|
|
50
|
+
getSigner: () => ZKLoginSignerState | null;
|
|
51
|
+
initFromStorage: () => Promise<void>;
|
|
52
|
+
|
|
53
|
+
// helpers
|
|
54
|
+
setError: (err: string | null) => void;
|
|
55
|
+
setLoading: (v: boolean) => void;
|
|
56
|
+
setNetwork: (n: string) => void;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export type WalletStore = WalletState & WalletActions;
|
|
60
|
+
|
|
61
|
+
export type WalletStoreConfig = {
|
|
62
|
+
suiClient: SuiClient;
|
|
63
|
+
oAuthClientId: string;
|
|
64
|
+
network?: string; // default 'testnet'
|
|
65
|
+
autoInit?: boolean; // call initFromStorage automatically on creation
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export function createWalletStore(
|
|
69
|
+
config: WalletStoreConfig,
|
|
70
|
+
): StoreApi<WalletStore> {
|
|
71
|
+
const {
|
|
72
|
+
suiClient,
|
|
73
|
+
oAuthClientId,
|
|
74
|
+
network = "testnet",
|
|
75
|
+
autoInit = true,
|
|
76
|
+
} = config;
|
|
77
|
+
|
|
78
|
+
const baseCreator: StateCreator<WalletStore> = (set, get) => ({
|
|
79
|
+
// state
|
|
80
|
+
suiClient,
|
|
81
|
+
isConnected: false,
|
|
82
|
+
isLoading: false,
|
|
83
|
+
account: null,
|
|
84
|
+
error: null,
|
|
85
|
+
network,
|
|
86
|
+
jwt: null,
|
|
87
|
+
decodedJwt: null,
|
|
88
|
+
authProvider: null,
|
|
89
|
+
ephemeralKeyPair: null,
|
|
90
|
+
suiAddress: null,
|
|
91
|
+
zkProof: null,
|
|
92
|
+
maxEpoch: null,
|
|
93
|
+
|
|
94
|
+
// actions
|
|
95
|
+
setError(err) {
|
|
96
|
+
set({ error: err });
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
setLoading(v) {
|
|
100
|
+
set({ isLoading: v });
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
setNetwork(n) {
|
|
104
|
+
set({ network: n });
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
async login(provider) {
|
|
108
|
+
try {
|
|
109
|
+
set({ isLoading: true, error: null, authProvider: provider });
|
|
110
|
+
|
|
111
|
+
// 1?? Generate ephemeral keypair and maxEpoch
|
|
112
|
+
const ephemeralKeyPair = new Ed25519Keypair();
|
|
113
|
+
const { epoch } = await suiClient.getLatestSuiSystemState();
|
|
114
|
+
const maxEpoch = Number(epoch) + 10;
|
|
115
|
+
const randomness = generateRandomness();
|
|
116
|
+
const nonce = generateNonce(
|
|
117
|
+
ephemeralKeyPair.getPublicKey(),
|
|
118
|
+
maxEpoch,
|
|
119
|
+
randomness,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const redirectUri = `${window.location.origin}/oauth-callback`;
|
|
123
|
+
const params = new URLSearchParams({
|
|
124
|
+
client_id: oAuthClientId,
|
|
125
|
+
redirect_uri: redirectUri,
|
|
126
|
+
response_type: "id_token",
|
|
127
|
+
scope: "openid profile email",
|
|
128
|
+
nonce,
|
|
129
|
+
});
|
|
130
|
+
const loginURL = `https://accounts.google.com/o/oauth2/v2/auth?${params}`;
|
|
131
|
+
|
|
132
|
+
const idToken = await new Promise<string>((resolve, reject) => {
|
|
133
|
+
const width = 500;
|
|
134
|
+
const height = 600;
|
|
135
|
+
const left = window.screenX + (window.innerWidth - width) / 2;
|
|
136
|
+
const top = window.screenY + (window.innerHeight - height) / 2;
|
|
137
|
+
|
|
138
|
+
const popup = window.open(
|
|
139
|
+
loginURL,
|
|
140
|
+
"oauth2-login",
|
|
141
|
+
`width=${width},height=${height},top=${top},left=${left}`,
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
if (!popup) return reject(new Error("Failed to open popup"));
|
|
145
|
+
|
|
146
|
+
let resolved = false;
|
|
147
|
+
|
|
148
|
+
// Listen for postMessage from the callback page
|
|
149
|
+
const messageHandler = (event: MessageEvent) => {
|
|
150
|
+
// Security: verify origin
|
|
151
|
+
if (event.origin !== window.location.origin) return;
|
|
152
|
+
if (event.data?.type !== "oauth-callback") return;
|
|
153
|
+
if (resolved) return;
|
|
154
|
+
|
|
155
|
+
resolved = true;
|
|
156
|
+
window.removeEventListener("message", messageHandler);
|
|
157
|
+
clearInterval(checkClosed);
|
|
158
|
+
|
|
159
|
+
if (event.data.error) {
|
|
160
|
+
return reject(new Error(event.data.error));
|
|
161
|
+
}
|
|
162
|
+
if (event.data.idToken) {
|
|
163
|
+
resolve(event.data.idToken);
|
|
164
|
+
} else {
|
|
165
|
+
reject(new Error("No ID token received"));
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
window.addEventListener("message", messageHandler);
|
|
170
|
+
|
|
171
|
+
// Check if popup was closed without completing auth
|
|
172
|
+
const checkClosed = setInterval(() => {
|
|
173
|
+
if (!popup || popup.closed) {
|
|
174
|
+
if (!resolved) {
|
|
175
|
+
resolved = true;
|
|
176
|
+
clearInterval(checkClosed);
|
|
177
|
+
window.removeEventListener("message", messageHandler);
|
|
178
|
+
reject(
|
|
179
|
+
new Error(
|
|
180
|
+
"Login popup closed before completing authentication",
|
|
181
|
+
),
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}, 1000);
|
|
186
|
+
|
|
187
|
+
// Timeout after 5 minutes
|
|
188
|
+
setTimeout(
|
|
189
|
+
() => {
|
|
190
|
+
if (!resolved) {
|
|
191
|
+
resolved = true;
|
|
192
|
+
clearInterval(checkClosed);
|
|
193
|
+
window.removeEventListener("message", messageHandler);
|
|
194
|
+
if (popup && !popup.closed) popup.close();
|
|
195
|
+
reject(new Error("Login timeout"));
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
5 * 60 * 1000,
|
|
199
|
+
);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// 4?? Decode JWT
|
|
203
|
+
const decoded = decodeJwt(idToken);
|
|
204
|
+
|
|
205
|
+
// 5?? Persist ephemeral secret / salt / jwt / maxEpoch
|
|
206
|
+
zkloginStorage
|
|
207
|
+
.getState()
|
|
208
|
+
.setEphemeralSecretKey(
|
|
209
|
+
toBase64(
|
|
210
|
+
decodeSuiPrivateKey(ephemeralKeyPair.getSecretKey()).secretKey,
|
|
211
|
+
),
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
zkloginStorage.getState().setJWT(idToken);
|
|
215
|
+
zkloginStorage.getState().setMaxEpoch(maxEpoch);
|
|
216
|
+
|
|
217
|
+
const extendedEphemeralPublicKey = getExtendedEphemeralPublicKey(
|
|
218
|
+
ephemeralKeyPair.getPublicKey(),
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const zkProofResponse = await createZkLoginProof({
|
|
222
|
+
jwt: idToken,
|
|
223
|
+
maxEpoch: String(maxEpoch),
|
|
224
|
+
extendedEphemeralPublicKey,
|
|
225
|
+
jwtRandomness: randomness,
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
if (!zkProofResponse?.result)
|
|
229
|
+
throw new Error(`Failed to get zkProof: ${zkProofResponse.error}`);
|
|
230
|
+
|
|
231
|
+
zkloginStorage
|
|
232
|
+
.getState()
|
|
233
|
+
.setZKProof(JSON.stringify(zkProofResponse.result.zkProof));
|
|
234
|
+
|
|
235
|
+
const suiAddress = zkProofResponse.result.suiAddress;
|
|
236
|
+
zkloginStorage.getState().setSuiAddress(suiAddress);
|
|
237
|
+
const account: WalletAccount = { jwt: decoded as any };
|
|
238
|
+
|
|
239
|
+
set({
|
|
240
|
+
jwt: idToken,
|
|
241
|
+
decodedJwt: decoded,
|
|
242
|
+
ephemeralKeyPair,
|
|
243
|
+
suiAddress,
|
|
244
|
+
zkProof: zkProofResponse.result.zkProof,
|
|
245
|
+
maxEpoch,
|
|
246
|
+
account,
|
|
247
|
+
isConnected: true,
|
|
248
|
+
isLoading: false,
|
|
249
|
+
error: null,
|
|
250
|
+
});
|
|
251
|
+
} catch (err: any) {
|
|
252
|
+
set({ error: err?.message ?? String(err), isLoading: false });
|
|
253
|
+
throw err;
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
|
|
257
|
+
async disconnect() {
|
|
258
|
+
try {
|
|
259
|
+
// reset zkloginStorage
|
|
260
|
+
zkloginStorage.getState().reset();
|
|
261
|
+
} catch {
|
|
262
|
+
// ignore storage errors
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
set({
|
|
266
|
+
jwt: null,
|
|
267
|
+
decodedJwt: null,
|
|
268
|
+
ephemeralKeyPair: null,
|
|
269
|
+
suiAddress: null,
|
|
270
|
+
zkProof: null,
|
|
271
|
+
maxEpoch: null,
|
|
272
|
+
account: null,
|
|
273
|
+
isConnected: false,
|
|
274
|
+
isLoading: false,
|
|
275
|
+
authProvider: null,
|
|
276
|
+
error: null,
|
|
277
|
+
});
|
|
278
|
+
},
|
|
279
|
+
|
|
280
|
+
getSigner() {
|
|
281
|
+
const {
|
|
282
|
+
jwt,
|
|
283
|
+
maxEpoch,
|
|
284
|
+
ephemeralKeyPair,
|
|
285
|
+
zkProof,
|
|
286
|
+
suiAddress,
|
|
287
|
+
decodedJwt,
|
|
288
|
+
} = get();
|
|
289
|
+
if (!jwt || !maxEpoch || !ephemeralKeyPair || !zkProof) return null;
|
|
290
|
+
return {
|
|
291
|
+
ephemeralKeyPair,
|
|
292
|
+
suiAddress,
|
|
293
|
+
decodedJwt,
|
|
294
|
+
zkProof,
|
|
295
|
+
maxEpoch,
|
|
296
|
+
} as ZKLoginSignerState;
|
|
297
|
+
},
|
|
298
|
+
|
|
299
|
+
async initFromStorage() {
|
|
300
|
+
try {
|
|
301
|
+
set({ isLoading: true });
|
|
302
|
+
const { epoch: currentEpoch } =
|
|
303
|
+
await suiClient.getLatestSuiSystemState();
|
|
304
|
+
|
|
305
|
+
const stored = {
|
|
306
|
+
jwt: zkloginStorage.getState().jwt,
|
|
307
|
+
maxEpoch: zkloginStorage.getState().maxEpoch,
|
|
308
|
+
ephemeralSecretKey: zkloginStorage.getState().ephemeralSecretKey,
|
|
309
|
+
zkProof: zkloginStorage.getState().zkProof,
|
|
310
|
+
suiAddress: zkloginStorage.getState().suiAddress,
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
if (
|
|
314
|
+
!stored.jwt ||
|
|
315
|
+
!stored.maxEpoch ||
|
|
316
|
+
!stored.ephemeralSecretKey ||
|
|
317
|
+
!stored.zkProof ||
|
|
318
|
+
!stored.suiAddress
|
|
319
|
+
) {
|
|
320
|
+
throw new Error("Missing required params");
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (Number(currentEpoch) >= Number(stored.maxEpoch))
|
|
324
|
+
throw new Error("Exceed maxEpoch");
|
|
325
|
+
|
|
326
|
+
const ephemeralKeyPair = Ed25519Keypair.fromSecretKey(
|
|
327
|
+
fromBase64(stored.ephemeralSecretKey),
|
|
328
|
+
);
|
|
329
|
+
const decoded = decodeJwt(stored.jwt);
|
|
330
|
+
const zkProofParsed = JSON.parse(stored.zkProof);
|
|
331
|
+
|
|
332
|
+
set({
|
|
333
|
+
jwt: stored.jwt,
|
|
334
|
+
decodedJwt: decoded,
|
|
335
|
+
ephemeralKeyPair,
|
|
336
|
+
suiAddress: stored.suiAddress,
|
|
337
|
+
zkProof: zkProofParsed,
|
|
338
|
+
maxEpoch: Number(stored.maxEpoch),
|
|
339
|
+
account: { jwt: decoded as any },
|
|
340
|
+
isConnected: true,
|
|
341
|
+
error: null,
|
|
342
|
+
});
|
|
343
|
+
} catch (err: any) {
|
|
344
|
+
set({ error: err?.message ?? String(err) });
|
|
345
|
+
}
|
|
346
|
+
set({ isLoading: false });
|
|
347
|
+
},
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
const store = createStore<WalletStore>()(baseCreator);
|
|
351
|
+
|
|
352
|
+
if (autoInit) {
|
|
353
|
+
store
|
|
354
|
+
.getState()
|
|
355
|
+
.initFromStorage()
|
|
356
|
+
.catch(() => {});
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return store;
|
|
360
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { createStore } from "zustand";
|
|
2
|
+
import { persist, createJSONStorage } from "zustand/middleware";
|
|
3
|
+
import type { WalrusStakingState, WalrusSystemState } from "../config/types.js";
|
|
4
|
+
import type { SealClient } from "@mysten/seal";
|
|
5
|
+
import type { WalrusClient } from "@mysten/walrus";
|
|
6
|
+
import type { StateCreator, StoreApi } from "zustand";
|
|
7
|
+
import type { StateStorage } from "zustand/middleware";
|
|
8
|
+
|
|
9
|
+
export type WalrusStore = {
|
|
10
|
+
walrusClient: WalrusClient;
|
|
11
|
+
sealClient: SealClient;
|
|
12
|
+
walrusStakingState?: WalrusStakingState;
|
|
13
|
+
walrusSystemState?: WalrusSystemState;
|
|
14
|
+
isLoading: boolean;
|
|
15
|
+
error?: string | null;
|
|
16
|
+
|
|
17
|
+
// actions
|
|
18
|
+
init: () => Promise<void>;
|
|
19
|
+
refreshWalrusState: () => Promise<void>;
|
|
20
|
+
setError: (err?: string | null) => void;
|
|
21
|
+
setStakingState: (state?: WalrusStakingState) => void;
|
|
22
|
+
setSystemState: (state?: WalrusSystemState) => void;
|
|
23
|
+
|
|
24
|
+
// NEW SETTERS
|
|
25
|
+
setWalrusClient: (c: WalrusClient) => void;
|
|
26
|
+
setSealClient: (c: SealClient) => void;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type WalrusStoreConfig = {
|
|
30
|
+
walrusClient: WalrusClient;
|
|
31
|
+
sealClient: SealClient;
|
|
32
|
+
storage?: StateStorage;
|
|
33
|
+
storageKey?: string;
|
|
34
|
+
autoInit?: boolean;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export function createWalrusStore(
|
|
38
|
+
config: WalrusStoreConfig,
|
|
39
|
+
): StoreApi<WalrusStore> {
|
|
40
|
+
const {
|
|
41
|
+
walrusClient,
|
|
42
|
+
sealClient,
|
|
43
|
+
storage,
|
|
44
|
+
storageKey = "walrus-store",
|
|
45
|
+
autoInit = true,
|
|
46
|
+
} = config;
|
|
47
|
+
|
|
48
|
+
const baseStore: StateCreator<WalrusStore, [], [], WalrusStore> = (
|
|
49
|
+
set,
|
|
50
|
+
get,
|
|
51
|
+
) => ({
|
|
52
|
+
walrusClient,
|
|
53
|
+
sealClient,
|
|
54
|
+
walrusStakingState: undefined,
|
|
55
|
+
walrusSystemState: undefined,
|
|
56
|
+
isLoading: false,
|
|
57
|
+
error: null,
|
|
58
|
+
|
|
59
|
+
setWalrusClient(c) {
|
|
60
|
+
set(() => ({ walrusClient: c }));
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
setSealClient(c) {
|
|
64
|
+
set(() => ({ sealClient: c }));
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
setError(err) {
|
|
68
|
+
set(() => ({ error: err ?? null }));
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
setStakingState(state) {
|
|
72
|
+
set(() => ({ walrusStakingState: state }));
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
setSystemState(state) {
|
|
76
|
+
set(() => ({ walrusSystemState: state }));
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
async refreshWalrusState() {
|
|
80
|
+
const client = get().walrusClient;
|
|
81
|
+
if (!client) {
|
|
82
|
+
set(() => ({ error: "WalrusClient not initialized" }));
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
set(() => ({ isLoading: true, error: null }));
|
|
88
|
+
const walrusStakingState = await client.stakingState();
|
|
89
|
+
const walrusSystemState = await client.systemState();
|
|
90
|
+
set(() => ({ walrusStakingState, walrusSystemState }));
|
|
91
|
+
} catch (e: any) {
|
|
92
|
+
set(() => ({ error: e instanceof Error ? e.message : String(e) }));
|
|
93
|
+
} finally {
|
|
94
|
+
set(() => ({ isLoading: false }));
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
async init() {
|
|
99
|
+
await get().refreshWalrusState();
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
if (storage) {
|
|
104
|
+
return createStore<WalrusStore>()(
|
|
105
|
+
persist(baseStore, {
|
|
106
|
+
name: storageKey,
|
|
107
|
+
storage: createJSONStorage(() => storage),
|
|
108
|
+
partialize: () => ({}),
|
|
109
|
+
}),
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const store = createStore<WalrusStore>()(baseStore);
|
|
114
|
+
|
|
115
|
+
if (autoInit) store.getState().init();
|
|
116
|
+
|
|
117
|
+
return store;
|
|
118
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { create } from "zustand";
|
|
2
|
+
import { persist, createJSONStorage } from "zustand/middleware";
|
|
3
|
+
|
|
4
|
+
export interface ZKLoginStorage {
|
|
5
|
+
jwt: string | null;
|
|
6
|
+
suiAddress: string | null;
|
|
7
|
+
maxEpoch: number | null;
|
|
8
|
+
expiresAt: number | null;
|
|
9
|
+
ephemeralSecretKey: string | null;
|
|
10
|
+
zkProof: string | null;
|
|
11
|
+
|
|
12
|
+
setSuiAddress: (suiAddress: string) => void;
|
|
13
|
+
setMaxEpoch: (maxEpoch: number) => void;
|
|
14
|
+
setExpiresAt: (expiresAt: number) => void;
|
|
15
|
+
setJWT: (jwt: string) => void;
|
|
16
|
+
setEphemeralSecretKey: (key: string) => void;
|
|
17
|
+
setZKProof: (proof: string) => void;
|
|
18
|
+
reset: () => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const zkloginStorage = create<ZKLoginStorage>()(
|
|
22
|
+
persist(
|
|
23
|
+
(set) => ({
|
|
24
|
+
jwt: null,
|
|
25
|
+
suiAddress: null,
|
|
26
|
+
maxEpoch: null,
|
|
27
|
+
expiresAt: null,
|
|
28
|
+
ephemeralSecretKey: null,
|
|
29
|
+
zkProof: null,
|
|
30
|
+
|
|
31
|
+
setSuiAddress: (suiAddress: string) => set({ suiAddress }),
|
|
32
|
+
setMaxEpoch: (maxEpoch: number) => set({ maxEpoch }),
|
|
33
|
+
setExpiresAt: (expiresAt: number) => set({ expiresAt }),
|
|
34
|
+
setJWT: (jwt: string) => set({ jwt }),
|
|
35
|
+
setEphemeralSecretKey: (ephemeralSecretKey: string) =>
|
|
36
|
+
set({ ephemeralSecretKey }),
|
|
37
|
+
setZKProof: (zkProof: string) => set({ zkProof }),
|
|
38
|
+
reset: () =>
|
|
39
|
+
set({
|
|
40
|
+
jwt: null,
|
|
41
|
+
suiAddress: null,
|
|
42
|
+
maxEpoch: null,
|
|
43
|
+
expiresAt: null,
|
|
44
|
+
ephemeralSecretKey: null,
|
|
45
|
+
zkProof: null,
|
|
46
|
+
}),
|
|
47
|
+
}),
|
|
48
|
+
{
|
|
49
|
+
name: "zklogin-storage", // localStorage key
|
|
50
|
+
storage: createJSONStorage(() => localStorage),
|
|
51
|
+
},
|
|
52
|
+
),
|
|
53
|
+
);
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { PlatformType } from "./config/common.js";
|
|
2
|
+
import type { SuiObjectRef, TransactionEffects } from "@mysten/sui/client";
|
|
3
|
+
import type { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
|
|
4
|
+
import type { decodeJwt, getZkLoginSignature } from "@mysten/sui/zklogin";
|
|
5
|
+
|
|
6
|
+
export type SuiNetwork = "mainnet" | "testnet";
|
|
7
|
+
// Auth
|
|
8
|
+
export type PartialZkLoginSignature = Omit<
|
|
9
|
+
Parameters<typeof getZkLoginSignature>["0"]["inputs"],
|
|
10
|
+
"addressSeed"
|
|
11
|
+
>;
|
|
12
|
+
|
|
13
|
+
export interface ZKLoginSignerState {
|
|
14
|
+
ephemeralKeyPair: Ed25519Keypair | null;
|
|
15
|
+
suiAddress: string | null;
|
|
16
|
+
decodedJwt: ReturnType<typeof decodeJwt> | null;
|
|
17
|
+
zkProof: PartialZkLoginSignature | null;
|
|
18
|
+
maxEpoch: number | null;
|
|
19
|
+
}
|
|
20
|
+
// Sui
|
|
21
|
+
export interface SponsoredGasParams {
|
|
22
|
+
gaslessTransactionBytes: string;
|
|
23
|
+
senderAddress: string;
|
|
24
|
+
gasBudget?: string;
|
|
25
|
+
gasPrice?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface GenerateSaltRequest {
|
|
29
|
+
sub: string;
|
|
30
|
+
iss: string;
|
|
31
|
+
aud: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface GenerateSaltResponse {
|
|
35
|
+
salt: string;
|
|
36
|
+
saltInt: string;
|
|
37
|
+
deviceId: string | null;
|
|
38
|
+
timestamp: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type SponsoredGasResponse =
|
|
42
|
+
| {
|
|
43
|
+
jsonrpc: string;
|
|
44
|
+
result: {
|
|
45
|
+
txBytes: string;
|
|
46
|
+
txDigest: string;
|
|
47
|
+
signature: string;
|
|
48
|
+
expireAtTime: number;
|
|
49
|
+
};
|
|
50
|
+
id: number;
|
|
51
|
+
error?: never; // explicitly forbid error here
|
|
52
|
+
}
|
|
53
|
+
| {
|
|
54
|
+
jsonrpc: string;
|
|
55
|
+
error: {
|
|
56
|
+
code: number;
|
|
57
|
+
message: string;
|
|
58
|
+
data?: {
|
|
59
|
+
details?: string;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
id: number;
|
|
63
|
+
result?: never; // explicitly forbid result here
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export interface ReserveGasRequest {
|
|
67
|
+
gas_budget: number;
|
|
68
|
+
reserve_duration_secs: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface ReserveGasResponse {
|
|
72
|
+
result: ReserveGasResult | null;
|
|
73
|
+
error: string | null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface ReserveGasResult {
|
|
77
|
+
sponsor_address: string;
|
|
78
|
+
reservation_id: string;
|
|
79
|
+
gas_coins: SuiObjectRef[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface ExecuteSponsoredTxRequest {
|
|
83
|
+
reservation_id: string;
|
|
84
|
+
tx_bytes: string;
|
|
85
|
+
user_sig: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface ExecuteSponsoredTxResponse {
|
|
89
|
+
effects: TransactionEffects | null;
|
|
90
|
+
error: string | null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type PlatformIdentifiers = Record<PlatformType, string>;
|
|
94
|
+
|
|
95
|
+
export interface ZkLoginProofParams {
|
|
96
|
+
jwt: string;
|
|
97
|
+
maxEpoch: string;
|
|
98
|
+
extendedEphemeralPublicKey: string;
|
|
99
|
+
jwtRandomness: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface ZkLoginProofResponse {
|
|
103
|
+
id: number;
|
|
104
|
+
result?: {
|
|
105
|
+
zkProof: PartialZkLoginSignature;
|
|
106
|
+
suiAddress: string; // Add this line
|
|
107
|
+
};
|
|
108
|
+
error?: {
|
|
109
|
+
code: number;
|
|
110
|
+
message: string;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface TierPlan {
|
|
115
|
+
name: string;
|
|
116
|
+
price: number;
|
|
117
|
+
duration: number;
|
|
118
|
+
storage: number;
|
|
119
|
+
features: string[];
|
|
120
|
+
}
|