Skip to content

Commit

Permalink
invoice: make chain non-optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Sep 18, 2023
1 parent 68cd0ec commit d452bb6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
7 changes: 4 additions & 3 deletions invoice/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ pub struct RgbInvoiceBuilder(RgbInvoice);

impl RgbInvoiceBuilder {
pub fn new(beneficiary: impl Into<Beneficiary>) -> Self {
let beneficiary = beneficiary.into();
Self(RgbInvoice {
transports: vec![RgbTransport::UnspecifiedMeans],
contract: None,
iface: None,
operation: None,
assignment: None,
beneficiary: beneficiary.into(),
chain: beneficiary.chain_info().unwrap_or(Chain::Bitcoin),
beneficiary,
owned_state: TypedState::Void,
chain: None,
expiry: None,
unknown_query: none!(),
})
Expand Down Expand Up @@ -126,7 +127,7 @@ impl RgbInvoiceBuilder {
chain2 != Chain::Regtest => {}
_ => return Err(self),
}
self.0.chain = Some(chain);
self.0.chain = chain;
Ok(self)
}

Expand Down
2 changes: 1 addition & 1 deletion invoice/src/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub struct RgbInvoice {
pub assignment: Option<FieldName>,
pub beneficiary: Beneficiary,
pub owned_state: TypedState,
pub chain: Option<Chain>,
pub chain: Chain,
/// UTC unix timestamp
pub expiry: Option<i64>,
pub unknown_query: IndexMap<String, String>,
Expand Down
37 changes: 18 additions & 19 deletions invoice/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,11 @@ pub enum InvoiceParseError {
impl RgbInvoice {
#[inline]
fn non_default_chain(&self) -> Option<Chain> {
if self.beneficiary.has_chain_info() {
return None;
}
if let Some(chain) = self.chain {
if chain != Chain::Bitcoin {
return Some(chain);
}
if self.chain == Chain::Bitcoin {
None
} else {
Some(self.chain)
}
None
}

#[inline]
Expand Down Expand Up @@ -384,7 +380,7 @@ impl FromStr for RgbInvoice {
assignment: None,
beneficiary,
owned_state: value,
chain,
chain: chain.unwrap_or(Chain::Bitcoin),
expiry,
unknown_query: query_params,
})
Expand Down Expand Up @@ -483,7 +479,10 @@ mod test {
.set_chain(Chain::Testnet3)
.unwrap()
.finish();
assert_eq!(invoice.to_string(), "rgb:~/RGB20/mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk");
assert_eq!(
invoice.to_string(),
"rgb:~/RGB20/mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk?chain=testnet"
);

// address-based regtest - mismatching
assert!(
Expand All @@ -499,44 +498,44 @@ mod test {
"rgb:~/RGB20/utxob:egXsFnw-5Eud7WKYn-7DVQvcPbc-rR69YmgmG-veacwmUFo-uMFKFb",
)
.unwrap();
assert_eq!(invoice.chain, None);
assert_eq!(invoice.chain, Chain::Bitcoin);

let invoice = RgbInvoice::from_str(
"rgb:~/RGB20/utxob:egXsFnw-5Eud7WKYn-7DVQvcPbc-rR69YmgmG-veacwmUFo-uMFKFb?\
chain=testnet",
)
.unwrap();
assert_eq!(invoice.chain, Some(Chain::Testnet3));
assert_eq!(invoice.chain, Chain::Testnet3);

let invoice = RgbInvoice::from_str(
"rgb:~/RGB20/utxob:egXsFnw-5Eud7WKYn-7DVQvcPbc-rR69YmgmG-veacwmUFo-uMFKFb?\
chain=testnet3",
)
.unwrap();
assert_eq!(invoice.chain, Some(Chain::Testnet3));
assert_eq!(invoice.chain, Chain::Testnet3);

let invoice = RgbInvoice::from_str(
"rgb:~/RGB20/utxob:egXsFnw-5Eud7WKYn-7DVQvcPbc-rR69YmgmG-veacwmUFo-uMFKFb?chain=signet",
)
.unwrap();
assert_eq!(invoice.chain, Some(Chain::Signet));
assert_eq!(invoice.chain, Chain::Signet);

let invoice = RgbInvoice::from_str(
"rgb:~/RGB20/utxob:egXsFnw-5Eud7WKYn-7DVQvcPbc-rR69YmgmG-veacwmUFo-uMFKFb?\
chain=regtest",
)
.unwrap();
assert_eq!(invoice.chain, Some(Chain::Regtest));
assert_eq!(invoice.chain, Chain::Regtest);

let invoice =
RgbInvoice::from_str("rgb:~/RGB20/bc1qpws79r3ea4yy2ujsahwrmy2gutdj8w5whnhket").unwrap();
assert_eq!(invoice.chain, None);
assert_eq!(invoice.chain, Chain::Bitcoin);

let invoice = RgbInvoice::from_str(
"rgb:~/RGB20/bc1qpws79r3ea4yy2ujsahwrmy2gutdj8w5whnhket?chain=bitcoin",
)
.unwrap();
assert_eq!(invoice.chain, Some(Chain::Bitcoin));
assert_eq!(invoice.chain, Chain::Bitcoin);

assert_eq!(
RgbInvoice::from_str(
Expand All @@ -551,12 +550,12 @@ mod test {
let invoice =
RgbInvoice::from_str("rgb:~/RGB20/mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk?chain=testnet")
.unwrap();
assert_eq!(invoice.chain, Some(Chain::Testnet3));
assert_eq!(invoice.chain, Chain::Testnet3);

let invoice =
RgbInvoice::from_str("rgb:~/RGB20/mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk?chain=signet")
.unwrap();
assert_eq!(invoice.chain, Some(Chain::Signet));
assert_eq!(invoice.chain, Chain::Signet);

assert_eq!(
RgbInvoice::from_str("rgb:~/RGB20/mxVFsFW5N4mu1HPkxPttorvocvzeZ7KZyk?chain=regtest",),
Expand Down

0 comments on commit d452bb6

Please sign in to comment.