-
Notifications
You must be signed in to change notification settings - Fork 56
(draft) feat: add from_string
to Transaction
#804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
reez
wants to merge
1
commit into
bitcoindevkit:master
Choose a base branch
from
reez:i-803
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ use bdk_wallet::bitcoin::consensus::encode::serialize; | |
use bdk_wallet::bitcoin::consensus::Decodable; | ||
use bdk_wallet::bitcoin::hashes::sha256::Hash as BitcoinSha256Hash; | ||
use bdk_wallet::bitcoin::hashes::sha256d::Hash as BitcoinDoubleSha256Hash; | ||
use bdk_wallet::bitcoin::hex::FromHex; | ||
use bdk_wallet::bitcoin::io::Cursor; | ||
use bdk_wallet::bitcoin::secp256k1::Secp256k1; | ||
use bdk_wallet::bitcoin::Amount as BdkAmount; | ||
|
@@ -333,6 +334,13 @@ impl Transaction { | |
Ok(Transaction(tx)) | ||
} | ||
|
||
/// Creates a new `Transaction` instance from a hexadecimal string representation. | ||
#[uniffi::constructor] | ||
pub fn from_string(tx_hex: String) -> Result<Self, TransactionError> { | ||
let tx_bytes = Vec::from_hex(&tx_hex)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enforce consensus transaction? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Self::new(tx_bytes) | ||
} | ||
|
||
/// Computes the Txid. | ||
/// Hashes the transaction excluding the segwit data (i.e. the marker, flag bytes, and the witness fields themselves). | ||
pub fn compute_txid(&self) -> Arc<Txid> { | ||
|
@@ -677,6 +685,8 @@ impl_hash_like!(TxMerkleNode, BitcoinDoubleSha256Hash); | |
mod tests { | ||
use crate::bitcoin::Address; | ||
use crate::bitcoin::Network; | ||
use crate::bitcoin::Transaction; | ||
use crate::error::TransactionError; | ||
|
||
#[test] | ||
fn test_is_valid_for_network() { | ||
|
@@ -1032,4 +1042,41 @@ mod tests { | |
let segwit_data = segwit.to_address_data(); | ||
println!("Segwit data: {:#?}", segwit_data); | ||
} | ||
|
||
#[test] | ||
fn test_transaction_from_string() { | ||
// A simple transaction hex (mainnet coinbase transaction) | ||
let tx_hex = "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000"; | ||
|
||
// Test successful parsing | ||
let tx = Transaction::from_string(tx_hex.to_string()).unwrap(); | ||
|
||
// Verify the transaction was parsed correctly | ||
assert_eq!(tx.version(), 1); | ||
assert_eq!(tx.input().len(), 1); | ||
assert_eq!(tx.output().len(), 1); | ||
assert!(tx.is_coinbase()); | ||
|
||
// Test that serializing and re-parsing gives the same result | ||
let serialized = tx.serialize(); | ||
let tx2 = Transaction::new(serialized).unwrap(); | ||
assert_eq!( | ||
tx.compute_txid().to_string(), | ||
tx2.compute_txid().to_string() | ||
); | ||
|
||
// Test invalid hex string | ||
let invalid_hex = "invalid_hex_string"; | ||
let result = Transaction::from_string(invalid_hex.to_string()); | ||
assert!(result.is_err()); | ||
match result.unwrap_err() { | ||
TransactionError::InvalidHexString => {} | ||
_ => panic!("Expected InvalidHexString error"), | ||
} | ||
|
||
// Test hex string with invalid transaction data | ||
let invalid_tx_hex = "deadbeef"; | ||
let result = Transaction::from_string(invalid_tx_hex.to_string()); | ||
assert!(result.is_err()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
naming convention>?