Skip to content

Commit

Permalink
[WalletConnect/BNB]: Add WalletConnectSigner optional module
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Dec 28, 2023
1 parent beb19be commit 183db68
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tw_coin_entry::error::AddressResult;
use tw_coin_entry::modules::json_signer::NoJsonSigner;
use tw_coin_entry::modules::message_signer::NoMessageSigner;
use tw_coin_entry::modules::plan_builder::NoPlanBuilder;
use tw_coin_entry::modules::wallet_connect_signer::NoWalletConnectSigner;
use tw_coin_entry::prefix::NoPrefix;
use tw_keypair::tw::PublicKey;
use tw_proto::{BLOCKCHAIN}::Proto;
Expand All @@ -33,6 +34,7 @@ impl CoinEntry for {BLOCKCHAIN}Entry {
type JsonSigner = NoJsonSigner;
type PlanBuilder = NoPlanBuilder;
type MessageSigner = NoMessageSigner;
type WalletConnectSigner = NoWalletConnectSigner;

#[inline]
fn parse_address(
Expand Down
2 changes: 2 additions & 0 deletions rust/chains/tw_binance/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tw_coin_entry::error::AddressResult;
use tw_coin_entry::modules::json_signer::NoJsonSigner;
use tw_coin_entry::modules::message_signer::NoMessageSigner;
use tw_coin_entry::modules::plan_builder::NoPlanBuilder;
use tw_coin_entry::modules::wallet_connect_signer::NoWalletConnectSigner;
use tw_keypair::tw::PublicKey;
use tw_proto::Binance::Proto;
use tw_proto::TxCompiler::Proto as CompilerProto;
Expand All @@ -33,6 +34,7 @@ impl CoinEntry for BinanceEntry {
type JsonSigner = NoJsonSigner;
type PlanBuilder = NoPlanBuilder;
type MessageSigner = NoMessageSigner;
type WalletConnectSigner = NoWalletConnectSigner;

#[inline]
fn parse_address(
Expand Down
2 changes: 2 additions & 0 deletions rust/chains/tw_cosmos/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tw_coin_entry::error::AddressResult;
use tw_coin_entry::modules::json_signer::NoJsonSigner;
use tw_coin_entry::modules::message_signer::NoMessageSigner;
use tw_coin_entry::modules::plan_builder::NoPlanBuilder;
use tw_coin_entry::modules::wallet_connect_signer::NoWalletConnectSigner;
use tw_cosmos_sdk::address::{Address, Bech32Prefix};
use tw_cosmos_sdk::context::StandardCosmosContext;
use tw_cosmos_sdk::modules::compiler::tw_compiler::TWTransactionCompiler;
Expand All @@ -31,6 +32,7 @@ impl CoinEntry for CosmosEntry {
type JsonSigner = NoJsonSigner;
type PlanBuilder = NoPlanBuilder;
type MessageSigner = NoMessageSigner;
type WalletConnectSigner = NoWalletConnectSigner;

#[inline]
fn parse_address(
Expand Down
2 changes: 2 additions & 0 deletions rust/chains/tw_greenfield/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tw_coin_entry::error::{AddressError, AddressResult};
use tw_coin_entry::modules::json_signer::NoJsonSigner;
use tw_coin_entry::modules::message_signer::NoMessageSigner;
use tw_coin_entry::modules::plan_builder::NoPlanBuilder;
use tw_coin_entry::modules::wallet_connect_signer::NoWalletConnectSigner;
use tw_coin_entry::prefix::NoPrefix;
use tw_keypair::tw::PublicKey;
use tw_proto::Greenfield::Proto;
Expand All @@ -33,6 +34,7 @@ impl CoinEntry for GreenfieldEntry {
type JsonSigner = NoJsonSigner;
type PlanBuilder = NoPlanBuilder;
type MessageSigner = NoMessageSigner;
type WalletConnectSigner = NoWalletConnectSigner;

#[inline]
fn parse_address(
Expand Down
2 changes: 2 additions & 0 deletions rust/chains/tw_native_evmos/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tw_coin_entry::error::AddressResult;
use tw_coin_entry::modules::json_signer::NoJsonSigner;
use tw_coin_entry::modules::message_signer::NoMessageSigner;
use tw_coin_entry::modules::plan_builder::NoPlanBuilder;
use tw_coin_entry::modules::wallet_connect_signer::NoWalletConnectSigner;
use tw_cosmos_sdk::address::{Address, Bech32Prefix};
use tw_cosmos_sdk::modules::compiler::tw_compiler::TWTransactionCompiler;
use tw_cosmos_sdk::modules::signer::tw_signer::TWSigner;
Expand All @@ -31,6 +32,7 @@ impl CoinEntry for NativeEvmosEntry {
type JsonSigner = NoJsonSigner;
type PlanBuilder = NoPlanBuilder;
type MessageSigner = NoMessageSigner;
type WalletConnectSigner = NoWalletConnectSigner;

#[inline]
fn parse_address(
Expand Down
2 changes: 2 additions & 0 deletions rust/chains/tw_native_injective/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tw_coin_entry::error::AddressResult;
use tw_coin_entry::modules::json_signer::NoJsonSigner;
use tw_coin_entry::modules::message_signer::NoMessageSigner;
use tw_coin_entry::modules::plan_builder::NoPlanBuilder;
use tw_coin_entry::modules::wallet_connect_signer::NoWalletConnectSigner;
use tw_cosmos_sdk::address::{Address, Bech32Prefix, CosmosAddress};
use tw_cosmos_sdk::modules::compiler::tw_compiler::TWTransactionCompiler;
use tw_cosmos_sdk::modules::signer::tw_signer::TWSigner;
Expand All @@ -31,6 +32,7 @@ impl CoinEntry for NativeInjectiveEntry {
type JsonSigner = NoJsonSigner;
type PlanBuilder = NoPlanBuilder;
type MessageSigner = NoMessageSigner;
type WalletConnectSigner = NoWalletConnectSigner;

#[inline]
fn parse_address(
Expand Down
2 changes: 2 additions & 0 deletions rust/chains/tw_thorchain/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use tw_coin_entry::error::AddressResult;
use tw_coin_entry::modules::json_signer::NoJsonSigner;
use tw_coin_entry::modules::message_signer::NoMessageSigner;
use tw_coin_entry::modules::plan_builder::NoPlanBuilder;
use tw_coin_entry::modules::wallet_connect_signer::NoWalletConnectSigner;
use tw_cosmos_sdk::address::{Address, Bech32Prefix, CosmosAddress};
use tw_keypair::tw;
use tw_proto::Cosmos::Proto;
Expand All @@ -30,6 +31,7 @@ impl CoinEntry for ThorchainEntry {
type JsonSigner = NoJsonSigner;
type PlanBuilder = NoPlanBuilder;
type MessageSigner = NoMessageSigner;
type WalletConnectSigner = NoWalletConnectSigner;

#[inline]
fn parse_address(
Expand Down
8 changes: 8 additions & 0 deletions rust/tw_any_coin/src/any_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ impl AnySigner {
let (ctx, entry) = coin_dispatcher(coin)?;
entry.plan(&ctx, input)
}

/// Signs a transaction in WalletConnect format.
/// It is optional. Returns an error if the chain does not support WalletConnect signing.
#[inline]
pub fn sign_wallet_connect(input: &[u8], coin: CoinType) -> SigningResult<Data> {
let (ctx, entry) = coin_dispatcher(coin)?;
entry.sign_wallet_connect(&ctx, input)
}
}
2 changes: 2 additions & 0 deletions rust/tw_aptos/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tw_coin_entry::error::{AddressError, AddressResult};
use tw_coin_entry::modules::json_signer::NoJsonSigner;
use tw_coin_entry::modules::message_signer::NoMessageSigner;
use tw_coin_entry::modules::plan_builder::NoPlanBuilder;
use tw_coin_entry::modules::wallet_connect_signer::NoWalletConnectSigner;
use tw_coin_entry::prefix::NoPrefix;
use tw_keypair::tw::PublicKey;
use tw_proto::Aptos::Proto;
Expand All @@ -33,6 +34,7 @@ impl CoinEntry for AptosEntry {
type JsonSigner = NoJsonSigner;
type PlanBuilder = NoPlanBuilder;
type MessageSigner = NoMessageSigner;
type WalletConnectSigner = NoWalletConnectSigner;

#[inline]
fn parse_address(
Expand Down
2 changes: 2 additions & 0 deletions rust/tw_bitcoin/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tw_coin_entry::derivation::Derivation;
use tw_coin_entry::error::{AddressError, AddressResult};
use tw_coin_entry::modules::json_signer::NoJsonSigner;
use tw_coin_entry::modules::message_signer::NoMessageSigner;
use tw_coin_entry::modules::wallet_connect_signer::NoWalletConnectSigner;
use tw_coin_entry::prefix::NoPrefix;
use tw_coin_entry::signing_output_error;
use tw_keypair::tw::PublicKey;
Expand Down Expand Up @@ -45,6 +46,7 @@ impl CoinEntry for BitcoinEntry {
type JsonSigner = NoJsonSigner;
type PlanBuilder = BitcoinPlanBuilder;
type MessageSigner = NoMessageSigner;
type WalletConnectSigner = NoWalletConnectSigner;

#[inline]
fn parse_address(
Expand Down
12 changes: 12 additions & 0 deletions rust/tw_coin_entry/src/coin_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tw_memory::Data;
use tw_proto::{MessageRead, MessageWrite};

use crate::modules::message_signer::MessageSigner;
use crate::modules::wallet_connect_signer::WalletConnectSigner;
pub use tw_proto::{ProtoError, ProtoResult};

pub type PrivateKeyBytes = Data;
Expand Down Expand Up @@ -60,6 +61,10 @@ pub trait CoinEntry {
///
/// **Optional**. Use `NoMessageSigner` if the blockchain does not support message signing.
type MessageSigner: MessageSigner;
/// WalletConnect Signer - the module allows to sign a transaction received in WalletConnect format.
///
/// **Optional**. Use `NoWalletConnectSigner` if the blockchain does not support WalletConnect transactions.
type WalletConnectSigner: WalletConnectSigner;

/// Tries to parse `Self::Address` from the given `address` string by `coin` type and address `prefix`.
fn parse_address(
Expand Down Expand Up @@ -125,4 +130,11 @@ pub trait CoinEntry {
fn message_signer(&self) -> Option<Self::MessageSigner> {
None
}

/// It is optional, Signing WalletConnect input with private key.
/// Returns `Ok(None)` if the blockchain does not support WalletConnect transactions.
#[inline]
fn wallet_connect_signer(&self) -> Option<Self::WalletConnectSigner> {
None
}
}
15 changes: 15 additions & 0 deletions rust/tw_coin_entry/src/coin_entry_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::error::{AddressResult, SigningError, SigningErrorType};
use crate::modules::json_signer::JsonSigner;
use crate::modules::message_signer::MessageSigner;
use crate::modules::plan_builder::PlanBuilder;
use crate::modules::wallet_connect_signer::WalletConnectSigner;
use crate::prefix::AddressPrefix;
use tw_keypair::tw::{PrivateKey, PublicKey};
use tw_memory::Data;
Expand Down Expand Up @@ -84,6 +85,9 @@ pub trait CoinEntryExt {

/// Verifies a signature for a message.
fn verify_message(&self, coin: &dyn CoinContext, input: &[u8]) -> SigningResult<bool>;

/// Signs a transaction in WalletConnect format.
fn sign_wallet_connect(&self, coin: &dyn CoinContext, input: &[u8]) -> SigningResult<Data>;
}

impl<T> CoinEntryExt for T
Expand Down Expand Up @@ -209,4 +213,15 @@ where
deserialize(input)?;
Ok(message_signer.verify_message(coin, input))
}

fn sign_wallet_connect(&self, coin: &dyn CoinContext, input: &[u8]) -> SigningResult<Data> {
let Some(wc_signer) = self.wallet_connect_signer() else {
return Err(SigningError(SigningErrorType::Error_not_supported));
};

let input: <T::WalletConnectSigner as WalletConnectSigner>::SigningInput<'_> =
deserialize(input)?;
let output = wc_signer.sign(coin, input);
serialize(&output).map_err(SigningError::from)
}
}
1 change: 1 addition & 0 deletions rust/tw_coin_entry/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
pub mod json_signer;
pub mod message_signer;
pub mod plan_builder;
pub mod wallet_connect_signer;
28 changes: 28 additions & 0 deletions rust/tw_coin_entry/src/modules/wallet_connect_signer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright © 2017-2023 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

use crate::coin_context::CoinContext;
use tw_proto::{DummyMessage, MessageRead, MessageWrite, NoMessage};

pub trait WalletConnectSigner {
type SigningInput<'a>: MessageRead<'a>;
type SigningOutput: MessageWrite;

/// Signs a transaction in WalletConnect format.
fn sign(&self, coin: &dyn CoinContext, input: Self::SigningInput<'_>) -> Self::SigningOutput;
}

/// `NoWalletConnectSigner` can't be created since there are no enum variants.
pub enum NoWalletConnectSigner {}

impl WalletConnectSigner for NoWalletConnectSigner {
type SigningInput<'a> = DummyMessage;
type SigningOutput = NoMessage;

fn sign(&self, _coin: &dyn CoinContext, _input: Self::SigningInput<'_>) -> Self::SigningOutput {
panic!("`NoWalletConnectSigner` should never be constructed and used")
}
}
2 changes: 2 additions & 0 deletions rust/tw_ethereum/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use tw_coin_entry::coin_entry::{CoinEntry, PublicKeyBytes, SignatureBytes};
use tw_coin_entry::derivation::Derivation;
use tw_coin_entry::error::{AddressError, AddressResult};
use tw_coin_entry::modules::plan_builder::NoPlanBuilder;
use tw_coin_entry::modules::wallet_connect_signer::NoWalletConnectSigner;
use tw_coin_entry::prefix::NoPrefix;
use tw_evm::address::Address;
use tw_evm::evm_context::StandardEvmContext;
Expand All @@ -35,6 +36,7 @@ impl CoinEntry for EthereumEntry {
type JsonSigner = EthJsonSigner<StandardEvmContext>;
type PlanBuilder = NoPlanBuilder;
type MessageSigner = EthMessageSigner;
type WalletConnectSigner = NoWalletConnectSigner;

#[inline]
fn parse_address(
Expand Down
3 changes: 3 additions & 0 deletions rust/tw_internet_computer/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tw_coin_entry::{
error::{AddressError, AddressResult, SigningError},
modules::{
json_signer::NoJsonSigner, message_signer::NoMessageSigner, plan_builder::NoPlanBuilder,
wallet_connect_signer::NoWalletConnectSigner,
},
prefix::NoPrefix,
signing_output_error,
Expand Down Expand Up @@ -43,6 +44,8 @@ impl CoinEntry for InternetComputerEntry {

type MessageSigner = NoMessageSigner;

type WalletConnectSigner = NoWalletConnectSigner;

#[inline]
fn parse_address(
&self,
Expand Down
2 changes: 2 additions & 0 deletions rust/tw_ronin/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tw_coin_entry::coin_entry::{CoinEntry, PublicKeyBytes, SignatureBytes};
use tw_coin_entry::derivation::Derivation;
use tw_coin_entry::error::{AddressError, AddressResult};
use tw_coin_entry::modules::plan_builder::NoPlanBuilder;
use tw_coin_entry::modules::wallet_connect_signer::NoWalletConnectSigner;
use tw_coin_entry::prefix::NoPrefix;
use tw_evm::evm_entry::EvmEntry;
use tw_evm::modules::compiler::Compiler;
Expand All @@ -35,6 +36,7 @@ impl CoinEntry for RoninEntry {
type JsonSigner = EthJsonSigner<RoninContext>;
type PlanBuilder = NoPlanBuilder;
type MessageSigner = EthMessageSigner;
type WalletConnectSigner = NoWalletConnectSigner;

#[inline]
fn parse_address(
Expand Down
35 changes: 35 additions & 0 deletions src/proto/WalletConnect.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
syntax = "proto3";

package TW.WalletConnect.Proto;
option java_package = "wallet.core.jni.proto";

import "Common.proto";

// The transaction protocol may differ from version to version.
enum Protocol {
V2 = 0;
}

message SigningInput {
// A signing method like "cosmos_signAmino" or "eth_signTransaction".
string method = 1;

// Wallet's private key.
bytes private_key = 2;

// Transaction payload to sign.
// Basically, a JSON object.
string payload = 3;
}

message SigningOutput {
// A signing result in string representation.
// It can be a serialized signed transaction in Base64, hex etc, or a JSON object.
string result = 1;

// OK (=0) or other codes in case of error
Common.Proto.SigningError error = 2;

// error description in case of error
string error_message = 3;
}

0 comments on commit 183db68

Please sign in to comment.