Skip to content

Commit

Permalink
chore: remove bdknetwork type
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Jan 24, 2024
1 parent d3ba569 commit f0548f4
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 61 deletions.
1 change: 1 addition & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ interface Script {
sequence<u8> to_bytes();
};

[NonExhaustive]
enum Network {
"Bitcoin",
"Testnet",
Expand Down
39 changes: 4 additions & 35 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use bdk::bitcoin::address::{NetworkChecked, NetworkUnchecked};
use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf;
use bdk::bitcoin::blockdata::transaction::TxOut as BdkTxOut;
use bdk::bitcoin::consensus::Decodable;
use bdk::bitcoin::network::constants::Network as BdkNetwork;
use bdk::bitcoin::psbt::PartiallySignedTransaction as BdkPartiallySignedTransaction;
use bdk::bitcoin::Address as BdkAddress;
use bdk::bitcoin::Network;
use bdk::bitcoin::OutPoint as BdkOutPoint;
use bdk::bitcoin::Transaction as BdkTransaction;
use bdk::bitcoin::Txid;
Expand Down Expand Up @@ -34,37 +34,6 @@ impl From<BdkScriptBuf> for Script {
}
}

#[derive(PartialEq, Debug)]
pub enum Network {
Bitcoin,
Testnet,
Signet,
Regtest,
}

impl From<Network> for BdkNetwork {
fn from(network: Network) -> Self {
match network {
Network::Bitcoin => BdkNetwork::Bitcoin,
Network::Testnet => BdkNetwork::Testnet,
Network::Signet => BdkNetwork::Signet,
Network::Regtest => BdkNetwork::Regtest,
}
}
}

impl From<BdkNetwork> for Network {
fn from(network: BdkNetwork) -> Self {
match network {
BdkNetwork::Bitcoin => Network::Bitcoin,
BdkNetwork::Testnet => Network::Testnet,
BdkNetwork::Signet => Network::Signet,
BdkNetwork::Regtest => Network::Regtest,
_ => panic!("Network {} not supported", network),
}
}
}

#[derive(Debug, PartialEq, Eq)]
pub struct Address {
inner: BdkAddress<NetworkChecked>,
Expand All @@ -77,7 +46,7 @@ impl Address {
.map_err(|_| Alpha3Error::Generic)?;

let network_checked_address = parsed_address
.require_network(network.into())
.require_network(network)
.map_err(|_| Alpha3Error::Generic)?;

Ok(Address {
Expand Down Expand Up @@ -108,7 +77,7 @@ impl Address {
// }

pub fn network(&self) -> Network {
self.inner.network.into()
self.inner.network
}

pub fn script_pubkey(&self) -> Arc<Script> {
Expand All @@ -126,7 +95,7 @@ impl Address {
pub fn is_valid_for_network(&self, network: Network) -> bool {
let address_str = self.inner.to_string();
if let Ok(unchecked_address) = address_str.parse::<BdkAddress<NetworkUnchecked>>() {
unchecked_address.is_valid_for_network(network.into())
unchecked_address.is_valid_for_network(network)
} else {
false
}
Expand Down
33 changes: 14 additions & 19 deletions bdk-ffi/src/descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::error::Alpha3Error;
use crate::keys::DescriptorPublicKey;
use crate::keys::DescriptorSecretKey;
use crate::Network;

use bdk::bitcoin::bip32::Fingerprint;
use bdk::bitcoin::key::Secp256k1;
use bdk::bitcoin::Network;
use bdk::descriptor::{ExtendedDescriptor, IntoWalletDescriptor};
use bdk::keys::DescriptorPublicKey as BdkDescriptorPublicKey;
use bdk::keys::{DescriptorSecretKey as BdkDescriptorSecretKey, KeyMap};
Expand All @@ -25,8 +25,7 @@ pub struct Descriptor {
impl Descriptor {
pub(crate) fn new(descriptor: String, network: Network) -> Result<Self, Alpha3Error> {
let secp = Secp256k1::new();
let (extended_descriptor, key_map) =
descriptor.into_wallet_descriptor(&secp, network.into())?;
let (extended_descriptor, key_map) = descriptor.into_wallet_descriptor(&secp, network)?;
Ok(Self {
extended_descriptor,
key_map,
Expand All @@ -46,9 +45,8 @@ impl Descriptor {
}
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) = Bip44(derivable_key, keychain_kind)
.build(network.into())
.unwrap();
let (extended_descriptor, key_map, _) =
Bip44(derivable_key, keychain_kind).build(network).unwrap();
Self {
extended_descriptor,
key_map,
Expand Down Expand Up @@ -77,7 +75,7 @@ impl Descriptor {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) =
Bip44Public(derivable_key, fingerprint, keychain_kind)
.build(network.into())
.build(network)
.unwrap();

Self {
Expand All @@ -104,9 +102,8 @@ impl Descriptor {
}
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) = Bip49(derivable_key, keychain_kind)
.build(network.into())
.unwrap();
let (extended_descriptor, key_map, _) =
Bip49(derivable_key, keychain_kind).build(network).unwrap();
Self {
extended_descriptor,
key_map,
Expand Down Expand Up @@ -135,7 +132,7 @@ impl Descriptor {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) =
Bip49Public(derivable_key, fingerprint, keychain_kind)
.build(network.into())
.build(network)
.unwrap();

Self {
Expand All @@ -162,9 +159,8 @@ impl Descriptor {
}
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) = Bip84(derivable_key, keychain_kind)
.build(network.into())
.unwrap();
let (extended_descriptor, key_map, _) =
Bip84(derivable_key, keychain_kind).build(network).unwrap();
Self {
extended_descriptor,
key_map,
Expand Down Expand Up @@ -193,7 +189,7 @@ impl Descriptor {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) =
Bip84Public(derivable_key, fingerprint, keychain_kind)
.build(network.into())
.build(network)
.unwrap();

Self {
Expand All @@ -220,9 +216,8 @@ impl Descriptor {
}
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) = Bip86(derivable_key, keychain_kind)
.build(network.into())
.unwrap();
let (extended_descriptor, key_map, _) =
Bip86(derivable_key, keychain_kind).build(network).unwrap();
Self {
extended_descriptor,
key_map,
Expand Down Expand Up @@ -251,7 +246,7 @@ impl Descriptor {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) =
Bip86Public(derivable_key, fingerprint, keychain_kind)
.build(network.into())
.build(network)
.unwrap();

Self {
Expand Down
4 changes: 2 additions & 2 deletions bdk-ffi/src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::error::Alpha3Error;
use crate::Network;

use bdk::bitcoin::bip32::DerivationPath as BdkDerivationPath;
use bdk::bitcoin::key::Secp256k1;
use bdk::bitcoin::secp256k1::rand;
use bdk::bitcoin::secp256k1::rand::Rng;
use bdk::bitcoin::Network;
use bdk::keys::bip39::WordCount;
use bdk::keys::bip39::{Language, Mnemonic as BdkMnemonic};
use bdk::keys::{
Expand Down Expand Up @@ -77,7 +77,7 @@ impl DescriptorSecretKey {
let xkey: ExtendedKey = (mnemonic, password).into_extended_key().unwrap();
let descriptor_secret_key = BdkDescriptorSecretKey::XPrv(DescriptorXKey {
origin: None,
xkey: xkey.into_xprv(network.into()).unwrap(),
xkey: xkey.into_xprv(network).unwrap(),
derivation_path: BdkDerivationPath::master(),
wildcard: Wildcard::Unhardened,
});
Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod types;
mod wallet;

use crate::bitcoin::Address;
use crate::bitcoin::Network;
use crate::bitcoin::OutPoint;
use crate::bitcoin::PartiallySignedTransaction;
use crate::bitcoin::Script;
Expand All @@ -33,6 +32,7 @@ use crate::wallet::TxBuilder;
use crate::wallet::Update;
use crate::wallet::Wallet;

use bdk::bitcoin::Network;
use bdk::keys::bip39::WordCount;
use bdk::wallet::tx_builder::ChangeSpendPolicy;
use bdk::KeychainKind;
Expand Down
8 changes: 4 additions & 4 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::error::{Alpha3Error, CalculateFeeError};
use crate::types::ScriptAmount;
use crate::types::{Balance, FeeRate};
use crate::Script;
use crate::{AddressIndex, AddressInfo, Network};
use crate::{AddressIndex, AddressInfo};

use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf;
use bdk::bitcoin::psbt::PartiallySignedTransaction as BdkPartiallySignedTransaction;
use bdk::bitcoin::Network;
use bdk::bitcoin::{OutPoint as BdkOutPoint, Sequence, Txid};
use bdk::wallet::tx_builder::ChangeSpendPolicy;
use bdk::wallet::Update as BdkUpdate;
Expand All @@ -33,8 +34,7 @@ impl Wallet {
let descriptor = descriptor.as_string_private();
let change_descriptor = change_descriptor.map(|d| d.as_string_private());

let wallet =
BdkWallet::new_no_persist(&descriptor, change_descriptor.as_ref(), network.into())?;
let wallet = BdkWallet::new_no_persist(&descriptor, change_descriptor.as_ref(), network)?;

Ok(Wallet {
inner_mutex: Mutex::new(wallet),
Expand All @@ -51,7 +51,7 @@ impl Wallet {
}

pub fn network(&self) -> Network {
self.get_wallet().network().into()
self.get_wallet().network()
}

pub fn get_internal_address(&self, address_index: AddressIndex) -> AddressInfo {
Expand Down

0 comments on commit f0548f4

Please sign in to comment.