use std::fmt;
use std::io;
use crate::consensus::{self, CanonicalBytes, Decodable, Encodable};
use crate::crypto::{Commit, SharedKeyId, TaggedElement};
use crate::protocol::Parameters;
use crate::protocol::{verify_vec_of_commitments, CoreArbitratingTransactions};
use crate::swap::SwapId;
use crate::Error;
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommitAliceParameters<C> {
pub swap_id: SwapId,
pub buy: C,
pub cancel: C,
pub refund: C,
pub punish: C,
pub adaptor: C,
pub extra_arbitrating_keys: Vec<TaggedElement<u16, C>>,
pub arbitrating_shared_keys: Vec<TaggedElement<SharedKeyId, C>>,
pub spend: C,
pub extra_accordant_keys: Vec<TaggedElement<u16, C>>,
pub accordant_shared_keys: Vec<TaggedElement<SharedKeyId, C>>,
}
impl<C> CommitAliceParameters<C>
where
C: Eq + Clone + CanonicalBytes,
{
pub fn verify_with_reveal<Pk, Qk, Rk, Sk, Addr, Pr>(
&self,
wallet: &impl Commit<C>,
reveal: RevealAliceParameters<Pk, Qk, Rk, Sk, Addr, Pr>,
) -> Result<(), Error>
where
Pk: CanonicalBytes,
Qk: CanonicalBytes,
Rk: CanonicalBytes,
Sk: CanonicalBytes,
{
wallet.validate(reveal.buy.as_canonical_bytes(), self.buy.clone())?;
wallet.validate(reveal.cancel.as_canonical_bytes(), self.cancel.clone())?;
wallet.validate(reveal.refund.as_canonical_bytes(), self.refund.clone())?;
wallet.validate(reveal.punish.as_canonical_bytes(), self.punish.clone())?;
wallet.validate(reveal.adaptor.as_canonical_bytes(), self.adaptor.clone())?;
verify_vec_of_commitments(
wallet,
reveal.extra_arbitrating_keys,
&self.extra_arbitrating_keys,
)?;
verify_vec_of_commitments(
wallet,
reveal.arbitrating_shared_keys,
&self.arbitrating_shared_keys,
)?;
wallet.validate(reveal.spend.as_canonical_bytes(), self.spend.clone())?;
verify_vec_of_commitments(
wallet,
reveal.extra_accordant_keys,
&self.extra_accordant_keys,
)?;
verify_vec_of_commitments(
wallet,
reveal.accordant_shared_keys,
&self.accordant_shared_keys,
)
}
}
impl<C> fmt::Display for CommitAliceParameters<C>
where
C: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl<C> Encodable for CommitAliceParameters<C>
where
C: CanonicalBytes,
{
fn consensus_encode<W: io::Write>(&self, s: &mut W) -> Result<usize, io::Error> {
let mut len = self.swap_id.consensus_encode(s)?;
len += self.buy.as_canonical_bytes().consensus_encode(s)?;
len += self.cancel.as_canonical_bytes().consensus_encode(s)?;
len += self.refund.as_canonical_bytes().consensus_encode(s)?;
len += self.punish.as_canonical_bytes().consensus_encode(s)?;
len += self.adaptor.as_canonical_bytes().consensus_encode(s)?;
len += self.extra_arbitrating_keys.consensus_encode(s)?;
len += self.arbitrating_shared_keys.consensus_encode(s)?;
len += self.spend.as_canonical_bytes().consensus_encode(s)?;
len += self.extra_accordant_keys.consensus_encode(s)?;
Ok(len + self.accordant_shared_keys.consensus_encode(s)?)
}
}
impl<C> Decodable for CommitAliceParameters<C>
where
C: CanonicalBytes,
{
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
Ok(Self {
swap_id: Decodable::consensus_decode(d)?,
buy: C::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
cancel: C::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
refund: C::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
punish: C::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
adaptor: C::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
extra_arbitrating_keys: Decodable::consensus_decode(d)?,
arbitrating_shared_keys: Decodable::consensus_decode(d)?,
spend: C::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
extra_accordant_keys: Decodable::consensus_decode(d)?,
accordant_shared_keys: Decodable::consensus_decode(d)?,
})
}
}
impl_strict_encoding!(CommitAliceParameters<C>, C: CanonicalBytes);
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommitBobParameters<C> {
pub swap_id: SwapId,
pub buy: C,
pub cancel: C,
pub refund: C,
pub adaptor: C,
pub extra_arbitrating_keys: Vec<TaggedElement<u16, C>>,
pub arbitrating_shared_keys: Vec<TaggedElement<SharedKeyId, C>>,
pub spend: C,
pub extra_accordant_keys: Vec<TaggedElement<u16, C>>,
pub accordant_shared_keys: Vec<TaggedElement<SharedKeyId, C>>,
}
impl<C> CommitBobParameters<C>
where
C: Eq + Clone + CanonicalBytes,
{
pub fn verify_with_reveal<Pk, Qk, Rk, Sk, Addr, Pr>(
&self,
wallet: &impl Commit<C>,
reveal: RevealBobParameters<Pk, Qk, Rk, Sk, Addr, Pr>,
) -> Result<(), Error>
where
Pk: CanonicalBytes,
Qk: CanonicalBytes,
Rk: CanonicalBytes,
Sk: CanonicalBytes,
{
wallet.validate(reveal.buy.as_canonical_bytes(), self.buy.clone())?;
wallet.validate(reveal.cancel.as_canonical_bytes(), self.cancel.clone())?;
wallet.validate(reveal.refund.as_canonical_bytes(), self.refund.clone())?;
wallet.validate(reveal.adaptor.as_canonical_bytes(), self.adaptor.clone())?;
verify_vec_of_commitments(
wallet,
reveal.extra_arbitrating_keys,
&self.extra_arbitrating_keys,
)?;
verify_vec_of_commitments(
wallet,
reveal.arbitrating_shared_keys,
&self.arbitrating_shared_keys,
)?;
wallet.validate(reveal.spend.as_canonical_bytes(), self.spend.clone())?;
verify_vec_of_commitments(
wallet,
reveal.extra_accordant_keys,
&self.extra_accordant_keys,
)?;
verify_vec_of_commitments(
wallet,
reveal.accordant_shared_keys,
&self.accordant_shared_keys,
)
}
}
impl<C> fmt::Display for CommitBobParameters<C>
where
C: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl<C> Encodable for CommitBobParameters<C>
where
C: CanonicalBytes,
{
fn consensus_encode<W: io::Write>(&self, s: &mut W) -> Result<usize, io::Error> {
let mut len = self.swap_id.consensus_encode(s)?;
len += self.buy.as_canonical_bytes().consensus_encode(s)?;
len += self.cancel.as_canonical_bytes().consensus_encode(s)?;
len += self.refund.as_canonical_bytes().consensus_encode(s)?;
len += self.adaptor.as_canonical_bytes().consensus_encode(s)?;
len += self.extra_arbitrating_keys.consensus_encode(s)?;
len += self.arbitrating_shared_keys.consensus_encode(s)?;
len += self.spend.as_canonical_bytes().consensus_encode(s)?;
len += self.extra_accordant_keys.consensus_encode(s)?;
Ok(len + self.accordant_shared_keys.consensus_encode(s)?)
}
}
impl<C> Decodable for CommitBobParameters<C>
where
C: CanonicalBytes,
{
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
Ok(Self {
swap_id: Decodable::consensus_decode(d)?,
buy: C::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
cancel: C::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
refund: C::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
adaptor: C::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
extra_arbitrating_keys: Decodable::consensus_decode(d)?,
arbitrating_shared_keys: Decodable::consensus_decode(d)?,
spend: C::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
extra_accordant_keys: Decodable::consensus_decode(d)?,
accordant_shared_keys: Decodable::consensus_decode(d)?,
})
}
}
impl_strict_encoding!(CommitBobParameters<C>, C: CanonicalBytes);
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct RevealAliceParameters<Pk, Qk, Rk, Sk, Addr, Pr> {
pub swap_id: SwapId,
pub buy: Pk,
pub cancel: Pk,
pub refund: Pk,
pub punish: Pk,
pub adaptor: Pk,
pub extra_arbitrating_keys: Vec<TaggedElement<u16, Pk>>,
pub arbitrating_shared_keys: Vec<TaggedElement<SharedKeyId, Rk>>,
pub spend: Qk,
pub extra_accordant_keys: Vec<TaggedElement<u16, Qk>>,
pub accordant_shared_keys: Vec<TaggedElement<SharedKeyId, Sk>>,
pub address: Addr,
pub proof: Pr,
}
impl<Pk, Qk, Rk, Sk, Addr, Pr> RevealAliceParameters<Pk, Qk, Rk, Sk, Addr, Pr> {
pub fn into_parameters<Ti, F>(self) -> Parameters<Pk, Qk, Rk, Sk, Addr, Ti, F, Pr> {
Parameters {
buy: self.buy,
cancel: self.cancel,
refund: self.refund,
punish: Some(self.punish),
adaptor: self.adaptor,
extra_arbitrating_keys: self.extra_arbitrating_keys,
arbitrating_shared_keys: self.arbitrating_shared_keys,
spend: self.spend,
extra_accordant_keys: self.extra_accordant_keys,
accordant_shared_keys: self.accordant_shared_keys,
proof: Some(self.proof),
destination_address: self.address,
cancel_timelock: None,
punish_timelock: None,
fee_strategy: None,
}
}
}
impl<Pk, Qk, Rk, Sk, Addr, Pr> fmt::Display for RevealAliceParameters<Pk, Qk, Rk, Sk, Addr, Pr>
where
Pk: fmt::Debug,
Qk: fmt::Debug,
Rk: fmt::Debug,
Sk: fmt::Debug,
Addr: fmt::Debug,
Pr: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl<Pk, Qk, Rk, Sk, Addr, Pr> Encodable for RevealAliceParameters<Pk, Qk, Rk, Sk, Addr, Pr>
where
Pk: CanonicalBytes,
Qk: CanonicalBytes,
Rk: CanonicalBytes,
Sk: CanonicalBytes,
Addr: CanonicalBytes,
Pr: CanonicalBytes,
{
fn consensus_encode<W: io::Write>(&self, s: &mut W) -> Result<usize, io::Error> {
let mut len = self.swap_id.consensus_encode(s)?;
len += self.buy.as_canonical_bytes().consensus_encode(s)?;
len += self.cancel.as_canonical_bytes().consensus_encode(s)?;
len += self.refund.as_canonical_bytes().consensus_encode(s)?;
len += self.punish.as_canonical_bytes().consensus_encode(s)?;
len += self.adaptor.as_canonical_bytes().consensus_encode(s)?;
len += self.extra_arbitrating_keys.consensus_encode(s)?;
len += self.arbitrating_shared_keys.consensus_encode(s)?;
len += self.spend.as_canonical_bytes().consensus_encode(s)?;
len += self.extra_accordant_keys.consensus_encode(s)?;
len += self.accordant_shared_keys.consensus_encode(s)?;
len += self.address.as_canonical_bytes().consensus_encode(s)?;
Ok(len + self.proof.as_canonical_bytes().consensus_encode(s)?)
}
}
impl<Pk, Qk, Rk, Sk, Addr, Pr> Decodable for RevealAliceParameters<Pk, Qk, Rk, Sk, Addr, Pr>
where
Pk: CanonicalBytes,
Qk: CanonicalBytes,
Rk: CanonicalBytes,
Sk: CanonicalBytes,
Addr: CanonicalBytes,
Pr: CanonicalBytes,
{
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
Ok(Self {
swap_id: Decodable::consensus_decode(d)?,
buy: Pk::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
cancel: Pk::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
refund: Pk::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
punish: Pk::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
adaptor: Pk::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
extra_arbitrating_keys: Decodable::consensus_decode(d)?,
arbitrating_shared_keys: Decodable::consensus_decode(d)?,
spend: Qk::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
extra_accordant_keys: Decodable::consensus_decode(d)?,
accordant_shared_keys: Decodable::consensus_decode(d)?,
address: Addr::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
proof: Pr::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
})
}
}
impl_strict_encoding!(RevealAliceParameters<Pk, Qk, Rk, Sk, Addr, Pr>, Pk: CanonicalBytes, Qk: CanonicalBytes, Rk: CanonicalBytes, Sk: CanonicalBytes, Addr: CanonicalBytes, Pr: CanonicalBytes);
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct RevealBobParameters<Pk, Qk, Rk, Sk, Addr, Pr> {
pub swap_id: SwapId,
pub buy: Pk,
pub cancel: Pk,
pub refund: Pk,
pub adaptor: Pk,
pub extra_arbitrating_keys: Vec<TaggedElement<u16, Pk>>,
pub arbitrating_shared_keys: Vec<TaggedElement<SharedKeyId, Rk>>,
pub spend: Qk,
pub extra_accordant_keys: Vec<TaggedElement<u16, Qk>>,
pub accordant_shared_keys: Vec<TaggedElement<SharedKeyId, Sk>>,
pub address: Addr,
pub proof: Pr,
}
impl<Pk, Qk, Rk, Sk, Addr, Pr> RevealBobParameters<Pk, Qk, Rk, Sk, Addr, Pr> {
pub fn into_parameters<Ti, F>(self) -> Parameters<Pk, Qk, Rk, Sk, Addr, Ti, F, Pr> {
Parameters {
buy: self.buy,
cancel: self.cancel,
refund: self.refund,
punish: None,
adaptor: self.adaptor,
extra_arbitrating_keys: self.extra_arbitrating_keys,
arbitrating_shared_keys: self.arbitrating_shared_keys,
spend: self.spend,
extra_accordant_keys: self.extra_accordant_keys,
accordant_shared_keys: self.accordant_shared_keys,
proof: Some(self.proof),
destination_address: self.address,
cancel_timelock: None,
punish_timelock: None,
fee_strategy: None,
}
}
}
impl<Pk, Qk, Rk, Sk, Addr, Pr> fmt::Display for RevealBobParameters<Pk, Qk, Rk, Sk, Addr, Pr>
where
Pk: fmt::Debug,
Qk: fmt::Debug,
Rk: fmt::Debug,
Sk: fmt::Debug,
Addr: fmt::Debug,
Pr: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl<Pk, Qk, Rk, Sk, Addr, Pr> Encodable for RevealBobParameters<Pk, Qk, Rk, Sk, Addr, Pr>
where
Pk: CanonicalBytes,
Qk: CanonicalBytes,
Rk: CanonicalBytes,
Sk: CanonicalBytes,
Addr: CanonicalBytes,
Pr: CanonicalBytes,
{
fn consensus_encode<W: io::Write>(&self, s: &mut W) -> Result<usize, io::Error> {
let mut len = self.swap_id.consensus_encode(s)?;
len += self.buy.as_canonical_bytes().consensus_encode(s)?;
len += self.cancel.as_canonical_bytes().consensus_encode(s)?;
len += self.refund.as_canonical_bytes().consensus_encode(s)?;
len += self.adaptor.as_canonical_bytes().consensus_encode(s)?;
len += self.extra_arbitrating_keys.consensus_encode(s)?;
len += self.arbitrating_shared_keys.consensus_encode(s)?;
len += self.spend.as_canonical_bytes().consensus_encode(s)?;
len += self.extra_accordant_keys.consensus_encode(s)?;
len += self.accordant_shared_keys.consensus_encode(s)?;
len += self.address.as_canonical_bytes().consensus_encode(s)?;
Ok(len + self.proof.as_canonical_bytes().consensus_encode(s)?)
}
}
impl<Pk, Qk, Rk, Sk, Addr, Pr> Decodable for RevealBobParameters<Pk, Qk, Rk, Sk, Addr, Pr>
where
Pk: CanonicalBytes,
Qk: CanonicalBytes,
Rk: CanonicalBytes,
Sk: CanonicalBytes,
Addr: CanonicalBytes,
Pr: CanonicalBytes,
{
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
Ok(Self {
swap_id: Decodable::consensus_decode(d)?,
buy: Pk::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
cancel: Pk::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
refund: Pk::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
adaptor: Pk::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
extra_arbitrating_keys: Decodable::consensus_decode(d)?,
arbitrating_shared_keys: Decodable::consensus_decode(d)?,
spend: Qk::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
extra_accordant_keys: Decodable::consensus_decode(d)?,
accordant_shared_keys: Decodable::consensus_decode(d)?,
address: Addr::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
proof: Pr::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
})
}
}
impl_strict_encoding!(RevealBobParameters<Pk, Qk, Rk, Sk, Addr, Pr>, Pk: CanonicalBytes, Qk: CanonicalBytes, Rk: CanonicalBytes, Sk: CanonicalBytes, Addr: CanonicalBytes, Pr: CanonicalBytes);
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CoreArbitratingSetup<Px, Sig> {
pub swap_id: SwapId,
pub lock: Px,
pub cancel: Px,
pub refund: Px,
pub cancel_sig: Sig,
}
impl<Px, Sig> CoreArbitratingSetup<Px, Sig> {
pub fn into_arbitrating_tx(self) -> CoreArbitratingTransactions<Px> {
CoreArbitratingTransactions {
lock: self.lock,
cancel: self.cancel,
refund: self.refund,
}
}
}
impl<Px, Sig> fmt::Display for CoreArbitratingSetup<Px, Sig>
where
Px: fmt::Debug,
Sig: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl<Px, Sig> Encodable for CoreArbitratingSetup<Px, Sig>
where
Px: CanonicalBytes,
Sig: CanonicalBytes,
{
fn consensus_encode<W: io::Write>(&self, s: &mut W) -> Result<usize, io::Error> {
let mut len = self.swap_id.consensus_encode(s)?;
len += self.lock.as_canonical_bytes().consensus_encode(s)?;
len += self.cancel.as_canonical_bytes().consensus_encode(s)?;
len += self.refund.as_canonical_bytes().consensus_encode(s)?;
Ok(len + self.cancel_sig.as_canonical_bytes().consensus_encode(s)?)
}
}
impl<Px, Sig> Decodable for CoreArbitratingSetup<Px, Sig>
where
Px: CanonicalBytes,
Sig: CanonicalBytes,
{
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
Ok(Self {
swap_id: Decodable::consensus_decode(d)?,
lock: Px::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
cancel: Px::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
refund: Px::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
cancel_sig: Sig::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
})
}
}
impl_strict_encoding!(CoreArbitratingSetup<Px, Sig>, Px: CanonicalBytes, Sig: CanonicalBytes);
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct RefundProcedureSignatures<Sig, EncSig> {
pub swap_id: SwapId,
pub cancel_sig: Sig,
pub refund_adaptor_sig: EncSig,
}
impl<Sig, EncSig> fmt::Display for RefundProcedureSignatures<Sig, EncSig>
where
Sig: fmt::Debug,
EncSig: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl<Sig, EncSig> Encodable for RefundProcedureSignatures<Sig, EncSig>
where
Sig: CanonicalBytes,
EncSig: CanonicalBytes,
{
fn consensus_encode<W: io::Write>(&self, s: &mut W) -> Result<usize, io::Error> {
let mut len = self.swap_id.consensus_encode(s)?;
len += self.cancel_sig.as_canonical_bytes().consensus_encode(s)?;
Ok(len
+ self
.refund_adaptor_sig
.as_canonical_bytes()
.consensus_encode(s)?)
}
}
impl<Sig, EncSig> Decodable for RefundProcedureSignatures<Sig, EncSig>
where
Sig: CanonicalBytes,
EncSig: CanonicalBytes,
{
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
Ok(Self {
swap_id: Decodable::consensus_decode(d)?,
cancel_sig: Sig::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
refund_adaptor_sig: EncSig::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
})
}
}
impl_strict_encoding!(RefundProcedureSignatures<Sig, EncSig>, Sig: CanonicalBytes, EncSig: CanonicalBytes);
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct BuyProcedureSignature<Px, EncSig> {
pub swap_id: SwapId,
pub buy: Px,
pub buy_adaptor_sig: EncSig,
}
impl<Px, EncSig> fmt::Display for BuyProcedureSignature<Px, EncSig>
where
Px: fmt::Debug,
EncSig: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl<Px, EncSig> Encodable for BuyProcedureSignature<Px, EncSig>
where
Px: CanonicalBytes,
EncSig: CanonicalBytes,
{
fn consensus_encode<W: io::Write>(&self, s: &mut W) -> Result<usize, io::Error> {
let mut len = self.swap_id.consensus_encode(s)?;
len += self.buy.as_canonical_bytes().consensus_encode(s)?;
Ok(len
+ self
.buy_adaptor_sig
.as_canonical_bytes()
.consensus_encode(s)?)
}
}
impl<Px, EncSig> Decodable for BuyProcedureSignature<Px, EncSig>
where
Px: CanonicalBytes,
EncSig: CanonicalBytes,
{
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
Ok(Self {
swap_id: Decodable::consensus_decode(d)?,
buy: Px::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
buy_adaptor_sig: EncSig::from_canonical_bytes(unwrap_vec_ref!(d).as_ref())?,
})
}
}
impl_strict_encoding!(BuyProcedureSignature<Px, EncSig>, Px: consensus::CanonicalBytes, EncSig: consensus::CanonicalBytes);
#[derive(Clone, Debug, Hash, Display, Serialize, Deserialize)]
#[display(Debug)]
pub struct Abort {
pub swap_id: SwapId,
pub error_body: Option<String>,
}
impl Encodable for Abort {
fn consensus_encode<W: io::Write>(&self, s: &mut W) -> Result<usize, io::Error> {
let len = self.swap_id.consensus_encode(s)?;
Ok(len + self.error_body.consensus_encode(s)?)
}
}
impl Decodable for Abort {
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
Ok(Self {
swap_id: Decodable::consensus_decode(d)?,
error_body: Option::<String>::consensus_decode(d)?,
})
}
}
impl_strict_encoding!(Abort);