Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dlccryptlib"
version = "0.9.0"
version = "0.9.1"
edition = "2021"
description = "Library for working with DLC's with adaptor signatures (Discrete Log Contracts), by Cadena Bitcoin"
license = "MIT"
Expand Down
4 changes: 2 additions & 2 deletions lib-py/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dlccryptlib-py"
version = "0.9.0"
version = "0.9.1"
edition = "2021"
description = "Python-wrapped library for working with DLC's with adaptor signatures (Discrete Log Contracts), by Cadena Bitcoin"
license = "MIT"
Expand All @@ -9,6 +9,6 @@ license = "MIT"
name = "dlccryptlib_py"

[dependencies]
#dlccryptlib = "0.9.0"
#dlccryptlib = "0.9.1"
dlccryptlib = { path = "../" }
pyo3 = { version = "0.23.1" }
30 changes: 15 additions & 15 deletions lib-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use pyo3::wrap_pyfunction;
/// Initialize the library, load secret from encrypted file. Return the XPUB.
#[pyfunction]
pub fn init(path_for_secret_file: String, encryption_password: String) -> PyResult<String> {
dlccryptlib::init_intern(&path_for_secret_file, &encryption_password, false)
dlccryptlib::init(&path_for_secret_file, &encryption_password, false)
.map_err(|e| PyErr::new::<PyException, _>(e))
}

Expand All @@ -19,45 +19,45 @@ pub fn reinit_for_testing(
path_for_secret_file: String,
encryption_password: String,
) -> PyResult<String> {
dlccryptlib::init_intern(&path_for_secret_file, &encryption_password, true)
dlccryptlib::init(&path_for_secret_file, &encryption_password, true)
.map_err(|e| PyErr::new::<PyException, _>(e))
}

/// network: "bitcoin", or "signet".
// #[cfg(test)]
#[pyfunction]
pub fn init_with_entropy(entropy: String, network: String) -> PyResult<String> {
dlccryptlib::init_with_entropy_intern(&entropy, &network).map_err(|e| PyErr::new::<PyException, _>(e))
dlccryptlib::init_with_entropy(&entropy, &network).map_err(|e| PyErr::new::<PyException, _>(e))
}

/// Return the XPUB
#[pyfunction]
pub fn get_xpub() -> PyResult<String> {
dlccryptlib::get_xpub_intern().map_err(|e| PyErr::new::<PyException, _>(e))
dlccryptlib::get_xpub().map_err(|e| PyErr::new::<PyException, _>(e))
}

/// Return a child public key (specified by its index).
#[pyfunction]
pub fn get_public_key(index: u32) -> PyResult<String> {
dlccryptlib::get_public_key_intern(index).map_err(|e| PyErr::new::<PyException, _>(e))
dlccryptlib::get_public_key(index).map_err(|e| PyErr::new::<PyException, _>(e))
}

/// Return a child address (specified by index).
#[pyfunction]
pub fn get_address(index: u32) -> PyResult<String> {
dlccryptlib::get_address_intern(index).map_err(|e| PyErr::new::<PyException, _>(e))
dlccryptlib::get_address(index).map_err(|e| PyErr::new::<PyException, _>(e))
}

/// Verify a child public key.
#[pyfunction]
pub fn verify_public_key(index: u32, pubkey: String) -> PyResult<bool> {
dlccryptlib::verify_public_key_intern(index, &pubkey).map_err(|e| PyErr::new::<PyException, _>(e))
dlccryptlib::verify_public_key(index, &pubkey).map_err(|e| PyErr::new::<PyException, _>(e))
}

/// Sign a hash with a child private key (specified by index).
#[pyfunction]
pub fn sign_hash_ecdsa(hash: String, signer_index: u32, signer_pubkey: String) -> PyResult<String> {
dlccryptlib::sign_hash_ecdsa_intern(&hash, signer_index, &signer_pubkey)
dlccryptlib::sign_hash_ecdsa(&hash, signer_index, &signer_pubkey)
.map_err(|e| PyErr::new::<PyException, _>(e))
}

Expand All @@ -67,28 +67,28 @@ pub fn create_deterministic_nonce(
event_id: String,
nonce_index: u32,
) -> PyResult<(String, String)> {
dlccryptlib::create_deterministic_nonce_intern(&event_id, nonce_index)
dlccryptlib::create_deterministic_nonce(&event_id, nonce_index)
.map_err(|e| PyErr::new::<PyException, _>(e))
}

/// Sign a message using Schnorr, with a nonce, using a child key
#[pyfunction]
pub fn sign_schnorr_with_nonce(msg: String, nonce_sec_hex: String, index: u32) -> PyResult<String> {
dlccryptlib::sign_schnorr_with_nonce_intern(&msg, &nonce_sec_hex, index)
dlccryptlib::sign_schnorr_with_nonce(&msg, &nonce_sec_hex, index)
.map_err(|e| PyErr::new::<PyException, _>(e))
}

/// Combine a number of public keys into one
#[pyfunction]
pub fn combine_pubkeys(keys_hex: String) -> PyResult<String> {
dlccryptlib::combine_pubkeys_intern(&keys_hex).map_err(|e| PyErr::new::<PyException, _>(e))
dlccryptlib::combine_pubkeys(&keys_hex).map_err(|e| PyErr::new::<PyException, _>(e))
}

/// Combine a number of secret keys into one.
/// Warning: Handle secret keys with caution!
#[pyfunction]
pub fn combine_seckeys(keys_hex: String) -> PyResult<String> {
dlccryptlib::combine_seckeys_intern(&keys_hex).map_err(|e| PyErr::new::<PyException, _>(e))
dlccryptlib::combine_seckeys(&keys_hex).map_err(|e| PyErr::new::<PyException, _>(e))
}

/// Create adaptor signatures for a number of CETs
Expand All @@ -104,7 +104,7 @@ pub fn create_cet_adaptor_sigs(
interval_wildcards: String,
sighashes: String,
) -> PyResult<String> {
dlccryptlib::create_cet_adaptor_sigs_intern(
dlccryptlib::create_cet_adaptor_sigs(
num_digits,
num_cets,
&digit_string_template,
Expand All @@ -131,7 +131,7 @@ pub fn verify_cet_adaptor_sigs(
sighashes: String,
signatures: String,
) -> PyResult<bool> {
dlccryptlib::verify_cet_adaptor_sigs_intern(
dlccryptlib::verify_cet_adaptor_sigs(
num_digits,
num_cets,
&digit_string_template,
Expand All @@ -157,7 +157,7 @@ pub fn create_final_cet_sigs(
cet_sighash: String,
other_adaptor_signature: String,
) -> PyResult<String> {
dlccryptlib::create_final_cet_sigs_intern(
dlccryptlib::create_final_cet_sigs(
signing_key_index,
&signing_pubkey,
&other_pubkey,
Expand Down
2 changes: 1 addition & 1 deletion samples/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
#dlccryptlib = "0.9.0"
#dlccryptlib = "0.9.1"
dlccryptlib = { path = "../../" }
6 changes: 3 additions & 3 deletions samples/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ fn main() {
let entropy_hex = "00000000000000000000000000000001"; // abandon x 11 actual
let network = "signet";

let xpub = dlccryptlib::init_with_entropy_intern(entropy_hex, network).unwrap();
let xpub = dlccryptlib::init_with_entropy(entropy_hex, network).unwrap();
println!("xpub: {xpub}");

let pubkey0 = dlccryptlib::get_public_key_intern(0).unwrap();
let pubkey0 = dlccryptlib::get_public_key(0).unwrap();
println!("pubkey 0: {pubkey0}");

let hash = "0001020300000000000000000000000000000000000000000000000000010203";
let sig = dlccryptlib::sign_hash_ecdsa_intern(hash, 0, &pubkey0).unwrap();
let sig = dlccryptlib::sign_hash_ecdsa(hash, 0, &pubkey0).unwrap();
println!("signature: {sig}");
}
16 changes: 8 additions & 8 deletions samples/rust/src/test_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const DEFAULT_NETWORK: &str = NETWORK_SIGNET;

#[test]
fn test_init_with_entropy() {
let xpub = dlccryptlib::init_with_entropy_intern(DUMMY_ENTROPY_STR, DEFAULT_NETWORK).unwrap();
let xpub = dlccryptlib::init_with_entropy(DUMMY_ENTROPY_STR, DEFAULT_NETWORK).unwrap();
assert_eq!(
xpub,
"tpubDCxVvuZwEu4oZypCT3pzos1MUoVJyjTHjfrhKFXNBkAEqBmkkzEb2dUgzpZmBWbd6wZnNmm3Ex2suMnEFUMmayH2a6S49R4pTnoQttGrxUm"
Expand All @@ -19,15 +19,15 @@ fn test_init_with_entropy() {

#[test]
fn test_get_public_key() {
let _xpub = dlccryptlib::init_with_entropy_intern(DUMMY_ENTROPY_STR, DEFAULT_NETWORK).unwrap();
let _xpub = dlccryptlib::init_with_entropy(DUMMY_ENTROPY_STR, DEFAULT_NETWORK).unwrap();

let pubkey0 = dlccryptlib::get_public_key_intern(0).unwrap();
let pubkey0 = dlccryptlib::get_public_key(0).unwrap();
assert_eq!(
pubkey0.to_string(),
"031941e84b8d111e094aefc46e7181757c93a1da87c93ab519a40d9d765176e704"
);

let pubkey3 = dlccryptlib::get_public_key_intern(3).unwrap();
let pubkey3 = dlccryptlib::get_public_key(3).unwrap();
assert_eq!(
pubkey3.to_string(),
"02a9569875400df2b7af9360fc5025de31fcd48ca8b658d61e535c3ff2f55aa128"
Expand All @@ -36,19 +36,19 @@ fn test_get_public_key() {

#[test]
fn test_sign_hash_ecdsa() {
let _xpub = dlccryptlib::init_with_entropy_intern(DUMMY_ENTROPY_STR, DEFAULT_NETWORK).unwrap();
let _xpub = dlccryptlib::init_with_entropy(DUMMY_ENTROPY_STR, DEFAULT_NETWORK).unwrap();

let pubkey3 = dlccryptlib::get_public_key_intern(3).unwrap();
let pubkey3 = dlccryptlib::get_public_key(3).unwrap();
assert_eq!(
pubkey3.to_string(),
"02a9569875400df2b7af9360fc5025de31fcd48ca8b658d61e535c3ff2f55aa128"
);

let hash = DUMMY_HASH07_STR;
let sig = dlccryptlib::sign_hash_ecdsa_intern(&hash, 3, &pubkey3).unwrap();
let sig = dlccryptlib::sign_hash_ecdsa(&hash, 3, &pubkey3).unwrap();

assert!(sig.len() >= 140 && sig.len() <= 146);

// negative test, wrong index
assert!(dlccryptlib::sign_hash_ecdsa_intern(&hash, 31, &pubkey3).is_err());
assert!(dlccryptlib::sign_hash_ecdsa(&hash, 31, &pubkey3).is_err());
}
38 changes: 19 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::os::raw::c_char;
use std::str::FromStr;

/// Initialize the library, load secret from encrypted file. Return the XPUB.
pub fn init_intern(
pub fn init(
path_for_secret_file: &str,
encryption_password: &str,
allow_reinit: bool,
Expand All @@ -43,7 +43,7 @@ pub fn init_intern(

/// Initialize the library, provide the secret as parameter. Return the XPUB.
// #[cfg(test)]
pub fn init_with_entropy_intern(entropy: &str, network: &str) -> Result<String, String> {
pub fn init_with_entropy(entropy: &str, network: &str) -> Result<String, String> {
let entropy_bin = parse_entropy_hex(entropy)?;
global_lib()
.write()
Expand All @@ -53,22 +53,22 @@ pub fn init_with_entropy_intern(entropy: &str, network: &str) -> Result<String,
Ok(xpub.to_string())
}

pub fn get_xpub_intern() -> Result<String, String> {
pub fn get_xpub() -> Result<String, String> {
let xpub = global_lib().read().unwrap().get_xpub()?;
Ok(xpub.to_string())
}

pub fn get_public_key_intern(index: u32) -> Result<String, String> {
pub fn get_public_key(index: u32) -> Result<String, String> {
let pubkey = global_lib().read().unwrap().get_child_public_key(index)?;
Ok(pubkey.to_string())
}

pub fn get_address_intern(index: u32) -> Result<String, String> {
pub fn get_address(index: u32) -> Result<String, String> {
let address = global_lib().read().unwrap().get_address(index)?;
Ok(address.to_string())
}

pub fn verify_public_key_intern(index: u32, pubkey_str: &str) -> Result<bool, String> {
pub fn verify_public_key(index: u32, pubkey_str: &str) -> Result<bool, String> {
let pubkey =
pubkey_from_hex(pubkey_str).map_err(|e| format!("Failed to parse pubkey {}", e))?;
let verify_result = global_lib()
Expand All @@ -78,7 +78,7 @@ pub fn verify_public_key_intern(index: u32, pubkey_str: &str) -> Result<bool, St
Ok(verify_result)
}

pub fn sign_hash_ecdsa_intern(
pub fn sign_hash_ecdsa(
hash_str: &str,
index: u32,
signer_pubkey_str: &str,
Expand All @@ -94,7 +94,7 @@ pub fn sign_hash_ecdsa_intern(
Ok(sig.to_lower_hex_string())
}

pub fn create_deterministic_nonce_intern(
pub fn create_deterministic_nonce(
event_id: &str,
index: u32,
) -> Result<(String, String), String> {
Expand All @@ -106,7 +106,7 @@ pub fn create_deterministic_nonce_intern(
}

// Schnorr signing with nonce
pub fn sign_schnorr_with_nonce_intern(
pub fn sign_schnorr_with_nonce(
msg: &str,
nonce_sec_hex: &str,
index: u32,
Expand All @@ -120,7 +120,7 @@ pub fn sign_schnorr_with_nonce_intern(
Ok(sig.to_string())
}

pub fn combine_pubkeys_intern(keys_hex: &str) -> Result<String, String> {
pub fn combine_pubkeys(keys_hex: &str) -> Result<String, String> {
let keys_split: Vec<_> = keys_hex.split(" ").collect();
let mut keys = Vec::<PublicKey>::with_capacity(keys_split.len());
for i in 0..keys_split.len() {
Expand All @@ -135,7 +135,7 @@ pub fn combine_pubkeys_intern(keys_hex: &str) -> Result<String, String> {
Ok(combined_key.to_string())
}

pub fn combine_seckeys_intern(keys_hex: &str) -> Result<String, String> {
pub fn combine_seckeys(keys_hex: &str) -> Result<String, String> {
let keys_split: Vec<_> = keys_hex.split(" ").collect();
let mut keys = Vec::<SecretKey>::with_capacity(keys_split.len());
for i in 0..keys_split.len() {
Expand All @@ -150,7 +150,7 @@ pub fn combine_seckeys_intern(keys_hex: &str) -> Result<String, String> {
Ok(combined_key.display_secret().to_string())
}

pub fn create_cet_adaptor_sigs_intern(
pub fn create_cet_adaptor_sigs(
num_digits: u8,
num_cets: u64,
digit_string_template: &str,
Expand Down Expand Up @@ -239,7 +239,7 @@ pub fn create_cet_adaptor_sigs_intern(
Ok(sigs_str)
}

pub fn verify_cet_adaptor_sigs_intern(
pub fn verify_cet_adaptor_sigs(
num_digits: u8,
num_cets: u64,
digit_string_template: &str,
Expand Down Expand Up @@ -339,7 +339,7 @@ pub fn verify_cet_adaptor_sigs_intern(
Ok(res.is_ok())
}

pub fn create_final_cet_sigs_intern(
pub fn create_final_cet_sigs(
signing_key_index: u32,
signing_pubkey_str: &str,
other_pubkey_str: &str,
Expand Down Expand Up @@ -419,7 +419,7 @@ pub extern "C" fn init_with_entropy_c(
.unwrap_or("Error in network parameter")
};

match init_with_entropy_intern(entropy_str, network_str) {
match init_with_entropy(entropy_str, network_str) {
Ok(xpub) => {
// Return as a C string
CString::new(xpub).unwrap().into_raw()
Expand All @@ -431,7 +431,7 @@ pub extern "C" fn init_with_entropy_c(
/// Return a child public key (specified by its index).
#[no_mangle]
pub extern "C" fn get_public_key_c(index: u32) -> *mut c_char {
match get_public_key_intern(index) {
match get_public_key(index) {
Ok(pubkey) => {
// Return as a C string
CString::new(pubkey).unwrap().into_raw()
Expand Down Expand Up @@ -459,7 +459,7 @@ pub extern "C" fn sign_hash_ecdsa_c(
.unwrap_or("Error in signer_pubkey parameter")
};

match sign_hash_ecdsa_intern(hash_str, signer_index, signer_pubkey_str) {
match sign_hash_ecdsa(hash_str, signer_index, signer_pubkey_str) {
Ok(sig) => {
// Return as a C string
CString::new(sig).unwrap().into_raw()
Expand Down Expand Up @@ -513,7 +513,7 @@ pub extern "C" fn create_cet_adaptor_sigs_c(
.unwrap_or("Error in sighashes parameter")
};

match create_cet_adaptor_sigs_intern(
match create_cet_adaptor_sigs(
num_digits,
num_cets as u64,
digit_string_template_str,
Expand Down Expand Up @@ -542,7 +542,7 @@ pub extern "C" fn create_deterministic_nonce_c(event_id: *const c_char, index: u
};

// Call your existing function that creates the nonce (assuming this is what you want)
match create_deterministic_nonce_intern(event_id_str, index) {
match create_deterministic_nonce(event_id_str, index) {
Ok((sk, pk)) => {
// Return as a C string
CString::new(format!("{} {}", sk, pk)).unwrap().into_raw()
Expand Down
Loading