Skip to content

Commit

Permalink
use Keychains
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Nov 19, 2023
1 parent cd9aeb5 commit 623fdd7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,15 @@ impl Exec for RgbArgs {

let outpoint = runtime
.wallet()
.coinselect(Sats::ZERO, |utxo| [9, 10].contains(&utxo.terminal.keychain))
.coinselect(Sats::ZERO, |utxo| {
RgbKeychain::contains_rgb(utxo.terminal.keychain)
})
.next();
let beneficiary = match (address_based, outpoint) {
(true, _) | (false, None) => {
let addr = runtime
.wallet()
.addresses(RgbKeychain::Rgb as u8)
.addresses(RgbKeychain::Rgb)
.next()
.expect("no addresses left")
.addr;
Expand Down
44 changes: 34 additions & 10 deletions src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::BTreeMap;
use std::ops::Range;
use std::collections::{BTreeMap, BTreeSet};
use std::str::FromStr;
use std::{iter, vec};

use amplify::Wrapper;
use bp::dbc::tapret::TapretCommitment;
use bp::seals::txout::CloseMethod;
use bpstd::{
CompressedPk, Derive, DeriveCompr, DeriveSet, DeriveXOnly, DerivedScript, Idx, IndexError,
IndexParseError, KeyOrigin, NormalIndex, TapDerivation, Terminal, XOnlyPk, XpubDerivable,
XpubSpec,
CompressedPk, Derive, DeriveCompr, DeriveSet, DeriveXOnly, DerivedScript, Idx, IdxBase,
IndexError, IndexParseError, KeyOrigin, Keychain, NormalIndex, TapDerivation, Terminal,
XOnlyPk, XpubDerivable, XpubSpec,
};
use descriptors::{Descriptor, StdDescr, TrKey};
use indexmap::IndexMap;
Expand Down Expand Up @@ -57,6 +57,12 @@ pub enum RgbKeychain {
}

impl RgbKeychain {
pub const RGB_ALL: [RgbKeychain; 2] = [RgbKeychain::Rgb, RgbKeychain::Tapret];

pub fn contains_rgb(keychain: impl Into<Keychain>) -> bool {
let k = keychain.into().into_inner();
k == Self::Rgb as u8 || k == Self::Tapret as u8
}
pub fn is_seal(self) -> bool { self == Self::Rgb || self == Self::Tapret }
}

Expand All @@ -78,6 +84,10 @@ impl FromStr for RgbKeychain {
}
}

impl From<RgbKeychain> for Keychain {
fn from(keychain: RgbKeychain) -> Self { Keychain::from(keychain as u8) }
}

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
#[derive(Serialize, Deserialize)]
#[serde(crate = "serde_crate", rename_all = "camelCase")]
Expand All @@ -96,11 +106,19 @@ impl<K: DeriveXOnly> TapretKey<K> {
}

impl<K: DeriveXOnly> Derive<DerivedScript> for TapretKey<K> {
fn keychains(&self) -> Range<u8> {
0..2 /* FIXME */
#[inline]
fn default_keychain(&self) -> Keychain { RgbKeychain::Rgb.into() }

fn keychains(&self) -> BTreeSet<Keychain> {
bset![
RgbKeychain::External.into(),
RgbKeychain::Internal.into(),
RgbKeychain::Rgb.into(),
RgbKeychain::Tapret.into(),
]
}

fn derive(&self, change: u8, index: impl Into<NormalIndex>) -> DerivedScript {
fn derive(&self, change: impl Into<Keychain>, index: impl Into<NormalIndex>) -> DerivedScript {
// TODO: Apply tweaks
let internal_key = self.internal_key.derive(change, index);
DerivedScript::TaprootKeyOnly(internal_key.into())
Expand Down Expand Up @@ -172,13 +190,19 @@ pub enum RgbDescr<S: DeriveSet = XpubDerivable> {
}

impl<S: DeriveSet> Derive<DerivedScript> for RgbDescr<S> {
fn keychains(&self) -> Range<u8> {
fn default_keychain(&self) -> Keychain {
match self {
RgbDescr::TapretKey(d) => d.default_keychain(),
}
}

fn keychains(&self) -> BTreeSet<Keychain> {
match self {
RgbDescr::TapretKey(d) => d.keychains(),
}
}

fn derive(&self, change: u8, index: impl Into<NormalIndex>) -> DerivedScript {
fn derive(&self, change: impl Into<Keychain>, index: impl Into<NormalIndex>) -> DerivedScript {
match self {
RgbDescr::TapretKey(d) => d.derive(change, index),
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub struct Runtime<D: DescriptorRgb<K> = RgbDescr, K = XpubDerivable> {
stock_path: PathBuf,
stock: Stock,
#[getter(as_mut)]
wallet: Wallet<K, D /* Add stock via layer 2 */>,
wallet: Wallet<K, D /* TODO: Add layer 2 */>,
#[getter(as_copy)]
network: Network,
}
Expand Down

0 comments on commit 623fdd7

Please sign in to comment.