Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
refactor: Refactor get_sign_bytes in pallet-cosmos-x-auth-signing
Browse files Browse the repository at this point in the history
  • Loading branch information
code0xff committed Sep 13, 2024
1 parent 91224f3 commit 3e3522c
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions frame/cosmos/x/auth/signing/src/sign_mode_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ use alloc::{
use cosmos_sdk_proto::{
cosmos::{
bank,
tx::v1beta1::{
mode_info::{Single, Sum},
ModeInfo, SignDoc, Tx, TxRaw,
tx::{
signing::v1beta1::SignMode,
v1beta1::{
mode_info::{Single, Sum},
ModeInfo, SignDoc, Tx, TxRaw,
},
},
},
cosmwasm::wasm,
Expand Down Expand Up @@ -64,6 +67,9 @@ pub enum SignModeHandlerError {
UnsupportedMode,
}

const SIGN_MODE_DIRECT: i32 = SignMode::Direct as i32;
const SIGN_MODE_LEGACY_AMINO_JSON: i32 = SignMode::LegacyAminoJson as i32;

pub struct SignModeHandler;
impl traits::SignModeHandler for SignModeHandler {
fn get_sign_bytes(
Expand All @@ -73,17 +79,19 @@ impl traits::SignModeHandler for SignModeHandler {
) -> Result<Vec<u8>, SignModeHandlerError> {
let sum = mode.sum.as_ref().ok_or(SignModeHandlerError::EmptyModeInfo)?;
let sign_bytes = match sum {
Sum::Single(Single { mode }) => match mode {
1 /* SIGN_MODE_DIRECT */ => {
let tx_raw = TxRaw::decode(&mut &*tx.encode_to_vec()).map_err(|_| SignModeHandlerError::DecodeTxError)?;
Sum::Single(Single { mode }) => match *mode {
SIGN_MODE_DIRECT => {
let tx_raw = TxRaw::decode(&mut &*tx.encode_to_vec())
.map_err(|_| SignModeHandlerError::DecodeTxError)?;
SignDoc {
body_bytes: tx_raw.body_bytes,
auth_info_bytes: tx_raw.auth_info_bytes,
chain_id: data.chain_id.clone(),
account_number: data.account_number,
}.encode_to_vec()
}
.encode_to_vec()
},
127 /* SIGN_MODE_LEGACY_AMINO_JSON */ => {
SIGN_MODE_LEGACY_AMINO_JSON => {
let body = tx.body.as_ref().ok_or(SignModeHandlerError::EmptyTxBody)?;
let mut msgs = Vec::<Value>::new();
for msg in body.messages.iter() {
Expand All @@ -100,7 +108,11 @@ impl traits::SignModeHandler for SignModeHandler {

msgs.push(legacy_msg);
}
let fee = tx.auth_info.as_ref().and_then(|auth_info| auth_info.fee.as_ref()).ok_or(SignModeHandlerError::EmptyFee)?;
let fee = tx
.auth_info
.as_ref()
.and_then(|auth_info| auth_info.fee.as_ref())
.ok_or(SignModeHandlerError::EmptyFee)?;
let sign_doc = StdSignDoc {
account_number: data.account_number.to_string(),
chain_id: data.chain_id.clone(),
Expand All @@ -110,7 +122,8 @@ impl traits::SignModeHandler for SignModeHandler {
sequence: data.sequence.to_string(),
};

serde_json::to_vec(&sign_doc).map_err(|_| SignModeHandlerError::SerializeError)?
serde_json::to_vec(&sign_doc)
.map_err(|_| SignModeHandlerError::SerializeError)?
},
_ => return Err(SignModeHandlerError::UnsupportedMode),
},
Expand Down

0 comments on commit 3e3522c

Please sign in to comment.