Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into publication
Browse files Browse the repository at this point in the history
  • Loading branch information
PaarthShah committed Dec 2, 2024
2 parents 785d3a6 + ed07792 commit 4218e8a
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 220 deletions.
7 changes: 7 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
edition = "2021"
max_width = 100
comment_width = 80
wrap_comments = true
imports_granularity = "Crate"
use_small_heuristics = "Max"
group_imports = "StdExternalCrate"
55 changes: 38 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vodozemac-python"
version = "0.7.0"
version = "0.8.1"
edition = "2021"
repository = "https://github.com/matrix-nio/vodozemac-python"
homepage = "https://github.com/matrix-nio/vodozemac-python"
Expand All @@ -12,18 +12,14 @@ crate-type = ["cdylib"]

[dependencies]
paste = "1.0.15"
thiserror = "1.0.63"
thiserror = "2.0.3"
[dependencies.vodozemac]
git = "https://github.com/matrix-org/vodozemac.git"
rev = "12f9036bf7f2536c172273602afcdc9aeddf8cf7"
version = "0.8.1"
features = ["insecure-pk-encryption"]

[package.metadata.maturin]
name = "vodozemac"

[dependencies.pyo3]
version = "0.22.2"
version = "0.23.2"
features = ["extension-module"]

[features]
gil-refs = []
42 changes: 11 additions & 31 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ use pyo3::{
};
use vodozemac::olm::SessionConfig;

use super::session::Session;
use crate::{
convert_to_pybytes,
error::{LibolmPickleError, PickleError, SessionError},
types::{Curve25519PublicKey, Ed25519PublicKey, Ed25519Signature, PreKeyMessage},
};

use super::session::Session;

#[pyclass]
pub struct Account {
inner: vodozemac::olm::Account,
Expand All @@ -23,9 +22,7 @@ pub struct Account {
impl Account {
#[new]
fn new() -> Self {
Self {
inner: vodozemac::olm::Account::new(),
}
Self { inner: vodozemac::olm::Account::new() }
}

#[classmethod]
Expand All @@ -34,9 +31,8 @@ impl Account {
pickle: &str,
pickle_key: &[u8],
) -> Result<Self, PickleError> {
let pickle_key: &[u8; 32] = pickle_key
.try_into()
.map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;
let pickle_key: &[u8; 32] =
pickle_key.try_into().map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;

let pickle = vodozemac::olm::AccountPickle::from_encrypted(pickle, pickle_key)?;

Expand All @@ -57,9 +53,8 @@ impl Account {
}

fn pickle(&self, pickle_key: &[u8]) -> Result<String, PickleError> {
let pickle_key: &[u8; 32] = pickle_key
.try_into()
.map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;
let pickle_key: &[u8; 32] =
pickle_key.try_into().map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;

Ok(self.inner.pickle().encrypt(pickle_key))
}
Expand All @@ -80,11 +75,7 @@ impl Account {

#[getter]
fn one_time_keys(&self) -> HashMap<String, Curve25519PublicKey> {
self.inner
.one_time_keys()
.into_iter()
.map(|(k, v)| (k.to_base64(), v.into()))
.collect()
self.inner.one_time_keys().into_iter().map(|(k, v)| (k.to_base64(), v.into())).collect()
}

#[getter]
Expand All @@ -98,11 +89,7 @@ impl Account {

#[getter]
fn fallback_key(&self) -> HashMap<String, Curve25519PublicKey> {
self.inner
.fallback_key()
.into_iter()
.map(|(k, v)| (k.to_base64(), v.into()))
.collect()
self.inner.fallback_key().into_iter().map(|(k, v)| (k.to_base64(), v.into())).collect()
}

fn generate_fallback_key(&mut self) {
Expand Down Expand Up @@ -132,15 +119,8 @@ impl Account {
identity_key: &Curve25519PublicKey,
message: &PreKeyMessage,
) -> Result<(Session, Py<PyBytes>), SessionError> {
let result = self
.inner
.create_inbound_session(identity_key.inner, &message.inner)?;

Ok((
Session {
inner: result.session,
},
convert_to_pybytes(result.plaintext.as_slice()),
))
let result = self.inner.create_inbound_session(identity_key.inner, &message.inner)?;

Ok((Session { inner: result.session }, convert_to_pybytes(result.plaintext.as_slice())))
}
}
28 changes: 6 additions & 22 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,10 @@ create_error!(vodozemac::megolm::SessionKeyDecodeError, SessionKeyDecode);
create_error!(vodozemac::DecodeError, Decode);

pyo3::create_exception!(module, PickleException, pyo3::exceptions::PyValueError);
pyo3::create_exception!(
module,
SessionCreationException,
pyo3::exceptions::PyValueError
);
pyo3::create_exception!(module, SessionCreationException, pyo3::exceptions::PyValueError);
pyo3::create_exception!(module, SasException, pyo3::exceptions::PyValueError);
pyo3::create_exception!(
module,
OlmDecryptionException,
pyo3::exceptions::PyValueError
);
pyo3::create_exception!(
module,
MegolmDecryptionException,
pyo3::exceptions::PyValueError
);
pyo3::create_exception!(module, OlmDecryptionException, pyo3::exceptions::PyValueError);
pyo3::create_exception!(module, MegolmDecryptionException, pyo3::exceptions::PyValueError);

#[derive(Debug, Error)]
pub enum MegolmDecryptionError {
Expand Down Expand Up @@ -140,8 +128,8 @@ impl From<PickleError> for PyErr {
}
}

/// An error type describing failures which can happen during the use of `PkEncryption`
/// and `PkDecryption` objects.
/// An error type describing failures which can happen during the use of
/// `PkEncryption` and `PkDecryption` objects.
#[derive(Debug, Error)]
pub enum PkEncryptionError {
#[error("The key doesn't have the correct size, got {0}, expected 32 bytes")]
Expand All @@ -150,11 +138,7 @@ pub enum PkEncryptionError {
Decode(#[from] vodozemac::pk_encryption::Error),
}

pyo3::create_exception!(
module,
PkInvalidKeySizeException,
pyo3::exceptions::PyValueError
);
pyo3::create_exception!(module, PkInvalidKeySizeException, pyo3::exceptions::PyValueError);
pyo3::create_exception!(module, PkDecodeException, pyo3::exceptions::PyValueError);

impl From<PkEncryptionError> for PyErr {
Expand Down
Loading

0 comments on commit 4218e8a

Please sign in to comment.