@openfort/openfort-js 0.10.12 → 0.10.13

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.
@@ -1 +1 @@
1
- "use strict";var e=require("../../../packages/internal/openapi-clients/dist/index.js"),r=require("../core/configuration/passkey.js"),t=require("../core/config/config.js"),a=require("../core/configuration/account.js"),s=require("../core/configuration/authentication.js"),n=require("../core/errors/openfortError.js"),o=require("../types/types.js"),i=require("../utils/debug.js"),d=require("../wallets/embedded.js"),c=require("../wallets/evm/evmProvider.js");require("../wallets/evm/types.js");var g=require("../wallets/evm/provider/eip6963.js"),h=require("../wallets/evm/walletHelpers.js"),y=require("../wallets/iframeManager.js"),p=require("../wallets/messaging/ReactNativeMessenger.js");require("../wallets/messaging/browserMessenger/backwardCompatibility.js");var u=require("../wallets/messaging/browserMessenger/messengers/WindowMessenger.js");exports.EmbeddedWalletApi=class{storage;validateAndRefreshToken;ensureInitialized;eventEmitter;passkeyHandler;iframeManager=null;iframeManagerPromise=null;signer=null;signerPromise=null;provider=null;messagePoster=null;messenger=null;constructor(e,r,t,a,s){this.storage=e,this.validateAndRefreshToken=r,this.ensureInitialized=t,this.eventEmitter=a,this.passkeyHandler=s,this.eventEmitter.on(o.OpenfortEvents.LOGGED_OUT,()=>{i.debugLog("Handling logout event in EmbeddedWalletApi"),this.handleLogout()})}get backendApiClients(){const r=t.SDKConfiguration.getInstance();if(!r)throw new n.OpenfortError("Configuration not found",n.OpenfortErrorType.INVALID_CONFIGURATION);return new e.BackendApiClients({basePath:r.backendUrl,accessToken:r.baseConfiguration.publishableKey})}async getIframeManager(){if(i.debugLog("[HANDSHAKE DEBUG] getIframeManager called"),this.iframeManager)return i.debugLog("[HANDSHAKE DEBUG] Returning existing iframeManager instance"),this.iframeManager;if(this.iframeManagerPromise)return i.debugLog("[HANDSHAKE DEBUG] Returning existing iframeManagerPromise"),this.iframeManagerPromise;i.debugLog("[HANDSHAKE DEBUG] Creating new iframeManager"),this.iframeManagerPromise=this.createIframeManager();try{return i.debugLog("[HANDSHAKE DEBUG] Awaiting iframeManager creation"),this.iframeManager=await this.iframeManagerPromise,i.debugLog("[HANDSHAKE DEBUG] IframeManager created successfully"),this.iframeManagerPromise=null,this.iframeManager}catch(e){throw i.debugLog("[HANDSHAKE DEBUG] Error creating iframeManager:",e),this.iframeManagerPromise=null,e}}async createIframeManager(){i.debugLog("[HANDSHAKE DEBUG] createIframeManager starting");const e=t.SDKConfiguration.getInstance();if(!e)throw i.debugLog("[HANDSHAKE DEBUG] Configuration not found"),new n.OpenfortError("Configuration not found",n.OpenfortErrorType.INVALID_CONFIGURATION);let r;if(i.debugLog("[HANDSHAKE DEBUG] Configuration found"),this.messagePoster)i.debugLog("[HANDSHAKE DEBUG] Creating ReactNativeMessenger with messagePoster"),this.messenger=new p.ReactNativeMessenger(this.messagePoster),i.debugLog("[HANDSHAKE DEBUG] Created new ReactNativeMessenger instance"),r=this.messenger;else{i.debugLog("[HANDSHAKE DEBUG] Creating WindowMessenger for browser mode");const t=this.createIframe(e.iframeUrl),a=new URL(e.iframeUrl).origin;r=new u({remoteWindow:t.contentWindow,allowedOrigins:[a]}),i.debugLog("[HANDSHAKE DEBUG] Created WindowMessenger")}return i.debugLog("[HANDSHAKE DEBUG] Creating IframeManager instance"),new y.IframeManager(e,this.storage,r)}async ensureSigner(){if(this.signer)return this.signer;if(this.signerPromise)return this.signerPromise;this.signerPromise=this.createSigner();try{return this.signer=await this.signerPromise,this.signer}catch(e){throw this.signerPromise=null,e}finally{this.signerPromise=null}}async createSigner(){const e=await this.getIframeManager();return new d.EmbeddedSigner(e,this.storage,this.backendApiClients,this.eventEmitter)}createIframe(e){if("undefined"==typeof document)throw new n.OpenfortError("Document is not available. Please provide a message poster for non-browser environments.",n.OpenfortErrorType.INVALID_CONFIGURATION);const r=document.getElementById("openfort-iframe");r&&r.remove();const t=document.createElement("iframe");return t.style.display="none",t.id="openfort-iframe",t.src=e,document.body.appendChild(t),t}async getPasskeyKey(e){const r=await s.Authentication.fromStorage(this.storage);return await this.passkeyHandler.deriveAndExportKey({id:e,seed:r.player})}async getEntropy(e){switch(e.recoveryMethod){case o.RecoveryMethod.PASSWORD:return{recoveryPassword:e.password};case o.RecoveryMethod.AUTOMATIC:return{encryptionSession:e.encryptionSession};case o.RecoveryMethod.PASSKEY:return{passkey:{id:e.passkeyInfo?.passkeyId,env:e.passkeyInfo?.passkeyEnv,key:e.passkeyInfo?.passkeyKey||await this.getPasskeyKey(e.passkeyInfo?.passkeyId)}};default:throw new n.OpenfortError("Invalid recovery method",n.OpenfortErrorType.INVALID_CONFIGURATION)}}async configure(e){await this.validateAndRefreshToken();const t=e.recoveryParams??{recoveryMethod:o.RecoveryMethod.AUTOMATIC},i=await s.Authentication.fromStorage(this.storage),d=await a.Account.fromStorage(this.storage);if(t.recoveryMethod===o.RecoveryMethod.PASSKEY){if(!t.passkeyInfo?.passkeyId||!t.passkeyInfo?.passkeyEnv)throw new n.OpenfortError("Passkey ID and Name must be provided for passkey recovery",n.OpenfortErrorType.INVALID_CONFIGURATION);if(d)t.passkeyInfo={passkeyId:d.recoveryMethodDetails?.passkeyId};else{const e=await this.passkeyHandler.createPasskey({id:r.PasskeyHandler.randomPasskeyName(),displayName:"Openfort - Embedded Wallet",seed:i?.player});t.passkeyInfo={passkeyId:e.id,passkeyKey:e.key}}}const[c,g]=await Promise.all([this.ensureSigner(),this.getEntropy(t)]),h={chainId:e.chainId,entropy:g,accountType:e.accountType??o.AccountTypeEnum.SMART_ACCOUNT,chainType:e.chainType??o.ChainTypeEnum.EVM},y=await c.configure(h);return{id:y.id,chainId:y.chainId,user:i.player,address:y.address,ownerAddress:y.ownerAddress,chainType:y.chainType,accountType:y.accountType,implementationType:y.implementationType,createdAt:y.createdAt,recoveryMethod:a.Account.parseRecoveryMethod(y.recoveryMethod),recoveryMethodDetails:y.recoveryMethodDetails}}async create(e){await this.validateAndRefreshToken();const t=e.recoveryParams??{recoveryMethod:o.RecoveryMethod.AUTOMATIC},i=await s.Authentication.fromStorage(this.storage);if(t.recoveryMethod===o.RecoveryMethod.PASSKEY){if(!t.passkeyInfo?.passkeyId||!t.passkeyInfo?.passkeyEnv)throw new n.OpenfortError("Passkey ID and Name must be provided for passkey recovery",n.OpenfortErrorType.INVALID_CONFIGURATION);const e=await this.passkeyHandler.createPasskey({id:r.PasskeyHandler.randomPasskeyName(),displayName:"Openfort - Embedded Wallet",seed:i?.player});t.passkeyInfo={passkeyId:e.id,passkeyKey:e.key}}const[d,c]=await Promise.all([this.ensureSigner(),this.getEntropy(t)]),g=await d.create({accountType:e.accountType,chainType:e.chainType,chainId:e.chainId,entropy:c});return{id:g.id,chainId:g.chainId,user:i.player,address:g.address,ownerAddress:g.ownerAddress,chainType:g.chainType,accountType:g.accountType,implementationType:g.implementationType,createdAt:g.createdAt,recoveryMethod:a.Account.parseRecoveryMethod(g.recoveryMethod),recoveryMethodDetails:g.recoveryMethodDetails}}async recover(e){await this.validateAndRefreshToken();const r=e.recoveryParams??{recoveryMethod:o.RecoveryMethod.AUTOMATIC};if(r.recoveryMethod===o.RecoveryMethod.PASSKEY){if(!r.passkeyInfo?.passkeyId||!r.passkeyInfo?.passkeyEnv)throw new n.OpenfortError("Passkey ID and Name must be provided for passkey recovery",n.OpenfortErrorType.INVALID_CONFIGURATION);r.passkeyInfo={passkeyId:r.passkeyInfo.passkeyId,passkeyEnv:r.passkeyInfo.passkeyEnv}}const[t,i,d]=await Promise.all([this.ensureSigner(),this.getEntropy(r),s.Authentication.fromStorage(this.storage)]),c=await t.recover({account:e.account,entropy:i});return{id:c.id,chainId:c.chainId,user:d.player,address:c.address,ownerAddress:c.ownerAddress,chainType:c.chainType,accountType:c.accountType,implementationType:c.implementationType,createdAt:c.createdAt,recoveryMethod:a.Account.parseRecoveryMethod(c.recoveryMethod),recoveryMethodDetails:c.recoveryMethodDetails}}async signMessage(e,r){await this.validateAndRefreshToken();const t=await this.ensureSigner(),{hashMessage:s=!0,arrayifyMessage:n=!1}=r||{},o=await a.Account.fromStorage(this.storage);return await t.sign(e,n,s,o?.chainType)}async signTypedData(e,r,t){await this.validateAndRefreshToken();const s=await this.ensureSigner(),o=await a.Account.fromStorage(this.storage);if(!o)throw new n.OpenfortError("No account found",n.OpenfortErrorType.MISSING_SIGNER_ERROR);const i={...r};delete i.EIP712Domain;const{_TypedDataEncoder:d}=await import("@ethersproject/hash"),c=d.hash(e,i,t);return await h.signMessage({hash:c,implementationType:o.implementationType||o.type,chainId:Number(o.chainId),signer:s,address:o.address,ownerAddress:o.ownerAddress,factoryAddress:o.factoryAddress,salt:o.salt})}async exportPrivateKey(){await this.validateAndRefreshToken();const e=await this.ensureSigner();return await e.export()}async setRecoveryMethod(e,r){await this.validateAndRefreshToken();const t=await this.ensureSigner();let s,i,d;if(e.recoveryMethod===o.RecoveryMethod.PASSKEY?d=e.passkeyInfo:r.recoveryMethod===o.RecoveryMethod.PASSKEY&&(d=r.passkeyInfo),console.log(`Passkey info is ${JSON.stringify(d)}`),e.recoveryMethod===o.RecoveryMethod.PASSWORD?s=e.password:r.recoveryMethod===o.RecoveryMethod.PASSWORD&&(s=r.password),e.recoveryMethod===o.RecoveryMethod.AUTOMATIC?i=e.encryptionSession:r.recoveryMethod===o.RecoveryMethod.AUTOMATIC&&(i=r.encryptionSession),!s&&!i)throw new n.OpenfortError("Password or encryption session is not provided",n.OpenfortErrorType.INVALID_CONFIGURATION);await t.setRecoveryMethod({recoveryMethod:r.recoveryMethod,recoveryPassword:s,encryptionSession:i});const c=await a.Account.fromStorage(this.storage);c&&new a.Account({...c,recoveryMethod:r.recoveryMethod}).save(this.storage)}async get(){const e=await a.Account.fromStorage(this.storage);if(!e)throw new n.OpenfortError("No signer configured",n.OpenfortErrorType.MISSING_SIGNER_ERROR);const r=await s.Authentication.fromStorage(this.storage);if(!r)throw new n.OpenfortError("No access token found",n.OpenfortErrorType.NOT_LOGGED_IN_ERROR);return{id:e.id,chainId:e.chainId,user:r.player,address:e.address,ownerAddress:e.ownerAddress,chainType:e.chainType,accountType:e.accountType,implementationType:e.implementationType,createdAt:e.createdAt,recoveryMethod:a.Account.parseRecoveryMethod(e.recoveryMethod),recoveryMethodDetails:e.recoveryMethodDetails}}async list(e){const r={accountType:o.AccountTypeEnum.SMART_ACCOUNT,...e},i=t.SDKConfiguration.getInstance();if(!i)throw new n.OpenfortError("Configuration not found",n.OpenfortErrorType.INVALID_CONFIGURATION);await this.validateAndRefreshToken();const d=await s.Authentication.fromStorage(this.storage);if(!d)throw new n.OpenfortError("No access token found",n.OpenfortErrorType.NOT_LOGGED_IN_ERROR);return n.withOpenfortError(async()=>(await this.backendApiClients.accountsApi.getAccountsV2(r,{headers:{authorization:`Bearer ${i.baseConfiguration.publishableKey}`,"x-player-token":d.token,"x-auth-provider":d.thirdPartyProvider,"x-token-type":d.thirdPartyTokenType}})).data.data.map(e=>({user:e.user,chainType:e.chainType,id:e.id,address:e.address,ownerAddress:e.ownerAddress,accountType:e.accountType,createdAt:e.createdAt,implementationType:e.smartAccount?.implementationType,chainId:e.chainId,recoveryMethod:a.Account.parseRecoveryMethod(e.recoveryMethod),recoveryMethodDetails:e.recoveryMethodDetails})),{default:n.OpenfortErrorType.AUTHENTICATION_ERROR})}async getEmbeddedState(){try{if(!await s.Authentication.fromStorage(this.storage))return o.EmbeddedState.UNAUTHENTICATED;return await a.Account.fromStorage(this.storage)?o.EmbeddedState.READY:o.EmbeddedState.EMBEDDED_SIGNER_NOT_CONFIGURED}catch(e){return i.debugLog("Failed to get embedded state:",e),o.EmbeddedState.UNAUTHENTICATED}}async getEthereumProvider(e){await this.ensureInitialized();const r={announceProvider:!0,...e},t=await s.Authentication.fromStorage(this.storage),n=await a.Account.fromStorage(this.storage);return this.provider?this.provider&&r.policy&&this.provider.updatePolicy(r.policy):(this.provider=new c.EvmProvider({storage:this.storage,openfortEventEmitter:this.eventEmitter,ensureSigner:this.ensureSigner.bind(this),account:n||void 0,authentication:t||void 0,backendApiClients:this.backendApiClients,policyId:r.policy,validateAndRefreshSession:this.validateAndRefreshToken.bind(this),chains:r.chains}),r.announceProvider&&g.announceProvider({info:{...g.openfortProviderInfo,...r.providerInfo},provider:this.provider})),this.provider}async ping(e){try{e>0&&await new Promise(r=>{setTimeout(r,e)});const r=await this.getIframeManager();if(!r.isLoaded())return!1;const t=await s.Authentication.fromStorage(this.storage);if(t)try{return await r.getCurrentDevice(t.player),!0}catch(e){return!1}return r.isLoaded()}catch(e){return i.debugLog("Ping failed:",e),!1}}getURL(){const e=t.SDKConfiguration.getInstance();if(!e)throw new n.OpenfortError("Configuration not found",n.OpenfortErrorType.INVALID_CONFIGURATION);return e.iframeUrl}async setMessagePoster(e){if(!e||"function"!=typeof e.postMessage)throw new n.OpenfortError("Invalid message poster",n.OpenfortErrorType.INVALID_CONFIGURATION);this.messagePoster=e,this.messenger&&this.messenger.destroy(),this.iframeManager&&this.iframeManager.destroy(),this.signer=null,this.signerPromise=null,this.iframeManager=null,this.iframeManagerPromise=null,this.messenger=null}async handleTokenRefreshed(){if(this.iframeManager)try{await this.iframeManager.updateAuthentication(),i.debugLog("Updated IframeManager authentication after token refresh")}catch(e){i.debugLog("Failed to update IframeManager authentication:",e)}else i.debugLog("IframeManager not initialized, skipping authentication update")}async handleLogout(){const e=await this.ensureSigner();await e.disconnect(),this.provider=null,this.messenger=null,this.iframeManager=null,this.iframeManagerPromise=null,this.signer=null,this.signerPromise=null}async onMessage(e){if(!e||"object"!=typeof e)return void i.debugLog("Invalid message received:",e);i.debugLog("[HANDSHAKE DEBUG] EmbeddedWalletApi onMessage:",e);const r="penpal"===e.namespace&&"SYN"===e.type||e.penpal&&"string"==typeof e.penpal;if(r&&this.messenger&&this.messagePoster)return i.debugLog("[HANDSHAKE DEBUG] Passing message directly to existing ReactNativeMessenger"),void this.messenger.handleMessage(e);const t=await this.getIframeManager();i.debugLog(`[HANDSHAKE DEBUG] IframeManager obtained, isLoaded: ${t.isLoaded()}`),r&&!t.isLoaded()&&i.debugLog("[HANDSHAKE DEBUG] Received penpal message before connection initialized, setting up connection..."),i.debugLog("[HANDSHAKE DEBUG] Calling iframeManager.onMessage"),await t.onMessage(e),i.debugLog("[HANDSHAKE DEBUG] iframeManager.onMessage completed")}isReady(){return this.iframeManager?.isLoaded()||!1}};
1
+ "use strict";var e=require("../../../packages/internal/openapi-clients/dist/index.js"),r=require("../core/configuration/passkey.js"),t=require("../core/config/config.js"),a=require("../core/configuration/account.js"),n=require("../core/configuration/authentication.js"),s=require("../core/errors/openfortError.js"),o=require("../types/types.js"),i=require("../utils/debug.js"),d=require("../wallets/embedded.js"),c=require("../wallets/evm/evmProvider.js");require("../wallets/evm/types.js");var g=require("../wallets/evm/provider/eip6963.js"),h=require("../wallets/evm/walletHelpers.js"),y=require("../wallets/iframeManager.js"),p=require("../wallets/messaging/ReactNativeMessenger.js");require("../wallets/messaging/browserMessenger/backwardCompatibility.js");var u=require("../wallets/messaging/browserMessenger/messengers/WindowMessenger.js");exports.EmbeddedWalletApi=class{storage;validateAndRefreshToken;ensureInitialized;eventEmitter;passkeyHandler;iframeManager=null;iframeManagerPromise=null;signer=null;signerPromise=null;provider=null;messagePoster=null;messenger=null;constructor(e,r,t,a,n){this.storage=e,this.validateAndRefreshToken=r,this.ensureInitialized=t,this.eventEmitter=a,this.passkeyHandler=n,this.eventEmitter.on(o.OpenfortEvents.LOGGED_OUT,()=>{i.debugLog("Handling logout event in EmbeddedWalletApi"),this.handleLogout()})}get backendApiClients(){const r=t.SDKConfiguration.getInstance();if(!r)throw new s.OpenfortError("Configuration not found",s.OpenfortErrorType.INVALID_CONFIGURATION);return new e.BackendApiClients({basePath:r.backendUrl,accessToken:r.baseConfiguration.publishableKey})}async getIframeManager(){if(i.debugLog("[HANDSHAKE DEBUG] getIframeManager called"),this.iframeManager)return i.debugLog("[HANDSHAKE DEBUG] Returning existing iframeManager instance"),this.iframeManager;if(this.iframeManagerPromise)return i.debugLog("[HANDSHAKE DEBUG] Returning existing iframeManagerPromise"),this.iframeManagerPromise;i.debugLog("[HANDSHAKE DEBUG] Creating new iframeManager"),this.iframeManagerPromise=this.createIframeManager();try{return i.debugLog("[HANDSHAKE DEBUG] Awaiting iframeManager creation"),this.iframeManager=await this.iframeManagerPromise,i.debugLog("[HANDSHAKE DEBUG] IframeManager created successfully"),this.iframeManagerPromise=null,this.iframeManager}catch(e){throw i.debugLog("[HANDSHAKE DEBUG] Error creating iframeManager:",e),this.iframeManagerPromise=null,e}}async createIframeManager(){i.debugLog("[HANDSHAKE DEBUG] createIframeManager starting");const e=t.SDKConfiguration.getInstance();if(!e)throw i.debugLog("[HANDSHAKE DEBUG] Configuration not found"),new s.OpenfortError("Configuration not found",s.OpenfortErrorType.INVALID_CONFIGURATION);let r;if(i.debugLog("[HANDSHAKE DEBUG] Configuration found"),this.messagePoster)i.debugLog("[HANDSHAKE DEBUG] Creating ReactNativeMessenger with messagePoster"),this.messenger=new p.ReactNativeMessenger(this.messagePoster),i.debugLog("[HANDSHAKE DEBUG] Created new ReactNativeMessenger instance"),r=this.messenger;else{i.debugLog("[HANDSHAKE DEBUG] Creating WindowMessenger for browser mode");const t=this.createIframe(e.iframeUrl),a=new URL(e.iframeUrl).origin;r=new u({remoteWindow:t.contentWindow,allowedOrigins:[a]}),i.debugLog("[HANDSHAKE DEBUG] Created WindowMessenger")}return i.debugLog("[HANDSHAKE DEBUG] Creating IframeManager instance"),new y.IframeManager(e,this.storage,r)}async ensureSigner(){if(this.signer)return this.signer;if(this.signerPromise)return this.signerPromise;this.signerPromise=this.createSigner();try{return this.signer=await this.signerPromise,this.signer}catch(e){throw this.signerPromise=null,e}finally{this.signerPromise=null}}async createSigner(){const e=await this.getIframeManager();return new d.EmbeddedSigner(e,this.storage,this.backendApiClients,this.passkeyHandler,this.eventEmitter)}createIframe(e){if("undefined"==typeof document)throw new s.OpenfortError("Document is not available. Please provide a message poster for non-browser environments.",s.OpenfortErrorType.INVALID_CONFIGURATION);const r=document.getElementById("openfort-iframe");r&&r.remove();const t=document.createElement("iframe");return t.style.display="none",t.id="openfort-iframe",t.src=e,document.body.appendChild(t),t}async getPasskeyKey(e){const r=await n.Authentication.fromStorage(this.storage);return await this.passkeyHandler.deriveAndExportKey({id:e,seed:r.player})}async getEntropy(e,r=!0){switch(e.recoveryMethod){case o.RecoveryMethod.PASSWORD:return{recoveryPassword:e.password};case o.RecoveryMethod.AUTOMATIC:return{encryptionSession:e.encryptionSession};case o.RecoveryMethod.PASSKEY:return r?{passkey:{id:e.passkeyInfo?.passkeyId,env:e.passkeyInfo?.passkeyEnv,key:e.passkeyInfo?.passkeyKey||await this.getPasskeyKey(e.passkeyInfo?.passkeyId)}}:{passkey:{}};default:throw new s.OpenfortError("Invalid recovery method",s.OpenfortErrorType.INVALID_CONFIGURATION)}}async configure(e){await this.validateAndRefreshToken();const r=e.recoveryParams??{recoveryMethod:o.RecoveryMethod.AUTOMATIC},[t,s,i]=await Promise.all([n.Authentication.fromStorage(this.storage),this.ensureSigner(),this.getEntropy(r,!1)]),d={chainId:e.chainId,entropy:i,accountType:e.accountType??o.AccountTypeEnum.SMART_ACCOUNT,chainType:e.chainType??o.ChainTypeEnum.EVM},c=await s.configure(d);return{id:c.id,chainId:c.chainId,user:t.player,address:c.address,ownerAddress:c.ownerAddress,chainType:c.chainType,accountType:c.accountType,implementationType:c.implementationType,createdAt:c.createdAt,recoveryMethod:a.Account.parseRecoveryMethod(c.recoveryMethod),recoveryMethodDetails:c.recoveryMethodDetails}}async create(e){await this.validateAndRefreshToken();const t=e.recoveryParams??{recoveryMethod:o.RecoveryMethod.AUTOMATIC},s=await n.Authentication.fromStorage(this.storage);if(t.recoveryMethod===o.RecoveryMethod.PASSKEY){const e=await this.passkeyHandler.createPasskey({id:r.PasskeyHandler.randomPasskeyName(),displayName:"Openfort - Embedded Wallet",seed:s?.player});t.passkeyInfo={passkeyId:e.id,passkeyKey:e.key}}const[i,d]=await Promise.all([this.ensureSigner(),this.getEntropy(t)]),c=await i.create({accountType:e.accountType,chainType:e.chainType,chainId:e.chainId,entropy:d});return{id:c.id,chainId:c.chainId,user:s.player,address:c.address,ownerAddress:c.ownerAddress,chainType:c.chainType,accountType:c.accountType,implementationType:c.implementationType,createdAt:c.createdAt,recoveryMethod:a.Account.parseRecoveryMethod(c.recoveryMethod),recoveryMethodDetails:c.recoveryMethodDetails}}async recover(e){await this.validateAndRefreshToken();const r=e.recoveryParams??{recoveryMethod:o.RecoveryMethod.AUTOMATIC};if(r.recoveryMethod===o.RecoveryMethod.PASSKEY){if(!r.passkeyInfo?.passkeyId)throw new s.OpenfortError("Passkey ID must be provided for passkey recovery",s.OpenfortErrorType.INVALID_CONFIGURATION);r.passkeyInfo={passkeyId:r.passkeyInfo.passkeyId,passkeyEnv:r.passkeyInfo.passkeyEnv}}const[t,i,d]=await Promise.all([this.ensureSigner(),this.getEntropy(r),n.Authentication.fromStorage(this.storage)]),c=await t.recover({account:e.account,entropy:i});return{id:c.id,chainId:c.chainId,user:d.player,address:c.address,ownerAddress:c.ownerAddress,chainType:c.chainType,accountType:c.accountType,implementationType:c.implementationType,createdAt:c.createdAt,recoveryMethod:a.Account.parseRecoveryMethod(c.recoveryMethod),recoveryMethodDetails:c.recoveryMethodDetails}}async signMessage(e,r){await this.validateAndRefreshToken();const t=await this.ensureSigner(),{hashMessage:n=!0,arrayifyMessage:s=!1}=r||{},o=await a.Account.fromStorage(this.storage);return await t.sign(e,s,n,o?.chainType)}async signTypedData(e,r,t){await this.validateAndRefreshToken();const n=await this.ensureSigner(),o=await a.Account.fromStorage(this.storage);if(!o)throw new s.OpenfortError("No account found",s.OpenfortErrorType.MISSING_SIGNER_ERROR);const i={...r};delete i.EIP712Domain;const{_TypedDataEncoder:d}=await import("@ethersproject/hash"),c=d.hash(e,i,t);return await h.signMessage({hash:c,implementationType:o.implementationType||o.type,chainId:Number(o.chainId),signer:n,address:o.address,ownerAddress:o.ownerAddress,factoryAddress:o.factoryAddress,salt:o.salt})}async exportPrivateKey(){await this.validateAndRefreshToken();const e=await this.ensureSigner();return await e.export()}async setRecoveryMethod(e,r){await this.validateAndRefreshToken();const t=await this.ensureSigner();let n,i;if(e.recoveryMethod===o.RecoveryMethod.PASSWORD?n=e.password:r.recoveryMethod===o.RecoveryMethod.PASSWORD&&(n=r.password),e.recoveryMethod===o.RecoveryMethod.AUTOMATIC?i=e.encryptionSession:r.recoveryMethod===o.RecoveryMethod.AUTOMATIC&&(i=r.encryptionSession),!n&&!i)throw new s.OpenfortError("Password or encryption session is not provided",s.OpenfortErrorType.INVALID_CONFIGURATION);await t.setRecoveryMethod({recoveryMethod:r.recoveryMethod,recoveryPassword:n,encryptionSession:i});const d=await a.Account.fromStorage(this.storage);d&&new a.Account({...d,recoveryMethod:r.recoveryMethod}).save(this.storage)}async get(){const e=await a.Account.fromStorage(this.storage);if(!e)throw new s.OpenfortError("No signer configured",s.OpenfortErrorType.MISSING_SIGNER_ERROR);const r=await n.Authentication.fromStorage(this.storage);if(!r)throw new s.OpenfortError("No access token found",s.OpenfortErrorType.NOT_LOGGED_IN_ERROR);return{id:e.id,chainId:e.chainId,user:r.player,address:e.address,ownerAddress:e.ownerAddress,chainType:e.chainType,accountType:e.accountType,implementationType:e.implementationType,createdAt:e.createdAt,recoveryMethod:a.Account.parseRecoveryMethod(e.recoveryMethod),recoveryMethodDetails:e.recoveryMethodDetails}}async list(e){const r={accountType:o.AccountTypeEnum.SMART_ACCOUNT,...e},i=t.SDKConfiguration.getInstance();if(!i)throw new s.OpenfortError("Configuration not found",s.OpenfortErrorType.INVALID_CONFIGURATION);await this.validateAndRefreshToken();const d=await n.Authentication.fromStorage(this.storage);if(!d)throw new s.OpenfortError("No access token found",s.OpenfortErrorType.NOT_LOGGED_IN_ERROR);return s.withOpenfortError(async()=>(await this.backendApiClients.accountsApi.getAccountsV2(r,{headers:{authorization:`Bearer ${i.baseConfiguration.publishableKey}`,"x-player-token":d.token,"x-auth-provider":d.thirdPartyProvider,"x-token-type":d.thirdPartyTokenType}})).data.data.map(e=>({user:e.user,chainType:e.chainType,id:e.id,address:e.address,ownerAddress:e.ownerAddress,accountType:e.accountType,createdAt:e.createdAt,implementationType:e.smartAccount?.implementationType,chainId:e.chainId,recoveryMethod:a.Account.parseRecoveryMethod(e.recoveryMethod),recoveryMethodDetails:e.recoveryMethodDetails})),{default:s.OpenfortErrorType.AUTHENTICATION_ERROR})}async getEmbeddedState(){try{if(!await n.Authentication.fromStorage(this.storage))return o.EmbeddedState.UNAUTHENTICATED;return await a.Account.fromStorage(this.storage)?o.EmbeddedState.READY:o.EmbeddedState.EMBEDDED_SIGNER_NOT_CONFIGURED}catch(e){return i.debugLog("Failed to get embedded state:",e),o.EmbeddedState.UNAUTHENTICATED}}async getEthereumProvider(e){await this.ensureInitialized();const r={announceProvider:!0,...e},t=await n.Authentication.fromStorage(this.storage),s=await a.Account.fromStorage(this.storage);return this.provider?this.provider&&r.policy&&this.provider.updatePolicy(r.policy):(this.provider=new c.EvmProvider({storage:this.storage,openfortEventEmitter:this.eventEmitter,ensureSigner:this.ensureSigner.bind(this),account:s||void 0,authentication:t||void 0,backendApiClients:this.backendApiClients,policyId:r.policy,validateAndRefreshSession:this.validateAndRefreshToken.bind(this),chains:r.chains}),r.announceProvider&&g.announceProvider({info:{...g.openfortProviderInfo,...r.providerInfo},provider:this.provider})),this.provider}async ping(e){try{e>0&&await new Promise(r=>{setTimeout(r,e)});const r=await this.getIframeManager();if(!r.isLoaded())return!1;const t=await n.Authentication.fromStorage(this.storage);if(t)try{return await r.getCurrentDevice(t.player),!0}catch(e){return!1}return r.isLoaded()}catch(e){return i.debugLog("Ping failed:",e),!1}}getURL(){const e=t.SDKConfiguration.getInstance();if(!e)throw new s.OpenfortError("Configuration not found",s.OpenfortErrorType.INVALID_CONFIGURATION);return e.iframeUrl}async setMessagePoster(e){if(!e||"function"!=typeof e.postMessage)throw new s.OpenfortError("Invalid message poster",s.OpenfortErrorType.INVALID_CONFIGURATION);this.messagePoster=e,this.messenger&&this.messenger.destroy(),this.iframeManager&&this.iframeManager.destroy(),this.signer=null,this.signerPromise=null,this.iframeManager=null,this.iframeManagerPromise=null,this.messenger=null}async handleTokenRefreshed(){if(this.iframeManager)try{await this.iframeManager.updateAuthentication(),i.debugLog("Updated IframeManager authentication after token refresh")}catch(e){i.debugLog("Failed to update IframeManager authentication:",e)}else i.debugLog("IframeManager not initialized, skipping authentication update")}async handleLogout(){const e=await this.ensureSigner();await e.disconnect(),this.provider=null,this.messenger=null,this.iframeManager=null,this.iframeManagerPromise=null,this.signer=null,this.signerPromise=null}async onMessage(e){if(!e||"object"!=typeof e)return void i.debugLog("Invalid message received:",e);i.debugLog("[HANDSHAKE DEBUG] EmbeddedWalletApi onMessage:",e);const r="penpal"===e.namespace&&"SYN"===e.type||e.penpal&&"string"==typeof e.penpal;if(r&&this.messenger&&this.messagePoster)return i.debugLog("[HANDSHAKE DEBUG] Passing message directly to existing ReactNativeMessenger"),void this.messenger.handleMessage(e);const t=await this.getIframeManager();i.debugLog(`[HANDSHAKE DEBUG] IframeManager obtained, isLoaded: ${t.isLoaded()}`),r&&!t.isLoaded()&&i.debugLog("[HANDSHAKE DEBUG] Received penpal message before connection initialized, setting up connection..."),i.debugLog("[HANDSHAKE DEBUG] Calling iframeManager.onMessage"),await t.onMessage(e),i.debugLog("[HANDSHAKE DEBUG] iframeManager.onMessage completed")}isReady(){return this.iframeManager?.isLoaded()||!1}};
@@ -1 +1 @@
1
- "use strict";exports.PACKAGE="@openfort/openfort-js",exports.VERSION="0.10.12";
1
+ "use strict";exports.PACKAGE="@openfort/openfort-js",exports.VERSION="0.10.13";
@@ -1 +1 @@
1
- "use strict";var e=require("../core/configuration/authentication.js"),t=require("../core/errors/openfortError.js"),r=require("../types/types.js"),a=require("../core/configuration/account.js");require("./messaging/browserMessenger/backwardCompatibility.js");var o=require("../storage/istorage.js"),n=require("../core/config/config.js");require("./types.js"),require("../core/errors/sentry.js"),require("jose"),require("human-id"),require("./evm/types.js"),require("./evm/JsonRpcError.js"),require("eventemitter3"),require("./evm/provider/eip6963.js");exports.EmbeddedSigner=class{iframeManager;storage;backendApiClients;eventEmitter;constructor(e,t,r,a){this.iframeManager=e,this.storage=t,this.backendApiClients=r,this.eventEmitter=a}async configure(o){const s=await e.Authentication.fromStorage(this.storage);if(!s)throw new t.OpenfortError("No access token found",t.OpenfortErrorType.NOT_LOGGED_IN_ERROR);const i=n.SDKConfiguration.getInstance();if(!i)throw new t.OpenfortError("Configuration not found",t.OpenfortErrorType.INVALID_CONFIGURATION);const c=await a.Account.fromStorage(this.storage);let d;if(c){const e={account:c.id,...o.entropy&&{entropy:{...o.entropy.recoveryPassword&&{recoveryPassword:o.entropy.recoveryPassword},...o.entropy.encryptionSession&&{encryptionSession:o.entropy.encryptionSession},...o.entropy.passkey&&{passkey:o.entropy.passkey}}}},t=await this.iframeManager.recover(e);d=t.account}else{const e=await this.backendApiClients.accountsApi.getAccountsV2({user:s.player,accountType:o.accountType,chainType:o.chainType,chainId:o.chainId},{headers:{authorization:`Bearer ${i.baseConfiguration.publishableKey}`,"x-player-token":s.token,"x-auth-provider":s.thirdPartyProvider,"x-token-type":s.thirdPartyTokenType}});if(0===e.data.data.length){const e={accountType:o.accountType,chainType:o.chainType,chainId:o.chainId,...o.entropy&&{entropy:{...o.entropy.recoveryPassword&&{recoveryPassword:o.entropy.recoveryPassword},...o.entropy.encryptionSession&&{encryptionSession:o.entropy.encryptionSession},...o.entropy.passkey&&{passkey:o.entropy.passkey}}}},t=await this.iframeManager.create(e);d=t.account}else{const t={account:e.data.data[0].id,...o.entropy&&{entropy:{...o.entropy.recoveryPassword&&{recoveryPassword:o.entropy.recoveryPassword},...o.entropy.encryptionSession&&{encryptionSession:o.entropy.encryptionSession},...o.entropy.passkey&&{passkey:o.entropy.passkey}}}},r=await this.iframeManager.recover(t);d=r.account}}return t.withOpenfortError(async()=>{const e=await this.backendApiClients.accountsApi.getAccountV2({id:d},{headers:{authorization:`Bearer ${i.baseConfiguration.publishableKey}`,"x-player-token":s.token,"x-auth-provider":s.thirdPartyProvider,"x-token-type":s.thirdPartyTokenType}}),t=new a.Account({user:e.data.user,chainType:e.data.chainType,id:e.data.id,address:e.data.address,ownerAddress:e.data.ownerAddress,accountType:e.data.accountType,createdAt:e.data.createdAt,implementationType:e.data.smartAccount?.implementationType,chainId:e.data.chainId,salt:e.data.smartAccount?.salt,factoryAddress:e.data.smartAccount?.factoryAddress,recoveryMethod:a.Account.parseRecoveryMethod(e.data.recoveryMethod)});return t.save(this.storage),this.eventEmitter.emit(r.OpenfortEvents.SWITCH_ACCOUNT,e.data.address),t},{default:t.OpenfortErrorType.AUTHENTICATION_ERROR})}async sign(e,t,r,a){return await this.iframeManager.sign(e,t,r,a)}async export(){return await this.iframeManager.export()}async switchChain({chainId:e}){const t=await this.iframeManager.switchChain(e),r=await a.Account.fromStorage(this.storage);new a.Account({...r,id:t.account,chainId:e}).save(this.storage)}async create(o){const s=await this.iframeManager.create(o),i=await e.Authentication.fromStorage(this.storage);if(!i)throw new t.OpenfortError("No access token found",t.OpenfortErrorType.NOT_LOGGED_IN_ERROR);const c=n.SDKConfiguration.getInstance();if(!c)throw new t.OpenfortError("Configuration not found",t.OpenfortErrorType.INVALID_CONFIGURATION);return t.withOpenfortError(async()=>{const e=await this.backendApiClients.accountsApi.getAccountV2({id:s.account},{headers:{authorization:`Bearer ${c.baseConfiguration.publishableKey}`,"x-player-token":i.token,"x-auth-provider":i.thirdPartyProvider,"x-token-type":i.thirdPartyTokenType}}),t=new a.Account({user:e.data.user,chainType:e.data.chainType,id:e.data.id,address:e.data.address,ownerAddress:e.data.ownerAddress,accountType:e.data.accountType,createdAt:e.data.createdAt,implementationType:e.data.smartAccount?.implementationType,chainId:e.data.chainId,salt:e.data.smartAccount?.salt,factoryAddress:e.data.smartAccount?.factoryAddress,recoveryMethod:a.Account.parseRecoveryMethod(e.data.recoveryMethod),recoveryMethodDetails:e.data.recoveryMethodDetails});return t.save(this.storage),this.eventEmitter.emit(r.OpenfortEvents.SWITCH_ACCOUNT,e.data.address),t},{default:t.OpenfortErrorType.AUTHENTICATION_ERROR})}async recover(o){const s=await this.iframeManager.recover(o),i=await e.Authentication.fromStorage(this.storage);if(!i)throw new t.OpenfortError("No access token found",t.OpenfortErrorType.NOT_LOGGED_IN_ERROR);const c=n.SDKConfiguration.getInstance();if(!c)throw new t.OpenfortError("Configuration not found",t.OpenfortErrorType.INVALID_CONFIGURATION);return t.withOpenfortError(async()=>{const e=await this.backendApiClients.accountsApi.getAccountV2({id:s.account},{headers:{authorization:`Bearer ${c.baseConfiguration.publishableKey}`,"x-player-token":i.token,"x-auth-provider":i.thirdPartyProvider,"x-token-type":i.thirdPartyTokenType}}),t=new a.Account({user:e.data.user,chainType:e.data.chainType,id:e.data.id,address:e.data.address,ownerAddress:e.data.ownerAddress,accountType:e.data.accountType,createdAt:e.data.createdAt,implementationType:e.data.smartAccount?.implementationType,chainId:e.data.chainId,salt:e.data.smartAccount?.salt,factoryAddress:e.data.smartAccount?.factoryAddress,recoveryMethod:a.Account.parseRecoveryMethod(e.data.recoveryMethod)});return t.save(this.storage),this.eventEmitter.emit(r.OpenfortEvents.SWITCH_ACCOUNT,e.data.address),t},{default:t.OpenfortErrorType.AUTHENTICATION_ERROR})}async setRecoveryMethod({recoveryMethod:e,recoveryPassword:t,encryptionSession:r}){await this.iframeManager.setRecoveryMethod(e,t,r)}async disconnect(){await this.iframeManager.disconnect(),this.storage.remove(o.StorageKeys.ACCOUNT)}};
1
+ "use strict";var e=require("../core/configuration/authentication.js"),t=require("../core/errors/openfortError.js"),r=require("../types/types.js"),a=require("../core/configuration/account.js");require("./messaging/browserMessenger/backwardCompatibility.js");var o=require("../storage/istorage.js"),n=require("../core/config/config.js");require("./types.js"),require("../core/errors/sentry.js"),require("jose");var s=require("../core/configuration/passkey.js");require("./evm/types.js"),require("./evm/JsonRpcError.js"),require("eventemitter3"),require("./evm/provider/eip6963.js");exports.EmbeddedSigner=class{iframeManager;storage;backendApiClients;passkeyHandler;eventEmitter;constructor(e,t,r,a,o){this.iframeManager=e,this.storage=t,this.backendApiClients=r,this.passkeyHandler=a,this.eventEmitter=o}async createPasskey(e){const t=await this.passkeyHandler.createPasskey({id:s.PasskeyHandler.randomPasskeyName(),displayName:"Openfort - Embedded Wallet",seed:e});return{id:t.id,key:t.key}}async configure(o){const s=await e.Authentication.fromStorage(this.storage);if(!s)throw new t.OpenfortError("No access token found",t.OpenfortErrorType.NOT_LOGGED_IN_ERROR);const i=n.SDKConfiguration.getInstance();if(!i)throw new t.OpenfortError("Configuration not found",t.OpenfortErrorType.INVALID_CONFIGURATION);const c=await a.Account.fromStorage(this.storage);let d;if(c){const e={account:c.id,...o.entropy&&{entropy:{...o.entropy.recoveryPassword&&{recoveryPassword:o.entropy.recoveryPassword},...o.entropy.encryptionSession&&{encryptionSession:o.entropy.encryptionSession},...o.entropy.passkey&&{passkey:o.entropy.passkey}}}},t=await this.iframeManager.recover(e);d=t.account}else{const e=await this.backendApiClients.accountsApi.getAccountsV2({user:s.player,accountType:o.accountType,chainType:o.chainType,chainId:o.chainId},{headers:{authorization:`Bearer ${i.baseConfiguration.publishableKey}`,"x-player-token":s.token,"x-auth-provider":s.thirdPartyProvider,"x-token-type":s.thirdPartyTokenType}});if(0===e.data.data.length){const e=o.entropy?.passkey?await this.createPasskey(s.player):void 0,t={accountType:o.accountType,chainType:o.chainType,chainId:o.chainId,...o.entropy&&{entropy:{...o.entropy.recoveryPassword&&{recoveryPassword:o.entropy.recoveryPassword},...o.entropy.encryptionSession&&{encryptionSession:o.entropy.encryptionSession},...o.entropy.passkey&&{passkey:e}}}},r=await this.iframeManager.create(t);d=r.account}else{const t={account:e.data.data[0].id,...o.entropy&&{entropy:{...o.entropy.recoveryPassword&&{recoveryPassword:o.entropy.recoveryPassword},...o.entropy.encryptionSession&&{encryptionSession:o.entropy.encryptionSession},...o.entropy.passkey&&{passkey:o.entropy.passkey}}}},r=await this.iframeManager.recover(t);d=r.account}}return t.withOpenfortError(async()=>{const e=await this.backendApiClients.accountsApi.getAccountV2({id:d},{headers:{authorization:`Bearer ${i.baseConfiguration.publishableKey}`,"x-player-token":s.token,"x-auth-provider":s.thirdPartyProvider,"x-token-type":s.thirdPartyTokenType}}),t=new a.Account({user:e.data.user,chainType:e.data.chainType,id:e.data.id,address:e.data.address,ownerAddress:e.data.ownerAddress,accountType:e.data.accountType,createdAt:e.data.createdAt,implementationType:e.data.smartAccount?.implementationType,chainId:e.data.chainId,salt:e.data.smartAccount?.salt,factoryAddress:e.data.smartAccount?.factoryAddress,recoveryMethod:a.Account.parseRecoveryMethod(e.data.recoveryMethod)});return t.save(this.storage),this.eventEmitter.emit(r.OpenfortEvents.SWITCH_ACCOUNT,e.data.address),t},{default:t.OpenfortErrorType.AUTHENTICATION_ERROR})}async sign(e,t,r,a){return await this.iframeManager.sign(e,t,r,a)}async export(){return await this.iframeManager.export()}async switchChain({chainId:e}){const t=await this.iframeManager.switchChain(e),r=await a.Account.fromStorage(this.storage);new a.Account({...r,id:t.account,chainId:e}).save(this.storage)}async create(o){const s=await this.iframeManager.create(o),i=await e.Authentication.fromStorage(this.storage);if(!i)throw new t.OpenfortError("No access token found",t.OpenfortErrorType.NOT_LOGGED_IN_ERROR);const c=n.SDKConfiguration.getInstance();if(!c)throw new t.OpenfortError("Configuration not found",t.OpenfortErrorType.INVALID_CONFIGURATION);return t.withOpenfortError(async()=>{const e=await this.backendApiClients.accountsApi.getAccountV2({id:s.account},{headers:{authorization:`Bearer ${c.baseConfiguration.publishableKey}`,"x-player-token":i.token,"x-auth-provider":i.thirdPartyProvider,"x-token-type":i.thirdPartyTokenType}}),t=new a.Account({user:e.data.user,chainType:e.data.chainType,id:e.data.id,address:e.data.address,ownerAddress:e.data.ownerAddress,accountType:e.data.accountType,createdAt:e.data.createdAt,implementationType:e.data.smartAccount?.implementationType,chainId:e.data.chainId,salt:e.data.smartAccount?.salt,factoryAddress:e.data.smartAccount?.factoryAddress,recoveryMethod:a.Account.parseRecoveryMethod(e.data.recoveryMethod),recoveryMethodDetails:e.data.recoveryMethodDetails});return t.save(this.storage),this.eventEmitter.emit(r.OpenfortEvents.SWITCH_ACCOUNT,e.data.address),t},{default:t.OpenfortErrorType.AUTHENTICATION_ERROR})}async recover(o){const s=await this.iframeManager.recover(o),i=await e.Authentication.fromStorage(this.storage);if(!i)throw new t.OpenfortError("No access token found",t.OpenfortErrorType.NOT_LOGGED_IN_ERROR);const c=n.SDKConfiguration.getInstance();if(!c)throw new t.OpenfortError("Configuration not found",t.OpenfortErrorType.INVALID_CONFIGURATION);return t.withOpenfortError(async()=>{const e=await this.backendApiClients.accountsApi.getAccountV2({id:s.account},{headers:{authorization:`Bearer ${c.baseConfiguration.publishableKey}`,"x-player-token":i.token,"x-auth-provider":i.thirdPartyProvider,"x-token-type":i.thirdPartyTokenType}}),t=new a.Account({user:e.data.user,chainType:e.data.chainType,id:e.data.id,address:e.data.address,ownerAddress:e.data.ownerAddress,accountType:e.data.accountType,createdAt:e.data.createdAt,implementationType:e.data.smartAccount?.implementationType,chainId:e.data.chainId,salt:e.data.smartAccount?.salt,factoryAddress:e.data.smartAccount?.factoryAddress,recoveryMethod:a.Account.parseRecoveryMethod(e.data.recoveryMethod)});return t.save(this.storage),this.eventEmitter.emit(r.OpenfortEvents.SWITCH_ACCOUNT,e.data.address),t},{default:t.OpenfortErrorType.AUTHENTICATION_ERROR})}async setRecoveryMethod({recoveryMethod:e,recoveryPassword:t,encryptionSession:r}){await this.iframeManager.setRecoveryMethod(e,t,r)}async disconnect(){await this.iframeManager.disconnect(),this.storage.remove(o.StorageKeys.ACCOUNT)}};
@@ -1 +1 @@
1
- import{BackendApiClients as e}from"../../../packages/internal/openapi-clients/dist/index.js";import{PasskeyHandler as r}from"../core/configuration/passkey.js";import{SDKConfiguration as a}from"../core/config/config.js";import{Account as t}from"../core/configuration/account.js";import{Authentication as s}from"../core/configuration/authentication.js";import{OpenfortError as n,OpenfortErrorType as i,withOpenfortError as o}from"../core/errors/openfortError.js";import{OpenfortEvents as d,RecoveryMethod as c,AccountTypeEnum as h,ChainTypeEnum as y,EmbeddedState as p}from"../types/types.js";import{debugLog as g}from"../utils/debug.js";import{EmbeddedSigner as m}from"../wallets/embedded.js";import{EvmProvider as l}from"../wallets/evm/evmProvider.js";import"../wallets/evm/types.js";import{announceProvider as f,openfortProviderInfo as u}from"../wallets/evm/provider/eip6963.js";import{signMessage as A}from"../wallets/evm/walletHelpers.js";import{IframeManager as I}from"../wallets/iframeManager.js";import{ReactNativeMessenger as w}from"../wallets/messaging/ReactNativeMessenger.js";import"../wallets/messaging/browserMessenger/backwardCompatibility.js";import v from"../wallets/messaging/browserMessenger/messengers/WindowMessenger.js";class M{storage;validateAndRefreshToken;ensureInitialized;eventEmitter;passkeyHandler;iframeManager=null;iframeManagerPromise=null;signer=null;signerPromise=null;provider=null;messagePoster=null;messenger=null;constructor(e,r,a,t,s){this.storage=e,this.validateAndRefreshToken=r,this.ensureInitialized=a,this.eventEmitter=t,this.passkeyHandler=s,this.eventEmitter.on(d.LOGGED_OUT,()=>{g("Handling logout event in EmbeddedWalletApi"),this.handleLogout()})}get backendApiClients(){const r=a.getInstance();if(!r)throw new n("Configuration not found",i.INVALID_CONFIGURATION);return new e({basePath:r.backendUrl,accessToken:r.baseConfiguration.publishableKey})}async getIframeManager(){if(g("[HANDSHAKE DEBUG] getIframeManager called"),this.iframeManager)return g("[HANDSHAKE DEBUG] Returning existing iframeManager instance"),this.iframeManager;if(this.iframeManagerPromise)return g("[HANDSHAKE DEBUG] Returning existing iframeManagerPromise"),this.iframeManagerPromise;g("[HANDSHAKE DEBUG] Creating new iframeManager"),this.iframeManagerPromise=this.createIframeManager();try{return g("[HANDSHAKE DEBUG] Awaiting iframeManager creation"),this.iframeManager=await this.iframeManagerPromise,g("[HANDSHAKE DEBUG] IframeManager created successfully"),this.iframeManagerPromise=null,this.iframeManager}catch(e){throw g("[HANDSHAKE DEBUG] Error creating iframeManager:",e),this.iframeManagerPromise=null,e}}async createIframeManager(){g("[HANDSHAKE DEBUG] createIframeManager starting");const e=a.getInstance();if(!e)throw g("[HANDSHAKE DEBUG] Configuration not found"),new n("Configuration not found",i.INVALID_CONFIGURATION);let r;if(g("[HANDSHAKE DEBUG] Configuration found"),this.messagePoster)g("[HANDSHAKE DEBUG] Creating ReactNativeMessenger with messagePoster"),this.messenger=new w(this.messagePoster),g("[HANDSHAKE DEBUG] Created new ReactNativeMessenger instance"),r=this.messenger;else{g("[HANDSHAKE DEBUG] Creating WindowMessenger for browser mode");const a=this.createIframe(e.iframeUrl),t=new URL(e.iframeUrl).origin;r=new v({remoteWindow:a.contentWindow,allowedOrigins:[t]}),g("[HANDSHAKE DEBUG] Created WindowMessenger")}return g("[HANDSHAKE DEBUG] Creating IframeManager instance"),new I(e,this.storage,r)}async ensureSigner(){if(this.signer)return this.signer;if(this.signerPromise)return this.signerPromise;this.signerPromise=this.createSigner();try{return this.signer=await this.signerPromise,this.signer}catch(e){throw this.signerPromise=null,e}finally{this.signerPromise=null}}async createSigner(){const e=await this.getIframeManager();return new m(e,this.storage,this.backendApiClients,this.eventEmitter)}createIframe(e){if("undefined"==typeof document)throw new n("Document is not available. Please provide a message poster for non-browser environments.",i.INVALID_CONFIGURATION);const r=document.getElementById("openfort-iframe");r&&r.remove();const a=document.createElement("iframe");return a.style.display="none",a.id="openfort-iframe",a.src=e,document.body.appendChild(a),a}async getPasskeyKey(e){const r=await s.fromStorage(this.storage);return await this.passkeyHandler.deriveAndExportKey({id:e,seed:r.player})}async getEntropy(e){switch(e.recoveryMethod){case c.PASSWORD:return{recoveryPassword:e.password};case c.AUTOMATIC:return{encryptionSession:e.encryptionSession};case c.PASSKEY:return{passkey:{id:e.passkeyInfo?.passkeyId,env:e.passkeyInfo?.passkeyEnv,key:e.passkeyInfo?.passkeyKey||await this.getPasskeyKey(e.passkeyInfo?.passkeyId)}};default:throw new n("Invalid recovery method",i.INVALID_CONFIGURATION)}}async configure(e){await this.validateAndRefreshToken();const a=e.recoveryParams??{recoveryMethod:c.AUTOMATIC},o=await s.fromStorage(this.storage),d=await t.fromStorage(this.storage);if(a.recoveryMethod===c.PASSKEY){if(!a.passkeyInfo?.passkeyId||!a.passkeyInfo?.passkeyEnv)throw new n("Passkey ID and Name must be provided for passkey recovery",i.INVALID_CONFIGURATION);if(d)a.passkeyInfo={passkeyId:d.recoveryMethodDetails?.passkeyId};else{const e=await this.passkeyHandler.createPasskey({id:r.randomPasskeyName(),displayName:"Openfort - Embedded Wallet",seed:o?.player});a.passkeyInfo={passkeyId:e.id,passkeyKey:e.key}}}const[p,g]=await Promise.all([this.ensureSigner(),this.getEntropy(a)]),m={chainId:e.chainId,entropy:g,accountType:e.accountType??h.SMART_ACCOUNT,chainType:e.chainType??y.EVM},l=await p.configure(m);return{id:l.id,chainId:l.chainId,user:o.player,address:l.address,ownerAddress:l.ownerAddress,chainType:l.chainType,accountType:l.accountType,implementationType:l.implementationType,createdAt:l.createdAt,recoveryMethod:t.parseRecoveryMethod(l.recoveryMethod),recoveryMethodDetails:l.recoveryMethodDetails}}async create(e){await this.validateAndRefreshToken();const a=e.recoveryParams??{recoveryMethod:c.AUTOMATIC},o=await s.fromStorage(this.storage);if(a.recoveryMethod===c.PASSKEY){if(!a.passkeyInfo?.passkeyId||!a.passkeyInfo?.passkeyEnv)throw new n("Passkey ID and Name must be provided for passkey recovery",i.INVALID_CONFIGURATION);const e=await this.passkeyHandler.createPasskey({id:r.randomPasskeyName(),displayName:"Openfort - Embedded Wallet",seed:o?.player});a.passkeyInfo={passkeyId:e.id,passkeyKey:e.key}}const[d,h]=await Promise.all([this.ensureSigner(),this.getEntropy(a)]),y=await d.create({accountType:e.accountType,chainType:e.chainType,chainId:e.chainId,entropy:h});return{id:y.id,chainId:y.chainId,user:o.player,address:y.address,ownerAddress:y.ownerAddress,chainType:y.chainType,accountType:y.accountType,implementationType:y.implementationType,createdAt:y.createdAt,recoveryMethod:t.parseRecoveryMethod(y.recoveryMethod),recoveryMethodDetails:y.recoveryMethodDetails}}async recover(e){await this.validateAndRefreshToken();const r=e.recoveryParams??{recoveryMethod:c.AUTOMATIC};if(r.recoveryMethod===c.PASSKEY){if(!r.passkeyInfo?.passkeyId||!r.passkeyInfo?.passkeyEnv)throw new n("Passkey ID and Name must be provided for passkey recovery",i.INVALID_CONFIGURATION);r.passkeyInfo={passkeyId:r.passkeyInfo.passkeyId,passkeyEnv:r.passkeyInfo.passkeyEnv}}const[a,o,d]=await Promise.all([this.ensureSigner(),this.getEntropy(r),s.fromStorage(this.storage)]),h=await a.recover({account:e.account,entropy:o});return{id:h.id,chainId:h.chainId,user:d.player,address:h.address,ownerAddress:h.ownerAddress,chainType:h.chainType,accountType:h.accountType,implementationType:h.implementationType,createdAt:h.createdAt,recoveryMethod:t.parseRecoveryMethod(h.recoveryMethod),recoveryMethodDetails:h.recoveryMethodDetails}}async signMessage(e,r){await this.validateAndRefreshToken();const a=await this.ensureSigner(),{hashMessage:s=!0,arrayifyMessage:n=!1}=r||{},i=await t.fromStorage(this.storage);return await a.sign(e,n,s,i?.chainType)}async signTypedData(e,r,a){await this.validateAndRefreshToken();const s=await this.ensureSigner(),o=await t.fromStorage(this.storage);if(!o)throw new n("No account found",i.MISSING_SIGNER_ERROR);const d={...r};delete d.EIP712Domain;const{_TypedDataEncoder:c}=await import("@ethersproject/hash"),h=c.hash(e,d,a);return await A({hash:h,implementationType:o.implementationType||o.type,chainId:Number(o.chainId),signer:s,address:o.address,ownerAddress:o.ownerAddress,factoryAddress:o.factoryAddress,salt:o.salt})}async exportPrivateKey(){await this.validateAndRefreshToken();const e=await this.ensureSigner();return await e.export()}async setRecoveryMethod(e,r){await this.validateAndRefreshToken();const a=await this.ensureSigner();let s,o,d;if(e.recoveryMethod===c.PASSKEY?d=e.passkeyInfo:r.recoveryMethod===c.PASSKEY&&(d=r.passkeyInfo),console.log(`Passkey info is ${JSON.stringify(d)}`),e.recoveryMethod===c.PASSWORD?s=e.password:r.recoveryMethod===c.PASSWORD&&(s=r.password),e.recoveryMethod===c.AUTOMATIC?o=e.encryptionSession:r.recoveryMethod===c.AUTOMATIC&&(o=r.encryptionSession),!s&&!o)throw new n("Password or encryption session is not provided",i.INVALID_CONFIGURATION);await a.setRecoveryMethod({recoveryMethod:r.recoveryMethod,recoveryPassword:s,encryptionSession:o});const h=await t.fromStorage(this.storage);h&&new t({...h,recoveryMethod:r.recoveryMethod}).save(this.storage)}async get(){const e=await t.fromStorage(this.storage);if(!e)throw new n("No signer configured",i.MISSING_SIGNER_ERROR);const r=await s.fromStorage(this.storage);if(!r)throw new n("No access token found",i.NOT_LOGGED_IN_ERROR);return{id:e.id,chainId:e.chainId,user:r.player,address:e.address,ownerAddress:e.ownerAddress,chainType:e.chainType,accountType:e.accountType,implementationType:e.implementationType,createdAt:e.createdAt,recoveryMethod:t.parseRecoveryMethod(e.recoveryMethod),recoveryMethodDetails:e.recoveryMethodDetails}}async list(e){const r={accountType:h.SMART_ACCOUNT,...e},d=a.getInstance();if(!d)throw new n("Configuration not found",i.INVALID_CONFIGURATION);await this.validateAndRefreshToken();const c=await s.fromStorage(this.storage);if(!c)throw new n("No access token found",i.NOT_LOGGED_IN_ERROR);return o(async()=>(await this.backendApiClients.accountsApi.getAccountsV2(r,{headers:{authorization:`Bearer ${d.baseConfiguration.publishableKey}`,"x-player-token":c.token,"x-auth-provider":c.thirdPartyProvider,"x-token-type":c.thirdPartyTokenType}})).data.data.map(e=>({user:e.user,chainType:e.chainType,id:e.id,address:e.address,ownerAddress:e.ownerAddress,accountType:e.accountType,createdAt:e.createdAt,implementationType:e.smartAccount?.implementationType,chainId:e.chainId,recoveryMethod:t.parseRecoveryMethod(e.recoveryMethod),recoveryMethodDetails:e.recoveryMethodDetails})),{default:i.AUTHENTICATION_ERROR})}async getEmbeddedState(){try{if(!await s.fromStorage(this.storage))return p.UNAUTHENTICATED;return await t.fromStorage(this.storage)?p.READY:p.EMBEDDED_SIGNER_NOT_CONFIGURED}catch(e){return g("Failed to get embedded state:",e),p.UNAUTHENTICATED}}async getEthereumProvider(e){await this.ensureInitialized();const r={announceProvider:!0,...e},a=await s.fromStorage(this.storage),n=await t.fromStorage(this.storage);return this.provider?this.provider&&r.policy&&this.provider.updatePolicy(r.policy):(this.provider=new l({storage:this.storage,openfortEventEmitter:this.eventEmitter,ensureSigner:this.ensureSigner.bind(this),account:n||void 0,authentication:a||void 0,backendApiClients:this.backendApiClients,policyId:r.policy,validateAndRefreshSession:this.validateAndRefreshToken.bind(this),chains:r.chains}),r.announceProvider&&f({info:{...u,...r.providerInfo},provider:this.provider})),this.provider}async ping(e){try{e>0&&await new Promise(r=>{setTimeout(r,e)});const r=await this.getIframeManager();if(!r.isLoaded())return!1;const a=await s.fromStorage(this.storage);if(a)try{return await r.getCurrentDevice(a.player),!0}catch(e){return!1}return r.isLoaded()}catch(e){return g("Ping failed:",e),!1}}getURL(){const e=a.getInstance();if(!e)throw new n("Configuration not found",i.INVALID_CONFIGURATION);return e.iframeUrl}async setMessagePoster(e){if(!e||"function"!=typeof e.postMessage)throw new n("Invalid message poster",i.INVALID_CONFIGURATION);this.messagePoster=e,this.messenger&&this.messenger.destroy(),this.iframeManager&&this.iframeManager.destroy(),this.signer=null,this.signerPromise=null,this.iframeManager=null,this.iframeManagerPromise=null,this.messenger=null}async handleTokenRefreshed(){if(this.iframeManager)try{await this.iframeManager.updateAuthentication(),g("Updated IframeManager authentication after token refresh")}catch(e){g("Failed to update IframeManager authentication:",e)}else g("IframeManager not initialized, skipping authentication update")}async handleLogout(){const e=await this.ensureSigner();await e.disconnect(),this.provider=null,this.messenger=null,this.iframeManager=null,this.iframeManagerPromise=null,this.signer=null,this.signerPromise=null}async onMessage(e){if(!e||"object"!=typeof e)return void g("Invalid message received:",e);g("[HANDSHAKE DEBUG] EmbeddedWalletApi onMessage:",e);const r="penpal"===e.namespace&&"SYN"===e.type||e.penpal&&"string"==typeof e.penpal;if(r&&this.messenger&&this.messagePoster)return g("[HANDSHAKE DEBUG] Passing message directly to existing ReactNativeMessenger"),void this.messenger.handleMessage(e);const a=await this.getIframeManager();g(`[HANDSHAKE DEBUG] IframeManager obtained, isLoaded: ${a.isLoaded()}`),r&&!a.isLoaded()&&g("[HANDSHAKE DEBUG] Received penpal message before connection initialized, setting up connection..."),g("[HANDSHAKE DEBUG] Calling iframeManager.onMessage"),await a.onMessage(e),g("[HANDSHAKE DEBUG] iframeManager.onMessage completed")}isReady(){return this.iframeManager?.isLoaded()||!1}}export{M as EmbeddedWalletApi};
1
+ import{BackendApiClients as e}from"../../../packages/internal/openapi-clients/dist/index.js";import{PasskeyHandler as r}from"../core/configuration/passkey.js";import{SDKConfiguration as t}from"../core/config/config.js";import{Account as a}from"../core/configuration/account.js";import{Authentication as s}from"../core/configuration/authentication.js";import{OpenfortError as i,OpenfortErrorType as n,withOpenfortError as o}from"../core/errors/openfortError.js";import{OpenfortEvents as d,RecoveryMethod as c,AccountTypeEnum as h,ChainTypeEnum as g,EmbeddedState as y}from"../types/types.js";import{debugLog as m}from"../utils/debug.js";import{EmbeddedSigner as p}from"../wallets/embedded.js";import{EvmProvider as l}from"../wallets/evm/evmProvider.js";import"../wallets/evm/types.js";import{announceProvider as f,openfortProviderInfo as u}from"../wallets/evm/provider/eip6963.js";import{signMessage as A}from"../wallets/evm/walletHelpers.js";import{IframeManager as w}from"../wallets/iframeManager.js";import{ReactNativeMessenger as v}from"../wallets/messaging/ReactNativeMessenger.js";import"../wallets/messaging/browserMessenger/backwardCompatibility.js";import I from"../wallets/messaging/browserMessenger/messengers/WindowMessenger.js";class M{storage;validateAndRefreshToken;ensureInitialized;eventEmitter;passkeyHandler;iframeManager=null;iframeManagerPromise=null;signer=null;signerPromise=null;provider=null;messagePoster=null;messenger=null;constructor(e,r,t,a,s){this.storage=e,this.validateAndRefreshToken=r,this.ensureInitialized=t,this.eventEmitter=a,this.passkeyHandler=s,this.eventEmitter.on(d.LOGGED_OUT,()=>{m("Handling logout event in EmbeddedWalletApi"),this.handleLogout()})}get backendApiClients(){const r=t.getInstance();if(!r)throw new i("Configuration not found",n.INVALID_CONFIGURATION);return new e({basePath:r.backendUrl,accessToken:r.baseConfiguration.publishableKey})}async getIframeManager(){if(m("[HANDSHAKE DEBUG] getIframeManager called"),this.iframeManager)return m("[HANDSHAKE DEBUG] Returning existing iframeManager instance"),this.iframeManager;if(this.iframeManagerPromise)return m("[HANDSHAKE DEBUG] Returning existing iframeManagerPromise"),this.iframeManagerPromise;m("[HANDSHAKE DEBUG] Creating new iframeManager"),this.iframeManagerPromise=this.createIframeManager();try{return m("[HANDSHAKE DEBUG] Awaiting iframeManager creation"),this.iframeManager=await this.iframeManagerPromise,m("[HANDSHAKE DEBUG] IframeManager created successfully"),this.iframeManagerPromise=null,this.iframeManager}catch(e){throw m("[HANDSHAKE DEBUG] Error creating iframeManager:",e),this.iframeManagerPromise=null,e}}async createIframeManager(){m("[HANDSHAKE DEBUG] createIframeManager starting");const e=t.getInstance();if(!e)throw m("[HANDSHAKE DEBUG] Configuration not found"),new i("Configuration not found",n.INVALID_CONFIGURATION);let r;if(m("[HANDSHAKE DEBUG] Configuration found"),this.messagePoster)m("[HANDSHAKE DEBUG] Creating ReactNativeMessenger with messagePoster"),this.messenger=new v(this.messagePoster),m("[HANDSHAKE DEBUG] Created new ReactNativeMessenger instance"),r=this.messenger;else{m("[HANDSHAKE DEBUG] Creating WindowMessenger for browser mode");const t=this.createIframe(e.iframeUrl),a=new URL(e.iframeUrl).origin;r=new I({remoteWindow:t.contentWindow,allowedOrigins:[a]}),m("[HANDSHAKE DEBUG] Created WindowMessenger")}return m("[HANDSHAKE DEBUG] Creating IframeManager instance"),new w(e,this.storage,r)}async ensureSigner(){if(this.signer)return this.signer;if(this.signerPromise)return this.signerPromise;this.signerPromise=this.createSigner();try{return this.signer=await this.signerPromise,this.signer}catch(e){throw this.signerPromise=null,e}finally{this.signerPromise=null}}async createSigner(){const e=await this.getIframeManager();return new p(e,this.storage,this.backendApiClients,this.passkeyHandler,this.eventEmitter)}createIframe(e){if("undefined"==typeof document)throw new i("Document is not available. Please provide a message poster for non-browser environments.",n.INVALID_CONFIGURATION);const r=document.getElementById("openfort-iframe");r&&r.remove();const t=document.createElement("iframe");return t.style.display="none",t.id="openfort-iframe",t.src=e,document.body.appendChild(t),t}async getPasskeyKey(e){const r=await s.fromStorage(this.storage);return await this.passkeyHandler.deriveAndExportKey({id:e,seed:r.player})}async getEntropy(e,r=!0){switch(e.recoveryMethod){case c.PASSWORD:return{recoveryPassword:e.password};case c.AUTOMATIC:return{encryptionSession:e.encryptionSession};case c.PASSKEY:return r?{passkey:{id:e.passkeyInfo?.passkeyId,env:e.passkeyInfo?.passkeyEnv,key:e.passkeyInfo?.passkeyKey||await this.getPasskeyKey(e.passkeyInfo?.passkeyId)}}:{passkey:{}};default:throw new i("Invalid recovery method",n.INVALID_CONFIGURATION)}}async configure(e){await this.validateAndRefreshToken();const r=e.recoveryParams??{recoveryMethod:c.AUTOMATIC},[t,i,n]=await Promise.all([s.fromStorage(this.storage),this.ensureSigner(),this.getEntropy(r,!1)]),o={chainId:e.chainId,entropy:n,accountType:e.accountType??h.SMART_ACCOUNT,chainType:e.chainType??g.EVM},d=await i.configure(o);return{id:d.id,chainId:d.chainId,user:t.player,address:d.address,ownerAddress:d.ownerAddress,chainType:d.chainType,accountType:d.accountType,implementationType:d.implementationType,createdAt:d.createdAt,recoveryMethod:a.parseRecoveryMethod(d.recoveryMethod),recoveryMethodDetails:d.recoveryMethodDetails}}async create(e){await this.validateAndRefreshToken();const t=e.recoveryParams??{recoveryMethod:c.AUTOMATIC},i=await s.fromStorage(this.storage);if(t.recoveryMethod===c.PASSKEY){const e=await this.passkeyHandler.createPasskey({id:r.randomPasskeyName(),displayName:"Openfort - Embedded Wallet",seed:i?.player});t.passkeyInfo={passkeyId:e.id,passkeyKey:e.key}}const[n,o]=await Promise.all([this.ensureSigner(),this.getEntropy(t)]),d=await n.create({accountType:e.accountType,chainType:e.chainType,chainId:e.chainId,entropy:o});return{id:d.id,chainId:d.chainId,user:i.player,address:d.address,ownerAddress:d.ownerAddress,chainType:d.chainType,accountType:d.accountType,implementationType:d.implementationType,createdAt:d.createdAt,recoveryMethod:a.parseRecoveryMethod(d.recoveryMethod),recoveryMethodDetails:d.recoveryMethodDetails}}async recover(e){await this.validateAndRefreshToken();const r=e.recoveryParams??{recoveryMethod:c.AUTOMATIC};if(r.recoveryMethod===c.PASSKEY){if(!r.passkeyInfo?.passkeyId)throw new i("Passkey ID must be provided for passkey recovery",n.INVALID_CONFIGURATION);r.passkeyInfo={passkeyId:r.passkeyInfo.passkeyId,passkeyEnv:r.passkeyInfo.passkeyEnv}}const[t,o,d]=await Promise.all([this.ensureSigner(),this.getEntropy(r),s.fromStorage(this.storage)]),h=await t.recover({account:e.account,entropy:o});return{id:h.id,chainId:h.chainId,user:d.player,address:h.address,ownerAddress:h.ownerAddress,chainType:h.chainType,accountType:h.accountType,implementationType:h.implementationType,createdAt:h.createdAt,recoveryMethod:a.parseRecoveryMethod(h.recoveryMethod),recoveryMethodDetails:h.recoveryMethodDetails}}async signMessage(e,r){await this.validateAndRefreshToken();const t=await this.ensureSigner(),{hashMessage:s=!0,arrayifyMessage:i=!1}=r||{},n=await a.fromStorage(this.storage);return await t.sign(e,i,s,n?.chainType)}async signTypedData(e,r,t){await this.validateAndRefreshToken();const s=await this.ensureSigner(),o=await a.fromStorage(this.storage);if(!o)throw new i("No account found",n.MISSING_SIGNER_ERROR);const d={...r};delete d.EIP712Domain;const{_TypedDataEncoder:c}=await import("@ethersproject/hash"),h=c.hash(e,d,t);return await A({hash:h,implementationType:o.implementationType||o.type,chainId:Number(o.chainId),signer:s,address:o.address,ownerAddress:o.ownerAddress,factoryAddress:o.factoryAddress,salt:o.salt})}async exportPrivateKey(){await this.validateAndRefreshToken();const e=await this.ensureSigner();return await e.export()}async setRecoveryMethod(e,r){await this.validateAndRefreshToken();const t=await this.ensureSigner();let s,o;if(e.recoveryMethod===c.PASSWORD?s=e.password:r.recoveryMethod===c.PASSWORD&&(s=r.password),e.recoveryMethod===c.AUTOMATIC?o=e.encryptionSession:r.recoveryMethod===c.AUTOMATIC&&(o=r.encryptionSession),!s&&!o)throw new i("Password or encryption session is not provided",n.INVALID_CONFIGURATION);await t.setRecoveryMethod({recoveryMethod:r.recoveryMethod,recoveryPassword:s,encryptionSession:o});const d=await a.fromStorage(this.storage);d&&new a({...d,recoveryMethod:r.recoveryMethod}).save(this.storage)}async get(){const e=await a.fromStorage(this.storage);if(!e)throw new i("No signer configured",n.MISSING_SIGNER_ERROR);const r=await s.fromStorage(this.storage);if(!r)throw new i("No access token found",n.NOT_LOGGED_IN_ERROR);return{id:e.id,chainId:e.chainId,user:r.player,address:e.address,ownerAddress:e.ownerAddress,chainType:e.chainType,accountType:e.accountType,implementationType:e.implementationType,createdAt:e.createdAt,recoveryMethod:a.parseRecoveryMethod(e.recoveryMethod),recoveryMethodDetails:e.recoveryMethodDetails}}async list(e){const r={accountType:h.SMART_ACCOUNT,...e},d=t.getInstance();if(!d)throw new i("Configuration not found",n.INVALID_CONFIGURATION);await this.validateAndRefreshToken();const c=await s.fromStorage(this.storage);if(!c)throw new i("No access token found",n.NOT_LOGGED_IN_ERROR);return o(async()=>(await this.backendApiClients.accountsApi.getAccountsV2(r,{headers:{authorization:`Bearer ${d.baseConfiguration.publishableKey}`,"x-player-token":c.token,"x-auth-provider":c.thirdPartyProvider,"x-token-type":c.thirdPartyTokenType}})).data.data.map(e=>({user:e.user,chainType:e.chainType,id:e.id,address:e.address,ownerAddress:e.ownerAddress,accountType:e.accountType,createdAt:e.createdAt,implementationType:e.smartAccount?.implementationType,chainId:e.chainId,recoveryMethod:a.parseRecoveryMethod(e.recoveryMethod),recoveryMethodDetails:e.recoveryMethodDetails})),{default:n.AUTHENTICATION_ERROR})}async getEmbeddedState(){try{if(!await s.fromStorage(this.storage))return y.UNAUTHENTICATED;return await a.fromStorage(this.storage)?y.READY:y.EMBEDDED_SIGNER_NOT_CONFIGURED}catch(e){return m("Failed to get embedded state:",e),y.UNAUTHENTICATED}}async getEthereumProvider(e){await this.ensureInitialized();const r={announceProvider:!0,...e},t=await s.fromStorage(this.storage),i=await a.fromStorage(this.storage);return this.provider?this.provider&&r.policy&&this.provider.updatePolicy(r.policy):(this.provider=new l({storage:this.storage,openfortEventEmitter:this.eventEmitter,ensureSigner:this.ensureSigner.bind(this),account:i||void 0,authentication:t||void 0,backendApiClients:this.backendApiClients,policyId:r.policy,validateAndRefreshSession:this.validateAndRefreshToken.bind(this),chains:r.chains}),r.announceProvider&&f({info:{...u,...r.providerInfo},provider:this.provider})),this.provider}async ping(e){try{e>0&&await new Promise(r=>{setTimeout(r,e)});const r=await this.getIframeManager();if(!r.isLoaded())return!1;const t=await s.fromStorage(this.storage);if(t)try{return await r.getCurrentDevice(t.player),!0}catch(e){return!1}return r.isLoaded()}catch(e){return m("Ping failed:",e),!1}}getURL(){const e=t.getInstance();if(!e)throw new i("Configuration not found",n.INVALID_CONFIGURATION);return e.iframeUrl}async setMessagePoster(e){if(!e||"function"!=typeof e.postMessage)throw new i("Invalid message poster",n.INVALID_CONFIGURATION);this.messagePoster=e,this.messenger&&this.messenger.destroy(),this.iframeManager&&this.iframeManager.destroy(),this.signer=null,this.signerPromise=null,this.iframeManager=null,this.iframeManagerPromise=null,this.messenger=null}async handleTokenRefreshed(){if(this.iframeManager)try{await this.iframeManager.updateAuthentication(),m("Updated IframeManager authentication after token refresh")}catch(e){m("Failed to update IframeManager authentication:",e)}else m("IframeManager not initialized, skipping authentication update")}async handleLogout(){const e=await this.ensureSigner();await e.disconnect(),this.provider=null,this.messenger=null,this.iframeManager=null,this.iframeManagerPromise=null,this.signer=null,this.signerPromise=null}async onMessage(e){if(!e||"object"!=typeof e)return void m("Invalid message received:",e);m("[HANDSHAKE DEBUG] EmbeddedWalletApi onMessage:",e);const r="penpal"===e.namespace&&"SYN"===e.type||e.penpal&&"string"==typeof e.penpal;if(r&&this.messenger&&this.messagePoster)return m("[HANDSHAKE DEBUG] Passing message directly to existing ReactNativeMessenger"),void this.messenger.handleMessage(e);const t=await this.getIframeManager();m(`[HANDSHAKE DEBUG] IframeManager obtained, isLoaded: ${t.isLoaded()}`),r&&!t.isLoaded()&&m("[HANDSHAKE DEBUG] Received penpal message before connection initialized, setting up connection..."),m("[HANDSHAKE DEBUG] Calling iframeManager.onMessage"),await t.onMessage(e),m("[HANDSHAKE DEBUG] iframeManager.onMessage completed")}isReady(){return this.iframeManager?.isLoaded()||!1}}export{M as EmbeddedWalletApi};
@@ -1 +1 @@
1
- const o="0.10.12",t="@openfort/openfort-js";export{t as PACKAGE,o as VERSION};
1
+ const o="0.10.13",t="@openfort/openfort-js";export{t as PACKAGE,o as VERSION};
@@ -1 +1 @@
1
- import{Authentication as e}from"../core/configuration/authentication.js";import{OpenfortError as t,OpenfortErrorType as a,withOpenfortError as r}from"../core/errors/openfortError.js";import{OpenfortEvents as o}from"../types/types.js";import{Account as n}from"../core/configuration/account.js";import"./messaging/browserMessenger/backwardCompatibility.js";import{StorageKeys as s}from"../storage/istorage.js";import{SDKConfiguration as i}from"../core/config/config.js";import"./types.js";import"../core/errors/sentry.js";import"jose";import"human-id";import"./evm/types.js";import"./evm/JsonRpcError.js";import"eventemitter3";import"./evm/provider/eip6963.js";class c{iframeManager;storage;backendApiClients;eventEmitter;constructor(e,t,a,r){this.iframeManager=e,this.storage=t,this.backendApiClients=a,this.eventEmitter=r}async configure(s){const c=await e.fromStorage(this.storage);if(!c)throw new t("No access token found",a.NOT_LOGGED_IN_ERROR);const d=i.getInstance();if(!d)throw new t("Configuration not found",a.INVALID_CONFIGURATION);const y=await n.fromStorage(this.storage);let p;if(y){const e={account:y.id,...s.entropy&&{entropy:{...s.entropy.recoveryPassword&&{recoveryPassword:s.entropy.recoveryPassword},...s.entropy.encryptionSession&&{encryptionSession:s.entropy.encryptionSession},...s.entropy.passkey&&{passkey:s.entropy.passkey}}}},t=await this.iframeManager.recover(e);p=t.account}else{const e=await this.backendApiClients.accountsApi.getAccountsV2({user:c.player,accountType:s.accountType,chainType:s.chainType,chainId:s.chainId},{headers:{authorization:`Bearer ${d.baseConfiguration.publishableKey}`,"x-player-token":c.token,"x-auth-provider":c.thirdPartyProvider,"x-token-type":c.thirdPartyTokenType}});if(0===e.data.data.length){const e={accountType:s.accountType,chainType:s.chainType,chainId:s.chainId,...s.entropy&&{entropy:{...s.entropy.recoveryPassword&&{recoveryPassword:s.entropy.recoveryPassword},...s.entropy.encryptionSession&&{encryptionSession:s.entropy.encryptionSession},...s.entropy.passkey&&{passkey:s.entropy.passkey}}}},t=await this.iframeManager.create(e);p=t.account}else{const t={account:e.data.data[0].id,...s.entropy&&{entropy:{...s.entropy.recoveryPassword&&{recoveryPassword:s.entropy.recoveryPassword},...s.entropy.encryptionSession&&{encryptionSession:s.entropy.encryptionSession},...s.entropy.passkey&&{passkey:s.entropy.passkey}}}},a=await this.iframeManager.recover(t);p=a.account}}return r(async()=>{const e=await this.backendApiClients.accountsApi.getAccountV2({id:p},{headers:{authorization:`Bearer ${d.baseConfiguration.publishableKey}`,"x-player-token":c.token,"x-auth-provider":c.thirdPartyProvider,"x-token-type":c.thirdPartyTokenType}}),t=new n({user:e.data.user,chainType:e.data.chainType,id:e.data.id,address:e.data.address,ownerAddress:e.data.ownerAddress,accountType:e.data.accountType,createdAt:e.data.createdAt,implementationType:e.data.smartAccount?.implementationType,chainId:e.data.chainId,salt:e.data.smartAccount?.salt,factoryAddress:e.data.smartAccount?.factoryAddress,recoveryMethod:n.parseRecoveryMethod(e.data.recoveryMethod)});return t.save(this.storage),this.eventEmitter.emit(o.SWITCH_ACCOUNT,e.data.address),t},{default:a.AUTHENTICATION_ERROR})}async sign(e,t,a,r){return await this.iframeManager.sign(e,t,a,r)}async export(){return await this.iframeManager.export()}async switchChain({chainId:e}){const t=await this.iframeManager.switchChain(e),a=await n.fromStorage(this.storage);new n({...a,id:t.account,chainId:e}).save(this.storage)}async create(s){const c=await this.iframeManager.create(s),d=await e.fromStorage(this.storage);if(!d)throw new t("No access token found",a.NOT_LOGGED_IN_ERROR);const y=i.getInstance();if(!y)throw new t("Configuration not found",a.INVALID_CONFIGURATION);return r(async()=>{const e=await this.backendApiClients.accountsApi.getAccountV2({id:c.account},{headers:{authorization:`Bearer ${y.baseConfiguration.publishableKey}`,"x-player-token":d.token,"x-auth-provider":d.thirdPartyProvider,"x-token-type":d.thirdPartyTokenType}}),t=new n({user:e.data.user,chainType:e.data.chainType,id:e.data.id,address:e.data.address,ownerAddress:e.data.ownerAddress,accountType:e.data.accountType,createdAt:e.data.createdAt,implementationType:e.data.smartAccount?.implementationType,chainId:e.data.chainId,salt:e.data.smartAccount?.salt,factoryAddress:e.data.smartAccount?.factoryAddress,recoveryMethod:n.parseRecoveryMethod(e.data.recoveryMethod),recoveryMethodDetails:e.data.recoveryMethodDetails});return t.save(this.storage),this.eventEmitter.emit(o.SWITCH_ACCOUNT,e.data.address),t},{default:a.AUTHENTICATION_ERROR})}async recover(s){const c=await this.iframeManager.recover(s),d=await e.fromStorage(this.storage);if(!d)throw new t("No access token found",a.NOT_LOGGED_IN_ERROR);const y=i.getInstance();if(!y)throw new t("Configuration not found",a.INVALID_CONFIGURATION);return r(async()=>{const e=await this.backendApiClients.accountsApi.getAccountV2({id:c.account},{headers:{authorization:`Bearer ${y.baseConfiguration.publishableKey}`,"x-player-token":d.token,"x-auth-provider":d.thirdPartyProvider,"x-token-type":d.thirdPartyTokenType}}),t=new n({user:e.data.user,chainType:e.data.chainType,id:e.data.id,address:e.data.address,ownerAddress:e.data.ownerAddress,accountType:e.data.accountType,createdAt:e.data.createdAt,implementationType:e.data.smartAccount?.implementationType,chainId:e.data.chainId,salt:e.data.smartAccount?.salt,factoryAddress:e.data.smartAccount?.factoryAddress,recoveryMethod:n.parseRecoveryMethod(e.data.recoveryMethod)});return t.save(this.storage),this.eventEmitter.emit(o.SWITCH_ACCOUNT,e.data.address),t},{default:a.AUTHENTICATION_ERROR})}async setRecoveryMethod({recoveryMethod:e,recoveryPassword:t,encryptionSession:a}){await this.iframeManager.setRecoveryMethod(e,t,a)}async disconnect(){await this.iframeManager.disconnect(),this.storage.remove(s.ACCOUNT)}}export{c as EmbeddedSigner};
1
+ import{Authentication as e}from"../core/configuration/authentication.js";import{OpenfortError as t,OpenfortErrorType as a,withOpenfortError as r}from"../core/errors/openfortError.js";import{OpenfortEvents as o}from"../types/types.js";import{Account as s}from"../core/configuration/account.js";import"./messaging/browserMessenger/backwardCompatibility.js";import{StorageKeys as n}from"../storage/istorage.js";import{SDKConfiguration as i}from"../core/config/config.js";import"./types.js";import"../core/errors/sentry.js";import"jose";import{PasskeyHandler as c}from"../core/configuration/passkey.js";import"./evm/types.js";import"./evm/JsonRpcError.js";import"eventemitter3";import"./evm/provider/eip6963.js";class d{iframeManager;storage;backendApiClients;passkeyHandler;eventEmitter;constructor(e,t,a,r,o){this.iframeManager=e,this.storage=t,this.backendApiClients=a,this.passkeyHandler=r,this.eventEmitter=o}async createPasskey(e){const t=await this.passkeyHandler.createPasskey({id:c.randomPasskeyName(),displayName:"Openfort - Embedded Wallet",seed:e});return{id:t.id,key:t.key}}async configure(n){const c=await e.fromStorage(this.storage);if(!c)throw new t("No access token found",a.NOT_LOGGED_IN_ERROR);const d=i.getInstance();if(!d)throw new t("Configuration not found",a.INVALID_CONFIGURATION);const y=await s.fromStorage(this.storage);let p;if(y){const e={account:y.id,...n.entropy&&{entropy:{...n.entropy.recoveryPassword&&{recoveryPassword:n.entropy.recoveryPassword},...n.entropy.encryptionSession&&{encryptionSession:n.entropy.encryptionSession},...n.entropy.passkey&&{passkey:n.entropy.passkey}}}},t=await this.iframeManager.recover(e);p=t.account}else{const e=await this.backendApiClients.accountsApi.getAccountsV2({user:c.player,accountType:n.accountType,chainType:n.chainType,chainId:n.chainId},{headers:{authorization:`Bearer ${d.baseConfiguration.publishableKey}`,"x-player-token":c.token,"x-auth-provider":c.thirdPartyProvider,"x-token-type":c.thirdPartyTokenType}});if(0===e.data.data.length){const e=n.entropy?.passkey?await this.createPasskey(c.player):void 0,t={accountType:n.accountType,chainType:n.chainType,chainId:n.chainId,...n.entropy&&{entropy:{...n.entropy.recoveryPassword&&{recoveryPassword:n.entropy.recoveryPassword},...n.entropy.encryptionSession&&{encryptionSession:n.entropy.encryptionSession},...n.entropy.passkey&&{passkey:e}}}},a=await this.iframeManager.create(t);p=a.account}else{const t={account:e.data.data[0].id,...n.entropy&&{entropy:{...n.entropy.recoveryPassword&&{recoveryPassword:n.entropy.recoveryPassword},...n.entropy.encryptionSession&&{encryptionSession:n.entropy.encryptionSession},...n.entropy.passkey&&{passkey:n.entropy.passkey}}}},a=await this.iframeManager.recover(t);p=a.account}}return r(async()=>{const e=await this.backendApiClients.accountsApi.getAccountV2({id:p},{headers:{authorization:`Bearer ${d.baseConfiguration.publishableKey}`,"x-player-token":c.token,"x-auth-provider":c.thirdPartyProvider,"x-token-type":c.thirdPartyTokenType}}),t=new s({user:e.data.user,chainType:e.data.chainType,id:e.data.id,address:e.data.address,ownerAddress:e.data.ownerAddress,accountType:e.data.accountType,createdAt:e.data.createdAt,implementationType:e.data.smartAccount?.implementationType,chainId:e.data.chainId,salt:e.data.smartAccount?.salt,factoryAddress:e.data.smartAccount?.factoryAddress,recoveryMethod:s.parseRecoveryMethod(e.data.recoveryMethod)});return t.save(this.storage),this.eventEmitter.emit(o.SWITCH_ACCOUNT,e.data.address),t},{default:a.AUTHENTICATION_ERROR})}async sign(e,t,a,r){return await this.iframeManager.sign(e,t,a,r)}async export(){return await this.iframeManager.export()}async switchChain({chainId:e}){const t=await this.iframeManager.switchChain(e),a=await s.fromStorage(this.storage);new s({...a,id:t.account,chainId:e}).save(this.storage)}async create(n){const c=await this.iframeManager.create(n),d=await e.fromStorage(this.storage);if(!d)throw new t("No access token found",a.NOT_LOGGED_IN_ERROR);const y=i.getInstance();if(!y)throw new t("Configuration not found",a.INVALID_CONFIGURATION);return r(async()=>{const e=await this.backendApiClients.accountsApi.getAccountV2({id:c.account},{headers:{authorization:`Bearer ${y.baseConfiguration.publishableKey}`,"x-player-token":d.token,"x-auth-provider":d.thirdPartyProvider,"x-token-type":d.thirdPartyTokenType}}),t=new s({user:e.data.user,chainType:e.data.chainType,id:e.data.id,address:e.data.address,ownerAddress:e.data.ownerAddress,accountType:e.data.accountType,createdAt:e.data.createdAt,implementationType:e.data.smartAccount?.implementationType,chainId:e.data.chainId,salt:e.data.smartAccount?.salt,factoryAddress:e.data.smartAccount?.factoryAddress,recoveryMethod:s.parseRecoveryMethod(e.data.recoveryMethod),recoveryMethodDetails:e.data.recoveryMethodDetails});return t.save(this.storage),this.eventEmitter.emit(o.SWITCH_ACCOUNT,e.data.address),t},{default:a.AUTHENTICATION_ERROR})}async recover(n){const c=await this.iframeManager.recover(n),d=await e.fromStorage(this.storage);if(!d)throw new t("No access token found",a.NOT_LOGGED_IN_ERROR);const y=i.getInstance();if(!y)throw new t("Configuration not found",a.INVALID_CONFIGURATION);return r(async()=>{const e=await this.backendApiClients.accountsApi.getAccountV2({id:c.account},{headers:{authorization:`Bearer ${y.baseConfiguration.publishableKey}`,"x-player-token":d.token,"x-auth-provider":d.thirdPartyProvider,"x-token-type":d.thirdPartyTokenType}}),t=new s({user:e.data.user,chainType:e.data.chainType,id:e.data.id,address:e.data.address,ownerAddress:e.data.ownerAddress,accountType:e.data.accountType,createdAt:e.data.createdAt,implementationType:e.data.smartAccount?.implementationType,chainId:e.data.chainId,salt:e.data.smartAccount?.salt,factoryAddress:e.data.smartAccount?.factoryAddress,recoveryMethod:s.parseRecoveryMethod(e.data.recoveryMethod)});return t.save(this.storage),this.eventEmitter.emit(o.SWITCH_ACCOUNT,e.data.address),t},{default:a.AUTHENTICATION_ERROR})}async setRecoveryMethod({recoveryMethod:e,recoveryPassword:t,encryptionSession:a}){await this.iframeManager.setRecoveryMethod(e,t,a)}async disconnect(){await this.iframeManager.disconnect(),this.storage.remove(n.ACCOUNT)}}export{d as EmbeddedSigner};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfort/openfort-js",
3
- "version": "0.10.12",
3
+ "version": "0.10.13",
4
4
  "author": "Openfort (https://www.openfort.io)",
5
5
  "bugs": "https://github.com/openfort-xyz/openfort-js/issues",
6
6
  "repository": "openfort-xyz/openfort-js.git",