Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cardano-serialization-lib",
"version": "15.0.0",
"version": "15.0.1",
"description": "(De)serialization functions for the Cardano blockchain along with related utility functions",
"scripts": {
"publish-all:prod": "node scripts/build-helper.js publish-all --env prod",
Expand Down
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardano-serialization-lib"
version = "15.0.0"
version = "15.0.1"
edition = "2018"
authors = ["EMURGO"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion rust/json-gen/Cargo.lock

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

25 changes: 17 additions & 8 deletions rust/src/protocol_types/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ use ed25519_bip32::XPub;

// returns (Number represented, bytes read) if valid encoding
// or None if decoding prematurely finished
pub(crate) fn variable_nat_decode(bytes: &[u8]) -> Option<(u64, usize)> {
pub(crate) fn variable_nat_decode(bytes: &[u8], fallback_to_max: bool) -> Option<(u64, usize)> {
let mut output = 0u128;
let mut bytes_read = 0;
for byte in bytes {
output = (output << 7) | (byte & 0x7F) as u128;
if output > u64::MAX.into() {
return None;
if output < u64::MAX.into() || !fallback_to_max {
output = (output << 7) | (byte & 0x7F) as u128;
}
//Since Conway era forbids pointer addresses, technically such large numbers would not make sense on mainnet
if output > u64::MAX.into(){
if !fallback_to_max {
return None;
}
output = u64::MAX.into();
}
bytes_read += 1;
if (byte & 0x80) == 0 {
Expand Down Expand Up @@ -399,7 +405,10 @@ impl Address {
fn from_bytes_impl_unsafe(data: &[u8]) -> Address {
match Self::from_bytes_internal_impl(data, true) {
Ok(addr) => addr,
Err(_) => Address(AddrType::Malformed(MalformedAddress(data.to_vec())))
Err(err) => {
println!("Address deserialization error: {:?}", err);
return Address(AddrType::Malformed(MalformedAddress(data.to_vec())));
}
}
}

Expand Down Expand Up @@ -540,19 +549,19 @@ impl Address {

fn decode_pointer(data: &[u8]) -> Result<(Pointer, usize), DeserializeError> {
let mut offset = 0;
let (slot, slot_bytes) = variable_nat_decode(&data).ok_or(DeserializeError::new(
let (slot, slot_bytes) = variable_nat_decode(&data, true).ok_or(DeserializeError::new(
"Address.Pointer.slot",
DeserializeFailure::VariableLenNatDecodeFailed,
))?;
offset += slot_bytes;
let (tx_index, tx_bytes) =
variable_nat_decode(&data[offset..]).ok_or(DeserializeError::new(
variable_nat_decode(&data[offset..], true).ok_or(DeserializeError::new(
"Address.Pointer.tx_index",
DeserializeFailure::VariableLenNatDecodeFailed,
))?;
offset += tx_bytes;
let (cert_index, cert_bytes) =
variable_nat_decode(&data[offset..]).ok_or(DeserializeError::new(
variable_nat_decode(&data[offset..], true).ok_or(DeserializeError::new(
"Address.Pointer.cert_index",
DeserializeFailure::VariableLenNatDecodeFailed,
))?;
Expand Down
10 changes: 8 additions & 2 deletions rust/src/tests/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ fn variable_nat_encoding() {
let cases = [0u64, 127u64, 128u64, 255u64, 256275757658493284u64];
for case in cases.iter() {
let encoded = variable_nat_encode(*case);
let decoded = variable_nat_decode(&encoded).unwrap().0;
let decoded = variable_nat_decode(&encoded, true).unwrap().0;
assert_eq!(*case, decoded);
}
}

#[test]
fn variable_nat_decode_too_big() {
let too_big = [129, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127];
assert_eq!(None, variable_nat_decode(&too_big));
assert_eq!(None, variable_nat_decode(&too_big, false));
}

#[test]
fn variable_nat_decode_too_big_fallback() {
let too_big = [129, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127];
assert_eq!(Some((u64::MAX, 11usize)), variable_nat_decode(&too_big, true));
}

#[test]
Expand Down