diff --git a/packages/solana/src/index.ts b/packages/solana/src/index.ts index 17f45102..8f0d3d74 100644 --- a/packages/solana/src/index.ts +++ b/packages/solana/src/index.ts @@ -7,6 +7,7 @@ import { } from "@turnkey/http"; import type { TurnkeyBrowserClient } from "@turnkey/sdk-browser"; import type { TurnkeyServerClient, TurnkeyApiTypes } from "@turnkey/sdk-server"; +import { sign } from "crypto"; type TClient = TurnkeyClient | TurnkeyBrowserClient | TurnkeyServerClient; @@ -26,19 +27,44 @@ export class TurnkeySigner { * @param fromAddress Solana address (base58 encoded) */ public async signAllTransactions( - txs: (Transaction | VersionedTransaction)[], - fromAddress: string, + // txs: (Transaction | VersionedTransaction)[], + txs: { + signWith: string; + transactions: (Transaction | VersionedTransaction)[]; + }[], organizationId?: string - ): Promise<(Transaction | VersionedTransaction)[]> { - const fromKey = new PublicKey(fromAddress); + ): Promise<({ + signWith: string; + transactions: (Transaction | VersionedTransaction)[] + }[])> { + const signingPayloads: { + signWith: string; + payloads: string[] + }[] = []; + + for (const tx of txs) { + const messages = tx.transactions.map((innerTx) => this.getMessageToSign(innerTx).toString("hex")); + + signingPayloads.push({ + signWith: tx.signWith, + payloads: messages + }) + } - let messages = txs.map((tx) => this.getMessageToSign(tx).toString("hex")); + const { signWithResults } = await this.signRawPayloads(signingPayloads, organizationId); - const signRawPayloadsResult = await this.signRawPayloads( - messages, - fromAddress, - organizationId - ); + const signedTxs: { + signWith: string; + transactions: (Transaction | VersionedTransaction)[]; + }[] = []; + + // for (const result of signWithResults) { + + // signedTxs.push({ + // signWith: result.signWith, + + // }) + // } const signatures = signRawPayloadsResult?.signatures?.map( (sig: TSignature) => `${sig?.r}${sig?.s}` @@ -218,18 +244,19 @@ export class TurnkeySigner { } private async signRawPayloads( - payloads: string[], - signWith: string, + signWithPayloads: { + signWith: string; + payloads: string[]; + }[], organizationId?: string ) { if (this.client instanceof TurnkeyClient) { const response = await this.client.signRawPayloads({ - type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOADS", + type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOADS_V2", organizationId: organizationId ?? this.organizationId, timestampMs: String(Date.now()), parameters: { - signWith, - payloads, + signWithPayloads, encoding: "PAYLOAD_ENCODING_HEXADECIMAL", // Note: unlike ECDSA, EdDSA's API does not support signing raw digests (see RFC 8032). // Turnkey's signer requires an explicit value to be passed here to minimize ambiguity. @@ -241,12 +268,11 @@ export class TurnkeySigner { assertActivityCompleted(activity); - return assertNonNull(activity?.result?.signRawPayloadsResult); + return assertNonNull(activity?.result?.signRawPayloadsResultV2); } else { - const { activity, signatures } = await this.client.signRawPayloads({ + const { activity, signWithResults } = await this.client.signRawPayloads({ ...(organizationId !== undefined && { organizationId }), - signWith, - payloads, + signWithPayloads, encoding: "PAYLOAD_ENCODING_HEXADECIMAL", // Note: unlike ECDSA, EdDSA's API does not support signing raw digests (see RFC 8032). // Turnkey's signer requires an explicit value to be passed here to minimize ambiguity. @@ -256,7 +282,7 @@ export class TurnkeySigner { assertActivityCompleted(activity); return assertNonNull({ - signatures: signatures as TurnkeyApiTypes["v1SignRawPayloadResult"][], + signWithResults, }); } }