Skip to content

Commit

Permalink
feat: adding sign tx
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Mar 17, 2024
1 parent 51df5fb commit bd5b711
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sidan-csl-rs"
version = "0.2.3"
version = "0.2.4"
edition = "2021"
license = "MIT"
description = "Wrapper around the cardano-serialization-lib for easier transaction building, heavily inspired by cardano-cli APIs"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The APIs of `sidan-csl-rs` consists of 3 parts:
| Util Function | `serialize_bech32_address` | To obtain pub key hash, script hash and stake cred from bech32 address |
| Util Function | `get_v2_script_hash` | To obtain script hash from script cbor |
| Util Function | `calculate_tx_hash` | To calculate the transaction hash from signed or unsigned hex |
| Util Function | `sign_transaction` | To add private key signature to current tx_hex |
| Util Function | `meshToPlutusData` | To be added -To serialize plutus data from mesh data type |
| Util Function | `jsonToPlutusData` | To be added -To serialize plutus data from json |
| Util Function | `cborToPlutusData` | To be added -To serialize plutus data from cbor |
Expand Down Expand Up @@ -84,6 +85,7 @@ The APIs of `sidan-csl-rs` consists of 3 parts:
| `MeshTxBuilderCore` User-facing Method | `invalid_hereafter` | Sets the transaction valid interval to be valid only before the slot. |
| `MeshTxBuilderCore` User-facing Method | `metadata_value` | Adds metadata to the transaction. |
| `MeshTxBuilderCore` User-facing Method | `signing_key` | Signs the transaction with the private key. |
| `MeshTxBuilderCore` User-facing Method | `tx_hex` | Obtain the current transaction hex from build |
| `MeshTxBuilderCore` User-facing Method | `reset` | [To be implemented] Resetting the whole MeshTxBuilderCore |
| `MeshTxBuilderCore` User-facing Method | `emptyTxBuilderBody` | [To be implemented] Resetting the body object |
| `MeshTxBuilderCore` User-facing Method | `complete_sync` | Determine whether using customizedTx, if not queue all last items then serialize the tx |
Expand Down
4 changes: 4 additions & 0 deletions src/builder/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ impl IMeshTxBuilderCore for MeshTxBuilderCore {
}
}

fn tx_hex(&mut self) -> String {
self.mesh_csl.tx_hex.to_string()
}

fn complete_sync(
&mut self,
customized_tx: Option<MeshTxBuilderBody>,
Expand Down
9 changes: 9 additions & 0 deletions src/builder/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ pub trait IMeshTxBuilderCore {
/// * `Self` - A new MeshTxBuilder instance
fn new() -> Self;

/// ## Transaction building method
///
/// Obtain the transaction hex
///
/// ### Returns
///
/// * tx_hex - The current transaction hex from build
fn tx_hex(&mut self) -> String;

/// ## Transaction building method
///
/// Complete the transaction building process synchronously
Expand Down
26 changes: 2 additions & 24 deletions src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub use cardano_serialization_lib as csl;

use crate::model::*;

use super::utils::{build_tx_builder, to_bignum, to_value};
use super::utils::{build_tx_builder, sign_transaction, to_bignum, to_value};

pub trait IMeshCSL {
fn new() -> Self;
Expand Down Expand Up @@ -345,29 +345,7 @@ impl IMeshCSL for MeshCSL {
}

fn add_signing_keys(&mut self, signing_keys: Vec<String>) {
let mut vkey_witnesses = csl::crypto::Vkeywitnesses::new();
let unsigned_transaction: csl::Transaction =
csl::Transaction::from_hex(&self.tx_hex).unwrap();
let tx_body = unsigned_transaction.body();
for key in signing_keys {
let clean_hex = if &key[0..4] == "5820" {
key[4..].to_string()
} else {
key
};
let skey = csl::crypto::PrivateKey::from_hex(&clean_hex).unwrap();
let vkey_witness =
csl::utils::make_vkey_witness(&csl::utils::hash_transaction(&tx_body), &skey);
vkey_witnesses.add(&vkey_witness);
}
let mut witness_set = unsigned_transaction.witness_set();
witness_set.set_vkeys(&vkey_witnesses);
let signed_transaction = csl::Transaction::new(
&tx_body,
&witness_set,
unsigned_transaction.auxiliary_data(),
);
self.tx_hex = signed_transaction.to_hex();
self.tx_hex = sign_transaction(self.tx_hex.to_string(), signing_keys);
}

fn add_required_signature(&mut self, pub_key_hash: String) {
Expand Down
25 changes: 25 additions & 0 deletions src/core/utils/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,28 @@ fn test_calculate_tx_hash_2() {
"e8b7aefcee2953cf55a01c97565cfe9d414a21e17064d8fcef1f632f7311f933"
)
}

pub fn sign_transaction(tx_hex: String, signing_keys: Vec<String>) -> String {
let mut vkey_witnesses = csl::crypto::Vkeywitnesses::new();
let unsigned_transaction: csl::Transaction = csl::Transaction::from_hex(&tx_hex).unwrap();
let tx_body = unsigned_transaction.body();
for key in signing_keys {
let clean_hex = if &key[0..4] == "5820" {
key[4..].to_string()
} else {
key
};
let skey = csl::crypto::PrivateKey::from_hex(&clean_hex).unwrap();
let vkey_witness =
csl::utils::make_vkey_witness(&csl::utils::hash_transaction(&tx_body), &skey);
vkey_witnesses.add(&vkey_witness);
}
let mut witness_set = unsigned_transaction.witness_set();
witness_set.set_vkeys(&vkey_witnesses);
let signed_transaction = csl::Transaction::new(
&tx_body,
&witness_set,
unsigned_transaction.auxiliary_data(),
);
signed_transaction.to_hex()
}

0 comments on commit bd5b711

Please sign in to comment.