diff --git a/web3.js/src/transaction.ts b/web3.js/src/transaction.ts index 7d6e0eb4cd1b03..5c189b49833f9a 100644 --- a/web3.js/src/transaction.ts +++ b/web3.js/src/transaction.ts @@ -395,13 +395,16 @@ export class Transaction { // Sort. Prioritizing first by signer, then by writable uniqueMetas.sort(function (x, y) { - const pubkeySorting = x.pubkey - .toBase58() - .localeCompare(y.pubkey.toBase58()); - const checkSigner = x.isSigner === y.isSigner ? 0 : x.isSigner ? -1 : 1; - const checkWritable = - x.isWritable === y.isWritable ? pubkeySorting : x.isWritable ? -1 : 1; - return checkSigner || checkWritable; + if (x.isSigner !== y.isSigner) { + // Signers always come before non-signers + return x.isSigner ? -1 : 1; + } + if (x.isWritable !== y.isWritable) { + // Writable accounts always come before read-only accounts + return x.isWritable ? -1 : 1; + } + // Otherwise, sort by pubkey. + return x.pubkey._bn.cmp(y.pubkey._bn); }); // Move fee payer to the front