-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkormir.rs
367 lines (310 loc) Β· 10.5 KB
/
kormir.rs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
use anyhow::anyhow;
use bitcoin::key::XOnlyPublicKey;
use dlc_messages::oracle_msgs::{OracleAnnouncement, OracleAttestation};
use hmac::{Hmac, Mac};
use kormir::storage::OracleEventData;
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
use serde::Deserialize;
use serde::Serialize;
use sha2::Sha256;
use uuid::Uuid;
async fn get<T>(host: &str, path: &str) -> anyhow::Result<T>
where
T: serde::de::DeserializeOwned,
{
let url = format!("{}/{}", host, path);
let request = reqwest::get(url).await?.json::<T>().await?;
Ok(request)
}
#[derive(Debug, Serialize)]
pub struct CreateEnumEvent {
pub event_id: String,
pub outcomes: Vec<String>,
pub event_maturity_epoch: u32,
}
#[derive(Debug, Serialize)]
struct SignEnumEvent {
pub event_id: String,
pub outcome: String,
}
#[derive(Debug, Serialize)]
pub struct CreateNumericEvent {
pub event_id: String,
pub num_digits: Option<u16>,
pub is_signed: Option<bool>,
pub precision: Option<i32>,
pub unit: String,
pub event_maturity_epoch: u32,
}
#[derive(Debug, Serialize)]
pub struct SignNumericEvent {
pub event_id: String,
pub outcome: i64,
}
/// Kormir oracle client.
///
/// Allows the creation of enum and numeric announcements as well as signing.
#[derive(Debug)]
pub struct KormirOracleClient {
pubkey: XOnlyPublicKey,
client: reqwest::Client,
host: String,
hmac_secret: Option<Vec<u8>>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct PubkeyResponse {
pub pubkey: XOnlyPublicKey,
}
impl KormirOracleClient {
pub async fn new(
host: &str,
hmac_secret: Option<Vec<u8>>,
) -> anyhow::Result<KormirOracleClient> {
let pubkey: XOnlyPublicKey = get::<PubkeyResponse>(host, "pubkey").await?.pubkey;
let client = reqwest::Client::new();
tracing::info!(
host,
pubkey = pubkey.to_string(),
"Connected to Kormir client."
);
Ok(KormirOracleClient {
pubkey,
client,
host: host.to_string(),
hmac_secret,
})
}
pub async fn get_pubkey(&self) -> anyhow::Result<XOnlyPublicKey> {
Ok(self.pubkey)
}
/// List all events stored with the connected Kormir server.
///
/// Kormir events includes announcements info, nonce index, signatures
/// if announcement has been signed, and nostr information.
pub async fn list_events(&self) -> anyhow::Result<Vec<OracleEventData>> {
get(&self.host, "list-events").await.map_err(|e| {
tracing::error!(error = e.to_string(), "Error getting all kormir events.");
anyhow!("List events")
})
}
/// Creates an enum oracle announcement.
///
/// Maturity should be the UNIX timestamp of contract maturity.
pub async fn create_enum_event(
&self,
outcomes: Vec<String>,
maturity: u32,
) -> anyhow::Result<OracleAnnouncement> {
let event_id = Uuid::new_v4().to_string();
let create_event_request = CreateEnumEvent {
event_id: event_id.clone(),
outcomes,
event_maturity_epoch: maturity,
};
let (body, headers) = self.body_and_headers(&create_event_request)?;
let announcement = self
.client
.post(format!("{}/create-enum", self.host))
.body(body)
.headers(headers)
.send()
.await?
.json::<OracleAnnouncement>()
.await?;
tracing::info!(event_id, "Created Kormir oracle event.");
Ok(announcement)
}
/// Requests for Kormir to sign an announcement with a given outcome.
pub async fn sign_enum_event(
&self,
event_id: String,
outcome: String,
) -> anyhow::Result<OracleAttestation> {
tracing::info!("Signing event. event_id={} outcome={}", event_id, outcome);
let event = SignEnumEvent {
event_id: event_id.clone(),
outcome: outcome.clone(),
};
let (body, headers) = self.body_and_headers(&event)?;
let attestation = self
.client
.post(format!("{}/sign-enum", &self.host))
.body(body)
.headers(headers)
.send()
.await?
.json::<OracleAttestation>()
.await?;
tracing::info!(event_id, outcome, "Signed Kormir oracle event.");
Ok(attestation)
}
/// Creates a numeric oracle announcement.
///
/// Kormir currently supports only numeric event with base 2.
///
/// Maturity should be the UNIX timestamp of contract maturity.
pub async fn create_numeric_event(
&self,
num_digits: Option<u16>,
is_signed: Option<bool>,
precision: Option<i32>,
unit: String,
maturity: u32,
) -> anyhow::Result<OracleAnnouncement> {
let event_id = Uuid::new_v4().to_string();
let create_event_request = CreateNumericEvent {
event_id: event_id.clone(),
num_digits,
is_signed,
precision,
unit,
event_maturity_epoch: maturity,
};
let (body, headers) = self.body_and_headers(&create_event_request)?;
let announcement = self
.client
.post(format!("{}/create-numeric", self.host))
.body(body)
.headers(headers)
.send()
.await?
.json::<OracleAnnouncement>()
.await?;
tracing::info!(event_id, "Created Kormir oracle event.");
Ok(announcement)
}
/// Requests for Kormir to sign an announcement with a given outcome.
pub async fn sign_numeric_event(
&self,
event_id: String,
outcome: i64,
) -> anyhow::Result<OracleAttestation> {
tracing::info!("Signing event. event_id={} outcome={}", event_id, outcome);
let event = SignNumericEvent {
event_id: event_id.clone(),
outcome,
};
let (body, headers) = self.body_and_headers(&event)?;
let attestation = self
.client
.post(format!("{}/sign-numeric", &self.host))
.body(body)
.headers(headers)
.send()
.await?
.json::<OracleAttestation>()
.await?;
tracing::info!(event_id, outcome, "Signed Kormir oracle event.");
Ok(attestation)
}
fn body_and_headers<T: Serialize + ?Sized>(
&self,
json: &T,
) -> anyhow::Result<(Vec<u8>, HeaderMap)> {
let body = serde_json::to_vec(json)?;
let mut headers = HeaderMap::new();
headers.append(CONTENT_TYPE, HeaderValue::from_static("application/json"));
if let Some(secret) = &self.hmac_secret {
let hmac = Self::calculate_hmac(&body, secret)?;
headers.append("X-Signature", HeaderValue::from_bytes(hmac.as_bytes())?);
}
Ok((body, headers))
}
fn calculate_hmac(payload: &[u8], secret: &[u8]) -> anyhow::Result<String> {
let mut mac = Hmac::<Sha256>::new_from_slice(secret)?;
mac.update(payload);
let result = mac.finalize().into_bytes();
Ok(hex::encode(result))
}
}
#[async_trait::async_trait]
impl ddk_manager::Oracle for KormirOracleClient {
fn get_public_key(&self) -> bitcoin::key::XOnlyPublicKey {
self.pubkey
}
async fn get_attestation(
&self,
event_id: &str,
) -> Result<dlc_messages::oracle_msgs::OracleAttestation, ddk_manager::error::Error> {
tracing::info!(event_id, "Getting attestation to close contract.");
let attestation = get::<OracleAttestation>(&self.host, &format!("attestation/{event_id}"))
.await
.map_err(|e| {
tracing::error!(error=?e, "Could not get attestation.");
ddk_manager::error::Error::OracleError("Could not get attestation".into())
})?;
tracing::info!(event_id, attestation =? attestation, "Kormir attestation.");
Ok(attestation)
}
async fn get_announcement(
&self,
event_id: &str,
) -> Result<dlc_messages::oracle_msgs::OracleAnnouncement, ddk_manager::error::Error> {
tracing::info!(event_id, "Getting oracle announcement.");
let announcement =
get::<OracleAnnouncement>(&self.host, &format!("announcement/{event_id}"))
.await
.map_err(|e| {
tracing::error!(error =? e, "Could not get announcement.");
ddk_manager::error::Error::OracleError("Could not get announcement".into())
})?;
tracing::info!(event_id, announcement=?announcement, "Kormir announcement.");
Ok(announcement)
}
}
impl crate::Oracle for KormirOracleClient {
fn name(&self) -> String {
"kormir".into()
}
}
#[cfg(test)]
mod tests {
use chrono::{Local, TimeDelta};
use super::*;
async fn create_kormir() -> KormirOracleClient {
KormirOracleClient::new("https://kormir.dlcdevkit.com", None)
.await
.unwrap()
}
#[tokio::test]
async fn kormir_enum_events() {
let kormir = create_kormir().await;
let expiry = TimeDelta::seconds(30);
let timestamp: u32 = Local::now()
.checked_add_signed(expiry)
.unwrap()
.timestamp()
.try_into()
.unwrap();
let announcement = kormir
.create_enum_event(vec!["rust".to_string(), "go".to_string()], timestamp)
.await;
assert!(announcement.is_ok());
let sign_enum = kormir
.sign_enum_event(
announcement.unwrap().oracle_event.event_id,
"rust".to_string(),
)
.await;
assert!(sign_enum.is_ok())
}
#[tokio::test]
async fn kormir_numeric_events() {
let kormir = create_kormir().await;
let expiry = TimeDelta::seconds(30);
let timestamp: u32 = Local::now()
.checked_add_signed(expiry)
.unwrap()
.timestamp()
.try_into()
.unwrap();
let announcement = kormir
.create_numeric_event(Some(14), Some(true), Some(0), "m/s".to_string(), timestamp)
.await;
assert!(announcement.is_ok());
let sign_numeric = kormir
.sign_numeric_event(announcement.unwrap().oracle_event.event_id, -12345)
.await;
assert!(sign_numeric.is_ok());
}
}