From 43efc3e79a7cbfd28466ea67ce9a0621f0c2f722 Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Thu, 28 Mar 2019 16:23:12 +0000 Subject: [PATCH] Fix getrawtransaction for coinbase transactions --- client/src/client.rs | 2 +- json/src/lib.rs | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/client/src/client.rs b/client/src/client.rs index 5e9a4674..ff537eb3 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -259,7 +259,7 @@ pub trait RpcApi: Sized { include_watchonly: Option, ) -> Result { let mut args = [into_json(txid)?, opt_into_json(include_watchonly)?]; - self.call("getrawtransaction", handle_defaults(&mut args, &[null()])) + self.call("gettransaction", handle_defaults(&mut args, &[null()])) } fn get_tx_out( diff --git a/json/src/lib.rs b/json/src/lib.rs index b3f78669..26d50554 100644 --- a/json/src/lib.rs +++ b/json/src/lib.rs @@ -166,14 +166,29 @@ impl GetRawTransactionResultVinScriptSig { #[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct GetRawTransactionResultVin { - pub txid: sha256d::Hash, - pub vout: u32, - pub script_sig: GetRawTransactionResultVinScriptSig, pub sequence: u32, + /// The raw scriptSig in case of a coinbase tx. + pub coinbase: Option, + /// Not provided for coinbase txs. + pub txid: Option, + /// Not provided for coinbase txs. + pub vout: Option, + /// The scriptSig in case of a non-coinbase tx. + pub script_sig: Option, + /// Not provided for coinbase txs. #[serde(default, deserialize_with = "deserialize_hex_array_opt")] pub txinwitness: Option>>, } +impl GetRawTransactionResultVin { + /// Whether this input is from a coinbase tx. + /// The [txid], [vout] and [script_sig] fields are not provided + /// for coinbase transactions. + pub fn is_coinbase(&self) -> bool { + self.coinbase.is_some() + } +} + #[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct GetRawTransactionResultVoutScriptPubKey { @@ -223,6 +238,11 @@ pub struct GetRawTransactionResult { } impl GetRawTransactionResult { + /// Whether this tx is a coinbase tx. + pub fn is_coinbase(&self) -> bool { + self.vin.len() == 1 && self.vin[0].is_coinbase() + } + pub fn transaction(&self) -> Result { Ok(encode::deserialize(&self.hex)?) } @@ -869,12 +889,13 @@ mod tests { version: 2, locktime: 1384957, vin: vec![GetRawTransactionResultVin{ - txid: hash!("f04a336cb0fac5611e625827bd89e0be5dd2504e6a98ecbfaa5fcf1528d06b58"), - vout: 0, - script_sig: GetRawTransactionResultVinScriptSig{ + txid: Some(hash!("f04a336cb0fac5611e625827bd89e0be5dd2504e6a98ecbfaa5fcf1528d06b58")), + vout: Some(0), + coinbase: None, + script_sig: Some(GetRawTransactionResultVinScriptSig{ asm: "3045022100e85425f6d7c589972ee061413bcf08dc8c8e589ce37b217535a42af924f0e4d602205c9ba9cb14ef15513c9d946fa1c4b797883e748e8c32171bdf6166583946e35c[ALL] 03dae30a4d7870cd87b45dd53e6012f71318fdd059c1c2623b8cc73f8af287bb2d".into(), hex: hex!("483045022100e85425f6d7c589972ee061413bcf08dc8c8e589ce37b217535a42af924f0e4d602205c9ba9cb14ef15513c9d946fa1c4b797883e748e8c32171bdf6166583946e35c012103dae30a4d7870cd87b45dd53e6012f71318fdd059c1c2623b8cc73f8af287bb2d"), - }, + }), sequence: 4294967294, txinwitness: None, @@ -966,7 +987,7 @@ mod tests { expected.transaction().unwrap().input[0].previous_output.txid, hash!("f04a336cb0fac5611e625827bd89e0be5dd2504e6a98ecbfaa5fcf1528d06b58") ); - assert!(expected.vin[0].script_sig.script().is_ok()); + assert!(expected.vin[0].script_sig.as_ref().unwrap().script().is_ok()); assert!(expected.vout[0].script_pub_key.script().is_ok()); }