use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use super::amount::CurrencyUnit;
use crate::error::FfiError;
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct KeySetInfo {
pub id: String,
pub unit: CurrencyUnit,
pub active: bool,
pub input_fee_ppk: u64,
}
impl From<cdk::nuts::KeySetInfo> for KeySetInfo {
fn from(keyset: cdk::nuts::KeySetInfo) -> Self {
Self {
id: keyset.id.to_string(),
unit: keyset.unit.into(),
active: keyset.active,
input_fee_ppk: keyset.input_fee_ppk,
}
}
}
impl TryFrom<KeySetInfo> for cdk::nuts::KeySetInfo {
type Error = FfiError;
fn try_from(keyset: KeySetInfo) -> Result<Self, Self::Error> {
use std::str::FromStr;
Ok(Self {
id: cdk::nuts::Id::from_str(&keyset.id)
.map_err(|e| FfiError::internal(format!("Invalid keyset ID: {}", e)))?,
unit: keyset.unit.into(),
active: keyset.active,
final_expiry: None,
input_fee_ppk: keyset.input_fee_ppk,
})
}
}
impl KeySetInfo {
pub fn to_json(&self) -> Result<String, FfiError> {
Ok(serde_json::to_string(self)?)
}
}
#[uniffi::export]
pub fn decode_key_set_info(json: String) -> Result<KeySetInfo, FfiError> {
Ok(serde_json::from_str(&json)?)
}
#[uniffi::export]
pub fn encode_key_set_info(info: KeySetInfo) -> Result<String, FfiError> {
Ok(serde_json::to_string(&info)?)
}
#[derive(Debug, Clone, Copy, uniffi::Enum)]
pub enum KeysetFilter {
Active,
All,
}
impl From<KeysetFilter> for cdk_common::wallet::KeysetFilter {
fn from(filter: KeysetFilter) -> Self {
match filter {
KeysetFilter::Active => cdk_common::wallet::KeysetFilter::Active,
KeysetFilter::All => cdk_common::wallet::KeysetFilter::All,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
#[serde(transparent)]
pub struct PublicKey {
pub hex: String,
}
impl From<cdk::nuts::PublicKey> for PublicKey {
fn from(key: cdk::nuts::PublicKey) -> Self {
Self {
hex: key.to_string(),
}
}
}
impl TryFrom<PublicKey> for cdk::nuts::PublicKey {
type Error = FfiError;
fn try_from(key: PublicKey) -> Result<Self, Self::Error> {
key.hex
.parse()
.map_err(|e| FfiError::internal(format!("Invalid public key: {}", e)))
}
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct Keys {
pub keys: HashMap<u64, String>,
}
impl From<cdk::nuts::Keys> for Keys {
fn from(keys: cdk::nuts::Keys) -> Self {
Self {
keys: keys
.keys()
.iter()
.map(|(amount, pubkey)| (u64::from(*amount), pubkey.to_string()))
.collect(),
}
}
}
impl TryFrom<Keys> for cdk::nuts::Keys {
type Error = FfiError;
fn try_from(keys: Keys) -> Result<Self, Self::Error> {
use std::collections::BTreeMap;
use std::str::FromStr;
let mut keys_map = BTreeMap::new();
for (amount_u64, pubkey_hex) in keys.keys {
let amount = cdk::Amount::from(amount_u64);
let pubkey = cdk::nuts::PublicKey::from_str(&pubkey_hex)
.map_err(|e| FfiError::internal(format!("Invalid public key: {}", e)))?;
keys_map.insert(amount, pubkey);
}
Ok(cdk::nuts::Keys::new(keys_map))
}
}
impl Keys {
pub fn to_json(&self) -> Result<String, FfiError> {
Ok(serde_json::to_string(self)?)
}
}
#[uniffi::export]
pub fn decode_keys(json: String) -> Result<Keys, FfiError> {
Ok(serde_json::from_str(&json)?)
}
#[uniffi::export]
pub fn encode_keys(keys: Keys) -> Result<String, FfiError> {
Ok(serde_json::to_string(&keys)?)
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct KeySet {
pub id: String,
pub unit: CurrencyUnit,
pub active: Option<bool>,
pub input_fee_ppk: u64,
pub keys: HashMap<u64, String>,
pub final_expiry: Option<u64>,
}
impl From<cdk::nuts::KeySet> for KeySet {
fn from(keyset: cdk::nuts::KeySet) -> Self {
Self {
id: keyset.id.to_string(),
unit: keyset.unit.into(),
active: keyset.active,
input_fee_ppk: keyset.input_fee_ppk,
keys: keyset
.keys
.keys()
.iter()
.map(|(amount, pubkey)| (u64::from(*amount), pubkey.to_string()))
.collect(),
final_expiry: keyset.final_expiry,
}
}
}
impl TryFrom<KeySet> for cdk::nuts::KeySet {
type Error = FfiError;
fn try_from(keyset: KeySet) -> Result<Self, Self::Error> {
use std::collections::BTreeMap;
use std::str::FromStr;
let id = cdk::nuts::Id::from_str(&keyset.id)
.map_err(|e| FfiError::internal(format!("Invalid keyset ID: {}", e)))?;
let unit: cdk::nuts::CurrencyUnit = keyset.unit.into();
let mut keys_map = BTreeMap::new();
for (amount_u64, pubkey_hex) in keyset.keys {
let amount = cdk::Amount::from(amount_u64);
let pubkey = cdk::nuts::PublicKey::from_str(&pubkey_hex)
.map_err(|e| FfiError::internal(format!("Invalid public key: {}", e)))?;
keys_map.insert(amount, pubkey);
}
let keys = cdk::nuts::Keys::new(keys_map);
Ok(cdk::nuts::KeySet {
id,
unit,
active: keyset.active,
input_fee_ppk: keyset.input_fee_ppk,
keys,
final_expiry: keyset.final_expiry,
})
}
}
impl KeySet {
pub fn to_json(&self) -> Result<String, FfiError> {
Ok(serde_json::to_string(self)?)
}
}
#[uniffi::export]
pub fn decode_key_set(json: String) -> Result<KeySet, FfiError> {
Ok(serde_json::from_str(&json)?)
}
#[uniffi::export]
pub fn encode_key_set(keyset: KeySet) -> Result<String, FfiError> {
Ok(serde_json::to_string(&keyset)?)
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
#[serde(transparent)]
pub struct Id {
pub hex: String,
}
impl From<cdk::nuts::Id> for Id {
fn from(id: cdk::nuts::Id) -> Self {
Self {
hex: id.to_string(),
}
}
}
impl TryFrom<Id> for cdk::nuts::Id {
type Error = FfiError;
fn try_from(id: Id) -> Result<Self, Self::Error> {
use std::str::FromStr;
Self::from_str(&id.hex).map_err(|e| FfiError::internal(format!("Invalid ID hex: {}", e)))
}
}