Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 51 additions & 16 deletions packages/app-solana/src/operations/signTxn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,57 @@ export const signTxn = async (
let serializedTxnHex: string | undefined;

if (params.serializeTxn) {
const solanaWeb3 = getSolanaWeb3();
const transaction = solanaWeb3.Transaction.populate(
solanaWeb3.Message.from(Buffer.from(params.txn, 'hex')),
);
if (latestBlockHash) transaction.recentBlockhash = latestBlockHash;
assert(
transaction.feePayer,
new Error('Cannot decode feePayer in solana txn'),
);
transaction.addSignature(
transaction.feePayer,
Buffer.from(signature, 'hex'),
);
const serializedTxnBuffer = transaction.serialize();
serializedTxnHex = uint8ArrayToHex(serializedTxnBuffer);
serializedTxn = base58Encode(serializedTxnBuffer);
const msgBytes = Buffer.from(params.txn, 'hex');
const isVersioned = msgBytes.length > 0 && msgBytes[0] === 0x80;

if (isVersioned) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to handle versioned transactions manually? Does SolanaWeb3 not support versioned transactions?

const updatedMsgBytes = Buffer.from(msgBytes);
if (latestBlockHash) {
const bhBytes = Buffer.from(base58Decode(latestBlockHash));
/* Locate blockhash in v0 message:
[0x80 version] [3 header bytes] [compact-u16 acct count] [acct keys * 32] [32 blockhash]
See: https://solana.com/docs/core/transactions/versioned-transactions */
let off = 4;
let acctCount = 0;
let shift = 0;
while (off < updatedMsgBytes.length) {
const b = updatedMsgBytes[off];
off += 1;
acctCount += (b % 128) * 2 ** shift;
if (b < 128) break;
shift += 7;
}
off += acctCount * 32;
bhBytes.copy(updatedMsgBytes, off);
}

const sigCount = Buffer.from([0x01]);
const sigBytes = Buffer.from(signature, 'hex');
const serializedTxnBuffer = Buffer.concat([
sigCount,
sigBytes,
updatedMsgBytes,
]);
serializedTxnHex = uint8ArrayToHex(serializedTxnBuffer);
serializedTxn = base58Encode(serializedTxnBuffer);
} else {
const solanaWeb3 = getSolanaWeb3();
const transaction = solanaWeb3.Transaction.populate(
solanaWeb3.Message.from(msgBytes),
);
if (latestBlockHash) transaction.recentBlockhash = latestBlockHash;
assert(
transaction.feePayer,
new Error('Cannot decode feePayer in solana txn'),
);
transaction.addSignature(
transaction.feePayer,
Buffer.from(signature, 'hex'),
);
const serializedTxnBuffer = transaction.serialize();
serializedTxnHex = uint8ArrayToHex(serializedTxnBuffer);
serializedTxn = base58Encode(serializedTxnBuffer);
}
}

return {
Expand Down
Loading