use crate::error::{Error, ErrorKind};
use crate::grin_core::core::hash::Hash;
use crate::grin_core::core::Transaction;
use crate::grin_core::libtx::{aggsig, secp_ser};
use crate::grin_core::ser;
use crate::grin_keychain::{Identifier, Keychain};
use crate::grin_util::secp::key::{PublicKey, SecretKey};
use crate::grin_util::secp::{self, pedersen, Secp256k1};
use crate::slate::ParticipantMessages;
use chrono::prelude::*;
use failure::ResultExt;
use serde;
use serde_json;
use std::collections::HashMap;
use std::fmt;
use uuid::Uuid;
pub trait WalletInst<C, K>: WalletBackend<C, K> + Send + Sync + 'static
where
C: NodeClient,
K: Keychain,
{
}
impl<T, C, K> WalletInst<C, K> for T
where
T: WalletBackend<C, K> + Send + Sync + 'static,
C: NodeClient,
K: Keychain,
{
}
pub trait WalletBackend<C, K>
where
C: NodeClient,
K: Keychain,
{
fn open_with_credentials(&mut self) -> Result<(), Error>;
fn close(&mut self) -> Result<(), Error>;
fn keychain(&mut self) -> &mut K;
fn w2n_client(&mut self) -> &mut C;
fn calc_commit_for_cache(
&mut self,
amount: u64,
id: &Identifier,
) -> Result<Option<String>, Error>;
fn set_parent_key_id_by_name(&mut self, label: &str) -> Result<(), Error>;
fn set_parent_key_id(&mut self, _: Identifier);
fn parent_key_id(&mut self) -> Identifier;
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = OutputData> + 'a>;
fn get(&self, id: &Identifier, mmr_index: &Option<u64>) -> Result<OutputData, Error>;
fn get_tx_log_entry(&self, uuid: &Uuid) -> Result<Option<TxLogEntry>, Error>;
fn get_private_context(
&mut self,
slate_id: &[u8],
participant_id: usize,
) -> Result<Context, Error>;
fn tx_log_iter<'a>(&'a self) -> Box<dyn Iterator<Item = TxLogEntry> + 'a>;
fn acct_path_iter<'a>(&'a self) -> Box<dyn Iterator<Item = AcctPathMapping> + 'a>;
fn get_acct_path(&self, label: String) -> Result<Option<AcctPathMapping>, Error>;
fn store_tx(&self, uuid: &str, tx: &Transaction) -> Result<(), Error>;
fn get_stored_tx(&self, entry: &TxLogEntry) -> Result<Option<Transaction>, Error>;
fn batch<'a>(&'a mut self) -> Result<Box<dyn WalletOutputBatch<K> + 'a>, Error>;
fn next_child<'a>(&mut self) -> Result<Identifier, Error>;
fn last_confirmed_height<'a>(&mut self) -> Result<u64, Error>;
fn restore(&mut self) -> Result<(), Error>;
fn check_repair(&mut self, delete_unconfirmed: bool) -> Result<(), Error>;
}
pub trait WalletOutputBatch<K>
where
K: Keychain,
{
fn keychain(&mut self) -> &mut K;
fn save(&mut self, out: OutputData) -> Result<(), Error>;
fn get(&self, id: &Identifier, mmr_index: &Option<u64>) -> Result<OutputData, Error>;
fn iter(&self) -> Box<dyn Iterator<Item = OutputData>>;
fn delete(&mut self, id: &Identifier, mmr_index: &Option<u64>) -> Result<(), Error>;
fn save_child_index(&mut self, parent_key_id: &Identifier, child_n: u32) -> Result<(), Error>;
fn save_last_confirmed_height(
&mut self,
parent_key_id: &Identifier,
height: u64,
) -> Result<(), Error>;
fn next_tx_log_id(&mut self, parent_key_id: &Identifier) -> Result<u32, Error>;
fn tx_log_iter(&self) -> Box<dyn Iterator<Item = TxLogEntry>>;
fn save_tx_log_entry(&mut self, t: TxLogEntry, parent_id: &Identifier) -> Result<(), Error>;
fn save_acct_path(&mut self, mapping: AcctPathMapping) -> Result<(), Error>;
fn acct_path_iter(&self) -> Box<dyn Iterator<Item = AcctPathMapping>>;
fn lock_output(&mut self, out: &mut OutputData) -> Result<(), Error>;
fn save_private_context(
&mut self,
slate_id: &[u8],
participant_id: usize,
ctx: &Context,
) -> Result<(), Error>;
fn delete_private_context(
&mut self,
slate_id: &[u8],
participant_id: usize,
) -> Result<(), Error>;
fn commit(&self) -> Result<(), Error>;
}
pub trait NodeClient: Sync + Send + Clone {
fn node_url(&self) -> &str;
fn set_node_url(&mut self, node_url: &str);
fn node_api_secret(&self) -> Option<String>;
fn set_node_api_secret(&mut self, node_api_secret: Option<String>);
fn post_tx(&self, tx: &TxWrapper, fluff: bool) -> Result<(), Error>;
fn get_version_info(&mut self) -> Option<NodeVersionInfo>;
fn get_chain_height(&self) -> Result<u64, Error>;
fn get_outputs_from_node(
&self,
wallet_outputs: Vec<pedersen::Commitment>,
) -> Result<HashMap<pedersen::Commitment, (String, u64, u64)>, Error>;
fn get_outputs_by_pmmr_index(
&self,
start_height: u64,
max_outputs: u64,
) -> Result<
(
u64,
u64,
Vec<(pedersen::Commitment, pedersen::RangeProof, bool, u64, u64)>,
),
Error,
>;
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct NodeVersionInfo {
pub node_version: String,
pub block_header_version: u16,
pub verified: Option<bool>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
pub struct OutputData {
pub root_key_id: Identifier,
pub key_id: Identifier,
pub n_child: u32,
pub commit: Option<String>,
#[serde(with = "secp_ser::opt_string_or_u64")]
pub mmr_index: Option<u64>,
#[serde(with = "secp_ser::string_or_u64")]
pub value: u64,
pub status: OutputStatus,
#[serde(with = "secp_ser::string_or_u64")]
pub height: u64,
#[serde(with = "secp_ser::string_or_u64")]
pub lock_height: u64,
pub is_coinbase: bool,
pub tx_log_entry: Option<u32>,
}
impl ser::Writeable for OutputData {
fn write<W: ser::Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
writer.write_bytes(&serde_json::to_vec(self).map_err(|_| ser::Error::CorruptedData)?)
}
}
impl ser::Readable for OutputData {
fn read(reader: &mut dyn ser::Reader) -> Result<OutputData, ser::Error> {
let data = reader.read_bytes_len_prefix()?;
serde_json::from_slice(&data[..]).map_err(|_| ser::Error::CorruptedData)
}
}
impl OutputData {
pub fn lock(&mut self) {
self.status = OutputStatus::Locked;
}
pub fn num_confirmations(&self, current_height: u64) -> u64 {
if self.height > current_height {
return 0;
}
if self.status == OutputStatus::Unconfirmed {
0
} else {
1 + (current_height - self.height)
}
}
pub fn eligible_to_spend(&self, current_height: u64, minimum_confirmations: u64) -> bool {
if [OutputStatus::Spent, OutputStatus::Locked].contains(&self.status) {
return false;
} else if self.status == OutputStatus::Unconfirmed && self.is_coinbase {
return false;
} else if self.lock_height > current_height {
return false;
} else if self.status == OutputStatus::Unspent
&& self.num_confirmations(current_height) >= minimum_confirmations
{
return true;
} else if self.status == OutputStatus::Unconfirmed && minimum_confirmations == 0 {
return true;
} else {
return false;
}
}
pub fn mark_unspent(&mut self) {
match self.status {
OutputStatus::Unconfirmed => self.status = OutputStatus::Unspent,
_ => (),
}
}
pub fn mark_spent(&mut self) {
match self.status {
OutputStatus::Unspent => self.status = OutputStatus::Spent,
OutputStatus::Locked => self.status = OutputStatus::Spent,
_ => (),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub enum OutputStatus {
Unconfirmed,
Unspent,
Locked,
Spent,
}
impl fmt::Display for OutputStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
OutputStatus::Unconfirmed => write!(f, "Unconfirmed"),
OutputStatus::Unspent => write!(f, "Unspent"),
OutputStatus::Locked => write!(f, "Locked"),
OutputStatus::Spent => write!(f, "Spent"),
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Context {
pub parent_key_id: Identifier,
pub sec_key: SecretKey,
pub sec_nonce: SecretKey,
pub output_ids: Vec<(Identifier, Option<u64>, u64)>,
pub input_ids: Vec<(Identifier, Option<u64>, u64)>,
pub fee: u64,
pub participant_id: usize,
}
impl Context {
pub fn new(
secp: &secp::Secp256k1,
sec_key: SecretKey,
parent_key_id: &Identifier,
use_test_rng: bool,
participant_id: usize,
) -> Context {
let sec_nonce = match use_test_rng {
false => aggsig::create_secnonce(secp).unwrap(),
true => SecretKey::from_slice(secp, &[1; 32]).unwrap(),
};
Context {
parent_key_id: parent_key_id.clone(),
sec_key: sec_key,
sec_nonce,
input_ids: vec![],
output_ids: vec![],
fee: 0,
participant_id: participant_id,
}
}
}
impl Context {
pub fn add_output(&mut self, output_id: &Identifier, mmr_index: &Option<u64>, amount: u64) {
self.output_ids
.push((output_id.clone(), mmr_index.clone(), amount));
}
pub fn get_outputs(&self) -> Vec<(Identifier, Option<u64>, u64)> {
self.output_ids.clone()
}
pub fn add_input(&mut self, input_id: &Identifier, mmr_index: &Option<u64>, amount: u64) {
self.input_ids
.push((input_id.clone(), mmr_index.clone(), amount));
}
pub fn get_inputs(&self) -> Vec<(Identifier, Option<u64>, u64)> {
self.input_ids.clone()
}
pub fn get_private_keys(&self) -> (SecretKey, SecretKey) {
(self.sec_key.clone(), self.sec_nonce.clone())
}
pub fn get_public_keys(&self, secp: &Secp256k1) -> (PublicKey, PublicKey) {
(
PublicKey::from_secret_key(secp, &self.sec_key).unwrap(),
PublicKey::from_secret_key(secp, &self.sec_nonce).unwrap(),
)
}
}
impl ser::Writeable for Context {
fn write<W: ser::Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
writer.write_bytes(&serde_json::to_vec(self).map_err(|_| ser::Error::CorruptedData)?)
}
}
impl ser::Readable for Context {
fn read(reader: &mut dyn ser::Reader) -> Result<Context, ser::Error> {
let data = reader.read_bytes_len_prefix()?;
serde_json::from_slice(&data[..]).map_err(|_| ser::Error::CorruptedData)
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
pub struct BlockIdentifier(pub Hash);
impl BlockIdentifier {
pub fn hash(&self) -> Hash {
self.0
}
pub fn from_hex(hex: &str) -> Result<BlockIdentifier, Error> {
let hash =
Hash::from_hex(hex).context(ErrorKind::GenericError("Invalid hex".to_owned()))?;
Ok(BlockIdentifier(hash))
}
}
impl serde::ser::Serialize for BlockIdentifier {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
serializer.serialize_str(&self.0.to_hex())
}
}
impl<'de> serde::de::Deserialize<'de> for BlockIdentifier {
fn deserialize<D>(deserializer: D) -> Result<BlockIdentifier, D::Error>
where
D: serde::de::Deserializer<'de>,
{
deserializer.deserialize_str(BlockIdentifierVisitor)
}
}
struct BlockIdentifierVisitor;
impl<'de> serde::de::Visitor<'de> for BlockIdentifierVisitor {
type Value = BlockIdentifier;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a block hash")
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
let block_hash = Hash::from_hex(s).unwrap();
Ok(BlockIdentifier(block_hash))
}
}
#[derive(Serialize, Eq, PartialEq, Deserialize, Debug, Clone)]
pub struct WalletInfo {
#[serde(with = "secp_ser::string_or_u64")]
pub last_confirmed_height: u64,
#[serde(with = "secp_ser::string_or_u64")]
pub minimum_confirmations: u64,
#[serde(with = "secp_ser::string_or_u64")]
pub total: u64,
#[serde(with = "secp_ser::string_or_u64")]
pub amount_awaiting_finalization: u64,
#[serde(with = "secp_ser::string_or_u64")]
pub amount_awaiting_confirmation: u64,
#[serde(with = "secp_ser::string_or_u64")]
pub amount_immature: u64,
#[serde(with = "secp_ser::string_or_u64")]
pub amount_currently_spendable: u64,
#[serde(with = "secp_ser::string_or_u64")]
pub amount_locked: u64,
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub enum TxLogEntryType {
ConfirmedCoinbase,
TxReceived,
TxSent,
TxReceivedCancelled,
TxSentCancelled,
}
impl fmt::Display for TxLogEntryType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
TxLogEntryType::ConfirmedCoinbase => write!(f, "Confirmed \nCoinbase"),
TxLogEntryType::TxReceived => write!(f, "Received Tx"),
TxLogEntryType::TxSent => write!(f, "Sent Tx"),
TxLogEntryType::TxReceivedCancelled => write!(f, "Received Tx\n- Cancelled"),
TxLogEntryType::TxSentCancelled => write!(f, "Sent Tx\n- Cancelled"),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TxLogEntry {
pub parent_key_id: Identifier,
pub id: u32,
pub tx_slate_id: Option<Uuid>,
pub tx_type: TxLogEntryType,
pub creation_ts: DateTime<Utc>,
pub confirmation_ts: Option<DateTime<Utc>>,
pub confirmed: bool,
pub num_inputs: usize,
pub num_outputs: usize,
#[serde(with = "secp_ser::string_or_u64")]
pub amount_credited: u64,
#[serde(with = "secp_ser::string_or_u64")]
pub amount_debited: u64,
#[serde(with = "secp_ser::opt_string_or_u64")]
pub fee: Option<u64>,
pub messages: Option<ParticipantMessages>,
pub stored_tx: Option<String>,
}
impl ser::Writeable for TxLogEntry {
fn write<W: ser::Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
writer.write_bytes(&serde_json::to_vec(self).map_err(|_| ser::Error::CorruptedData)?)
}
}
impl ser::Readable for TxLogEntry {
fn read(reader: &mut dyn ser::Reader) -> Result<TxLogEntry, ser::Error> {
let data = reader.read_bytes_len_prefix()?;
serde_json::from_slice(&data[..]).map_err(|_| ser::Error::CorruptedData)
}
}
impl TxLogEntry {
pub fn new(parent_key_id: Identifier, t: TxLogEntryType, id: u32) -> Self {
TxLogEntry {
parent_key_id: parent_key_id,
tx_type: t,
id: id,
tx_slate_id: None,
creation_ts: Utc::now(),
confirmation_ts: None,
confirmed: false,
amount_credited: 0,
amount_debited: 0,
num_inputs: 0,
num_outputs: 0,
fee: None,
messages: None,
stored_tx: None,
}
}
pub fn sum_confirmed(txs: &Vec<TxLogEntry>) -> (u64, u64) {
txs.iter().fold((0, 0), |acc, tx| match tx.confirmed {
true => (acc.0 + tx.amount_credited, acc.1 + tx.amount_debited),
false => acc,
})
}
pub fn update_confirmation_ts(&mut self) {
self.confirmation_ts = Some(Utc::now());
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AcctPathMapping {
pub label: String,
pub path: Identifier,
}
impl ser::Writeable for AcctPathMapping {
fn write<W: ser::Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
writer.write_bytes(&serde_json::to_vec(self).map_err(|_| ser::Error::CorruptedData)?)
}
}
impl ser::Readable for AcctPathMapping {
fn read(reader: &mut dyn ser::Reader) -> Result<AcctPathMapping, ser::Error> {
let data = reader.read_bytes_len_prefix()?;
serde_json::from_slice(&data[..]).map_err(|_| ser::Error::CorruptedData)
}
}
#[derive(Serialize, Deserialize)]
pub struct TxWrapper {
pub tx_hex: String,
}