Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce gRPC replication client api #1062

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bindings_ffi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub async fn create_client(
host,
is_secure
);
let api_client = TonicApiClient::create(host.clone(), is_secure).await?;
let api_client = TonicApiClient::create(host.clone(), is_secure, false).await?;

log::info!(
"Creating message store with path: {:?} and encryption key: {} of length {:?}",
Expand Down Expand Up @@ -160,7 +160,7 @@ pub async fn get_inbox_id_for_address(
account_address: String,
) -> Result<Option<String>, GenericError> {
let api_client = ApiClientWrapper::new(
TonicApiClient::create(host.clone(), is_secure).await?,
TonicApiClient::create(host.clone(), is_secure, false).await?,
Retry::default(),
);

Expand Down
2 changes: 1 addition & 1 deletion bindings_ffi/src/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn create_v2_client(
host: String,
is_secure: bool,
) -> Result<Arc<FfiV2ApiClient>, GenericError> {
let client = GrpcClient::create(host, is_secure).await?;
let client = GrpcClient::create(host, is_secure, false).await?;

let client = FfiV2ApiClient {
inner_client: Arc::new(client),
Expand Down
2 changes: 2 additions & 0 deletions bindings_node/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bindings_node/src/mls_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub async fn create_client(
encryption_key: Option<Uint8Array>,
history_sync_url: Option<String>,
) -> Result<NapiClient> {
let api_client = TonicApiClient::create(host.clone(), is_secure)
let api_client = TonicApiClient::create(host.clone(), is_secure, false)
.await
.map_err(|_| Error::from_reason("Error creating Tonic API client"))?;

Expand Down Expand Up @@ -126,7 +126,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.clone(), is_secure, false)
.await
.map_err(|e| Error::from_reason(format!("{}", e)))?,
Retry::default(),
Expand Down
2 changes: 1 addition & 1 deletion dev/gen_protos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if ! cargo install --list | grep "protoc-gen-prost-crate" > /dev/null; then
fi
fi

if ! buf generate https://github.com/xmtp/proto.git#branch=main,subdir=proto; then
if ! buf generate https://github.com/xmtp/proto.git#branch=nm/add-get-inbox-ids,subdir=proto; then
echo "Failed to generate protobuf definitions"
exit 1
fi
Expand Down
61 changes: 47 additions & 14 deletions examples/cli/cli-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ struct Cli {
local: bool,
#[clap(long, default_value_t = false)]
json: bool,
#[clap(long, default_value_t = false)]
testnet: bool,
}

#[derive(ValueEnum, Debug, Copy, Clone)]
Expand All @@ -79,6 +81,10 @@ enum Commands {
#[clap(long)]
seed_phrase: Option<String>,
},
Check {
#[arg(value_name = "address")]
address: String,
},
CreateGroup {
#[clap(value_enum, default_value_t = Permissions::EveryoneIsAdmin)]
permissions: Permissions,
Expand Down Expand Up @@ -188,6 +194,14 @@ async fn main() {
Commands::Register { seed_phrase } => {
unreachable!()
}
Commands::Check { address } => {
info!("Check {:?}", address);
let client = create_client(&cli, IdentityStrategy::CachedOnly)
.await
.unwrap();
let dict = client.can_message(vec![address.clone()]).await.unwrap();
info!("Can message: {:?}", dict.get(address).unwrap());
}
Commands::Info {} => {
info!("Info");
let client = create_client(&cli, IdentityStrategy::CachedOnly)
Expand Down Expand Up @@ -415,24 +429,43 @@ async fn create_client(cli: &Cli, account: IdentityStrategy) -> Result<Client, C
let msg_store = get_encrypted_store(&cli.db).unwrap();
let mut builder = ClientBuilder::new(account).store(msg_store);

if cli.local {
info!("Using local network");
builder = builder
.api_client(
ApiClient::create("http://localhost:5556".into(), false)
if cli.testnet {
if cli.local {
info!("Using local testnet network");
builder = builder.api_client(
ApiClient::create("http://localhost:5050".into(), false, true)
.await
.unwrap(),
)
.history_sync_url(MessageHistoryUrls::LOCAL_ADDRESS);
} else {
info!("Using dev network");
builder = builder
.api_client(
ApiClient::create("https://grpc.dev.xmtp.network:443".into(), true)
);
} else {
info!("Using testnet network");
builder = builder.api_client(
ApiClient::create("http://2.tcp.ngrok.io:10747".into(), true, true)
.await
.unwrap(),
)
.history_sync_url(MessageHistoryUrls::DEV_ADDRESS);
);
}
} else {
#[deny(clippy::collapsible_else_if)]
if cli.local {
info!("Using local network");
builder = builder
.api_client(
ApiClient::create("http://localhost:5556".into(), false, false)
.await
.unwrap(),
)
.history_sync_url(MessageHistoryUrls::LOCAL_ADDRESS);
} else {
info!("Using dev network");
builder = builder
.api_client(
ApiClient::create("https://grpc.dev.xmtp.network:443".into(), true, false)
.await
.unwrap(),
)
.history_sync_url(MessageHistoryUrls::DEV_ADDRESS);
}
}

let client = builder.build().await.map_err(CliError::ClientBuilder)?;
Expand Down
42 changes: 42 additions & 0 deletions xmtp_api_grpc/src/conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use prost::Message;
use xmtp_proto::xmtp::xmtpv4::{
ClientEnvelope, OriginatorEnvelope, PayerEnvelope, PublishEnvelopeRequest,
UnsignedOriginatorEnvelope,
};

pub fn wrap_client_envelope(req: ClientEnvelope) -> PublishEnvelopeRequest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: create_payer_envelope?

let mut buf = vec![];
req.encode(&mut buf).unwrap();

PublishEnvelopeRequest {
payer_envelope: Some(PayerEnvelope {
unsigned_client_envelope: buf,
payer_signature: None,
}),
}
}

pub fn extract_unsigned_originator_envelope(
req: &OriginatorEnvelope,
) -> UnsignedOriginatorEnvelope {
let mut unsigned_bytes = req.unsigned_originator_envelope.as_slice();
UnsignedOriginatorEnvelope::decode(&mut unsigned_bytes)
.expect("Failed to decode unsigned originator envelope")
}

pub fn extract_client_envelope(req: &OriginatorEnvelope) -> ClientEnvelope {
let unsigned_originator = extract_unsigned_originator_envelope(req);

let payer_envelope = unsigned_originator.payer_envelope.unwrap();
let mut payer_bytes = payer_envelope.unsigned_client_envelope.as_slice();
ClientEnvelope::decode(&mut payer_bytes).expect("Failed to decode client envelope")
}

pub fn extract_group_id_from_topic(topic: Vec<u8>) -> Vec<u8> {
let topic_str = String::from_utf8(topic).expect("Failed to convert topic to string");
let group_id = topic_str
.split("/")
.nth(1)
.expect("Failed to extract group id from topic");
group_id.as_bytes().to_vec()
}
Loading
Loading