Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/fix solana pay vtx #411

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 9 additions & 17 deletions src/components/payments/SolanaPay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,15 @@ onMounted(async () => {
invalidLink.value = "Invalid Link";
} else if (isUrl(requestLink)) {
// request link is an url. fetch transaction from url
const result = await parseSolanaPayRequestLink(requestLink, ControllerModule.selectedAddress, ControllerModule.connection);
if (result.transaction.feePayer && result.transaction.recentBlockhash) {
const messageV0 = new TransactionMessage({
payerKey: result.transaction.feePayer,
instructions: result.transaction.instructions,
recentBlockhash: result.transaction.recentBlockhash,
}).compileToV0Message();
log.info(result);
transaction.value = new VersionedTransaction(messageV0);
linkParams.value = {
icon: result.icon,
label: result.label,
origin: result.link.origin,
decodedInst: result.decodedInst,
message: result.message || "",
};
}
const result = await parseSolanaPayRequestLink(requestLink, ControllerModule.selectedAddress);
transaction.value = result.transaction;
linkParams.value = {
icon: result.icon,
label: result.label,
origin: result.link.origin,
decodedInst: result.decodedInst,
message: result.message || "",
};
} else {
// check for publickey format, redirect to transfer page
try {
Expand Down
50 changes: 26 additions & 24 deletions src/utils/solanaHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
SimulatedTransactionResponse,
SystemInstruction,
SystemProgram,
Transaction,
TransactionInstruction,
TransactionMessage,
VersionedMessage,
Expand Down Expand Up @@ -359,7 +358,7 @@ export async function getEstimateBalanceChange(connection: Connection, tx: Versi
// Simulate Transaction with Accounts
const instructionAddresses = Array.from(accounts.keys());
const addresses = lookupAddresses?.length ? lookupAddresses : instructionAddresses;
const result = await connection.simulateTransaction(tx, { accounts: { addresses, encoding: "base64" } });
const result = await connection.simulateTransaction(tx, { accounts: { addresses, encoding: "base64" }, commitment: "confirmed" });

if (result.value.err) {
throw new Error(result.value.err.toString());
Expand Down Expand Up @@ -435,17 +434,17 @@ export async function parsingTransferAmount(
}

// SolanaPay
export const validateUrlTransactionSignature = (transaction: Transaction, selectedAddress: string) => {
let signRequired = false;
transaction.signatures.forEach((sig) => {
if (sig.signature === null && sig.publicKey.toBase58() !== selectedAddress) throw new Error("Merchant Signature Verifcation Failed");
signRequired = signRequired || sig.publicKey.toBase58() === selectedAddress;
});
if (!signRequired) throw new Error("Wallet Signature Not Required");
transaction.serialize({ requireAllSignatures: false });
};

export const parseSolanaPayRequestLink = async (request: string, account: string, connection: Connection) => {
// export const validateUrlTransactionSignature = (transaction: VersionedTransaction, selectedAddress: string) => {
// let signRequired = false;
// transaction.signatures.forEach((sig) => {
// if (signature === null && sig.publicKey.toBase58() !== selectedAddress) throw new Error("Merchant Signature Verifcation Failed");
// signRequired = signRequired || sig.publicKey.toBase58() === selectedAddress;
// });
// if (!signRequired) throw new Error("Wallet Signature Not Required");
// transaction.serialize({ requireAllSignatures: false });
// };

export const parseSolanaPayRequestLink = async (request: string, account: string) => {
log.info(request);
const { label, message, link } = parseURL(request) as TransactionRequestURL;
// get link
Expand All @@ -457,19 +456,22 @@ export const parseSolanaPayRequestLink = async (request: string, account: string
// return {"transaction":"<transaction>"} (base64)
const postResult = await post<{ transaction: string; message?: string }>(link.toString(), { account });

const transaction = Transaction.from(Buffer.from(postResult.transaction, "base64"));
const decodedInst = transaction.instructions.map((inst) => decodeInstruction(inst));
// const transaction = Transaction.from(Buffer.from(postResult.transaction, "base64"));
const transaction = VersionedTransaction.deserialize(Buffer.from(postResult.transaction, "base64"));
const { instructions } = TransactionMessage.decompile(transaction.message);
const decodedInst = instructions.map((inst) => decodeInstruction(inst));

// assign transaction object

if (transaction.signatures.length === 0) {
log.info("empty signature");
transaction.feePayer = new PublicKey(account);
const block = await connection.getLatestBlockhash();
transaction.lastValidBlockHeight = block.lastValidBlockHeight;
transaction.recentBlockhash = block.blockhash;
} else {
validateUrlTransactionSignature(transaction, account);
}
// if (transaction.signatures.length === 0) {
// log.info("empty signature");
// transaction.feePayer = new PublicKey(account);
// const block = await connection.getLatestBlockhash();
// transaction.lastValidBlockHeight = block.lastValidBlockHeight;
// transaction.recentBlockhash = block.blockhash;
// } else {
// validateUrlTransactionSignature(transaction, account);
// }

return { transaction, decodedInst, message: message || postResult.message, label: label || getResult.label, icon: getResult.icon, link };
};