Skip to content

Commit 890e1c4

Browse files
committed
POC
1 parent 6123778 commit 890e1c4

File tree

10 files changed

+813
-613
lines changed

10 files changed

+813
-613
lines changed

crates/chain/src/rusqlite_impl.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ pub fn migrate_schema(
7070
Ok(())
7171
}
7272

73+
/// Serde!
74+
pub struct SerdeImpl<T>(pub T);
75+
76+
impl<T: serde_crate::de::DeserializeOwned> FromSql for SerdeImpl<T> {
77+
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
78+
serde_json::from_str(value.as_str()?)
79+
.map(SerdeImpl)
80+
.map_err(from_sql_error)
81+
}
82+
}
83+
84+
impl<T: serde_crate::Serialize> ToSql for SerdeImpl<T> {
85+
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
86+
serde_json::to_string(&self.0)
87+
.map(Into::into)
88+
.map_err(to_sql_error)
89+
}
90+
}
91+
7392
impl FromSql for Impl<bitcoin::Txid> {
7493
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
7594
bitcoin::Txid::from_str(value.as_str()?)

crates/wallet/src/types.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ use bitcoin::{psbt, Weight};
1818

1919
use serde::{Deserialize, Serialize};
2020

21+
/// Trait that determines if a keychain variant is trusted.
22+
pub trait WalletKeychain {
23+
const DEFAULT_CHANGE_VARIANT: Self;
24+
25+
/// Returns whether the keychain variant is trusted.
26+
fn is_trusted_variant(&self) -> bool;
27+
}
28+
29+
impl WalletKeychain for KeychainKind {
30+
const DEFAULT_CHANGE_VARIANT: Self = KeychainKind::Internal;
31+
32+
fn is_trusted_variant(&self) -> bool {
33+
match self {
34+
KeychainKind::External => false,
35+
KeychainKind::Internal => true,
36+
}
37+
}
38+
}
39+
2140
/// Types of keychains
2241
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
2342
pub enum KeychainKind {
@@ -50,13 +69,13 @@ impl AsRef<[u8]> for KeychainKind {
5069
///
5170
/// [`Wallet`]: crate::Wallet
5271
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
53-
pub struct LocalOutput {
72+
pub struct LocalOutput<K> {
5473
/// Reference to a transaction output
5574
pub outpoint: OutPoint,
5675
/// Transaction output
5776
pub txout: TxOut,
5877
/// Type of keychain
59-
pub keychain: KeychainKind,
78+
pub keychain: K,
6079
/// Whether this UTXO is spent or not
6180
pub is_spent: bool,
6281
/// The derivation index for the script pubkey in the wallet
@@ -67,21 +86,21 @@ pub struct LocalOutput {
6786

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

8099
#[derive(Debug, Clone, PartialEq, Eq)]
81100
/// An unspent transaction output (UTXO).
82-
pub enum Utxo {
101+
pub enum Utxo<K> {
83102
/// A UTXO owned by the local wallet.
84-
Local(LocalOutput),
103+
Local(LocalOutput<K>),
85104
/// A UTXO owned by another wallet.
86105
Foreign {
87106
/// The location of the output.
@@ -94,7 +113,7 @@ pub enum Utxo {
94113
},
95114
}
96115

97-
impl Utxo {
116+
impl<K> Utxo<K> {
98117
/// Get the location of the UTXO
99118
pub fn outpoint(&self) -> OutPoint {
100119
match &self {

0 commit comments

Comments
 (0)