Add API method for getting authorization of paid subscription setup transaction

This commit is contained in:
silverpill 2022-01-26 14:59:15 +00:00
parent 464d4886fa
commit 4e0f14df4c
4 changed files with 71 additions and 13 deletions

View file

@ -87,6 +87,20 @@ paths:
$ref: '#/components/schemas/Account'
400:
description: Invalid user data
/api/v1/accounts/authorize_subscription:
get:
summary: Get authorization for setting up paid subscription.
responses:
200:
description: Signature created
content:
application/json:
schema:
$ref: '#/components/schemas/Signature'
403:
description: User's wallet address is not known
418:
description: Blockchain integration is not enabled
/api/v1/accounts/relationships:
get:
summary: Find out whether a given actor is followed, blocked, muted, etc.
@ -431,24 +445,13 @@ paths:
content:
application/json:
schema:
type: object
properties:
v:
type: integer
format: int64
example: 27
r:
type: string
example: '6f61670e67bf72...'
s:
type: string
example: '6a5cb313907cd3...'
$ref: '#/components/schemas/Signature'
403:
description: Post does not belong to user, is not public or not saved to IPFS, or user's wallet address is not known
404:
description: Post not found
418:
description: Ethereum integration is not enabled
description: Blockchain integration is not enabled
/api/v1/statuses/{status_id}/token_minted:
post:
summary: Register transaction that mints a token
@ -648,6 +651,19 @@ components:
requested:
description: Do you have a pending follow request for this user?
type: boolean
Signature:
type: object
properties:
v:
type: integer
format: int64
example: 27
r:
type: string
example: '6f61670e67bf72...'
s:
type: string
example: '6a5cb313907cd3...'
Status:
type: object
properties:

View file

@ -4,4 +4,5 @@ mod errors;
pub mod gate;
pub mod nft;
pub mod signatures;
pub mod subscriptions;
pub mod utils;

View file

@ -0,0 +1,20 @@
use crate::config::BlockchainConfig;
use super::errors::EthereumError;
use super::signatures::{sign_contract_call, CallArgs, SignatureData};
use super::utils::parse_address;
pub fn create_subscription_signature(
blockchain_config: &BlockchainConfig,
user_address: &str,
) -> Result<SignatureData, EthereumError> {
let user_address = parse_address(user_address)?;
let call_args: CallArgs = vec![Box::new(user_address)];
let signature = sign_contract_call(
&blockchain_config.signing_key,
blockchain_config.ethereum_chain_id(),
&blockchain_config.contract_address,
"configureSubscription",
call_args,
)?;
Ok(signature)
}

View file

@ -14,6 +14,7 @@ use crate::config::Config;
use crate::database::{Pool, get_database_client};
use crate::errors::{DatabaseError, HttpError, ValidationError};
use crate::ethereum::gate::is_allowed_user;
use crate::ethereum::subscriptions::create_subscription_signature;
use crate::mastodon_api::oauth::auth::get_current_user;
use crate::models::posts::helpers::{
get_actions_for_posts,
@ -171,6 +172,25 @@ async fn update_credentials(
Ok(HttpResponse::Ok().json(account))
}
#[get("/authorize_subscription")]
async fn authorize_subscription(
auth: BearerAuth,
config: web::Data<Config>,
db_pool: web::Data<Pool>,
) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?;
let blockchain_config = config.blockchain.as_ref()
.ok_or(HttpError::NotSupported)?;
let wallet_address = current_user.wallet_address
.ok_or(HttpError::PermissionError)?;
let signature = create_subscription_signature(
blockchain_config,
&wallet_address,
).map_err(|_| HttpError::InternalError)?;
Ok(HttpResponse::Ok().json(signature))
}
// TODO: actix currently doesn't support parameter arrays
// https://github.com/actix/actix-web/issues/2044
#[derive(Deserialize)]
@ -384,6 +404,7 @@ pub fn account_api_scope() -> Scope {
.service(get_relationships_view)
.service(verify_credentials)
.service(update_credentials)
.service(authorize_subscription)
// Routes with account ID
.service(get_account)
.service(follow_account)