@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.
- package/dist/_generated/index.js +6 -0
- package/dist/_generated/utils/index.d.ts +12 -0
- package/dist/_generated/utils/index.js +135 -0
- package/dist/_generated/zing_framework/coin_utils.d.ts +15 -0
- package/dist/_generated/zing_framework/coin_utils.js +19 -0
- package/dist/_generated/zing_framework/deps/sui/object.d.ts +8 -0
- package/{src/_generated/zing_governance/deps/sui/object.ts → dist/_generated/zing_framework/deps/sui/object.js} +4 -7
- package/dist/_generated/zing_framework/derived_object_bag.d.ts +166 -0
- package/dist/_generated/zing_framework/derived_object_bag.js +221 -0
- package/dist/_generated/zing_framework/derived_table.d.ts +153 -0
- package/dist/_generated/zing_framework/derived_table.js +200 -0
- package/dist/_generated/zing_governance/admin.d.ts +20 -0
- package/dist/_generated/zing_governance/admin.js +26 -0
- package/dist/_generated/zing_governance/deps/sui/object.d.ts +8 -0
- package/{src/_generated/zing_identity/deps/sui/object.ts → dist/_generated/zing_governance/deps/sui/object.js} +4 -7
- package/dist/_generated/zing_governance/treasury.d.ts +56 -0
- package/dist/_generated/zing_governance/treasury.js +78 -0
- package/dist/_generated/zing_identity/config.d.ts +87 -0
- package/dist/_generated/zing_identity/config.js +98 -0
- package/{src/_generated/zing_studio/deps/std/type_name.ts → dist/_generated/zing_identity/deps/std/type_name.d.ts} +3 -11
- package/dist/_generated/zing_identity/deps/std/type_name.js +22 -0
- package/dist/_generated/zing_identity/deps/sui/object.d.ts +8 -0
- package/{src/_generated/zing_studio/deps/sui/object.ts → dist/_generated/zing_identity/deps/sui/object.js} +4 -7
- package/dist/_generated/zing_identity/deps/sui/object_table.d.ts +12 -0
- package/{src/_generated/zing_identity/deps/sui/object_table.ts → dist/_generated/zing_identity/deps/sui/object_table.js} +7 -10
- package/dist/_generated/zing_identity/deps/sui/table.d.ts +12 -0
- package/{src/_generated/zing_studio/deps/sui/table.ts → dist/_generated/zing_identity/deps/sui/table.js} +7 -10
- package/{src/_generated/zing_identity/deps/sui/vec_set.ts → dist/_generated/zing_identity/deps/sui/vec_set.d.ts} +3 -14
- package/dist/_generated/zing_identity/deps/sui/vec_set.js +22 -0
- package/dist/_generated/zing_identity/ecdsa.d.ts +81 -0
- package/dist/_generated/zing_identity/ecdsa.js +87 -0
- package/dist/_generated/zing_identity/identity.d.ts +262 -0
- package/dist/_generated/zing_identity/identity.js +294 -0
- package/dist/_generated/zing_identity/reclaim.d.ts +356 -0
- package/dist/_generated/zing_identity/reclaim.js +414 -0
- package/dist/_generated/zing_studio/app.d.ts +351 -0
- package/dist/_generated/zing_studio/app.js +451 -0
- package/dist/_generated/zing_studio/article.d.ts +331 -0
- package/dist/_generated/zing_studio/article.js +331 -0
- package/dist/_generated/zing_studio/config.d.ts +203 -0
- package/dist/_generated/zing_studio/config.js +233 -0
- package/{src/_generated/zing_identity/deps/std/type_name.ts → dist/_generated/zing_studio/deps/std/type_name.d.ts} +3 -11
- package/dist/_generated/zing_studio/deps/std/type_name.js +22 -0
- package/dist/_generated/zing_studio/deps/sui/bag.d.ts +12 -0
- package/{src/_generated/zing_studio/deps/sui/bag.ts → dist/_generated/zing_studio/deps/sui/bag.js} +7 -10
- package/dist/_generated/zing_studio/deps/sui/balance.d.ts +12 -0
- package/{src/_generated/zing_studio/deps/sui/balance.ts → dist/_generated/zing_studio/deps/sui/balance.js} +4 -7
- package/dist/_generated/zing_studio/deps/sui/coin.d.ts +12 -0
- package/{src/_generated/zing_studio/deps/sui/coin.ts → dist/_generated/zing_studio/deps/sui/coin.js} +5 -8
- package/dist/_generated/zing_studio/deps/sui/dynamic_field.d.ts +126 -0
- package/dist/_generated/zing_studio/deps/sui/dynamic_field.js +140 -0
- package/dist/_generated/zing_studio/deps/sui/object.d.ts +8 -0
- package/{src/_generated/zing_framework/deps/sui/object.ts → dist/_generated/zing_studio/deps/sui/object.js} +4 -7
- package/dist/_generated/zing_studio/deps/sui/table.d.ts +12 -0
- package/{src/_generated/zing_identity/deps/sui/table.ts → dist/_generated/zing_studio/deps/sui/table.js} +7 -10
- package/dist/_generated/zing_studio/deps/sui/vec_map.d.ts +30 -0
- package/{src/_generated/zing_studio/deps/sui/vec_map.ts → dist/_generated/zing_studio/deps/sui/vec_map.js} +15 -21
- package/{src/_generated/zing_studio/deps/sui/vec_set.ts → dist/_generated/zing_studio/deps/sui/vec_set.d.ts} +3 -14
- package/dist/_generated/zing_studio/deps/sui/vec_set.js +22 -0
- package/dist/_generated/zing_studio/deps/walrus/blob.d.ts +20 -0
- package/{src/_generated/zing_studio/deps/walrus/blob.ts → dist/_generated/zing_studio/deps/walrus/blob.js} +11 -12
- package/dist/_generated/zing_studio/deps/walrus/events.d.ts +83 -0
- package/dist/_generated/zing_studio/deps/walrus/events.js +122 -0
- package/dist/_generated/zing_studio/deps/walrus/metadata.d.ts +71 -0
- package/dist/_generated/zing_studio/deps/walrus/metadata.js +73 -0
- package/dist/_generated/zing_studio/deps/walrus/storage_resource.d.ts +12 -0
- package/{src/_generated/zing_studio/deps/walrus/storage_resource.ts → dist/_generated/zing_studio/deps/walrus/storage_resource.js} +7 -8
- package/dist/_generated/zing_studio/deps/zing_framework/derived_object_bag.d.ts +12 -0
- 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
- package/dist/_generated/zing_studio/deps/zing_framework/derived_table.d.ts +12 -0
- package/{src/_generated/zing_studio/deps/zing_framework/derived_table.ts → dist/_generated/zing_studio/deps/zing_framework/derived_table.js} +7 -8
- package/dist/_generated/zing_studio/donation.d.ts +24 -0
- package/{src/_generated/zing_studio/donation.ts → dist/_generated/zing_studio/donation.js} +7 -8
- package/dist/_generated/zing_studio/enclave.d.ts +187 -0
- package/dist/_generated/zing_studio/enclave.js +231 -0
- package/dist/_generated/zing_studio/nitro_attestation.d.ts +148 -0
- package/dist/_generated/zing_studio/nitro_attestation.js +183 -0
- package/dist/_generated/zing_studio/referral.d.ts +166 -0
- package/dist/_generated/zing_studio/referral.js +224 -0
- package/dist/_generated/zing_studio/storage.d.ts +327 -0
- package/dist/_generated/zing_studio/storage.js +365 -0
- package/dist/_generated/zing_studio/studio.d.ts +282 -0
- package/dist/_generated/zing_studio/studio.js +360 -0
- package/dist/bcs.d.ts +61 -0
- package/dist/bcs.js +109 -0
- package/dist/client/index.d.ts +228 -0
- package/dist/client/index.js +296 -0
- package/dist/client/types.d.ts +23 -0
- package/dist/client/types.js +1 -0
- package/dist/components/index.js +3 -0
- package/dist/components/wallet-provider.d.ts +7 -0
- package/dist/components/wallet-provider.js +33 -0
- package/dist/components/walrus-provider.d.ts +31 -0
- package/dist/components/walrus-provider.js +71 -0
- package/dist/components/zing-provider.d.ts +15 -0
- package/dist/components/zing-provider.js +22 -0
- package/dist/config/common.d.ts +23 -0
- package/dist/config/common.js +61 -0
- package/dist/config/index.d.ts +8 -0
- package/dist/config/index.js +28 -0
- package/dist/config/mainnet.d.ts +6 -0
- package/dist/config/mainnet.js +80 -0
- package/dist/config/testnet.d.ts +6 -0
- package/dist/config/testnet.js +81 -0
- package/dist/config/types.d.ts +49 -0
- package/dist/config/types.js +1 -0
- package/dist/const.d.ts +4 -0
- package/dist/const.js +25 -0
- package/dist/getters.d.ts +228 -0
- package/dist/getters.js +317 -0
- package/{src/hooks/index.ts → dist/hooks/index.d.ts} +2 -3
- package/dist/hooks/index.js +9 -0
- package/dist/hooks/useGetStudio.d.ts +26 -0
- package/dist/hooks/useGetStudio.js +17 -0
- package/dist/hooks/useGetTierPlan.d.ts +7 -0
- package/dist/hooks/useGetTierPlan.js +85 -0
- package/dist/hooks/useReclaim.d.ts +12 -0
- package/dist/hooks/useReclaim.js +87 -0
- package/dist/hooks/useSignAndExecuteTransaction.d.ts +6 -0
- package/dist/hooks/useSignAndExecuteTransaction.js +40 -0
- package/dist/hooks/useSignPersonalMessage.d.ts +8 -0
- package/dist/hooks/useSignPersonalMessage.js +21 -0
- package/dist/hooks/useZingInfiniteQueries.d.ts +75 -0
- package/dist/hooks/useZingInfiniteQueries.js +28 -0
- package/dist/hooks/useZingMutation.d.ts +35 -0
- package/dist/hooks/useZingMutation.js +37 -0
- package/dist/hooks/useZingQueries.d.ts +127 -0
- package/dist/hooks/useZingQueries.js +37 -0
- package/dist/hooks/useZingQuery.d.ts +147 -0
- package/dist/hooks/useZingQuery.js +34 -0
- package/{src/index.ts → dist/index.d.ts} +2 -3
- package/dist/index.js +9 -0
- package/dist/lib/utils/colorful-logger.d.ts +2 -0
- package/dist/lib/utils/colorful-logger.js +23 -0
- package/dist/lib/utils/const.d.ts +25 -0
- package/dist/lib/utils/const.js +25 -0
- package/dist/lib/utils/helpers.d.ts +10 -0
- package/dist/lib/utils/helpers.js +49 -0
- package/dist/lib/utils/index.js +4 -0
- package/dist/lib/utils/init-app-with-shadow.d.ts +6 -0
- package/dist/lib/utils/init-app-with-shadow.js +28 -0
- package/dist/lib/utils/quilt.d.ts +61 -0
- package/dist/lib/utils/quilt.js +194 -0
- package/dist/lib/utils/reclaim.d.ts +29 -0
- package/dist/lib/utils/reclaim.js +88 -0
- package/dist/lib/utils/types.d.ts +5 -0
- package/dist/lib/utils/types.js +1 -0
- package/dist/mutations/index.js +3 -0
- package/dist/mutations/seal.d.ts +12 -0
- package/dist/mutations/seal.js +41 -0
- package/dist/mutations/signer.d.ts +10 -0
- package/dist/mutations/signer.js +176 -0
- package/dist/mutations/walrus.d.ts +98 -0
- package/dist/mutations/walrus.js +488 -0
- package/dist/stores/index.js +3 -0
- package/dist/stores/walletStore.d.ts +48 -0
- package/dist/stores/walletStore.js +232 -0
- package/dist/stores/walrusStore.d.ts +28 -0
- package/dist/stores/walrusStore.js +61 -0
- package/dist/stores/zkloginStore.d.ts +28 -0
- package/dist/stores/zkloginStore.js +27 -0
- package/dist/types.d.ts +99 -0
- package/dist/types.js +1 -0
- package/package.json +18 -10
- package/src/_generated/utils/index.ts +0 -181
- package/src/_generated/zing_framework/coin_utils.ts +0 -41
- package/src/_generated/zing_framework/derived_object_bag.ts +0 -400
- package/src/_generated/zing_framework/derived_table.ts +0 -366
- package/src/_generated/zing_governance/admin.ts +0 -47
- package/src/_generated/zing_governance/treasury.ts +0 -134
- package/src/_generated/zing_identity/config.ts +0 -191
- package/src/_generated/zing_identity/ecdsa.ts +0 -166
- package/src/_generated/zing_identity/identity.ts +0 -661
- package/src/_generated/zing_identity/reclaim.ts +0 -803
- package/src/_generated/zing_studio/app.ts +0 -855
- package/src/_generated/zing_studio/article.ts +0 -598
- package/src/_generated/zing_studio/config.ts +0 -475
- package/src/_generated/zing_studio/deps/sui/dynamic_field.ts +0 -272
- package/src/_generated/zing_studio/deps/walrus/events.ts +0 -124
- package/src/_generated/zing_studio/deps/walrus/metadata.ts +0 -137
- package/src/_generated/zing_studio/enclave.ts +0 -466
- package/src/_generated/zing_studio/nitro_attestation.ts +0 -308
- package/src/_generated/zing_studio/referral.ts +0 -447
- package/src/_generated/zing_studio/storage.ts +0 -664
- package/src/_generated/zing_studio/studio.ts +0 -682
- package/src/bcs.ts +0 -139
- package/src/client/index.ts +0 -557
- package/src/client/types.ts +0 -28
- package/src/components/wallet-provider.tsx +0 -53
- package/src/components/walrus-provider.tsx +0 -212
- package/src/components/zing-provider.tsx +0 -54
- package/src/config/common.ts +0 -82
- package/src/config/index.ts +0 -45
- package/src/config/mainnet.ts +0 -109
- package/src/config/testnet.ts +0 -111
- package/src/config/types.ts +0 -69
- package/src/const.ts +0 -32
- package/src/getters.ts +0 -523
- package/src/hooks/useGetStudio.ts +0 -22
- package/src/hooks/useGetTierPlan.ts +0 -115
- package/src/hooks/useReclaim.ts +0 -165
- package/src/hooks/useSignAndExecuteTransaction.ts +0 -57
- package/src/hooks/useSignPersonalMessage.ts +0 -28
- package/src/hooks/useZingInfiniteQueries.ts +0 -92
- package/src/hooks/useZingMutation.ts +0 -66
- package/src/hooks/useZingQueries.ts +0 -107
- package/src/hooks/useZingQuery.ts +0 -88
- package/src/lib/utils/colorful-logger.ts +0 -27
- package/src/lib/utils/const.ts +0 -25
- package/src/lib/utils/helpers.ts +0 -78
- package/src/lib/utils/init-app-with-shadow.ts +0 -42
- package/src/lib/utils/quilt.ts +0 -317
- package/src/lib/utils/reclaim.ts +0 -148
- package/src/lib/utils/types.ts +0 -14
- package/src/mutations/seal.ts +0 -78
- package/src/mutations/signer.ts +0 -241
- package/src/mutations/walrus.ts +0 -862
- package/src/stores/walletStore.ts +0 -360
- package/src/stores/walrusStore.ts +0 -118
- package/src/stores/zkloginStore.ts +0 -53
- package/src/types.ts +0 -120
- package/tsconfig.json +0 -12
- /package/{src/_generated/index.js → dist/_generated/index.d.ts} +0 -0
- /package/{src/components/index.ts → dist/components/index.d.ts} +0 -0
- /package/{src/lib/utils/index.ts → dist/lib/utils/index.d.ts} +0 -0
- /package/{src/mutations/index.ts → dist/mutations/index.d.ts} +0 -0
- /package/{src/stores/index.ts → dist/stores/index.d.ts} +0 -0
package/src/lib/utils/reclaim.ts
DELETED
|
@@ -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
|
-
};
|
package/src/lib/utils/types.ts
DELETED
|
@@ -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>>[];
|
package/src/mutations/seal.ts
DELETED
|
@@ -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
|
-
}
|
package/src/mutations/signer.ts
DELETED
|
@@ -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
|
-
}
|