-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ens): updating name attributes handler #509
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
use { | ||
crate::database::{error::DatabaseError, types, utils}, | ||
chrono::{DateTime, Utc}, | ||
sqlx::{PgPool, Postgres}, | ||
sqlx::{PgPool, Postgres, Row}, | ||
std::collections::HashMap, | ||
tracing::{error, instrument}, | ||
}; | ||
|
@@ -68,21 +68,29 @@ pub async fn delete_name( | |
} | ||
|
||
#[instrument(skip(postgres))] | ||
pub async fn update_name( | ||
pub async fn update_name_attributes( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor this function name to reflect that it updates only attributes. |
||
name: String, | ||
attributes: HashMap<String, String>, | ||
postgres: &PgPool, | ||
) -> Result<sqlx::postgres::PgQueryResult, sqlx::error::Error> { | ||
let insert_name_query = " | ||
) -> Result<HashMap<String, String>, DatabaseError> { | ||
let update_attributes_query = " | ||
UPDATE names SET attributes = $2::hstore, updated_at = NOW() | ||
WHERE name = $1 | ||
WHERE name = $1 | ||
RETURNING attributes::json | ||
"; | ||
sqlx::query::<Postgres>(insert_name_query) | ||
let row = sqlx::query(update_attributes_query) | ||
.bind(&name) | ||
// Convert JSON to String for hstore update | ||
.bind(&utils::hashmap_to_hstore(&attributes)) | ||
.execute(postgres) | ||
.await | ||
.fetch_one(postgres) | ||
.await?; | ||
let result: serde_json::Value = row.get(0); | ||
let updated_attributes_result: Result<HashMap<String, String>, DatabaseError> = | ||
serde_json::from_value(result.clone()).map_err(|e| { | ||
error!("Failed to deserialize updated attributes: {}", e); | ||
DatabaseError::SerdeJson(e) | ||
}); | ||
|
||
updated_attributes_result | ||
} | ||
|
||
#[instrument(skip(postgres))] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
use { | ||
super::{ | ||
super::HANDLER_TASK_METRICS, | ||
utils::{check_attributes, is_timestamp_within_interval}, | ||
Eip155SupportedChains, | ||
RegisterRequest, | ||
UpdateAttributesPayload, | ||
UNIXTIMESTAMP_SYNC_THRESHOLD, | ||
}, | ||
crate::{ | ||
database::helpers::{get_name_and_addresses_by_name, update_name_attributes}, | ||
error::RpcError, | ||
state::AppState, | ||
utils::crypto::{constant_time_eq, verify_message_signature}, | ||
}, | ||
axum::{ | ||
extract::{Path, State}, | ||
response::{IntoResponse, Response}, | ||
Json, | ||
}, | ||
hyper::StatusCode, | ||
num_enum::TryFromPrimitive, | ||
std::{str::FromStr, sync::Arc}, | ||
tracing::log::{error, info}, | ||
wc::future::FutureExt, | ||
}; | ||
|
||
pub async fn handler( | ||
state: State<Arc<AppState>>, | ||
name: Path<String>, | ||
Json(request_payload): Json<RegisterRequest>, | ||
) -> Result<Response, RpcError> { | ||
handler_internal(state, name, request_payload) | ||
.with_metrics(HANDLER_TASK_METRICS.with_name("profile_attributes_update")) | ||
.await | ||
} | ||
|
||
#[tracing::instrument(skip(state))] | ||
pub async fn handler_internal( | ||
state: State<Arc<AppState>>, | ||
Path(name): Path<String>, | ||
request_payload: RegisterRequest, | ||
) -> Result<Response, RpcError> { | ||
let raw_payload = &request_payload.message; | ||
let payload = match serde_json::from_str::<UpdateAttributesPayload>(raw_payload) { | ||
Ok(payload) => payload, | ||
Err(e) => { | ||
info!("Failed to deserialize update attributes payload: {}", e); | ||
return Ok(( | ||
StatusCode::BAD_REQUEST, | ||
format!("Failed to deserialize update attributes payload: {}", e), | ||
) | ||
.into_response()); | ||
} | ||
}; | ||
|
||
// Check for the supported ENSIP-11 coin type | ||
if Eip155SupportedChains::try_from_primitive(request_payload.coin_type).is_err() { | ||
info!("Unsupported coin type {}", request_payload.coin_type); | ||
return Ok(( | ||
StatusCode::BAD_REQUEST, | ||
"Unsupported coin type for name attributes update", | ||
) | ||
.into_response()); | ||
} | ||
|
||
// Check is name registered | ||
let name_addresses = | ||
match get_name_and_addresses_by_name(name.clone(), &state.postgres.clone()).await { | ||
Ok(result) => result, | ||
Err(_) => { | ||
info!( | ||
"Update attributes request for not registered name {}", | ||
name.clone() | ||
); | ||
return Ok((StatusCode::BAD_REQUEST, "Name is not registered").into_response()); | ||
} | ||
}; | ||
|
||
// Check the timestamp is within the sync threshold interval | ||
if !is_timestamp_within_interval(payload.timestamp, UNIXTIMESTAMP_SYNC_THRESHOLD) { | ||
return Ok(( | ||
StatusCode::BAD_REQUEST, | ||
"Timestamp is too old or in the future", | ||
) | ||
.into_response()); | ||
} | ||
|
||
let payload_owner = match ethers::types::H160::from_str(&request_payload.address) { | ||
Ok(owner) => owner, | ||
Err(e) => { | ||
info!("Failed to parse H160 address: {}", e); | ||
return Ok((StatusCode::BAD_REQUEST, "Invalid H160 address format").into_response()); | ||
} | ||
}; | ||
|
||
// Check the signature | ||
let sinature_check = | ||
match verify_message_signature(raw_payload, &request_payload.signature, &payload_owner) { | ||
Ok(sinature_check) => sinature_check, | ||
Err(e) => { | ||
info!("Invalid signature: {}", e); | ||
return Ok(( | ||
StatusCode::UNAUTHORIZED, | ||
"Invalid signature or message format", | ||
) | ||
.into_response()); | ||
} | ||
}; | ||
if !sinature_check { | ||
return Ok((StatusCode::UNAUTHORIZED, "Signature verification error").into_response()); | ||
} | ||
|
||
// Check for the name address ownership and address from the signed payload | ||
let name_owner = match name_addresses | ||
.addresses | ||
.get(&Eip155SupportedChains::EthereumMainnet.into()) | ||
{ | ||
Some(address_entry) => match ethers::types::H160::from_str(&address_entry.address) { | ||
Ok(owner) => owner, | ||
Err(e) => { | ||
info!("Failed to parse H160 address: {}", e); | ||
return Ok((StatusCode::BAD_REQUEST, "Invalid H160 address format").into_response()); | ||
} | ||
}, | ||
None => { | ||
info!("Address entry not found for key 60"); | ||
return Ok(( | ||
StatusCode::BAD_REQUEST, | ||
"Address entry not found for key 60", | ||
) | ||
.into_response()); | ||
} | ||
}; | ||
if !constant_time_eq(payload_owner, name_owner) { | ||
return Ok(( | ||
StatusCode::UNAUTHORIZED, | ||
"Address is not the owner of the name", | ||
) | ||
.into_response()); | ||
} | ||
|
||
// Check for supported attributes | ||
if !check_attributes( | ||
&payload.attributes, | ||
&super::SUPPORTED_ATTRIBUTES, | ||
super::ATTRIBUTES_VALUE_MAX_LENGTH, | ||
) { | ||
return Ok(( | ||
StatusCode::BAD_REQUEST, | ||
"Unsupported attribute in | ||
payload", | ||
) | ||
.into_response()); | ||
} | ||
|
||
match update_name_attributes(name.clone(), payload.attributes, &state.postgres).await { | ||
Err(e) => { | ||
error!("Failed to update attributes: {}", e); | ||
Ok(( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
format!("Failed to update attributes: {}", e), | ||
) | ||
.into_response()) | ||
} | ||
Ok(attributes) => Ok(Json(attributes).into_response()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes to use the random value here.