Skip to content

Commit a9a0227

Browse files
committed
Move Nonce to a separate offers sub-module
Nonce is used when constructing Offer::metadata and will soon be need when constructing BlindedPath for use in authentication. Move it to separate module now that it is public and will be more widely used.
1 parent 1e26048 commit a9a0227

File tree

7 files changed

+74
-51
lines changed

7 files changed

+74
-51
lines changed

lightning/src/ln/inbound_payment.rs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::crypto::utils::hkdf_extract_expand_5x;
1919
use crate::ln::types::{PaymentHash, PaymentPreimage, PaymentSecret};
2020
use crate::ln::msgs;
2121
use crate::ln::msgs::MAX_VALUE_MSAT;
22+
use crate::offers::nonce::Nonce;
2223
use crate::sign::{KeyMaterial, EntropySource};
2324
use crate::util::errors::APIError;
2425
use crate::util::logger::Logger;
@@ -97,53 +98,6 @@ impl ExpandedKey {
9798
}
9899
}
99100

100-
/// A 128-bit number used only once.
101-
///
102-
/// Needed when constructing [`Offer::metadata`] and deriving [`Offer::signing_pubkey`] from
103-
/// [`ExpandedKey`]. Must not be reused for any other derivation without first hashing.
104-
///
105-
/// [`Offer::metadata`]: crate::offers::offer::Offer::metadata
106-
/// [`Offer::signing_pubkey`]: crate::offers::offer::Offer::signing_pubkey
107-
#[derive(Clone, Copy, Debug, PartialEq)]
108-
pub struct Nonce(pub(crate) [u8; Self::LENGTH]);
109-
110-
impl Nonce {
111-
/// Number of bytes in the nonce.
112-
pub const LENGTH: usize = 16;
113-
114-
/// Creates a `Nonce` from the given [`EntropySource`].
115-
pub fn from_entropy_source<ES: Deref>(entropy_source: ES) -> Self
116-
where
117-
ES::Target: EntropySource,
118-
{
119-
let mut bytes = [0u8; Self::LENGTH];
120-
let rand_bytes = entropy_source.get_secure_random_bytes();
121-
bytes.copy_from_slice(&rand_bytes[..Self::LENGTH]);
122-
123-
Nonce(bytes)
124-
}
125-
126-
/// Returns a slice of the underlying bytes of size [`Nonce::LENGTH`].
127-
pub fn as_slice(&self) -> &[u8] {
128-
&self.0
129-
}
130-
}
131-
132-
impl TryFrom<&[u8]> for Nonce {
133-
type Error = ();
134-
135-
fn try_from(bytes: &[u8]) -> Result<Self, ()> {
136-
if bytes.len() != Self::LENGTH {
137-
return Err(());
138-
}
139-
140-
let mut copied_bytes = [0u8; Self::LENGTH];
141-
copied_bytes.copy_from_slice(bytes);
142-
143-
Ok(Self(copied_bytes))
144-
}
145-
}
146-
147101
enum Method {
148102
LdkPaymentHash = 0,
149103
UserPaymentHash = 1,

lightning/src/offers/invoice_request.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ use crate::blinded_path::BlindedPath;
6868
use crate::ln::types::PaymentHash;
6969
use crate::ln::channelmanager::PaymentId;
7070
use crate::ln::features::InvoiceRequestFeatures;
71-
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
71+
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN};
7272
use crate::ln::msgs::DecodeError;
7373
use crate::offers::invoice::BlindedPayInfo;
7474
use crate::offers::merkle::{SignError, SignFn, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, self};
75+
use crate::offers::nonce::Nonce;
7576
use crate::offers::offer::{Offer, OfferContents, OfferId, OfferTlvStream, OfferTlvStreamRef};
7677
use crate::offers::parse::{Bolt12ParseError, ParsedMessage, Bolt12SemanticError};
7778
use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};

lightning/src/offers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod invoice_error;
2020
mod invoice_macros;
2121
pub mod invoice_request;
2222
pub mod merkle;
23+
pub mod nonce;
2324
pub mod parse;
2425
mod payer;
2526
pub mod refund;

lightning/src/offers/nonce.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! A number used only once.
11+
12+
use core::ops::Deref;
13+
use crate::sign::EntropySource;
14+
15+
#[allow(unused_imports)]
16+
use crate::prelude::*;
17+
18+
/// A 128-bit number used only once.
19+
///
20+
/// Needed when constructing [`Offer::metadata`] and deriving [`Offer::signing_pubkey`] from
21+
/// [`ExpandedKey`]. Must not be reused for any other derivation without first hashing.
22+
///
23+
/// [`Offer::metadata`]: crate::offers::offer::Offer::metadata
24+
/// [`Offer::signing_pubkey`]: crate::offers::offer::Offer::signing_pubkey
25+
/// [`ExpandedKey`]: crate::ln::inbound_payment::ExpandedKey
26+
#[derive(Clone, Copy, Debug, PartialEq)]
27+
pub struct Nonce(pub(crate) [u8; Self::LENGTH]);
28+
29+
impl Nonce {
30+
/// Number of bytes in the nonce.
31+
pub const LENGTH: usize = 16;
32+
33+
/// Creates a `Nonce` from the given [`EntropySource`].
34+
pub fn from_entropy_source<ES: Deref>(entropy_source: ES) -> Self
35+
where
36+
ES::Target: EntropySource,
37+
{
38+
let mut bytes = [0u8; Self::LENGTH];
39+
let rand_bytes = entropy_source.get_secure_random_bytes();
40+
bytes.copy_from_slice(&rand_bytes[..Self::LENGTH]);
41+
42+
Nonce(bytes)
43+
}
44+
45+
/// Returns a slice of the underlying bytes of size [`Nonce::LENGTH`].
46+
pub fn as_slice(&self) -> &[u8] {
47+
&self.0
48+
}
49+
}
50+
51+
impl TryFrom<&[u8]> for Nonce {
52+
type Error = ();
53+
54+
fn try_from(bytes: &[u8]) -> Result<Self, ()> {
55+
if bytes.len() != Self::LENGTH {
56+
return Err(());
57+
}
58+
59+
let mut copied_bytes = [0u8; Self::LENGTH];
60+
copied_bytes.copy_from_slice(bytes);
61+
62+
Ok(Self(copied_bytes))
63+
}
64+
}

lightning/src/offers/offer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ use crate::io;
9090
use crate::blinded_path::BlindedPath;
9191
use crate::ln::channelmanager::PaymentId;
9292
use crate::ln::features::OfferFeatures;
93-
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
93+
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN};
9494
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
9595
use crate::offers::merkle::{TaggedHash, TlvStream};
96+
use crate::offers::nonce::Nonce;
9697
use crate::offers::parse::{Bech32Encode, Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
9798
use crate::offers::signer::{Metadata, MetadataMaterial, self};
9899
use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, WithoutLength, Writeable, Writer};

lightning/src/offers/refund.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ use crate::blinded_path::BlindedPath;
9595
use crate::ln::types::PaymentHash;
9696
use crate::ln::channelmanager::PaymentId;
9797
use crate::ln::features::InvoiceRequestFeatures;
98-
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
98+
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN};
9999
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
100100
use crate::offers::invoice::BlindedPayInfo;
101101
use crate::offers::invoice_request::{InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef};
102+
use crate::offers::nonce::Nonce;
102103
use crate::offers::offer::{OfferTlvStream, OfferTlvStreamRef};
103104
use crate::offers::parse::{Bech32Encode, Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
104105
use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};

lightning/src/offers/signer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ use bitcoin::hashes::sha256::Hash as Sha256;
1616
use bitcoin::secp256k1::{Keypair, PublicKey, Secp256k1, SecretKey, self};
1717
use core::fmt;
1818
use crate::ln::channelmanager::PaymentId;
19-
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
19+
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN};
2020
use crate::offers::merkle::TlvRecord;
21+
use crate::offers::nonce::Nonce;
2122
use crate::util::ser::Writeable;
2223

2324
use crate::prelude::*;

0 commit comments

Comments
 (0)