-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WalletConnect/BNB]: Implement
cosmos_signAmino
handler
* TODO fix compilation error
- Loading branch information
1 parent
183db68
commit 649cd54
Showing
19 changed files
with
390 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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::address::BinanceAddress; | ||
use tw_cosmos_sdk::context::CosmosContext; | ||
use tw_cosmos_sdk::hasher::keccak256_hasher::Keccak256Hasher; | ||
use tw_cosmos_sdk::hasher::sha256_hasher::Sha256Hasher; | ||
use tw_cosmos_sdk::private_key::secp256k1::Secp256PrivateKey; | ||
use tw_cosmos_sdk::public_key::secp256k1::Secp256PublicKey; | ||
use tw_cosmos_sdk::signature::secp256k1::Secp256k1Signature; | ||
|
||
pub struct BinanceContext; | ||
|
||
impl CosmosContext for BinanceContext { | ||
type Address = BinanceAddress; | ||
/// Binance Beacon chain uses `sha256` hash. | ||
type TxHasher = Sha256Hasher; | ||
type PrivateKey = Secp256PrivateKey; | ||
type PublicKey = Secp256PublicKey; | ||
type Signature = Secp256k1Signature; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
pub mod preimager; | ||
pub mod serializer; | ||
pub mod tx_builder; | ||
pub mod wallet_connect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// 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. | ||
|
||
pub const COSMOS_SIGN_AMINO: &str = "cosmos_signAmino"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// 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. | ||
|
||
pub mod methods; | ||
pub mod signer; | ||
pub mod types; |
103 changes: 103 additions & 0 deletions
103
rust/chains/tw_binance/src/modules/wallet_connect/signer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// 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::address::BinanceAddress; | ||
use crate::context::BinanceContext; | ||
use crate::modules::preimager::{JsonPreimager, JsonTxPreimage}; | ||
use crate::modules::wallet_connect::methods::COSMOS_SIGN_AMINO; | ||
use crate::modules::wallet_connect::types::{SignAminoRequest, SignAminoResponse}; | ||
use crate::signature::BinanceSignature; | ||
use crate::transaction::UnsignedTransaction; | ||
use std::borrow::Cow; | ||
use tw_coin_entry::coin_context::CoinContext; | ||
use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult}; | ||
use tw_coin_entry::modules::wallet_connect_signer::WalletConnectSigner; | ||
use tw_coin_entry::signing_output_error; | ||
use tw_cosmos_sdk::modules::serializer::json_serializer::JsonSerializer; | ||
use tw_cosmos_sdk::public_key::secp256k1::Secp256PublicKey; | ||
use tw_cosmos_sdk::public_key::CosmosPublicKey; | ||
use tw_keypair::ecdsa::secp256k1; | ||
use tw_keypair::traits::{KeyPairTrait, SigningKeyTrait}; | ||
use tw_keypair::tw; | ||
use tw_keypair::tw::Curve; | ||
use tw_misc::traits::ToBytesVec; | ||
use tw_proto::WalletConnect::Proto as WCProto; | ||
|
||
pub struct BinanceWalletConnectSigner; | ||
|
||
impl WalletConnectSigner for BinanceWalletConnectSigner { | ||
type SigningInput<'a> = WCProto::SigningInput<'a>; | ||
type SigningOutput = WCProto::SigningOutput<'static>; | ||
|
||
fn sign(&self, coin: &dyn CoinContext, input: Self::SigningInput<'_>) -> Self::SigningOutput { | ||
Self::sign_impl(coin, input) | ||
.unwrap_or_else(|e| signing_output_error!(WCProto::SigningOutput, e)) | ||
} | ||
} | ||
|
||
impl BinanceWalletConnectSigner { | ||
pub fn sign_impl( | ||
coin: &dyn CoinContext, | ||
input: WCProto::SigningInput<'_>, | ||
) -> SigningResult<WCProto::SigningOutput<'static>> { | ||
match input.method.as_ref() { | ||
COSMOS_SIGN_AMINO => Self::sign_amino(coin, input), | ||
_ => Err(SigningError(SigningErrorType::Error_not_supported)), | ||
} | ||
} | ||
|
||
pub fn sign_amino( | ||
coin: &dyn CoinContext, | ||
input: WCProto::SigningInput<'_>, | ||
) -> SigningResult<WCProto::SigningOutput<'static>> { | ||
let request: SignAminoRequest = serde_json::from_str(&input.payload) | ||
.map_err(|_| SigningError(SigningErrorType::Error_input_parse))?; | ||
|
||
let private_key = tw::PrivateKey::new(input.private_key.to_vec())?; | ||
|
||
// First, check if the `SignAminoRequest::signer_address` belongs to the `SigningInput::private_key`. | ||
if let Some(ref expected_addr) = request.signer_address { | ||
Self::validate_signer_address(coin, &private_key, expected_addr)?; | ||
} | ||
|
||
let JsonTxPreimage { tx_hash, .. } = JsonPreimager::preimage_hash(&request.sign_doc)?; | ||
|
||
let signature_bytes = private_key.sign(tx_hash.as_slice(), Curve::Secp256k1)?; | ||
|
||
let public_key = Secp256PublicKey::from_private_key(coin, &private_key)?; | ||
let signature = | ||
JsonSerializer::<BinanceContext>::serialize_signature(&public_key, signature_bytes); | ||
|
||
let result = SignAminoResponse { | ||
signature, | ||
signed: request.sign_doc, | ||
}; | ||
let result_json = serde_json::to_string(&result) | ||
.map_err(|_| SigningError(SigningErrorType::Error_internal))?; | ||
|
||
Ok(WCProto::SigningOutput { | ||
result: Cow::from(result_json), | ||
..WCProto::SigningOutput::default() | ||
}) | ||
} | ||
|
||
fn validate_signer_address( | ||
coin: &dyn CoinContext, | ||
private_key: &tw::PrivateKey, | ||
expected_addr: &BinanceAddress, | ||
) -> SigningResult<()> { | ||
let tw_public_key = private_key.get_public_key_by_type(coin.public_key_type())?; | ||
let prefix = None; | ||
let actual_address = | ||
BinanceAddress::with_public_key_coin_context(coin, &tw_public_key, prefix)?; | ||
|
||
if actual_address == *expected_addr { | ||
return Ok(()); | ||
} | ||
|
||
Err(SigningError(SigningErrorType::Error_invalid_address)) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
rust/chains/tw_binance/src/modules/wallet_connect/types.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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::address::BinanceAddress; | ||
use crate::transaction::UnsignedTransaction; | ||
use serde::{Deserialize, Serialize}; | ||
use tw_cosmos_sdk::modules::serializer::json_serializer::SignatureJson; | ||
use tw_encoding::base64::Base64Encoded; | ||
|
||
#[derive(Deserialize)] | ||
pub struct SignAminoRequest { | ||
#[serde(rename = "signerAddress")] | ||
pub signer_address: Option<BinanceAddress>, | ||
#[serde(rename = "signDoc")] | ||
pub sign_doc: UnsignedTransaction, | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct SignAminoResponse { | ||
pub signature: SignatureJson, | ||
pub signed: UnsignedTransaction, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.