|
| 1 | +use crate::{formatter::TryFormat, parser::Parsable}; |
| 2 | +pub use rust_bitcoin; |
| 3 | +use rust_bitcoin::consensus::{Decodable, Encodable}; |
| 4 | + |
| 5 | +#[cfg(not(feature = "std"))] |
| 6 | +use alloc::vec::Vec; |
| 7 | + |
| 8 | +#[derive(Debug)] |
| 9 | +pub enum ConversionError { |
| 10 | + ParsingError, |
| 11 | + FormattingError, |
| 12 | +} |
| 13 | + |
| 14 | +pub trait ConvertFromInterlayBitcoin { |
| 15 | + type Output; |
| 16 | + fn to_rust_bitcoin(&self) -> Result<Self::Output, ConversionError>; |
| 17 | +} |
| 18 | + |
| 19 | +pub trait ConvertToInterlayBitcoin { |
| 20 | + type Output; |
| 21 | + fn to_interlay(&self) -> Result<Self::Output, ConversionError>; |
| 22 | +} |
| 23 | + |
| 24 | +/// Macro to implement type conversion from interlay type to rust-bitcoin, using consensus encoding |
| 25 | +macro_rules! impl_bitcoin_conversion { |
| 26 | + ($a:path, $b:path) => { |
| 27 | + impl ConvertFromInterlayBitcoin for $a { |
| 28 | + type Output = $b; |
| 29 | + fn to_rust_bitcoin(&self) -> Result<Self::Output, ConversionError> { |
| 30 | + let mut bytes = Vec::<u8>::new(); |
| 31 | + self.try_format(&mut bytes) |
| 32 | + .map_err(|_| ConversionError::FormattingError)?; |
| 33 | + |
| 34 | + let result = Self::Output::consensus_decode_from_finite_reader(&mut &bytes[..]) |
| 35 | + .map_err(|_| ConversionError::ParsingError)?; |
| 36 | + |
| 37 | + Ok(result) |
| 38 | + } |
| 39 | + } |
| 40 | + }; |
| 41 | +} |
| 42 | +/// Macro to implement type conversion to interlay type from rust-bitcoin, using consensus encoding |
| 43 | +macro_rules! impl_to_interlay_bitcoin_conversion { |
| 44 | + ($a:path, $b:path) => { |
| 45 | + impl ConvertToInterlayBitcoin for $b { |
| 46 | + type Output = $a; |
| 47 | + fn to_interlay(&self) -> Result<Self::Output, ConversionError> { |
| 48 | + let mut data: Vec<u8> = Vec::new(); |
| 49 | + self.consensus_encode(&mut data) |
| 50 | + .map_err(|_| ConversionError::FormattingError)?; |
| 51 | + let result = Self::Output::parse(&data, 0).map_err(|_| ConversionError::ParsingError)?; |
| 52 | + Ok(result.0) |
| 53 | + } |
| 54 | + } |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +macro_rules! impl_bidirectional_bitcoin_conversion { |
| 59 | + ($a:path, $b:path) => { |
| 60 | + impl_bitcoin_conversion!($a, $b); |
| 61 | + impl_to_interlay_bitcoin_conversion!($a, $b); |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +// there also exists rust_bitcoin::Script but we can't convert to that since it's unsized |
| 66 | +impl_bitcoin_conversion!(crate::Script, rust_bitcoin::ScriptBuf); |
| 67 | + |
| 68 | +// Transcation conversions |
| 69 | +impl_bidirectional_bitcoin_conversion!(crate::types::Transaction, rust_bitcoin::Transaction); |
| 70 | + |
| 71 | +// Address <--> Payload |
| 72 | +impl ConvertToInterlayBitcoin for rust_bitcoin::address::Payload { |
| 73 | + type Output = crate::Address; |
| 74 | + fn to_interlay(&self) -> Result<Self::Output, ConversionError> { |
| 75 | + let bitcoin_script = self.script_pubkey(); |
| 76 | + let bitcoin_script_bytes = bitcoin_script.to_bytes(); |
| 77 | + let interlay_script = crate::Script::from(bitcoin_script_bytes); |
| 78 | + Ok(crate::Address::from_script_pub_key(&interlay_script).map_err(|_| ConversionError::ParsingError)?) |
| 79 | + } |
| 80 | +} |
| 81 | +impl ConvertFromInterlayBitcoin for crate::Address { |
| 82 | + type Output = rust_bitcoin::address::Payload; |
| 83 | + fn to_rust_bitcoin(&self) -> Result<Self::Output, ConversionError> { |
| 84 | + let interlay_script = self.to_script_pub_key(); |
| 85 | + let bitcoin_script = rust_bitcoin::blockdata::script::Script::from_bytes(interlay_script.as_bytes()); |
| 86 | + Ok(rust_bitcoin::address::Payload::from_script(&bitcoin_script).map_err(|_| ConversionError::ParsingError)?) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(test)] |
| 91 | +mod tests { |
| 92 | + use super::*; |
| 93 | + use crate::parser::parse_transaction; |
| 94 | + #[test] |
| 95 | + fn test_bitcoin_compat() { |
| 96 | + // txid eb3db053cd139147f2fd676cf59a491fd5aebc54bddfde829704585b659126fc |
| 97 | + let raw_tx = "0100000000010120e6fb8f0e2cfb8667a140a92d045d5db7c1b56635790bc907c3e71d43720a150e00000017160014641e441c2ba32dd7cf05afde7922144dd106b09bffffffff019dbd54000000000017a914bd847a4912984cf6152547feca51c1b9c2bcbe2787024830450221008f00033064c26cfca4dc98e5dba800b18729c3441dca37b49358ae0df9be7fad02202a81085318466ea66ef390d5dab6737e44a05f7f2e747932ebba917e0098f37d012102c109fc47335c3a2e206d462ad52590b1842aa9d6e0eb9c683c896fa8723590b400000000"; |
| 98 | + let tx_bytes = hex::decode(&raw_tx).unwrap(); |
| 99 | + let interlay_transaction = parse_transaction(&tx_bytes).unwrap(); |
| 100 | + |
| 101 | + let rust_bitcoin_transaction = interlay_transaction.to_rust_bitcoin().unwrap(); |
| 102 | + |
| 103 | + // check that the rust-bitcoin type encoded to the same bytes |
| 104 | + let mut reencoded_bytes: Vec<u8> = Vec::new(); |
| 105 | + rust_bitcoin_transaction.consensus_encode(&mut reencoded_bytes).unwrap(); |
| 106 | + assert_eq!(tx_bytes, reencoded_bytes); |
| 107 | + |
| 108 | + // check that the conversion back works |
| 109 | + assert_eq!(interlay_transaction, rust_bitcoin_transaction.to_interlay().unwrap()); |
| 110 | + } |
| 111 | +} |
0 commit comments