@mysten/enoki 0.4.26 → 0.5.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 CHANGED
@@ -1,5 +1,18 @@
1
1
  # @mysten/enoki
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 8baac61: remove jose dependency
8
+ - d3cff85: Update EnokiKeypair and EnokiPublicKey classes to use correct publicKey bytes
9
+
10
+ ### Patch Changes
11
+
12
+ - Updated dependencies [8baac61]
13
+ - Updated dependencies [8baac61]
14
+ - @mysten/sui@1.23.0
15
+
3
16
  ## 0.4.26
4
17
 
5
18
  ### Patch Changes
@@ -23,6 +23,7 @@ export interface GetZkLoginApiInput {
23
23
  }
24
24
  export interface GetZkLoginApiResponse {
25
25
  address: string;
26
+ publicKey: string;
26
27
  salt: string;
27
28
  }
28
29
  export interface CreateZkLoginNonceApiInput {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/EnokiClient/type.ts"],
4
- "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { PublicKey } from '@mysten/sui/cryptography';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';\n\nimport type { AuthProvider } from '../EnokiFlow.js';\n\nexport type EnokiNetwork = 'mainnet' | 'testnet' | 'devnet';\nexport type EnokiDomainNetwork = 'mainnet' | 'testnet';\nexport type EnokiSubanameStatus = 'PENDING' | 'ACTIVE';\n\nexport interface GetAppApiInput {}\nexport interface GetAppApiResponse {\n\tallowedOrigins: string[];\n\tauthenticationProviders: {\n\t\tproviderType: AuthProvider;\n\t\tclientId: string;\n\t}[];\n\tdomains: {\n\t\tnftId: string;\n\t\tname: string;\n\t\tnetwork: EnokiDomainNetwork;\n\t}[];\n}\n\nexport interface GetZkLoginApiInput {\n\tjwt: string;\n}\nexport interface GetZkLoginApiResponse {\n\taddress: string;\n\tsalt: string;\n}\n\nexport interface CreateZkLoginNonceApiInput {\n\tnetwork?: EnokiNetwork;\n\tephemeralPublicKey: PublicKey;\n\tadditionalEpochs?: number;\n}\nexport interface CreateZkLoginNonceApiResponse {\n\tnonce: string;\n\trandomness: string;\n\tepoch: number;\n\tmaxEpoch: number;\n\testimatedExpiration: number;\n}\n\nexport interface CreateZkLoginZkpApiInput {\n\tnetwork?: EnokiNetwork;\n\tjwt: string;\n\tephemeralPublicKey: PublicKey;\n\trandomness: string;\n\tmaxEpoch: number;\n}\nexport interface CreateZkLoginZkpApiResponse extends ZkLoginSignatureInputs {}\n\nexport type CreateSponsoredTransactionApiInput = {\n\tnetwork?: EnokiNetwork;\n\ttransactionKindBytes: string;\n} & (\n\t| {\n\t\t\tjwt: string;\n\t\t\tsender?: never;\n\t\t\tallowedAddresses?: never;\n\t\t\tallowedMoveCallTargets?: never;\n\t }\n\t| {\n\t\t\tsender: string;\n\t\t\tallowedAddresses?: string[];\n\t\t\tallowedMoveCallTargets?: string[];\n\t\t\tjwt?: never;\n\t }\n);\n\nexport interface CreateSponsoredTransactionApiResponse {\n\tbytes: string;\n\tdigest: string;\n}\n\nexport interface ExecuteSponsoredTransactionApiInput {\n\tdigest: string;\n\tsignature: string;\n}\n\nexport interface ExecuteSponsoredTransactionApiResponse {\n\tdigest: string;\n}\n\nexport interface GetSubnamesApiInput {\n\taddress?: string;\n\tnetwork?: EnokiDomainNetwork;\n\tdomain?: string;\n}\nexport interface GetSubnamesApiResponse {\n\tsubnames: {\n\t\tname: string;\n\t\tstatus: EnokiSubanameStatus;\n\t}[];\n}\n\nexport type CreateSubnameApiInput = {\n\tdomain: string;\n\tnetwork?: EnokiDomainNetwork;\n\tsubname: string;\n} & (\n\t| {\n\t\t\tjwt: string;\n\t\t\ttargetAddress?: never;\n\t }\n\t| {\n\t\t\ttargetAddress: string;\n\t\t\tjwt?: never;\n\t }\n);\nexport interface CreateSubnameApiResponse {\n\tname: string;\n}\n\nexport interface DeleteSubnameApiInput {\n\tdomain: string;\n\tnetwork?: EnokiDomainNetwork;\n\tsubname: string;\n}\nexport interface DeleteSubnameApiResponse {\n\tname: string;\n}\n"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { PublicKey } from '@mysten/sui/cryptography';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';\n\nimport type { AuthProvider } from '../EnokiFlow.js';\n\nexport type EnokiNetwork = 'mainnet' | 'testnet' | 'devnet';\nexport type EnokiDomainNetwork = 'mainnet' | 'testnet';\nexport type EnokiSubanameStatus = 'PENDING' | 'ACTIVE';\n\nexport interface GetAppApiInput {}\nexport interface GetAppApiResponse {\n\tallowedOrigins: string[];\n\tauthenticationProviders: {\n\t\tproviderType: AuthProvider;\n\t\tclientId: string;\n\t}[];\n\tdomains: {\n\t\tnftId: string;\n\t\tname: string;\n\t\tnetwork: EnokiDomainNetwork;\n\t}[];\n}\n\nexport interface GetZkLoginApiInput {\n\tjwt: string;\n}\nexport interface GetZkLoginApiResponse {\n\taddress: string;\n\tpublicKey: string;\n\tsalt: string;\n}\n\nexport interface CreateZkLoginNonceApiInput {\n\tnetwork?: EnokiNetwork;\n\tephemeralPublicKey: PublicKey;\n\tadditionalEpochs?: number;\n}\nexport interface CreateZkLoginNonceApiResponse {\n\tnonce: string;\n\trandomness: string;\n\tepoch: number;\n\tmaxEpoch: number;\n\testimatedExpiration: number;\n}\n\nexport interface CreateZkLoginZkpApiInput {\n\tnetwork?: EnokiNetwork;\n\tjwt: string;\n\tephemeralPublicKey: PublicKey;\n\trandomness: string;\n\tmaxEpoch: number;\n}\nexport interface CreateZkLoginZkpApiResponse extends ZkLoginSignatureInputs {}\n\nexport type CreateSponsoredTransactionApiInput = {\n\tnetwork?: EnokiNetwork;\n\ttransactionKindBytes: string;\n} & (\n\t| {\n\t\t\tjwt: string;\n\t\t\tsender?: never;\n\t\t\tallowedAddresses?: never;\n\t\t\tallowedMoveCallTargets?: never;\n\t }\n\t| {\n\t\t\tsender: string;\n\t\t\tallowedAddresses?: string[];\n\t\t\tallowedMoveCallTargets?: string[];\n\t\t\tjwt?: never;\n\t }\n);\n\nexport interface CreateSponsoredTransactionApiResponse {\n\tbytes: string;\n\tdigest: string;\n}\n\nexport interface ExecuteSponsoredTransactionApiInput {\n\tdigest: string;\n\tsignature: string;\n}\n\nexport interface ExecuteSponsoredTransactionApiResponse {\n\tdigest: string;\n}\n\nexport interface GetSubnamesApiInput {\n\taddress?: string;\n\tnetwork?: EnokiDomainNetwork;\n\tdomain?: string;\n}\nexport interface GetSubnamesApiResponse {\n\tsubnames: {\n\t\tname: string;\n\t\tstatus: EnokiSubanameStatus;\n\t}[];\n}\n\nexport type CreateSubnameApiInput = {\n\tdomain: string;\n\tnetwork?: EnokiDomainNetwork;\n\tsubname: string;\n} & (\n\t| {\n\t\t\tjwt: string;\n\t\t\ttargetAddress?: never;\n\t }\n\t| {\n\t\t\ttargetAddress: string;\n\t\t\tjwt?: never;\n\t }\n);\nexport interface CreateSubnameApiResponse {\n\tname: string;\n}\n\nexport interface DeleteSubnameApiInput {\n\tdomain: string;\n\tnetwork?: EnokiDomainNetwork;\n\tsubname: string;\n}\nexport interface DeleteSubnameApiResponse {\n\tname: string;\n}\n"],
5
5
  "mappings": ";;;;;;;;;;;;;;AAAA;AAAA;",
6
6
  "names": []
7
7
  }
@@ -32,7 +32,7 @@ module.exports = __toCommonJS(EnokiFlow_exports);
32
32
  var import_cryptography = require("@mysten/sui/cryptography");
33
33
  var import_ed25519 = require("@mysten/sui/keypairs/ed25519");
34
34
  var import_utils = require("@mysten/sui/utils");
35
- var import_jose = require("jose");
35
+ var import_zklogin = require("@mysten/sui/zklogin");
36
36
  var import_nanostores = require("nanostores");
37
37
  var import_encryption = require("./encryption.js");
38
38
  var import_EnokiClient = require("./EnokiClient/index.js");
@@ -138,10 +138,7 @@ class EnokiFlow {
138
138
  if (!jwt) {
139
139
  throw new Error("Missing ID Token");
140
140
  }
141
- const decodedJwt = (0, import_jose.decodeJwt)(jwt);
142
- if (!decodedJwt.sub || !decodedJwt.aud || typeof decodedJwt.aud !== "string") {
143
- throw new Error("Missing JWT data");
144
- }
141
+ (0, import_zklogin.decodeJwt)(jwt);
145
142
  const { address, salt } = await __privateGet(this, _enokiClient).getZkLogin({ jwt });
146
143
  this.$zkLoginState.set({
147
144
  ...this.$zkLoginState.get(),
@@ -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/client';\nimport { decodeSuiPrivateKey } from '@mysten/sui/cryptography';\nimport { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';\nimport type { Transaction } from '@mysten/sui/transactions';\nimport { fromBase64, toBase64 } 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: toBase64(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(fromBase64(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(fromBase64(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: toBase64(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(fromBase64(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;AAAA;AAAA;AAAA;AAAA;AAIA,0BAAoC;AACpC,qBAA+B;AAE/B,mBAAqC;AAErC,kBAA0B;AAE1B,wBAAqC;AAGrC,wBAAwC;AAExC,yBAA4B;AAC5B,0BAA6B;AAE7B,oBAAqC;AAnBrC;AAsDA,MAAM,oBAAoB,CAAC,YAAoB;AAAA,EAC9C,OAAO,qBAAqB,MAAM;AAAA,EAClC,SAAS,uBAAuB,MAAM;AACvC;AAEO,MAAM,UAAU;AAAA,EAUtB,YAAY,QAAyB;AAV/B;AACN;AACA;AACA;AACA;AACA;AAMC,uBAAK,cAAe,IAAI,+BAAY;AAAA,MACnC,QAAQ,OAAO;AAAA,MACf,QAAQ,OAAO;AAAA,IAChB,CAAC;AACD,uBAAK,gBAAiB,OAAO;AAC7B,uBAAK,aAAc,OAAO,kBAAc,2CAAwB;AAChE,uBAAK,QAAS,OAAO,aAAS,oCAAqB;AACnD,uBAAK,cAAe,kBAAkB,OAAO,MAAM;AAEnD,QAAI,cAAc;AAClB,QAAI;AACH,YAAM,iBAAiB,mBAAK,QAAO,IAAI,mBAAK,cAAa,K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B;AAAA,IACpD,CAAC;AAAA,EACF;AAAA,EAEA,MAAM,mBAAmB;AAAA,IACxB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,GAKG;AACF,UAAM,UAAU,MAAM,KAAK,WAAW,EAAE,QAAQ,CAAC;AACjD,UAAM,gBAAgB,MAAM,QAAQ,oBAAgB,yBAAW,KAAK,CAAC;AAErE,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;AALM;AAsJA,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;",
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 { fromBase64, toBase64 } from '@mysten/sui/utils';\nimport { decodeJwt } from '@mysten/sui/zklogin';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';\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: toBase64(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\tdecodeJwt(jwt);\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(fromBase64(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(fromBase64(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: toBase64(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(fromBase64(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;AAAA;AAAA;AAAA;AAAA;AAIA,0BAAoC;AACpC,qBAA+B;AAE/B,mBAAqC;AACrC,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AAM,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,19 +1,11 @@
1
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';
2
+ import { Signer } from '@mysten/sui/cryptography';
3
+ import type { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
4
4
  import type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';
5
- export declare class EnokiPublicKey extends PublicKey {
6
- #private;
7
- constructor(input: {
8
- address: string;
9
- ephemeralPublicKey: Ed25519PublicKey;
10
- });
11
- flag(): number;
12
- toSuiAddress(): string;
13
- verify(): never;
14
- toRawBytes(): Uint8Array;
5
+ import { ZkLoginPublicIdentifier } from '@mysten/sui/zklogin';
6
+ export declare class EnokiPublicKey extends ZkLoginPublicIdentifier {
15
7
  }
16
- export declare class EnokiKeypair extends Keypair {
8
+ export declare class EnokiKeypair extends Signer {
17
9
  #private;
18
10
  constructor(input: {
19
11
  address: string;
@@ -26,6 +18,4 @@ export declare class EnokiKeypair extends Keypair {
26
18
  signTransaction(bytes: Uint8Array): Promise<SignatureWithBytes>;
27
19
  getKeyScheme(): import("@mysten/sui/cryptography").SignatureScheme;
28
20
  getPublicKey(): EnokiPublicKey;
29
- export(): never;
30
- getSecretKey(): never;
31
21
  }
@@ -31,31 +31,10 @@ __export(EnokiKeypair_exports, {
31
31
  module.exports = __toCommonJS(EnokiKeypair_exports);
32
32
  var import_cryptography = require("@mysten/sui/cryptography");
33
33
  var import_zklogin = require("@mysten/sui/zklogin");
34
- var _address, _ephemeralPublicKey, _proof, _maxEpoch, _ephemeralKeypair, _publicKey;
35
- class EnokiPublicKey extends import_cryptography.PublicKey {
36
- constructor(input) {
37
- super();
38
- __privateAdd(this, _address);
39
- __privateAdd(this, _ephemeralPublicKey);
40
- __privateSet(this, _address, input.address);
41
- __privateSet(this, _ephemeralPublicKey, input.ephemeralPublicKey);
42
- }
43
- flag() {
44
- return import_cryptography.SIGNATURE_SCHEME_TO_FLAG["ZkLogin"];
45
- }
46
- toSuiAddress() {
47
- return __privateGet(this, _address);
48
- }
49
- verify() {
50
- throw new Error("Verification for EnokiPublicKey is not supported");
51
- }
52
- toRawBytes() {
53
- return __privateGet(this, _ephemeralPublicKey).toRawBytes();
54
- }
34
+ var _proof, _maxEpoch, _ephemeralKeypair, _publicKey;
35
+ class EnokiPublicKey extends import_zklogin.ZkLoginPublicIdentifier {
55
36
  }
56
- _address = new WeakMap();
57
- _ephemeralPublicKey = new WeakMap();
58
- class EnokiKeypair extends import_cryptography.Keypair {
37
+ class EnokiKeypair extends import_cryptography.Signer {
59
38
  constructor(input) {
60
39
  super();
61
40
  __privateAdd(this, _proof);
@@ -65,10 +44,7 @@ class EnokiKeypair extends import_cryptography.Keypair {
65
44
  __privateSet(this, _proof, input.proof);
66
45
  __privateSet(this, _maxEpoch, input.maxEpoch);
67
46
  __privateSet(this, _ephemeralKeypair, input.ephemeralKeypair);
68
- __privateSet(this, _publicKey, new EnokiPublicKey({
69
- address: input.address,
70
- ephemeralPublicKey: input.ephemeralKeypair.getPublicKey()
71
- }));
47
+ __privateSet(this, _publicKey, EnokiPublicKey.fromProof(input.address, input.proof));
72
48
  }
73
49
  async sign(data) {
74
50
  return __privateGet(this, _ephemeralKeypair).sign(data);
@@ -103,12 +79,6 @@ class EnokiKeypair extends import_cryptography.Keypair {
103
79
  getPublicKey() {
104
80
  return __privateGet(this, _publicKey);
105
81
  }
106
- export() {
107
- throw new Error("EnokiKeypair does not support exporting");
108
- }
109
- getSecretKey() {
110
- throw new Error("EnokiKeypair does not support getting the secret key");
111
- }
112
82
  }
113
83
  _proof = new WeakMap();
114
84
  _maxEpoch = 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/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/sui/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;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,0BAA6D;AAG7D,qBAAoC;AAPpC;AASO,MAAM,uBAAuB,8BAAU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AAA,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;",
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SignatureWithBytes } from '@mysten/sui/cryptography';\nimport { Signer } from '@mysten/sui/cryptography';\nimport type { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';\nimport { getZkLoginSignature, ZkLoginPublicIdentifier } from '@mysten/sui/zklogin';\n\nexport class EnokiPublicKey extends ZkLoginPublicIdentifier {}\n\nexport class EnokiKeypair extends Signer {\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 = EnokiPublicKey.fromProof(input.address, input.proof);\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"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,0BAAuB;AAGvB,qBAA6D;AAP7D;AASO,MAAM,uBAAuB,uCAAwB;AAAC;AAEtD,MAAM,qBAAqB,2BAAO;AAAA,EAMxC,YAAY,OAKT;AACF,UAAM;AAXP;AACA;AACA;AACA;AASC,uBAAK,QAAS,MAAM;AACpB,uBAAK,WAAY,MAAM;AACvB,uBAAK,mBAAoB,MAAM;AAC/B,uBAAK,YAAa,eAAe,UAAU,MAAM,SAAS,MAAM,KAAK;AAAA,EACtE;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,kBAAc,oCAAoB;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,kBAAc,oCAAoB;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;AACD;AA7DC;AACA;AACA;AACA;",
6
6
  "names": []
7
7
  }
@@ -23,6 +23,7 @@ export interface GetZkLoginApiInput {
23
23
  }
24
24
  export interface GetZkLoginApiResponse {
25
25
  address: string;
26
+ publicKey: string;
26
27
  salt: string;
27
28
  }
28
29
  export interface CreateZkLoginNonceApiInput {
@@ -10,7 +10,7 @@ var _storageKeys, _enokiClient, _encryption, _encryptionKey, _store, _EnokiFlow_
10
10
  import { decodeSuiPrivateKey } from "@mysten/sui/cryptography";
11
11
  import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
12
12
  import { fromBase64, toBase64 } from "@mysten/sui/utils";
13
- import { decodeJwt } from "jose";
13
+ import { decodeJwt } from "@mysten/sui/zklogin";
14
14
  import { atom, onMount, onSet } from "nanostores";
15
15
  import { createDefaultEncryption } from "./encryption.js";
16
16
  import { EnokiClient } from "./EnokiClient/index.js";
@@ -115,10 +115,7 @@ class EnokiFlow {
115
115
  if (!jwt) {
116
116
  throw new Error("Missing ID Token");
117
117
  }
118
- const decodedJwt = decodeJwt(jwt);
119
- if (!decodedJwt.sub || !decodedJwt.aud || typeof decodedJwt.aud !== "string") {
120
- throw new Error("Missing JWT data");
121
- }
118
+ decodeJwt(jwt);
122
119
  const { address, salt } = await __privateGet(this, _enokiClient).getZkLogin({ jwt });
123
120
  this.$zkLoginState.set({
124
121
  ...this.$zkLoginState.get(),
@@ -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/client';\nimport { decodeSuiPrivateKey } from '@mysten/sui/cryptography';\nimport { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';\nimport type { Transaction } from '@mysten/sui/transactions';\nimport { fromBase64, toBase64 } 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: toBase64(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(fromBase64(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(fromBase64(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: toBase64(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(fromBase64(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,YAAY,gBAAgB;AAErC,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,MAAM;AAAA,EAClC,SAAS,uBAAuB,MAAM;AACvC;AAEO,MAAM,UAAU;AAAA,EAUtB,YAAY,QAAyB;AAV/B;AACN;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,MACxC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ACA;AAAA,IACA;AAAA,EACD,GAKG;AACF,UAAM,UAAU,MAAM,KAAK,WAAW,EAAE,QAAQ,CAAC;AACjD,UAAM,gBAAgB,MAAM,QAAQ,gBAAgB,WAAW,KAAK,CAAC;AAErE,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;AALM;AAsJA,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;",
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 { fromBase64, toBase64 } from '@mysten/sui/utils';\nimport { decodeJwt } from '@mysten/sui/zklogin';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';\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: toBase64(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\tdecodeJwt(jwt);\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(fromBase64(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(fromBase64(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: toBase64(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(fromBase64(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,YAAY,gBAAgB;AACrC,SAAS,iBAAiB;AAG1B,SAAS,MAAM,SAAS,aAAa;AAGrC,SAAS,+BAA+B;AAExC,SAAS,mBAAmB;AAC5B,SAAS,oBAAoB;A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AEA,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,19 +1,11 @@
1
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';
2
+ import { Signer } from '@mysten/sui/cryptography';
3
+ import type { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
4
4
  import type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';
5
- export declare class EnokiPublicKey extends PublicKey {
6
- #private;
7
- constructor(input: {
8
- address: string;
9
- ephemeralPublicKey: Ed25519PublicKey;
10
- });
11
- flag(): number;
12
- toSuiAddress(): string;
13
- verify(): never;
14
- toRawBytes(): Uint8Array;
5
+ import { ZkLoginPublicIdentifier } from '@mysten/sui/zklogin';
6
+ export declare class EnokiPublicKey extends ZkLoginPublicIdentifier {
15
7
  }
16
- export declare class EnokiKeypair extends Keypair {
8
+ export declare class EnokiKeypair extends Signer {
17
9
  #private;
18
10
  constructor(input: {
19
11
  address: string;
@@ -26,6 +18,4 @@ export declare class EnokiKeypair extends Keypair {
26
18
  signTransaction(bytes: Uint8Array): Promise<SignatureWithBytes>;
27
19
  getKeyScheme(): import("@mysten/sui/cryptography").SignatureScheme;
28
20
  getPublicKey(): EnokiPublicKey;
29
- export(): never;
30
- getSecretKey(): never;
31
21
  }
@@ -5,33 +5,12 @@ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot
5
5
  var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
6
  var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
7
  var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
- var _address, _ephemeralPublicKey, _proof, _maxEpoch, _ephemeralKeypair, _publicKey;
9
- import { Keypair, PublicKey, SIGNATURE_SCHEME_TO_FLAG } from "@mysten/sui/cryptography";
10
- import { getZkLoginSignature } from "@mysten/sui/zklogin";
11
- class EnokiPublicKey extends PublicKey {
12
- constructor(input) {
13
- super();
14
- __privateAdd(this, _address);
15
- __privateAdd(this, _ephemeralPublicKey);
16
- __privateSet(this, _address, input.address);
17
- __privateSet(this, _ephemeralPublicKey, input.ephemeralPublicKey);
18
- }
19
- flag() {
20
- return SIGNATURE_SCHEME_TO_FLAG["ZkLogin"];
21
- }
22
- toSuiAddress() {
23
- return __privateGet(this, _address);
24
- }
25
- verify() {
26
- throw new Error("Verification for EnokiPublicKey is not supported");
27
- }
28
- toRawBytes() {
29
- return __privateGet(this, _ephemeralPublicKey).toRawBytes();
30
- }
8
+ var _proof, _maxEpoch, _ephemeralKeypair, _publicKey;
9
+ import { Signer } from "@mysten/sui/cryptography";
10
+ import { getZkLoginSignature, ZkLoginPublicIdentifier } from "@mysten/sui/zklogin";
11
+ class EnokiPublicKey extends ZkLoginPublicIdentifier {
31
12
  }
32
- _address = new WeakMap();
33
- _ephemeralPublicKey = new WeakMap();
34
- class EnokiKeypair extends Keypair {
13
+ class EnokiKeypair extends Signer {
35
14
  constructor(input) {
36
15
  super();
37
16
  __privateAdd(this, _proof);
@@ -41,10 +20,7 @@ class EnokiKeypair extends Keypair {
41
20
  __privateSet(this, _proof, input.proof);
42
21
  __privateSet(this, _maxEpoch, input.maxEpoch);
43
22
  __privateSet(this, _ephemeralKeypair, input.ephemeralKeypair);
44
- __privateSet(this, _publicKey, new EnokiPublicKey({
45
- address: input.address,
46
- ephemeralPublicKey: input.ephemeralKeypair.getPublicKey()
47
- }));
23
+ __privateSet(this, _publicKey, EnokiPublicKey.fromProof(input.address, input.proof));
48
24
  }
49
25
  async sign(data) {
50
26
  return __privateGet(this, _ephemeralKeypair).sign(data);
@@ -79,12 +55,6 @@ class EnokiKeypair extends Keypair {
79
55
  getPublicKey() {
80
56
  return __privateGet(this, _publicKey);
81
57
  }
82
- export() {
83
- throw new Error("EnokiKeypair does not support exporting");
84
- }
85
- getSecretKey() {
86
- throw new Error("EnokiKeypair does not support getting the secret key");
87
- }
88
58
  }
89
59
  _proof = new WeakMap();
90
60
  _maxEpoch = 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/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/sui/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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B,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;",
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { SignatureWithBytes } from '@mysten/sui/cryptography';\nimport { Signer } from '@mysten/sui/cryptography';\nimport type { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';\nimport { getZkLoginSignature, ZkLoginPublicIdentifier } from '@mysten/sui/zklogin';\n\nexport class EnokiPublicKey extends ZkLoginPublicIdentifier {}\n\nexport class EnokiKeypair extends Signer {\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 = EnokiPublicKey.fromProof(input.address, input.proof);\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"],
5
+ "mappings": ";;;;;;;AAAA;AAIA,SAAS,cAAc;AAGvB,SAAS,qBAAqB,+BAA+B;AAEtD,MAAM,uBAAuB,wBAAwB;AAAC;AAEtD,MAAM,qBAAqB,OAAO;AAAA,EAMxC,YAAY,OAKT;AACF,UAAM;AAXP;AACA;AACA;AACA;AASC,uBAAK,QAAS,MAAM;AACpB,uBAAK,WAAY,MAAM;AACvB,uBAAK,mBAAoB,MAAM;AAC/B,uBAAK,YAAa,eAAe,UAAU,MAAM,SAAS,MAAM,KAAK;AAAA,EACtE;AAAA,EAEA,MAAM,KAAK,MAAkB;AAC5B,WAAO,mBAAK,mBAAkB,KAAK,IAAI;AAAA,EACxC;AAAA,E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