Skip to content
Draft
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
290 changes: 196 additions & 94 deletions app/components/instruction/ed25519/Ed25519DetailsCard.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Program } from '@coral-xyz/anchor';
import { IdlType } from '@coral-xyz/anchor/dist/cjs/idl';
import { sha256 } from '@noble/hashes/sha256';
import {
ParsedTransaction,
PartiallyDecodedInstruction,
Expand All @@ -6,7 +9,11 @@ import {
TransactionInstruction,
} from '@solana/web3.js';
import bs58 from 'bs58';
import React from 'react';
import React, { useMemo } from 'react';

import { useAnchorProgram } from '@/app/providers/anchor';
import { useCluster } from '@/app/providers/cluster';
import { mapField } from '@/app/utils/anchor';

import { Address } from '../../common/Address';
import { Copyable } from '../../common/Copyable';
Expand Down Expand Up @@ -77,6 +84,178 @@ const extractData = (
}
};

function decodeMessageFromAnchorProgram(
anchorProgram: Program,
message: Uint8Array
): { name: string; data: any } | null {
const messageDisc = Buffer.from(message.slice(0, 8)).toString('hex');
const coder = anchorProgram.coder.types;
for (const [_, typeLayouts] of Object.entries(anchorProgram.coder.types)) {
for (const [name] of typeLayouts.entries()) {
try {
const capitalizedName = name.charAt(0).toUpperCase() + name.slice(1);
const disc = Buffer.from(sha256(`global:${capitalizedName}`).slice(0, 8)).toString('hex');

if (disc === messageDisc) {
const decoded = coder.decode(name, Buffer.from(message.slice(8)));
if (decoded) {
return { data: decoded, name };
}
}
} catch (e) {
console.log('Error decoding message with anchor program', e);
}
}
}
return null;
}

function SignatureDetails({
index,
offset,
signature,
pubkey,
message,
messageIx,
}: {
index: number;
offset: Ed25519SignatureOffsets;
signature: Uint8Array | null;
pubkey: Uint8Array | null;
message: Uint8Array;
messageIx: PartiallyDecodedInstruction;
}) {
const { url } = useCluster();
const anchorProgram = useAnchorProgram(messageIx.programId.toBase58(), url);

const decodedMessage = useMemo(() => {
if (!anchorProgram?.idl || !anchorProgram?.program) {
return null;
}
return decodeMessageFromAnchorProgram(anchorProgram.program, Buffer.from(message.toString(), 'hex'));
}, [anchorProgram, message]);

const messageRow = useMemo(() => {
if (!decodedMessage || !anchorProgram?.idl || !anchorProgram?.program) {
return (
<tr>
<td>Message</td>
<td
className="text-lg-end"
style={{
fontSize: '0.85rem',
lineHeight: '1.2',
maxWidth: '100%',
overflowWrap: 'break-word',
whiteSpace: 'normal',
wordBreak: 'break-all',
}}
>
<Copyable text={Buffer.from(message).toString('base64')}>
<span className="font-monospace">{Buffer.from(message).toString('base64')}</span>
</Copyable>
</td>
</tr>
);
}

const name = decodedMessage.name;
const data = decodedMessage.data;

if (!name || !data) {
return null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should you instead just fallback to rendering the message as a base64 string like in the clause above?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yea you were right

}

const type: IdlType = { defined: { name } };

return (
<React.Fragment>
<tr className="table-sep">
<td colSpan={1} className="text-lg-start" align="left">
Message Payload
</td>
<td colSpan={1} className="text-lg-start" align="left">
Payload Type
</td>
<td colSpan={1} className="text-lg-end">
Value
</td>
</tr>
{mapField(name, data, type, anchorProgram.program.idl, 'sigverify-message', 0)}
</React.Fragment>
);
}, [decodedMessage, message, anchorProgram?.idl, anchorProgram?.program]);

return (
<React.Fragment>
<tr className="table-sep">
<td colSpan={3} className="text-lg-start" align="left">
Signature #{index + 1}
</td>
</tr>
<tr>
<td colSpan={1}>Signature Reference</td>
<td colSpan={2} className="text-lg-end">
Instruction {offset.signatureInstructionIndex}, Offset {offset.signatureOffset}
</td>
</tr>
<tr>
<td colSpan={1}>Signature</td>
<td
colSpan={2}
className="text-lg-end font-monospace"
style={{
fontSize: '0.85rem',
lineHeight: '1.2',
maxWidth: '100%',
overflowWrap: 'break-word',
whiteSpace: 'normal',
wordBreak: 'break-all',
}}
>
{signature ? (
<Copyable text={Buffer.from(signature).toString('base64')}>
<span className="font-monospace">{Buffer.from(signature).toString('base64')}</span>
</Copyable>
) : (
<span className="font-monospace">Invalid Reference</span>
)}
</td>
</tr>
<tr>
<td colSpan={1}>Public Key Reference</td>
<td colSpan={2} className="text-lg-end">
Instruction {offset.publicKeyInstructionIndex}, Offset {offset.publicKeyOffset}
</td>
</tr>
<tr>
<td colSpan={1}>Public Key</td>
<td colSpan={2} className="text-lg-end">
{pubkey ? (
<Address pubkey={new PublicKey(pubkey)} alignRight link />
) : (
<span className="font-monospace">Invalid Reference</span>
)}
</td>
</tr>
<tr>
<td colSpan={1}>Message Reference</td>
<td colSpan={2} className="text-lg-end">
Instruction {offset.messageInstructionIndex}, Offset {offset.messageDataOffset}, Size{' '}
{offset.messageDataSize}
</td>
</tr>
<tr>
<td colSpan={1}>Message Program</td>
<td colSpan={2} className="text-lg-end">
<Address pubkey={messageIx.programId} alignRight link />
</td>
</tr>
{messageRow}
</React.Fragment>
);
}

export function Ed25519DetailsCard(props: DetailsProps) {
const { tx, ix, index, result, innerCards, childIndex } = props;

Expand All @@ -97,7 +276,6 @@ export function Ed25519DetailsCard(props: DetailsProps) {
<Address pubkey={ED25519_PROGRAM_ID} alignRight link />
</td>
</tr>

{offsets.map((offset, index) => {
const signature = extractData(
tx,
Expand All @@ -109,100 +287,24 @@ export function Ed25519DetailsCard(props: DetailsProps) {

const pubkey = extractData(tx, offset.publicKeyInstructionIndex, ix.data, offset.publicKeyOffset, 32);

const message = extractData(
tx,
offset.messageInstructionIndex,
ix.data,
offset.messageDataOffset,
offset.messageDataSize
);
const messageIx = tx.message.instructions[
offset.messageInstructionIndex
] as PartiallyDecodedInstruction;

const message = bs58
.decode(messageIx.data)
.slice(offset.messageDataOffset, offset.messageDataOffset + offset.messageDataSize);

return (
<React.Fragment key={index}>
<tr className="table-sep">
<td colSpan={2} className="text-lg-start" align="left">
Signature #{index + 1}
</td>
</tr>
<tr>
<td>Signature Reference</td>
<td className="text-lg-end">
{offset.signatureInstructionIndex === ED25519_SELF_REFERENCE_INSTRUCTION_INDEX
? 'This instruction'
: `Instruction ${offset.signatureInstructionIndex}`}
{', '}
Offset {offset.signatureOffset}
</td>
</tr>
<tr>
<td>Signature</td>
<td className="text-lg-end">
{signature ? (
<Copyable text={Buffer.from(signature).toString('base64')}>
<span className="font-monospace">
{Buffer.from(signature).toString('base64')}
</span>
</Copyable>
) : (
'Invalid reference'
)}
</td>
</tr>
<tr>
<td>Public Key Reference</td>
<td className="text-lg-end">
{offset.publicKeyInstructionIndex === ED25519_SELF_REFERENCE_INSTRUCTION_INDEX
? 'This instruction'
: `Instruction ${offset.publicKeyInstructionIndex}`}
{', '}
Offset {offset.publicKeyOffset}
</td>
</tr>
<tr>
<td>Public Key</td>
<td className="text-lg-end">
{pubkey ? (
<Address pubkey={new PublicKey(pubkey)} alignRight link />
) : (
'Invalid reference'
)}
</td>
</tr>
<tr>
<td>Message Reference</td>
<td className="text-lg-end">
{offset.messageInstructionIndex === ED25519_SELF_REFERENCE_INSTRUCTION_INDEX
? 'This instruction'
: `Instruction ${offset.messageInstructionIndex}`}
{', '}
Offset {offset.messageDataOffset}, Size {offset.messageDataSize}
</td>
</tr>
<tr>
<td>Message</td>
<td
className="text-lg-end"
style={{
fontSize: '0.85rem',
lineHeight: '1.2',
maxWidth: '100%',
overflowWrap: 'break-word',
whiteSpace: 'normal',
wordBreak: 'break-all',
}}
>
{message ? (
<Copyable text={Buffer.from(message).toString('base64')}>
<span className="font-monospace">
{Buffer.from(message).toString('base64')}
</span>
</Copyable>
) : (
'Invalid reference'
)}
</td>
</tr>
</React.Fragment>
<SignatureDetails
key={index}
index={index}
offset={offset}
signature={signature}
pubkey={pubkey}
message={message}
messageIx={messageIx}
/>
);
})}
</InstructionCard>
Expand Down
Loading