use chia_wallet_sdk::driver::DriverError;
use thiserror::Error;
pub type DidResult<T> = Result<T, DidError>;
#[derive(Debug, Error)]
pub enum DidError {
#[error("chia driver error: {0}")]
Driver(#[from] DriverError),
#[error("signature calculation failed: {0}")]
Signer(String),
#[error("failed to parse DID: {0}")]
Parse(String),
#[error("coin is not a DID singleton")]
NotDid,
#[error("invalid did:chia string: {0}")]
InvalidDidString(String),
#[error("invalid recovery configuration: {0}")]
InvalidRecovery(String),
#[error("missing lineage proof for DID")]
MissingLineage,
#[error("missing owner hint on DID coin")]
MissingHint,
#[error("chain precondition failed: {0}")]
Chain(String),
#[error("DID singleton has no current on-chain coin (unlaunched or melted)")]
NoIdentitySingleton,
#[error("coin is not a genuine singleton")]
NotASingleton,
#[error("coin is not rooted in the DID's singleton lineage")]
NotDidRooted,
#[error("the DID tip's authenticated launcher does not match the requested launcher")]
LauncherMismatch,
#[error("singleton lineage exceeds the maximum authenticated depth")]
LineageTooDeep,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_messages_are_descriptive() {
assert_eq!(DidError::NotDid.to_string(), "coin is not a DID singleton");
assert_eq!(
DidError::MissingLineage.to_string(),
"missing lineage proof for DID"
);
assert_eq!(
DidError::MissingHint.to_string(),
"missing owner hint on DID coin"
);
assert_eq!(
DidError::Parse("bad".into()).to_string(),
"failed to parse DID: bad"
);
assert_eq!(
DidError::InvalidDidString("nope".into()).to_string(),
"invalid did:chia string: nope"
);
assert_eq!(
DidError::InvalidRecovery("mismatch".into()).to_string(),
"invalid recovery configuration: mismatch"
);
assert_eq!(
DidError::Signer("boom".into()).to_string(),
"signature calculation failed: boom"
);
assert_eq!(
DidError::Chain("wrong launcher".into()).to_string(),
"chain precondition failed: wrong launcher"
);
}
#[test]
fn wraps_driver_errors_via_from() {
let driver = DriverError::InvalidSingletonStruct;
let err: DidError = driver.into();
assert!(matches!(err, DidError::Driver(_)));
assert!(err.to_string().starts_with("chia driver error:"));
}
}