Skip to content

Commit a53982f

Browse files
committed
Add User-Agent header to requests
1 parent 8cc5806 commit a53982f

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

.github/workflows/release.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ jobs:
4747
if: github.ref == 'refs/heads/main'
4848
run: cp .env.build.main .env
4949

50+
- name: Set git commit hash
51+
run: echo -e "\nVITE_GIT_COMMIT=$(git rev-parse --short HEAD)" >> .env
52+
5053
- name: Build
5154
run: npm run build
5255

app/utils/chains.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
import type { Chain } from "../types/chains";
22

3+
/**
4+
* Custom fetch function for Sourcify API calls that adds User-Agent header
5+
*/
6+
function sourcifyFetch(url: string, options: RequestInit = {}): Promise<Response> {
7+
const gitCommit = import.meta.env.VITE_GIT_COMMIT || "dev";
8+
const userAgent = `Sourcify-UI/${gitCommit} (verify.sourcify.dev; Web)`;
9+
10+
return fetch(url, {
11+
...options,
12+
headers: {
13+
...options.headers,
14+
"User-Agent": userAgent,
15+
},
16+
});
17+
}
18+
319
export async function fetchChains(serverUrl: string): Promise<Chain[]> {
420
try {
5-
const response = await fetch(`${serverUrl}/chains`);
21+
const response = await sourcifyFetch(`${serverUrl}/chains`);
622

723
if (!response.ok) {
824
throw new Error(`Failed to fetch chains: ${response.status} ${response.statusText}`);

app/utils/sourcifyApi.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ import type { Language } from "../types/verification";
22
import { fetchFromEtherscan, processEtherscanResult } from "./etherscanApi";
33
import type { VyperVersion } from "../contexts/CompilerVersionsContext";
44

5+
/**
6+
* Custom fetch function for Sourcify API calls that adds User-Agent header
7+
*/
8+
function sourcifyFetch(url: string, options: RequestInit = {}): Promise<Response> {
9+
const gitCommit = import.meta.env.VITE_GIT_COMMIT || "dev";
10+
const userAgent = `Sourcify-UI/${gitCommit} (verify.sourcify.dev; Web)`;
11+
12+
return fetch(url, {
13+
...options,
14+
headers: {
15+
...options.headers,
16+
"User-Agent": userAgent,
17+
},
18+
});
19+
}
20+
521
export interface SolidityCompilerSettings {
622
evmVersion?: string;
723
optimizerEnabled: boolean;
@@ -104,7 +120,7 @@ async function submitStandardJsonVerification(
104120
...(creationTransactionHash && { creationTransactionHash }),
105121
};
106122

107-
const response = await fetch(`${serverUrl}/v2/verify/${chainId}/${address}`, {
123+
const response = await sourcifyFetch(`${serverUrl}/v2/verify/${chainId}/${address}`, {
108124
method: "POST",
109125
headers: {
110126
"Content-Type": "application/json",
@@ -193,7 +209,7 @@ export async function submitMetadataVerification(
193209
...(creationTransactionHash && { creationTransactionHash }),
194210
};
195211

196-
const response = await fetch(`${serverUrl}/v2/verify/metadata/${chainId}/${address}`, {
212+
const response = await sourcifyFetch(`${serverUrl}/v2/verify/metadata/${chainId}/${address}`, {
197213
method: "POST",
198214
headers: {
199215
"Content-Type": "application/json",
@@ -255,7 +271,7 @@ export async function getVerificationJobStatus(
255271
serverUrl: string,
256272
verificationId: string
257273
): Promise<VerificationJobStatus> {
258-
const response = await fetch(`${serverUrl}/v2/verify/${verificationId}`, {
274+
const response = await sourcifyFetch(`${serverUrl}/v2/verify/${verificationId}`, {
259275
method: "GET",
260276
headers: {
261277
"Content-Type": "application/json",

app/utils/verification.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,25 @@ import type { AllChainsResponse, VerifiedContractMinimal } from "../types/verifi
22

33
const SOURCIFY_REPO_URL = import.meta.env.VITE_SOURCIFY_REPO_URL || "https://repo.sourcify.dev";
44

5+
/**
6+
* Custom fetch function for Sourcify API calls that adds User-Agent header
7+
*/
8+
function sourcifyFetch(url: string, options: RequestInit = {}): Promise<Response> {
9+
const gitCommit = import.meta.env.VITE_GIT_COMMIT || "dev";
10+
const userAgent = `Sourcify-UI/${gitCommit} (verify.sourcify.dev; Web)`;
11+
12+
return fetch(url, {
13+
...options,
14+
headers: {
15+
...options.headers,
16+
"User-Agent": userAgent,
17+
},
18+
});
19+
}
20+
521
export async function fetchVerifiedAllChains(serverUrl: string, address: string): Promise<VerifiedContractMinimal[]> {
622
try {
7-
const response = await fetch(`${serverUrl}/v2/contract/all-chains/${address}`);
23+
const response = await sourcifyFetch(`${serverUrl}/v2/contract/all-chains/${address}`);
824

925
if (!response.ok) {
1026
if (response.status === 404) {
@@ -24,7 +40,7 @@ export async function fetchVerifiedAllChains(serverUrl: string, address: string)
2440

2541
export async function fetchVerifiedContract(serverUrl: string, chainId: string, address: string): Promise<VerifiedContractMinimal | null> {
2642
try {
27-
const response = await fetch(`${serverUrl}/v2/contract/${chainId}/${address}`);
43+
const response = await sourcifyFetch(`${serverUrl}/v2/contract/${chainId}/${address}`);
2844

2945
if (!response.ok) {
3046
if (response.status === 404) {

0 commit comments

Comments
 (0)