Skip to content

Commit 80ea306

Browse files
committed
resolve comments
1 parent e1bd68f commit 80ea306

File tree

12 files changed

+319
-347
lines changed

12 files changed

+319
-347
lines changed

.changeset/rude-signs-like.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@turnkey/sdk-types": minor
3+
---
4+
5+
Added `WALLET_CONNECT_INITIALIZATION_ERROR` error code

.changeset/six-laws-see.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"@turnkey/core": minor
33
---
44

5+
- Fixed `stamp*` methods for query endpoints in `httpClient` incorrectly formatting request body
56
- Parallelized stamper and session initialization
67
- Separated WalletConnect initialization from client init
78
- Optimized `fetchWallet` by reducing redundant queries and running wallet/user fetches in parallel

packages/core/scripts/codegen.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ const generateSDKClientFromSwagger = async (
369369
const inputType = `T${operationNameWithoutNamespace}Body`;
370370
const responseType = `T${operationNameWithoutNamespace}Response`;
371371

372-
// For query methods
372+
// for query methods, we use flat body structure
373373
if (methodType === "query") {
374374
codeBuffer.push(
375375
`\n\t${methodName} = async (input: SdkTypes.${inputType}${
@@ -434,8 +434,38 @@ const generateSDKClientFromSwagger = async (
434434
VERSIONED_ACTIVITY_TYPES[unversionedActivityType];
435435

436436
// generate a stamping method for each method
437-
codeBuffer.push(
438-
`\n\tstamp${operationNameWithoutNamespace} = async (input: SdkTypes.${inputType}, stampWith?: StamperType): Promise<TSignedRequest | undefined> => {
437+
438+
if (methodType === "noop") {
439+
// we skip stamp method generation for noop methods
440+
continue;
441+
} else if (methodType === "query") {
442+
// for query methods, we use flat body structure
443+
codeBuffer.push(
444+
`\n\tstamp${operationNameWithoutNamespace} = async (input: SdkTypes.${inputType}, stampWith?: StamperType): Promise<TSignedRequest | undefined> => {
445+
const activeStamper = this.getStamper(stampWith);
446+
if (!activeStamper) {
447+
return undefined;
448+
}
449+
450+
const fullUrl = this.config.apiBaseUrl + "${endpointPath}";
451+
const body = {
452+
...input,
453+
organizationId: input.organizationId
454+
};
455+
456+
const stringifiedBody = JSON.stringify(body);
457+
const stamp = await activeStamper.stamp(stringifiedBody);
458+
return {
459+
body: stringifiedBody,
460+
stamp: stamp,
461+
url: fullUrl,
462+
};
463+
}`,
464+
);
465+
} else {
466+
// for activity and activityDecision methods, use parameters wrapper and type field
467+
codeBuffer.push(
468+
`\n\tstamp${operationNameWithoutNamespace} = async (input: SdkTypes.${inputType}, stampWith?: StamperType): Promise<TSignedRequest | undefined> => {
439469
const activeStamper = this.getStamper(stampWith);
440470
if (!activeStamper) {
441471
return undefined;
@@ -454,7 +484,6 @@ const generateSDKClientFromSwagger = async (
454484
type: "${versionedActivityType ?? unversionedActivityType}"
455485
};
456486
457-
458487
const stringifiedBody = JSON.stringify(bodyWithType);
459488
const stamp = await activeStamper.stamp(stringifiedBody);
460489
return {
@@ -463,7 +492,8 @@ const generateSDKClientFromSwagger = async (
463492
url: fullUrl,
464493
};
465494
}`,
466-
);
495+
);
496+
}
467497
}
468498

469499
for (const endpointPath in authProxySwaggerSpec.paths) {

packages/core/src/__clients__/core.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
type v1CreatePolicyIntentV3,
1919
type v1BootProof,
2020
ProxyTSignupResponse,
21+
TGetWalletsResponse,
22+
TGetUserResponse,
2123
} from "@turnkey/sdk-types";
2224
import {
2325
DEFAULT_SESSION_EXPIRATION_IN_SECONDS,
@@ -1966,11 +1968,22 @@ export class TurnkeyClient {
19661968
| Promise<{ ethereum: string[]; solana: string[] }>
19671969
| undefined;
19681970
if (organizationId && userId && this.walletManager?.connector) {
1969-
userPromise = this.fetchUser({
1970-
userId,
1971-
organizationId,
1971+
const signedUserRequest = await this.httpClient.stampGetUser(
1972+
{
1973+
userId,
1974+
organizationId,
1975+
},
19721976
stampWith,
1973-
}).then(getAuthenticatorAddresses);
1977+
);
1978+
if (!signedUserRequest) {
1979+
throw new TurnkeyError(
1980+
"Failed to stamp user request",
1981+
TurnkeyErrorCodes.INVALID_REQUEST,
1982+
);
1983+
}
1984+
userPromise = sendSignedRequest<TGetUserResponse>(
1985+
signedUserRequest,
1986+
).then((response) => getAuthenticatorAddresses(response.user));
19741987
}
19751988

19761989
// if connectedOnly is true, we skip fetching embedded wallets
@@ -1989,18 +2002,29 @@ export class TurnkeyClient {
19892002
);
19902003
}
19912004

2005+
// we stamp the wallet request first
2006+
// this is done to avoid concurrent passkey prompts
2007+
const signedWalletsRequest = await this.httpClient.stampGetWallets(
2008+
{
2009+
organizationId,
2010+
},
2011+
stampWith,
2012+
);
2013+
2014+
if (!signedWalletsRequest) {
2015+
throw new TurnkeyError(
2016+
"Failed to stamp wallet request",
2017+
TurnkeyErrorCodes.INVALID_REQUEST,
2018+
);
2019+
}
2020+
19922021
const [accounts, walletsRes] = await Promise.all([
19932022
fetchAllWalletAccountsWithCursor(
19942023
this.httpClient,
19952024
organizationId,
19962025
stampWith,
19972026
),
1998-
this.httpClient.getWallets(
1999-
{
2000-
organizationId,
2001-
},
2002-
stampWith,
2003-
),
2027+
sendSignedRequest<TGetWalletsResponse>(signedWalletsRequest),
20042028
]);
20052029

20062030
// create a map of walletId to EmbeddedWallet for easy lookup
@@ -2726,7 +2750,7 @@ export class TurnkeyClient {
27262750
);
27272751
}
27282752

2729-
return userResponse.user as v1User;
2753+
return userResponse.user;
27302754
},
27312755
{
27322756
errorMessage: "Failed to fetch user",

0 commit comments

Comments
 (0)