forked from reown-com/reown-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: add WebSocket client support for WASM #1
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6bc30f3
add wasm support
borngraced 7059677
impl unmanaged ws client
borngraced 59a95f3
add http feature and minor changes
borngraced 22f2424
reorder
borngraced cb1ba9e
fix review notes and add toolchain
borngraced b2536e8
rename release.yaml to kdf.yaml and make it run ci.yaml only
shamardy 2ceb628
merge with main
borngraced 4e96457
trigger ci
borngraced 8e56f6d
revert ci.yaml
borngraced File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: "kdf" | ||
|
||
on: | ||
push: | ||
branches: ["kdf"] | ||
paths-ignore: | ||
- ".github/**" | ||
- "docs/**" | ||
- "README.md" | ||
|
||
workflow_dispatch: | ||
|
||
jobs: | ||
run-tests: | ||
uses: ./.github/workflows/ci.yaml | ||
secrets: inherit | ||
|
||
release: | ||
needs: [run-tests] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
use { | ||
futures_util::StreamExt, | ||
relay_client::{ | ||
websocket::{Client, Connection, ConnectionControl, PublishedMessage, StreamEvent}, | ||
ConnectionOptions, | ||
}, | ||
relay_rpc::{ | ||
auth::{ed25519_dalek::SigningKey, AuthToken}, | ||
domain::Topic, | ||
}, | ||
std::{sync::Arc, time::Duration}, | ||
structopt::StructOpt, | ||
tokio::spawn, | ||
}; | ||
|
||
#[derive(StructOpt)] | ||
struct Args { | ||
/// Specify WebSocket address. | ||
#[structopt(short, long, default_value = "wss://relay.walletconnect.org")] | ||
address: String, | ||
|
||
/// Specify WalletConnect project ID. | ||
#[structopt(short, long, default_value = "86e916bcbacee7f98225dde86b697f5b")] | ||
project_id: String, | ||
} | ||
|
||
fn create_conn_opts(address: &str, project_id: &str) -> ConnectionOptions { | ||
let key = SigningKey::generate(&mut rand::thread_rng()); | ||
|
||
let auth = AuthToken::new("http://127.0.0.1:8000") | ||
.aud(address) | ||
.ttl(Duration::from_secs(60 * 60)) | ||
.as_jwt(&key) | ||
.unwrap(); | ||
|
||
ConnectionOptions::new(project_id, auth).with_address(address) | ||
} | ||
|
||
async fn client_event_loop(client: Arc<Client>) { | ||
let mut conn = Connection::new(); | ||
if let Some(control_rx) = client.control_rx() { | ||
let mut control_rx = control_rx.lock().await; | ||
|
||
loop { | ||
tokio::select! { | ||
event = control_rx.recv() => { | ||
match event { | ||
Some(event) => match event { | ||
ConnectionControl::Connect { request, tx } => { | ||
let result = conn.connect(request).await; | ||
if result.is_ok() { | ||
println!("Client connected"); | ||
} | ||
tx.send(result).ok(); | ||
} | ||
ConnectionControl::Disconnect { tx } => { | ||
tx.send(conn.disconnect().await).ok(); | ||
} | ||
ConnectionControl::OutboundRequest(request) => { | ||
conn.request(request); | ||
} | ||
} | ||
// Control TX has been dropped, shutting down. | ||
None => { | ||
conn.disconnect().await.ok(); | ||
println!("Client disconnected"); | ||
break; | ||
} | ||
} | ||
} | ||
event = conn.select_next_some() => { | ||
match event { | ||
StreamEvent::InboundSubscriptionRequest(request) => { | ||
println!("messaged: received: {:?}", PublishedMessage::from_request(&request)); | ||
request.respond(Ok(true)).ok(); | ||
} | ||
StreamEvent::InboundError(error) => { | ||
println!("Inbound error: {:?}", error); | ||
} | ||
StreamEvent::OutboundError(error) => { | ||
println!("Outbound error: {:?}", error); | ||
} | ||
StreamEvent::ConnectionClosed(frame) => { | ||
println!("connection closed: frame={frame:?}"); | ||
conn.reset(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
let args = Args::from_args(); | ||
|
||
let client1 = Arc::new(Client::new_unmanaged()); | ||
spawn(client_event_loop(client1.clone())); | ||
|
||
client1 | ||
.connect(&create_conn_opts(&args.address, &args.project_id)) | ||
.await?; | ||
|
||
let client2 = Arc::new(Client::new_unmanaged()); | ||
spawn(client_event_loop(client2.clone())); | ||
|
||
client2 | ||
.connect(&create_conn_opts(&args.address, &args.project_id)) | ||
.await?; | ||
|
||
let topic = Topic::generate(); | ||
|
||
let subscription_id = client1.subscribe(topic.clone()).await?; | ||
println!("[client1] subscribed: topic={topic} subscription_id={subscription_id}"); | ||
|
||
client2 | ||
.publish( | ||
topic.clone(), | ||
Arc::from("Hello WalletConnect!"), | ||
None, | ||
0, | ||
Duration::from_secs(60), | ||
false, | ||
) | ||
.await?; | ||
|
||
println!("[client2] published message with topic: {topic}",); | ||
|
||
tokio::time::sleep(Duration::from_millis(500)).await; | ||
|
||
drop(client1); | ||
drop(client2); | ||
|
||
tokio::time::sleep(Duration::from_millis(100)).await; | ||
|
||
println!("clients disconnected"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please note that we use a different revision in kdf https://github.com/KomodoPlatform/komodo-defi-framework/blob/8b1170d9f33208e1c260d94d9ef9a7eb492e1047/mm2src/coins/Cargo.toml#L112
You will need to update the kdf revision when you integrate this client in kdf.