Skip to content

Commit

Permalink
raw: impl base set of bitcoin types
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Feb 2, 2023
1 parent 2172087 commit 4e9686f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
10 changes: 10 additions & 0 deletions raw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

#[macro_use]
extern crate amplify;
#[macro_use]
extern crate strict_encoding;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde_crate as serde;

pub extern crate secp256k1;

Expand Down Expand Up @@ -43,6 +48,11 @@ mod types {
Wrapper, WrapperMut, Clone, Ord, PartialOrd, Eq, PartialEq, Hash,
Default, Debug, From
)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
#[wrapper(Deref, RangeOps, BorrowSlice)]
#[wrapper_mut(DerefMut, RangeMut, BorrowSliceMut)]
pub struct VarIntBytes(VarIntArray<u8>);
Expand Down
2 changes: 2 additions & 0 deletions raw/src/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,6 @@ impl ScriptPubkey {
) -> Self {
todo!()
}

pub fn p2tr_tweaked(output_key: XOnlyPublicKey) -> Self { todo!() }
}
79 changes: 78 additions & 1 deletion raw/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,120 @@ use amplify::Bytes32;

use super::{VarIntArray, VarIntBytes};

#[derive(Wrapper, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, From)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
#[wrapper(RangeOps, BorrowSlice, Hex, Display, FromStr)]
// all-zeros used in coinbase
pub struct Txid(Bytes32);
pub struct Txid(
#[from]
#[from([u8; 32])]
Bytes32,
);

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
// 0xFFFFFFFF used in coinbase
pub struct Vout(u32);

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
pub struct Outpoint {
pub txid: Txid,
pub vout: Vout,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct SeqNo(u32);

#[derive(Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct SigScript(VarIntBytes);

#[derive(Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
pub struct TxIn {
pub prev_output: Outpoint,
pub sig_script: SigScript,
pub sequence: SeqNo,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct Sats(u64);

#[derive(Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct ScriptPubkey(VarIntBytes);

#[derive(Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
pub struct TxOut {
pub value: Sats,
pub script_pubkey: ScriptPubkey,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
pub enum TxVer {
V1,
V2,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct LockTime(u32);

#[derive(Clone, Eq, PartialEq, Debug)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
pub struct Tx {
pub version: TxVer,
pub inputs: VarIntArray<TxIn>,
Expand Down

0 comments on commit 4e9686f

Please sign in to comment.