@turnkey/core 1.2.0 → 1.4.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.
Files changed (53) hide show
  1. package/dist/__clients__/core.d.ts +57 -2
  2. package/dist/__clients__/core.d.ts.map +1 -1
  3. package/dist/__clients__/core.js +210 -123
  4. package/dist/__clients__/core.js.map +1 -1
  5. package/dist/__clients__/core.mjs +212 -125
  6. package/dist/__clients__/core.mjs.map +1 -1
  7. package/dist/__generated__/sdk-client-base.d.ts +2 -2
  8. package/dist/__generated__/sdk-client-base.d.ts.map +1 -1
  9. package/dist/__generated__/sdk-client-base.js +23 -3
  10. package/dist/__generated__/sdk-client-base.js.map +1 -1
  11. package/dist/__generated__/sdk-client-base.mjs +23 -3
  12. package/dist/__generated__/sdk-client-base.mjs.map +1 -1
  13. package/dist/__generated__/version.d.ts +1 -1
  14. package/dist/__generated__/version.js +1 -1
  15. package/dist/__generated__/version.mjs +1 -1
  16. package/dist/__stampers__/passkey/base.d.ts +1 -3
  17. package/dist/__stampers__/passkey/base.d.ts.map +1 -1
  18. package/dist/__stampers__/passkey/base.js.map +1 -1
  19. package/dist/__stampers__/passkey/base.mjs.map +1 -1
  20. package/dist/__types__/config.d.ts +5 -0
  21. package/dist/__types__/config.d.ts.map +1 -1
  22. package/dist/__types__/http.d.ts +17 -0
  23. package/dist/__types__/http.d.ts.map +1 -0
  24. package/dist/__types__/http.js +11 -0
  25. package/dist/__types__/http.js.map +1 -0
  26. package/dist/__types__/http.mjs +9 -0
  27. package/dist/__types__/http.mjs.map +1 -0
  28. package/dist/__types__/index.d.ts +1 -0
  29. package/dist/__types__/index.d.ts.map +1 -1
  30. package/dist/__types__/method-types.d.ts +24 -2
  31. package/dist/__types__/method-types.d.ts.map +1 -1
  32. package/dist/__wallet__/web/manager.d.ts +10 -0
  33. package/dist/__wallet__/web/manager.d.ts.map +1 -1
  34. package/dist/__wallet__/web/manager.js +41 -2
  35. package/dist/__wallet__/web/manager.js.map +1 -1
  36. package/dist/__wallet__/web/manager.mjs +41 -2
  37. package/dist/__wallet__/web/manager.mjs.map +1 -1
  38. package/dist/__wallet__/web/native/solana.js +2 -2
  39. package/dist/__wallet__/web/native/solana.js.map +1 -1
  40. package/dist/__wallet__/web/native/solana.mjs +2 -2
  41. package/dist/__wallet__/web/native/solana.mjs.map +1 -1
  42. package/dist/index.d.ts +2 -1
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +3 -1
  45. package/dist/index.js.map +1 -1
  46. package/dist/index.mjs +2 -2
  47. package/dist/utils.d.ts +16 -1
  48. package/dist/utils.d.ts.map +1 -1
  49. package/dist/utils.js +69 -1
  50. package/dist/utils.js.map +1 -1
  51. package/dist/utils.mjs +68 -4
  52. package/dist/utils.mjs.map +1 -1
  53. package/package.json +5 -6
@@ -1,8 +1,8 @@
1
1
  import { TurnkeySDKClientBase } from '../__generated__/sdk-client-base.mjs';
2
- import { TurnkeyErrorCodes, TurnkeyError, TurnkeyNetworkError, AuthAction } from '@turnkey/sdk-types';
2
+ import { TurnkeyErrorCodes, TurnkeyError, AuthAction } from '@turnkey/sdk-types';
3
3
  import { DEFAULT_SESSION_EXPIRATION_IN_SECONDS } from '../__types__/auth.mjs';
4
4
  import { SessionKey, StamperType, WalletSource, Chain, FilterType, OtpTypeToFilterTypeMap, OtpType, Curve, SignIntent } from '../__types__/enums.mjs';
5
- import { withTurnkeyErrorHandling, isValidPasskeyName, isWeb, isReactNative, buildSignUpBody, findWalletProviderFromAddress, addressFromPublicKey, getCurveTypeFromProvider, getPublicKeyFromStampHeader, mapAccountsToWallet, toExternalTimestamp, getAuthenticatorAddresses, isEthereumProvider, isSolanaProvider, getActiveSessionOrThrowIfRequired, getHashFunction, getEncodingType, getEncodedMessage, splitSignature, broadcastTransaction, getPolicySignature, googleISS, isWalletAccountArray, generateWalletAccountsFromAddressFormat } from '../utils.mjs';
5
+ import { withTurnkeyErrorHandling, isValidPasskeyName, isWeb, isReactNative, buildSignUpBody, findWalletProviderFromAddress, getPublicKeyFromStampHeader, addressFromPublicKey, getCurveTypeFromProvider, sendSignedRequest, fetchAllWalletAccountsWithCursor, mapAccountsToWallet, toExternalTimestamp, getAuthenticatorAddresses, isEthereumProvider, isSolanaProvider, getActiveSessionOrThrowIfRequired, getHashFunction, getEncodingType, getEncodedMessage, splitSignature, broadcastTransaction, getPolicySignature, googleISS, isWalletAccountArray, generateWalletAccountsFromAddressFormat } from '../utils.mjs';
6
6
  import { createStorageManager } from '../__storage__/base.mjs';
7
7
  import { CrossPlatformApiKeyStamper } from '../__stampers__/api/base.mjs';
8
8
  import { CrossPlatformPasskeyStamper } from '../__stampers__/passkey/base.mjs';
@@ -15,6 +15,41 @@ class TurnkeyClient {
15
15
  constructor(config,
16
16
  // Users can pass in their own stampers, or we will create them. Should we remove this?
17
17
  apiKeyStamper, passkeyStamper, walletManager) {
18
+ /**
19
+ * Creates a new TurnkeySDKClientBase instance with the provided configuration.
20
+ * This method is used internally to create the HTTP client for making API requests,
21
+ * but can also be used to create an additional client with different configurations if needed.
22
+ * By default, it uses the configuration provided during the TurnkeyClient initialization.
23
+ *
24
+ * @param params - Optional configuration parameters to override the default client configuration.
25
+ * @param params.apiBaseUrl - The base URL of the Turnkey API (defaults to `https://api.turnkey.com` if not provided).
26
+ * @param params.organizationId - The organization ID to associate requests with.
27
+ * @param params.authProxyUrl - The base URL of the Auth Proxy (defaults to `https://authproxy.turnkey.com` if not provided).
28
+ * @param params.authProxyConfigId - The configuration ID to use when making Auth Proxy requests.
29
+ * @param params.defaultStamperType - The default stamper type to use for signing requests
30
+ * (overrides automatic detection of ApiKey, Passkey, or Wallet stampers).
31
+ *
32
+ * @returns A new instance of {@link TurnkeySDKClientBase} configured with the provided parameters.
33
+ */
34
+ this.createHttpClient = (params) => {
35
+ // We can comfortably default to the prod urls here
36
+ const apiBaseUrl = params?.apiBaseUrl || this.config.apiBaseUrl || "https://api.turnkey.com";
37
+ const authProxyUrl = params?.authProxyUrl ||
38
+ this.config.authProxyUrl ||
39
+ "https://authproxy.turnkey.com";
40
+ const organizationId = params?.organizationId || this.config.organizationId;
41
+ return new TurnkeySDKClientBase({
42
+ ...this.config,
43
+ ...params,
44
+ apiBaseUrl,
45
+ authProxyUrl,
46
+ organizationId,
47
+ apiKeyStamper: this.apiKeyStamper,
48
+ passkeyStamper: this.passkeyStamper,
49
+ walletStamper: this.walletManager?.stamper,
50
+ storageManager: this.storageManager,
51
+ });
52
+ };
18
53
  /**
19
54
  * Creates a new passkey authenticator for the user.
20
55
  *
@@ -377,6 +412,105 @@ class TurnkeyClient {
377
412
  errorCode: TurnkeyErrorCodes.SWITCH_WALLET_CHAIN_ERROR,
378
413
  });
379
414
  };
415
+ /**
416
+ * Builds and signs a wallet login request without submitting it to Turnkey.
417
+ *
418
+ * - This function prepares a signed request for wallet authentication, which can later be used
419
+ * to log in or sign up a user with Turnkey.
420
+ * - It initializes the wallet stamper, ensures a valid session public key (generating one if needed),
421
+ * and signs the login intent with the connected wallet.
422
+ * - For Ethereum wallets, derives the public key from the stamped request header.
423
+ * - For Solana wallets, retrieves the public key directly from the connected wallet.
424
+ * - The signed request is not sent to Turnkey immediately; it is meant to be used in a subsequent flow
425
+ * (e.g., `loginOrSignupWithWallet`) where sub-organization existence is verified or created first.
426
+ *
427
+ * @param params.walletProvider - the wallet provider used for authentication and signing.
428
+ * @param params.publicKey - optional pre-generated session public key (auto-generated if not provided).
429
+ * @param params.expirationSeconds - optional session expiration time in seconds (defaults to the configured default).
430
+ * @returns A promise resolving to an object containing:
431
+ * - `signedRequest`: the signed wallet login request.
432
+ * - `publicKey`: the public key associated with the signed request.
433
+ * @throws {TurnkeyError} If the wallet stamper is not initialized, the signing process fails,
434
+ * or the public key cannot be derived or generated.
435
+ */
436
+ this.buildWalletLoginRequest = async (params) => {
437
+ const { walletProvider, publicKey: providedPublicKey } = params;
438
+ const expirationSeconds = params.expirationSeconds || DEFAULT_SESSION_EXPIRATION_IN_SECONDS;
439
+ let generatedPublicKey = undefined;
440
+ return withTurnkeyErrorHandling(async () => {
441
+ if (!this.walletManager?.stamper) {
442
+ throw new TurnkeyError("Wallet stamper is not initialized", TurnkeyErrorCodes.WALLET_MANAGER_COMPONENT_NOT_INITIALIZED);
443
+ }
444
+ const futureSessionPublicKey = providedPublicKey ??
445
+ (generatedPublicKey = await this.apiKeyStamper?.createKeyPair());
446
+ if (!futureSessionPublicKey) {
447
+ throw new TurnkeyError("Failed to find or generate a public key for building the wallet login request", TurnkeyErrorCodes.WALLET_BUILD_LOGIN_REQUEST_ERROR);
448
+ }
449
+ this.walletManager.stamper.setProvider(walletProvider.interfaceType, walletProvider);
450
+ // here we sign the request with the wallet, but we don't send it to Turnkey yet
451
+ // this is because we need to check if the subOrg exists first, and create one if it doesn't
452
+ // once we have the subOrg for the publicKey, we then can send the request to Turnkey
453
+ const signedRequest = await withTurnkeyErrorHandling(async () => {
454
+ return this.httpClient.stampStampLogin({
455
+ publicKey: futureSessionPublicKey,
456
+ organizationId: this.config.organizationId,
457
+ expirationSeconds,
458
+ }, StamperType.Wallet);
459
+ }, {
460
+ errorMessage: "Failed to create stamped request for wallet login",
461
+ errorCode: TurnkeyErrorCodes.WALLET_BUILD_LOGIN_REQUEST_ERROR,
462
+ customErrorsByMessages: {
463
+ "WalletConnect: The connection request has expired. Please scan the QR code again.": {
464
+ message: "Your WalletConnect session expired. Please scan the QR code again.",
465
+ code: TurnkeyErrorCodes.WALLET_CONNECT_EXPIRED,
466
+ },
467
+ "Failed to sign the message": {
468
+ message: "Wallet auth was cancelled by the user.",
469
+ code: TurnkeyErrorCodes.CONNECT_WALLET_CANCELLED,
470
+ },
471
+ },
472
+ });
473
+ if (!signedRequest) {
474
+ throw new TurnkeyError("Failed to create stamped request for wallet login", TurnkeyErrorCodes.BAD_RESPONSE);
475
+ }
476
+ let publicKey;
477
+ switch (walletProvider.chainInfo.namespace) {
478
+ case Chain.Ethereum: {
479
+ // for Ethereum, there is no way to get the public key from the wallet address
480
+ // so we derive it from the signed request
481
+ publicKey = getPublicKeyFromStampHeader(signedRequest.stamp.stampHeaderValue);
482
+ break;
483
+ }
484
+ case Chain.Solana: {
485
+ // for Solana, we can get the public key from the wallet address
486
+ // since the wallet address is the public key
487
+ // this doesn't require any action from the user as long as the wallet is connected
488
+ // which it has to be since they just called stampStampLogin()
489
+ publicKey = await this.walletManager.stamper.getPublicKey(walletProvider.interfaceType, walletProvider);
490
+ break;
491
+ }
492
+ default:
493
+ throw new TurnkeyError(`Unsupported interface type: ${walletProvider.interfaceType}`, TurnkeyErrorCodes.INVALID_REQUEST);
494
+ }
495
+ return {
496
+ signedRequest,
497
+ publicKey: publicKey,
498
+ };
499
+ }, {
500
+ errorCode: TurnkeyErrorCodes.WALLET_BUILD_LOGIN_REQUEST_ERROR,
501
+ errorMessage: "Failed to build wallet login request",
502
+ catchFn: async () => {
503
+ if (generatedPublicKey) {
504
+ try {
505
+ await this.apiKeyStamper?.deleteKeyPair(generatedPublicKey);
506
+ }
507
+ catch (cleanupError) {
508
+ throw new TurnkeyError(`Failed to clean up generated key pair`, TurnkeyErrorCodes.KEY_PAIR_CLEANUP_ERROR, cleanupError);
509
+ }
510
+ }
511
+ },
512
+ });
513
+ };
380
514
  /**
381
515
  * Logs in a user using the specified wallet provider.
382
516
  *
@@ -397,7 +531,7 @@ class TurnkeyClient {
397
531
  * @throws {TurnkeyError} If the wallet stamper is uninitialized, a public key cannot be found or generated, or login fails.
398
532
  */
399
533
  this.loginWithWallet = async (params) => {
400
- let publicKey = params.publicKey || (await this.apiKeyStamper?.createKeyPair());
534
+ const publicKey = params.publicKey || (await this.apiKeyStamper?.createKeyPair());
401
535
  return withTurnkeyErrorHandling(async () => {
402
536
  if (!this.walletManager?.stamper) {
403
537
  throw new TurnkeyError("Wallet stamper is not initialized", TurnkeyErrorCodes.WALLET_MANAGER_COMPONENT_NOT_INITIALIZED);
@@ -544,6 +678,7 @@ class TurnkeyClient {
544
678
  * - Stores the resulting session token under the specified session key, or the default session key if not provided.
545
679
  *
546
680
  * @param params.walletProvider - wallet provider to use for authentication.
681
+ * @param params.publicKey - optional public key to associate with the session (generated if not provided).
547
682
  * @param params.createSubOrgParams - optional parameters for creating a sub-organization (e.g., authenticators, user metadata).
548
683
  * @param params.sessionKey - session key to use for storing the session (defaults to the default session key).
549
684
  * @param params.expirationSeconds - session expiration time in seconds (defaults to the configured default).
@@ -558,59 +693,8 @@ class TurnkeyClient {
558
693
  const createSubOrgParams = params.createSubOrgParams;
559
694
  const sessionKey = params.sessionKey || SessionKey.DefaultSessionkey;
560
695
  const walletProvider = params.walletProvider;
561
- const expirationSeconds = params.expirationSeconds || DEFAULT_SESSION_EXPIRATION_IN_SECONDS;
562
- let generatedPublicKey = undefined;
563
696
  return withTurnkeyErrorHandling(async () => {
564
- if (!this.walletManager?.stamper) {
565
- throw new TurnkeyError("Wallet stamper is not initialized", TurnkeyErrorCodes.WALLET_MANAGER_COMPONENT_NOT_INITIALIZED);
566
- }
567
- generatedPublicKey = await this.apiKeyStamper?.createKeyPair();
568
- this.walletManager.stamper.setProvider(walletProvider.interfaceType, walletProvider);
569
- // here we sign the request with the wallet, but we don't send it to Turnkey yet
570
- // this is because we need to check if the subOrg exists first, and create one if it doesn't
571
- // once we have the subOrg for the publicKey, we then can send the request to Turnkey
572
- const signedRequest = await withTurnkeyErrorHandling(async () => {
573
- return this.httpClient.stampStampLogin({
574
- publicKey: generatedPublicKey,
575
- organizationId: this.config.organizationId,
576
- expirationSeconds,
577
- }, StamperType.Wallet);
578
- }, {
579
- errorMessage: "Failed to create stamped request for wallet login",
580
- errorCode: TurnkeyErrorCodes.WALLET_LOGIN_OR_SIGNUP_ERROR,
581
- customErrorsByMessages: {
582
- "WalletConnect: The connection request has expired. Please scan the QR code again.": {
583
- message: "Your WalletConnect session expired. Please scan the QR code again.",
584
- code: TurnkeyErrorCodes.WALLET_CONNECT_EXPIRED,
585
- },
586
- "Failed to sign the message": {
587
- message: "Wallet auth was cancelled by the user.",
588
- code: TurnkeyErrorCodes.CONNECT_WALLET_CANCELLED,
589
- },
590
- },
591
- });
592
- if (!signedRequest) {
593
- throw new TurnkeyError("Failed to create stamped request for wallet login", TurnkeyErrorCodes.BAD_RESPONSE);
594
- }
595
- let publicKey;
596
- switch (walletProvider.chainInfo.namespace) {
597
- case Chain.Ethereum: {
598
- // for Ethereum, there is no way to get the public key from the wallet address
599
- // so we derive it from the signed request
600
- publicKey = getPublicKeyFromStampHeader(signedRequest.stamp.stampHeaderValue);
601
- break;
602
- }
603
- case Chain.Solana: {
604
- // for Solana, we can get the public key from the wallet address
605
- // since the wallet address is the public key
606
- // this doesn't require any action from the user as long as the wallet is connected
607
- // which it has to be since they just called stampStampLogin()
608
- publicKey = await this.walletManager.stamper.getPublicKey(walletProvider.interfaceType, walletProvider);
609
- break;
610
- }
611
- default:
612
- throw new TurnkeyError(`Unsupported interface type: ${walletProvider.interfaceType}`, TurnkeyErrorCodes.INVALID_REQUEST);
613
- }
697
+ const { signedRequest, publicKey } = await this.buildWalletLoginRequest(params);
614
698
  // here we check if the subOrg exists and create one
615
699
  // then we send off the stamped request to Turnkey
616
700
  const accountRes = await this.httpClient.proxyGetAccount({
@@ -641,20 +725,7 @@ class TurnkeyClient {
641
725
  }
642
726
  }
643
727
  // now we can send the stamped request to Turnkey
644
- const headers = {
645
- "Content-Type": "application/json",
646
- [signedRequest.stamp.stampHeaderName]: signedRequest.stamp.stampHeaderValue,
647
- };
648
- const res = await fetch(signedRequest.url, {
649
- method: "POST",
650
- headers,
651
- body: signedRequest.body,
652
- });
653
- if (!res.ok) {
654
- const errorText = await res.text();
655
- throw new TurnkeyNetworkError(`Stamped request failed`, res.status, TurnkeyErrorCodes.WALLET_LOGIN_AUTH_ERROR, errorText);
656
- }
657
- const sessionResponse = await res.json();
728
+ const sessionResponse = await sendSignedRequest(signedRequest);
658
729
  const sessionToken = sessionResponse.activity.result.stampLoginResult?.session;
659
730
  if (!sessionToken) {
660
731
  throw new TurnkeyError("Session token not found in the response", TurnkeyErrorCodes.BAD_RESPONSE);
@@ -673,14 +744,6 @@ class TurnkeyClient {
673
744
  errorCode: TurnkeyErrorCodes.WALLET_LOGIN_OR_SIGNUP_ERROR,
674
745
  errorMessage: "Failed to log in or sign up with wallet",
675
746
  catchFn: async () => {
676
- if (generatedPublicKey) {
677
- try {
678
- await this.apiKeyStamper?.deleteKeyPair(generatedPublicKey);
679
- }
680
- catch (cleanupError) {
681
- throw new TurnkeyError(`Failed to clean up generated key pair`, TurnkeyErrorCodes.KEY_PAIR_CLEANUP_ERROR, cleanupError);
682
- }
683
- }
684
747
  },
685
748
  });
686
749
  };
@@ -1123,7 +1186,7 @@ class TurnkeyClient {
1123
1186
  * @throws {TurnkeyError} If no active session is found or if there is an error fetching wallets.
1124
1187
  */
1125
1188
  this.fetchWallets = async (params) => {
1126
- const { walletProviders, organizationId: organizationIdFromParams, userId: userIdFromParams, connectedOnly, stampWith, } = params || {};
1189
+ const { walletProviders, organizationId: organizationIdFromParams, userId: userIdFromParams, connectedOnly, stampWith = this.config.defaultStamperType, } = params || {};
1127
1190
  const session = await this.storageManager.getActiveSession();
1128
1191
  if (!session && !connectedOnly) {
1129
1192
  throw new TurnkeyError("No active session found. Fetching embedded wallets requires a valid session. If you only need connected wallets, set the 'connectedOnly' parameter to true.", TurnkeyErrorCodes.NO_SESSION_FOUND);
@@ -1145,16 +1208,10 @@ class TurnkeyClient {
1145
1208
  if (!userId) {
1146
1209
  throw new TurnkeyError("No user ID provided and no active session found. Please log in first or pass in a user ID.", TurnkeyErrorCodes.INVALID_REQUEST);
1147
1210
  }
1148
- const res = await this.httpClient.getWalletAccounts({
1149
- organizationId,
1150
- includeWalletDetails: true,
1151
- }, stampWith);
1211
+ const accounts = await fetchAllWalletAccountsWithCursor(this.httpClient, organizationId, stampWith);
1152
1212
  const walletsRes = await this.httpClient.getWallets({
1153
1213
  organizationId,
1154
1214
  }, stampWith);
1155
- if (!res || !res.accounts) {
1156
- throw new TurnkeyError("No wallet accounts found in the response", TurnkeyErrorCodes.BAD_RESPONSE);
1157
- }
1158
1215
  // create a map of walletId to EmbeddedWallet for easy lookup
1159
1216
  const walletMap = new Map(walletsRes.wallets.map((wallet) => [
1160
1217
  wallet.walletId,
@@ -1165,7 +1222,7 @@ class TurnkeyClient {
1165
1222
  },
1166
1223
  ]));
1167
1224
  // map the accounts to their respective wallets
1168
- embedded = mapAccountsToWallet(res.accounts, walletMap);
1225
+ embedded = mapAccountsToWallet(accounts, walletMap);
1169
1226
  }
1170
1227
  // if wallet connecting is disabled we return only embedded wallets
1171
1228
  // this will never be hit if `connectedOnly` is true because of the check above
@@ -1238,7 +1295,7 @@ class TurnkeyClient {
1238
1295
  * @throws {TurnkeyError} If no active session is found or if there is an error fetching wallet accounts.
1239
1296
  */
1240
1297
  this.fetchWalletAccounts = async (params) => {
1241
- const { wallet, stampWith, walletProviders, paginationOptions } = params;
1298
+ const { wallet, stampWith = this.config.defaultStamperType, walletProviders, paginationOptions, } = params;
1242
1299
  const session = await this.storageManager.getActiveSession();
1243
1300
  const organizationId = params?.organizationId || session?.organizationId;
1244
1301
  const userId = params?.userId || session?.userId;
@@ -1367,7 +1424,7 @@ class TurnkeyClient {
1367
1424
  * @throws {TurnkeyError} If no active session is found or if there is an error fetching private keys.
1368
1425
  */
1369
1426
  this.fetchPrivateKeys = async (params) => {
1370
- const { stampWith } = params || {};
1427
+ const { stampWith = this.config.defaultStamperType } = params || {};
1371
1428
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
1372
1429
  const organizationId = params?.organizationId || session?.organizationId;
1373
1430
  if (!organizationId) {
@@ -1423,7 +1480,7 @@ class TurnkeyClient {
1423
1480
  * @throws {TurnkeyError} If signing fails, the wallet type does not support message signing, or the response is invalid.
1424
1481
  */
1425
1482
  this.signMessage = async (params) => {
1426
- const { message, walletAccount, stampWith, addEthereumPrefix, organizationId, } = params;
1483
+ const { message, walletAccount, stampWith = this.config.defaultStamperType, addEthereumPrefix, organizationId, } = params;
1427
1484
  const hashFunction = params.hashFunction || getHashFunction(walletAccount.addressFormat);
1428
1485
  const payloadEncoding = params.encoding || getEncodingType(walletAccount.addressFormat);
1429
1486
  return withTurnkeyErrorHandling(async () => {
@@ -1494,7 +1551,7 @@ class TurnkeyClient {
1494
1551
  * @throws {TurnkeyError} If the wallet type is unsupported, signing fails, or the response is invalid.
1495
1552
  */
1496
1553
  this.signTransaction = async (params) => {
1497
- const { walletAccount, unsignedTransaction, transactionType, stampWith, organizationId, } = params;
1554
+ const { walletAccount, unsignedTransaction, transactionType, stampWith = this.config.defaultStamperType, organizationId, } = params;
1498
1555
  return withTurnkeyErrorHandling(async () => {
1499
1556
  if (walletAccount.source === WalletSource.Connected) {
1500
1557
  switch (walletAccount.chainInfo.namespace) {
@@ -1549,7 +1606,7 @@ class TurnkeyClient {
1549
1606
  * @throws {TurnkeyError} If the wallet type is unsupported, or if signing/broadcasting fails.
1550
1607
  */
1551
1608
  this.signAndSendTransaction = async (params) => {
1552
- const { walletAccount, unsignedTransaction, transactionType, rpcUrl, stampWith, organizationId, } = params;
1609
+ const { walletAccount, unsignedTransaction, transactionType, rpcUrl, stampWith = this.config.defaultStamperType, organizationId, } = params;
1553
1610
  return withTurnkeyErrorHandling(async () => {
1554
1611
  if (walletAccount.source === WalletSource.Connected) {
1555
1612
  // this is a connected wallet account
@@ -1614,7 +1671,7 @@ class TurnkeyClient {
1614
1671
  * @throws {TurnkeyError} If there is no active session, if there is no userId, or if there is an error fetching user details.
1615
1672
  */
1616
1673
  this.fetchUser = async (params) => {
1617
- const { organizationId: organizationIdFromParams, userId: userIdFromParams, stampWith, } = params || {};
1674
+ const { organizationId: organizationIdFromParams, userId: userIdFromParams, stampWith = this.config.defaultStamperType, } = params || {};
1618
1675
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
1619
1676
  const userId = userIdFromParams || session?.userId;
1620
1677
  if (!userId) {
@@ -1652,7 +1709,7 @@ class TurnkeyClient {
1652
1709
  * @throws {TurnkeyError} If there is no active session, if the input is invalid, if user retrieval fails, or if user creation fails.
1653
1710
  */
1654
1711
  this.fetchOrCreateP256ApiKeyUser = async (params) => {
1655
- const { publicKey, createParams, stampWith, organizationId: organizationIdFromParams, } = params;
1712
+ const { publicKey, createParams, stampWith = this.config.defaultStamperType, organizationId: organizationIdFromParams, } = params;
1656
1713
  return withTurnkeyErrorHandling(async () => {
1657
1714
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
1658
1715
  const organizationId = organizationIdFromParams || session?.organizationId;
@@ -1734,7 +1791,7 @@ class TurnkeyClient {
1734
1791
  * if fetching policies fails, or if creating policies fails.
1735
1792
  */
1736
1793
  this.fetchOrCreatePolicies = async (params) => {
1737
- const { policies, stampWith } = params;
1794
+ const { policies, stampWith = this.config.defaultStamperType } = params;
1738
1795
  return await withTurnkeyErrorHandling(async () => {
1739
1796
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
1740
1797
  if (!Array.isArray(policies) || policies.length === 0) {
@@ -1817,7 +1874,7 @@ class TurnkeyClient {
1817
1874
  * @throws {TurnkeyError} If there is no active session, if the userId is missing, or if there is an error updating or verifying the user email.
1818
1875
  */
1819
1876
  this.updateUserEmail = async (params) => {
1820
- const { verificationToken, email, stampWith, organizationId } = params;
1877
+ const { verificationToken, email, stampWith = this.config.defaultStamperType, organizationId, } = params;
1821
1878
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
1822
1879
  const userId = params?.userId || session?.userId;
1823
1880
  if (!userId) {
@@ -1861,7 +1918,7 @@ class TurnkeyClient {
1861
1918
  * @throws {TurnkeyError} If there is no active session, if the userId is missing, or if there is an error removing the user email.
1862
1919
  */
1863
1920
  this.removeUserEmail = async (params) => {
1864
- const { stampWith, organizationId } = params || {};
1921
+ const { stampWith = this.config.defaultStamperType, organizationId } = params || {};
1865
1922
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
1866
1923
  return withTurnkeyErrorHandling(async () => {
1867
1924
  const userId = params?.userId || session?.userId;
@@ -1900,7 +1957,7 @@ class TurnkeyClient {
1900
1957
  * @throws {TurnkeyError} If there is no active session, if the userId is missing, or if there is an error updating or verifying the user phone number.
1901
1958
  */
1902
1959
  this.updateUserPhoneNumber = async (params) => {
1903
- const { verificationToken, phoneNumber, stampWith, organizationId } = params;
1960
+ const { verificationToken, phoneNumber, stampWith = this.config.defaultStamperType, organizationId, } = params;
1904
1961
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
1905
1962
  const userId = params?.userId || session?.userId;
1906
1963
  if (!userId) {
@@ -1937,7 +1994,7 @@ class TurnkeyClient {
1937
1994
  * @throws {TurnkeyError} If there is no active session, if the userId is missing, or if there is an error removing the user phone number.
1938
1995
  */
1939
1996
  this.removeUserPhoneNumber = async (params) => {
1940
- const { stampWith, organizationId } = params || {};
1997
+ const { stampWith = this.config.defaultStamperType, organizationId } = params || {};
1941
1998
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
1942
1999
  const userId = params?.userId || session?.userId;
1943
2000
  if (!userId) {
@@ -1975,7 +2032,7 @@ class TurnkeyClient {
1975
2032
  * @throws {TurnkeyError} If there is no active session, if the userId is missing, or if there is an error updating the user name.
1976
2033
  */
1977
2034
  this.updateUserName = async (params) => {
1978
- const { userName, stampWith, organizationId } = params;
2035
+ const { userName, stampWith = this.config.defaultStamperType, organizationId, } = params;
1979
2036
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
1980
2037
  const userId = params?.userId || session?.userId;
1981
2038
  if (!userId) {
@@ -2015,7 +2072,7 @@ class TurnkeyClient {
2015
2072
  * @throws {TurnkeyError} If there is no active session, if the account already exists, or if there is an error adding the OAuth provider.
2016
2073
  */
2017
2074
  this.addOauthProvider = async (params) => {
2018
- const { providerName, oidcToken, stampWith } = params;
2075
+ const { providerName, oidcToken, stampWith = this.config.defaultStamperType, } = params;
2019
2076
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
2020
2077
  return withTurnkeyErrorHandling(async () => {
2021
2078
  const accountRes = await this.httpClient.proxyGetAccount({
@@ -2091,7 +2148,7 @@ class TurnkeyClient {
2091
2148
  * @throws {TurnkeyError} If there is no active session, if the userId is missing, or if there is an error removing the OAuth provider.
2092
2149
  */
2093
2150
  this.removeOauthProviders = async (params) => {
2094
- const { providerIds, stampWith, organizationId } = params;
2151
+ const { providerIds, stampWith = this.config.defaultStamperType, organizationId, } = params;
2095
2152
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
2096
2153
  const userId = params?.userId || session?.userId;
2097
2154
  if (!userId) {
@@ -2130,7 +2187,7 @@ class TurnkeyClient {
2130
2187
  * @throws {TurnkeyError} If there is no active session, if passkey creation fails, or if there is an error adding the passkey.
2131
2188
  */
2132
2189
  this.addPasskey = async (params) => {
2133
- const { stampWith, organizationId } = params || {};
2190
+ const { stampWith = this.config.defaultStamperType, organizationId } = params || {};
2134
2191
  const name = params?.name || `Turnkey Passkey-${Date.now()}`;
2135
2192
  return withTurnkeyErrorHandling(async () => {
2136
2193
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
@@ -2178,7 +2235,7 @@ class TurnkeyClient {
2178
2235
  * @throws {TurnkeyError} If there is no active session, if the userId is missing, or if there is an error removing the passkeys.
2179
2236
  */
2180
2237
  this.removePasskeys = async (params) => {
2181
- const { authenticatorIds, stampWith, organizationId } = params;
2238
+ const { authenticatorIds, stampWith = this.config.defaultStamperType, organizationId, } = params;
2182
2239
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
2183
2240
  const userId = params?.userId || session?.userId;
2184
2241
  if (!userId) {
@@ -2219,7 +2276,7 @@ class TurnkeyClient {
2219
2276
  * @throws {TurnkeyError} If there is no active session or if there is an error creating the wallet.
2220
2277
  */
2221
2278
  this.createWallet = async (params) => {
2222
- const { walletName, accounts, organizationId: organizationIdFromParams, mnemonicLength, stampWith, } = params;
2279
+ const { walletName, accounts, organizationId: organizationIdFromParams, mnemonicLength, stampWith = this.config.defaultStamperType, } = params;
2223
2280
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
2224
2281
  const organizationId = organizationIdFromParams || session?.organizationId;
2225
2282
  if (!organizationId) {
@@ -2271,7 +2328,7 @@ class TurnkeyClient {
2271
2328
  * @throws {TurnkeyError} If there is no active session, if the wallet does not exist, or if there is an error creating the wallet accounts.
2272
2329
  */
2273
2330
  this.createWalletAccounts = async (params) => {
2274
- const { accounts, walletId, organizationId: organizationIdFromParams, stampWith, } = params;
2331
+ const { accounts, walletId, organizationId: organizationIdFromParams, stampWith = this.config.defaultStamperType, } = params;
2275
2332
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
2276
2333
  const organizationId = organizationIdFromParams || session?.organizationId;
2277
2334
  if (!organizationId) {
@@ -2326,7 +2383,7 @@ class TurnkeyClient {
2326
2383
  * @throws {TurnkeyError} If there is no active session, if the targetPublicKey is missing, or if there is an error exporting the wallet.
2327
2384
  */
2328
2385
  this.exportWallet = async (params) => {
2329
- const { walletId, targetPublicKey, stampWith, organizationId: organizationIdFromParams, } = params;
2386
+ const { walletId, targetPublicKey, stampWith = this.config.defaultStamperType, organizationId: organizationIdFromParams, } = params;
2330
2387
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
2331
2388
  const organizationId = organizationIdFromParams || session?.organizationId;
2332
2389
  if (!organizationId) {
@@ -2364,7 +2421,7 @@ class TurnkeyClient {
2364
2421
  * @throws {TurnkeyError} If there is no active session, if the targetPublicKey is missing, or if there is an error exporting the private key.
2365
2422
  */
2366
2423
  this.exportPrivateKey = async (params) => {
2367
- const { privateKeyId, targetPublicKey, stampWith, organizationId: organizationIdFromParams, } = params;
2424
+ const { privateKeyId, targetPublicKey, stampWith = this.config.defaultStamperType, organizationId: organizationIdFromParams, } = params;
2368
2425
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
2369
2426
  const organizationId = organizationIdFromParams || session?.organizationId;
2370
2427
  if (!organizationId) {
@@ -2403,7 +2460,7 @@ class TurnkeyClient {
2403
2460
  *
2404
2461
  */
2405
2462
  this.exportWalletAccount = async (params) => {
2406
- const { address, targetPublicKey, stampWith, organizationId: organizationIdFromParams, } = params;
2463
+ const { address, targetPublicKey, stampWith = this.config.defaultStamperType, organizationId: organizationIdFromParams, } = params;
2407
2464
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
2408
2465
  const organizationId = organizationIdFromParams || session?.organizationId;
2409
2466
  if (!organizationId) {
@@ -2444,7 +2501,7 @@ class TurnkeyClient {
2444
2501
  * @throws {TurnkeyError} If there is no active session, if the encrypted bundle is invalid, or if there is an error importing the wallet.
2445
2502
  */
2446
2503
  this.importWallet = async (params) => {
2447
- const { encryptedBundle, accounts, walletName, organizationId: organizationIdFromParams, userId: userIdFromParams, stampWith, } = params;
2504
+ const { encryptedBundle, accounts, walletName, organizationId: organizationIdFromParams, userId: userIdFromParams, stampWith = this.config.defaultStamperType, } = params;
2448
2505
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
2449
2506
  const organizationId = organizationIdFromParams || session?.organizationId;
2450
2507
  if (!organizationId) {
@@ -2502,7 +2559,7 @@ class TurnkeyClient {
2502
2559
  * @throws {TurnkeyError} If there is no active session, if the encrypted bundle is invalid, or if there is an error importing the wallet.
2503
2560
  */
2504
2561
  this.importPrivateKey = async (params) => {
2505
- const { encryptedBundle, privateKeyName, addressFormats, curve, organizationId: organizationIdFromParams, userId: userIdFromParams, stampWith, } = params;
2562
+ const { encryptedBundle, privateKeyName, addressFormats, curve, organizationId: organizationIdFromParams, userId: userIdFromParams, stampWith = this.config.defaultStamperType, } = params;
2506
2563
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
2507
2564
  const organizationId = organizationIdFromParams || session?.organizationId;
2508
2565
  if (!organizationId) {
@@ -2552,7 +2609,7 @@ class TurnkeyClient {
2552
2609
  * @throws {TurnkeyError} If there is no active session or if there is an error deleting the sub-organization.
2553
2610
  */
2554
2611
  this.deleteSubOrganization = async (params) => {
2555
- const { deleteWithoutExport = false, organizationId: organizationIdFromParams, stampWith, } = params || {};
2612
+ const { deleteWithoutExport = false, organizationId: organizationIdFromParams, stampWith = this.config.defaultStamperType, } = params || {};
2556
2613
  const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
2557
2614
  const organizationId = organizationIdFromParams || session?.organizationId;
2558
2615
  return withTurnkeyErrorHandling(async () => {
@@ -2660,7 +2717,7 @@ class TurnkeyClient {
2660
2717
  * @throws {TurnkeyError} If the session key does not exist, if there is no active session, or if there is an error refreshing the session.
2661
2718
  */
2662
2719
  this.refreshSession = async (params) => {
2663
- const { sessionKey = await this.storageManager.getActiveSessionKey(), expirationSeconds = DEFAULT_SESSION_EXPIRATION_IN_SECONDS, publicKey, invalidateExisitng = false, } = params || {};
2720
+ const { sessionKey = await this.storageManager.getActiveSessionKey(), expirationSeconds = DEFAULT_SESSION_EXPIRATION_IN_SECONDS, publicKey, stampWith = this.config.defaultStamperType, invalidateExisitng = false, } = params || {};
2664
2721
  if (!sessionKey) {
2665
2722
  throw new TurnkeyError("No session key provided or active session to refresh session", TurnkeyErrorCodes.NO_SESSION_FOUND);
2666
2723
  }
@@ -2683,7 +2740,7 @@ class TurnkeyClient {
2683
2740
  publicKey: keyPair,
2684
2741
  expirationSeconds,
2685
2742
  invalidateExisting: invalidateExisitng,
2686
- }, params?.stampWith);
2743
+ }, stampWith);
2687
2744
  if (!res || !res.session) {
2688
2745
  throw new TurnkeyError("No session found in the refresh response", TurnkeyErrorCodes.BAD_RESPONSE);
2689
2746
  }
@@ -2881,6 +2938,46 @@ class TurnkeyClient {
2881
2938
  errorCode: TurnkeyErrorCodes.GET_PROXY_AUTH_CONFIG_ERROR,
2882
2939
  });
2883
2940
  };
2941
+ /**
2942
+ * Fetches the boot proof for a given app proof.
2943
+ *
2944
+ * - This function is idempotent: multiple calls with the same `app proof` will always return the boot proof.
2945
+ * - Attempts to find the boot proof for the given app proof.
2946
+ * - If a boot proof is found, it is returned as is.
2947
+ * - If no boot proof is found, an error is thrown.
2948
+ *
2949
+ * @param params.appProof - the app proof for which the boot proof is being fetched.
2950
+ * @param params.organizationId - organization ID to specify the sub-organization (defaults to the current session's organizationId).
2951
+ * @param params.stampWith - parameter to stamp the request with a specific stamper (StamperType.Passkey, StamperType.ApiKey, or StamperType.Wallet).
2952
+ * @returns A promise that resolves to the {@link v1BootProof} associated with the given app proof.
2953
+ * @throws {TurnkeyError} If there is no active session, if the input is invalid, or if boot proof retrieval fails.
2954
+ */
2955
+ this.fetchBootProofForAppProof = async (params) => {
2956
+ const { appProof, stampWith = this.config.defaultStamperType, organizationId: organizationIdFromParams, } = params;
2957
+ return withTurnkeyErrorHandling(async () => {
2958
+ const session = await getActiveSessionOrThrowIfRequired(stampWith, this.storageManager.getActiveSession);
2959
+ const organizationId = organizationIdFromParams || session?.organizationId;
2960
+ if (!organizationId) {
2961
+ throw new TurnkeyError("Organization ID is required to fetch a Boot Proof.", TurnkeyErrorCodes.INVALID_REQUEST);
2962
+ }
2963
+ // validate their input
2964
+ if (appProof === null || appProof?.publicKey === null) {
2965
+ throw new TurnkeyError("'appProof' is required and cannot be empty.", TurnkeyErrorCodes.INVALID_REQUEST);
2966
+ }
2967
+ const ephemeralKey = appProof.publicKey;
2968
+ const bootProofResponse = await this.httpClient.getBootProof({
2969
+ organizationId,
2970
+ ephemeralKey,
2971
+ }, stampWith);
2972
+ if (!bootProofResponse || !bootProofResponse.bootProof) {
2973
+ throw new TurnkeyError("No boot proof found in the response", TurnkeyErrorCodes.BAD_RESPONSE);
2974
+ }
2975
+ return bootProofResponse.bootProof;
2976
+ }, {
2977
+ errorMessage: "Failed to get boot proof for app proof",
2978
+ errorCode: TurnkeyErrorCodes.FETCH_BOOT_PROOF_ERROR,
2979
+ });
2980
+ };
2884
2981
  this.config = config;
2885
2982
  // Just store any explicitly provided stampers
2886
2983
  this.apiKeyStamper = apiKeyStamper;
@@ -2903,19 +3000,9 @@ class TurnkeyClient {
2903
3000
  this.config.walletConfig?.features?.connecting) {
2904
3001
  this.walletManager = await createWalletManager(this.config.walletConfig);
2905
3002
  }
2906
- // We can comfortably default to the prod urls here
2907
- const apiBaseUrl = this.config.apiBaseUrl || "https://api.turnkey.com";
2908
- const authProxyUrl = this.config.authProxyUrl || "https://authproxy.turnkey.com";
2909
3003
  // Initialize the HTTP client with the appropriate stampers
2910
- this.httpClient = new TurnkeySDKClientBase({
2911
- ...this.config,
2912
- apiBaseUrl,
2913
- authProxyUrl,
2914
- apiKeyStamper: this.apiKeyStamper,
2915
- passkeyStamper: this.passkeyStamper,
2916
- walletStamper: this.walletManager?.stamper,
2917
- storageManager: this.storageManager,
2918
- });
3004
+ // Note: not passing anything here since we want to use the configured stampers and this.config
3005
+ this.httpClient = this.createHttpClient();
2919
3006
  }
2920
3007
  }
2921
3008