From 8519e91f6d4d176c220091d432069c7a18d44286 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sun, 30 Jul 2023 12:57:02 +0200 Subject: [PATCH] descriptor: add set of standard descriptors --- std/src/derive.rs | 7 +++++++ std/src/descriptors.rs | 30 ++++++++++++++++++++++++------ std/src/lib.rs | 4 ++-- std/src/wallet.rs | 6 ++++-- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/std/src/derive.rs b/std/src/derive.rs index 41e45ab..66c70d1 100644 --- a/std/src/derive.rs +++ b/std/src/derive.rs @@ -20,6 +20,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use bc::secp256k1::PublicKey; use bc::{InternalPk, ScriptPubkey}; use crate::{Address, AddressNetwork, ComprPubkey, Idx, NormalIndex}; @@ -88,3 +89,9 @@ pub trait DeriveSpk: Derive { } } impl> DeriveSpk for T {} + +pub trait DeriveSet { + type Base: Derive; + type Compr: DeriveCompr; + type XOnly: DeriveXOnly; +} diff --git a/std/src/descriptors.rs b/std/src/descriptors.rs index e12205e..0cee5d6 100644 --- a/std/src/descriptors.rs +++ b/std/src/descriptors.rs @@ -22,10 +22,17 @@ use bc::ScriptPubkey; -use crate::{Derive, DeriveXOnly, NormalIndex}; +use crate::{Derive, DeriveSet, DeriveSpk, DeriveXOnly, NormalIndex}; pub struct TrKey(K); +/* +pub struct TrScript { + internal_key: K, + tap_tree: TapTree>, +} +*/ + impl Derive for TrKey { type Derived = ScriptPubkey; @@ -39,9 +46,20 @@ impl Derive for TrKey { } } -/* -pub struct TrScript { - internal_key: K, - tap_tree: TapTree>, +pub enum DescriptorStd { + TrKey(TrKey), +} + +impl Derive for DescriptorStd { + type Derived = ScriptPubkey; + + fn derive( + &self, + change: impl Into, + index: impl Into, + ) -> Self::Derived { + match self { + DescriptorStd::TrKey(d) => d.derive(change, index), + } + } } -*/ diff --git a/std/src/lib.rs b/std/src/lib.rs index 388fd1e..5576ad7 100644 --- a/std/src/lib.rs +++ b/std/src/lib.rs @@ -44,8 +44,8 @@ pub use address::{ }; pub use bc::{secp256k1, *}; pub use chain::{AddrInfo, BlockInfo, MiningInfo, TxInInfo, TxInfo, TxOutInfo, TxStatus, UtxoInfo}; -pub use derive::{Derive, DeriveCompr, DeriveSpk, DeriveXOnly}; -pub use descriptors::TrKey; +pub use derive::{Derive, DeriveCompr, DeriveSet, DeriveSpk, DeriveXOnly}; +pub use descriptors::{DescriptorStd, TrKey}; pub use index::{ DerivationIndex, HardenedIndex, Idx, IndexError, IndexParseError, NormalIndex, HARDENED_INDEX_BOUNDARY, diff --git a/std/src/wallet.rs b/std/src/wallet.rs index 42168b0..219f244 100644 --- a/std/src/wallet.rs +++ b/std/src/wallet.rs @@ -25,7 +25,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::num::NonZeroU32; use std::ops::Deref; -use bc::{Outpoint, ScriptPubkey, Txid}; +use bc::{Chain, Outpoint, ScriptPubkey, Txid}; use crate::chain::BlockHeight; use crate::derive::DeriveSpk; @@ -37,6 +37,8 @@ where D: DeriveSpk { script_pubkey: D, keychains: BTreeSet, + #[getter(as_copy)] + chain: Chain, } impl Deref for WalletDescr { @@ -63,7 +65,7 @@ pub struct WalletCache { max_known: HashMap, } -pub struct Wallet { +pub struct Wallet { descr: WalletDescr, data: WalletData, cache: WalletCache,