{"version":3,"file":"injected-evm-wallet-provider--ylDXgqa.mjs","sources":["../.rollup-tmp/auth/providers/injected-evm-wallet-provider.js"],"sourcesContent":["import { getActiveSessionManager as SessionManager, createSessionWithSignature, genAuthNonce, genSiweMessage, getConfig, normalizeEvmAddress, } from '@bounded-sh/core';\nimport { setCurrentUser, setAuthLoading } from '../../global';\nimport { setStoredAuthMethod } from '../stored-auth-method';\nimport { defaultInjectedEvmProvider, } from './injected-evm-wallet-types';\n// Re-export the type surface + discovery helper for callers who import the\n// provider module directly.\nexport { defaultInjectedEvmProvider };\n// Must equal the authMethod the registry (auth/index.ts) sets as currentAuthMethod,\n// so clearIncompatibleSession() keeps (not wipes) this session on the next init().\nconst INJECTED_EVM_AUTH_METHOD = 'evm-wallet';\n// The /session issuer's discriminator for the SIWE (EIP-4361 secp256k1) exchange.\nconst SIWE_SESSION_METHOD = 'siwe';\nconst NO_WALLET_ERR = 'No injected EVM wallet found. Install a wallet like MetaMask (metamask.io) or Rabby and try again, ' +\n    'or pass config.evmWalletConfig.getProvider to point at your wallet.';\nconst NO_SOL_TX_ERR = 'EVM wallet login is a LOGIN-ONLY surface (SIWE + personal_sign) and has no Solana ' +\n    'transaction surface: signTransaction/signAndSubmitTransaction are unavailable.';\nfunction normalizeConfig(config) {\n    if (config.chainId !== undefined && (!Number.isSafeInteger(config.chainId) || config.chainId <= 0)) {\n        throw new Error('EVM wallet chainId must be a positive safe integer');\n    }\n    return Object.assign({}, config);\n}\nexport class InjectedEvmWalletProvider {\n    constructor(config = {}) {\n        this.authenticatedAddress = null;\n        if (typeof window === 'undefined') {\n            throw new Error('InjectedEvmWalletProvider can only be instantiated in a browser environment');\n        }\n        const normalized = normalizeConfig(config);\n        // Singleton so the SDK reuses one instance (mirrors the other providers).\n        if (InjectedEvmWalletProvider.instance) {\n            InjectedEvmWalletProvider.instance.config = normalized;\n            InjectedEvmWalletProvider.instance.authenticatedAddress = null;\n            return InjectedEvmWalletProvider.instance;\n        }\n        this.config = normalized;\n        InjectedEvmWalletProvider.instance = this;\n    }\n    // @user.address (the Solana wallet) is null for an EVM-wallet login: the user\n    // authenticated with an EVM wallet and has no Solana address. User.address is\n    // typed `string | null`, so this is a plain null, not a cast.\n    evmUser(address) {\n        return { id: address, address: null, evmAddress: address, provider: this };\n    }\n    resolveProvider() {\n        var _a;\n        const provider = (_a = (this.config.getProvider ? this.config.getProvider() : null)) !== null && _a !== void 0 ? _a : defaultInjectedEvmProvider();\n        if (!provider)\n            throw new Error(NO_WALLET_ERR);\n        return provider;\n    }\n    /** Connect the injected wallet and return its canonical lowercase EVM address. */\n    async connectAddress(provider) {\n        const accounts = await provider.request({ method: 'eth_requestAccounts' });\n        const raw = Array.isArray(accounts) ? accounts[0] : undefined;\n        const address = normalizeEvmAddress(raw);\n        if (!address)\n            throw new Error('Wallet did not return a valid EVM address on connect');\n        return address;\n    }\n    /** Current chain id the wallet is connected to (EIP-155 number), or null. */\n    async currentChainId(provider) {\n        try {\n            const hex = await provider.request({ method: 'eth_chainId' });\n            if (typeof hex !== 'string' || !/^0x[0-9a-fA-F]+$/.test(hex))\n                return null;\n            const n = Number(BigInt(hex));\n            return Number.isSafeInteger(n) && n > 0 ? n : null;\n        }\n        catch (_a) {\n            return null;\n        }\n    }\n    async login() {\n        var _a;\n        setAuthLoading(true);\n        try {\n            const provider = this.resolveProvider();\n            const address = await this.connectAddress(provider);\n            // Reuse a still-valid session for THIS wallet.\n            const existing = await SessionManager().getSession();\n            if (existing && existing.address === address) {\n                const user = this.evmUser(address);\n                this.authenticatedAddress = address;\n                setCurrentUser(user);\n                this.markAuthMethod();\n                return user;\n            }\n            // SIWE: nonce -> canonical EIP-4361 prompt -> the wallet personal_sign's it.\n            const nonce = await genAuthNonce();\n            const appId = (await getConfig()).appId;\n            const chainId = (_a = this.config.chainId) !== null && _a !== void 0 ? _a : await this.currentChainId(provider);\n            if (chainId === null)\n                throw new Error('Could not verify the EVM wallet chain for SIWE login');\n            const message = genSiweMessage(address, nonce, appId, this.resolveDomain(), this.resolveUri(), { chainId });\n            // personal_sign(message, address) -> 0x r‖s‖v hex (the shape the\n            // bounded-auth verifier's recoverSiweSigner consumes). The raw UTF-8\n            // message is signed under the EIP-191 envelope the verifier hashes.\n            const signature = await provider.request({\n                method: 'personal_sign',\n                params: [message, address],\n            });\n            const result = await createSessionWithSignature(address, message, signature, SIWE_SESSION_METHOD);\n            await SessionManager().storeSession(address, result.accessToken, result.idToken, result.refreshToken);\n            this.markAuthMethod();\n            const user = this.evmUser(address);\n            this.authenticatedAddress = address;\n            setCurrentUser(user);\n            return user;\n        }\n        finally {\n            setAuthLoading(false);\n        }\n    }\n    resolveDomain() {\n        const actual = window.location.host.toLowerCase();\n        if (this.config.domain && this.config.domain.toLowerCase() !== actual) {\n            throw new Error('SIWE domain override must match the current browser origin');\n        }\n        return actual;\n    }\n    resolveUri() {\n        const actual = window.location.origin.toLowerCase();\n        if (this.config.uri) {\n            let configured;\n            try {\n                configured = new URL(this.config.uri).origin.toLowerCase();\n            }\n            catch (_a) {\n                throw new Error('SIWE URI override must be an absolute URL');\n            }\n            if (configured !== actual)\n                throw new Error('SIWE URI override must match the current browser origin');\n        }\n        return actual;\n    }\n    markAuthMethod() {\n        setStoredAuthMethod(INJECTED_EVM_AUTH_METHOD);\n    }\n    async restoreSession() {\n        const session = await SessionManager().getSession();\n        if (!session)\n            return null;\n        // An EVM-wallet session stores the EVM address as its address; @user.address\n        // (the Solana wallet) is null for this login.\n        const address = normalizeEvmAddress(session.address);\n        if (!address) {\n            await SessionManager().clearSession();\n            return null;\n        }\n        this.authenticatedAddress = address;\n        const user = this.evmUser(address);\n        setCurrentUser(user);\n        return user;\n    }\n    async logout() {\n        // EIP-1193 has no standard disconnect; clearing the Bounded session is the\n        // logout. (wallet_revokePermissions is non-standard/opt-in — skip it.)\n        await SessionManager().clearSession();\n        this.authenticatedAddress = null;\n        setCurrentUser(null);\n    }\n    // --- Signing surface -------------------------------------------------------\n    /** App-facing message signing: EIP-191 personal_sign, returns 0x r‖s‖v hex. */\n    async signMessage(message) {\n        const provider = this.resolveProvider();\n        const address = await this.connectAddress(provider);\n        if (!this.authenticatedAddress || address !== this.authenticatedAddress) {\n            throw new Error('The connected EVM wallet account changed after login. Sign in again before signing.');\n        }\n        return provider.request({ method: 'personal_sign', params: [message, address] });\n    }\n    // Solana-typed methods are structurally unsupported on an EVM wallet, and\n    // this provider is login-only, so there is no transaction surface at all.\n    async signTransaction(_transaction) {\n        throw new Error(NO_SOL_TX_ERR);\n    }\n    async signAndSubmitTransaction(_transaction, _feePayer) {\n        throw new Error(NO_SOL_TX_ERR);\n    }\n    async getNativeMethods() {\n        return this.resolveProvider();\n    }\n}\nInjectedEvmWalletProvider.instance = null;\n"],"names":["SessionManager"],"mappings":";;;;AAOA;AACA;AACA,MAAM,wBAAwB,GAAG,YAAY;AAC7C;AACA,MAAM,mBAAmB,GAAG,MAAM;AAClC,MAAM,aAAa,GAAG,qGAAqG;AAC3H,IAAI,qEAAqE;AACzE,MAAM,aAAa,GAAG,oFAAoF;AAC1G,IAAI,gFAAgF;AACpF,SAAS,eAAe,CAAC,MAAM,EAAE;AACjC,IAAI,IAAI,MAAM,CAAC,OAAO,KAAK,SAAS,KAAK,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,OAAO,IAAI,CAAC,CAAC,EAAE;AACxG,QAAQ,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC;AAC7E,IAAI;AACJ,IAAI,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC;AACpC;AACO,MAAM,yBAAyB,CAAC;AACvC,IAAI,WAAW,CAAC,MAAM,GAAG,EAAE,EAAE;AAC7B,QAAQ,IAAI,CAAC,oBAAoB,GAAG,IAAI;AACxC,QAAQ,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;AAC3C,YAAY,MAAM,IAAI,KAAK,CAAC,6EAA6E,CAAC;AAC1G,QAAQ;AACR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