Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BIP21 parser: add tests that cover rounding errors #1073

Merged
merged 6 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ jobs:

- name: Run sdk-common tests
working-directory: libs/sdk-common
run: cargo test
run: cargo test --features liquid

- name: Check git status
env:
Expand Down
48 changes: 48 additions & 0 deletions libs/sdk-common/src/input_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,34 @@ pub(crate) mod tests {
Ok(())
}

/// BIP21 amounts which can lead to rounding errors.
/// The format is: (sat amount, BIP21 BTC amount)
fn get_bip21_rounding_test_vectors() -> Vec<(u64, f64)> {
vec![
(999, 0.0000_0999),
(1_000, 0.0000_1000),
(59_810, 0.0005_9810),
]
}

#[tokio::test]
async fn test_bitcoin_address_bip21_rounding() -> Result<()> {
for (amount_sat, amount_btc) in get_bip21_rounding_test_vectors() {
let addr = format!("bitcoin:1andreas3batLhQa2FawWjeyjCqyBzypd?amount={amount_btc}");

match parse(&addr).await? {
InputType::BitcoinAddress {
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]
#[cfg(feature = "liquid")]
async fn test_liquid_address() -> Result<()> {
Expand Down Expand Up @@ -830,6 +858,26 @@ pub(crate) mod tests {
Ok(())
}

#[tokio::test]
ok300 marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "liquid")]
async fn test_liquid_address_bip21_rounding() -> Result<()> {
let asset_id = elements::issuance::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_bolt11() -> Result<()> {
let bolt11 = "lnbc110n1p38q3gtpp5ypz09jrd8p993snjwnm68cph4ftwp22le34xd4r8ftspwshxhmnsdqqxqyjw5qcqpxsp5htlg8ydpywvsa7h3u4hdn77ehs4z4e844em0apjyvmqfkzqhhd2q9qgsqqqyssqszpxzxt9uuqzymr7zxcdccj5g69s8q7zzjs7sgxn9ejhnvdh6gqjcy22mss2yexunagm5r2gqczh8k24cwrqml3njskm548aruhpwssq9nvrvz";
Expand Down
Loading