Skip to content
This repository was archived by the owner on Apr 13, 2021. It is now read-only.

Commit 4e81a10

Browse files
committed
Make docs mandatory and fix missing docs
1 parent 0ea735a commit 4e81a10

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

src/de.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,9 @@ impl FromBase32 for Route {
582582
}
583583
}
584584

585+
/// Errors that indicate what is wrong with the invoice. They have some granularity for debug
586+
/// reasons, but should generally result in an "invalid BOLT11 invoice" message for the user.
587+
#[allow(missing_docs)]
585588
#[derive(PartialEq, Debug, Clone)]
586589
pub enum ParseError {
587590
Bech32Error(bech32::Error),
@@ -601,13 +604,22 @@ pub enum ParseError {
601604
InvalidScriptHashLength,
602605
InvalidRecoveryId,
603606
InvalidSliceLength(String),
607+
608+
/// Not an error, but used internally to signal that a part of the invoice should be ignored
609+
/// according to BOLT11
604610
Skip,
605611
TimestampOverflow,
606612
}
607613

614+
/// Indicates that something went wrong while parsing or validating the invoice. Parsing errors
615+
/// should be mostly seen as opaque and are only there for debugging reasons. Semantic errors
616+
/// like wrong signatures, missing fields etc. could mean that someone tampered with the invoice.
608617
#[derive(PartialEq, Debug, Clone)]
609618
pub enum ParseOrSemanticError {
619+
/// The invoice couldn't be decoded
610620
ParseError(ParseError),
621+
622+
/// The invoice could be decoded but violates the BOLT11 standard
611623
SemanticError(::SemanticError),
612624
}
613625

src/lib.rs

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
#![deny(missing_docs)]
2+
#![deny(non_upper_case_globals)]
3+
#![deny(non_camel_case_types)]
4+
#![deny(non_snake_case)]
5+
#![deny(unused_mut)]
6+
7+
#![cfg_attr(feature = "strict", deny(warnings))]
8+
9+
//! This crate provides data structures to represent
10+
//! [lightning BOLT11](https://github.com/lightningnetwork/lightning-rfc/blob/master/11-payment-encoding.md)
11+
//! invoices and functions to create, encode and decode these. If you just want to use the standard
12+
//! en-/decoding functionality this should get you started:
13+
//!
14+
//! * For parsing use `str::parse::<Invoice>(&self)` (see the docs of `impl FromStr for Invoice`)
15+
//! * For constructing invoices use the `InvoiceBuilder`
16+
//! * For serializing invoices use the `Display`/`ToString` traits
17+
118
extern crate bech32;
219
extern crate bitcoin_hashes;
320
extern crate num_traits;
@@ -156,10 +173,15 @@ pub struct Invoice {
156173
signed_invoice: SignedRawInvoice,
157174
}
158175

176+
/// Represents the description of an invoice which has to be either a directly included string or
177+
/// a hash of a description provided out of band.
159178
#[derive(Eq, PartialEq, Debug, Clone)]
160179
pub enum InvoiceDescription<'f> {
180+
/// Reference to the directly supplied description in the invoice
161181
Direct(&'f Description),
162-
Hash(&'f Sha256)
182+
183+
/// Reference to the description's hash included in the invoice
184+
Hash(&'f Sha256),
163185
}
164186

165187
/// Represents a signed `RawInvoice` with cached hash. The signature is not checked and may be
@@ -188,6 +210,8 @@ pub struct SignedRawInvoice {
188210
/// Represents an syntactically correct Invoice for a payment on the lightning network,
189211
/// but without the signature information.
190212
/// De- and encoding should not lead to information loss but may lead to different hashes.
213+
///
214+
/// For methods without docs see the corresponding methods in `Invoice`.
191215
#[derive(Eq, PartialEq, Debug, Clone)]
192216
pub struct RawInvoice {
193217
/// human readable part
@@ -263,6 +287,8 @@ impl SiPrefix {
263287
}
264288
}
265289

290+
/// Enum representing the crypto currencies supported by this library
291+
#[allow(missing_docs)]
266292
#[derive(Eq, PartialEq, Debug, Clone)]
267293
pub enum Currency {
268294
Bitcoin,
@@ -279,6 +305,9 @@ pub enum RawTaggedField {
279305
}
280306

281307
/// Tagged field with known tag
308+
///
309+
/// For descriptions of the enum values please refer to the enclosed type's docs.
310+
#[allow(missing_docs)]
282311
#[derive(Eq, PartialEq, Debug, Clone)]
283312
pub enum TaggedField {
284313
PaymentHash(Sha256),
@@ -322,6 +351,7 @@ pub struct MinFinalCltvExpiry(pub u64);
322351

323352
// TODO: better types instead onf byte arrays
324353
/// Fallback address in case no LN payment is possible
354+
#[allow(missing_docs)]
325355
#[derive(Eq, PartialEq, Debug, Clone)]
326356
pub enum Fallback {
327357
SegWitProgram {
@@ -344,15 +374,27 @@ pub struct Signature(pub RecoverableSignature);
344374
#[derive(Eq, PartialEq, Debug, Clone)]
345375
pub struct Route(Vec<RouteHop>);
346376

377+
/// Node on a private route
347378
#[derive(Eq, PartialEq, Debug, Clone)]
348379
pub struct RouteHop {
380+
/// Node's public key
349381
pub pubkey: PublicKey,
382+
383+
/// Which channel of this node we would be using
350384
pub short_channel_id: [u8; 8],
385+
386+
/// Fee charged by this node per transaction
351387
pub fee_base_msat: u32,
388+
389+
/// Fee charged by this node proportional to the amount routed
352390
pub fee_proportional_millionths: u32,
391+
392+
/// Delta substracted by this node from incoming cltv_expiry value
353393
pub cltv_expiry_delta: u16,
354394
}
355395

396+
/// Tag constants as specified in BOLT11
397+
#[allow(missing_docs)]
356398
pub mod constants {
357399
pub const TAG_PAYMENT_HASH: u8 = 1;
358400
pub const TAG_DESCRIPTION: u8 = 13;
@@ -665,6 +707,7 @@ macro_rules! find_extract {
665707
};
666708
}
667709

710+
#[allow(missing_docs)]
668711
impl RawInvoice {
669712
/// Hash the HRP as bytes and signatureless data part.
670713
fn hash_from_parts(hrp_bytes: &[u8], data_without_signature: &[u5]) -> [u8; 32] {
@@ -841,6 +884,7 @@ impl Deref for PositiveTimestamp {
841884
}
842885

843886
impl Invoice {
887+
/// Transform the `Invoice` into it's unchecked version
844888
pub fn into_signed_raw(self) -> SignedRawInvoice {
845889
self.signed_invoice
846890
}
@@ -911,6 +955,7 @@ impl Invoice {
911955
Ok(invoice)
912956
}
913957

958+
/// Returns the `Invoice`'s timestamp (should equal it's creation time)
914959
pub fn timestamp(&self) -> &SystemTime {
915960
self.signed_invoice.raw_invoice().data.timestamp.as_time()
916961
}
@@ -921,10 +966,12 @@ impl Invoice {
921966
self.signed_invoice.raw_invoice().known_tagged_fields()
922967
}
923968

969+
/// Returns the hash to which we will receive the preimage on completion of the payment
924970
pub fn payment_hash(&self) -> &Sha256 {
925971
self.signed_invoice.payment_hash().expect("checked by constructor")
926972
}
927973

974+
/// Return the description or a hash of it for longer ones
928975
pub fn description(&self) -> InvoiceDescription {
929976
if let Some(ref direct) = self.signed_invoice.description() {
930977
return InvoiceDescription::Direct(direct);
@@ -934,34 +981,42 @@ impl Invoice {
934981
unreachable!("ensured by constructor");
935982
}
936983

984+
/// Get the payee's public key if one was included in the invoice
937985
pub fn payee_pub_key(&self) -> Option<&PayeePubKey> {
938986
self.signed_invoice.payee_pub_key()
939987
}
940988

989+
/// Recover the payee's public key (only to be used if none was included in the invoice)
941990
pub fn recover_payee_pub_key(&self) -> PayeePubKey {
942991
self.signed_invoice.recover_payee_pub_key().expect("was checked by constructor")
943992
}
944993

994+
/// Returns the invoice's expiry time if present
945995
pub fn expiry_time(&self) -> Option<&ExpiryTime> {
946996
self.signed_invoice.expiry_time()
947997
}
948998

999+
/// Returns the invoice's `min_cltv_expiry` time if present
9491000
pub fn min_final_cltv_expiry(&self) -> Option<&MinFinalCltvExpiry> {
9501001
self.signed_invoice.min_final_cltv_expiry()
9511002
}
9521003

1004+
/// Returns a list of all fallback addresses
9531005
pub fn fallbacks(&self) -> Vec<&Fallback> {
9541006
self.signed_invoice.fallbacks()
9551007
}
9561008

1009+
/// Returns a list of all routes included in the invoice
9571010
pub fn routes(&self) -> Vec<&Route> {
9581011
self.signed_invoice.routes()
9591012
}
9601013

1014+
/// Returns the currency for which the invoice was issued
9611015
pub fn currency(&self) -> Currency {
9621016
self.signed_invoice.currency()
9631017
}
9641018

1019+
/// Returns the amount if specified in the invoice as pico <currency>.
9651020
pub fn amount_pico_btc(&self) -> Option<u64> {
9661021
self.signed_invoice.amount_pico_btc()
9671022
}
@@ -1005,6 +1060,7 @@ impl Description {
10051060
}
10061061
}
10071062

1063+
/// Returns the underlying description `String`
10081064
pub fn into_inner(self) -> String {
10091065
self.0
10101066
}
@@ -1039,6 +1095,9 @@ impl Deref for PayeePubKey {
10391095
}
10401096

10411097
impl ExpiryTime {
1098+
/// Construct an `ExpiryTime` from seconds. If there exists a `PositiveTimestamp` which would
1099+
/// overflow on adding the `EpiryTime` to it then this function will return a
1100+
/// `CreationError::ExpiryTimeOutOfBounds`.
10421101
pub fn from_seconds(seconds: u64) -> Result<ExpiryTime, CreationError> {
10431102
if seconds <= MAX_EXPIRY_TIME {
10441103
Ok(ExpiryTime(Duration::from_secs(seconds)))
@@ -1047,6 +1106,9 @@ impl ExpiryTime {
10471106
}
10481107
}
10491108

1109+
/// Construct an `ExpiryTime` from a `Duration`. If there exists a `PositiveTimestamp` which
1110+
/// would overflow on adding the `EpiryTime` to it then this function will return a
1111+
/// `CreationError::ExpiryTimeOutOfBounds`.
10501112
pub fn from_duration(duration: Duration) -> Result<ExpiryTime, CreationError> {
10511113
if duration.as_secs() <= MAX_EXPIRY_TIME {
10521114
Ok(ExpiryTime(duration))
@@ -1055,16 +1117,19 @@ impl ExpiryTime {
10551117
}
10561118
}
10571119

1120+
/// Returns the expiry time in seconds
10581121
pub fn as_seconds(&self) -> u64 {
10591122
self.0.as_secs()
10601123
}
10611124

1125+
/// Returns a reference to the underlying `Duration` (=expiry time)
10621126
pub fn as_duration(&self) -> &Duration {
10631127
&self.0
10641128
}
10651129
}
10661130

10671131
impl Route {
1132+
/// Create a new (partial) route from a list of hops
10681133
pub fn new(hops: Vec<RouteHop>) -> Result<Route, CreationError> {
10691134
if hops.len() <= 12 {
10701135
Ok(Route(hops))
@@ -1073,7 +1138,8 @@ impl Route {
10731138
}
10741139
}
10751140

1076-
fn into_inner(self) -> Vec<RouteHop> {
1141+
/// Returrn the underlying vector of hops
1142+
pub fn into_inner(self) -> Vec<RouteHop> {
10771143
self.0
10781144
}
10791145
}
@@ -1128,21 +1194,33 @@ pub enum CreationError {
11281194
/// requirements sections in BOLT #11
11291195
#[derive(Eq, PartialEq, Debug, Clone)]
11301196
pub enum SemanticError {
1197+
/// The invoice is missing the mandatory payment hash
11311198
NoPaymentHash,
1199+
1200+
/// The invoice has multiple payment hashes which isn't allowed
11321201
MultiplePaymentHashes,
11331202

1203+
/// No description or description hash are part of the invoice
11341204
NoDescription,
1205+
1206+
/// The invoice contains multiple descriptions and/or description hashes which isn't allowed
11351207
MultipleDescriptions,
11361208

1209+
/// The recovery id doesn't fit the signature/pub key
11371210
InvalidRecoveryId,
1211+
1212+
/// The invoice's signature is invalid
11381213
InvalidSignature,
11391214
}
11401215

11411216
/// When signing using a fallible method either an user-supplied `SignError` or a `CreationError`
11421217
/// may occur.
11431218
#[derive(Eq, PartialEq, Debug, Clone)]
11441219
pub enum SignOrCreationError<S> {
1220+
/// An error occurred during signing
11451221
SignError(S),
1222+
1223+
/// An error occurred while building the transaction
11461224
CreationError(CreationError),
11471225
}
11481226

0 commit comments

Comments
 (0)