Skip to content
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

Separating opret and tapret anchor types #69

Merged
merged 13 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
58 changes: 51 additions & 7 deletions consensus/src/coding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use std::io::{self, Cursor, Read, Write};

use amplify::confinement::{Confined, U32};
use amplify::confinement::{Confined, MediumBlob, SmallBlob, TinyBlob, U32};
use amplify::{confinement, ByteArray, Bytes32, IoError, Wrapper};

use crate::{
Expand All @@ -37,6 +37,8 @@ use crate::{
/// maximum size here with just 32 bits.
pub type VarIntArray<T> = Confined<Vec<T>, 0, U32>;

pub type VarIntBytes = Confined<Vec<u8>, 0, U32>;

/// A variable-length unsigned integer.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Default)]
#[derive(StrictType, StrictEncode, StrictDecode)]
Expand Down Expand Up @@ -88,12 +90,7 @@ impl<T> LenVarInt for VarIntArray<T> {
#[strict_type(lib = LIB_NAME_BITCOIN)]
#[wrapper(Deref, Index, RangeOps, BorrowSlice, Hex)]
#[wrapper_mut(DerefMut, IndexMut, RangeMut, BorrowSliceMut)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct ByteStr(VarIntArray<u8>);
pub struct ByteStr(VarIntBytes);

impl AsRef<[u8]> for ByteStr {
fn as_ref(&self) -> &[u8] { self.0.as_slice() }
Expand All @@ -103,12 +100,59 @@ impl From<Vec<u8>> for ByteStr {
fn from(value: Vec<u8>) -> Self { Self(Confined::try_from(value).expect("u32 >= usize")) }
}

impl From<TinyBlob> for ByteStr {
fn from(vec: TinyBlob) -> Self { ByteStr(Confined::from_collection_unsafe(vec.into_inner())) }
}

impl From<SmallBlob> for ByteStr {
fn from(vec: SmallBlob) -> Self { ByteStr(Confined::from_collection_unsafe(vec.into_inner())) }
}

impl From<MediumBlob> for ByteStr {
fn from(vec: MediumBlob) -> Self { ByteStr(Confined::from_collection_unsafe(vec.into_inner())) }
}

impl ByteStr {
pub fn len_var_int(&self) -> VarInt { VarInt(self.len() as u64) }

pub fn into_vec(self) -> Vec<u8> { self.0.into_inner() }
}

#[cfg(feature = "serde")]
mod _serde {
use amplify::hex::{FromHex, ToHex};
use serde_crate::de::Error;
use serde_crate::{Deserialize, Deserializer, Serialize, Serializer};

use super::*;

impl Serialize for ByteStr {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
if serializer.is_human_readable() {
serializer.serialize_str(&self.to_hex())
} else {
serializer.serialize_bytes(self.as_slice())
}
}
}

impl<'de> Deserialize<'de> for ByteStr {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
if deserializer.is_human_readable() {
String::deserialize(deserializer).and_then(|string| {
Self::from_hex(&string).map_err(|_| D::Error::custom("wrong hex data"))
})
} else {
let bytes = Vec::<u8>::deserialize(deserializer)?;
Self::try_from(bytes)
.map_err(|_| D::Error::custom("invalid script length exceeding 4GB"))
}
}
}
}

#[derive(Clone, PartialEq, Eq, Debug, Display, Error, From)]
#[display(inner)]
pub enum ConsensusDecodeError {
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod coding;
pub use block::{BlockHash, BlockHeader};
pub use coding::{
ByteStr, ConsensusDataError, ConsensusDecode, ConsensusDecodeError, ConsensusEncode, LenVarInt,
VarInt, VarIntArray,
VarInt, VarIntArray, VarIntBytes,
};
pub use hashtypes::{PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash};
pub use opcodes::OpCode;
Expand Down
8 changes: 4 additions & 4 deletions consensus/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use amplify::confinement;
use amplify::confinement::Confined;

use crate::opcodes::*;
use crate::{ScriptHash, VarInt, VarIntArray, LIB_NAME_BITCOIN};
use crate::{ScriptHash, VarInt, VarIntArray, VarIntBytes, LIB_NAME_BITCOIN};

#[derive(Wrapper, WrapperMut, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From, Default)]
#[wrapper(Deref, AsSlice, Hex)]
Expand Down Expand Up @@ -148,7 +148,7 @@ impl ScriptPubkey {
}

#[inline]
pub fn is_op_return(&self) -> bool { self[0] == OpCode::Return as u8 }
pub fn is_op_return(&self) -> bool { !self.is_empty() && self[0] == OpCode::Return as u8 }

/// Adds a single opcode to the script.
#[inline]
Expand Down Expand Up @@ -208,7 +208,7 @@ impl RedeemScript {
#[wrapper_mut(DerefMut, AsSliceMut)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_BITCOIN)]
pub struct ScriptBytes(VarIntArray<u8>);
pub struct ScriptBytes(VarIntBytes);

impl TryFrom<Vec<u8>> for ScriptBytes {
type Error = confinement::Error;
Expand Down Expand Up @@ -317,7 +317,7 @@ mod _serde {
})
} else {
let bytes = Vec::<u8>::deserialize(deserializer)?;
ScriptBytes::try_from(bytes)
Self::try_from(bytes)
.map_err(|_| D::Error::custom("invalid script length exceeding 4GB"))
}
}
Expand Down
Loading