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

Add support for decoding raw sig without data #71

Merged
merged 3 commits into from
Jul 18, 2022
Merged
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
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
es6: true,
},
extends: ['react-app', 'plugin:storybook/recommended'],
plugins: ['no-only-tests', 'simple-import-sort', 'unused-imports', 'import'],
plugins: ['no-only-tests', 'simple-import-sort', 'unused-imports'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.json',
Expand Down
15 changes: 7 additions & 8 deletions src/components/DecodedCalldataTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function attachValues(components: ParamType[], decoded: Decoded): TreeNode[] {
return components.map((input, index): TreeNode => {
if (input.type === 'tuple') {
const value = decoded[index];

if (!Array.isArray(value)) {
throw new Error(
'input.type is tuple, but decoded value is not an array',
Expand All @@ -41,7 +40,7 @@ function attachValues(components: ParamType[], decoded: Decoded): TreeNode[] {
return {
name: input.name,
type: input.type,
value: String(decoded[index]),
value: decoded[index] ? String(decoded[index]) : '',
};
});
}
Expand Down Expand Up @@ -83,14 +82,14 @@ function CalldataTreeNode({
})}
</section>
</div>
) : (
<NodeBlock
className="my-2"
str={node.value ?? 'value missing'}
nodeType={node.type}
>
) : node.value ? (
<NodeBlock className="my-2" str={node.value} nodeType={node.type}>
{node.name ? <code className="text-pink">{node.name}</code> : ' '}
</NodeBlock>
) : (
<code id="node-type" className="text-purple">
{node.type}
</code>
)}
</div>
</span>
Expand Down
19 changes: 10 additions & 9 deletions src/components/NodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,20 @@ export function NodeBlock({
{nodeType}
</code>
<div
onClick={(e) => {
onClick={async (event) => {
const value =
e.currentTarget.children.namedItem('node-value')?.textContent;
void navigator.clipboard.writeText(value ?? '');

setCopyNotification(true);
setTimeout(() => {
setCopyNotification(false);
}, 1500);
event.currentTarget.children.namedItem('node-value')?.textContent;
if (value) {
await navigator.clipboard.writeText(value);
setCopyNotification(true);
setTimeout(() => {
setCopyNotification(false);
}, 1500);
}
}}
className="flex h-10 items-center gap-3 rounded-md border
border-gray-600 p-1 px-2 duration-200 hover:bg-gray-700
hover:outline active:bg-gray-800"
hover:outline active:bg-gray-800"
>
<code aria-label="decoded value" id="node-value">
{state.value}
Expand Down
13 changes: 13 additions & 0 deletions src/lib/decodeBySigHash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ describe(fetchAndDecodeWithCalldata.name, async () => {
expect(decodedResults![0].fragment.name).toEqual('transferFrom');
expect(decodedResults![0].sigHash).toEqual('0x23b872dd');
});

it('decodes fragment with raw signature', async () => {
const sigHash = '0x23c32819';
const calldata = sigHash;

const decodeResult = await fetchAndDecodeWithCalldata(sigHash, calldata);

expect(decodeResult).toBeDefined();
expect(decodeResult!.length).toEqual(1);
expect(decodeResult![0].decoded).toEqual([]);
expect(decodeResult![0].fragment).toBeDefined();
expect(decodeResult![0].sigHash).toEqual(sigHash);
});
});

describe(decodeWithEventProps.name, async () => {
Expand Down
7 changes: 6 additions & 1 deletion src/lib/decodeBySigHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export async function fetchAndDecodeWithCalldata(
const data = await getSignaturesByCalldata(sigHash);
if (data) {
const ifaces = parse4BytesResToIfaces(data);
return decodeByCalldata(ifaces, calldata);
const decodedByCalldata = decodeByCalldata(ifaces, calldata);
if (decodedByCalldata.length === 0 && ifaces.length > 0) {
return [{ decoded: [], fragment: ifaces[0].fragments[0], sigHash }];
} else {
return decodedByCalldata;
}
}
}

Expand Down