@phantom/embedded-provider-core 1.0.0-beta.21 → 1.0.0-beta.22

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
@@ -32,10 +32,10 @@ var src_exports = {};
32
32
  __export(src_exports, {
33
33
  AUTHENTICATOR_EXPIRATION_TIME_MS: () => AUTHENTICATOR_EXPIRATION_TIME_MS,
34
34
  AUTHENTICATOR_RENEWAL_WINDOW_MS: () => AUTHENTICATOR_RENEWAL_WINDOW_MS,
35
+ EMBEDDED_PROVIDER_AUTH_TYPES: () => EMBEDDED_PROVIDER_AUTH_TYPES,
35
36
  EmbeddedEthereumChain: () => EmbeddedEthereumChain,
36
37
  EmbeddedProvider: () => EmbeddedProvider,
37
38
  EmbeddedSolanaChain: () => EmbeddedSolanaChain,
38
- JWTAuth: () => JWTAuth,
39
39
  generateSessionId: () => generateSessionId,
40
40
  retryWithBackoff: () => retryWithBackoff
41
41
  });
@@ -52,91 +52,7 @@ var import_bs582 = __toESM(require("bs58"));
52
52
  // src/constants.ts
53
53
  var AUTHENTICATOR_EXPIRATION_TIME_MS = 7 * 24 * 60 * 60 * 1e3;
54
54
  var AUTHENTICATOR_RENEWAL_WINDOW_MS = 2 * 24 * 60 * 60 * 1e3;
55
-
56
- // src/auth/jwt-auth.ts
57
- var JWTAuth = class {
58
- async authenticate(options) {
59
- if (!options.jwtToken || typeof options.jwtToken !== "string") {
60
- throw new Error("Invalid JWT token: token must be a non-empty string");
61
- }
62
- const jwtParts = options.jwtToken.split(".");
63
- if (jwtParts.length !== 3) {
64
- throw new Error("Invalid JWT token format: token must have 3 parts separated by dots");
65
- }
66
- try {
67
- const response = await fetch("/api/auth/jwt", {
68
- method: "POST",
69
- headers: {
70
- "Content-Type": "application/json",
71
- Authorization: `Bearer ${options.jwtToken}`,
72
- "X-PHANTOM-APPID": options.appId
73
- },
74
- body: JSON.stringify({
75
- appId: options.appId,
76
- publicKey: options.publicKey
77
- })
78
- });
79
- if (!response.ok) {
80
- let errorMessage = `HTTP ${response.status}`;
81
- try {
82
- const errorData = await response.json();
83
- errorMessage = errorData.message || errorData.error || errorMessage;
84
- } catch {
85
- errorMessage = response.statusText || errorMessage;
86
- }
87
- switch (response.status) {
88
- case 400:
89
- throw new Error(`Invalid JWT authentication request: ${errorMessage}`);
90
- case 401:
91
- throw new Error(`JWT token is invalid or expired: ${errorMessage}`);
92
- case 403:
93
- throw new Error(`JWT authentication forbidden: ${errorMessage}`);
94
- case 404:
95
- throw new Error(`JWT authentication endpoint not found: ${errorMessage}`);
96
- case 429:
97
- throw new Error(`Too many JWT authentication requests: ${errorMessage}`);
98
- case 500:
99
- case 502:
100
- case 503:
101
- case 504:
102
- throw new Error(`JWT authentication server error: ${errorMessage}`);
103
- default:
104
- throw new Error(`JWT authentication failed: ${errorMessage}`);
105
- }
106
- }
107
- let result;
108
- try {
109
- result = await response.json();
110
- } catch (parseError) {
111
- throw new Error("Invalid response from JWT authentication server: response is not valid JSON");
112
- }
113
- if (!result.walletId) {
114
- throw new Error("Invalid JWT authentication response: missing walletId");
115
- }
116
- if (!result.organizationId) {
117
- throw new Error("Invalid JWT authentication response: missing organizationId");
118
- }
119
- if (!result.expiresInMs) {
120
- throw new Error("Invalid JWT authentication response: missing expiresInMs");
121
- }
122
- return {
123
- walletId: result.walletId,
124
- organizationId: result.organizationId,
125
- provider: "jwt",
126
- expiresInMs: result.expiresInMs,
127
- accountDerivationIndex: result.accountDerivationIndex || 0
128
- };
129
- } catch (error) {
130
- if (error instanceof TypeError && error.message.includes("fetch")) {
131
- throw new Error("JWT authentication failed: network error or invalid endpoint");
132
- }
133
- if (error instanceof Error) {
134
- throw error;
135
- }
136
- throw new Error(`JWT authentication error: ${String(error)}`);
137
- }
138
- }
139
- };
55
+ var EMBEDDED_PROVIDER_AUTH_TYPES = ["google", "apple", "x", "phantom", "tiktok"];
140
56
 
141
57
  // src/chains/SolanaChain.ts
142
58
  var import_eventemitter3 = require("eventemitter3");
@@ -536,7 +452,6 @@ var EmbeddedProvider = class {
536
452
  this.phantomAppProvider = platform.phantomAppProvider;
537
453
  this.urlParamsAccessor = platform.urlParamsAccessor;
538
454
  this.stamper = platform.stamper;
539
- this.jwtAuth = new JWTAuth();
540
455
  this.solana = new EmbeddedSolanaChain(this);
541
456
  this.ethereum = new EmbeddedEthereumChain(this);
542
457
  this.logger.info("EMBEDDED_PROVIDER", "EmbeddedProvider initialized");
@@ -677,7 +592,11 @@ var EmbeddedProvider = class {
677
592
  this.logger.log("EMBEDDED_PROVIDER", "Getting existing session");
678
593
  let session = await this.storage.getSession();
679
594
  session = await this.validateAndCleanSession(session);
680
- if (session && session.status === "completed") {
595
+ if (!session) {
596
+ this.logger.log("EMBEDDED_PROVIDER", "No existing session found");
597
+ return null;
598
+ }
599
+ if (session.status === "completed") {
681
600
  this.logger.info("EMBEDDED_PROVIDER", "Using existing completed session", {
682
601
  sessionId: session.sessionId,
683
602
  walletId: session.walletId
@@ -694,8 +613,8 @@ var EmbeddedProvider = class {
694
613
  walletId: this.walletId,
695
614
  addresses: this.addresses,
696
615
  status: "completed",
697
- providerType: "embedded",
698
- authUserId: session.authUserId
616
+ authUserId: session.authUserId,
617
+ authProvider: session.authProvider
699
618
  };
700
619
  this.emit("connect", {
701
620
  ...result,
@@ -705,7 +624,7 @@ var EmbeddedProvider = class {
705
624
  }
706
625
  this.logger.log("EMBEDDED_PROVIDER", "No completed session found, checking for redirect resume");
707
626
  if (this.authProvider.resumeAuthFromRedirect) {
708
- const authResult = this.authProvider.resumeAuthFromRedirect();
627
+ const authResult = this.authProvider.resumeAuthFromRedirect(session.authProvider);
709
628
  if (authResult) {
710
629
  this.logger.info("EMBEDDED_PROVIDER", "Resuming from redirect", {
711
630
  walletId: authResult.walletId,
@@ -737,11 +656,10 @@ var EmbeddedProvider = class {
737
656
  * This ensures only supported auth providers are used and required tokens are present.
738
657
  */
739
658
  validateAuthOptions(authOptions) {
740
- if (!["google", "apple", "jwt", "phantom"].includes(authOptions.provider)) {
741
- throw new Error(`Invalid auth provider: ${authOptions.provider}. Must be "google", "apple", "jwt", or "phantom"`);
742
- }
743
- if (authOptions.provider === "jwt" && !authOptions.jwtToken) {
744
- throw new Error("JWT token is required when using JWT authentication");
659
+ if (!EMBEDDED_PROVIDER_AUTH_TYPES.includes(authOptions.provider)) {
660
+ throw new Error(
661
+ `Invalid auth provider: ${authOptions.provider}. Must be "google", "apple", "phantom", "tiktok", or "x"`
662
+ );
745
663
  }
746
664
  }
747
665
  /*
@@ -881,8 +799,7 @@ var EmbeddedProvider = class {
881
799
  try {
882
800
  this.logger.info("EMBEDDED_PROVIDER", "Starting embedded provider connect", {
883
801
  authOptions: {
884
- provider: authOptions.provider,
885
- hasJwtToken: !!authOptions.jwtToken
802
+ provider: authOptions.provider
886
803
  }
887
804
  });
888
805
  this.emit("connect_start", {
@@ -912,10 +829,10 @@ var EmbeddedProvider = class {
912
829
  return {
913
830
  addresses: [],
914
831
  status: "pending",
915
- providerType: "embedded"
832
+ authProvider: authOptions.provider
916
833
  };
917
834
  }
918
- if (authOptions.provider === "jwt" || this.config.embeddedWalletType === "app-wallet") {
835
+ if (this.config.embeddedWalletType === "app-wallet") {
919
836
  session.lastUsed = Date.now();
920
837
  await this.storage.saveSession(session);
921
838
  }
@@ -925,8 +842,8 @@ var EmbeddedProvider = class {
925
842
  walletId: this.walletId,
926
843
  addresses: this.addresses,
927
844
  status: "completed",
928
- providerType: "embedded",
929
- authUserId: session?.authUserId
845
+ authUserId: session?.authUserId,
846
+ authProvider: session?.authProvider
930
847
  };
931
848
  this.emit("connect", {
932
849
  ...result,
@@ -1151,9 +1068,7 @@ var EmbeddedProvider = class {
1151
1068
  this.logger.info("EMBEDDED_PROVIDER", "Creating user-wallet, routing authentication", {
1152
1069
  authProvider: authOptions.provider
1153
1070
  });
1154
- if (authOptions.provider === "jwt") {
1155
- return await this.handleJWTAuth(publicKey, stamperInfo, authOptions, expiresInMs);
1156
- } else if (authOptions.provider === "phantom") {
1071
+ if (authOptions.provider === "phantom") {
1157
1072
  return await this.handlePhantomAuth(publicKey, stamperInfo, expiresInMs);
1158
1073
  } else {
1159
1074
  this.logger.info("EMBEDDED_PROVIDER", "Starting redirect-based authentication flow", {
@@ -1186,7 +1101,8 @@ var EmbeddedProvider = class {
1186
1101
  organizationId,
1187
1102
  appId: this.config.appId,
1188
1103
  stamperInfo,
1189
- authProvider: "app-wallet",
1104
+ authProvider: "device",
1105
+ // For now app wallets have no auth provider.
1190
1106
  accountDerivationIndex: 0,
1191
1107
  // App wallets default to index 0
1192
1108
  status: "completed",
@@ -1201,51 +1117,6 @@ var EmbeddedProvider = class {
1201
1117
  return session;
1202
1118
  }
1203
1119
  }
1204
- /*
1205
- * We use this method to handle JWT-based authentication for user-wallets.
1206
- * It authenticates using the provided JWT token and creates a completed session.
1207
- */
1208
- async handleJWTAuth(publicKey, stamperInfo, authOptions, localExpiresInMs) {
1209
- this.logger.info("EMBEDDED_PROVIDER", "Using JWT authentication flow");
1210
- if (!authOptions.jwtToken) {
1211
- this.logger.error("EMBEDDED_PROVIDER", "JWT token missing for JWT authentication");
1212
- throw new Error("JWT token is required for JWT authentication");
1213
- }
1214
- this.logger.log("EMBEDDED_PROVIDER", "Starting JWT authentication");
1215
- const authResult = await this.jwtAuth.authenticate({
1216
- publicKey,
1217
- appId: this.config.appId,
1218
- jwtToken: authOptions.jwtToken
1219
- });
1220
- const walletId = authResult.walletId;
1221
- const organizationId = authResult.organizationId;
1222
- const expiresInMs = authResult.expiresInMs > 0 ? authResult.expiresInMs : localExpiresInMs;
1223
- this.logger.info("EMBEDDED_PROVIDER", "JWT authentication completed", {
1224
- walletId,
1225
- organizationId,
1226
- expiresInMs,
1227
- source: authResult.expiresInMs ? "server" : "local"
1228
- });
1229
- const now = Date.now();
1230
- const session = {
1231
- sessionId: generateSessionId(),
1232
- walletId,
1233
- organizationId,
1234
- appId: this.config.appId,
1235
- stamperInfo,
1236
- authProvider: authResult.provider,
1237
- accountDerivationIndex: authResult.accountDerivationIndex,
1238
- status: "completed",
1239
- createdAt: now,
1240
- lastUsed: now,
1241
- authenticatorCreatedAt: now,
1242
- authenticatorExpiresAt: Date.now() + expiresInMs,
1243
- lastRenewalAttempt: void 0
1244
- };
1245
- this.logger.log("EMBEDDED_PROVIDER", "Saving JWT session");
1246
- await this.storage.saveSession(session);
1247
- return session;
1248
- }
1249
1120
  /*
1250
1121
  * We use this method to handle Phantom app-based authentication for user-wallets.
1251
1122
  * This method uses the PhantomAppProvider to authenticate via the browser extension or mobile app.
@@ -1419,8 +1290,8 @@ var EmbeddedProvider = class {
1419
1290
  walletId: this.walletId,
1420
1291
  addresses: this.addresses,
1421
1292
  status: "completed",
1422
- providerType: "embedded",
1423
- authUserId: session.authUserId
1293
+ authUserId: session.authUserId,
1294
+ authProvider: session.authProvider
1424
1295
  };
1425
1296
  }
1426
1297
  /*
@@ -1480,10 +1351,10 @@ var EmbeddedProvider = class {
1480
1351
  0 && (module.exports = {
1481
1352
  AUTHENTICATOR_EXPIRATION_TIME_MS,
1482
1353
  AUTHENTICATOR_RENEWAL_WINDOW_MS,
1354
+ EMBEDDED_PROVIDER_AUTH_TYPES,
1483
1355
  EmbeddedEthereumChain,
1484
1356
  EmbeddedProvider,
1485
1357
  EmbeddedSolanaChain,
1486
- JWTAuth,
1487
1358
  generateSessionId,
1488
1359
  retryWithBackoff
1489
1360
  });