Skip to content

Commit

Permalink
Add signature methods to WASM client
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Nov 21, 2024
1 parent b4ab79b commit 8bc72ba
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions bindings_wasm/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use wasm_bindgen::JsValue;
use xmtp_api_http::XmtpHttpApiClient;
use xmtp_cryptography::signature::ed25519_public_key_to_address;
use xmtp_id::associations::builder::SignatureRequest;
use xmtp_id::associations::verify_signed_with_public_context;
use xmtp_mls::builder::ClientBuilder;
use xmtp_mls::groups::scoped_client::ScopedGroupClient;
use xmtp_mls::identity::IdentityStrategy;
Expand Down Expand Up @@ -283,4 +284,52 @@ impl Client {
pub fn conversations(&self) -> Conversations {
Conversations::new(self.inner_client.clone())
}

#[wasm_bindgen(js_name = signWithInstallationKey)]
pub fn sign_with_installation_key(&self, signature_text: String) -> Result<Uint8Array, JsError> {
let result = self
.inner_client
.context()
.sign_with_public_context(signature_text)
.map_err(|e| JsError::new(format!("{}", e).as_str()))?;

Ok(Uint8Array::from(result.as_slice()))
}

#[wasm_bindgen(js_name = verifySignedWithInstallationKey)]
pub fn verify_signed_with_installation_key(
&self,
signature_text: String,
signature_bytes: Uint8Array,
) -> Result<(), JsError> {
let public_key = self.inner_client().installation_public_key();
self.verify_signed_with_public_key(
signature_text,
signature_bytes,
Uint8Array::from(public_key.as_slice()),
)
}

#[wasm_bindgen(js_name = verifySignedWithPublicKey)]
pub fn verify_signed_with_public_key(
&self,
signature_text: String,
signature_bytes: Uint8Array,
public_key: Uint8Array,
) -> Result<(), JsError> {
let signature_bytes = signature_bytes.to_vec();
let signature_bytes: [u8; 64] = signature_bytes
.try_into()
.map_err(|_| JsError::new("signature_bytes is not 64 bytes long."))?;

let public_key = public_key.to_vec();
let public_key: [u8; 32] = public_key
.try_into()
.map_err(|_| JsError::new("public_key is not 32 bytes long."))?;

Ok(
verify_signed_with_public_context(signature_text, &signature_bytes, &public_key)
.map_err(|e| JsError::new(format!("{}", e).as_str()))?,
)
}
}

0 comments on commit 8bc72ba

Please sign in to comment.