|
| 1 | +// RGB node providing smart contracts functionality for Bitcoin & Lightning. |
| 2 | +// |
| 3 | +// Written in 2022 by |
| 4 | +// Dr. Maxim Orlovsky <orlovsky@lnp-bp.org> |
| 5 | +// |
| 6 | +// Copyright (C) 2022 by LNP/BP Standards Association, Switzerland. |
| 7 | +// |
| 8 | +// You should have received a copy of the MIT License along with this software. |
| 9 | +// If not, see <https://opensource.org/licenses/MIT>. |
| 10 | + |
| 11 | +use bitcoin::OutPoint; |
| 12 | +use bp::seals::txout::CloseMethod; |
| 13 | + |
| 14 | +#[derive(From, PartialEq, Eq, Debug, Clone, StrictEncode, StrictDecode)] |
| 15 | +pub struct Reveal { |
| 16 | + /// Outpoint blinding factor (generated when the utxo blinded was created) |
| 17 | + pub blinding_factor: u64, |
| 18 | + |
| 19 | + /// Locally-controlled outpoint (specified when the utxo blinded was created) |
| 20 | + pub outpoint: OutPoint, |
| 21 | + |
| 22 | + /// method (specified when the utxo blinded was created) |
| 23 | + pub close_method: CloseMethod, |
| 24 | +} |
| 25 | + |
| 26 | +impl std::fmt::Display for Reveal { |
| 27 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 28 | + write!(f, "{}@{}#{}", self.close_method, self.outpoint, self.blinding_factor) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// Parses a blinding factor. |
| 33 | +fn parse_blind(s: &str) -> Result<u64, ParseRevealError> { |
| 34 | + s.parse().map_err(ParseRevealError::BlindingFactor) |
| 35 | +} |
| 36 | + |
| 37 | +impl ::core::str::FromStr for Reveal { |
| 38 | + type Err = ParseRevealError; |
| 39 | + |
| 40 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 41 | + // 9 + 19 + 1 + 64 + 1 + 10 |
| 42 | + if s.len() > 97 { |
| 43 | + return Err(ParseRevealError::TooLong); |
| 44 | + } |
| 45 | + let find_method = s.find('@'); |
| 46 | + if find_method == None { |
| 47 | + return Err(ParseRevealError::Format); |
| 48 | + } |
| 49 | + |
| 50 | + let colon_method = find_method.unwrap(); |
| 51 | + if colon_method == 0 || colon_method == s.len() - 1 { |
| 52 | + return Err(ParseRevealError::Format); |
| 53 | + } |
| 54 | + |
| 55 | + let find_blind = s.find('#'); |
| 56 | + if find_blind == None { |
| 57 | + return Err(ParseRevealError::Format); |
| 58 | + } |
| 59 | + |
| 60 | + let colon_blind = find_blind.unwrap(); |
| 61 | + if colon_blind == 0 || colon_blind == s.len() - 1 { |
| 62 | + return Err(ParseRevealError::Format); |
| 63 | + } |
| 64 | + |
| 65 | + Ok(Reveal { |
| 66 | + close_method: match CloseMethod::from_str(&s[..colon_method]) { |
| 67 | + Ok(it) => it, |
| 68 | + Err(_) => return Err(ParseRevealError::CloseMethod), |
| 69 | + }, |
| 70 | + outpoint: match OutPoint::from_str(&s[colon_method + 1..colon_blind]) { |
| 71 | + Ok(it) => it, |
| 72 | + Err(_) => return Err(ParseRevealError::Outpoint), |
| 73 | + }, |
| 74 | + blinding_factor: parse_blind(&s[colon_blind + 1..])?, |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// An error in parsing an OutPoint. |
| 80 | +#[derive(Clone, PartialEq, Eq, Debug)] |
| 81 | +pub enum ParseRevealError { |
| 82 | + /// Error in outpoint part. |
| 83 | + CloseMethod, |
| 84 | + /// Error in outpoint part. |
| 85 | + Outpoint, |
| 86 | + /// Error in blinding factor part. |
| 87 | + BlindingFactor(::core::num::ParseIntError), |
| 88 | + /// Error in general format. |
| 89 | + Format, |
| 90 | + /// Size exceeds max. |
| 91 | + TooLong, |
| 92 | +} |
| 93 | + |
| 94 | +impl std::fmt::Display for ParseRevealError { |
| 95 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 96 | + match *self { |
| 97 | + ParseRevealError::CloseMethod => write!(f, "error parsing CloseMethod"), |
| 98 | + ParseRevealError::Outpoint => write!(f, "error parsing OutPoint"), |
| 99 | + ParseRevealError::BlindingFactor(ref e) => { |
| 100 | + write!(f, "error parsing blinding_factor: {}", e) |
| 101 | + } |
| 102 | + ParseRevealError::Format => { |
| 103 | + write!(f, "Reveal not in <blind_factor>@<txid>:<vout> format") |
| 104 | + } |
| 105 | + ParseRevealError::TooLong => write!(f, "reveal should be at most 95 digits"), |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl ::std::error::Error for ParseRevealError { |
| 111 | + fn cause(&self) -> Option<&dyn ::std::error::Error> { |
| 112 | + match *self { |
| 113 | + ParseRevealError::BlindingFactor(ref e) => Some(e), |
| 114 | + _ => None, |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments