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(walletconnect): walletconnect integration #2223

Open
wants to merge 132 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
132 commits
Select commit Hold shift + click to select a range
9100780
init, start walletconnect client impl
borngraced Sep 9, 2024
ca3f558
save dev state - walletconnect client impl
borngraced Sep 9, 2024
c3dc6c6
save dev state - WalletConnect client impl
borngraced Sep 9, 2024
0eea639
rough implementations
borngraced Sep 11, 2024
588316f
commit lock file
borngraced Sep 11, 2024
60a2fc5
save dev state - handle connection close, session improvements, etc
borngraced Sep 12, 2024
fd8797c
minor changes
borngraced Sep 12, 2024
437767b
fix wasm compilation
borngraced Sep 13, 2024
6374da3
minor changes - create metadata mod
borngraced Sep 13, 2024
1c9cfe0
minor changes and code organization
borngraced Sep 14, 2024
f7987dd
minor changes + merge session and session_key mod
borngraced Sep 14, 2024
1f2adb2
minor changes to namespaces
borngraced Sep 15, 2024
658357f
some improvements
borngraced Sep 16, 2024
98026ac
add some session fn doc comments
borngraced Sep 16, 2024
9dddb91
big improvements to session handling
borngraced Sep 16, 2024
52a0f72
implement session delete and cleaup
borngraced Sep 17, 2024
9f5944c
improve code and locking
borngraced Sep 17, 2024
dff1b42
remove unused dep
borngraced Sep 17, 2024
591fbdb
refactorings and cleanupds
borngraced Sep 17, 2024
f6e7864
minor changes
borngraced Sep 18, 2024
f535f0b
make session single
borngraced Sep 19, 2024
3eb8969
minor changes
borngraced Sep 19, 2024
2d68488
save dev state
borngraced Sep 19, 2024
42dd5ba
implement coin activation for tendermint and other minor changes
borngraced Sep 21, 2024
edcb5a7
improve code and minor needed changes
borngraced Sep 22, 2024
e606ca4
improve relayer disconnection, remove unneeded changes, track topic s…
borngraced Sep 23, 2024
d7b5a17
fix tendermint pubkey derivation from walletconnect
borngraced Sep 23, 2024
3a85a17
start persistent session storage implementation
borngraced Sep 23, 2024
be98c40
implement sqlite session storage table and insert session method
borngraced Sep 24, 2024
6453a33
fix cyclic deps and continue storage implementations
borngraced Sep 25, 2024
9f87a9e
implement WASM persistent storage
borngraced Sep 26, 2024
6135665
implement persistent indexed_db session storage
borngraced Sep 26, 2024
5457d80
save dev state - implement wc rpc commands
borngraced Sep 26, 2024
9d12079
move walletconnect rpc to mm2_main rpc
borngraced Sep 26, 2024
03d9978
remove mm2_test_helpers lock file
borngraced Sep 26, 2024
32e46d9
format mm2_net cargo.toml
borngraced Sep 26, 2024
1e352b1
update storage session upon session settle response/request
borngraced Sep 26, 2024
a30511b
add tokio to wc dependency list
borngraced Sep 26, 2024
341b18e
improve tenderming with_pubkey activation params
borngraced Sep 26, 2024
4c8e299
minor changes
borngraced Sep 29, 2024
73d8315
fix proposer mod
borngraced Sep 29, 2024
c1accc9
merge with dev
borngraced Sep 29, 2024
7ab33d5
implement walletconnect sign tx for tendermint - wip
borngraced Oct 2, 2024
0bcd41c
remove chain params from get cosmos account method
borngraced Oct 2, 2024
b2ec309
tx impl - wip
borngraced Oct 2, 2024
2e25bde
save dev state
borngraced Oct 6, 2024
328bc76
handle client connnection/reconnection efficiently and minor renaming
borngraced Oct 6, 2024
9b20d7f
minor changes
borngraced Oct 6, 2024
ca2e19c
implement multi session management
borngraced Oct 8, 2024
481f1c3
remove mm2_test_helpers lock file
borngraced Oct 8, 2024
0c00710
refactoring and unit tests
borngraced Oct 8, 2024
e5e2150
fix session topic generation bug
borngraced Oct 8, 2024
8d749de
tendermint sign tx impl fixes
borngraced Oct 9, 2024
8b1b6c3
improve wc tx_signing codes
borngraced Oct 9, 2024
4202950
minor fix
borngraced Oct 9, 2024
d440ed9
add more session rpc endpoint
borngraced Oct 10, 2024
f3cf28b
update wc deps
borngraced Oct 10, 2024
e56f8d7
update wc_common deps
borngraced Oct 10, 2024
1ab21d7
merge with dev and fix conflicts
borngraced Oct 10, 2024
a498381
use rustls
borngraced Oct 10, 2024
c7b41a8
update bip39 deps
borngraced Oct 11, 2024
8fc7620
fix wasm clippy
borngraced Oct 11, 2024
2d52404
add chains/tendermint test
borngraced Oct 11, 2024
4cbd2fa
minor changes, improve api
borngraced Oct 14, 2024
a57495d
remove subscription on session delete
borngraced Oct 14, 2024
c7e7de6
improve ledger external wallet connection checks
borngraced Oct 14, 2024
cc3c568
wc tx handling for tendermint
borngraced Oct 14, 2024
7683090
merge with dev and fix conflicts
borngraced Oct 14, 2024
5a574a6
complete persistent session storage with unit test for native and wasm
borngraced Oct 15, 2024
81ebee7
minor changes
borngraced Oct 15, 2024
46e3802
fix is ledger connection fn
borngraced Oct 17, 2024
500cb87
module refactorings
borngraced Oct 19, 2024
b2dea85
Merge branch 'dev' of github.com:KomodoPlatform/komodo-defi-framework…
borngraced Oct 20, 2024
1840ca8
introduce WcRequstOps trait, untighten code and more
borngraced Oct 21, 2024
6501134
commit cargo.lock
borngraced Oct 21, 2024
38dadbe
implement cosmos ledger amino sign support
borngraced Oct 21, 2024
301a5b0
fix fmt
borngraced Oct 21, 2024
3cde05b
start eth pubkey mode impl
borngraced Oct 22, 2024
5eb3e4c
save progress - impl wc eth_personal sign
borngraced Oct 22, 2024
b35bd4a
implement eth coin activation using walletconnect
borngraced Oct 23, 2024
337154c
eth walletconnect coin activation and message handler code improvements
borngraced Oct 24, 2024
b358825
improve session message handling process
borngraced Oct 24, 2024
afc4058
further wc improve message handling
borngraced Oct 24, 2024
18eb62f
uncommit mm2_test_helpers lock file
borngraced Oct 24, 2024
9c083ac
eth wc transaction - save dev state
borngraced Oct 25, 2024
7f37fd3
impl eth withdraw for walletconnect policy
borngraced Oct 26, 2024
6e57330
don't support sign_raw_tx rpc in walletconnect mode
borngraced Oct 26, 2024
e45243b
cleanups
borngraced Oct 27, 2024
a8d937e
improvements to session, wc ops, and more
borngraced Oct 28, 2024
1569ede
create connection with custom namespaces
borngraced Oct 28, 2024
4fc5a43
impl eth sign and send tx for WalletConnect
borngraced Oct 31, 2024
3792027
swaps working, impl connection health manager
borngraced Nov 1, 2024
0b1da04
remove debug log
borngraced Nov 1, 2024
f0659a4
connection_handler improvements
borngraced Nov 1, 2024
187bf5e
improve tendermint wc code
borngraced Nov 2, 2024
2a2f167
use const_hex
borngraced Nov 2, 2024
cd368bf
major changes to connection handling and other minor fixes
borngraced Nov 3, 2024
0c61287
add logging to wc storage impl
borngraced Nov 3, 2024
fe0d096
Merge branch 'refs/heads/dev' into wc-integration
borngraced Nov 3, 2024
a0220d7
fix conflict and minor changes + merge with dev branch
borngraced Nov 3, 2024
3468502
minor changes
borngraced Nov 4, 2024
c4a07e4
monitor if relay client is connected
borngraced Nov 4, 2024
1e78257
changes to client impls
borngraced Nov 6, 2024
a43dc60
handle broadcast option for WalletConnect and some minor fixes
borngraced Nov 6, 2024
eea1532
remove unused
borngraced Nov 6, 2024
fb9f8c1
fix: handle unexpected end of file
borngraced Nov 6, 2024
d58b18f
re-enable use_watcher
borngraced Nov 6, 2024
045625b
cleanups
borngraced Nov 6, 2024
65c7f90
fix websocket connection drop
borngraced Nov 12, 2024
2326959
handle conn drop for session creation
borngraced Nov 12, 2024
42eaa29
nits
borngraced Nov 12, 2024
4ad423e
Merge branch 'refs/heads/dev' into wc-integration
borngraced Nov 12, 2024
e82083a
fix conflict after merge
borngraced Nov 12, 2024
ad38241
nits
borngraced Nov 12, 2024
59db165
move storage to session manager and minor improvements
borngraced Nov 13, 2024
f5caa19
fix review notes
borngraced Nov 13, 2024
bb186c2
fix: self review
borngraced Nov 15, 2024
77d7909
fix review notes
borngraced Nov 16, 2024
0d3fdfe
increase rpc timeout and return PublicKey from recover
borngraced Nov 18, 2024
35d0fa4
fix review notes
borngraced Nov 18, 2024
db1e687
fmt
borngraced Nov 18, 2024
11d114d
remove dup session delete rpc
borngraced Nov 19, 2024
af91d47
rename var and remove unnecessary sorting from storage sessions loading
borngraced Nov 19, 2024
10e9e67
refactor wait_for_tx_appears_on_rpc to use secs for time
borngraced Nov 19, 2024
b9075ce
batch subscribe to topics
borngraced Nov 26, 2024
1ff17cd
rename ctx.session to ctx.session_manager
borngraced Nov 26, 2024
5e89b9f
use weak spawner to spawn wc related task/event loops
borngraced Nov 27, 2024
1fec24f
validate table name only during initialization
borngraced Nov 27, 2024
bdf9636
make required_namespaces required for wc activation and remove defaults
borngraced Nov 27, 2024
31c9594
use sync mutex for active topic
borngraced Nov 27, 2024
37a36f7
fix review notes
borngraced Nov 28, 2024
0351d67
Merge branch 'dev' into wc-integration
borngraced Nov 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions mm2src/coins/eth/wallet_connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ use kdf_walletconnect::{chain::{WcChainId, WcRequestMethods},
error::WalletConnectError,
WalletConnectCtx};
use mm2_err_handle::prelude::*;
use secp256k1::PublicKey;
use secp256k1::{recovery::{RecoverableSignature, RecoveryId},
Secp256k1};
use std::str::FromStr;
use web3::signing::hash_message;

use super::EthCoin;

// Wait for 20 seconds for the transaction to appear on the RPC node.
mariocynicys marked this conversation as resolved.
Show resolved Hide resolved
const WAIT_RPC_TIMEOUT: u64 = 30_000;

#[derive(Display, Debug, EnumFromStringify)]
pub enum EthWalletConnectError {
UnsupportedChainId(WcChainId),
Expand Down Expand Up @@ -131,10 +135,8 @@ impl WalletConnectOps for EthCoin {
};
let tx_hash = tx_hash.strip_prefix("0x").unwrap_or(&tx_hash);
let maybe_signed_tx = {
// Wait for 10 seconds for the transaction to appear on the RPC node.
let wait_rpc_timeout = 10_000;
let check_every = 1.;
mariocynicys marked this conversation as resolved.
Show resolved Hide resolved
self.wait_for_tx_appears_on_rpc(H256::from_slice(&hex::decode(tx_hash)?), wait_rpc_timeout, check_every)
self.wait_for_tx_appears_on_rpc(H256::from_slice(&hex::decode(tx_hash)?), WAIT_RPC_TIMEOUT, check_every)
.await
.mm_err(|err| EthWalletConnectError::InternalError(err.to_string()))?
};
Expand Down Expand Up @@ -183,15 +185,17 @@ fn extract_pubkey_from_signature(
) -> MmResult<(H520, Address), EthWalletConnectError> {
let account =
H160::from_str(&account[2..]).map_to_mm(|err| EthWalletConnectError::InternalError(err.to_string()))?;
let uncompressed = {
let uncompressed: H520 = {
let message_hash = hash_message(message.to_string());
let signature = Signature::from_str(&signature_str[2..])
.map_to_mm(|err| EthWalletConnectError::InvalidSignature(err.to_string()))?;
recover(&signature, &message_hash).map_to_mm(|err| {
let pubkey = recover(&signature, &message_hash).map_to_mm(|err| {
let error = format!("Couldn't recover a public key from the signature: '{signature:?}, error: {err:?}'");
EthWalletConnectError::InvalidSignature(error)
})?
})?;
pubkey.serialize_uncompressed().into()
};

let mut public = Public::default();
public.as_mut().copy_from_slice(&uncompressed[1..65]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

ummm, looks like we skip the first 8bits of the H520, thus rendering it essentially an H512. maybe we could return a Public = H512 instead of PublicKey = H520 from recover().

that's just an early suggestion, i will want to check what these trimmed 8bits actually for first.

Copy link
Member Author

Choose a reason for hiding this comment

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

why is this changed needed ? I think its more appropriate to return Seckp2561 pubkey here

pub(crate) fn recover(signature: &Signature, message: &Message) -> Result<PublicKey, ethkey::Error> {
let recovery_id = {
let recovery_id = (signature[64] as i32)
.checked_sub(27)
.ok_or_else(|| ethkey::Error::InvalidSignature)?;
RecoveryId::from_i32(recovery_id)?
};
let sig = RecoverableSignature::from_compact(&signature[0..64], recovery_id)?;
let pubkey = Secp256k1::new().recover(&secp256k1::Message::from_slice(&message[..])?, &sig)?;
Ok(pubkey)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

not really needed. but I'm trying to understand why we have two different pubkey types and making them into one if possible.
will dig more into it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

looks like the first octet (byte/8-bits) is a flag to signify that the pubkey is uncompressed (even though one would usually know from the pubkey length, but i guess this is the standard anyways).
0x04 -> uncompressed
0x02 -> compressed with even y co-ordinate
0x03 -> compressed with odd y co-ordinate
https://www.rfc-editor.org/rfc/rfc5480#section-2.2

we can leave a little note saying that the first bit is not part of the uncomprssed key but rather just part of the encoding.


Expand All @@ -204,7 +208,7 @@ fn extract_pubkey_from_signature(
Ok((uncompressed, recovered_address))
}

pub(crate) fn recover(signature: &Signature, message: &Message) -> Result<H520, ethkey::Error> {
pub(crate) fn recover(signature: &Signature, message: &Message) -> Result<PublicKey, ethkey::Error> {
let recovery_id = {
let recovery_id = (signature[64] as i32)
.checked_sub(27)
Expand All @@ -213,9 +217,8 @@ pub(crate) fn recover(signature: &Signature, message: &Message) -> Result<H520,
};
let sig = RecoverableSignature::from_compact(&signature[0..64], recovery_id)?;
let pubkey = Secp256k1::new().recover(&secp256k1::Message::from_slice(&message[..])?, &sig)?;
let serialized = pubkey.serialize_uncompressed();

Ok(serialized.into())
Ok(pubkey)
}

/// Sign and send eth transaction with WalletConnect,
Expand Down
Loading