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

API improvements for v0.11 #68

Merged
merged 24 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8964c18
consensus: add RedeemScript::to_script_pubkey method
dr-orlovsky Nov 14, 2023
affd469
release v0.11.0-beta.2
dr-orlovsky Nov 14, 2023
a953b10
consensus: fix checked-assign operations on Sats
dr-orlovsky Dec 14, 2023
af3c57f
consensus: implement Div and Rem for Sats
dr-orlovsky Dec 14, 2023
31e9b11
consensus: add Sats::btc_sats split method
dr-orlovsky Dec 14, 2023
5bd829e
seals: export SealTxid trait
dr-orlovsky Dec 14, 2023
ea4e7c7
seals: add default generic to ExplicitSeal
dr-orlovsky Dec 14, 2023
59de59f
contract: remove excessive non-exhaustives
dr-orlovsky Dec 14, 2023
14175ef
consensus: add more opcodes
dr-orlovsky Dec 14, 2023
38e0447
epic: make anchor types generic over specific Dbc proof
dr-orlovsky Dec 14, 2023
fbdd7f3
dbc: refactor validation workflow
dr-orlovsky Dec 14, 2023
7c1e02d
seals: remove unused error variant
dr-orlovsky Dec 15, 2023
3959319
dbc: remove AnchorId type
dr-orlovsky Dec 15, 2023
2d38062
dbc: improve generics in Anchor convertors
dr-orlovsky Dec 15, 2023
d305adf
dbc: add strict serialization to TapretProof
dr-orlovsky Dec 17, 2023
8bb8a63
dbc: move CloseMethod to DBC
dr-orlovsky Dec 17, 2023
d3a57c2
consensus: add ByteStr convertors from blob types
dr-orlovsky Dec 17, 2023
3f877d8
consensus: use custom confinement collection for ByteStr to support p…
dr-orlovsky Dec 24, 2023
536e773
consensus: provide custom serde implementation for ByteStr
dr-orlovsky Dec 24, 2023
79a3399
chore: update dependencies
dr-orlovsky Dec 25, 2023
2de8f05
dbc: stop on the first tapret output in restoring original tx
dr-orlovsky Dec 25, 2023
241eee5
dbc: rename tapret/opret to *First
dr-orlovsky Dec 25, 2023
4f93149
Merge pull request #69 from BP-WG/doubleanchors
dr-orlovsky Dec 25, 2023
3ad4349
release v0.11.0-beta.2
dr-orlovsky Dec 25, 2023
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
165 changes: 83 additions & 82 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ default-members = [
]

[workspace.package]
version = "0.11.0-beta.1"
version = "0.11.0-beta.2"
authors = ["Dr Maxim Orlovsky <[email protected]>"]
homepage = "https://github.com/BP-WG"
repository = "https://github.com/BP-WG/bp-core"
Expand All @@ -25,11 +25,11 @@ license = "Apache-2.0"
amplify = "4.5.0"
strict_encoding = "2.6.1"
strict_types = "1.6.3"
commit_verify = "0.11.0-beta.1"
single_use_seals = "0.11.0-beta.1"
bp-consensus = { version = "0.11.0-beta.1", path = "consensus" }
bp-dbc = { version = "0.11.0-beta.1", path = "./dbc" }
bp-seals = { version = "0.11.0-beta.1", path = "./seals" }
commit_verify = "0.11.0-beta.2"
single_use_seals = "0.11.0-beta.2"
bp-consensus = { version = "0.11.0-beta.3", path = "consensus" }
bp-dbc = { version = "0.11.0-beta.2", path = "./dbc" }
bp-seals = { version = "0.11.0-beta.2", path = "./seals" }
secp256k1 = { version = "0.28.0", features = ["global-context"] }
serde_crate = { package = "serde", version = "1", features = ["derive"] }

Expand Down
2 changes: 1 addition & 1 deletion consensus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bp-consensus"
version = { workspace = true }
version = "0.11.0-beta.3"
description = "Bitcoin protocol consensus library"
keywords = ["lnp-bp", "smart-contracts", "bitcoin", "blockchain"]
categories = ["cryptography"]
Expand Down
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
5 changes: 3 additions & 2 deletions consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ 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;
pub use pubkeys::{CompressedPk, InvalidPubkey, LegacyPk, PubkeyParseError, UncompressedPk};
pub use script::{OpCode, RedeemScript, ScriptBytes, ScriptPubkey, SigScript};
pub use script::{RedeemScript, ScriptBytes, ScriptPubkey, SigScript};
pub use segwit::{SegwitError, Witness, WitnessProgram, WitnessScript, WitnessVer, Wtxid};
pub use sigtypes::{Bip340Sig, LegacySig, SigError, SighashFlag, SighashType};
pub use taproot::{
Expand Down
206 changes: 206 additions & 0 deletions consensus/src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::LIB_NAME_BITCOIN;

/// Push an empty array onto the stack.
pub const OP_PUSHBYTES_0: u8 = 0x00;
/// Push the next byte as an array onto the stack.
Expand Down Expand Up @@ -548,3 +550,207 @@ pub const OP_RETURN_253: u8 = 0xfd;
pub const OP_RETURN_254: u8 = 0xfe;
/// Synonym for OP_RETURN.
pub const OP_INVALIDOPCODE: u8 = 0xff;

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_BITCOIN, tags = repr, into_u8, try_from_u8)]
#[non_exhaustive]
#[repr(u8)]
pub enum OpCode {
/// Push an empty array onto the stack.
#[display("OP_PUSH_BYTES0")]
PushBytes0 = OP_PUSHBYTES_0,

/// Push the next byte as an array onto the stack.
#[display("OP_PUSH_BYTES1")]
PushBytes1 = OP_PUSHBYTES_1,

/// Push the next 2 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES2")]
PushBytes2 = OP_PUSHBYTES_2,

/// Push the next 3 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES3")]
PushBytes3 = OP_PUSHBYTES_3,

/// Push the next 4 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES4")]
PushBytes4 = OP_PUSHBYTES_4,

/// Push the next 5 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES5")]
PushBytes5 = OP_PUSHBYTES_5,

/// Push the next 6 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES6")]
PushBytes6 = OP_PUSHBYTES_6,

/// Push the next 7 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES7")]
PushBytes7 = OP_PUSHBYTES_7,

/// Push the next 8 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES8")]
PushBytes8 = OP_PUSHBYTES_8,

/// Push the next 9 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES9")]
PushBytes9 = OP_PUSHBYTES_9,

/// Push the next 10 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES10")]
PushBytes10 = OP_PUSHBYTES_10,

/// Push the next 11 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES11")]
PushBytes11 = OP_PUSHBYTES_11,

/// Push the next 12 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES12")]
PushBytes12 = OP_PUSHBYTES_12,

/// Push the next 13 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES13")]
PushBytes13 = OP_PUSHBYTES_13,

/// Push the next 14 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES14")]
PushBytes14 = OP_PUSHBYTES_14,

/// Push the next 15 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES15")]
PushBytes15 = OP_PUSHBYTES_15,

/// Push the next 16 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES16")]
PushBytes16 = OP_PUSHBYTES_16,

/// Push the next 17 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES17")]
PushBytes17 = OP_PUSHBYTES_17,

/// Push the next 18 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES18")]
PushBytes18 = OP_PUSHBYTES_18,

/// Push the next 19 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES19")]
PushBytes19 = OP_PUSHBYTES_19,

/// Push the next 20 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES20")]
PushBytes20 = OP_PUSHBYTES_20,

/// Push the next 21 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES21")]
PushBytes21 = OP_PUSHBYTES_21,

/// Push the next 22 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES22")]
PushBytes22 = OP_PUSHBYTES_22,

/// Push the next 23 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES23")]
PushBytes23 = OP_PUSHBYTES_23,

/// Push the next 24 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES24")]
PushBytes24 = OP_PUSHBYTES_24,

/// Push the next 25 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES25")]
PushBytes25 = OP_PUSHBYTES_25,

/// Push the next 26 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES26")]
PushBytes26 = OP_PUSHBYTES_26,

/// Push the next 27 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES27")]
PushBytes27 = OP_PUSHBYTES_27,

/// Push the next 28 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES28")]
PushBytes28 = OP_PUSHBYTES_28,

/// Push the next 29 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES29")]
PushBytes29 = OP_PUSHBYTES_29,

/// Push the next 30 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES30")]
PushBytes30 = OP_PUSHBYTES_30,

/// Push the next 30 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES31")]
PushBytes31 = OP_PUSHBYTES_31,

/// Push the next 32 bytes as an array onto the stack.
#[display("OP_PUSH_BYTES32")]
PushBytes32 = OP_PUSHBYTES_32,

/// Read the next byte as N; push the next N bytes as an array onto the
/// stack.
#[display("OP_PUSH_DATA1")]
PushData1 = OP_PUSHDATA1,
/// Read the next 2 bytes as N; push the next N bytes as an array onto the
/// stack.
#[display("OP_PUSH_DATA2")]
PushData2 = OP_PUSHDATA2,
/// Read the next 4 bytes as N; push the next N bytes as an array onto the
/// stack.
#[display("OP_PUSH_DATA3")]
PushData4 = OP_PUSHDATA4,

/// Push the array `0x01` onto the stack.
#[display("OP_PUSHNUM_1")]
PushNum1 = OP_PUSHNUM_1,

/// Synonym for OP_RETURN.
Reserved = OP_RESERVED,

/// Fail the script immediately.
#[display("OP_RETURN")]
#[strict_type(dumb)]
Return = OP_RETURN,

/// Duplicates the top stack item.
#[display("OP_DUP")]
Dup = OP_DUP,

/// Pushes 1 if the inputs are exactly equal, 0 otherwise.
#[display("OP_EQUAL")]
Equal = OP_EQUAL,

/// Returns success if the inputs are exactly equal, failure otherwise.
#[display("OP_EQUALVERIFY")]
EqualVerify = OP_EQUALVERIFY,

/// Pop the top stack item and push its RIPEMD160 hash.
#[display("OP_RIPEMD160")]
Ripemd160 = OP_RIPEMD160,

/// Pop the top stack item and push its SHA1 hash.
#[display("OP_SHA1")]
Sha1 = OP_SHA1,

/// Pop the top stack item and push its SHA256 hash.
#[display("OP_SHA256")]
Sha256 = OP_SHA256,

/// Pop the top stack item and push its RIPEMD(SHA256) hash.
#[display("OP_HASH160")]
Hash160 = OP_HASH160,

/// Pop the top stack item and push its SHA256(SHA256) hash.
#[display("OP_HASH256")]
Hash256 = OP_HASH256,

/// <https://en.bitcoin.it/wiki/OP_CHECKSIG> pushing 1/0 for success/failure.
#[display("OP_CHECKSIG")]
CheckSig = OP_CHECKSIG,

/// <https://en.bitcoin.it/wiki/OP_CHECKSIG> returning success/failure.
#[display("OP_CHECKSIGVERIFY")]
CheckSigVerify = OP_CHECKSIGVERIFY,
}
Loading
Loading