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(&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(&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(&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().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(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(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(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(&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(&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(&msg, &nonce_sec_hex, index)
78        .map_err(|e| PyErr::new::<PyException, _>(e))
79}
80
81/// Verify a Schnorr signature over a message, using a child key
82#[pyfunction]
83pub fn verify_schnorr(msg: String, signature_hex: String, index: u32) -> PyResult<bool> {
84    dlccryptlib::verify_schnorr(&msg, &signature_hex, index)
85        .map_err(|e| PyErr::new::<PyException, _>(e))
86}
87
88/// Combine a number of public keys into one
89#[pyfunction]
90pub fn combine_pubkeys(keys_hex: String) -> PyResult<String> {
91    dlccryptlib::combine_pubkeys(&keys_hex).map_err(|e| PyErr::new::<PyException, _>(e))
92}
93
94/// Combine a number of secret keys into one.
95/// Warning: Handle secret keys with caution!
96#[pyfunction]
97pub fn combine_seckeys(keys_hex: String) -> PyResult<String> {
98    dlccryptlib::combine_seckeys(&keys_hex).map_err(|e| PyErr::new::<PyException, _>(e))
99}
100
101/// Create adaptor signatures for a number of CETs
102#[pyfunction]
103pub fn create_cet_adaptor_sigs(
104    num_digits: u8,
105    num_cets: u64,
106    digit_string_template: String,
107    oracle_pubkey: String,
108    signing_key_index: u32,
109    signing_pubkey: String,
110    nonces: String,
111    interval_wildcards: String,
112    sighashes: String,
113) -> PyResult<String> {
114    dlccryptlib::create_cet_adaptor_sigs(
115        num_digits,
116        num_cets,
117        &digit_string_template,
118        &oracle_pubkey,
119        signing_key_index,
120        &signing_pubkey,
121        &nonces,
122        &interval_wildcards,
123        &sighashes,
124    )
125    .map_err(|e| PyErr::new::<PyException, _>(e))
126}
127
128/// Verify adaptor signatures for a number of CETs
129#[pyfunction]
130pub fn verify_cet_adaptor_sigs(
131    num_digits: u8,
132    num_cets: u64,
133    digit_string_template: String,
134    oracle_pubkey: String,
135    signing_pubkey: String,
136    nonces: String,
137    interval_wildcards: String,
138    sighashes: String,
139    signatures: String,
140) -> PyResult<bool> {
141    dlccryptlib::verify_cet_adaptor_sigs(
142        num_digits,
143        num_cets,
144        &digit_string_template,
145        &oracle_pubkey,
146        &signing_pubkey,
147        &nonces,
148        &interval_wildcards,
149        &sighashes,
150        &signatures,
151    )
152    .map_err(|e| PyErr::new::<PyException, _>(e))
153}
154
155/// Perform final signing of a CET
156#[pyfunction]
157pub fn create_final_cet_sigs(
158    signing_key_index: u32,
159    signing_pubkey: String,
160    other_pubkey: String,
161    num_digits: u8,
162    oracle_signatures: String,
163    cet_value_wildcard: String,
164    cet_sighash: String,
165    other_adaptor_signature: String,
166) -> PyResult<String> {
167    dlccryptlib::create_final_cet_sigs(
168        signing_key_index,
169        &signing_pubkey,
170        &other_pubkey,
171        num_digits,
172        &oracle_signatures,
173        &cet_value_wildcard,
174        &cet_sighash,
175        &other_adaptor_signature,
176    )
177    .map_err(|e| PyErr::new::<PyException, _>(e))
178}
179
180#[pymodule]
181fn dlccryptlib_py(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
182    m.add_function(wrap_pyfunction!(init, m)?)?;
183    m.add_function(wrap_pyfunction!(reinit_for_testing, m)?)?;
184    m.add_function(wrap_pyfunction!(init_with_entropy, m)?)?;
185    m.add_function(wrap_pyfunction!(get_xpub, m)?)?;
186    m.add_function(wrap_pyfunction!(get_public_key, m)?)?;
187    m.add_function(wrap_pyfunction!(get_address, m)?)?;
188    m.add_function(wrap_pyfunction!(verify_public_key, m)?)?;
189    m.add_function(wrap_pyfunction!(sign_hash_ecdsa, m)?)?;
190    m.add_function(wrap_pyfunction!(create_deterministic_nonce, m)?)?;
191    m.add_function(wrap_pyfunction!(sign_schnorr_with_nonce, m)?)?;
192    m.add_function(wrap_pyfunction!(verify_schnorr, m)?)?;
193    m.add_function(wrap_pyfunction!(combine_pubkeys, m)?)?;
194    m.add_function(wrap_pyfunction!(combine_seckeys, m)?)?;
195    m.add_function(wrap_pyfunction!(create_cet_adaptor_sigs, m)?)?;
196    m.add_function(wrap_pyfunction!(verify_cet_adaptor_sigs, m)?)?;
197    m.add_function(wrap_pyfunction!(create_final_cet_sigs, m)?)?;
198    Ok(())
199}
200