Skip to content

Commit

Permalink
Merge pull-request #431
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Nov 25, 2024
2 parents f7066ec + 9eaf38a commit 601498e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-jeans-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@turnkey/solana": patch
---

Add optional org id for all signing methods
35 changes: 24 additions & 11 deletions packages/solana/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ export class TurnkeySigner {
*/
public async signAllTransactions(
txs: (Transaction | VersionedTransaction)[],
fromAddress: string
fromAddress: string,
organizationId?: string
): Promise<(Transaction | VersionedTransaction)[]> {
const fromKey = new PublicKey(fromAddress);

let messages = txs.map((tx) => this.getMessageToSign(tx).toString("hex"));

const signRawPayloadsResult = await this.signRawPayloads(
messages,
fromAddress
fromAddress,
organizationId
);

const signatures = signRawPayloadsResult?.signatures?.map(
Expand Down Expand Up @@ -65,7 +67,7 @@ export class TurnkeySigner {
const signRawPayloadResult = await this.signRawPayload(
messageToSign.toString("hex"),
fromAddress,
organizationId!
organizationId ?? this.organizationId
);
const signature = `${signRawPayloadResult?.r}${signRawPayloadResult?.s}`;

Expand All @@ -80,11 +82,13 @@ export class TurnkeySigner {
*/
public async signMessage(
message: Uint8Array,
fromAddress: string
fromAddress: string,
organizationId?: string
): Promise<Uint8Array> {
const signRawPayloadResult = await this.signRawPayload(
Buffer.from(message).toString("hex"),
fromAddress
fromAddress,
organizationId
);
return Buffer.from(
`${signRawPayloadResult?.r}${signRawPayloadResult?.s}`,
Expand All @@ -101,7 +105,8 @@ export class TurnkeySigner {
*/
public async signTransaction(
tx: Transaction | VersionedTransaction,
fromAddress: string
fromAddress: string,
organizationId?: string
): Promise<Transaction | VersionedTransaction> {
const payloadToSign = Buffer.from(
tx.serialize({
Expand All @@ -112,7 +117,8 @@ export class TurnkeySigner {

const signedTransaction = await this.signTransactionImpl(
payloadToSign,
fromAddress
fromAddress,
organizationId
);

const decodedTransaction = Buffer.from(signedTransaction, "hex");
Expand All @@ -127,12 +133,13 @@ export class TurnkeySigner {

private async signTransactionImpl(
unsignedTransaction: string,
signWith: string
signWith: string,
organizationId?: string
) {
if (this.client instanceof TurnkeyClient) {
const response = await this.client.signTransaction({
type: "ACTIVITY_TYPE_SIGN_TRANSACTION_V2",
organizationId: this.organizationId,
organizationId: organizationId ?? this.organizationId,
timestampMs: String(Date.now()),
parameters: {
signWith,
Expand All @@ -151,6 +158,7 @@ export class TurnkeySigner {
} else {
const { activity, signedTransaction } = await this.client.signTransaction(
{
...(organizationId !== undefined && { organizationId }),
signWith,
unsignedTransaction,
type: "TRANSACTION_TYPE_SOLANA",
Expand Down Expand Up @@ -209,11 +217,15 @@ export class TurnkeySigner {
}
}

private async signRawPayloads(payloads: string[], signWith: string) {
private async signRawPayloads(
payloads: string[],
signWith: string,
organizationId?: string
) {
if (this.client instanceof TurnkeyClient) {
const response = await this.client.signRawPayloads({
type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOADS",
organizationId: this.organizationId,
organizationId: organizationId ?? this.organizationId,
timestampMs: String(Date.now()),
parameters: {
signWith,
Expand All @@ -232,6 +244,7 @@ export class TurnkeySigner {
return assertNonNull(activity?.result?.signRawPayloadsResult);
} else {
const { activity, signatures } = await this.client.signRawPayloads({
...(organizationId !== undefined && { organizationId }),
signWith,
payloads,
encoding: "PAYLOAD_ENCODING_HEXADECIMAL",
Expand Down

0 comments on commit 601498e

Please sign in to comment.