Skip to content

Commit

Permalink
feat(applying): use minicbor for transaction data serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
MaicoLeberle committed Nov 8, 2023
1 parent aeb1a1a commit cf3067f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
1 change: 0 additions & 1 deletion pallas-applying/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ authors = ["Maico Leberle <[email protected]>"]
doctest = false

[dependencies]
cbor_event = "^2.1.1"
pallas-addresses = { path = "../pallas-addresses" }
pallas-codec = { path = "../pallas-codec" }
pallas-crypto = { path = "../pallas-crypto" }
Expand Down
43 changes: 19 additions & 24 deletions pallas-applying/src/byron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use crate::types::{
ValidationResult,
};

use cbor_event::se::Serializer;
use pallas_addresses::byron::{
AddrAttrs, AddrType, AddressId, AddressPayload, ByronAddress, SpendingData,
};
use pallas_codec::{minicbor::encode, utils::CborWrap};
use pallas_codec::{
minicbor::{encode, Encoder},
utils::CborWrap,
};
use pallas_crypto::{
hash::Hash,
key::ed25519::{PublicKey, Signature},
Expand Down Expand Up @@ -210,23 +212,6 @@ fn mk_spending_data(pub_key: &PubKey, addr_type: &AddrType) -> SpendingData {
}
}

fn serialize_tag(
se: &mut Serializer<Vec<u8>>,
sign: &TaggedSignature,
) -> Result<(), ValidationError> {
match sign {
TaggedSignature::PkWitness(_) => {
se.write_unsigned_integer(SigningTag::Tx as u64)
.map_err(|_| ValidationError::UnableToProcessWitnesses)?;
}
TaggedSignature::RedeemWitness(_) => {
se.write_unsigned_integer(SigningTag::RedeemTx as u64)
.map_err(|_| ValidationError::UnableToProcessWitnesses)?;
}
}
Ok(())
}

fn get_verification_key(pk: &PubKey) -> PublicKey {
let mut trunc_len: [u8; PublicKey::SIZE] = [0; PublicKey::SIZE];
trunc_len.copy_from_slice(&pk.as_slice()[0..PublicKey::SIZE]);
Expand All @@ -238,13 +223,23 @@ fn get_data_to_verify(
prot_magic: &u32,
tx_hash: &Hash<32>,
) -> Result<Vec<u8>, ValidationError> {
let mut se = Serializer::new_vec();
serialize_tag(&mut se, sign)?;
se.serialize(&prot_magic)
let buff: &mut Vec<u8> = &mut Vec::new();
let mut enc: Encoder<&mut Vec<u8>> = Encoder::new(buff);
match sign {
TaggedSignature::PkWitness(_) => {
enc.encode(SigningTag::Tx as u64)
.map_err(|_| ValidationError::UnableToProcessWitnesses)?;
}
TaggedSignature::RedeemWitness(_) => {
enc.encode(SigningTag::RedeemTx as u64)
.map_err(|_| ValidationError::UnableToProcessWitnesses)?;
}
}
enc.encode(prot_magic)
.map_err(|_| ValidationError::UnableToProcessWitnesses)?;
se.serialize(&tx_hash.as_ref())
enc.encode(tx_hash)
.map_err(|_| ValidationError::UnableToProcessWitnesses)?;
Ok(se.finalize())
Ok(enc.into_writer().clone())
}

fn get_signature(tagged_signature: &TaggedSignature<'_>) -> Signature {
Expand Down

0 comments on commit cf3067f

Please sign in to comment.