Skip to content

Commit cb0b4bf

Browse files
authored
Merge pull request #903 from TheBlueMatt/2021-04-invoice-bindings
Prepare lightning-invoice for export in C
2 parents 0725098 + c9afea2 commit cb0b4bf

File tree

5 files changed

+46
-23
lines changed

5 files changed

+46
-23
lines changed

lightning-invoice/src/de.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl FromStr for SignedRawInvoice {
264264
hrp.as_bytes(),
265265
&data[..data.len()-104]
266266
),
267-
signature: Signature::from_base32(&data[data.len()-104..])?,
267+
signature: InvoiceSignature::from_base32(&data[data.len()-104..])?,
268268
})
269269
}
270270
}
@@ -338,17 +338,17 @@ impl FromBase32 for PositiveTimestamp {
338338
}
339339
}
340340

341-
impl FromBase32 for Signature {
341+
impl FromBase32 for InvoiceSignature {
342342
type Err = ParseError;
343343
fn from_base32(signature: &[u5]) -> Result<Self, Self::Err> {
344344
if signature.len() != 104 {
345-
return Err(ParseError::InvalidSliceLength("Signature::from_base32()".into()));
345+
return Err(ParseError::InvalidSliceLength("InvoiceSignature::from_base32()".into()));
346346
}
347347
let recoverable_signature_bytes = Vec::<u8>::from_base32(signature)?;
348348
let signature = &recoverable_signature_bytes[0..64];
349349
let recovery_id = RecoveryId::from_i32(recoverable_signature_bytes[64] as i32)?;
350350

351-
Ok(Signature(RecoverableSignature::from_compact(
351+
Ok(InvoiceSignature(RecoverableSignature::from_compact(
352352
signature,
353353
recovery_id
354354
)?))
@@ -999,7 +999,7 @@ mod test {
999999
use lightning::ln::features::InvoiceFeatures;
10001000
use secp256k1::recovery::{RecoveryId, RecoverableSignature};
10011001
use TaggedField::*;
1002-
use {SiPrefix, SignedRawInvoice, Signature, RawInvoice, RawHrp, RawDataPart,
1002+
use {SiPrefix, SignedRawInvoice, InvoiceSignature, RawInvoice, RawHrp, RawDataPart,
10031003
Currency, Sha256, PositiveTimestamp};
10041004

10051005
// Feature bits 9, 15, and 99 are set.
@@ -1025,7 +1025,7 @@ mod test {
10251025
hash: [0xb1, 0x96, 0x46, 0xc3, 0xbc, 0x56, 0x76, 0x1d, 0x20, 0x65, 0x6e, 0x0e, 0x32,
10261026
0xec, 0xd2, 0x69, 0x27, 0xb7, 0x62, 0x6e, 0x2a, 0x8b, 0xe6, 0x97, 0x71, 0x9f,
10271027
0xf8, 0x7e, 0x44, 0x54, 0x55, 0xb9],
1028-
signature: Signature(RecoverableSignature::from_compact(
1028+
signature: InvoiceSignature(RecoverableSignature::from_compact(
10291029
&[0xd7, 0x90, 0x4c, 0xc4, 0xb7, 0x4a, 0x22, 0x26, 0x9c, 0x68, 0xc1, 0xdf, 0x68,
10301030
0xa9, 0x6c, 0x21, 0x4d, 0x65, 0x1b, 0x93, 0x76, 0xe9, 0xf1, 0x64, 0xd3, 0x60,
10311031
0x4d, 0xa4, 0xb7, 0xde, 0xcc, 0xce, 0x0e, 0x82, 0xaa, 0xab, 0x4c, 0x85, 0xd3,
@@ -1045,7 +1045,7 @@ mod test {
10451045
fn test_raw_signed_invoice_deserialization() {
10461046
use TaggedField::*;
10471047
use secp256k1::recovery::{RecoveryId, RecoverableSignature};
1048-
use {SignedRawInvoice, Signature, RawInvoice, RawHrp, RawDataPart, Currency, Sha256,
1048+
use {SignedRawInvoice, InvoiceSignature, RawInvoice, RawHrp, RawDataPart, Currency, Sha256,
10491049
PositiveTimestamp};
10501050

10511051
assert_eq!(
@@ -1078,7 +1078,7 @@ mod test {
10781078
0x7b, 0x1d, 0x85, 0x8d, 0xb1, 0xd1, 0xf7, 0xab, 0x71, 0x37, 0xdc, 0xb7,
10791079
0x83, 0x5d, 0xb2, 0xec, 0xd5, 0x18, 0xe1, 0xc9
10801080
],
1081-
signature: Signature(RecoverableSignature::from_compact(
1081+
signature: InvoiceSignature(RecoverableSignature::from_compact(
10821082
& [
10831083
0x38u8, 0xec, 0x68, 0x91, 0x34, 0x5e, 0x20, 0x41, 0x45, 0xbe, 0x8a,
10841084
0x3a, 0x99, 0xde, 0x38, 0xe9, 0x8a, 0x39, 0xd6, 0xa5, 0x69, 0x43,

lightning-invoice/src/lib.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ pub fn check_platform() {
151151
/// * `D`: exactly one `Description` or `DescriptionHash`
152152
/// * `H`: exactly one `PaymentHash`
153153
/// * `T`: the timestamp is set
154+
///
155+
/// (C-not exported) as we likely need to manually select one set of boolean type parameters.
154156
#[derive(Eq, PartialEq, Debug, Clone)]
155157
pub struct InvoiceBuilder<D: tb::Bool, H: tb::Bool, T: tb::Bool> {
156158
currency: Currency,
@@ -178,6 +180,9 @@ pub struct Invoice {
178180

179181
/// Represents the description of an invoice which has to be either a directly included string or
180182
/// a hash of a description provided out of band.
183+
///
184+
/// (C-not exported) As we don't have a good way to map the reference lifetimes making this
185+
/// practically impossible to use safely in languages like C.
181186
#[derive(Eq, PartialEq, Debug, Clone)]
182187
pub enum InvoiceDescription<'f> {
183188
/// Reference to the directly supplied description in the invoice
@@ -207,7 +212,7 @@ pub struct SignedRawInvoice {
207212
hash: [u8; 32],
208213

209214
/// signature of the payment request
210-
signature: Signature,
215+
signature: InvoiceSignature,
211216
}
212217

213218
/// Represents an syntactically correct Invoice for a payment on the lightning network,
@@ -225,6 +230,8 @@ pub struct RawInvoice {
225230
}
226231

227232
/// Data of the `RawInvoice` that is encoded in the human readable part
233+
///
234+
/// (C-not exported) As we don't yet support Option<Enum>
228235
#[derive(Eq, PartialEq, Debug, Clone)]
229236
pub struct RawHrp {
230237
/// The currency deferred from the 3rd and 4th character of the bech32 transaction
@@ -283,6 +290,9 @@ impl SiPrefix {
283290

284291
/// Returns all enum variants of `SiPrefix` sorted in descending order of their associated
285292
/// multiplier.
293+
///
294+
/// (C-not exported) As we don't yet support a slice of enums, and also because this function
295+
/// isn't the most critical to expose.
286296
pub fn values_desc() -> &'static [SiPrefix] {
287297
use SiPrefix::*;
288298
static VALUES: [SiPrefix; 4] = [Milli, Micro, Nano, Pico];
@@ -381,7 +391,7 @@ pub enum Fallback {
381391

382392
/// Recoverable signature
383393
#[derive(Eq, PartialEq, Debug, Clone)]
384-
pub struct Signature(pub RecoverableSignature);
394+
pub struct InvoiceSignature(pub RecoverableSignature);
385395

386396
/// Private routing information
387397
///
@@ -630,7 +640,7 @@ impl SignedRawInvoice {
630640
/// 1. raw invoice
631641
/// 2. hash of the raw invoice
632642
/// 3. signature
633-
pub fn into_parts(self) -> (RawInvoice, [u8; 32], Signature) {
643+
pub fn into_parts(self) -> (RawInvoice, [u8; 32], InvoiceSignature) {
634644
(self.raw_invoice, self.hash, self.signature)
635645
}
636646

@@ -644,8 +654,8 @@ impl SignedRawInvoice {
644654
&self.hash
645655
}
646656

647-
/// Signature for the invoice.
648-
pub fn signature(&self) -> &Signature {
657+
/// InvoiceSignature for the invoice.
658+
pub fn signature(&self) -> &InvoiceSignature {
649659
&self.signature
650660
}
651661

@@ -760,6 +770,9 @@ impl RawInvoice {
760770
/// Signs the invoice using the supplied `sign_function`. This function MAY fail with an error
761771
/// of type `E`. Since the signature of a `SignedRawInvoice` is not required to be valid there
762772
/// are no constraints regarding the validity of the produced signature.
773+
///
774+
/// (C-not exported) As we don't currently support passing function pointers into methods
775+
/// explicitly.
763776
pub fn sign<F, E>(self, sign_method: F) -> Result<SignedRawInvoice, E>
764777
where F: FnOnce(&Message) -> Result<RecoverableSignature, E>
765778
{
@@ -771,11 +784,13 @@ impl RawInvoice {
771784
Ok(SignedRawInvoice {
772785
raw_invoice: self,
773786
hash: raw_hash,
774-
signature: Signature(signature),
787+
signature: InvoiceSignature(signature),
775788
})
776789
}
777790

778791
/// Returns an iterator over all tagged fields with known semantics.
792+
///
793+
/// (C-not exported) As there is not yet a manual mapping for a FilterMap
779794
pub fn known_tagged_fields(&self)
780795
-> FilterMap<Iter<RawTaggedField>, fn(&RawTaggedField) -> Option<&TaggedField>>
781796
{
@@ -824,6 +839,7 @@ impl RawInvoice {
824839
find_extract!(self.known_tagged_fields(), TaggedField::Features(ref x), x)
825840
}
826841

842+
/// (C-not exported) as we don't support Vec<&NonOpaqueType>
827843
pub fn fallbacks(&self) -> Vec<&Fallback> {
828844
self.known_tagged_fields().filter_map(|tf| match tf {
829845
&TaggedField::Fallback(ref f) => Some(f),
@@ -981,6 +997,8 @@ impl Invoice {
981997
}
982998

983999
/// Returns an iterator over all tagged fields of this Invoice.
1000+
///
1001+
/// (C-not exported) As there is not yet a manual mapping for a FilterMap
9841002
pub fn tagged_fields(&self)
9851003
-> FilterMap<Iter<RawTaggedField>, fn(&RawTaggedField) -> Option<&TaggedField>> {
9861004
self.signed_invoice.raw_invoice().known_tagged_fields()
@@ -992,6 +1010,8 @@ impl Invoice {
9921010
}
9931011

9941012
/// Return the description or a hash of it for longer ones
1013+
///
1014+
/// (C-not exported) because we don't yet export InvoiceDescription
9951015
pub fn description(&self) -> InvoiceDescription {
9961016
if let Some(ref direct) = self.signed_invoice.description() {
9971017
return InvoiceDescription::Direct(direct);
@@ -1029,11 +1049,13 @@ impl Invoice {
10291049
}
10301050

10311051
/// Returns the invoice's `min_cltv_expiry` time if present
1032-
pub fn min_final_cltv_expiry(&self) -> Option<&u64> {
1033-
self.signed_invoice.min_final_cltv_expiry().map(|x| &x.0)
1052+
pub fn min_final_cltv_expiry(&self) -> Option<u64> {
1053+
self.signed_invoice.min_final_cltv_expiry().map(|x| x.0)
10341054
}
10351055

10361056
/// Returns a list of all fallback addresses
1057+
///
1058+
/// (C-not exported) as we don't support Vec<&NonOpaqueType>
10371059
pub fn fallbacks(&self) -> Vec<&Fallback> {
10381060
self.signed_invoice.fallbacks()
10391061
}
@@ -1192,7 +1214,7 @@ impl Deref for RouteHint {
11921214
}
11931215
}
11941216

1195-
impl Deref for Signature {
1217+
impl Deref for InvoiceSignature {
11961218
type Target = RecoverableSignature;
11971219

11981220
fn deref(&self) -> &RecoverableSignature {
@@ -1277,6 +1299,8 @@ impl std::error::Error for SemanticError { }
12771299

12781300
/// When signing using a fallible method either an user-supplied `SignError` or a `CreationError`
12791301
/// may occur.
1302+
///
1303+
/// (C-not exported) As we don't support unbounded generics
12801304
#[derive(Eq, PartialEq, Debug, Clone)]
12811305
pub enum SignOrCreationError<S> {
12821306
/// An error occurred during signing
@@ -1354,7 +1378,7 @@ mod test {
13541378
use secp256k1::Secp256k1;
13551379
use secp256k1::recovery::{RecoveryId, RecoverableSignature};
13561380
use secp256k1::key::{SecretKey, PublicKey};
1357-
use {SignedRawInvoice, Signature, RawInvoice, RawHrp, RawDataPart, Currency, Sha256,
1381+
use {SignedRawInvoice, InvoiceSignature, RawInvoice, RawHrp, RawDataPart, Currency, Sha256,
13581382
PositiveTimestamp};
13591383

13601384
let invoice = SignedRawInvoice {
@@ -1383,7 +1407,7 @@ mod test {
13831407
0x7b, 0x1d, 0x85, 0x8d, 0xb1, 0xd1, 0xf7, 0xab, 0x71, 0x37, 0xdc, 0xb7,
13841408
0x83, 0x5d, 0xb2, 0xec, 0xd5, 0x18, 0xe1, 0xc9
13851409
],
1386-
signature: Signature(RecoverableSignature::from_compact(
1410+
signature: InvoiceSignature(RecoverableSignature::from_compact(
13871411
& [
13881412
0x38u8, 0xec, 0x68, 0x91, 0x34, 0x5e, 0x20, 0x41, 0x45, 0xbe, 0x8a,
13891413
0x3a, 0x99, 0xde, 0x38, 0xe9, 0x8a, 0x39, 0xd6, 0xa5, 0x69, 0x43,
@@ -1591,7 +1615,7 @@ mod test {
15911615
);
15921616
assert_eq!(invoice.payee_pub_key(), Some(&public_key));
15931617
assert_eq!(invoice.expiry_time(), Duration::from_secs(54321));
1594-
assert_eq!(invoice.min_final_cltv_expiry(), Some(&144));
1618+
assert_eq!(invoice.min_final_cltv_expiry(), Some(144));
15951619
assert_eq!(invoice.fallbacks(), vec![&Fallback::PubKeyHash([0;20])]);
15961620
assert_eq!(invoice.routes(), vec![&RouteHint(route_1), &RouteHint(route_2)]);
15971621
assert_eq!(

lightning-invoice/src/ser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ impl ToBase32 for TaggedField {
461461
}
462462
}
463463

464-
impl ToBase32 for Signature {
464+
impl ToBase32 for InvoiceSignature {
465465
fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
466466
let mut converter = BytesToBase32::new(writer);
467467
let (recovery_id, signature) = self.0.serialize_compact();

lightning/src/chain/chainmonitor.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ where C::Target: chain::Filter,
144144
impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>
145145
chain::Listen for ChainMonitor<ChannelSigner, C, T, F, L, P>
146146
where
147-
ChannelSigner: Sign,
148147
C::Target: chain::Filter,
149148
T::Target: BroadcasterInterface,
150149
F::Target: FeeEstimator,
@@ -172,7 +171,6 @@ where
172171
impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>
173172
chain::Confirm for ChainMonitor<ChannelSigner, C, T, F, L, P>
174173
where
175-
ChannelSigner: Sign,
176174
C::Target: chain::Filter,
177175
T::Target: BroadcasterInterface,
178176
F::Target: FeeEstimator,

lightning/src/ln/features.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ impl<T: sealed::Context> Features<T> {
502502

503503
/// Create a Features given a set of flags, in little-endian. This is in reverse byte order from
504504
/// most on-the-wire encodings.
505+
/// (C-not exported) as we don't support export across multiple T
505506
pub fn from_le_bytes(flags: Vec<u8>) -> Features<T> {
506507
Features {
507508
flags,

0 commit comments

Comments
 (0)