Skip to content

Commit

Permalink
chore: clean GET chainhooks payloads, add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludo Galabru committed Nov 2, 2022
1 parent 44306f0 commit 7d9cb8d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
51 changes: 51 additions & 0 deletions components/chainhook-event-observer/src/chainhooks/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clarity_repl::clarity::util::hash::hex_bytes;
use reqwest::Url;
use serde::ser::{SerializeSeq, Serializer};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
Expand All @@ -24,6 +25,30 @@ impl HookFormation {
}
}

pub fn serialized_stacks_predicates(
&self,
) -> Vec<(&String, &StacksNetwork, &StacksTransactionFilterPredicate)> {
let mut stacks = vec![];
for chainhook in self.stacks_chainhooks.iter() {
stacks.push((
&chainhook.uuid,
&chainhook.network,
&chainhook.transaction_predicate,
));
}
stacks
}

pub fn serialized_bitcoin_predicates(
&self,
) -> Vec<(&String, &BitcoinNetwork, &BitcoinTransactionFilterPredicate)> {
let mut bitcoin = vec![];
for chainhook in self.bitcoin_chainhooks.iter() {
bitcoin.push((&chainhook.uuid, &chainhook.network, &chainhook.predicate));
}
bitcoin
}

pub fn register_hook(&mut self, hook: ChainhookSpecification) {
match hook {
ChainhookSpecification::Stacks(hook) => self.stacks_chainhooks.push(hook),
Expand Down Expand Up @@ -103,6 +128,18 @@ impl ChainhookSpecification {
Self::Stacks(data) => &data.uuid,
}
}

pub fn validate(&self) -> Result<(), String> {
match &self {
Self::Bitcoin(data) => {
let _ = data.action.validate()?;
}
Self::Stacks(data) => {
let _ = data.action.validate()?;
}
}
Ok(())
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
Expand All @@ -129,6 +166,20 @@ pub enum HookAction {
Noop,
}

impl HookAction {
pub fn validate(&self) -> Result<(), String> {
match &self {
HookAction::Http(spec) => {
let _ = Url::parse(&spec.url)
.map_err(|e| format!("hook action url invalid ({})", e.to_string()))?;
}
HookAction::File(spec) => {}
HookAction::Noop => {}
}
Ok(())
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct HttpHook {
Expand Down
45 changes: 41 additions & 4 deletions components/chainhook-event-observer/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,10 +1057,40 @@ pub fn handle_get_hooks(
"status": 404,
}))
}
Some(hooks) => Json(json!({
"status": 200,
"result": hooks,
})),
Some(hooks) => {
let mut predicates = vec![];
let mut stacks_predicates = hooks
.serialized_stacks_predicates()
.iter()
.map(|(uuid, network, predicate)| {
json!({
"chain": "stacks",
"uuid": uuid,
"network": network,
"predicate": predicate,
})
})
.collect::<Vec<_>>();
predicates.append(&mut stacks_predicates);
let mut bitcoin_predicates = hooks
.serialized_bitcoin_predicates()
.iter()
.map(|(uuid, network, predicate)| {
json!({
"chain": "bitcoin",
"uuid": uuid,
"network": network,
"predicate": predicate,
})
})
.collect::<Vec<_>>();
predicates.append(&mut bitcoin_predicates);

Json(json!({
"status": 200,
"result": predicates
}))
}
}
} else {
Json(json!({
Expand All @@ -1079,6 +1109,13 @@ pub fn handle_create_hook(
) -> Json<JsonValue> {
info!("POST /v1/chainhooks");
let hook = hook.into_inner();
if let Err(e) = hook.validate() {
return Json(json!({
"status": 422,
"error": e,
}));
}

let background_job_tx = background_job_tx.inner();
match background_job_tx.lock() {
Ok(tx) => {
Expand Down

0 comments on commit 7d9cb8d

Please sign in to comment.