Skip to content

add decode_raw_transaction #271

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

Merged
merged 1 commit into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,16 @@ pub trait RpcApi: Sized {
deserialize_hex(&hex)
}

fn decode_raw_transaction<R: RawTx>(
&self,
tx: R,
is_witness: Option<bool>,
) -> Result<json::DecodeRawTransactionResult> {
let mut args = [tx.raw_hex().into(), opt_into_json(is_witness)?];
let defaults = [null()];
self.call("decoderawtransaction", handle_defaults(&mut args, &defaults))
}

fn fund_raw_transaction<R: RawTx>(
&self,
tx: R,
Expand Down
30 changes: 30 additions & 0 deletions integration_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ fn main() {
test_invalidate_block_reconsider_block(&cl);
test_key_pool_refill(&cl);
test_create_raw_transaction(&cl);
test_decode_raw_transaction(&cl);
test_fund_raw_transaction(&cl);
test_test_mempool_accept(&cl);
test_wallet_create_funded_psbt(&cl);
Expand Down Expand Up @@ -645,6 +646,35 @@ fn test_create_raw_transaction(cl: &Client) {
assert_eq!(hex, serialize(&tx).to_hex());
}

fn test_decode_raw_transaction(cl: &Client) {
let options = json::ListUnspentQueryOptions {
minimum_amount: Some(btc(2)),
..Default::default()
};
let unspent = cl.list_unspent(Some(6), None, None, None, Some(options)).unwrap();
let unspent = unspent.into_iter().nth(0).unwrap();

let input = json::CreateRawTransactionInput {
txid: unspent.txid,
vout: unspent.vout,
sequence: None,
};
let mut output = HashMap::new();
output.insert(RANDOM_ADDRESS.to_string(), btc(1));

let tx =
cl.create_raw_transaction(&[input.clone()], &output, Some(500_000), Some(true)).unwrap();
let hex = cl.create_raw_transaction_hex(&[input], &output, Some(500_000), Some(true)).unwrap();

let decoded_transaction = cl.decode_raw_transaction(hex, None).unwrap();

assert_eq!(tx.txid(), decoded_transaction.txid);
assert_eq!(500_000, decoded_transaction.locktime);

assert_eq!(decoded_transaction.vin[0].txid.unwrap(), unspent.txid);
assert_eq!(decoded_transaction.vout[0].clone().value, btc(1));
}

fn test_fund_raw_transaction(cl: &Client) {
let addr = cl.get_new_address(None, None).unwrap();
let mut output = HashMap::new();
Expand Down
14 changes: 14 additions & 0 deletions json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,20 @@ pub struct FinalizePsbtResult {
pub complete: bool,
}

/// Model for decode transaction
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct DecodeRawTransactionResult {
pub txid: bitcoin::Txid,
pub hash: bitcoin::Wtxid,
pub size: u32,
pub vsize: u32,
pub weight: u32,
pub version: u32,
pub locktime: u32,
pub vin: Vec<GetRawTransactionResultVin>,
pub vout: Vec<GetRawTransactionResultVout>,
}

/// Models the result of "getchaintips"
pub type GetChainTipsResult = Vec<GetChainTipsResultTip>;

Expand Down