Skip to content

Commit afb1721

Browse files
committed
feat: show signature types
1 parent 1530e2d commit afb1721

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { pad } from 'viem';
2+
import { SignatureType } from '@/chains';
3+
4+
type Props = {
5+
base: SignatureType[];
6+
target: SignatureType[];
7+
onlyShowDiff: boolean;
8+
};
9+
10+
const formatSigType = (contents: SignatureType | undefined) => {
11+
if (!contents) return <p>Not present</p>;
12+
return (
13+
<>
14+
<p>{contents.description}</p>
15+
<p className='text-secondary text-sm'>{contents.signedData}</p>
16+
</>
17+
);
18+
};
19+
20+
// Returns a hex string with a leading `0x` and padded to 2 characters.
21+
const formatPrefixByte = (prefix: number) => {
22+
return pad(`0x${prefix.toString(16).toUpperCase()}`, { size: 1 });
23+
};
24+
25+
export const DiffSignatureTypes = ({ base, target, onlyShowDiff }: Props) => {
26+
// Generate a sorted list of all transaction types from both base and target.
27+
const allPrefixBytes = [...base.map((t) => t.prefixByte), ...target.map((t) => t.prefixByte)];
28+
const prefixBytes = [...new Set(allPrefixBytes.sort((a, b) => a - b))];
29+
30+
return (
31+
<>
32+
{prefixBytes.map((prefix) => {
33+
const baseSigType = base.find((s) => s.prefixByte === prefix);
34+
const targetSigType = target.find((s) => s.prefixByte === prefix);
35+
36+
const isEqual = JSON.stringify(baseSigType) === JSON.stringify(targetSigType);
37+
const showSigType = !isEqual || !onlyShowDiff;
38+
39+
return (
40+
showSigType && (
41+
<div
42+
key={prefix}
43+
className='flex items-center justify-between border-b border-zinc-500/10 py-1 dark:border-zinc-500/20'
44+
>
45+
<div className='flex-1'>{formatSigType(baseSigType)}</div>
46+
<div className='flex-1 text-center'>{formatPrefixByte(prefix)}</div>
47+
<div className='flex-1'>{formatSigType(targetSigType)}</div>
48+
</div>
49+
)
50+
);
51+
})}
52+
</>
53+
);
54+
};

src/pages/diff.tsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ import { Chain, chains } from '@/chains';
44
import { ChainDiffSelector } from '@/components/ChainDiffSelector';
55
import { DiffMetadata } from '@/components/diff/DiffMetadata';
66
import { DiffPrecompiles } from '@/components/diff/DiffPrecompiles';
7+
import { DiffSignatureTypes } from '@/components/diff/DiffSignatureTypes';
78
import { Toggle } from '@/components/ui/Toggle';
89

10+
const SECTION_MAP: Record<string, string> = {
11+
metadata: 'Metadata',
12+
precompiles: 'Precompiles and Predeploys',
13+
signatureTypes: 'Transaction and Signature Types',
14+
};
15+
916
const Diff = () => {
1017
// -------- Parse query parameters --------
1118

@@ -43,17 +50,11 @@ const Diff = () => {
4350

4451
const [onlyShowDiff, setOnlyShowDiff] = useState(true);
4552

46-
const SectionHeader = ({ section }: { section: string }) => {
47-
if (section === 'metadata') section = 'Metadata';
48-
else if (section === 'precompiles') section = 'Precompiles and Predeploys';
49-
50-
return (
51-
<h2 className='border-b border-zinc-500/10 text-center font-bold dark:border-zinc-500/20'>
52-
{section}
53-
</h2>
54-
);
55-
};
56-
53+
const SectionHeader = ({ section }: { section: string }) => (
54+
<h2 className='border-b border-zinc-500/10 text-center font-bold dark:border-zinc-500/20'>
55+
{SECTION_MAP[section] || section}
56+
</h2>
57+
);
5758
// We take `baseChain` and `targetChain` as arguments to ensure that they are not `undefined`
5859
// and remove the need for `?` and `!` operators.
5960
const DiffDiv = ({ baseChain, targetChain }: { baseChain: Chain; targetChain: Chain }) => {
@@ -81,6 +82,14 @@ const Diff = () => {
8182
onlyShowDiff={onlyShowDiff}
8283
/>
8384
);
85+
} else if (section === 'signatureTypes') {
86+
content = (
87+
<DiffSignatureTypes
88+
base={baseChain.signatureTypes}
89+
target={targetChain.signatureTypes}
90+
onlyShowDiff={onlyShowDiff}
91+
/>
92+
);
8493
}
8594

8695
return (

0 commit comments

Comments
 (0)