Skip to content

Commit

Permalink
Partial sign transactions *after* modifying them
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher committed May 13, 2024
1 parent 5a538a4 commit dbf9bb8
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions packages/secure-clients/src/SolanaClient/BackpackSolanaWallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Blockchain } from "@coral-xyz/common";
import type { SVMClient } from "@coral-xyz/secure-background/clients";
import type { SecureEvent } from "@coral-xyz/secure-background/types";
import type {
SolanaSignInInput} from "@solana/wallet-standard-features";
import {
SolanaSignInOutput,
} from "@solana/wallet-standard-features";
import type {
Commitment,
ConfirmOptions,
Expand All @@ -26,7 +31,6 @@ import {
deserializeTransaction,
isVersionedTransaction,
} from "./utils/transaction-helpers";
import { SolanaSignInInput, SolanaSignInOutput } from "@solana/wallet-standard-features"

export class BackpackSolanaWallet {
private secureSvmClient: SVMClient;
Expand Down Expand Up @@ -135,17 +139,15 @@ export class BackpackSolanaWallet {
return decode(svmResponse.response.signedMessage);
}

public async signIn(
input?: SolanaSignInInput
): Promise<{
public async signIn(input?: SolanaSignInInput): Promise<{
signedMessage: string;
signature: string;
publicKey: string;
connectionUrl: string;
}> {
const svmResponse = await this.secureSvmClient.signIn({
blockchain: Blockchain.SOLANA,
input
input,
});
if (!svmResponse.response) {
throw svmResponse.error;
Expand All @@ -154,7 +156,7 @@ export class BackpackSolanaWallet {
}

private async prepareTransaction<
T extends Transaction | VersionedTransaction
T extends Transaction | VersionedTransaction,
>(request: {
publicKey: PublicKey;
tx: T;
Expand All @@ -169,18 +171,18 @@ export class BackpackSolanaWallet {
const commitment = request.commitment;

if (!isVersionedTransaction(tx)) {
if (signers) {
signers.forEach((s: Signer) => {
tx.partialSign(s);
});
}
if (!tx.feePayer) {
tx.feePayer = publicKey;
}
if (!tx.recentBlockhash) {
const { blockhash } = await connection.getLatestBlockhash(commitment);
tx.recentBlockhash = blockhash;
}
if (signers) {
signers.forEach((s: Signer) => {
tx.partialSign(s);
});
}
} else {
if (signers) {
tx.sign(signers);
Expand Down Expand Up @@ -227,7 +229,7 @@ export class BackpackSolanaWallet {
}

public async signAllTransactions<
T extends Transaction | VersionedTransaction
T extends Transaction | VersionedTransaction,
>(
request: {
publicKey: PublicKey;
Expand Down Expand Up @@ -366,22 +368,22 @@ export class BackpackSolanaWallet {
const signersOrConf =
"message" in tx
? ({
accounts: {
encoding: "base64",
addresses: [
...(request.includedAccounts ?? []),
publicKey.toString(),
],
},
} as SimulateTransactionConfig)
accounts: {
encoding: "base64",
addresses: [
...(request.includedAccounts ?? []),
publicKey.toString(),
],
},
} as SimulateTransactionConfig)
: undefined;

const response = await (isVersionedTransaction(preparedTx)
? connection.simulateTransaction(preparedTx, signersOrConf)
: this.connection.simulateTransaction(preparedTx, undefined, [
...(request.includedAccounts?.map((p) => new PublicKey(p)) ?? []),
publicKey,
]));
...(request.includedAccounts?.map((p) => new PublicKey(p)) ?? []),
publicKey,
]));

return response.value;
}
Expand Down

0 comments on commit dbf9bb8

Please sign in to comment.