Skip to content

Commit

Permalink
Add 4byte service
Browse files Browse the repository at this point in the history
  • Loading branch information
Destiner committed Apr 29, 2024
1 parent 4201b8f commit bafb738
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/services/4byte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Hex } from 'viem';

interface HexSignatureResponse {
count: number;
next: null;
previous: null;
results: {
id: number;
created_at: string;
text_signature: string;
hex_signature: Hex;
bytes_signature: string;
}[];
}

class Service {
async getSignature(signature: Hex): Promise<string | null> {
const response = await fetch(
`https://www.4byte.directory/api/v1/signatures/?hex_signature=${signature}`,
);
const data = (await response.json()) as HexSignatureResponse;
if (data.count === 0) {
return null;
}
const firstResult = data.results[0];
return firstResult ? firstResult.text_signature : null;
}
}

export default Service;

0 comments on commit bafb738

Please sign in to comment.