1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/*
This file is part of TALER
Copyright (C) 2022 Taler Systems SA
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
use std::path::PathBuf;
use bitcoin::{Address, Amount, BlockHash, Network};
use btc_wire::{
config::BitcoinConfig,
rpc::{Category, Rpc},
rpc_utils::default_data_dir,
};
use clap::StructOpt;
use common::{
config::{Config, CoreConfig},
postgres::{Client, NoTls},
rand_slice,
};
/// btc-wire test utils
#[derive(clap::Parser, Debug)]
#[clap(name = "btc-wire-utils")]
struct Args {
/// Override default configuration file path
#[clap(global = true, short, long)]
config: Option<PathBuf>,
/// Override default data directory path
#[clap(global = true, short, long)]
datadir: Option<PathBuf>,
#[clap(subcommand)]
cmd: Cmd,
}
#[derive(clap::Subcommand, Debug)]
enum Cmd {
/// Send taler deposit transactions
Transfer {
#[clap(short, long, default_value_t = String::from("client"))]
/// sender wallet
from: String,
#[clap(short, long, default_value_t = String::from("wire"))]
/// receiver wallet
to: String,
/// amount to send in btc
amount: f64,
},
/// Wait or mine the next block
Nblock {
#[clap(default_value_t = String::from("wire"))]
/// receiver wallet
to: String,
},
/// Abandon all unconfirmed transaction
Abandon {
#[clap(default_value_t = String::from("wire"))]
/// sender wallet
from: String,
},
/// Clear database
Resetdb,
}
pub fn auto_wallet(rpc: &mut Rpc, config: &BitcoinConfig, name: &str) -> (Rpc, Address) {
// Auto load
rpc.load_wallet(name).ok();
let mut wallet = Rpc::wallet(config, name).unwrap();
let addr = wallet
.gen_addr()
.unwrap_or_else(|_| panic!("Failed to get wallet address {}", name));
(wallet, addr)
}
fn main() {
common::log::init();
let args = Args::parse();
let config = args
.config
.map(|path| CoreConfig::load_taler_config(Some(&path), Some("BTC")));
let data_dir: PathBuf = config
.as_ref()
.and_then(|it| it.data_dir.clone())
.or(args.datadir)
.unwrap_or_else(default_data_dir);
let btc_config = BitcoinConfig::load(data_dir).unwrap();
let mut rpc = Rpc::common(&btc_config).unwrap();
match args.cmd {
Cmd::Transfer { from, to, amount } => {
let (mut client, _) = auto_wallet(&mut rpc, &btc_config, &from);
let (_, to) = auto_wallet(&mut rpc, &btc_config, &to);
let tx = client
.send_segwit_key(&to, &Amount::from_btc(amount).unwrap(), &rand_slice())
.unwrap();
println!("{}", tx);
}
Cmd::Nblock { to } => {
match btc_config.network {
Network::Regtest => {
// Manually mine a block
let (_, addr) = auto_wallet(&mut rpc, &btc_config, &to);
rpc.mine(1, &addr).unwrap();
}
_ => {
// Wait for next network block
rpc.wait_for_new_block().ok();
}
}
}
Cmd::Abandon { from } => {
let (mut wire, _) = auto_wallet(&mut rpc, &btc_config, &from);
let list = wire.list_since_block(None, 1).unwrap();
for tx in list.transactions {
if tx.category == Category::Send && tx.confirmations == 0 {
wire.abandon_tx(&tx.txid).unwrap();
}
}
}
Cmd::Resetdb => {
let hash: BlockHash = rpc.get_block_hash(0).unwrap();
let mut db = Client::connect(&config.unwrap().db_url, NoTls).unwrap();
let mut tx = db.transaction().unwrap();
// Clear transaction tables and reset state
tx.execute("DELETE FROM tx_in", &[]).unwrap();
tx.execute("DELETE FROM tx_out", &[]).unwrap();
tx.execute("DELETE FROM bounce", &[]).unwrap();
tx.execute(
"UPDATE state SET value=$1 WHERE name='last_hash'",
&[&hash.as_ref()],
)
.unwrap();
tx.commit().unwrap();
}
}
}
|