use crypto::keys::bip44::Bip44;
use serde::{Deserialize, Serialize};
use crate::{
types::{
block::{
address::Address,
output::{dto::OutputDto, Output, OutputId, OutputMetadata},
},
TryFromDto, ValidationParams,
},
utils::serde::bip44::option_bip44,
};
#[cfg(feature = "stronghold")]
#[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StrongholdDto {
pub password: Option<crate::client::Password>,
pub timeout: Option<u64>,
pub snapshot_path: String,
}
#[cfg(feature = "stronghold")]
impl core::fmt::Debug for StrongholdDto {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("StrongholdDto")
.field("timeout", &self.timeout)
.field("snapshot_path", &self.snapshot_path)
.finish()
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AccountAddress {
pub address: Address,
pub key_index: u32,
pub internal: bool,
}
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct GenerateAddressOptions {
pub internal: bool,
pub ledger_nano_prompt: bool,
}
impl GenerateAddressOptions {
pub const fn internal() -> Self {
Self {
internal: true,
ledger_nano_prompt: false,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct LedgerApp {
pub(crate) name: String,
pub(crate) version: String,
}
impl LedgerApp {
pub fn name(&self) -> &String {
&self.name
}
pub fn version(&self) -> &String {
&self.version
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum LedgerDeviceType {
#[serde(alias = "ledgerNanoS")]
LedgerNanoS,
#[serde(alias = "ledgerNanoX")]
LedgerNanoX,
#[serde(alias = "ledgerNanoSPlus")]
LedgerNanoSPlus,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LedgerNanoStatus {
pub(crate) connected: bool,
pub(crate) locked: Option<bool>,
pub(crate) blind_signing_enabled: bool,
pub(crate) app: Option<LedgerApp>,
pub(crate) device: Option<LedgerDeviceType>,
pub(crate) buffer_size: Option<usize>,
}
impl LedgerNanoStatus {
pub fn connected(&self) -> bool {
self.connected
}
pub fn locked(&self) -> Option<bool> {
self.locked
}
pub fn blind_signing_enabled(&self) -> bool {
self.blind_signing_enabled
}
pub fn app(&self) -> Option<&LedgerApp> {
self.app.as_ref()
}
pub fn device(&self) -> Option<LedgerDeviceType> {
self.device
}
pub fn buffer_size(&self) -> Option<usize> {
self.buffer_size
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct InputSigningData {
pub output: Output,
pub output_metadata: OutputMetadata,
pub chain: Option<Bip44>,
}
impl InputSigningData {
pub fn output_id(&self) -> &OutputId {
self.output_metadata.output_id()
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InputSigningDataDto {
pub output: OutputDto,
pub output_metadata: OutputMetadata,
#[serde(with = "option_bip44", default)]
pub chain: Option<Bip44>,
}
impl TryFromDto for InputSigningData {
type Dto = InputSigningDataDto;
type Error = crate::client::Error;
fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
Ok(Self {
output: Output::try_from_dto_with_params_inner(dto.output, params)?,
output_metadata: dto.output_metadata,
chain: dto.chain,
})
}
}
impl From<&InputSigningData> for InputSigningDataDto {
fn from(input: &InputSigningData) -> Self {
Self {
output: OutputDto::from(&input.output),
output_metadata: input.output_metadata,
chain: input.chain,
}
}
}