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

Commit 38d2049

Browse files
committed
Make docs mandatory and fix missing docs
1 parent bcb778f commit 38d2049

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;
@@ -115,10 +132,15 @@ pub struct Invoice {
115132
signed_invoice: SignedRawInvoice,
116133
}
117134

135+
/// Represents the description of an invoice which has to be either a directly included string or
136+
/// a hash of a description provided out of band.
118137
#[derive(Eq, PartialEq, Debug, Clone)]
119138
pub enum InvoiceDescription<'f> {
139+
/// Reference to the directly supplied description in the invoice
120140
Direct(&'f Description),
121-
Hash(&'f Sha256)
141+
142+
/// Reference to the description's hash included in the invoice
143+
Hash(&'f Sha256),
122144
}
123145

124146
/// Represents a signed `RawInvoice` with cached hash. The signature is not checked and may be
@@ -147,6 +169,8 @@ pub struct SignedRawInvoice {
147169
/// Represents an syntactically correct Invoice for a payment on the lightning network,
148170
/// but without the signature information.
149171
/// De- and encoding should not lead to information loss but may lead to different hashes.
172+
///
173+
/// For methods without docs see the corresponding methods in `Invoice`.
150174
#[derive(Eq, PartialEq, Debug, Clone)]
151175
pub struct RawInvoice {
152176
/// human readable part
@@ -222,6 +246,8 @@ impl SiPrefix {
222246
}
223247
}
224248

249+
/// Enum representing the crypto currencies supported by this library
250+
#[allow(missing_docs)]
225251
#[derive(Eq, PartialEq, Debug, Clone)]
226252
pub enum Currency {
227253
Bitcoin,
@@ -238,6 +264,9 @@ pub enum RawTaggedField {
238264
}
239265

240266
/// Tagged field with known tag
267+
///
268+
/// For descriptions of the enum values please refer to the enclosed type's docs.
269+
#[allow(missing_docs)]
241270
#[derive(Eq, PartialEq, Debug, Clone)]
242271
pub enum TaggedField {
243272
PaymentHash(Sha256),
@@ -279,6 +308,7 @@ pub struct MinFinalCltvExpiry(pub u64);
279308

280309
// TODO: better types instead onf byte arrays
281310
/// Fallback address in case no LN payment is possible
311+
#[allow(missing_docs)]
282312
#[derive(Eq, PartialEq, Debug, Clone)]
283313
pub enum Fallback {
284314
SegWitProgram {
@@ -301,15 +331,27 @@ pub struct Signature(pub RecoverableSignature);
301331
#[derive(Eq, PartialEq, Debug, Clone)]
302332
pub struct Route(Vec<RouteHop>);
303333

334+
/// Node on a private route
304335
#[derive(Eq, PartialEq, Debug, Clone)]
305336
pub struct RouteHop {
337+
/// Node's public key
306338
pub pubkey: PublicKey,
339+
340+
/// Which channel of this node we would be using
307341
pub short_channel_id: [u8; 8],
342+
343+
/// Fee charged by this node per transaction
308344
pub fee_base_msat: u32,
345+
346+
/// Fee charged by this node proportional to the amount routed
309347
pub fee_proportional_millionths: u32,
348+
349+
/// Delta substracted by this node from incoming cltv_expiry value
310350
pub cltv_expiry_delta: u16,
311351
}
312352

353+
/// Tag constants as specified in BOLT11
354+
#[allow(missing_docs)]
313355
pub mod constants {
314356
pub const TAG_PAYMENT_HASH: u8 = 1;
315357
pub const TAG_DESCRIPTION: u8 = 13;
@@ -629,6 +671,7 @@ macro_rules! find_extract {
629671
};
630672
}
631673

674+
#[allow(missing_docs)]
632675
impl RawInvoice {
633676
/// Hash the HRP as bytes and signatureless data part.
634677
fn hash_from_parts(hrp_bytes: &[u8], data_without_signature: &[u5]) -> [u8; 32] {
@@ -806,6 +849,7 @@ impl Deref for PositiveTimestamp {
806849
}
807850

808851
impl Invoice {
852+
/// Transform the `Invoice` into it's unchecked version
809853
pub fn into_signed_raw(self) -> SignedRawInvoice {
810854
self.signed_invoice
811855
}
@@ -876,6 +920,7 @@ impl Invoice {
876920
Ok(invoice)
877921
}
878922

923+
/// Returns the `Invoice`'s timestamp (should equal it's creation time)
879924
pub fn timestamp(&self) -> &SystemTime {
880925
self.signed_invoice.raw_invoice().data.timestamp.as_time()
881926
}
@@ -886,10 +931,12 @@ impl Invoice {
886931
self.signed_invoice.raw_invoice().known_tagged_fields()
887932
}
888933

934+
/// Returns the hash to which we will receive the preimage on completion of the payment
889935
pub fn payment_hash(&self) -> &Sha256 {
890936
self.signed_invoice.payment_hash().expect("checked by constructor")
891937
}
892938

939+
/// Return the description or a hash of it for longer ones
893940
pub fn description(&self) -> InvoiceDescription {
894941
if let Some(ref direct) = self.signed_invoice.description() {
895942
return InvoiceDescription::Direct(direct);
@@ -899,34 +946,42 @@ impl Invoice {
899946
unreachable!("ensured by constructor");
900947
}
901948

949+
/// Get the payee's public key if one was included in the invoice
902950
pub fn payee_pub_key(&self) -> Option<&PayeePubKey> {
903951
self.signed_invoice.payee_pub_key()
904952
}
905953

954+
/// Recover the payee's public key (only to be used if none was included in the invoice)
906955
pub fn recover_payee_pub_key(&self) -> PayeePubKey {
907956
self.signed_invoice.recover_payee_pub_key().expect("was checked by constructor")
908957
}
909958

959+
/// Returns the invoice's expiry time if present
910960
pub fn expiry_time(&self) -> Option<&ExpiryTime> {
911961
self.signed_invoice.expiry_time()
912962
}
913963

964+
/// Returns the invoice's `min_cltv_expiry` time if present
914965
pub fn min_final_cltv_expiry(&self) -> Option<&MinFinalCltvExpiry> {
915966
self.signed_invoice.min_final_cltv_expiry()
916967
}
917968

969+
/// Returns a list of all fallback addresses
918970
pub fn fallbacks(&self) -> Vec<&Fallback> {
919971
self.signed_invoice.fallbacks()
920972
}
921973

974+
/// Returns a list of all routes included in the invoice
922975
pub fn routes(&self) -> Vec<&Route> {
923976
self.signed_invoice.routes()
924977
}
925978

979+
/// Returns the currency for which the invoice was issued
926980
pub fn currency(&self) -> Currency {
927981
self.signed_invoice.currency()
928982
}
929983

984+
/// Returns the amount if specified in the invoice as pico <currency>.
930985
pub fn amount_pico_btc(&self) -> Option<u64> {
931986
self.signed_invoice.amount_pico_btc()
932987
}
@@ -970,6 +1025,7 @@ impl Description {
9701025
}
9711026
}
9721027

1028+
/// Returns the underlying description `String`
9731029
pub fn into_inner(self) -> String {
9741030
self.0
9751031
}
@@ -1004,6 +1060,9 @@ impl Deref for PayeePubKey {
10041060
}
10051061

10061062
impl ExpiryTime {
1063+
/// Construct an `ExpiryTime` from seconds. If there exists a `PositiveTimestamp` which would
1064+
/// overflow on adding the `EpiryTime` to it then this function will return a
1065+
/// `CreationError::ExpiryTimeOutOfBounds`.
10071066
pub fn from_seconds(seconds: u64) -> Result<ExpiryTime, CreationError> {
10081067
if seconds <= SYSTEM_TIME_MAX_UNIX_TIMESTAMP - MAX_EXPIRY_TIME {
10091068
Ok(ExpiryTime(Duration::from_secs(seconds)))
@@ -1012,6 +1071,9 @@ impl ExpiryTime {
10121071
}
10131072
}
10141073

1074+
/// Construct an `ExpiryTime` from a `Duration`. If there exists a `PositiveTimestamp` which
1075+
/// would overflow on adding the `EpiryTime` to it then this function will return a
1076+
/// `CreationError::ExpiryTimeOutOfBounds`.
10151077
pub fn from_duration(duration: Duration) -> Result<ExpiryTime, CreationError> {
10161078
if duration.as_secs() <= SYSTEM_TIME_MAX_UNIX_TIMESTAMP - MAX_EXPIRY_TIME {
10171079
Ok(ExpiryTime(duration))
@@ -1020,16 +1082,19 @@ impl ExpiryTime {
10201082
}
10211083
}
10221084

1085+
/// Returns the expiry time in seconds
10231086
pub fn as_seconds(&self) -> u64 {
10241087
self.0.as_secs()
10251088
}
10261089

1090+
/// Returns a reference to the underlying `Duration` (=expiry time)
10271091
pub fn as_duration(&self) -> &Duration {
10281092
&self.0
10291093
}
10301094
}
10311095

10321096
impl Route {
1097+
/// Create a new (partial) route from a list of hops
10331098
pub fn new(hops: Vec<RouteHop>) -> Result<Route, CreationError> {
10341099
if hops.len() <= 12 {
10351100
Ok(Route(hops))
@@ -1038,7 +1103,8 @@ impl Route {
10381103
}
10391104
}
10401105

1041-
fn into_inner(self) -> Vec<RouteHop> {
1106+
/// Returrn the underlying vector of hops
1107+
pub fn into_inner(self) -> Vec<RouteHop> {
10421108
self.0
10431109
}
10441110
}
@@ -1093,21 +1159,33 @@ pub enum CreationError {
10931159
/// requirements sections in BOLT #11
10941160
#[derive(Eq, PartialEq, Debug, Clone)]
10951161
pub enum SemanticError {
1162+
/// The invoice is missing the mandatory payment hash
10961163
NoPaymentHash,
1164+
1165+
/// The invoice has multiple payment hashes which isn't allowed
10971166
MultiplePaymentHashes,
10981167

1168+
/// No description or description hash are part of the invoice
10991169
NoDescription,
1170+
1171+
/// The invoice contains multiple descriptions and/or description hashes which isn't allowed
11001172
MultipleDescriptions,
11011173

1174+
/// The recovery id doesn't fit the signature/pub key
11021175
InvalidRecoveryId,
1176+
1177+
/// The invoice's signature is invalid
11031178
InvalidSignature,
11041179
}
11051180

11061181
/// When signing using a fallible method either an user-supplied `SignError` or a `CreationError`
11071182
/// may occur.
11081183
#[derive(Eq, PartialEq, Debug, Clone)]
11091184
pub enum SignOrCreationError<S> {
1185+
/// An error occurred during signing
11101186
SignError(S),
1187+
1188+
/// An error occurred while building the transaction
11111189
CreationError(CreationError),
11121190
}
11131191

0 commit comments

Comments
 (0)