Skip to content

Commit

Permalink
feat: implement grpc_gateway api client
Browse files Browse the repository at this point in the history
  • Loading branch information
dmccartney committed Sep 14, 2023
1 parent 21fe52c commit 8948a20
Show file tree
Hide file tree
Showing 31 changed files with 1,857 additions and 184 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/xmtp_api_grpc_gateway_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
on:
push:
branches:
- main
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: nanasess/setup-chromedriver@master
- run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- run: wasm-pack test --headless --chrome
working-directory: xmtp_api_grpc_gateway
86 changes: 52 additions & 34 deletions Cargo.lock

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

10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ members = [
"examples/cli",
"xmtp",
"xmtp_cryptography",
"xmtp_networking",
# TODO: replace ^ with:
# "xmtp_api_grpc",
# "xmtp_api_grpc_gateway",
"xmtp_api_grpc",
"xmtp_proto",
]

# Exclude since
# 1) no reason to share profile with other core crates
# 2) moreover, bindings_swift and xmtp_dh need their own size-optimized profile
# Exclude since `bindings_swift` and `bindings_wasm` need specific profiles.
exclude = [
"bindings_ffi",
"bindings_js",
"bindings_wasm",
"xmtp_api_grpc_gateway",
]

# Make the feature resolver explicit.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Start Docker Desktop.

- [`xmtp`](https://github.com/xmtp/libxmtp/tree/main/xmtp): Pure Rust implementation of XMTP APIs, agnostic to any per-language or per-platform binding
- [`xmtp_cryptography`](https://github.com/xmtp/libxmtp/tree/main/xmtp_cryptography): Cryptographic operations
- [`xmtp_networking`](https://github.com/xmtp/libxmtp/tree/main/xmtp_networking): API client for XMTP's gRPC API, using code from `xmtp_proto`
- [`xmtp_api_grpc`](https://github.com/xmtp/libxmtp/tree/main/xmtp_api_grpc): API client for XMTP's gRPC API, using code from `xmtp_proto`
- [`xmtp_api_grpc_gateway`](https://github.com/xmtp/libxmtp/tree/main/xmtp_api_grpc_gateway): API client for XMTP's gRPC Gateway API, using code from `xmtp_proto` (in progress)
- [`xmtp_proto`](https://github.com/xmtp/libxmtp/tree/main/xmtp_proto): Generated code for handling XMTP protocol buffers
- [`examples/cli`](https://github.com/xmtp/libxmtp/tree/main/examples/cli): Example XMTP console client. Use the CLI to try out sending double ratchet messages on the XMTP `dev` network.
- [`examples/android/xmtpv3_example`](https://github.com/xmtp/libxmtp/tree/main/examples/android/xmtpv3_example): Example Android app (in progress)
Expand Down
2 changes: 1 addition & 1 deletion bindings_ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "cae8edc45ba5b56b
uniffi_macros = { git = "https://github.com/mozilla/uniffi-rs", rev = "cae8edc45ba5b56bfcbf35b60c1ab6a97d1bf9da" }
xmtp = { path = "../xmtp", features = ["grpc", "native"] }
xmtp_cryptography = { path = "../xmtp_cryptography" }
xmtp_networking = { path = "../xmtp_networking" }
xmtp_api_grpc = { path = "../xmtp_api_grpc" }

[build_dependencies]
uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "cae8edc45ba5b56bfcbf35b60c1ab6a97d1bf9da", features = [
Expand Down
12 changes: 6 additions & 6 deletions bindings_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use xmtp::conversation::{ListMessagesOptions, Conversation};
use xmtp::conversations::Conversations;
use xmtp::storage::{EncryptedMessageStore, EncryptionKey, StorageOption, StoredMessage};
use xmtp::types::Address;
use xmtp_networking::grpc_api_helper::Client as TonicApiClient;
use xmtp_api_grpc::grpc_api_helper::Client as TonicApiClient;

use crate::inbox_owner::RustInboxOwner;
pub use crate::inbox_owner::SigningError;
Expand Down Expand Up @@ -314,7 +314,7 @@ mod tests {
create_client(
Box::new(MockLogger {}),
Box::new(ffi_inbox_owner),
xmtp_networking::LOCALHOST_ADDRESS.to_string(),
xmtp_api_grpc::LOCALHOST_ADDRESS.to_string(),
false,
None,
None,
Expand All @@ -339,7 +339,7 @@ mod tests {
let client_a = create_client(
Box::new(MockLogger {}),
Box::new(ffi_inbox_owner.clone()),
xmtp_networking::LOCALHOST_ADDRESS.to_string(),
xmtp_api_grpc::LOCALHOST_ADDRESS.to_string(),
false,
Some(path.to_string_lossy().to_string()),
None,
Expand All @@ -353,7 +353,7 @@ mod tests {
let client_b = create_client(
Box::new(MockLogger {}),
Box::new(ffi_inbox_owner),
xmtp_networking::LOCALHOST_ADDRESS.to_string(),
xmtp_api_grpc::LOCALHOST_ADDRESS.to_string(),
false,
Some(path.to_string_lossy().to_string()),
None,
Expand Down Expand Up @@ -381,7 +381,7 @@ mod tests {
let client_a = create_client(
Box::new(MockLogger {}),
Box::new(ffi_inbox_owner.clone()),
xmtp_networking::LOCALHOST_ADDRESS.to_string(),
xmtp_api_grpc::LOCALHOST_ADDRESS.to_string(),
false,
Some(path.to_string_lossy().to_string()),
Some(key),
Expand All @@ -397,7 +397,7 @@ mod tests {
let result_errored = create_client(
Box::new(MockLogger {}),
Box::new(ffi_inbox_owner),
xmtp_networking::LOCALHOST_ADDRESS.to_string(),
xmtp_api_grpc::LOCALHOST_ADDRESS.to_string(),
false,
Some(path.to_string_lossy().to_string()),
Some(other_key.to_vec()),
Expand Down
1 change: 1 addition & 0 deletions bindings_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ prost = { version = "0.11", features = ["prost-derive"] }
prost-types = "0.11"
wasm-bindgen = "0.2.87"
wasm-bindgen-futures = "0.4.37"
xmtp_api_grpc_gateway = { path = "../xmtp_api_grpc_gateway" }
xmtp_cryptography = { path = "../xmtp_cryptography" }
xmtp_proto = { path = "../xmtp_proto", features = ["proto_full"] }

Expand Down
2 changes: 1 addition & 1 deletion bindings_wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
This code is still under development.

- `$ cd bindings_wasm`
- `$ wasm-pack test --headless chrome`
- `$ wasm-pack test --headless --chrome`
- `$ wasm-pack build`
18 changes: 14 additions & 4 deletions bindings_wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen::prelude::{wasm_bindgen, JsError};
use xmtp_api_grpc_gateway::XmtpGrpcGatewayClient;

#[wasm_bindgen]
pub struct WasmXmtpClient {
// TODO
api: XmtpGrpcGatewayClient,
// inbox_owner: WasmInboxOwner,
}

impl WasmXmtpClient {
pub fn api(&self) -> &XmtpGrpcGatewayClient {
&self.api
}
}

#[wasm_bindgen]
impl WasmXmtpClient {
#[wasm_bindgen(constructor)]
pub fn new() -> Result<WasmXmtpClient, JsError> {
pub fn new(url: String) -> Result<WasmXmtpClient, JsError> {
// TODO
Ok(WasmXmtpClient {})
Ok(WasmXmtpClient {
api: XmtpGrpcGatewayClient::new(url),
})
}
}
16 changes: 14 additions & 2 deletions bindings_wasm/tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ extern crate bindings_wasm;
extern crate wasm_bindgen_test;
use bindings_wasm::*;
use prost::Message;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use xmtp_cryptography::signature::RecoverableSignature;
use xmtp_proto::xmtp::message_api::v1::Envelope;

// Only run these tests in a browser.
wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen]
extern "C" {
// Use `js_namespace` here to bind `console.log(..)` instead of just
// `log(..)`
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}

#[wasm_bindgen_test]
pub fn test_client_construction() {
let client = WasmXmtpClient::new();
let xmtp_url: String = "http://localhost:5555".to_string();
let client = WasmXmtpClient::new(xmtp_url).unwrap_or_else(|_error| {
panic!("client should be constructed");
});
// TODO: assert things about the client once it does anything.
assert!(matches!(client, Ok(_)));
client.api();
}

#[wasm_bindgen_test]
Expand Down
3 changes: 2 additions & 1 deletion examples/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ url = "2.3.1"
walletconnect = {git="https://github.com/nlordell/walletconnect-rs.git", features=["qr", "transport"]}
xmtp = { path = "../../xmtp", features = ["grpc", "native"] }
xmtp_cryptography = { path = "../../xmtp_cryptography"}
xmtp_networking = { path = "../../xmtp_networking"}
xmtp_api_grpc = { path = "../../xmtp_api_grpc" }
xmtp_proto = { path = "../../xmtp_proto", features = ["proto_full", "grpc"] }
timeago = "0.4.1"

4 changes: 2 additions & 2 deletions examples/cli/cli-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ use xmtp::conversations::Conversations;
use xmtp::storage::{
now, EncryptedMessageStore, EncryptionKey, MessageState, StorageError, StorageOption,
};
use xmtp::types::networking::XmtpApiClient;
use xmtp::InboxOwner;
use xmtp_api_grpc::grpc_api_helper::Client as ApiClient;
use xmtp_cryptography::signature::{h160addr_to_string, RecoverableSignature, SignatureError};
use xmtp_cryptography::utils::{rng, seeded_rng, LocalWallet};
use xmtp_networking::grpc_api_helper::Client as ApiClient;
use xmtp_proto::api_client::XmtpApiClient;
type Client = xmtp::client::Client<ApiClient>;
type ClientBuilder = xmtp::builder::ClientBuilder<ApiClient, Wallet>;

Expand Down
Loading

0 comments on commit 8948a20

Please sign in to comment.