Skip to content

Commit

Permalink
feedback: pass in base url and rename flag
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Nov 13, 2024
1 parent 12e73c6 commit b8fdceb
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 38 deletions.
2 changes: 1 addition & 1 deletion examples/with-ethers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/with-solana/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion examples/with-viem/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
23 changes: 16 additions & 7 deletions packages/ethers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ export class TurnkeySigner extends AbstractSigner implements ethers.Signer {
private async _signTransactionImpl(
unsignedTransaction: string
): Promise<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 { activity } = await this.client.signTransaction({
type: "ACTIVITY_TYPE_SIGN_TRANSACTION_V2",
organizationId: this.organizationId,
Expand All @@ -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,
Expand Down Expand Up @@ -240,11 +244,12 @@ export class TurnkeySigner extends AbstractSigner implements ethers.Signer {

async _signMessageImpl(message: string): Promise<string> {
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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export async function sealAndStampRequestBody(input: {

export type THttpConfig = {
baseUrl: string;
overrideTimestamp?: boolean;
useTurnkeyRemoteTimestamp?: boolean;
};

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/http/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
const timestampResponse = await fetch("https://api.turnkey.com/health");
export async function getLiveTimestamp(baseUrl: string): Promise<string> {
const timestampResponse = await fetch(`${baseUrl}/health`);
const parsedResponse = await timestampResponse.json();

return parsedResponse.currentTime ?? "";
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-browser/src/__types__/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ interface BaseSDKClientConfig {
apiBaseUrl: string;
organizationId: string;
activityPoller?: TActivityPollerConfig | undefined;
overrideTimestamp?: boolean;
useTurnkeyRemoteTimestamp?: boolean;
}

interface SDKClientConfigWithStamper extends BaseSDKClientConfig {
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-server/src/__types__/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export interface TurnkeySDKClientConfig {
apiBaseUrl: string;
organizationId: string;
activityPoller?: TActivityPollerConfig | undefined;
overrideTimestamp?: boolean;
useTurnkeyRemoteTimestamp?: boolean;
}

export interface TurnkeySDKServerConfig {
Expand All @@ -87,7 +87,7 @@ export interface TurnkeySDKServerConfig {
apiPublicKey: string;
defaultOrganizationId: string;
activityPoller?: TActivityPollerConfig | undefined;
overrideTimestamp?: boolean;
useTurnkeyRemoteTimestamp?: boolean;
}

export interface TurnkeyProxyHandlerConfig {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-server/src/sdk-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
};

Expand Down
36 changes: 24 additions & 12 deletions packages/solana/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
23 changes: 16 additions & 7 deletions packages/viem/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,11 @@ async function signTransactionImpl(
organizationId: string,
signWith: string
): Promise<string> {
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,
Expand All @@ -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",
Expand Down Expand Up @@ -467,11 +471,12 @@ async function signMessageImpl(
signWith: string
): Promise<string> {
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,
Expand All @@ -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,
Expand Down

0 comments on commit b8fdceb

Please sign in to comment.