From 214e10baefb0aa804c001fd8826f73a185ddfa45 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Tue, 8 Sep 2020 23:36:48 +0200 Subject: [PATCH 1/2] PSBT types are now constants --- src/util/psbt/map/global.rs | 9 ++- src/util/psbt/map/input.rs | 120 ++++++++++++++++++++++-------------- src/util/psbt/map/output.rs | 19 ++++-- 3 files changed, 93 insertions(+), 55 deletions(-) diff --git a/src/util/psbt/map/global.rs b/src/util/psbt/map/global.rs index 681a3e1b..ba6bf569 100644 --- a/src/util/psbt/map/global.rs +++ b/src/util/psbt/map/global.rs @@ -23,6 +23,9 @@ use util::psbt::raw; use util::psbt; use util::psbt::Error; +/// Type: Unsigned Transaction PSBT_GLOBAL_UNSIGNED_TX = 0x00 +const PSBT_GLOBAL_UNSIGNED_TX: u8 = 0x00; + /// A key-value map for global data. #[derive(Clone, Debug, PartialEq)] pub struct Global { @@ -62,7 +65,7 @@ impl Map for Global { } = pair; match raw_key.type_value { - 0u8 => return Err(Error::DuplicateKey(raw_key).into()), + PSBT_GLOBAL_UNSIGNED_TX => return Err(Error::DuplicateKey(raw_key).into()), _ => match self.unknown.entry(raw_key) { Entry::Vacant(empty_key) => {empty_key.insert(raw_value);}, Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()), @@ -77,7 +80,7 @@ impl Map for Global { rv.push(raw::Pair { key: raw::Key { - type_value: 0u8, + type_value: PSBT_GLOBAL_UNSIGNED_TX, key: vec![], }, value: { @@ -127,7 +130,7 @@ impl Decodable for Global { match raw::Pair::consensus_decode(&mut d) { Ok(pair) => { match pair.key.type_value { - 0u8 => { + PSBT_GLOBAL_UNSIGNED_TX => { // key has to be empty if pair.key.key.is_empty() { // there can only be one unsigned transaction diff --git a/src/util/psbt/map/input.rs b/src/util/psbt/map/input.rs index 34ee93a1..1d2be37a 100644 --- a/src/util/psbt/map/input.rs +++ b/src/util/psbt/map/input.rs @@ -25,6 +25,34 @@ use util::psbt::map::Map; use util::psbt::raw; use util::psbt::serialize::Deserialize; use util::psbt::{Error, error}; + +/// Type: Non-Witness UTXO PSBT_IN_NON_WITNESS_UTXO = 0x00 +const PSBT_IN_NON_WITNESS_UTXO: u8 = 0x00; +/// Type: Witness UTXO PSBT_IN_WITNESS_UTXO = 0x01 +const PSBT_IN_WITNESS_UTXO: u8 = 0x01; +/// Type: Partial Signature PSBT_IN_PARTIAL_SIG = 0x02 +const PSBT_IN_PARTIAL_SIG: u8 = 0x02; +/// Type: Sighash Type PSBT_IN_SIGHASH_TYPE = 0x03 +const PSBT_IN_SIGHASH_TYPE: u8 = 0x03; +/// Type: Redeem Script PSBT_IN_REDEEM_SCRIPT = 0x04 +const PSBT_IN_REDEEM_SCRIPT: u8 = 0x04; +/// Type: Witness Script PSBT_IN_WITNESS_SCRIPT = 0x05 +const PSBT_IN_WITNESS_SCRIPT: u8 = 0x05; +/// Type: BIP 32 Derivation Path PSBT_IN_BIP32_DERIVATION = 0x06 +const PSBT_IN_BIP32_DERIVATION: u8 = 0x06; +/// Type: Finalized scriptSig PSBT_IN_FINAL_SCRIPTSIG = 0x07 +const PSBT_IN_FINAL_SCRIPTSIG: u8 = 0x07; +/// Type: Finalized scriptWitness PSBT_IN_FINAL_SCRIPTWITNESS = 0x08 +const PSBT_IN_FINAL_SCRIPTWITNESS: u8 = 0x08; +/// Type: RIPEMD160 preimage +const PSBT_IN_RIPEMD160_PREIMAGES: u8 = 0x0a; +/// Type: SHA256 preimage +const PSBT_IN_SHA256_PREIMAGES: u8 = 0x0b; +/// Type: HASH160 preimage +const PSBT_IN_HASH160_PREIMAGES: u8 = 0x0c; +/// Type: HASH256 preimage +const PSBT_IN_HASH256_PREIMAGES: u8 = 0x0d; + /// A key-value map for an input of the corresponding index in the unsigned /// transaction. #[derive(Clone, Default, Debug, PartialEq)] @@ -84,61 +112,61 @@ impl Map for Input { } = pair; match raw_key.type_value { - 0u8 => { + PSBT_IN_NON_WITNESS_UTXO => { impl_psbt_insert_pair! { self.non_witness_utxo <= | } } - 1u8 => { + PSBT_IN_WITNESS_UTXO => { impl_psbt_insert_pair! { self.witness_utxo <= | } } - 3u8 => { - impl_psbt_insert_pair! { - self.sighash_type <= | - } - } - 4u8 => { - impl_psbt_insert_pair! { - self.redeem_script <= | - } - } - 5u8 => { - impl_psbt_insert_pair! { - self.witness_script <= | - } - } - 7u8 => { - impl_psbt_insert_pair! { - self.final_script_sig <= | - } - } - 8u8 => { - impl_psbt_insert_pair! { - self.final_script_witness <= |>> - } - } - 2u8 => { + PSBT_IN_PARTIAL_SIG => { impl_psbt_insert_pair! { self.partial_sigs <= |> } } - 6u8 => { + PSBT_IN_SIGHASH_TYPE => { + impl_psbt_insert_pair! { + self.sighash_type <= | + } + } + PSBT_IN_REDEEM_SCRIPT => { + impl_psbt_insert_pair! { + self.redeem_script <= | + } + } + PSBT_IN_WITNESS_SCRIPT => { + impl_psbt_insert_pair! { + self.witness_script <= | + } + } + PSBT_IN_BIP32_DERIVATION => { impl_psbt_insert_pair! { self.bip32_derivation <= | } } - 10u8 => { + PSBT_IN_FINAL_SCRIPTSIG => { + impl_psbt_insert_pair! { + self.final_script_sig <= | + } + } + PSBT_IN_FINAL_SCRIPTWITNESS => { + impl_psbt_insert_pair! { + self.final_script_witness <= |>> + } + } + PSBT_IN_RIPEMD160_PREIMAGES => { psbt_insert_hash_pair(&mut self.ripemd160_preimages, raw_key, raw_value, error::PsbtHash::Ripemd)?; } - 11u8 => { + PSBT_IN_SHA256_PREIMAGES => { psbt_insert_hash_pair(&mut self.sha256_preimages, raw_key, raw_value, error::PsbtHash::Sha256)?; } - 12u8 => { + PSBT_IN_HASH160_PREIMAGES => { psbt_insert_hash_pair(&mut self.hash160_preimages, raw_key, raw_value, error::PsbtHash::Hash160)?; } - 13u8 => { + PSBT_IN_HASH256_PREIMAGES => { psbt_insert_hash_pair(&mut self.hash256_preimages, raw_key, raw_value, error::PsbtHash::Hash256)?; } _ => match self.unknown.entry(raw_key) { @@ -158,55 +186,55 @@ impl Map for Input { let mut rv: Vec = Default::default(); impl_psbt_get_pair! { - rv.push(self.non_witness_utxo as <0u8, _>|) + rv.push(self.non_witness_utxo as |) } impl_psbt_get_pair! { - rv.push(self.witness_utxo as <1u8, _>|) + rv.push(self.witness_utxo as |) } impl_psbt_get_pair! { - rv.push(self.partial_sigs as <2u8, PublicKey>|>) + rv.push(self.partial_sigs as |>) } impl_psbt_get_pair! { - rv.push(self.sighash_type as <3u8, _>|) + rv.push(self.sighash_type as |) } impl_psbt_get_pair! { - rv.push(self.redeem_script as <4u8, _>|