From b8fdceb29d2020706d32f0a147c3c53d523a2065 Mon Sep 17 00:00:00 2001 From: Andrew Min Date: Wed, 13 Nov 2024 10:46:50 +0700 Subject: [PATCH] feedback: pass in base url and rename flag --- examples/with-ethers/src/index.ts | 2 +- examples/with-solana/src/index.ts | 2 +- examples/with-viem/src/index.ts | 2 +- packages/ethers/src/index.ts | 23 +++++++++----- packages/http/src/base.ts | 2 +- packages/http/src/shared.ts | 8 ++--- packages/sdk-browser/src/__types__/base.ts | 2 +- packages/sdk-server/src/__types__/base.ts | 4 +-- packages/sdk-server/src/sdk-client.ts | 2 +- packages/solana/src/index.ts | 36 ++++++++++++++-------- packages/viem/src/index.ts | 23 +++++++++----- 11 files changed, 68 insertions(+), 38 deletions(-) diff --git a/examples/with-ethers/src/index.ts b/examples/with-ethers/src/index.ts index 9a39cdf44..ac7712f6d 100644 --- a/examples/with-ethers/src/index.ts +++ b/examples/with-ethers/src/index.ts @@ -47,7 +47,7 @@ async function main() { // ----- // // Optional flag to override timestamp using Turnkey's system clock - // overrideTimestamp: true, + // useTurnkeyRemoteTimestamp: true, }); // Initialize a Turnkey Signer diff --git a/examples/with-solana/src/index.ts b/examples/with-solana/src/index.ts index 842d4b3ba..909e1762c 100644 --- a/examples/with-solana/src/index.ts +++ b/examples/with-solana/src/index.ts @@ -50,7 +50,7 @@ async function main() { // ----- // // Optional flag to override timestamp using Turnkey's system clock - // overrideTimestamp: true, + // useTurnkeyRemoteTimestamp: true, }); const turnkeySigner = new TurnkeySigner({ diff --git a/examples/with-viem/src/index.ts b/examples/with-viem/src/index.ts index 3d5e7fe7d..d9c21ab4b 100644 --- a/examples/with-viem/src/index.ts +++ b/examples/with-viem/src/index.ts @@ -53,7 +53,7 @@ async function main() { // ----- // // Optional flag to override timestamp using Turnkey's system clock - // overrideTimestamp: true, + // useTurnkeyRemoteTimestamp: true, }); const turnkeyAccount = await createAccount({ diff --git a/packages/ethers/src/index.ts b/packages/ethers/src/index.ts index 9dabb75d4..8450dffeb 100644 --- a/packages/ethers/src/index.ts +++ b/packages/ethers/src/index.ts @@ -109,11 +109,11 @@ export class TurnkeySigner extends AbstractSigner implements ethers.Signer { private async _signTransactionImpl( unsignedTransaction: string ): Promise { - const timestampMs = this.client.config.overrideTimestamp - ? `${await getLiveTimestamp()}000` - : String(Date.now()); - if (this.client instanceof TurnkeyClient) { + const timestampMs = this.client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(this.client.config.baseUrl)}000` + : String(Date.now()); + const { activity } = await this.client.signTransaction({ type: "ACTIVITY_TYPE_SIGN_TRANSACTION_V2", organizationId: this.organizationId, @@ -131,6 +131,10 @@ export class TurnkeySigner extends AbstractSigner implements ethers.Signer { activity?.result?.signTransactionResult?.signedTransaction ); } else { + const timestampMs = this.client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(this.client.config.apiBaseUrl)}000` + : String(Date.now()); + const { activity, signedTransaction } = await this.client.signTransaction( { signWith: this.signWith, @@ -240,11 +244,12 @@ export class TurnkeySigner extends AbstractSigner implements ethers.Signer { async _signMessageImpl(message: string): Promise { let result; - const timestampMs = this.client.config.overrideTimestamp - ? `${await getLiveTimestamp()}000` - : String(Date.now()); if (this.client instanceof TurnkeyClient) { + const timestampMs = this.client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(this.client.config.baseUrl)}000` + : String(Date.now()); + const { activity } = await this.client.signRawPayload({ type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2", organizationId: this.organizationId, @@ -261,6 +266,10 @@ export class TurnkeySigner extends AbstractSigner implements ethers.Signer { result = assertNonNull(activity?.result?.signRawPayloadResult); } else { + const timestampMs = this.client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(this.client.config.apiBaseUrl)}000` + : String(Date.now()); + const { activity, r, s, v } = await this.client.signRawPayload({ signWith: this.signWith, payload: message, diff --git a/packages/http/src/base.ts b/packages/http/src/base.ts index 7187d3520..468e1492f 100644 --- a/packages/http/src/base.ts +++ b/packages/http/src/base.ts @@ -256,7 +256,7 @@ export async function sealAndStampRequestBody(input: { export type THttpConfig = { baseUrl: string; - overrideTimestamp?: boolean; + useTurnkeyRemoteTimestamp?: boolean; }; /** diff --git a/packages/http/src/shared.ts b/packages/http/src/shared.ts index fcd50682c..cc0a2446e 100644 --- a/packages/http/src/shared.ts +++ b/packages/http/src/shared.ts @@ -204,12 +204,12 @@ export function getSignedTransactionFromActivity( /** * - * A utility function to get a live timestmap directly from Turnkey + * A utility function to get a live stamp directly from Turnkey * - * @returns string reprenting timestamp in seconds + * @returns string representing the current UNIX timestamp in seconds. If it fails to fetch a timestamp from Turnkey, this returns an empty string. */ -export async function getLiveTimestamp(): Promise { - const timestampResponse = await fetch("https://api.turnkey.com/health"); +export async function getLiveTimestamp(baseUrl: string): Promise { + const timestampResponse = await fetch(`${baseUrl}/health`); const parsedResponse = await timestampResponse.json(); return parsedResponse.currentTime ?? ""; diff --git a/packages/sdk-browser/src/__types__/base.ts b/packages/sdk-browser/src/__types__/base.ts index 39e8a093e..f32b7087d 100644 --- a/packages/sdk-browser/src/__types__/base.ts +++ b/packages/sdk-browser/src/__types__/base.ts @@ -68,7 +68,7 @@ interface BaseSDKClientConfig { apiBaseUrl: string; organizationId: string; activityPoller?: TActivityPollerConfig | undefined; - overrideTimestamp?: boolean; + useTurnkeyRemoteTimestamp?: boolean; } interface SDKClientConfigWithStamper extends BaseSDKClientConfig { diff --git a/packages/sdk-server/src/__types__/base.ts b/packages/sdk-server/src/__types__/base.ts index a660f0962..89b5d80d8 100644 --- a/packages/sdk-server/src/__types__/base.ts +++ b/packages/sdk-server/src/__types__/base.ts @@ -78,7 +78,7 @@ export interface TurnkeySDKClientConfig { apiBaseUrl: string; organizationId: string; activityPoller?: TActivityPollerConfig | undefined; - overrideTimestamp?: boolean; + useTurnkeyRemoteTimestamp?: boolean; } export interface TurnkeySDKServerConfig { @@ -87,7 +87,7 @@ export interface TurnkeySDKServerConfig { apiPublicKey: string; defaultOrganizationId: string; activityPoller?: TActivityPollerConfig | undefined; - overrideTimestamp?: boolean; + useTurnkeyRemoteTimestamp?: boolean; } export interface TurnkeyProxyHandlerConfig { diff --git a/packages/sdk-server/src/sdk-client.ts b/packages/sdk-server/src/sdk-client.ts index 1f2136524..c7dbe26c3 100644 --- a/packages/sdk-server/src/sdk-client.ts +++ b/packages/sdk-server/src/sdk-client.ts @@ -42,7 +42,7 @@ export class TurnkeyServerSDK { apiBaseUrl: this.config.apiBaseUrl, organizationId: this.config.defaultOrganizationId, activityPoller: this.config.activityPoller, - overrideTimestamp: this.config.overrideTimestamp ?? false, + useTurnkeyRemoteTimestamp: this.config.useTurnkeyRemoteTimestamp ?? false, }); }; diff --git a/packages/solana/src/index.ts b/packages/solana/src/index.ts index fd852874e..ab5230764 100644 --- a/packages/solana/src/index.ts +++ b/packages/solana/src/index.ts @@ -128,11 +128,11 @@ export class TurnkeySigner { unsignedTransaction: string, signWith: string ) { - const timestampMs = this.client.config.overrideTimestamp - ? `${await getLiveTimestamp()}000` - : String(Date.now()); - if (this.client instanceof TurnkeyClient) { + const timestampMs = this.client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(this.client.config.baseUrl)}000` + : String(Date.now()); + const response = await this.client.signTransaction({ type: "ACTIVITY_TYPE_SIGN_TRANSACTION_V2", organizationId: this.organizationId, @@ -152,6 +152,10 @@ export class TurnkeySigner { activity?.result?.signTransactionResult?.signedTransaction ); } else { + const timestampMs = this.client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(this.client.config.apiBaseUrl)}000` + : String(Date.now()); + const { activity, signedTransaction } = await this.client.signTransaction( { signWith, @@ -168,11 +172,11 @@ export class TurnkeySigner { } private async signRawPayload(payload: string, signWith: string) { - const timestampMs = this.client.config.overrideTimestamp - ? `${await getLiveTimestamp()}000` - : String(Date.now()); - if (this.client instanceof TurnkeyClient) { + const timestampMs = this.client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(this.client.config.baseUrl)}000` + : String(Date.now()); + const response = await this.client.signRawPayload({ type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2", organizationId: this.organizationId, @@ -193,6 +197,10 @@ export class TurnkeySigner { return assertNonNull(activity?.result?.signRawPayloadResult); } else { + const timestampMs = this.client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(this.client.config.apiBaseUrl)}000` + : String(Date.now()); + const { activity, r, s, v } = await this.client.signRawPayload({ signWith, payload, @@ -214,11 +222,11 @@ export class TurnkeySigner { } private async signRawPayloads(payloads: string[], signWith: string) { - const timestampMs = this.client.config.overrideTimestamp - ? `${await getLiveTimestamp()}000` - : String(Date.now()); - if (this.client instanceof TurnkeyClient) { + const timestampMs = this.client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(this.client.config.baseUrl)}000` + : String(Date.now()); + const response = await this.client.signRawPayloads({ type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOADS", organizationId: this.organizationId, @@ -239,6 +247,10 @@ export class TurnkeySigner { return assertNonNull(activity?.result?.signRawPayloadsResult); } else { + const timestampMs = this.client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(this.client.config.apiBaseUrl)}000` + : String(Date.now()); + const { activity, signatures } = await this.client.signRawPayloads({ signWith, payloads, diff --git a/packages/viem/src/index.ts b/packages/viem/src/index.ts index 20aa9a75e..0c38a3315 100644 --- a/packages/viem/src/index.ts +++ b/packages/viem/src/index.ts @@ -385,11 +385,11 @@ async function signTransactionImpl( organizationId: string, signWith: string ): Promise { - const timestampMs = client.config.overrideTimestamp - ? `${await getLiveTimestamp()}000` - : String(Date.now()); - if (client instanceof TurnkeyClient) { + const timestampMs = client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(client.config.baseUrl)}000` + : String(Date.now()); + const { activity } = await client.signTransaction({ type: "ACTIVITY_TYPE_SIGN_TRANSACTION_V2", organizationId: organizationId, @@ -407,6 +407,10 @@ async function signTransactionImpl( activity?.result?.signTransactionResult?.signedTransaction ); } else { + const timestampMs = client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(client.config.apiBaseUrl)}000` + : String(Date.now()); + const { activity, signedTransaction } = await client.signTransaction({ signWith, type: "TRANSACTION_TYPE_ETHEREUM", @@ -467,11 +471,12 @@ async function signMessageImpl( signWith: string ): Promise { let result; - const timestampMs = client.config.overrideTimestamp - ? `${await getLiveTimestamp()}000` - : String(Date.now()); if (client instanceof TurnkeyClient) { + const timestampMs = client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(client.config.baseUrl)}000` + : String(Date.now()); + const { activity } = await client.signRawPayload({ type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2", organizationId: organizationId, @@ -488,6 +493,10 @@ async function signMessageImpl( result = assertNonNull(activity?.result?.signRawPayloadResult); } else { + const timestampMs = client.config.useTurnkeyRemoteTimestamp + ? `${await getLiveTimestamp(client.config.apiBaseUrl)}000` + : String(Date.now()); + const { activity, r, s, v } = await client.signRawPayload({ signWith, payload: message,