Skip to content

Commit

Permalink
Create display-wallet-addresses main method
Browse files Browse the repository at this point in the history
This subroutine dumps all addresses known to the wallet, it can be
used for debugging and searching with grep.

Removes the older display-wallet-keys method.
  • Loading branch information
chris-belcher committed Jun 1, 2022
1 parent 831844b commit 9b9f6b8
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/fidelity_bonds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::error::Error;
use crate::messages::FidelityBondProof;
use crate::wallet_sync::{generate_keypair, UTXOSpendInfo, Wallet};

const TIMELOCKED_MPK_PATH: &str = "m/84'/0'/0'/2";
pub const TIMELOCKED_MPK_PATH: &str = "m/84'/0'/0'/2";
pub const TIMELOCKED_ADDRESS_COUNT: u32 = 960;

pub const REGTEST_DUMMY_ONION_HOSTNAME: &str = "regtest-dummy-onion-hostname.onion";
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use bitcoincore_rpc::{Auth, Client, Error, RpcApi};
use chrono::NaiveDateTime;

pub mod wallet_sync;
use wallet_sync::{UTXOSpendInfo, Wallet, WalletSwapCoin, WalletSyncAddressAmount};
use wallet_sync::{
DisplayAddressType, UTXOSpendInfo, Wallet, WalletSwapCoin, WalletSyncAddressAmount,
};

pub mod direct_send;
use direct_send::{CoinToSpend, Destination, SendAmount};
Expand Down Expand Up @@ -397,7 +399,7 @@ pub fn display_wallet_balance(wallet_file_name: &PathBuf, long_form: Option<bool
}
}

pub fn display_wallet_keys(wallet_file_name: &PathBuf) {
pub fn display_wallet_addresses(wallet_file_name: &PathBuf, types: DisplayAddressType) {
let wallet = match Wallet::load_wallet_from_file(
wallet_file_name,
Network::Regtest,
Expand All @@ -409,7 +411,7 @@ pub fn display_wallet_keys(wallet_file_name: &PathBuf) {
return;
}
};
wallet.print_wallet_key_data();
wallet.display_addresses(types);
}

pub fn print_receive_invoice(wallet_file_name: &PathBuf) {
Expand Down
18 changes: 13 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use teleport;
use teleport::direct_send::{CoinToSpend, Destination, SendAmount};
use teleport::fidelity_bonds::YearAndMonth;
use teleport::maker_protocol::MakerBehavior;
use teleport::wallet_sync::WalletSyncAddressAmount;
use teleport::wallet_sync::{DisplayAddressType, WalletSyncAddressAmount};
use teleport::watchtower_protocol::{ContractTransaction, ContractsInfo};

#[derive(Debug, StructOpt)]
Expand Down Expand Up @@ -48,8 +48,13 @@ enum Subcommand {
long_form: Option<bool>,
},

/// Dumps all information in wallet file for debugging
DisplayWalletKeys,
/// Dumps all addresses in wallet file, only useful for debugging
DisplayWalletAddresses {
/// Address types: "all", "masterkey", "seed", "incomingswap", "outgoingswap",
/// "swap", "incomingcontract", "outgoingcontract", "contract", "fidelitybond".
/// Default is "all"
types: Option<DisplayAddressType>,
},

/// Prints receive invoice.
GetReceiveInvoice,
Expand Down Expand Up @@ -130,8 +135,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Subcommand::WalletBalance { long_form } => {
teleport::display_wallet_balance(&args.wallet_file_name, long_form);
}
Subcommand::DisplayWalletKeys => {
teleport::display_wallet_keys(&args.wallet_file_name);
Subcommand::DisplayWalletAddresses { types } => {
teleport::display_wallet_addresses(
&args.wallet_file_name,
types.unwrap_or(DisplayAddressType::All),
);
}
Subcommand::GetReceiveInvoice => {
teleport::print_receive_invoice(&args.wallet_file_name);
Expand Down
193 changes: 167 additions & 26 deletions src/wallet_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ use serde_json::Value;
use rand::rngs::OsRng;
use rand::RngCore;

use chrono::NaiveDateTime;

use crate::contracts;
use crate::contracts::SwapCoin;
use crate::error::Error;
Expand Down Expand Up @@ -93,6 +95,40 @@ pub enum WalletSyncAddressAmount {

const WATCH_ONLY_SWAPCOIN_LABEL: &str = "watchonly_swapcoin_label";

#[derive(PartialEq, Debug)]
pub enum DisplayAddressType {
All,
MasterKey,
Seed,
IncomingSwap,
OutgoingSwap,
Swap,
IncomingContract,
OutgoingContract,
Contract,
FidelityBond,
}

impl FromStr for DisplayAddressType {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"all" => DisplayAddressType::All,
"masterkey" => DisplayAddressType::MasterKey,
"seed" => DisplayAddressType::Seed,
"incomingswap" => DisplayAddressType::IncomingSwap,
"outgoingswap" => DisplayAddressType::OutgoingSwap,
"swap" => DisplayAddressType::Swap,
"incomingcontract" => DisplayAddressType::IncomingContract,
"outgoingcontract" => DisplayAddressType::OutgoingContract,
"contract" => DisplayAddressType::Contract,
"fidelitybond" => DisplayAddressType::FidelityBond,
_ => Err("unknown type")?,
})
}
}

//data needed to find information in addition to ListUnspentResultEntry
//about a UTXO required to spend it
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -443,37 +479,142 @@ impl WalletSwapCoin for OutgoingSwapCoin {
}

impl Wallet {
pub fn print_wallet_key_data(&self) {
println!(
"master key = {}, external_index = {}",
self.master_key, self.external_index
);
pub fn display_addresses(&self, types: DisplayAddressType) {
if types == DisplayAddressType::All || types == DisplayAddressType::MasterKey {
println!(
"master key = {}, external_index = {}",
self.master_key, self.external_index
);
}
let secp = Secp256k1::new();

for (multisig_redeemscript, swapcoin) in &self.incoming_swapcoins {
Self::print_script_and_coin(multisig_redeemscript, swapcoin, self.network);
if types == DisplayAddressType::All || types == DisplayAddressType::Seed {
let top_branch = ExtendedPubKey::from_private(
&secp,
&self
.master_key
.derive_priv(&secp, &DerivationPath::from_str(DERIVATION_PATH).unwrap())
.unwrap(),
);
for c in 0..2 {
println!(
"{} branch from seed",
if c == 0 { "Receive" } else { "Change" }
);
let recv_or_change_branch = top_branch
.ckd_pub(&secp, ChildNumber::Normal { index: c })
.unwrap();
for i in 0..self.initial_address_import_count {
let addr = Address::p2wpkh(
&recv_or_change_branch
.ckd_pub(&secp, ChildNumber::Normal { index: i as u32 })
.unwrap()
.public_key,
self.network,
)
.unwrap();
println!("{} from seed {}/{}/{}", addr, DERIVATION_PATH, c, i);
}
}
}
for (multisig_redeemscript, swapcoin) in &self.outgoing_swapcoins {
Self::print_script_and_coin(multisig_redeemscript, swapcoin, self.network);

if types == DisplayAddressType::All
|| types == DisplayAddressType::IncomingSwap
|| types == DisplayAddressType::Swap
{
println!(
"incoming swapcoin count = {}",
self.incoming_swapcoins.len()
);
for (multisig_redeemscript, swapcoin) in &self.incoming_swapcoins {
println!(
"{} incoming_swapcoin other_privkey={} contract_txid={}",
Address::p2wsh(multisig_redeemscript, self.network),
if swapcoin.other_privkey.is_some() {
"known "
} else {
"unknown"
},
swapcoin.contract_tx.txid()
);
}
}
println!(
"swapcoin count = {}",
self.incoming_swapcoins.len() + self.outgoing_swapcoins.len()
);
}

fn print_script_and_coin(script: &Script, coin: &dyn SwapCoin, network: Network) {
let contract_tx = coin.get_contract_tx();
println!(
"{} {}:{} {}",
Address::p2wsh(script, network),
contract_tx.input[0].previous_output.txid,
contract_tx.input[0].previous_output.vout,
if coin.is_hash_preimage_known() {
" known"
} else {
"unknown"
if types == DisplayAddressType::All
|| types == DisplayAddressType::OutgoingSwap
|| types == DisplayAddressType::Swap
{
println!(
"outgoing swapcoin count = {}",
self.outgoing_swapcoins.len()
);
for (multisig_redeemscript, swapcoin) in &self.outgoing_swapcoins {
println!(
"{} outgoing_swapcoin contract_txid={}",
Address::p2wsh(multisig_redeemscript, self.network),
swapcoin.contract_tx.txid()
);
}
)
}

if types == DisplayAddressType::All
|| types == DisplayAddressType::IncomingContract
|| types == DisplayAddressType::Contract
{
println!(
"incoming swapcoin count = {}",
self.incoming_swapcoins.len()
);
for (_multisig_redeemscript, swapcoin) in &self.incoming_swapcoins {
println!(
"{} incoming_swapcoin_contract hashvalue={} locktime={} contract_txid={}",
Address::p2wsh(&swapcoin.contract_redeemscript, self.network),
&swapcoin.get_hashvalue().to_hex()[..],
swapcoin.get_timelock(),
swapcoin.contract_tx.txid()
);
}
}

if types == DisplayAddressType::All
|| types == DisplayAddressType::OutgoingContract
|| types == DisplayAddressType::Contract
{
println!(
"outgoing swapcoin count = {}",
self.outgoing_swapcoins.len()
);
for (_multisig_redeemscript, swapcoin) in &self.outgoing_swapcoins {
println!(
"{} outgoing_swapcoin_contract hashvalue={} locktime={} contract_txid={}",
Address::p2wsh(&swapcoin.contract_redeemscript, self.network),
&swapcoin.get_hashvalue().to_hex()[..],
swapcoin.get_timelock(),
swapcoin.contract_tx.txid()
);
}
}

if types == DisplayAddressType::All || types == DisplayAddressType::FidelityBond {
let mut timelocked_scripts_list = self
.timelocked_script_index_map
.iter()
.collect::<Vec<(&Script, &u32)>>();
timelocked_scripts_list.sort_by(|a, b| a.1.cmp(b.1));
for (timelocked_scriptpubkey, index) in &timelocked_scripts_list {
let locktime = fidelity_bonds::get_locktime_from_index(**index);
println!(
"{} {}/{} [{}] locktime={}",
Address::from_script(timelocked_scriptpubkey, self.network).unwrap(),
fidelity_bonds::TIMELOCKED_MPK_PATH,
index,
NaiveDateTime::from_timestamp(locktime, 0)
.format("%Y-%m-%d")
.to_string(),
locktime,
);
}
}
}

pub fn save_new_wallet_file<P: AsRef<Path>>(
Expand Down

0 comments on commit 9b9f6b8

Please sign in to comment.