vodozemac supports serializing internal states into a "pickle" to support features like device dehydration. There are two formats:
- Legacy pickles: A binary format used by
libolm. vodozemac supports unpickling these for interoperability, but not creating them. - Modern pickles: Uses Serde. The exact format is not mandated, but you can use any Serde-supported format (like JSON).
Important: Serialization Pattern
Core structs like olm::Account, olm::Session, megolm::GroupSession, and megolm::InboundGroupSession do not implement serde::Serialize directly. To serialize them, you must first call the .pickle() method, which returns a special serializable struct. To restore the original struct, call .unpickle() or convert the pickle struct back into the original type.
Example: Encrypting and restoring an Olm Account
use anyhow::Result;
use vodozemac::olm::{Account, AccountPickle};
const PICKLE_KEY: [u8; 32] = [0u8; 32];
fn main() -> Result<()>{
let mut account = Account::new();
account.generate_one_time_keys(10);
account.generate_fallback_key();
// Serialize and encrypt the account state
let pickle = account.pickle().encrypt(&PICKLE_KEY);
// Restore the account from the encrypted pickle
let account2: Account = AccountPickle::from_encrypted(&pickle, &PICKLE_KEY)?.into();
assert_eq!(account.identity_keys(), account2.identity_keys());
Ok(())
}