Skip to content

Commit 42af2ab

Browse files
committed
fixup: Add OurPeerStorage for serialized Peer Storage backups
1 parent 57341a1 commit 42af2ab

File tree

1 file changed

+20
-49
lines changed

1 file changed

+20
-49
lines changed

lightning/src/ln/our_peer_storage.rs

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,13 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10-
//! `OurPeerStorage` enables versioned and timestamped storage of serialized channel data.
10+
//! `OurPeerStorage` enables versioned storage of serialized channel data.
1111
//! It supports encryption and decryption to maintain data integrity and security during
1212
//! transmission.
1313
1414
use crate::crypto::chacha20poly1305rfc::ChaCha20Poly1305RFC;
15-
16-
use crate::util::ser::{Readable, VecWriter, Writeable, Writer};
17-
18-
use crate::io::{self, Error};
19-
use crate::ln::msgs::DecodeError;
2015
use crate::prelude::*;
2116

22-
use bitcoin::secp256k1::SecretKey;
23-
2417
/// [`OurPeerStorage`] is used to store channel information that allows for the creation of a
2518
/// `peer_storage` backup. It includes versioning and timestamping for comparison between
2619
/// instances of [`OurPeerStorage`].
@@ -30,8 +23,7 @@ use bitcoin::secp256k1::SecretKey;
3023
///
3124
/// # Key Methods
3225
/// - `create_from_data`: Returns an encrypted [`OurPeerStorage`] instance created from the provided data.
33-
/// - `get_ser_channels`: Retrieves the serialized channel data.
34-
/// - `decrypt_our_peer_storage`: Decrypts the ciphertext using the key and returns [`OurPeerStorage`] populated with data.
26+
/// - `decrypt_our_peer_storage`: Decrypts the [`OurPeerStorage::encrypted_data`] using the key and returns decrypted data.
3527
///
3628
/// # Usage
3729
/// This structure can be used for securely managing and exchanging peer storage backups. It
@@ -47,14 +39,16 @@ use bitcoin::secp256k1::SecretKey;
4739
/// ```
4840
#[derive(PartialEq)]
4941
pub struct OurPeerStorage {
50-
version: u8,
51-
ser_channels: Vec<u8>,
42+
encrypted_data: Vec<u8>,
5243
}
5344

5445
impl OurPeerStorage {
55-
/// Get `ser_channels` field from [`OurPeerStorage`]
56-
pub fn get_ser_channels(&self) -> Vec<u8> {
57-
self.ser_channels.clone()
46+
pub fn new(encrypted_data: Vec<u8>) -> Self {
47+
Self { encrypted_data }
48+
}
49+
50+
pub fn encrypted_data(&self) -> Vec<u8> {
51+
self.encrypted_data.clone()
5852
}
5953

6054
/// Creates a serialised representation of [`OurPeerStorage`] from the given `ser_channels` data.
@@ -63,31 +57,28 @@ impl OurPeerStorage {
6357
/// (serialised channel information), and returns a serialised [`OurPeerStorage`] as a `Vec<u8>`.
6458
///
6559
/// The resulting serialised data is intended to be directly used for transmission to the peers.
66-
pub fn create_from_data(key: SecretKey, ser_channels: Vec<u8>) -> Vec<u8> {
67-
let our_peer_storage = Self { version: 1, ser_channels };
68-
60+
pub fn create_from_data(key: [u8; 32], ser_channels: Vec<u8>) -> OurPeerStorage {
6961
let n = 0u64;
70-
let mut peer_storage = VecWriter(Vec::new());
71-
our_peer_storage.write(&mut peer_storage).unwrap();
72-
let mut res = vec![0; peer_storage.0.len() + 16];
7362

74-
let plaintext = &peer_storage.0[..];
63+
let mut res = vec![0; ser_channels.len() + 16];
64+
65+
let plaintext = &ser_channels[..];
7566
let mut nonce = [0; 12];
7667
nonce[4..].copy_from_slice(&n.to_le_bytes()[..]);
7768

78-
let mut chacha = ChaCha20Poly1305RFC::new(&key.secret_bytes(), &nonce, b"");
69+
let mut chacha = ChaCha20Poly1305RFC::new(&key, &nonce, b"");
7970
let mut tag = [0; 16];
8071
chacha.encrypt(plaintext, &mut res[0..plaintext.len()], &mut tag);
8172
res[plaintext.len()..].copy_from_slice(&tag);
82-
res
73+
74+
Self { encrypted_data: res }
8375
}
8476

8577
/// Decrypt `OurPeerStorage` using the `key`, result is stored inside the `res`.
8678
/// Returns an error if the the `cyphertext` is not correct.
87-
pub fn decrypt_our_peer_storage(
88-
cyphertext: &[u8], key: SecretKey,
89-
) -> Result<OurPeerStorage, ()> {
79+
pub fn decrypt_our_peer_storage(&self, key: [u8; 32]) -> Result<Vec<u8>, ()> {
9080
const MIN_CYPHERTEXT_LEN: usize = 16;
81+
let cyphertext = &self.encrypted_data[..];
9182

9283
let mut res = vec![0; cyphertext.len() - 16];
9384
// Ensure the cyphertext is at least as large as the MIN_CYPHERTEXT_LEN.
@@ -99,7 +90,7 @@ impl OurPeerStorage {
9990
let mut nonce = [0; 12];
10091
nonce[4..].copy_from_slice(&n.to_le_bytes()[..]);
10192

102-
let mut chacha = ChaCha20Poly1305RFC::new(&key.secret_bytes(), &nonce, b"");
93+
let mut chacha = ChaCha20Poly1305RFC::new(&key, &nonce, b"");
10394
if chacha
10495
.variable_time_decrypt(
10596
&cyphertext[0..cyphertext.len() - 16],
@@ -110,27 +101,7 @@ impl OurPeerStorage {
110101
{
111102
return Err(());
112103
}
113-
let our_peer_storage =
114-
<OurPeerStorage as Readable>::read(&mut ::bitcoin::io::Cursor::new(res)).unwrap();
115-
116-
Ok(our_peer_storage)
117-
}
118-
}
119-
120-
impl Writeable for OurPeerStorage {
121-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
122-
write_ver_prefix!(writer, self.version, 1);
123-
self.ser_channels.write(writer)?;
124-
Ok(())
125-
}
126-
}
127-
128-
impl Readable for OurPeerStorage {
129-
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
130-
let ver = read_ver_prefix!(reader, 1u8);
131-
let ser_channels = <Vec<u8> as Readable>::read(reader)?;
132104

133-
let ps = OurPeerStorage { version: ver, ser_channels };
134-
Ok(ps)
105+
Ok(res)
135106
}
136107
}

0 commit comments

Comments
 (0)