@openfort/openfort-js 0.9.2 → 0.9.3

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("../wallets/signer.js"),t=require("../core/configuration/account.js"),o=require("../core/configuration/authentication.js"),i=require("../core/config/config.js"),n=require("../core/errors/openfortError.js"),a=require("../wallets/evm/walletHelpers.js"),s=require("../wallets/evm/evmProvider.js");require("../wallets/evm/types.js");var d=require("../wallets/evm/provider/eip6963.js"),c=require("../utils/typedEventEmitter.js"),h=require("../types/types.js"),f=require("../wallets/iframeManager.js");exports.EmbeddedWalletApi=class{storage;validateAndRefreshToken;ensureInitialized;provider=null;iframeManager=null;constructor(e,r,t){this.storage=e,this.validateAndRefreshToken=r,this.ensureInitialized=t}get backendApiClients(){const r=i.SDKConfiguration.fromStorage();if(!r)throw new n.OpenfortError("Configuration not found",n.OpenfortErrorType.INVALID_CONFIGURATION);return new e.BackendApiClients({basePath:r.backendUrl,accessToken:r.baseConfiguration.publishableKey})}getIframeManager(){if(!this.iframeManager){const e=i.SDKConfiguration.fromStorage();if(!e)throw new n.OpenfortError("Configuration not found",n.OpenfortErrorType.INVALID_CONFIGURATION);this.iframeManager=new f.IframeManager(e,this.storage)}return this.iframeManager}async configure(e={}){const t=e.recoveryParams??{recoveryMethod:h.RecoveryMethod.AUTOMATIC};await this.ensureInitialized(),await this.validateAndRefreshToken();const o=i.SDKConfiguration.fromStorage();let a=null;(t.recoveryMethod===h.RecoveryMethod.PASSWORD||e.shieldAuthentication?.encryptionSession)&&(a={encryptionSession:e.shieldAuthentication?.encryptionSession||null,recoveryPassword:t.recoveryMethod===h.RecoveryMethod.PASSWORD?t.password:null,encryptionPart:o?.shieldConfiguration?.shieldEncryptionKey||null});let s=null,d=null;if(e.shieldAuthentication&&(s="openfort"===e.shieldAuthentication.auth?"openfort":"custom",d=e.shieldAuthentication.token),!this.storage)throw new n.OpenfortError("Storage not available in EmbeddedWalletApi",n.OpenfortErrorType.INVALID_CONFIGURATION);return await r.SignerManager.embedded(this.storage,e.chainId,a,s,d),this.get()}async signMessage(e,t){await this.ensureInitialized(),await this.validateAndRefreshToken();const o=await r.SignerManager.fromStorage(this.storage);if(!o)throw new n.OpenfortError("No signer configured",n.OpenfortErrorType.MISSING_SIGNER_ERROR);const{hashMessage:i=!0,arrayifyMessage:a=!1}=t||{};return await o.sign(e,a,i)}async signTypedData(e,o,i){await this.ensureInitialized(),await this.validateAndRefreshToken();const s=await r.SignerManager.fromStorage(this.storage),d=await t.Account.fromStorage(this.storage);if(!s||!d)throw new n.OpenfortError("No signer configured",n.OpenfortErrorType.MISSING_SIGNER_ERROR);return await a.getSignedTypedData({domain:e,types:o,message:i},d.type,Number(d.chainId),s,d.address)}async exportPrivateKey(){await this.ensureInitialized(),await this.validateAndRefreshToken();const e=await r.SignerManager.fromStorage(this.storage);if(!e)throw new n.OpenfortError("No signer configured",n.OpenfortErrorType.MISSING_SIGNER_ERROR);return await e.export()}async setEmbeddedRecovery({recoveryMethod:e,recoveryPassword:t,encryptionSession:o}){await this.ensureInitialized(),await this.validateAndRefreshToken();const i=await r.SignerManager.fromStorage(this.storage);if(!i)throw new n.OpenfortError("No signer configured",n.OpenfortErrorType.MISSING_SIGNER_ERROR);if("password"===e&&!t)throw new n.OpenfortError("Recovery password is required",n.OpenfortErrorType.INVALID_CONFIGURATION);await i.setEmbeddedRecovery({recoveryMethod:e,recoveryPassword:t,encryptionSession:o})}async get(){const e=await t.Account.fromStorage(this.storage);if(!e)throw new n.OpenfortError("No signer configured",n.OpenfortErrorType.MISSING_SIGNER_ERROR);const r=await o.Authentication.fromStorage(this.storage);if(!r)throw new n.OpenfortError("No access token found",n.OpenfortErrorType.INTERNAL_ERROR);return{chainId:e.chainId.toString(),owner:{id:r.player},address:e.address,ownerAddress:e.ownerAddress,chainType:"ethereum",implementationType:e.type}}async list(){const e=i.SDKConfiguration.fromStorage();if(!e)throw new n.OpenfortError("Configuration not found",n.OpenfortErrorType.INVALID_CONFIGURATION);await this.ensureInitialized(),await this.validateAndRefreshToken();const r=await o.Authentication.fromStorage(this.storage);if(!r)throw new n.OpenfortError("No access token found",n.OpenfortErrorType.NOT_LOGGED_IN_ERROR);return n.withOpenfortError(async()=>(await this.backendApiClients.accountsApi.getAccounts(void 0,{headers:{authorization:`Bearer ${e.baseConfiguration.publishableKey}`,"x-player-token":r.token,"x-auth-provider":r.thirdPartyProvider,"x-token-type":r.thirdPartyTokenType}})).data.data.map(e=>({owner:{id:e.player.id},chainType:"ethereum",address:e.address,ownerAddress:e.ownerAddress,createdAt:e.createdAt,implementationType:e.accountType,chainId:e.chainId.toString()})),{default:n.OpenfortErrorType.AUTHENTICATION_ERROR})}async getEmbeddedState(){try{if(!await o.Authentication.fromStorage(this.storage))return h.EmbeddedState.UNAUTHENTICATED;if(!await r.SignerManager.fromStorage(this.storage))return h.EmbeddedState.EMBEDDED_SIGNER_NOT_CONFIGURED;return await t.Account.fromStorage(this.storage)?h.EmbeddedState.READY:h.EmbeddedState.CREATING_ACCOUNT}catch(e){return console.error("Failed to get embedded state:",e),h.EmbeddedState.UNAUTHENTICATED}}async getEthereumProvider(e){await this.ensureInitialized();const i={announceProvider:!0,...e},n=await o.Authentication.fromStorage(this.storage),a=await r.SignerManager.fromStorage(this.storage),h=await t.Account.fromStorage(this.storage);return this.provider?this.provider&&i.policy&&this.provider.updatePolicy(i.policy):(this.provider=new s.EvmProvider({storage:this.storage,openfortEventEmitter:new c,signer:a||void 0,account:h||void 0,authentication:n||void 0,backendApiClients:this.backendApiClients,policyId:i.policy,validateAndRefreshSession:this.validateAndRefreshToken.bind(this),chains:i.chains}),i.announceProvider&&d.announceProvider({info:{...d.openfortProviderInfo,...i.providerInfo},provider:this.provider})),this.provider}async ping(e){try{const r=this.getIframeManager();if(!r.isLoaded())return!1;e>0&&await new Promise(r=>{setTimeout(r,e)});const t=await o.Authentication.fromStorage(this.storage);if(t)try{return await r.getCurrentUser(t.player),!0}catch(e){return!1}return r.isLoaded()}catch(e){return!1}}getURL(){const e=i.SDKConfiguration.fromStorage();if(!e)throw new n.OpenfortError("Configuration not found",n.OpenfortErrorType.INVALID_CONFIGURATION);return e.iframeUrl}};
1
+ "use strict";var e=require("../../../packages/internal/openapi-clients/dist/index.js"),r=require("../wallets/signer.js"),t=require("../core/configuration/account.js"),o=require("../core/configuration/authentication.js"),i=require("../core/config/config.js"),n=require("../core/errors/openfortError.js"),a=require("../wallets/evm/walletHelpers.js"),s=require("../wallets/evm/evmProvider.js");require("../wallets/evm/types.js");var d=require("../wallets/evm/provider/eip6963.js"),c=require("../utils/typedEventEmitter.js"),h=require("../types/types.js"),f=require("../wallets/iframeManager.js");exports.EmbeddedWalletApi=class{storage;validateAndRefreshToken;ensureInitialized;provider=null;iframeManager=null;constructor(e,r,t){this.storage=e,this.validateAndRefreshToken=r,this.ensureInitialized=t}get backendApiClients(){const r=i.SDKConfiguration.fromStorage();if(!r)throw new n.OpenfortError("Configuration not found",n.OpenfortErrorType.INVALID_CONFIGURATION);return new e.BackendApiClients({basePath:r.backendUrl,accessToken:r.baseConfiguration.publishableKey})}getIframeManager(){if(!this.iframeManager){const e=i.SDKConfiguration.fromStorage();if(!e)throw new n.OpenfortError("Configuration not found",n.OpenfortErrorType.INVALID_CONFIGURATION);this.iframeManager=new f.IframeManager(e,this.storage)}return this.iframeManager}async configure(e={}){const t=e.recoveryParams??{recoveryMethod:h.RecoveryMethod.AUTOMATIC};await this.ensureInitialized(),await this.validateAndRefreshToken();const o=i.SDKConfiguration.fromStorage();let a=null;(t.recoveryMethod===h.RecoveryMethod.PASSWORD||e.shieldAuthentication?.encryptionSession)&&(a={encryptionSession:e.shieldAuthentication?.encryptionSession||null,recoveryPassword:t.recoveryMethod===h.RecoveryMethod.PASSWORD?t.password:null,encryptionPart:o?.shieldConfiguration?.shieldEncryptionKey||null});let s=null,d=null;if(e.shieldAuthentication&&(s="openfort"===e.shieldAuthentication.auth?"openfort":"custom",d=e.shieldAuthentication.token),!this.storage)throw new n.OpenfortError("Storage not available in EmbeddedWalletApi",n.OpenfortErrorType.INVALID_CONFIGURATION);return await r.SignerManager.embedded(this.storage,e.chainId,a,s,d),this.get()}async signMessage(e,t){await this.ensureInitialized(),await this.validateAndRefreshToken();const o=await r.SignerManager.fromStorage(this.storage);if(!o)throw new n.OpenfortError("No signer configured",n.OpenfortErrorType.MISSING_SIGNER_ERROR);const{hashMessage:i=!0,arrayifyMessage:a=!1}=t||{};return await o.sign(e,a,i)}async signTypedData(e,o,i){await this.ensureInitialized(),await this.validateAndRefreshToken();const s=await r.SignerManager.fromStorage(this.storage),d=await t.Account.fromStorage(this.storage);if(!s||!d)throw new n.OpenfortError("No signer configured",n.OpenfortErrorType.MISSING_SIGNER_ERROR);return await a.getSignedTypedData({domain:e,types:o,message:i},d.type,Number(d.chainId),s,d.address)}async exportPrivateKey(){await this.ensureInitialized(),await this.validateAndRefreshToken();const e=await r.SignerManager.fromStorage(this.storage);if(!e)throw new n.OpenfortError("No signer configured",n.OpenfortErrorType.MISSING_SIGNER_ERROR);return await e.export()}async setEmbeddedRecovery({recoveryMethod:e,recoveryPassword:t,encryptionSession:o}){await this.ensureInitialized(),await this.validateAndRefreshToken();const i=await r.SignerManager.fromStorage(this.storage);if(!i)throw new n.OpenfortError("No signer configured",n.OpenfortErrorType.MISSING_SIGNER_ERROR);if("password"===e&&!t)throw new n.OpenfortError("Recovery password is required",n.OpenfortErrorType.INVALID_CONFIGURATION);await i.setEmbeddedRecovery({recoveryMethod:e,recoveryPassword:t,encryptionSession:o})}async get(){const e=await t.Account.fromStorage(this.storage);if(!e)throw new n.OpenfortError("No signer configured",n.OpenfortErrorType.MISSING_SIGNER_ERROR);const r=await o.Authentication.fromStorage(this.storage);if(!r)throw new n.OpenfortError("No access token found",n.OpenfortErrorType.INTERNAL_ERROR);return{chainId:e.chainId.toString(),owner:{id:r.player},address:e.address,ownerAddress:"solana"===e.type?void 0:e.ownerAddress,chainType:"solana"===e.type?"solana":"ethereum",implementationType:"solana"===e.type?void 0:e.type}}async list(){const e=i.SDKConfiguration.fromStorage();if(!e)throw new n.OpenfortError("Configuration not found",n.OpenfortErrorType.INVALID_CONFIGURATION);await this.ensureInitialized(),await this.validateAndRefreshToken();const r=await o.Authentication.fromStorage(this.storage);if(!r)throw new n.OpenfortError("No access token found",n.OpenfortErrorType.NOT_LOGGED_IN_ERROR);return n.withOpenfortError(async()=>(await this.backendApiClients.accountsApi.getAccounts(void 0,{headers:{authorization:`Bearer ${e.baseConfiguration.publishableKey}`,"x-player-token":r.token,"x-auth-provider":r.thirdPartyProvider,"x-token-type":r.thirdPartyTokenType}})).data.data.map(e=>({owner:{id:e.player.id},chainType:"ethereum",address:e.address,ownerAddress:e.ownerAddress,createdAt:e.createdAt,implementationType:e.accountType,chainId:e.chainId.toString()})),{default:n.OpenfortErrorType.AUTHENTICATION_ERROR})}async getEmbeddedState(){try{if(!await o.Authentication.fromStorage(this.storage))return h.EmbeddedState.UNAUTHENTICATED;if(!await r.SignerManager.fromStorage(this.storage))return h.EmbeddedState.EMBEDDED_SIGNER_NOT_CONFIGURED;return await t.Account.fromStorage(this.storage)?h.EmbeddedState.READY:h.EmbeddedState.CREATING_ACCOUNT}catch(e){return console.error("Failed to get embedded state:",e),h.EmbeddedState.UNAUTHENTICATED}}async getEthereumProvider(e){await this.ensureInitialized();const i={announceProvider:!0,...e},n=await o.Authentication.fromStorage(this.storage),a=await r.SignerManager.fromStorage(this.storage),h=await t.Account.fromStorage(this.storage);return this.provider?this.provider&&i.policy&&this.provider.updatePolicy(i.policy):(this.provider=new s.EvmProvider({storage:this.storage,openfortEventEmitter:new c,signer:a||void 0,account:h||void 0,authentication:n||void 0,backendApiClients:this.backendApiClients,policyId:i.policy,validateAndRefreshSession:this.validateAndRefreshToken.bind(this),chains:i.chains}),i.announceProvider&&d.announceProvider({info:{...d.openfortProviderInfo,...i.providerInfo},provider:this.provider})),this.provider}async ping(e){try{const r=this.getIframeManager();if(!r.isLoaded())return!1;e>0&&await new Promise(r=>{setTimeout(r,e)});const t=await o.Authentication.fromStorage(this.storage);if(t)try{return await r.getCurrentUser(t.player),!0}catch(e){return!1}return r.isLoaded()}catch(e){return!1}}getURL(){const e=i.SDKConfiguration.fromStorage();if(!e)throw new n.OpenfortError("Configuration not found",n.OpenfortErrorType.INVALID_CONFIGURATION);return e.iframeUrl}};
@@ -1 +1 @@
1
- "use strict";var o;exports.StorageKeys=void 0,(o=exports.StorageKeys||(exports.StorageKeys={})).AUTHENTICATION="openfort.authentication",o.SIGNER="openfort.signer",o.CONFIGURATION="openfort.configuration",o.ACCOUNT="openfort.account",o.TEST="openfort.test",o.RECOVERY="openfort.recovery",o.SESSION="openfort.session",o.PKCE_STATE="openfort.pkce_state",o.PKCE_VERIFIER="openfort.pkce_verifier";
1
+ "use strict";var o;exports.StorageKeys=void 0,(o=exports.StorageKeys||(exports.StorageKeys={})).AUTHENTICATION="openfort.authentication",o.CONFIGURATION="openfort.configuration",o.ACCOUNT="openfort.account",o.TEST="openfort.test",o.RECOVERY="openfort.recovery",o.SESSION="openfort.session",o.PKCE_STATE="openfort.pkce_state",o.PKCE_VERIFIER="openfort.pkce_verifier";
@@ -1 +1 @@
1
- "use strict";exports.PACKAGE="@openfort/openfort-js",exports.VERSION="0.9.2";
1
+ "use strict";exports.PACKAGE="@openfort/openfort-js",exports.VERSION="0.9.3";
@@ -1 +1 @@
1
- "use strict";var e=require("../../../node_modules/penpal/dist/penpal.js"),r=require("../utils/crypto.js"),o=require("../core/errors/openfortError.js"),t=require("./types.js");class n extends Error{constructor(){super("This embedded signer requires a password to be recovered")}}class i extends Error{constructor(){super("MissingProjectEntropyError")}}class s extends Error{constructor(){super("Wrong recovery password for this embedded signer")}}class a extends Error{message;constructor(e){super(`Unknown response from iframe: ${e}`),this.message=e||""}}class d extends Error{constructor(){super("Not configured")}}class c{iframe;connection;remote;storage;sdkConfiguration;isInitialized=!1;constructor(e,r){if(!e)throw new o.OpenfortError("Configuration is required for IframeManager",o.OpenfortErrorType.INVALID_CONFIGURATION);if(!r)throw new o.OpenfortError("Storage is required for IframeManager",o.OpenfortErrorType.INVALID_CONFIGURATION);this.sdkConfiguration=e,this.storage=r}async iframeSetup(){if("undefined"==typeof document)throw new o.OpenfortError("Document is not available. Please provide a message poster for non-browser environments.",o.OpenfortErrorType.INVALID_CONFIGURATION);const e=document.getElementById("openfort-iframe");e&&document.body.removeChild(e);const r=document.createElement("iframe");r.style.display="none",r.id="openfort-iframe",document.body.appendChild(r),r.src=this.sdkConfiguration.iframeUrl,this.iframe=r,await new Promise((e,o)=>{const t=setTimeout(()=>{o(new Error("Iframe load timeout"))},1e4);r.onload=()=>{clearTimeout(t),e()},r.onerror=()=>{clearTimeout(t),o(new Error("Failed to load iframe"))}})}async establishIframeConnection(){if("undefined"==typeof document)throw new o.OpenfortError("Document is not available.",o.OpenfortErrorType.INVALID_CONFIGURATION);if(!this.iframe?.contentWindow)throw new o.OpenfortError("Iframe does not have content window",o.OpenfortErrorType.INVALID_CONFIGURATION);const r=new URL(this.sdkConfiguration.iframeUrl).origin,t=new e.WindowMessenger({remoteWindow:this.iframe.contentWindow,allowedOrigins:[r]});this.connection=e.connect({messenger:t,timeout:5e3});try{this.remote=await this.connection.promise,this.isInitialized=!0}catch(e){const r=e;throw new Error(`v3 Failed to establish Iframe connection: ${r.cause||r.message}`,r)}}isLoaded(){return this.isInitialized&&void 0!==this.remote}async ensureConnection(){if(!this.isLoaded()){if("undefined"==typeof document)throw new o.OpenfortError("Document is not available.",o.OpenfortErrorType.INVALID_CONFIGURATION);await this.iframeSetup(),await this.establishIframeConnection()}if(!this.remote)throw new Error("Failed to establish connection with iframe");return this.remote}static handleError(e){if(t.isErrorResponse(e)){if(e.error===t.NOT_CONFIGURED_ERROR)throw new d;if(e.error===t.MISSING_USER_ENTROPY_ERROR)throw new n;if(e.error===t.MISSING_PROJECT_ENTROPY_ERROR)throw new i;if(e.error===t.INCORRECT_USER_ENTROPY_ERROR)throw new s;throw new a(e.error)}throw e}async configure(e){if(!this.sdkConfiguration.shieldConfiguration)throw new Error("shieldConfiguration is required");const o=await this.ensureConnection(),n={uuid:r.randomUUID(),action:t.Event.CONFIGURE,chainId:e.chainId,recovery:e.recovery,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,shieldAPIKey:this.sdkConfiguration.shieldConfiguration?.shieldPublishableKey||"",accessToken:e.accessToken,playerID:e.playerID,thirdPartyProvider:e.thirdPartyProvider,thirdPartyTokenType:e.thirdPartyTokenType,encryptionKey:e.password,encryptionPart:this.sdkConfiguration?.shieldConfiguration?.shieldEncryptionKey??null,encryptionSession:e.recovery?.encryptionSession??null,openfortURL:this.sdkConfiguration.backendUrl,shieldURL:this.sdkConfiguration.shieldUrl},i=await o.configure(n);return t.isErrorResponse(i)&&c.handleError(i),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",i.version??"undefined"),i}async sign(e,o,n,i){const s=await this.ensureConnection(),a={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},h=new t.SignRequest(r.randomUUID(),o,n,i,a);try{const e=await s.sign(h);return t.isErrorResponse(e)&&c.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e.signature}catch(r){if(r instanceof d)return await this.configure(e),this.sign(e,o,n,i);throw r}}async switchChain(e,o){const n=await this.ensureConnection(),i={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},s=new t.SwitchChainRequest(r.randomUUID(),o,i);try{const e=await n.switchChain(s);return t.isErrorResponse(e)&&c.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e}catch(r){if(r instanceof d)return await this.configure(e),this.switchChain(e,o);throw r}}async export(e){const o=await this.ensureConnection(),n={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},i=new t.ExportPrivateKeyRequest(r.randomUUID(),n);try{const e=await o.export(i);return t.isErrorResponse(e)&&c.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e.key}catch(r){if(r instanceof d)return await this.configure(e),this.export(e);throw r}}async setEmbeddedRecovery(e,o,n,i){const s=await this.ensureConnection(),a={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},h=new t.SetRecoveryMethodRequest(r.randomUUID(),o,n,i,a);try{const e=await s.setRecoveryMethod(h);t.isErrorResponse(e)&&c.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined")}catch(r){if(r instanceof d)return await this.configure(e),this.setEmbeddedRecovery(e,o,n,i);throw r}}async getCurrentUser(e){const o=await this.ensureConnection(),n=new t.GetCurrentDeviceRequest(r.randomUUID(),e);try{const e=await o.getCurrentDevice(n);return t.isErrorResponse(e)&&c.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e}catch(e){if(e instanceof d)return null;throw e}}async logout(){const e=await this.ensureConnection(),o={uuid:r.randomUUID()};await e.logout(o)}async updateAuthentication(e,o,n){const i={...e};i.accessToken=o,n===t.ShieldAuthType.OPENFORT&&i.recovery&&(i.recovery={...i.recovery,token:o});const s=await this.ensureConnection(),a=new t.UpdateAuthenticationRequest(r.randomUUID(),o);try{await s.updateAuthentication(a)}catch(e){if(e instanceof d)return await this.configure(i),void await this.updateAuthentication(i,o,n);throw e}}destroy(){if(this.connection&&(this.connection.destroy(),this.connection=void 0),this.remote=void 0,this.isInitialized=!1,this.iframe&&"undefined"!=typeof document){const e=document.getElementById("openfort-iframe");e&&e.parentNode&&e.parentNode.removeChild(e),this.iframe=void 0}}}exports.IframeManager=c,exports.MissingProjectEntropyError=i,exports.MissingRecoveryPasswordError=n,exports.NotConfiguredError=d,exports.UnknownResponseError=a,exports.WrongRecoveryPasswordError=s;
1
+ "use strict";var e=require("../../../node_modules/penpal/dist/penpal.js"),r=require("../storage/istorage.js"),o=require("../utils/crypto.js"),t=require("../core/errors/openfortError.js"),n=require("./types.js");class i extends Error{constructor(){super("This embedded signer requires a password to be recovered")}}class s extends Error{constructor(){super("MissingProjectEntropyError")}}class a extends Error{constructor(){super("Wrong recovery password for this embedded signer")}}class d extends Error{message;constructor(e){super(`Unknown response from iframe: ${e}`),this.message=e||""}}class c extends Error{constructor(){super("Not configured")}}exports.IframeManager=class{iframe;connection;remote;storage;sdkConfiguration;isInitialized=!1;constructor(e,r){if(!e)throw new t.OpenfortError("Configuration is required for IframeManager",t.OpenfortErrorType.INVALID_CONFIGURATION);if(!r)throw new t.OpenfortError("Storage is required for IframeManager",t.OpenfortErrorType.INVALID_CONFIGURATION);this.sdkConfiguration=e,this.storage=r}async iframeSetup(){if("undefined"==typeof document)throw new t.OpenfortError("Document is not available. Please provide a message poster for non-browser environments.",t.OpenfortErrorType.INVALID_CONFIGURATION);const e=document.getElementById("openfort-iframe");e&&document.body.removeChild(e);const r=document.createElement("iframe");r.style.display="none",r.id="openfort-iframe",document.body.appendChild(r),r.src=this.sdkConfiguration.iframeUrl,this.iframe=r,await new Promise((e,o)=>{const t=setTimeout(()=>{o(new Error("Iframe load timeout"))},1e4);r.onload=()=>{clearTimeout(t),e()},r.onerror=()=>{clearTimeout(t),o(new Error("Failed to load iframe"))}})}async establishIframeConnection(){if("undefined"==typeof document)throw new t.OpenfortError("Document is not available.",t.OpenfortErrorType.INVALID_CONFIGURATION);if(!this.iframe?.contentWindow)throw new t.OpenfortError("Iframe does not have content window",t.OpenfortErrorType.INVALID_CONFIGURATION);const r=new URL(this.sdkConfiguration.iframeUrl).origin,o=new e.WindowMessenger({remoteWindow:this.iframe.contentWindow,allowedOrigins:[r]});this.connection=e.connect({messenger:o,timeout:5e3});try{this.remote=await this.connection.promise,this.isInitialized=!0}catch(e){const r=e;throw new t.OpenfortError(`Failed to establish iFrame connection: ${r.cause||r.message}`,t.OpenfortErrorType.INTERNAL_ERROR,e)}}isLoaded(){return this.isInitialized&&void 0!==this.remote}async ensureConnection(){if(!this.isLoaded()){if("undefined"==typeof document)throw new t.OpenfortError("Document is not available.",t.OpenfortErrorType.INVALID_CONFIGURATION);await this.iframeSetup(),await this.establishIframeConnection()}if(!this.remote)throw new t.OpenfortError("Failed to establish iFrame connection",t.OpenfortErrorType.INTERNAL_ERROR);return this.remote}handleError(e){if(n.isErrorResponse(e)){if(e.error===n.NOT_CONFIGURED_ERROR)throw new c;if(e.error===n.MISSING_USER_ENTROPY_ERROR)throw this.storage.remove(r.StorageKeys.ACCOUNT),new i;if(e.error===n.MISSING_PROJECT_ENTROPY_ERROR)throw this.storage.remove(r.StorageKeys.ACCOUNT),new s;if(e.error===n.INCORRECT_USER_ENTROPY_ERROR)throw new a;throw new d(e.error)}throw e}async configure(e){if(!this.sdkConfiguration.shieldConfiguration)throw new Error("shieldConfiguration is required");const r=await this.ensureConnection(),t={uuid:o.randomUUID(),action:n.Event.CONFIGURE,chainId:e.chainId,recovery:e.recovery,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,shieldAPIKey:this.sdkConfiguration.shieldConfiguration?.shieldPublishableKey||"",accessToken:e.accessToken,playerID:e.playerID,thirdPartyProvider:e.thirdPartyProvider,thirdPartyTokenType:e.thirdPartyTokenType,encryptionKey:e.password,encryptionPart:this.sdkConfiguration?.shieldConfiguration?.shieldEncryptionKey??null,encryptionSession:e.recovery?.encryptionSession??null,openfortURL:this.sdkConfiguration.backendUrl,shieldURL:this.sdkConfiguration.shieldUrl},i=await r.configure(t);return n.isErrorResponse(i)&&this.handleError(i),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",i.version??"undefined"),i}async sign(e,r,t,i){const s=await this.ensureConnection(),a={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},d=new n.SignRequest(o.randomUUID(),r,t,i,a);try{const e=await s.sign(d);return n.isErrorResponse(e)&&this.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e.signature}catch(o){if(o instanceof c)return await this.configure(e),this.sign(e,r,t,i);throw o}}async switchChain(e,r){const t=await this.ensureConnection(),i={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},s=new n.SwitchChainRequest(o.randomUUID(),r,i);try{const e=await t.switchChain(s);return n.isErrorResponse(e)&&this.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e}catch(o){if(o instanceof c)return await this.configure(e),this.switchChain(e,r);throw o}}async export(e){const r=await this.ensureConnection(),t={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},i=new n.ExportPrivateKeyRequest(o.randomUUID(),t);try{const e=await r.export(i);return n.isErrorResponse(e)&&this.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e.key}catch(r){if(r instanceof c)return await this.configure(e),this.export(e);throw r}}async setEmbeddedRecovery(e,r,t,i){const s=await this.ensureConnection(),a={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},d=new n.SetRecoveryMethodRequest(o.randomUUID(),r,t,i,a);try{const e=await s.setRecoveryMethod(d);n.isErrorResponse(e)&&this.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined")}catch(o){if(o instanceof c)return await this.configure(e),this.setEmbeddedRecovery(e,r,t,i);throw o}}async getCurrentUser(e){const r=await this.ensureConnection(),t=new n.GetCurrentDeviceRequest(o.randomUUID(),e);try{const e=await r.getCurrentDevice(t);return n.isErrorResponse(e)&&this.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e}catch(e){if(e instanceof c)return null;throw e}}async logout(){const e=await this.ensureConnection(),r={uuid:o.randomUUID()};await e.logout(r)}async updateAuthentication(e,r,t){const i={...e};i.accessToken=r,t===n.ShieldAuthType.OPENFORT&&i.recovery&&(i.recovery={...i.recovery,token:r});const s=await this.ensureConnection(),a=new n.UpdateAuthenticationRequest(o.randomUUID(),r);try{await s.updateAuthentication(a)}catch(e){if(e instanceof c)return await this.configure(i),void await this.updateAuthentication(i,r,t);throw e}}destroy(){if(this.connection&&(this.connection.destroy(),this.connection=void 0),this.remote=void 0,this.isInitialized=!1,this.iframe&&"undefined"!=typeof document){const e=document.getElementById("openfort-iframe");e&&e.parentNode&&e.parentNode.removeChild(e),this.iframe=void 0}}},exports.MissingProjectEntropyError=s,exports.MissingRecoveryPasswordError=i,exports.NotConfiguredError=c,exports.UnknownResponseError=d,exports.WrongRecoveryPasswordError=a;
package/dist/index.d.ts CHANGED
@@ -392,7 +392,6 @@ type RecoverParams = {
392
392
 
393
393
  declare enum StorageKeys {
394
394
  AUTHENTICATION = "openfort.authentication",
395
- SIGNER = "openfort.signer",
396
395
  CONFIGURATION = "openfort.configuration",
397
396
  ACCOUNT = "openfort.account",
398
397
  TEST = "openfort.test",
@@ -1470,7 +1469,7 @@ interface AccountCreateRequest {
1470
1469
  'publicKey': string;
1471
1470
  }
1472
1471
  declare const AccountCreateRequestAccountTypeEnum: {
1473
- readonly Sol: "sol";
1472
+ readonly Solana: "solana";
1474
1473
  };
1475
1474
  type AccountCreateRequestAccountTypeEnum = typeof AccountCreateRequestAccountTypeEnum[keyof typeof AccountCreateRequestAccountTypeEnum];
1476
1475
 
@@ -1 +1 @@
1
- import{BackendApiClients as e}from"../../../packages/internal/openapi-clients/dist/index.js";import{SignerManager as t}from"../wallets/signer.js";import{Account as r}from"../core/configuration/account.js";import{Authentication as i}from"../core/configuration/authentication.js";import{SDKConfiguration as o}from"../core/config/config.js";import{OpenfortError as a,OpenfortErrorType as n,withOpenfortError as s}from"../core/errors/openfortError.js";import{getSignedTypedData as d}from"../wallets/evm/walletHelpers.js";import{EvmProvider as c}from"../wallets/evm/evmProvider.js";import"../wallets/evm/types.js";import{announceProvider as h,openfortProviderInfo as f}from"../wallets/evm/provider/eip6963.js";import g from"../utils/typedEventEmitter.js";import{RecoveryMethod as l,EmbeddedState as u}from"../types/types.js";import{IframeManager as w}from"../wallets/iframeManager.js";class p{storage;validateAndRefreshToken;ensureInitialized;provider=null;iframeManager=null;constructor(e,t,r){this.storage=e,this.validateAndRefreshToken=t,this.ensureInitialized=r}get backendApiClients(){const t=o.fromStorage();if(!t)throw new a("Configuration not found",n.INVALID_CONFIGURATION);return new e({basePath:t.backendUrl,accessToken:t.baseConfiguration.publishableKey})}getIframeManager(){if(!this.iframeManager){const e=o.fromStorage();if(!e)throw new a("Configuration not found",n.INVALID_CONFIGURATION);this.iframeManager=new w(e,this.storage)}return this.iframeManager}async configure(e={}){const r=e.recoveryParams??{recoveryMethod:l.AUTOMATIC};await this.ensureInitialized(),await this.validateAndRefreshToken();const i=o.fromStorage();let s=null;(r.recoveryMethod===l.PASSWORD||e.shieldAuthentication?.encryptionSession)&&(s={encryptionSession:e.shieldAuthentication?.encryptionSession||null,recoveryPassword:r.recoveryMethod===l.PASSWORD?r.password:null,encryptionPart:i?.shieldConfiguration?.shieldEncryptionKey||null});let d=null,c=null;if(e.shieldAuthentication&&(d="openfort"===e.shieldAuthentication.auth?"openfort":"custom",c=e.shieldAuthentication.token),!this.storage)throw new a("Storage not available in EmbeddedWalletApi",n.INVALID_CONFIGURATION);return await t.embedded(this.storage,e.chainId,s,d,c),this.get()}async signMessage(e,r){await this.ensureInitialized(),await this.validateAndRefreshToken();const i=await t.fromStorage(this.storage);if(!i)throw new a("No signer configured",n.MISSING_SIGNER_ERROR);const{hashMessage:o=!0,arrayifyMessage:s=!1}=r||{};return await i.sign(e,s,o)}async signTypedData(e,i,o){await this.ensureInitialized(),await this.validateAndRefreshToken();const s=await t.fromStorage(this.storage),c=await r.fromStorage(this.storage);if(!s||!c)throw new a("No signer configured",n.MISSING_SIGNER_ERROR);return await d({domain:e,types:i,message:o},c.type,Number(c.chainId),s,c.address)}async exportPrivateKey(){await this.ensureInitialized(),await this.validateAndRefreshToken();const e=await t.fromStorage(this.storage);if(!e)throw new a("No signer configured",n.MISSING_SIGNER_ERROR);return await e.export()}async setEmbeddedRecovery({recoveryMethod:e,recoveryPassword:r,encryptionSession:i}){await this.ensureInitialized(),await this.validateAndRefreshToken();const o=await t.fromStorage(this.storage);if(!o)throw new a("No signer configured",n.MISSING_SIGNER_ERROR);if("password"===e&&!r)throw new a("Recovery password is required",n.INVALID_CONFIGURATION);await o.setEmbeddedRecovery({recoveryMethod:e,recoveryPassword:r,encryptionSession:i})}async get(){const e=await r.fromStorage(this.storage);if(!e)throw new a("No signer configured",n.MISSING_SIGNER_ERROR);const t=await i.fromStorage(this.storage);if(!t)throw new a("No access token found",n.INTERNAL_ERROR);return{chainId:e.chainId.toString(),owner:{id:t.player},address:e.address,ownerAddress:e.ownerAddress,chainType:"ethereum",implementationType:e.type}}async list(){const e=o.fromStorage();if(!e)throw new a("Configuration not found",n.INVALID_CONFIGURATION);await this.ensureInitialized(),await this.validateAndRefreshToken();const t=await i.fromStorage(this.storage);if(!t)throw new a("No access token found",n.NOT_LOGGED_IN_ERROR);return s(async()=>(await this.backendApiClients.accountsApi.getAccounts(void 0,{headers:{authorization:`Bearer ${e.baseConfiguration.publishableKey}`,"x-player-token":t.token,"x-auth-provider":t.thirdPartyProvider,"x-token-type":t.thirdPartyTokenType}})).data.data.map(e=>({owner:{id:e.player.id},chainType:"ethereum",address:e.address,ownerAddress:e.ownerAddress,createdAt:e.createdAt,implementationType:e.accountType,chainId:e.chainId.toString()})),{default:n.AUTHENTICATION_ERROR})}async getEmbeddedState(){try{if(!await i.fromStorage(this.storage))return u.UNAUTHENTICATED;if(!await t.fromStorage(this.storage))return u.EMBEDDED_SIGNER_NOT_CONFIGURED;return await r.fromStorage(this.storage)?u.READY:u.CREATING_ACCOUNT}catch(e){return console.error("Failed to get embedded state:",e),u.UNAUTHENTICATED}}async getEthereumProvider(e){await this.ensureInitialized();const o={announceProvider:!0,...e},a=await i.fromStorage(this.storage),n=await t.fromStorage(this.storage),s=await r.fromStorage(this.storage);return this.provider?this.provider&&o.policy&&this.provider.updatePolicy(o.policy):(this.provider=new c({storage:this.storage,openfortEventEmitter:new g,signer:n||void 0,account:s||void 0,authentication:a||void 0,backendApiClients:this.backendApiClients,policyId:o.policy,validateAndRefreshSession:this.validateAndRefreshToken.bind(this),chains:o.chains}),o.announceProvider&&h({info:{...f,...o.providerInfo},provider:this.provider})),this.provider}async ping(e){try{const t=this.getIframeManager();if(!t.isLoaded())return!1;e>0&&await new Promise(t=>{setTimeout(t,e)});const r=await i.fromStorage(this.storage);if(r)try{return await t.getCurrentUser(r.player),!0}catch(e){return!1}return t.isLoaded()}catch(e){return!1}}getURL(){const e=o.fromStorage();if(!e)throw new a("Configuration not found",n.INVALID_CONFIGURATION);return e.iframeUrl}}export{p as EmbeddedWalletApi};
1
+ import{BackendApiClients as e}from"../../../packages/internal/openapi-clients/dist/index.js";import{SignerManager as t}from"../wallets/signer.js";import{Account as r}from"../core/configuration/account.js";import{Authentication as i}from"../core/configuration/authentication.js";import{SDKConfiguration as a}from"../core/config/config.js";import{OpenfortError as o,OpenfortErrorType as n,withOpenfortError as s}from"../core/errors/openfortError.js";import{getSignedTypedData as d}from"../wallets/evm/walletHelpers.js";import{EvmProvider as c}from"../wallets/evm/evmProvider.js";import"../wallets/evm/types.js";import{announceProvider as h,openfortProviderInfo as f}from"../wallets/evm/provider/eip6963.js";import g from"../utils/typedEventEmitter.js";import{RecoveryMethod as l,EmbeddedState as u}from"../types/types.js";import{IframeManager as w}from"../wallets/iframeManager.js";class p{storage;validateAndRefreshToken;ensureInitialized;provider=null;iframeManager=null;constructor(e,t,r){this.storage=e,this.validateAndRefreshToken=t,this.ensureInitialized=r}get backendApiClients(){const t=a.fromStorage();if(!t)throw new o("Configuration not found",n.INVALID_CONFIGURATION);return new e({basePath:t.backendUrl,accessToken:t.baseConfiguration.publishableKey})}getIframeManager(){if(!this.iframeManager){const e=a.fromStorage();if(!e)throw new o("Configuration not found",n.INVALID_CONFIGURATION);this.iframeManager=new w(e,this.storage)}return this.iframeManager}async configure(e={}){const r=e.recoveryParams??{recoveryMethod:l.AUTOMATIC};await this.ensureInitialized(),await this.validateAndRefreshToken();const i=a.fromStorage();let s=null;(r.recoveryMethod===l.PASSWORD||e.shieldAuthentication?.encryptionSession)&&(s={encryptionSession:e.shieldAuthentication?.encryptionSession||null,recoveryPassword:r.recoveryMethod===l.PASSWORD?r.password:null,encryptionPart:i?.shieldConfiguration?.shieldEncryptionKey||null});let d=null,c=null;if(e.shieldAuthentication&&(d="openfort"===e.shieldAuthentication.auth?"openfort":"custom",c=e.shieldAuthentication.token),!this.storage)throw new o("Storage not available in EmbeddedWalletApi",n.INVALID_CONFIGURATION);return await t.embedded(this.storage,e.chainId,s,d,c),this.get()}async signMessage(e,r){await this.ensureInitialized(),await this.validateAndRefreshToken();const i=await t.fromStorage(this.storage);if(!i)throw new o("No signer configured",n.MISSING_SIGNER_ERROR);const{hashMessage:a=!0,arrayifyMessage:s=!1}=r||{};return await i.sign(e,s,a)}async signTypedData(e,i,a){await this.ensureInitialized(),await this.validateAndRefreshToken();const s=await t.fromStorage(this.storage),c=await r.fromStorage(this.storage);if(!s||!c)throw new o("No signer configured",n.MISSING_SIGNER_ERROR);return await d({domain:e,types:i,message:a},c.type,Number(c.chainId),s,c.address)}async exportPrivateKey(){await this.ensureInitialized(),await this.validateAndRefreshToken();const e=await t.fromStorage(this.storage);if(!e)throw new o("No signer configured",n.MISSING_SIGNER_ERROR);return await e.export()}async setEmbeddedRecovery({recoveryMethod:e,recoveryPassword:r,encryptionSession:i}){await this.ensureInitialized(),await this.validateAndRefreshToken();const a=await t.fromStorage(this.storage);if(!a)throw new o("No signer configured",n.MISSING_SIGNER_ERROR);if("password"===e&&!r)throw new o("Recovery password is required",n.INVALID_CONFIGURATION);await a.setEmbeddedRecovery({recoveryMethod:e,recoveryPassword:r,encryptionSession:i})}async get(){const e=await r.fromStorage(this.storage);if(!e)throw new o("No signer configured",n.MISSING_SIGNER_ERROR);const t=await i.fromStorage(this.storage);if(!t)throw new o("No access token found",n.INTERNAL_ERROR);return{chainId:e.chainId.toString(),owner:{id:t.player},address:e.address,ownerAddress:"solana"===e.type?void 0:e.ownerAddress,chainType:"solana"===e.type?"solana":"ethereum",implementationType:"solana"===e.type?void 0:e.type}}async list(){const e=a.fromStorage();if(!e)throw new o("Configuration not found",n.INVALID_CONFIGURATION);await this.ensureInitialized(),await this.validateAndRefreshToken();const t=await i.fromStorage(this.storage);if(!t)throw new o("No access token found",n.NOT_LOGGED_IN_ERROR);return s(async()=>(await this.backendApiClients.accountsApi.getAccounts(void 0,{headers:{authorization:`Bearer ${e.baseConfiguration.publishableKey}`,"x-player-token":t.token,"x-auth-provider":t.thirdPartyProvider,"x-token-type":t.thirdPartyTokenType}})).data.data.map(e=>({owner:{id:e.player.id},chainType:"ethereum",address:e.address,ownerAddress:e.ownerAddress,createdAt:e.createdAt,implementationType:e.accountType,chainId:e.chainId.toString()})),{default:n.AUTHENTICATION_ERROR})}async getEmbeddedState(){try{if(!await i.fromStorage(this.storage))return u.UNAUTHENTICATED;if(!await t.fromStorage(this.storage))return u.EMBEDDED_SIGNER_NOT_CONFIGURED;return await r.fromStorage(this.storage)?u.READY:u.CREATING_ACCOUNT}catch(e){return console.error("Failed to get embedded state:",e),u.UNAUTHENTICATED}}async getEthereumProvider(e){await this.ensureInitialized();const a={announceProvider:!0,...e},o=await i.fromStorage(this.storage),n=await t.fromStorage(this.storage),s=await r.fromStorage(this.storage);return this.provider?this.provider&&a.policy&&this.provider.updatePolicy(a.policy):(this.provider=new c({storage:this.storage,openfortEventEmitter:new g,signer:n||void 0,account:s||void 0,authentication:o||void 0,backendApiClients:this.backendApiClients,policyId:a.policy,validateAndRefreshSession:this.validateAndRefreshToken.bind(this),chains:a.chains}),a.announceProvider&&h({info:{...f,...a.providerInfo},provider:this.provider})),this.provider}async ping(e){try{const t=this.getIframeManager();if(!t.isLoaded())return!1;e>0&&await new Promise(t=>{setTimeout(t,e)});const r=await i.fromStorage(this.storage);if(r)try{return await t.getCurrentUser(r.player),!0}catch(e){return!1}return t.isLoaded()}catch(e){return!1}}getURL(){const e=a.fromStorage();if(!e)throw new o("Configuration not found",n.INVALID_CONFIGURATION);return e.iframeUrl}}export{p as EmbeddedWalletApi};
@@ -1 +1 @@
1
- var o;!function(o){o.AUTHENTICATION="openfort.authentication",o.SIGNER="openfort.signer",o.CONFIGURATION="openfort.configuration",o.ACCOUNT="openfort.account",o.TEST="openfort.test",o.RECOVERY="openfort.recovery",o.SESSION="openfort.session",o.PKCE_STATE="openfort.pkce_state",o.PKCE_VERIFIER="openfort.pkce_verifier"}(o||(o={}));export{o as StorageKeys};
1
+ var o;!function(o){o.AUTHENTICATION="openfort.authentication",o.CONFIGURATION="openfort.configuration",o.ACCOUNT="openfort.account",o.TEST="openfort.test",o.RECOVERY="openfort.recovery",o.SESSION="openfort.session",o.PKCE_STATE="openfort.pkce_state",o.PKCE_VERIFIER="openfort.pkce_verifier"}(o||(o={}));export{o as StorageKeys};
@@ -1 +1 @@
1
- const o="0.9.2",t="@openfort/openfort-js";export{t as PACKAGE,o as VERSION};
1
+ const o="0.9.3",t="@openfort/openfort-js";export{t as PACKAGE,o as VERSION};
@@ -1 +1 @@
1
- import{WindowMessenger as e,connect as t}from"../../../node_modules/penpal/dist/penpal.js";import{randomUUID as i}from"../utils/crypto.js";import{OpenfortError as o,OpenfortErrorType as r}from"../core/errors/openfortError.js";import{isErrorResponse as n,NOT_CONFIGURED_ERROR as s,MISSING_USER_ENTROPY_ERROR as a,MISSING_PROJECT_ENTROPY_ERROR as d,INCORRECT_USER_ENTROPY_ERROR as c,Event as h,SignRequest as u,SwitchChainRequest as f,ExportPrivateKeyRequest as y,SetRecoveryMethodRequest as l,GetCurrentDeviceRequest as w,ShieldAuthType as m,UpdateAuthenticationRequest as p}from"./types.js";class g extends Error{constructor(){super("This embedded signer requires a password to be recovered")}}class v extends Error{constructor(){super("MissingProjectEntropyError")}}class C extends Error{constructor(){super("Wrong recovery password for this embedded signer")}}class I extends Error{message;constructor(e){super(`Unknown response from iframe: ${e}`),this.message=e||""}}class b extends Error{constructor(){super("Not configured")}}class k{iframe;connection;remote;storage;sdkConfiguration;isInitialized=!1;constructor(e,t){if(!e)throw new o("Configuration is required for IframeManager",r.INVALID_CONFIGURATION);if(!t)throw new o("Storage is required for IframeManager",r.INVALID_CONFIGURATION);this.sdkConfiguration=e,this.storage=t}async iframeSetup(){if("undefined"==typeof document)throw new o("Document is not available. Please provide a message poster for non-browser environments.",r.INVALID_CONFIGURATION);const e=document.getElementById("openfort-iframe");e&&document.body.removeChild(e);const t=document.createElement("iframe");t.style.display="none",t.id="openfort-iframe",document.body.appendChild(t),t.src=this.sdkConfiguration.iframeUrl,this.iframe=t,await new Promise((e,i)=>{const o=setTimeout(()=>{i(new Error("Iframe load timeout"))},1e4);t.onload=()=>{clearTimeout(o),e()},t.onerror=()=>{clearTimeout(o),i(new Error("Failed to load iframe"))}})}async establishIframeConnection(){if("undefined"==typeof document)throw new o("Document is not available.",r.INVALID_CONFIGURATION);if(!this.iframe?.contentWindow)throw new o("Iframe does not have content window",r.INVALID_CONFIGURATION);const i=new URL(this.sdkConfiguration.iframeUrl).origin,n=new e({remoteWindow:this.iframe.contentWindow,allowedOrigins:[i]});this.connection=t({messenger:n,timeout:5e3});try{this.remote=await this.connection.promise,this.isInitialized=!0}catch(e){const t=e;throw new Error(`v3 Failed to establish Iframe connection: ${t.cause||t.message}`,t)}}isLoaded(){return this.isInitialized&&void 0!==this.remote}async ensureConnection(){if(!this.isLoaded()){if("undefined"==typeof document)throw new o("Document is not available.",r.INVALID_CONFIGURATION);await this.iframeSetup(),await this.establishIframeConnection()}if(!this.remote)throw new Error("Failed to establish connection with iframe");return this.remote}static handleError(e){if(n(e)){if(e.error===s)throw new b;if(e.error===a)throw new g;if(e.error===d)throw new v;if(e.error===c)throw new C;throw new I(e.error)}throw e}async configure(e){if(!this.sdkConfiguration.shieldConfiguration)throw new Error("shieldConfiguration is required");const t=await this.ensureConnection(),o={uuid:i(),action:h.CONFIGURE,chainId:e.chainId,recovery:e.recovery,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,shieldAPIKey:this.sdkConfiguration.shieldConfiguration?.shieldPublishableKey||"",accessToken:e.accessToken,playerID:e.playerID,thirdPartyProvider:e.thirdPartyProvider,thirdPartyTokenType:e.thirdPartyTokenType,encryptionKey:e.password,encryptionPart:this.sdkConfiguration?.shieldConfiguration?.shieldEncryptionKey??null,encryptionSession:e.recovery?.encryptionSession??null,openfortURL:this.sdkConfiguration.backendUrl,shieldURL:this.sdkConfiguration.shieldUrl},r=await t.configure(o);return n(r)&&k.handleError(r),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",r.version??"undefined"),r}async sign(e,t,o,r){const s=await this.ensureConnection(),a={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},d=new u(i(),t,o,r,a);try{const e=await s.sign(d);return n(e)&&k.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e.signature}catch(i){if(i instanceof b)return await this.configure(e),this.sign(e,t,o,r);throw i}}async switchChain(e,t){const o=await this.ensureConnection(),r={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},s=new f(i(),t,r);try{const e=await o.switchChain(s);return n(e)&&k.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e}catch(i){if(i instanceof b)return await this.configure(e),this.switchChain(e,t);throw i}}async export(e){const t=await this.ensureConnection(),o={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},r=new y(i(),o);try{const e=await t.export(r);return n(e)&&k.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e.key}catch(t){if(t instanceof b)return await this.configure(e),this.export(e);throw t}}async setEmbeddedRecovery(e,t,o,r){const s=await this.ensureConnection(),a={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},d=new l(i(),t,o,r,a);try{const e=await s.setRecoveryMethod(d);n(e)&&k.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined")}catch(i){if(i instanceof b)return await this.configure(e),this.setEmbeddedRecovery(e,t,o,r);throw i}}async getCurrentUser(e){const t=await this.ensureConnection(),o=new w(i(),e);try{const e=await t.getCurrentDevice(o);return n(e)&&k.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e}catch(e){if(e instanceof b)return null;throw e}}async logout(){const e=await this.ensureConnection(),t={uuid:i()};await e.logout(t)}async updateAuthentication(e,t,o){const r={...e};r.accessToken=t,o===m.OPENFORT&&r.recovery&&(r.recovery={...r.recovery,token:t});const n=await this.ensureConnection(),s=new p(i(),t);try{await n.updateAuthentication(s)}catch(e){if(e instanceof b)return await this.configure(r),void await this.updateAuthentication(r,t,o);throw e}}destroy(){if(this.connection&&(this.connection.destroy(),this.connection=void 0),this.remote=void 0,this.isInitialized=!1,this.iframe&&"undefined"!=typeof document){const e=document.getElementById("openfort-iframe");e&&e.parentNode&&e.parentNode.removeChild(e),this.iframe=void 0}}}export{k as IframeManager,v as MissingProjectEntropyError,g as MissingRecoveryPasswordError,b as NotConfiguredError,I as UnknownResponseError,C as WrongRecoveryPasswordError};
1
+ import{WindowMessenger as e,connect as t}from"../../../node_modules/penpal/dist/penpal.js";import{StorageKeys as i}from"../storage/istorage.js";import{randomUUID as o}from"../utils/crypto.js";import{OpenfortError as r,OpenfortErrorType as n}from"../core/errors/openfortError.js";import{isErrorResponse as s,NOT_CONFIGURED_ERROR as a,MISSING_USER_ENTROPY_ERROR as d,MISSING_PROJECT_ENTROPY_ERROR as c,INCORRECT_USER_ENTROPY_ERROR as h,Event as u,SignRequest as f,SwitchChainRequest as y,ExportPrivateKeyRequest as l,SetRecoveryMethodRequest as m,GetCurrentDeviceRequest as w,ShieldAuthType as p,UpdateAuthenticationRequest as g}from"./types.js";class v extends Error{constructor(){super("This embedded signer requires a password to be recovered")}}class C extends Error{constructor(){super("MissingProjectEntropyError")}}class I extends Error{constructor(){super("Wrong recovery password for this embedded signer")}}class b extends Error{message;constructor(e){super(`Unknown response from iframe: ${e}`),this.message=e||""}}class k extends Error{constructor(){super("Not configured")}}class T{iframe;connection;remote;storage;sdkConfiguration;isInitialized=!1;constructor(e,t){if(!e)throw new r("Configuration is required for IframeManager",n.INVALID_CONFIGURATION);if(!t)throw new r("Storage is required for IframeManager",n.INVALID_CONFIGURATION);this.sdkConfiguration=e,this.storage=t}async iframeSetup(){if("undefined"==typeof document)throw new r("Document is not available. Please provide a message poster for non-browser environments.",n.INVALID_CONFIGURATION);const e=document.getElementById("openfort-iframe");e&&document.body.removeChild(e);const t=document.createElement("iframe");t.style.display="none",t.id="openfort-iframe",document.body.appendChild(t),t.src=this.sdkConfiguration.iframeUrl,this.iframe=t,await new Promise((e,i)=>{const o=setTimeout(()=>{i(new Error("Iframe load timeout"))},1e4);t.onload=()=>{clearTimeout(o),e()},t.onerror=()=>{clearTimeout(o),i(new Error("Failed to load iframe"))}})}async establishIframeConnection(){if("undefined"==typeof document)throw new r("Document is not available.",n.INVALID_CONFIGURATION);if(!this.iframe?.contentWindow)throw new r("Iframe does not have content window",n.INVALID_CONFIGURATION);const i=new URL(this.sdkConfiguration.iframeUrl).origin,o=new e({remoteWindow:this.iframe.contentWindow,allowedOrigins:[i]});this.connection=t({messenger:o,timeout:5e3});try{this.remote=await this.connection.promise,this.isInitialized=!0}catch(e){const t=e;throw new r(`Failed to establish iFrame connection: ${t.cause||t.message}`,n.INTERNAL_ERROR,e)}}isLoaded(){return this.isInitialized&&void 0!==this.remote}async ensureConnection(){if(!this.isLoaded()){if("undefined"==typeof document)throw new r("Document is not available.",n.INVALID_CONFIGURATION);await this.iframeSetup(),await this.establishIframeConnection()}if(!this.remote)throw new r("Failed to establish iFrame connection",n.INTERNAL_ERROR);return this.remote}handleError(e){if(s(e)){if(e.error===a)throw new k;if(e.error===d)throw this.storage.remove(i.ACCOUNT),new v;if(e.error===c)throw this.storage.remove(i.ACCOUNT),new C;if(e.error===h)throw new I;throw new b(e.error)}throw e}async configure(e){if(!this.sdkConfiguration.shieldConfiguration)throw new Error("shieldConfiguration is required");const t=await this.ensureConnection(),i={uuid:o(),action:u.CONFIGURE,chainId:e.chainId,recovery:e.recovery,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,shieldAPIKey:this.sdkConfiguration.shieldConfiguration?.shieldPublishableKey||"",accessToken:e.accessToken,playerID:e.playerID,thirdPartyProvider:e.thirdPartyProvider,thirdPartyTokenType:e.thirdPartyTokenType,encryptionKey:e.password,encryptionPart:this.sdkConfiguration?.shieldConfiguration?.shieldEncryptionKey??null,encryptionSession:e.recovery?.encryptionSession??null,openfortURL:this.sdkConfiguration.backendUrl,shieldURL:this.sdkConfiguration.shieldUrl},r=await t.configure(i);return s(r)&&this.handleError(r),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",r.version??"undefined"),r}async sign(e,t,i,r){const n=await this.ensureConnection(),a={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},d=new f(o(),t,i,r,a);try{const e=await n.sign(d);return s(e)&&this.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e.signature}catch(o){if(o instanceof k)return await this.configure(e),this.sign(e,t,i,r);throw o}}async switchChain(e,t){const i=await this.ensureConnection(),r={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},n=new y(o(),t,r);try{const e=await i.switchChain(n);return s(e)&&this.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e}catch(i){if(i instanceof k)return await this.configure(e),this.switchChain(e,t);throw i}}async export(e){const t=await this.ensureConnection(),i={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},r=new l(o(),i);try{const e=await t.export(r);return s(e)&&this.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e.key}catch(t){if(t instanceof k)return await this.configure(e),this.export(e);throw t}}async setEmbeddedRecovery(e,t,i,r){const n=await this.ensureConnection(),a={thirdPartyProvider:e.thirdPartyProvider??void 0,thirdPartyTokenType:e.thirdPartyTokenType??void 0,token:e.accessToken??void 0,publishableKey:this.sdkConfiguration.baseConfiguration.publishableKey,openfortURL:this.sdkConfiguration.backendUrl},d=new m(o(),t,i,r,a);try{const e=await n.setRecoveryMethod(d);s(e)&&this.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined")}catch(o){if(o instanceof k)return await this.configure(e),this.setEmbeddedRecovery(e,t,i,r);throw o}}async getCurrentUser(e){const t=await this.ensureConnection(),i=new w(o(),e);try{const e=await t.getCurrentDevice(i);return s(e)&&this.handleError(e),"undefined"!=typeof sessionStorage&&sessionStorage.setItem("iframe-version",e.version??"undefined"),e}catch(e){if(e instanceof k)return null;throw e}}async logout(){const e=await this.ensureConnection(),t={uuid:o()};await e.logout(t)}async updateAuthentication(e,t,i){const r={...e};r.accessToken=t,i===p.OPENFORT&&r.recovery&&(r.recovery={...r.recovery,token:t});const n=await this.ensureConnection(),s=new g(o(),t);try{await n.updateAuthentication(s)}catch(e){if(e instanceof k)return await this.configure(r),void await this.updateAuthentication(r,t,i);throw e}}destroy(){if(this.connection&&(this.connection.destroy(),this.connection=void 0),this.remote=void 0,this.isInitialized=!1,this.iframe&&"undefined"!=typeof document){const e=document.getElementById("openfort-iframe");e&&e.parentNode&&e.parentNode.removeChild(e),this.iframe=void 0}}}export{T as IframeManager,C as MissingProjectEntropyError,v as MissingRecoveryPasswordError,k as NotConfiguredError,b as UnknownResponseError,I as WrongRecoveryPasswordError};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfort/openfort-js",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
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",