From ef3c383e3265ecc07f3861c6aed17f7b614494aa Mon Sep 17 00:00:00 2001 From: Jacob Persson <7156+typfel@users.noreply.github.com> Date: Wed, 9 Oct 2024 13:56:15 +0200 Subject: [PATCH] chore: expose e2ei_rotate in the wasm wrapper --- crypto-ffi/bindings/js/CoreCrypto.ts | 41 ++++++++++++++++++++++++++++ crypto-ffi/src/wasm.rs | 20 ++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/crypto-ffi/bindings/js/CoreCrypto.ts b/crypto-ffi/bindings/js/CoreCrypto.ts index 10c3904db..0b9411bc8 100644 --- a/crypto-ffi/bindings/js/CoreCrypto.ts +++ b/crypto-ffi/bindings/js/CoreCrypto.ts @@ -1559,6 +1559,47 @@ export class CoreCrypto { } } + /** + * Creates an update commit which replaces your leaf containing basic credentials with a leaf node containing x509 credentials in the conversation. + * + * NOTE: you can only call this after you've completed the enrollment for an end-to-end identity, calling this without + * a valid end-to-end identity will result in an error. + * + * **CAUTION**: {@link CoreCrypto.commitAccepted} **HAS TO** be called afterward **ONLY IF** the Delivery Service responds + * '200 OK' to the {@link CommitBundle} upload. It will "merge" the commit locally i.e. increment the local group + * epoch, use new encryption secrets etc... + * + * @param conversationId - The ID of the conversation + * + * @returns A {@link CommitBundle} + */ + async e2eiRotate( + conversationId: ConversationId + ): Promise { + try { + const ffiRet: CoreCryptoFfiTypes.CommitBundle = + await CoreCryptoError.asyncMapErr( + this.#cc.e2ei_rotate(conversationId) + ); + + const gi = ffiRet.group_info; + + const ret: CommitBundle = { + welcome: ffiRet.welcome, + commit: ffiRet.commit, + groupInfo: { + encryptionType: gi.encryption_type, + ratchetTreeType: gi.ratchet_tree_type, + payload: gi.payload, + }, + }; + + return ret; + } catch (e) { + throw CoreCryptoError.fromStdError(e as Error); + } + } + /** * Commits the local pending proposals and returns the {@link CommitBundle} object containing what can result from this operation. * diff --git a/crypto-ffi/src/wasm.rs b/crypto-ffi/src/wasm.rs index 6e9879f43..4e769424c 100644 --- a/crypto-ffi/src/wasm.rs +++ b/crypto-ffi/src/wasm.rs @@ -2844,6 +2844,26 @@ impl CoreCrypto { .err_into(), ) } + /// Returns: [`WasmCryptoResult`] + /// + /// see [core_crypto::mls::MlsCentral::e2ei_rotate_all] + pub fn e2ei_rotate(&self, conversation_id: ConversationId) -> Promise { + let this = self.inner.clone(); + future_to_promise( + async move { + let mut central = this.write().await; + let commit = central + .e2ei_rotate(&conversation_id, None) + .await + .map_err(CoreCryptoError::from)?; + + let commit: CommitBundle = commit.try_into()?; + + WasmCryptoResult::Ok(serde_wasm_bindgen::to_value(&commit)?) + } + .err_into(), + ) + } /// see [core_crypto::mls::MlsCentral::e2ei_enrollment_stash] pub fn e2ei_enrollment_stash(&self, enrollment: E2eiEnrollment) -> Promise {