Skip to content

Commit 64ba9c3

Browse files
committed
feat: initial commit
1 parent 3142f87 commit 64ba9c3

File tree

27 files changed

+3854
-0
lines changed

27 files changed

+3854
-0
lines changed

.github/workflows/ci.yaml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: ci
2+
3+
on:
4+
pull_request:
5+
paths-ignore:
6+
- ".github/**"
7+
- "docs/**"
8+
- "README.md"
9+
10+
push:
11+
branches: ["main"]
12+
paths-ignore:
13+
- ".github/**"
14+
- "docs/**"
15+
- "README.md"
16+
17+
concurrency:
18+
# Support push/pr as event types with different behaviors each:
19+
# 1. push: queue up builds
20+
# 2. pr: only allow one run per PR
21+
group: ${{ github.workflow }}-${{ github.event_name }}${{ github.event.pull_request.number }}
22+
# If there is already a workflow running for the same pull request, cancel it
23+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
24+
25+
jobs:
26+
tasks:
27+
name: "${{ matrix.cargo.name }}"
28+
runs-on:
29+
group: ubuntu-runners
30+
strategy:
31+
fail-fast: false
32+
matrix:
33+
cargo:
34+
- name: "Clippy"
35+
cmd: clippy
36+
args: --workspace --all-features --tests -- -D clippy::all -W clippy::style
37+
rust: stable
38+
- name: "Formatting"
39+
cmd: fmt
40+
args: --all -- --check
41+
rust: nightly
42+
- name: "Tests"
43+
cmd: nextest
44+
args: run --workspace --retries 3
45+
rust: stable
46+
- name: "Documentation Tests"
47+
cmd: test
48+
args: --workspace --doc
49+
rust: stable
50+
env:
51+
RUST_BACKTRACE: full
52+
steps:
53+
- uses: actions/checkout@v3
54+
55+
# Install Rust toolchain
56+
- name: "Install Rust ${{ matrix.cargo.rust }}"
57+
uses: actions-rs/toolchain@v1
58+
with:
59+
toolchain: ${{ matrix.cargo.rust }}
60+
profile: default
61+
override: true
62+
63+
- uses: Swatinem/rust-cache@v2
64+
65+
- uses: taiki-e/install-action@v1
66+
with:
67+
tool: nextest
68+
69+
- name: "Cargo ${{ matrix.cargo.name }}"
70+
uses: actions-rs/cargo@v1
71+
with:
72+
command: ${{ matrix.cargo.cmd }}
73+
args: ${{ matrix.cargo.args }}

.github/workflows/release.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: "release"
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
paths-ignore:
7+
- ".github/**"
8+
- "docs/**"
9+
- "README.md"
10+
11+
jobs:
12+
release:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
with:
17+
fetch-depth: 0
18+
19+
- name: "Install Rust toolchain (stable)"
20+
uses: actions-rs/toolchain@v1
21+
with:
22+
toolchain: stable
23+
profile: minimal
24+
default: true
25+
26+
- name: Cache cargo registry
27+
uses: Swatinem/rust-cache@v2
28+
29+
- name: "Cocogitto release"
30+
id: release
31+
uses: cocogitto/cocogitto-action@v3
32+
with:
33+
check: true
34+
check-latest-tag-only: true
35+
release: true
36+
git-user: "github-actions"
37+
git-user-email: "[email protected]"
38+
39+
- name: "Generate Changelog"
40+
run: cog changelog --at ${{ steps.release.outputs.version }} -t full_hash > GITHUB_CHANGELOG.md
41+
42+
- name: "Update Github release notes"
43+
uses: softprops/action-gh-release@v1
44+
with:
45+
body_path: GITHUB_CHANGELOG.md
46+
tag_name: ${{ steps.release.outputs.version }}
47+
token: ${{ secrets.RELEASE_TOKEN }}

Cargo.toml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Root Crate
2+
[package]
3+
name = "walletconnect_sdk"
4+
version = "0.1.0"
5+
edition = "2021"
6+
authors = ["WalletConnect Team"]
7+
license = "Apache-2.0"
8+
9+
[workspace]
10+
members = [
11+
"relay_client",
12+
"relay_rpc"
13+
]
14+
15+
[features]
16+
full = [
17+
"client",
18+
"rpc",
19+
]
20+
client = ["dep:relay_client"]
21+
rpc = ["dep:relay_rpc"]
22+
23+
[dependencies]
24+
relay_client = { path = "./relay_client", optional = true }
25+
relay_rpc = { path = "./relay_rpc", optional = true }
26+
27+
[dev-dependencies]
28+
anyhow = "1"
29+
structopt = { version = "0.3", default-features = false }
30+
tokio = { version = "1.22", features = ["full"] }
31+
32+
[[example]]
33+
name = "basic_client"
34+
required-features = ["full"]

clippy.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
await-holding-invalid-types = [
2+
"tracing::trace::Entered",
3+
"tracing::trace::EnteredSpan",
4+
]

cog.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pre_bump_hooks = [
2+
"cargo check",
3+
]
4+
5+
tag_prefix = "v"
6+
7+
[commit_types]
8+
tests = { changelog_title = "Tests" }
9+
10+
[changelog]
11+
path = "CHANGELOG.md"
12+
template = "remote"
13+
remote = "github.com"
14+
owner = "WalletConnect"
15+
repository = "WalletConnectRust"
16+
17+
authors = [
18+
{ signature = "Ivan Reshetnikov", username = "heilhead" }
19+
]

examples/basic_client.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
use {
2+
relay_client::{
3+
Client,
4+
CloseFrame,
5+
ConnectionHandler,
6+
ConnectionOptions,
7+
Error,
8+
PublishedMessage,
9+
},
10+
relay_rpc::{
11+
auth::{ed25519_dalek::Keypair, rand, AuthToken},
12+
domain::{AuthSubject, Topic},
13+
},
14+
std::{sync::Arc, time::Duration},
15+
structopt::StructOpt,
16+
};
17+
18+
#[derive(StructOpt)]
19+
struct Args {
20+
/// Specify WebSocket address.
21+
#[structopt(short, long, default_value = "wss://relay.walletconnect.com")]
22+
address: String,
23+
24+
/// Specify WalletConnect project ID.
25+
#[structopt(short, long, default_value = "3cbaa32f8fbf3cdcc87d27ca1fa68069")]
26+
project_id: String,
27+
}
28+
29+
struct Handler {
30+
name: &'static str,
31+
}
32+
33+
impl Handler {
34+
fn new(name: &'static str) -> Self {
35+
Self { name }
36+
}
37+
}
38+
39+
impl ConnectionHandler for Handler {
40+
fn connected(&mut self) {
41+
println!("[{}] connection open", self.name);
42+
}
43+
44+
fn disconnected(&mut self, frame: Option<CloseFrame<'static>>) {
45+
println!("[{}] connection closed: frame={frame:?}", self.name);
46+
}
47+
48+
fn message_received(&mut self, message: PublishedMessage) {
49+
println!(
50+
"[{}] inbound message: topic={} message={}",
51+
self.name, message.topic, message.message
52+
);
53+
}
54+
55+
fn inbound_error(&mut self, error: Error) {
56+
println!("[{}] inbound error: {error}", self.name);
57+
}
58+
59+
fn outbound_error(&mut self, error: Error) {
60+
println!("[{}] outbound error: {error}", self.name);
61+
}
62+
}
63+
64+
fn create_conn_opts(address: &str, project_id: &str) -> ConnectionOptions {
65+
let key = Keypair::generate(&mut rand::thread_rng());
66+
67+
let auth = AuthToken::new(AuthSubject::generate())
68+
.aud(address)
69+
.ttl(Duration::from_secs(60 * 60))
70+
.as_jwt(&key)
71+
.unwrap();
72+
73+
ConnectionOptions::new(project_id, auth).with_address(address)
74+
}
75+
76+
#[tokio::main]
77+
async fn main() -> anyhow::Result<()> {
78+
let args = Args::from_args();
79+
80+
let client1 = Client::new(Handler::new("client1"));
81+
client1
82+
.connect(create_conn_opts(&args.address, &args.project_id))
83+
.await?;
84+
85+
let client2 = Client::new(Handler::new("client2"));
86+
client2
87+
.connect(create_conn_opts(&args.address, &args.project_id))
88+
.await?;
89+
90+
let topic = Topic::generate();
91+
92+
let subscription_id = client1.subscribe(topic.clone()).await?;
93+
println!("[client1] subscribed: topic={topic} subscription_id={subscription_id}");
94+
95+
client2
96+
.publish(
97+
topic.clone(),
98+
Arc::from("Hello WalletConnect!"),
99+
0,
100+
Duration::from_secs(60),
101+
)
102+
.await?;
103+
104+
println!("[client2] published message with topic: {topic}");
105+
106+
drop(client1);
107+
drop(client2);
108+
109+
tokio::time::sleep(Duration::from_millis(100)).await;
110+
111+
println!("clients disconnected");
112+
113+
Ok(())
114+
}

justfile

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
binary-crate := "."
2+
3+
export JUST_ROOT := justfile_directory()
4+
5+
# Default to listing recipes
6+
_default:
7+
@just --list --list-prefix ' > '
8+
9+
# Open project documentation in your local browser
10+
open-docs: (_build-docs "open")
11+
@echo '==> Opening documentation in system browser'
12+
13+
# Fast check project for errors
14+
check:
15+
@echo '==> Checking project for compile errors'
16+
cargo check --workspace
17+
18+
# Build service for development
19+
build:
20+
@echo '==> Building project'
21+
cargo build
22+
23+
# Build project documentation
24+
build-docs: (_build-docs "")
25+
26+
# Run project test suite, skipping storage tests
27+
test:
28+
@echo '==> Testing project (default)'
29+
cargo test --workspace
30+
31+
# Run project test suite, testing all features
32+
test-all:
33+
@echo '==> Testing project (all features)'
34+
cargo test --workspace --all-features
35+
36+
# Run test from project documentation
37+
test-doc:
38+
@echo '==> Testing project docs'
39+
cargo test --workspace --doc
40+
41+
# Clean build artifacts
42+
clean:
43+
@echo '==> Cleaning project target/*'
44+
cargo clean
45+
46+
# Lint the project for any quality issues
47+
lint: check fmt clippy commit-check
48+
49+
# Run project linter
50+
clippy:
51+
#!/bin/bash
52+
set -euo pipefail
53+
54+
if command -v cargo-clippy >/dev/null; then
55+
echo '==> Running clippy'
56+
cargo clippy --workspace --all-features --tests -- -D clippy::all -W clippy::style
57+
else
58+
echo '==> clippy not found in PATH, skipping'
59+
echo ' ^^^^^^ To install `rustup component add clippy`, see https://github.com/rust-lang/rust-clippy for details'
60+
fi
61+
62+
# Run code formatting check
63+
fmt:
64+
#!/bin/bash
65+
set -euo pipefail
66+
67+
if command -v cargo-fmt >/dev/null; then
68+
echo '==> Running rustfmt'
69+
cargo +nightly fmt --all -- --check
70+
else
71+
echo '==> rustfmt not found in PATH, skipping'
72+
echo ' ^^^^^^ To install `rustup component add rustfmt`, see https://github.com/rust-lang/rustfmt for details'
73+
fi
74+
75+
# Run commit checker
76+
commit-check:
77+
#!/bin/bash
78+
set -euo pipefail
79+
80+
if command -v cog >/dev/null; then
81+
echo '==> Running cog check'
82+
cog check --from-latest-tag
83+
else
84+
echo '==> cog not found in PATH, skipping'
85+
echo ' ^^^ To install `cargo install --locked cocogitto`, see https://github.com/cocogitto/cocogitto for details'
86+
fi
87+
88+
# Build project documentation
89+
_build-docs $open="":
90+
@echo "==> Building project documentation @$JUST_ROOT/target/doc"
91+
@cargo doc --all-features --workspace --no-deps ${open:+--open}

0 commit comments

Comments
 (0)