forked from bisq-network/bisq-musig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmp_service.rs
More file actions
174 lines (140 loc) · 5.81 KB
/
bmp_service.rs
File metadata and controls
174 lines (140 loc) · 5.81 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::collections::HashMap;
use std::sync::Mutex;
use bdk_wallet::bitcoin::Amount;
use protocol::protocol_musig_adaptor::{BMPContext, BMPProtocol, ProtocolRole, Round1Parameter};
use protocol::wallet_service::WalletService;
use testenv::TestEnv;
use tonic::{Request, Response, Result, Status};
use tracing::info;
use wallet::protocol_wallet_api::MemWallet;
use crate::pb::bmp_protocol::bmp_protocol_service_server::BmpProtocolService;
use crate::pb::bmp_protocol::{self, InitializeRequest, InitializeResponse, Role};
use crate::pb::convert::TryProtoInto as _;
#[derive(Default)]
pub struct BmpServiceImpl {
// Each trade protocol is stored against a unique ID.
active_protocols: Mutex<HashMap<String, BMPProtocol>>,
}
#[tonic::async_trait]
impl BmpProtocolService for BmpServiceImpl {
async fn initialize(
&self,
request: Request<InitializeRequest>,
) -> Result<Response<InitializeResponse>> {
let req = request.into_inner();
info!("Received initialize request: {req:?}");
//todo retrieve the actual wallet
let mut env = TestEnv::new().unwrap(); // TODO move Wallet loading
let mock_wallet = MemWallet::funded_wallet(&mut env);
let wallet_service = WalletService::new().load(mock_wallet);
let role =
Role::try_from(req.role).map_err(|_| Status::invalid_argument("Unrecognised role"))?;
let role = match role {
Role::Seller => ProtocolRole::Seller,
Role::Buyer => ProtocolRole::Buyer,
};
let context = BMPContext::new(
wallet_service,
role,
Amount::from_sat(req.seller_amount_sats),
Amount::from_sat(req.buyer_amount_sats),
).map_err(|e| Status::internal(e.to_string()))?;
let protocol = BMPProtocol::new(context).map_err(|e| Status::internal(e.to_string()))?;
let trade_id = &req.trade_id;
if trade_id.is_empty() {
return Err(Status::invalid_argument("Trade ID must not be empty"));
}
self.active_protocols
.lock()
.unwrap()
.insert(trade_id.clone(), protocol);
Ok(Response::new(InitializeResponse {
trade_id: trade_id.clone(),
}))
}
async fn execute_round1(
&self,
request: Request<bmp_protocol::Round1Request>,
) -> Result<Response<bmp_protocol::Round1Response>> {
let req = request.into_inner();
let trade_id = req.trade_id;
let mut protocols = self.active_protocols.lock().unwrap();
let protocol = protocols
.get_mut(&trade_id)
.ok_or_else(|| Status::not_found(format!("Trade not found: {trade_id}")))?;
let round1_result = protocol
.round1()
.map_err(|e| Status::aborted(e.to_string()))?;
drop(protocols);
Ok(Response::new(round1_result.try_into()?))
}
async fn execute_round2(
&self,
request: Request<bmp_protocol::Round2Request>,
) -> Result<Response<bmp_protocol::Round2Response>> {
let req = request.into_inner();
let trade_id = req.trade_id;
let mut protocols = self.active_protocols.lock().unwrap();
let protocol = protocols
.get_mut(&trade_id)
.ok_or_else(|| Status::not_found(format!("Trade not found: {trade_id}")))?;
let peer_round1_params: Round1Parameter =
req.peer_round1_response.unwrap().try_proto_into()?;
let round2_result = protocol
.round2(peer_round1_params)
.map_err(|e| Status::aborted(e.to_string()))?;
drop(protocols);
Ok(Response::new(round2_result.try_into()?))
}
async fn execute_round3(
&self,
request: Request<bmp_protocol::Round3Request>,
) -> Result<Response<bmp_protocol::Round3Response>> {
let req = request.into_inner();
let trade_id = req.trade_id;
let mut protocols = self.active_protocols.lock().unwrap();
let protocol = protocols
.get_mut(&trade_id)
.ok_or_else(|| Status::not_found(format!("Trade not found: {trade_id}")))?;
let peer_round2_params = req.peer_round2_response.unwrap().try_proto_into()?;
let round3_result = protocol
.round3(peer_round2_params)
.map_err(|e| Status::aborted(e.to_string()))?;
drop(protocols);
Ok(Response::new(round3_result.try_into()?))
}
async fn execute_round4(
&self,
request: Request<bmp_protocol::Round4Request>,
) -> Result<Response<bmp_protocol::Round4Response>> {
let req = request.into_inner();
let trade_id = req.trade_id;
let mut protocols = self.active_protocols.lock().unwrap();
let protocol = protocols
.get_mut(&trade_id)
.ok_or_else(|| Status::not_found(format!("Trade not found: {trade_id}")))?;
let peer_round3_params = req.peer_round3_response.unwrap().try_proto_into()?;
let round4_result = protocol
.round4(peer_round3_params)
.map_err(|e| Status::aborted(e.to_string()))?;
drop(protocols);
Ok(Response::new(round4_result.try_into()?))
}
async fn execute_round5(
&self,
request: Request<bmp_protocol::Round5Request>,
) -> Result<Response<()>> {
let req = request.into_inner();
let trade_id = req.trade_id;
let mut protocols = self.active_protocols.lock().unwrap();
let protocol = protocols
.get_mut(&trade_id)
.ok_or_else(|| Status::not_found(format!("Trade not found: {trade_id}")))?;
let peer_round4_params = req.peer_round4_response.unwrap().try_proto_into()?;
protocol
.round5(peer_round4_params)
.map_err(|e| Status::aborted(e.to_string()))?;
drop(protocols);
Ok(Response::new(()))
}
}