Skip to content

Commit

Permalink
wip: sign raw payloads v2
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Dec 30, 2024
1 parent e5fa4a4 commit 799d873
Showing 1 changed file with 46 additions and 20 deletions.
66 changes: 46 additions & 20 deletions packages/solana/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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}`
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -256,7 +282,7 @@ export class TurnkeySigner {
assertActivityCompleted(activity);

return assertNonNull({
signatures: signatures as TurnkeyApiTypes["v1SignRawPayloadResult"][],
signWithResults,
});
}
}
Expand Down

0 comments on commit 799d873

Please sign in to comment.