Skip to content

Commit

Permalink
try other ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
beltram committed Nov 24, 2023
1 parent 2bf99b1 commit ef1f926
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 87 deletions.
12 changes: 3 additions & 9 deletions crypto-ffi/bindings/js/CoreCrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1139,26 +1139,20 @@ export class CoreCrypto {
* epoch, use new encryption secrets etc...
*
* @param conversationId - The ID of the conversation
* @param clients - Array of {@link Invitee} (which are Client ID / KeyPackage pairs)
* @param keyPackages - KeyPackages of the new clients to add
*
* @returns A {@link CommitBundle}
*/
async addClientsToConversation(
conversationId: ConversationId,
clients: Invitee[]
keyPackages: Uint8Array[]
): Promise<MemberAddedMessages> {
try {
const ffiClients = clients.map(
(invitee) => new InviteeFfi(invitee.id, invitee.kp)
);

const ffiRet: CoreCryptoFfiTypes.MemberAddedMessages = await CoreCryptoError.asyncMapErr(this.#cc.add_clients_to_conversation(
conversationId,
ffiClients
keyPackages
));

ffiClients.forEach(c => c.free());

const gi = ffiRet.group_info;

const ret: MemberAddedMessages = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,11 @@ class MLSClient(private val cc: com.wire.crypto.CoreCrypto) {
* It will "merge" the commit locally i.e. increment the local group epoch, use new encryption secrets etc...
*
* @param id conversation identifier
* @param members pairs of client identifier and its KeyPackage
* @param KeyPackages of the new clients to add
* @return a [CommitBundle] to upload to the backend and if it succeeds call [commitAccepted]
*/
suspend fun addMember(id: MLSGroupId, members: Map<ClientId, MLSKeyPackage>): CommitBundle {
val invitees = members.map { (clientId, kp) -> com.wire.crypto.Invitee(clientId.lower(), kp.lower()) }
return cc.addClientsToConversation(id.lower(), invitees).lift()
suspend fun addMember(id: MLSGroupId, keyPackages: List<MLSKeyPackage>): CommitBundle {
return cc.addClientsToConversation(id.lower(), keyPackages.map { it.lower() }).lift()
}

/**
Expand Down
8 changes: 3 additions & 5 deletions crypto-ffi/bindings/swift/Sources/CoreCrypto/CoreCrypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -755,12 +755,10 @@ public class CoreCryptoWrapper {
/// epoch, use new encryption secrets etc...
///
/// - parameter conversationId: conversation identifier
/// - parameter clients: Array of ``Invitee`` (which are Client ID / KeyPackage pairs)
/// - parameter keyPackages: of the clients to add
/// - returns: A ``CommitBundle`` byte array to fan out to the Delivery Service
public func addClientsToConversation(conversationId: ConversationId, clients: [Invitee]) async throws -> MemberAddedMessages {
return try await self.coreCrypto.addClientsToConversation(conversationId: conversationId, clients: clients.map({ (invitee) -> CoreCryptoSwift.Invitee in
return invitee.convert()
})).convertTo()
public func addClientsToConversation(conversationId: ConversationId, keyPackages: [[UInt8]]) async throws -> MemberAddedMessages {
return try await self.coreCrypto.addClientsToConversation(conversationId: conversationId, keyPackages: keyPackages).convertTo()
}

/// Removes the provided clients from a conversation; Assuming those clients exist and the current client is allowed
Expand Down
82 changes: 13 additions & 69 deletions crypto-ffi/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,66 +777,6 @@ impl From<core_crypto::prelude::WireIdentity> for WireIdentity {
}
}

#[wasm_bindgen]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
/// see [core_crypto::prelude::ConversationMember]
pub struct Invitee {
id: Vec<u8>,
kp: Vec<u8>,
}

#[wasm_bindgen]
impl Invitee {
#[wasm_bindgen(constructor)]
pub fn new(id: Uint8Array, kp: Uint8Array) -> Self {
Self {
id: id.to_vec(),
kp: kp.to_vec(),
}
}

#[wasm_bindgen(getter)]
pub fn id(&self) -> Uint8Array {
Uint8Array::from(&*self.id)
}

#[wasm_bindgen(getter)]
pub fn kp(&self) -> Uint8Array {
Uint8Array::from(&*self.kp)
}
}

impl Invitee {
#[inline(always)]
fn group_to_conversation_member(
clients: Vec<Self>,
backend: &MlsCryptoProvider,
) -> WasmCryptoResult<Vec<ConversationMember>> {
Ok(clients
.into_iter()
.try_fold(
HashMap::new(),
|mut acc, c| -> WasmCryptoResult<HashMap<ClientId, ConversationMember>> {
let client_id: ClientId = c.id.into();
if let Some(member) = acc.get_mut(&client_id) {
member
.add_keypackage(c.kp.to_vec(), backend)
.map_err(CoreCryptoError::from)?;
} else {
acc.insert(
client_id.clone(),
ConversationMember::new_raw(client_id, c.kp.to_vec(), backend)
.map_err(CoreCryptoError::from)?,
);
}
Ok(acc)
},
)?
.into_values()
.collect::<Vec<ConversationMember>>())
}
}

#[wasm_bindgen]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
/// see [core_crypto::prelude::MlsConversationConfiguration]
Expand Down Expand Up @@ -1566,22 +1506,26 @@ impl CoreCrypto {
/// Returns: [`WasmCryptoResult<Option<MemberAddedMessages>>`]
///
/// see [core_crypto::mls::MlsCentral::add_members_to_conversation]
pub fn add_clients_to_conversation(&self, conversation_id: ConversationId, clients: Box<[JsValue]>) -> Promise {
pub fn add_clients_to_conversation(
&self,
conversation_id: ConversationId,
key_packages: Box<[Uint8Array]>,
) -> Promise {
let this = self.inner.clone();

future_to_promise(
async move {
let invitees = clients
.iter()
.cloned()
.map(|js_client| Ok(serde_wasm_bindgen::from_value(js_client)?))
.collect::<WasmCryptoResult<Vec<Invitee>>>()?;
let key_packages = key_packages
.into_iter()

Check failure on line 1519 in crypto-ffi/src/wasm.rs

View workflow job for this annotation

GitHub Actions / build

this `.into_iter()` call is equivalent to `.iter()` and will not consume the `slice`
.map(|kp| {
KeyPackageIn::tls_deserialize_bytes(kp.to_vec())
.map_err(|e| CoreCryptoError(WasmError::CryptoError(CryptoError::MlsError(e.into()))))
})
.collect::<CoreCryptoResult<Vec<_>>>()?;

let mut central = this.write().await;
let backend = central.provider();
let mut members = Invitee::group_to_conversation_member(invitees, backend)?;
let commit = central
.add_members_to_conversation(&conversation_id, &mut members)
.add_members_to_conversation(&conversation_id, key_packages)
.await?;
let commit: MemberAddedMessages = commit.try_into()?;
WasmCryptoResult::Ok(serde_wasm_bindgen::to_value(&commit)?)
Expand Down

0 comments on commit ef1f926

Please sign in to comment.