@@ -74,7 +74,8 @@ use crate::{
74
74
} ,
75
75
types:: {
76
76
events:: room_key_withheld:: RoomKeyWithheldEvent , BackupSecrets , CrossSigningSecrets ,
77
- EventEncryptionAlgorithm , MegolmBackupV1Curve25519AesSha2Secrets , SecretsBundle ,
77
+ EventEncryptionAlgorithm , MegolmBackupV1Curve25519AesSha2Secrets , RoomKeyExport ,
78
+ SecretsBundle ,
78
79
} ,
79
80
verification:: VerificationMachine ,
80
81
CrossSigningStatus , OwnUserIdentityData , RoomKeyImportResult ,
@@ -1832,6 +1833,54 @@ impl Store {
1832
1833
from_backup_version : Option < & str > ,
1833
1834
progress_listener : impl Fn ( usize , usize ) ,
1834
1835
) -> Result < RoomKeyImportResult > {
1836
+ let exported_keys: Vec < & ExportedRoomKey > = exported_keys. iter ( ) . collect ( ) ;
1837
+ self . import_sessions_impl ( exported_keys, from_backup_version, progress_listener) . await
1838
+ }
1839
+
1840
+ /// Import the given room keys into our store.
1841
+ ///
1842
+ /// # Arguments
1843
+ ///
1844
+ /// * `exported_keys` - A list of previously exported keys that should be
1845
+ /// imported into our store. If we already have a better version of a key
1846
+ /// the key will *not* be imported.
1847
+ ///
1848
+ /// Returns a tuple of numbers that represent the number of sessions that
1849
+ /// were imported and the total number of sessions that were found in the
1850
+ /// key export.
1851
+ ///
1852
+ /// # Examples
1853
+ ///
1854
+ /// ```no_run
1855
+ /// # use std::io::Cursor;
1856
+ /// # use matrix_sdk_crypto::{OlmMachine, decrypt_room_key_export};
1857
+ /// # use ruma::{device_id, user_id};
1858
+ /// # let alice = user_id!("@alice:example.org");
1859
+ /// # async {
1860
+ /// # let machine = OlmMachine::new(&alice, device_id!("DEVICEID")).await;
1861
+ /// # let export = Cursor::new("".to_owned());
1862
+ /// let exported_keys = decrypt_room_key_export(export, "1234").unwrap();
1863
+ /// machine.store().import_exported_room_keys(exported_keys, |_, _| {}).await.unwrap();
1864
+ /// # };
1865
+ /// ```
1866
+ pub async fn import_exported_room_keys (
1867
+ & self ,
1868
+ exported_keys : Vec < ExportedRoomKey > ,
1869
+ progress_listener : impl Fn ( usize , usize ) ,
1870
+ ) -> Result < RoomKeyImportResult > {
1871
+ self . import_room_keys ( exported_keys, None , progress_listener) . await
1872
+ }
1873
+
1874
+ async fn import_sessions_impl < T > (
1875
+ & self ,
1876
+ room_keys : Vec < T > ,
1877
+ from_backup_version : Option < & str > ,
1878
+ progress_listener : impl Fn ( usize , usize ) ,
1879
+ ) -> Result < RoomKeyImportResult >
1880
+ where
1881
+ T : TryInto < InboundGroupSession > + RoomKeyExport + Copy ,
1882
+ T :: Error : Debug ,
1883
+ {
1835
1884
let mut sessions = Vec :: new ( ) ;
1836
1885
1837
1886
async fn new_session_better (
@@ -1845,11 +1894,11 @@ impl Store {
1845
1894
}
1846
1895
}
1847
1896
1848
- let total_count = exported_keys . len ( ) ;
1897
+ let total_count = room_keys . len ( ) ;
1849
1898
let mut keys = BTreeMap :: new ( ) ;
1850
1899
1851
- for ( i, key) in exported_keys . into_iter ( ) . enumerate ( ) {
1852
- match InboundGroupSession :: from_export ( & key) {
1900
+ for ( i, key) in room_keys . into_iter ( ) . enumerate ( ) {
1901
+ match key. try_into ( ) {
1853
1902
Ok ( session) => {
1854
1903
let old_session = self
1855
1904
. inner
@@ -1875,9 +1924,9 @@ impl Store {
1875
1924
}
1876
1925
Err ( e) => {
1877
1926
warn ! (
1878
- sender_key= key. sender_key. to_base64( ) ,
1879
- room_id = ?key. room_id,
1880
- session_id = key. session_id,
1927
+ sender_key = key. sender_key( ) . to_base64( ) ,
1928
+ room_id = ?key. room_id( ) ,
1929
+ session_id = key. session_id( ) ,
1881
1930
error = ?e,
1882
1931
"Couldn't import a room key from a file export."
1883
1932
) ;
@@ -1896,40 +1945,6 @@ impl Store {
1896
1945
Ok ( RoomKeyImportResult :: new ( imported_count, total_count, keys) )
1897
1946
}
1898
1947
1899
- /// Import the given room keys into our store.
1900
- ///
1901
- /// # Arguments
1902
- ///
1903
- /// * `exported_keys` - A list of previously exported keys that should be
1904
- /// imported into our store. If we already have a better version of a key
1905
- /// the key will *not* be imported.
1906
- ///
1907
- /// Returns a tuple of numbers that represent the number of sessions that
1908
- /// were imported and the total number of sessions that were found in the
1909
- /// key export.
1910
- ///
1911
- /// # Examples
1912
- ///
1913
- /// ```no_run
1914
- /// # use std::io::Cursor;
1915
- /// # use matrix_sdk_crypto::{OlmMachine, decrypt_room_key_export};
1916
- /// # use ruma::{device_id, user_id};
1917
- /// # let alice = user_id!("@alice:example.org");
1918
- /// # async {
1919
- /// # let machine = OlmMachine::new(&alice, device_id!("DEVICEID")).await;
1920
- /// # let export = Cursor::new("".to_owned());
1921
- /// let exported_keys = decrypt_room_key_export(export, "1234").unwrap();
1922
- /// machine.store().import_exported_room_keys(exported_keys, |_, _| {}).await.unwrap();
1923
- /// # };
1924
- /// ```
1925
- pub async fn import_exported_room_keys (
1926
- & self ,
1927
- exported_keys : Vec < ExportedRoomKey > ,
1928
- progress_listener : impl Fn ( usize , usize ) ,
1929
- ) -> Result < RoomKeyImportResult > {
1930
- self . import_room_keys ( exported_keys, None , progress_listener) . await
1931
- }
1932
-
1933
1948
pub ( crate ) fn crypto_store ( & self ) -> Arc < CryptoStoreWrapper > {
1934
1949
self . inner . store . clone ( )
1935
1950
}
0 commit comments