@@ -26,6 +26,10 @@ pub use de::{ParseError, ParseOrSemanticError};
26
26
/// ```
27
27
/// extern crate secp256k1;
28
28
/// extern crate lightning_invoice;
29
+ /// extern crate bitcoin_hashes;
30
+ ///
31
+ /// use bitcoin_hashes::Hash;
32
+ /// use bitcoin_hashes::sha256::Sha256Hash;
29
33
///
30
34
/// use secp256k1::Secp256k1;
31
35
/// use secp256k1::key::SecretKey;
@@ -42,9 +46,11 @@ pub use de::{ParseError, ParseOrSemanticError};
42
46
/// ][..]
43
47
/// ).unwrap();
44
48
///
49
+ /// let payment_hash = Sha256Hash::from_slice(&[0; 32][..]).unwrap();
50
+ ///
45
51
/// let invoice = InvoiceBuilder::new(Currency::Bitcoin)
46
52
/// .description("Coins pls!".into())
47
- /// .payment_hash([0u8; 32] )
53
+ /// .payment_hash(payment_hash )
48
54
/// .current_timestamp()
49
55
/// .build_signed(|hash| {
50
56
/// Secp256k1::new().sign_recoverable(hash, &private_key)
@@ -213,10 +219,9 @@ pub enum TaggedField {
213
219
Route ( Route ) ,
214
220
}
215
221
216
- // TODO: use struct from bitcoin_hashes
217
222
/// SHA-256 hash
218
223
#[ derive( Eq , PartialEq , Debug , Clone ) ]
219
- pub struct Sha256 ( pub [ u8 ; 32 ] ) ;
224
+ pub struct Sha256 ( pub Sha256Hash ) ;
220
225
221
226
/// Description string
222
227
///
@@ -421,15 +426,15 @@ impl<H: tb::Bool, T: tb::Bool> InvoiceBuilder<tb::False, H, T> {
421
426
}
422
427
423
428
/// Set the description hash. This function is only available if no description (hash) was set.
424
- pub fn description_hash ( mut self , description_hash : [ u8 ; 32 ] ) -> InvoiceBuilder < tb:: True , H , T > {
429
+ pub fn description_hash ( mut self , description_hash : Sha256Hash ) -> InvoiceBuilder < tb:: True , H , T > {
425
430
self . tagged_fields . push ( TaggedField :: DescriptionHash ( Sha256 ( description_hash) ) ) ;
426
431
self . set_flags ( )
427
432
}
428
433
}
429
434
430
435
impl < D : tb:: Bool , T : tb:: Bool > InvoiceBuilder < D , tb:: False , T > {
431
436
/// Set the payment hash. This function is only available if no payment hash was set.
432
- pub fn payment_hash ( mut self , hash : [ u8 ; 32 ] ) -> InvoiceBuilder < D , tb:: True , T > {
437
+ pub fn payment_hash ( mut self , hash : Sha256Hash ) -> InvoiceBuilder < D , tb:: True , T > {
433
438
self . tagged_fields . push ( TaggedField :: PaymentHash ( Sha256 ( hash) ) ) ;
434
439
self . set_flags ( )
435
440
}
@@ -988,6 +993,9 @@ pub enum SignOrCreationError<S> {
988
993
989
994
#[ cfg( test) ]
990
995
mod test {
996
+ use bitcoin_hashes:: Hash ;
997
+ use bitcoin_hashes:: hex:: FromHex ;
998
+ use bitcoin_hashes:: sha256:: Sha256Hash ;
991
999
992
1000
#[ test]
993
1001
fn test_calc_invoice_hash ( ) {
@@ -1004,12 +1012,12 @@ mod test {
1004
1012
data : RawDataPart {
1005
1013
timestamp : 1496314658 ,
1006
1014
tagged_fields : vec ! [
1007
- PaymentHash ( :: Sha256 ( [
1008
- 0x00u8 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x00 ,
1009
- 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x00 , 0x01 ,
1010
- 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x01 , 0x02
1011
- ] ) ) . into ( ) ,
1012
- Description ( :: Description :: new ( "Please consider supporting this project" . to_owned ( ) ) . unwrap( ) ) . into( ) ,
1015
+ PaymentHash ( :: Sha256 ( Sha256Hash :: from_hex (
1016
+ "0001020304050607080900010203040506070809000102030405060708090102"
1017
+ ) . unwrap ( ) ) ) . into ( ) ,
1018
+ Description ( :: Description :: new (
1019
+ "Please consider supporting this project" . to_owned ( )
1020
+ ) . unwrap( ) ) . into( ) ,
1013
1021
] ,
1014
1022
} ,
1015
1023
} ;
@@ -1040,11 +1048,9 @@ mod test {
1040
1048
data : RawDataPart {
1041
1049
timestamp : 1496314658 ,
1042
1050
tagged_fields : vec ! [
1043
- PaymentHash ( Sha256 ( [
1044
- 0x00u8 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x00 ,
1045
- 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x00 , 0x01 ,
1046
- 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x01 , 0x02
1047
- ] ) ) . into( ) ,
1051
+ PaymentHash ( Sha256 ( Sha256Hash :: from_hex(
1052
+ "0001020304050607080900010203040506070809000102030405060708090102"
1053
+ ) . unwrap( ) ) ) . into( ) ,
1048
1054
Description (
1049
1055
:: Description :: new(
1050
1056
"Please consider supporting this project" . to_owned( )
@@ -1100,7 +1106,7 @@ mod test {
1100
1106
1101
1107
let builder = InvoiceBuilder :: new ( Currency :: Bitcoin )
1102
1108
. description ( "Test" . into ( ) )
1103
- . payment_hash ( [ 0 ; 32 ] )
1109
+ . payment_hash ( Sha256Hash :: from_slice ( & [ 0 ; 32 ] [ .. ] ) . unwrap ( ) )
1104
1110
. current_timestamp ( ) ;
1105
1111
1106
1112
let invoice = builder. clone ( )
@@ -1129,7 +1135,7 @@ mod test {
1129
1135
use secp256k1:: Secp256k1 ;
1130
1136
1131
1137
let builder = InvoiceBuilder :: new ( Currency :: Bitcoin )
1132
- . payment_hash ( [ 0 ; 32 ] )
1138
+ . payment_hash ( Sha256Hash :: from_slice ( & [ 0 ; 32 ] [ .. ] ) . unwrap ( ) )
1133
1139
. current_timestamp ( ) ;
1134
1140
1135
1141
let too_long_string = String :: from_iter (
@@ -1232,8 +1238,8 @@ mod test {
1232
1238
. fallback ( Fallback :: PubKeyHash ( [ 0 ; 20 ] ) )
1233
1239
. route ( route_1. clone ( ) )
1234
1240
. route ( route_2. clone ( ) )
1235
- . description_hash ( [ 3 ; 32 ] )
1236
- . payment_hash ( [ 21 ; 32 ] ) ;
1241
+ . description_hash ( Sha256Hash :: from_slice ( & [ 3 ; 32 ] [ .. ] ) . unwrap ( ) )
1242
+ . payment_hash ( Sha256Hash :: from_slice ( & [ 21 ; 32 ] [ .. ] ) . unwrap ( ) ) ;
1237
1243
1238
1244
let invoice = builder. clone ( ) . build_signed ( |hash| {
1239
1245
secp_ctx. sign_recoverable ( hash, & private_key)
@@ -1250,8 +1256,11 @@ mod test {
1250
1256
assert_eq ! ( invoice. min_final_cltv_expiry( ) , Some ( & MinFinalCltvExpiry ( 144 ) ) ) ;
1251
1257
assert_eq ! ( invoice. fallbacks( ) , vec![ & Fallback :: PubKeyHash ( [ 0 ; 20 ] ) ] ) ;
1252
1258
assert_eq ! ( invoice. routes( ) , vec![ & Route ( route_1) , & Route ( route_2) ] ) ;
1253
- assert_eq ! ( invoice. description( ) , InvoiceDescription :: Hash ( & Sha256 ( [ 3 ; 32 ] ) ) ) ;
1254
- assert_eq ! ( invoice. payment_hash( ) , & Sha256 ( [ 21 ; 32 ] ) ) ;
1259
+ assert_eq ! (
1260
+ invoice. description( ) ,
1261
+ InvoiceDescription :: Hash ( & Sha256 ( Sha256Hash :: from_slice( & [ 3 ; 32 ] [ ..] ) . unwrap( ) ) )
1262
+ ) ;
1263
+ assert_eq ! ( invoice. payment_hash( ) , & Sha256 ( Sha256Hash :: from_slice( & [ 21 ; 32 ] [ ..] ) . unwrap( ) ) ) ;
1255
1264
1256
1265
let raw_invoice = builder. build_raw ( ) . unwrap ( ) ;
1257
1266
assert_eq ! ( raw_invoice, * invoice. into_signed_raw( ) . raw_invoice( ) )
0 commit comments