Skip to content

Commit

Permalink
fixed roundtrip, add add_vkey_witness add_bootstrap_witness, transact…
Browse files Browse the repository at this point in the history
…ion_hash functions for FixedTransaction
  • Loading branch information
lisicky committed Sep 6, 2024
1 parent 66aeedb commit 6d89e3c
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 9 deletions.
13 changes: 13 additions & 0 deletions rust/src/protocol_types/fixed_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::error::JsError;
use crate::*;

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FixedTransaction {
pub(crate) body: TransactionBody,
pub(crate) body_bytes: Vec<u8>,
Expand Down Expand Up @@ -138,6 +139,18 @@ impl FixedTransaction {
self.auxiliary_bytes.clone()
}

pub fn transaction_hash(&self) -> TransactionHash {
self.tx_hash.clone()
}

pub fn add_vkey_witness(&mut self, vkey_witness: &Vkeywitness) {
self.witness_set.add_vkey_witness(vkey_witness.clone());
}

pub fn add_bootstrap_witness(&mut self, bootstrap_witness: &BootstrapWitness) {
self.witness_set.add_bootstrap_witness(bootstrap_witness.clone());
}

pub fn sign_and_add_vkey_signature(&mut self, private_key: &PrivateKey) -> Result<(), JsError> {
let vkey_witness = make_vkey_witness(&self.tx_hash, private_key);
self.witness_set.add_vkey_witness(vkey_witness);
Expand Down
5 changes: 5 additions & 0 deletions rust/src/protocol_types/witnesses/bootstrap_witnesses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ impl BootstrapWitnesses {
dedup,
}
}

#[allow(dead_code)]
pub (crate) fn contains(&self, elem: &BootstrapWitness) -> bool {
self.dedup.contains(elem)
}
}

impl NoneOrEmpty for BootstrapWitnesses {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::*;
#[allow(dead_code)]

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct FixedTxWitnessesSet {
pub(crate) raw_parts: TransactionWitnessSetRaw,
pub(crate) tx_witnesses_set: TransactionWitnessSet,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::*;
use crate::traits::EmptyToNone;

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct TransactionWitnessSetRaw {
pub(crate) vkeys: Option<Vec<u8>>,
pub(crate) native_scripts: Option<Vec<u8>>,
Expand Down
5 changes: 5 additions & 0 deletions rust/src/protocol_types/witnesses/vkeywitnesses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ impl Vkeywitnesses {
}
Self { witnesses, dedup }
}

#[allow(dead_code)]
pub(crate) fn contains(&self, elem: &Vkeywitness) -> bool {
self.dedup.contains(elem)
}
}

impl NoneOrEmpty for Vkeywitnesses {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/serialization/witnesses/fixed_tx_witnesses_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Deserialize for FixedTxWitnessesSet {
where
Self: Sized
{
let (witness_set, raw_parts) = super::transaction_witnesses_set::deserialize(raw, false)?;
let (witness_set, raw_parts) = super::transaction_witnesses_set::deserialize(raw, true)?;
Ok(Self {
tx_witnesses_set: witness_set,
raw_parts
Expand Down
16 changes: 8 additions & 8 deletions rust/src/serialization/witnesses/transaction_witnesses_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ pub(super) fn serialize<'se, W: Write>(
if let Some(field) = &wit_set.vkeys {
if let Some(raw_vkeys) = raw_parts.map(|x| x.vkeys.as_ref()).flatten() {
serializer.write_unsigned_integer(0)?;
serializer.write_bytes(raw_vkeys)?;
serializer.write_raw_bytes(raw_vkeys)?;
} else if !field.is_none_or_empty() {
serializer.write_unsigned_integer(0)?;
field.serialize(serializer)?;
Expand All @@ -234,7 +234,7 @@ pub(super) fn serialize<'se, W: Write>(
if let Some(field) = &wit_set.native_scripts {
if let Some(raw) = raw_parts.as_ref().map(|x| x.native_scripts.as_ref()).flatten() {
serializer.write_unsigned_integer(1)?;
serializer.write_bytes(raw)?;
serializer.write_raw_bytes(raw)?;
} else if !field.is_none_or_empty() {
serializer.write_unsigned_integer(1)?;
//transaction witness set already has deduplicated native scripts
Expand All @@ -244,7 +244,7 @@ pub(super) fn serialize<'se, W: Write>(
if let Some(field) = &wit_set.bootstraps {
if let Some(raw) = raw_parts.as_ref().map(|x| x.bootstraps.as_ref()).flatten() {
serializer.write_unsigned_integer(2)?;
serializer.write_bytes(raw)?;
serializer.write_raw_bytes(raw)?;
} else if !field.is_none_or_empty() {
serializer.write_unsigned_integer(2)?;
field.serialize(serializer)?;
Expand All @@ -256,7 +256,7 @@ pub(super) fn serialize<'se, W: Write>(
if has_plutus_v1 {
if let Some(raw) = raw_parts.as_ref().map(|x| x.plutus_scripts_v1.as_ref()).flatten() {
serializer.write_unsigned_integer(3)?;
serializer.write_bytes(raw)?;
serializer.write_raw_bytes(raw)?;
} else {
serializer.write_unsigned_integer(3)?;
plutus_scripts.serialize_as_set_by_version(false, &Language::new_plutus_v1(), serializer)?;
Expand All @@ -265,7 +265,7 @@ pub(super) fn serialize<'se, W: Write>(
if has_plutus_v2 {
if let Some(raw) = raw_parts.as_ref().map(|x| x.plutus_scripts_v2.as_ref()).flatten() {
serializer.write_unsigned_integer(6)?;
serializer.write_bytes(raw)?;
serializer.write_raw_bytes(raw)?;
} else {
serializer.write_unsigned_integer(6)?;
plutus_scripts.serialize_as_set_by_version(false, &Language::new_plutus_v2(), serializer)?;
Expand All @@ -274,7 +274,7 @@ pub(super) fn serialize<'se, W: Write>(
if has_plutus_v3 {
if let Some(raw) = raw_parts.as_ref().map(|x| x.plutus_scripts_v3.as_ref()).flatten() {
serializer.write_unsigned_integer(7)?;
serializer.write_bytes(raw)?;
serializer.write_raw_bytes(raw)?;
} else {
serializer.write_unsigned_integer(7)?;
plutus_scripts.serialize_as_set_by_version(false, &Language::new_plutus_v3(), serializer)?;
Expand All @@ -284,7 +284,7 @@ pub(super) fn serialize<'se, W: Write>(
if let Some(field) = &wit_set.plutus_data {
if let Some(raw) = raw_parts.as_ref().map(|x| x.plutus_data.as_ref()).flatten() {
serializer.write_unsigned_integer(4)?;
serializer.write_bytes(&raw)?;
serializer.write_raw_bytes(&raw)?;
} else if !field.is_none_or_empty() {
serializer.write_unsigned_integer(4)?;
//transaction witness set already has deduplicated plutus data
Expand All @@ -294,7 +294,7 @@ pub(super) fn serialize<'se, W: Write>(
if let Some(field) = &wit_set.redeemers {
if let Some(raw) = raw_parts.as_ref().map(|x| x.redeemers.as_ref()).flatten() {
serializer.write_unsigned_integer(5)?;
serializer.write_bytes(raw)?;
serializer.write_raw_bytes(raw)?;
} else if !field.is_none_or_empty() {
serializer.write_unsigned_integer(5)?;
field.serialize(serializer)?;
Expand Down
101 changes: 101 additions & 0 deletions rust/src/tests/protocol_types/fixed_tx.rs

Large diffs are not rendered by default.

0 comments on commit 6d89e3c

Please sign in to comment.