Skip to content

Commit

Permalink
fix: genesis hash input is optional, added genesis hash card
Browse files Browse the repository at this point in the history
  • Loading branch information
varovainen committed Oct 30, 2023
1 parent ccace30 commit a034270
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 126 deletions.
4 changes: 3 additions & 1 deletion src/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ impl ParsedData {
}],
ParsedData::Era(value) => single_card!(Era, value, indent, info_flat),
ParsedData::Event(event) => event.card(indent, short_specs, spec_name),
ParsedData::GenesisHash(_) => Vec::new(),
ParsedData::GenesisHash(value) => single_card!(GenesisHash, value, indent, info_flat),
ParsedData::H160(value) => single_card!(H160, value, indent, info_flat),
ParsedData::H256(value) => single_card!(H256, value, indent, info_flat),
ParsedData::H512(value) => single_card!(H512, value, indent, info_flat),
Expand Down Expand Up @@ -983,6 +983,7 @@ pub enum ParserCard {
EventName(String),
FieldName(String),
FieldNumber(usize),
GenesisHash(H256),
H160(H160),
H256(H256),
H512(H512),
Expand Down Expand Up @@ -1134,6 +1135,7 @@ impl ExtendedCard {
ParserCard::EventName(a) => readable(self.indent, "Event", a),
ParserCard::FieldName(a) => readable(self.indent, "Field Name", a),
ParserCard::FieldNumber(a) => readable(self.indent, "Field Number", &a.to_string()),
ParserCard::GenesisHash(a) => readable(self.indent, "Genesis Hash", &hex::encode(a)),
ParserCard::H160(a) => readable(self.indent, "H160", &hex::encode(a.0)),
ParserCard::H256(a) => readable(self.indent, "H256", &hex::encode(a.0)),
ParserCard::H512(a) => readable(self.indent, "H512", &hex::encode(a.0)),
Expand Down
34 changes: 22 additions & 12 deletions src/decoding_sci_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn decode_extensions<B, E, M>(
marked_data: &MarkedData<B, E>,
ext_memory: &mut E,
meta_v14: &M,
genesis_hash: H256,
optional_genesis_hash: Option<H256>,
) -> Result<Vec<ExtendedData>, SignableError<E>>
where
B: AddressableBuffer<E>,
Expand All @@ -48,7 +48,13 @@ where
let mut position = marked_data.extensions_start();
let data = marked_data.data();

decode_extensions_unmarked(data, &mut position, ext_memory, meta_v14, genesis_hash)
decode_extensions_unmarked(
data,
&mut position,
ext_memory,
meta_v14,
optional_genesis_hash,
)
}

/// Parse extensions part of the signable transaction using provided metadata.
Expand All @@ -68,7 +74,7 @@ pub fn decode_extensions_unmarked<B, E, M>(
position: &mut usize,
ext_memory: &mut E,
meta_v14: &M,
genesis_hash: H256,
optional_genesis_hash: Option<H256>,
) -> Result<Vec<ExtendedData>, SignableError<E>>
where
B: AddressableBuffer<E>,
Expand Down Expand Up @@ -113,7 +119,7 @@ where
check_extensions::<E>(
&extensions,
&spec_name_version.printed_spec_version,
genesis_hash,
optional_genesis_hash,
)?;
Ok(extensions)
}
Expand All @@ -126,7 +132,7 @@ where
fn check_extensions<E: ExternalMemory>(
extensions: &[ExtendedData],
version: &str,
genesis_hash: H256,
optional_genesis_hash: Option<H256>,
) -> Result<(), SignableError<E>> {
let mut collected_ext = CollectedExt::new();
for ext in extensions.iter() {
Expand Down Expand Up @@ -156,11 +162,13 @@ fn check_extensions<E: ExternalMemory>(
}
match collected_ext.genesis_hash {
Some(found_genesis_hash) => {
if found_genesis_hash != genesis_hash {
return Err(SignableError::WrongGenesisHash {
as_decoded: found_genesis_hash,
expected: genesis_hash,
});
if let Some(genesis_hash) = optional_genesis_hash {
if found_genesis_hash != genesis_hash {
return Err(SignableError::WrongGenesisHash {
as_decoded: found_genesis_hash,
expected: genesis_hash,
});
}
}
}
None => {
Expand All @@ -171,8 +179,10 @@ fn check_extensions<E: ExternalMemory>(
}
if let Some(Era::Immortal) = collected_ext.era {
if let Some(block_hash) = collected_ext.block_hash {
if genesis_hash != block_hash {
return Err(SignableError::ImmortalHashMismatch);
if let Some(genesis_hash) = collected_ext.genesis_hash {
if genesis_hash != block_hash {
return Err(SignableError::ImmortalHashMismatch);
}
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
//! &signable_data.as_ref(),
//! &mut (),
//! &metadata_westend9111,
//! westend_genesis_hash,
//! Some(westend_genesis_hash),
//! ).unwrap();
//!
//! let call_data = parsed.call_result.unwrap();
Expand Down Expand Up @@ -620,7 +620,7 @@ pub fn parse_transaction<B, E, M>(
data: &B,
ext_memory: &mut E,
meta_v14: &M,
genesis_hash: H256,
optional_genesis_hash: Option<H256>,
) -> Result<TransactionParsed<E>, SignableError<E>>
where
B: AddressableBuffer<E>,
Expand All @@ -634,7 +634,7 @@ where
// try parsing extensions, check that spec version and genesis hash are
// correct
let extensions =
decode_extensions::<B, E, M>(&marked_data, ext_memory, meta_v14, genesis_hash)?;
decode_extensions::<B, E, M>(&marked_data, ext_memory, meta_v14, optional_genesis_hash)?;

// try parsing call data
let call_result = decode_as_call::<B, E, M>(&marked_data, ext_memory, meta_v14);
Expand Down Expand Up @@ -683,7 +683,7 @@ pub fn parse_transaction_unmarked<B, E, M>(
data: &B,
ext_memory: &mut E,
meta_v14: &M,
genesis_hash: H256,
optional_genesis_hash: Option<H256>,
) -> Result<TransactionUnmarkedParsed, SignableError<E>>
where
B: AddressableBuffer<E>,
Expand All @@ -702,7 +702,7 @@ where
&mut position,
ext_memory,
meta_v14,
genesis_hash,
optional_genesis_hash,
)?;

Ok(TransactionUnmarkedParsed { call, extensions })
Expand Down
Loading

0 comments on commit a034270

Please sign in to comment.