iwlink 0.1.1

IntelliWallet Link - encrypted phone to computer protocol for IntelliWallet for Browser
Documentation
//! Error type for the link protocol.
//!
//! Variants are coarse on purpose. A caller's response to almost every failure
//! is the same, drop the session and let the phone reconnect, so the variants
//! exist to make logs and tests readable rather than to drive recovery logic.
//! None of them carries key material or message contents.

use thiserror::Error;

/// Result alias used throughout the crate.
pub type Result<T> = std::result::Result<T, LinkError>;

/// Everything that can go wrong in the link protocol.
#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum LinkError {
    /// The pairing QR code could not be parsed.
    #[error("Invalid pairing offer: {0}")]
    InvalidOffer(&'static str),

    /// The pairing QR code is past its expiry time.
    #[error("Pairing offer expired")]
    OfferExpired,

    /// The two sides have no protocol version in common.
    #[error("No common protocol version")]
    VersionMismatch,

    /// A key had the wrong length or could not be generated.
    #[error("Invalid key: {0}")]
    InvalidKey(&'static str),

    /// The Noise handshake failed: wrong keys, wrong pre-shared key, a
    /// message out of turn, or a tampered message.
    #[error("Handshake failed: {0}")]
    Handshake(String),

    /// A transport message failed to decrypt or authenticate.
    #[error("Decryption failed")]
    Decrypt,

    /// A message is larger than the protocol allows.
    #[error("Message too large")]
    TooLarge,

    /// A Bluetooth fragment was malformed or arrived out of order.
    #[error("Bad fragment: {0}")]
    BadFragment(&'static str),

    /// A decrypted message is not a valid protocol message.
    #[error("Bad message: {0}")]
    BadMessage(String),

    /// The operating system's random number generator failed.
    #[error("Random number generator failed")]
    Random,
}

impl From<snow::Error> for LinkError {
    fn from(error: snow::Error) -> Self {
        // snow's errors describe the failed step ("decrypt", "pattern" and so
        // on) without including any secret, so the text is safe to keep.
        LinkError::Handshake(error.to_string())
    }
}