Skip to content

Commit

Permalink
primitives: impl FromHex for script types
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Sep 19, 2023
1 parent 272faaf commit 133cbbe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 10 additions & 1 deletion primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ mod types {
use std::fmt::{Formatter, LowerHex, UpperHex};

use amplify::confinement::{Confined, U32};
use amplify::hex::ToHex;
use amplify::hex::{Error, FromHex, ToHex};

use super::LIB_NAME_BITCOIN;
use crate::opcodes::*;
Expand Down Expand Up @@ -108,6 +108,15 @@ mod types {
}
}

impl FromHex for ScriptBytes {
fn from_hex(s: &str) -> Result<Self, Error> { Vec::<u8>::from_hex(s).map(Self::from) }
fn from_byte_iter<I>(_: I) -> Result<Self, Error>
where I: Iterator<Item = Result<u8, Error>> + ExactSizeIterator + DoubleEndedIterator
{
unreachable!()
}
}

impl ScriptBytes {
/// Adds instructions to push some arbitrary data onto the stack.
///
Expand Down
19 changes: 19 additions & 0 deletions primitives/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// limitations under the License.

use amplify::confinement::Confined;
use amplify::hex::{Error, FromHex};

use crate::opcodes::*;
use crate::{ScriptBytes, LIB_NAME_BITCOIN};
Expand Down Expand Up @@ -121,6 +122,15 @@ pub struct SigScript(
ScriptBytes,
);

impl FromHex for SigScript {
fn from_hex(s: &str) -> Result<Self, Error> { ScriptBytes::from_hex(s).map(Self) }

fn from_byte_iter<I>(_: I) -> Result<Self, Error>
where I: Iterator<Item = Result<u8, Error>> + ExactSizeIterator + DoubleEndedIterator {
unreachable!()
}
}

#[derive(Wrapper, WrapperMut, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From, Default)]
#[wrapper(Deref, Index, RangeOps, BorrowSlice, LowerHex, UpperHex)]
#[wrapper_mut(DerefMut, IndexMut, RangeMut, BorrowSliceMut)]
Expand Down Expand Up @@ -196,3 +206,12 @@ impl ScriptPubkey {
self.0.push(op_code as u8).expect("script exceeds 4GB");
}
}

impl FromHex for ScriptPubkey {
fn from_hex(s: &str) -> Result<Self, Error> { ScriptBytes::from_hex(s).map(Self) }

fn from_byte_iter<I>(_: I) -> Result<Self, Error>
where I: Iterator<Item = Result<u8, Error>> + ExactSizeIterator + DoubleEndedIterator {
unreachable!()
}
}

0 comments on commit 133cbbe

Please sign in to comment.