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

feat: implement Pairing API #3

Merged
merged 81 commits into from
Dec 3, 2024
Merged

feat: implement Pairing API #3

merged 81 commits into from
Dec 3, 2024

Conversation

borngraced
Copy link
Member

@borngraced borngraced commented Sep 3, 2024

This PR implements the WalletConnect Pairing API, enabling secure communication between dApps and wallets. It manages pairing requests, responses, and lifecycle events (creating, deleting, extending, and pinging pairings).
Key features:

cargo run --example pairing --features="example"

Test overview:

  • Establishes WebSocket connection
  • Creates pairing with PairingClient
  • Handles pairing requests (delete, extend, ping)
  • Shuts down on Ctrl+C signal

The test demonstrates the full lifecycle of a WalletConnect pairing, from establishment to termination, using the pairing_api and relay_client crates.

https://specs.walletconnect.com/2.0/specs/clients/core/pairing/
https://specs.walletconnect.com/2.0/specs/clients/core/crypto/crypto-keys
https://specs.walletconnect.com/2.0/specs/clients/core/pairing/data-structures
https://specs.walletconnect.com/2.0/specs/clients/core/pairing/pairing-api

@borngraced borngraced self-assigned this Sep 3, 2024
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

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

Huge work! First review iteration from my side.

wc_common/src/crypto.rs Show resolved Hide resolved
wc_common/src/crypto.rs Show resolved Hide resolved
pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

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

Next review iteration!

pairing_api/Cargo.toml Outdated Show resolved Hide resolved
pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
pairing_api/src/uri.rs Show resolved Hide resolved
pairing_api/Cargo.toml Outdated Show resolved Hide resolved
Comment on lines 46 to 47
/// Pairing Delete error code.
const PAIRING_DELETE_ERROR_CODE: i64 = 6000;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Something we need to do when writing doc comments is not to add unnecessary ones, this is the same as the const name and not needed. If a comment doesn't explain something not obvious then it's not needed. For pub items we need doc comments, so we should try to write something that makes it clearer to the user of the sdk.

pairing_api/src/pairing.rs Outdated Show resolved Hide resolved
@laruh
Copy link
Member

laruh commented Nov 29, 2024

@borngraced ps signature tests started to fail

@laruh
Copy link
Member

laruh commented Nov 29, 2024

Btw noticed WalletConnectRs is actively using alloy with foundry(forge) in their project, a good example to learn from (as we should move to alloy in kdf, instead of using old web3 crate)

@borngraced
Copy link
Member Author

@borngraced ps signature tests started to fail

AFAIK, only CI is failing.. I will check ✔️

@laruh
Copy link
Member

laruh commented Nov 29, 2024

@borngraced ps signature tests started to fail

AFAIK, only CI is failing.. I will check ✔️

I have this locally too, run this command

cargo test --workspace --all-features

image

@laruh
Copy link
Member

laruh commented Nov 29, 2024

@borngraced

just need to add --broadcast flag in deploy_contract function (test_helpers.rs)

    let mut args = vec![
        "create",
        "--contracts=relay_rpc/contracts",
        contract_name,
        "--rpc-url",
        rpc_url.as_str(),
        "--private-key",
        &key_encoded,
        "--cache-path",
        &cache_folder,
        "--out",
        &out_folder,
        "--broadcast",
    ];

UPD: strange that Smith (author of the code) didnt add --broadcast flag (it submits the transaction to the blockchain specified by the --rpc-url argument), it is safe to add this flag as they use anvil.
anvil is foundry's built-in Ethereum development node, so the deployment transaction will be submitted to that local blockchain.

    #[tokio::test]
    async fn test_eip1271_pass() {
        let (_anvil, rpc_url, private_key) = spawn_anvil().await;
        let contract_address = deploy_contract(
            &rpc_url,
            &private_key,
            EIP1271_MOCK_CONTRACT,
            Some(&Address::from_private_key(&private_key).to_string()),
        )
        .await;
        // the rest of the code
    }

pub async fn spawn_anvil() -> (AnvilInstance, Url, SigningKey) {
    let anvil = Anvil::at(format_foundry_dir("bin/anvil")).spawn();
    let provider = anvil.endpoint().parse().unwrap();
    let private_key = anvil.keys().first().unwrap().clone();
    (
        anvil,
        provider,
        SigningKey::from_bytes(&private_key.to_bytes()).unwrap(),
    )
}

@borngraced
Copy link
Member Author

@borngraced

just need to add --broadcast flag in deploy_contract function (test_helpers.rs)

    let mut args = vec![
        "create",
        "--contracts=relay_rpc/contracts",
        contract_name,
        "--rpc-url",
        rpc_url.as_str(),
        "--private-key",
        &key_encoded,
        "--cache-path",
        &cache_folder,
        "--out",
        &out_folder,
        "--broadcast",
    ];

UPD: strange that Smith (author of the code) didnt add --broadcast flag (it submits the transaction to the blockchain specified by the --rpc-url argument), it is safe to add this flag as they use anvil. anvil is foundry's built-in Ethereum development node, so the deployment transaction will be submitted to that local blockchain.

    #[tokio::test]
    async fn test_eip1271_pass() {
        let (_anvil, rpc_url, private_key) = spawn_anvil().await;
        let contract_address = deploy_contract(
            &rpc_url,
            &private_key,
            EIP1271_MOCK_CONTRACT,
            Some(&Address::from_private_key(&private_key).to_string()),
        )
        .await;
        // the rest of the code
    }

pub async fn spawn_anvil() -> (AnvilInstance, Url, SigningKey) {
    let anvil = Anvil::at(format_foundry_dir("bin/anvil")).spawn();
    let provider = anvil.endpoint().parse().unwrap();
    let private_key = anvil.keys().first().unwrap().clone();
    (
        anvil,
        provider,
        SigningKey::from_bytes(&private_key.to_bytes()).unwrap(),
    )
}

I wonder how this has suddenly become a problem perhaps an update has happened somewhere in CI. fix anyways.. thanks

Copy link
Collaborator

@shamardy shamardy left a comment

Choose a reason for hiding this comment

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

LGTM!

@borngraced borngraced merged commit 0a5a4f7 into kdf Dec 3, 2024
8 checks passed
@borngraced borngraced deleted the pairing-api branch December 3, 2024 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants