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

The inspector will now accept a transaction, a versioned transaction, a message, or a versioned message #340

Merged
merged 1 commit into from
May 17, 2024
Merged
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
98 changes: 54 additions & 44 deletions app/components/inspector/RawInputCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,62 @@ import React from 'react';

import type { TransactionData } from './InspectorPage';

function deserializeTransaction(bytes: Uint8Array): {
function getMessageDataFromBytes(bytes: Uint8Array): {
message: VersionedMessage;
signatures: string[];
} | null {
const SIGNATURE_LENGTH = 64;
rawMessage: Uint8Array;
} {
const message = VersionedMessage.deserialize(bytes);
return {
message,
rawMessage: bytes,
};
}

function getTransactionDataFromUserSuppliedBytes(bytes: Uint8Array): {
message: VersionedMessage;
rawMessage: Uint8Array;
signatures?: string[];
} {
/**
* Step 1: Try to parse the bytes as a *transaction* first (ie. with signatures at the front)
*/
let offset = 0;
const numSignatures = bytes[offset++];
// If this were a transaction, would its message expect exactly `numSignatures`?
let requiredSignaturesByteOffset = 1 + numSignatures * 64;
if (VersionedMessage.deserializeMessageVersion(bytes.slice(requiredSignaturesByteOffset)) !== 'legacy') {
requiredSignaturesByteOffset++;
}
const numRequiredSignaturesAccordingToMessage = bytes[requiredSignaturesByteOffset];
if (numRequiredSignaturesAccordingToMessage !== numSignatures) {
// We looked ahead into the message and could not match the number of signatures indicated
// by the first byte of the transaction with the expected number of signatures in the
// message. This is likely not a transaction at all, so try to parse it as a message now.
return getMessageDataFromBytes(bytes);
}
const signatures = [];
try {
const signaturesLen = bytes[0];
bytes = bytes.slice(1);
for (let i = 0; i < signaturesLen; i++) {
const rawSignature = bytes.slice(0, SIGNATURE_LENGTH);
bytes = bytes.slice(SIGNATURE_LENGTH);
signatures.push(base58.encode(rawSignature));
for (let ii = 0; ii < numSignatures; ii++) {
const signatureBytes = bytes.subarray(offset, offset + 64);
if (signatureBytes.length !== 64) {
// We hit the end of the byte array before consuming `numSignatures` signatures. This
// can't have been a transaction, so try to parse it as a message now.
return getMessageDataFromBytes(bytes);
}
} catch (err) {
// Errors above indicate that the bytes do not encode a transaction.
return null;
signatures.push(base58.encode(signatureBytes));
offset += 64;
}
try {
const transactionData = getMessageDataFromBytes(bytes.slice(offset));
return {
...transactionData,
...(signatures.length ? { signatures } : null),
};
} catch {
/**
* Step 2: That didn't work, so presume that the bytes are a message, as asked for in the UI
*/
return getMessageDataFromBytes(bytes);
}

const message = VersionedMessage.deserialize(bytes);
return { message, signatures };
}

export const MIN_MESSAGE_LENGTH =
Expand All @@ -35,13 +70,6 @@ export const MIN_MESSAGE_LENGTH =
32 + // recent blockhash
1; // instructions length

const MIN_TRANSACTION_LENGTH =
1 + // signatures length
64 + // signatures, must have at least one for fees
MIN_MESSAGE_LENGTH;

const MAX_TRANSACTION_SIGNATURES = Math.floor((1232 - MIN_TRANSACTION_LENGTH) / (64 + 32)) + 1;

export function RawInput({
value,
setTransactionData,
Expand Down Expand Up @@ -86,27 +114,9 @@ export function RawInput({
try {
if (buffer.length < MIN_MESSAGE_LENGTH) {
throw new Error('Input is not long enough to be valid.');
} else if (buffer[0] > MAX_TRANSACTION_SIGNATURES) {
throw new Error(`Input starts with invalid byte: "${buffer[0]}"`);
}

const tx = deserializeTransaction(buffer);
if (tx) {
const message = tx.message;
const rawMessage = message.serialize();
setTransactionData({
message,
rawMessage,
signatures: tx.signatures,
});
} else {
const message = VersionedMessage.deserialize(buffer);
setTransactionData({
message,
rawMessage: buffer,
});
}

const transactionData = getTransactionDataFromUserSuppliedBytes(buffer);
setTransactionData(transactionData);
setError(undefined);
return;
} catch (err) {
Expand Down
Loading