Skip to content

Commit

Permalink
refactor(experimental): optimise signTransaction to only encode the m…
Browse files Browse the repository at this point in the history
…essage once
  • Loading branch information
mcintyre94 committed Oct 27, 2023
1 parent 6910664 commit 52cb8a8
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/transactions/src/signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isSignerRole } from '@solana/instructions';
import { Ed25519Signature, signBytes } from '@solana/keys';

import { ITransactionWithFeePayer } from './fee-payer';
import { CompiledMessage, compileMessage } from './message';
import { compileMessage } from './message';
import { getCompiledMessageEncoder } from './serializers/message';

export interface IFullySignedTransaction extends ITransactionWithSignatures {
Expand Down Expand Up @@ -63,8 +63,7 @@ export function isTransactionSignature(
return true;
}

async function getCompiledMessageSignature(message: CompiledMessage, secretKey: CryptoKey) {
const wireMessageBytes = getCompiledMessageEncoder().encode(message);
async function getCompiledMessageSignature(wireMessageBytes: Uint8Array, secretKey: CryptoKey) {
const signature = await signBytes(secretKey, wireMessageBytes);
return signature;
}
Expand All @@ -91,11 +90,12 @@ export async function signTransaction<TTransaction extends Parameters<typeof com
const compiledMessage = compileMessage(transaction);
const nextSignatures: Record<Base58EncodedAddress, Ed25519Signature> =
'signatures' in transaction ? { ...transaction.signatures } : {};
const wireMessageBytes = getCompiledMessageEncoder().encode(compiledMessage);
const publicKeySignaturePairs = await Promise.all(
keyPairs.map(keyPair =>
Promise.all([
getAddressFromPublicKey(keyPair.publicKey),
getCompiledMessageSignature(compiledMessage, keyPair.privateKey),
getCompiledMessageSignature(wireMessageBytes, keyPair.privateKey),
])
)
);
Expand Down

0 comments on commit 52cb8a8

Please sign in to comment.