Skip to content
This repository was archived by the owner on Apr 13, 2021. It is now read-only.

Commit c8755e1

Browse files
authored
Add accessors to Invoice to get hrp data
Add accessors to Invoice to get hrp data
2 parents 130a123 + 09cf64f commit c8755e1

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ license = "Apache-2.0"
66

77
[dependencies]
88
bech32 = "0.5.0"
9-
secp256k1 = "0.10"
10-
bitcoin = "0.13"
9+
secp256k1 = "0.11"
10+
bitcoin = "0.14"
1111
num-traits = "0.2"
12-
bitcoin_hashes = {git = "https://github.com/rust-bitcoin/bitcoin_hashes"}
12+
bitcoin_hashes = {git = "https://github.com/rust-bitcoin/bitcoin_hashes"}

src/de.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,13 @@ impl FromStr for RawHrp {
283283
let si_prefix: Option<SiPrefix> = if parts.2.is_empty() {
284284
None
285285
} else {
286-
Some(parts.2.parse()?)
286+
let si: SiPrefix = parts.2.parse()?;
287+
if let Some(amt) = amount {
288+
if amt.checked_mul(si.multiplier()).is_none() {
289+
return Err(ParseError::IntegerOverflowError);
290+
}
291+
}
292+
Some(si)
287293
};
288294

289295
Ok(RawHrp {
@@ -931,4 +937,4 @@ mod test {
931937
)
932938
)
933939
}
934-
}
940+
}

src/lib.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,20 @@ pub enum SiPrefix {
159159
Pico,
160160
}
161161

162-
#[derive(Eq, PartialEq, Debug)]
162+
impl SiPrefix {
163+
/// Returns the multiplier to go from a BTC value to picoBTC implied by this SiPrefix.
164+
/// This is effectively 10^12 * the prefix multiplier
165+
pub fn multiplier(&self) -> u64 {
166+
match self {
167+
&SiPrefix::Milli => 1_000_000_000,
168+
&SiPrefix::Micro => 1_000_000,
169+
&SiPrefix::Nano => 1_000,
170+
&SiPrefix::Pico => 1,
171+
}
172+
}
173+
}
174+
175+
#[derive(Eq, PartialEq, Debug, Clone)]
163176
pub enum Currency {
164177
Bitcoin,
165178
BitcoinTestnet,
@@ -312,14 +325,6 @@ impl<D: tb::Bool, H: tb::Bool> InvoiceBuilder<D, H> {
312325
self
313326
}
314327

315-
/// Sets the amount as a combination of a SI prefix and a multiplier. This function won't change
316-
/// the SI prefix even if there is a more optimal one.
317-
pub fn amount_si(mut self, amount: u64, si_prefix: SiPrefix) -> Self {
318-
self.amount = Some(amount);
319-
self.si_prefix = Some(si_prefix);
320-
self
321-
}
322-
323328
/// Sets the timestamp. `time` is a UNIX timestamp.
324329
pub fn timestamp(mut self, time: u64) -> Self {
325330
self.timestamp = Some(time);
@@ -667,6 +672,16 @@ impl RawInvoice {
667672
num_traits => None,
668673
}).collect::<Vec<&Route>>()
669674
}
675+
676+
pub fn amount_pico_btc(&self) -> Option<u64> {
677+
self.hrp.raw_amount.map(|v| {
678+
v * self.hrp.si_prefix.as_ref().map_or(1_000_000_000_000, |si| { si.multiplier() })
679+
})
680+
}
681+
682+
pub fn currency(&self) -> Currency {
683+
self.hrp.currency.clone()
684+
}
670685
}
671686

672687
impl Invoice {
@@ -778,6 +793,14 @@ impl Invoice {
778793
pub fn routes(&self) -> Vec<&Route> {
779794
self.signed_invoice.routes()
780795
}
796+
797+
pub fn currency(&self) -> Currency {
798+
self.signed_invoice.currency()
799+
}
800+
801+
pub fn amount_pico_btc(&self) -> Option<u64> {
802+
self.signed_invoice.amount_pico_btc()
803+
}
781804
}
782805

783806
impl From<TaggedField> for RawTaggedField {
@@ -1033,4 +1056,4 @@ mod test {
10331056

10341057
assert!(new_signed.check_signature());
10351058
}
1036-
}
1059+
}

0 commit comments

Comments
 (0)