Skip to content

Commit

Permalink
Update Node and WASM bindings (#1313)
Browse files Browse the repository at this point in the history
* Add installation_id_bytes to Node client

* Prepare node bindings release

* Add installation_id_bytes to WASM client

* Add signature methods to WASM client

* Prepare WASM bindings release

* Clippy fix
  • Loading branch information
rygine authored Nov 21, 2024
1 parent c7d8381 commit ed350d2
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
4 changes: 4 additions & 0 deletions bindings_node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @xmtp/node-bindings

## 0.0.21

- Added `installation_id_bytes` to `Client`

## 0.0.20

- Fixed argument types for new signing methods
Expand Down
2 changes: 1 addition & 1 deletion bindings_node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xmtp/node-bindings",
"version": "0.0.20",
"version": "0.0.21",
"repository": {
"type": "git",
"url": "git+https://[email protected]/xmtp/libxmtp.git",
Expand Down
5 changes: 5 additions & 0 deletions bindings_node/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ impl Client {
ed25519_public_key_to_address(self.inner_client.installation_public_key().as_slice())
}

#[napi]
pub fn installation_id_bytes(&self) -> Uint8Array {
self.inner_client.installation_public_key().into()
}

#[napi]
pub async fn can_message(&self, account_addresses: Vec<String>) -> Result<HashMap<String, bool>> {
let results: HashMap<String, bool> = self
Expand Down
5 changes: 5 additions & 0 deletions bindings_wasm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# @xmtp/wasm-bindings

## 0.0.6

- Added `installation_id_bytes` to `Client`
- Added `sign_with_installation_key`, `verify_signed_with_installation_key`, and `verify_signed_with_public_key` to `Client`

## 0.0.5

- Filtered out group membership messages from DM groups
Expand Down
2 changes: 1 addition & 1 deletion bindings_wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xmtp/wasm-bindings",
"version": "0.0.5",
"version": "0.0.6",
"type": "module",
"license": "MIT",
"description": "WASM bindings for the libXMTP rust library",
Expand Down
52 changes: 52 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 @@ -203,6 +204,11 @@ impl Client {
ed25519_public_key_to_address(self.inner_client.installation_public_key().as_slice())
}

#[wasm_bindgen(getter, js_name = installationIdBytes)]
pub fn installation_id_bytes(&self) -> Uint8Array {
Uint8Array::from(self.inner_client.installation_public_key().as_slice())
}

#[wasm_bindgen(js_name = canMessage)]
pub async fn can_message(&self, account_addresses: Vec<String>) -> Result<JsValue, JsError> {
let results: HashMap<String, bool> = self
Expand Down Expand Up @@ -278,4 +284,50 @@ 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."))?;

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

0 comments on commit ed350d2

Please sign in to comment.