Skip to content

Commit 9364ab8

Browse files
committed
Merge branch 'main' of github.com:thirdweb-dev/engine-core
2 parents 59a375c + 84f4e77 commit 9364ab8

File tree

3 files changed

+95
-80
lines changed

3 files changed

+95
-80
lines changed

server/src/http/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl ApiEngineError {
6868
},
6969
EngineError::VaultError { .. } => StatusCode::INTERNAL_SERVER_ERROR,
7070
EngineError::IawError { error } => match error {
71-
thirdweb_core::iaw::IAWError::ApiError(_) => StatusCode::INTERNAL_SERVER_ERROR,
71+
thirdweb_core::iaw::IAWError::ApiError { .. } => StatusCode::INTERNAL_SERVER_ERROR,
7272
thirdweb_core::iaw::IAWError::SerializationError { .. } => StatusCode::BAD_REQUEST,
7373
thirdweb_core::iaw::IAWError::NetworkError { .. } => StatusCode::BAD_REQUEST,
7474
thirdweb_core::iaw::IAWError::AuthError(_) => StatusCode::UNAUTHORIZED,

server/src/http/extractors.rs

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,46 +99,43 @@ where
9999
.get("x-wallet-access-token")
100100
.and_then(|v| v.to_str().ok())
101101
{
102-
// Extract ThirdwebAuth for billing purposes
103-
let thirdweb_auth = if let Some(secret_key) = parts
102+
// Try client ID and service key combination
103+
let client_id = parts
104104
.headers
105-
.get("x-thirdweb-secret-key")
105+
.get("x-thirdweb-client-id")
106106
.and_then(|v| v.to_str().ok())
107-
{
108-
ThirdwebAuth::SecretKey(secret_key.to_string())
109-
} else {
110-
// Try client ID and service key combination
111-
let client_id = parts
112-
.headers
113-
.get("x-thirdweb-client-id")
114-
.and_then(|v| v.to_str().ok())
115-
.ok_or_else(|| {
116-
ApiEngineError(EngineError::ValidationError {
117-
message: "Missing x-thirdweb-client-id header when using IAW".to_string(),
118-
})
119-
})?;
120-
121-
let service_key = parts
122-
.headers
123-
.get("x-thirdweb-service-key")
124-
.and_then(|v| v.to_str().ok())
125-
.ok_or_else(|| {
126-
ApiEngineError(EngineError::ValidationError {
127-
message: "Missing x-thirdweb-service-key header when using IAW".to_string(),
128-
})
129-
})?;
130-
131-
ThirdwebAuth::ClientIdServiceKey(thirdweb_core::auth::ThirdwebClientIdAndServiceKey {
107+
.ok_or_else(|| {
108+
ApiEngineError(EngineError::ValidationError {
109+
message:
110+
"Missing x-thirdweb-client-id header when using x-wallet-access-token"
111+
.to_string(),
112+
})
113+
})?;
114+
115+
let service_key = parts
116+
.headers
117+
.get("x-thirdweb-service-key")
118+
.and_then(|v| v.to_str().ok())
119+
.ok_or_else(|| {
120+
ApiEngineError(EngineError::ValidationError {
121+
message:
122+
"Missing x-thirdweb-service-key header when using x-wallet-access-token"
123+
.to_string(),
124+
})
125+
})?;
126+
127+
let thirdweb_auth = ThirdwebAuth::ClientIdServiceKey(
128+
thirdweb_core::auth::ThirdwebClientIdAndServiceKey {
132129
client_id: client_id.to_string(),
133130
service_key: service_key.to_string(),
134-
})
135-
};
131+
},
132+
);
136133

137134
return Ok(SigningCredentialsExtractor(SigningCredential::Iaw {
138135
auth_token: wallet_token.to_string(),
139136
thirdweb_auth,
140137
}));
141-
}
138+
};
142139

143140
// Fall back to Vault credentials
144141
let vault_access_token = parts

thirdweb-core/src/iaw/mod.rs

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub type AuthToken = String;
3030
)]
3131
#[serde(tag = "type", rename_all = "SCREAMING_SNAKE_CASE")]
3232
pub enum IAWError {
33-
#[error("API error: {0}")]
34-
ApiError(String),
33+
#[error("API error: {message}")]
34+
ApiError { message: String },
3535
#[error("Serialization error: {message}")]
3636
SerializationError { message: String },
3737
#[error("Network error: {error}")]
@@ -197,14 +197,16 @@ impl IAWClient {
197197
.await?;
198198

199199
if !response.status().is_success() {
200-
return Err(IAWError::ApiError(format!(
201-
"Failed to sign message - {} {}",
202-
response.status(),
203-
response
204-
.status()
205-
.canonical_reason()
206-
.unwrap_or("Unknown error")
207-
)));
200+
return Err(IAWError::ApiError {
201+
message: format!(
202+
"Failed to sign message - {} {}",
203+
response.status(),
204+
response
205+
.status()
206+
.canonical_reason()
207+
.unwrap_or("Unknown error")
208+
),
209+
});
208210
}
209211

210212
// Parse the response
@@ -214,7 +216,9 @@ impl IAWClient {
214216
let signature = signed_response
215217
.get("signature")
216218
.and_then(|s| s.as_str())
217-
.ok_or_else(|| IAWError::ApiError("No signature in response".to_string()))?;
219+
.ok_or_else(|| IAWError::ApiError {
220+
message: "No signature in response".to_string(),
221+
})?;
218222

219223
Ok(SignMessageData {
220224
signature: signature.to_string(),
@@ -262,14 +266,16 @@ impl IAWClient {
262266
.await?;
263267

264268
if !response.status().is_success() {
265-
return Err(IAWError::ApiError(format!(
266-
"Failed to sign typed data - {} {}",
267-
response.status(),
268-
response
269-
.status()
270-
.canonical_reason()
271-
.unwrap_or("Unknown error")
272-
)));
269+
return Err(IAWError::ApiError {
270+
message: format!(
271+
"Failed to sign typed data - {} {}",
272+
response.status(),
273+
response
274+
.status()
275+
.canonical_reason()
276+
.unwrap_or("Unknown error")
277+
),
278+
});
273279
}
274280

275281
// Parse the response
@@ -279,7 +285,9 @@ impl IAWClient {
279285
let signature = signed_response
280286
.get("signature")
281287
.and_then(|s| s.as_str())
282-
.ok_or_else(|| IAWError::ApiError("No signature in response".to_string()))?;
288+
.ok_or_else(|| IAWError::ApiError {
289+
message: "No signature in response".to_string(),
290+
})?;
283291

284292
Ok(SignTypedDataData {
285293
signature: signature.to_string(),
@@ -328,14 +336,16 @@ impl IAWClient {
328336
.await?;
329337

330338
if !response.status().is_success() {
331-
return Err(IAWError::ApiError(format!(
332-
"Failed to sign transaction - {} {}",
333-
response.status(),
334-
response
335-
.status()
336-
.canonical_reason()
337-
.unwrap_or("Unknown error")
338-
)));
339+
return Err(IAWError::ApiError {
340+
message: format!(
341+
"Failed to sign transaction - {} {}",
342+
response.status(),
343+
response
344+
.status()
345+
.canonical_reason()
346+
.unwrap_or("Unknown error")
347+
),
348+
});
339349
}
340350

341351
// Parse the response
@@ -345,7 +355,9 @@ impl IAWClient {
345355
let signature = signed_response
346356
.get("signature")
347357
.and_then(|s| s.as_str())
348-
.ok_or_else(|| IAWError::ApiError("No signature in response".to_string()))?;
358+
.ok_or_else(|| IAWError::ApiError {
359+
message: "No signature in response".to_string(),
360+
})?;
349361

350362
Ok(SignTransactionData {
351363
signature: signature.to_string(),
@@ -397,14 +409,16 @@ impl IAWClient {
397409
.await?;
398410

399411
if !response.status().is_success() {
400-
return Err(IAWError::ApiError(format!(
401-
"Failed to sign authorization - {} {}",
402-
response.status(),
403-
response
404-
.status()
405-
.canonical_reason()
406-
.unwrap_or("Unknown error")
407-
)));
412+
return Err(IAWError::ApiError {
413+
message: format!(
414+
"Failed to sign authorization - {} {}",
415+
response.status(),
416+
response
417+
.status()
418+
.canonical_reason()
419+
.unwrap_or("Unknown error")
420+
),
421+
});
408422
}
409423

410424
// Parse the response
@@ -414,8 +428,8 @@ impl IAWClient {
414428
let signed_authorization: SignedAuthorization = serde_json::from_value(
415429
signed_response
416430
.get("signedAuthorization")
417-
.ok_or_else(|| {
418-
IAWError::ApiError("No signedAuthorization in response".to_string())
431+
.ok_or_else(|| IAWError::ApiError {
432+
message: "No signedAuthorization in response".to_string(),
419433
})?
420434
.clone(),
421435
)?;
@@ -483,14 +497,16 @@ impl IAWClient {
483497
.await?;
484498

485499
if !response.status().is_success() {
486-
return Err(IAWError::ApiError(format!(
487-
"Failed to sign userop - {} {}",
488-
response.status(),
489-
response
490-
.status()
491-
.canonical_reason()
492-
.unwrap_or("Unknown error")
493-
)));
500+
return Err(IAWError::ApiError {
501+
message: format!(
502+
"Failed to sign userop - {} {}",
503+
response.status(),
504+
response
505+
.status()
506+
.canonical_reason()
507+
.unwrap_or("Unknown error")
508+
),
509+
});
494510
}
495511

496512
// Parse the response
@@ -500,7 +516,9 @@ impl IAWClient {
500516
let signature = signed_response
501517
.get("signature")
502518
.and_then(|s| s.as_str())
503-
.ok_or_else(|| IAWError::ApiError("No signature in response".to_string()))?;
519+
.ok_or_else(|| IAWError::ApiError {
520+
message: "No signature in response".to_string(),
521+
})?;
504522

505523
Ok(SignUserOpData {
506524
signature: signature.to_string(),

0 commit comments

Comments
 (0)