dlccryptlib_py/
lib.rs

1use dlccryptlib;
2
3use pyo3::prelude::*;
4use pyo3::exceptions::PyException;
5use pyo3::wrap_pyfunction;
6
7
8// ##### Facade functions for easy Python invocations (pyo3/maturin)
9
10/// Initialize the library, load secret from encrypted file. Return the XPUB.
11#[pyfunction]
12pub fn init(path_for_secret_file: String, encryption_password: String) -> PyResult<String> {
13    dlccryptlib::init_intern(&path_for_secret_file, &encryption_password, false)
14        .map_err(|e| PyErr::new::<PyException, _>(e))
15}
16
17#[pyfunction]
18pub fn reinit_for_testing(
19    path_for_secret_file: String,
20    encryption_password: String,
21) -> PyResult<String> {
22    dlccryptlib::init_intern(&path_for_secret_file, &encryption_password, true)
23        .map_err(|e| PyErr::new::<PyException, _>(e))
24}
25
26/// network: "bitcoin", or "signet".
27// #[cfg(test)]
28#[pyfunction]
29pub fn init_with_entropy(entropy: String, network: String) -> PyResult<String> {
30    dlccryptlib::init_with_entropy_intern(&entropy, &network).map_err(|e| PyErr::new::<PyException, _>(e))
31}
32
33/// Return the XPUB
34#[pyfunction]
35pub fn get_xpub() -> PyResult<String> {
36    dlccryptlib::get_xpub_intern().map_err(|e| PyErr::new::<PyException, _>(e))
37}
38
39/// Return a child public key (specified by its index).
40#[pyfunction]
41pub fn get_public_key(index: u32) -> PyResult<String> {
42    dlccryptlib::get_public_key_intern(index).map_err(|e| PyErr::new::<PyException, _>(e))
43}
44
45/// Return a child address (specified by index).
46#[pyfunction]
47pub fn get_address(index: u32) -> PyResult<String> {
48    dlccryptlib::get_address_intern(index).map_err(|e| PyErr::new::<PyException, _>(e))
49}
50
51/// Verify a child public key.
52#[pyfunction]
53pub fn verify_public_key(index: u32, pubkey: String) -> PyResult<bool> {
54    dlccryptlib::verify_public_key_intern(index, &pubkey).map_err(|e| PyErr::new::<PyException, _>(e))
55}
56
57/// Sign a hash with a child private key (specified by index).
58#[pyfunction]
59pub fn sign_hash_ecdsa(hash: String, signer_index: u32, signer_pubkey: String) -> PyResult<String> {
60    dlccryptlib::sign_hash_ecdsa_intern(&hash, signer_index, &signer_pubkey)
61        .map_err(|e| PyErr::new::<PyException, _>(e))
62}
63
64/// Create a nonce value deterministically
65#[pyfunction]
66pub fn create_deterministic_nonce(
67    event_id: String,
68    nonce_index: u32,
69) -> PyResult<(String, String)> {
70    dlccryptlib::create_deterministic_nonce_intern(&event_id, nonce_index)
71        .map_err(|e| PyErr::new::<PyException, _>(e))
72}
73
74/// Sign a message using Schnorr, with a nonce, using a child key
75#[pyfunction]
76pub fn sign_schnorr_with_nonce(msg: String, nonce_sec_hex: String, index: u32) -> PyResult<String> {
77    dlccryptlib::sign_schnorr_with_nonce_intern(&msg, &nonce_sec_hex, index)
78        .map_err(|e| PyErr::new::<PyException, _>(e))
79}
80
81/// Combine a number of public keys into one
82#[pyfunction]
83pub fn combine_pubkeys(keys_hex: String) -> PyResult<String> {
84    dlccryptlib::combine_pubkeys_intern(&keys_hex).map_err(|e| PyErr::new::<PyException, _>(e))
85}
86
87/// Combine a number of secret keys into one.
88/// Warning: Handle secret keys with caution!
89#[pyfunction]
90pub fn combine_seckeys(keys_hex: String) -> PyResult<String> {
91    dlccryptlib::combine_seckeys_intern(&keys_hex).map_err(|e| PyErr::new::<PyException, _>(e))
92}
93
94/// Create adaptor signatures for a number of CETs
95#[pyfunction]
96pub fn create_cet_adaptor_sigs(
97    num_digits: u8,
98    num_cets: u64,
99    digit_string_template: String,
100    oracle_pubkey: String,
101    signing_key_index: u32,
102    signing_pubkey: String,
103    nonces: String,
104    interval_wildcards: String,
105    sighashes: String,
106) -> PyResult<String> {
107    dlccryptlib::create_cet_adaptor_sigs_intern(
108        num_digits,
109        num_cets,
110        &digit_string_template,
111        &oracle_pubkey,
112        signing_key_index,
113        &signing_pubkey,
114        &nonces,
115        &interval_wildcards,
116        &sighashes,
117    )
118    .map_err(|e| PyErr::new::<PyException, _>(e))
119}
120
121/// Verify adaptor signatures for a number of CETs
122#[pyfunction]
123pub fn verify_cet_adaptor_sigs(
124    num_digits: u8,
125    num_cets: u64,
126    digit_string_template: String,
127    oracle_pubkey: String,
128    signing_pubkey: String,
129    nonces: String,
130    interval_wildcards: String,
131    sighashes: String,
132    signatures: String,
133) -> PyResult<bool> {
134    dlccryptlib::verify_cet_adaptor_sigs_intern(
135        num_digits,
136        num_cets,
137        &digit_string_template,
138        &oracle_pubkey,
139        &signing_pubkey,
140        &nonces,
141        &interval_wildcards,
142        &sighashes,
143        &signatures,
144    )
145    .map_err(|e| PyErr::new::<PyException, _>(e))
146}
147
148/// Perform final signing of a CET
149#[pyfunction]
150pub fn create_final_cet_sigs(
151    signing_key_index: u32,
152    signing_pubkey: String,
153    other_pubkey: String,
154    num_digits: u8,
155    oracle_signatures: String,
156    cet_value_wildcard: String,
157    cet_sighash: String,
158    other_adaptor_signature: String,
159) -> PyResult<String> {
160    dlccryptlib::create_final_cet_sigs_intern(
161        signing_key_index,
162        &signing_pubkey,
163        &other_pubkey,
164        num_digits,
165        &oracle_signatures,
166        &cet_value_wildcard,
167        &cet_sighash,
168        &other_adaptor_signature,
169    )
170    .map_err(|e| PyErr::new::<PyException, _>(e))
171}
172
173#[pymodule]
174fn dlccryptlib_py(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
175    m.add_function(wrap_pyfunction!(init, m)?)?;
176    m.add_function(wrap_pyfunction!(reinit_for_testing, m)?)?;
177    m.add_function(wrap_pyfunction!(init_with_entropy, m)?)?;
178    m.add_function(wrap_pyfunction!(get_xpub, m)?)?;
179    m.add_function(wrap_pyfunction!(get_public_key, m)?)?;
180    m.add_function(wrap_pyfunction!(get_address, m)?)?;
181    m.add_function(wrap_pyfunction!(verify_public_key, m)?)?;
182    m.add_function(wrap_pyfunction!(sign_hash_ecdsa, m)?)?;
183    m.add_function(wrap_pyfunction!(create_deterministic_nonce, m)?)?;
184    m.add_function(wrap_pyfunction!(sign_schnorr_with_nonce, m)?)?;
185    m.add_function(wrap_pyfunction!(combine_pubkeys, m)?)?;
186    m.add_function(wrap_pyfunction!(combine_seckeys, m)?)?;
187    m.add_function(wrap_pyfunction!(create_cet_adaptor_sigs, m)?)?;
188    m.add_function(wrap_pyfunction!(verify_cet_adaptor_sigs, m)?)?;
189    m.add_function(wrap_pyfunction!(create_final_cet_sigs, m)?)?;
190    Ok(())
191}
192