7
7
// You may not use this file except in accordance with one or both of these
8
8
// licenses.
9
9
10
- //! `OurPeerStorage` enables versioned and timestamped storage of serialized channel data.
10
+ //! `OurPeerStorage` enables versioned storage of serialized channel data.
11
11
//! It supports encryption and decryption to maintain data integrity and security during
12
12
//! transmission.
13
13
14
14
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 ;
20
15
use crate :: prelude:: * ;
21
16
22
- use bitcoin:: secp256k1:: SecretKey ;
23
-
24
17
/// [`OurPeerStorage`] is used to store channel information that allows for the creation of a
25
18
/// `peer_storage` backup. It includes versioning and timestamping for comparison between
26
19
/// instances of [`OurPeerStorage`].
@@ -30,8 +23,7 @@ use bitcoin::secp256k1::SecretKey;
30
23
///
31
24
/// # Key Methods
32
25
/// - `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.
35
27
///
36
28
/// # Usage
37
29
/// This structure can be used for securely managing and exchanging peer storage backups. It
@@ -47,14 +39,16 @@ use bitcoin::secp256k1::SecretKey;
47
39
/// ```
48
40
#[ derive( PartialEq ) ]
49
41
pub struct OurPeerStorage {
50
- version : u8 ,
51
- ser_channels : Vec < u8 > ,
42
+ encrypted_data : Vec < u8 > ,
52
43
}
53
44
54
45
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 ( )
58
52
}
59
53
60
54
/// Creates a serialised representation of [`OurPeerStorage`] from the given `ser_channels` data.
@@ -63,31 +57,28 @@ impl OurPeerStorage {
63
57
/// (serialised channel information), and returns a serialised [`OurPeerStorage`] as a `Vec<u8>`.
64
58
///
65
59
/// 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 {
69
61
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 ] ;
73
62
74
- let plaintext = & peer_storage. 0 [ ..] ;
63
+ let mut res = vec ! [ 0 ; ser_channels. len( ) + 16 ] ;
64
+
65
+ let plaintext = & ser_channels[ ..] ;
75
66
let mut nonce = [ 0 ; 12 ] ;
76
67
nonce[ 4 ..] . copy_from_slice ( & n. to_le_bytes ( ) [ ..] ) ;
77
68
78
- let mut chacha = ChaCha20Poly1305RFC :: new ( & key. secret_bytes ( ) , & nonce, b"" ) ;
69
+ let mut chacha = ChaCha20Poly1305RFC :: new ( & key, & nonce, b"" ) ;
79
70
let mut tag = [ 0 ; 16 ] ;
80
71
chacha. encrypt ( plaintext, & mut res[ 0 ..plaintext. len ( ) ] , & mut tag) ;
81
72
res[ plaintext. len ( ) ..] . copy_from_slice ( & tag) ;
82
- res
73
+
74
+ Self { encrypted_data : res }
83
75
}
84
76
85
77
/// Decrypt `OurPeerStorage` using the `key`, result is stored inside the `res`.
86
78
/// 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 > , ( ) > {
90
80
const MIN_CYPHERTEXT_LEN : usize = 16 ;
81
+ let cyphertext = & self . encrypted_data [ ..] ;
91
82
92
83
let mut res = vec ! [ 0 ; cyphertext. len( ) - 16 ] ;
93
84
// Ensure the cyphertext is at least as large as the MIN_CYPHERTEXT_LEN.
@@ -99,7 +90,7 @@ impl OurPeerStorage {
99
90
let mut nonce = [ 0 ; 12 ] ;
100
91
nonce[ 4 ..] . copy_from_slice ( & n. to_le_bytes ( ) [ ..] ) ;
101
92
102
- let mut chacha = ChaCha20Poly1305RFC :: new ( & key. secret_bytes ( ) , & nonce, b"" ) ;
93
+ let mut chacha = ChaCha20Poly1305RFC :: new ( & key, & nonce, b"" ) ;
103
94
if chacha
104
95
. variable_time_decrypt (
105
96
& cyphertext[ 0 ..cyphertext. len ( ) - 16 ] ,
@@ -110,27 +101,7 @@ impl OurPeerStorage {
110
101
{
111
102
return Err ( ( ) ) ;
112
103
}
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) ?;
132
104
133
- let ps = OurPeerStorage { version : ver, ser_channels } ;
134
- Ok ( ps)
105
+ Ok ( res)
135
106
}
136
107
}
0 commit comments