Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/minibf/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub enum Error {
InvalidXpub,
InvalidDerivationRole,
InvalidDerivationIndex,
InvalidCertIndex,
InvalidGovActionId,
}

#[derive(Serialize)]
Expand Down Expand Up @@ -159,6 +161,24 @@ impl IntoResponse for Error {
)),
)
.into_response(),
Error::InvalidCertIndex => (
StatusCode::BAD_REQUEST,
Json(ErrorBody::new(
400,
"Bad Request",
"params/cert_index must be integer",
)),
)
.into_response(),
Error::InvalidGovActionId => (
StatusCode::BAD_REQUEST,
Json(ErrorBody::new(
400,
"Bad Request",
"Invalid or malformed gov action id.",
)),
)
.into_response(),
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/minibf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,14 @@ where
"/governance/proposals",
get(routes::governance::proposals::<D>),
)
.route(
"/governance/proposals/{tx_hash}/{cert_index}/withdrawals",
get(routes::governance::proposal_withdrawals::<D>),
)
.route(
"/governance/proposals/{gov_action_id}/withdrawals",
get(routes::governance::proposal_withdrawals_by_gov_action::<D>),
)
.with_state(facade)
.layer(
trace::TraceLayer::new_for_http()
Expand Down
26 changes: 26 additions & 0 deletions crates/minibf/src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ pub fn bech32_gov_action(tx: &Hash<32>, idx: u32) -> Result<String, StatusCode>
bech32(GOV_ACTION_HRP, [tx.as_slice(), &bytes[first..]].concat())
}

/// Read a CIP-129 governance action id back into the proposing tx hash and
/// the action index, the inverse of [`bech32_gov_action`].
///
/// The index is whatever big-endian bytes trail the hash, so both the
/// one-byte form Blockfrost writes for index 0 and the bare 32-byte form
/// explorers write for it resolve to the same proposal.
pub fn parse_gov_action_id(id: &str) -> Result<(Hash<32>, u32), StatusCode> {
let (hrp, payload) = bech32::decode(id).map_err(|_| StatusCode::BAD_REQUEST)?;

if hrp != GOV_ACTION_HRP {
return Err(StatusCode::BAD_REQUEST);
}

let Some((tx, idx)) = payload.split_at_checked(32) else {
return Err(StatusCode::BAD_REQUEST);
};

if idx.len() > 4 {
return Err(StatusCode::BAD_REQUEST);
}

let idx = idx.iter().fold(0u32, |acc, byte| (acc << 8) | *byte as u32);

Ok((Hash::from(tx), idx))
}

pub fn asset_fingerprint(subject: &[u8]) -> Result<String, StatusCode> {
let mut hasher = pallas::crypto::hash::Hasher::<160>::new();
hasher.input(subject);
Expand Down
Loading
Loading