@openfort/openfort-js 0.7.7 → 0.7.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -10,60 +10,6 @@ import { secp256k1 } from '@noble/curves/secp256k1';
10
10
  import { SigningKey } from '@ethersproject/signing-key';
11
11
  import { computeAddress } from '@ethersproject/transactions';
12
12
 
13
- var EmbeddedState;
14
- (function (EmbeddedState) {
15
- EmbeddedState[EmbeddedState["NONE"] = 0] = "NONE";
16
- EmbeddedState[EmbeddedState["UNAUTHENTICATED"] = 1] = "UNAUTHENTICATED";
17
- EmbeddedState[EmbeddedState["EMBEDDED_SIGNER_NOT_CONFIGURED"] = 2] = "EMBEDDED_SIGNER_NOT_CONFIGURED";
18
- EmbeddedState[EmbeddedState["CREATING_ACCOUNT"] = 3] = "CREATING_ACCOUNT";
19
- EmbeddedState[EmbeddedState["READY"] = 4] = "READY";
20
- })(EmbeddedState || (EmbeddedState = {}));
21
- var OpenfortEvents;
22
- (function (OpenfortEvents) {
23
- OpenfortEvents["LOGGED_OUT"] = "loggedOut";
24
- })(OpenfortEvents || (OpenfortEvents = {}));
25
- var AccountType;
26
- (function (AccountType) {
27
- AccountType["UPGRADEABLE_V4"] = "Upgradeable_v04";
28
- AccountType["MANAGED_V4"] = "Managed_v04";
29
- AccountType["ERC6551_V4"] = "ERC6551_v04";
30
- AccountType["ERC6551_V5"] = "ERC6551_v05";
31
- AccountType["RECOVERABLE_V4"] = "Recoverable_v04";
32
- AccountType["MANAGED_V5"] = "Managed_v05";
33
- AccountType["UPGRADEABLE_V5"] = "Upgradeable_v05";
34
- })(AccountType || (AccountType = {}));
35
- var TokenType;
36
- (function (TokenType) {
37
- TokenType["ID_TOKEN"] = "idToken";
38
- TokenType["CUSTOM_TOKEN"] = "customToken";
39
- })(TokenType || (TokenType = {}));
40
- var ThirdPartyOAuthProvider;
41
- (function (ThirdPartyOAuthProvider) {
42
- ThirdPartyOAuthProvider["ACCELBYTE"] = "accelbyte";
43
- ThirdPartyOAuthProvider["FIREBASE"] = "firebase";
44
- ThirdPartyOAuthProvider["LOOTLOCKER"] = "lootlocker";
45
- ThirdPartyOAuthProvider["PLAYFAB"] = "playfab";
46
- ThirdPartyOAuthProvider["SUPABASE"] = "supabase";
47
- ThirdPartyOAuthProvider["CUSTOM"] = "custom";
48
- ThirdPartyOAuthProvider["OIDC"] = "oidc";
49
- })(ThirdPartyOAuthProvider || (ThirdPartyOAuthProvider = {}));
50
- var BasicAuthProvider;
51
- (function (BasicAuthProvider) {
52
- BasicAuthProvider["EMAIL"] = "email";
53
- BasicAuthProvider["WALLET"] = "wallet";
54
- })(BasicAuthProvider || (BasicAuthProvider = {}));
55
- var OAuthProvider;
56
- (function (OAuthProvider) {
57
- OAuthProvider["GOOGLE"] = "google";
58
- OAuthProvider["TWITTER"] = "twitter";
59
- OAuthProvider["FACEBOOK"] = "facebook";
60
- })(OAuthProvider || (OAuthProvider = {}));
61
- var CodeChallengeMethodEnum;
62
- (function (CodeChallengeMethodEnum) {
63
- CodeChallengeMethodEnum["PLAIN"] = "plain";
64
- CodeChallengeMethodEnum["S256"] = "S256";
65
- })(CodeChallengeMethodEnum || (CodeChallengeMethodEnum = {}));
66
-
67
13
  /* tslint:disable */
68
14
  /* eslint-disable */
69
15
  /**
@@ -3067,6 +3013,65 @@ const createConfig = ({ basePath, accessToken, }) => {
3067
3013
  return new Configuration(apiConfigOptions);
3068
3014
  };
3069
3015
 
3016
+ var EmbeddedState;
3017
+ (function (EmbeddedState) {
3018
+ EmbeddedState[EmbeddedState["NONE"] = 0] = "NONE";
3019
+ EmbeddedState[EmbeddedState["UNAUTHENTICATED"] = 1] = "UNAUTHENTICATED";
3020
+ EmbeddedState[EmbeddedState["EMBEDDED_SIGNER_NOT_CONFIGURED"] = 2] = "EMBEDDED_SIGNER_NOT_CONFIGURED";
3021
+ EmbeddedState[EmbeddedState["CREATING_ACCOUNT"] = 3] = "CREATING_ACCOUNT";
3022
+ EmbeddedState[EmbeddedState["READY"] = 4] = "READY";
3023
+ })(EmbeddedState || (EmbeddedState = {}));
3024
+ var OpenfortEvents;
3025
+ (function (OpenfortEvents) {
3026
+ OpenfortEvents["LOGGED_OUT"] = "loggedOut";
3027
+ })(OpenfortEvents || (OpenfortEvents = {}));
3028
+ var AccountType;
3029
+ (function (AccountType) {
3030
+ AccountType["UPGRADEABLE_V4"] = "Upgradeable_v04";
3031
+ AccountType["MANAGED_V4"] = "Managed_v04";
3032
+ AccountType["ERC6551_V4"] = "ERC6551_v04";
3033
+ AccountType["ERC6551_V5"] = "ERC6551_v05";
3034
+ AccountType["RECOVERABLE_V4"] = "Recoverable_v04";
3035
+ AccountType["MANAGED_V5"] = "Managed_v05";
3036
+ AccountType["UPGRADEABLE_V5"] = "Upgradeable_v05";
3037
+ })(AccountType || (AccountType = {}));
3038
+ var AuthType;
3039
+ (function (AuthType) {
3040
+ AuthType["OPENFORT"] = "openfort";
3041
+ AuthType["THIRD_PARTY"] = "thirdParty";
3042
+ })(AuthType || (AuthType = {}));
3043
+ var TokenType;
3044
+ (function (TokenType) {
3045
+ TokenType["ID_TOKEN"] = "idToken";
3046
+ TokenType["CUSTOM_TOKEN"] = "customToken";
3047
+ })(TokenType || (TokenType = {}));
3048
+ var ThirdPartyOAuthProvider;
3049
+ (function (ThirdPartyOAuthProvider) {
3050
+ ThirdPartyOAuthProvider["ACCELBYTE"] = "accelbyte";
3051
+ ThirdPartyOAuthProvider["FIREBASE"] = "firebase";
3052
+ ThirdPartyOAuthProvider["LOOTLOCKER"] = "lootlocker";
3053
+ ThirdPartyOAuthProvider["PLAYFAB"] = "playfab";
3054
+ ThirdPartyOAuthProvider["SUPABASE"] = "supabase";
3055
+ ThirdPartyOAuthProvider["CUSTOM"] = "custom";
3056
+ ThirdPartyOAuthProvider["OIDC"] = "oidc";
3057
+ })(ThirdPartyOAuthProvider || (ThirdPartyOAuthProvider = {}));
3058
+ var BasicAuthProvider;
3059
+ (function (BasicAuthProvider) {
3060
+ BasicAuthProvider["EMAIL"] = "email";
3061
+ BasicAuthProvider["WALLET"] = "wallet";
3062
+ })(BasicAuthProvider || (BasicAuthProvider = {}));
3063
+ var OAuthProvider;
3064
+ (function (OAuthProvider) {
3065
+ OAuthProvider["GOOGLE"] = "google";
3066
+ OAuthProvider["TWITTER"] = "twitter";
3067
+ OAuthProvider["FACEBOOK"] = "facebook";
3068
+ })(OAuthProvider || (OAuthProvider = {}));
3069
+ var CodeChallengeMethodEnum;
3070
+ (function (CodeChallengeMethodEnum) {
3071
+ CodeChallengeMethodEnum["PLAIN"] = "plain";
3072
+ CodeChallengeMethodEnum["S256"] = "S256";
3073
+ })(CodeChallengeMethodEnum || (CodeChallengeMethodEnum = {}));
3074
+
3070
3075
  var OpenfortErrorType;
3071
3076
  (function (OpenfortErrorType) {
3072
3077
  OpenfortErrorType["AUTHENTICATION_ERROR"] = "AUTHENTICATION_ERROR";
@@ -4601,8 +4606,8 @@ class EvmProvider {
4601
4606
  const openfortProviderInfo = {
4602
4607
  // eslint-disable-next-line max-len
4603
4608
  icon: 'data:image/svg+xml,<svg aria-hidden="true" viewBox="597.32 331.34 171.36 105.32" ><g><rect x="673.9" y="404.26" width="18.2" height="32.4" /><polygon points="768.68,331.36 768.68,331.36 768.68,331.34 610.78,331.34 610.78,331.36 597.32,331.36 597.32,436.64 615.52,436.64 615.52,349.54 750.48,349.54 750.48,436.64 768.68,436.64 " /><polygon points="732.16,367.79 633.83,367.79 633.83,370.19 633.79,370.19 633.79,436.64 651.99,436.64 651.99,385.99 713.9,385.99 713.9,436.64 732.09,436.64 732.09,385.99 732.16,385.99 " /></g></svg>',
4604
- name: 'Ecosystem ID',
4605
- rdns: 'com.ecosystem.id',
4609
+ name: 'Openfort',
4610
+ rdns: 'xyz.openfort',
4606
4611
  uuid: v4(),
4607
4612
  };
4608
4613
  function announceProvider(detail) {
@@ -4660,11 +4665,14 @@ class AuthManager {
4660
4665
  this.instanceManager = instanceManager;
4661
4666
  }
4662
4667
  async initOAuth(provider, options) {
4668
+ const usePooling = options?.usePooling ?? false;
4669
+ // eslint-disable-next-line no-param-reassign
4670
+ delete options?.usePooling;
4663
4671
  const request = {
4664
4672
  oAuthInitRequest: {
4665
4673
  provider,
4666
4674
  options,
4667
- usePooling: options?.usePooling || false,
4675
+ usePooling,
4668
4676
  },
4669
4677
  };
4670
4678
  const result = await this.backendApiClients.authenticationApi.initOAuth(request);
@@ -4846,7 +4854,13 @@ class AuthManager {
4846
4854
  return response.data;
4847
4855
  }, OpenfortErrorType.USER_REGISTRATION_ERROR);
4848
4856
  }
4849
- async validateCredentials(accessToken, refreshToken, jwk) {
4857
+ async validateCredentials() {
4858
+ const jwk = await this.instanceManager.getJWK();
4859
+ const accessToken = this.instanceManager.getAccessToken()?.token;
4860
+ const refreshToken = this.instanceManager.getRefreshToken();
4861
+ if (!accessToken || !refreshToken || !jwk) {
4862
+ throw new OpenfortError('Must be logged in to validate and refresh token', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
4863
+ }
4850
4864
  try {
4851
4865
  const key = (await importJWK({
4852
4866
  kty: jwk.kty,
@@ -5022,6 +5036,8 @@ const chainIDStorageKey = 'openfort.chain_id';
5022
5036
  const jwksStorageKey = 'openfort.jwk';
5023
5037
  const deviceIDStorageKey = 'openfort.device_id';
5024
5038
  const accountTypeStorageKey = 'openfort.account_type';
5039
+ const shieldAuthTypeStorageKey = 'openfort.shield_auth_type';
5040
+ const shieldAuthTokenStorageKey = 'openfort.shield_auth_token';
5025
5041
  const accountAddressStorageKey = 'openfort.account_address';
5026
5042
 
5027
5043
  class InstanceManager {
@@ -5031,6 +5047,7 @@ class InstanceManager {
5031
5047
  jwk = null;
5032
5048
  sessionKey = null;
5033
5049
  deviceID = null;
5050
+ shieldAuthType = null;
5034
5051
  chainId = null;
5035
5052
  accountAddress = null;
5036
5053
  accountType = null;
@@ -5162,9 +5179,10 @@ class InstanceManager {
5162
5179
  };
5163
5180
  const response = await this.backendApiClients.authenticationApi.getJwks(request);
5164
5181
  if (response.data.keys.length === 0) {
5165
- throw new Error('No keys found');
5182
+ throw new Error('Internal error: No JWKS keys found');
5166
5183
  }
5167
5184
  const jwtKey = response.data.keys[0];
5185
+ this.setJWK(jwtKey);
5168
5186
  this.jwk = {
5169
5187
  kty: jwtKey.kty,
5170
5188
  crv: jwtKey.crv,
@@ -5249,6 +5267,34 @@ class InstanceManager {
5249
5267
  this.accountType = null;
5250
5268
  this.persistentStorage.remove(accountTypeStorageKey);
5251
5269
  }
5270
+ setShieldAuthType(accountType) {
5271
+ this.accountType = accountType;
5272
+ this.persistentStorage.save(shieldAuthTypeStorageKey, accountType);
5273
+ }
5274
+ getShieldAuthType() {
5275
+ if (!this.accountType) {
5276
+ this.accountType = this.persistentStorage.get(shieldAuthTypeStorageKey);
5277
+ }
5278
+ return this.accountType;
5279
+ }
5280
+ removeShieldAuthType() {
5281
+ this.accountType = null;
5282
+ this.persistentStorage.remove(shieldAuthTypeStorageKey);
5283
+ }
5284
+ setShieldAuthToken(accountType) {
5285
+ this.accountType = accountType;
5286
+ this.persistentStorage.save(shieldAuthTokenStorageKey, accountType);
5287
+ }
5288
+ getShieldAuthToken() {
5289
+ if (!this.accountType) {
5290
+ this.accountType = this.persistentStorage.get(shieldAuthTokenStorageKey);
5291
+ }
5292
+ return this.accountType;
5293
+ }
5294
+ removeShieldAuthToken() {
5295
+ this.accountType = null;
5296
+ this.persistentStorage.remove(shieldAuthTokenStorageKey);
5297
+ }
5252
5298
  }
5253
5299
 
5254
5300
  class LocalStorage {
@@ -5594,11 +5640,11 @@ class UpdateAuthenticationRequest {
5594
5640
  this.recovery = recovery;
5595
5641
  }
5596
5642
  }
5597
- var AuthType;
5598
- (function (AuthType) {
5599
- AuthType["OPENFORT"] = "openfort";
5600
- AuthType["CUSTOM"] = "custom";
5601
- })(AuthType || (AuthType = {}));
5643
+ var ShieldAuthType;
5644
+ (function (ShieldAuthType) {
5645
+ ShieldAuthType["OPENFORT"] = "openfort";
5646
+ ShieldAuthType["CUSTOM"] = "custom";
5647
+ })(ShieldAuthType || (ShieldAuthType = {}));
5602
5648
 
5603
5649
  class MissingRecoveryPasswordError extends Error {
5604
5650
  constructor() {
@@ -5849,6 +5895,9 @@ class Openfort {
5849
5895
  this.openfortEventEmitter = new TypedEventEmitter();
5850
5896
  this.iframeManager = new IframeManager(this.config);
5851
5897
  }
5898
+ /**
5899
+ * Logs the user out by flushing the signer and removing credentials.
5900
+ */
5852
5901
  async logout() {
5853
5902
  await this.flushSigner();
5854
5903
  if (this.credentialsProvided()) {
@@ -5876,11 +5925,18 @@ class Openfort {
5876
5925
  this.instanceManager.removeChainID();
5877
5926
  this.instanceManager.removeDeviceID();
5878
5927
  this.instanceManager.removeJWK();
5928
+ this.instanceManager.removeShieldAuthType();
5929
+ this.instanceManager.removeShieldAuthToken();
5879
5930
  }
5880
5931
  }
5881
- getEthereumProvider(options = {
5882
- announceProvider: true,
5883
- }) {
5932
+ /**
5933
+ * Returns an Ethereum provider using the configured signer.
5934
+ *
5935
+ * @param options - Configuration options for the Ethereum provider.
5936
+ * @returns A Provider instance.
5937
+ * @throws {OpenfortError} If the signer is not an EmbeddedSigner.
5938
+ */
5939
+ getEthereumProvider(options = { announceProvider: true }) {
5884
5940
  if (!(this.signer instanceof EmbeddedSigner)) {
5885
5941
  throw new OpenfortError('Embedded signer must be configured to get Ethereum provider', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
5886
5942
  }
@@ -5901,33 +5957,15 @@ class Openfort {
5901
5957
  }
5902
5958
  return provider;
5903
5959
  }
5904
- async flushSigner() {
5905
- if (this.signer) {
5906
- await this.signer.logout();
5907
- this.instanceManager.removeSignerType();
5908
- return;
5909
- }
5910
- const signerType = this.instanceManager.getSignerType();
5911
- switch (signerType) {
5912
- case SignerType.EMBEDDED: {
5913
- const iframeConfiguration = {
5914
- accessToken: this.instanceManager.getAccessToken()?.token ?? null,
5915
- thirdPartyProvider: this.instanceManager.getAccessToken()?.thirdPartyProvider ?? null,
5916
- thirdPartyTokenType: this.instanceManager.getAccessToken()?.thirdPartyTokenType ?? null,
5917
- chainId: null,
5918
- recovery: null,
5919
- };
5920
- const embeddedSigner = new EmbeddedSigner(this.iframeManager, this.instanceManager, iframeConfiguration);
5921
- await embeddedSigner.logout();
5922
- break;
5923
- }
5924
- case SignerType.SESSION:
5925
- this.configureSessionKey();
5926
- break;
5927
- }
5928
- this.instanceManager.removeSignerType();
5929
- }
5960
+ /**
5961
+ * Configures a session key and returns the session key details.
5962
+ *
5963
+ * @returns A SessionKey object containing the address and registration status.
5964
+ */
5930
5965
  configureSessionKey() {
5966
+ if (this.instanceManager.getSignerType() === SignerType.EMBEDDED) {
5967
+ throw new OpenfortError('Session signer must be configured to sign a session', OpenfortErrorType.MISSING_SESSION_SIGNER_ERROR);
5968
+ }
5931
5969
  const signer = new SessionSigner(this.instanceManager);
5932
5970
  this.signer = signer;
5933
5971
  const publicKey = signer.loadKeys();
@@ -5938,30 +5976,35 @@ class Openfort {
5938
5976
  this.instanceManager.setSignerType(SignerType.SESSION);
5939
5977
  return { address: publicKey, isRegistered: true };
5940
5978
  }
5979
+ /**
5980
+ * Configures an embedded signer.
5981
+ *
5982
+ * @param chainId - The chain ID for the embedded signer.
5983
+ * @param shieldAuthentication - Shield authentication details.
5984
+ * @param recoveryPassword - Recovery password.
5985
+ */
5941
5986
  async configureEmbeddedSigner(chainId, shieldAuthentication, recoveryPassword) {
5942
- const signer = this.newEmbeddedSigner(chainId, shieldAuthentication);
5943
5987
  await this.validateAndRefreshToken();
5988
+ if (shieldAuthentication) {
5989
+ this.instanceManager.setShieldAuthType(shieldAuthentication?.auth);
5990
+ if (shieldAuthentication?.auth === ShieldAuthType.CUSTOM) {
5991
+ this.instanceManager.setShieldAuthToken(shieldAuthentication?.token);
5992
+ }
5993
+ }
5994
+ const signer = this.newEmbeddedSigner(chainId);
5944
5995
  await signer.ensureEmbeddedAccount(recoveryPassword);
5945
5996
  this.signer = signer;
5946
5997
  this.instanceManager.setSignerType(SignerType.EMBEDDED);
5947
5998
  }
5948
- newEmbeddedSigner(chainId, shieldAuthentication) {
5949
- if (!this.credentialsProvided()) {
5950
- throw new OpenfortError('Must be logged in to configure embedded signer', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
5951
- }
5952
- const iframeConfiguration = {
5953
- accessToken: this.instanceManager.getAccessToken()?.token ?? null,
5954
- thirdPartyProvider: this.instanceManager.getAccessToken()?.thirdPartyProvider ?? null,
5955
- thirdPartyTokenType: this.instanceManager.getAccessToken()?.thirdPartyTokenType ?? null,
5956
- chainId: !chainId ? Number(this.instanceManager.getChainID()) ?? null : chainId,
5957
- recovery: shieldAuthentication ?? null,
5958
- };
5959
- return new EmbeddedSigner(this.iframeManager, this.instanceManager, iframeConfiguration);
5960
- }
5961
- async loginWithEmailPassword({ email, password, }) {
5962
- this.instanceManager.removeAccessToken();
5963
- this.instanceManager.removeRefreshToken();
5964
- this.instanceManager.removePlayerID();
5999
+ /**
6000
+ * Logs in a user with email and password.
6001
+ *
6002
+ * @param email - User's email.
6003
+ * @param password - User's password.
6004
+ * @returns An AuthResponse object containing authentication details.
6005
+ */
6006
+ async logInWithEmailPassword({ email, password }) {
6007
+ this.logout();
5965
6008
  const result = await this.authManager.loginEmailPassword(email, password);
5966
6009
  this.storeCredentials({
5967
6010
  player: result.player.id,
@@ -5970,10 +6013,16 @@ class Openfort {
5970
6013
  });
5971
6014
  return result;
5972
6015
  }
5973
- async signUpWithEmailPassword({ email, password, options, }) {
5974
- this.instanceManager.removeAccessToken();
5975
- this.instanceManager.removeRefreshToken();
5976
- this.instanceManager.removePlayerID();
6016
+ /**
6017
+ * Signs up a new user with email and password.
6018
+ *
6019
+ * @param email - User's email.
6020
+ * @param password - User's password.
6021
+ * @param options - Additional options for the sign-up process.
6022
+ * @returns An AuthResponse object containing authentication details.
6023
+ */
6024
+ async signUpWithEmailPassword({ email, password, options }) {
6025
+ this.logout();
5977
6026
  const result = await this.authManager.signupEmailPassword(email, password, options?.data.name);
5978
6027
  this.storeCredentials({
5979
6028
  player: result.player.id,
@@ -5982,31 +6031,117 @@ class Openfort {
5982
6031
  });
5983
6032
  return result;
5984
6033
  }
6034
+ /**
6035
+ * Links an email and password to an existing account using an authentication token.
6036
+ *
6037
+ * @param email - User's email.
6038
+ * @param password - User's password.
6039
+ * @param authToken - Authentication token.
6040
+ * @returns An AuthPlayerResponse object.
6041
+ */
6042
+ async linkEmailPassword({ email, password, authToken }) {
6043
+ const result = await this.authManager.linkEmail(email, password, authToken);
6044
+ return result;
6045
+ }
6046
+ /**
6047
+ * Unlinks an email and password from an existing account using an authentication token.
6048
+ *
6049
+ * @param email - User's email.
6050
+ * @param authToken - Authentication token.
6051
+ * @returns An AuthPlayerResponse object.
6052
+ */
6053
+ async unlinkEmailPassword({ email, authToken }) {
6054
+ const result = await this.authManager.unlinkEmail(email, authToken);
6055
+ return result;
6056
+ }
6057
+ /**
6058
+ * Requests an email verification link.
6059
+ *
6060
+ * @param email - User's email.
6061
+ * @param redirectUrl - Redirect URL after verification.
6062
+ */
5985
6063
  async requestEmailVerification({ email, redirectUrl }) {
5986
6064
  await this.authManager.requestEmailVerification(email, redirectUrl);
5987
6065
  }
6066
+ /**
6067
+ * Resets the user's password.
6068
+ *
6069
+ * @param email - User's email.
6070
+ * @param password - New password.
6071
+ * @param state - Verification state.
6072
+ */
5988
6073
  async resetPassword({ email, password, state }) {
5989
6074
  await this.authManager.resetPassword(email, password, state);
5990
6075
  }
6076
+ /**
6077
+ * Requests a password reset link.
6078
+ *
6079
+ * @param email - User's email.
6080
+ * @param redirectUrl - Redirect URL after resetting password.
6081
+ */
5991
6082
  async requestResetPassword({ email, redirectUrl }) {
5992
6083
  await this.authManager.requestResetPassword(email, redirectUrl);
5993
6084
  }
6085
+ /**
6086
+ * Verifies the user's email.
6087
+ *
6088
+ * @param email - User's email.
6089
+ * @param state - Verification state.
6090
+ */
5994
6091
  async verifyEmail({ email, state }) {
5995
6092
  await this.authManager.verifyEmail(email, state);
5996
6093
  }
6094
+ /**
6095
+ * Initializes an OAuth authentication process.
6096
+ *
6097
+ * @param provider - OAuth provider.
6098
+ * @param options - Additional options for initialization.
6099
+ * @returns An InitAuthResponse object.
6100
+ */
5997
6101
  async initOAuth({ provider, options }) {
6102
+ this.logout();
5998
6103
  const authResponse = await this.authManager.initOAuth(provider, options);
5999
6104
  return authResponse;
6000
6105
  }
6001
- async initLinkOAuth({ provider, playerToken, options }) {
6002
- return await this.authManager.linkOAuth(provider, playerToken, options);
6106
+ /**
6107
+ * Initializes an OAuth linking process.
6108
+ *
6109
+ * @param provider - OAuth provider.
6110
+ * @param authToken - Authentication token.
6111
+ * @param options - Additional options for initialization.
6112
+ * @returns An InitAuthResponse object.
6113
+ */
6114
+ async initLinkOAuth({ provider, authToken, options }) {
6115
+ return await this.authManager.linkOAuth(provider, authToken, options);
6116
+ }
6117
+ /**
6118
+ * Unlinks an OAuth provider from the account.
6119
+ *
6120
+ * @param provider - OAuth provider.
6121
+ * @param authToken - Authentication token.
6122
+ * @returns An AuthPlayerResponse object.
6123
+ */
6124
+ async unlinkOAuth({ provider, authToken }) {
6125
+ const result = await this.authManager.unlinkOAuth(provider, authToken);
6126
+ return result;
6003
6127
  }
6128
+ /**
6129
+ * Polls for OAuth authentication completion.
6130
+ *
6131
+ * @param key - OAuth polling key.
6132
+ * @returns An AuthResponse object.
6133
+ */
6004
6134
  async poolOAuth(key) {
6005
6135
  return await this.authManager.poolOAuth(key);
6006
6136
  }
6007
- async initSIWE({ address }) {
6008
- return await this.authManager.initSIWE(address);
6009
- }
6137
+ /**
6138
+ * Authenticates using a third-party OAuth provider.
6139
+ *
6140
+ * @param provider - Third-party OAuth provider.
6141
+ * @param token - OAuth token.
6142
+ * @param tokenType - Type of the OAuth token.
6143
+ * @returns An AuthPlayerResponse object.
6144
+ */
6010
6145
  async authenticateWithThirdPartyProvider({ provider, token, tokenType }) {
6011
6146
  const result = await this.authManager.authenticateThirdParty(provider, token, tokenType);
6012
6147
  this.instanceManager.setAccessToken({
@@ -6020,6 +6155,24 @@ class Openfort {
6020
6155
  }
6021
6156
  return result;
6022
6157
  }
6158
+ /**
6159
+ * Initializes Sign-In with Ethereum (SIWE).
6160
+ *
6161
+ * @param address - Ethereum address.
6162
+ * @returns A SIWEInitResponse object.
6163
+ */
6164
+ async initSIWE({ address }) {
6165
+ return await this.authManager.initSIWE(address);
6166
+ }
6167
+ /**
6168
+ * Authenticates using Sign-In with Ethereum (SIWE).
6169
+ *
6170
+ * @param signature - SIWE signature.
6171
+ * @param message - SIWE message.
6172
+ * @param walletClientType - Wallet client type.
6173
+ * @param connectorType - Connector type.
6174
+ * @returns An AuthResponse object.
6175
+ */
6023
6176
  async authenticateWithSIWE({ signature, message, walletClientType, connectorType, }) {
6024
6177
  this.instanceManager.removeAccessToken();
6025
6178
  this.instanceManager.removeRefreshToken();
@@ -6032,6 +6185,36 @@ class Openfort {
6032
6185
  });
6033
6186
  return result;
6034
6187
  }
6188
+ /**
6189
+ * Links a wallet using SIWE.
6190
+ *
6191
+ * @param signature - SIWE signature.
6192
+ * @param message - SIWE message.
6193
+ * @param walletClientType - Wallet client type.
6194
+ * @param connectorType - Connector type.
6195
+ * @param authToken - Authentication token.
6196
+ * @returns An AuthPlayerResponse object.
6197
+ */
6198
+ async linkWallet({ signature, message, walletClientType, connectorType, authToken, }) {
6199
+ const result = await this.authManager.linkWallet(signature, message, walletClientType, connectorType, authToken);
6200
+ return result;
6201
+ }
6202
+ /**
6203
+ * Unlinks a wallet.
6204
+ *
6205
+ * @param address - Wallet address.
6206
+ * @param authToken - Authentication token.
6207
+ * @returns An AuthPlayerResponse object.
6208
+ */
6209
+ async unlinkWallet({ address, authToken }) {
6210
+ const result = await this.authManager.unlinkWallet(address, authToken);
6211
+ return result;
6212
+ }
6213
+ /**
6214
+ * Stores authentication credentials.
6215
+ *
6216
+ * @param auth - Authentication details.
6217
+ */
6035
6218
  storeCredentials(auth) {
6036
6219
  this.instanceManager.setAccessToken({
6037
6220
  token: auth.accessToken,
@@ -6039,9 +6222,20 @@ class Openfort {
6039
6222
  thirdPartyTokenType: null,
6040
6223
  });
6041
6224
  this.instanceManager.setRefreshToken(auth.refreshToken);
6042
- this.instanceManager.setPlayerID(auth.player);
6225
+ if (auth.player)
6226
+ this.instanceManager.setPlayerID(auth.player);
6043
6227
  }
6044
- async sendSignatureTransactionIntentRequest(transactionIntentId, userOperationHash = null, signature = null) {
6228
+ /**
6229
+ * Sends a signature transaction intent request.
6230
+ *
6231
+ * @param transactionIntentId - Transaction intent ID.
6232
+ * @param userOperationHash - User operation hash.
6233
+ * @param signature - Transaction signature.
6234
+ * @param optimistic - Whether the request is optimistic.
6235
+ * @returns A TransactionIntentResponse object.
6236
+ * @throws {OpenfortError} If no userOperationHash or signature is provided.
6237
+ */
6238
+ async sendSignatureTransactionIntentRequest(transactionIntentId, userOperationHash = null, signature = null, optimistic = false) {
6045
6239
  let newSignature = signature;
6046
6240
  if (!newSignature) {
6047
6241
  if (!userOperationHash) {
@@ -6060,11 +6254,20 @@ class Openfort {
6060
6254
  id: transactionIntentId,
6061
6255
  signatureRequest: {
6062
6256
  signature: newSignature,
6257
+ optimistic,
6063
6258
  },
6064
6259
  };
6065
6260
  const result = await this.backendApiClients.transactionIntentsApi.signature(request);
6066
6261
  return result.data;
6067
6262
  }
6263
+ /**
6264
+ * Signs a message.
6265
+ *
6266
+ * @param message - Message to sign.
6267
+ * @param options - Additional options for signing.
6268
+ * @returns The signature.
6269
+ * @throws {OpenfortError} If no signer is configured.
6270
+ */
6068
6271
  async signMessage(message, options) {
6069
6272
  await this.recoverSigner();
6070
6273
  if (!this.signer) {
@@ -6076,6 +6279,15 @@ class Openfort {
6076
6279
  const { hashMessage = true, arrayifyMessage = false } = options || {};
6077
6280
  return await this.signer.sign(message, arrayifyMessage, hashMessage);
6078
6281
  }
6282
+ /**
6283
+ * Signs typed data.
6284
+ *
6285
+ * @param domain - EIP-712 domain.
6286
+ * @param types - Typed data types.
6287
+ * @param value - Typed data value.
6288
+ * @returns The signature.
6289
+ * @throws {OpenfortError} If no signer is configured.
6290
+ */
6079
6291
  async signTypedData(domain, types, value) {
6080
6292
  await this.recoverSigner();
6081
6293
  if (!this.signer) {
@@ -6109,14 +6321,15 @@ class Openfort {
6109
6321
  }
6110
6322
  return await this.signer.sign(hash, false, false);
6111
6323
  }
6324
+ /**
6325
+ * Sends a session registration request.
6326
+ *
6327
+ * @param sessionId - Session ID.
6328
+ * @param signature - Session signature.
6329
+ * @param optimistic - Whether the request is optimistic.
6330
+ * @returns A SessionResponse object.
6331
+ */
6112
6332
  async sendRegisterSessionRequest(sessionId, signature, optimistic) {
6113
- await this.recoverSigner();
6114
- if (!this.signer) {
6115
- throw new OpenfortError('No signer configured nor signature provided', OpenfortErrorType.MISSING_SIGNER_ERROR);
6116
- }
6117
- if (this.signer.getSingerType() !== SignerType.SESSION) {
6118
- throw new OpenfortError('Session signer must be configured to sign a session', OpenfortErrorType.MISSING_SESSION_SIGNER_ERROR);
6119
- }
6120
6333
  const request = {
6121
6334
  id: sessionId,
6122
6335
  signatureRequest: {
@@ -6127,6 +6340,78 @@ class Openfort {
6127
6340
  const result = await this.backendApiClients.sessionsApi.signatureSession(request);
6128
6341
  return result.data;
6129
6342
  }
6343
+ /**
6344
+ * Gets the embedded state of the current session.
6345
+ *
6346
+ * @returns The embedded state.
6347
+ */
6348
+ getEmbeddedState() {
6349
+ if (!this.credentialsProvided()) {
6350
+ return EmbeddedState.UNAUTHENTICATED;
6351
+ }
6352
+ if (this.instanceManager.getSignerType() !== SignerType.EMBEDDED) {
6353
+ return EmbeddedState.EMBEDDED_SIGNER_NOT_CONFIGURED;
6354
+ }
6355
+ if (!this.signer) {
6356
+ this.signer = this.newEmbeddedSigner();
6357
+ }
6358
+ if (!this.instanceManager.getDeviceID()) {
6359
+ return EmbeddedState.CREATING_ACCOUNT;
6360
+ }
6361
+ return EmbeddedState.READY;
6362
+ }
6363
+ /**
6364
+ * Gets the current access token.
6365
+ *
6366
+ * @returns The access token, or null if not available.
6367
+ */
6368
+ getAccessToken() {
6369
+ return this.instanceManager.getAccessToken()?.token ?? null;
6370
+ }
6371
+ /**
6372
+ * Retrieves the user details.
6373
+ *
6374
+ * @returns An AuthPlayerResponse object.
6375
+ * @throws {OpenfortError} If no access token is found.
6376
+ */
6377
+ async getUser() {
6378
+ await this.validateAndRefreshToken();
6379
+ const accessToken = this.instanceManager.getAccessToken();
6380
+ if (!accessToken) {
6381
+ throw new OpenfortError('No accessToken found', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
6382
+ }
6383
+ return await this.authManager.getUser(accessToken.token);
6384
+ }
6385
+ /**
6386
+ * Validates and refreshes the access token if needed.
6387
+ */
6388
+ async validateAndRefreshToken() {
6389
+ const authType = this.credentialsProvided();
6390
+ if (!authType) {
6391
+ throw new OpenfortError('Must be logged in to validate and refresh token', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
6392
+ }
6393
+ if (authType === AuthType.OPENFORT) {
6394
+ const auth = await this.authManager.validateCredentials();
6395
+ this.storeCredentials(auth);
6396
+ if (this.signer && this.signer.useCredentials()) {
6397
+ await this.signer.updateAuthentication();
6398
+ }
6399
+ }
6400
+ }
6401
+ credentialsProvided() {
6402
+ const token = this.instanceManager.getAccessToken();
6403
+ const refreshToken = this.instanceManager.getRefreshToken();
6404
+ if (!token) {
6405
+ return null;
6406
+ }
6407
+ if (token.token && token.thirdPartyProvider && token.thirdPartyTokenType) {
6408
+ return AuthType.THIRD_PARTY;
6409
+ }
6410
+ if (token.token && refreshToken) {
6411
+ return AuthType.OPENFORT;
6412
+ }
6413
+ return null;
6414
+ }
6130
6415
  async recoverSigner() {
6131
6416
  if (this.signer) {
6132
6417
  return;
@@ -6160,66 +6445,62 @@ class Openfort {
6160
6445
  };
6161
6446
  await checkSignerType(0);
6162
6447
  }
6163
- getEmbeddedState() {
6448
+ newEmbeddedSigner(chainId) {
6164
6449
  if (!this.credentialsProvided()) {
6165
- return EmbeddedState.UNAUTHENTICATED;
6166
- }
6167
- if (this.instanceManager.getSignerType() !== SignerType.EMBEDDED) {
6168
- return EmbeddedState.EMBEDDED_SIGNER_NOT_CONFIGURED;
6169
- }
6170
- if (!this.signer) {
6171
- this.signer = this.newEmbeddedSigner();
6172
- }
6173
- if (!this.instanceManager.getDeviceID()) {
6174
- return EmbeddedState.CREATING_ACCOUNT;
6175
- }
6176
- return EmbeddedState.READY;
6177
- }
6178
- credentialsProvided() {
6179
- const token = this.instanceManager.getAccessToken();
6180
- const refreshToken = this.instanceManager.getRefreshToken();
6181
- return (token
6182
- && ((token.token && token.thirdPartyProvider && token.thirdPartyTokenType)
6183
- || (token.token && refreshToken)));
6184
- }
6185
- getAccessToken() {
6186
- return this.instanceManager.getAccessToken()?.token ?? null;
6187
- }
6188
- isLoaded() {
6189
- if (!this.instanceManager.getJWK()) {
6190
- return false;
6450
+ throw new OpenfortError('Must be logged in to configure embedded signer', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
6191
6451
  }
6192
- if (this.signer && this.signer.getSingerType() === SignerType.EMBEDDED) {
6193
- return this.signer.iFrameLoaded();
6452
+ let shieldAuthType = this.instanceManager.getShieldAuthType();
6453
+ if (!shieldAuthType) {
6454
+ // TODO: remove, this is for backward compatibility
6455
+ this.instanceManager.setShieldAuthType(ShieldAuthType.OPENFORT);
6456
+ shieldAuthType = ShieldAuthType.OPENFORT;
6457
+ // throw new OpenfortError('Shield auth type is not set', OpenfortErrorType.INVALID_CONFIGURATION);
6194
6458
  }
6195
- return true;
6196
- }
6197
- async getUser() {
6198
- await this.validateAndRefreshToken();
6199
- const accessToken = this.instanceManager.getAccessToken();
6200
- if (!accessToken) {
6201
- throw new OpenfortError('No accessToken found', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
6459
+ const token = shieldAuthType === ShieldAuthType.OPENFORT
6460
+ ? this.instanceManager.getAccessToken()?.token
6461
+ : this.instanceManager.getShieldAuthToken();
6462
+ if (!token) {
6463
+ throw new OpenfortError('Shield auth token is not set', OpenfortErrorType.INVALID_CONFIGURATION);
6202
6464
  }
6203
- return await this.authManager.getUser(accessToken.token);
6465
+ const shieldAuth = {
6466
+ auth: shieldAuthType,
6467
+ token,
6468
+ };
6469
+ const iframeConfiguration = {
6470
+ accessToken: this.instanceManager.getAccessToken()?.token ?? null,
6471
+ thirdPartyProvider: this.instanceManager.getAccessToken()?.thirdPartyProvider ?? null,
6472
+ thirdPartyTokenType: this.instanceManager.getAccessToken()?.thirdPartyTokenType ?? null,
6473
+ chainId: !chainId ? Number(this.instanceManager.getChainID()) ?? null : chainId,
6474
+ recovery: shieldAuth,
6475
+ };
6476
+ return new EmbeddedSigner(this.iframeManager, this.instanceManager, iframeConfiguration);
6204
6477
  }
6205
- async validateAndRefreshToken() {
6206
- if (!this.credentialsProvided()) {
6207
- return;
6208
- }
6209
- const accessToken = this.instanceManager.getAccessToken();
6210
- const refreshToken = this.instanceManager.getRefreshToken();
6211
- const jwk = await this.instanceManager.getJWK();
6212
- if (!accessToken || !refreshToken || !jwk) {
6478
+ async flushSigner() {
6479
+ if (this.signer) {
6480
+ await this.signer.logout();
6481
+ this.instanceManager.removeSignerType();
6213
6482
  return;
6214
6483
  }
6215
- const auth = await this.authManager.validateCredentials(accessToken.token, refreshToken, jwk);
6216
- if (auth.accessToken !== accessToken.token) {
6217
- this.storeCredentials(auth);
6218
- }
6219
- if (this.signer && this.signer.useCredentials()) {
6220
- await this.signer.updateAuthentication();
6484
+ const signerType = this.instanceManager.getSignerType();
6485
+ switch (signerType) {
6486
+ case SignerType.EMBEDDED: {
6487
+ const iframeConfiguration = {
6488
+ accessToken: this.instanceManager.getAccessToken()?.token ?? null,
6489
+ thirdPartyProvider: this.instanceManager.getAccessToken()?.thirdPartyProvider ?? null,
6490
+ thirdPartyTokenType: this.instanceManager.getAccessToken()?.thirdPartyTokenType ?? null,
6491
+ chainId: null,
6492
+ recovery: null,
6493
+ };
6494
+ const embeddedSigner = new EmbeddedSigner(this.iframeManager, this.instanceManager, iframeConfiguration);
6495
+ await embeddedSigner.logout();
6496
+ break;
6497
+ }
6498
+ case SignerType.SESSION:
6499
+ this.configureSessionKey();
6500
+ break;
6221
6501
  }
6502
+ this.instanceManager.removeSignerType();
6222
6503
  }
6223
6504
  }
6224
6505
 
6225
- export { AuthType, BasicAuthProvider, EmbeddedState, OAuthProvider, OpenfortConfiguration, SDKConfiguration, ShieldConfiguration, ThirdPartyOAuthProvider, TokenType, Openfort as default };
6506
+ export { AuthType, BasicAuthProvider, EmbeddedState, OAuthProvider, OpenfortConfiguration, OpenfortError, SDKConfiguration, ShieldAuthType, ShieldConfiguration, ThirdPartyOAuthProvider, TokenType, Openfort as default };