Skip to content

Commit

Permalink
consensus: add script convertors
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 8, 2023
1 parent 8369075 commit 33ba0be
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
65 changes: 64 additions & 1 deletion consensus/src/hashtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
// limitations under the License.

use amplify::{Bytes20, Bytes32, Wrapper};
use commit_verify::{DigestExt, Ripemd160, Sha256};

use crate::LIB_NAME_BITCOIN;
use crate::{
CompressedPk, LegacyPk, RedeemScript, UncompressedPk, WitnessScript, LIB_NAME_BITCOIN,
};

#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]

Check warning on line 29 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L29

Added line #L29 was not covered by tests
#[wrapper(Index, RangeOps, AsSlice, BorrowSlice, Hex, Display, FromStr)]
Expand All @@ -42,6 +45,36 @@ impl From<PubkeyHash> for [u8; 20] {
fn from(value: PubkeyHash) -> Self { value.0.into_inner() }

Check warning on line 45 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L45

Added line #L45 was not covered by tests
}

impl From<CompressedPk> for PubkeyHash {
fn from(pk: CompressedPk) -> Self {
let mut engine = Sha256::default();
engine.input_raw(&pk.to_byte_array());
let mut engine2 = Ripemd160::default();
engine2.input_raw(&engine.finish());
Self(engine2.finish().into())
}

Check warning on line 55 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L49-L55

Added lines #L49 - L55 were not covered by tests
}

impl From<UncompressedPk> for PubkeyHash {
fn from(pk: UncompressedPk) -> Self {
let mut engine = Sha256::default();
engine.input_raw(&pk.to_byte_array());
let mut engine2 = Ripemd160::default();
engine2.input_raw(&engine.finish());
Self(engine2.finish().into())
}

Check warning on line 65 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L59-L65

Added lines #L59 - L65 were not covered by tests
}

impl From<LegacyPk> for PubkeyHash {
fn from(pk: LegacyPk) -> Self {
let mut engine = Sha256::default();
engine.input_raw(&pk.to_vec());
let mut engine2 = Ripemd160::default();
engine2.input_raw(&engine.finish());
Self(engine2.finish().into())
}

Check warning on line 75 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L69-L75

Added lines #L69 - L75 were not covered by tests
}

#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]

Check warning on line 78 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L78

Added line #L78 was not covered by tests
#[wrapper(Index, RangeOps, AsSlice, BorrowSlice, Hex, Display, FromStr)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
Expand All @@ -61,6 +94,16 @@ impl From<ScriptHash> for [u8; 20] {
fn from(value: ScriptHash) -> Self { value.0.into_inner() }

Check warning on line 94 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L94

Added line #L94 was not covered by tests
}

impl From<&RedeemScript> for ScriptHash {
fn from(redeem_script: &RedeemScript) -> Self {
let mut engine = Sha256::default();
engine.input_raw(redeem_script.as_slice());
let mut engine2 = Ripemd160::default();
engine2.input_raw(&engine.finish());
Self(engine2.finish().into())
}

Check warning on line 104 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L98-L104

Added lines #L98 - L104 were not covered by tests
}

#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]

Check warning on line 107 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L107

Added line #L107 was not covered by tests
#[wrapper(Index, RangeOps, AsSlice, BorrowSlice, Hex, Display, FromStr)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
Expand All @@ -80,6 +123,16 @@ impl From<WPubkeyHash> for [u8; 20] {
fn from(value: WPubkeyHash) -> Self { value.0.into_inner() }

Check warning on line 123 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L123

Added line #L123 was not covered by tests
}

impl From<CompressedPk> for WPubkeyHash {
fn from(pk: CompressedPk) -> Self {
let mut engine = Sha256::default();
engine.input_raw(&pk.to_byte_array());
let mut engine2 = Ripemd160::default();
engine2.input_raw(&engine.finish());
Self(engine2.finish().into())
}

Check warning on line 133 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L127-L133

Added lines #L127 - L133 were not covered by tests
}

#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]

Check warning on line 136 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L136

Added line #L136 was not covered by tests
#[wrapper(Index, RangeOps, AsSlice, BorrowSlice, Hex, Display, FromStr)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
Expand All @@ -98,3 +151,13 @@ pub struct WScriptHash(
impl From<WScriptHash> for [u8; 32] {
fn from(value: WScriptHash) -> Self { value.0.into_inner() }

Check warning on line 152 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L152

Added line #L152 was not covered by tests
}

impl From<&WitnessScript> for WScriptHash {
fn from(witness_script: &WitnessScript) -> Self {
let mut engine = Sha256::default();
engine.input_raw(witness_script.as_slice());
let mut engine2 = Sha256::default();
engine2.input_raw(&engine.finish());
Self(engine2.finish().into())
}

Check warning on line 162 in consensus/src/hashtypes.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/hashtypes.rs#L156-L162

Added lines #L156 - L162 were not covered by tests
}
7 changes: 7 additions & 0 deletions consensus/src/pubkeys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ impl LegacyPk {
_ => unreachable!(),

Check warning on line 208 in consensus/src/pubkeys.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/pubkeys.rs#L202-L208

Added lines #L202 - L208 were not covered by tests
})
}

Check warning on line 210 in consensus/src/pubkeys.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/pubkeys.rs#L210

Added line #L210 was not covered by tests

pub fn to_vec(&self) -> Vec<u8> {
match self.compressed {
true => self.pubkey.serialize().to_vec(),
false => self.pubkey.serialize_uncompressed().to_vec(),

Check warning on line 215 in consensus/src/pubkeys.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/pubkeys.rs#L212-L215

Added lines #L212 - L215 were not covered by tests
}
}

Check warning on line 217 in consensus/src/pubkeys.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/pubkeys.rs#L217

Added line #L217 was not covered by tests
}

impl StrictEncode for LegacyPk {
Expand Down
13 changes: 11 additions & 2 deletions consensus/src/segwit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
use std::vec;

use amplify::confinement::Confined;
use amplify::Bytes32StrRev;
use amplify::{Bytes32StrRev, Wrapper};

use crate::opcodes::*;
use crate::{OpCode, ScriptBytes, ScriptPubkey, VarIntArray, LIB_NAME_BITCOIN};
use crate::{
OpCode, RedeemScript, ScriptBytes, ScriptPubkey, VarIntArray, WScriptHash, LIB_NAME_BITCOIN,
};

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display, Error)]
#[display(doc_comments)]
Expand Down Expand Up @@ -328,6 +330,13 @@ impl WitnessScript {
/// Adds a single opcode to the script.
pub fn push_opcode(&mut self, op_code: OpCode) { self.0.push(op_code as u8); }

pub fn to_redeem_script(&self) -> RedeemScript {
let script = ScriptPubkey::p2wsh(WScriptHash::from(self));
RedeemScript::from_inner(script.into_inner())
}

Check warning on line 336 in consensus/src/segwit.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/segwit.rs#L333-L336

Added lines #L333 - L336 were not covered by tests

pub fn to_script_pubkey(&self) -> ScriptPubkey { ScriptPubkey::p2wsh(WScriptHash::from(self)) }

Check warning on line 338 in consensus/src/segwit.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/segwit.rs#L338

Added line #L338 was not covered by tests

pub fn as_script_bytes(&self) -> &ScriptBytes { &self.0 }

Check warning on line 340 in consensus/src/segwit.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/segwit.rs#L340

Added line #L340 was not covered by tests
}

Expand Down
2 changes: 2 additions & 0 deletions consensus/src/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ impl OutputPk {
XOnlyPk::from_bytes(bytes).map(Self)
}

Check warning on line 248 in consensus/src/taproot.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/taproot.rs#L246-L248

Added lines #L246 - L248 were not covered by tests

pub fn to_script_pubkey(&self) -> ScriptPubkey { ScriptPubkey::p2tr_tweaked(*self) }

Check warning on line 250 in consensus/src/taproot.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/taproot.rs#L250

Added line #L250 was not covered by tests

#[inline]
pub fn to_byte_array(&self) -> [u8; 32] { self.0.to_byte_array() }

Check warning on line 253 in consensus/src/taproot.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/taproot.rs#L253

Added line #L253 was not covered by tests
}
Expand Down

0 comments on commit 33ba0be

Please sign in to comment.