Skip to content

Commit 6fe2f3c

Browse files
committed
feat(wip): uniffi wrapper for payment preimage
1 parent 45a1a73 commit 6fe2f3c

2 files changed

Lines changed: 132 additions & 15 deletions

File tree

bindings/ldk_node.udl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,11 @@ interface Bolt12Invoice {
803803
sequence<u8> encode();
804804
};
805805

806+
interface PaymentPreimage {
807+
[Throws=NodeError, Name=from_str]
808+
constructor([ByRef] string preimage_str);
809+
};
810+
806811
[Custom]
807812
typedef string Txid;
808813

@@ -830,9 +835,6 @@ typedef string PaymentId;
830835
[Custom]
831836
typedef string PaymentHash;
832837

833-
[Custom]
834-
typedef string PaymentPreimage;
835-
836838
[Custom]
837839
typedef string PaymentSecret;
838840

src/ffi/types.rs

Lines changed: 127 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ pub use crate::graph::{ChannelInfo, ChannelUpdateInfo, NodeAnnouncementInfo, Nod
1818
pub use crate::liquidity::{LSPS1OrderStatus, LSPS2ServiceConfig, OnchainPaymentInfo, PaymentInfo};
1919
pub use crate::logger::{LogLevel, LogRecord, LogWriter};
2020
pub use crate::payment::store::{
21-
ConfirmationStatus, LSPFeeLimits, PaymentDirection, PaymentKind, PaymentStatus,
21+
ConfirmationStatus, LSPFeeLimits, PaymentDirection, PaymentKind as NodePaymentKind,
22+
PaymentStatus,
2223
};
2324
pub use crate::payment::{MaxTotalRoutingFeeLimit, QrPaymentResult, SendingParameters};
2425

@@ -29,7 +30,9 @@ pub use lightning::offers::offer::OfferId;
2930
pub use lightning::routing::gossip::{NodeAlias, NodeId, RoutingFees};
3031
pub use lightning::util::string::UntrustedString;
3132

32-
pub use lightning_types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
33+
pub use lightning_types::payment::{
34+
PaymentHash, PaymentPreimage as LdkPaymentPreimage, PaymentSecret,
35+
};
3336

3437
pub use lightning_invoice::{Description, SignedRawBolt11Invoice};
3538

@@ -665,21 +668,133 @@ impl UniffiCustomTypeConverter for PaymentHash {
665668
}
666669
}
667670

668-
impl UniffiCustomTypeConverter for PaymentPreimage {
669-
type Builtin = String;
671+
/// The payment preimage is the "secret key" which is used to claim the funds of an HTLC on-chain
672+
/// or in a lightning channel.
673+
#[derive(Hash, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
674+
pub struct PaymentPreimage {
675+
pub(crate) inner: LdkPaymentPreimage,
676+
}
670677

671-
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
672-
if let Some(bytes_vec) = hex_utils::to_vec(&val) {
673-
let bytes_res = bytes_vec.try_into();
674-
if let Ok(bytes) = bytes_res {
675-
return Ok(PaymentPreimage(bytes));
678+
impl PaymentPreimage {
679+
pub fn from_str(preimage_str: &str) -> Result<Self, Error> {
680+
preimage_str.parse()
681+
}
682+
}
683+
684+
impl FromStr for PaymentPreimage {
685+
type Err = Error;
686+
687+
fn from_str(preimage_str: &str) -> Result<Self, Self::Err> {
688+
if let Some(bytes) = hex_utils::to_vec(preimage_str) {
689+
if let Ok(array) = bytes.try_into() {
690+
return Ok(Self::from(LdkPaymentPreimage(array)));
676691
}
677692
}
678-
Err(Error::InvalidPaymentPreimage.into())
693+
694+
Err(Error::InvalidPaymentPreimage)
679695
}
696+
}
680697

681-
fn from_custom(obj: Self) -> Self::Builtin {
682-
hex_utils::to_string(&obj.0)
698+
impl From<LdkPaymentPreimage> for PaymentPreimage {
699+
fn from(preimage: LdkPaymentPreimage) -> Self {
700+
PaymentPreimage { inner: preimage }
701+
}
702+
}
703+
704+
impl std::fmt::Display for PaymentPreimage {
705+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
706+
write!(f, "{}", self.inner)
707+
}
708+
}
709+
710+
/// Represents the kind of a payment.
711+
#[derive(Clone, Debug, PartialEq, Eq)]
712+
pub enum PaymentKind {
713+
Onchain {
714+
txid: Txid,
715+
status: ConfirmationStatus,
716+
},
717+
Bolt11 {
718+
hash: PaymentHash,
719+
preimage: Option<Arc<PaymentPreimage>>,
720+
secret: Option<PaymentSecret>,
721+
},
722+
Bolt11Jit {
723+
hash: PaymentHash,
724+
preimage: Option<Arc<PaymentPreimage>>,
725+
secret: Option<PaymentSecret>,
726+
counterparty_skimmed_fee_msat: Option<u64>,
727+
lsp_fee_limits: LSPFeeLimits,
728+
},
729+
Bolt12Offer {
730+
hash: Option<PaymentHash>,
731+
preimage: Option<Arc<PaymentPreimage>>,
732+
secret: Option<PaymentSecret>,
733+
offer_id: OfferId,
734+
payer_note: Option<UntrustedString>,
735+
quantity: Option<u64>,
736+
},
737+
Bolt12Refund {
738+
hash: Option<PaymentHash>,
739+
preimage: Option<Arc<PaymentPreimage>>,
740+
secret: Option<PaymentSecret>,
741+
payer_note: Option<UntrustedString>,
742+
quantity: Option<u64>,
743+
},
744+
Spontaneous {
745+
hash: PaymentHash,
746+
preimage: Option<Arc<PaymentPreimage>>,
747+
},
748+
}
749+
750+
impl From<NodePaymentKind> for PaymentKind {
751+
fn from(payment_kind: NodePaymentKind) -> Self {
752+
match payment_kind {
753+
NodePaymentKind::Onchain { txid, status } => PaymentKind::Onchain { txid, status },
754+
NodePaymentKind::Bolt11 { hash, preimage, secret } => {
755+
PaymentKind::Bolt11 { hash, preimage: preimage.map(|p| Arc::new(p)), secret }
756+
},
757+
NodePaymentKind::Bolt11Jit {
758+
hash,
759+
preimage,
760+
secret,
761+
counterparty_skimmed_fee_msat,
762+
lsp_fee_limits,
763+
} => PaymentKind::Bolt11Jit {
764+
hash,
765+
preimage: preimage.map(|p| Arc::new(p)),
766+
secret,
767+
counterparty_skimmed_fee_msat,
768+
lsp_fee_limits,
769+
},
770+
NodePaymentKind::Bolt12Offer {
771+
hash,
772+
preimage,
773+
secret,
774+
offer_id,
775+
payer_note,
776+
quantity,
777+
} => PaymentKind::Bolt12Offer {
778+
hash,
779+
preimage: preimage.map(|p| Arc::new(p)),
780+
secret,
781+
offer_id,
782+
payer_note,
783+
quantity,
784+
},
785+
NodePaymentKind::Bolt12Refund { hash, preimage, secret, payer_note, quantity } => {
786+
PaymentKind::Bolt12Refund {
787+
hash,
788+
preimage: preimage.map(|p| Arc::new(p)),
789+
secret,
790+
payer_note,
791+
quantity,
792+
}
793+
},
794+
NodePaymentKind::Spontaneous { hash, preimage } => {
795+
PaymentKind::Spontaneous { hash, preimage: preimage.map(|p| Arc::new(p)) }
796+
},
797+
}
683798
}
684799
}
685800

0 commit comments

Comments
 (0)