-
Notifications
You must be signed in to change notification settings - Fork 53
added generic client type that only requires specifc methods from our clients, that way version bumps are cross compatible #1203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@turnkey/viem": minor | ||
| --- | ||
|
|
||
| Replaced concrete client types with a generic method-based interface to avoid type errors and make client types version-agnostic. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,11 +35,86 @@ import { | |
| TurnkeyActivityConsensusNeededError as TurnkeyHttpActivityConsensusNeededError, | ||
| TurnkeyClient, | ||
| type TurnkeyApiTypes, | ||
| type TurnkeyApi as HttpApiTypes, | ||
| } from "@turnkey/http"; | ||
| import { ApiKeyStamper } from "@turnkey/api-key-stamper"; | ||
| import type { TurnkeyBrowserClient } from "@turnkey/sdk-browser"; | ||
| import type { TurnkeySDKClientBase } from "@turnkey/core"; | ||
| import type { TurnkeyServerClient } from "@turnkey/sdk-server"; | ||
| import type { TurnkeySDKApiTypes as BrowserApiTypes } from "@turnkey/sdk-browser"; | ||
| import type { TurnkeySDKApiTypes as ServerApiTypes } from "@turnkey/sdk-server"; | ||
| import type { | ||
| TGetPrivateKeyBody as SdkTypesGetPrivateKeyBody, | ||
| TSignTransactionBody as SdkTypesSignTransactionBody, | ||
| TSignRawPayloadBody as SdkTypesSignRawPayloadBody, | ||
| } from "@turnkey/sdk-types"; | ||
|
|
||
| /** | ||
| * Union types for inputs — accept the body format from any SDK package. | ||
| */ | ||
| export type TGetPrivateKeyBody = | ||
| | BrowserApiTypes.TGetPrivateKeyBody | ||
| | ServerApiTypes.TGetPrivateKeyBody | ||
| | SdkTypesGetPrivateKeyBody | ||
| | HttpApiTypes.TGetPrivateKeyBody; | ||
|
|
||
| export type TSignTransactionBody = | ||
| | BrowserApiTypes.TSignTransactionBody | ||
| | ServerApiTypes.TSignTransactionBody | ||
| | SdkTypesSignTransactionBody | ||
| | HttpApiTypes.TSignTransactionBody; | ||
|
|
||
| export type TSignRawPayloadBody = | ||
| | BrowserApiTypes.TSignRawPayloadBody | ||
| | ServerApiTypes.TSignRawPayloadBody | ||
| | SdkTypesSignRawPayloadBody | ||
| | HttpApiTypes.TSignRawPayloadBody; | ||
|
|
||
| /** | ||
| * Minimal response types — only the fields the viem package actually reads. | ||
| * This makes responses version-agnostic; any SDK version will satisfy these. | ||
| */ | ||
| type TGetPrivateKeyResponse = { | ||
| privateKey: { | ||
| addresses: Array<{ format?: string; address?: string }>; | ||
| }; | ||
| }; | ||
|
|
||
| type TSignTransactionResponse = { | ||
| activity: { | ||
| id: string; | ||
| status: string; | ||
| result: { | ||
| signTransactionResult?: { | ||
| signedTransaction?: string; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| type TSignRawPayloadResponse = { | ||
| activity: { | ||
| id: string; | ||
| status: string; | ||
| result: { | ||
| signRawPayloadResult?: { | ||
| r?: string; | ||
| s?: string; | ||
| v?: string; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Generic client interface for any Turnkey client (HTTP, browser SDK, server SDK, core, etc.). | ||
| */ | ||
| export interface TurnkeyClientInterface { | ||
| getPrivateKey(input: TGetPrivateKeyBody): Promise<TGetPrivateKeyResponse>; | ||
| signTransaction( | ||
| input: TSignTransactionBody, | ||
| ): Promise<TSignTransactionResponse>; | ||
| signRawPayload(input: TSignRawPayloadBody): Promise<TSignRawPayloadResponse>; | ||
| } | ||
|
|
||
| export type TTurnkeyClient = TurnkeyClientInterface; | ||
|
|
||
| export type TTurnkeyConsensusNeededErrorType = TurnkeyConsensusNeededError & { | ||
| name: "TurnkeyConsensusNeededError"; | ||
|
|
@@ -124,11 +199,7 @@ export class TurnkeyActivityError extends BaseError { | |
| } | ||
|
|
||
| export function createAccountWithAddress(input: { | ||
| client: | ||
| | TurnkeyClient | ||
| | TurnkeyBrowserClient | ||
| | TurnkeyServerClient | ||
| | TurnkeySDKClientBase; | ||
| client: TTurnkeyClient; | ||
| organizationId: string; | ||
| // This can be a wallet account address, private key address, or private key ID. | ||
| signWith: string; | ||
|
|
@@ -202,11 +273,7 @@ export function createAccountWithAddress(input: { | |
| } | ||
|
|
||
| export async function createAccount(input: { | ||
| client: | ||
| | TurnkeyClient | ||
| | TurnkeyBrowserClient | ||
| | TurnkeyServerClient | ||
| | TurnkeySDKClientBase; | ||
| client: TTurnkeyClient; | ||
| organizationId: string; | ||
| // This can be a wallet account address, private key address, or private key ID. | ||
| signWith: string; | ||
|
|
@@ -371,11 +438,7 @@ export async function createApiKeyAccount( | |
| } | ||
|
|
||
| export async function signAuthorization( | ||
| client: | ||
| | TurnkeyClient | ||
| | TurnkeyBrowserClient | ||
| | TurnkeyServerClient | ||
| | TurnkeySDKClientBase, | ||
| client: TTurnkeyClient, | ||
| parameters: TSignAuthorizationParameters, | ||
| organizationId: string, | ||
| signWith: string, | ||
|
|
@@ -417,11 +480,7 @@ export async function signAuthorization( | |
| } | ||
|
|
||
| export async function signMessage( | ||
| client: | ||
| | TurnkeyClient | ||
| | TurnkeyBrowserClient | ||
| | TurnkeyServerClient | ||
| | TurnkeySDKClientBase, | ||
| client: TTurnkeyClient, | ||
| message: SignableMessage, | ||
| organizationId: string, | ||
| signWith: string, | ||
|
|
@@ -438,11 +497,7 @@ export async function signMessage( | |
| export async function signTransaction< | ||
| TTransactionSerializable extends TransactionSerializable, | ||
| >( | ||
| client: | ||
| | TurnkeyClient | ||
| | TurnkeyBrowserClient | ||
| | TurnkeyServerClient | ||
| | TurnkeySDKClientBase, | ||
| client: TTurnkeyClient, | ||
| transaction: TTransactionSerializable, | ||
| serializer: SerializeTransactionFn<TTransactionSerializable>, | ||
| organizationId: string, | ||
|
|
@@ -485,11 +540,7 @@ export async function signTransaction< | |
| } | ||
|
|
||
| export async function signTypedData( | ||
| client: | ||
| | TurnkeyClient | ||
| | TurnkeyBrowserClient | ||
| | TurnkeyServerClient | ||
| | TurnkeySDKClientBase, | ||
| client: TTurnkeyClient, | ||
| data: TypedData | { [key: string]: unknown }, | ||
| organizationId: string, | ||
| signWith: string, | ||
|
|
@@ -505,11 +556,7 @@ export async function signTypedData( | |
| } | ||
|
|
||
| async function signTransactionWithErrorWrapping( | ||
| client: | ||
| | TurnkeyClient | ||
| | TurnkeyBrowserClient | ||
| | TurnkeyServerClient | ||
| | TurnkeySDKClientBase, | ||
| client: TTurnkeyClient, | ||
| unsignedTransaction: string, | ||
| organizationId: string, | ||
| signWith: string, | ||
|
|
@@ -551,11 +598,7 @@ async function signTransactionWithErrorWrapping( | |
| } | ||
|
|
||
| async function signTransactionImpl( | ||
| client: | ||
| | TurnkeyClient | ||
| | TurnkeyBrowserClient | ||
| | TurnkeyServerClient | ||
| | TurnkeySDKClientBase, | ||
| client: TTurnkeyClient, | ||
| unsignedTransaction: string, | ||
| organizationId: string, | ||
| signWith: string, | ||
|
|
@@ -579,7 +622,7 @@ async function signTransactionImpl( | |
| activity?.result?.signTransactionResult?.signedTransaction, | ||
| ); | ||
| } else { | ||
| const { activity, signedTransaction } = await client.signTransaction({ | ||
| const { activity } = await client.signTransaction({ | ||
| organizationId, | ||
| signWith, | ||
| type: transactionType ?? "TRANSACTION_TYPE_ETHEREUM", | ||
|
|
@@ -590,16 +633,14 @@ async function signTransactionImpl( | |
| activity as any /* Type casting is ok here. The invalid types are both actually strings. TS is too strict here! */, | ||
| ); | ||
|
|
||
| const signedTransaction = | ||
| activity.result.signTransactionResult?.signedTransaction; | ||
| return assertNonNull(signedTransaction); | ||
| } | ||
| } | ||
|
|
||
| async function signMessageWithErrorWrapping( | ||
| client: | ||
| | TurnkeyClient | ||
| | TurnkeyBrowserClient | ||
| | TurnkeyServerClient | ||
| | TurnkeySDKClientBase, | ||
| client: TTurnkeyClient, | ||
| message: string, | ||
| organizationId: string, | ||
| signWith: string, | ||
|
|
@@ -644,11 +685,7 @@ async function signMessageWithErrorWrapping( | |
| } | ||
|
|
||
| async function signMessageImpl( | ||
| client: | ||
| | TurnkeyClient | ||
| | TurnkeyBrowserClient | ||
| | TurnkeyServerClient | ||
| | TurnkeySDKClientBase, | ||
| client: TTurnkeyClient, | ||
| message: string, | ||
| organizationId: string, | ||
| signWith: string, | ||
|
|
@@ -674,7 +711,7 @@ async function signMessageImpl( | |
|
|
||
| result = assertNonNull(activity?.result?.signRawPayloadResult); | ||
| } else { | ||
| const { activity, r, s, v } = await client.signRawPayload({ | ||
| const { activity } = await client.signRawPayload({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same thing goes here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tested and we chillin, |
||
| organizationId, | ||
| signWith, | ||
| payload: message, | ||
|
|
@@ -687,9 +724,9 @@ async function signMessageImpl( | |
| ); | ||
|
|
||
| result = { | ||
| r, | ||
| s, | ||
| v, | ||
| r: assertNonNull(activity?.result?.signRawPayloadResult?.r), | ||
| s: assertNonNull(activity?.result?.signRawPayloadResult?.s), | ||
| v: assertNonNull(activity?.result?.signRawPayloadResult?.v), | ||
| }; | ||
| } | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m wondering why we’re still conditionally rendering based on
isHttpClient(). Will that even work? I don’t think so 🤔with your changes,
isHttpClient()will now always return false, which means we’ll always hit the same code path. That makes this conditional statment deadbut the bigger issue is how we’re constructing and sending the request. The reason we differentiate
httpClientfrom other clients isn’t because the response shape is different, it’s because the intent format is differentfor
httpClient, the intent is structured like this:whereas for other clients, the intent is structured like this:
so we would POST the request to the Turnkey without the required
type: "ACTIVITY_TYPE_...", ``timestampMs, andparamaters` wrapperwhich would fail :(