rgb/
filters.rs

1// RGB API library for smart contracts on Bitcoin & Lightning network
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Written in 2019-2023 by
6//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
7//
8// Copyright (C) 2019-2023 LNP/BP Standards Association. All rights reserved.
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14//     http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21
22use rgbstd::contract::AssignmentsFilter;
23use rgbstd::{Outpoint, Txid};
24
25use crate::WalletProvider;
26
27#[derive(Copy, Clone)]
28pub enum Filter {
29    Outpoints,
30    Unspent,
31    Witness,
32}
33
34pub struct WalletFilter<'wallet, W: WalletProvider + ?Sized> {
35    wallet: &'wallet W,
36    filter: Filter,
37}
38
39impl<'wallet, W: WalletProvider + ?Sized> WalletFilter<'wallet, W> {
40    pub fn new(wallet: &'wallet W, filter: Filter) -> WalletFilter<'wallet, W> {
41        Self { wallet, filter }
42    }
43}
44
45impl<W: WalletProvider + ?Sized> Copy for WalletFilter<'_, W> {}
46impl<W: WalletProvider + ?Sized> Clone for WalletFilter<'_, W> {
47    fn clone(&self) -> Self { *self }
48}
49
50impl<W: WalletProvider + ?Sized> AssignmentsFilter for WalletFilter<'_, W> {
51    fn should_include(&self, outpoint: impl Into<Outpoint>, witness_id: Option<Txid>) -> bool {
52        match self.filter {
53            Filter::Outpoints => self.wallet.has_outpoint(outpoint.into()),
54            Filter::Unspent => self.wallet.is_unspent(outpoint.into()),
55            Filter::Witness => self.wallet.should_include_witness(witness_id),
56        }
57    }
58}