@mysten/enoki 0.5.2 → 0.6.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 +24 -0
- package/dist/cjs/EnokiClient/type.d.ts +1 -1
- package/dist/cjs/EnokiClient/type.js.map +1 -1
- package/dist/cjs/EnokiFlow.d.ts +19 -27
- package/dist/cjs/EnokiFlow.js +41 -52
- package/dist/cjs/EnokiFlow.js.map +2 -2
- package/dist/cjs/EnokiKeypair.d.ts +1 -2
- package/dist/cjs/EnokiKeypair.js.map +2 -2
- package/dist/cjs/encryption.d.ts +1 -1
- package/dist/cjs/encryption.js.map +1 -1
- package/dist/cjs/index.d.ts +5 -1
- package/dist/cjs/index.js +6 -1
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/react.d.ts +8 -2
- package/dist/cjs/react.js.map +2 -2
- package/dist/cjs/utils.d.ts +2 -0
- package/dist/cjs/utils.js +27 -0
- package/dist/cjs/utils.js.map +7 -0
- package/dist/cjs/wallet/index.d.ts +34 -0
- package/dist/cjs/wallet/index.js +330 -0
- package/dist/cjs/wallet/index.js.map +7 -0
- package/dist/cjs/wallet/providers.d.ts +7 -0
- package/dist/cjs/wallet/providers.js +41 -0
- package/dist/cjs/wallet/providers.js.map +7 -0
- package/dist/cjs/wallet/state.d.ts +43 -0
- package/dist/cjs/wallet/state.js +263 -0
- package/dist/cjs/wallet/state.js.map +7 -0
- package/dist/cjs/wallet/types.d.ts +40 -0
- package/dist/cjs/wallet/types.js +17 -0
- package/dist/cjs/wallet/types.js.map +7 -0
- package/dist/esm/EnokiClient/type.d.ts +1 -1
- package/dist/esm/EnokiFlow.d.ts +19 -27
- package/dist/esm/EnokiFlow.js +41 -52
- package/dist/esm/EnokiFlow.js.map +2 -2
- package/dist/esm/EnokiKeypair.d.ts +1 -2
- package/dist/esm/EnokiKeypair.js.map +2 -2
- package/dist/esm/encryption.d.ts +1 -1
- package/dist/esm/encryption.js.map +1 -1
- package/dist/esm/index.d.ts +5 -1
- package/dist/esm/index.js +6 -1
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/react.d.ts +8 -2
- package/dist/esm/react.js.map +2 -2
- package/dist/esm/utils.d.ts +2 -0
- package/dist/esm/utils.js +7 -0
- package/dist/esm/utils.js.map +7 -0
- package/dist/esm/wallet/index.d.ts +34 -0
- package/dist/esm/wallet/index.js +300 -0
- package/dist/esm/wallet/index.js.map +7 -0
- package/dist/esm/wallet/providers.d.ts +7 -0
- package/dist/esm/wallet/providers.js +21 -0
- package/dist/esm/wallet/providers.js.map +7 -0
- package/dist/esm/wallet/state.d.ts +43 -0
- package/dist/esm/wallet/state.js +243 -0
- package/dist/esm/wallet/state.js.map +7 -0
- package/dist/esm/wallet/types.d.ts +40 -0
- package/dist/esm/wallet/types.js +1 -0
- package/dist/esm/wallet/types.js.map +7 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -3
|
@@ -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 { 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\tpublicKey?: 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, publicKey } = 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\tpublicKey,\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;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { ExportedWebCryptoKeypair } from '@mysten/signers/webcrypto';\nimport { WebCryptoSigner } from '@mysten/signers/webcrypto';\nimport { decodeSuiPrivateKey } from '@mysten/sui/cryptography';\nimport { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';\nimport { fromBase64, toBase64 } from '@mysten/sui/utils';\nimport { decodeJwt } from '@mysten/sui/zklogin';\nimport type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';\nimport type { UseStore } from 'idb-keyval';\nimport { clear, createStore, get, set } from 'idb-keyval';\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 type { AuthProvider, EnokiNetwork } from './EnokiClient/type.js';\nimport { EnokiKeypair } from './EnokiKeypair.js';\nimport type { SyncStore } from './stores.js';\nimport { createSessionStorage } from './stores.js';\n\n/**\n * @deprecated Use `RegisterEnokiWalletsOptions` instead\n */\nexport type EnokiFlowConfig = EnokiClientConfig &\n\t(\n\t\t| {\n\t\t\t\texperimental_nativeCryptoSigner?: unknown;\n\t\t\t\t/**\n\t\t\t\t * The storage interface to persist Enoki data locally.\n\t\t\t\t * If not provided, it will use a sessionStorage-backed store.\n\t\t\t\t */\n\t\t\t\tstore?: SyncStore;\n\t\t\t\t/**\n\t\t\t\t * The encryption interface that will be used to encrypt data before storing it locally.\n\t\t\t\t * If not provided, it will use a default encryption interface.\n\t\t\t\t */\n\t\t\t\tencryption?: Encryption;\n\t\t }\n\t\t| {\n\t\t\t\tstore?: never;\n\t\t\t\tencryption?: never;\n\t\t\t\t/**\n\t\t\t\t * Enables the new native crypto signer for the EnokiFlow, which is more secure.\n\t\t\t\t */\n\t\t\t\texperimental_nativeCryptoSigner: true;\n\t\t }\n\t);\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\tpublicKey?: 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\nconst createStorageKeys = (apiKey: string) => ({\n\tSTATE: `@enoki/flow/state/${apiKey}`,\n\tSESSION: `@enoki/flow/session/${apiKey}`,\n});\n\n/**\n * @deprecated Use `registerEnokiWallets` instead\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\t#useNativeCryptoSigner: boolean;\n\t#idbStore?: UseStore;\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\n\t\tif (config.experimental_nativeCryptoSigner) {\n\t\t\tthis.#useNativeCryptoSigner = true;\n\t\t\tthis.#idbStore = createStore('enoki', config.apiKey);\n\t\t} else {\n\t\t\tthis.#useNativeCryptoSigner = false;\n\t\t}\n\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 = this.#useNativeCryptoSigner\n\t\t\t? await WebCryptoSigner.generate()\n\t\t\t: new Ed25519Keypair();\n\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\tif (this.#useNativeCryptoSigner) {\n\t\t\tawait set('ephemeralKeyPair', ephemeralKeyPair, this.#idbStore);\n\t\t}\n\n\t\tawait this.#setSession({\n\t\t\texpiresAt: estimatedExpiration,\n\t\t\tmaxEpoch,\n\t\t\trandomness,\n\t\t\tephemeralKeyPair: this.#useNativeCryptoSigner\n\t\t\t\t? '@@native'\n\t\t\t\t: toBase64(\n\t\t\t\t\t\tdecodeSuiPrivateKey((ephemeralKeyPair as Ed25519Keypair).getSecretKey()).secretKey,\n\t\t\t\t\t),\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, publicKey } = 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\tpublicKey,\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\tif (this.#useNativeCryptoSigner) {\n\t\t\tawait clear(this.#idbStore);\n\t\t}\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?: EnokiNetwork } = {}) {\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\tlet storedNativeSigner: ExportedWebCryptoKeypair | undefined = undefined;\n\t\tif (this.#useNativeCryptoSigner && zkp.ephemeralKeyPair === '@@native') {\n\t\t\tstoredNativeSigner = await get('ephemeralKeyPair', this.#idbStore);\n\t\t\tif (!storedNativeSigner) {\n\t\t\t\tthrow new Error('Native signer not found in store.');\n\t\t\t}\n\t\t}\n\n\t\tconst ephemeralKeyPair =\n\t\t\tzkp.ephemeralKeyPair === '@@native'\n\t\t\t\t? WebCryptoSigner.import(storedNativeSigner!)\n\t\t\t\t: 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?: EnokiNetwork } = {}) {\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\tlet storedNativeSigner: ExportedWebCryptoKeypair | undefined = undefined;\n\t\tif (this.#useNativeCryptoSigner && zkp.ephemeralKeyPair === '@@native') {\n\t\t\tstoredNativeSigner = await get('ephemeralKeyPair', this.#idbStore);\n\t\t\tif (!storedNativeSigner) {\n\t\t\t\tthrow new Error('Native signer not found in store.');\n\t\t\t}\n\t\t}\n\n\t\tconst ephemeralKeypair =\n\t\t\tzkp.ephemeralKeyPair === '@@native'\n\t\t\t\t? WebCryptoSigner.import(storedNativeSigner!)\n\t\t\t\t: Ed25519Keypair.fromSecretKey(fromBase64(zkp.ephemeralKeyPair));\n\n\t\treturn new EnokiKeypair({\n\t\t\taddress,\n\t\t\tephemeralKeypair,\n\t\t\tmaxEpoch: zkp.maxEpoch,\n\t\t\tproof: zkp.proof,\n\t\t});\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;AAAA;AAIA,SAAS,uBAAuB;AAChC,SAAS,2BAA2B;AACpC,SAAS,sBAAsB;AAC/B,SAAS,YAAY,gBAAgB;AACrC,SAAS,iBAAiB;AAG1B,SAAS,OAAO,aAAa,KAAK,WAAW;AAE7C,SAAS,MAAM,SAAS,aAAa;AAGrC,SAAS,+BAA+B;AAExC,SAAS,mBAAmB;AAE5B,SAAS,oBAAoB;AAE7B,SAAS,4BAA4B;AAiDrC,MAAM,oBAAoB,CAAC,YAAoB;AAAA,EAC9C,OAAO,qBAAqB,MAAM;AAAA,EAClC,SAAS,uBAAuB,MAAM;AACvC;AAKO,MAAM,UAAU;AAAA,EAYtB,YAAY,QAAyB;AAZ/B;AACN;AACA;AACA;AACA;AACA;AACA;AACA;AAMC,uBAAK,cAAe,IAAI,YAAY;AAAA,MACnC,QAAQ,OAAO;AAAA,MACf,QAAQ,OAAO;AAAA,IAChB,CAAC;AACD,uBAAK,gBAAiB,OAAO;AAE7B,QAAI,OAAO,iCAAiC;AAC3C,yBAAK,wBAAyB;AAC9B,yBAAK,WAAY,YAAY,SAAS,OAAO,MAAM;AAAA,IACpD,OAAO;AACN,yBAAK,wBAAyB;AAAA,IAC/B;AAEA,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,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ACD;AAEA,UAAM,mBACL,IAAI,qBAAqB,aACtB,gBAAgB,OAAO,kBAAmB,IAC1C,eAAe,cAAc,WAAW,IAAI,gBAAgB,CAAC;AAEjE,WAAO,IAAI,aAAa;AAAA,MACvB;AAAA,MACA;AAAA,MACA,UAAU,IAAI;AAAA,MACd,OAAO,IAAI;AAAA,IACZ,CAAC;AAAA,EACF;AACD;AA9SC;AACA;AACA;AACA;AACA;AACA;AACA;AAPM;AAyKA,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,6 +1,5 @@
|
|
|
1
1
|
import type { SignatureWithBytes } from '@mysten/sui/cryptography';
|
|
2
2
|
import { Signer } from '@mysten/sui/cryptography';
|
|
3
|
-
import type { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
4
3
|
import type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';
|
|
5
4
|
import { ZkLoginPublicIdentifier } from '@mysten/sui/zklogin';
|
|
6
5
|
export declare class EnokiPublicKey extends ZkLoginPublicIdentifier {
|
|
@@ -11,7 +10,7 @@ export declare class EnokiKeypair extends Signer {
|
|
|
11
10
|
address: string;
|
|
12
11
|
maxEpoch: number;
|
|
13
12
|
proof: ZkLoginSignatureInputs;
|
|
14
|
-
ephemeralKeypair:
|
|
13
|
+
ephemeralKeypair: Signer;
|
|
15
14
|
});
|
|
16
15
|
sign(data: Uint8Array): Promise<Uint8Array<ArrayBufferLike>>;
|
|
17
16
|
signPersonalMessage(bytes: Uint8Array): Promise<SignatureWithBytes>;
|
|
@@ -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 { Signer } from '@mysten/sui/cryptography';\nimport type {
|
|
5
|
-
"mappings": ";;;;;;;AAAA;AAIA,SAAS,cAAc;
|
|
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 { 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: Signer;\n\t#publicKey: EnokiPublicKey;\n\n\tconstructor(input: {\n\t\taddress: string;\n\t\tmaxEpoch: number;\n\t\tproof: ZkLoginSignatureInputs;\n\t\tephemeralKeypair: Signer;\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;AAEvB,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,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;AACD;AA7DC;AACA;AACA;AACA;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/encryption.d.ts
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 { fromBase64, toBase64 } from '@mysten/sui/utils';\n\n/**\n *
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromBase64, toBase64 } from '@mysten/sui/utils';\n\n/**\n * A general interface for specifying how data should be encrypted and decrypted.\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: toBase64(new Uint8Array(payload)),\n\t\t\t\tiv: toBase64(iv),\n\t\t\t\tsalt: toBase64(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, fromBase64(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: fromBase64(parsed.iv),\n\t\t\t\t},\n\t\t\t\tderivedKey,\n\t\t\t\tfromBase64(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,YAAY,gBAAgB;AAa9B,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,IACtB;AAEA,WAAO,EAAE,KAAK,WAAW;AAAA,EAC1B;AAEA,SAAO;AAAA,IACN,MAAM,QAAQ,UAAU,MAAM;AAC7B,YAAM,OAAO,OAAO,gBAAgB,IAAI,WAAW,EAAE,CAAC;AACtD,YAAM,KAAK,OAAO,gBAAgB,IAAI,WAAW,EAAE,CAAC;AAEpD,YAAM,EAAE,WAAW,IAAI,MAAM,gBAAgB,UAAU,IAAI;AAE3D,YAAM,UAAU,MAAM,OAAO,OAAO;AAAA,QACnC;AAAA,UACC,MAAM;AAAA,UACN;AAAA,QACD;AAAA,QACA;AAAA,QACA,IAAI,YAAY,EAAE,OAAO,IAAI;AAAA,MAC9B;AAEA,aAAO,KAAK,UAAU;AAAA,QACrB,SAAS,SAAS,IAAI,WAAW,OAAO,CAAC;AAAA,QACzC,IAAI,SAAS,EAAE;AAAA,QACf,MAAM,SAAS,IAAI;AAAA,MACpB,CAAyB;AAAA,IAC1B;AAAA,IACA,MAAM,QAAQ,UAAU,MAAM;AAC7B,YAAM,SAAS,KAAK,MAAM,IAAI;AAC9B,UAAI,CAAC,OAAO,WAAW,CAAC,OAAO,MAAM,CAAC,OAAO,MAAM;AAClD,cAAM,IAAI,MAAM,wBAAwB;AAAA,MACzC;AAEA,YAAM,EAAE,WAAW,IAAI,MAAM,gBAAgB,UAAU,WAAW,OAAO,IAAI,CAAC;AAE9E,YAAM,mBAAmB,MAAM,OAAO,OAAO;AAAA,QAC5C;AAAA,UACC,MAAM;AAAA,UACN,IAAI,WAAW,OAAO,EAAE;AAAA,QACzB;AAAA,QACA;AAAA,QACA,WAAW,OAAO,OAAO;AAAA,MAC1B;AAEA,aAAO,IAAI,YAAY,EAAE,OAAO,gBAAgB;AAAA,IACjD;AAAA,EACD;AACD;AAKO,SAAS,8BAA0C;AACzD,SAAO;AAAA,IACN,MAAM,QAAQ,WAAW,MAAM;AAC9B,aAAO;AAAA,IACR;AAAA,IACA,MAAM,QAAQ,WAAW,MAAM;AAC9B,aAAO;AAAA,IACR;AAAA,EACD;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export { EnokiClient, type EnokiClientConfig, EnokiClientError } from './EnokiClient/index.js';
|
|
2
|
-
export {
|
|
2
|
+
export type { EnokiNetwork, AuthProvider } from './EnokiClient/type.js';
|
|
3
|
+
export { EnokiFlow, type EnokiFlowConfig } from './EnokiFlow.js';
|
|
3
4
|
export { createLocalStorage, createSessionStorage, createInMemoryStorage, type SyncStore, } from './stores.js';
|
|
4
5
|
export { createDefaultEncryption, type Encryption } from './encryption.js';
|
|
5
6
|
export { EnokiKeypair, EnokiPublicKey } from './EnokiKeypair.js';
|
|
7
|
+
export { isEnokiWallet, registerEnokiWallets, type EnokiWallet } from './wallet/index.js';
|
|
8
|
+
export { type RegisterEnokiWalletsOptions } from './wallet/types.js';
|
|
9
|
+
export { isEnokiNetwork } from './utils.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
} from "./stores.js";
|
|
8
8
|
import { createDefaultEncryption } from "./encryption.js";
|
|
9
9
|
import { EnokiKeypair, EnokiPublicKey } from "./EnokiKeypair.js";
|
|
10
|
+
import { isEnokiWallet, registerEnokiWallets } from "./wallet/index.js";
|
|
11
|
+
import { isEnokiNetwork } from "./utils.js";
|
|
10
12
|
export {
|
|
11
13
|
EnokiClient,
|
|
12
14
|
EnokiClientError,
|
|
@@ -16,6 +18,9 @@ export {
|
|
|
16
18
|
createDefaultEncryption,
|
|
17
19
|
createInMemoryStorage,
|
|
18
20
|
createLocalStorage,
|
|
19
|
-
createSessionStorage
|
|
21
|
+
createSessionStorage,
|
|
22
|
+
isEnokiNetwork,
|
|
23
|
+
isEnokiWallet,
|
|
24
|
+
registerEnokiWallets
|
|
20
25
|
};
|
|
21
26
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport { EnokiClient, type EnokiClientConfig, EnokiClientError } from './EnokiClient/index.js';\nexport {
|
|
5
|
-
"mappings": "AAGA,SAAS,aAAqC,wBAAwB;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nexport { EnokiClient, type EnokiClientConfig, EnokiClientError } from './EnokiClient/index.js';\nexport type { EnokiNetwork, AuthProvider } from './EnokiClient/type.js';\nexport { EnokiFlow, type EnokiFlowConfig } from './EnokiFlow.js';\nexport {\n\tcreateLocalStorage,\n\tcreateSessionStorage,\n\tcreateInMemoryStorage,\n\ttype SyncStore,\n} from './stores.js';\nexport { createDefaultEncryption, type Encryption } from './encryption.js';\nexport { EnokiKeypair, EnokiPublicKey } from './EnokiKeypair.js';\n\nexport { isEnokiWallet, registerEnokiWallets, type EnokiWallet } from './wallet/index.js';\nexport { type RegisterEnokiWalletsOptions } from './wallet/types.js';\n\nexport { isEnokiNetwork } from './utils.js';\n"],
|
|
5
|
+
"mappings": "AAGA,SAAS,aAAqC,wBAAwB;AAEtE,SAAS,iBAAuC;AAChD;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OAEM;AACP,SAAS,+BAAgD;AACzD,SAAS,cAAc,sBAAsB;AAE7C,SAAS,eAAe,4BAA8C;AAGtE,SAAS,sBAAsB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/react.d.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import type { ReactNode } from 'react';
|
|
2
2
|
import type { EnokiFlowConfig } from './EnokiFlow.js';
|
|
3
3
|
import { EnokiFlow } from './EnokiFlow.js';
|
|
4
|
-
|
|
4
|
+
/** @deprecated use `registerEnokiWallets` instead */
|
|
5
|
+
export type EnokiFlowProviderProps = EnokiFlowConfig & {
|
|
5
6
|
children: ReactNode;
|
|
6
|
-
}
|
|
7
|
+
};
|
|
8
|
+
/** @deprecated use `registerEnokiWallets` instead */
|
|
7
9
|
export declare function EnokiFlowProvider({ children, ...config }: EnokiFlowProviderProps): import("react/jsx-runtime.js").JSX.Element;
|
|
10
|
+
/** @deprecated use `registerEnokiWallets` and dapp-kit wallet hooks instead */
|
|
8
11
|
export declare function useEnokiFlow(): EnokiFlow;
|
|
12
|
+
/** @deprecated use `registerEnokiWallets` and dapp-kit wallet hooks instead */
|
|
9
13
|
export declare function useZkLogin(): import("./EnokiFlow.js").ZkLoginState;
|
|
14
|
+
/** @deprecated use `registerEnokiWallets` and dapp-kit wallet hooks instead */
|
|
10
15
|
export declare function useZkLoginSession(): import("./EnokiFlow.js").ZkLoginSession | null;
|
|
16
|
+
/** @deprecated use `registerEnokiWallets` and dapp-kit wallet hooks instead */
|
|
11
17
|
export declare function useAuthCallback(): {
|
|
12
18
|
handled: boolean;
|
|
13
19
|
state: string | null;
|
package/dist/esm/react.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/react.tsx"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { useStore } from '@nanostores/react';\nimport type { ReactNode } from 'react';\nimport { createContext, useContext, useEffect, useState } from 'react';\n\nimport type { EnokiFlowConfig } from './EnokiFlow.js';\nimport { EnokiFlow } from './EnokiFlow.js';\n\nconst EnokiFlowContext = createContext<EnokiFlow | null>(null);\n\nexport
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { useStore } from '@nanostores/react';\nimport type { ReactNode } from 'react';\nimport { createContext, useContext, useEffect, useState } from 'react';\n\nimport type { EnokiFlowConfig } from './EnokiFlow.js';\nimport { EnokiFlow } from './EnokiFlow.js';\n\nconst EnokiFlowContext = createContext<EnokiFlow | null>(null);\n\n/** @deprecated use `registerEnokiWallets` instead */\nexport type EnokiFlowProviderProps = EnokiFlowConfig & {\n\tchildren: ReactNode;\n};\n\n/** @deprecated use `registerEnokiWallets` instead */\nexport function EnokiFlowProvider({ children, ...config }: EnokiFlowProviderProps) {\n\tconst [enokiFlow] = useState(() => new EnokiFlow(config));\n\treturn <EnokiFlowContext.Provider value={enokiFlow}>{children}</EnokiFlowContext.Provider>;\n}\n\n/** @deprecated use `registerEnokiWallets` and dapp-kit wallet hooks instead */\nexport function useEnokiFlow() {\n\tconst context = useContext(EnokiFlowContext);\n\tif (!context) {\n\t\tthrow new Error('Missing `EnokiFlowContext` provider');\n\t}\n\treturn context;\n}\n\n/** @deprecated use `registerEnokiWallets` and dapp-kit wallet hooks instead */\nexport function useZkLogin() {\n\tconst flow = useEnokiFlow();\n\treturn useStore(flow.$zkLoginState);\n}\n\n/** @deprecated use `registerEnokiWallets` and dapp-kit wallet hooks instead */\nexport function useZkLoginSession() {\n\tconst flow = useEnokiFlow();\n\treturn useStore(flow.$zkLoginSession).value;\n}\n\n/** @deprecated use `registerEnokiWallets` and dapp-kit wallet hooks instead */\nexport function useAuthCallback() {\n\tconst flow = useEnokiFlow();\n\tconst [state, setState] = useState<string | null>(null);\n\tconst [handled, setHandled] = useState(false);\n\tconst [hash, setHash] = useState<string | null>(null);\n\n\tuseEffect(() => {\n\t\tconst listener = () => setHash(window.location.hash.slice(1).trim());\n\t\tlistener();\n\n\t\twindow.addEventListener('hashchange', listener);\n\t\treturn () => window.removeEventListener('hashchange', listener);\n\t}, []);\n\n\tuseEffect(() => {\n\t\tif (!hash) return;\n\n\t\t(async () => {\n\t\t\ttry {\n\t\t\t\tsetState(await flow.handleAuthCallback(hash));\n\n\t\t\t\twindow.location.hash = '';\n\t\t\t} finally {\n\t\t\t\tsetHandled(true);\n\t\t\t}\n\t\t})();\n\t}, [hash, flow]);\n\n\treturn { handled, state };\n}\n"],
|
|
5
|
+
"mappings": "AAoBQ;AAjBR,SAAS,gBAAgB;AAEzB,SAAS,eAAe,YAAY,WAAW,gBAAgB;AAG/D,SAAS,iBAAiB;AAE1B,MAAM,mBAAmB,cAAgC,IAAI;AAQtD,SAAS,kBAAkB,EAAE,UAAU,GAAG,OAAO,GAA2B;AAClF,QAAM,CAAC,SAAS,IAAI,SAAS,MAAM,IAAI,UAAU,MAAM,CAAC;AACxD,SAAO,oBAAC,iBAAiB,UAAjB,EAA0B,OAAO,WAAY,UAAS;AAC/D;AAGO,SAAS,eAAe;AAC9B,QAAM,UAAU,WAAW,gBAAgB;AAC3C,MAAI,CAAC,SAAS;AACb,UAAM,IAAI,MAAM,qCAAqC;AAAA,EACtD;AACA,SAAO;AACR;AAGO,SAAS,aAAa;AAC5B,QAAM,OAAO,aAAa;AAC1B,SAAO,SAAS,KAAK,aAAa;AACnC;AAGO,SAAS,oBAAoB;AACnC,QAAM,OAAO,aAAa;AAC1B,SAAO,SAAS,KAAK,eAAe,EAAE;AACvC;AAGO,SAAS,kBAAkB;AACjC,QAAM,OAAO,aAAa;AAC1B,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAwB,IAAI;AACtD,QAAM,CAAC,SAAS,UAAU,IAAI,SAAS,KAAK;AAC5C,QAAM,CAAC,MAAM,OAAO,IAAI,SAAwB,IAAI;AAEpD,YAAU,MAAM;AACf,UAAM,WAAW,MAAM,QAAQ,OAAO,SAAS,KAAK,MAAM,CAAC,EAAE,KAAK,CAAC;AACnE,aAAS;AAET,WAAO,iBAAiB,cAAc,QAAQ;AAC9C,WAAO,MAAM,OAAO,oBAAoB,cAAc,QAAQ;AAAA,EAC/D,GAAG,CAAC,CAAC;AAEL,YAAU,MAAM;AACf,QAAI,CAAC,KAAM;AAEX,KAAC,YAAY;AACZ,UAAI;AACH,iBAAS,MAAM,KAAK,mBAAmB,IAAI,CAAC;AAE5C,eAAO,SAAS,OAAO;AAAA,MACxB,UAAE;AACD,mBAAW,IAAI;AAAA,MAChB;AAAA,IACD,GAAG;AAAA,EACJ,GAAG,CAAC,MAAM,IAAI,CAAC;AAEf,SAAO,EAAE,SAAS,MAAM;AACzB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/utils.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { EnokiNetwork } from './EnokiClient/type.js';\n\nexport function isEnokiNetwork(network: string): network is EnokiNetwork {\n\treturn network === 'mainnet' || network === 'testnet' || network === 'devnet';\n}\n"],
|
|
5
|
+
"mappings": "AAKO,SAAS,eAAe,SAA0C;AACxE,SAAO,YAAY,aAAa,YAAY,aAAa,YAAY;AACtE;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { SuiClient } from '@mysten/sui/client';
|
|
2
|
+
import type { StandardConnectFeature, StandardDisconnectFeature, StandardEventsFeature, SuiSignAndExecuteTransactionFeature, SuiSignPersonalMessageFeature, SuiSignTransactionFeature, Wallet } from '@mysten/wallet-standard';
|
|
3
|
+
import { ReadonlyWalletAccount } from '@mysten/wallet-standard';
|
|
4
|
+
import type { AuthProvider } from '../EnokiClient/type.js';
|
|
5
|
+
import { INTERNAL_ONLY_EnokiFlow } from './state.js';
|
|
6
|
+
import type { RegisterEnokiWalletsOptions } from './types.js';
|
|
7
|
+
export declare class EnokiWallet implements Wallet {
|
|
8
|
+
#private;
|
|
9
|
+
get id(): string;
|
|
10
|
+
get name(): string;
|
|
11
|
+
get provider(): AuthProvider;
|
|
12
|
+
get icon(): `data:image/svg+xml;base64,${string}` | `data:image/webp;base64,${string}` | `data:image/png;base64,${string}` | `data:image/gif;base64,${string}`;
|
|
13
|
+
get version(): "1.0.0";
|
|
14
|
+
get chains(): readonly ["sui:mainnet" | "sui:testnet" | "sui:devnet"];
|
|
15
|
+
get accounts(): ReadonlyWalletAccount[];
|
|
16
|
+
get features(): StandardConnectFeature & StandardDisconnectFeature & StandardEventsFeature & SuiSignTransactionFeature & SuiSignAndExecuteTransactionFeature & SuiSignPersonalMessageFeature;
|
|
17
|
+
constructor({ name, icon, flow, provider, clientId, redirectUrl, extraParams, client, windowFeatures, }: {
|
|
18
|
+
icon: Wallet['icon'];
|
|
19
|
+
name: string;
|
|
20
|
+
flow: INTERNAL_ONLY_EnokiFlow;
|
|
21
|
+
provider: AuthProvider;
|
|
22
|
+
clientId: string;
|
|
23
|
+
redirectUrl?: string;
|
|
24
|
+
extraParams?: Record<string, string>;
|
|
25
|
+
client: SuiClient;
|
|
26
|
+
windowFeatures?: string | (() => string);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export declare function registerEnokiWallets({ providers, client, network, windowFeatures, ...config }: RegisterEnokiWalletsOptions): {
|
|
30
|
+
wallets: Partial<Record<AuthProvider, EnokiWallet>>;
|
|
31
|
+
unregister: () => void;
|
|
32
|
+
};
|
|
33
|
+
export declare function isEnokiWallet(wallet: Wallet): wallet is EnokiWallet;
|
|
34
|
+
export declare function defaultWindowFeatures(): string;
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
var __typeError = (msg) => {
|
|
2
|
+
throw TypeError(msg);
|
|
3
|
+
};
|
|
4
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
5
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
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
|
+
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 __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
9
|
+
var _events, _accounts, _name, _id, _icon, _flow, _provider, _clientId, _redirectUrl, _extraParams, _client, _windowFeatures, _signTransaction, _signAndExecuteTransaction, _signPersonalMessage, _on, _EnokiWallet_instances, setAccount_fn, _connect, _disconnect, validateChain_fn;
|
|
10
|
+
import { Transaction } from "@mysten/sui/transactions";
|
|
11
|
+
import { fromBase64, toBase64 } from "@mysten/sui/utils";
|
|
12
|
+
import { getWallets, ReadonlyWalletAccount } from "@mysten/wallet-standard";
|
|
13
|
+
import mitt from "mitt";
|
|
14
|
+
import { ENOKI_PROVIDER_WALLETS_INFO } from "./providers.js";
|
|
15
|
+
import { INTERNAL_ONLY_EnokiFlow } from "./state.js";
|
|
16
|
+
class EnokiWallet {
|
|
17
|
+
constructor({
|
|
18
|
+
name,
|
|
19
|
+
icon,
|
|
20
|
+
flow,
|
|
21
|
+
provider,
|
|
22
|
+
clientId,
|
|
23
|
+
redirectUrl,
|
|
24
|
+
extraParams,
|
|
25
|
+
client,
|
|
26
|
+
windowFeatures
|
|
27
|
+
}) {
|
|
28
|
+
__privateAdd(this, _EnokiWallet_instances);
|
|
29
|
+
__privateAdd(this, _events);
|
|
30
|
+
__privateAdd(this, _accounts);
|
|
31
|
+
__privateAdd(this, _name);
|
|
32
|
+
__privateAdd(this, _id);
|
|
33
|
+
__privateAdd(this, _icon);
|
|
34
|
+
__privateAdd(this, _flow);
|
|
35
|
+
__privateAdd(this, _provider);
|
|
36
|
+
__privateAdd(this, _clientId);
|
|
37
|
+
__privateAdd(this, _redirectUrl);
|
|
38
|
+
__privateAdd(this, _extraParams);
|
|
39
|
+
__privateAdd(this, _client);
|
|
40
|
+
__privateAdd(this, _windowFeatures);
|
|
41
|
+
__privateAdd(this, _signTransaction, async ({ transaction, chain, account }) => {
|
|
42
|
+
__privateMethod(this, _EnokiWallet_instances, validateChain_fn).call(this, chain);
|
|
43
|
+
const parsedTransaction = Transaction.from(await transaction.toJSON());
|
|
44
|
+
const keypair = await __privateGet(this, _flow).getKeypair();
|
|
45
|
+
const suiAddress = keypair.toSuiAddress();
|
|
46
|
+
if (suiAddress !== account.address) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
`The specified account ${account.address} does not match the currently connected Enoki address ${suiAddress}.`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
parsedTransaction.setSenderIfNotSet(suiAddress);
|
|
52
|
+
return keypair.signTransaction(await parsedTransaction.build({ client: __privateGet(this, _client) }));
|
|
53
|
+
});
|
|
54
|
+
__privateAdd(this, _signAndExecuteTransaction, async ({
|
|
55
|
+
transaction,
|
|
56
|
+
chain,
|
|
57
|
+
account
|
|
58
|
+
}) => {
|
|
59
|
+
const { signature, bytes } = await __privateGet(this, _signTransaction).call(this, { transaction, account, chain });
|
|
60
|
+
const { digest, rawEffects } = await __privateGet(this, _client).executeTransactionBlock({
|
|
61
|
+
transactionBlock: bytes,
|
|
62
|
+
signature,
|
|
63
|
+
options: {
|
|
64
|
+
showRawEffects: true
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
return {
|
|
68
|
+
digest,
|
|
69
|
+
signature,
|
|
70
|
+
bytes,
|
|
71
|
+
effects: toBase64(Uint8Array.from(rawEffects))
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
__privateAdd(this, _signPersonalMessage, async ({ message, account, chain }) => {
|
|
75
|
+
__privateMethod(this, _EnokiWallet_instances, validateChain_fn).call(this, chain);
|
|
76
|
+
const keypair = await __privateGet(this, _flow).getKeypair();
|
|
77
|
+
const suiAddress = keypair.toSuiAddress();
|
|
78
|
+
if (suiAddress !== account.address) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`The specified account ${account.address} does not match the currently connected Enoki address ${suiAddress}.`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return keypair.signPersonalMessage(message);
|
|
84
|
+
});
|
|
85
|
+
__privateAdd(this, _on, (event, listener) => {
|
|
86
|
+
__privateGet(this, _events).on(event, listener);
|
|
87
|
+
return () => __privateGet(this, _events).off(event, listener);
|
|
88
|
+
});
|
|
89
|
+
__privateAdd(this, _connect, async (input) => {
|
|
90
|
+
__privateMethod(this, _EnokiWallet_instances, setAccount_fn).call(this);
|
|
91
|
+
if (this.accounts.length || input?.silent) {
|
|
92
|
+
return { accounts: this.accounts };
|
|
93
|
+
}
|
|
94
|
+
const popup = window.open(
|
|
95
|
+
void 0,
|
|
96
|
+
"_blank",
|
|
97
|
+
typeof __privateGet(this, _windowFeatures) === "function" ? __privateGet(this, _windowFeatures).call(this) : __privateGet(this, _windowFeatures)
|
|
98
|
+
);
|
|
99
|
+
if (!popup) {
|
|
100
|
+
throw new Error("Failed to open popup");
|
|
101
|
+
}
|
|
102
|
+
const url = await __privateGet(this, _flow).createAuthorizationURL({
|
|
103
|
+
provider: __privateGet(this, _provider),
|
|
104
|
+
clientId: __privateGet(this, _clientId),
|
|
105
|
+
redirectUrl: __privateGet(this, _redirectUrl) ?? window.location.href.split("#")[0],
|
|
106
|
+
extraParams: __privateGet(this, _extraParams)
|
|
107
|
+
});
|
|
108
|
+
popup.location = url;
|
|
109
|
+
await new Promise((resolve, reject) => {
|
|
110
|
+
const interval = setInterval(() => {
|
|
111
|
+
try {
|
|
112
|
+
if (popup.closed) {
|
|
113
|
+
clearInterval(interval);
|
|
114
|
+
reject(new Error("Popup closed"));
|
|
115
|
+
}
|
|
116
|
+
if (!popup.location.hash) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
} catch (e) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
clearInterval(interval);
|
|
123
|
+
__privateGet(this, _flow).handleAuthCallback(popup.location.hash).then(() => resolve(), reject);
|
|
124
|
+
try {
|
|
125
|
+
popup.close();
|
|
126
|
+
} catch (e) {
|
|
127
|
+
console.error(e);
|
|
128
|
+
}
|
|
129
|
+
}, 16);
|
|
130
|
+
});
|
|
131
|
+
__privateMethod(this, _EnokiWallet_instances, setAccount_fn).call(this);
|
|
132
|
+
return { accounts: this.accounts };
|
|
133
|
+
});
|
|
134
|
+
__privateAdd(this, _disconnect, async () => {
|
|
135
|
+
await __privateGet(this, _flow).logout();
|
|
136
|
+
__privateMethod(this, _EnokiWallet_instances, setAccount_fn).call(this);
|
|
137
|
+
});
|
|
138
|
+
__privateSet(this, _accounts, []);
|
|
139
|
+
__privateSet(this, _events, mitt());
|
|
140
|
+
__privateSet(this, _client, client);
|
|
141
|
+
__privateSet(this, _name, name);
|
|
142
|
+
__privateSet(this, _id, `enoki:${provider}:${flow.network}:${clientId}`);
|
|
143
|
+
__privateSet(this, _icon, icon);
|
|
144
|
+
__privateSet(this, _flow, flow);
|
|
145
|
+
__privateSet(this, _provider, provider);
|
|
146
|
+
__privateSet(this, _clientId, clientId);
|
|
147
|
+
__privateSet(this, _redirectUrl, redirectUrl);
|
|
148
|
+
__privateSet(this, _extraParams, extraParams);
|
|
149
|
+
__privateSet(this, _windowFeatures, windowFeatures);
|
|
150
|
+
__privateMethod(this, _EnokiWallet_instances, setAccount_fn).call(this);
|
|
151
|
+
}
|
|
152
|
+
get id() {
|
|
153
|
+
return __privateGet(this, _id);
|
|
154
|
+
}
|
|
155
|
+
get name() {
|
|
156
|
+
return __privateGet(this, _name);
|
|
157
|
+
}
|
|
158
|
+
get provider() {
|
|
159
|
+
return __privateGet(this, _provider);
|
|
160
|
+
}
|
|
161
|
+
get icon() {
|
|
162
|
+
return __privateGet(this, _icon);
|
|
163
|
+
}
|
|
164
|
+
get version() {
|
|
165
|
+
return "1.0.0";
|
|
166
|
+
}
|
|
167
|
+
get chains() {
|
|
168
|
+
return [`sui:${__privateGet(this, _flow).network}`];
|
|
169
|
+
}
|
|
170
|
+
get accounts() {
|
|
171
|
+
return __privateGet(this, _accounts);
|
|
172
|
+
}
|
|
173
|
+
get features() {
|
|
174
|
+
return {
|
|
175
|
+
"standard:connect": {
|
|
176
|
+
version: "1.0.0",
|
|
177
|
+
connect: __privateGet(this, _connect)
|
|
178
|
+
},
|
|
179
|
+
"standard:disconnect": {
|
|
180
|
+
version: "1.0.0",
|
|
181
|
+
disconnect: __privateGet(this, _disconnect)
|
|
182
|
+
},
|
|
183
|
+
"standard:events": {
|
|
184
|
+
version: "1.0.0",
|
|
185
|
+
on: __privateGet(this, _on)
|
|
186
|
+
},
|
|
187
|
+
"sui:signTransaction": {
|
|
188
|
+
version: "2.0.0",
|
|
189
|
+
signTransaction: __privateGet(this, _signTransaction)
|
|
190
|
+
},
|
|
191
|
+
"sui:signAndExecuteTransaction": {
|
|
192
|
+
version: "2.0.0",
|
|
193
|
+
signAndExecuteTransaction: __privateGet(this, _signAndExecuteTransaction)
|
|
194
|
+
},
|
|
195
|
+
"sui:signPersonalMessage": {
|
|
196
|
+
version: "1.1.0",
|
|
197
|
+
signPersonalMessage: __privateGet(this, _signPersonalMessage)
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
_events = new WeakMap();
|
|
203
|
+
_accounts = new WeakMap();
|
|
204
|
+
_name = new WeakMap();
|
|
205
|
+
_id = new WeakMap();
|
|
206
|
+
_icon = new WeakMap();
|
|
207
|
+
_flow = new WeakMap();
|
|
208
|
+
_provider = new WeakMap();
|
|
209
|
+
_clientId = new WeakMap();
|
|
210
|
+
_redirectUrl = new WeakMap();
|
|
211
|
+
_extraParams = new WeakMap();
|
|
212
|
+
_client = new WeakMap();
|
|
213
|
+
_windowFeatures = new WeakMap();
|
|
214
|
+
_signTransaction = new WeakMap();
|
|
215
|
+
_signAndExecuteTransaction = new WeakMap();
|
|
216
|
+
_signPersonalMessage = new WeakMap();
|
|
217
|
+
_on = new WeakMap();
|
|
218
|
+
_EnokiWallet_instances = new WeakSet();
|
|
219
|
+
setAccount_fn = function() {
|
|
220
|
+
const state = __privateGet(this, _flow).$zkLoginState.get();
|
|
221
|
+
if (state.address && state.publicKey) {
|
|
222
|
+
__privateSet(this, _accounts, [
|
|
223
|
+
new ReadonlyWalletAccount({
|
|
224
|
+
address: state.address,
|
|
225
|
+
chains: this.chains,
|
|
226
|
+
features: Object.keys(this.features),
|
|
227
|
+
publicKey: fromBase64(state.publicKey)
|
|
228
|
+
})
|
|
229
|
+
]);
|
|
230
|
+
} else {
|
|
231
|
+
__privateSet(this, _accounts, []);
|
|
232
|
+
}
|
|
233
|
+
__privateGet(this, _events).emit("change", { accounts: this.accounts });
|
|
234
|
+
};
|
|
235
|
+
_connect = new WeakMap();
|
|
236
|
+
_disconnect = new WeakMap();
|
|
237
|
+
validateChain_fn = function(chain) {
|
|
238
|
+
if (!chain || !this.chains.includes(chain)) {
|
|
239
|
+
throw new Error(
|
|
240
|
+
`A valid Sui chain identifier was not provided in the request. Please report this issue to the dApp developer. Examples of valid Sui chain identifiers are 'sui:testnet' and 'sui:mainnet'. Consider using the '@mysten/dapp-kit' package, which provides this value automatically.`
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
function registerEnokiWallets({
|
|
245
|
+
providers,
|
|
246
|
+
client,
|
|
247
|
+
network = "mainnet",
|
|
248
|
+
windowFeatures = defaultWindowFeatures,
|
|
249
|
+
...config
|
|
250
|
+
}) {
|
|
251
|
+
const walletsApi = getWallets();
|
|
252
|
+
const flow = new INTERNAL_ONLY_EnokiFlow({ ...config, network });
|
|
253
|
+
const unregisterCallbacks = [];
|
|
254
|
+
const wallets = {};
|
|
255
|
+
for (const { name, icon, provider } of ENOKI_PROVIDER_WALLETS_INFO) {
|
|
256
|
+
const providerOptions = providers[provider];
|
|
257
|
+
if (providerOptions) {
|
|
258
|
+
const { clientId, redirectUrl, extraParams } = providerOptions;
|
|
259
|
+
const wallet = new EnokiWallet({
|
|
260
|
+
name,
|
|
261
|
+
icon,
|
|
262
|
+
flow,
|
|
263
|
+
provider,
|
|
264
|
+
clientId,
|
|
265
|
+
client,
|
|
266
|
+
redirectUrl,
|
|
267
|
+
extraParams,
|
|
268
|
+
windowFeatures
|
|
269
|
+
});
|
|
270
|
+
const unregister = walletsApi.register(wallet);
|
|
271
|
+
unregisterCallbacks.push(unregister);
|
|
272
|
+
wallets[provider] = wallet;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return {
|
|
276
|
+
wallets,
|
|
277
|
+
unregister: () => {
|
|
278
|
+
for (const unregister of unregisterCallbacks) {
|
|
279
|
+
unregister();
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
function isEnokiWallet(wallet) {
|
|
285
|
+
return !!wallet.id?.startsWith("enoki:");
|
|
286
|
+
}
|
|
287
|
+
function defaultWindowFeatures() {
|
|
288
|
+
const width = 500;
|
|
289
|
+
const height = 800;
|
|
290
|
+
const left = (screen.width - width) / 2;
|
|
291
|
+
const top = (screen.height - height) / 4;
|
|
292
|
+
return `popup=1;toolbar=0;status=0;resizable=1,width=${width},height=${height},top=${top},left=${left}`;
|
|
293
|
+
}
|
|
294
|
+
export {
|
|
295
|
+
EnokiWallet,
|
|
296
|
+
defaultWindowFeatures,
|
|
297
|
+
isEnokiWallet,
|
|
298
|
+
registerEnokiWallets
|
|
299
|
+
};
|
|
300
|
+
//# sourceMappingURL=index.js.map
|