Skip to content

Trivial Bindings Updates #2229

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lightning-invoice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,15 @@ pub enum TaggedField {
pub struct Sha256(/// This is not exported to bindings users as the native hash types are not currently mapped
pub sha256::Hash);

impl Sha256 {
/// Constructs a new [`Sha256`] from the given bytes, which are assumed to be the output of a
/// single sha256 hash.
#[cfg(c_bindings)]
pub fn from_bytes(bytes: &[u8; 32]) -> Self {
Self(sha256::Hash::from_slice(bytes).expect("from_slice only fails if len is not 32"))
}
}

/// Description string
///
/// # Invariants
Expand Down
12 changes: 12 additions & 0 deletions lightning/src/offers/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ pub(super) const SIGNATURE_TAG: &'static str = concat!("lightning", "invoice", "
///
/// See [module-level documentation] for usage.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
/// [`Refund`]: crate::offers::refund::Refund
/// [module-level documentation]: self
Expand All @@ -145,12 +147,18 @@ pub struct InvoiceBuilder<'a, S: SigningPubkeyStrategy> {
}

/// Indicates how [`Invoice::signing_pubkey`] was set.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub trait SigningPubkeyStrategy {}

/// [`Invoice::signing_pubkey`] was explicitly set.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub struct ExplicitSigningPubkey {}

/// [`Invoice::signing_pubkey`] was derived.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub struct DerivedSigningPubkey {}

impl SigningPubkeyStrategy for ExplicitSigningPubkey {}
Expand Down Expand Up @@ -369,6 +377,8 @@ impl<'a> UnsignedInvoice<'a> {
}

/// Signs the invoice using the given function.
///
/// This is not exported to bindings users as functions aren't currently mapped.
Comment on lines +380 to +381
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be on the struct? It only can be created from InvoiceBuilder.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well once we fix the builders to work in bindings, we should also add a sign that just takes a private key?

pub fn sign<F, E>(self, sign: F) -> Result<Invoice, SignError<E>>
where
F: FnOnce(&Message) -> Result<Signature, E>
Expand Down Expand Up @@ -405,6 +415,8 @@ impl<'a> UnsignedInvoice<'a> {
/// An invoice may be sent in response to an [`InvoiceRequest`] in the case of an offer or sent
/// directly after scanning a refund. It includes all the information needed to pay a recipient.
///
/// This is not exported to bindings users as its name conflicts with the BOLT 11 Invoice type.
///
/// [`Offer`]: crate::offers::offer::Offer
/// [`Refund`]: crate::offers::refund::Refund
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
Expand Down
18 changes: 18 additions & 0 deletions lightning/src/offers/invoice_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ pub(super) const IV_BYTES: &[u8; IV_LEN] = b"LDK Invreq ~~~~~";
///
/// See [module-level documentation] for usage.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [module-level documentation]: self
pub struct InvoiceRequestBuilder<'a, 'b, P: PayerIdStrategy, T: secp256k1::Signing> {
offer: &'a Offer,
Expand All @@ -94,12 +96,18 @@ pub struct InvoiceRequestBuilder<'a, 'b, P: PayerIdStrategy, T: secp256k1::Signi
}

/// Indicates how [`InvoiceRequest::payer_id`] will be set.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub trait PayerIdStrategy {}

/// [`InvoiceRequest::payer_id`] will be explicitly set.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub struct ExplicitPayerId {}

/// [`InvoiceRequest::payer_id`] will be derived.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub struct DerivedPayerId {}

impl PayerIdStrategy for ExplicitPayerId {}
Expand Down Expand Up @@ -340,6 +348,8 @@ pub struct UnsignedInvoiceRequest<'a> {

impl<'a> UnsignedInvoiceRequest<'a> {
/// Signs the invoice request using the given function.
///
/// This is not exported to bindings users as functions are not yet mapped.
Comment on lines +351 to +352
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise.

pub fn sign<F, E>(self, sign: F) -> Result<InvoiceRequest, SignError<E>>
where
F: FnOnce(&Message) -> Result<Signature, E>
Expand Down Expand Up @@ -465,6 +475,8 @@ impl InvoiceRequest {
/// See [`InvoiceRequest::respond_with_no_std`] for further details where the aforementioned
/// creation time is used for the `created_at` parameter.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [`Duration`]: core::time::Duration
#[cfg(feature = "std")]
pub fn respond_with(
Expand Down Expand Up @@ -493,6 +505,8 @@ impl InvoiceRequest {
///
/// Errors if the request contains unknown required features.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
pub fn respond_with_no_std(
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
Expand All @@ -511,6 +525,8 @@ impl InvoiceRequest {
///
/// See [`InvoiceRequest::respond_with`] for further details.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [`Invoice`]: crate::offers::invoice::Invoice
#[cfg(feature = "std")]
pub fn verify_and_respond_using_derived_keys<T: secp256k1::Signing>(
Expand All @@ -532,6 +548,8 @@ impl InvoiceRequest {
///
/// See [`InvoiceRequest::respond_with_no_std`] for further details.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [`Invoice`]: crate::offers::invoice::Invoice
pub fn verify_and_respond_using_derived_keys_no_std<T: secp256k1::Signing>(
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
Expand Down
14 changes: 14 additions & 0 deletions lightning/src/offers/offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ pub(super) const IV_BYTES: &[u8; IV_LEN] = b"LDK Offer ~~~~~~";
///
/// See [module-level documentation] for usage.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [module-level documentation]: self
pub struct OfferBuilder<'a, M: MetadataStrategy, T: secp256k1::Signing> {
offer: OfferContents,
Expand All @@ -106,12 +108,18 @@ pub struct OfferBuilder<'a, M: MetadataStrategy, T: secp256k1::Signing> {
}

/// Indicates how [`Offer::metadata`] may be set.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub trait MetadataStrategy {}

/// [`Offer::metadata`] may be explicitly set or left empty.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub struct ExplicitMetadata {}

/// [`Offer::metadata`] will be derived.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub struct DerivedMetadata {}

impl MetadataStrategy for ExplicitMetadata {}
Expand Down Expand Up @@ -448,6 +456,8 @@ impl Offer {
///
/// Useful to protect the sender's privacy.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [`InvoiceRequest::payer_id`]: crate::offers::invoice_request::InvoiceRequest::payer_id
/// [`InvoiceRequest::metadata`]: crate::offers::invoice_request::InvoiceRequest::metadata
/// [`Invoice::verify`]: crate::offers::invoice::Invoice::verify
Expand All @@ -470,6 +480,8 @@ impl Offer {
///
/// Useful for recurring payments using the same `payer_id` with different invoices.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [`InvoiceRequest::payer_id`]: crate::offers::invoice_request::InvoiceRequest::payer_id
pub fn request_invoice_deriving_metadata<ES: Deref>(
&self, payer_id: PublicKey, expanded_key: &ExpandedKey, entropy_source: ES
Expand All @@ -496,6 +508,8 @@ impl Offer {
///
/// Errors if the offer contains unknown required features.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
pub fn request_invoice(
&self, metadata: Vec<u8>, payer_id: PublicKey
Expand Down
4 changes: 4 additions & 0 deletions lightning/src/offers/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ impl<T: SeekReadable> TryFrom<Vec<u8>> for ParsedMessage<T> {
}

/// Error when parsing a bech32 encoded message using [`str::parse`].
///
/// This is not exported to bindings users as its name conflicts with the BOLT 11 ParseError type.
Comment on lines +119 to +120
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not exporting this affect using FromStr and TryFrom implementations for Offer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, those are not exported as a result.

#[derive(Debug, PartialEq)]
pub enum ParseError {
/// The bech32 encoding does not conform to the BOLT 12 requirements for continuing messages
Expand All @@ -135,6 +137,8 @@ pub enum ParseError {
}

/// Error when interpreting a TLV stream as a specific type.
///
/// This is not exported to bindings users as its name conflicts with the BOLT 11 SemanticError type.
#[derive(Debug, PartialEq)]
pub enum SemanticError {
/// The current [`std::time::SystemTime`] is past the offer or invoice's expiration.
Expand Down
10 changes: 10 additions & 0 deletions lightning/src/offers/refund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ pub(super) const IV_BYTES: &[u8; IV_LEN] = b"LDK Refund ~~~~~";
///
/// See [module-level documentation] for usage.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [module-level documentation]: self
pub struct RefundBuilder<'a, T: secp256k1::Signing> {
refund: RefundContents,
Expand Down Expand Up @@ -387,6 +389,8 @@ impl Refund {
/// See [`Refund::respond_with_no_std`] for further details where the aforementioned creation
/// time is used for the `created_at` parameter.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [`Duration`]: core::time::Duration
#[cfg(feature = "std")]
pub fn respond_with(
Expand Down Expand Up @@ -419,6 +423,8 @@ impl Refund {
///
/// Errors if the request contains unknown required features.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
pub fn respond_with_no_std(
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
Expand All @@ -436,6 +442,8 @@ impl Refund {
///
/// See [`Refund::respond_with`] for further details.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [`Invoice`]: crate::offers::invoice::Invoice
#[cfg(feature = "std")]
pub fn respond_using_derived_keys<ES: Deref>(
Expand All @@ -459,6 +467,8 @@ impl Refund {
///
/// See [`Refund::respond_with_no_std`] for further details.
///
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
///
/// [`Invoice`]: crate::offers::invoice::Invoice
pub fn respond_using_derived_keys_no_std<ES: Deref>(
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
Expand Down