Skip to content

Commit

Permalink
Merge pull request #42 from Zondax/dev
Browse files Browse the repository at this point in the history
New Release
  • Loading branch information
emmanuelm41 authored May 10, 2024
2 parents 92593d3 + 308d057 commit 7ae6569
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 141 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
8 changes: 2 additions & 6 deletions docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,12 @@ components:
description: Tx shortened metadata on hex format
TxPayload:
required:
- callData
- signedExtensions
- txBlob
- chainConfig
properties:
callData:
txBlob:
type: string
description: Transaction payload to be signed, in serialized format
signedExtensions:
type: string
description: Signed extensions as complementary data required to the signing process, in serialized format
chain:
type: object
properties:
Expand Down
4 changes: 3 additions & 1 deletion rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ frame-metadata = { version = "16.0.0", default-features = false, features = [
"decode",
] }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
merkleized-metadata = { git = "https://github.com/Zondax/merkleized-metadata", default-features = false, rev = "7bfc09349f6ca29e1950644116d786a3aa140a40" }
merkleized-metadata = { git = "https://github.com/Zondax/merkleized-metadata", default-features = false, rev = "cd1363a2c4702abf34fcc461055f0059b3c32bec" }
neon = "1.0.0"
parity-scale-codec = { version = "3.6.9", default-features = false }
scale-info = { version = "2.11.1", default-features = false }
array-bytes = { version = "6.2.2", default-features = false }
scale-decode = { version = "0.12.0", default-features = false }
19 changes: 12 additions & 7 deletions rust/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { ChainProps } from '../src/types'
import { ChainProps } from "../src/types";

export interface RootHashParams {
metadata: string
props: ChainProps
metadata: string;
props: ChainProps;
}

export interface MetadataParams extends RootHashParams {
callData: string
signedExtensions: string
callData: string;
seIncludedInExtrinsic: string;
seIncludedInSignedData: string;
}
export interface MetadataParamsTxBlob extends RootHashParams {
txBlob: string;
}

export declare function getShortMetadata(params: MetadataParams): string
export declare function getMetadataDigest(params: RootHashParams): string
export declare function getShortMetadataFromTxBlob(params: MetadataParamsTxBlob): string;
export declare function getShortMetadata(params: MetadataParams): string;
export declare function getMetadataDigest(params: RootHashParams): string;
77 changes: 77 additions & 0 deletions rust/src/helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use frame_metadata::RuntimeMetadata;
use merkleized_metadata::{FrameMetadataPrepared, SignedExtrinsicData,TypeResolver,CollectAccessedTypes};
use scale_decode::visitor::decode_with_visitor;

pub fn get_parts_len_from_tx_blob(
mut tx_blob: &[u8],
metadata: &RuntimeMetadata,
) -> Result<Vec<usize>, String> {
let mut call_data_len:usize = 0;
let mut signed_extensions_in_extrinsic_len:usize = 0;

let prepared = FrameMetadataPrepared::prepare(metadata)?;
let type_information = prepared.as_type_information()?;

let tx_blob_ptr = &mut tx_blob;
let tx_blob_ptr_init_len = tx_blob_ptr.len();

let type_resolver = TypeResolver::new(type_information.types.values());

let visitor = CollectAccessedTypes::default();

let mut visitor = decode_with_visitor(
tx_blob_ptr,
type_information.extrinsic_metadata.call_ty,
&type_resolver,
visitor,
)
.map_err(|e| format!("Failed to decode call: {e}"))?;

call_data_len = tx_blob_ptr_init_len - tx_blob_ptr.len();

if !tx_blob_ptr.is_empty() {
let included_in_extrinsic = &tx_blob_ptr.to_vec();
let signed_ext_data:Option<SignedExtrinsicData> = Some(SignedExtrinsicData{
included_in_extrinsic,
included_in_signed_data: &[],
});

let visitor = signed_ext_data
.map(|mut signed_ext_data| {
visitor.collect_all_types(
&type_information.extrinsic_metadata.address_ty,
&type_information,
);
visitor.collect_all_types(
&type_information.extrinsic_metadata.signature_ty,
&type_information,
);

let included_in_extrinsic_ptr = &mut signed_ext_data.included_in_extrinsic;
let initial_len = included_in_extrinsic_ptr.len();

let fold_result = type_information
.extrinsic_metadata
.signed_extensions
.iter()
.try_fold(visitor.clone(), |visitor, se| {
decode_with_visitor(
included_in_extrinsic_ptr,
se.included_in_extrinsic,
&type_resolver,
visitor,
)
.map_err(|e| format!("Failed to decode data in extrinsic ({}): {e}", se.identifier))
});

// Handle the fold result and calculate the length
fold_result.map(|final_visitor| {
signed_extensions_in_extrinsic_len = initial_len - included_in_extrinsic_ptr.len();
final_visitor
})
})
.unwrap_or_else(|| Ok(visitor))?; // Handle error case or provide default
}

return Ok(Vec::from([call_data_len, signed_extensions_in_extrinsic_len]))
}
Loading

0 comments on commit 7ae6569

Please sign in to comment.