Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
debug invoicing
Browse files Browse the repository at this point in the history
dr-orlovsky committed Jan 28, 2025

Verified

This commit was signed with the committer’s verified signature.
dr-orlovsky Dr. Maxim Orlovsky
1 parent c39d12e commit ebb9ed6
Showing 9 changed files with 124 additions and 30 deletions.
19 changes: 10 additions & 9 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ name = "rgb"
[dependencies]
amplify.workspace = true
strict_encoding.workspace = true
strict_types.workspace = true
bp-wallet = { workspace = true, features = ["fs", "serde", "cli"] }
bp-electrum = { workspace = true }
bp-esplora = { workspace = true }
105 changes: 86 additions & 19 deletions cli/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -35,13 +35,17 @@ use bpwallet::indexers::esplora;
use bpwallet::psbt::TxParams;
use bpwallet::{AnyIndexer, Keychain, Network, Psbt, Sats, Wpkh, XpubDerivable};
use clap::ValueHint;
use rgb::invoice::RgbInvoice;
use rgb::invoice::{RgbBeneficiary, RgbInvoice};
use rgb::popls::bp::file::{BpDirMound, DirBarrow};
use rgb::popls::bp::{OpRequestSet, PrefabBundle, WoutAssignment};
use rgb::{AuthToken, Consensus, ContractId, CreateParams, Outpoint};
use rgb::{
AuthToken, CallScope, Consensus, ContractId, ContractRef, CreateParams, MethodName, Outpoint,
StateName,
};
use rgbp::descriptor::RgbDescr;
use rgbp::{CoinselectStrategy, RgbDirRuntime, RgbWallet};
use strict_encoding::{StrictDeserialize, StrictSerialize};
use strict_encoding::{StrictDeserialize, StrictSerialize, TypeName};
use strict_types::StrictVal;

use crate::opts::WalletOpts;

@@ -162,19 +166,47 @@ pub enum Cmd {
wallet: Option<String>,
},

// TODO: Convert to `invoice` command, also use contract names to simplify invoice creation
/// Generate a seal for an invoice
Seal {
/// Generate an invoice
Invoice {
/// Wallet to use
#[clap(short, long, global = true, env = RGB_WALLET_ENV)]
wallet: Option<String>,

/// Generate a witness output-based seal
/// Just generate a single-use seal, and not an entire invoice
#[clap(long)]
seal_only: bool,

/// Use witness output-based seal
#[clap(short = 'o', long)]
wout: bool,

/// Nonce number to use
nonce: u64,
#[clap(long, global = true)]
nonce: Option<u64>,

/// Contract to use
contract: Option<ContractRef>,

/// API name to interface the contract
///
/// If skipped, a default contract API will be used.
#[clap(short, long, global = true)]
api: Option<TypeName>,

/// Method name to call the contract with
///
/// If skipped, a default API method will be used.
#[clap(short, long, global = true)]
method: Option<MethodName>,

/// State name used for the invoice
///
/// If skipped, a default API state for the default method will be used.
#[clap(short, long, global = true)]
state: Option<StateName>,

/// Invoiced state value
value: Option<u64>,
},

/// Print out a contract state
@@ -429,27 +461,62 @@ impl Args {
println!("{addr}");
}

Cmd::Seal { wallet, wout, nonce } => {
Cmd::Invoice {
wallet,
seal_only,
wout,
nonce,
contract,
api,
method,
state,
value,
} => {
let mut runtime = self.runtime(wallet.as_deref());
if *wout {
let beneficiary = if *wout {
let wout = runtime.wout(*nonce);
println!("{wout}");
RgbBeneficiary::WitnessOut(wout)
} else {
match runtime.auth_token(*nonce) {
None => println!(
"Wallet has no unspent outputs; try `fund` first, or use `-w` flag to \
generate a witness output-based seal"
),
Some(token) => println!("{token}"),
}
let auth = runtime.auth_token(*nonce).ok_or(anyhow::anyhow!(
"Wallet has no unspent outputs; try `fund` first, or use `-w` flag to \
generate a witness output-based seal"
))?;
RgbBeneficiary::Token(auth)
};
if *seal_only {
println!("{beneficiary}");
return Ok(());
}

let contract_id = if let Some(contract) = contract {
let id = runtime
.mound
.find_contract_id(contract.clone())
.ok_or(anyhow::anyhow!("unknown contract '{contract}'"))?;
CallScope::ContractId(id)
} else {
CallScope::ContractQuery(s!(""))
};
let value = value.map(StrictVal::num);
let mut invoice = RgbInvoice::new(contract_id, beneficiary, value);
if let Some(api) = api {
invoice = invoice.use_api(api.clone());
}
if let Some(method) = method {
invoice = invoice.use_method(method.clone());
}
if let Some(state) = state {
invoice = invoice.use_state(state.clone());
}

println!("{invoice}");
}

Cmd::State { wallet, all, global, owned, contract } => {
let mut runtime = self.runtime(wallet.wallet.as_deref());
if wallet.sync {
let indexer = self.indexer(&wallet.resolver);
runtime.wallet.update(&indexer);
runtime.wallet.update(&indexer, false);
println!();
}
let state = if *all {
2 changes: 2 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

#[macro_use]
extern crate amplify;
#[macro_use]
extern crate clap;

1 change: 1 addition & 0 deletions examples/data/bitcoin.testnet/alice.wallet/descriptor.toml
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ network = "testnet4"
[generator]
seals = []
noise = "84ddd1f3abf251b47c878b454c94733a7408ff8938c891ee04760b42ff5aff01"
nonce = 0

[generator.deriver.opretOnly]
wpkh = "[cf05f8ea/84h/1h/0h]tpubDCpZeo9QAvMetovHqqqMgzRnWU2Wy5cgZUG9g3v6SJLihmARTw2sSNga8xfZTqqAFJpoTgEnVjwLjUEmf36GZ2gzba5w3GbjDbxLitzDGZA/<0;1>/*"
1 change: 1 addition & 0 deletions examples/data2/bitcoin.testnet/bob.wallet/descriptor.toml
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ network = "testnet4"

[generator]
noise = "b2594d09688565e0206593e720711eea03d560e38b491b4e3c66f9e3957aac86"
nonce = 0

[generator.deriver.opretOnly]
wpkh = "[cf05f8ea/84h/1h/1h]tpubDCpZeo9QAvMewp1ZhxjW25z3DSFC2eyt4wYimHuDNMkGzGhZsfUffCULroWTVUEfEaERv1rbbYPqG6fYb5Uivw86M3y4erJ7tGLRYcdbEFE/<0;1>/*"
3 changes: 2 additions & 1 deletion examples/script.sh
Original file line number Diff line number Diff line change
@@ -15,9 +15,10 @@ cp -r examples/data/bitcoin.testnet/DemoToken.contract examples/data2/bitcoin.te
$RGB contracts
$RGB state -go -w alice
#$RGB fund alice
$RGB_2 seal -w bob 0
INVOICE=$($RGB_2 invoice -w bob --nonce 0 DemoToken 10)

rm examples/transfer.psbt
$RGB script -w alice "$INVOICE" examples/Transfer.yaml
$RGB exec -w alice examples/Transfer.yaml examples/transfer.pfab 1000 examples/transfer.psbt

$RGB complete -w alice examples/transfer.pfab examples/transfer.psbt
15 changes: 14 additions & 1 deletion src/descriptor.rs
Original file line number Diff line number Diff line change
@@ -230,6 +230,7 @@ pub struct RgbDescr<K: DeriveSet = XpubDerivable> {
deriver: RgbDeriver<K>,
seals: SealDescr,
noise: Bytes32,
nonce: u64,
}

impl<K: DeriveSet> RgbDescr<K> {
@@ -239,7 +240,12 @@ impl<K: DeriveSet> RgbDescr<K> {
StdDescr::TrKey(tr) => RgbDeriver::Universal { tr: Tr::KeyOnly(tr), tweaks: empty!() },
_ => unreachable!(),
};
Self { deriver, seals: empty!(), noise: noise.into().into() }
Self {
deriver,
seals: empty!(),
noise: noise.into().into(),
nonce: 0,
}
}

pub fn key_only_unfunded(internal_key: K, noise: impl Into<[u8; 32]>) -> Self
@@ -251,9 +257,16 @@ impl<K: DeriveSet> RgbDescr<K> {
},
seals: empty!(),
noise: noise.into().into(),
nonce: 0,
}
}

pub fn next_nonce(&mut self) -> u64 {
let nonce = self.nonce;
self.nonce += 1;
nonce
}

pub fn noise(&self) -> Bytes32 { self.noise }

pub fn seals(&self) -> impl Iterator<Item = &TxoSeal> { self.seals.iter() }
7 changes: 7 additions & 0 deletions src/wallet.rs
Original file line number Diff line number Diff line change
@@ -72,6 +72,13 @@ impl WalletProvider for RgbWallet {
}

fn next_address(&mut self) -> Address { self.0.next_address(Keychain::OUTER, true) }

fn next_nonce(&mut self) -> u64 {
self.0.descriptor_mut(|d| unsafe {
d.with_descriptor_mut(|d| Ok::<_, Infallible>(d.next_nonce()))
.unwrap_unchecked()
})
}
}

impl RgbWallet {

0 comments on commit ebb9ed6

Please sign in to comment.