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

Add decode command for event, message and constructor data decoding #481

Merged
merged 22 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 2 additions & 0 deletions src/cmd/extrinsics/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ impl CallCommand {
let (_, contract_metadata) = load_metadata(self.extrinsic_opts.manifest_path.as_ref())?;
let transcoder = ContractMessageTranscoder::new(&contract_metadata);
let call_data = transcoder.encode(&self.message, &self.args)?;
log::debug!("message data: {:?}", hex::encode(&call_data));

let signer = super::pair_signer(self.extrinsic_opts.signer()?);

async_std::task::block_on(async {
Expand Down
63 changes: 63 additions & 0 deletions src/cmd/extrinsics/decode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
ascjones marked this conversation as resolved.
Show resolved Hide resolved
// This file is part of cargo-contract.
//
// cargo-contract is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// cargo-contract is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.

use super::load_metadata;
use crate::cmd::extrinsics::ContractMessageTranscoder;
use crate::DEFAULT_KEY_COL_WIDTH;
use anyhow::Result;
use colored::Colorize as _;

#[derive(Debug, Clone, clap::Args)]
#[clap(name = "decode", about = "Decode input_data for a contract")]
pub struct DecodeCommand {
/// Type of data
#[clap(arg_enum, short, long)]
r#type: DataType,
/// The data to decode
#[clap(long)]
data: String,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ArgEnum)]
enum DataType {
Event,
Message,
}

impl DecodeCommand {
pub fn run(&self) -> Result<()> {
let (_, contract_metadata) = load_metadata(None)?;
let transcoder = ContractMessageTranscoder::new(&contract_metadata);

let decoded_data = match self.r#type {
DataType::Event => {
transcoder.decode_contract_event(&mut &hex::decode(&self.data).unwrap()[..])
ascjones marked this conversation as resolved.
Show resolved Hide resolved
}
DataType::Message => {
transcoder.decode_contract_message(&mut &hex::decode(&self.data).unwrap()[..])
}
};

println!(
"{:>width$} {:?}",
"Decoded data:".bright_green().bold(),
decoded_data,
width = DEFAULT_KEY_COL_WIDTH
);

Ok(())
}
}
1 change: 1 addition & 0 deletions src/cmd/extrinsics/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub fn display_events(
if <ContractEmitted as Event>::is_event(&event.pallet, &event.variant)
&& field.name() == Some(&"data".to_string())
{
log::debug!("event data: {:?}", hex::encode(&event_data));
let contract_event = transcoder.decode_contract_event(event_data)?;
maybe_println!(
verbosity,
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/extrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.

mod call;
mod decode;
mod events;
mod instantiate;
mod runtime_api;
Expand All @@ -38,6 +39,7 @@ use sp_core::{crypto::Pair, sr25519};
use subxt::{Config, DefaultConfig};

pub use call::CallCommand;
pub use decode::DecodeCommand;
pub use instantiate::InstantiateCommand;
pub use runtime_api::api::{DispatchError as RuntimeDispatchError, Event as RuntimeEvent};
pub use upload::UploadCommand;
Expand Down
43 changes: 42 additions & 1 deletion src/cmd/extrinsics/transcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ impl<'a> ContractMessageTranscoder<'a> {
// data is an encoded `Vec<u8>` so is prepended with its length `Compact<u32>`, which we
// ignore because the structure of the event data is known for decoding.
let _len = <Compact<u32>>::decode(data)?;

let variant_index = data.read_byte()?;
let event_spec = self
.metadata
Expand Down Expand Up @@ -203,6 +202,37 @@ impl<'a> ContractMessageTranscoder<'a> {
Ok(Value::Map(map))
}

pub fn decode_contract_message(&self, data: &mut &[u8]) -> Result<Value> {
let mut msg_selector = [0u8; 4];
data.read(&mut msg_selector)?;
let msg_spec = self
.metadata
.spec()
.messages()
ascjones marked this conversation as resolved.
Show resolved Hide resolved
.iter()
.filter(|x| msg_selector == x.selector().to_bytes())
.next()
.ok_or_else(|| {
anyhow::anyhow!(
"Message with selector {} not found in contract metadata",
hex::encode(&msg_selector)
)
})?;
log::debug!("decoding contract message '{}'", msg_spec.label());

let mut args = Vec::new();
for arg in msg_spec.args() {
let name = arg.label().to_string();
let value = self.transcoder.decode(arg.ty().ty().id(), data)?;
args.push((Value::String(name), value));
}

let name = msg_spec.label().to_string();
let map = Map::new(Some(&name), args.into_iter().collect());

Ok(Value::Map(map))
}

pub fn decode_return(&self, name: &str, data: &mut &[u8]) -> Result<Value> {
let msg_spec = self
.find_message_spec(name)
Expand Down Expand Up @@ -391,4 +421,15 @@ mod tests {

Ok(())
}

#[test]
fn decode_contract_message() -> Result<()> {
let metadata = generate_metadata();
let transcoder = ContractMessageTranscoder::new(&metadata);

let encoded_bytes = hex::decode("633aa551").unwrap();
let _ = transcoder.decode_contract_message(&mut &encoded_bytes[..])?;

Ok(())
}
}
2 changes: 1 addition & 1 deletion src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ pub(crate) use self::{
};
mod extrinsics;

pub(crate) use self::extrinsics::{CallCommand, InstantiateCommand, UploadCommand};
pub(crate) use self::extrinsics::{CallCommand, DecodeCommand, InstantiateCommand, UploadCommand};
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ mod workspace;

use self::{
cmd::{
metadata::MetadataResult, BuildCommand, CallCommand, CheckCommand, InstantiateCommand,
TestCommand, UploadCommand,
metadata::MetadataResult, BuildCommand, CallCommand, CheckCommand, DecodeCommand,
InstantiateCommand, TestCommand, UploadCommand,
},
util::DEFAULT_KEY_COL_WIDTH,
workspace::ManifestPath,
Expand Down Expand Up @@ -454,6 +454,9 @@ enum Command {
/// Call a contract
#[clap(name = "call")]
Call(CallCommand),
/// Decode a contract input data
#[clap(name = "decode")]
Decode(DecodeCommand),
}

fn main() {
Expand Down Expand Up @@ -511,6 +514,7 @@ fn exec(cmd: Command) -> Result<()> {
Command::Upload(upload) => upload.run(),
Command::Instantiate(instantiate) => instantiate.run(),
Command::Call(call) => call.run(),
Command::Decode(decode) => decode.run(),
}
}

Expand Down