@mysten/enoki 0.2.7 → 0.3.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 +20 -0
- package/dist/cjs/EnokiClient/index.d.ts +3 -3
- package/dist/cjs/EnokiClient/index.js +3 -3
- package/dist/cjs/EnokiClient/index.js.map +2 -2
- package/dist/cjs/EnokiClient/type.d.ts +7 -7
- package/dist/cjs/EnokiClient/type.js.map +1 -1
- package/dist/cjs/EnokiFlow.d.ts +9 -9
- package/dist/cjs/EnokiFlow.js +18 -25
- package/dist/cjs/EnokiFlow.js.map +2 -2
- package/dist/cjs/EnokiKeypair.d.ts +7 -8
- package/dist/cjs/EnokiKeypair.js +15 -9
- package/dist/cjs/EnokiKeypair.js.map +2 -2
- package/dist/cjs/encryption.js +1 -1
- package/dist/cjs/encryption.js.map +1 -1
- package/dist/esm/EnokiClient/index.d.ts +3 -3
- package/dist/esm/EnokiClient/index.js +3 -3
- package/dist/esm/EnokiClient/index.js.map +2 -2
- package/dist/esm/EnokiClient/type.d.ts +7 -7
- package/dist/esm/EnokiFlow.d.ts +9 -9
- package/dist/esm/EnokiFlow.js +18 -25
- package/dist/esm/EnokiFlow.js.map +2 -2
- package/dist/esm/EnokiKeypair.d.ts +7 -8
- package/dist/esm/EnokiKeypair.js +15 -9
- package/dist/esm/EnokiKeypair.js.map +2 -2
- package/dist/esm/encryption.js +1 -1
- package/dist/esm/encryption.js.map +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { PublicKey } from '@mysten/sui
|
|
2
|
-
import type { ZkLoginSignatureInputs } from '@mysten/sui
|
|
1
|
+
import type { PublicKey } from '@mysten/sui/cryptography';
|
|
2
|
+
import type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';
|
|
3
3
|
import type { AuthProvider } from '../EnokiFlow.js';
|
|
4
4
|
export type EnokiNetwork = 'mainnet' | 'testnet' | 'devnet';
|
|
5
5
|
export interface GetAppApiInput {
|
|
@@ -38,9 +38,9 @@ export interface CreateZkLoginZkpApiInput {
|
|
|
38
38
|
}
|
|
39
39
|
export interface CreateZkLoginZkpApiResponse extends ZkLoginSignatureInputs {
|
|
40
40
|
}
|
|
41
|
-
export type
|
|
41
|
+
export type CreateSponsoredTransactionApiInput = {
|
|
42
42
|
network?: EnokiNetwork;
|
|
43
|
-
|
|
43
|
+
transactionKindBytes: string;
|
|
44
44
|
} & ({
|
|
45
45
|
jwt: string;
|
|
46
46
|
sender?: never;
|
|
@@ -52,14 +52,14 @@ export type CreateSponsoredTransactionBlockApiInput = {
|
|
|
52
52
|
allowedMoveCallTargets?: string[];
|
|
53
53
|
jwt?: never;
|
|
54
54
|
});
|
|
55
|
-
export interface
|
|
55
|
+
export interface CreateSponsoredTransactionApiResponse {
|
|
56
56
|
bytes: string;
|
|
57
57
|
digest: string;
|
|
58
58
|
}
|
|
59
|
-
export interface
|
|
59
|
+
export interface ExecuteSponsoredTransactionApiInput {
|
|
60
60
|
digest: string;
|
|
61
61
|
signature: string;
|
|
62
62
|
}
|
|
63
|
-
export interface
|
|
63
|
+
export interface ExecuteSponsoredTransactionApiResponse {
|
|
64
64
|
digest: string;
|
|
65
65
|
}
|
package/dist/esm/EnokiFlow.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { SuiClient } from '@mysten/sui
|
|
2
|
-
import type {
|
|
3
|
-
import type { ZkLoginSignatureInputs } from '@mysten/sui
|
|
1
|
+
import type { SuiClient } from '@mysten/sui/client';
|
|
2
|
+
import type { Transaction } from '@mysten/sui/transactions';
|
|
3
|
+
import type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';
|
|
4
4
|
import type { WritableAtom } from 'nanostores';
|
|
5
5
|
import type { Encryption } from './encryption.js';
|
|
6
6
|
import type { EnokiClientConfig } from './EnokiClient/index.js';
|
|
@@ -58,12 +58,12 @@ export declare class EnokiFlow {
|
|
|
58
58
|
getKeypair({ network }?: {
|
|
59
59
|
network?: 'mainnet' | 'testnet';
|
|
60
60
|
}): Promise<EnokiKeypair>;
|
|
61
|
-
|
|
61
|
+
sponsorTransaction({ network, transaction, client, }: {
|
|
62
62
|
network?: 'mainnet' | 'testnet';
|
|
63
|
-
|
|
63
|
+
transaction: Transaction;
|
|
64
64
|
client: SuiClient;
|
|
65
|
-
}): Promise<import("./EnokiClient/type.js").
|
|
66
|
-
|
|
65
|
+
}): Promise<import("./EnokiClient/type.js").CreateSponsoredTransactionApiResponse>;
|
|
66
|
+
executeTransaction({ network, bytes, digest, client, }: {
|
|
67
67
|
network?: 'mainnet' | 'testnet';
|
|
68
68
|
bytes: string;
|
|
69
69
|
digest: string;
|
|
@@ -71,9 +71,9 @@ export declare class EnokiFlow {
|
|
|
71
71
|
}): Promise<{
|
|
72
72
|
digest: string;
|
|
73
73
|
}>;
|
|
74
|
-
|
|
74
|
+
sponsorAndExecuteTransaction({ network, transaction, client, }: {
|
|
75
75
|
network?: 'mainnet' | 'testnet';
|
|
76
|
-
|
|
76
|
+
transaction: Transaction;
|
|
77
77
|
client: SuiClient;
|
|
78
78
|
}): Promise<{
|
|
79
79
|
digest: string;
|
package/dist/esm/EnokiFlow.js
CHANGED
|
@@ -21,9 +21,9 @@ var __privateMethod = (obj, member, method) => {
|
|
|
21
21
|
return method;
|
|
22
22
|
};
|
|
23
23
|
var _storageKeys, _enokiClient, _encryption, _encryptionKey, _store, _setSession, setSession_fn;
|
|
24
|
-
import { decodeSuiPrivateKey } from "@mysten/sui
|
|
25
|
-
import { Ed25519Keypair } from "@mysten/sui
|
|
26
|
-
import { fromB64, toB64 } from "@mysten/sui
|
|
24
|
+
import { decodeSuiPrivateKey } from "@mysten/sui/cryptography";
|
|
25
|
+
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
|
|
26
|
+
import { fromB64, toB64 } from "@mysten/sui/utils";
|
|
27
27
|
import { decodeJwt } from "jose";
|
|
28
28
|
import { atom, onMount, onSet } from "nanostores";
|
|
29
29
|
import { createDefaultEncryption } from "./encryption.js";
|
|
@@ -215,58 +215,51 @@ class EnokiFlow {
|
|
|
215
215
|
ephemeralKeypair: Ed25519Keypair.fromSecretKey(fromB64(zkp.ephemeralKeyPair))
|
|
216
216
|
});
|
|
217
217
|
}
|
|
218
|
-
async
|
|
218
|
+
async sponsorTransaction({
|
|
219
219
|
network,
|
|
220
|
-
|
|
220
|
+
transaction,
|
|
221
221
|
client
|
|
222
222
|
}) {
|
|
223
223
|
const session = await this.getSession();
|
|
224
224
|
if (!session || !session.jwt) {
|
|
225
225
|
throw new Error("Missing required data for sponsorship.");
|
|
226
226
|
}
|
|
227
|
-
const
|
|
227
|
+
const transactionKindBytes = await transaction.build({
|
|
228
228
|
onlyTransactionKind: true,
|
|
229
|
-
client
|
|
230
|
-
// Theses limits will get verified during the final transaction construction, so we can safely ignore them here:
|
|
231
|
-
limits: {
|
|
232
|
-
maxGasObjects: Infinity,
|
|
233
|
-
maxPureArgumentSize: Infinity,
|
|
234
|
-
maxTxGas: Infinity,
|
|
235
|
-
maxTxSizeBytes: Infinity
|
|
236
|
-
}
|
|
229
|
+
client
|
|
237
230
|
});
|
|
238
|
-
return await __privateGet(this, _enokiClient).
|
|
231
|
+
return await __privateGet(this, _enokiClient).createSponsoredTransaction({
|
|
239
232
|
jwt: session.jwt,
|
|
240
233
|
network,
|
|
241
|
-
|
|
234
|
+
transactionKindBytes: toB64(transactionKindBytes)
|
|
242
235
|
});
|
|
243
236
|
}
|
|
244
|
-
async
|
|
237
|
+
async executeTransaction({
|
|
245
238
|
network,
|
|
246
239
|
bytes,
|
|
247
240
|
digest,
|
|
248
241
|
client
|
|
249
242
|
}) {
|
|
250
243
|
const keypair = await this.getKeypair({ network });
|
|
251
|
-
const userSignature = await keypair.
|
|
252
|
-
await __privateGet(this, _enokiClient).
|
|
244
|
+
const userSignature = await keypair.signTransaction(fromB64(bytes));
|
|
245
|
+
await __privateGet(this, _enokiClient).executeSponsoredTransaction({
|
|
253
246
|
digest,
|
|
254
247
|
signature: userSignature.signature
|
|
255
248
|
});
|
|
256
|
-
await client.
|
|
249
|
+
await client.waitForTransaction({ digest });
|
|
257
250
|
return { digest };
|
|
258
251
|
}
|
|
259
|
-
async
|
|
252
|
+
async sponsorAndExecuteTransaction({
|
|
260
253
|
network,
|
|
261
|
-
|
|
254
|
+
transaction,
|
|
262
255
|
client
|
|
263
256
|
}) {
|
|
264
|
-
const { bytes, digest } = await this.
|
|
257
|
+
const { bytes, digest } = await this.sponsorTransaction({
|
|
265
258
|
network,
|
|
266
|
-
|
|
259
|
+
transaction,
|
|
267
260
|
client
|
|
268
261
|
});
|
|
269
|
-
return await this.
|
|
262
|
+
return await this.executeTransaction({ network, bytes, digest, client });
|
|
270
263
|
}
|
|
271
264
|
}
|
|
272
265
|
_storageKeys = new WeakMap();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/EnokiFlow.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SuiClient } from '@mysten/sui.js/client';\nimport { decodeSuiPrivateKey } from '@mysten/sui.js/cryptography';\nimport { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519';\nimport type { TransactionBlock } from '@mysten/sui.js/transactions';\nimport { fromB64, toB64 } from '@mysten/sui.js/utils';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui.js/zklogin';\nimport { decodeJwt } from 'jose';\nimport type { WritableAtom } from 'nanostores';\nimport { atom, onMount, onSet } from 'nanostores';\n\nimport type { Encryption } from './encryption.js';\nimport { createDefaultEncryption } from './encryption.js';\nimport type { EnokiClientConfig } from './EnokiClient/index.js';\nimport { EnokiClient } from './EnokiClient/index.js';\nimport { EnokiKeypair } from './EnokiKeypair.js';\nimport type { SyncStore } from './stores.js';\nimport { createSessionStorage } from './stores.js';\n\nexport interface EnokiFlowConfig extends EnokiClientConfig {\n\t/**\n\t * The storage interface to persist Enoki data locally.\n\t * If not provided, it will use a sessionStorage-backed store.\n\t */\n\tstore?: SyncStore;\n\t/**\n\t * The encryption interface that will be used to encrypt data before storing it locally.\n\t * If not provided, it will use a default encryption interface.\n\t */\n\tencryption?: Encryption;\n}\n\n// State that is not bound to a session, and is encrypted.\nexport interface ZkLoginState {\n\tprovider?: AuthProvider;\n\taddress?: string;\n\tsalt?: string;\n}\n\n// State that session-bound, and is encrypted in storage.\nexport interface ZkLoginSession {\n\tephemeralKeyPair: string;\n\tmaxEpoch: number;\n\trandomness: string;\n\texpiresAt: number;\n\n\tjwt?: string;\n\tproof?: ZkLoginSignatureInputs;\n}\n\nexport type AuthProvider = 'google' | 'facebook' | 'twitch';\n\nconst createStorageKeys = (apiKey: string) => ({\n\tSTATE: `@enoki/flow/state/${apiKey}`,\n\tSESSION: `@enoki/flow/session/${apiKey}`,\n});\n\nexport class EnokiFlow {\n\t#storageKeys: { STATE: string; SESSION: string };\n\t#enokiClient: EnokiClient;\n\t#encryption: Encryption;\n\t#encryptionKey: string;\n\t#store: SyncStore;\n\n\t$zkLoginSession: WritableAtom<{ initialized: boolean; value: ZkLoginSession | null }>;\n\t$zkLoginState: WritableAtom<ZkLoginState>;\n\n\tconstructor(config: EnokiFlowConfig) {\n\t\tthis.#enokiClient = new EnokiClient({\n\t\t\tapiKey: config.apiKey,\n\t\t\tapiUrl: config.apiUrl,\n\t\t});\n\t\tthis.#encryptionKey = config.apiKey;\n\t\tthis.#encryption = config.encryption ?? createDefaultEncryption();\n\t\tthis.#store = config.store ?? createSessionStorage();\n\t\tthis.#storageKeys = createStorageKeys(config.apiKey);\n\n\t\tlet storedState = null;\n\t\ttry {\n\t\t\tconst rawStoredValue = this.#store.get(this.#storageKeys.STATE);\n\t\t\tif (rawStoredValue) {\n\t\t\t\tstoredState = JSON.parse(rawStoredValue);\n\t\t\t}\n\t\t} catch {\n\t\t\t// Ignore errors\n\t\t}\n\n\t\tthis.$zkLoginState = atom(storedState || {});\n\t\tthis.$zkLoginSession = atom({ initialized: false, value: null });\n\n\t\t// Hydrate the session on mount:\n\t\tonMount(this.$zkLoginSession, () => {\n\t\t\tthis.getSession();\n\t\t});\n\n\t\tonSet(this.$zkLoginState, ({ newValue }) => {\n\t\t\tthis.#store.set(this.#storageKeys.STATE, JSON.stringify(newValue));\n\t\t});\n\t}\n\n\tget enokiClient() {\n\t\treturn this.#enokiClient;\n\t}\n\n\tasync createAuthorizationURL(input: {\n\t\tprovider: AuthProvider;\n\t\tclientId: string;\n\t\tredirectUrl: string;\n\t\tnetwork?: 'mainnet' | 'testnet' | 'devnet';\n\t\textraParams?: Record<string, unknown>;\n\t}) {\n\t\tconst ephemeralKeyPair = new Ed25519Keypair();\n\t\tconst { nonce, randomness, maxEpoch, estimatedExpiration } =\n\t\t\tawait this.#enokiClient.createZkLoginNonce({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tephemeralPublicKey: ephemeralKeyPair.getPublicKey(),\n\t\t\t});\n\n\t\tconst params = new URLSearchParams({\n\t\t\t...input.extraParams,\n\t\t\tnonce,\n\t\t\tclient_id: input.clientId,\n\t\t\tredirect_uri: input.redirectUrl,\n\t\t\tresponse_type: 'id_token',\n\t\t\t// TODO: Eventually fetch the scopes for this client ID from the Enoki service:\n\t\t\tscope: [\n\t\t\t\t'openid',\n\t\t\t\t// Merge the requested scopes in with the required openid scopes:\n\t\t\t\t...(input.extraParams && 'scope' in input.extraParams\n\t\t\t\t\t? (input.extraParams.scope as string[])\n\t\t\t\t\t: []),\n\t\t\t]\n\t\t\t\t.filter(Boolean)\n\t\t\t\t.join(' '),\n\t\t});\n\n\t\tlet oauthUrl: string;\n\t\tswitch (input.provider) {\n\t\t\tcase 'google': {\n\t\t\t\toauthUrl = `https://accounts.google.com/o/oauth2/v2/auth?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase 'facebook': {\n\t\t\t\toauthUrl = `https://www.facebook.com/v17.0/dialog/oauth?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase 'twitch': {\n\t\t\t\tparams.set('force_verify', 'true');\n\t\t\t\toauthUrl = `https://id.twitch.tv/oauth2/authorize?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Invalid provider: ${input.provider}`);\n\t\t}\n\n\t\tthis.$zkLoginState.set({ provider: input.provider });\n\t\tawait this.#setSession({\n\t\t\texpiresAt: estimatedExpiration,\n\t\t\tmaxEpoch,\n\t\t\trandomness,\n\t\t\tephemeralKeyPair: toB64(decodeSuiPrivateKey(ephemeralKeyPair.getSecretKey()).secretKey),\n\t\t});\n\n\t\treturn oauthUrl;\n\t}\n\n\t// TODO: Should our SDK manage this automatically in addition to exposing a method?\n\tasync handleAuthCallback(hash: string = window.location.hash) {\n\t\tconst params = new URLSearchParams(hash.startsWith('#') ? hash.slice(1) : hash);\n\n\t\t// Before we handle the auth redirect and get the state, we need to restore it:\n\t\tconst zkp = await this.getSession();\n\n\t\tif (!zkp || !zkp.ephemeralKeyPair || !zkp.maxEpoch || !zkp.randomness) {\n\t\t\tthrow new Error(\n\t\t\t\t'Start of sign-in flow could not be found. Ensure you have started the sign-in flow before calling this.',\n\t\t\t);\n\t\t}\n\n\t\tconst jwt = params.get('id_token');\n\t\tif (!jwt) {\n\t\t\tthrow new Error('Missing ID Token');\n\t\t}\n\n\t\tconst decodedJwt = decodeJwt(jwt);\n\t\tif (!decodedJwt.sub || !decodedJwt.aud || typeof decodedJwt.aud !== 'string') {\n\t\t\tthrow new Error('Missing JWT data');\n\t\t}\n\n\t\tconst { address, salt } = await this.#enokiClient.getZkLogin({ jwt });\n\n\t\tthis.$zkLoginState.set({\n\t\t\t...this.$zkLoginState.get(),\n\t\t\tsalt,\n\t\t\taddress,\n\t\t});\n\t\tawait this.#setSession({\n\t\t\t...zkp,\n\t\t\tjwt,\n\t\t});\n\n\t\treturn params.get('state');\n\t}\n\n\tasync #setSession(newValue: ZkLoginSession | null) {\n\t\tif (newValue) {\n\t\t\tconst storedValue = await this.#encryption.encrypt(\n\t\t\t\tthis.#encryptionKey,\n\t\t\t\tJSON.stringify(newValue),\n\t\t\t);\n\n\t\t\tthis.#store.set(this.#storageKeys.SESSION, storedValue);\n\t\t} else {\n\t\t\tthis.#store.delete(this.#storageKeys.SESSION);\n\t\t}\n\n\t\tthis.$zkLoginSession.set({ initialized: true, value: newValue });\n\t}\n\n\tasync getSession() {\n\t\tif (this.$zkLoginSession.get().initialized) {\n\t\t\treturn this.$zkLoginSession.get().value;\n\t\t}\n\n\t\ttry {\n\t\t\tconst storedValue = this.#store.get(this.#storageKeys.SESSION);\n\t\t\tif (!storedValue) return null;\n\n\t\t\tconst state: ZkLoginSession = JSON.parse(\n\t\t\t\tawait this.#encryption.decrypt(this.#encryptionKey, storedValue),\n\t\t\t);\n\n\t\t\t// TODO: Rather than having expiration act as a logout, we should keep the state that still is relevant,\n\t\t\t// and just clear out the expired session, but keep the other zkLogin state.\n\t\t\tif (state?.expiresAt && Date.now() > state.expiresAt) {\n\t\t\t\tawait this.logout();\n\t\t\t} else {\n\t\t\t\tthis.$zkLoginSession.set({ initialized: true, value: state });\n\t\t\t}\n\t\t} catch {\n\t\t\tthis.$zkLoginSession.set({ initialized: true, value: null });\n\t\t}\n\n\t\treturn this.$zkLoginSession.get().value;\n\t}\n\n\tasync logout() {\n\t\tthis.$zkLoginState.set({});\n\t\tthis.#store.delete(this.#storageKeys.STATE);\n\n\t\tawait this.#setSession(null);\n\t}\n\n\t// TODO: Should this return the proof if it already exists?\n\tasync getProof({ network }: { network?: 'mainnet' | 'testnet' } = {}) {\n\t\tconst zkp = await this.getSession();\n\t\tconst { salt } = this.$zkLoginState.get();\n\n\t\tif (zkp?.proof) {\n\t\t\tif (zkp.expiresAt && Date.now() > zkp.expiresAt) {\n\t\t\t\tthrow new Error('Stored proof is expired.');\n\t\t\t}\n\n\t\t\treturn zkp.proof;\n\t\t}\n\n\t\tif (!salt || !zkp || !zkp.jwt) {\n\t\t\tthrow new Error('Missing required parameters for proof generation');\n\t\t}\n\n\t\tconst ephemeralKeyPair = Ed25519Keypair.fromSecretKey(fromB64(zkp.ephemeralKeyPair));\n\n\t\tconst proof = await this.#enokiClient.createZkLoginZkp({\n\t\t\tnetwork,\n\t\t\tjwt: zkp.jwt,\n\t\t\tmaxEpoch: zkp.maxEpoch,\n\t\t\trandomness: zkp.randomness,\n\t\t\tephemeralPublicKey: ephemeralKeyPair.getPublicKey(),\n\t\t});\n\n\t\tawait this.#setSession({\n\t\t\t...zkp,\n\t\t\tproof,\n\t\t});\n\n\t\treturn proof;\n\t}\n\n\tasync getKeypair({ network }: { network?: 'mainnet' | 'testnet' } = {}) {\n\t\t// Get the proof, so that we ensure it exists in state:\n\t\tawait this.getProof({ network });\n\n\t\tconst zkp = await this.getSession();\n\n\t\t// Check to see if we have the essentials for a keypair:\n\t\tconst { address } = this.$zkLoginState.get();\n\t\tif (!address || !zkp || !zkp.proof) {\n\t\t\tthrow new Error('Missing required data for keypair generation.');\n\t\t}\n\n\t\tif (Date.now() > zkp.expiresAt) {\n\t\t\tthrow new Error('Stored proof is expired.');\n\t\t}\n\n\t\treturn new EnokiKeypair({\n\t\t\taddress,\n\t\t\tmaxEpoch: zkp.maxEpoch,\n\t\t\tproof: zkp.proof,\n\t\t\tephemeralKeypair: Ed25519Keypair.fromSecretKey(fromB64(zkp.ephemeralKeyPair)),\n\t\t});\n\t}\n\n\tasync sponsorTransactionBlock({\n\t\tnetwork,\n\t\ttransactionBlock,\n\t\tclient,\n\t}: {\n\t\tnetwork?: 'mainnet' | 'testnet';\n\t\ttransactionBlock: TransactionBlock;\n\t\tclient: SuiClient;\n\t}) {\n\t\tconst session = await this.getSession();\n\n\t\tif (!session || !session.jwt) {\n\t\t\tthrow new Error('Missing required data for sponsorship.');\n\t\t}\n\n\t\tconst transactionBlockKindBytes = await transactionBlock.build({\n\t\t\tonlyTransactionKind: true,\n\t\t\tclient,\n\t\t\t// Theses limits will get verified during the final transaction construction, so we can safely ignore them here:\n\t\t\tlimits: {\n\t\t\t\tmaxGasObjects: Infinity,\n\t\t\t\tmaxPureArgumentSize: Infinity,\n\t\t\t\tmaxTxGas: Infinity,\n\t\t\t\tmaxTxSizeBytes: Infinity,\n\t\t\t},\n\t\t});\n\n\t\treturn await this.#enokiClient.createSponsoredTransactionBlock({\n\t\t\tjwt: session.jwt,\n\t\t\tnetwork,\n\t\t\ttransactionBlockKindBytes: toB64(transactionBlockKindBytes),\n\t\t});\n\t}\n\n\tasync executeTransactionBlock({\n\t\tnetwork,\n\t\tbytes,\n\t\tdigest,\n\t\tclient,\n\t}: {\n\t\tnetwork?: 'mainnet' | 'testnet';\n\t\tbytes: string;\n\t\tdigest: string;\n\t\tclient: SuiClient;\n\t}) {\n\t\tconst keypair = await this.getKeypair({ network });\n\t\tconst userSignature = await keypair.signTransactionBlock(fromB64(bytes));\n\n\t\tawait this.#enokiClient.executeSponsoredTransactionBlock({\n\t\t\tdigest,\n\t\t\tsignature: userSignature.signature,\n\t\t});\n\n\t\t// TODO: Should the parent just do this?\n\t\tawait client.waitForTransactionBlock({ digest });\n\n\t\treturn { digest };\n\t}\n\n\tasync sponsorAndExecuteTransactionBlock({\n\t\tnetwork,\n\t\ttransactionBlock,\n\t\tclient,\n\t}: {\n\t\tnetwork?: 'mainnet' | 'testnet';\n\t\ttransactionBlock: TransactionBlock;\n\t\tclient: SuiClient;\n\t}) {\n\t\tconst { bytes, digest } = await this.sponsorTransactionBlock({\n\t\t\tnetwork,\n\t\t\ttransactionBlock,\n\t\t\tclient,\n\t\t});\n\t\treturn await this.executeTransactionBlock({ network, bytes, digest, client });\n\t}\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAIA,SAAS,2BAA2B;AACpC,SAAS,sBAAsB;AAE/B,SAAS,SAA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AW;AAC/B,YAAM,IAAI,MAAM,0BAA0B;AAAA,IAC3C;AAEA,WAAO,IAAI,aAAa;AAAA,MACvB;AAAA,MACA,UAAU,IAAI;AAAA,MACd,OAAO,IAAI;AAAA,MACX,kBAAkB,eAAe,cAAc,QAAQ,IAAI,gBAAgB,CAAC;AAAA,IAC7E,CAAC;AAAA,EACF;AAAA,EAEA,MAAM,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SuiClient } from '@mysten/sui/client';\nimport { decodeSuiPrivateKey } from '@mysten/sui/cryptography';\nimport { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';\nimport type { Transaction } from '@mysten/sui/transactions';\nimport { fromB64, toB64 } from '@mysten/sui/utils';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';\nimport { decodeJwt } from 'jose';\nimport type { WritableAtom } from 'nanostores';\nimport { atom, onMount, onSet } from 'nanostores';\n\nimport type { Encryption } from './encryption.js';\nimport { createDefaultEncryption } from './encryption.js';\nimport type { EnokiClientConfig } from './EnokiClient/index.js';\nimport { EnokiClient } from './EnokiClient/index.js';\nimport { EnokiKeypair } from './EnokiKeypair.js';\nimport type { SyncStore } from './stores.js';\nimport { createSessionStorage } from './stores.js';\n\nexport interface EnokiFlowConfig extends EnokiClientConfig {\n\t/**\n\t * The storage interface to persist Enoki data locally.\n\t * If not provided, it will use a sessionStorage-backed store.\n\t */\n\tstore?: SyncStore;\n\t/**\n\t * The encryption interface that will be used to encrypt data before storing it locally.\n\t * If not provided, it will use a default encryption interface.\n\t */\n\tencryption?: Encryption;\n}\n\n// State that is not bound to a session, and is encrypted.\nexport interface ZkLoginState {\n\tprovider?: AuthProvider;\n\taddress?: string;\n\tsalt?: string;\n}\n\n// State that session-bound, and is encrypted in storage.\nexport interface ZkLoginSession {\n\tephemeralKeyPair: string;\n\tmaxEpoch: number;\n\trandomness: string;\n\texpiresAt: number;\n\n\tjwt?: string;\n\tproof?: ZkLoginSignatureInputs;\n}\n\nexport type AuthProvider = 'google' | 'facebook' | 'twitch';\n\nconst createStorageKeys = (apiKey: string) => ({\n\tSTATE: `@enoki/flow/state/${apiKey}`,\n\tSESSION: `@enoki/flow/session/${apiKey}`,\n});\n\nexport class EnokiFlow {\n\t#storageKeys: { STATE: string; SESSION: string };\n\t#enokiClient: EnokiClient;\n\t#encryption: Encryption;\n\t#encryptionKey: string;\n\t#store: SyncStore;\n\n\t$zkLoginSession: WritableAtom<{ initialized: boolean; value: ZkLoginSession | null }>;\n\t$zkLoginState: WritableAtom<ZkLoginState>;\n\n\tconstructor(config: EnokiFlowConfig) {\n\t\tthis.#enokiClient = new EnokiClient({\n\t\t\tapiKey: config.apiKey,\n\t\t\tapiUrl: config.apiUrl,\n\t\t});\n\t\tthis.#encryptionKey = config.apiKey;\n\t\tthis.#encryption = config.encryption ?? createDefaultEncryption();\n\t\tthis.#store = config.store ?? createSessionStorage();\n\t\tthis.#storageKeys = createStorageKeys(config.apiKey);\n\n\t\tlet storedState = null;\n\t\ttry {\n\t\t\tconst rawStoredValue = this.#store.get(this.#storageKeys.STATE);\n\t\t\tif (rawStoredValue) {\n\t\t\t\tstoredState = JSON.parse(rawStoredValue);\n\t\t\t}\n\t\t} catch {\n\t\t\t// Ignore errors\n\t\t}\n\n\t\tthis.$zkLoginState = atom(storedState || {});\n\t\tthis.$zkLoginSession = atom({ initialized: false, value: null });\n\n\t\t// Hydrate the session on mount:\n\t\tonMount(this.$zkLoginSession, () => {\n\t\t\tthis.getSession();\n\t\t});\n\n\t\tonSet(this.$zkLoginState, ({ newValue }) => {\n\t\t\tthis.#store.set(this.#storageKeys.STATE, JSON.stringify(newValue));\n\t\t});\n\t}\n\n\tget enokiClient() {\n\t\treturn this.#enokiClient;\n\t}\n\n\tasync createAuthorizationURL(input: {\n\t\tprovider: AuthProvider;\n\t\tclientId: string;\n\t\tredirectUrl: string;\n\t\tnetwork?: 'mainnet' | 'testnet' | 'devnet';\n\t\textraParams?: Record<string, unknown>;\n\t}) {\n\t\tconst ephemeralKeyPair = new Ed25519Keypair();\n\t\tconst { nonce, randomness, maxEpoch, estimatedExpiration } =\n\t\t\tawait this.#enokiClient.createZkLoginNonce({\n\t\t\t\tnetwork: input.network,\n\t\t\t\tephemeralPublicKey: ephemeralKeyPair.getPublicKey(),\n\t\t\t});\n\n\t\tconst params = new URLSearchParams({\n\t\t\t...input.extraParams,\n\t\t\tnonce,\n\t\t\tclient_id: input.clientId,\n\t\t\tredirect_uri: input.redirectUrl,\n\t\t\tresponse_type: 'id_token',\n\t\t\t// TODO: Eventually fetch the scopes for this client ID from the Enoki service:\n\t\t\tscope: [\n\t\t\t\t'openid',\n\t\t\t\t// Merge the requested scopes in with the required openid scopes:\n\t\t\t\t...(input.extraParams && 'scope' in input.extraParams\n\t\t\t\t\t? (input.extraParams.scope as string[])\n\t\t\t\t\t: []),\n\t\t\t]\n\t\t\t\t.filter(Boolean)\n\t\t\t\t.join(' '),\n\t\t});\n\n\t\tlet oauthUrl: string;\n\t\tswitch (input.provider) {\n\t\t\tcase 'google': {\n\t\t\t\toauthUrl = `https://accounts.google.com/o/oauth2/v2/auth?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase 'facebook': {\n\t\t\t\toauthUrl = `https://www.facebook.com/v17.0/dialog/oauth?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase 'twitch': {\n\t\t\t\tparams.set('force_verify', 'true');\n\t\t\t\toauthUrl = `https://id.twitch.tv/oauth2/authorize?${params}`;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Invalid provider: ${input.provider}`);\n\t\t}\n\n\t\tthis.$zkLoginState.set({ provider: input.provider });\n\t\tawait this.#setSession({\n\t\t\texpiresAt: estimatedExpiration,\n\t\t\tmaxEpoch,\n\t\t\trandomness,\n\t\t\tephemeralKeyPair: toB64(decodeSuiPrivateKey(ephemeralKeyPair.getSecretKey()).secretKey),\n\t\t});\n\n\t\treturn oauthUrl;\n\t}\n\n\t// TODO: Should our SDK manage this automatically in addition to exposing a method?\n\tasync handleAuthCallback(hash: string = window.location.hash) {\n\t\tconst params = new URLSearchParams(hash.startsWith('#') ? hash.slice(1) : hash);\n\n\t\t// Before we handle the auth redirect and get the state, we need to restore it:\n\t\tconst zkp = await this.getSession();\n\n\t\tif (!zkp || !zkp.ephemeralKeyPair || !zkp.maxEpoch || !zkp.randomness) {\n\t\t\tthrow new Error(\n\t\t\t\t'Start of sign-in flow could not be found. Ensure you have started the sign-in flow before calling this.',\n\t\t\t);\n\t\t}\n\n\t\tconst jwt = params.get('id_token');\n\t\tif (!jwt) {\n\t\t\tthrow new Error('Missing ID Token');\n\t\t}\n\n\t\tconst decodedJwt = decodeJwt(jwt);\n\t\tif (!decodedJwt.sub || !decodedJwt.aud || typeof decodedJwt.aud !== 'string') {\n\t\t\tthrow new Error('Missing JWT data');\n\t\t}\n\n\t\tconst { address, salt } = await this.#enokiClient.getZkLogin({ jwt });\n\n\t\tthis.$zkLoginState.set({\n\t\t\t...this.$zkLoginState.get(),\n\t\t\tsalt,\n\t\t\taddress,\n\t\t});\n\t\tawait this.#setSession({\n\t\t\t...zkp,\n\t\t\tjwt,\n\t\t});\n\n\t\treturn params.get('state');\n\t}\n\n\tasync #setSession(newValue: ZkLoginSession | null) {\n\t\tif (newValue) {\n\t\t\tconst storedValue = await this.#encryption.encrypt(\n\t\t\t\tthis.#encryptionKey,\n\t\t\t\tJSON.stringify(newValue),\n\t\t\t);\n\n\t\t\tthis.#store.set(this.#storageKeys.SESSION, storedValue);\n\t\t} else {\n\t\t\tthis.#store.delete(this.#storageKeys.SESSION);\n\t\t}\n\n\t\tthis.$zkLoginSession.set({ initialized: true, value: newValue });\n\t}\n\n\tasync getSession() {\n\t\tif (this.$zkLoginSession.get().initialized) {\n\t\t\treturn this.$zkLoginSession.get().value;\n\t\t}\n\n\t\ttry {\n\t\t\tconst storedValue = this.#store.get(this.#storageKeys.SESSION);\n\t\t\tif (!storedValue) return null;\n\n\t\t\tconst state: ZkLoginSession = JSON.parse(\n\t\t\t\tawait this.#encryption.decrypt(this.#encryptionKey, storedValue),\n\t\t\t);\n\n\t\t\t// TODO: Rather than having expiration act as a logout, we should keep the state that still is relevant,\n\t\t\t// and just clear out the expired session, but keep the other zkLogin state.\n\t\t\tif (state?.expiresAt && Date.now() > state.expiresAt) {\n\t\t\t\tawait this.logout();\n\t\t\t} else {\n\t\t\t\tthis.$zkLoginSession.set({ initialized: true, value: state });\n\t\t\t}\n\t\t} catch {\n\t\t\tthis.$zkLoginSession.set({ initialized: true, value: null });\n\t\t}\n\n\t\treturn this.$zkLoginSession.get().value;\n\t}\n\n\tasync logout() {\n\t\tthis.$zkLoginState.set({});\n\t\tthis.#store.delete(this.#storageKeys.STATE);\n\n\t\tawait this.#setSession(null);\n\t}\n\n\t// TODO: Should this return the proof if it already exists?\n\tasync getProof({ network }: { network?: 'mainnet' | 'testnet' } = {}) {\n\t\tconst zkp = await this.getSession();\n\t\tconst { salt } = this.$zkLoginState.get();\n\n\t\tif (zkp?.proof) {\n\t\t\tif (zkp.expiresAt && Date.now() > zkp.expiresAt) {\n\t\t\t\tthrow new Error('Stored proof is expired.');\n\t\t\t}\n\n\t\t\treturn zkp.proof;\n\t\t}\n\n\t\tif (!salt || !zkp || !zkp.jwt) {\n\t\t\tthrow new Error('Missing required parameters for proof generation');\n\t\t}\n\n\t\tconst ephemeralKeyPair = Ed25519Keypair.fromSecretKey(fromB64(zkp.ephemeralKeyPair));\n\n\t\tconst proof = await this.#enokiClient.createZkLoginZkp({\n\t\t\tnetwork,\n\t\t\tjwt: zkp.jwt,\n\t\t\tmaxEpoch: zkp.maxEpoch,\n\t\t\trandomness: zkp.randomness,\n\t\t\tephemeralPublicKey: ephemeralKeyPair.getPublicKey(),\n\t\t});\n\n\t\tawait this.#setSession({\n\t\t\t...zkp,\n\t\t\tproof,\n\t\t});\n\n\t\treturn proof;\n\t}\n\n\tasync getKeypair({ network }: { network?: 'mainnet' | 'testnet' } = {}) {\n\t\t// Get the proof, so that we ensure it exists in state:\n\t\tawait this.getProof({ network });\n\n\t\tconst zkp = await this.getSession();\n\n\t\t// Check to see if we have the essentials for a keypair:\n\t\tconst { address } = this.$zkLoginState.get();\n\t\tif (!address || !zkp || !zkp.proof) {\n\t\t\tthrow new Error('Missing required data for keypair generation.');\n\t\t}\n\n\t\tif (Date.now() > zkp.expiresAt) {\n\t\t\tthrow new Error('Stored proof is expired.');\n\t\t}\n\n\t\treturn new EnokiKeypair({\n\t\t\taddress,\n\t\t\tmaxEpoch: zkp.maxEpoch,\n\t\t\tproof: zkp.proof,\n\t\t\tephemeralKeypair: Ed25519Keypair.fromSecretKey(fromB64(zkp.ephemeralKeyPair)),\n\t\t});\n\t}\n\n\tasync sponsorTransaction({\n\t\tnetwork,\n\t\ttransaction,\n\t\tclient,\n\t}: {\n\t\tnetwork?: 'mainnet' | 'testnet';\n\t\ttransaction: Transaction;\n\t\tclient: SuiClient;\n\t}) {\n\t\tconst session = await this.getSession();\n\n\t\tif (!session || !session.jwt) {\n\t\t\tthrow new Error('Missing required data for sponsorship.');\n\t\t}\n\n\t\tconst transactionKindBytes = await transaction.build({\n\t\t\tonlyTransactionKind: true,\n\t\t\tclient,\n\t\t});\n\n\t\treturn await this.#enokiClient.createSponsoredTransaction({\n\t\t\tjwt: session.jwt,\n\t\t\tnetwork,\n\t\t\ttransactionKindBytes: toB64(transactionKindBytes),\n\t\t});\n\t}\n\n\tasync executeTransaction({\n\t\tnetwork,\n\t\tbytes,\n\t\tdigest,\n\t\tclient,\n\t}: {\n\t\tnetwork?: 'mainnet' | 'testnet';\n\t\tbytes: string;\n\t\tdigest: string;\n\t\tclient: SuiClient;\n\t}) {\n\t\tconst keypair = await this.getKeypair({ network });\n\t\tconst userSignature = await keypair.signTransaction(fromB64(bytes));\n\n\t\tawait this.#enokiClient.executeSponsoredTransaction({\n\t\t\tdigest,\n\t\t\tsignature: userSignature.signature,\n\t\t});\n\n\t\t// TODO: Should the parent just do this?\n\t\tawait client.waitForTransaction({ digest });\n\n\t\treturn { digest };\n\t}\n\n\tasync sponsorAndExecuteTransaction({\n\t\tnetwork,\n\t\ttransaction,\n\t\tclient,\n\t}: {\n\t\tnetwork?: 'mainnet' | 'testnet';\n\t\ttransaction: Transaction;\n\t\tclient: SuiClient;\n\t}) {\n\t\tconst { bytes, digest } = await this.sponsorTransaction({\n\t\t\tnetwork,\n\t\t\ttransaction,\n\t\t\tclient,\n\t\t});\n\t\treturn await this.executeTransaction({ network, bytes, digest, client });\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAIA,SAAS,2BAA2B;AACpC,SAAS,sBAAsB;AAE/B,SAAS,SAAS,aAAa;AAE/B,SAAS,iBAAiB;AAE1B,SAAS,MAAM,SAAS,aAAa;AAGrC,SAAS,+BAA+B;AAExC,SAAS,mBAAmB;AAC5B,SAAS,oBAAoB;AAE7B,SAAS,4BAA4B;AAmCrC,MAAM,oBAAoB,CAAC,YAAoB;AAAA,EAC9C,OAAO,qBAAqB;AAAA,EAC5B,SAAS,uBAAuB;AACjC;AAEO,MAAM,UAAU;AAAA,EAUtB,YAAY,QAAyB;AA4IrC,uBAAM;AArJN;AACA;AACA;AACA;AACA;AAMC,uBAAK,cAAe,IAAI,YAAY;AAAA,MACnC,QAAQ,OAAO;AAAA,MACf,QAAQ,OAAO;AAAA,IAChB,CAAC;AACD,uBAAK,gBAAiB,OAAO;AAC7B,uBAAK,aAAc,OAAO,cAAc,wBAAwB;AAChE,uBAAK,QAAS,OAAO,SAAS,qBAAqB;AACnD,uBAAK,cAAe,kBAAkB,OAAO,MAAM;AAEnD,QAAI,cAAc;AAClB,QAAI;AACH,YAAM,iBAAiB,mBAAK,QAAO,IAAI,mBAAK,cAAa,KAAK;AAC9D,UAAI,gBAAgB;AACnB,sBAAc,KAAK,MAAM,cAAc;AAAA,M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,EACD,GAKG;AACF,UAAM,UAAU,MAAM,KAAK,WAAW,EAAE,QAAQ,CAAC;AACjD,UAAM,gBAAgB,MAAM,QAAQ,gBAAgB,QAAQ,KAAK,CAAC;AAElE,UAAM,mBAAK,cAAa,4BAA4B;AAAA,MACnD;AAAA,MACA,WAAW,cAAc;AAAA,IAC1B,CAAC;AAGD,UAAM,OAAO,mBAAmB,EAAE,OAAO,CAAC;AAE1C,WAAO,EAAE,OAAO;AAAA,EACjB;AAAA,EAEA,MAAM,6BAA6B;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,EACD,GAIG;AACF,UAAM,EAAE,OAAO,OAAO,IAAI,MAAM,KAAK,mBAAmB;AAAA,MACvD;AAAA,MACA;AAAA,MACA;AAAA,IACD,CAAC;AACD,WAAO,MAAM,KAAK,mBAAmB,EAAE,SAAS,OAAO,QAAQ,OAAO,CAAC;AAAA,EACxE;AACD;AArUC;AACA;AACA;AACA;AACA;AAiJM;AAAA,gBAAW,eAAC,UAAiC;AAClD,MAAI,UAAU;AACb,UAAM,cAAc,MAAM,mBAAK,aAAY;AAAA,MAC1C,mBAAK;AAAA,MACL,KAAK,UAAU,QAAQ;AAAA,IACxB;AAEA,uBAAK,QAAO,IAAI,mBAAK,cAAa,SAAS,WAAW;AAAA,EACvD,OAAO;AACN,uBAAK,QAAO,OAAO,mBAAK,cAAa,OAAO;AAAA,EAC7C;AAEA,OAAK,gBAAgB,IAAI,EAAE,aAAa,MAAM,OAAO,SAAS,CAAC;AAChE;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { SignatureWithBytes } from '@mysten/sui
|
|
2
|
-
import { Keypair, PublicKey } from '@mysten/sui
|
|
3
|
-
import type { Ed25519Keypair, Ed25519PublicKey } from '@mysten/sui
|
|
4
|
-
import type { ZkLoginSignatureInputs } from '@mysten/sui
|
|
1
|
+
import type { SignatureWithBytes } from '@mysten/sui/cryptography';
|
|
2
|
+
import { Keypair, PublicKey } from '@mysten/sui/cryptography';
|
|
3
|
+
import type { Ed25519Keypair, Ed25519PublicKey } from '@mysten/sui/keypairs/ed25519';
|
|
4
|
+
import type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';
|
|
5
5
|
export declare class EnokiPublicKey extends PublicKey {
|
|
6
6
|
#private;
|
|
7
7
|
constructor(input: {
|
|
@@ -21,11 +21,10 @@ export declare class EnokiKeypair extends Keypair {
|
|
|
21
21
|
proof: ZkLoginSignatureInputs;
|
|
22
22
|
ephemeralKeypair: Ed25519Keypair;
|
|
23
23
|
});
|
|
24
|
-
signData(data: Uint8Array): Uint8Array;
|
|
25
24
|
sign(data: Uint8Array): Promise<Uint8Array>;
|
|
26
|
-
signPersonalMessage():
|
|
27
|
-
|
|
28
|
-
getKeyScheme(): import("@mysten/sui
|
|
25
|
+
signPersonalMessage(bytes: Uint8Array): Promise<SignatureWithBytes>;
|
|
26
|
+
signTransaction(bytes: Uint8Array): Promise<SignatureWithBytes>;
|
|
27
|
+
getKeyScheme(): import("@mysten/sui/cryptography").SignatureScheme;
|
|
29
28
|
getPublicKey(): EnokiPublicKey;
|
|
30
29
|
export(): never;
|
|
31
30
|
getSecretKey(): never;
|
package/dist/esm/EnokiKeypair.js
CHANGED
|
@@ -17,7 +17,7 @@ var __privateSet = (obj, member, value, setter) => {
|
|
|
17
17
|
return value;
|
|
18
18
|
};
|
|
19
19
|
var _address, _ephemeralPublicKey, _proof, _maxEpoch, _ephemeralKeypair, _publicKey;
|
|
20
|
-
import { Keypair, PublicKey, SIGNATURE_SCHEME_TO_FLAG } from "@mysten/sui
|
|
20
|
+
import { Keypair, PublicKey, SIGNATURE_SCHEME_TO_FLAG } from "@mysten/sui/cryptography";
|
|
21
21
|
import { getZkLoginSignature } from "@mysten/zklogin";
|
|
22
22
|
class EnokiPublicKey extends PublicKey {
|
|
23
23
|
constructor(input) {
|
|
@@ -57,17 +57,23 @@ class EnokiKeypair extends Keypair {
|
|
|
57
57
|
ephemeralPublicKey: input.ephemeralKeypair.getPublicKey()
|
|
58
58
|
}));
|
|
59
59
|
}
|
|
60
|
-
signData(data) {
|
|
61
|
-
return __privateGet(this, _ephemeralKeypair).signData(data);
|
|
62
|
-
}
|
|
63
60
|
async sign(data) {
|
|
64
61
|
return __privateGet(this, _ephemeralKeypair).sign(data);
|
|
65
62
|
}
|
|
66
|
-
signPersonalMessage() {
|
|
67
|
-
|
|
63
|
+
async signPersonalMessage(bytes) {
|
|
64
|
+
const { bytes: signedBytes, signature: userSignature } = await __privateGet(this, _ephemeralKeypair).signPersonalMessage(bytes);
|
|
65
|
+
const zkSignature = getZkLoginSignature({
|
|
66
|
+
inputs: __privateGet(this, _proof),
|
|
67
|
+
maxEpoch: __privateGet(this, _maxEpoch),
|
|
68
|
+
userSignature
|
|
69
|
+
});
|
|
70
|
+
return {
|
|
71
|
+
bytes: signedBytes,
|
|
72
|
+
signature: zkSignature
|
|
73
|
+
};
|
|
68
74
|
}
|
|
69
|
-
async
|
|
70
|
-
const { bytes: signedBytes, signature: userSignature } = await __privateGet(this, _ephemeralKeypair).
|
|
75
|
+
async signTransaction(bytes) {
|
|
76
|
+
const { bytes: signedBytes, signature: userSignature } = await __privateGet(this, _ephemeralKeypair).signTransaction(bytes);
|
|
71
77
|
const zkSignature = getZkLoginSignature({
|
|
72
78
|
inputs: __privateGet(this, _proof),
|
|
73
79
|
maxEpoch: __privateGet(this, _maxEpoch),
|
|
@@ -88,7 +94,7 @@ class EnokiKeypair extends Keypair {
|
|
|
88
94
|
throw new Error("EnokiKeypair does not support exporting");
|
|
89
95
|
}
|
|
90
96
|
getSecretKey() {
|
|
91
|
-
throw new Error("EnokiKeypair does not support
|
|
97
|
+
throw new Error("EnokiKeypair does not support getting the secret key");
|
|
92
98
|
}
|
|
93
99
|
}
|
|
94
100
|
_proof = new WeakMap();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/EnokiKeypair.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SignatureWithBytes } from '@mysten/sui
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAIA,SAAS,SAAS,WAAW,gCAAgC;AAG7D,SAAS,2BAA2B;AAE7B,MAAM,uBAAuB,UAAU;AAAA,EAI7C,YAAY,OAAkE;AAC7E,UAAM;AAJP;AACA;AAIC,uBAAK,UAAW,MAAM;AACtB,uBAAK,qBAAsB,MAAM;AAAA,EAClC;AAAA,EAEA,OAAe;AACd,WAAO,yBAAyB,SAAS;AAAA,EAC1C;AAAA,EAEA,eAAuB;AACtB,WAAO,mBAAK;AAAA,EACb;AAAA,EAEA,SAAgB;AACf,UAAM,IAAI,MAAM,kDAAkD;AAAA,EACnE;AAAA,EAEA,aAAyB;AACxB,WAAO,mBAAK,qBAAoB,WAAW;AAAA,EAC5C;AACD;AAxBC;AACA;AAyBM,MAAM,qBAAqB,QAAQ;AAAA,EAMzC,YAAY,OAKT;AACF,UAAM;AAXP;AACA;AACA;AACA;AASC,uBAAK,QAAS,MAAM;AACpB,uBAAK,WAAY,MAAM;AACvB,uBAAK,mBAAoB,MAAM;AAC/B,uBAAK,YAAa,IAAI,eAAe;AAAA,MACpC,SAAS,MAAM;AAAA,MACf,oBAAoB,MAAM,iBAAiB,aAAa;AAAA,IACzD,CAAC;AAAA,EACF;AAAA,EAEA,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SignatureWithBytes } from '@mysten/sui/cryptography';\nimport { Keypair, PublicKey, SIGNATURE_SCHEME_TO_FLAG } from '@mysten/sui/cryptography';\nimport type { Ed25519Keypair, Ed25519PublicKey } from '@mysten/sui/keypairs/ed25519';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';\nimport { getZkLoginSignature } from '@mysten/zklogin';\n\nexport class EnokiPublicKey extends PublicKey {\n\t#address: string;\n\t#ephemeralPublicKey: Ed25519PublicKey;\n\n\tconstructor(input: { address: string; ephemeralPublicKey: Ed25519PublicKey }) {\n\t\tsuper();\n\t\tthis.#address = input.address;\n\t\tthis.#ephemeralPublicKey = input.ephemeralPublicKey;\n\t}\n\n\tflag(): number {\n\t\treturn SIGNATURE_SCHEME_TO_FLAG['ZkLogin'];\n\t}\n\n\ttoSuiAddress(): string {\n\t\treturn this.#address;\n\t}\n\n\tverify(): never {\n\t\tthrow new Error('Verification for EnokiPublicKey is not supported');\n\t}\n\n\ttoRawBytes(): Uint8Array {\n\t\treturn this.#ephemeralPublicKey.toRawBytes();\n\t}\n}\n\nexport class EnokiKeypair extends Keypair {\n\t#proof: ZkLoginSignatureInputs;\n\t#maxEpoch: number;\n\t#ephemeralKeypair: Ed25519Keypair;\n\t#publicKey: EnokiPublicKey;\n\n\tconstructor(input: {\n\t\taddress: string;\n\t\tmaxEpoch: number;\n\t\tproof: ZkLoginSignatureInputs;\n\t\tephemeralKeypair: Ed25519Keypair;\n\t}) {\n\t\tsuper();\n\t\tthis.#proof = input.proof;\n\t\tthis.#maxEpoch = input.maxEpoch;\n\t\tthis.#ephemeralKeypair = input.ephemeralKeypair;\n\t\tthis.#publicKey = new EnokiPublicKey({\n\t\t\taddress: input.address,\n\t\t\tephemeralPublicKey: input.ephemeralKeypair.getPublicKey(),\n\t\t});\n\t}\n\n\tasync sign(data: Uint8Array) {\n\t\treturn this.#ephemeralKeypair.sign(data);\n\t}\n\n\tasync signPersonalMessage(bytes: Uint8Array): Promise<SignatureWithBytes> {\n\t\tconst { bytes: signedBytes, signature: userSignature } =\n\t\t\tawait this.#ephemeralKeypair.signPersonalMessage(bytes);\n\n\t\tconst zkSignature = getZkLoginSignature({\n\t\t\tinputs: this.#proof,\n\t\t\tmaxEpoch: this.#maxEpoch,\n\t\t\tuserSignature,\n\t\t});\n\n\t\treturn {\n\t\t\tbytes: signedBytes,\n\t\t\tsignature: zkSignature,\n\t\t};\n\t}\n\n\tasync signTransaction(bytes: Uint8Array): Promise<SignatureWithBytes> {\n\t\tconst { bytes: signedBytes, signature: userSignature } =\n\t\t\tawait this.#ephemeralKeypair.signTransaction(bytes);\n\n\t\tconst zkSignature = getZkLoginSignature({\n\t\t\tinputs: this.#proof,\n\t\t\tmaxEpoch: this.#maxEpoch,\n\t\t\tuserSignature,\n\t\t});\n\n\t\treturn {\n\t\t\tbytes: signedBytes,\n\t\t\tsignature: zkSignature,\n\t\t};\n\t}\n\n\tgetKeyScheme() {\n\t\treturn this.#ephemeralKeypair.getKeyScheme();\n\t}\n\n\tgetPublicKey() {\n\t\treturn this.#publicKey;\n\t}\n\n\texport(): never {\n\t\tthrow new Error('EnokiKeypair does not support exporting');\n\t}\n\n\tgetSecretKey(): never {\n\t\tthrow new Error('EnokiKeypair does not support getting the secret key');\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAIA,SAAS,SAAS,WAAW,gCAAgC;AAG7D,SAAS,2BAA2B;AAE7B,MAAM,uBAAuB,UAAU;AAAA,EAI7C,YAAY,OAAkE;AAC7E,UAAM;AAJP;AACA;AAIC,uBAAK,UAAW,MAAM;AACtB,uBAAK,qBAAsB,MAAM;AAAA,EAClC;AAAA,EAEA,OAAe;AACd,WAAO,yBAAyB,SAAS;AAAA,EAC1C;AAAA,EAEA,eAAuB;AACtB,WAAO,mBAAK;AAAA,EACb;AAAA,EAEA,SAAgB;AACf,UAAM,IAAI,MAAM,kDAAkD;AAAA,EACnE;AAAA,EAEA,aAAyB;AACxB,WAAO,mBAAK,qBAAoB,WAAW;AAAA,EAC5C;AACD;AAxBC;AACA;AAyBM,MAAM,qBAAqB,QAAQ;AAAA,EAMzC,YAAY,OAKT;AACF,UAAM;AAXP;AACA;AACA;AACA;AASC,uBAAK,QAAS,MAAM;AACpB,uBAAK,WAAY,MAAM;AACvB,uBAAK,mBAAoB,MAAM;AAC/B,uBAAK,YAAa,IAAI,eAAe;AAAA,MACpC,SAAS,MAAM;AAAA,MACf,oBAAoB,MAAM,iBAAiB,aAAa;AAAA,IACzD,CAAC;AAAA,EACF;AAAA,EAEA,MAAM,KAAK,MAAkB;AAC5B,WAAO,mBAAK,mBAAkB,KAAK,IAAI;AAAA,EACxC;AAAA,EAEA,MAAM,oBAAoB,OAAgD;AACzE,UAAM,EAAE,OAAO,aAAa,WAAW,cAAc,IACpD,MAAM,mBAAK,mBAAkB,oBAAoB,KAAK;AAEvD,UAAM,cAAc,oBAAoB;AAAA,MACvC,QAAQ,mBAAK;AAAA,MACb,UAAU,mBAAK;AAAA,MACf;AAAA,IACD,CAAC;AAED,WAAO;AAAA,MACN,OAAO;AAAA,MACP,WAAW;AAAA,IACZ;AAAA,EACD;AAAA,EAEA,MAAM,gBAAgB,OAAgD;AACrE,UAAM,EAAE,OAAO,aAAa,WAAW,cAAc,IACpD,MAAM,mBAAK,mBAAkB,gBAAgB,KAAK;AAEnD,UAAM,cAAc,oBAAoB;AAAA,MACvC,QAAQ,mBAAK;AAAA,MACb,UAAU,mBAAK;AAAA,MACf;AAAA,IACD,CAAC;AAED,WAAO;AAAA,MACN,OAAO;AAAA,MACP,WAAW;AAAA,IACZ;AAAA,EACD;AAAA,EAEA,eAAe;AACd,WAAO,mBAAK,mBAAkB,aAAa;AAAA,EAC5C;AAAA,EAEA,eAAe;AACd,WAAO,mBAAK;AAAA,EACb;AAAA,EAEA,SAAgB;AACf,UAAM,IAAI,MAAM,yCAAyC;AAAA,EAC1D;AAAA,EAEA,eAAsB;AACrB,UAAM,IAAI,MAAM,sDAAsD;AAAA,EACvE;AACD;AAxEC;AACA;AACA;AACA;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/encryption.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/encryption.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromB64, toB64 } from '@mysten/sui
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromB64, toB64 } from '@mysten/sui/utils';\n\n/**\n * An interface\n */\nexport interface Encryption {\n\tencrypt(password: string, data: string): Promise<string>;\n\tdecrypt(password: string, data: string): Promise<string>;\n}\n\n/**\n * Create the default encryption interface, which uses the browsers built-in crypto primitives.\n */\nexport function createDefaultEncryption(): Encryption {\n\ttype EncryptedJSON = {\n\t\tpayload: string;\n\t\tiv: string;\n\t\tsalt: string;\n\t};\n\n\tasync function keyFromPassword(password: string, salt: Uint8Array) {\n\t\tconst key = await crypto.subtle.importKey(\n\t\t\t'raw',\n\t\t\tnew TextEncoder().encode(password),\n\t\t\t{ name: 'PBKDF2' },\n\t\t\tfalse,\n\t\t\t['deriveBits', 'deriveKey'],\n\t\t);\n\n\t\tconst derivedKey = await crypto.subtle.deriveKey(\n\t\t\t{\n\t\t\t\tname: 'PBKDF2',\n\t\t\t\tsalt,\n\t\t\t\titerations: 900_000,\n\t\t\t\thash: 'SHA-256',\n\t\t\t},\n\t\t\tkey,\n\t\t\t{ name: 'AES-GCM', length: 256 },\n\t\t\tfalse,\n\t\t\t['encrypt', 'decrypt'],\n\t\t);\n\n\t\treturn { key, derivedKey };\n\t}\n\n\treturn {\n\t\tasync encrypt(password, data) {\n\t\t\tconst salt = crypto.getRandomValues(new Uint8Array(16));\n\t\t\tconst iv = crypto.getRandomValues(new Uint8Array(12));\n\n\t\t\tconst { derivedKey } = await keyFromPassword(password, salt);\n\n\t\t\tconst payload = await crypto.subtle.encrypt(\n\t\t\t\t{\n\t\t\t\t\tname: 'AES-GCM',\n\t\t\t\t\tiv,\n\t\t\t\t},\n\t\t\t\tderivedKey,\n\t\t\t\tnew TextEncoder().encode(data),\n\t\t\t);\n\n\t\t\treturn JSON.stringify({\n\t\t\t\tpayload: toB64(new Uint8Array(payload)),\n\t\t\t\tiv: toB64(iv),\n\t\t\t\tsalt: toB64(salt),\n\t\t\t} satisfies EncryptedJSON);\n\t\t},\n\t\tasync decrypt(password, data) {\n\t\t\tconst parsed = JSON.parse(data) as EncryptedJSON;\n\t\t\tif (!parsed.payload || !parsed.iv || !parsed.salt) {\n\t\t\t\tthrow new Error('Invalid encrypted data');\n\t\t\t}\n\n\t\t\tconst { derivedKey } = await keyFromPassword(password, fromB64(parsed.salt));\n\n\t\t\tconst decryptedContent = await crypto.subtle.decrypt(\n\t\t\t\t{\n\t\t\t\t\tname: 'AES-GCM',\n\t\t\t\t\tiv: fromB64(parsed.iv),\n\t\t\t\t},\n\t\t\t\tderivedKey,\n\t\t\t\tfromB64(parsed.payload),\n\t\t\t);\n\n\t\t\treturn new TextDecoder().decode(decryptedContent);\n\t\t},\n\t};\n}\n\n/**\n * Create a passthrough encryption interface, which does not encrypt or decrypt data.\n */\nexport function createPassthroughEncryption(): Encryption {\n\treturn {\n\t\tasync encrypt(_password, data) {\n\t\t\treturn data;\n\t\t},\n\t\tasync decrypt(_password, data) {\n\t\t\treturn data;\n\t\t},\n\t};\n}\n"],
|
|
5
5
|
"mappings": "AAGA,SAAS,SAAS,aAAa;AAaxB,SAAS,0BAAsC;AAOrD,iBAAe,gBAAgB,UAAkB,MAAkB;AAClE,UAAM,MAAM,MAAM,OAAO,OAAO;AAAA,MAC/B;AAAA,MACA,IAAI,YAAY,EAAE,OAAO,QAAQ;AAAA,MACjC,EAAE,MAAM,SAAS;AAAA,MACjB;AAAA,MACA,CAAC,cAAc,WAAW;AAAA,IAC3B;AAEA,UAAM,aAAa,MAAM,OAAO,OAAO;AAAA,MACtC;AAAA,QACC,MAAM;AAAA,QACN;AAAA,QACA,YAAY;AAAA,QACZ,MAAM;AAAA,MACP;AAAA,MACA;AAAA,MACA,EAAE,MAAM,WAAW,QAAQ,IAAI;AAAA,MAC/B;AAAA,MACA,CAAC,WAAW,SAAS;AAAA,IAC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