Skip to content

Commit 794d040

Browse files
committed
add decode_raw_transaction
1 parent 6c4cd8a commit 794d040

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
@@ -777,6 +777,16 @@ pub trait RpcApi: Sized {
777777
deserialize_hex(&hex)
778778
}
779779

780+
fn decode_raw_transaction<R: RawTx>(
781+
&self,
782+
tx: R,
783+
is_witness: Option<bool>,
784+
) -> Result<json::DecodeRawTransactionResult> {
785+
let mut args = [tx.raw_hex().into(), opt_into_json(is_witness)?];
786+
let defaults = [null()];
787+
self.call("decoderawtransaction", handle_defaults(&mut args, &defaults))
788+
}
789+
780790
fn fund_raw_transaction<R: RawTx>(
781791
&self,
782792
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);
@@ -645,6 +646,35 @@ fn test_create_raw_transaction(cl: &Client) {
645646
assert_eq!(hex, serialize(&tx).to_hex());
646647
}
647648

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

json/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,20 @@ pub struct FinalizePsbtResult {
16991699
pub complete: bool,
17001700
}
17011701

1702+
/// Model for decode transaction
1703+
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
1704+
pub struct DecodeRawTransactionResult {
1705+
pub txid: bitcoin::Txid,
1706+
pub hash: bitcoin::Wtxid,
1707+
pub size: u32,
1708+
pub vsize: u32,
1709+
pub weight: u32,
1710+
pub version: u32,
1711+
pub locktime: u32,
1712+
pub vin: Vec<GetRawTransactionResultVin>,
1713+
pub vout: Vec<GetRawTransactionResultVout>,
1714+
}
1715+
17021716
/// Models the result of "getchaintips"
17031717
pub type GetChainTipsResult = Vec<GetChainTipsResultTip>;
17041718

0 commit comments

Comments
 (0)