-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathwallet.rs
163 lines (151 loc) · 5.16 KB
/
wallet.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// RGB wallet library for smart contracts on Bitcoin & Lightning network
//
// SPDX-License-Identifier: Apache-2.0
//
// Written in 2019-2023 by
// Dr Maxim Orlovsky <[email protected]>
//
// Copyright (C) 2019-2023 LNP/BP Standards Association. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::marker::PhantomData;
#[cfg(feature = "fs")]
use std::path::PathBuf;
use bpstd::XpubDerivable;
#[cfg(feature = "fs")]
use bpwallet::fs::FsTextStore;
#[cfg(feature = "fs")]
use bpwallet::Wallet;
use bpwallet::{Layer2, NoLayer2};
#[cfg(not(target_arch = "wasm32"))]
use nonasync::persistence::PersistenceProvider;
use psrgbt::{Psbt, PsbtMeta};
use rgbstd::containers::Transfer;
use rgbstd::interface::{ContractOp, IfaceRef};
#[cfg(feature = "fs")]
use rgbstd::persistence::fs::FsBinStore;
use rgbstd::persistence::{
ContractIfaceError, IndexProvider, MemIndex, MemStash, MemState, StashProvider, StateProvider,
Stock, StockError,
};
use super::{
CompletionError, CompositionError, ContractId, DescriptorRgb, PayError, TransferParams,
WalletError, WalletProvider,
};
use crate::invoice::RgbInvoice;
#[derive(Getters)]
pub struct RgbWallet<
W: WalletProvider<K, L2>,
K = XpubDerivable,
S: StashProvider = MemStash,
H: StateProvider = MemState,
P: IndexProvider = MemIndex,
L2: Layer2 = NoLayer2,
> where W::Descr: DescriptorRgb<K>
{
stock: Stock<S, H, P>,
wallet: W,
#[getter(skip)]
_key_phantom: PhantomData<K>,
#[getter(skip)]
_layer2_phantom: PhantomData<L2>,
}
#[cfg(feature = "fs")]
impl<K, D: DescriptorRgb<K>, S: StashProvider, H: StateProvider, P: IndexProvider, L2: Layer2>
RgbWallet<Wallet<K, D, L2>, K, S, H, P, L2>
{
#[allow(clippy::result_large_err)]
pub fn load(
stock_path: PathBuf,
wallet_path: PathBuf,
autosave: bool,
) -> Result<Self, WalletError>
where
D: serde::Serialize + for<'de> serde::Deserialize<'de>,
L2::Descr: serde::Serialize + for<'de> serde::Deserialize<'de>,
L2::Data: serde::Serialize + for<'de> serde::Deserialize<'de>,
L2::Cache: serde::Serialize + for<'de> serde::Deserialize<'de>,
FsBinStore: PersistenceProvider<S>,
FsBinStore: PersistenceProvider<H>,
FsBinStore: PersistenceProvider<P>,
FsTextStore: PersistenceProvider<L2>,
{
use nonasync::persistence::PersistenceError;
let provider = FsBinStore::new(stock_path)
.map_err(|e| WalletError::StockPersist(PersistenceError::with(e)))?;
let stock = Stock::load(provider, autosave).map_err(WalletError::StockPersist)?;
let provider = FsTextStore::new(wallet_path)
.map_err(|e| WalletError::WalletPersist(PersistenceError::with(e)))?;
let wallet = Wallet::load(provider, autosave).map_err(WalletError::WalletPersist)?;
Ok(Self {
wallet,
stock,
_key_phantom: PhantomData,
_layer2_phantom: PhantomData,
})
}
}
impl<
K,
W: WalletProvider<K, L2>,
S: StashProvider,
H: StateProvider,
P: IndexProvider,
L2: Layer2,
> RgbWallet<W, K, S, H, P, L2>
where W::Descr: DescriptorRgb<K>
{
pub fn new(stock: Stock<S, H, P>, wallet: W) -> Self {
Self {
stock,
wallet,
_key_phantom: PhantomData,
_layer2_phantom: PhantomData,
}
}
pub fn stock_mut(&mut self) -> &mut Stock<S, H, P> { &mut self.stock }
pub fn wallet_mut(&mut self) -> &mut W { &mut self.wallet }
pub fn history(
&self,
contract_id: ContractId,
iface: impl Into<IfaceRef>,
) -> Result<Vec<ContractOp>, StockError<S, H, P, ContractIfaceError>> {
let contract = self.stock.contract_iface(contract_id, iface.into())?;
let wallet = &self.wallet;
Ok(contract.history(wallet.filter_outpoints(), wallet.filter_witnesses()))
}
#[allow(clippy::result_large_err)]
pub fn pay(
&mut self,
invoice: &RgbInvoice,
params: TransferParams,
) -> Result<(Psbt, PsbtMeta, Transfer), PayError> {
self.wallet.pay(&mut self.stock, invoice, params)
}
#[allow(clippy::result_large_err)]
pub fn construct_psbt(
&mut self,
invoice: &RgbInvoice,
params: TransferParams,
) -> Result<(Psbt, PsbtMeta), CompositionError> {
self.wallet.construct_psbt_rgb(&self.stock, invoice, params)
}
#[allow(clippy::result_large_err)]
pub fn transfer(
&mut self,
invoice: &RgbInvoice,
psbt: &mut Psbt,
) -> Result<Transfer, CompletionError> {
self.wallet.transfer(&mut self.stock, invoice, psbt)
}
}