@turnkey/core 1.10.0 → 1.11.1

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 (67) hide show
  1. package/dist/__clients__/core.d.ts +2 -1
  2. package/dist/__clients__/core.d.ts.map +1 -1
  3. package/dist/__clients__/core.js +144 -125
  4. package/dist/__clients__/core.js.map +1 -1
  5. package/dist/__clients__/core.mjs +144 -125
  6. package/dist/__clients__/core.mjs.map +1 -1
  7. package/dist/__generated__/sdk-client-base.d.ts +29 -0
  8. package/dist/__generated__/sdk-client-base.d.ts.map +1 -1
  9. package/dist/__generated__/sdk-client-base.js +105 -38
  10. package/dist/__generated__/sdk-client-base.js.map +1 -1
  11. package/dist/__generated__/sdk-client-base.mjs +105 -38
  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__/api/base.d.ts +2 -0
  17. package/dist/__stampers__/api/base.d.ts.map +1 -1
  18. package/dist/__stampers__/api/base.js +19 -0
  19. package/dist/__stampers__/api/base.js.map +1 -1
  20. package/dist/__stampers__/api/base.mjs +19 -0
  21. package/dist/__stampers__/api/base.mjs.map +1 -1
  22. package/dist/__stampers__/api/mobile/stamper.d.ts +2 -0
  23. package/dist/__stampers__/api/mobile/stamper.d.ts.map +1 -1
  24. package/dist/__stampers__/api/mobile/stamper.js +19 -0
  25. package/dist/__stampers__/api/mobile/stamper.js.map +1 -1
  26. package/dist/__stampers__/api/mobile/stamper.mjs +20 -1
  27. package/dist/__stampers__/api/mobile/stamper.mjs.map +1 -1
  28. package/dist/__stampers__/api/web/stamper.d.ts +2 -1
  29. package/dist/__stampers__/api/web/stamper.d.ts.map +1 -1
  30. package/dist/__stampers__/api/web/stamper.js +9 -4
  31. package/dist/__stampers__/api/web/stamper.js.map +1 -1
  32. package/dist/__stampers__/api/web/stamper.mjs +9 -4
  33. package/dist/__stampers__/api/web/stamper.mjs.map +1 -1
  34. package/dist/__storage__/mobile/storage.js +1 -1
  35. package/dist/__storage__/mobile/storage.js.map +1 -1
  36. package/dist/__storage__/mobile/storage.mjs +1 -1
  37. package/dist/__storage__/mobile/storage.mjs.map +1 -1
  38. package/dist/__types__/auth.d.ts +2 -0
  39. package/dist/__types__/auth.d.ts.map +1 -1
  40. package/dist/__types__/auth.js.map +1 -1
  41. package/dist/__types__/auth.mjs.map +1 -1
  42. package/dist/__types__/external-wallets.d.ts +9 -1
  43. package/dist/__types__/external-wallets.d.ts.map +1 -1
  44. package/dist/__types__/method-types/shared.d.ts +2 -0
  45. package/dist/__types__/method-types/shared.d.ts.map +1 -1
  46. package/dist/__wallet__/web/native/ethereum.d.ts.map +1 -1
  47. package/dist/__wallet__/web/native/ethereum.js +13 -5
  48. package/dist/__wallet__/web/native/ethereum.js.map +1 -1
  49. package/dist/__wallet__/web/native/ethereum.mjs +13 -5
  50. package/dist/__wallet__/web/native/ethereum.mjs.map +1 -1
  51. package/dist/__wallet__/web/native/solana.d.ts.map +1 -1
  52. package/dist/__wallet__/web/native/solana.js +17 -9
  53. package/dist/__wallet__/web/native/solana.js.map +1 -1
  54. package/dist/__wallet__/web/native/solana.mjs +17 -9
  55. package/dist/__wallet__/web/native/solana.mjs.map +1 -1
  56. package/dist/index.d.ts +1 -1
  57. package/dist/index.d.ts.map +1 -1
  58. package/dist/index.js +3 -0
  59. package/dist/index.js.map +1 -1
  60. package/dist/index.mjs +1 -1
  61. package/dist/utils.d.ts +34 -2
  62. package/dist/utils.d.ts.map +1 -1
  63. package/dist/utils.js +103 -9
  64. package/dist/utils.js.map +1 -1
  65. package/dist/utils.mjs +100 -10
  66. package/dist/utils.mjs.map +1 -1
  67. package/package.json +8 -8
@@ -1533,7 +1533,7 @@ class TurnkeySDKClientBase {
1533
1533
  this.config.organizationId,
1534
1534
  timestampMs: timestampMs ?? String(Date.now()),
1535
1535
  type: "ACTIVITY_TYPE_CREATE_PRIVATE_KEYS_V2",
1536
- }, "createPrivateKeysResult", stampWith);
1536
+ }, "createPrivateKeysResultV2", stampWith);
1537
1537
  };
1538
1538
  this.stampCreatePrivateKeys = async (input, stampWith) => {
1539
1539
  const activeStamper = this.getStamper(stampWith);
@@ -1601,7 +1601,7 @@ class TurnkeySDKClientBase {
1601
1601
  this.config.organizationId,
1602
1602
  timestampMs: timestampMs ?? String(Date.now()),
1603
1603
  type: "ACTIVITY_TYPE_CREATE_READ_WRITE_SESSION_V2",
1604
- }, "createReadWriteSessionResult", stampWith);
1604
+ }, "createReadWriteSessionResultV2", stampWith);
1605
1605
  };
1606
1606
  this.stampCreateReadWriteSession = async (input, stampWith) => {
1607
1607
  const activeStamper = this.getStamper(stampWith);
@@ -1670,7 +1670,7 @@ class TurnkeySDKClientBase {
1670
1670
  this.config.organizationId,
1671
1671
  timestampMs: timestampMs ?? String(Date.now()),
1672
1672
  type: "ACTIVITY_TYPE_CREATE_SUB_ORGANIZATION_V7",
1673
- }, "createSubOrganizationResult", stampWith);
1673
+ }, "createSubOrganizationResultV7", stampWith);
1674
1674
  };
1675
1675
  this.stampCreateSubOrganization = async (input, stampWith) => {
1676
1676
  const activeStamper = this.getStamper(stampWith);
@@ -2794,7 +2794,7 @@ class TurnkeySDKClientBase {
2794
2794
  this.config.organizationId,
2795
2795
  timestampMs: timestampMs ?? String(Date.now()),
2796
2796
  type: "ACTIVITY_TYPE_INIT_OTP_AUTH_V3",
2797
- }, "initOtpAuthResult", stampWith);
2797
+ }, "initOtpAuthResultV2", stampWith);
2798
2798
  };
2799
2799
  this.stampInitOtpAuth = async (input, stampWith) => {
2800
2800
  const activeStamper = this.getStamper(stampWith);
@@ -3373,7 +3373,7 @@ class TurnkeySDKClientBase {
3373
3373
  this.config.organizationId,
3374
3374
  timestampMs: timestampMs ?? String(Date.now()),
3375
3375
  type: "ACTIVITY_TYPE_UPDATE_POLICY_V2",
3376
- }, "updatePolicyResult", stampWith);
3376
+ }, "updatePolicyResultV2", stampWith);
3377
3377
  };
3378
3378
  this.stampUpdatePolicy = async (input, stampWith) => {
3379
3379
  const activeStamper = this.getStamper(stampWith);
@@ -3805,6 +3805,52 @@ class TurnkeySDKClientBase {
3805
3805
  return this.apiKeyStamper;
3806
3806
  }
3807
3807
  }
3808
+ sleep(ms) {
3809
+ return new Promise((resolve) => setTimeout(resolve, ms));
3810
+ }
3811
+ /**
3812
+ * Poll for activity completion until terminal status or max retries.
3813
+ * @internal
3814
+ */
3815
+ async pollForCompletion(activityId, stampWith) {
3816
+ const pollingDuration = this.config.activityPoller?.intervalMs ?? 1000;
3817
+ const maxRetries = this.config.activityPoller?.numRetries ?? 3;
3818
+ let attempts = 0;
3819
+ let activityData;
3820
+ do {
3821
+ await this.sleep(pollingDuration);
3822
+ activityData = (await this.getActivity({ activityId }, stampWith));
3823
+ attempts++;
3824
+ } while (!TERMINAL_ACTIVITY_STATUSES.includes(activityData.activity.status) &&
3825
+ attempts < maxRetries);
3826
+ return activityData;
3827
+ }
3828
+ /**
3829
+ * Extract and flatten result fields from a completed activity response.
3830
+ * @internal
3831
+ */
3832
+ handleActivityResponse(activityData, resultKey) {
3833
+ const { result, status } = activityData.activity;
3834
+ if (status === "ACTIVITY_STATUS_COMPLETED" && result) {
3835
+ // If a specific resultKey was provided, use it
3836
+ if (resultKey && result[resultKey]) {
3837
+ return {
3838
+ ...result[resultKey],
3839
+ ...activityData,
3840
+ };
3841
+ }
3842
+ // Otherwise, try to find any result field and flatten it
3843
+ for (const key of Object.keys(result)) {
3844
+ if (key.endsWith("Result") && result[key]) {
3845
+ return {
3846
+ ...result[key],
3847
+ ...activityData,
3848
+ };
3849
+ }
3850
+ }
3851
+ }
3852
+ return activityData;
3853
+ }
3808
3854
  async request(url, body, stampWith) {
3809
3855
  const fullUrl = this.config.apiBaseUrl + url;
3810
3856
  const stringifiedBody = JSON.stringify(body);
@@ -3837,40 +3883,13 @@ class TurnkeySDKClientBase {
3837
3883
  return data;
3838
3884
  }
3839
3885
  async activity(url, body, resultKey, stampWith) {
3840
- const pollingDuration = this.config.activityPoller?.intervalMs ?? 1000;
3841
- const maxRetries = this.config.activityPoller?.numRetries ?? 3;
3842
- const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
3843
- const handleResponse = (activityData) => {
3844
- const { result, status } = activityData.activity;
3845
- if (status === "ACTIVITY_STATUS_COMPLETED") {
3846
- return {
3847
- ...result[`${resultKey}`],
3848
- ...activityData,
3849
- };
3850
- }
3851
- return activityData;
3852
- };
3853
- let attempts = 0;
3854
- const pollStatus = async (activityId) => {
3855
- const pollBody = { activityId };
3856
- // Pass the stampWith parameter to getActivity
3857
- const pollData = (await this.getActivity(pollBody, stampWith));
3858
- if (attempts > maxRetries) {
3859
- return handleResponse(pollData);
3860
- }
3861
- attempts += 1;
3862
- if (!TERMINAL_ACTIVITY_STATUSES.includes(pollData.activity.status)) {
3863
- await sleep(pollingDuration);
3864
- return pollStatus(activityId);
3865
- }
3866
- return handleResponse(pollData);
3867
- };
3868
- // Use the specified stamper for the initial request
3869
- const responseData = (await this.request(url, body, stampWith));
3870
- if (!TERMINAL_ACTIVITY_STATUSES.includes(responseData.activity.status)) {
3871
- return pollStatus(responseData.activity.id);
3886
+ // Make the initial request
3887
+ let activityData = await this.request(url, body, stampWith);
3888
+ // Poll if not in terminal status
3889
+ if (!TERMINAL_ACTIVITY_STATUSES.includes(activityData.activity.status)) {
3890
+ activityData = await this.pollForCompletion(activityData.activity.id, stampWith);
3872
3891
  }
3873
- return handleResponse(responseData);
3892
+ return this.handleActivityResponse(activityData, resultKey);
3874
3893
  }
3875
3894
  async activityDecision(url, body, stampWith) {
3876
3895
  // Use the specified stamper for this request
@@ -3908,6 +3927,54 @@ class TurnkeySDKClientBase {
3908
3927
  const data = await response.json();
3909
3928
  return data;
3910
3929
  }
3930
+ /**
3931
+ * Submit a pre-signed request to Turnkey.
3932
+ *
3933
+ * Use this method to execute any TSignedRequest returned by the SDK's
3934
+ * stamping methods (stampCreateApiKeys, stampGetPolicies, stampGetWallets, etc.).
3935
+ *
3936
+ * Works for both query and activity requests:
3937
+ * - For queries: returns the response directly
3938
+ * - For activities: automatically polls until completion and extracts the result
3939
+ *
3940
+ * @param signedRequest - A TSignedRequest object returned by a stamping method
3941
+ * @param options - Optional configuration for the request
3942
+ * @param options.resultKey - For activity requests, the key to extract from the result (e.g., "createApiKeysResultV2")
3943
+ * @returns The parsed response, with activity result fields flattened if applicable
3944
+ */
3945
+ async sendSignedRequest(signedRequest, options) {
3946
+ const headers = {
3947
+ "Content-Type": "application/json",
3948
+ "X-Client-Version": VERSION,
3949
+ [signedRequest.stamp.stampHeaderName]: signedRequest.stamp.stampHeaderValue,
3950
+ };
3951
+ const response = await fetch(signedRequest.url, {
3952
+ method: "POST",
3953
+ headers,
3954
+ body: signedRequest.body,
3955
+ });
3956
+ if (!response.ok) {
3957
+ let res;
3958
+ try {
3959
+ res = await response.json();
3960
+ }
3961
+ catch (_) {
3962
+ throw new Error(`${response.status} ${response.statusText}`);
3963
+ }
3964
+ throw new TurnkeyRequestError(res);
3965
+ }
3966
+ const data = await response.json();
3967
+ // Check if this is an activity response that needs polling
3968
+ if (data.activity) {
3969
+ let activityData = data;
3970
+ // Poll if not in terminal status
3971
+ if (!TERMINAL_ACTIVITY_STATUSES.includes(activityData.activity.status)) {
3972
+ activityData = await this.pollForCompletion(activityData.activity.id);
3973
+ }
3974
+ return this.handleActivityResponse(activityData, options?.resultKey);
3975
+ }
3976
+ return data;
3977
+ }
3911
3978
  }
3912
3979
 
3913
3980
  export { TurnkeySDKClientBase };