diff --git a/invoice/src/builder.rs b/invoice/src/builder.rs index 6bbdd3b3..ab229fdc 100644 --- a/invoice/src/builder.rs +++ b/invoice/src/builder.rs @@ -32,15 +32,16 @@ pub struct RgbInvoiceBuilder(RgbInvoice); impl RgbInvoiceBuilder { pub fn new(beneficiary: impl Into) -> 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!(), }) @@ -126,7 +127,7 @@ impl RgbInvoiceBuilder { chain2 != Chain::Regtest => {} _ => return Err(self), } - self.0.chain = Some(chain); + self.0.chain = chain; Ok(self) } diff --git a/invoice/src/invoice.rs b/invoice/src/invoice.rs index cda5b4a9..52e80c0c 100644 --- a/invoice/src/invoice.rs +++ b/invoice/src/invoice.rs @@ -86,7 +86,7 @@ pub struct RgbInvoice { pub assignment: Option, pub beneficiary: Beneficiary, pub owned_state: TypedState, - pub chain: Option, + pub chain: Chain, /// UTC unix timestamp pub expiry: Option, pub unknown_query: IndexMap, diff --git a/invoice/src/parse.rs b/invoice/src/parse.rs index a0e8c298..e9c62511 100644 --- a/invoice/src/parse.rs +++ b/invoice/src/parse.rs @@ -127,15 +127,11 @@ pub enum InvoiceParseError { impl RgbInvoice { #[inline] fn non_default_chain(&self) -> Option { - 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] @@ -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, }) @@ -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!( @@ -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( @@ -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",),