Skip to content

Commit

Permalink
Chore/tests (#61)
Browse files Browse the repository at this point in the history
* type clean up and add reference native script support

* use fixed transaction for signing transasctions

* add test for creating native script ref

* fix transaction signing util

* add test for transaction signing

* use fixed transaction for remove witness set

* add test for remove witness set

* fix build paths

* add util for merging vkeys

* add test for merging vkeys

* fix build

* return error when protocol param is invalid

* feat: bump version

---------

Co-authored-by: SIDANWhatever <[email protected]>
  • Loading branch information
twwu123 and HinsonSIDAN authored Jul 26, 2024
1 parent a2eef56 commit 85dfc6c
Show file tree
Hide file tree
Showing 12 changed files with 1,526 additions and 37 deletions.
1,357 changes: 1,357 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"version": "0.0.0",
"description": "(De)serialization functions for the Cardano blockchain along with related utility functions",
"scripts": {
"core:build-nodejs": "(rimraf ./packages/sidan-csl-rs/pkg && cd packages/sidan-csl-rs; wasm-pack build --target=nodejs; cd ..; npm run js:ts-json-gen; cd packages/sidan-csl-rs; wasm-pack pack) && npm run js:flowgen",
"core:build-browser": "(rimraf ./packages/sidan-csl-rs/pkg && cd packages/sidan-csl-rs; wasm-pack build --target=browser; cd ..; npm run js:ts-json-gen; cd packages/sidan-csl-rs; wasm-pack pack) && npm run js:flowgen",
"core:build-web": "(rimraf ./packages/sidan-csl-rs/pkg && cd packages/sidan-csl-rs; wasm-pack build --target=web; cd ..; npm run js:ts-json-gen; cd packages/sidan-csl-rs; wasm-pack pack) && npm run js:flowgen",
"core:build-asm": "(rimraf ./packages/sidan-csl-rs/pkg && cd packages/sidan-csl-rs; wasm-pack build --target=browser; cd ..; npm run js:ts-json-gen; cd packages/sidan-csl-rs; wasm-pack pack) && npm run asm:build && npm run js:flowgen",
"core:build-nodejs": "(rimraf ./packages/sidan-csl-rs/pkg && cd packages/sidan-csl-rs; wasm-pack build --target=nodejs; cd ../..; npm run js:ts-json-gen; cd packages/sidan-csl-rs; wasm-pack pack) && npm run js:flowgen",
"core:build-browser": "(rimraf ./packages/sidan-csl-rs/pkg && cd packages/sidan-csl-rs; wasm-pack build --target=browser; cd ../..; npm run js:ts-json-gen; cd packages/sidan-csl-rs; wasm-pack pack) && npm run js:flowgen",
"core:build-web": "(rimraf ./packages/sidan-csl-rs/pkg && cd packages/sidan-csl-rs; wasm-pack build --target=web; cd ../..; npm run js:ts-json-gen; cd packages/sidan-csl-rs; wasm-pack pack) && npm run js:flowgen",
"core:build-asm": "(rimraf ./packages/sidan-csl-rs/pkg && cd packages/sidan-csl-rs; wasm-pack build --target=browser; cd ../..; npm run js:ts-json-gen; cd packages/sidan-csl-rs; wasm-pack pack) && npm run asm:build && npm run js:flowgen",
"rust:publish": "cd packages/whisky && cargo publish && cd ../",
"asm:build": "./binaryen/bin/wasm2js ./packages/sidan-csl-rs/pkg/sidan_csl_rs_bg.wasm --output ./packages/sidan-csl-rs/pkg/sidan_csl_rs.asm.js && node ./scripts/wasm-to-asm && node ./scripts/fix-buffer-ref.js",
"core:check-warnings": "(cd packages/sidan-csl-rs; RUSTFLAGS=\"-D warnings\" cargo +stable build)",
Expand All @@ -17,7 +17,7 @@
"js:publish-nodejs": "npm run core:build-nodejs && npm run js:prepublish && node ./scripts/publish-helper -nodejs && cd publish && npm publish --access public",
"js:publish-browser": "npm run core:build-browser && npm run js:prepublish && node ./scripts/publish-helper -browser && cd publish && npm publish --access public",
"js:publish-asm": "npm run core:build-asm && npm run js:prepublish && node ./scripts/publish-helper -asmjs && cd publish && npm publish --access public",
"js:ts-json-gen": "cd packages/sidan-csl-rs/json-gen && cargo +stable run && cd ../.. && node ./scripts/run-json2ts.js && node ./scripts/json-ts-types.js",
"js:ts-json-gen": "cd packages/sidan-csl-rs/json-gen && cargo +stable run && cd ../../.. && node ./scripts/run-json2ts.js && node ./scripts/json-ts-types.js",
"rust:doc-clear": "rm -rf ./docs && mkdir docs && echo '<meta http-equiv=\"refresh\" content=\"0; url=whisky\">' > docs/index.html",
"rust:doc": "npm run rust:doc-clear && RUSTDOCFLAGS=\"--cfg docsrs\" cargo doc --workspace --manifest-path packages/Cargo.toml --no-deps && cp -r ./packages/target/doc/* ./docs",
"start-doc": "npm run rust:doc && npx http-server ./docs",
Expand Down
15 changes: 12 additions & 3 deletions packages/Cargo.lock

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

5 changes: 3 additions & 2 deletions packages/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[workspace]
version = "0.6.11"
version = "0.6.12"
resolver = "2"
members = [
"sidan-csl-rs",
"sidan-csl-rs/json-gen",
"whisky",
"whisky-examples"
]
]
2 changes: 1 addition & 1 deletion packages/sidan-csl-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sidan-csl-rs"
version = "0.6.11"
version = "0.6.12"
edition = "2021"
license = "Apache-2.0"
description = "Wrapper around the cardano-serialization-lib for easier transaction building, heavily inspired by cardano-cli APIs"
Expand Down
3 changes: 1 addition & 2 deletions packages/sidan-csl-rs/json-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ edition = "2018"
authors = ["EMURGO"]
license = "MIT"


[dependencies]
serde_json = { version = "1.0.114", features = ["arbitrary_precision"] }
schemars = "0.8.8"
#serde = { version = "1.0", features = ["derive"] }
cardano-serialization-lib = { path = ".." }
cardano-serialization-lib = "12.0.0-beta.2"
7 changes: 6 additions & 1 deletion packages/sidan-csl-rs/src/builder/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ pub fn js_serialize_tx_body(mesh_tx_builder_body_json: &str, params_json: &str)

let params: Option<Protocol> = match serde_json::from_str(params_json) {
Ok(params) => Some(params),
Err(_) => None,
Err(e) => {
return TxBuildResult::new(
"failure".to_string(),
format!("Invalid Protocol Param JSON: {:?} \n {:?}", params_json, e),
)
}
};

match serialize_tx_body(mesh_tx_builder_body, params) {
Expand Down
98 changes: 82 additions & 16 deletions packages/sidan-csl-rs/src/core/utils/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,43 @@ pub fn calculate_tx_hash(tx_hex: &str) -> String {

#[wasm_bindgen]
pub fn sign_transaction(tx_hex: String, signing_keys: JsVecString) -> String {
let unsigned_transaction: csl::Transaction = csl::Transaction::from_hex(&tx_hex).unwrap();
let tx_body = unsigned_transaction.body();
let unsigned_transaction: csl::FixedTransaction =
csl::FixedTransaction::from_hex(&tx_hex).unwrap();
let mut witness_set = unsigned_transaction.witness_set();
let mut vkey_witnesses = witness_set
.vkeys()
.unwrap_or_else(csl::Vkeywitnesses::new)
.clone();
for key in signing_keys {
let clean_hex = if &key[0..4] == "5820" {
let clean_hex = if &key[0..4] == "5820" && key.len() == 68 {
key[4..].to_string()
} else {
key.to_string()
};
let skey = csl::PrivateKey::from_hex(&clean_hex).unwrap();
let vkey_witness = csl::make_vkey_witness(&csl::hash_transaction(&tx_body), &skey);
let vkey_witness = csl::make_vkey_witness(
&csl::TransactionHash::from(blake2b256(&unsigned_transaction.raw_body())),
&skey,
);
vkey_witnesses.add(&vkey_witness);
}
witness_set.set_vkeys(&vkey_witnesses);
let signed_transaction = csl::Transaction::new(
&tx_body,
&witness_set,
unsigned_transaction.auxiliary_data(),
);
let signed_transaction: csl::FixedTransaction = match &unsigned_transaction.raw_auxiliary_data()
{
Some(raw_auxiliary_data) => csl::FixedTransaction::new_with_auxiliary(
&unsigned_transaction.raw_body(),
&unsigned_transaction.raw_witness_set(),
&raw_auxiliary_data,
true,
)
.unwrap(),
None => csl::FixedTransaction::new(
&unsigned_transaction.raw_body(),
&witness_set.to_bytes(),
true,
)
.unwrap(),
};
signed_transaction.to_hex()
}

Expand All @@ -49,12 +63,64 @@ pub fn sign_transaction(tx_hex: String, signing_keys: JsVecString) -> String {
// println!("Pub key hash {:?}", skey.to_public().hash().to_hex());
// }

#[wasm_bindgen]
pub fn remove_witness_set(tx_hex: String) -> String {
let signed_transaction = csl::Transaction::from_hex(&tx_hex).unwrap();
csl::Transaction::new(
&signed_transaction.body(),
&csl::TransactionWitnessSet::new(),
signed_transaction.auxiliary_data().clone(),
)
.to_hex()
let signed_transaction = csl::FixedTransaction::from_hex(&tx_hex).unwrap();
let unsigned_transaction: csl::FixedTransaction = match &signed_transaction.raw_auxiliary_data()
{
Some(raw_auxiliary_data) => csl::FixedTransaction::new_with_auxiliary(
&signed_transaction.raw_body(),
&csl::TransactionWitnessSet::new().to_bytes(),
&raw_auxiliary_data,
true,
)
.unwrap(),
None => csl::FixedTransaction::new(
&signed_transaction.raw_body(),
&csl::TransactionWitnessSet::new().to_bytes(),
true,
)
.unwrap(),
};
unsigned_transaction.to_hex()
}

#[wasm_bindgen]
pub fn merge_vkey_witnesses_to_transaction(
tx_hex: String,
added_witness_set_hex: String,
) -> String {
let unsigned_transaction: csl::FixedTransaction =
csl::FixedTransaction::from_hex(&tx_hex).unwrap();
let mut witness_set = unsigned_transaction.witness_set();
let mut vkey_witnesses = witness_set
.vkeys()
.unwrap_or_else(csl::Vkeywitnesses::new)
.clone();
let added_vkey_witnesses = csl::TransactionWitnessSet::from_hex(&added_witness_set_hex)
.unwrap()
.vkeys()
.expect("Expected vkeys to add to transaction");

for index in 0..added_vkey_witnesses.len() {
vkey_witnesses.add(&added_vkey_witnesses.get(index));
}
witness_set.set_vkeys(&vkey_witnesses);
let signed_transaction: csl::FixedTransaction = match &unsigned_transaction.raw_auxiliary_data()
{
Some(raw_auxiliary_data) => csl::FixedTransaction::new_with_auxiliary(
&unsigned_transaction.raw_body(),
&unsigned_transaction.raw_witness_set(),
&raw_auxiliary_data,
true,
)
.unwrap(),
None => csl::FixedTransaction::new(
&unsigned_transaction.raw_body(),
&witness_set.to_bytes(),
true,
)
.unwrap(),
};
signed_transaction.to_hex()
}
24 changes: 23 additions & 1 deletion packages/sidan-csl-rs/tests/transaction_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
mod transaction_tests {
use cardano_serialization_lib as csl;
use sidan_csl_rs::core::utils::{build_tx_builder, calculate_tx_hash, to_bignum};
use sidan_csl_rs::{
core::utils::{
build_tx_builder, calculate_tx_hash, remove_witness_set, sign_transaction, to_bignum,
},
model::JsVecString,
};

#[test]
fn test_calculate_tx_hash() {
Expand Down Expand Up @@ -105,4 +110,21 @@ mod transaction_tests {

assert!(tx_builder.build().unwrap().outputs().len() == 2);
}

#[test]
fn test_signing_transaction() {
let mut signing_keys = JsVecString::new();
signing_keys.add(
"5820010f2ecd1312019694f7cc3bba11cdc386ed320906881cd0a3a8ca05988882ba".to_string(),
);
let signed_tx = sign_transaction("84a30081825820616733c08d8e5846b2669f78074c6c0d1e0e63ec8ef33acbe573aff1f622748e010182a200581d70a67529ee536a0dc1cd6d138ac6644baa11a0858bb32a46b1e9834a97011a001e8480a200581d602aa80698b309b95c849a426edc5b600b8fe6cf2598bc14a3b444bdfd011b0000000253c9cc4e021a00028759a0f5f6".to_string(), signing_keys);
println!("{}", signed_tx);
assert!(signed_tx == "84a30081825820616733c08d8e5846b2669f78074c6c0d1e0e63ec8ef33acbe573aff1f622748e010182a200581d70a67529ee536a0dc1cd6d138ac6644baa11a0858bb32a46b1e9834a97011a001e8480a200581d602aa80698b309b95c849a426edc5b600b8fe6cf2598bc14a3b444bdfd011b0000000253c9cc4e021a00028759a10081825820f444bd136f6b12767072dc541571412479280efd367335afcf15c5ddf09a98335840ea5ac790a6f75d47da28b13d55021c234ec961f81e35d5b9c447055377c70b066fb28a7e2c01978439712cc85d42f5e00b8c07b6b2b8f36d87178de6ee6d4e02f5f6")
}

#[test]
fn test_remove_witness_set() {
let signed_tx = "84a30081825820616733c08d8e5846b2669f78074c6c0d1e0e63ec8ef33acbe573aff1f622748e010182a200581d70a67529ee536a0dc1cd6d138ac6644baa11a0858bb32a46b1e9834a97011a001e8480a200581d602aa80698b309b95c849a426edc5b600b8fe6cf2598bc14a3b444bdfd011b0000000253c9cc4e021a00028759a10081825820f444bd136f6b12767072dc541571412479280efd367335afcf15c5ddf09a98335840ea5ac790a6f75d47da28b13d55021c234ec961f81e35d5b9c447055377c70b066fb28a7e2c01978439712cc85d42f5e00b8c07b6b2b8f36d87178de6ee6d4e02f5f6";
assert!(remove_witness_set(signed_tx.to_string()) == "84a30081825820616733c08d8e5846b2669f78074c6c0d1e0e63ec8ef33acbe573aff1f622748e010182a200581d70a67529ee536a0dc1cd6d138ac6644baa11a0858bb32a46b1e9834a97011a001e8480a200581d602aa80698b309b95c849a426edc5b600b8fe6cf2598bc14a3b444bdfd011b0000000253c9cc4e021a00028759a0f5f6")
}
}
4 changes: 2 additions & 2 deletions packages/whisky-examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "whisky-examples"
version = "0.6.11"
version = "0.6.12"
edition = "2021"
license = "Apache-2.0"
description = "The Cardano Rust SDK, inspired by MeshJS"

[dependencies]
whisky = { version = "=0.6.11", path = "../whisky" }
whisky = { version = "=0.6.12", path = "../whisky" }
4 changes: 2 additions & 2 deletions packages/whisky/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "whisky"
version = "0.6.11"
version = "0.6.12"
edition = "2021"
license = "Apache-2.0"
description = "The Cardano Rust SDK, inspired by MeshJS"
Expand All @@ -25,7 +25,7 @@ pallas-codec = "0.22.0"
pallas-primitives = "0.22.0"
pallas-traverse = "0.22.0"
maestro = { git = "https://github.com/maestro-org/rust-sdk.git" }
sidan-csl-rs = { version = "=0.6.11", path = "../sidan-csl-rs" }
sidan-csl-rs = { version = "=0.6.12", path = "../sidan-csl-rs" }
reqwest = "0.12.5"
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] }

Expand Down
34 changes: 32 additions & 2 deletions packages/whisky/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ mod int_tests {
core::common::con_str0,
model::{Asset, Budget, LanguageVersion},
};
use whisky::builder::{
IMeshTxBuilder, MeshTxBuilder, MeshTxBuilderParam, WData::JSON, WRedeemer,
use whisky::{
builder::{IMeshTxBuilder, MeshTxBuilder, MeshTxBuilderParam, WData::JSON, WRedeemer},
core::utils::merge_vkey_witnesses_to_transaction,
};

#[test]
Expand Down Expand Up @@ -277,4 +278,33 @@ mod int_tests {
println!("{}", signed_tx);
assert!(mesh.core.mesh_csl.tx_hex != *"");
}

#[test]
fn test_native_script_ref() {
let mut mesh = MeshTxBuilder::new(MeshTxBuilderParam {
evaluator: None,
fetcher: None,
submitter: None,
params: None,
});

let unsigned_tx = mesh
.tx_in(
"db0937db0e8a743e6e97e8cf29077af1e951b52e46f2e2c63ef12a3abaaf9052",
80,
vec![Asset::new_from_str("lovelace", "4633697637")],
"addr_test1qr3a9rrclgf9rx90lmll2qnfzfwgrw35ukvgjrk36pmlzu0jemqwylc286744g0tnqkrvu0dkl8r48k0upkfmg7mncpqf0672w",
)
.change_address("addr_test1qr3a9rrclgf9rx90lmll2qnfzfwgrw35ukvgjrk36pmlzu0jemqwylc286744g0tnqkrvu0dkl8r48k0upkfmg7mncpqf0672w")
.tx_out("addr_test1qr3a9rrclgf9rx90lmll2qnfzfwgrw35ukvgjrk36pmlzu0jemqwylc286744g0tnqkrvu0dkl8r48k0upkfmg7mncpqf0672w", vec![Asset::new_from_str("lovelace", "5000000")])
.tx_out_reference_script("8200581ce3d28c78fa125198affefff50269125c81ba34e598890ed1d077f171", None)
.complete_sync(None)
.unwrap()
.complete_signing();

let signed_tx = merge_vkey_witnesses_to_transaction(unsigned_tx, "a10081825820096348a7a3640d8ecc89819abffc7ed89cde399346046d50444acbd6e467f9df5840111279e89d341c9ab51f9ee7d5bb3a8db068ca6d09b7d3d4aaa48940dc55162903fd8f194df5c048055c9ac869e95729273b4ebb752be8a998f3483fac5d6e05".to_string());

println!("{}", signed_tx);
assert!(mesh.core.mesh_csl.tx_hex != *"");
}
}

0 comments on commit 85dfc6c

Please sign in to comment.