Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions crates/chain/src/rusqlite_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ pub fn migrate_schema(
Ok(())
}

/// Serde!
pub struct SerdeImpl<T>(pub T);

impl<T: serde_crate::de::DeserializeOwned> FromSql for SerdeImpl<T> {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
serde_json::from_str(value.as_str()?)
.map(SerdeImpl)
.map_err(from_sql_error)
}
}

impl<T: serde_crate::Serialize> ToSql for SerdeImpl<T> {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
serde_json::to_string(&self.0)
.map(Into::into)
.map_err(to_sql_error)
}
}

impl FromSql for Impl<bitcoin::Txid> {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
bitcoin::Txid::from_str(value.as_str()?)
Expand Down
33 changes: 26 additions & 7 deletions crates/wallet/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ use bitcoin::{psbt, Weight};

use serde::{Deserialize, Serialize};

/// Trait that determines if a keychain variant is trusted.
pub trait WalletKeychain {
const DEFAULT_CHANGE_VARIANT: Self;

/// Returns whether the keychain variant is trusted.
fn is_trusted_variant(&self) -> bool;
}

impl WalletKeychain for KeychainKind {
const DEFAULT_CHANGE_VARIANT: Self = KeychainKind::Internal;

fn is_trusted_variant(&self) -> bool {
match self {
KeychainKind::External => false,
KeychainKind::Internal => true,
}
}
}

/// Types of keychains
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum KeychainKind {
Expand Down Expand Up @@ -50,13 +69,13 @@ impl AsRef<[u8]> for KeychainKind {
///
/// [`Wallet`]: crate::Wallet
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
pub struct LocalOutput {
pub struct LocalOutput<K> {
/// Reference to a transaction output
pub outpoint: OutPoint,
/// Transaction output
pub txout: TxOut,
/// Type of keychain
pub keychain: KeychainKind,
pub keychain: K,
/// Whether this UTXO is spent or not
pub is_spent: bool,
/// The derivation index for the script pubkey in the wallet
Expand All @@ -67,21 +86,21 @@ pub struct LocalOutput {

/// A [`Utxo`] with its `satisfaction_weight`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WeightedUtxo {
pub struct WeightedUtxo<K> {
/// The weight of the witness data and `scriptSig` expressed in [weight units]. This is used to
/// properly maintain the feerate when adding this input to a transaction during coin selection.
///
/// [weight units]: https://en.bitcoin.it/wiki/Weight_units
pub satisfaction_weight: Weight,
/// The UTXO
pub utxo: Utxo,
pub utxo: Utxo<K>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// An unspent transaction output (UTXO).
pub enum Utxo {
pub enum Utxo<K> {
/// A UTXO owned by the local wallet.
Local(LocalOutput),
Local(LocalOutput<K>),
/// A UTXO owned by another wallet.
Foreign {
/// The location of the output.
Expand All @@ -94,7 +113,7 @@ pub enum Utxo {
},
}

impl Utxo {
impl<K> Utxo<K> {
/// Get the location of the UTXO
pub fn outpoint(&self) -> OutPoint {
match &self {
Expand Down
Loading