Feat: Add bip329 wallet labels#266
Conversation
- Loads the \'Labels\' state from \'<wallet>.labels.jsonl\' after \'load_wallet_config\'. - Updates \'handle_offline_wallet_subcommand\' to accept mutable label state. - Exports and saves the in-memory labels to disk before the command return.
- Injects label lookups into the `unspent` and `transactions` match arms. - Updates the `--pretty` table formatting to include a new 'Label' column (using an em dash for missing labels). - Appends the label string to the standard JSON output
- Introduces the `Label` variant to `OfflineWalletSubCommand` with strict `clap` conflict rules. - Adds a `parse_txid` helper to utils.rs. - Implements an `add_or_add_update_label` helper in `handlers.rs` to mutate the in-memory label state in place.
|
Hi @tvpeter, I am done with this. You can check it out when you are free. I will start working on the follow-up PR to add import and export of label files. |
- Introduces `ImportLabels` and `ExportLabels` variants to `OfflineWalletSubCommand` for CLI parsing. - Implements label merging from external JSONL files using `try_from_file` and `add_or_update_label`. - Enables backing up the current label state to a user-specified path using `export_to_file`.
|
Hi @tvpeter, I eventually decided to add the import and export of BIP-329 JSONL files' implementation here when, after I was done with the implementation, I saw that it was something that could fit into this PR without bloating it. So, instead of creating a second PR and having reviewers go through the stress of reviewing two PRs that can probably fit into one, I decided to make it one PR. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #266 +/- ##
==========================================
- Coverage 11.13% 10.59% -0.55%
==========================================
Files 8 8
Lines 2488 2615 +127
==========================================
Hits 277 277
- Misses 2211 2338 +127
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
This looks like a very useful feature and one that I'd love to see in the It will be a little more complicated than |
|
Thanks, @notmandatory, for the review and the suggestion! I really like the idea of creating a new Regarding the persistence trait to tie into the user's choice of database, I was thinking we could build an interface that lets users store labels internally in their database of choice (like SQLite or Redb). To keep it efficient, we could mirror bdk_wallet's approach by abstracting the backend and using a changeset pattern to persist only the diffs. To maintain strict BIP-329 compliance, the crate would then provide dedicated import and export methods to convert the internal database state to and from the standard JSONL format. Does this approach align with what you have in mind? |
|
Yes this sounds like a good approach. |
|
Hey @Musab1258, glad to see you're working on this. I've been playing around with bdk-cli and had some thoughts around this My thinking is to create a
Just brainstorming somethings that I think would be useful. Would love to hear know your thoughts |
|
@notmandatory, since I don't have permissions to create a repository in the bitcoindevkit organization, what should the next steps be? Would you set up an empty repository within the organization (similar to how bdk-bip322 is set up) for me to work from?
|
|
@brh28, these are really nice ideas for expanding the labeling functionality. Actually, some of the features you mentioned (like importing a BIP-329 JSONL file and adding labels to existing wallet items) are already implemented in my current PR here. Since @notmandatory suggested implementing it in a new bdk_bip329 crate, we can implement the existing add, import, and export functionalities first. Then we can add the multi-tagging and filter logic later. Also, the examples you made (like cat mywallet.jsonl | bdk_labels import --bip329 and the rest) show that you wanted the tool to be a CLI. But, I think what @notmandatory wants is a crate that can be integrated into other crates and CLI tools @notmandatory may also have something to say about this.
|
|
Thanks @Musab1258 I created my own repo bdk-labels to start implementing some of these features. As you pointed out, many of the features overlap with what you're working on, so it's quite similar to your PR, but perhaps it'll be of interest to you. The biggest feature difference is
|
| LabelRef::Address(addr) => format!("addr:{}", addr.assume_checked_ref()), | ||
| LabelRef::Output(op) => format!("output:{op}"), | ||
| LabelRef::Input(op) => format!("input:{op}"), | ||
| _ => item_ref.to_string(), // Fallback for pubkey/xpub |
There was a problem hiding this comment.
why fallback, rather than handle other types?
Go ahead and create a new repo in your personal account and we can move it into the bitcoindevkit org when it's ready. I also prefer @brh28 's repo name |
@Musab1258 you're correct that I do have a general purpose library in mind for this feature, not something only for use in |
| })?; | ||
|
|
||
| for label in imported_labels.into_iter() { | ||
| add_or_update_label(labels, label); |
There was a problem hiding this comment.
To avoid incidental data loss, I would suggest adding an --override flag on import. If the flag is not provided, either the command fails, or only new labels are added
Description
This PR adds BIP-329 wallet label support to
bdk-cli, to fix #184. It uses the[bip329]crate to manage label data, persists it in a separatelabels.jsonlfile within the wallet's data directory (e.g.~/.bdk-bitcoin/<wallet_name>/labels.jsonl), and keeps it decoupled from the wallet's SQLite/redb database. It also exports and imports label files.This PR includes the following functionality:
labeloffline subcommand to attach a human-readable label to either a transaction ID, address, or UTXO (outpoint).unspentandtransactionscommands now include aLabelcolumn in both--prettytable output and standard JSON output, showing the stored label or an em dash if none exists.import_labelssubcommand merges labels from an external BIP-329 JSONL file into the wallet's current label state. If a label for the same item already exists, it is overwritten by the imported one.export_labelssubcommand writes the wallet's current label state to a user-specified BIP-329 JSONL file, suitable for backup or use in other BIP-329 compliant wallets.Notes to the reviewers
labels.jsonlfile rather than inside the wallet database. This keeps the label format portable and interoperable with other BIP-329 compliant wallets.Labelimport frombip329is aliased asBipLabelinhandlers.rsto avoid a name clash with theOfflineWalletSubCommand::Labelvariant brought into scope viause crate::commands::OfflineWalletSubCommand::*.import_labelscommand uses the existingadd_or_update_labelhelper to merge incoming labels one at a time, new label references are inserted, and existing label references are overwritten by the imported value.export_labelscommand writes to a user-specified path, which is independent of the wallet's internallabels.jsonlfile. The internal file is always managed automatically by the save-on-exit mechanism, so the two files remain separate.Changelog notice
Added
labelsubcommand toOfflineWalletSubCommandfor tagging transactions, addresses, and UTXOs with human-readable labels.import_labelssubcommand to merge labels from an external BIP-329 JSONL file into the wallet's current label state.export_labelssubcommand to back up the wallet's current label state to a user-specified BIP-329 JSONL file.labels.jsonlfile in the wallet data directory.Labelcolumn inunspentandtransactionsoutput (both--prettyand JSON).parse_txidhelper inutils.rs.bip329 = "0.4.0"dependency inCargo.toml.Checklists
All Submissions:
cargo fmtandcargo clippybefore committingNew Features:
CHANGELOG.mdBugfixes: