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

Commit 01cda89

Browse files
committed
Use bitcoin_hashes Sha256Hash instead of [u8; 32]
1 parent e89dded commit 01cda89

File tree

3 files changed

+45
-36
lines changed

3 files changed

+45
-36
lines changed

src/de.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use std::str::FromStr;
88
use bech32;
99
use bech32::{Bech32, u5, FromBase32};
1010

11+
use bitcoin_hashes::Hash;
12+
use bitcoin_hashes::sha256::Sha256Hash;
13+
1114
use num_traits::{CheckedAdd, CheckedMul};
1215

1316
use secp256k1;
@@ -429,9 +432,8 @@ impl FromBase32 for Sha256 {
429432
// "A reader MUST skip over […] a p, [or] h […] field that does not have data_length 52 […]."
430433
Err(ParseError::Skip)
431434
} else {
432-
let mut hash: [u8; 32] = Default::default();
433-
hash.copy_from_slice(&Vec::<u8>::from_base32(field_data)?);
434-
Ok(Sha256(hash))
435+
Ok(Sha256(Sha256Hash::from_slice(&Vec::<u8>::from_base32(field_data)?)
436+
.expect("length was checked before (52 u5 -> 32 u8)")))
435437
}
436438
}
437439
}
@@ -694,6 +696,8 @@ mod test {
694696
use secp256k1::{PublicKey, Secp256k1};
695697
use bech32::u5;
696698
use SignedRawInvoice;
699+
use bitcoin_hashes::hex::FromHex;
700+
use bitcoin_hashes::sha256::Sha256Hash;
697701

698702
const CHARSET_REV: [i8; 128] = [
699703
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -742,11 +746,9 @@ mod test {
742746
"qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypq".as_bytes()
743747
);
744748

745-
let hash = [
746-
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x01, 0x02, 0x03,
747-
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
748-
0x08, 0x09, 0x01, 0x02
749-
];
749+
let hash = Sha256Hash::from_hex(
750+
"0001020304050607080900010203040506070809000102030405060708090102"
751+
).unwrap();
750752
let expected = Ok(Sha256(hash));
751753

752754
assert_eq!(Sha256::from_base32(&input), expected);
@@ -903,11 +905,9 @@ mod test {
903905
data: RawDataPart {
904906
timestamp: 1496314658,
905907
tagged_fields: vec ! [
906-
PaymentHash(Sha256([
907-
0x00u8, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,
908-
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x01,
909-
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x01, 0x02
910-
])).into(),
908+
PaymentHash(Sha256(Sha256Hash::from_hex(
909+
"0001020304050607080900010203040506070809000102030405060708090102"
910+
).unwrap())).into(),
911911
Description(
912912
::Description::new(
913913
"Please consider supporting this project".to_owned()

src/lib.rs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ pub use de::{ParseError, ParseOrSemanticError};
2626
/// ```
2727
/// extern crate secp256k1;
2828
/// extern crate lightning_invoice;
29+
/// extern crate bitcoin_hashes;
30+
///
31+
/// use bitcoin_hashes::Hash;
32+
/// use bitcoin_hashes::sha256::Sha256Hash;
2933
///
3034
/// use secp256k1::Secp256k1;
3135
/// use secp256k1::key::SecretKey;
@@ -42,9 +46,11 @@ pub use de::{ParseError, ParseOrSemanticError};
4246
/// ][..]
4347
/// ).unwrap();
4448
///
49+
/// let payment_hash = Sha256Hash::from_slice(&[0; 32][..]).unwrap();
50+
///
4551
/// let invoice = InvoiceBuilder::new(Currency::Bitcoin)
4652
/// .description("Coins pls!".into())
47-
/// .payment_hash([0u8; 32])
53+
/// .payment_hash(payment_hash)
4854
/// .current_timestamp()
4955
/// .build_signed(|hash| {
5056
/// Secp256k1::new().sign_recoverable(hash, &private_key)
@@ -213,10 +219,9 @@ pub enum TaggedField {
213219
Route(Route),
214220
}
215221

216-
// TODO: use struct from bitcoin_hashes
217222
/// SHA-256 hash
218223
#[derive(Eq, PartialEq, Debug, Clone)]
219-
pub struct Sha256(pub [u8; 32]);
224+
pub struct Sha256(pub Sha256Hash);
220225

221226
/// Description string
222227
///
@@ -421,15 +426,15 @@ impl<H: tb::Bool, T: tb::Bool> InvoiceBuilder<tb::False, H, T> {
421426
}
422427

423428
/// 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> {
425430
self.tagged_fields.push(TaggedField::DescriptionHash(Sha256(description_hash)));
426431
self.set_flags()
427432
}
428433
}
429434

430435
impl<D: tb::Bool, T: tb::Bool> InvoiceBuilder<D, tb::False, T> {
431436
/// 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> {
433438
self.tagged_fields.push(TaggedField::PaymentHash(Sha256(hash)));
434439
self.set_flags()
435440
}
@@ -988,6 +993,9 @@ pub enum SignOrCreationError<S> {
988993

989994
#[cfg(test)]
990995
mod test {
996+
use bitcoin_hashes::Hash;
997+
use bitcoin_hashes::hex::FromHex;
998+
use bitcoin_hashes::sha256::Sha256Hash;
991999

9921000
#[test]
9931001
fn test_calc_invoice_hash() {
@@ -1004,12 +1012,12 @@ mod test {
10041012
data: RawDataPart {
10051013
timestamp: 1496314658,
10061014
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(),
10131021
],
10141022
},
10151023
};
@@ -1040,11 +1048,9 @@ mod test {
10401048
data: RawDataPart {
10411049
timestamp: 1496314658,
10421050
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(),
10481054
Description(
10491055
::Description::new(
10501056
"Please consider supporting this project".to_owned()
@@ -1100,7 +1106,7 @@ mod test {
11001106

11011107
let builder = InvoiceBuilder::new(Currency::Bitcoin)
11021108
.description("Test".into())
1103-
.payment_hash([0;32])
1109+
.payment_hash(Sha256Hash::from_slice(&[0;32][..]).unwrap())
11041110
.current_timestamp();
11051111

11061112
let invoice = builder.clone()
@@ -1129,7 +1135,7 @@ mod test {
11291135
use secp256k1::Secp256k1;
11301136

11311137
let builder = InvoiceBuilder::new(Currency::Bitcoin)
1132-
.payment_hash([0;32])
1138+
.payment_hash(Sha256Hash::from_slice(&[0;32][..]).unwrap())
11331139
.current_timestamp();
11341140

11351141
let too_long_string = String::from_iter(
@@ -1232,8 +1238,8 @@ mod test {
12321238
.fallback(Fallback::PubKeyHash([0;20]))
12331239
.route(route_1.clone())
12341240
.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());
12371243

12381244
let invoice = builder.clone().build_signed(|hash| {
12391245
secp_ctx.sign_recoverable(hash, &private_key)
@@ -1250,8 +1256,11 @@ mod test {
12501256
assert_eq!(invoice.min_final_cltv_expiry(), Some(&MinFinalCltvExpiry(144)));
12511257
assert_eq!(invoice.fallbacks(), vec![&Fallback::PubKeyHash([0;20])]);
12521258
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()));
12551264

12561265
let raw_invoice = builder.build_raw().unwrap();
12571266
assert_eq!(raw_invoice, *invoice.into_signed_raw().raw_invoice())

src/ser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl ToBase32<Vec<u5>> for RawTaggedField {
145145

146146
impl ToBase32<Vec<u5>> for Sha256 {
147147
fn to_base32(&self) -> Vec<u5> {
148-
self.0.to_base32()
148+
(&self.0[..]).to_base32()
149149
}
150150
}
151151

0 commit comments

Comments
 (0)