lwk/
contract.rs

1use std::sync::Arc;
2
3use elements::hashes::hex::FromHex;
4
5use crate::LwkError;
6
7/// Wrapper over [`lwk_wollet::Contract`]
8#[derive(uniffi::Object)]
9#[uniffi::export(Display)]
10pub struct Contract {
11    inner: lwk_wollet::Contract,
12}
13
14impl From<lwk_wollet::Contract> for Contract {
15    fn from(inner: lwk_wollet::Contract) -> Self {
16        Self { inner }
17    }
18}
19
20impl From<Contract> for lwk_wollet::Contract {
21    fn from(contract: Contract) -> Self {
22        contract.inner
23    }
24}
25
26impl From<&Contract> for lwk_wollet::Contract {
27    fn from(contract: &Contract) -> Self {
28        contract.inner.clone()
29    }
30}
31
32impl std::fmt::Display for Contract {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        let json = serde_json::to_string(&self.inner).expect("contain simple types");
35        write!(f, "{}", &json)
36    }
37}
38
39#[uniffi::export]
40impl Contract {
41    /// Construct a Contract object
42    #[uniffi::constructor]
43    pub fn new(
44        domain: String,
45        issuer_pubkey: &str,
46        name: String,
47        precision: u8,
48        ticker: String,
49        version: u8,
50    ) -> Result<Arc<Self>, LwkError> {
51        let inner = lwk_wollet::Contract {
52            entity: lwk_wollet::Entity::Domain(domain),
53            issuer_pubkey: Vec::<u8>::from_hex(issuer_pubkey)
54                .map_err(|e| format!("invalid issuer pubkey: {e}"))?,
55            name,
56            precision,
57            ticker,
58            version,
59        };
60        inner.validate()?; // TODO validate should be the constructor
61        Ok(Arc::new(Self { inner }))
62    }
63}