@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.cjs CHANGED
@@ -33,60 +33,6 @@ function _interopNamespaceDefault(e) {
33
33
 
34
34
  var crypto__namespace = /*#__PURE__*/_interopNamespaceDefault(crypto);
35
35
 
36
- exports.EmbeddedState = void 0;
37
- (function (EmbeddedState) {
38
- EmbeddedState[EmbeddedState["NONE"] = 0] = "NONE";
39
- EmbeddedState[EmbeddedState["UNAUTHENTICATED"] = 1] = "UNAUTHENTICATED";
40
- EmbeddedState[EmbeddedState["EMBEDDED_SIGNER_NOT_CONFIGURED"] = 2] = "EMBEDDED_SIGNER_NOT_CONFIGURED";
41
- EmbeddedState[EmbeddedState["CREATING_ACCOUNT"] = 3] = "CREATING_ACCOUNT";
42
- EmbeddedState[EmbeddedState["READY"] = 4] = "READY";
43
- })(exports.EmbeddedState || (exports.EmbeddedState = {}));
44
- var OpenfortEvents;
45
- (function (OpenfortEvents) {
46
- OpenfortEvents["LOGGED_OUT"] = "loggedOut";
47
- })(OpenfortEvents || (OpenfortEvents = {}));
48
- var AccountType;
49
- (function (AccountType) {
50
- AccountType["UPGRADEABLE_V4"] = "Upgradeable_v04";
51
- AccountType["MANAGED_V4"] = "Managed_v04";
52
- AccountType["ERC6551_V4"] = "ERC6551_v04";
53
- AccountType["ERC6551_V5"] = "ERC6551_v05";
54
- AccountType["RECOVERABLE_V4"] = "Recoverable_v04";
55
- AccountType["MANAGED_V5"] = "Managed_v05";
56
- AccountType["UPGRADEABLE_V5"] = "Upgradeable_v05";
57
- })(AccountType || (AccountType = {}));
58
- exports.TokenType = void 0;
59
- (function (TokenType) {
60
- TokenType["ID_TOKEN"] = "idToken";
61
- TokenType["CUSTOM_TOKEN"] = "customToken";
62
- })(exports.TokenType || (exports.TokenType = {}));
63
- exports.ThirdPartyOAuthProvider = void 0;
64
- (function (ThirdPartyOAuthProvider) {
65
- ThirdPartyOAuthProvider["ACCELBYTE"] = "accelbyte";
66
- ThirdPartyOAuthProvider["FIREBASE"] = "firebase";
67
- ThirdPartyOAuthProvider["LOOTLOCKER"] = "lootlocker";
68
- ThirdPartyOAuthProvider["PLAYFAB"] = "playfab";
69
- ThirdPartyOAuthProvider["SUPABASE"] = "supabase";
70
- ThirdPartyOAuthProvider["CUSTOM"] = "custom";
71
- ThirdPartyOAuthProvider["OIDC"] = "oidc";
72
- })(exports.ThirdPartyOAuthProvider || (exports.ThirdPartyOAuthProvider = {}));
73
- exports.BasicAuthProvider = void 0;
74
- (function (BasicAuthProvider) {
75
- BasicAuthProvider["EMAIL"] = "email";
76
- BasicAuthProvider["WALLET"] = "wallet";
77
- })(exports.BasicAuthProvider || (exports.BasicAuthProvider = {}));
78
- exports.OAuthProvider = void 0;
79
- (function (OAuthProvider) {
80
- OAuthProvider["GOOGLE"] = "google";
81
- OAuthProvider["TWITTER"] = "twitter";
82
- OAuthProvider["FACEBOOK"] = "facebook";
83
- })(exports.OAuthProvider || (exports.OAuthProvider = {}));
84
- var CodeChallengeMethodEnum;
85
- (function (CodeChallengeMethodEnum) {
86
- CodeChallengeMethodEnum["PLAIN"] = "plain";
87
- CodeChallengeMethodEnum["S256"] = "S256";
88
- })(CodeChallengeMethodEnum || (CodeChallengeMethodEnum = {}));
89
-
90
36
  /* tslint:disable */
91
37
  /* eslint-disable */
92
38
  /**
@@ -3090,6 +3036,65 @@ const createConfig = ({ basePath, accessToken, }) => {
3090
3036
  return new Configuration(apiConfigOptions);
3091
3037
  };
3092
3038
 
3039
+ exports.EmbeddedState = void 0;
3040
+ (function (EmbeddedState) {
3041
+ EmbeddedState[EmbeddedState["NONE"] = 0] = "NONE";
3042
+ EmbeddedState[EmbeddedState["UNAUTHENTICATED"] = 1] = "UNAUTHENTICATED";
3043
+ EmbeddedState[EmbeddedState["EMBEDDED_SIGNER_NOT_CONFIGURED"] = 2] = "EMBEDDED_SIGNER_NOT_CONFIGURED";
3044
+ EmbeddedState[EmbeddedState["CREATING_ACCOUNT"] = 3] = "CREATING_ACCOUNT";
3045
+ EmbeddedState[EmbeddedState["READY"] = 4] = "READY";
3046
+ })(exports.EmbeddedState || (exports.EmbeddedState = {}));
3047
+ var OpenfortEvents;
3048
+ (function (OpenfortEvents) {
3049
+ OpenfortEvents["LOGGED_OUT"] = "loggedOut";
3050
+ })(OpenfortEvents || (OpenfortEvents = {}));
3051
+ var AccountType;
3052
+ (function (AccountType) {
3053
+ AccountType["UPGRADEABLE_V4"] = "Upgradeable_v04";
3054
+ AccountType["MANAGED_V4"] = "Managed_v04";
3055
+ AccountType["ERC6551_V4"] = "ERC6551_v04";
3056
+ AccountType["ERC6551_V5"] = "ERC6551_v05";
3057
+ AccountType["RECOVERABLE_V4"] = "Recoverable_v04";
3058
+ AccountType["MANAGED_V5"] = "Managed_v05";
3059
+ AccountType["UPGRADEABLE_V5"] = "Upgradeable_v05";
3060
+ })(AccountType || (AccountType = {}));
3061
+ exports.AuthType = void 0;
3062
+ (function (AuthType) {
3063
+ AuthType["OPENFORT"] = "openfort";
3064
+ AuthType["THIRD_PARTY"] = "thirdParty";
3065
+ })(exports.AuthType || (exports.AuthType = {}));
3066
+ exports.TokenType = void 0;
3067
+ (function (TokenType) {
3068
+ TokenType["ID_TOKEN"] = "idToken";
3069
+ TokenType["CUSTOM_TOKEN"] = "customToken";
3070
+ })(exports.TokenType || (exports.TokenType = {}));
3071
+ exports.ThirdPartyOAuthProvider = void 0;
3072
+ (function (ThirdPartyOAuthProvider) {
3073
+ ThirdPartyOAuthProvider["ACCELBYTE"] = "accelbyte";
3074
+ ThirdPartyOAuthProvider["FIREBASE"] = "firebase";
3075
+ ThirdPartyOAuthProvider["LOOTLOCKER"] = "lootlocker";
3076
+ ThirdPartyOAuthProvider["PLAYFAB"] = "playfab";
3077
+ ThirdPartyOAuthProvider["SUPABASE"] = "supabase";
3078
+ ThirdPartyOAuthProvider["CUSTOM"] = "custom";
3079
+ ThirdPartyOAuthProvider["OIDC"] = "oidc";
3080
+ })(exports.ThirdPartyOAuthProvider || (exports.ThirdPartyOAuthProvider = {}));
3081
+ exports.BasicAuthProvider = void 0;
3082
+ (function (BasicAuthProvider) {
3083
+ BasicAuthProvider["EMAIL"] = "email";
3084
+ BasicAuthProvider["WALLET"] = "wallet";
3085
+ })(exports.BasicAuthProvider || (exports.BasicAuthProvider = {}));
3086
+ exports.OAuthProvider = void 0;
3087
+ (function (OAuthProvider) {
3088
+ OAuthProvider["GOOGLE"] = "google";
3089
+ OAuthProvider["TWITTER"] = "twitter";
3090
+ OAuthProvider["FACEBOOK"] = "facebook";
3091
+ })(exports.OAuthProvider || (exports.OAuthProvider = {}));
3092
+ var CodeChallengeMethodEnum;
3093
+ (function (CodeChallengeMethodEnum) {
3094
+ CodeChallengeMethodEnum["PLAIN"] = "plain";
3095
+ CodeChallengeMethodEnum["S256"] = "S256";
3096
+ })(CodeChallengeMethodEnum || (CodeChallengeMethodEnum = {}));
3097
+
3093
3098
  var OpenfortErrorType;
3094
3099
  (function (OpenfortErrorType) {
3095
3100
  OpenfortErrorType["AUTHENTICATION_ERROR"] = "AUTHENTICATION_ERROR";
@@ -4624,8 +4629,8 @@ class EvmProvider {
4624
4629
  const openfortProviderInfo = {
4625
4630
  // eslint-disable-next-line max-len
4626
4631
  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>',
4627
- name: 'Ecosystem ID',
4628
- rdns: 'com.ecosystem.id',
4632
+ name: 'Openfort',
4633
+ rdns: 'xyz.openfort',
4629
4634
  uuid: uuid.v4(),
4630
4635
  };
4631
4636
  function announceProvider(detail) {
@@ -4683,11 +4688,14 @@ class AuthManager {
4683
4688
  this.instanceManager = instanceManager;
4684
4689
  }
4685
4690
  async initOAuth(provider, options) {
4691
+ const usePooling = options?.usePooling ?? false;
4692
+ // eslint-disable-next-line no-param-reassign
4693
+ delete options?.usePooling;
4686
4694
  const request = {
4687
4695
  oAuthInitRequest: {
4688
4696
  provider,
4689
4697
  options,
4690
- usePooling: options?.usePooling || false,
4698
+ usePooling,
4691
4699
  },
4692
4700
  };
4693
4701
  const result = await this.backendApiClients.authenticationApi.initOAuth(request);
@@ -4869,7 +4877,13 @@ class AuthManager {
4869
4877
  return response.data;
4870
4878
  }, OpenfortErrorType.USER_REGISTRATION_ERROR);
4871
4879
  }
4872
- async validateCredentials(accessToken, refreshToken, jwk) {
4880
+ async validateCredentials() {
4881
+ const jwk = await this.instanceManager.getJWK();
4882
+ const accessToken = this.instanceManager.getAccessToken()?.token;
4883
+ const refreshToken = this.instanceManager.getRefreshToken();
4884
+ if (!accessToken || !refreshToken || !jwk) {
4885
+ throw new OpenfortError('Must be logged in to validate and refresh token', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
4886
+ }
4873
4887
  try {
4874
4888
  const key = (await jose.importJWK({
4875
4889
  kty: jwk.kty,
@@ -5045,6 +5059,8 @@ const chainIDStorageKey = 'openfort.chain_id';
5045
5059
  const jwksStorageKey = 'openfort.jwk';
5046
5060
  const deviceIDStorageKey = 'openfort.device_id';
5047
5061
  const accountTypeStorageKey = 'openfort.account_type';
5062
+ const shieldAuthTypeStorageKey = 'openfort.shield_auth_type';
5063
+ const shieldAuthTokenStorageKey = 'openfort.shield_auth_token';
5048
5064
  const accountAddressStorageKey = 'openfort.account_address';
5049
5065
 
5050
5066
  class InstanceManager {
@@ -5054,6 +5070,7 @@ class InstanceManager {
5054
5070
  jwk = null;
5055
5071
  sessionKey = null;
5056
5072
  deviceID = null;
5073
+ shieldAuthType = null;
5057
5074
  chainId = null;
5058
5075
  accountAddress = null;
5059
5076
  accountType = null;
@@ -5185,9 +5202,10 @@ class InstanceManager {
5185
5202
  };
5186
5203
  const response = await this.backendApiClients.authenticationApi.getJwks(request);
5187
5204
  if (response.data.keys.length === 0) {
5188
- throw new Error('No keys found');
5205
+ throw new Error('Internal error: No JWKS keys found');
5189
5206
  }
5190
5207
  const jwtKey = response.data.keys[0];
5208
+ this.setJWK(jwtKey);
5191
5209
  this.jwk = {
5192
5210
  kty: jwtKey.kty,
5193
5211
  crv: jwtKey.crv,
@@ -5272,6 +5290,34 @@ class InstanceManager {
5272
5290
  this.accountType = null;
5273
5291
  this.persistentStorage.remove(accountTypeStorageKey);
5274
5292
  }
5293
+ setShieldAuthType(accountType) {
5294
+ this.accountType = accountType;
5295
+ this.persistentStorage.save(shieldAuthTypeStorageKey, accountType);
5296
+ }
5297
+ getShieldAuthType() {
5298
+ if (!this.accountType) {
5299
+ this.accountType = this.persistentStorage.get(shieldAuthTypeStorageKey);
5300
+ }
5301
+ return this.accountType;
5302
+ }
5303
+ removeShieldAuthType() {
5304
+ this.accountType = null;
5305
+ this.persistentStorage.remove(shieldAuthTypeStorageKey);
5306
+ }
5307
+ setShieldAuthToken(accountType) {
5308
+ this.accountType = accountType;
5309
+ this.persistentStorage.save(shieldAuthTokenStorageKey, accountType);
5310
+ }
5311
+ getShieldAuthToken() {
5312
+ if (!this.accountType) {
5313
+ this.accountType = this.persistentStorage.get(shieldAuthTokenStorageKey);
5314
+ }
5315
+ return this.accountType;
5316
+ }
5317
+ removeShieldAuthToken() {
5318
+ this.accountType = null;
5319
+ this.persistentStorage.remove(shieldAuthTokenStorageKey);
5320
+ }
5275
5321
  }
5276
5322
 
5277
5323
  class LocalStorage {
@@ -5617,11 +5663,11 @@ class UpdateAuthenticationRequest {
5617
5663
  this.recovery = recovery;
5618
5664
  }
5619
5665
  }
5620
- exports.AuthType = void 0;
5621
- (function (AuthType) {
5622
- AuthType["OPENFORT"] = "openfort";
5623
- AuthType["CUSTOM"] = "custom";
5624
- })(exports.AuthType || (exports.AuthType = {}));
5666
+ exports.ShieldAuthType = void 0;
5667
+ (function (ShieldAuthType) {
5668
+ ShieldAuthType["OPENFORT"] = "openfort";
5669
+ ShieldAuthType["CUSTOM"] = "custom";
5670
+ })(exports.ShieldAuthType || (exports.ShieldAuthType = {}));
5625
5671
 
5626
5672
  class MissingRecoveryPasswordError extends Error {
5627
5673
  constructor() {
@@ -5872,6 +5918,9 @@ class Openfort {
5872
5918
  this.openfortEventEmitter = new TypedEventEmitter();
5873
5919
  this.iframeManager = new IframeManager(this.config);
5874
5920
  }
5921
+ /**
5922
+ * Logs the user out by flushing the signer and removing credentials.
5923
+ */
5875
5924
  async logout() {
5876
5925
  await this.flushSigner();
5877
5926
  if (this.credentialsProvided()) {
@@ -5899,11 +5948,18 @@ class Openfort {
5899
5948
  this.instanceManager.removeChainID();
5900
5949
  this.instanceManager.removeDeviceID();
5901
5950
  this.instanceManager.removeJWK();
5951
+ this.instanceManager.removeShieldAuthType();
5952
+ this.instanceManager.removeShieldAuthToken();
5902
5953
  }
5903
5954
  }
5904
- getEthereumProvider(options = {
5905
- announceProvider: true,
5906
- }) {
5955
+ /**
5956
+ * Returns an Ethereum provider using the configured signer.
5957
+ *
5958
+ * @param options - Configuration options for the Ethereum provider.
5959
+ * @returns A Provider instance.
5960
+ * @throws {OpenfortError} If the signer is not an EmbeddedSigner.
5961
+ */
5962
+ getEthereumProvider(options = { announceProvider: true }) {
5907
5963
  if (!(this.signer instanceof EmbeddedSigner)) {
5908
5964
  throw new OpenfortError('Embedded signer must be configured to get Ethereum provider', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
5909
5965
  }
@@ -5924,33 +5980,15 @@ class Openfort {
5924
5980
  }
5925
5981
  return provider;
5926
5982
  }
5927
- async flushSigner() {
5928
- if (this.signer) {
5929
- await this.signer.logout();
5930
- this.instanceManager.removeSignerType();
5931
- return;
5932
- }
5933
- const signerType = this.instanceManager.getSignerType();
5934
- switch (signerType) {
5935
- case SignerType.EMBEDDED: {
5936
- const iframeConfiguration = {
5937
- accessToken: this.instanceManager.getAccessToken()?.token ?? null,
5938
- thirdPartyProvider: this.instanceManager.getAccessToken()?.thirdPartyProvider ?? null,
5939
- thirdPartyTokenType: this.instanceManager.getAccessToken()?.thirdPartyTokenType ?? null,
5940
- chainId: null,
5941
- recovery: null,
5942
- };
5943
- const embeddedSigner = new EmbeddedSigner(this.iframeManager, this.instanceManager, iframeConfiguration);
5944
- await embeddedSigner.logout();
5945
- break;
5946
- }
5947
- case SignerType.SESSION:
5948
- this.configureSessionKey();
5949
- break;
5950
- }
5951
- this.instanceManager.removeSignerType();
5952
- }
5983
+ /**
5984
+ * Configures a session key and returns the session key details.
5985
+ *
5986
+ * @returns A SessionKey object containing the address and registration status.
5987
+ */
5953
5988
  configureSessionKey() {
5989
+ if (this.instanceManager.getSignerType() === SignerType.EMBEDDED) {
5990
+ throw new OpenfortError('Session signer must be configured to sign a session', OpenfortErrorType.MISSING_SESSION_SIGNER_ERROR);
5991
+ }
5954
5992
  const signer = new SessionSigner(this.instanceManager);
5955
5993
  this.signer = signer;
5956
5994
  const publicKey = signer.loadKeys();
@@ -5961,30 +5999,35 @@ class Openfort {
5961
5999
  this.instanceManager.setSignerType(SignerType.SESSION);
5962
6000
  return { address: publicKey, isRegistered: true };
5963
6001
  }
6002
+ /**
6003
+ * Configures an embedded signer.
6004
+ *
6005
+ * @param chainId - The chain ID for the embedded signer.
6006
+ * @param shieldAuthentication - Shield authentication details.
6007
+ * @param recoveryPassword - Recovery password.
6008
+ */
5964
6009
  async configureEmbeddedSigner(chainId, shieldAuthentication, recoveryPassword) {
5965
- const signer = this.newEmbeddedSigner(chainId, shieldAuthentication);
5966
6010
  await this.validateAndRefreshToken();
6011
+ if (shieldAuthentication) {
6012
+ this.instanceManager.setShieldAuthType(shieldAuthentication?.auth);
6013
+ if (shieldAuthentication?.auth === exports.ShieldAuthType.CUSTOM) {
6014
+ this.instanceManager.setShieldAuthToken(shieldAuthentication?.token);
6015
+ }
6016
+ }
6017
+ const signer = this.newEmbeddedSigner(chainId);
5967
6018
  await signer.ensureEmbeddedAccount(recoveryPassword);
5968
6019
  this.signer = signer;
5969
6020
  this.instanceManager.setSignerType(SignerType.EMBEDDED);
5970
6021
  }
5971
- newEmbeddedSigner(chainId, shieldAuthentication) {
5972
- if (!this.credentialsProvided()) {
5973
- throw new OpenfortError('Must be logged in to configure embedded signer', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
5974
- }
5975
- const iframeConfiguration = {
5976
- accessToken: this.instanceManager.getAccessToken()?.token ?? null,
5977
- thirdPartyProvider: this.instanceManager.getAccessToken()?.thirdPartyProvider ?? null,
5978
- thirdPartyTokenType: this.instanceManager.getAccessToken()?.thirdPartyTokenType ?? null,
5979
- chainId: !chainId ? Number(this.instanceManager.getChainID()) ?? null : chainId,
5980
- recovery: shieldAuthentication ?? null,
5981
- };
5982
- return new EmbeddedSigner(this.iframeManager, this.instanceManager, iframeConfiguration);
5983
- }
5984
- async loginWithEmailPassword({ email, password, }) {
5985
- this.instanceManager.removeAccessToken();
5986
- this.instanceManager.removeRefreshToken();
5987
- this.instanceManager.removePlayerID();
6022
+ /**
6023
+ * Logs in a user with email and password.
6024
+ *
6025
+ * @param email - User's email.
6026
+ * @param password - User's password.
6027
+ * @returns An AuthResponse object containing authentication details.
6028
+ */
6029
+ async logInWithEmailPassword({ email, password }) {
6030
+ this.logout();
5988
6031
  const result = await this.authManager.loginEmailPassword(email, password);
5989
6032
  this.storeCredentials({
5990
6033
  player: result.player.id,
@@ -5993,10 +6036,16 @@ class Openfort {
5993
6036
  });
5994
6037
  return result;
5995
6038
  }
5996
- async signUpWithEmailPassword({ email, password, options, }) {
5997
- this.instanceManager.removeAccessToken();
5998
- this.instanceManager.removeRefreshToken();
5999
- this.instanceManager.removePlayerID();
6039
+ /**
6040
+ * Signs up a new user with email and password.
6041
+ *
6042
+ * @param email - User's email.
6043
+ * @param password - User's password.
6044
+ * @param options - Additional options for the sign-up process.
6045
+ * @returns An AuthResponse object containing authentication details.
6046
+ */
6047
+ async signUpWithEmailPassword({ email, password, options }) {
6048
+ this.logout();
6000
6049
  const result = await this.authManager.signupEmailPassword(email, password, options?.data.name);
6001
6050
  this.storeCredentials({
6002
6051
  player: result.player.id,
@@ -6005,31 +6054,117 @@ class Openfort {
6005
6054
  });
6006
6055
  return result;
6007
6056
  }
6057
+ /**
6058
+ * Links an email and password to an existing account using an authentication token.
6059
+ *
6060
+ * @param email - User's email.
6061
+ * @param password - User's password.
6062
+ * @param authToken - Authentication token.
6063
+ * @returns An AuthPlayerResponse object.
6064
+ */
6065
+ async linkEmailPassword({ email, password, authToken }) {
6066
+ const result = await this.authManager.linkEmail(email, password, authToken);
6067
+ return result;
6068
+ }
6069
+ /**
6070
+ * Unlinks an email and password from an existing account using an authentication token.
6071
+ *
6072
+ * @param email - User's email.
6073
+ * @param authToken - Authentication token.
6074
+ * @returns An AuthPlayerResponse object.
6075
+ */
6076
+ async unlinkEmailPassword({ email, authToken }) {
6077
+ const result = await this.authManager.unlinkEmail(email, authToken);
6078
+ return result;
6079
+ }
6080
+ /**
6081
+ * Requests an email verification link.
6082
+ *
6083
+ * @param email - User's email.
6084
+ * @param redirectUrl - Redirect URL after verification.
6085
+ */
6008
6086
  async requestEmailVerification({ email, redirectUrl }) {
6009
6087
  await this.authManager.requestEmailVerification(email, redirectUrl);
6010
6088
  }
6089
+ /**
6090
+ * Resets the user's password.
6091
+ *
6092
+ * @param email - User's email.
6093
+ * @param password - New password.
6094
+ * @param state - Verification state.
6095
+ */
6011
6096
  async resetPassword({ email, password, state }) {
6012
6097
  await this.authManager.resetPassword(email, password, state);
6013
6098
  }
6099
+ /**
6100
+ * Requests a password reset link.
6101
+ *
6102
+ * @param email - User's email.
6103
+ * @param redirectUrl - Redirect URL after resetting password.
6104
+ */
6014
6105
  async requestResetPassword({ email, redirectUrl }) {
6015
6106
  await this.authManager.requestResetPassword(email, redirectUrl);
6016
6107
  }
6108
+ /**
6109
+ * Verifies the user's email.
6110
+ *
6111
+ * @param email - User's email.
6112
+ * @param state - Verification state.
6113
+ */
6017
6114
  async verifyEmail({ email, state }) {
6018
6115
  await this.authManager.verifyEmail(email, state);
6019
6116
  }
6117
+ /**
6118
+ * Initializes an OAuth authentication process.
6119
+ *
6120
+ * @param provider - OAuth provider.
6121
+ * @param options - Additional options for initialization.
6122
+ * @returns An InitAuthResponse object.
6123
+ */
6020
6124
  async initOAuth({ provider, options }) {
6125
+ this.logout();
6021
6126
  const authResponse = await this.authManager.initOAuth(provider, options);
6022
6127
  return authResponse;
6023
6128
  }
6024
- async initLinkOAuth({ provider, playerToken, options }) {
6025
- return await this.authManager.linkOAuth(provider, playerToken, options);
6129
+ /**
6130
+ * Initializes an OAuth linking process.
6131
+ *
6132
+ * @param provider - OAuth provider.
6133
+ * @param authToken - Authentication token.
6134
+ * @param options - Additional options for initialization.
6135
+ * @returns An InitAuthResponse object.
6136
+ */
6137
+ async initLinkOAuth({ provider, authToken, options }) {
6138
+ return await this.authManager.linkOAuth(provider, authToken, options);
6139
+ }
6140
+ /**
6141
+ * Unlinks an OAuth provider from the account.
6142
+ *
6143
+ * @param provider - OAuth provider.
6144
+ * @param authToken - Authentication token.
6145
+ * @returns An AuthPlayerResponse object.
6146
+ */
6147
+ async unlinkOAuth({ provider, authToken }) {
6148
+ const result = await this.authManager.unlinkOAuth(provider, authToken);
6149
+ return result;
6026
6150
  }
6151
+ /**
6152
+ * Polls for OAuth authentication completion.
6153
+ *
6154
+ * @param key - OAuth polling key.
6155
+ * @returns An AuthResponse object.
6156
+ */
6027
6157
  async poolOAuth(key) {
6028
6158
  return await this.authManager.poolOAuth(key);
6029
6159
  }
6030
- async initSIWE({ address }) {
6031
- return await this.authManager.initSIWE(address);
6032
- }
6160
+ /**
6161
+ * Authenticates using a third-party OAuth provider.
6162
+ *
6163
+ * @param provider - Third-party OAuth provider.
6164
+ * @param token - OAuth token.
6165
+ * @param tokenType - Type of the OAuth token.
6166
+ * @returns An AuthPlayerResponse object.
6167
+ */
6033
6168
  async authenticateWithThirdPartyProvider({ provider, token, tokenType }) {
6034
6169
  const result = await this.authManager.authenticateThirdParty(provider, token, tokenType);
6035
6170
  this.instanceManager.setAccessToken({
@@ -6043,6 +6178,24 @@ class Openfort {
6043
6178
  }
6044
6179
  return result;
6045
6180
  }
6181
+ /**
6182
+ * Initializes Sign-In with Ethereum (SIWE).
6183
+ *
6184
+ * @param address - Ethereum address.
6185
+ * @returns A SIWEInitResponse object.
6186
+ */
6187
+ async initSIWE({ address }) {
6188
+ return await this.authManager.initSIWE(address);
6189
+ }
6190
+ /**
6191
+ * Authenticates using Sign-In with Ethereum (SIWE).
6192
+ *
6193
+ * @param signature - SIWE signature.
6194
+ * @param message - SIWE message.
6195
+ * @param walletClientType - Wallet client type.
6196
+ * @param connectorType - Connector type.
6197
+ * @returns An AuthResponse object.
6198
+ */
6046
6199
  async authenticateWithSIWE({ signature, message, walletClientType, connectorType, }) {
6047
6200
  this.instanceManager.removeAccessToken();
6048
6201
  this.instanceManager.removeRefreshToken();
@@ -6055,6 +6208,36 @@ class Openfort {
6055
6208
  });
6056
6209
  return result;
6057
6210
  }
6211
+ /**
6212
+ * Links a wallet using SIWE.
6213
+ *
6214
+ * @param signature - SIWE signature.
6215
+ * @param message - SIWE message.
6216
+ * @param walletClientType - Wallet client type.
6217
+ * @param connectorType - Connector type.
6218
+ * @param authToken - Authentication token.
6219
+ * @returns An AuthPlayerResponse object.
6220
+ */
6221
+ async linkWallet({ signature, message, walletClientType, connectorType, authToken, }) {
6222
+ const result = await this.authManager.linkWallet(signature, message, walletClientType, connectorType, authToken);
6223
+ return result;
6224
+ }
6225
+ /**
6226
+ * Unlinks a wallet.
6227
+ *
6228
+ * @param address - Wallet address.
6229
+ * @param authToken - Authentication token.
6230
+ * @returns An AuthPlayerResponse object.
6231
+ */
6232
+ async unlinkWallet({ address, authToken }) {
6233
+ const result = await this.authManager.unlinkWallet(address, authToken);
6234
+ return result;
6235
+ }
6236
+ /**
6237
+ * Stores authentication credentials.
6238
+ *
6239
+ * @param auth - Authentication details.
6240
+ */
6058
6241
  storeCredentials(auth) {
6059
6242
  this.instanceManager.setAccessToken({
6060
6243
  token: auth.accessToken,
@@ -6062,9 +6245,20 @@ class Openfort {
6062
6245
  thirdPartyTokenType: null,
6063
6246
  });
6064
6247
  this.instanceManager.setRefreshToken(auth.refreshToken);
6065
- this.instanceManager.setPlayerID(auth.player);
6248
+ if (auth.player)
6249
+ this.instanceManager.setPlayerID(auth.player);
6066
6250
  }
6067
- async sendSignatureTransactionIntentRequest(transactionIntentId, userOperationHash = null, signature = null) {
6251
+ /**
6252
+ * Sends a signature transaction intent request.
6253
+ *
6254
+ * @param transactionIntentId - Transaction intent ID.
6255
+ * @param userOperationHash - User operation hash.
6256
+ * @param signature - Transaction signature.
6257
+ * @param optimistic - Whether the request is optimistic.
6258
+ * @returns A TransactionIntentResponse object.
6259
+ * @throws {OpenfortError} If no userOperationHash or signature is provided.
6260
+ */
6261
+ async sendSignatureTransactionIntentRequest(transactionIntentId, userOperationHash = null, signature = null, optimistic = false) {
6068
6262
  let newSignature = signature;
6069
6263
  if (!newSignature) {
6070
6264
  if (!userOperationHash) {
@@ -6083,11 +6277,20 @@ class Openfort {
6083
6277
  id: transactionIntentId,
6084
6278
  signatureRequest: {
6085
6279
  signature: newSignature,
6280
+ optimistic,
6086
6281
  },
6087
6282
  };
6088
6283
  const result = await this.backendApiClients.transactionIntentsApi.signature(request);
6089
6284
  return result.data;
6090
6285
  }
6286
+ /**
6287
+ * Signs a message.
6288
+ *
6289
+ * @param message - Message to sign.
6290
+ * @param options - Additional options for signing.
6291
+ * @returns The signature.
6292
+ * @throws {OpenfortError} If no signer is configured.
6293
+ */
6091
6294
  async signMessage(message, options) {
6092
6295
  await this.recoverSigner();
6093
6296
  if (!this.signer) {
@@ -6099,6 +6302,15 @@ class Openfort {
6099
6302
  const { hashMessage = true, arrayifyMessage = false } = options || {};
6100
6303
  return await this.signer.sign(message, arrayifyMessage, hashMessage);
6101
6304
  }
6305
+ /**
6306
+ * Signs typed data.
6307
+ *
6308
+ * @param domain - EIP-712 domain.
6309
+ * @param types - Typed data types.
6310
+ * @param value - Typed data value.
6311
+ * @returns The signature.
6312
+ * @throws {OpenfortError} If no signer is configured.
6313
+ */
6102
6314
  async signTypedData(domain, types, value) {
6103
6315
  await this.recoverSigner();
6104
6316
  if (!this.signer) {
@@ -6132,14 +6344,15 @@ class Openfort {
6132
6344
  }
6133
6345
  return await this.signer.sign(hash$1, false, false);
6134
6346
  }
6347
+ /**
6348
+ * Sends a session registration request.
6349
+ *
6350
+ * @param sessionId - Session ID.
6351
+ * @param signature - Session signature.
6352
+ * @param optimistic - Whether the request is optimistic.
6353
+ * @returns A SessionResponse object.
6354
+ */
6135
6355
  async sendRegisterSessionRequest(sessionId, signature, optimistic) {
6136
- await this.recoverSigner();
6137
- if (!this.signer) {
6138
- throw new OpenfortError('No signer configured nor signature provided', OpenfortErrorType.MISSING_SIGNER_ERROR);
6139
- }
6140
- if (this.signer.getSingerType() !== SignerType.SESSION) {
6141
- throw new OpenfortError('Session signer must be configured to sign a session', OpenfortErrorType.MISSING_SESSION_SIGNER_ERROR);
6142
- }
6143
6356
  const request = {
6144
6357
  id: sessionId,
6145
6358
  signatureRequest: {
@@ -6150,6 +6363,78 @@ class Openfort {
6150
6363
  const result = await this.backendApiClients.sessionsApi.signatureSession(request);
6151
6364
  return result.data;
6152
6365
  }
6366
+ /**
6367
+ * Gets the embedded state of the current session.
6368
+ *
6369
+ * @returns The embedded state.
6370
+ */
6371
+ getEmbeddedState() {
6372
+ if (!this.credentialsProvided()) {
6373
+ return exports.EmbeddedState.UNAUTHENTICATED;
6374
+ }
6375
+ if (this.instanceManager.getSignerType() !== SignerType.EMBEDDED) {
6376
+ return exports.EmbeddedState.EMBEDDED_SIGNER_NOT_CONFIGURED;
6377
+ }
6378
+ if (!this.signer) {
6379
+ this.signer = this.newEmbeddedSigner();
6380
+ }
6381
+ if (!this.instanceManager.getDeviceID()) {
6382
+ return exports.EmbeddedState.CREATING_ACCOUNT;
6383
+ }
6384
+ return exports.EmbeddedState.READY;
6385
+ }
6386
+ /**
6387
+ * Gets the current access token.
6388
+ *
6389
+ * @returns The access token, or null if not available.
6390
+ */
6391
+ getAccessToken() {
6392
+ return this.instanceManager.getAccessToken()?.token ?? null;
6393
+ }
6394
+ /**
6395
+ * Retrieves the user details.
6396
+ *
6397
+ * @returns An AuthPlayerResponse object.
6398
+ * @throws {OpenfortError} If no access token is found.
6399
+ */
6400
+ async getUser() {
6401
+ await this.validateAndRefreshToken();
6402
+ const accessToken = this.instanceManager.getAccessToken();
6403
+ if (!accessToken) {
6404
+ throw new OpenfortError('No accessToken found', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
6405
+ }
6406
+ return await this.authManager.getUser(accessToken.token);
6407
+ }
6408
+ /**
6409
+ * Validates and refreshes the access token if needed.
6410
+ */
6411
+ async validateAndRefreshToken() {
6412
+ const authType = this.credentialsProvided();
6413
+ if (!authType) {
6414
+ throw new OpenfortError('Must be logged in to validate and refresh token', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
6415
+ }
6416
+ if (authType === exports.AuthType.OPENFORT) {
6417
+ const auth = await this.authManager.validateCredentials();
6418
+ this.storeCredentials(auth);
6419
+ if (this.signer && this.signer.useCredentials()) {
6420
+ await this.signer.updateAuthentication();
6421
+ }
6422
+ }
6423
+ }
6424
+ credentialsProvided() {
6425
+ const token = this.instanceManager.getAccessToken();
6426
+ const refreshToken = this.instanceManager.getRefreshToken();
6427
+ if (!token) {
6428
+ return null;
6429
+ }
6430
+ if (token.token && token.thirdPartyProvider && token.thirdPartyTokenType) {
6431
+ return exports.AuthType.THIRD_PARTY;
6432
+ }
6433
+ if (token.token && refreshToken) {
6434
+ return exports.AuthType.OPENFORT;
6435
+ }
6436
+ return null;
6437
+ }
6153
6438
  async recoverSigner() {
6154
6439
  if (this.signer) {
6155
6440
  return;
@@ -6183,69 +6468,66 @@ class Openfort {
6183
6468
  };
6184
6469
  await checkSignerType(0);
6185
6470
  }
6186
- getEmbeddedState() {
6471
+ newEmbeddedSigner(chainId) {
6187
6472
  if (!this.credentialsProvided()) {
6188
- return exports.EmbeddedState.UNAUTHENTICATED;
6189
- }
6190
- if (this.instanceManager.getSignerType() !== SignerType.EMBEDDED) {
6191
- return exports.EmbeddedState.EMBEDDED_SIGNER_NOT_CONFIGURED;
6192
- }
6193
- if (!this.signer) {
6194
- this.signer = this.newEmbeddedSigner();
6195
- }
6196
- if (!this.instanceManager.getDeviceID()) {
6197
- return exports.EmbeddedState.CREATING_ACCOUNT;
6198
- }
6199
- return exports.EmbeddedState.READY;
6200
- }
6201
- credentialsProvided() {
6202
- const token = this.instanceManager.getAccessToken();
6203
- const refreshToken = this.instanceManager.getRefreshToken();
6204
- return (token
6205
- && ((token.token && token.thirdPartyProvider && token.thirdPartyTokenType)
6206
- || (token.token && refreshToken)));
6207
- }
6208
- getAccessToken() {
6209
- return this.instanceManager.getAccessToken()?.token ?? null;
6210
- }
6211
- isLoaded() {
6212
- if (!this.instanceManager.getJWK()) {
6213
- return false;
6473
+ throw new OpenfortError('Must be logged in to configure embedded signer', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
6214
6474
  }
6215
- if (this.signer && this.signer.getSingerType() === SignerType.EMBEDDED) {
6216
- return this.signer.iFrameLoaded();
6475
+ let shieldAuthType = this.instanceManager.getShieldAuthType();
6476
+ if (!shieldAuthType) {
6477
+ // TODO: remove, this is for backward compatibility
6478
+ this.instanceManager.setShieldAuthType(exports.ShieldAuthType.OPENFORT);
6479
+ shieldAuthType = exports.ShieldAuthType.OPENFORT;
6480
+ // throw new OpenfortError('Shield auth type is not set', OpenfortErrorType.INVALID_CONFIGURATION);
6217
6481
  }
6218
- return true;
6219
- }
6220
- async getUser() {
6221
- await this.validateAndRefreshToken();
6222
- const accessToken = this.instanceManager.getAccessToken();
6223
- if (!accessToken) {
6224
- throw new OpenfortError('No accessToken found', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
6482
+ const token = shieldAuthType === exports.ShieldAuthType.OPENFORT
6483
+ ? this.instanceManager.getAccessToken()?.token
6484
+ : this.instanceManager.getShieldAuthToken();
6485
+ if (!token) {
6486
+ throw new OpenfortError('Shield auth token is not set', OpenfortErrorType.INVALID_CONFIGURATION);
6225
6487
  }
6226
- return await this.authManager.getUser(accessToken.token);
6488
+ const shieldAuth = {
6489
+ auth: shieldAuthType,
6490
+ token,
6491
+ };
6492
+ const iframeConfiguration = {
6493
+ accessToken: this.instanceManager.getAccessToken()?.token ?? null,
6494
+ thirdPartyProvider: this.instanceManager.getAccessToken()?.thirdPartyProvider ?? null,
6495
+ thirdPartyTokenType: this.instanceManager.getAccessToken()?.thirdPartyTokenType ?? null,
6496
+ chainId: !chainId ? Number(this.instanceManager.getChainID()) ?? null : chainId,
6497
+ recovery: shieldAuth,
6498
+ };
6499
+ return new EmbeddedSigner(this.iframeManager, this.instanceManager, iframeConfiguration);
6227
6500
  }
6228
- async validateAndRefreshToken() {
6229
- if (!this.credentialsProvided()) {
6230
- return;
6231
- }
6232
- const accessToken = this.instanceManager.getAccessToken();
6233
- const refreshToken = this.instanceManager.getRefreshToken();
6234
- const jwk = await this.instanceManager.getJWK();
6235
- if (!accessToken || !refreshToken || !jwk) {
6501
+ async flushSigner() {
6502
+ if (this.signer) {
6503
+ await this.signer.logout();
6504
+ this.instanceManager.removeSignerType();
6236
6505
  return;
6237
6506
  }
6238
- const auth = await this.authManager.validateCredentials(accessToken.token, refreshToken, jwk);
6239
- if (auth.accessToken !== accessToken.token) {
6240
- this.storeCredentials(auth);
6241
- }
6242
- if (this.signer && this.signer.useCredentials()) {
6243
- await this.signer.updateAuthentication();
6507
+ const signerType = this.instanceManager.getSignerType();
6508
+ switch (signerType) {
6509
+ case SignerType.EMBEDDED: {
6510
+ const iframeConfiguration = {
6511
+ accessToken: this.instanceManager.getAccessToken()?.token ?? null,
6512
+ thirdPartyProvider: this.instanceManager.getAccessToken()?.thirdPartyProvider ?? null,
6513
+ thirdPartyTokenType: this.instanceManager.getAccessToken()?.thirdPartyTokenType ?? null,
6514
+ chainId: null,
6515
+ recovery: null,
6516
+ };
6517
+ const embeddedSigner = new EmbeddedSigner(this.iframeManager, this.instanceManager, iframeConfiguration);
6518
+ await embeddedSigner.logout();
6519
+ break;
6520
+ }
6521
+ case SignerType.SESSION:
6522
+ this.configureSessionKey();
6523
+ break;
6244
6524
  }
6525
+ this.instanceManager.removeSignerType();
6245
6526
  }
6246
6527
  }
6247
6528
 
6248
6529
  exports.OpenfortConfiguration = OpenfortConfiguration;
6530
+ exports.OpenfortError = OpenfortError;
6249
6531
  exports.SDKConfiguration = SDKConfiguration;
6250
6532
  exports.ShieldConfiguration = ShieldConfiguration;
6251
6533
  exports.default = Openfort;