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

fix compatibility issues with trust wallet #315

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Changes from all commits
Commits
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
30 changes: 26 additions & 4 deletions signers/src/eth_signer/json_rpc_signer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::eth_signer::{EthSignerError, PackedEthSignature};
use crate::RpcErr;
use hex;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -39,11 +40,32 @@ impl JsonRpcSigner {
self.provider.selectedAddress()
}

// https://docs.metamask.io/wallet/reference/provider-api/#legacy-properties
// https://docs.metamask.io/whats-new/#march-2024
// selectedAddress is deprecated and not all wallet extensions provide this method, use eth_requestAccounts instead
pub async fn get_request_account(&self) -> Result<String, EthSignerError> {
let req_params = RequestArguments {
method: "eth_requestAccounts".to_string(),
params: Vec::new(),
};
let params = serde_wasm_bindgen::to_value(&req_params)
.map_err(|e| EthSignerError::CustomError(e.to_string()))?;
let address = self.provider.request(params).await.map_err(|e| {
EthSignerError::RpcSignError(serde_wasm_bindgen::from_value::<RpcErr>(e).unwrap())
})?;
let address = serde_wasm_bindgen::from_value::<Vec<String>>(address)
.map_err(|e| EthSignerError::SigningFailed(e.to_string()))?;
Ok(address[0].clone())
}

pub async fn sign_message(&self, message: &[u8]) -> Result<PackedEthSignature, EthSignerError> {
let provider_address = self.provider.selectedAddress();
let provider_address = self.get_request_account().await?;
let mut params = Vec::new();
let msg_str =
std::str::from_utf8(message).map_err(|e| EthSignerError::CustomError(e.to_string()))?;

// https://docs.metamask.io/wallet/reference/personal_sign/
// Convert message to hex string since non-hex string are not well supported by all wallet extensions, result signature should be the same.
let msg_str = format! { "0x{}", hex::encode(message) };

params.push(serde_json::to_value(msg_str).unwrap());
params.push(serde_json::to_value(provider_address).unwrap());
let req_params = RequestArguments {
Expand All @@ -64,7 +86,7 @@ impl JsonRpcSigner {
&self,
message: &[u8],
) -> Result<PackedEthSignature, EthSignerError> {
let provider_address = self.provider.selectedAddress();
let provider_address = self.get_request_account().await?;
let mut params = Vec::new();
let msg_str =
std::str::from_utf8(message).map_err(|e| EthSignerError::CustomError(e.to_string()))?;
Expand Down
Loading