@@ -13,6 +13,7 @@ const SHUTTER_API_URL = {
13
13
} ;
14
14
15
15
const SHUTTER_API = env . optional ( "SHUTTER_API" , "mainnet" ) as keyof typeof SHUTTER_API_URL ;
16
+ const SHUTTER_API_TOKEN = env . optionalNoDefault ( "SHUTTER_API_TOKEN" ) ;
16
17
17
18
interface ShutterApiMessageData {
18
19
eon : number ;
@@ -33,6 +34,23 @@ interface ShutterDecryptionKeyData {
33
34
decryption_timestamp : number ;
34
35
}
35
36
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
+
36
54
/**
37
55
* Fetches encryption data from the Shutter API
38
56
* @param decryptionTimestamp Unix timestamp when decryption should be possible
@@ -49,7 +67,7 @@ async function fetchShutterData(decryptionTimestamp: number): Promise<ShutterApi
49
67
const response = await fetch ( `${ SHUTTER_API_URL [ SHUTTER_API ] } /api/register_identity` , {
50
68
method : "POST" ,
51
69
headers : {
52
- accept : "application/json" ,
70
+ ... getApiHeaders ( ) ,
53
71
"Content-Type" : "application/json" ,
54
72
} ,
55
73
body : JSON . stringify ( {
@@ -98,9 +116,7 @@ async function fetchDecryptionKey(identity: string): Promise<ShutterDecryptionKe
98
116
99
117
const response = await fetch ( `${ SHUTTER_API_URL [ SHUTTER_API ] } /api/get_decryption_key?identity=${ identity } ` , {
100
118
method : "GET" ,
101
- headers : {
102
- accept : "application/json" ,
103
- } ,
119
+ headers : getApiHeaders ( ) ,
104
120
} ) ;
105
121
106
122
// Get the response text
0 commit comments