-
Notifications
You must be signed in to change notification settings - Fork 37
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 NWC commands #1152
add NWC commands #1152
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overall looking good
mutiny-core/src/nostr/nwc.rs
Outdated
nostr_manager: &NostrManager<S, P, C>, | ||
) -> anyhow::Result<Option<NwcResponse>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for all of these you're passing in the NostrManager
which forces you to put all the type params and stuff but you only end up calling client.send_event
, would be better to just pass in the nostr client
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh yeah, thanks
mutiny-core/src/nostr/nwc.rs
Outdated
let incoming = match params.transaction_type.unwrap() { | ||
TransactionType::Incoming => true, | ||
TransactionType::Outgoing => false, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be simplified to let incoming = params.transaction_type.unwrap() == TransactionType::Incoming
mutiny-core/src/nostr/nwc.rs
Outdated
}; | ||
let preimage = PaymentPreimage(Sha256::hash(input.as_bytes()).into_32()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to incorporate some sort of secret here so its not derivable from public data, should also hash in the server private key for this nwc.
mutiny-core/src/nostr/nwc.rs
Outdated
code: ErrorCode::PaymentFailed, | ||
message: String::from("not enough balance to make payment"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be better to do an UNAUTHORIZED
here with a better error string about the budget
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what error message do you think would be good here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Keysend only supported for profiles with a budget"
mutiny-core/src/nostr/nwc.rs
Outdated
.unwrap_or(self.profile.name.clone()); | ||
// attempt keysend payment now | ||
match node | ||
.keysend(to_node_pubkey, sats, None, vec![label], Some(preimage.0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're never doing anything with the TLV records will need to add that to the keysend function too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TonyGiorgio passing that optional preimage here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benthecarman did some changes to include the TLV records. I changed the previous message param and passing new Vec<CustomTLV>
. This would require changes in the frontend tho. I saw the frontend was not passing a message anyways so it would be changing from undefined to an empty array. Let me know what you think
@@ -1617,6 +1617,7 @@ impl<S: MutinyStorage> Node<S> { | |||
message: Option<String>, | |||
labels: Vec<String>, | |||
payment_id: PaymentId, | |||
preimage: Option<[u8; 32]>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is added but it's never used. I don't know why it would be useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is used from the NWC stuff, allows for specifying a preimage for the keysend.
We need it for ourself as well so we don't generate a completely random preimage for the event, need to do it deterministically so if we process the event multiple times we won't send the payment multiple times
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm generating the preimage deterministically from the nwc request event in the handle_nwc_keysend_payment
method and passing it as an option. Got from @benthecarman that this would be needed to avoid making another keysend payment for the same event in case we try to process that same request event more than once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoops, saw your comment after posted mine lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked and didn't see it. Where is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setting that optional preimage here https://github.com/elnosh/mutiny-node/blob/nwc-commands/mutiny-core/src/nostr/nwc.rs#L1258-L1269
and passing it here https://github.com/elnosh/mutiny-node/blob/nwc-commands/mutiny-core/src/nostr/nwc.rs#L1332-L1334
thanks for the feedback. Added some changes based on it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
few nits, and hoping you can improve the tests a bit
mutiny-core/src/node.rs
Outdated
.into_iter() | ||
.map(|tlv| (tlv.tlv_type, tlv.value.encode())) | ||
.collect(); | ||
|
||
RecipientOnionFields::secret_only(payment_secret) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to change this to spontaneous_empty
as well, probably will get the same error as before
mutiny-core/src/node.rs
Outdated
RecipientOnionFields::secret_only(payment_secret) | ||
.with_custom_tlvs(vec![(34349334, msg.encode())]) | ||
.with_custom_tlvs(custom_tlvs) | ||
.map_err(|_| { | ||
log_error!(self.logger, "could not encode keysend message"); | ||
MutinyError::InvoiceCreationFailed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make this InvalidArgumentsError
, that'd be more fitting here
mutiny-core/src/nostr/nwc.rs
Outdated
SpendingConditions::Budget(mut budget) => { | ||
// generate deterministic preimage for keysend payment from event info | ||
let preimage = match params.preimage { | ||
Some(preimage) => PaymentPreimage(Sha256::hash(preimage.as_bytes()).into_32()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't hash the preimage here, we should be using what they gave us
mutiny-core/src/nostr/nwc.rs
Outdated
if params.limit.is_some() { | ||
let limit = params.limit.unwrap() as usize; | ||
if limit - 1 < transactions.len() { | ||
transactions = transactions[..limit].to_vec(); | ||
} | ||
} | ||
if params.offset.is_some() { | ||
let offset = params.offset.unwrap() as usize; | ||
if offset - 1 < transactions.len() { | ||
transactions = transactions[offset..].to_vec(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have similar logic here: https://github.com/MutinyWallet/mutiny-node/blob/master/mutiny-core/src/lib.rs#L1805
i would just copy this logic, should probably have tests around this as well
mutiny-core/src/nostr/nwc.rs
Outdated
node.expect_keysend() | ||
.once() | ||
.returning(move |_, _, _, _, preimage| { | ||
let payment_preimage = PaymentPreimage(preimage.unwrap()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should assert the preimage, pubkey, and amount are what we expect
a382b4b
to
f855a0a
Compare
I don't think we should support any of these commands. Sorry for the wasted effort but a strong NACK from me. |
no worries! got to learn about NWC and how that part of Mutiny works, so not a waste at all |
for #1120 added
multi_pay_invoice
,pay_keysend
,multi_pay_keysend
andlist_transactions
NWC commands. I added those requests to the mutinynet faucet to help me test these from the UI.Reasoning for certain changes I did:
multi_pay_invoice
andmulti_pay_keysend
) can return multiple events. So instead of waiting to return them untilstart_nostr
method, now broadcasting in the respective handle methods where the response events are generated.add_payment
andremove_payment
because they were previously taking a Bolt11Invoice so I changed it toTrackedPayment
to now be able to accommodate adding keysend payments as well instead of only invoices.InvoiceHandler
trait that I needed for the new commands.