@zing-protocol/zing-sdk 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. package/dist/_generated/index.js +6 -0
  2. package/dist/_generated/utils/index.d.ts +12 -0
  3. package/dist/_generated/utils/index.js +135 -0
  4. package/dist/_generated/zing_framework/coin_utils.d.ts +15 -0
  5. package/dist/_generated/zing_framework/coin_utils.js +19 -0
  6. package/dist/_generated/zing_framework/deps/sui/object.d.ts +8 -0
  7. package/{src/_generated/zing_governance/deps/sui/object.ts → dist/_generated/zing_framework/deps/sui/object.js} +4 -7
  8. package/dist/_generated/zing_framework/derived_object_bag.d.ts +166 -0
  9. package/dist/_generated/zing_framework/derived_object_bag.js +221 -0
  10. package/dist/_generated/zing_framework/derived_table.d.ts +153 -0
  11. package/dist/_generated/zing_framework/derived_table.js +200 -0
  12. package/dist/_generated/zing_governance/admin.d.ts +20 -0
  13. package/dist/_generated/zing_governance/admin.js +26 -0
  14. package/dist/_generated/zing_governance/deps/sui/object.d.ts +8 -0
  15. package/{src/_generated/zing_identity/deps/sui/object.ts → dist/_generated/zing_governance/deps/sui/object.js} +4 -7
  16. package/dist/_generated/zing_governance/treasury.d.ts +56 -0
  17. package/dist/_generated/zing_governance/treasury.js +78 -0
  18. package/dist/_generated/zing_identity/config.d.ts +87 -0
  19. package/dist/_generated/zing_identity/config.js +98 -0
  20. package/{src/_generated/zing_studio/deps/std/type_name.ts → dist/_generated/zing_identity/deps/std/type_name.d.ts} +3 -11
  21. package/dist/_generated/zing_identity/deps/std/type_name.js +22 -0
  22. package/dist/_generated/zing_identity/deps/sui/object.d.ts +8 -0
  23. package/{src/_generated/zing_studio/deps/sui/object.ts → dist/_generated/zing_identity/deps/sui/object.js} +4 -7
  24. package/dist/_generated/zing_identity/deps/sui/object_table.d.ts +12 -0
  25. package/{src/_generated/zing_identity/deps/sui/object_table.ts → dist/_generated/zing_identity/deps/sui/object_table.js} +7 -10
  26. package/dist/_generated/zing_identity/deps/sui/table.d.ts +12 -0
  27. package/{src/_generated/zing_studio/deps/sui/table.ts → dist/_generated/zing_identity/deps/sui/table.js} +7 -10
  28. package/{src/_generated/zing_identity/deps/sui/vec_set.ts → dist/_generated/zing_identity/deps/sui/vec_set.d.ts} +3 -14
  29. package/dist/_generated/zing_identity/deps/sui/vec_set.js +22 -0
  30. package/dist/_generated/zing_identity/ecdsa.d.ts +81 -0
  31. package/dist/_generated/zing_identity/ecdsa.js +87 -0
  32. package/dist/_generated/zing_identity/identity.d.ts +262 -0
  33. package/dist/_generated/zing_identity/identity.js +294 -0
  34. package/dist/_generated/zing_identity/reclaim.d.ts +356 -0
  35. package/dist/_generated/zing_identity/reclaim.js +414 -0
  36. package/dist/_generated/zing_studio/app.d.ts +351 -0
  37. package/dist/_generated/zing_studio/app.js +451 -0
  38. package/dist/_generated/zing_studio/article.d.ts +331 -0
  39. package/dist/_generated/zing_studio/article.js +331 -0
  40. package/dist/_generated/zing_studio/config.d.ts +203 -0
  41. package/dist/_generated/zing_studio/config.js +233 -0
  42. package/{src/_generated/zing_identity/deps/std/type_name.ts → dist/_generated/zing_studio/deps/std/type_name.d.ts} +3 -11
  43. package/dist/_generated/zing_studio/deps/std/type_name.js +22 -0
  44. package/dist/_generated/zing_studio/deps/sui/bag.d.ts +12 -0
  45. package/{src/_generated/zing_studio/deps/sui/bag.ts → dist/_generated/zing_studio/deps/sui/bag.js} +7 -10
  46. package/dist/_generated/zing_studio/deps/sui/balance.d.ts +12 -0
  47. package/{src/_generated/zing_studio/deps/sui/balance.ts → dist/_generated/zing_studio/deps/sui/balance.js} +4 -7
  48. package/dist/_generated/zing_studio/deps/sui/coin.d.ts +12 -0
  49. package/{src/_generated/zing_studio/deps/sui/coin.ts → dist/_generated/zing_studio/deps/sui/coin.js} +5 -8
  50. package/dist/_generated/zing_studio/deps/sui/dynamic_field.d.ts +126 -0
  51. package/dist/_generated/zing_studio/deps/sui/dynamic_field.js +140 -0
  52. package/dist/_generated/zing_studio/deps/sui/object.d.ts +8 -0
  53. package/{src/_generated/zing_framework/deps/sui/object.ts → dist/_generated/zing_studio/deps/sui/object.js} +4 -7
  54. package/dist/_generated/zing_studio/deps/sui/table.d.ts +12 -0
  55. package/{src/_generated/zing_identity/deps/sui/table.ts → dist/_generated/zing_studio/deps/sui/table.js} +7 -10
  56. package/dist/_generated/zing_studio/deps/sui/vec_map.d.ts +30 -0
  57. package/{src/_generated/zing_studio/deps/sui/vec_map.ts → dist/_generated/zing_studio/deps/sui/vec_map.js} +15 -21
  58. package/{src/_generated/zing_studio/deps/sui/vec_set.ts → dist/_generated/zing_studio/deps/sui/vec_set.d.ts} +3 -14
  59. package/dist/_generated/zing_studio/deps/sui/vec_set.js +22 -0
  60. package/dist/_generated/zing_studio/deps/walrus/blob.d.ts +20 -0
  61. package/{src/_generated/zing_studio/deps/walrus/blob.ts → dist/_generated/zing_studio/deps/walrus/blob.js} +11 -12
  62. package/dist/_generated/zing_studio/deps/walrus/events.d.ts +83 -0
  63. package/dist/_generated/zing_studio/deps/walrus/events.js +122 -0
  64. package/dist/_generated/zing_studio/deps/walrus/metadata.d.ts +71 -0
  65. package/dist/_generated/zing_studio/deps/walrus/metadata.js +73 -0
  66. package/dist/_generated/zing_studio/deps/walrus/storage_resource.d.ts +12 -0
  67. package/{src/_generated/zing_studio/deps/walrus/storage_resource.ts → dist/_generated/zing_studio/deps/walrus/storage_resource.js} +7 -8
  68. package/dist/_generated/zing_studio/deps/zing_framework/derived_object_bag.d.ts +12 -0
  69. package/{src/_generated/zing_studio/deps/zing_framework/derived_object_bag.ts → dist/_generated/zing_studio/deps/zing_framework/derived_object_bag.js} +7 -8
  70. package/dist/_generated/zing_studio/deps/zing_framework/derived_table.d.ts +12 -0
  71. package/{src/_generated/zing_studio/deps/zing_framework/derived_table.ts → dist/_generated/zing_studio/deps/zing_framework/derived_table.js} +7 -8
  72. package/dist/_generated/zing_studio/donation.d.ts +24 -0
  73. package/{src/_generated/zing_studio/donation.ts → dist/_generated/zing_studio/donation.js} +7 -8
  74. package/dist/_generated/zing_studio/enclave.d.ts +187 -0
  75. package/dist/_generated/zing_studio/enclave.js +231 -0
  76. package/dist/_generated/zing_studio/nitro_attestation.d.ts +148 -0
  77. package/dist/_generated/zing_studio/nitro_attestation.js +183 -0
  78. package/dist/_generated/zing_studio/referral.d.ts +166 -0
  79. package/dist/_generated/zing_studio/referral.js +224 -0
  80. package/dist/_generated/zing_studio/storage.d.ts +327 -0
  81. package/dist/_generated/zing_studio/storage.js +365 -0
  82. package/dist/_generated/zing_studio/studio.d.ts +282 -0
  83. package/dist/_generated/zing_studio/studio.js +360 -0
  84. package/dist/bcs.d.ts +61 -0
  85. package/dist/bcs.js +109 -0
  86. package/dist/client/index.d.ts +228 -0
  87. package/dist/client/index.js +296 -0
  88. package/dist/client/types.d.ts +23 -0
  89. package/dist/client/types.js +1 -0
  90. package/dist/components/index.js +3 -0
  91. package/dist/components/wallet-provider.d.ts +7 -0
  92. package/dist/components/wallet-provider.js +33 -0
  93. package/dist/components/walrus-provider.d.ts +31 -0
  94. package/dist/components/walrus-provider.js +71 -0
  95. package/dist/components/zing-provider.d.ts +15 -0
  96. package/dist/components/zing-provider.js +22 -0
  97. package/dist/config/common.d.ts +23 -0
  98. package/dist/config/common.js +61 -0
  99. package/dist/config/index.d.ts +8 -0
  100. package/dist/config/index.js +28 -0
  101. package/dist/config/mainnet.d.ts +6 -0
  102. package/dist/config/mainnet.js +80 -0
  103. package/dist/config/testnet.d.ts +6 -0
  104. package/dist/config/testnet.js +81 -0
  105. package/dist/config/types.d.ts +49 -0
  106. package/dist/config/types.js +1 -0
  107. package/dist/const.d.ts +4 -0
  108. package/dist/const.js +25 -0
  109. package/dist/getters.d.ts +228 -0
  110. package/dist/getters.js +317 -0
  111. package/{src/hooks/index.ts → dist/hooks/index.d.ts} +2 -3
  112. package/dist/hooks/index.js +9 -0
  113. package/dist/hooks/useGetStudio.d.ts +26 -0
  114. package/dist/hooks/useGetStudio.js +17 -0
  115. package/dist/hooks/useGetTierPlan.d.ts +7 -0
  116. package/dist/hooks/useGetTierPlan.js +85 -0
  117. package/dist/hooks/useReclaim.d.ts +12 -0
  118. package/dist/hooks/useReclaim.js +87 -0
  119. package/dist/hooks/useSignAndExecuteTransaction.d.ts +6 -0
  120. package/dist/hooks/useSignAndExecuteTransaction.js +40 -0
  121. package/dist/hooks/useSignPersonalMessage.d.ts +8 -0
  122. package/dist/hooks/useSignPersonalMessage.js +21 -0
  123. package/dist/hooks/useZingInfiniteQueries.d.ts +75 -0
  124. package/dist/hooks/useZingInfiniteQueries.js +28 -0
  125. package/dist/hooks/useZingMutation.d.ts +35 -0
  126. package/dist/hooks/useZingMutation.js +37 -0
  127. package/dist/hooks/useZingQueries.d.ts +127 -0
  128. package/dist/hooks/useZingQueries.js +37 -0
  129. package/dist/hooks/useZingQuery.d.ts +147 -0
  130. package/dist/hooks/useZingQuery.js +34 -0
  131. package/{src/index.ts → dist/index.d.ts} +2 -3
  132. package/dist/index.js +9 -0
  133. package/dist/lib/utils/colorful-logger.d.ts +2 -0
  134. package/dist/lib/utils/colorful-logger.js +23 -0
  135. package/dist/lib/utils/const.d.ts +25 -0
  136. package/dist/lib/utils/const.js +25 -0
  137. package/dist/lib/utils/helpers.d.ts +10 -0
  138. package/dist/lib/utils/helpers.js +49 -0
  139. package/dist/lib/utils/index.js +4 -0
  140. package/dist/lib/utils/init-app-with-shadow.d.ts +6 -0
  141. package/dist/lib/utils/init-app-with-shadow.js +28 -0
  142. package/dist/lib/utils/quilt.d.ts +61 -0
  143. package/dist/lib/utils/quilt.js +194 -0
  144. package/dist/lib/utils/reclaim.d.ts +29 -0
  145. package/dist/lib/utils/reclaim.js +88 -0
  146. package/dist/lib/utils/types.d.ts +5 -0
  147. package/dist/lib/utils/types.js +1 -0
  148. package/dist/mutations/index.js +3 -0
  149. package/dist/mutations/seal.d.ts +12 -0
  150. package/dist/mutations/seal.js +41 -0
  151. package/dist/mutations/signer.d.ts +10 -0
  152. package/dist/mutations/signer.js +176 -0
  153. package/dist/mutations/walrus.d.ts +98 -0
  154. package/dist/mutations/walrus.js +488 -0
  155. package/dist/stores/index.js +3 -0
  156. package/dist/stores/walletStore.d.ts +48 -0
  157. package/dist/stores/walletStore.js +232 -0
  158. package/dist/stores/walrusStore.d.ts +28 -0
  159. package/dist/stores/walrusStore.js +61 -0
  160. package/dist/stores/zkloginStore.d.ts +28 -0
  161. package/dist/stores/zkloginStore.js +27 -0
  162. package/dist/types.d.ts +99 -0
  163. package/dist/types.js +1 -0
  164. package/package.json +18 -10
  165. package/src/_generated/utils/index.ts +0 -181
  166. package/src/_generated/zing_framework/coin_utils.ts +0 -41
  167. package/src/_generated/zing_framework/derived_object_bag.ts +0 -400
  168. package/src/_generated/zing_framework/derived_table.ts +0 -366
  169. package/src/_generated/zing_governance/admin.ts +0 -47
  170. package/src/_generated/zing_governance/treasury.ts +0 -134
  171. package/src/_generated/zing_identity/config.ts +0 -191
  172. package/src/_generated/zing_identity/ecdsa.ts +0 -166
  173. package/src/_generated/zing_identity/identity.ts +0 -661
  174. package/src/_generated/zing_identity/reclaim.ts +0 -803
  175. package/src/_generated/zing_studio/app.ts +0 -855
  176. package/src/_generated/zing_studio/article.ts +0 -598
  177. package/src/_generated/zing_studio/config.ts +0 -475
  178. package/src/_generated/zing_studio/deps/sui/dynamic_field.ts +0 -272
  179. package/src/_generated/zing_studio/deps/walrus/events.ts +0 -124
  180. package/src/_generated/zing_studio/deps/walrus/metadata.ts +0 -137
  181. package/src/_generated/zing_studio/enclave.ts +0 -466
  182. package/src/_generated/zing_studio/nitro_attestation.ts +0 -308
  183. package/src/_generated/zing_studio/referral.ts +0 -447
  184. package/src/_generated/zing_studio/storage.ts +0 -664
  185. package/src/_generated/zing_studio/studio.ts +0 -682
  186. package/src/bcs.ts +0 -139
  187. package/src/client/index.ts +0 -557
  188. package/src/client/types.ts +0 -28
  189. package/src/components/wallet-provider.tsx +0 -53
  190. package/src/components/walrus-provider.tsx +0 -212
  191. package/src/components/zing-provider.tsx +0 -54
  192. package/src/config/common.ts +0 -82
  193. package/src/config/index.ts +0 -45
  194. package/src/config/mainnet.ts +0 -109
  195. package/src/config/testnet.ts +0 -111
  196. package/src/config/types.ts +0 -69
  197. package/src/const.ts +0 -32
  198. package/src/getters.ts +0 -523
  199. package/src/hooks/useGetStudio.ts +0 -22
  200. package/src/hooks/useGetTierPlan.ts +0 -115
  201. package/src/hooks/useReclaim.ts +0 -165
  202. package/src/hooks/useSignAndExecuteTransaction.ts +0 -57
  203. package/src/hooks/useSignPersonalMessage.ts +0 -28
  204. package/src/hooks/useZingInfiniteQueries.ts +0 -92
  205. package/src/hooks/useZingMutation.ts +0 -66
  206. package/src/hooks/useZingQueries.ts +0 -107
  207. package/src/hooks/useZingQuery.ts +0 -88
  208. package/src/lib/utils/colorful-logger.ts +0 -27
  209. package/src/lib/utils/const.ts +0 -25
  210. package/src/lib/utils/helpers.ts +0 -78
  211. package/src/lib/utils/init-app-with-shadow.ts +0 -42
  212. package/src/lib/utils/quilt.ts +0 -317
  213. package/src/lib/utils/reclaim.ts +0 -148
  214. package/src/lib/utils/types.ts +0 -14
  215. package/src/mutations/seal.ts +0 -78
  216. package/src/mutations/signer.ts +0 -241
  217. package/src/mutations/walrus.ts +0 -862
  218. package/src/stores/walletStore.ts +0 -360
  219. package/src/stores/walrusStore.ts +0 -118
  220. package/src/stores/zkloginStore.ts +0 -53
  221. package/src/types.ts +0 -120
  222. package/tsconfig.json +0 -12
  223. /package/{src/_generated/index.js → dist/_generated/index.d.ts} +0 -0
  224. /package/{src/components/index.ts → dist/components/index.d.ts} +0 -0
  225. /package/{src/lib/utils/index.ts → dist/lib/utils/index.d.ts} +0 -0
  226. /package/{src/mutations/index.ts → dist/mutations/index.d.ts} +0 -0
  227. /package/{src/stores/index.ts → dist/stores/index.d.ts} +0 -0
@@ -1,148 +0,0 @@
1
- import {
2
- ClaimInfo,
3
- SignedClaim,
4
- } from "../../_generated/zing_identity/reclaim.js";
5
- import { bcs } from "@mysten/sui/bcs";
6
- import { ReclaimProofRequest } from "@reclaimprotocol/js-sdk";
7
- import sha3 from "js-sha3";
8
-
9
- export function hexToBytes(hex: string): Uint8Array {
10
- return new Uint8Array(
11
- hex.match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16)),
12
- );
13
- }
14
-
15
- export function bytesToHex(bytes: Uint8Array): string {
16
- return Array.from(bytes)
17
- .map((byte) => byte.toString(16).padStart(2, "0"))
18
- .join("");
19
- }
20
-
21
- export function textToBytes(text: string): Uint8Array {
22
- return new TextEncoder().encode(text);
23
- }
24
-
25
- // Uint8Array ? String
26
- export function bytesToText(bytes: Uint8Array): string {
27
- return new TextDecoder().decode(bytes);
28
- }
29
-
30
- export async function startVerification({
31
- reclaimAppId,
32
- reclaimAppSecret,
33
- reclaimProviderId,
34
- }: {
35
- reclaimAppId: string;
36
- reclaimAppSecret: string;
37
- reclaimProviderId: string;
38
- }) {
39
- try {
40
- // Initialize the Reclaim SDK with your credentials
41
- const reclaimProofRequest = await ReclaimProofRequest.init(
42
- reclaimAppId,
43
- reclaimAppSecret,
44
- reclaimProviderId,
45
- );
46
-
47
- reclaimProofRequest.setModalOptions({ modalPopupTimer: 3 }); // 3min
48
-
49
- // Trigger the verification session
50
- await reclaimProofRequest.triggerReclaimFlow();
51
-
52
- // Start listening for proof submissions and return a promise
53
- const proofs = await new Promise((resolve, reject) => {
54
- reclaimProofRequest.startSession({
55
- onSuccess: (proofs) => {
56
- console.log("Verification successful:", proofs);
57
- resolve(proofs);
58
- },
59
- onError: (error) => {
60
- console.error("Verification failed", error);
61
- reject(error);
62
- },
63
- });
64
- });
65
-
66
- return proofs;
67
- } catch (error) {
68
- console.error("Error starting verification:", error);
69
- throw error;
70
- }
71
- }
72
-
73
- export function generateCommitNonce(
74
- userAddress: string,
75
- claimIdentifier: string,
76
- ) {
77
- // Use the same random data as in Move function
78
- const randomBytes = crypto.getRandomValues(new Uint8Array(32));
79
- // Encode user address as BCS (32 bytes for address type)
80
- const addressBytes = bcs.Address.serialize(userAddress).toBytes();
81
-
82
- // Encode claim identifier as BCS address (32 bytes for address type)
83
- const identifierBytes = bcs.Address.serialize(claimIdentifier).toBytes();
84
-
85
- // Combine: random + userAddress + identifier (no timestamp to match Move logic)
86
- const combined = new Uint8Array(
87
- randomBytes.length + addressBytes.length + identifierBytes.length,
88
- );
89
-
90
- let offset = 0;
91
- combined.set(randomBytes, offset);
92
- offset += randomBytes.length;
93
- combined.set(addressBytes, offset);
94
- offset += addressBytes.length;
95
- combined.set(identifierBytes, offset);
96
-
97
- return Array.from(combined);
98
- }
99
-
100
- export function generateCommitmentHash(proof: ReclaimProof, nonce: number[]) {
101
- const claimInfoBytes = ClaimInfo.serialize({
102
- provider: proof.claimData.provider,
103
- parameters: proof.claimData.parameters,
104
- context: proof.claimData.context,
105
- }).toBytes(); // checked
106
- const signedClaimBytes = SignedClaim.serialize({
107
- claim: {
108
- identifier: proof.claimData.identifier,
109
- owner: proof.claimData.owner,
110
- epoch: proof.claimData.epoch.toString(),
111
- timestamp_s: proof.claimData.timestampS.toString(),
112
- },
113
- signatures: proof.signatures.map((sig) => hexToBytes(sig.substring(2))),
114
- }).toBytes();
115
-
116
- const combined = new Uint8Array(
117
- claimInfoBytes.length + signedClaimBytes.length + nonce.length,
118
- );
119
-
120
- let offset = 0;
121
- combined.set(claimInfoBytes, offset);
122
- offset += claimInfoBytes.length;
123
- combined.set(signedClaimBytes, offset);
124
- offset += signedClaimBytes.length;
125
- combined.set(nonce, offset);
126
-
127
- // Convert Uint8Array -> Buffer before hashing
128
- return hexToBytes(sha3.keccak256(combined));
129
- }
130
-
131
- export type ReclaimProof = {
132
- identifier: string;
133
- claimData: {
134
- provider: string;
135
- parameters: string;
136
- owner: string;
137
- timestampS: number;
138
- context: string;
139
- identifier: string;
140
- epoch: number;
141
- };
142
- signatures: string[];
143
- witnesses: {
144
- id: string;
145
- url: string;
146
- }[];
147
- publicData: string | null;
148
- };
@@ -1,14 +0,0 @@
1
- import type { COLORS } from "./const.js";
2
- import type { TupleToUnion } from "type-fest";
3
-
4
- export type * from "type-fest";
5
- export type ColorType =
6
- | "success"
7
- | "info"
8
- | "error"
9
- | "warning"
10
- | keyof typeof COLORS;
11
- export type ExcludeValuesFromBaseArrayType<
12
- B extends string[],
13
- E extends (string | number)[],
14
- > = Exclude<TupleToUnion<B>, TupleToUnion<E>>[];
@@ -1,78 +0,0 @@
1
- export async function createFileKey(): Promise<Uint8Array> {
2
- // Generate AES-GCM key
3
- const key = await crypto.subtle.generateKey(
4
- { name: "AES-GCM", length: 256 },
5
- true, // extractable (so we can export)
6
- ["encrypt", "decrypt"],
7
- );
8
-
9
- // Export as raw bytes
10
- const rawKey = await crypto.subtle.exportKey("raw", key);
11
- return new Uint8Array(rawKey); // 32 bytes (256 bits)
12
- }
13
-
14
- export async function importFileKey(
15
- raw: Uint8Array | ArrayBuffer,
16
- ): Promise<CryptoKey> {
17
- // Convert to Uint8Array and create a clean copy
18
- const keyBytes = raw instanceof Uint8Array ? raw : new Uint8Array(raw);
19
- const cleanBytes = new Uint8Array(keyBytes);
20
-
21
- return crypto.subtle.importKey(
22
- "raw",
23
- cleanBytes,
24
- { name: "AES-GCM" },
25
- false,
26
- ["encrypt", "decrypt"],
27
- );
28
- }
29
-
30
- /**
31
- * Encrypt data using AES-256-GCM.
32
- * @returns {Uint8Array} Concatenated [IV | ciphertext]
33
- */
34
- export async function encryptData(
35
- key: CryptoKey,
36
- data: ArrayBuffer | Uint8Array,
37
- iv: Uint8Array, // Accept IV as parameter
38
- ): Promise<Uint8Array> {
39
- const encoded = data instanceof Uint8Array ? data : new Uint8Array(data);
40
- const cleanEncoded = new Uint8Array(encoded);
41
- const cleanivBytes = new Uint8Array(iv);
42
- const ciphertext = new Uint8Array(
43
- await crypto.subtle.encrypt(
44
- { name: "AES-GCM", iv: cleanivBytes },
45
- key,
46
- cleanEncoded,
47
- ),
48
- );
49
-
50
- // Combine IV + ciphertext for easy storage
51
- const combined = new Uint8Array(iv.length + ciphertext.length);
52
- combined.set(iv);
53
- combined.set(ciphertext, iv.length);
54
- return combined;
55
- }
56
-
57
- /**
58
- * Decrypt data using AES-256-GCM.
59
- * Input must be the combined [IV | ciphertext] format.
60
- */
61
- export async function decryptData(
62
- key: CryptoKey,
63
- encrypted: ArrayBuffer | Uint8Array,
64
- ): Promise<Uint8Array> {
65
- const bytes =
66
- encrypted instanceof Uint8Array ? encrypted : new Uint8Array(encrypted);
67
- const iv = bytes.slice(0, 12);
68
- const ciphertext = bytes.slice(12);
69
-
70
- console.log({ iv, ciphertext });
71
-
72
- const plaintext = await crypto.subtle.decrypt(
73
- { name: "AES-GCM", iv },
74
- key,
75
- ciphertext,
76
- );
77
- return new Uint8Array(plaintext);
78
- }
@@ -1,241 +0,0 @@
1
- import { zkloginStorage } from "../stores/zkloginStore.js";
2
- import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
3
- import { fromBase64 } from "@mysten/sui/utils";
4
- import { decodeJwt } from "@mysten/sui/zklogin";
5
- import type {
6
- ZkLoginProofParams,
7
- ZkLoginProofResponse,
8
- ZKLoginSignerState,
9
- } from "../types.js";
10
- import type { SuiClient } from "@mysten/sui/client";
11
- import type { Transaction } from "@mysten/sui/transactions";
12
-
13
- const SALT_SERVER_BASE_URL = "https://salt-server-production.up.railway.app";
14
-
15
- export async function getSigner(
16
- suiClient: SuiClient,
17
- ): Promise<ZKLoginSignerState> {
18
- const zkLoginStorage = await zkloginStorage.getState();
19
-
20
- const { epoch: currentEpoch } = await suiClient.getLatestSuiSystemState();
21
-
22
- if (
23
- !zkLoginStorage?.jwt ||
24
- !zkLoginStorage?.maxEpoch ||
25
- !zkLoginStorage.ephemeralSecretKey ||
26
- !zkLoginStorage.zkProof ||
27
- !zkLoginStorage.suiAddress
28
- )
29
- return {
30
- ephemeralKeyPair: null,
31
- suiAddress: null,
32
- decodedJwt: null,
33
- zkProof: null,
34
- maxEpoch: null,
35
- };
36
-
37
- if (Number(currentEpoch) > zkLoginStorage.maxEpoch)
38
- return {
39
- ephemeralKeyPair: null,
40
- suiAddress: null,
41
- decodedJwt: null,
42
- zkProof: null,
43
- maxEpoch: null,
44
- };
45
-
46
- const ephemeralKeyPair = Ed25519Keypair.fromSecretKey(
47
- fromBase64(zkLoginStorage.ephemeralSecretKey),
48
- );
49
-
50
- return {
51
- suiAddress: zkLoginStorage.suiAddress,
52
- ephemeralKeyPair,
53
- decodedJwt: decodeJwt(zkLoginStorage.jwt),
54
- zkProof: JSON.parse(zkLoginStorage.zkProof),
55
- maxEpoch: zkLoginStorage.maxEpoch,
56
- };
57
- }
58
-
59
- export async function signAndExecuteGasPoolTransaction(
60
- signer: ZKLoginSignerState,
61
- suiClient: SuiClient,
62
- tx: Transaction,
63
- gasBudget = 0.1 * 10 ** 9,
64
- reserveDurationSecs = 30,
65
- ) {
66
- const { ephemeralKeyPair, suiAddress, decodedJwt, zkProof, maxEpoch } =
67
- signer;
68
- if (!ephemeralKeyPair || !suiAddress || !decodedJwt || !zkProof || !maxEpoch)
69
- return;
70
-
71
- try {
72
- if (!decodedJwt.sub || !decodedJwt.iss || !decodedJwt.aud) {
73
- throw new Error("JWT claims (sub, iss, aud) not found");
74
- }
75
-
76
- // Step 1: Request gas reservation from server
77
- const prepareResponse = await fetch(`${SALT_SERVER_BASE_URL}/prepare_tx`, {
78
- method: "POST",
79
- headers: {
80
- "Content-Type": "application/json",
81
- },
82
- body: JSON.stringify({
83
- sub: decodedJwt.sub,
84
- iss: decodedJwt.iss,
85
- aud: Array.isArray(decodedJwt.aud) ? decodedJwt.aud[0] : decodedJwt.aud,
86
- gasBudget,
87
- reserveDurationSecs,
88
- }),
89
- });
90
-
91
- if (!prepareResponse.ok) {
92
- const errorData = await prepareResponse.json().catch(() => ({}));
93
- throw new Error(
94
- errorData.error || `Server error: ${prepareResponse.statusText}`,
95
- );
96
- }
97
-
98
- const prepareData = await prepareResponse.json();
99
-
100
- if (!prepareData.result) {
101
- throw new Error(`Failed to reserve gas: ${prepareData.error}`);
102
- }
103
-
104
- // Step 2: Set gas payment and sender
105
- tx.setSender(suiAddress);
106
- tx.setGasOwner(prepareData.result.sponsor_address);
107
- tx.setGasPayment(prepareData.result.gas_coins);
108
-
109
- // Step 4: Sign only the transaction bytes with ephemeral key
110
- const { bytes, signature: ephemeralSignature } = await tx.sign({
111
- client: suiClient,
112
- signer: ephemeralKeyPair,
113
- });
114
-
115
- // Step 5: Send to server for zkLogin signature generation and execution
116
- const executeResponse = await fetch(`${SALT_SERVER_BASE_URL}/execute_tx`, {
117
- method: "POST",
118
- headers: {
119
- "Content-Type": "application/json",
120
- },
121
- body: JSON.stringify({
122
- sub: decodedJwt.sub,
123
- iss: decodedJwt.iss,
124
- aud: Array.isArray(decodedJwt.aud) ? decodedJwt.aud[0] : decodedJwt.aud,
125
- reservationId: prepareData.result.reservation_id,
126
- txBytes: bytes,
127
- ephemeralSignature,
128
- maxEpoch,
129
- zkProof,
130
- }),
131
- });
132
-
133
- const executeData = await executeResponse.json().catch(() => ({}));
134
-
135
- if (!executeResponse.ok) {
136
- throw new Error(
137
- executeData.error || `Server error: ${executeResponse.statusText}`,
138
- );
139
- }
140
-
141
- if (!executeData.effects) {
142
- throw new Error(
143
- `Transaction execution failed: ${executeData.error || "Unknown error"}`,
144
- );
145
- }
146
-
147
- return executeData.effects;
148
- } catch (error) {
149
- console.error(error);
150
- throw error;
151
- }
152
- }
153
-
154
- export async function signPersonalMessage(
155
- signer: ZKLoginSignerState,
156
- message: Uint8Array,
157
- ) {
158
- const { ephemeralKeyPair, decodedJwt, zkProof, maxEpoch } = signer;
159
- if (!ephemeralKeyPair || !decodedJwt || !zkProof || !maxEpoch) {
160
- throw new Error("Missing required params");
161
- }
162
-
163
- try {
164
- if (!decodedJwt.sub || !decodedJwt.iss || !decodedJwt.aud) {
165
- throw new Error("JWT claims (sub, iss, aud) not found");
166
- }
167
-
168
- // Sign message with ephemeral key
169
- const { bytes, signature } =
170
- await ephemeralKeyPair.signPersonalMessage(message);
171
-
172
- // Convert bytes to base64 for transmission
173
- const response = await fetch(
174
- `${SALT_SERVER_BASE_URL}/sign_personal_message`,
175
- {
176
- method: "POST",
177
- headers: {
178
- "Content-Type": "application/json",
179
- },
180
- body: JSON.stringify({
181
- sub: decodedJwt.sub,
182
- iss: decodedJwt.iss,
183
- aud: Array.isArray(decodedJwt.aud)
184
- ? decodedJwt.aud[0]
185
- : decodedJwt.aud,
186
- messageBytes: bytes,
187
- ephemeralSignature: signature,
188
- maxEpoch,
189
- zkProof,
190
- }),
191
- },
192
- );
193
-
194
- if (!response.ok) {
195
- const errorData = await response.json().catch(() => ({}));
196
- throw new Error(
197
- errorData.error || `Server error: ${response.statusText}`,
198
- );
199
- }
200
-
201
- const data = await response.json();
202
-
203
- if (!data.zkLoginSignature) {
204
- throw new Error("Failed to generate zkLogin signature");
205
- }
206
-
207
- return {
208
- zkLoginSignature: data.zkLoginSignature,
209
- bytes: fromBase64(data.bytes),
210
- };
211
- } catch (error) {
212
- console.error(error);
213
- throw error;
214
- }
215
- }
216
-
217
- export async function createZkLoginProof(
218
- params: ZkLoginProofParams,
219
- ): Promise<ZkLoginProofResponse> {
220
- try {
221
- const response = await fetch(`${SALT_SERVER_BASE_URL}/zklogin-proof`, {
222
- method: "POST",
223
- headers: {
224
- "Content-Type": "application/json",
225
- },
226
- body: JSON.stringify(params),
227
- });
228
-
229
- if (!response.ok) {
230
- const errorData = await response.json().catch(() => ({}));
231
- throw new Error(errorData.error || `HTTP ${response.status}`);
232
- }
233
-
234
- return await response.json();
235
- } catch (error) {
236
- if (error instanceof Error) {
237
- throw error;
238
- }
239
- throw new Error("Unknown error occurred");
240
- }
241
- }