conxius-enclave-sdk 2.0.9

Hardware-backed security primitives for the broader Conxian ecosystem. Provides high-integrity root of trust for security-sensitive wallet, signing, attestation, and policy flows.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use crate::enclave::EnclaveManager;
use crate::protocol::ark::ArkManager;
use crate::protocol::bitvm::BitVmManager;
use crate::protocol::ethereum::EthereumManager;
use crate::protocol::solana::SolanaManager;
use hex;
use serde_wasm_bindgen;
use std::sync::Arc;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct ConclaveWasmClient {
    enclave: Arc<dyn EnclaveManager>,
}

#[wasm_bindgen]
impl ConclaveWasmClient {
    #[wasm_bindgen(constructor)]
    pub fn new(enclave_url: &str) -> Result<ConclaveWasmClient, JsValue> {
        let enclave = Arc::new(
            crate::enclave::cloud::CloudEnclave::new(enclave_url.to_string())
                .map_err(to_js_error)?,
        );
        Ok(ConclaveWasmClient { enclave })
    }

    pub fn ark(&self) -> WasmArkClient {
        WasmArkClient {
            inner: Arc::new(ArkManager::new(self.enclave.clone())),
        }
    }

    pub fn bitvm(&self) -> WasmBitVmClient {
        WasmBitVmClient {
            inner: Arc::new(BitVmManager::new(self.enclave.clone())),
        }
    }
}

#[wasm_bindgen]
pub struct WasmArkClient {
    #[wasm_bindgen(skip)]
    pub inner: Arc<ArkManager>,
}

#[wasm_bindgen]
impl WasmArkClient {
    pub fn derive_vutxo_key(&self, seed_hex: &str, index: u32) -> Result<String, JsValue> {
        let seed = hex::decode(seed_hex).map_err(to_js_error)?;
        let key = self.inner.derive_vutxo_key(&seed, index);
        Ok(hex::encode(key))
    }

    pub async fn recovery_scan(
        &self,
        master_seed_hex: &str,
        gap_limit: u32,
        asp_url: &str,
    ) -> Result<JsValue, JsValue> {
        let seed_bytes = hex::decode(master_seed_hex).map_err(to_js_error)?;
        let seed: [u8; 32] = seed_bytes
            .try_into()
            .map_err(|_| JsValue::from_str("Invalid seed length"))?;

        let found = self
            .inner
            .recovery_scan(seed, gap_limit, asp_url)
            .await
            .map_err(to_js_error)?;
        serde_wasm_bindgen::to_value(&found).map_err(to_js_error)
    }

    pub fn construct_vtxo_tree(&self, leaves: JsValue) -> Result<JsValue, JsValue> {
        let leaves_vec = serde_wasm_bindgen::from_value(leaves).map_err(to_js_error)?;
        let root = self
            .inner
            .construct_vtxo_tree(leaves_vec)
            .map_err(to_js_error)?;
        serde_wasm_bindgen::to_value(&root).map_err(to_js_error)
    }
}

#[wasm_bindgen]
pub struct WasmBitVmClient {
    #[wasm_bindgen(skip)]
    pub inner: Arc<BitVmManager>,
}

#[wasm_bindgen]
impl WasmBitVmClient {
    pub fn sign_challenge(
        &self,
        challenge: JsValue,
        path: &str,
        key_id: &str,
    ) -> Result<String, JsValue> {
        let chal = serde_wasm_bindgen::from_value(challenge).map_err(to_js_error)?;
        self.inner
            .sign_challenge(chal, path, key_id)
            .map_err(to_js_error)
    }

    pub fn aggregate_challenge_signatures(
        &self,
        pubkeys_hex: JsValue,
        pub_nonces_hex: JsValue,
        partial_sigs_hex: JsValue,
        challenge: JsValue,
    ) -> Result<JsValue, JsValue> {
        let pks: Vec<String> = serde_wasm_bindgen::from_value(pubkeys_hex).map_err(to_js_error)?;
        let nonces: Vec<String> =
            serde_wasm_bindgen::from_value(pub_nonces_hex).map_err(to_js_error)?;
        let sigs: Vec<String> =
            serde_wasm_bindgen::from_value(partial_sigs_hex).map_err(to_js_error)?;
        let chal = serde_wasm_bindgen::from_value(challenge).map_err(to_js_error)?;

        let mut pks_decoded = Vec::new();
        for pk in pks {
            let bytes = hex::decode(pk).map_err(to_js_error)?;
            pks_decoded.push(secp256k1::PublicKey::from_slice(&bytes).map_err(to_js_error)?);
        }

        let mut nonces_decoded = Vec::new();
        for n in nonces {
            nonces_decoded.push(musig2::PubNonce::from_hex(&n).map_err(to_js_error)?);
        }

        let mut sigs_decoded = Vec::new();
        for s in sigs {
            sigs_decoded.push(musig2::PartialSignature::from_hex(&s).map_err(to_js_error)?);
        }

        let aggregate = self
            .inner
            .aggregate_challenge_signatures(&pks_decoded, nonces_decoded, sigs_decoded, chal)
            .map_err(to_js_error)?;
        serde_wasm_bindgen::to_value(&aggregate).map_err(to_js_error)
    }
}

#[wasm_bindgen]
pub struct Iso20022Wrapper;

#[wasm_bindgen]
impl Iso20022Wrapper {
    pub fn wrap_pacs008(card: JsValue) -> Result<String, JsValue> {
        let card: crate::protocol::job_card::ConxianJobCard =
            serde_wasm_bindgen::from_value(card).map_err(to_js_error)?;
        crate::protocol::job_card::Iso20022Wrapper::wrap_pacs008(&card).map_err(to_js_error)
    }
}

#[wasm_bindgen]
pub struct WasmEthereumManager {
    #[wasm_bindgen(skip)]
    pub enclave: Arc<dyn EnclaveManager>,
}

#[wasm_bindgen]
impl WasmEthereumManager {
    pub fn prepare_erc20_transfer(
        &self,
        to: &str,
        amount: &str,
        contract: &str,
    ) -> Result<Vec<u8>, JsValue> {
        let amt = amount.parse::<u128>().map_err(to_js_error)?;
        let transfer = crate::protocol::ethereum::Erc20Transfer {
            to: to.to_string(),
            amount: amt,
            contract_address: contract.to_string(),
        };
        Ok(EthereumManager::new(self.enclave.as_ref()).prepare_erc20_transfer(transfer))
    }
}

#[wasm_bindgen]
pub struct WasmSolanaManager {
    #[wasm_bindgen(skip)]
    pub enclave: Arc<dyn EnclaveManager>,
}

#[wasm_bindgen]
impl WasmSolanaManager {
    pub fn prepare_spl_transfer(
        &self,
        source: &str,
        dest: &str,
        amount: u64,
        owner: &str,
    ) -> Result<Vec<u8>, JsValue> {
        let transfer = crate::protocol::solana::SplTransfer {
            source_token_account: source.to_string(),
            destination_token_account: dest.to_string(),
            amount,
            owner: owner.to_string(),
        };
        Ok(SolanaManager::new(self.enclave.as_ref()).prepare_spl_transfer(transfer))
    }
}

#[wasm_bindgen]
pub struct WasmAccountClient {
    #[wasm_bindgen(skip)]
    pub inner: crate::protocol::account_abstraction::ModularAccountManager,
}

#[wasm_bindgen]
impl WasmAccountClient {
    pub fn prepare_execution(&self, actions: JsValue) -> Result<JsValue, JsValue> {
        let acts = serde_wasm_bindgen::from_value(actions).map_err(to_js_error)?;
        let execution = self.inner.prepare_execution(acts);
        serde_wasm_bindgen::to_value(&execution).map_err(to_js_error)
    }
}

#[wasm_bindgen]
pub struct WasmCctpClient {
    #[wasm_bindgen(skip)]
    pub inner: crate::protocol::cctp::CctpManager,
}

#[wasm_bindgen]
impl WasmCctpClient {
    pub fn prepare_burn_payload(&self, intent: JsValue) -> Result<Vec<u8>, JsValue> {
        let intent_obj = serde_wasm_bindgen::from_value(intent).map_err(to_js_error)?;
        Ok(self.inner.prepare_burn_payload(intent_obj))
    }
}

#[wasm_bindgen]
impl ConclaveWasmClient {
    pub fn ethereum(&self) -> WasmEthereumManager {
        WasmEthereumManager {
            enclave: self.enclave.clone(),
        }
    }

    pub fn solana(&self) -> WasmSolanaManager {
        WasmSolanaManager {
            enclave: self.enclave.clone(),
        }
    }

    pub fn accounts(&self) -> WasmAccountClient {
        WasmAccountClient {
            inner: crate::protocol::account_abstraction::ModularAccountManager::new(),
        }
    }

    pub fn cctp(&self) -> WasmCctpClient {
        WasmCctpClient {
            inner: crate::protocol::cctp::CctpManager::new(),
        }
    }
}

#[wasm_bindgen]
pub struct WasmIntentClient;

#[wasm_bindgen]
impl WasmIntentClient {
    pub fn instrument_context(symbol: &str, chain: &str) -> Result<JsValue, JsValue> {
        let ctx = crate::protocol::intent::Fdc3Context::instrument(symbol, chain);
        serde_wasm_bindgen::to_value(&ctx).map_err(to_js_error)
    }

    pub fn settlement_context(
        amount: &str,
        asset: &str,
        recipient: &str,
    ) -> Result<JsValue, JsValue> {
        let amt = amount.parse::<u128>().map_err(to_js_error)?;
        let ctx = crate::protocol::intent::Fdc3Context::settlement(amt, asset, recipient);
        serde_wasm_bindgen::to_value(&ctx).map_err(to_js_error)
    }
}

#[wasm_bindgen]
impl ConclaveWasmClient {
    pub fn intent(&self) -> WasmIntentClient {
        WasmIntentClient
    }
}

#[wasm_bindgen]
pub struct WasmFrostClient {
    #[wasm_bindgen(skip)]
    pub inner: crate::protocol::frost::FrostManager,
}

#[wasm_bindgen]
impl WasmFrostClient {
    pub fn generate_key_package(
        &self,
        min_signers: u32,
        total_signers: u32,
        identifier: &str,
    ) -> Result<JsValue, JsValue> {
        let package = crate::protocol::frost::FrostManager::generate_key_package(
            min_signers,
            total_signers,
            identifier,
        )
        .map_err(to_js_error)?;
        serde_wasm_bindgen::to_value(&package).map_err(to_js_error)
    }
}

#[wasm_bindgen]
pub struct WasmCovenantClient {
    #[wasm_bindgen(skip)]
    pub inner: crate::protocol::covenant::CovenantManager,
}

#[wasm_bindgen]
impl WasmCovenantClient {
    pub fn generate_cat_vault_script(
        &self,
        internal_key_hex: &str,
        template_hash_hex: &str,
    ) -> Result<JsValue, JsValue> {
        let pk_bytes = hex::decode(internal_key_hex).map_err(to_js_error)?;
        let pk_arr: [u8; 32] = pk_bytes
            .try_into()
            .map_err(|_| JsValue::from_str("Invalid key length"))?;
        let pk = bitcoin::XOnlyPublicKey::from_byte_array(&pk_arr).map_err(to_js_error)?;
        let hash_bytes = hex::decode(template_hash_hex).map_err(to_js_error)?;
        let hash: [u8; 32] = hash_bytes
            .try_into()
            .map_err(|_| JsValue::from_str("Invalid hash length"))?;

        let script =
            crate::protocol::covenant::CovenantManager::generate_cat_vault_script(&pk, hash)
                .map_err(to_js_error)?;
        serde_wasm_bindgen::to_value(&script).map_err(to_js_error)
    }

    pub fn verify_recursive_invariant(
        &self,
        witness: JsValue,
        expected_hash_hex: &str,
    ) -> Result<bool, JsValue> {
        let witness_vec: Vec<Vec<u8>> =
            serde_wasm_bindgen::from_value(witness).map_err(to_js_error)?;
        let hash_bytes = hex::decode(expected_hash_hex).map_err(to_js_error)?;
        let hash: [u8; 32] = hash_bytes
            .try_into()
            .map_err(|_| JsValue::from_str("Invalid hash length"))?;

        self.inner
            .verify_recursive_invariant(&witness_vec, hash)
            .map_err(to_js_error)
    }
}

#[wasm_bindgen]
impl ConclaveWasmClient {
    pub fn frost(&self) -> WasmFrostClient {
        WasmFrostClient {
            inner: crate::protocol::frost::FrostManager,
        }
    }

    pub fn covenants(&self) -> WasmCovenantClient {
        WasmCovenantClient {
            inner: crate::protocol::covenant::CovenantManager,
        }
    }
}

fn to_js_error<E: std::fmt::Display>(e: E) -> JsValue {
    JsValue::from_str(&e.to_string())
}

#[wasm_bindgen]
pub struct WasmFedimintClient {
    #[wasm_bindgen(skip)]
    pub inner: crate::protocol::nexus::fedimint::FedimintAdapter,
}

#[wasm_bindgen]
impl WasmFedimintClient {
    pub fn register_federation(&mut self, federation_id: &str) -> Result<(), JsValue> {
        self.inner
            .register_federation(federation_id)
            .map_err(to_js_error)
    }

    pub fn join_federation(&mut self, invite_code: &str) -> Result<String, JsValue> {
        self.inner.join_federation(invite_code).map_err(to_js_error)
    }

    pub fn prepare_mint_intent(
        &self,
        federation_id: &str,
        amount_sats: u64,
        secrets: JsValue,
    ) -> Result<JsValue, JsValue> {
        let secrets_vec: Vec<String> =
            serde_wasm_bindgen::from_value(secrets).map_err(to_js_error)?;
        let secrets_refs: Vec<&str> = secrets_vec.iter().map(|s| s.as_str()).collect();
        let (intent, bf) = self
            .inner
            .prepare_mint_intent(federation_id, amount_sats, secrets_refs)
            .map_err(to_js_error)?;

        let res = serde_json::json!({
            "intent": intent,
            "blinding_factors": bf
        });
        serde_wasm_bindgen::to_value(&res).map_err(to_js_error)
    }

    pub fn issue_ecash(
        &self,
        intent: JsValue,
        blinding_factors: JsValue,
        original_secrets: JsValue,
    ) -> Result<JsValue, JsValue> {
        let intent_obj = serde_wasm_bindgen::from_value(intent).map_err(to_js_error)?;
        let bf_obj = serde_wasm_bindgen::from_value(blinding_factors).map_err(to_js_error)?;
        let secrets_obj = serde_wasm_bindgen::from_value(original_secrets).map_err(to_js_error)?;

        let ecash = self
            .inner
            .issue_ecash(intent_obj, bf_obj, secrets_obj)
            .map_err(to_js_error)?;
        serde_wasm_bindgen::to_value(&ecash).map_err(to_js_error)
    }

    pub fn verify_note(&self, note: JsValue) -> Result<bool, JsValue> {
        let note_obj = serde_wasm_bindgen::from_value(note).map_err(to_js_error)?;
        Ok(self.inner.verify_note(&note_obj))
    }
}

#[wasm_bindgen]
impl ConclaveWasmClient {
    pub fn fedimint(&self) -> WasmFedimintClient {
        WasmFedimintClient {
            inner: crate::protocol::nexus::fedimint::FedimintAdapter::new(),
        }
    }
}