Skip to content

Commit 038c409

Browse files
venilinvasilevtr11
authored andcommitted
fix: refer signed sources (hiero-ledger#1080)
Signed-off-by: venilinvasilev <[email protected]> Signed-off-by: Tiago Requeijo <[email protected]>
1 parent f8656cb commit 038c409

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/transaction/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,9 @@ impl<D: TransactionExecute> Transaction<D> {
588588
/// # Panics
589589
/// - If `!self.is_frozen()`.
590590
pub fn to_bytes(&self) -> crate::Result<Vec<u8>> {
591-
let transaction_list = self.make_transaction_list().unwrap();
591+
let transaction_list = self
592+
.signed_sources()
593+
.map_or_else(|| self.make_transaction_list(), |it| Ok(it.transactions().to_vec()))?;
592594
Ok(hedera_proto::sdk::TransactionList { transaction_list }.encode_to_vec())
593595
}
594596

src/transaction/tests.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,43 @@ fn to_bytes_from_bytes() -> crate::Result<()> {
6262
Ok(())
6363
}
6464

65+
#[test]
66+
fn signed_to_bytes_from_bytes_preserves_signatures() -> crate::Result<()> {
67+
let mut tx = TransferTransaction::new();
68+
69+
// Build a minimal, frozen transaction (no network dependency)
70+
let mut tx = tx
71+
.max_transaction_fee(Hbar::new(10))
72+
.transaction_valid_duration(time::Duration::seconds(119))
73+
.transaction_memo("signed-preserve-test")
74+
.hbar_transfer(2.into(), Hbar::new(2))
75+
.hbar_transfer(101.into(), Hbar::new(-2))
76+
.transaction_id(TransactionId {
77+
account_id: 101.into(),
78+
valid_start: OffsetDateTime::now_utc(),
79+
nonce: None,
80+
scheduled: false,
81+
})
82+
.node_account_ids([6.into(), 7.into()])
83+
.freeze()?;
84+
85+
// Sign with an arbitrary key
86+
let key: PrivateKey = "302e020100300506032b657004220420e40d4241d093b22910c78135e0501b137cd9205bbb9c0153c5adf2c65e7dc95a"
87+
.parse()
88+
.unwrap();
89+
tx.sign(key);
90+
91+
// Serialize, then deserialize, then serialize again
92+
let bytes_before = tx.to_bytes()?;
93+
let tx2 = AnyTransaction::from_bytes(&bytes_before)?;
94+
let bytes_after = tx2.to_bytes()?;
95+
96+
// If signatures are preserved, bytes should match
97+
assert_eq!(bytes_before, bytes_after);
98+
99+
Ok(())
100+
}
101+
65102
#[test]
66103
fn from_bytes_sign_to_bytes() -> crate::Result<()> {
67104
let mut tx = TransferTransaction::new();

0 commit comments

Comments
 (0)