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;
@@ -115,10 +132,15 @@ pub struct Invoice {
115
132
signed_invoice : SignedRawInvoice ,
116
133
}
117
134
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.
118
137
#[ derive( Eq , PartialEq , Debug , Clone ) ]
119
138
pub enum InvoiceDescription < ' f > {
139
+ /// Reference to the directly supplied description in the invoice
120
140
Direct ( & ' f Description ) ,
121
- Hash ( & ' f Sha256 )
141
+
142
+ /// Reference to the description's hash included in the invoice
143
+ Hash ( & ' f Sha256 ) ,
122
144
}
123
145
124
146
/// Represents a signed `RawInvoice` with cached hash. The signature is not checked and may be
@@ -147,6 +169,8 @@ pub struct SignedRawInvoice {
147
169
/// Represents an syntactically correct Invoice for a payment on the lightning network,
148
170
/// but without the signature information.
149
171
/// 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`.
150
174
#[ derive( Eq , PartialEq , Debug , Clone ) ]
151
175
pub struct RawInvoice {
152
176
/// human readable part
@@ -222,6 +246,8 @@ impl SiPrefix {
222
246
}
223
247
}
224
248
249
+ /// Enum representing the crypto currencies supported by this library
250
+ #[ allow( missing_docs) ]
225
251
#[ derive( Eq , PartialEq , Debug , Clone ) ]
226
252
pub enum Currency {
227
253
Bitcoin ,
@@ -238,6 +264,9 @@ pub enum RawTaggedField {
238
264
}
239
265
240
266
/// 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) ]
241
270
#[ derive( Eq , PartialEq , Debug , Clone ) ]
242
271
pub enum TaggedField {
243
272
PaymentHash ( Sha256 ) ,
@@ -279,6 +308,7 @@ pub struct MinFinalCltvExpiry(pub u64);
279
308
280
309
// TODO: better types instead onf byte arrays
281
310
/// Fallback address in case no LN payment is possible
311
+ #[ allow( missing_docs) ]
282
312
#[ derive( Eq , PartialEq , Debug , Clone ) ]
283
313
pub enum Fallback {
284
314
SegWitProgram {
@@ -301,15 +331,27 @@ pub struct Signature(pub RecoverableSignature);
301
331
#[ derive( Eq , PartialEq , Debug , Clone ) ]
302
332
pub struct Route ( Vec < RouteHop > ) ;
303
333
334
+ /// Node on a private route
304
335
#[ derive( Eq , PartialEq , Debug , Clone ) ]
305
336
pub struct RouteHop {
337
+ /// Node's public key
306
338
pub pubkey : PublicKey ,
339
+
340
+ /// Which channel of this node we would be using
307
341
pub short_channel_id : [ u8 ; 8 ] ,
342
+
343
+ /// Fee charged by this node per transaction
308
344
pub fee_base_msat : u32 ,
345
+
346
+ /// Fee charged by this node proportional to the amount routed
309
347
pub fee_proportional_millionths : u32 ,
348
+
349
+ /// Delta substracted by this node from incoming cltv_expiry value
310
350
pub cltv_expiry_delta : u16 ,
311
351
}
312
352
353
+ /// Tag constants as specified in BOLT11
354
+ #[ allow( missing_docs) ]
313
355
pub mod constants {
314
356
pub const TAG_PAYMENT_HASH : u8 = 1 ;
315
357
pub const TAG_DESCRIPTION : u8 = 13 ;
@@ -629,6 +671,7 @@ macro_rules! find_extract {
629
671
} ;
630
672
}
631
673
674
+ #[ allow( missing_docs) ]
632
675
impl RawInvoice {
633
676
/// Hash the HRP as bytes and signatureless data part.
634
677
fn hash_from_parts ( hrp_bytes : & [ u8 ] , data_without_signature : & [ u5 ] ) -> [ u8 ; 32 ] {
@@ -806,6 +849,7 @@ impl Deref for PositiveTimestamp {
806
849
}
807
850
808
851
impl Invoice {
852
+ /// Transform the `Invoice` into it's unchecked version
809
853
pub fn into_signed_raw ( self ) -> SignedRawInvoice {
810
854
self . signed_invoice
811
855
}
@@ -876,6 +920,7 @@ impl Invoice {
876
920
Ok ( invoice)
877
921
}
878
922
923
+ /// Returns the `Invoice`'s timestamp (should equal it's creation time)
879
924
pub fn timestamp ( & self ) -> & SystemTime {
880
925
self . signed_invoice . raw_invoice ( ) . data . timestamp . as_time ( )
881
926
}
@@ -886,10 +931,12 @@ impl Invoice {
886
931
self . signed_invoice . raw_invoice ( ) . known_tagged_fields ( )
887
932
}
888
933
934
+ /// Returns the hash to which we will receive the preimage on completion of the payment
889
935
pub fn payment_hash ( & self ) -> & Sha256 {
890
936
self . signed_invoice . payment_hash ( ) . expect ( "checked by constructor" )
891
937
}
892
938
939
+ /// Return the description or a hash of it for longer ones
893
940
pub fn description ( & self ) -> InvoiceDescription {
894
941
if let Some ( ref direct) = self . signed_invoice . description ( ) {
895
942
return InvoiceDescription :: Direct ( direct) ;
@@ -899,34 +946,42 @@ impl Invoice {
899
946
unreachable ! ( "ensured by constructor" ) ;
900
947
}
901
948
949
+ /// Get the payee's public key if one was included in the invoice
902
950
pub fn payee_pub_key ( & self ) -> Option < & PayeePubKey > {
903
951
self . signed_invoice . payee_pub_key ( )
904
952
}
905
953
954
+ /// Recover the payee's public key (only to be used if none was included in the invoice)
906
955
pub fn recover_payee_pub_key ( & self ) -> PayeePubKey {
907
956
self . signed_invoice . recover_payee_pub_key ( ) . expect ( "was checked by constructor" )
908
957
}
909
958
959
+ /// Returns the invoice's expiry time if present
910
960
pub fn expiry_time ( & self ) -> Option < & ExpiryTime > {
911
961
self . signed_invoice . expiry_time ( )
912
962
}
913
963
964
+ /// Returns the invoice's `min_cltv_expiry` time if present
914
965
pub fn min_final_cltv_expiry ( & self ) -> Option < & MinFinalCltvExpiry > {
915
966
self . signed_invoice . min_final_cltv_expiry ( )
916
967
}
917
968
969
+ /// Returns a list of all fallback addresses
918
970
pub fn fallbacks ( & self ) -> Vec < & Fallback > {
919
971
self . signed_invoice . fallbacks ( )
920
972
}
921
973
974
+ /// Returns a list of all routes included in the invoice
922
975
pub fn routes ( & self ) -> Vec < & Route > {
923
976
self . signed_invoice . routes ( )
924
977
}
925
978
979
+ /// Returns the currency for which the invoice was issued
926
980
pub fn currency ( & self ) -> Currency {
927
981
self . signed_invoice . currency ( )
928
982
}
929
983
984
+ /// Returns the amount if specified in the invoice as pico <currency>.
930
985
pub fn amount_pico_btc ( & self ) -> Option < u64 > {
931
986
self . signed_invoice . amount_pico_btc ( )
932
987
}
@@ -970,6 +1025,7 @@ impl Description {
970
1025
}
971
1026
}
972
1027
1028
+ /// Returns the underlying description `String`
973
1029
pub fn into_inner ( self ) -> String {
974
1030
self . 0
975
1031
}
@@ -1004,6 +1060,9 @@ impl Deref for PayeePubKey {
1004
1060
}
1005
1061
1006
1062
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`.
1007
1066
pub fn from_seconds ( seconds : u64 ) -> Result < ExpiryTime , CreationError > {
1008
1067
if seconds <= SYSTEM_TIME_MAX_UNIX_TIMESTAMP - MAX_EXPIRY_TIME {
1009
1068
Ok ( ExpiryTime ( Duration :: from_secs ( seconds) ) )
@@ -1012,6 +1071,9 @@ impl ExpiryTime {
1012
1071
}
1013
1072
}
1014
1073
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`.
1015
1077
pub fn from_duration ( duration : Duration ) -> Result < ExpiryTime , CreationError > {
1016
1078
if duration. as_secs ( ) <= SYSTEM_TIME_MAX_UNIX_TIMESTAMP - MAX_EXPIRY_TIME {
1017
1079
Ok ( ExpiryTime ( duration) )
@@ -1020,16 +1082,19 @@ impl ExpiryTime {
1020
1082
}
1021
1083
}
1022
1084
1085
+ /// Returns the expiry time in seconds
1023
1086
pub fn as_seconds ( & self ) -> u64 {
1024
1087
self . 0 . as_secs ( )
1025
1088
}
1026
1089
1090
+ /// Returns a reference to the underlying `Duration` (=expiry time)
1027
1091
pub fn as_duration ( & self ) -> & Duration {
1028
1092
& self . 0
1029
1093
}
1030
1094
}
1031
1095
1032
1096
impl Route {
1097
+ /// Create a new (partial) route from a list of hops
1033
1098
pub fn new ( hops : Vec < RouteHop > ) -> Result < Route , CreationError > {
1034
1099
if hops. len ( ) <= 12 {
1035
1100
Ok ( Route ( hops) )
@@ -1038,7 +1103,8 @@ impl Route {
1038
1103
}
1039
1104
}
1040
1105
1041
- fn into_inner ( self ) -> Vec < RouteHop > {
1106
+ /// Returrn the underlying vector of hops
1107
+ pub fn into_inner ( self ) -> Vec < RouteHop > {
1042
1108
self . 0
1043
1109
}
1044
1110
}
@@ -1093,21 +1159,33 @@ pub enum CreationError {
1093
1159
/// requirements sections in BOLT #11
1094
1160
#[ derive( Eq , PartialEq , Debug , Clone ) ]
1095
1161
pub enum SemanticError {
1162
+ /// The invoice is missing the mandatory payment hash
1096
1163
NoPaymentHash ,
1164
+
1165
+ /// The invoice has multiple payment hashes which isn't allowed
1097
1166
MultiplePaymentHashes ,
1098
1167
1168
+ /// No description or description hash are part of the invoice
1099
1169
NoDescription ,
1170
+
1171
+ /// The invoice contains multiple descriptions and/or description hashes which isn't allowed
1100
1172
MultipleDescriptions ,
1101
1173
1174
+ /// The recovery id doesn't fit the signature/pub key
1102
1175
InvalidRecoveryId ,
1176
+
1177
+ /// The invoice's signature is invalid
1103
1178
InvalidSignature ,
1104
1179
}
1105
1180
1106
1181
/// When signing using a fallible method either an user-supplied `SignError` or a `CreationError`
1107
1182
/// may occur.
1108
1183
#[ derive( Eq , PartialEq , Debug , Clone ) ]
1109
1184
pub enum SignOrCreationError < S > {
1185
+ /// An error occurred during signing
1110
1186
SignError ( S ) ,
1187
+
1188
+ /// An error occurred while building the transaction
1111
1189
CreationError ( CreationError ) ,
1112
1190
}
1113
1191
0 commit comments