whisky_pallas/wrapper/transaction_body/
transaction.rs1use crate::wrapper::{
2 auxiliary_data::auxiliary_data::AuxiliaryData,
3 transaction_body::transaction_body::TransactionBody, witness_set::witness_set::WitnessSet,
4};
5use pallas::{
6 codec::utils::{KeepRaw, Nullable},
7 ledger::primitives::{conway::Tx as PallasTx, Fragment},
8};
9use whisky_common::WError;
10
11#[derive(Clone, Debug)]
12pub struct Transaction<'a> {
13 pub inner: PallasTx<'a>,
14}
15
16impl<'a> Transaction<'a> {
17 pub fn new(
18 transaction_body: TransactionBody<'a>,
19 transaction_witness_set: WitnessSet<'a>,
20 success: bool,
21 auxiliary_data: Option<AuxiliaryData>,
22 ) -> Result<Self, WError> {
23 let inner = PallasTx {
24 transaction_body: KeepRaw::from(transaction_body.inner),
25 transaction_witness_set: KeepRaw::from(transaction_witness_set.inner),
26 success,
27 auxiliary_data: match auxiliary_data {
28 Some(aux_data) => Nullable::Some(KeepRaw::from(aux_data.inner)),
29 None => Nullable::Null,
30 },
31 };
32
33 Ok(Self { inner })
34 }
35
36 pub fn encode(&self) -> Result<String, WError> {
37 self.inner
38 .encode_fragment()
39 .map(|bytes| hex::encode(bytes))
40 .map_err(|_| {
41 WError::new(
42 "WhiskyPallas - Encoding transaction:",
43 "Failed to encode fragment",
44 )
45 })
46 }
47
48 pub fn decode_bytes(bytes: &'a [u8]) -> Result<Self, WError> {
49 let inner = PallasTx::decode_fragment(&bytes).map_err(|e| {
50 WError::new(
51 "WhiskyPallas - Decoding transaction:",
52 &format!("Fragment decode error: {}", e.to_string()),
53 )
54 })?;
55 Ok(Self { inner })
56 }
57}