@phantom/embedded-provider-core 1.0.6 → 2.0.0-beta.0

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/README.md CHANGED
@@ -61,8 +61,9 @@ const config: EmbeddedProviderConfig = {
61
61
  embeddedWalletType: "user-wallet", // or 'app-wallet'
62
62
  addressTypes: ["solana", "ethereum"],
63
63
  authOptions: {
64
- authUrl: "https://auth.phantom.app",
64
+ authUrl: "https://connect.phantom.app/login/start",
65
65
  redirectUrl: "https://your-app.com/callback",
66
+ authApiBaseUrl: "https://auth.phantom.app",
66
67
  },
67
68
  };
68
69
 
@@ -275,7 +276,8 @@ interface EmbeddedProviderConfig {
275
276
  // Optional
276
277
  authOptions?: {
277
278
  authUrl?: string; // Custom auth URL
278
- redirectUrl?: string; // OAuth redirect URL
279
+ redirectUrl: string?; // OAuth redirect URL
280
+ authUrl?: string; // Custom OAuth URL
279
281
  };
280
282
  }
281
283
  ```
package/dist/index.d.mts CHANGED
@@ -1,8 +1,8 @@
1
- import { AddressType, SpendingLimitError } from '@phantom/client';
1
+ import { AddressType, PresignTransactionContext, SpendingLimitError } from '@phantom/client';
2
2
  import { NetworkId, ClientSideSdkHeaders } from '@phantom/constants';
3
3
  import { ParsedSignatureResult, ParsedTransactionResult } from '@phantom/parsers';
4
4
  import { Algorithm, StamperWithKeyManagement, Transaction, VersionedTransaction } from '@phantom/sdk-types';
5
- import { ISolanaChain, IEthereumChain, EthTransactionRequest } from '@phantom/chain-interfaces';
5
+ import { ISolanaChain, IEthereumChain, SignAndSendTransactionOptions, EthTransactionRequest } from '@phantom/chain-interfaces';
6
6
  import { Logger } from '@phantom/utils';
7
7
  export { Logger as DebugLogger } from '@phantom/utils';
8
8
 
@@ -34,6 +34,7 @@ interface SignTransactionParams {
34
34
  interface SignAndSendTransactionParams {
35
35
  transaction: any;
36
36
  networkId: NetworkId;
37
+ presignTransaction?: (transaction: string, context: PresignTransactionContext) => Promise<string>;
37
38
  }
38
39
  interface SignedTransaction extends ParsedTransactionResult {
39
40
  }
@@ -46,13 +47,9 @@ interface EmbeddedProviderConfig {
46
47
  apiBaseUrl: string;
47
48
  appId: string;
48
49
  authOptions: {
49
- authUrl?: string;
50
- redirectUrl?: string;
51
- };
52
- /** When also provided, the Auth2 PKCE flow is used instead of the legacy Phantom Connect flow. */
53
- unstable__auth2Options?: {
50
+ authUrl: string;
51
+ redirectUrl: string;
54
52
  authApiBaseUrl: string;
55
- clientId: string;
56
53
  };
57
54
  embeddedWalletType: "app-wallet" | "user-wallet" | (string & Record<never, never>);
58
55
  addressTypes: AddressType[];
@@ -224,7 +221,8 @@ declare class EmbeddedProvider {
224
221
  disconnect(shouldClearPreviousSession?: boolean): Promise<void>;
225
222
  /**
226
223
  * Handles errors from signing operations.
227
- * Disconnects the user if the server returns a 401/403 (revoked or expired authenticator).
224
+ * Disconnects the user if the server returns a 401/403 (revoked or expired authenticator)
225
+ * or if the Auth2 token (a2t) has expired.
228
226
  */
229
227
  private handleSigningError;
230
228
  signMessage(params: SignMessageParams): Promise<ParsedSignatureResult>;
@@ -263,7 +261,7 @@ declare class EmbeddedSolanaChain implements ISolanaChain {
263
261
  publicKey: string;
264
262
  }>;
265
263
  signTransaction(transaction: Transaction | VersionedTransaction): Promise<Transaction | VersionedTransaction>;
266
- signAndSendTransaction(transaction: Transaction | VersionedTransaction): Promise<{
264
+ signAndSendTransaction(transaction: Transaction | VersionedTransaction, options?: SignAndSendTransactionOptions): Promise<{
267
265
  signature: string;
268
266
  }>;
269
267
  signAllTransactions(transactions: (Transaction | VersionedTransaction)[]): Promise<(Transaction | VersionedTransaction)[]>;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { AddressType, SpendingLimitError } from '@phantom/client';
1
+ import { AddressType, PresignTransactionContext, SpendingLimitError } from '@phantom/client';
2
2
  import { NetworkId, ClientSideSdkHeaders } from '@phantom/constants';
3
3
  import { ParsedSignatureResult, ParsedTransactionResult } from '@phantom/parsers';
4
4
  import { Algorithm, StamperWithKeyManagement, Transaction, VersionedTransaction } from '@phantom/sdk-types';
5
- import { ISolanaChain, IEthereumChain, EthTransactionRequest } from '@phantom/chain-interfaces';
5
+ import { ISolanaChain, IEthereumChain, SignAndSendTransactionOptions, EthTransactionRequest } from '@phantom/chain-interfaces';
6
6
  import { Logger } from '@phantom/utils';
7
7
  export { Logger as DebugLogger } from '@phantom/utils';
8
8
 
@@ -34,6 +34,7 @@ interface SignTransactionParams {
34
34
  interface SignAndSendTransactionParams {
35
35
  transaction: any;
36
36
  networkId: NetworkId;
37
+ presignTransaction?: (transaction: string, context: PresignTransactionContext) => Promise<string>;
37
38
  }
38
39
  interface SignedTransaction extends ParsedTransactionResult {
39
40
  }
@@ -46,13 +47,9 @@ interface EmbeddedProviderConfig {
46
47
  apiBaseUrl: string;
47
48
  appId: string;
48
49
  authOptions: {
49
- authUrl?: string;
50
- redirectUrl?: string;
51
- };
52
- /** When also provided, the Auth2 PKCE flow is used instead of the legacy Phantom Connect flow. */
53
- unstable__auth2Options?: {
50
+ authUrl: string;
51
+ redirectUrl: string;
54
52
  authApiBaseUrl: string;
55
- clientId: string;
56
53
  };
57
54
  embeddedWalletType: "app-wallet" | "user-wallet" | (string & Record<never, never>);
58
55
  addressTypes: AddressType[];
@@ -224,7 +221,8 @@ declare class EmbeddedProvider {
224
221
  disconnect(shouldClearPreviousSession?: boolean): Promise<void>;
225
222
  /**
226
223
  * Handles errors from signing operations.
227
- * Disconnects the user if the server returns a 401/403 (revoked or expired authenticator).
224
+ * Disconnects the user if the server returns a 401/403 (revoked or expired authenticator)
225
+ * or if the Auth2 token (a2t) has expired.
228
226
  */
229
227
  private handleSigningError;
230
228
  signMessage(params: SignMessageParams): Promise<ParsedSignatureResult>;
@@ -263,7 +261,7 @@ declare class EmbeddedSolanaChain implements ISolanaChain {
263
261
  publicKey: string;
264
262
  }>;
265
263
  signTransaction(transaction: Transaction | VersionedTransaction): Promise<Transaction | VersionedTransaction>;
266
- signAndSendTransaction(transaction: Transaction | VersionedTransaction): Promise<{
264
+ signAndSendTransaction(transaction: Transaction | VersionedTransaction, options?: SignAndSendTransactionOptions): Promise<{
267
265
  signature: string;
268
266
  }>;
269
267
  signAllTransactions(transactions: (Transaction | VersionedTransaction)[]): Promise<(Transaction | VersionedTransaction)[]>;
package/dist/index.js CHANGED
@@ -105,11 +105,12 @@ var EmbeddedSolanaChain = class {
105
105
  }
106
106
  return signedTransaction;
107
107
  }
108
- async signAndSendTransaction(transaction) {
108
+ async signAndSendTransaction(transaction, options) {
109
109
  this.ensureConnected();
110
110
  const result = await this.provider.signAndSendTransaction({
111
111
  transaction,
112
- networkId: this.currentNetworkId
112
+ networkId: this.currentNetworkId,
113
+ presignTransaction: options?.presignTransaction
113
114
  });
114
115
  if (!result.hash) {
115
116
  throw new Error("Transaction not submitted");
@@ -410,6 +411,7 @@ function generateSessionId() {
410
411
  }
411
412
 
412
413
  // src/embedded-provider.ts
414
+ var import_auth2 = require("@phantom/auth2");
413
415
  var noopLogger = {
414
416
  info: () => {
415
417
  },
@@ -893,7 +895,8 @@ var EmbeddedProvider = class {
893
895
  }
894
896
  /**
895
897
  * Handles errors from signing operations.
896
- * Disconnects the user if the server returns a 401/403 (revoked or expired authenticator).
898
+ * Disconnects the user if the server returns a 401/403 (revoked or expired authenticator)
899
+ * or if the Auth2 token (a2t) has expired.
897
900
  */
898
901
  async handleSigningError(error) {
899
902
  if ((0, import_client.isAuthenticationError)(error)) {
@@ -901,6 +904,11 @@ var EmbeddedProvider = class {
901
904
  await this.disconnect(false);
902
905
  throw new Error("Authenticator revoked");
903
906
  }
907
+ if (error instanceof Error && error.name === import_auth2.Auth2TokenExpiredError.name) {
908
+ this.logger.warn("EMBEDDED_PROVIDER", "Auth2 token expired, disconnecting", { error });
909
+ await this.disconnect(false);
910
+ throw error;
911
+ }
904
912
  if (error instanceof import_client.SpendingLimitError) {
905
913
  this.emit("spending_limit_reached", { error });
906
914
  }
@@ -1053,7 +1061,8 @@ var EmbeddedProvider = class {
1053
1061
  transaction: transactionPayload,
1054
1062
  networkId: params.networkId,
1055
1063
  derivationIndex,
1056
- account
1064
+ account,
1065
+ presignTransaction: params.presignTransaction
1057
1066
  }).catch((error) => this.handleSigningError(error));
1058
1067
  this.logger.info("EMBEDDED_PROVIDER", "Transaction signed and sent successfully", {
1059
1068
  walletId: this.walletId,
@@ -1348,15 +1357,18 @@ var EmbeddedProvider = class {
1348
1357
  if (!this.stamper.getKeyInfo()) {
1349
1358
  await this.stamper.init();
1350
1359
  }
1360
+ const authHeaderStamper = this.stamper;
1351
1361
  this.client = new import_client.PhantomClient(
1352
1362
  {
1353
1363
  apiBaseUrl: this.config.apiBaseUrl,
1354
1364
  organizationId: session.organizationId,
1355
1365
  headers: {
1356
- ...this.platform.analyticsHeaders || {},
1357
- ...session.authUserId ? { "x-auth-user-id": session.authUserId } : {},
1358
- ...session.bearerToken ? { Authorization: session.bearerToken } : {}
1359
- }
1366
+ ...this.platform.analyticsHeaders || {}
1367
+ },
1368
+ getHeaders: () => ({
1369
+ authorization: authHeaderStamper.bearerToken ?? session.bearerToken,
1370
+ "x-auth-user-id": authHeaderStamper.auth2Token?.sub ?? session.authUserId
1371
+ })
1360
1372
  },
1361
1373
  this.stamper
1362
1374
  );