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

Handle chain information in explicit form in invoices #104

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 Cargo.lock

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

22 changes: 17 additions & 5 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 @@ -114,9 +115,20 @@ impl RgbInvoiceBuilder {
self.set_amount(coins as u64, cents as u64, precision)
}

pub fn set_chain(mut self, chain: impl Into<Chain>) -> Self {
self.0.chain = Some(chain.into());
self
pub fn set_chain(mut self, chain: impl Into<Chain>) -> Result<Self, Self> {
let chain = chain.into();
match (self.0.beneficiary.chain_info(), chain) {
(None, _) => {}
(Some(chain1), chain2) if chain1 == chain2 => {}
(Some(chain1), chain2)
if chain1.is_testnet() &&
chain2.is_testnet() &&
chain1 != Chain::Regtest &&
chain2 != Chain::Regtest => {}
_ => return Err(self),
}
self.0.chain = chain;
Ok(self)
}

pub fn set_expiry_timestamp(mut self, expiry: i64) -> Self {
Expand Down
26 changes: 24 additions & 2 deletions invoice/src/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use bp::Address;
use bp::{Address, AddressNetwork};
use indexmap::IndexMap;
use rgb::interface::TypedState;
use rgb::{AttachId, Chain, ContractId, SecretSeal};
Expand Down Expand Up @@ -55,6 +55,28 @@ pub enum Beneficiary {
WitnessUtxo(Address),
}

impl Beneficiary {
pub fn chain_info(&self) -> Option<Chain> {
match self {
Beneficiary::BlindedSeal(_) => None,
Beneficiary::WitnessUtxo(Address {
network: AddressNetwork::Mainnet,
..
}) => Some(Chain::Bitcoin),
Beneficiary::WitnessUtxo(Address {
network: AddressNetwork::Regtest,
..
}) => Some(Chain::Regtest),
Beneficiary::WitnessUtxo(Address {
network: AddressNetwork::Testnet,
..
}) => Some(Chain::Testnet3),
}
}

pub fn has_chain_info(&self) -> bool { self.chain_info().is_some() }
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct RgbInvoice {
pub transports: Vec<RgbTransport>,
Expand All @@ -64,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
Loading