Skip to content

Commit

Permalink
Remove TypeId runtime for SpendableScript to get ScriptKind (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp authored Feb 8, 2025
1 parent 9e71637 commit 7a02daf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
4 changes: 2 additions & 2 deletions core/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl Actor {

use crate::builder::script::ScriptKind as Kind;

let mut witness = match script.into() {
let mut witness = match script.kind() {
Kind::PreimageRevealScript(script) => {
if script.0 != self.xonly_public_key {
return Err(BridgeError::NotOwnedScriptPath);
Expand Down Expand Up @@ -354,7 +354,7 @@ impl Actor {
use crate::builder::script::ScriptKind as Kind;

// Set the script inputs of the witness
let mut witness: Witness = match script.into() {
let mut witness: Witness = match script.kind() {
Kind::DepositScript(script) => {
match (sig, script.0 == self.xonly_public_key) {
(Some(sig), _) => script.generate_script_inputs(&sig),
Expand Down
50 changes: 28 additions & 22 deletions core/src/builder/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use bitvm::signatures::winternitz::{self, SecretKey};
use bitvm::signatures::winternitz::{Parameters, PublicKey};
use std::any::Any;
use std::fmt::Debug;
use std::sync::Arc;

#[derive(Debug, Copy, Clone)]
pub enum SpendPath {
Expand All @@ -38,6 +37,8 @@ pub enum SpendPath {
pub trait SpendableScript: Send + Sync + 'static + std::any::Any {
fn as_any(&self) -> &dyn Any;

fn kind(&self) -> ScriptKind;

fn to_script_buf(&self) -> ScriptBuf;
}

Expand All @@ -62,6 +63,10 @@ impl SpendableScript for OtherSpendable {
self
}

fn kind(&self) -> ScriptKind {
ScriptKind::Other(self)
}

fn to_script_buf(&self) -> ScriptBuf {
self.0.clone()
}
Expand Down Expand Up @@ -89,6 +94,10 @@ impl SpendableScript for CheckSig {
self
}

fn kind(&self) -> ScriptKind {
ScriptKind::CheckSig(self)
}

fn to_script_buf(&self) -> ScriptBuf {
Builder::new()
.push_x_only_key(&self.0)
Expand All @@ -115,6 +124,10 @@ impl SpendableScript for WinternitzCommit {
self
}

fn kind(&self) -> ScriptKind {
ScriptKind::WinternitzCommit(self)
}

fn to_script_buf(&self) -> ScriptBuf {
let winternitz_pubkey = self.0.clone();
let params = self.1.clone();
Expand Down Expand Up @@ -171,6 +184,10 @@ impl SpendableScript for TimelockScript {
self
}

fn kind(&self) -> ScriptKind {
ScriptKind::TimelockScript(self)
}

fn to_script_buf(&self) -> ScriptBuf {
let script_builder = Builder::new()
.push_int(self.1 as i64)
Expand Down Expand Up @@ -209,6 +226,10 @@ impl SpendableScript for PreimageRevealScript {
self
}

fn kind(&self) -> ScriptKind {
ScriptKind::PreimageRevealScript(self)
}

fn to_script_buf(&self) -> ScriptBuf {
Builder::new()
.push_opcode(OP_HASH160)
Expand Down Expand Up @@ -245,6 +266,10 @@ impl SpendableScript for DepositScript {
self
}

fn kind(&self) -> ScriptKind {
ScriptKind::DepositScript(self)
}

fn to_script_buf(&self) -> ScriptBuf {
let citrea: [u8; 6] = "citrea".as_bytes().try_into().expect("length == 6");

Expand Down Expand Up @@ -281,26 +306,6 @@ pub enum ScriptKind<'a> {
Other(&'a OtherSpendable),
}

impl<'a> From<&'a Arc<dyn SpendableScript>> for ScriptKind<'a> {
fn from(script: &'a Arc<dyn SpendableScript>) -> ScriptKind<'a> {
let type_id = script.as_any().type_id();

if type_id == std::any::TypeId::of::<CheckSig>() {
Self::CheckSig(script.as_any().downcast_ref().expect("just checked"))
} else if type_id == std::any::TypeId::of::<WinternitzCommit>() {
Self::WinternitzCommit(script.as_any().downcast_ref().expect("just checked"))
} else if type_id == std::any::TypeId::of::<TimelockScript>() {
Self::TimelockScript(script.as_any().downcast_ref().expect("just checked"))
} else if type_id == std::any::TypeId::of::<PreimageRevealScript>() {
Self::PreimageRevealScript(script.as_any().downcast_ref().expect("just checked"))
} else if type_id == std::any::TypeId::of::<DepositScript>() {
Self::DepositScript(script.as_any().downcast_ref().expect("just checked"))
} else {
Self::Other(script.as_any().downcast_ref().expect("just checked"))
}
}
}

#[cfg(test)]
fn get_script_from_arr<T: SpendableScript>(
arr: &Vec<Box<dyn SpendableScript>>,
Expand All @@ -312,6 +317,7 @@ fn get_script_from_arr<T: SpendableScript>(
#[cfg(test)]
mod tests {
use crate::utils;
use std::sync::Arc;

use super::*;

Expand Down Expand Up @@ -436,7 +442,7 @@ mod tests {
];

for (expected, script) in script_variants {
let kind = ScriptKind::from(&script);
let kind = script.kind();
match (expected, kind) {
("CheckSig", ScriptKind::CheckSig(_)) => (),
("WinternitzCommit", ScriptKind::WinternitzCommit(_)) => (),
Expand Down

0 comments on commit 7a02daf

Please sign in to comment.