Skip to content

Commit f999611

Browse files
committed
feat: support for Shutter API token at the bot level
1 parent 277992c commit f999611

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

contracts/scripts/shutter.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const SHUTTER_API_URL = {
1313
};
1414

1515
const SHUTTER_API = env.optional("SHUTTER_API", "mainnet") as keyof typeof SHUTTER_API_URL;
16+
const SHUTTER_API_TOKEN = env.optionalNoDefault("SHUTTER_API_TOKEN");
1617

1718
interface ShutterApiMessageData {
1819
eon: number;
@@ -33,6 +34,23 @@ interface ShutterDecryptionKeyData {
3334
decryption_timestamp: number;
3435
}
3536

37+
/**
38+
* Gets the appropriate headers for API requests, including bearer token for mainnet if available
39+
* @returns Headers object for fetch requests
40+
*/
41+
function getApiHeaders(): Record<string, string> {
42+
const headers: Record<string, string> = {
43+
accept: "application/json",
44+
};
45+
46+
// Add bearer token for mainnet if available
47+
if (SHUTTER_API === "mainnet" && SHUTTER_API_TOKEN && SHUTTER_API_TOKEN?.trim() !== "") {
48+
headers.Authorization = `Bearer ${SHUTTER_API_TOKEN}`;
49+
}
50+
51+
return headers;
52+
}
53+
3654
/**
3755
* Fetches encryption data from the Shutter API
3856
* @param decryptionTimestamp Unix timestamp when decryption should be possible
@@ -49,7 +67,7 @@ async function fetchShutterData(decryptionTimestamp: number): Promise<ShutterApi
4967
const response = await fetch(`${SHUTTER_API_URL[SHUTTER_API]}/api/register_identity`, {
5068
method: "POST",
5169
headers: {
52-
accept: "application/json",
70+
...getApiHeaders(),
5371
"Content-Type": "application/json",
5472
},
5573
body: JSON.stringify({
@@ -98,9 +116,7 @@ async function fetchDecryptionKey(identity: string): Promise<ShutterDecryptionKe
98116

99117
const response = await fetch(`${SHUTTER_API_URL[SHUTTER_API]}/api/get_decryption_key?identity=${identity}`, {
100118
method: "GET",
101-
headers: {
102-
accept: "application/json",
103-
},
119+
headers: getApiHeaders(),
104120
});
105121

106122
// Get the response text

0 commit comments

Comments
 (0)