Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jul 6, 2024
1 parent 08ec7a6 commit a77e168
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 822 deletions.
648 changes: 11 additions & 637 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ features = ["all"]

[patch.crates-io]
bp-wallet = { git = "https://github.com/BP-WG/bp-wallet", branch = "runtime" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "master" }
rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "runtime" }
6 changes: 3 additions & 3 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use bpwallet::Wallet;
use rgb::persistence::fs::{LoadFs, StoreFs};
use rgb::persistence::Stock;
use rgb::resolvers::AnyResolver;
use rgb::{RgbDescr, StoredStock, StoredWallet, TapretKey, WalletError};
use rgb::{RgbDescr, StoredWallet, TapretKey, WalletError};
use strict_types::encoding::{DecodeError, DeserializeError};

use crate::Command;
Expand Down Expand Up @@ -101,7 +101,7 @@ impl RgbArgs {
})
}

pub fn rgb_stock(&self) -> Result<StoredStock, WalletError> {
pub fn rgb_stock(&self) -> Result<Stock, WalletError> {
let stock_path = self.general.base_dir();
let stock = self.load_stock(&stock_path)?;
Ok(StoredStock::attach(stock_path, stock))
Expand All @@ -124,7 +124,7 @@ impl RgbArgs {
let stock_path = self.general.base_dir();
let wallet = self.inner.bp_wallet::<RgbDescr>(config)?;
let wallet_path = wallet.path().clone();
let wallet = StoredWallet::attach(stock_path, wallet_path, stock, wallet.detach());
let wallet = StoredWallet::new(stock, wallet);

Ok(wallet)
}
Expand Down
1 change: 0 additions & 1 deletion cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use rgb::containers::{
};
use rgb::interface::{AmountChange, IfaceId};
use rgb::invoice::{Beneficiary, Pay2Vout, RgbInvoice, RgbInvoiceBuilder, XChainNet};
use rgb::persistence::StashReadProvider;
use rgb::schema::SchemaId;
use rgb::validation::Validity;
use rgb::vm::RgbIsa;
Expand Down
3 changes: 1 addition & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ pub enum WalletError {

#[cfg(feature = "fs")]
#[from]
#[from(bpwallet::LoadError)]
Bp(bpwallet::WalletError),
Bp(bpwallet::fs::LoadError),

/// resolver error: {0}
#[display(doc_comments)]
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ mod indexers;
mod wallet;
pub mod pay;
mod errors;
#[cfg(feature = "fs")]
mod store;

pub use descriptor::{DescriptorRgb, RgbDescr, RgbKeychain, TapTweakAlreadyAssigned, TapretKey};
Expand All @@ -44,6 +43,5 @@ pub mod resolvers {
pub use super::indexers::*;
pub use super::indexers::{AnyResolver, RgbResolver};
}
#[cfg(feature = "fs")]
pub use store::{StoredStock, StoredWallet};
pub use store::StoredWallet;
pub use wallet::{WalletStock, WalletWrapper};
16 changes: 9 additions & 7 deletions src/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@

use std::collections::{BTreeMap, BTreeSet};
use std::marker::PhantomData;
use std::ops::DerefMut;

use bp::dbc::tapret::TapretProof;
use bp::seals::txout::ExplicitSeal;
use bp::{Outpoint, Sats, ScriptPubkey, Vout};
use bpstd::{psbt, Address};
use bpwallet::Wallet;
use bpwallet::{Save, Wallet, WalletDescr};
use psrgbt::{
Beneficiary as BpBeneficiary, Psbt, PsbtConstructor, PsbtMeta, RgbPsbt, TapretKeyError,
TxParams,
Expand Down Expand Up @@ -99,7 +98,7 @@ where Self::Descr: DescriptorRgb<K>
type Filter<'a>: Copy + WitnessFilter + OutpointFilter
where Self: 'a;
fn filter(&self) -> Self::Filter<'_>;
fn descriptor_mut(&mut self) -> &mut Self::Descr;
fn descriptor_mut<R>(&mut self, f: impl FnOnce(&mut WalletDescr<K, Self::Descr>) -> R) -> R;
fn outpoints(&self) -> impl Iterator<Item = Outpoint>;
fn txids(&self) -> impl Iterator<Item = Txid>;

Expand Down Expand Up @@ -291,8 +290,7 @@ where Self::Descr: DescriptorRgb<K>
.terminal_derivation()
.ok_or(CompletionError::InconclusiveDerivation)?;
let tapret_commitment = output.tapret_commitment()?;
self.descriptor_mut()
.add_tapret_tweak(terminal, tapret_commitment)?;
self.descriptor_mut(|descr| descr.add_tapret_tweak(terminal, tapret_commitment))?;
}

let witness_txid = psbt.txid();
Expand Down Expand Up @@ -322,10 +320,14 @@ where Self::Descr: DescriptorRgb<K>
}
}

impl<K, D: DescriptorRgb<K>> WalletProvider<K> for Wallet<K, D> {
impl<K, D: DescriptorRgb<K>> WalletProvider<K> for Wallet<K, D>
where Wallet<K, D>: Save
{
type Filter<'a> = WalletWrapper<'a, K, D> where Self: 'a;
fn filter(&self) -> Self::Filter<'_> { WalletWrapper(self) }
fn descriptor_mut(&mut self) -> &mut Self::Descr { self.deref_mut() }
fn descriptor_mut<R>(&mut self, f: impl FnOnce(&mut WalletDescr<K, D>) -> R) -> R {
self.descriptor_mut(f)
}
fn outpoints(&self) -> impl Iterator<Item = Outpoint> { self.coins().map(|coin| coin.outpoint) }
fn txids(&self) -> impl Iterator<Item = Txid> { self.transactions().keys().copied() }
}
200 changes: 37 additions & 163 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
// limitations under the License.

use std::collections::HashMap;
use std::error::Error;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
#[cfg(feature = "fs")]
use std::path::Path;

use bpstd::XpubDerivable;
use bpwallet::{StoreError, Wallet, WalletDescr};
use bpwallet::fs::Warning;
use bpwallet::{Wallet, WalletDescr};
use psrgbt::{Psbt, PsbtMeta};
use rgbstd::containers::Transfer;
use rgbstd::interface::{AmountChange, IfaceOp, IfaceRef};
use rgbstd::persistence::fs::StoreFs;
use rgbstd::persistence::fs::LoadFs;
use rgbstd::persistence::{
IndexProvider, MemIndex, MemStash, MemState, StashProvider, StateProvider, Stock,
};
Expand All @@ -41,152 +41,63 @@ use super::{
};
use crate::invoice::RgbInvoice;

pub trait Store {
type Err: Error;
fn store(&self, path: impl AsRef<Path>) -> Result<(), Self::Err>;
}

impl<K, D: DescriptorRgb<K>> Store for Wallet<K, D>
where
for<'de> WalletDescr<K, D>: serde::Serialize + serde::Deserialize<'de>,
for<'de> D: serde::Serialize + serde::Deserialize<'de>,
{
type Err = StoreError;
fn store(&self, path: impl AsRef<Path>) -> Result<(), Self::Err> { self.store(path.as_ref()) }
}

#[derive(Getters)]
pub struct StoredStock<
S: StashProvider = MemStash,
H: StateProvider = MemState,
P: IndexProvider = MemIndex,
> where
S: StoreFs,
H: StoreFs,
P: StoreFs,
{
stock_path: PathBuf,
stock: Stock<S, H, P>,
#[getter(prefix = "is_")]
dirty: bool,
}

impl<S: StashProvider, H: StateProvider, P: IndexProvider> Deref for StoredStock<S, H, P>
where
S: StoreFs,
H: StoreFs,
P: StoreFs,
{
type Target = Stock<S, H, P>;

fn deref(&self) -> &Self::Target { &self.stock }
}

impl<S: StashProvider, H: StateProvider, P: IndexProvider> DerefMut for StoredStock<S, H, P>
where
S: StoreFs,
H: StoreFs,
P: StoreFs,
{
fn deref_mut(&mut self) -> &mut Self::Target {
self.dirty = true;
&mut self.stock
}
}

impl<S: StashProvider, H: StateProvider, P: IndexProvider> StoredStock<S, H, P>
where
S: StoreFs,
H: StoreFs,
P: StoreFs,
{
pub fn attach(path: PathBuf, stock: Stock<S, H, P>) -> Self {
Self {
stock_path: path,
stock,
dirty: false,
}
}

pub fn store(&self) {
if self.dirty {
self.stock
.store(&self.stock_path)
.expect("error saving data");
}
}
}

impl<S: StashProvider, H: StateProvider, P: IndexProvider> Drop for StoredStock<S, H, P>
where
S: StoreFs,
H: StoreFs,
P: StoreFs,
{
fn drop(&mut self) { self.store() }
}

#[derive(Getters)]
pub struct StoredWallet<
W: WalletProvider<K>,
K = XpubDerivable,
S: StashProvider = MemStash,
H: StateProvider = MemState,
P: IndexProvider = MemIndex,
> where
W::Descr: DescriptorRgb<K>,
W: Store,
S: StoreFs,
H: StoreFs,
P: StoreFs,
> where W::Descr: DescriptorRgb<K>
{
stock_path: PathBuf,
wallet_path: Option<PathBuf>,
stock: Stock<S, H, P>,
wallet: W,
#[getter(prefix = "is_")]
stock_dirty: bool,
#[getter(prefix = "is_")]
wallet_dirty: bool,
warnings: Vec<Warning>,
#[getter(skip)]
_phantom: PhantomData<K>,
}

#[cfg(feature = "fs")]
impl<K, D: DescriptorRgb<K>, S: StashProvider, H: StateProvider, P: IndexProvider>
StoredWallet<Wallet<K, D>, K, S, H, P>
where
S: LoadFs,
H: LoadFs,
P: LoadFs,
for<'de> WalletDescr<K, D>: serde::Serialize + serde::Deserialize<'de>,
for<'de> D: serde::Serialize + serde::Deserialize<'de>,
{
pub fn load(
stock_path: impl AsRef<Path>,
wallet_path: impl AsRef<Path>,
) -> Result<Self, WalletError> {
let stock = Stock::load(stock_path)?;
let (wallet, warnings) = Wallet::load(wallet_path.as_ref(), true)?;
Ok(Self {
wallet,
stock,
warnings,
_phantom: PhantomData,
})
}
}

impl<K, W: WalletProvider<K>, S: StashProvider, H: StateProvider, P: IndexProvider>
StoredWallet<W, K, S, H, P>
where
W::Descr: DescriptorRgb<K>,
W: Store,
S: StoreFs,
H: StoreFs,
P: StoreFs,
where W::Descr: DescriptorRgb<K>
{
pub fn attach(
stock_path: PathBuf,
wallet_path: Option<PathBuf>,
stock: Stock<S, H, P>,
wallet: W,
) -> Self {
pub fn new(stock: Stock<S, H, P>, wallet: W) -> Self {
Self {
stock_path,
wallet_path,
stock,
wallet,
stock_dirty: false,
wallet_dirty: false,
warnings: none!(),
_phantom: PhantomData,
}
}

pub fn stock_mut(&mut self) -> &mut Stock<S, H, P> {
self.stock_dirty = true;
&mut self.stock
}
pub fn stock_mut(&mut self) -> &mut Stock<S, H, P> { &mut self.stock }

pub fn wallet_mut(&mut self) -> &mut W {
self.wallet_dirty = true;
&mut self.wallet
}
pub fn wallet_mut(&mut self) -> &mut W { &mut self.wallet }

#[allow(clippy::result_large_err)]
pub fn fungible_history(
Expand All @@ -204,8 +115,6 @@ where
invoice: &RgbInvoice,
params: TransferParams,
) -> Result<(Psbt, PsbtMeta, Transfer), PayError> {
self.stock_dirty = true;
self.wallet_dirty = true;
self.wallet.pay(&mut self.stock, invoice, params)
}

Expand All @@ -215,7 +124,6 @@ where
invoice: &RgbInvoice,
params: TransferParams,
) -> Result<(Psbt, PsbtMeta), CompositionError> {
self.wallet_dirty = true;
self.wallet.construct_psbt_rgb(&self.stock, invoice, params)
}

Expand All @@ -225,40 +133,6 @@ where
invoice: &RgbInvoice,
psbt: &mut Psbt,
) -> Result<Transfer, CompletionError> {
self.stock_dirty = true;
self.wallet_dirty = true;
self.wallet.transfer(&mut self.stock, invoice, psbt)
}

pub fn store(&self) {
let r1 = if self.stock_dirty {
self.stock
.store(&self.stock_path)
.map_err(|e| e.to_string())
} else {
Ok(())
};
let r2 = if self.wallet_dirty {
if let Some(path) = self.wallet_path.as_ref() {
self.wallet.store(path).map_err(|e| e.to_string())
} else {
Ok(())
}
} else {
Ok(())
};
r1.and(r2).expect("error saving data");
}
}

impl<K, W: WalletProvider<K>, S: StashProvider, H: StateProvider, P: IndexProvider> Drop
for StoredWallet<W, K, S, H, P>
where
W::Descr: DescriptorRgb<K>,
W: Store,
S: StoreFs,
H: StoreFs,
P: StoreFs,
{
fn drop(&mut self) { self.store() }
}
Loading

0 comments on commit a77e168

Please sign in to comment.