No eth support

This commit is contained in:
Rafael Caricio 2023-04-24 16:10:08 +02:00
parent c0049e6d49
commit c5dbb0257f
Signed by: rafaelcaricio
GPG key ID: 3C86DBCE8E93C947

View file

@ -598,96 +598,8 @@ async fn make_permanent(
Ok(HttpResponse::Ok().json(status))
}
#[cfg(feature = "ethereum-extras")]
use {
mitra_utils::currencies::Currency,
crate::ethereum::nft::create_mint_signature,
crate::ipfs::utils::get_ipfs_url,
crate::models::posts::queries::set_post_token_tx_id,
super::types::TransactionData,
};
#[cfg(feature = "ethereum-extras")]
#[get("/{status_id}/signature")]
async fn get_signature(
auth: BearerAuth,
config: web::Data<Config>,
db_pool: web::Data<DbPool>,
status_id: web::Path<Uuid>,
) -> Result<HttpResponse, MastodonError> {
let db_client = &**get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?;
let ethereum_config = config.blockchain()
.ok_or(MastodonError::NotSupported)?
.ethereum_config()
.ok_or(MastodonError::NotSupported)?;
// User must have a public ethereum address
let wallet_address = current_user
.public_wallet_address(&Currency::Ethereum)
.ok_or(MastodonError::PermissionError)?;
let post = get_post_by_id(db_client, &status_id).await?;
if post.author.id != current_user.id || !post.is_public() || post.repost_of_id.is_some() {
// Users can only tokenize their own public posts
return Err(MastodonError::PermissionError);
};
let ipfs_cid = post.ipfs_cid
// Post metadata is not immutable
.ok_or(MastodonError::PermissionError)?;
let token_uri = get_ipfs_url(&ipfs_cid);
let signature = create_mint_signature(
ethereum_config,
&wallet_address,
&token_uri,
).map_err(|_| MastodonError::InternalError)?;
Ok(HttpResponse::Ok().json(signature))
}
#[cfg(feature = "ethereum-extras")]
#[post("/{status_id}/token_minted")]
async fn token_minted(
auth: BearerAuth,
connection_info: ConnectionInfo,
config: web::Data<Config>,
db_pool: web::Data<DbPool>,
status_id: web::Path<Uuid>,
transaction_data: web::Json<TransactionData>,
) -> Result<HttpResponse, MastodonError> {
let db_client = &**get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?;
let mut post = get_post_by_id(db_client, &status_id).await?;
if post.token_tx_id.is_some() {
return Err(MastodonError::OperationError("transaction is already registered"));
};
if post.author.id != current_user.id || !post.is_public() || post.repost_of_id.is_some() {
return Err(MastodonError::PermissionError);
};
let token_tx_id = transaction_data.into_inner().transaction_id;
set_post_token_tx_id(db_client, &post.id, &token_tx_id).await?;
post.token_tx_id = Some(token_tx_id);
let status = build_status(
db_client,
&get_request_base_url(connection_info),
&config.instance_url(),
Some(&current_user),
post,
).await?;
Ok(HttpResponse::Ok().json(status))
}
#[cfg(feature = "ethereum-extras")]
fn with_ethereum_extras(scope: Scope) -> Scope {
scope
.service(get_signature)
.service(token_minted)
}
#[cfg(not(feature = "ethereum-extras"))]
fn with_ethereum_extras(scope: Scope) -> Scope {
scope
}
pub fn status_api_scope() -> Scope {
let scope = web::scope("/api/v1/statuses")
web::scope("/api/v1/statuses")
// Routes without status ID
.service(create_status)
.service(preview_status)
@ -700,6 +612,5 @@ pub fn status_api_scope() -> Scope {
.service(unfavourite)
.service(reblog)
.service(unreblog)
.service(make_permanent);
with_ethereum_extras(scope)
.service(make_permanent)
}