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
+
1
18
extern crate bech32;
2
19
extern crate bitcoin_hashes;
3
20
extern crate num_traits;
@@ -156,10 +173,15 @@ pub struct Invoice {
156
173
signed_invoice : SignedRawInvoice ,
157
174
}
158
175
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.
159
178
#[ derive( Eq , PartialEq , Debug , Clone ) ]
160
179
pub enum InvoiceDescription < ' f > {
180
+ /// Reference to the directly supplied description in the invoice
161
181
Direct ( & ' f Description ) ,
162
- Hash ( & ' f Sha256 )
182
+
183
+ /// Reference to the description's hash included in the invoice
184
+ Hash ( & ' f Sha256 ) ,
163
185
}
164
186
165
187
/// Represents a signed `RawInvoice` with cached hash. The signature is not checked and may be
@@ -188,6 +210,8 @@ pub struct SignedRawInvoice {
188
210
/// Represents an syntactically correct Invoice for a payment on the lightning network,
189
211
/// but without the signature information.
190
212
/// 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`.
191
215
#[ derive( Eq , PartialEq , Debug , Clone ) ]
192
216
pub struct RawInvoice {
193
217
/// human readable part
@@ -263,6 +287,8 @@ impl SiPrefix {
263
287
}
264
288
}
265
289
290
+ /// Enum representing the crypto currencies supported by this library
291
+ #[ allow( missing_docs) ]
266
292
#[ derive( Eq , PartialEq , Debug , Clone ) ]
267
293
pub enum Currency {
268
294
Bitcoin ,
@@ -279,6 +305,9 @@ pub enum RawTaggedField {
279
305
}
280
306
281
307
/// 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) ]
282
311
#[ derive( Eq , PartialEq , Debug , Clone ) ]
283
312
pub enum TaggedField {
284
313
PaymentHash ( Sha256 ) ,
@@ -322,6 +351,7 @@ pub struct MinFinalCltvExpiry(pub u64);
322
351
323
352
// TODO: better types instead onf byte arrays
324
353
/// Fallback address in case no LN payment is possible
354
+ #[ allow( missing_docs) ]
325
355
#[ derive( Eq , PartialEq , Debug , Clone ) ]
326
356
pub enum Fallback {
327
357
SegWitProgram {
@@ -344,15 +374,27 @@ pub struct Signature(pub RecoverableSignature);
344
374
#[ derive( Eq , PartialEq , Debug , Clone ) ]
345
375
pub struct Route ( Vec < RouteHop > ) ;
346
376
377
+ /// Node on a private route
347
378
#[ derive( Eq , PartialEq , Debug , Clone ) ]
348
379
pub struct RouteHop {
380
+ /// Node's public key
349
381
pub pubkey : PublicKey ,
382
+
383
+ /// Which channel of this node we would be using
350
384
pub short_channel_id : [ u8 ; 8 ] ,
385
+
386
+ /// Fee charged by this node per transaction
351
387
pub fee_base_msat : u32 ,
388
+
389
+ /// Fee charged by this node proportional to the amount routed
352
390
pub fee_proportional_millionths : u32 ,
391
+
392
+ /// Delta substracted by this node from incoming cltv_expiry value
353
393
pub cltv_expiry_delta : u16 ,
354
394
}
355
395
396
+ /// Tag constants as specified in BOLT11
397
+ #[ allow( missing_docs) ]
356
398
pub mod constants {
357
399
pub const TAG_PAYMENT_HASH : u8 = 1 ;
358
400
pub const TAG_DESCRIPTION : u8 = 13 ;
@@ -665,6 +707,7 @@ macro_rules! find_extract {
665
707
} ;
666
708
}
667
709
710
+ #[ allow( missing_docs) ]
668
711
impl RawInvoice {
669
712
/// Hash the HRP as bytes and signatureless data part.
670
713
fn hash_from_parts ( hrp_bytes : & [ u8 ] , data_without_signature : & [ u5 ] ) -> [ u8 ; 32 ] {
@@ -841,6 +884,7 @@ impl Deref for PositiveTimestamp {
841
884
}
842
885
843
886
impl Invoice {
887
+ /// Transform the `Invoice` into it's unchecked version
844
888
pub fn into_signed_raw ( self ) -> SignedRawInvoice {
845
889
self . signed_invoice
846
890
}
@@ -911,6 +955,7 @@ impl Invoice {
911
955
Ok ( invoice)
912
956
}
913
957
958
+ /// Returns the `Invoice`'s timestamp (should equal it's creation time)
914
959
pub fn timestamp ( & self ) -> & SystemTime {
915
960
self . signed_invoice . raw_invoice ( ) . data . timestamp . as_time ( )
916
961
}
@@ -921,10 +966,12 @@ impl Invoice {
921
966
self . signed_invoice . raw_invoice ( ) . known_tagged_fields ( )
922
967
}
923
968
969
+ /// Returns the hash to which we will receive the preimage on completion of the payment
924
970
pub fn payment_hash ( & self ) -> & Sha256 {
925
971
self . signed_invoice . payment_hash ( ) . expect ( "checked by constructor" )
926
972
}
927
973
974
+ /// Return the description or a hash of it for longer ones
928
975
pub fn description ( & self ) -> InvoiceDescription {
929
976
if let Some ( ref direct) = self . signed_invoice . description ( ) {
930
977
return InvoiceDescription :: Direct ( direct) ;
@@ -934,34 +981,42 @@ impl Invoice {
934
981
unreachable ! ( "ensured by constructor" ) ;
935
982
}
936
983
984
+ /// Get the payee's public key if one was included in the invoice
937
985
pub fn payee_pub_key ( & self ) -> Option < & PayeePubKey > {
938
986
self . signed_invoice . payee_pub_key ( )
939
987
}
940
988
989
+ /// Recover the payee's public key (only to be used if none was included in the invoice)
941
990
pub fn recover_payee_pub_key ( & self ) -> PayeePubKey {
942
991
self . signed_invoice . recover_payee_pub_key ( ) . expect ( "was checked by constructor" )
943
992
}
944
993
994
+ /// Returns the invoice's expiry time if present
945
995
pub fn expiry_time ( & self ) -> Option < & ExpiryTime > {
946
996
self . signed_invoice . expiry_time ( )
947
997
}
948
998
999
+ /// Returns the invoice's `min_cltv_expiry` time if present
949
1000
pub fn min_final_cltv_expiry ( & self ) -> Option < & MinFinalCltvExpiry > {
950
1001
self . signed_invoice . min_final_cltv_expiry ( )
951
1002
}
952
1003
1004
+ /// Returns a list of all fallback addresses
953
1005
pub fn fallbacks ( & self ) -> Vec < & Fallback > {
954
1006
self . signed_invoice . fallbacks ( )
955
1007
}
956
1008
1009
+ /// Returns a list of all routes included in the invoice
957
1010
pub fn routes ( & self ) -> Vec < & Route > {
958
1011
self . signed_invoice . routes ( )
959
1012
}
960
1013
1014
+ /// Returns the currency for which the invoice was issued
961
1015
pub fn currency ( & self ) -> Currency {
962
1016
self . signed_invoice . currency ( )
963
1017
}
964
1018
1019
+ /// Returns the amount if specified in the invoice as pico <currency>.
965
1020
pub fn amount_pico_btc ( & self ) -> Option < u64 > {
966
1021
self . signed_invoice . amount_pico_btc ( )
967
1022
}
@@ -1005,6 +1060,7 @@ impl Description {
1005
1060
}
1006
1061
}
1007
1062
1063
+ /// Returns the underlying description `String`
1008
1064
pub fn into_inner ( self ) -> String {
1009
1065
self . 0
1010
1066
}
@@ -1039,6 +1095,9 @@ impl Deref for PayeePubKey {
1039
1095
}
1040
1096
1041
1097
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`.
1042
1101
pub fn from_seconds ( seconds : u64 ) -> Result < ExpiryTime , CreationError > {
1043
1102
if seconds <= MAX_EXPIRY_TIME {
1044
1103
Ok ( ExpiryTime ( Duration :: from_secs ( seconds) ) )
@@ -1047,6 +1106,9 @@ impl ExpiryTime {
1047
1106
}
1048
1107
}
1049
1108
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`.
1050
1112
pub fn from_duration ( duration : Duration ) -> Result < ExpiryTime , CreationError > {
1051
1113
if duration. as_secs ( ) <= MAX_EXPIRY_TIME {
1052
1114
Ok ( ExpiryTime ( duration) )
@@ -1055,16 +1117,19 @@ impl ExpiryTime {
1055
1117
}
1056
1118
}
1057
1119
1120
+ /// Returns the expiry time in seconds
1058
1121
pub fn as_seconds ( & self ) -> u64 {
1059
1122
self . 0 . as_secs ( )
1060
1123
}
1061
1124
1125
+ /// Returns a reference to the underlying `Duration` (=expiry time)
1062
1126
pub fn as_duration ( & self ) -> & Duration {
1063
1127
& self . 0
1064
1128
}
1065
1129
}
1066
1130
1067
1131
impl Route {
1132
+ /// Create a new (partial) route from a list of hops
1068
1133
pub fn new ( hops : Vec < RouteHop > ) -> Result < Route , CreationError > {
1069
1134
if hops. len ( ) <= 12 {
1070
1135
Ok ( Route ( hops) )
@@ -1073,7 +1138,8 @@ impl Route {
1073
1138
}
1074
1139
}
1075
1140
1076
- fn into_inner ( self ) -> Vec < RouteHop > {
1141
+ /// Returrn the underlying vector of hops
1142
+ pub fn into_inner ( self ) -> Vec < RouteHop > {
1077
1143
self . 0
1078
1144
}
1079
1145
}
@@ -1128,21 +1194,33 @@ pub enum CreationError {
1128
1194
/// requirements sections in BOLT #11
1129
1195
#[ derive( Eq , PartialEq , Debug , Clone ) ]
1130
1196
pub enum SemanticError {
1197
+ /// The invoice is missing the mandatory payment hash
1131
1198
NoPaymentHash ,
1199
+
1200
+ /// The invoice has multiple payment hashes which isn't allowed
1132
1201
MultiplePaymentHashes ,
1133
1202
1203
+ /// No description or description hash are part of the invoice
1134
1204
NoDescription ,
1205
+
1206
+ /// The invoice contains multiple descriptions and/or description hashes which isn't allowed
1135
1207
MultipleDescriptions ,
1136
1208
1209
+ /// The recovery id doesn't fit the signature/pub key
1137
1210
InvalidRecoveryId ,
1211
+
1212
+ /// The invoice's signature is invalid
1138
1213
InvalidSignature ,
1139
1214
}
1140
1215
1141
1216
/// When signing using a fallible method either an user-supplied `SignError` or a `CreationError`
1142
1217
/// may occur.
1143
1218
#[ derive( Eq , PartialEq , Debug , Clone ) ]
1144
1219
pub enum SignOrCreationError < S > {
1220
+ /// An error occurred during signing
1145
1221
SignError ( S ) ,
1222
+
1223
+ /// An error occurred while building the transaction
1146
1224
CreationError ( CreationError ) ,
1147
1225
}
1148
1226
0 commit comments