-
Notifications
You must be signed in to change notification settings - Fork 13
feat(add-bond-invoice): implement AddBondInvoice action #169
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a29e90f
feat(add-bond-invoice): implement AddBondInvoice action
grunch 5c04712
fix(add-bond-invoice): only treat wait timeout as a successful submis…
grunch 1a7c3e7
docs(add-bond-invoice): use short flags in the reply hint
grunch 0a3d077
fix(add-bond-invoice): pin newest info event and require a bolt11 reply
grunch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| use crate::parser::common::{ | ||
| create_emoji_field_row, create_field_value_header, create_standard_table, | ||
| }; | ||
| use crate::util::{print_dm_events, send_dm, wait_for_dm, WaitForDmTimeout}; | ||
| use crate::{cli::Context, db::Order, lightning::is_valid_invoice}; | ||
| use anyhow::Result; | ||
| use mostro_core::prelude::*; | ||
| use nostr_sdk::prelude::*; | ||
| use uuid::Uuid; | ||
|
|
||
| /// Reply to a Mostro `add-bond-invoice` request: the non-slashed counterparty | ||
| /// provides a bolt11 sized at their share of a slashed bond. | ||
| /// | ||
| /// This is the inbound `add-bond-invoice` request's dual — Mostro asks for a | ||
| /// bolt11 (carried as [`Payload::BondPayoutRequest`]) and we answer with the | ||
| /// invoice in the standard [`Payload::PaymentRequest`] shape, signed with the | ||
| /// order's trade key. See the protocol's "Bond payout invoice" action. | ||
| pub async fn execute_add_bond_invoice(order_id: &Uuid, invoice: &str, ctx: &Context) -> Result<()> { | ||
| // Get order from order id | ||
| let order = Order::get_by_id(&ctx.pool, &order_id.to_string()).await?; | ||
| // Get trade keys of specific order (the non-slashed counterparty side) | ||
| let trade_keys = order | ||
| .trade_keys | ||
| .clone() | ||
| .ok_or(anyhow::anyhow!("Missing trade keys"))?; | ||
|
|
||
| let order_trade_keys = Keys::parse(&trade_keys)?; | ||
|
|
||
| println!("🪙 Add Bond Payout Invoice"); | ||
| println!("═══════════════════════════════════════"); | ||
|
|
||
| let mut table = create_standard_table(); | ||
| table.set_header(create_field_value_header()); | ||
| table.add_row(create_emoji_field_row( | ||
| "📋 ", | ||
| "Order ID", | ||
| &order_id.to_string(), | ||
| )); | ||
| table.add_row(create_emoji_field_row( | ||
| "🔑 ", | ||
| "Trade Keys", | ||
| &order_trade_keys.public_key().to_hex(), | ||
| )); | ||
| table.add_row(create_emoji_field_row( | ||
| "🎯 ", | ||
| "Target", | ||
| &ctx.mostro_pubkey.to_string(), | ||
| )); | ||
| println!("{table}"); | ||
| println!("💡 Sending bond payout invoice to Mostro...\n"); | ||
| // The bond payout reply must be a bolt11 sized at the counterparty share. | ||
| // Lightning Addresses are not accepted here (the protocol's "Bond payout | ||
| // invoice" reply is a bolt11): validate locally so a bad input fails fast | ||
| // instead of bouncing back as a `cant-do` / `invalid-invoice` from Mostro. | ||
| let invoice = is_valid_invoice(invoice) | ||
| .map_err(|e| anyhow::anyhow!("Invalid invoice: {}", e))? | ||
| .to_string(); | ||
| let payload = Payload::PaymentRequest(None, invoice, None); | ||
|
|
||
| // Create request id | ||
| let request_id = Uuid::new_v4().as_u128() as u64; | ||
| // Create AddBondInvoice reply message | ||
| let add_bond_invoice_message = Message::new_order( | ||
| Some(*order_id), | ||
| Some(request_id), | ||
| None, | ||
| Action::AddBondInvoice, | ||
| Some(payload), | ||
| ); | ||
|
|
||
| // Serialize the message | ||
| let message_json = add_bond_invoice_message | ||
| .as_json() | ||
| .map_err(|_| anyhow::anyhow!("Failed to serialize message"))?; | ||
|
|
||
| // Send the DM | ||
| let sent_message = send_dm( | ||
| &ctx.client, | ||
| &ctx.identity_keys, | ||
| &order_trade_keys, | ||
| &ctx.mostro_pubkey, | ||
| message_json, | ||
| None, | ||
| false, | ||
| ); | ||
|
|
||
| // Wait for a possible reply. On success Mostro pays the invoice from its | ||
| // wallet without acknowledging over Nostr, so a *timeout* here is the happy | ||
| // path; Mostro only answers with `cant-do` on failure (late reply, wrong | ||
| // sender, bad invoice, etc.). Any other error (subscribe/sign/transport) | ||
| // means the reply may never have been sent — surface it instead of | ||
| // misreporting it as success. | ||
| match wait_for_dm(ctx, Some(&order_trade_keys), sent_message).await { | ||
| Ok(recv_event) => { | ||
| print_dm_events(recv_event, request_id, ctx, Some(&order_trade_keys)).await?; | ||
| } | ||
| Err(e) if e.downcast_ref::<WaitForDmTimeout>().is_some() => { | ||
| println!("✅ Bond payout invoice submitted to Mostro."); | ||
| println!("💡 Mostro will pay it from its wallet; no further confirmation is sent."); | ||
| println!("💡 Run `get-dm` to check for a `cant-do` response in case of an error."); | ||
| } | ||
| Err(e) => return Err(e), | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.