Skip to content

Commit

Permalink
chore: bump fedimint-recoverytool to bitcoin v0.32
Browse files Browse the repository at this point in the history
  • Loading branch information
tvolk131 committed Oct 27, 2024
1 parent 2adbaee commit aae376a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

36 changes: 36 additions & 0 deletions fedimint-core/src/bitcoin_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ pub fn bitcoin30_to_bitcoin32_invoice(
.expect("Failed to convert bitcoin30 invoice to bitcoin32 invoice")
}

pub fn bitcoin30_to_bitcoin32_secp256k1_secret_key(
secret_key: &bitcoin30::secp256k1::SecretKey,
) -> bitcoin::secp256k1::SecretKey {
bincode::deserialize(
&bincode::serialize(&secret_key)
.expect("Failed to serialize bitcoin30 secp256k1 secret key"),
)
.expect("Failed to convert bitcoin30 secp256k1 secret key to bitcoin32 secp256k1 secret key")
}

pub fn bitcoin32_to_bitcoin30_secp256k1_secret_key(
secret_key: &bitcoin::secp256k1::SecretKey,
) -> bitcoin30::secp256k1::SecretKey {
bincode::deserialize(
&bincode::serialize(&secret_key)
.expect("Failed to serialize bitcoin32 secp256k1 secret key"),
)
.expect("Failed to convert bitcoin32 secp256k1 secret key to bitcoin30 secp256k1 secret key")
}

pub fn bitcoin30_to_bitcoin32_secp256k1_pubkey(
pubkey: &bitcoin30::secp256k1::PublicKey,
) -> bitcoin::secp256k1::PublicKey {
Expand Down Expand Up @@ -97,6 +117,10 @@ pub fn bitcoin32_to_bitcoin30_unchecked_address(
.expect("Failed to convert bitcoin32 address to bitcoin30 address")
}

pub fn bitcoin30_to_bitcoin32_amount(amount: &bitcoin30::Amount) -> bitcoin::Amount {
bitcoin::Amount::from_sat(amount.to_sat())
}

pub fn bitcoin32_to_bitcoin30_amount(amount: &bitcoin::Amount) -> bitcoin30::Amount {
bitcoin30::Amount::from_sat(amount.to_sat())
}
Expand All @@ -108,11 +132,23 @@ pub fn bitcoin30_to_bitcoin32_network(network: &bitcoin30::Network) -> bitcoin::
.expect("Failed to convert bitcoin30 network to bitcoin32 network")
}

fn bitcoin30_to_bitcoin32_txid(txid: &bitcoin30::Txid) -> bitcoin::Txid {
bitcoin::Txid::from_str(&txid.to_string())
.expect("Failed to convert bitcoin30 txid to bitcoin32 txid")
}

fn bitcoin32_to_bitcoin30_txid(txid: &bitcoin::Txid) -> bitcoin30::Txid {
bitcoin30::Txid::from_str(&txid.to_string())
.expect("Failed to convert bitcoin32 txid to bitcoin30 txid")
}

pub fn bitcoin30_to_bitcoin32_outpoint(outpoint: &bitcoin30::OutPoint) -> bitcoin::OutPoint {
bitcoin::OutPoint {
txid: bitcoin30_to_bitcoin32_txid(&outpoint.txid),
vout: outpoint.vout,
}
}

pub fn bitcoin32_to_bitcoin30_outpoint(outpoint: &bitcoin::OutPoint) -> bitcoin30::OutPoint {
bitcoin30::OutPoint {
txid: bitcoin32_to_bitcoin30_txid(&outpoint.txid),
Expand Down
2 changes: 1 addition & 1 deletion fedimint-recoverytool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ path = "src/main.rs"

[dependencies]
anyhow = { workspace = true }
bitcoin30 = { workspace = true }
bitcoin = { workspace = true }
clap = { workspace = true }
fedimint-core = { workspace = true }
fedimint-logging = { workspace = true }
Expand Down
13 changes: 8 additions & 5 deletions fedimint-recoverytool/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::hash::Hasher;

use fedimint_core::bitcoin_migration::bitcoin32_to_bitcoin30_secp256k1_secret_key;
use fedimint_wallet_server::common::keys::CompressedPublicKey;
use miniscript::MiniscriptKey;

Expand All @@ -10,7 +11,7 @@ use miniscript::MiniscriptKey;
#[derive(Debug, Clone, Copy, Eq)]
pub enum Key {
Public(CompressedPublicKey),
Private(bitcoin30::key::PrivateKey),
Private(bitcoin::key::PrivateKey),
}

impl PartialOrd for Key {
Expand Down Expand Up @@ -47,7 +48,9 @@ impl Key {
match self {
Key::Public(pk) => pk,
Key::Private(sk) => {
CompressedPublicKey::new(secp256k1::PublicKey::from_secret_key_global(&sk.inner))
CompressedPublicKey::new(secp256k1::PublicKey::from_secret_key_global(
&bitcoin32_to_bitcoin30_secp256k1_secret_key(&sk.inner),
))
}
}
}
Expand All @@ -71,8 +74,8 @@ impl MiniscriptKey for Key {
0
}

type Sha256 = bitcoin30::hashes::sha256::Hash;
type Sha256 = bitcoin::hashes::sha256::Hash;
type Hash256 = miniscript::hash256::Hash;
type Ripemd160 = bitcoin30::hashes::ripemd160::Hash;
type Hash160 = bitcoin30::hashes::hash160::Hash;
type Ripemd160 = bitcoin::hashes::ripemd160::Hash;
type Hash160 = bitcoin::hashes::hash160::Hash;
}
26 changes: 15 additions & 11 deletions fedimint-recoverytool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ use std::collections::BTreeSet;
use std::path::{Path, PathBuf};

use anyhow::anyhow;
use bitcoin30::network::constants::Network;
use bitcoin30::OutPoint;
use bitcoin::network::Network;
use bitcoin::OutPoint;
use clap::{ArgGroup, Parser, Subcommand};
use fedimint_core::bitcoin_migration::{
bitcoin30_to_bitcoin32_amount, bitcoin30_to_bitcoin32_network, bitcoin30_to_bitcoin32_outpoint,
bitcoin30_to_bitcoin32_secp256k1_secret_key,
};
use fedimint_core::core::LEGACY_HARDCODED_INSTANCE_ID_WALLET;
use fedimint_core::db::{Database, IDatabaseTransactionOpsCoreTyped};
use fedimint_core::epoch::ConsensusItem;
Expand Down Expand Up @@ -125,7 +129,7 @@ async fn main() -> anyhow::Result<()> {
.expect("Malformed wallet config");
let base_descriptor = wallet_cfg.consensus.peg_in_descriptor;
let base_key = wallet_cfg.private.peg_in_key;
let network = wallet_cfg.consensus.network;
let network = bitcoin30_to_bitcoin32_network(&wallet_cfg.consensus.network);

(base_descriptor, base_key, network)
} else if let (Some(descriptor), Some(key)) = (opts.descriptor, opts.key) {
Expand Down Expand Up @@ -172,9 +176,9 @@ async fn process_and_print_tweak_source(
let descriptor = tweak_descriptor(base_descriptor, base_key, &tweak, network);

ImportableWallet {
outpoint,
outpoint: bitcoin30_to_bitcoin32_outpoint(&outpoint),
descriptor,
amount_sat: amount,
amount_sat: bitcoin30_to_bitcoin32_amount(&amount),
}
})
.collect()
Expand Down Expand Up @@ -293,10 +297,10 @@ fn tweak_descriptor(
base_descriptor
.tweak(tweak, secp256k1::SECP256K1)
.translate_pk(&mut SecretKeyInjector {
secret: bitcoin30::key::PrivateKey {
secret: bitcoin::key::PrivateKey {
compressed: true,
network,
inner: secret_key,
network: network.into(),
inner: bitcoin30_to_bitcoin32_secp256k1_secret_key(&secret_key),
},
public: pub_key,
})
Expand All @@ -308,8 +312,8 @@ fn tweak_descriptor(
struct ImportableWallet {
outpoint: OutPoint,
descriptor: Descriptor<Key>,
#[serde(with = "bitcoin30::amount::serde::as_sat")]
amount_sat: bitcoin30::Amount,
#[serde(with = "bitcoin::amount::serde::as_sat")]
amount_sat: bitcoin::Amount,
}

/// A Bitcoin Core importable descriptor
Expand All @@ -322,7 +326,7 @@ struct ImportableWalletMin {
/// know.
#[derive(Debug)]
struct SecretKeyInjector {
secret: bitcoin30::key::PrivateKey,
secret: bitcoin::key::PrivateKey,
public: CompressedPublicKey,
}

Expand Down

0 comments on commit aae376a

Please sign in to comment.