Skip to content

Commit

Permalink
[Zcash]: Add google::protobuf::Any chain_specific parameters to Bit…
Browse files Browse the repository at this point in the history
…coinV2
  • Loading branch information
satoshiotomakan committed Jan 16, 2025
1 parent 0c1ff27 commit e801cf0
Show file tree
Hide file tree
Showing 31 changed files with 172 additions and 120 deletions.
1 change: 1 addition & 0 deletions rust/chains/tw_bitcoin/src/modules/planner/psbt_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl<Context: UtxoContext> PsbtPlanner<Context> {
let out_point = Proto::OutPoint {
hash: txin.previous_output.hash.to_vec().into(),
vout: txin.previous_output.index,
..Proto::OutPoint::default()
};
let sequence = Proto::mod_Input::Sequence {
sequence: txin.sequence,
Expand Down
1 change: 1 addition & 0 deletions rust/chains/tw_bitcoin/src/modules/protobuf_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl ProtobufBuilder {
out_point: Some(Proto::OutPoint {
hash: Cow::from(input.previous_output.hash.to_vec()),
vout: input.previous_output.index,
..Proto::OutPoint::default()
}),
sequence: input.sequence,
script_sig: Self::script_data(&input.script_sig),
Expand Down
4 changes: 2 additions & 2 deletions rust/chains/tw_greenfield/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ impl JsonPublicKey for GreenfieldPublicKey {
}

impl ProtobufPublicKey for GreenfieldPublicKey {
fn to_proto(&self) -> google::protobuf::Any {
fn to_proto(&self) -> google::protobuf::Any<'static> {
let proto = tw_cosmos_sdk::proto::cosmos::crypto::eth::ethsecp256k1::PubKey {
key: self.0.compressed().to_vec(),
key: self.0.compressed().to_vec().into(),
};
to_any(&proto)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ pub struct GreenfieldTransferOut {
impl CosmosMessage for GreenfieldTransferOut {
fn to_proto(&self) -> SigningResult<ProtobufMessage> {
let msg = GreenfieldProto::bridge::MsgTransferOut {
from: self.from.to_string(),
to: self.to.to_string(),
from: self.from.to_string().into(),
to: self.to.to_string().into(),
amount: Some(build_coin(&self.amount)),
};
Ok(to_any(&msg))
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_native_evmos/src/ethermint_public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl JsonPublicKey for EthermintEthSecp256PublicKey {
}

impl ProtobufPublicKey for EthermintEthSecp256PublicKey {
fn to_proto(&self) -> google::protobuf::Any {
fn to_proto(&self) -> google::protobuf::Any<'static> {
self.0.to_proto()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl JsonPublicKey for InjectiveEthSecp256PublicKey {
}

impl ProtobufPublicKey for InjectiveEthSecp256PublicKey {
fn to_proto(&self) -> google::protobuf::Any {
fn to_proto(&self) -> google::protobuf::Any<'static> {
self.0.to_proto()
}
}
1 change: 0 additions & 1 deletion rust/tw_cosmos_sdk/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ fn main() {
)
.expect("Error configuring pb-rs builder")
.gen_info(true)
.dont_use_cow(true)
.build();
FileDescriptor::run(&out_protos).expect("Error generating proto files");
}
68 changes: 40 additions & 28 deletions rust/tw_cosmos_sdk/src/modules/serializer/protobuf_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ use crate::public_key::ProtobufPublicKey;
use crate::transaction::{
Coin, Fee, SignMode, SignedTransaction, SignerInfo, TxBody, UnsignedTransaction,
};
use std::borrow::Cow;
use std::marker::PhantomData;
use tw_coin_entry::error::prelude::*;
use tw_memory::Data;
use tw_proto::serialize;

pub fn build_coin(coin: &Coin) -> base_proto::Coin {
pub fn build_coin(coin: &Coin) -> base_proto::Coin<'static> {
base_proto::Coin {
amount: coin.amount.to_string(),
denom: coin.denom.clone(),
amount: coin.amount.to_string().into(),
denom: coin.denom.clone().into(),
}
}

Expand All @@ -37,62 +38,73 @@ pub struct SignDirectArgs {
impl<Context: CosmosContext> ProtobufSerializer<Context> {
/// Serializes a signed transaction into the Cosmos [`tx_proto::TxRaw`] message.
/// [`tx_proto::TxRaw`] can be broadcasted to the network.
pub fn build_signed_tx(signed: &SignedTransaction<Context>) -> SigningResult<tx_proto::TxRaw> {
pub fn build_signed_tx(
signed: &SignedTransaction<Context>,
) -> SigningResult<tx_proto::TxRaw<'static>> {
let tx_body = Self::build_tx_body(&signed.tx_body)?;
let body_bytes = serialize(&tx_body).expect("Unexpected error on tx_body serialization");
let body_bytes = serialize(&tx_body)
.expect("Unexpected error on tx_body serialization")
.into();

let auth_info = Self::build_auth_info(&signed.signer, &signed.fee);
let auth_info_bytes =
serialize(&auth_info).expect("Unexpected error on auth_info serialization");
let auth_info_bytes = serialize(&auth_info)
.expect("Unexpected error on auth_info serialization")
.into();

Ok(tx_proto::TxRaw {
body_bytes,
auth_info_bytes,
signatures: vec![signed.signature.clone()],
signatures: vec![signed.signature.clone().into()],
})
}

pub fn build_direct_signed_tx(args: &SignDirectArgs, signature: Data) -> tx_proto::TxRaw {
pub fn build_direct_signed_tx(
args: &SignDirectArgs,
signature: Data,
) -> tx_proto::TxRaw<'static> {
tx_proto::TxRaw {
body_bytes: args.tx_body.clone(),
auth_info_bytes: args.auth_info.clone(),
signatures: vec![signature],
body_bytes: args.tx_body.clone().into(),
auth_info_bytes: args.auth_info.clone().into(),
signatures: vec![signature.into()],
}
}

/// Serializes an unsigned transaction into the Cosmos [`tx_proto::SignDoc`] message.
/// [`tx_proto::SignDoc`] is used to generate a transaction prehash and sign it.
pub fn build_sign_doc(
unsigned: &UnsignedTransaction<Context>,
) -> SigningResult<tx_proto::SignDoc> {
) -> SigningResult<tx_proto::SignDoc<'static>> {
let tx_body = Self::build_tx_body(&unsigned.tx_body)?;
let body_bytes = serialize(&tx_body).expect("Unexpected error on tx_body serialization");
let body_bytes = serialize(&tx_body)
.expect("Unexpected error on tx_body serialization")
.into();

let auth_info = Self::build_auth_info(&unsigned.signer, &unsigned.fee);
let auth_info_bytes =
serialize(&auth_info).expect("Unexpected error on auth_info serialization");
let auth_info_bytes = serialize(&auth_info)
.expect("Unexpected error on auth_info serialization")
.into();

Ok(tx_proto::SignDoc {
body_bytes,
auth_info_bytes,
chain_id: unsigned.chain_id.clone(),
chain_id: unsigned.chain_id.clone().into(),
account_number: unsigned.account_number,
})
}

pub fn build_direct_sign_doc(args: &SignDirectArgs) -> tx_proto::SignDoc {
pub fn build_direct_sign_doc(args: &SignDirectArgs) -> tx_proto::SignDoc<'static> {
tx_proto::SignDoc {
body_bytes: args.tx_body.clone(),
auth_info_bytes: args.auth_info.clone(),
chain_id: args.chain_id.clone(),
body_bytes: args.tx_body.clone().into(),
auth_info_bytes: args.auth_info.clone().into(),
chain_id: args.chain_id.clone().into(),
account_number: args.account_number,
}
}

pub fn build_auth_info(
signer: &SignerInfo<Context::PublicKey>,
fee: &Fee<Context::Address>,
) -> tx_proto::AuthInfo {
) -> tx_proto::AuthInfo<'static> {
tx_proto::AuthInfo {
signer_infos: vec![Self::build_signer_info(signer)],
fee: Some(Self::build_fee(fee)),
Expand All @@ -101,7 +113,7 @@ impl<Context: CosmosContext> ProtobufSerializer<Context> {
}
}

pub fn build_tx_body(tx_body: &TxBody) -> SigningResult<tx_proto::TxBody> {
pub fn build_tx_body(tx_body: &TxBody) -> SigningResult<tx_proto::TxBody<'static>> {
let messages: Vec<_> = tx_body
.messages
.iter()
Expand All @@ -110,14 +122,14 @@ impl<Context: CosmosContext> ProtobufSerializer<Context> {

Ok(tx_proto::TxBody {
messages,
memo: tx_body.memo.clone(),
memo: tx_body.memo.clone().into(),
timeout_height: tx_body.timeout_height,
extension_options: Vec::default(),
non_critical_extension_options: Vec::default(),
})
}

pub fn build_signer_info(signer: &SignerInfo<Context::PublicKey>) -> tx_proto::SignerInfo {
pub fn build_signer_info(signer: &SignerInfo<Context::PublicKey>) -> tx_proto::SignerInfo<'static> {
use tx_proto::mod_ModeInfo::{self as mode_info, OneOfsum as SumEnum};

// Single is the mode info for a single signer. It is structured as a message
Expand All @@ -135,13 +147,13 @@ impl<Context: CosmosContext> ProtobufSerializer<Context> {
}
}

fn build_fee(fee: &Fee<Context::Address>) -> tx_proto::Fee {
fn build_fee(fee: &Fee<Context::Address>) -> tx_proto::Fee<'static> {
tx_proto::Fee {
amount: fee.amounts.iter().map(build_coin).collect(),
gas_limit: fee.gas_limit,
// Ignore `payer` and `granter` even if they set.
payer: String::default(),
granter: String::default(),
payer: Cow::default(),
granter: Cow::default(),
}
}

Expand Down
5 changes: 3 additions & 2 deletions rust/tw_cosmos_sdk/src/modules/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,10 +632,11 @@ where

let grant_msg = match auth.grant_type {
ProtoGrantType::grant_stake(ref stake) => google::protobuf::Any {
type_url: STAKE_AUTHORIZATION_MSG_TYPE.to_string(),
type_url: STAKE_AUTHORIZATION_MSG_TYPE.to_string().into(),
value: serialize(stake)
.tw_err(|_| SigningErrorType::Error_invalid_params)
.context("Error serializing Grant Stake Protobuf message")?,
.context("Error serializing Grant Stake Protobuf message")?
.into(),
},
ProtoGrantType::None => {
return SigningError::err(SigningErrorType::Error_invalid_params)
Expand Down
2 changes: 1 addition & 1 deletion rust/tw_cosmos_sdk/src/public_key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub trait CosmosPublicKey: JsonPublicKey + ProtobufPublicKey + Sized {
}

pub trait ProtobufPublicKey {
fn to_proto(&self) -> google::protobuf::Any;
fn to_proto(&self) -> google::protobuf::Any<'static>;
}

pub trait JsonPublicKey {
Expand Down
4 changes: 2 additions & 2 deletions rust/tw_cosmos_sdk/src/public_key/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ impl CosmosPublicKey for Secp256PublicKey {
}

impl ProtobufPublicKey for Secp256PublicKey {
fn to_proto(&self) -> google::protobuf::Any {
fn to_proto(&self) -> google::protobuf::Any<'static> {
let proto = cosmos::crypto::secp256k1::PubKey {
key: self.public_key.clone(),
key: self.public_key.clone().into(),
};
to_any_with_type_url(&proto, self.protobuf_type_url.clone())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tw_proto::{google, to_any};
pub struct AuthGrantMessage<Address: CosmosAddress> {
pub granter: Address,
pub grantee: Address,
pub grant_msg: google::protobuf::Any,
pub grant_msg: ProtobufMessage,
pub expiration_secs: i64,
}

Expand All @@ -28,8 +28,8 @@ impl<Address: CosmosAddress> CosmosMessage for AuthGrantMessage<Address> {
};

let proto_msg = cosmos::authz::v1beta1::MsgGrant {
granter: self.granter.to_string(),
grantee: self.grantee.to_string(),
granter: self.granter.to_string().into(),
grantee: self.grantee.to_string().into(),
grant: Some(grant),
};
Ok(to_any(&proto_msg))
Expand All @@ -46,9 +46,9 @@ pub struct AuthRevokeMessage<Address: CosmosAddress> {
impl<Address: CosmosAddress> CosmosMessage for AuthRevokeMessage<Address> {
fn to_proto(&self) -> SigningResult<ProtobufMessage> {
let proto_msg = cosmos::authz::v1beta1::MsgRevoke {
granter: self.granter.to_string(),
grantee: self.grantee.to_string(),
msg_type_url: self.msg_type_url.clone(),
granter: self.granter.to_string().into(),
grantee: self.grantee.to_string().into(),
msg_type_url: self.msg_type_url.clone().into(),
};
Ok(to_any(&proto_msg))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub struct SendMessage<Address: CosmosAddress> {
impl<Address: CosmosAddress> CosmosMessage for SendMessage<Address> {
fn to_proto(&self) -> SigningResult<ProtobufMessage> {
let proto_msg = cosmos::bank::v1beta1::MsgSend {
from_address: self.from_address.to_string(),
to_address: self.to_address.to_string(),
from_address: self.from_address.to_string().into(),
to_address: self.to_address.to_string().into(),
amount: self.amount.iter().map(build_coin).collect(),
};
Ok(to_any(&proto_msg))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<Address: CosmosAddress> CosmosMessage for VoteMessage<Address> {

let proto_msg = cosmos::gov::v1beta1::MsgVote {
proposal_id: self.proposal_id,
voter: self.voter.to_string(),
voter: self.voter.to_string().into(),
option,
};
Ok(to_any(&proto_msg))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ impl<Address: CosmosAddress> CosmosMessage for DelegateMessage<Address> {
fn to_proto(&self) -> SigningResult<ProtobufMessage> {
let proto_msg = cosmos::staking::v1beta1::MsgDelegate {
amount: Some(build_coin(&self.amount)),
delegator_address: self.delegator_address.to_string(),
validator_address: self.validator_address.to_string(),
delegator_address: self.delegator_address.to_string().into(),
validator_address: self.validator_address.to_string().into(),
};
Ok(to_any(&proto_msg))
}
Expand Down Expand Up @@ -60,8 +60,8 @@ impl<Address: CosmosAddress> CosmosMessage for UndelegateMessage<Address> {
fn to_proto(&self) -> SigningResult<ProtobufMessage> {
let proto_msg = cosmos::staking::v1beta1::MsgUndelegate {
amount: Some(build_coin(&self.amount)),
delegator_address: self.delegator_address.to_string(),
validator_address: self.validator_address.to_string(),
delegator_address: self.delegator_address.to_string().into(),
validator_address: self.validator_address.to_string().into(),
};
Ok(to_any(&proto_msg))
}
Expand Down Expand Up @@ -90,9 +90,9 @@ impl<Address: CosmosAddress> CosmosMessage for BeginRedelegateMessage<Address> {
fn to_proto(&self) -> SigningResult<ProtobufMessage> {
let proto_msg = cosmos::staking::v1beta1::MsgBeginRedelegate {
amount: Some(build_coin(&self.amount)),
delegator_address: self.delegator_address.to_string(),
validator_src_address: self.validator_src_address.to_string(),
validator_dst_address: self.validator_dst_address.to_string(),
delegator_address: self.delegator_address.to_string().into(),
validator_src_address: self.validator_src_address.to_string().into(),
validator_dst_address: self.validator_dst_address.to_string().into(),
};
Ok(to_any(&proto_msg))
}
Expand All @@ -118,8 +118,8 @@ pub struct WithdrawDelegationRewardMessage<Address: CosmosAddress> {
impl<Address: CosmosAddress> CosmosMessage for WithdrawDelegationRewardMessage<Address> {
fn to_proto(&self) -> SigningResult<ProtobufMessage> {
let proto_msg = cosmos::distribution::v1beta1::MsgWithdrawDelegatorReward {
delegator_address: self.delegator_address.to_string(),
validator_address: self.validator_address.to_string(),
delegator_address: self.delegator_address.to_string().into(),
validator_address: self.validator_address.to_string().into(),
};
Ok(to_any(&proto_msg))
}
Expand All @@ -145,8 +145,8 @@ pub struct SetWithdrawAddressMessage<Address: CosmosAddress> {
impl<Address: CosmosAddress> CosmosMessage for SetWithdrawAddressMessage<Address> {
fn to_proto(&self) -> SigningResult<ProtobufMessage> {
let proto_msg = cosmos::distribution::v1beta1::MsgSetWithdrawAddress {
delegator_address: self.delegator_address.to_string(),
withdraw_address: self.withdraw_address.to_string(),
delegator_address: self.delegator_address.to_string().into(),
withdraw_address: self.withdraw_address.to_string().into(),
};
Ok(to_any(&proto_msg))
}
Expand Down
8 changes: 4 additions & 4 deletions rust/tw_cosmos_sdk/src/transaction/message/ibc_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ impl<Address: CosmosAddress> CosmosMessage for TransferTokensMessage<Address> {
};

let proto_msg = ibc::applications::transfer::v1::MsgTransfer {
source_port: self.source_port.clone(),
source_channel: self.source_channel.clone(),
source_port: self.source_port.clone().into(),
source_channel: self.source_channel.clone().into(),
token: Some(build_coin(&self.token)),
sender: self.sender.to_string(),
receiver: self.receiver.to_string(),
sender: self.sender.to_string().into(),
receiver: self.receiver.to_string().into(),
timeout_height: Some(height),
timeout_timestamp: self.timeout_timestamp,
};
Expand Down
2 changes: 1 addition & 1 deletion rust/tw_cosmos_sdk/src/transaction/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub mod terra_wasm_message;
pub mod thorchain_message;
pub mod wasm_message;

pub type ProtobufMessage = google::protobuf::Any;
pub type ProtobufMessage = google::protobuf::Any<'static>;
pub type CosmosMessageBox = Box<dyn CosmosMessage>;
pub type JsonMessage = AnyMsg<Json>;

Expand Down
Loading

0 comments on commit e801cf0

Please sign in to comment.