Skip to content

feat(transaction): expose to_bytes() and from_bytes() for consensus serialization#39

Merged
darioAnongba merged 3 commits intomainfrom
feat/transaction-bytes-serialization
Mar 16, 2026
Merged

feat(transaction): expose to_bytes() and from_bytes() for consensus serialization#39
darioAnongba merged 3 commits intomainfrom
feat/transaction-bytes-serialization

Conversation

@darioAnongba
Copy link
Collaborator

@darioAnongba darioAnongba commented Mar 15, 2026

Summary

Adds Transaction.to_bytes() and Transaction.from_bytes() methods for consensus-encoded byte serialization/deserialization.

Motivation

The Transaction type currently has no serialization path, making it impossible to:

  • Pass transactions between WASM modules (e.g. BDK → LDK for channel funding)
  • Broadcast via APIs that expect raw bytes/hex
  • Store raw transactions in IndexedDB or send over WebSocket

Every other major type (Psbt, ChangeSet) has serialization — Transaction was the gap.

API

// Serialize to Uint8Array
const txBytes: Uint8Array = transaction.to_bytes()

// Deserialize from Uint8Array
const tx: Transaction = Transaction.from_bytes(txBytes)

Implementation

Thin wrapper around bitcoin::consensus::serialize / deserialize. wasm_bindgen handles Vec<u8>Uint8Array conversion automatically. Trivial enough that dedicated tests would be more maintenance than value.

Use case: LDK channel funding

const tx = psbt.extract_tx()
const rawTx = tx.to_bytes()
channelManager.funding_transaction_generated(tempChannelId, counterpartyNodeId, rawTx)

Closes #38

…erialization

Add Transaction.to_bytes() and Transaction.from_bytes() methods that
serialize/deserialize to consensus-encoded bytes (BIP144 format).

This enables passing transactions between WASM modules (e.g. BDK -> LDK),
broadcasting via external APIs, and storing raw transactions.

wasm_bindgen automatically converts Vec<u8> <-> Uint8Array for the JS API.

Closes #38
@darioAnongba
Copy link
Collaborator Author

Missing tests?

- Round-trip serialization with known mainnet transactions
- Coinbase tx (block 170) and Satoshi's first spend
- Error cases: empty bytes, truncated, garbage input
- Property verification after deserialization
- Multiple round-trip stability test
Per review — the implementation is trivial (thin wrapper around
bitcoin::consensus::{serialize,deserialize}), tests add more
maintenance burden than value.
@darioAnongba darioAnongba merged commit ea2ac4d into main Mar 16, 2026
12 checks passed
@darioAnongba darioAnongba deleted the feat/transaction-bytes-serialization branch March 16, 2026 10:53
@ConorOkus
Copy link

Thanks, @darioAnongba. How often are you releasing new versions of this package?

@darioAnongba
Copy link
Collaborator Author

Thanks, @darioAnongba. How often are you releasing new versions of this package?

There is no fixed schedule. v0.3.0 is released with this feature though, check the CHANGELOG.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Expose Transaction.to_bytes() and Transaction.from_bytes() via wasm_bindgen

2 participants