Skip to content

Commit c7f3370

Browse files
committed
Merge #271: add decode_raw_transaction
794d040 add `decode_raw_transaction` (thesimplekid) Pull request description: ACKs for top commit: apoelstra: ACK 794d040 Tree-SHA512: 7a3e7f938daf966b02d511027433babc451e814f5a01ebdda495592134e048dfe99763440e8e3f340443e99ef4502f722eb52e7073cfecca2c19ad07b01a14e7
2 parents 6cd787a + 794d040 commit c7f3370

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

client/src/client.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,16 @@ pub trait RpcApi: Sized {
785785
deserialize_hex(&hex)
786786
}
787787

788+
fn decode_raw_transaction<R: RawTx>(
789+
&self,
790+
tx: R,
791+
is_witness: Option<bool>,
792+
) -> Result<json::DecodeRawTransactionResult> {
793+
let mut args = [tx.raw_hex().into(), opt_into_json(is_witness)?];
794+
let defaults = [null()];
795+
self.call("decoderawtransaction", handle_defaults(&mut args, &defaults))
796+
}
797+
788798
fn fund_raw_transaction<R: RawTx>(
789799
&self,
790800
tx: R,

integration_test/src/main.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ fn main() {
169169
test_invalidate_block_reconsider_block(&cl);
170170
test_key_pool_refill(&cl);
171171
test_create_raw_transaction(&cl);
172+
test_decode_raw_transaction(&cl);
172173
test_fund_raw_transaction(&cl);
173174
test_test_mempool_accept(&cl);
174175
test_wallet_create_funded_psbt(&cl);
@@ -646,6 +647,35 @@ fn test_create_raw_transaction(cl: &Client) {
646647
assert_eq!(hex, serialize(&tx).to_hex());
647648
}
648649

650+
fn test_decode_raw_transaction(cl: &Client) {
651+
let options = json::ListUnspentQueryOptions {
652+
minimum_amount: Some(btc(2)),
653+
..Default::default()
654+
};
655+
let unspent = cl.list_unspent(Some(6), None, None, None, Some(options)).unwrap();
656+
let unspent = unspent.into_iter().nth(0).unwrap();
657+
658+
let input = json::CreateRawTransactionInput {
659+
txid: unspent.txid,
660+
vout: unspent.vout,
661+
sequence: None,
662+
};
663+
let mut output = HashMap::new();
664+
output.insert(RANDOM_ADDRESS.to_string(), btc(1));
665+
666+
let tx =
667+
cl.create_raw_transaction(&[input.clone()], &output, Some(500_000), Some(true)).unwrap();
668+
let hex = cl.create_raw_transaction_hex(&[input], &output, Some(500_000), Some(true)).unwrap();
669+
670+
let decoded_transaction = cl.decode_raw_transaction(hex, None).unwrap();
671+
672+
assert_eq!(tx.txid(), decoded_transaction.txid);
673+
assert_eq!(500_000, decoded_transaction.locktime);
674+
675+
assert_eq!(decoded_transaction.vin[0].txid.unwrap(), unspent.txid);
676+
assert_eq!(decoded_transaction.vout[0].clone().value, btc(1));
677+
}
678+
649679
fn test_fund_raw_transaction(cl: &Client) {
650680
let addr = cl.get_new_address(None, None).unwrap();
651681
let mut output = HashMap::new();

json/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,20 @@ pub struct FinalizePsbtResult {
17171717
pub complete: bool,
17181718
}
17191719

1720+
/// Model for decode transaction
1721+
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
1722+
pub struct DecodeRawTransactionResult {
1723+
pub txid: bitcoin::Txid,
1724+
pub hash: bitcoin::Wtxid,
1725+
pub size: u32,
1726+
pub vsize: u32,
1727+
pub weight: u32,
1728+
pub version: u32,
1729+
pub locktime: u32,
1730+
pub vin: Vec<GetRawTransactionResultVin>,
1731+
pub vout: Vec<GetRawTransactionResultVout>,
1732+
}
1733+
17201734
/// Models the result of "getchaintips"
17211735
pub type GetChainTipsResult = Vec<GetChainTipsResultTip>;
17221736

0 commit comments

Comments
 (0)