forked from StellarSend/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathescrows.rs
More file actions
154 lines (135 loc) · 4.95 KB
/
Copy pathescrows.rs
File metadata and controls
154 lines (135 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::{
error::{AppError, AppResult},
middleware::auth::AuthUser,
models::escrow::{CreateEscrowRequest, EscrowActionRequest},
services::{
escrow::{EscrowAction, EscrowService},
soroban::SorobanService,
stellar::StellarService,
transaction::TransactionService,
},
AppState,
};
use axum::{
extract::{Path, State},
Json,
};
use serde_json::{json, Value};
use std::sync::Arc;
use uuid::Uuid;
/// POST /api/escrows
///
/// Records an escrow that the client has already funded (or is about to
/// fund) via its own signed `create_escrow` transaction.
pub async fn create_escrow(
State(state): State<Arc<AppState>>,
auth: AuthUser,
Json(req): Json<CreateEscrowRequest>,
) -> AppResult<Json<Value>> {
let svc = EscrowService::new(state.pool.clone());
let escrow = svc.create(auth.user_id, &req).await?;
Ok(Json(json!({ "success": true, "data": escrow })))
}
/// GET /api/escrows
pub async fn list_escrows(
State(state): State<Arc<AppState>>,
auth: AuthUser,
) -> AppResult<Json<Value>> {
let svc = EscrowService::new(state.pool.clone());
let escrows = svc.list_for_user(auth.user_id).await?;
Ok(Json(json!({ "success": true, "data": escrows })))
}
/// GET /api/escrows/:id
///
/// Looked up by unguessable id, not restricted to the depositor: the
/// beneficiary and arbiter (who are not necessarily StellarSend users with
/// an account in `users`) also need to be able to check escrow status.
pub async fn get_escrow(
State(state): State<Arc<AppState>>,
_auth: AuthUser,
Path(id): Path<Uuid>,
) -> AppResult<Json<Value>> {
let svc = EscrowService::new(state.pool.clone());
let escrow = svc
.get_by_id(id)
.await?
.ok_or_else(|| AppError::NotFound("Escrow".into()))?;
Ok(Json(json!({ "success": true, "data": escrow })))
}
/// POST /api/escrows/:id/release/build
///
/// Builds an unsigned `release_escrow` invocation sourced from the calling
/// party's own account (`req.account`), for the client to sign with its own
/// wallet — the on-chain call requires the actual beneficiary/arbiter
/// signature (`caller.require_auth()`), so this cannot be keeper-executed.
pub async fn build_release_escrow(
State(state): State<Arc<AppState>>,
_auth: AuthUser,
Path(id): Path<Uuid>,
Json(req): Json<EscrowActionRequest>,
) -> AppResult<Json<Value>> {
let svc = EscrowService::new(state.pool.clone());
let stellar = StellarService::new(&state.config.horizon_url);
let soroban = SorobanService::new(&state.config.soroban_rpc_url);
let xdr = svc
.build_action_tx(&state.config, &stellar, &soroban, id, EscrowAction::Release, &req)
.await?;
Ok(Json(json!({ "success": true, "data": { "xdr": xdr } })))
}
/// POST /api/escrows/:id/release
///
/// Relays a client-signed `release_escrow` envelope (from the `/release/build`
/// step) and records the resulting on-chain state.
pub async fn release_escrow(
State(state): State<Arc<AppState>>,
_auth: AuthUser,
Path(id): Path<Uuid>,
Json(req): Json<SignedActionRequest>,
) -> AppResult<Json<Value>> {
let svc = EscrowService::new(state.pool.clone());
let soroban = SorobanService::new(&state.config.soroban_rpc_url);
let tx_svc = TransactionService::new(state.pool.clone());
let escrow = svc
.submit_signed_action(&soroban, &tx_svc, id, EscrowAction::Release, &req.signed_xdr)
.await?;
Ok(Json(json!({ "success": true, "data": escrow })))
}
/// POST /api/escrows/:id/refund/build
///
/// Same as `/release/build`, for `refund_escrow`.
pub async fn build_refund_escrow(
State(state): State<Arc<AppState>>,
_auth: AuthUser,
Path(id): Path<Uuid>,
Json(req): Json<EscrowActionRequest>,
) -> AppResult<Json<Value>> {
let svc = EscrowService::new(state.pool.clone());
let stellar = StellarService::new(&state.config.horizon_url);
let soroban = SorobanService::new(&state.config.soroban_rpc_url);
let xdr = svc
.build_action_tx(&state.config, &stellar, &soroban, id, EscrowAction::Refund, &req)
.await?;
Ok(Json(json!({ "success": true, "data": { "xdr": xdr } })))
}
/// POST /api/escrows/:id/refund
///
/// Relays a client-signed `refund_escrow` envelope and records the result.
pub async fn refund_escrow(
State(state): State<Arc<AppState>>,
_auth: AuthUser,
Path(id): Path<Uuid>,
Json(req): Json<SignedActionRequest>,
) -> AppResult<Json<Value>> {
let svc = EscrowService::new(state.pool.clone());
let soroban = SorobanService::new(&state.config.soroban_rpc_url);
let tx_svc = TransactionService::new(state.pool.clone());
let escrow = svc
.submit_signed_action(&soroban, &tx_svc, id, EscrowAction::Refund, &req.signed_xdr)
.await?;
Ok(Json(json!({ "success": true, "data": escrow })))
}
/// Request body for POST /api/escrows/:id/release and /refund.
#[derive(Debug, serde::Deserialize)]
pub struct SignedActionRequest {
pub signed_xdr: String,
}