Skip to content

Conversation

@borngraced
Copy link

@borngraced borngraced commented Sep 16, 2024

#1543
This PR introduces the integration of WalletConnect into the Komodo DeFi Framework (KDF), enabling secure wallet connections for Cosmos and EVM-based chains. KDF acts as the DApp(in this PR), allowing users to initiate and manage transactions securely with external wallets e.g via Metamask.

Changes included:

  • Implemented multi-session handling for concurrent WalletConnect connections across accounts, integrates SQLite/IndexedDB for persistent session storage across devices and enhances relayer disconnection handling for smoother session management
  • Implemented WalletConnect coin activation for Tendermint and EVM.
  • Implemented Withdraw and Swap functionalities for Tendermint and EVM

https://specs.walletconnect.com/2.0/specs/clients/sign/
https://specs.walletconnect.com/2.0/specs/clients/core/pairing/
https://specs.walletconnect.com/2.0/specs/clients/core/crypto/
https://specs.walletconnect.com/2.0/specs/servers/relay/
https://docs.reown.com/advanced/multichain/rpc-reference/ethereum-rpc

Additional improvements include cleanup of unused dependencies, minor code refinements, and WASM compatibility

Updated deps:
Added deps:
Removed deps:

How to test using EVM coin e.g ETH (Native && WASM)

  1. Start kdf (cargo run)
  2. Create WalletConnect session connection
    • Use the RPC below
    • Copy the connection URL and either:
      • Paste directly in your wallet or
      • Generate QR code using QR Code Generator and scan with your wallet (e.g., MetaMask)
  3. Activate ETH coin (set "priv_key_policy": "wallet_connect": { "session_topic": your_session_topic" } in activation params).
  4. Approve authentication request in your Wallet
  5. Withdraw or trade.

Note: To add more eip155 chains, modify the chains array like this: ["eip155:1", "eip155:250"]

New RPC Methods

New connection

method: wc_new_connection

{
	"method": "wc_new_connection",
	"userpass": "{{ _.userpass }}",
	"mmrpc": "2.0",
	"params": {
		"required_namespaces": {
			"eip155": {
				"chains": [
					"eip155:1"
				],
				"methods": [
					"eth_sendTransaction",
					"eth_signTransaction",
					"personal_sign"
				],
				"events": [
					"accountsChanged",
					"chainChanged"
				]
			},
			"cosmos": {
				"chains": [
					"cosmos:cosmoshub-4"
				],
				"methods": [
					"cosmos_signDirect",
					"cosmos_signAmino",
					"cosmos_getAccounts"
				],
				"events": []
			}
		}
	}
}

Get/Retrieve Walletconnect session(s)

method: wc_get_session | wc_get_sessions

{
	"method": "wc_get_session" | "wc_get_sessions",
	"userpass": "{{ _.userpass }}",
	"mmrpc": "2.0",
	"params": {
		"topic": "3a6327f78840427d3a428e8fbaaaaabc8c89c582bb2ff4464d3c73d8e2e5b253"
	}
}

Ping Walletconnect session

method: wc_ping_session

{
	"method": "wc_ping_session",
	"userpass": "{{ _.userpass }}",
	"mmrpc": "2.0",
	"params": {
		"topic": "3a6327f78840427d3a428e8fbaaaaabc8c89c582bb2ff4464d3c73d8e2e5b253"
	}
}

Delete Walletconnect session

method: wc_delete_session

{
	"method": "wc_delete_session",
	"userpass": "{{ _.userpass }}",
	"mmrpc": "2.0",
	"params": {
		"topic": "3a6327f78840427d3a428e8fbaaaaabc8c89c582bb2ff4464d3c73d8e2e5b253"
	}
}

Active ETH/ERC

method: enable_eth_with_tokens

{
    "priv_key_policy": {
        "wallet_connect": {
            "session_topic": "3a6327f78840427d3a428e8fbaaaaabc8c89c582bb2ff4464d3c73d8e2e5b253"
        }
    },
    ...
}

Active Tendermint/Cosmos

method: enable_tendermint_with_assets

{
    "activation_params": {
        "wallet_connect": {
            "session_topic": "'3a6327f78840427d3a428e8fbaaaaabc8c89c582bb2ff4464d3c73d8e2e5b253"
        }
    },
    ...
}

cc @smk762

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

shamardy commented Jun 2, 2025

@smk762 this https://github.com/KomodoPlatform/komodo-defi-framework/blob/8a96e79b4a7b44eacd403f9c9522ccbf2a29c060/mm2src/coins/eth/v2_activation.rs#L170-L172 is an API breaking change. Users and GUIs have to use the below after the PR is merged.

"priv_key_policy": { "type": "ContextPrivKey" }

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.

Thanks a lot for the huge effort @borngraced. All the below comments are either questions or things that can be done later in another PR to not delay this anymore.

Comment on lines +26 to +29
let delete_request = SessionDeleteRequest {
code: USER_REQUESTED,
message: "User Disconnected".to_owned(),
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we allow this to be passed in the request?

Comment on lines +5 to +19
pub(crate) const RELAY_ADDRESS: &str = "wss://relay.walletconnect.com";
pub(crate) const PROJECT_ID: &str = "86e916bcbacee7f98225dde86b697f5b";
pub(crate) const AUTH_TOKEN_SUB: &str = "http://127.0.0.1:3000";
pub(crate) const AUTH_TOKEN_DURATION: Duration = Duration::from_secs(5 * 60 * 60);
pub(crate) const APP_NAME: &str = "Komodefi Framework";
pub(crate) const APP_DESCRIPTION: &str = "WallectConnect Komodefi Framework Playground";

#[inline]
pub(crate) fn generate_metadata() -> Metadata {
Metadata {
description: APP_DESCRIPTION.to_owned(),
url: AUTH_TOKEN_SUB.to_owned(),
icons: vec!["https://avatars.githubusercontent.com/u/21276113?s=200&v=4".to_owned()],
name: APP_NAME.to_owned(),
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

These should be part of the external wallet initialization request in this PR #2411

wc.validate_update_active_chain_id(session_topic, &chain_id).await?;

let (account_str, _) = wc.get_account_and_properties_for_chain_id(session_topic, &chain_id)?;
let message = "Authenticate with Komodefi";
Copy link
Collaborator

Choose a reason for hiding this comment

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

It should be part of the request instead, so whitelabels and GUIs can place their own messages. Can be done in another PR though.

Comment on lines +116 to +122
async fn wc_send_tx<'a>(
&self,
_ctx: &WalletConnectCtx,
_params: Self::Params<'a>,
) -> Result<Self::SendTxData, Self::Error> {
todo!()
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see that we are not using this, so not a priority to implement in this PR but this comment so that I can remember all the todos.

Comment on lines +43 to +46
Params::SessionRequest(_param) => {
// TODO: Implement when integrating KDF as a Dapp.
return MmError::err(WalletConnectError::NotImplemented);
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't this PR an implementation of KDF as a Dapp? A better note would be that this in not needed for our current usecases.

Copy link
Author

@borngraced borngraced Jun 3, 2025

Choose a reason for hiding this comment

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

Yes it is. As a Dapp


self.validate_chain_id(&session, chain_id)?;

// TODO: uncomment when WalletConnect wallets start listening to chainChanged event
Copy link
Collaborator

Choose a reason for hiding this comment

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

This can be done in another PR

};
},
"accountsChanged" => {
// TODO: Handle accountsChanged event logic.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess we need this implemented in next PRs. Will add it to the issue checklist, this should be done by disabling and enabling the coin again with a different pubkey.

@shamardy shamardy changed the title feat(walletconnect): walletconnect integration feat(walletconnect): add WalletConnect v2 support for Ethereum and Cosmos coins Jun 3, 2025
@shamardy shamardy changed the title feat(walletconnect): add WalletConnect v2 support for Ethereum and Cosmos coins feat(walletconnect): add WalletConnect v2 support for EVM and Cosmos Jun 3, 2025
@shamardy shamardy merged commit 9ccb42f into dev Jun 3, 2025
33 of 34 checks passed
@shamardy shamardy deleted the wc-integration branch June 3, 2025 03:21
dimxy added a commit that referenced this pull request Jun 3, 2025
* dev:
  feat(walletconnect): add WalletConnect v2 support for EVM and Cosmos (#2223)
  feat(ibc-routing-part-1): supporting entire Cosmos network for swaps (#2459)
  fix(test): fix HD Wallet message signing tests (#2474)
  improvement(builds): enable static CRT linking for MSVC builds (#2464)
  feat(wallet): implement HD multi-address support for message signing (#2432)
@smk762
Copy link

smk762 commented Jun 4, 2025

@smk762 this

https://github.com/KomodoPlatform/komodo-defi-framework/blob/8a96e79b4a7b44eacd403f9c9522ccbf2a29c060/mm2src/coins/eth/v2_activation.rs#L170-L172

is an API breaking change. Users and GUIs have to use the below after the PR is merged.

"priv_key_policy": { "type": "ContextPrivKey" }

Existing docs for wallet connect suggest using

      "priv_key_policy": {
        "wallet_connect": {
          "session_topic": "3569914dd09a5cc4ac92dedab354f06ff5db17ef616233a8ba562cbea51269fd"
        }
      }

Is it correct to assume the following?

  • "priv_key_policy": { "type": "ContextPrivKey" } is only for non-WalletConnect activations.
  • `"priv_key_policy":"Trezor" & "priv_key_policy":"Metamask" are still valid options.
  • "priv_key_policy" is now a required field (if not, please specify the default)
  • "priv_key_policy" being a required field only applies to v2 ETH activation methods (enable_eth_with_tokens and task::enable_eth::init), and not to tendermint or other v2 activation methods.
  • "priv_key_policy" being a required field does not apply to any v1 activation methods

dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Jun 8, 2025
* lr-swap-wip: (37 commits)
  fix custom token error name
  fix getting chain_id from protocol_data
  refactor (review): use dedicated large error cfg, add new fn to FromApiValueError, fix TODO, use experimental namespace for lr rpc, more Ticker alias
  feat(walletconnect): add WalletConnect v2 support for EVM and Cosmos (GLEECBTC#2223)
  feat(ibc-routing-part-1): supporting entire Cosmos network for swaps (GLEECBTC#2459)
  fix(test): fix HD Wallet message signing tests (GLEECBTC#2474)
  improvement(builds): enable static CRT linking for MSVC builds (GLEECBTC#2464)
  feat(wallet): implement HD multi-address support for message signing (GLEECBTC#2432)
  fix(p2pk): validate expected pubkey correctly for p2pk inputs (GLEECBTC#2408)
  chore(docs): update old urls referencing atomicdex or old docs pages (GLEECBTC#2428)
  improvement(p2p): remove hardcoded seeds (GLEECBTC#2439)
  fix(evm-api): find enabled erc20 token using platform ticker (GLEECBTC#2445)
  chore(docs): add DeepWiki badge to README (GLEECBTC#2463)
  chore(core): organize deps using workspace.dependencies (GLEECBTC#2449)
  feat(db-arch): more dbdir to address_dir replacements (GLEECBTC#2398)
  chore(build-artifacts): remove duplicated mm2 build artifacts (GLEECBTC#2448)
  feat(pubkey-banning): expirable bans (GLEECBTC#2455)
  fix(eth-balance-events): serialize eth address using AddrToString (GLEECBTC#2440)
  chore(deps): remove base58 and replace it completely with bs58 (GLEECBTC#2427)
  feat(tron): initial groundwork for full TRON integration (GLEECBTC#2425)
  ...
dimxy added a commit to dimxy/komodo-defi-framework that referenced this pull request Jun 27, 2025
* dev: (30 commits)
  chore(core): replace hash_raw_entry with stable entry() API (GLEECBTC#2473)
  chore(core): adapt `MmError` and usages for compatibility with new rustc versions (GLEECBTC#2443)
  feat(wallet): add `delete_wallet` RPC (GLEECBTC#2497)
  chore(release): add changelog entries for v2.5.0-beta (GLEECBTC#2494)
  chore(release): bump kdf version to 2.5.0-beta (GLEECBTC#2492)
  feat(tests): zcoin unit test to validate dex fee (GLEECBTC#2460)
  fix(zcoin): correctly track unconfirmed z-coin notes (GLEECBTC#2331)
  improvement(orders): remove BTC specific volume from min_trading_vol logic (GLEECBTC#2483)
  feat(ibc-routing-part-2): supporting entire Cosmos network for swaps (GLEECBTC#2476)
  fix(startup): don't initialize WalletConnect during startup (GLEECBTC#2485)
  fix(dns): better ip resolution (GLEECBTC#2487)
  improvement(event-streaming): strong type streamer IDs (GLEECBTC#2441)
  bump timed-map to `1.4.1` (GLEECBTC#2481)
  improvement(RPC): unified interface for legacy and current RPC interfaces (GLEECBTC#2450)
  improvement(tendermint): `tendermint_tx_internal_id` helper (GLEECBTC#2438)
  feat(walletconnect): add WalletConnect v2 support for EVM and Cosmos (GLEECBTC#2223)
  feat(ibc-routing-part-1): supporting entire Cosmos network for swaps (GLEECBTC#2459)
  fix(test): fix HD Wallet message signing tests (GLEECBTC#2474)
  improvement(builds): enable static CRT linking for MSVC builds (GLEECBTC#2464)
  feat(wallet): implement HD multi-address support for message signing (GLEECBTC#2432)
  ...

# Conflicts:
#	mm2src/coins/qrc20.rs
#	mm2src/mm2_main/src/lp_swap/maker_swap.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants