@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
@@ -1535,7 +1535,7 @@ class TurnkeySDKClientBase {
1535
1535
  this.config.organizationId,
1536
1536
  timestampMs: timestampMs ?? String(Date.now()),
1537
1537
  type: "ACTIVITY_TYPE_CREATE_PRIVATE_KEYS_V2",
1538
- }, "createPrivateKeysResult", stampWith);
1538
+ }, "createPrivateKeysResultV2", stampWith);
1539
1539
  };
1540
1540
  this.stampCreatePrivateKeys = async (input, stampWith) => {
1541
1541
  const activeStamper = this.getStamper(stampWith);
@@ -1603,7 +1603,7 @@ class TurnkeySDKClientBase {
1603
1603
  this.config.organizationId,
1604
1604
  timestampMs: timestampMs ?? String(Date.now()),
1605
1605
  type: "ACTIVITY_TYPE_CREATE_READ_WRITE_SESSION_V2",
1606
- }, "createReadWriteSessionResult", stampWith);
1606
+ }, "createReadWriteSessionResultV2", stampWith);
1607
1607
  };
1608
1608
  this.stampCreateReadWriteSession = async (input, stampWith) => {
1609
1609
  const activeStamper = this.getStamper(stampWith);
@@ -1672,7 +1672,7 @@ class TurnkeySDKClientBase {
1672
1672
  this.config.organizationId,
1673
1673
  timestampMs: timestampMs ?? String(Date.now()),
1674
1674
  type: "ACTIVITY_TYPE_CREATE_SUB_ORGANIZATION_V7",
1675
- }, "createSubOrganizationResult", stampWith);
1675
+ }, "createSubOrganizationResultV7", stampWith);
1676
1676
  };
1677
1677
  this.stampCreateSubOrganization = async (input, stampWith) => {
1678
1678
  const activeStamper = this.getStamper(stampWith);
@@ -2796,7 +2796,7 @@ class TurnkeySDKClientBase {
2796
2796
  this.config.organizationId,
2797
2797
  timestampMs: timestampMs ?? String(Date.now()),
2798
2798
  type: "ACTIVITY_TYPE_INIT_OTP_AUTH_V3",
2799
- }, "initOtpAuthResult", stampWith);
2799
+ }, "initOtpAuthResultV2", stampWith);
2800
2800
  };
2801
2801
  this.stampInitOtpAuth = async (input, stampWith) => {
2802
2802
  const activeStamper = this.getStamper(stampWith);
@@ -3375,7 +3375,7 @@ class TurnkeySDKClientBase {
3375
3375
  this.config.organizationId,
3376
3376
  timestampMs: timestampMs ?? String(Date.now()),
3377
3377
  type: "ACTIVITY_TYPE_UPDATE_POLICY_V2",
3378
- }, "updatePolicyResult", stampWith);
3378
+ }, "updatePolicyResultV2", stampWith);
3379
3379
  };
3380
3380
  this.stampUpdatePolicy = async (input, stampWith) => {
3381
3381
  const activeStamper = this.getStamper(stampWith);
@@ -3807,6 +3807,52 @@ class TurnkeySDKClientBase {
3807
3807
  return this.apiKeyStamper;
3808
3808
  }
3809
3809
  }
3810
+ sleep(ms) {
3811
+ return new Promise((resolve) => setTimeout(resolve, ms));
3812
+ }
3813
+ /**
3814
+ * Poll for activity completion until terminal status or max retries.
3815
+ * @internal
3816
+ */
3817
+ async pollForCompletion(activityId, stampWith) {
3818
+ const pollingDuration = this.config.activityPoller?.intervalMs ?? 1000;
3819
+ const maxRetries = this.config.activityPoller?.numRetries ?? 3;
3820
+ let attempts = 0;
3821
+ let activityData;
3822
+ do {
3823
+ await this.sleep(pollingDuration);
3824
+ activityData = (await this.getActivity({ activityId }, stampWith));
3825
+ attempts++;
3826
+ } while (!http.TERMINAL_ACTIVITY_STATUSES.includes(activityData.activity.status) &&
3827
+ attempts < maxRetries);
3828
+ return activityData;
3829
+ }
3830
+ /**
3831
+ * Extract and flatten result fields from a completed activity response.
3832
+ * @internal
3833
+ */
3834
+ handleActivityResponse(activityData, resultKey) {
3835
+ const { result, status } = activityData.activity;
3836
+ if (status === "ACTIVITY_STATUS_COMPLETED" && result) {
3837
+ // If a specific resultKey was provided, use it
3838
+ if (resultKey && result[resultKey]) {
3839
+ return {
3840
+ ...result[resultKey],
3841
+ ...activityData,
3842
+ };
3843
+ }
3844
+ // Otherwise, try to find any result field and flatten it
3845
+ for (const key of Object.keys(result)) {
3846
+ if (key.endsWith("Result") && result[key]) {
3847
+ return {
3848
+ ...result[key],
3849
+ ...activityData,
3850
+ };
3851
+ }
3852
+ }
3853
+ }
3854
+ return activityData;
3855
+ }
3810
3856
  async request(url, body, stampWith) {
3811
3857
  const fullUrl = this.config.apiBaseUrl + url;
3812
3858
  const stringifiedBody = JSON.stringify(body);
@@ -3839,40 +3885,13 @@ class TurnkeySDKClientBase {
3839
3885
  return data;
3840
3886
  }
3841
3887
  async activity(url, body, resultKey, stampWith) {
3842
- const pollingDuration = this.config.activityPoller?.intervalMs ?? 1000;
3843
- const maxRetries = this.config.activityPoller?.numRetries ?? 3;
3844
- const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
3845
- const handleResponse = (activityData) => {
3846
- const { result, status } = activityData.activity;
3847
- if (status === "ACTIVITY_STATUS_COMPLETED") {
3848
- return {
3849
- ...result[`${resultKey}`],
3850
- ...activityData,
3851
- };
3852
- }
3853
- return activityData;
3854
- };
3855
- let attempts = 0;
3856
- const pollStatus = async (activityId) => {
3857
- const pollBody = { activityId };
3858
- // Pass the stampWith parameter to getActivity
3859
- const pollData = (await this.getActivity(pollBody, stampWith));
3860
- if (attempts > maxRetries) {
3861
- return handleResponse(pollData);
3862
- }
3863
- attempts += 1;
3864
- if (!http.TERMINAL_ACTIVITY_STATUSES.includes(pollData.activity.status)) {
3865
- await sleep(pollingDuration);
3866
- return pollStatus(activityId);
3867
- }
3868
- return handleResponse(pollData);
3869
- };
3870
- // Use the specified stamper for the initial request
3871
- const responseData = (await this.request(url, body, stampWith));
3872
- if (!http.TERMINAL_ACTIVITY_STATUSES.includes(responseData.activity.status)) {
3873
- return pollStatus(responseData.activity.id);
3888
+ // Make the initial request
3889
+ let activityData = await this.request(url, body, stampWith);
3890
+ // Poll if not in terminal status
3891
+ if (!http.TERMINAL_ACTIVITY_STATUSES.includes(activityData.activity.status)) {
3892
+ activityData = await this.pollForCompletion(activityData.activity.id, stampWith);
3874
3893
  }
3875
- return handleResponse(responseData);
3894
+ return this.handleActivityResponse(activityData, resultKey);
3876
3895
  }
3877
3896
  async activityDecision(url, body, stampWith) {
3878
3897
  // Use the specified stamper for this request
@@ -3910,6 +3929,54 @@ class TurnkeySDKClientBase {
3910
3929
  const data = await response.json();
3911
3930
  return data;
3912
3931
  }
3932
+ /**
3933
+ * Submit a pre-signed request to Turnkey.
3934
+ *
3935
+ * Use this method to execute any TSignedRequest returned by the SDK's
3936
+ * stamping methods (stampCreateApiKeys, stampGetPolicies, stampGetWallets, etc.).
3937
+ *
3938
+ * Works for both query and activity requests:
3939
+ * - For queries: returns the response directly
3940
+ * - For activities: automatically polls until completion and extracts the result
3941
+ *
3942
+ * @param signedRequest - A TSignedRequest object returned by a stamping method
3943
+ * @param options - Optional configuration for the request
3944
+ * @param options.resultKey - For activity requests, the key to extract from the result (e.g., "createApiKeysResultV2")
3945
+ * @returns The parsed response, with activity result fields flattened if applicable
3946
+ */
3947
+ async sendSignedRequest(signedRequest, options) {
3948
+ const headers = {
3949
+ "Content-Type": "application/json",
3950
+ "X-Client-Version": version.VERSION,
3951
+ [signedRequest.stamp.stampHeaderName]: signedRequest.stamp.stampHeaderValue,
3952
+ };
3953
+ const response = await fetch(signedRequest.url, {
3954
+ method: "POST",
3955
+ headers,
3956
+ body: signedRequest.body,
3957
+ });
3958
+ if (!response.ok) {
3959
+ let res;
3960
+ try {
3961
+ res = await response.json();
3962
+ }
3963
+ catch (_) {
3964
+ throw new Error(`${response.status} ${response.statusText}`);
3965
+ }
3966
+ throw new error.TurnkeyRequestError(res);
3967
+ }
3968
+ const data = await response.json();
3969
+ // Check if this is an activity response that needs polling
3970
+ if (data.activity) {
3971
+ let activityData = data;
3972
+ // Poll if not in terminal status
3973
+ if (!http.TERMINAL_ACTIVITY_STATUSES.includes(activityData.activity.status)) {
3974
+ activityData = await this.pollForCompletion(activityData.activity.id);
3975
+ }
3976
+ return this.handleActivityResponse(activityData, options?.resultKey);
3977
+ }
3978
+ return data;
3979
+ }
3913
3980
  }
3914
3981
 
3915
3982
  exports.TurnkeySDKClientBase = TurnkeySDKClientBase;