Skip to content

Commit

Permalink
Refactorize some addresses and Script related parts (#61)
Browse files Browse the repository at this point in the history
* Refactorize some addresses and Script related parts

* A ScriptBuilder example on TypeScript

* addOps Opcodes are now BinaryT

* Move txscript bindings to relevant folders

* Sort lines of deps and imports

* Lint
  • Loading branch information
KaffinPX authored Jul 4, 2024
1 parent 334a56c commit 2745ab4
Show file tree
Hide file tree
Showing 15 changed files with 371 additions and 212 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions consensus/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ cfg_if::cfg_if! {
mod utils;
mod hash;
mod sign;
mod script;
mod serializable;


pub use header::*;
pub use input::*;
pub use transaction::*;
pub use serializable::*;
pub use utils::*;
pub use hash::*;
// pub use signing::*;
pub use script::*;
pub use sign::sign_with_multiple_v3;
}
}
12 changes: 8 additions & 4 deletions consensus/client/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::imports::*;
use crate::result::Result;
use kaspa_addresses::*;
use kaspa_consensus_core::tx::ScriptPublicKeyT;
use kaspa_consensus_core::{
network::{NetworkType, NetworkTypeT},
tx::ScriptPublicKeyT,
};
use kaspa_txscript::{script_class::ScriptClass, standard};
use kaspa_utils::hex::ToHex;
use kaspa_wasm_core::types::{BinaryT, HexString};
Expand Down Expand Up @@ -40,10 +43,11 @@ pub fn pay_to_script_hash_signature_script(redeem_script: BinaryT, signature: Bi
/// @param prefix - The address prefix.
/// @category Wallet SDK
#[wasm_bindgen(js_name = addressFromScriptPublicKey)]
pub fn address_from_script_public_key(script_public_key: ScriptPublicKeyT, prefix: String) -> Result<AddressOrUndefinedT> {
pub fn address_from_script_public_key(script_public_key: ScriptPublicKeyT, network: &NetworkTypeT) -> Result<AddressOrUndefinedT> {
let script_public_key = ScriptPublicKey::try_cast_from(script_public_key)?;
let prefix = Prefix::try_from(prefix.as_str())?;
match standard::extract_script_pub_key_address(script_public_key.as_ref(), prefix) {
let network_type = NetworkType::try_from(network)?;

match standard::extract_script_pub_key_address(script_public_key.as_ref(), network_type.into()) {
Ok(address) => Ok(AddressOrUndefinedT::from(JsValue::from(address))),
Err(_) => Ok(AddressOrUndefinedT::from(JsValue::UNDEFINED)),
}
Expand Down
9 changes: 9 additions & 0 deletions crypto/txscript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,33 @@ include.workspace = true
license.workspace = true
repository.workspace = true

[features]
wasm32-sdk = []

[dependencies]
blake2b_simd.workspace = true
borsh.workspace = true
cfg-if.workspace = true
indexmap.workspace = true
itertools.workspace = true
kaspa-addresses.workspace = true
kaspa-consensus-core.workspace = true
kaspa-hashes.workspace = true
kaspa-txscript-errors.workspace = true
kaspa-utils.workspace = true
kaspa-wasm-core.workspace = true
log.workspace = true
parking_lot.workspace = true
rand.workspace = true
secp256k1.workspace = true
serde_json.workspace = true
serde-wasm-bindgen.workspace = true
serde.workspace = true
sha2.workspace = true
smallvec.workspace = true
thiserror.workspace = true
wasm-bindgen.workspace = true
workflow-wasm.workspace = true

[dev-dependencies]
criterion.workspace = true
Expand Down
89 changes: 89 additions & 0 deletions crypto/txscript/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use crate::script_builder;
use thiserror::Error;
use wasm_bindgen::{JsError, JsValue};
use workflow_wasm::jserror::JsErrorData;

#[derive(Debug, Error, Clone)]
pub enum Error {
#[error("{0}")]
Custom(String),

#[error(transparent)]
JsValue(JsErrorData),

#[error(transparent)]
Wasm(#[from] workflow_wasm::error::Error),

#[error(transparent)]
ScriptBuilder(#[from] script_builder::ScriptBuilderError),

#[error("{0}")]
ParseInt(#[from] std::num::ParseIntError),

#[error(transparent)]
SerdeWasmBindgen(JsErrorData),

#[error(transparent)]
NetworkType(#[from] kaspa_consensus_core::network::NetworkTypeError),

#[error("Error converting property `{0}`: {1}")]
Convert(&'static str, String),

#[error("Error processing JSON: {0}")]
SerdeJson(String),
}

impl Error {
pub fn custom<T: Into<String>>(msg: T) -> Self {
Error::Custom(msg.into())
}

pub fn convert<S: std::fmt::Display>(prop: &'static str, msg: S) -> Self {
Self::Convert(prop, msg.to_string())
}
}

impl From<String> for Error {
fn from(err: String) -> Self {
Self::Custom(err)
}
}

impl From<&str> for Error {
fn from(err: &str) -> Self {
Self::Custom(err.to_string())
}
}

impl From<Error> for JsValue {
fn from(value: Error) -> Self {
match value {
Error::JsValue(js_error_data) => js_error_data.into(),
_ => JsValue::from(value.to_string()),
}
}
}

impl From<JsValue> for Error {
fn from(err: JsValue) -> Self {
Self::JsValue(err.into())
}
}

impl From<JsError> for Error {
fn from(err: JsError) -> Self {
Self::JsValue(err.into())
}
}

impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Self::SerdeJson(err.to_string())
}
}

impl From<serde_wasm_bindgen::Error> for Error {
fn from(err: serde_wasm_bindgen::Error) -> Self {
Self::SerdeWasmBindgen(JsValue::from(err).into())
}
}
4 changes: 4 additions & 0 deletions crypto/txscript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ extern crate core;

pub mod caches;
mod data_stack;
pub mod error;
pub mod opcodes;
pub mod result;
pub mod script_builder;
pub mod script_class;
pub mod standard;
#[cfg(feature = "wasm32-sdk")]
pub mod wasm;

use crate::caches::Cache;
use crate::data_stack::{DataStack, Stack};
Expand Down
1 change: 1 addition & 0 deletions crypto/txscript/src/result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub type Result<T, E = super::error::Error> = std::result::Result<T, E>;
2 changes: 1 addition & 1 deletion crypto/txscript/src/script_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl ScriptBuilder {
&self.script
}

#[cfg(test)]
#[cfg(any(test, target_arch = "wasm32"))]
pub fn extend(&mut self, data: &[u8]) {
self.script.extend(data);
}
Expand Down
Loading

0 comments on commit 2745ab4

Please sign in to comment.