Skip to content

Commit

Permalink
This is it on a high level
Browse files Browse the repository at this point in the history
  • Loading branch information
codabrink committed Nov 27, 2024
1 parent 6ff8ae4 commit 07c4b2f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 16 deletions.
9 changes: 7 additions & 2 deletions bindings_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate-type = ["cdylib"]

[dependencies]
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
futures.workspace = true
hex.workspace = true
napi = { version = "2.12.2", default-features = false, features = [
"napi4",
Expand All @@ -17,14 +18,18 @@ napi = { version = "2.12.2", default-features = false, features = [
napi-derive = "2.12.2"
prost.workspace = true
tokio = { workspace = true, features = ["sync"] }
tracing-subscriber = { workspace = true, features = ["env-filter", "fmt", "json", "chrono"] }
tracing-subscriber = { workspace = true, features = [
"env-filter",
"fmt",
"json",
"chrono",
] }
tracing.workspace = true
xmtp_api_grpc = { path = "../xmtp_api_grpc" }
xmtp_cryptography = { path = "../xmtp_cryptography" }
xmtp_id = { path = "../xmtp_id" }
xmtp_mls = { path = "../xmtp_mls" }
xmtp_proto = { path = "../xmtp_proto", features = ["proto_full"] }
futures.workspace = true

[build-dependencies]
napi-build = "2.0.1"
Expand Down
76 changes: 73 additions & 3 deletions bindings_node/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ use tracing_subscriber::{fmt, prelude::*};
pub use xmtp_api_grpc::grpc_api_helper::Client as TonicApiClient;
use xmtp_cryptography::signature::ed25519_public_key_to_address;
use xmtp_id::associations::builder::SignatureRequest;
use xmtp_id::associations::MemberIdentifier;
use xmtp_id::associations::{AssociationState, IdentityAction, MemberIdentifier};
use xmtp_id::scw_verifier::MultiSmartContractSignatureVerifier;
use xmtp_mls::api::{ApiClientWrapper, GetIdentityUpdatesV2Filter};
use xmtp_mls::builder::ClientBuilder;
use xmtp_mls::groups::scoped_client::LocalScopedGroupClient;
use xmtp_mls::identity::IdentityStrategy;
use xmtp_mls::storage::{EncryptedMessageStore, EncryptionKey, StorageOption};
use xmtp_mls::retry::Retry;
use xmtp_mls::storage::{association_state, EncryptedMessageStore, EncryptionKey, StorageOption};
use xmtp_mls::Client as MlsClient;
use xmtp_proto::xmtp::mls::message_contents::DeviceSyncKind;

Expand Down Expand Up @@ -131,7 +134,7 @@ pub async fn create_client(
log_options: Option<LogOptions>,
) -> Result<Client> {
init_logging(log_options.unwrap_or_default())?;
let api_client = TonicApiClient::create(host.clone(), is_secure)
let api_client = TonicApiClient::create(&host, is_secure)
.await
.map_err(|_| Error::from_reason("Error creating Tonic API client"))?;

Expand Down Expand Up @@ -343,3 +346,70 @@ impl Client {
Ok(association_state.get(identifier).is_some())
}
}

pub async fn is_installation_authorized(
host: String,
inbox_id: String,
installation_id: Uint8Array,
) -> Result<bool> {
is_member_of_association_state(
&host,
&inbox_id,
&MemberIdentifier::Installation(installation_id.to_vec()),
)
.await
}

pub async fn is_address_authorized(
host: String,
inbox_id: String,
address: String,
) -> Result<bool> {
is_member_of_association_state(&host, &inbox_id, &MemberIdentifier::Address(address)).await
}

async fn is_member_of_association_state(
host: &str,
inbox_id: &str,
identifier: &MemberIdentifier,
) -> Result<bool> {
let api_client = TonicApiClient::create(host, true)
.await
.map_err(ErrorWrapper::from)?;
let wrapper = ApiClientWrapper::new(Arc::new(api_client), Retry::default());

let filters = vec![GetIdentityUpdatesV2Filter {
inbox_id: inbox_id.to_string(),
sequence_id: None,
}];
let mut updates = wrapper
.get_identity_updates_v2(filters)
.await
.map_err(ErrorWrapper::from)?;

let Some(updates) = updates.remove(inbox_id) else {
return Err(Error::from_reason("Unable to find provided inbox_id"));
};
let updates: Vec<_> = updates.into_iter().map(|u| u.update).collect();

let scw_verifier =
MultiSmartContractSignatureVerifier::new_from_env().map_err(ErrorWrapper::from)?;

let mut association_state = None;

for update in updates {
let update = update
.to_verified(&scw_verifier)
.await
.map_err(ErrorWrapper::from)?;
association_state = Some(
update
.update_state(association_state, update.client_timestamp_ns)
.map_err(ErrorWrapper::from)?,
);
}
let association_state =
association_state.ok_or(Error::from_reason("Unable to create association state"))?;

Ok(association_state.get(identifier).is_some())
}
2 changes: 1 addition & 1 deletion bindings_node/src/inbox_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub async fn get_inbox_id_for_address(
) -> Result<Option<String>> {
let account_address = account_address.to_lowercase();
let api_client = ApiClientWrapper::new(
TonicApiClient::create(host.clone(), is_secure)
TonicApiClient::create(host, is_secure)
.await
.map_err(ErrorWrapper::from)?
.into(),
Expand Down
12 changes: 5 additions & 7 deletions examples/cli/cli-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,13 @@ async fn main() -> color_eyre::eyre::Result<()> {
)
.await?,
),
(false, Env::Local) => {
Box::new(ClientV3::create("http://localhost:5556".into(), false).await?)
}
(false, Env::Local) => Box::new(ClientV3::create("http://localhost:5556", false).await?),
(false, Env::Dev) => {
Box::new(ClientV3::create("https://grpc.dev.xmtp.network:443".into(), true).await?)
Box::new(ClientV3::create("https://grpc.dev.xmtp.network:443", true).await?)
}
(false, Env::Production) => {
Box::new(ClientV3::create("https://grpc.production.xmtp.network:443", true).await?)
}
(false, Env::Production) => Box::new(
ClientV3::create("https://grpc.production.xmtp.network:443".into(), true).await?,
),
(true, Env::Production) => todo!("not supported"),
};

Expand Down
2 changes: 1 addition & 1 deletion xmtp_api_grpc/src/grpc_api_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct Client {
}

impl Client {
pub async fn create(host: String, is_secure: bool) -> Result<Self, Error> {
pub async fn create(host: impl ToString, is_secure: bool) -> Result<Self, Error> {
let host = host.to_string();
let app_version = MetadataValue::try_from(&String::from("0.0.0"))
.map_err(|e| Error::new(ErrorKind::MetadataError).with(e))?;
Expand Down
4 changes: 2 additions & 2 deletions xmtp_api_grpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ mod utils {
#[async_trait::async_trait]
impl XmtpTestClient for crate::Client {
async fn create_local() -> Self {
crate::Client::create("http://localhost:5556".into(), false)
crate::Client::create("http://localhost:5556", false)
.await
.unwrap()
}

async fn create_dev() -> Self {
crate::Client::create("https://grpc.dev.xmtp.network:443".into(), false)
crate::Client::create("https://grpc.dev.xmtp.network:443", false)
.await
.unwrap()
}
Expand Down

0 comments on commit 07c4b2f

Please sign in to comment.