-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BIP21 parser: add tests that cover rounding errors (#1073)
* BIP21 parser: add tests that cover rounding errors * Enable liquid feature in CI tests * Move liquid BIP21 rounding test to liquid module * Fix BIP21 amount parsing for Liquid * Add test for reverse BIP21 amount parsing for Liquid * Cargo fmt
- Loading branch information
Showing
4 changed files
with
86 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,53 @@ | ||
pub mod bip21; | ||
pub use bip21::*; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use anyhow::{anyhow, Result}; | ||
use elements::AssetId; | ||
|
||
use crate::input_parser::tests::get_bip21_rounding_test_vectors; | ||
use crate::input_parser::*; | ||
use crate::liquid::LiquidAddressData; | ||
|
||
#[tokio::test] | ||
async fn test_liquid_address_bip21_rounding() -> Result<()> { | ||
let asset_id = AssetId::LIQUID_BTC.to_string(); | ||
for (amount_sat, amount_btc) in get_bip21_rounding_test_vectors() { | ||
let addr = format!("liquidnetwork:tlq1qqw5ur50rnvcx33vmljjtnez3hrtl6n7vs44tdj2c9fmnxrrgzgwnhw6jtpn8cljkmlr8tgfw9hemrr5y8u2nu024hhak3tpdk?amount={amount_btc}&assetid={asset_id}"); | ||
|
||
match parse(&addr).await? { | ||
InputType::LiquidAddress { | ||
address: addr_with_amount_parsed, | ||
} => { | ||
assert_eq!(addr_with_amount_parsed.amount_sat, Some(amount_sat)); | ||
} | ||
_ => return Err(anyhow!("Invalid type parsed")), | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_liquid_address_bip21_rounding_reverse() -> Result<()> { | ||
for (amount_sat, amount_btc) in get_bip21_rounding_test_vectors() { | ||
let data = LiquidAddressData { | ||
address: "tlq1qqw5ur50rnvcx33vmljjtnez3hrtl6n7vs44tdj2c9fmnxrrgzgwnhw6jtpn8cljkmlr8tgfw9hemrr5y8u2nu024hhak3tpdk".to_string(), | ||
network: crate::model::Network::Bitcoin, | ||
asset_id: Some(AssetId::LIQUID_BTC.to_string()), | ||
amount_sat: Some(amount_sat), | ||
label: None, | ||
message: None, | ||
}; | ||
|
||
let serialized = data | ||
.to_uri() | ||
.map_err(|e| anyhow!("BIP21 URI serialization error {e:?}"))?; | ||
|
||
assert!(serialized.contains(&format!("amount={amount_btc}"))); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |