2018-03-05 22:33:44 +03:00
|
|
|
|
// Copyright 2018 The Grin Developers
|
2017-09-24 07:40:31 +03:00
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
|
use crate::core::core::{self, amount_to_hr_string};
|
|
|
|
|
use crate::core::global;
|
|
|
|
|
use crate::libwallet::types::{AcctPathMapping, OutputData, OutputStatus, TxLogEntry, WalletInfo};
|
|
|
|
|
use crate::libwallet::Error;
|
|
|
|
|
use crate::util;
|
|
|
|
|
use crate::util::secp::pedersen;
|
2017-11-10 17:03:31 +03:00
|
|
|
|
use prettytable;
|
2018-06-14 15:16:14 +03:00
|
|
|
|
use std::io::prelude::Write;
|
2018-06-06 17:36:29 +03:00
|
|
|
|
use term;
|
2017-09-24 07:40:31 +03:00
|
|
|
|
|
2018-06-06 17:36:29 +03:00
|
|
|
|
/// Display outputs in a pretty way
|
2018-08-17 14:15:06 +03:00
|
|
|
|
pub fn outputs(
|
2018-10-10 12:11:01 +03:00
|
|
|
|
account: &str,
|
2018-08-17 14:15:06 +03:00
|
|
|
|
cur_height: u64,
|
|
|
|
|
validated: bool,
|
|
|
|
|
outputs: Vec<(OutputData, pedersen::Commitment)>,
|
2018-11-03 11:42:41 +03:00
|
|
|
|
dark_background_color_scheme: bool,
|
2018-08-17 14:15:06 +03:00
|
|
|
|
) -> Result<(), Error> {
|
2018-10-10 12:11:01 +03:00
|
|
|
|
let title = format!(
|
|
|
|
|
"Wallet Outputs - Account '{}' - Block Height: {}",
|
|
|
|
|
account, cur_height
|
|
|
|
|
);
|
2018-06-06 17:36:29 +03:00
|
|
|
|
println!();
|
|
|
|
|
let mut t = term::stdout().unwrap();
|
|
|
|
|
t.fg(term::color::MAGENTA).unwrap();
|
|
|
|
|
writeln!(t, "{}", title).unwrap();
|
|
|
|
|
t.reset().unwrap();
|
|
|
|
|
|
|
|
|
|
let mut table = table!();
|
|
|
|
|
|
|
|
|
|
table.set_titles(row![
|
2018-08-17 14:15:06 +03:00
|
|
|
|
bMG->"Output Commitment",
|
2018-06-06 17:36:29 +03:00
|
|
|
|
bMG->"Block Height",
|
|
|
|
|
bMG->"Locked Until",
|
|
|
|
|
bMG->"Status",
|
2018-08-17 14:15:06 +03:00
|
|
|
|
bMG->"Coinbase?",
|
|
|
|
|
bMG->"# Confirms",
|
2018-07-19 12:35:36 +03:00
|
|
|
|
bMG->"Value",
|
2018-08-17 14:15:06 +03:00
|
|
|
|
bMG->"Tx"
|
2018-06-06 17:36:29 +03:00
|
|
|
|
]);
|
|
|
|
|
|
2018-08-17 14:15:06 +03:00
|
|
|
|
for (out, commit) in outputs {
|
|
|
|
|
let commit = format!("{}", util::to_hex(commit.as_ref().to_vec()));
|
2018-06-06 17:36:29 +03:00
|
|
|
|
let height = format!("{}", out.height);
|
|
|
|
|
let lock_height = format!("{}", out.lock_height);
|
|
|
|
|
let is_coinbase = format!("{}", out.is_coinbase);
|
2018-10-18 16:42:31 +03:00
|
|
|
|
|
|
|
|
|
// Mark unconfirmed coinbase outputs as "Mining" instead of "Unconfirmed"
|
|
|
|
|
let status = match out.status {
|
|
|
|
|
OutputStatus::Unconfirmed if out.is_coinbase => "Mining".to_string(),
|
|
|
|
|
_ => format!("{}", out.status),
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-06 17:36:29 +03:00
|
|
|
|
let num_confirmations = format!("{}", out.num_confirmations(cur_height));
|
2018-08-17 20:05:35 +03:00
|
|
|
|
let value = format!("{}", core::amount_to_hr_string(out.value, false));
|
2018-07-19 12:35:36 +03:00
|
|
|
|
let tx = match out.tx_log_entry {
|
2018-08-17 14:15:06 +03:00
|
|
|
|
None => "".to_owned(),
|
2018-07-19 12:35:36 +03:00
|
|
|
|
Some(t) => t.to_string(),
|
|
|
|
|
};
|
2018-11-03 11:42:41 +03:00
|
|
|
|
|
|
|
|
|
if dark_background_color_scheme {
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFC->commit,
|
|
|
|
|
bFB->height,
|
|
|
|
|
bFB->lock_height,
|
|
|
|
|
bFR->status,
|
|
|
|
|
bFY->is_coinbase,
|
|
|
|
|
bFB->num_confirmations,
|
|
|
|
|
bFG->value,
|
|
|
|
|
bFC->tx,
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFD->commit,
|
|
|
|
|
bFB->height,
|
|
|
|
|
bFB->lock_height,
|
|
|
|
|
bFR->status,
|
|
|
|
|
bFD->is_coinbase,
|
|
|
|
|
bFB->num_confirmations,
|
|
|
|
|
bFG->value,
|
|
|
|
|
bFD->tx,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2018-06-06 17:36:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
table.set_format(*prettytable::format::consts::FORMAT_NO_COLSEP);
|
|
|
|
|
table.printstd();
|
|
|
|
|
println!();
|
|
|
|
|
|
|
|
|
|
if !validated {
|
|
|
|
|
println!(
|
|
|
|
|
"\nWARNING: Wallet failed to verify data. \
|
|
|
|
|
The above is from local cache and possibly invalid! \
|
|
|
|
|
(is your `grin server` offline or broken?)"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-19 12:35:36 +03:00
|
|
|
|
/// Display transaction log in a pretty way
|
2018-07-20 17:13:37 +03:00
|
|
|
|
pub fn txs(
|
2018-10-10 12:11:01 +03:00
|
|
|
|
account: &str,
|
2018-07-20 17:13:37 +03:00
|
|
|
|
cur_height: u64,
|
|
|
|
|
validated: bool,
|
|
|
|
|
txs: Vec<TxLogEntry>,
|
|
|
|
|
include_status: bool,
|
2018-11-03 11:42:41 +03:00
|
|
|
|
dark_background_color_scheme: bool,
|
2018-07-20 17:13:37 +03:00
|
|
|
|
) -> Result<(), Error> {
|
2018-10-10 12:11:01 +03:00
|
|
|
|
let title = format!(
|
|
|
|
|
"Transaction Log - Account '{}' - Block Height: {}",
|
|
|
|
|
account, cur_height
|
|
|
|
|
);
|
2018-07-19 12:35:36 +03:00
|
|
|
|
println!();
|
|
|
|
|
let mut t = term::stdout().unwrap();
|
|
|
|
|
t.fg(term::color::MAGENTA).unwrap();
|
|
|
|
|
writeln!(t, "{}", title).unwrap();
|
|
|
|
|
t.reset().unwrap();
|
|
|
|
|
|
|
|
|
|
let mut table = table!();
|
|
|
|
|
|
|
|
|
|
table.set_titles(row![
|
|
|
|
|
bMG->"Id",
|
|
|
|
|
bMG->"Type",
|
|
|
|
|
bMG->"Shared Transaction Id",
|
|
|
|
|
bMG->"Creation Time",
|
|
|
|
|
bMG->"Confirmed?",
|
|
|
|
|
bMG->"Confirmation Time",
|
2018-10-31 00:40:53 +03:00
|
|
|
|
bMG->"Num. \nInputs",
|
|
|
|
|
bMG->"Num. \nOutputs",
|
|
|
|
|
bMG->"Amount \nCredited",
|
|
|
|
|
bMG->"Amount \nDebited",
|
2018-07-20 17:13:37 +03:00
|
|
|
|
bMG->"Fee",
|
2018-10-31 00:40:53 +03:00
|
|
|
|
bMG->"Net \nDifference",
|
|
|
|
|
bMG->"Tx \nData",
|
2018-07-19 12:35:36 +03:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
for t in txs {
|
|
|
|
|
let id = format!("{}", t.id);
|
|
|
|
|
let slate_id = match t.tx_slate_id {
|
|
|
|
|
Some(m) => format!("{}", m),
|
|
|
|
|
None => "None".to_owned(),
|
|
|
|
|
};
|
|
|
|
|
let entry_type = format!("{}", t.tx_type);
|
2018-08-17 20:05:35 +03:00
|
|
|
|
let creation_ts = format!("{}", t.creation_ts.format("%Y-%m-%d %H:%M:%S"));
|
2018-07-19 12:35:36 +03:00
|
|
|
|
let confirmation_ts = match t.confirmation_ts {
|
2018-08-17 20:05:35 +03:00
|
|
|
|
Some(m) => format!("{}", m.format("%Y-%m-%d %H:%M:%S")),
|
2018-07-19 12:35:36 +03:00
|
|
|
|
None => "None".to_owned(),
|
|
|
|
|
};
|
|
|
|
|
let confirmed = format!("{}", t.confirmed);
|
|
|
|
|
let num_inputs = format!("{}", t.num_inputs);
|
|
|
|
|
let num_outputs = format!("{}", t.num_outputs);
|
2018-08-17 20:05:35 +03:00
|
|
|
|
let amount_debited_str = core::amount_to_hr_string(t.amount_debited, true);
|
|
|
|
|
let amount_credited_str = core::amount_to_hr_string(t.amount_credited, true);
|
2018-07-20 17:13:37 +03:00
|
|
|
|
let fee = match t.fee {
|
2018-08-17 20:05:35 +03:00
|
|
|
|
Some(f) => format!("{}", core::amount_to_hr_string(f, true)),
|
2018-07-20 17:13:37 +03:00
|
|
|
|
None => "None".to_owned(),
|
|
|
|
|
};
|
|
|
|
|
let net_diff = if t.amount_credited >= t.amount_debited {
|
2018-08-17 20:05:35 +03:00
|
|
|
|
core::amount_to_hr_string(t.amount_credited - t.amount_debited, true)
|
2018-07-20 17:13:37 +03:00
|
|
|
|
} else {
|
|
|
|
|
format!(
|
|
|
|
|
"-{}",
|
2018-08-17 20:05:35 +03:00
|
|
|
|
core::amount_to_hr_string(t.amount_debited - t.amount_credited, true)
|
2018-07-20 17:13:37 +03:00
|
|
|
|
)
|
|
|
|
|
};
|
2018-12-18 19:44:03 +03:00
|
|
|
|
let tx_data = match t.stored_tx {
|
2018-12-14 19:24:53 +03:00
|
|
|
|
Some(t) => format!("{}", t),
|
2018-09-05 14:12:29 +03:00
|
|
|
|
None => "None".to_owned(),
|
|
|
|
|
};
|
2018-11-03 11:42:41 +03:00
|
|
|
|
if dark_background_color_scheme {
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFC->id,
|
|
|
|
|
bFC->entry_type,
|
|
|
|
|
bFC->slate_id,
|
|
|
|
|
bFB->creation_ts,
|
|
|
|
|
bFC->confirmed,
|
|
|
|
|
bFB->confirmation_ts,
|
|
|
|
|
bFC->num_inputs,
|
|
|
|
|
bFC->num_outputs,
|
|
|
|
|
bFG->amount_credited_str,
|
|
|
|
|
bFR->amount_debited_str,
|
|
|
|
|
bFR->fee,
|
|
|
|
|
bFY->net_diff,
|
|
|
|
|
bFb->tx_data,
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
if t.confirmed {
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFD->id,
|
|
|
|
|
bFb->entry_type,
|
|
|
|
|
bFD->slate_id,
|
|
|
|
|
bFB->creation_ts,
|
|
|
|
|
bFg->confirmed,
|
|
|
|
|
bFB->confirmation_ts,
|
|
|
|
|
bFD->num_inputs,
|
|
|
|
|
bFD->num_outputs,
|
|
|
|
|
bFG->amount_credited_str,
|
|
|
|
|
bFD->amount_debited_str,
|
|
|
|
|
bFD->fee,
|
|
|
|
|
bFG->net_diff,
|
|
|
|
|
bFB->tx_data,
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFD->id,
|
|
|
|
|
bFb->entry_type,
|
|
|
|
|
bFD->slate_id,
|
|
|
|
|
bFB->creation_ts,
|
|
|
|
|
bFR->confirmed,
|
|
|
|
|
bFB->confirmation_ts,
|
|
|
|
|
bFD->num_inputs,
|
|
|
|
|
bFD->num_outputs,
|
|
|
|
|
bFG->amount_credited_str,
|
|
|
|
|
bFD->amount_debited_str,
|
|
|
|
|
bFD->fee,
|
|
|
|
|
bFG->net_diff,
|
|
|
|
|
bFB->tx_data,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-19 12:35:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
table.set_format(*prettytable::format::consts::FORMAT_NO_COLSEP);
|
|
|
|
|
table.printstd();
|
|
|
|
|
println!();
|
|
|
|
|
|
2018-07-20 17:13:37 +03:00
|
|
|
|
if !validated && include_status {
|
2018-07-19 12:35:36 +03:00
|
|
|
|
println!(
|
|
|
|
|
"\nWARNING: Wallet failed to verify data. \
|
|
|
|
|
The above is from local cache and possibly invalid! \
|
|
|
|
|
(is your `grin server` offline or broken?)"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2018-06-06 17:36:29 +03:00
|
|
|
|
/// Display summary info in a pretty way
|
2018-11-03 11:42:41 +03:00
|
|
|
|
pub fn info(
|
|
|
|
|
account: &str,
|
|
|
|
|
wallet_info: &WalletInfo,
|
|
|
|
|
validated: bool,
|
|
|
|
|
dark_background_color_scheme: bool,
|
|
|
|
|
) {
|
2018-03-04 03:19:54 +03:00
|
|
|
|
println!(
|
2018-10-10 12:11:01 +03:00
|
|
|
|
"\n____ Wallet Summary Info - Account '{}' as of height {} ____\n",
|
2018-11-20 14:17:03 +03:00
|
|
|
|
account, wallet_info.last_confirmed_height,
|
2018-03-04 03:19:54 +03:00
|
|
|
|
);
|
2018-11-20 14:17:03 +03:00
|
|
|
|
|
|
|
|
|
let mut table = table!();
|
|
|
|
|
|
|
|
|
|
if dark_background_color_scheme {
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFG->"Total",
|
|
|
|
|
FG->amount_to_hr_string(wallet_info.total, false)
|
|
|
|
|
]);
|
|
|
|
|
// Only dispay "Immature Coinbase" if we have related outputs in the wallet.
|
|
|
|
|
// This row just introduces confusion if the wallet does not receive coinbase rewards.
|
|
|
|
|
if wallet_info.amount_immature > 0 {
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFY->format!("Immature Coinbase (< {})", global::coinbase_maturity()),
|
|
|
|
|
FY->amount_to_hr_string(wallet_info.amount_immature, false)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFY->format!("Awaiting Confirmation (< {})", wallet_info.minimum_confirmations),
|
|
|
|
|
FY->amount_to_hr_string(wallet_info.amount_awaiting_confirmation, false)
|
|
|
|
|
]);
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
Fr->"Locked by previous transaction",
|
|
|
|
|
Fr->amount_to_hr_string(wallet_info.amount_locked, false)
|
|
|
|
|
]);
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
Fw->"--------------------------------",
|
|
|
|
|
Fw->"-------------"
|
|
|
|
|
]);
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFG->"Currently Spendable",
|
|
|
|
|
FG->amount_to_hr_string(wallet_info.amount_currently_spendable, false)
|
|
|
|
|
]);
|
2018-11-03 11:42:41 +03:00
|
|
|
|
} else {
|
2018-11-20 14:17:03 +03:00
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFG->"Total",
|
|
|
|
|
FG->amount_to_hr_string(wallet_info.total, false)
|
|
|
|
|
]);
|
|
|
|
|
// Only dispay "Immature Coinbase" if we have related outputs in the wallet.
|
|
|
|
|
// This row just introduces confusion if the wallet does not receive coinbase rewards.
|
|
|
|
|
if wallet_info.amount_immature > 0 {
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFB->format!("Immature Coinbase (< {})", global::coinbase_maturity()),
|
|
|
|
|
FB->amount_to_hr_string(wallet_info.amount_immature, false)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFB->format!("Awaiting Confirmation (< {})", wallet_info.minimum_confirmations),
|
|
|
|
|
FB->amount_to_hr_string(wallet_info.amount_awaiting_confirmation, false)
|
|
|
|
|
]);
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
Fr->"Locked by previous transaction",
|
|
|
|
|
Fr->amount_to_hr_string(wallet_info.amount_locked, false)
|
|
|
|
|
]);
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
Fw->"--------------------------------",
|
|
|
|
|
Fw->"-------------"
|
|
|
|
|
]);
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFG->"Currently Spendable",
|
|
|
|
|
FG->amount_to_hr_string(wallet_info.amount_currently_spendable, false)
|
|
|
|
|
]);
|
2018-11-03 11:42:41 +03:00
|
|
|
|
};
|
2018-02-06 14:42:26 +03:00
|
|
|
|
table.set_format(*prettytable::format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
|
|
|
|
|
table.printstd();
|
|
|
|
|
println!();
|
2018-06-14 19:02:05 +03:00
|
|
|
|
if !validated {
|
2018-02-06 14:42:26 +03:00
|
|
|
|
println!(
|
2018-06-14 19:02:05 +03:00
|
|
|
|
"\nWARNING: Wallet failed to verify data against a live chain. \
|
|
|
|
|
The above is from local cache and only valid up to the given height! \
|
|
|
|
|
(is your `grin server` offline or broken?)"
|
2018-02-06 14:42:26 +03:00
|
|
|
|
);
|
2018-06-14 19:02:05 +03:00
|
|
|
|
}
|
2017-09-24 07:40:31 +03:00
|
|
|
|
}
|
2018-10-10 12:11:01 +03:00
|
|
|
|
/// Display list of wallet accounts in a pretty way
|
2018-10-10 18:56:15 +03:00
|
|
|
|
pub fn accounts(acct_mappings: Vec<AcctPathMapping>) {
|
2018-10-10 12:11:01 +03:00
|
|
|
|
println!("\n____ Wallet Accounts ____\n",);
|
|
|
|
|
let mut table = table!();
|
|
|
|
|
|
|
|
|
|
table.set_titles(row![
|
|
|
|
|
mMG->"Name",
|
|
|
|
|
bMG->"Parent BIP-32 Derivation Path",
|
|
|
|
|
]);
|
|
|
|
|
for m in acct_mappings {
|
|
|
|
|
table.add_row(row![
|
|
|
|
|
bFC->m.label,
|
|
|
|
|
bGC->m.path.to_bip_32_string(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
table.set_format(*prettytable::format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
|
|
|
|
|
table.printstd();
|
|
|
|
|
println!();
|
|
|
|
|
}
|