Skip to content
Merged
18 changes: 6 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,7 @@ wasm-bindgen-test = "0.3"
features = [ "all" ]

[patch.crates-io]
strict_types = { git = "https://github.com/strict-types/strict-types" }
strict_types = { git = "https://github.com/strict-types/strict-types" }
commit_verify = { git = "https://github.com/LNP-BP/client_side_validation" }
bp-core = { git = "https://github.com/BP-WG/bp-core" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core" }
51 changes: 47 additions & 4 deletions src/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use std::num::ParseIntError;
use std::str::FromStr;

use bitcoin::{Address, Network};
use bp::Chain;
use fluent_uri::Uri;
use indexmap::IndexMap;
use rgb::{AttachId, ContractId, SecretSeal};
Expand Down Expand Up @@ -53,18 +55,27 @@ pub enum InvoiceState {
Attach(AttachId),
}

#[derive(Clone, Eq, PartialEq, Hash, Debug, Display)]
#[display(inner)]
pub enum Beneficiary {
BlindedSeal(SecretSeal),
WitnessUtxo(Address),
// TODO: add BifrostNode(),
}

#[derive(Clone, Eq, PartialEq, Debug, Display)]
// TODO: Change to custom display impl providing support for optionals & query
#[display("{transport}{contract}/{iface}/{value}@{seal}")]
#[display("{transport}{contract}/{iface}/{value}+{beneficiary}")]
pub struct RgbInvoice {
pub transport: RgbTransport,
pub contract: ContractId,
pub iface: TypeName,
pub operation: Option<TypeName>,
pub assignment: Option<TypeName>,
// pub owned_state: Option<String>,
pub seal: SecretSeal,
pub beneficiary: Beneficiary,
pub value: u64, // TODO: Change to TypedState
pub chain: Option<Chain>,
pub unknown_query: IndexMap<String, String>,
}

Expand All @@ -81,6 +92,15 @@ pub enum InvoiceParseError {
#[from]
Id(baid58::Baid58ParseError),

#[display(doc_comments)]
/// can't recognize beneficiary "": it should be either a bitcoin address or
/// a blinded UTXO seal.
Beneficiary(String),

#[display(doc_comments)]
/// network {0} is not supported.
UnsupportedNetwork(Network),

#[from]
Num(ParseIntError),

Expand All @@ -102,9 +122,31 @@ impl FromStr for RgbInvoice {
.map(|e| e.to_string())
.collect::<Vec<String>>();

let mut chain = None;

let mut assignment = path[2].split('@');
let (seal, value) = match (assignment.next(), assignment.next()) {
(Some(a), Some(b)) => (SecretSeal::from_str(b)?, u64::from_str_radix(a, 10)?),
(Some(a), Some(b)) => {
let value = u64::from_str_radix(a, 10)?;
let benefactor = match (SecretSeal::from_str(b), Address::from_str(b)) {
(Ok(seal), Err(_)) => Beneficiary::BlindedSeal(seal),
(Err(_), Ok(addr)) => {
chain = Some(match addr.network {
Network::Bitcoin => Chain::Bitcoin,
Network::Testnet => Chain::Testnet3,
Network::Signet => Chain::Signet,
Network::Regtest => Chain::Regtest,
unknown => return Err(InvoiceParseError::UnsupportedNetwork(unknown)),
});
Beneficiary::WitnessUtxo(addr.assume_checked())
}
(Err(_), Err(_)) => return Err(InvoiceParseError::Beneficiary(a.to_owned())),
(Ok(_), Ok(_)) => panic!(
"found a string which is both valid bitcoin address and UTXO blind seal"
),
};
(benefactor, value)
}
_ => return Err(InvoiceParseError::Invalid),
};

Expand All @@ -114,8 +156,9 @@ impl FromStr for RgbInvoice {
iface: TypeName::try_from(path[1].clone())?,
operation: None,
assignment: None,
seal,
beneficiary: seal,
value,
chain,
unknown_query: Default::default(),
})
}
Expand Down
14 changes: 1 addition & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@
extern crate amplify;

mod invoice;
mod pay;
pub mod psbt;

pub use invoice::{InvoiceParseError, InvoiceState, RgbInvoice, RgbTransport};
use rgbstd::interface::TransitionBuilder;
use rgbstd::persistence::Stash;

// 1. Construct main state transition with transition builder
// -- shortcut using invoice to do that construction (like .with_invoice())
Expand All @@ -76,14 +75,3 @@ use rgbstd::persistence::Stash;
// that output
// 3. Extract from PSBT all spent prevouts and construct blank state transitions
// for each one of them; embed them into PSBT

pub enum InvoiceInconsistency {}

impl RgbInvoice {
pub fn to_builder(
&self,
stash: &impl Stash,
) -> Result<TransitionBuilder, InvoiceInconsistency> {
todo!()
}
}
Loading