@openfort/openfort-js 0.7.15 → 0.7.17

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
@@ -3204,10 +3204,12 @@ class OpenfortConfiguration {
3204
3204
  class ShieldConfiguration {
3205
3205
  shieldPublishableKey;
3206
3206
  shieldEncryptionKey;
3207
+ shieldEncryptionSession;
3207
3208
  debug = false;
3208
3209
  constructor(options) {
3209
3210
  this.shieldPublishableKey = options.shieldPublishableKey;
3210
3211
  this.shieldEncryptionKey = options.shieldEncryptionKey;
3212
+ this.shieldEncryptionSession = options.shieldEncryptionSession;
3211
3213
  this.debug = options.shieldDebug || false;
3212
3214
  }
3213
3215
  }
@@ -5178,6 +5180,163 @@ class AuthManager {
5178
5180
  }
5179
5181
  }
5180
5182
 
5183
+ var Event;
5184
+ (function (Event) {
5185
+ Event["LOADED"] = "loaded";
5186
+ Event["CONFIGURE"] = "configure";
5187
+ Event["CONFIGURED"] = "configured";
5188
+ Event["UPDATE_AUTHENTICATION"] = "update-authentication";
5189
+ Event["AUTHENTICATION_UPDATED"] = "authentication-updated";
5190
+ Event["SIGN"] = "sign";
5191
+ Event["EXPORT"] = "export";
5192
+ Event["SIGNED"] = "signed";
5193
+ Event["LOGOUT"] = "logout";
5194
+ Event["LOGGED_OUT"] = "logged-out";
5195
+ Event["GET_CURRENT_DEVICE"] = "get-current-device";
5196
+ Event["CURRENT_DEVICE"] = "current-device";
5197
+ Event["PING"] = "ping";
5198
+ Event["PONG"] = "pong";
5199
+ })(Event || (Event = {}));
5200
+ const NOT_CONFIGURED_ERROR = 'not-configured-error';
5201
+ const MISSING_USER_ENTROPY_ERROR = 'missing-user-entropy-error';
5202
+ const INCORRECT_USER_ENTROPY_ERROR = 'incorrect-user-entropy-error';
5203
+ class GetCurrentDeviceRequest {
5204
+ uuid;
5205
+ action = Event.GET_CURRENT_DEVICE;
5206
+ playerID;
5207
+ constructor(uuid, playerId) {
5208
+ this.uuid = uuid;
5209
+ this.playerID = playerId;
5210
+ }
5211
+ }
5212
+ class PingRequest {
5213
+ uuid;
5214
+ action = Event.PING;
5215
+ constructor(uuid) {
5216
+ this.uuid = uuid;
5217
+ }
5218
+ }
5219
+ class GetCurrentDeviceResponse {
5220
+ uuid;
5221
+ success;
5222
+ action = Event.CURRENT_DEVICE;
5223
+ deviceID;
5224
+ accountType;
5225
+ version = null;
5226
+ chainId;
5227
+ address;
5228
+ constructor(uuid, deviceID, accountType, chainId, address) {
5229
+ this.uuid = uuid;
5230
+ this.success = true;
5231
+ this.deviceID = deviceID;
5232
+ this.accountType = accountType;
5233
+ this.chainId = chainId;
5234
+ this.address = address;
5235
+ }
5236
+ }
5237
+ class LogoutRequest {
5238
+ uuid;
5239
+ action = Event.LOGOUT;
5240
+ constructor(uuid) {
5241
+ this.uuid = uuid;
5242
+ }
5243
+ }
5244
+ class SignRequest {
5245
+ uuid;
5246
+ action = Event.SIGN;
5247
+ message;
5248
+ requireArrayify;
5249
+ requireHash;
5250
+ requestConfiguration;
5251
+ constructor(uuid, message, requireArrayify, requireHash, requestConfiguration) {
5252
+ this.uuid = uuid;
5253
+ this.message = message;
5254
+ this.requireArrayify = requireArrayify;
5255
+ this.requireHash = requireHash;
5256
+ this.requestConfiguration = requestConfiguration;
5257
+ }
5258
+ }
5259
+ class ExportPrivateKeyRequest {
5260
+ uuid;
5261
+ action = Event.EXPORT;
5262
+ requestConfiguration;
5263
+ constructor(uuid, requestConfiguration) {
5264
+ this.uuid = uuid;
5265
+ this.requestConfiguration = requestConfiguration;
5266
+ }
5267
+ }
5268
+ function isErrorResponse(response) {
5269
+ return 'error' in response;
5270
+ }
5271
+ class ConfigureResponse {
5272
+ uuid;
5273
+ success;
5274
+ deviceID;
5275
+ address;
5276
+ chainId;
5277
+ accountType;
5278
+ action = Event.CONFIGURED;
5279
+ version;
5280
+ constructor(uuid, deviceID, accountType, chainId, address) {
5281
+ this.success = true;
5282
+ this.deviceID = deviceID;
5283
+ this.uuid = uuid;
5284
+ this.accountType = accountType;
5285
+ this.chainId = chainId;
5286
+ this.address = address;
5287
+ this.version = null;
5288
+ }
5289
+ }
5290
+ class UpdateAuthenticationResponse {
5291
+ uuid;
5292
+ success;
5293
+ action = Event.AUTHENTICATION_UPDATED;
5294
+ version;
5295
+ constructor(uuid) {
5296
+ this.success = true;
5297
+ this.uuid = uuid;
5298
+ this.version = null;
5299
+ }
5300
+ }
5301
+ class SignResponse {
5302
+ uuid;
5303
+ success;
5304
+ signature;
5305
+ action = Event.SIGNED;
5306
+ version;
5307
+ constructor(uuid, signature) {
5308
+ this.success = true;
5309
+ this.signature = signature;
5310
+ this.uuid = uuid;
5311
+ this.version = null;
5312
+ }
5313
+ }
5314
+ class LogoutResponse {
5315
+ uuid;
5316
+ success;
5317
+ action = Event.LOGGED_OUT;
5318
+ constructor(uuid) {
5319
+ this.success = true;
5320
+ this.uuid = uuid;
5321
+ }
5322
+ }
5323
+ class UpdateAuthenticationRequest {
5324
+ uuid;
5325
+ action = Event.UPDATE_AUTHENTICATION;
5326
+ accessToken;
5327
+ recovery;
5328
+ constructor(uuid, accessToken, recovery) {
5329
+ this.uuid = uuid;
5330
+ this.accessToken = accessToken;
5331
+ this.recovery = recovery;
5332
+ }
5333
+ }
5334
+ exports.ShieldAuthType = void 0;
5335
+ (function (ShieldAuthType) {
5336
+ ShieldAuthType["OPENFORT"] = "openfort";
5337
+ ShieldAuthType["CUSTOM"] = "custom";
5338
+ })(exports.ShieldAuthType || (exports.ShieldAuthType = {}));
5339
+
5181
5340
  const authTokenStorageKey = 'openfort.auth_token';
5182
5341
  const thirdPartyProviderStorageKey = 'openfort.third_party_provider';
5183
5342
  const thirdPartyProviderTokenTypeStorageKey = 'openfort.third_party_provider_token_type';
@@ -5427,6 +5586,11 @@ class InstanceManager {
5427
5586
  getShieldAuthType() {
5428
5587
  if (!this.accountType) {
5429
5588
  this.accountType = this.persistentStorage.get(shieldAuthTypeStorageKey);
5589
+ if (this.accountType === null) {
5590
+ // TODO: remove, this is for backward compatibility
5591
+ this.setShieldAuthType(exports.ShieldAuthType.OPENFORT);
5592
+ this.accountType = exports.ShieldAuthType.OPENFORT;
5593
+ }
5430
5594
  }
5431
5595
  return this.accountType;
5432
5596
  }
@@ -5553,6 +5717,12 @@ class SessionSigner {
5553
5717
  updateAuthentication() {
5554
5718
  return Promise.resolve();
5555
5719
  }
5720
+ export() {
5721
+ if (this.sessionKey === null) {
5722
+ throw new Error('Session key is not loaded.');
5723
+ }
5724
+ return Promise.resolve(this.sessionKey.getPrivateKey());
5725
+ }
5556
5726
  }
5557
5727
 
5558
5728
  class EmbeddedSigner {
@@ -5577,7 +5747,8 @@ class EmbeddedSigner {
5577
5747
  if (!accessToken) {
5578
5748
  return;
5579
5749
  }
5580
- await this.iframeManager.updateAuthentication(this.iframeConfiguration, accessToken.token);
5750
+ const shieldAuthType = this.instanceManager.getShieldAuthType();
5751
+ await this.iframeManager.updateAuthentication(this.iframeConfiguration, accessToken.token, shieldAuthType);
5581
5752
  }
5582
5753
  // eslint-disable-next-line class-methods-use-this
5583
5754
  getSingerType() {
@@ -5606,6 +5777,13 @@ class EmbeddedSigner {
5606
5777
  }
5607
5778
  return await this.iframeManager.sign(this.iframeConfiguration, message, requireArrayify, requireHash);
5608
5779
  }
5780
+ async export() {
5781
+ const loaded = await this.isLoaded();
5782
+ if (!loaded) {
5783
+ throw new Error('Signer is not loaded');
5784
+ }
5785
+ return await this.iframeManager.export(this.iframeConfiguration);
5786
+ }
5609
5787
  getDeviceID() {
5610
5788
  return this.instanceManager.getDeviceID();
5611
5789
  }
@@ -5652,153 +5830,6 @@ class SessionStorage {
5652
5830
  }
5653
5831
  }
5654
5832
 
5655
- var Event;
5656
- (function (Event) {
5657
- Event["LOADED"] = "loaded";
5658
- Event["CONFIGURE"] = "configure";
5659
- Event["CONFIGURED"] = "configured";
5660
- Event["UPDATE_AUTHENTICATION"] = "update-authentication";
5661
- Event["AUTHENTICATION_UPDATED"] = "authentication-updated";
5662
- Event["SIGN"] = "sign";
5663
- Event["SIGNED"] = "signed";
5664
- Event["LOGOUT"] = "logout";
5665
- Event["LOGGED_OUT"] = "logged-out";
5666
- Event["GET_CURRENT_DEVICE"] = "get-current-device";
5667
- Event["CURRENT_DEVICE"] = "current-device";
5668
- Event["PING"] = "ping";
5669
- Event["PONG"] = "pong";
5670
- })(Event || (Event = {}));
5671
- const NOT_CONFIGURED_ERROR = 'not-configured-error';
5672
- const MISSING_USER_ENTROPY_ERROR = 'missing-user-entropy-error';
5673
- const INCORRECT_USER_ENTROPY_ERROR = 'incorrect-user-entropy-error';
5674
- class GetCurrentDeviceRequest {
5675
- uuid;
5676
- action = Event.GET_CURRENT_DEVICE;
5677
- playerID;
5678
- constructor(uuid, playerId) {
5679
- this.uuid = uuid;
5680
- this.playerID = playerId;
5681
- }
5682
- }
5683
- class PingRequest {
5684
- uuid;
5685
- action = Event.PING;
5686
- constructor(uuid) {
5687
- this.uuid = uuid;
5688
- }
5689
- }
5690
- class GetCurrentDeviceResponse {
5691
- uuid;
5692
- success;
5693
- action = Event.CURRENT_DEVICE;
5694
- deviceID;
5695
- accountType;
5696
- version = null;
5697
- chainId;
5698
- address;
5699
- constructor(uuid, deviceID, accountType, chainId, address) {
5700
- this.uuid = uuid;
5701
- this.success = true;
5702
- this.deviceID = deviceID;
5703
- this.accountType = accountType;
5704
- this.chainId = chainId;
5705
- this.address = address;
5706
- }
5707
- }
5708
- class LogoutRequest {
5709
- uuid;
5710
- action = Event.LOGOUT;
5711
- constructor(uuid) {
5712
- this.uuid = uuid;
5713
- }
5714
- }
5715
- class SignRequest {
5716
- uuid;
5717
- action = Event.SIGN;
5718
- message;
5719
- requireArrayify;
5720
- requireHash;
5721
- requestConfiguration;
5722
- constructor(uuid, message, requireArrayify, requireHash, requestConfiguration) {
5723
- this.uuid = uuid;
5724
- this.message = message;
5725
- this.requireArrayify = requireArrayify;
5726
- this.requireHash = requireHash;
5727
- this.requestConfiguration = requestConfiguration;
5728
- }
5729
- }
5730
- function isErrorResponse(response) {
5731
- return 'error' in response;
5732
- }
5733
- class ConfigureResponse {
5734
- uuid;
5735
- success;
5736
- deviceID;
5737
- address;
5738
- chainId;
5739
- accountType;
5740
- action = Event.CONFIGURED;
5741
- version;
5742
- constructor(uuid, deviceID, accountType, chainId, address) {
5743
- this.success = true;
5744
- this.deviceID = deviceID;
5745
- this.uuid = uuid;
5746
- this.accountType = accountType;
5747
- this.chainId = chainId;
5748
- this.address = address;
5749
- this.version = null;
5750
- }
5751
- }
5752
- class UpdateAuthenticationResponse {
5753
- uuid;
5754
- success;
5755
- action = Event.AUTHENTICATION_UPDATED;
5756
- version;
5757
- constructor(uuid) {
5758
- this.success = true;
5759
- this.uuid = uuid;
5760
- this.version = null;
5761
- }
5762
- }
5763
- class SignResponse {
5764
- uuid;
5765
- success;
5766
- signature;
5767
- action = Event.SIGNED;
5768
- version;
5769
- constructor(uuid, signature) {
5770
- this.success = true;
5771
- this.signature = signature;
5772
- this.uuid = uuid;
5773
- this.version = null;
5774
- }
5775
- }
5776
- class LogoutResponse {
5777
- uuid;
5778
- success;
5779
- action = Event.LOGGED_OUT;
5780
- constructor(uuid) {
5781
- this.success = true;
5782
- this.uuid = uuid;
5783
- }
5784
- }
5785
- class UpdateAuthenticationRequest {
5786
- uuid;
5787
- action = Event.UPDATE_AUTHENTICATION;
5788
- accessToken;
5789
- recovery;
5790
- constructor(uuid, accessToken, recovery) {
5791
- this.uuid = uuid;
5792
- this.accessToken = accessToken;
5793
- this.recovery = recovery;
5794
- }
5795
- }
5796
- exports.ShieldAuthType = void 0;
5797
- (function (ShieldAuthType) {
5798
- ShieldAuthType["OPENFORT"] = "openfort";
5799
- ShieldAuthType["CUSTOM"] = "custom";
5800
- })(exports.ShieldAuthType || (exports.ShieldAuthType = {}));
5801
-
5802
5833
  class MissingRecoveryPasswordError extends Error {
5803
5834
  constructor() {
5804
5835
  super('This embedded signer requires a password to be recovered');
@@ -5944,6 +5975,7 @@ class IframeManager {
5944
5975
  thirdPartyTokenType: iframeConfiguration.thirdPartyTokenType,
5945
5976
  encryptionKey: password ?? null,
5946
5977
  encryptionPart: this.sdkConfiguration?.shieldConfiguration?.shieldEncryptionKey ?? null,
5978
+ encryptionSession: this.sdkConfiguration?.shieldConfiguration?.shieldEncryptionSession ?? null,
5947
5979
  openfortURL: this.sdkConfiguration.backendUrl,
5948
5980
  shieldURL: this.sdkConfiguration.shieldUrl,
5949
5981
  };
@@ -5978,6 +6010,32 @@ class IframeManager {
5978
6010
  sessionStorage.setItem('iframe-version', response.version ?? 'undefined');
5979
6011
  return response.signature;
5980
6012
  }
6013
+ async export(iframeConfiguration) {
6014
+ await this.waitForIframeLoad();
6015
+ const uuid = this.generateShortUUID();
6016
+ const requestConfiguration = {
6017
+ thirdPartyProvider: iframeConfiguration.thirdPartyProvider ?? undefined,
6018
+ thirdPartyTokenType: iframeConfiguration.thirdPartyTokenType ?? undefined,
6019
+ token: iframeConfiguration.accessToken ?? undefined,
6020
+ publishableKey: this.sdkConfiguration.baseConfiguration.publishableKey,
6021
+ openfortURL: this.sdkConfiguration.backendUrl,
6022
+ };
6023
+ const request = new ExportPrivateKeyRequest(uuid, requestConfiguration);
6024
+ this.iframe?.contentWindow?.postMessage(request, '*');
6025
+ let response;
6026
+ try {
6027
+ response = await this.waitForResponse(uuid);
6028
+ }
6029
+ catch (e) {
6030
+ if (e instanceof NotConfiguredError) {
6031
+ await this.configure(iframeConfiguration);
6032
+ return this.export(iframeConfiguration);
6033
+ }
6034
+ throw e;
6035
+ }
6036
+ sessionStorage.setItem('iframe-version', response.version ?? 'undefined');
6037
+ return response.key;
6038
+ }
5981
6039
  async getCurrentUser(playerId) {
5982
6040
  await this.waitForIframeLoad();
5983
6041
  const uuid = this.generateShortUUID();
@@ -6002,9 +6060,13 @@ class IframeManager {
6002
6060
  this.iframe?.contentWindow?.postMessage(request, '*');
6003
6061
  await this.waitForResponse(uuid);
6004
6062
  }
6005
- async updateAuthentication(iframeConfiguration, token) {
6063
+ async updateAuthentication(iframeConfiguration, token, shieldAuthType) {
6006
6064
  // eslint-disable-next-line no-param-reassign
6007
6065
  iframeConfiguration.accessToken = token;
6066
+ if (shieldAuthType === exports.ShieldAuthType.OPENFORT && iframeConfiguration.recovery) {
6067
+ // eslint-disable-next-line no-param-reassign
6068
+ iframeConfiguration.recovery.token = token;
6069
+ }
6008
6070
  await this.waitForIframeLoad();
6009
6071
  const uuid = this.generateShortUUID();
6010
6072
  const request = new UpdateAuthenticationRequest(uuid, token);
@@ -6015,7 +6077,7 @@ class IframeManager {
6015
6077
  catch (e) {
6016
6078
  if (e instanceof NotConfiguredError) {
6017
6079
  await this.configure(iframeConfiguration);
6018
- await this.updateAuthentication(iframeConfiguration, token);
6080
+ await this.updateAuthentication(iframeConfiguration, token, shieldAuthType);
6019
6081
  return;
6020
6082
  }
6021
6083
  throw e;
@@ -6144,10 +6206,18 @@ class Openfort {
6144
6206
  this.instanceManager.setShieldAuthToken(shieldAuthentication?.token);
6145
6207
  }
6146
6208
  }
6147
- const signer = this.newEmbeddedSigner(chainId);
6148
- await signer.ensureEmbeddedAccount(recoveryPassword);
6149
- this.signer = signer;
6150
- this.instanceManager.setSignerType(SignerType.EMBEDDED);
6209
+ try {
6210
+ const signer = this.newEmbeddedSigner(chainId);
6211
+ await signer.ensureEmbeddedAccount(recoveryPassword);
6212
+ this.signer = signer;
6213
+ this.instanceManager.setSignerType(SignerType.EMBEDDED);
6214
+ }
6215
+ catch (e) {
6216
+ if (e instanceof MissingRecoveryPasswordError) {
6217
+ await this.flushSigner();
6218
+ throw e;
6219
+ }
6220
+ }
6151
6221
  }
6152
6222
  /**
6153
6223
  * Logs in a user with email and password.
@@ -6433,6 +6503,22 @@ class Openfort {
6433
6503
  const { hashMessage = true, arrayifyMessage = false } = options || {};
6434
6504
  return await this.signer.sign(message, arrayifyMessage, hashMessage);
6435
6505
  }
6506
+ /**
6507
+ * Exports the private key.
6508
+ *
6509
+ * @returns The private key.
6510
+ * @throws {OpenfortError} If no signer is configured.
6511
+ */
6512
+ async exportPrivateKey() {
6513
+ await this.recoverSigner();
6514
+ if (!this.signer) {
6515
+ throw new OpenfortError('In order to sign a message, an embedded signer must be configured', exports.OpenfortErrorType.MISSING_EMBEDDED_SIGNER_ERROR);
6516
+ }
6517
+ if (this.signer.useCredentials()) {
6518
+ await this.validateAndRefreshToken();
6519
+ }
6520
+ return await this.signer.export();
6521
+ }
6436
6522
  /**
6437
6523
  * Signs typed data.
6438
6524
  *
@@ -6603,13 +6689,7 @@ class Openfort {
6603
6689
  if (!this.credentialsProvided()) {
6604
6690
  throw new OpenfortError('Must be logged in to configure embedded signer', exports.OpenfortErrorType.NOT_LOGGED_IN_ERROR);
6605
6691
  }
6606
- let shieldAuthType = this.instanceManager.getShieldAuthType();
6607
- if (!shieldAuthType) {
6608
- // TODO: remove, this is for backward compatibility
6609
- this.instanceManager.setShieldAuthType(exports.ShieldAuthType.OPENFORT);
6610
- shieldAuthType = exports.ShieldAuthType.OPENFORT;
6611
- // throw new OpenfortError('Shield auth type is not set', OpenfortErrorType.INVALID_CONFIGURATION);
6612
- }
6692
+ const shieldAuthType = this.instanceManager.getShieldAuthType();
6613
6693
  const token = shieldAuthType === exports.ShieldAuthType.OPENFORT
6614
6694
  ? this.instanceManager.getAccessToken()?.token
6615
6695
  : this.instanceManager.getShieldAuthToken();
package/dist/index.d.ts CHANGED
@@ -433,10 +433,12 @@ declare class OpenfortConfiguration {
433
433
  declare class ShieldConfiguration {
434
434
  readonly shieldPublishableKey: string;
435
435
  readonly shieldEncryptionKey?: string;
436
+ readonly shieldEncryptionSession?: string;
436
437
  readonly debug?: boolean;
437
438
  constructor(options: {
438
439
  shieldPublishableKey: string;
439
440
  shieldEncryptionKey?: string;
441
+ shieldEncryptionSession?: string;
440
442
  shieldDebug?: boolean;
441
443
  });
442
444
  }
@@ -800,6 +802,13 @@ declare class Openfort {
800
802
  hashMessage?: boolean;
801
803
  arrayifyMessage?: boolean;
802
804
  }): Promise<string>;
805
+ /**
806
+ * Exports the private key.
807
+ *
808
+ * @returns The private key.
809
+ * @throws {OpenfortError} If no signer is configured.
810
+ */
811
+ exportPrivateKey(): Promise<string>;
803
812
  /**
804
813
  * Signs typed data.
805
814
  *
package/dist/index.js CHANGED
@@ -3181,10 +3181,12 @@ class OpenfortConfiguration {
3181
3181
  class ShieldConfiguration {
3182
3182
  shieldPublishableKey;
3183
3183
  shieldEncryptionKey;
3184
+ shieldEncryptionSession;
3184
3185
  debug = false;
3185
3186
  constructor(options) {
3186
3187
  this.shieldPublishableKey = options.shieldPublishableKey;
3187
3188
  this.shieldEncryptionKey = options.shieldEncryptionKey;
3189
+ this.shieldEncryptionSession = options.shieldEncryptionSession;
3188
3190
  this.debug = options.shieldDebug || false;
3189
3191
  }
3190
3192
  }
@@ -5155,6 +5157,163 @@ class AuthManager {
5155
5157
  }
5156
5158
  }
5157
5159
 
5160
+ var Event;
5161
+ (function (Event) {
5162
+ Event["LOADED"] = "loaded";
5163
+ Event["CONFIGURE"] = "configure";
5164
+ Event["CONFIGURED"] = "configured";
5165
+ Event["UPDATE_AUTHENTICATION"] = "update-authentication";
5166
+ Event["AUTHENTICATION_UPDATED"] = "authentication-updated";
5167
+ Event["SIGN"] = "sign";
5168
+ Event["EXPORT"] = "export";
5169
+ Event["SIGNED"] = "signed";
5170
+ Event["LOGOUT"] = "logout";
5171
+ Event["LOGGED_OUT"] = "logged-out";
5172
+ Event["GET_CURRENT_DEVICE"] = "get-current-device";
5173
+ Event["CURRENT_DEVICE"] = "current-device";
5174
+ Event["PING"] = "ping";
5175
+ Event["PONG"] = "pong";
5176
+ })(Event || (Event = {}));
5177
+ const NOT_CONFIGURED_ERROR = 'not-configured-error';
5178
+ const MISSING_USER_ENTROPY_ERROR = 'missing-user-entropy-error';
5179
+ const INCORRECT_USER_ENTROPY_ERROR = 'incorrect-user-entropy-error';
5180
+ class GetCurrentDeviceRequest {
5181
+ uuid;
5182
+ action = Event.GET_CURRENT_DEVICE;
5183
+ playerID;
5184
+ constructor(uuid, playerId) {
5185
+ this.uuid = uuid;
5186
+ this.playerID = playerId;
5187
+ }
5188
+ }
5189
+ class PingRequest {
5190
+ uuid;
5191
+ action = Event.PING;
5192
+ constructor(uuid) {
5193
+ this.uuid = uuid;
5194
+ }
5195
+ }
5196
+ class GetCurrentDeviceResponse {
5197
+ uuid;
5198
+ success;
5199
+ action = Event.CURRENT_DEVICE;
5200
+ deviceID;
5201
+ accountType;
5202
+ version = null;
5203
+ chainId;
5204
+ address;
5205
+ constructor(uuid, deviceID, accountType, chainId, address) {
5206
+ this.uuid = uuid;
5207
+ this.success = true;
5208
+ this.deviceID = deviceID;
5209
+ this.accountType = accountType;
5210
+ this.chainId = chainId;
5211
+ this.address = address;
5212
+ }
5213
+ }
5214
+ class LogoutRequest {
5215
+ uuid;
5216
+ action = Event.LOGOUT;
5217
+ constructor(uuid) {
5218
+ this.uuid = uuid;
5219
+ }
5220
+ }
5221
+ class SignRequest {
5222
+ uuid;
5223
+ action = Event.SIGN;
5224
+ message;
5225
+ requireArrayify;
5226
+ requireHash;
5227
+ requestConfiguration;
5228
+ constructor(uuid, message, requireArrayify, requireHash, requestConfiguration) {
5229
+ this.uuid = uuid;
5230
+ this.message = message;
5231
+ this.requireArrayify = requireArrayify;
5232
+ this.requireHash = requireHash;
5233
+ this.requestConfiguration = requestConfiguration;
5234
+ }
5235
+ }
5236
+ class ExportPrivateKeyRequest {
5237
+ uuid;
5238
+ action = Event.EXPORT;
5239
+ requestConfiguration;
5240
+ constructor(uuid, requestConfiguration) {
5241
+ this.uuid = uuid;
5242
+ this.requestConfiguration = requestConfiguration;
5243
+ }
5244
+ }
5245
+ function isErrorResponse(response) {
5246
+ return 'error' in response;
5247
+ }
5248
+ class ConfigureResponse {
5249
+ uuid;
5250
+ success;
5251
+ deviceID;
5252
+ address;
5253
+ chainId;
5254
+ accountType;
5255
+ action = Event.CONFIGURED;
5256
+ version;
5257
+ constructor(uuid, deviceID, accountType, chainId, address) {
5258
+ this.success = true;
5259
+ this.deviceID = deviceID;
5260
+ this.uuid = uuid;
5261
+ this.accountType = accountType;
5262
+ this.chainId = chainId;
5263
+ this.address = address;
5264
+ this.version = null;
5265
+ }
5266
+ }
5267
+ class UpdateAuthenticationResponse {
5268
+ uuid;
5269
+ success;
5270
+ action = Event.AUTHENTICATION_UPDATED;
5271
+ version;
5272
+ constructor(uuid) {
5273
+ this.success = true;
5274
+ this.uuid = uuid;
5275
+ this.version = null;
5276
+ }
5277
+ }
5278
+ class SignResponse {
5279
+ uuid;
5280
+ success;
5281
+ signature;
5282
+ action = Event.SIGNED;
5283
+ version;
5284
+ constructor(uuid, signature) {
5285
+ this.success = true;
5286
+ this.signature = signature;
5287
+ this.uuid = uuid;
5288
+ this.version = null;
5289
+ }
5290
+ }
5291
+ class LogoutResponse {
5292
+ uuid;
5293
+ success;
5294
+ action = Event.LOGGED_OUT;
5295
+ constructor(uuid) {
5296
+ this.success = true;
5297
+ this.uuid = uuid;
5298
+ }
5299
+ }
5300
+ class UpdateAuthenticationRequest {
5301
+ uuid;
5302
+ action = Event.UPDATE_AUTHENTICATION;
5303
+ accessToken;
5304
+ recovery;
5305
+ constructor(uuid, accessToken, recovery) {
5306
+ this.uuid = uuid;
5307
+ this.accessToken = accessToken;
5308
+ this.recovery = recovery;
5309
+ }
5310
+ }
5311
+ var ShieldAuthType;
5312
+ (function (ShieldAuthType) {
5313
+ ShieldAuthType["OPENFORT"] = "openfort";
5314
+ ShieldAuthType["CUSTOM"] = "custom";
5315
+ })(ShieldAuthType || (ShieldAuthType = {}));
5316
+
5158
5317
  const authTokenStorageKey = 'openfort.auth_token';
5159
5318
  const thirdPartyProviderStorageKey = 'openfort.third_party_provider';
5160
5319
  const thirdPartyProviderTokenTypeStorageKey = 'openfort.third_party_provider_token_type';
@@ -5404,6 +5563,11 @@ class InstanceManager {
5404
5563
  getShieldAuthType() {
5405
5564
  if (!this.accountType) {
5406
5565
  this.accountType = this.persistentStorage.get(shieldAuthTypeStorageKey);
5566
+ if (this.accountType === null) {
5567
+ // TODO: remove, this is for backward compatibility
5568
+ this.setShieldAuthType(ShieldAuthType.OPENFORT);
5569
+ this.accountType = ShieldAuthType.OPENFORT;
5570
+ }
5407
5571
  }
5408
5572
  return this.accountType;
5409
5573
  }
@@ -5530,6 +5694,12 @@ class SessionSigner {
5530
5694
  updateAuthentication() {
5531
5695
  return Promise.resolve();
5532
5696
  }
5697
+ export() {
5698
+ if (this.sessionKey === null) {
5699
+ throw new Error('Session key is not loaded.');
5700
+ }
5701
+ return Promise.resolve(this.sessionKey.getPrivateKey());
5702
+ }
5533
5703
  }
5534
5704
 
5535
5705
  class EmbeddedSigner {
@@ -5554,7 +5724,8 @@ class EmbeddedSigner {
5554
5724
  if (!accessToken) {
5555
5725
  return;
5556
5726
  }
5557
- await this.iframeManager.updateAuthentication(this.iframeConfiguration, accessToken.token);
5727
+ const shieldAuthType = this.instanceManager.getShieldAuthType();
5728
+ await this.iframeManager.updateAuthentication(this.iframeConfiguration, accessToken.token, shieldAuthType);
5558
5729
  }
5559
5730
  // eslint-disable-next-line class-methods-use-this
5560
5731
  getSingerType() {
@@ -5583,6 +5754,13 @@ class EmbeddedSigner {
5583
5754
  }
5584
5755
  return await this.iframeManager.sign(this.iframeConfiguration, message, requireArrayify, requireHash);
5585
5756
  }
5757
+ async export() {
5758
+ const loaded = await this.isLoaded();
5759
+ if (!loaded) {
5760
+ throw new Error('Signer is not loaded');
5761
+ }
5762
+ return await this.iframeManager.export(this.iframeConfiguration);
5763
+ }
5586
5764
  getDeviceID() {
5587
5765
  return this.instanceManager.getDeviceID();
5588
5766
  }
@@ -5629,153 +5807,6 @@ class SessionStorage {
5629
5807
  }
5630
5808
  }
5631
5809
 
5632
- var Event;
5633
- (function (Event) {
5634
- Event["LOADED"] = "loaded";
5635
- Event["CONFIGURE"] = "configure";
5636
- Event["CONFIGURED"] = "configured";
5637
- Event["UPDATE_AUTHENTICATION"] = "update-authentication";
5638
- Event["AUTHENTICATION_UPDATED"] = "authentication-updated";
5639
- Event["SIGN"] = "sign";
5640
- Event["SIGNED"] = "signed";
5641
- Event["LOGOUT"] = "logout";
5642
- Event["LOGGED_OUT"] = "logged-out";
5643
- Event["GET_CURRENT_DEVICE"] = "get-current-device";
5644
- Event["CURRENT_DEVICE"] = "current-device";
5645
- Event["PING"] = "ping";
5646
- Event["PONG"] = "pong";
5647
- })(Event || (Event = {}));
5648
- const NOT_CONFIGURED_ERROR = 'not-configured-error';
5649
- const MISSING_USER_ENTROPY_ERROR = 'missing-user-entropy-error';
5650
- const INCORRECT_USER_ENTROPY_ERROR = 'incorrect-user-entropy-error';
5651
- class GetCurrentDeviceRequest {
5652
- uuid;
5653
- action = Event.GET_CURRENT_DEVICE;
5654
- playerID;
5655
- constructor(uuid, playerId) {
5656
- this.uuid = uuid;
5657
- this.playerID = playerId;
5658
- }
5659
- }
5660
- class PingRequest {
5661
- uuid;
5662
- action = Event.PING;
5663
- constructor(uuid) {
5664
- this.uuid = uuid;
5665
- }
5666
- }
5667
- class GetCurrentDeviceResponse {
5668
- uuid;
5669
- success;
5670
- action = Event.CURRENT_DEVICE;
5671
- deviceID;
5672
- accountType;
5673
- version = null;
5674
- chainId;
5675
- address;
5676
- constructor(uuid, deviceID, accountType, chainId, address) {
5677
- this.uuid = uuid;
5678
- this.success = true;
5679
- this.deviceID = deviceID;
5680
- this.accountType = accountType;
5681
- this.chainId = chainId;
5682
- this.address = address;
5683
- }
5684
- }
5685
- class LogoutRequest {
5686
- uuid;
5687
- action = Event.LOGOUT;
5688
- constructor(uuid) {
5689
- this.uuid = uuid;
5690
- }
5691
- }
5692
- class SignRequest {
5693
- uuid;
5694
- action = Event.SIGN;
5695
- message;
5696
- requireArrayify;
5697
- requireHash;
5698
- requestConfiguration;
5699
- constructor(uuid, message, requireArrayify, requireHash, requestConfiguration) {
5700
- this.uuid = uuid;
5701
- this.message = message;
5702
- this.requireArrayify = requireArrayify;
5703
- this.requireHash = requireHash;
5704
- this.requestConfiguration = requestConfiguration;
5705
- }
5706
- }
5707
- function isErrorResponse(response) {
5708
- return 'error' in response;
5709
- }
5710
- class ConfigureResponse {
5711
- uuid;
5712
- success;
5713
- deviceID;
5714
- address;
5715
- chainId;
5716
- accountType;
5717
- action = Event.CONFIGURED;
5718
- version;
5719
- constructor(uuid, deviceID, accountType, chainId, address) {
5720
- this.success = true;
5721
- this.deviceID = deviceID;
5722
- this.uuid = uuid;
5723
- this.accountType = accountType;
5724
- this.chainId = chainId;
5725
- this.address = address;
5726
- this.version = null;
5727
- }
5728
- }
5729
- class UpdateAuthenticationResponse {
5730
- uuid;
5731
- success;
5732
- action = Event.AUTHENTICATION_UPDATED;
5733
- version;
5734
- constructor(uuid) {
5735
- this.success = true;
5736
- this.uuid = uuid;
5737
- this.version = null;
5738
- }
5739
- }
5740
- class SignResponse {
5741
- uuid;
5742
- success;
5743
- signature;
5744
- action = Event.SIGNED;
5745
- version;
5746
- constructor(uuid, signature) {
5747
- this.success = true;
5748
- this.signature = signature;
5749
- this.uuid = uuid;
5750
- this.version = null;
5751
- }
5752
- }
5753
- class LogoutResponse {
5754
- uuid;
5755
- success;
5756
- action = Event.LOGGED_OUT;
5757
- constructor(uuid) {
5758
- this.success = true;
5759
- this.uuid = uuid;
5760
- }
5761
- }
5762
- class UpdateAuthenticationRequest {
5763
- uuid;
5764
- action = Event.UPDATE_AUTHENTICATION;
5765
- accessToken;
5766
- recovery;
5767
- constructor(uuid, accessToken, recovery) {
5768
- this.uuid = uuid;
5769
- this.accessToken = accessToken;
5770
- this.recovery = recovery;
5771
- }
5772
- }
5773
- var ShieldAuthType;
5774
- (function (ShieldAuthType) {
5775
- ShieldAuthType["OPENFORT"] = "openfort";
5776
- ShieldAuthType["CUSTOM"] = "custom";
5777
- })(ShieldAuthType || (ShieldAuthType = {}));
5778
-
5779
5810
  class MissingRecoveryPasswordError extends Error {
5780
5811
  constructor() {
5781
5812
  super('This embedded signer requires a password to be recovered');
@@ -5921,6 +5952,7 @@ class IframeManager {
5921
5952
  thirdPartyTokenType: iframeConfiguration.thirdPartyTokenType,
5922
5953
  encryptionKey: password ?? null,
5923
5954
  encryptionPart: this.sdkConfiguration?.shieldConfiguration?.shieldEncryptionKey ?? null,
5955
+ encryptionSession: this.sdkConfiguration?.shieldConfiguration?.shieldEncryptionSession ?? null,
5924
5956
  openfortURL: this.sdkConfiguration.backendUrl,
5925
5957
  shieldURL: this.sdkConfiguration.shieldUrl,
5926
5958
  };
@@ -5955,6 +5987,32 @@ class IframeManager {
5955
5987
  sessionStorage.setItem('iframe-version', response.version ?? 'undefined');
5956
5988
  return response.signature;
5957
5989
  }
5990
+ async export(iframeConfiguration) {
5991
+ await this.waitForIframeLoad();
5992
+ const uuid = this.generateShortUUID();
5993
+ const requestConfiguration = {
5994
+ thirdPartyProvider: iframeConfiguration.thirdPartyProvider ?? undefined,
5995
+ thirdPartyTokenType: iframeConfiguration.thirdPartyTokenType ?? undefined,
5996
+ token: iframeConfiguration.accessToken ?? undefined,
5997
+ publishableKey: this.sdkConfiguration.baseConfiguration.publishableKey,
5998
+ openfortURL: this.sdkConfiguration.backendUrl,
5999
+ };
6000
+ const request = new ExportPrivateKeyRequest(uuid, requestConfiguration);
6001
+ this.iframe?.contentWindow?.postMessage(request, '*');
6002
+ let response;
6003
+ try {
6004
+ response = await this.waitForResponse(uuid);
6005
+ }
6006
+ catch (e) {
6007
+ if (e instanceof NotConfiguredError) {
6008
+ await this.configure(iframeConfiguration);
6009
+ return this.export(iframeConfiguration);
6010
+ }
6011
+ throw e;
6012
+ }
6013
+ sessionStorage.setItem('iframe-version', response.version ?? 'undefined');
6014
+ return response.key;
6015
+ }
5958
6016
  async getCurrentUser(playerId) {
5959
6017
  await this.waitForIframeLoad();
5960
6018
  const uuid = this.generateShortUUID();
@@ -5979,9 +6037,13 @@ class IframeManager {
5979
6037
  this.iframe?.contentWindow?.postMessage(request, '*');
5980
6038
  await this.waitForResponse(uuid);
5981
6039
  }
5982
- async updateAuthentication(iframeConfiguration, token) {
6040
+ async updateAuthentication(iframeConfiguration, token, shieldAuthType) {
5983
6041
  // eslint-disable-next-line no-param-reassign
5984
6042
  iframeConfiguration.accessToken = token;
6043
+ if (shieldAuthType === ShieldAuthType.OPENFORT && iframeConfiguration.recovery) {
6044
+ // eslint-disable-next-line no-param-reassign
6045
+ iframeConfiguration.recovery.token = token;
6046
+ }
5985
6047
  await this.waitForIframeLoad();
5986
6048
  const uuid = this.generateShortUUID();
5987
6049
  const request = new UpdateAuthenticationRequest(uuid, token);
@@ -5992,7 +6054,7 @@ class IframeManager {
5992
6054
  catch (e) {
5993
6055
  if (e instanceof NotConfiguredError) {
5994
6056
  await this.configure(iframeConfiguration);
5995
- await this.updateAuthentication(iframeConfiguration, token);
6057
+ await this.updateAuthentication(iframeConfiguration, token, shieldAuthType);
5996
6058
  return;
5997
6059
  }
5998
6060
  throw e;
@@ -6121,10 +6183,18 @@ class Openfort {
6121
6183
  this.instanceManager.setShieldAuthToken(shieldAuthentication?.token);
6122
6184
  }
6123
6185
  }
6124
- const signer = this.newEmbeddedSigner(chainId);
6125
- await signer.ensureEmbeddedAccount(recoveryPassword);
6126
- this.signer = signer;
6127
- this.instanceManager.setSignerType(SignerType.EMBEDDED);
6186
+ try {
6187
+ const signer = this.newEmbeddedSigner(chainId);
6188
+ await signer.ensureEmbeddedAccount(recoveryPassword);
6189
+ this.signer = signer;
6190
+ this.instanceManager.setSignerType(SignerType.EMBEDDED);
6191
+ }
6192
+ catch (e) {
6193
+ if (e instanceof MissingRecoveryPasswordError) {
6194
+ await this.flushSigner();
6195
+ throw e;
6196
+ }
6197
+ }
6128
6198
  }
6129
6199
  /**
6130
6200
  * Logs in a user with email and password.
@@ -6410,6 +6480,22 @@ class Openfort {
6410
6480
  const { hashMessage = true, arrayifyMessage = false } = options || {};
6411
6481
  return await this.signer.sign(message, arrayifyMessage, hashMessage);
6412
6482
  }
6483
+ /**
6484
+ * Exports the private key.
6485
+ *
6486
+ * @returns The private key.
6487
+ * @throws {OpenfortError} If no signer is configured.
6488
+ */
6489
+ async exportPrivateKey() {
6490
+ await this.recoverSigner();
6491
+ if (!this.signer) {
6492
+ throw new OpenfortError('In order to sign a message, an embedded signer must be configured', OpenfortErrorType.MISSING_EMBEDDED_SIGNER_ERROR);
6493
+ }
6494
+ if (this.signer.useCredentials()) {
6495
+ await this.validateAndRefreshToken();
6496
+ }
6497
+ return await this.signer.export();
6498
+ }
6413
6499
  /**
6414
6500
  * Signs typed data.
6415
6501
  *
@@ -6580,13 +6666,7 @@ class Openfort {
6580
6666
  if (!this.credentialsProvided()) {
6581
6667
  throw new OpenfortError('Must be logged in to configure embedded signer', OpenfortErrorType.NOT_LOGGED_IN_ERROR);
6582
6668
  }
6583
- let shieldAuthType = this.instanceManager.getShieldAuthType();
6584
- if (!shieldAuthType) {
6585
- // TODO: remove, this is for backward compatibility
6586
- this.instanceManager.setShieldAuthType(ShieldAuthType.OPENFORT);
6587
- shieldAuthType = ShieldAuthType.OPENFORT;
6588
- // throw new OpenfortError('Shield auth type is not set', OpenfortErrorType.INVALID_CONFIGURATION);
6589
- }
6669
+ const shieldAuthType = this.instanceManager.getShieldAuthType();
6590
6670
  const token = shieldAuthType === ShieldAuthType.OPENFORT
6591
6671
  ? this.instanceManager.getAccessToken()?.token
6592
6672
  : this.instanceManager.getShieldAuthToken();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfort/openfort-js",
3
- "version": "0.7.15",
3
+ "version": "0.7.17",
4
4
  "author": "Openfort (https://www.openfort.xyz)",
5
5
  "bugs": "https://github.com/openfort-xyz/openfort-js/issues",
6
6
  "repository": "openfort-xyz/openfort-js.git",