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

Add command to export BIP-329 labels for wallet outputs #3120

Merged
merged 10 commits into from
Apr 16, 2024
Merged
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 crates/mockcore/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl Api for Server {
keypool_size: 0,
keypool_size_hd_internal: 0,
pay_tx_fee: Amount::from_sat(0),
private_keys_enabled: false,
private_keys_enabled: true,
scanning: None,
tx_count: 0,
unconfirmed_balance: Amount::from_sat(0),
Expand Down
18 changes: 11 additions & 7 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod create;
pub mod dump;
pub mod inscribe;
pub mod inscriptions;
mod label;
pub mod mint;
pub mod outputs;
pub mod receive;
Expand Down Expand Up @@ -44,6 +45,8 @@ pub(crate) enum Subcommand {
Balance,
#[command(about = "Create inscriptions and runes")]
Batch(batch_command::Batch),
#[command(about = "List unspent cardinal outputs in wallet")]
Cardinals,
#[command(about = "Create new wallet")]
Create(create::Create),
#[command(about = "Dump wallet descriptors")]
Expand All @@ -52,8 +55,12 @@ pub(crate) enum Subcommand {
Inscribe(inscribe::Inscribe),
#[command(about = "List wallet inscriptions")]
Inscriptions,
#[command(about = "Export output labels")]
Label,
#[command(about = "Mint a rune")]
Mint(mint::Mint),
#[command(about = "List all unspent outputs in wallet")]
Outputs,
#[command(about = "Generate receive address")]
Receive(receive::Receive),
#[command(about = "Restore wallet")]
Expand All @@ -66,10 +73,6 @@ pub(crate) enum Subcommand {
Send(send::Send),
#[command(about = "See wallet transactions")]
Transactions(transactions::Transactions),
#[command(about = "List all unspent outputs in wallet")]
Outputs,
#[command(about = "List unspent cardinal outputs in wallet")]
Cardinals,
}

impl WalletCommand {
Expand Down Expand Up @@ -97,18 +100,19 @@ impl WalletCommand {
match self.subcommand {
Subcommand::Balance => balance::run(wallet),
Subcommand::Batch(batch) => batch.run(wallet),
Subcommand::Cardinals => cardinals::run(wallet),
Subcommand::Create(_) | Subcommand::Restore(_) => unreachable!(),
Subcommand::Dump => dump::run(wallet),
Subcommand::Inscribe(inscribe) => inscribe.run(wallet),
Subcommand::Inscriptions => inscriptions::run(wallet),
Subcommand::Label => label::run(wallet),
Subcommand::Mint(mint) => mint.run(wallet),
Subcommand::Outputs => outputs::run(wallet),
Subcommand::Receive(receive) => receive.run(wallet),
Subcommand::Resume => resume::run(wallet),
Subcommand::Sats(sats) => sats.run(wallet),
Subcommand::Send(send) => send.run(wallet),
Subcommand::Transactions(transactions) => transactions.run(wallet),
Subcommand::Outputs => outputs::run(wallet),
Subcommand::Cardinals => cardinals::run(wallet),
Subcommand::Create(_) | Subcommand::Restore(_) => unreachable!(),
}
}
}
71 changes: 71 additions & 0 deletions src/subcommand/wallet/label.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use super::*;

#[derive(Serialize)]
struct Label {
first_sat: SatLabel,
inscriptions: BTreeMap<u64, BTreeSet<InscriptionId>>,
}

#[derive(Serialize)]
struct SatLabel {
name: String,
number: u64,
rarity: Rarity,
}

#[derive(Serialize)]
struct Line {
label: String,
r#ref: String,
r#type: String,
}

pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
let mut lines: Vec<Line> = Vec::new();

let sat_ranges = wallet.get_output_sat_ranges()?;

let mut inscriptions_by_output: BTreeMap<OutPoint, BTreeMap<u64, Vec<InscriptionId>>> =
BTreeMap::new();

for (satpoint, inscriptions) in wallet.inscriptions() {
inscriptions_by_output
.entry(satpoint.outpoint)
.or_default()
.insert(satpoint.offset, inscriptions.clone());
}

for (output, ranges) in sat_ranges {
let sat = Sat(ranges[0].0);
let mut inscriptions = BTreeMap::<u64, BTreeSet<InscriptionId>>::new();

if let Some(output_inscriptions) = inscriptions_by_output.get(&output) {
for (&offset, offset_inscriptions) in output_inscriptions {
inscriptions
.entry(offset)
.or_default()
.extend(offset_inscriptions);
}
}

lines.push(Line {
label: serde_json::to_string(&Label {
first_sat: SatLabel {
name: sat.name(),
number: sat.n(),
rarity: sat.rarity(),
},
inscriptions,
})?,
r#ref: output.to_string(),
r#type: "output".into(),
});
}

for line in lines {
serde_json::to_writer(io::stdout(), &line)?;
println!();
}

Ok(None)
}
Loading
Loading