Skip to main content

hns_marketplace_protocol/
lib.rs

1#![doc = "Canonical Handshake marketplace and bilateral cross-chain wire protocols."]
2
3mod crypto;
4mod direct;
5mod relay_acceptance;
6mod shakescape;
7mod swap;
8mod types;
9
10pub use direct::*;
11pub use relay_acceptance::*;
12pub use shakescape::*;
13pub use swap::*;
14pub use types::*;
15
16use thiserror::Error;
17
18// Version 2 makes the maker-selected swap session identifier part of every
19// signed direct offer. Version 1 let a taker supply an unrelated identifier,
20// which could not be reconciled with the maker settlement key advertised by
21// the offer.
22pub const MARKETPLACE_PROTOCOL_VERSION: u16 = 2;
23
24#[derive(Debug, Error)]
25pub enum MarketplaceError {
26    #[error(transparent)]
27    Decode(#[from] hns_encoding::DecodeError),
28    #[error(transparent)]
29    Envelope(#[from] hns_p2p_experimental::EnvelopeError),
30    #[error(transparent)]
31    Swap(#[from] hns_swap::SwapError),
32    #[error("unsupported marketplace protocol version {0}")]
33    UnsupportedVersion(u16),
34    #[error("invalid marketplace field: {0}")]
35    Invalid(&'static str),
36    #[error("marketplace object is {actual} bytes; maximum is {maximum}")]
37    TooLarge { actual: usize, maximum: usize },
38    #[error("marketplace arithmetic overflow")]
39    ArithmeticOverflow,
40    #[error("invalid or noncanonical marketplace signature")]
41    InvalidSignature,
42    #[error("signing key does not match the advertised public key")]
43    SigningKeyMismatch,
44    #[error("marketplace object is bound to another network")]
45    NetworkMismatch,
46    #[error("marketplace object is not valid until {created_at}; current time is {now}")]
47    NotYetValid { created_at: u64, now: u64 },
48    #[error("marketplace object expired at {expires_at}; current time is {now}")]
49    Expired { expires_at: u64, now: u64 },
50    #[error("marketplace object hash differs from its canonical fields")]
51    HashMismatch,
52    #[error("Shakescape message type {message_type} is unknown for protocol {protocol_id}")]
53    UnknownMessage { protocol_id: u16, message_type: u16 },
54}
55
56pub type Result<T> = core::result::Result<T, MarketplaceError>;
57
58pub(crate) fn ensure_size(bytes: Vec<u8>, maximum: usize) -> Result<Vec<u8>> {
59    if bytes.len() > maximum {
60        Err(MarketplaceError::TooLarge {
61            actual: bytes.len(),
62            maximum,
63        })
64    } else {
65        Ok(bytes)
66    }
67}