Skip to content

Commit

Permalink
fix: unwrap changed with address parsing error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mikarasv committed Jun 4, 2024
1 parent e312960 commit 5cb9aac
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
8 changes: 7 additions & 1 deletion napi-pallas/src/validations/alonzo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,13 @@ fn from_amounts(amounts: &Vec<TxContentOutputAmountInner>) -> Value {
}

fn from_tx_in(tx_in: &TxContentUtxoOutputsInner) -> (String, Value, Option<Hash<32>>) {
let address = Address::from_bech32(&tx_in.address).unwrap();
let address = match Address::from_bech32(&tx_in.address) {
Ok(addr) => addr,
_ => {
println!("Error parsing address: {:?}", tx_in.address);
return ("".to_string(), Value::Coin(0), None);
}
};
let value = from_amounts(&tx_in.amount);

let datum_opt: Option<Hash<32>> = match &tx_in.data_hash {
Expand Down
5 changes: 4 additions & 1 deletion napi-pallas/src/validations/babbage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,10 @@ fn from_tx_in(
) {
let address = match Address::from_bech32(&tx_in.address) {
Ok(addr) => addr,
_ => return ("Address Not Found".to_string(), Value::Coin(0), None, None),
_ => {
println!("Error parsing address: {:?}", tx_in.address);
return ("".to_string(), Value::Coin(0), None, None);
}
};
let value = from_amounts(&tx_in.amount);

Expand Down
51 changes: 35 additions & 16 deletions napi-pallas/src/validations/byron.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{borrow::Cow, iter::zip};

use crate::{tx::get_input, Validation, ValidationContext, Validations};
use blockfrost_openapi::models::tx_content_utxo_outputs_inner::TxContentUtxoOutputsInner;
use pallas::{
applying::{
byron::{
Expand All @@ -10,7 +11,10 @@ use pallas::{
utils::ByronProtParams,
UTxOs,
},
codec::minicbor::encode,
codec::{
minicbor::{bytes::ByteVec, encode},
utils::TagWrap,
},
ledger::{
addresses::ByronAddress,
primitives::byron::{Address as PrimitiveAddress, MintedTxPayload, Tx, TxIn, TxOut},
Expand Down Expand Up @@ -144,6 +148,32 @@ pub fn mk_utxo_for_byron_tx<'a>(tx: &Tx, tx_outs_info: &Vec<(ByronAddress, u64)>
utxos
}

fn from_tx_in(tx_in: &TxContentUtxoOutputsInner) -> (ByronAddress, u64) {
let address = match ByronAddress::from_base58(&tx_in.address) {
Ok(a) => a,
Err(_) => {
println!("Error parsing address {:?}", tx_in.address);
let addr = ByronAddress {
payload: TagWrap(ByteVec::from(vec![])),
crc: 0,
};
return (addr, 0);
}
};

let mut lovelace_am = 0;
for amt in &tx_in.amount {
match amt.quantity.parse::<u64>() {
Ok(a) => lovelace_am += a,
Err(_) => {
// TODO: Handle error appropriately
continue; // Skip this iteration if parsing fails
}
}
}
(address, lovelace_am)
}

pub async fn validate_byron(mtxp: &MintedTxPayload<'_>, context: ValidationContext) -> Validations {
let tx: &Tx = &mtxp.transaction;
let size: &u64 = &get_tx_size(&tx);
Expand Down Expand Up @@ -183,21 +213,10 @@ pub async fn validate_byron(mtxp: &MintedTxPayload<'_>, context: ValidationConte
}
}
let mut tx_outs_info = vec![];
for tx_in in inputs {
let address = ByronAddress::from_base58(&tx_in.address).unwrap();

let mut lovelace_am = 0;
for amt in &tx_in.amount {
match amt.quantity.parse::<u64>() {
Ok(a) => lovelace_am += a,
Err(_) => {
// TODO: Handle error appropriately
continue; // Skip this iteration if parsing fails
}
}
}
tx_outs_info.push((address, lovelace_am));
}
inputs.iter().for_each(|tx_in| {
tx_outs_info.push(from_tx_in(&tx_in));
});

let utxos = mk_utxo_for_byron_tx(&mtxp.transaction, &tx_outs_info);
let mut magic = 764824073; // For mainnet
if context.network == "Preprod" {
Expand Down
8 changes: 7 additions & 1 deletion napi-pallas/src/validations/shelley_ma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ fn from_amounts(amounts: &Vec<TxContentOutputAmountInner>) -> Value {
}

fn from_tx_in(tx_in: &TxContentUtxoOutputsInner) -> (String, Value, Option<Hash<32>>) {
let address = Address::from_bech32(&tx_in.address).unwrap();
let address = match Address::from_bech32(&tx_in.address) {
Ok(addr) => addr,
Err(_) => {
println!("Error parsing address: {:?}", tx_in.address);
return ("".to_string(), Value::Coin(0), None);
}
};
let value = from_amounts(&tx_in.amount);

let datum_opt: Option<Hash<32>> = match &tx_in.data_hash {
Expand Down

0 comments on commit 5cb9aac

Please sign in to comment.