-
Notifications
You must be signed in to change notification settings - Fork 110
Support passing preimage for spontaneous payments #549
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,6 +212,10 @@ interface SpontaneousPayment { | |
[Throws=NodeError] | ||
PaymentId send_with_custom_tlvs(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters, sequence<CustomTlvRecord> custom_tlvs); | ||
[Throws=NodeError] | ||
PaymentId send_with_preimage(u64 amount_msat, PublicKey node_id, PaymentPreimage preimage, SendingParameters? sending_parameters); | ||
[Throws=NodeError] | ||
PaymentId send_with_preimage_and_custom_tlvs(u64 amount_msat, PublicKey node_id, sequence<CustomTlvRecord> custom_tlvs, PaymentPreimage preimage, SendingParameters? sending_parameters); | ||
[Throws=NodeError] | ||
void send_probes(u64 amount_msat, PublicKey node_id); | ||
}; | ||
|
||
|
@@ -446,6 +450,10 @@ dictionary PaymentDetails { | |
u64 latest_update_timestamp; | ||
}; | ||
|
||
dictionary PaymentPreimage { | ||
bytes inner; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder about what would be the most appropriate description for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}; | ||
|
||
dictionary SendingParameters { | ||
MaxTotalRoutingFeeLimit? max_total_routing_fee_msat; | ||
u32? max_total_cltv_expiry_delta; | ||
|
@@ -829,9 +837,6 @@ typedef string PaymentId; | |
[Custom] | ||
typedef string PaymentHash; | ||
|
||
[Custom] | ||
typedef string PaymentPreimage; | ||
|
||
[Custom] | ||
typedef string PaymentSecret; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,14 @@ where | |
wrapped_type.as_ref().as_ref() | ||
} | ||
|
||
#[cfg(feature = "uniffi")] | ||
pub fn maybe_extract<T, R>(wrapped_type: T) -> Result<R, crate::error::Error> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this, as we could just reuse In general, I'd prefer it if we wouldn't add any more wrapper helpers in this PR, I think (?) we should be able to make it work with what's there, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I will make use of the wrapper helper we currently have. |
||
where | ||
R: TryFrom<T, Error = crate::error::Error>, | ||
{ | ||
R::try_from(wrapped_type) | ||
} | ||
|
||
#[cfg(feature = "uniffi")] | ||
pub fn maybe_try_convert_enum<T, R>(wrapped_type: &T) -> Result<R, crate::error::Error> | ||
where | ||
|
@@ -27,10 +35,15 @@ where | |
} | ||
|
||
#[cfg(feature = "uniffi")] | ||
pub fn maybe_wrap<T>(ldk_type: impl Into<T>) -> std::sync::Arc<T> { | ||
pub fn maybe_wrap_arc<T>(ldk_type: impl Into<T>) -> std::sync::Arc<T> { | ||
std::sync::Arc::new(ldk_type.into()) | ||
} | ||
|
||
#[cfg(feature = "uniffi")] | ||
pub fn maybe_wrap<T>(ldk_type: impl Into<T>) -> T { | ||
ldk_type.into() | ||
} | ||
|
||
Comment on lines
+38
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm probably missing something here but I'm wondering why we need to "split" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my understanding, I thought I noticed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In #542, the wrapper types introduced were for structs with a bit more complexity and state than Also, when compiled with This change is easily revertible though if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification! This makes sense. |
||
#[cfg(not(feature = "uniffi"))] | ||
pub fn maybe_deref<T>(value: &T) -> &T { | ||
value | ||
|
@@ -41,7 +54,19 @@ pub fn maybe_try_convert_enum<T>(value: &T) -> Result<&T, crate::error::Error> { | |
Ok(value) | ||
} | ||
|
||
#[cfg(not(feature = "uniffi"))] | ||
pub fn maybe_wrap_arc<T>(value: T) -> T { | ||
value | ||
} | ||
|
||
#[cfg(not(feature = "uniffi"))] | ||
pub fn maybe_wrap<T>(value: T) -> T { | ||
value | ||
} | ||
Comment on lines
+57
to
65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These 2 fns are basically the same, please is there a reason why we need both of them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. both |
||
|
||
#[cfg(not(feature = "uniffi"))] | ||
pub fn maybe_extract<T>(wrapped_type: T) -> Result<T, crate::error::Error> | ||
where | ||
{ | ||
Ok(wrapped_type) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,14 +22,18 @@ pub use crate::payment::store::{ | |
}; | ||
pub use crate::payment::{MaxTotalRoutingFeeLimit, QrPaymentResult, SendingParameters}; | ||
|
||
use bitcoin::io::Read; | ||
pub use lightning::chain::channelmonitor::BalanceSource; | ||
pub use lightning::events::{ClosureReason, PaymentFailureReason}; | ||
use lightning::ln::msgs::DecodeError; | ||
pub use lightning::ln::types::ChannelId; | ||
pub use lightning::offers::offer::OfferId; | ||
pub use lightning::routing::gossip::{NodeAlias, NodeId, RoutingFees}; | ||
pub use lightning::util::string::UntrustedString; | ||
|
||
pub use lightning_types::payment::{PaymentHash, PaymentPreimage, PaymentSecret}; | ||
pub use lightning_types::payment::{ | ||
PaymentHash, PaymentPreimage as LdkPaymentPreimage, PaymentSecret, | ||
}; | ||
|
||
pub use lightning_invoice::{Description, SignedRawBolt11Invoice}; | ||
|
||
|
@@ -58,7 +62,7 @@ use lightning::ln::channelmanager::PaymentId; | |
use lightning::offers::invoice::Bolt12Invoice as LdkBolt12Invoice; | ||
use lightning::offers::offer::{Amount as LdkAmount, Offer as LdkOffer}; | ||
use lightning::offers::refund::Refund as LdkRefund; | ||
use lightning::util::ser::Writeable; | ||
use lightning::util::ser::{Readable, Writeable}; | ||
use lightning_invoice::{Bolt11Invoice as LdkBolt11Invoice, Bolt11InvoiceDescriptionRef}; | ||
|
||
use std::convert::TryInto; | ||
|
@@ -665,21 +669,64 @@ impl UniffiCustomTypeConverter for PaymentHash { | |
} | ||
} | ||
|
||
impl UniffiCustomTypeConverter for PaymentPreimage { | ||
type Builtin = String; | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct PaymentPreimage { | ||
pub(crate) inner: Vec<u8>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency reasons, please just store an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright. Thank you |
||
} | ||
|
||
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> { | ||
if let Some(bytes_vec) = hex_utils::to_vec(&val) { | ||
let bytes_res = bytes_vec.try_into(); | ||
if let Ok(bytes) = bytes_res { | ||
return Ok(PaymentPreimage(bytes)); | ||
impl From<LdkPaymentPreimage> for PaymentPreimage { | ||
fn from(ldk_value: LdkPaymentPreimage) -> Self { | ||
PaymentPreimage { inner: ldk_value.0.to_vec() } | ||
} | ||
} | ||
|
||
impl TryFrom<PaymentPreimage> for LdkPaymentPreimage { | ||
type Error = Error; | ||
|
||
fn try_from(preimage: PaymentPreimage) -> Result<Self, Self::Error> { | ||
if preimage.inner.len() != 32 { | ||
return Err(Error::InvalidPaymentPreimage); | ||
} | ||
|
||
let mut array = [0u8; 32]; | ||
array.copy_from_slice(&preimage.inner); | ||
Ok(LdkPaymentPreimage(array)) | ||
} | ||
} | ||
|
||
impl FromStr for PaymentPreimage { | ||
type Err = Error; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
if let Some(bytes) = hex_utils::to_vec(s) { | ||
if let Ok(array) = bytes.try_into() { | ||
return Ok(Self::from(LdkPaymentPreimage(array))); | ||
} | ||
} | ||
Err(Error::InvalidPaymentPreimage.into()) | ||
|
||
Err(Error::InvalidPaymentPreimage) | ||
} | ||
} | ||
|
||
fn from_custom(obj: Self) -> Self::Builtin { | ||
hex_utils::to_string(&obj.0) | ||
impl Writeable for PaymentPreimage { | ||
fn write<W: lightning::util::ser::Writer>( | ||
&self, writer: &mut W, | ||
) -> Result<(), lightning::io::Error> { | ||
let ldk_preimage = LdkPaymentPreimage::try_from(self.clone()).map_err(|_| { | ||
lightning::io::Error::new( | ||
lightning::io::ErrorKind::InvalidData, | ||
"Invalid payment preimage", | ||
) | ||
})?; | ||
|
||
ldk_preimage.0.write(writer) | ||
} | ||
} | ||
|
||
impl Readable for PaymentPreimage { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we really shouldn't roll our own custom serialization There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright. Noted |
||
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> { | ||
let buf: [u8; 32] = Readable::read(r)?; | ||
Ok(PaymentPreimage { inner: buf.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.
nit: Let's move these new entries to the correct location w.r.t. API, i.e. below
send_with_custom_tlvs
.