2018-06-06 17:36:29 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! Wrappers around library functions, intended to split functions
|
|
|
|
//! into external and internal APIs (i.e. functions for the local wallet
|
|
|
|
//! vs. functions to interact with someone else)
|
|
|
|
//! Still experimental, not sure this is the best way to do this
|
|
|
|
|
2018-06-08 08:21:54 +03:00
|
|
|
use std::marker::PhantomData;
|
2018-07-12 18:49:37 +03:00
|
|
|
use std::sync::{Arc, Mutex};
|
2018-06-08 08:21:54 +03:00
|
|
|
|
2018-06-14 15:16:14 +03:00
|
|
|
use core::ser;
|
|
|
|
use keychain::Keychain;
|
2018-06-06 17:36:29 +03:00
|
|
|
use libtx::slate::Slate;
|
2018-06-29 20:41:28 +03:00
|
|
|
use libwallet::internal::{tx, updater};
|
2018-07-09 20:01:19 +03:00
|
|
|
use libwallet::types::{
|
|
|
|
BlockFees, CbData, OutputData, TxWrapper, WalletBackend, WalletClient, WalletInfo,
|
|
|
|
};
|
|
|
|
use libwallet::Error;
|
2018-06-07 17:04:21 +03:00
|
|
|
use util::{self, LOGGER};
|
2018-06-06 17:36:29 +03:00
|
|
|
|
|
|
|
/// Wrapper around internal API functions, containing a reference to
|
|
|
|
/// the wallet/keychain that they're acting upon
|
2018-07-12 18:49:37 +03:00
|
|
|
pub struct APIOwner<W: ?Sized, C, K>
|
2018-06-06 17:36:29 +03:00
|
|
|
where
|
2018-07-12 18:49:37 +03:00
|
|
|
W: WalletBackend<C, K>,
|
2018-07-10 11:18:24 +03:00
|
|
|
C: WalletClient,
|
2018-06-08 08:21:54 +03:00
|
|
|
K: Keychain,
|
2018-06-06 17:36:29 +03:00
|
|
|
{
|
|
|
|
/// Wallet, contains its keychain (TODO: Split these up into 2 traits
|
|
|
|
/// perhaps)
|
2018-07-12 18:49:37 +03:00
|
|
|
pub wallet: Arc<Mutex<Box<W>>>,
|
2018-06-08 08:21:54 +03:00
|
|
|
phantom: PhantomData<K>,
|
2018-07-10 11:18:24 +03:00
|
|
|
phantom_c: PhantomData<C>,
|
2018-06-06 17:36:29 +03:00
|
|
|
}
|
|
|
|
|
2018-07-12 18:49:37 +03:00
|
|
|
impl<W: ?Sized, C, K> APIOwner<W, C, K>
|
2018-06-06 17:36:29 +03:00
|
|
|
where
|
2018-07-12 18:49:37 +03:00
|
|
|
W: WalletBackend<C, K>,
|
2018-07-10 11:18:24 +03:00
|
|
|
C: WalletClient,
|
2018-06-08 08:21:54 +03:00
|
|
|
K: Keychain,
|
2018-06-06 17:36:29 +03:00
|
|
|
{
|
|
|
|
/// Create new API instance
|
2018-07-12 18:49:37 +03:00
|
|
|
pub fn new(wallet_in: Arc<Mutex<Box<W>>>) -> Self {
|
2018-06-08 08:21:54 +03:00
|
|
|
APIOwner {
|
|
|
|
wallet: wallet_in,
|
|
|
|
phantom: PhantomData,
|
2018-07-10 11:18:24 +03:00
|
|
|
phantom_c: PhantomData,
|
2018-06-08 08:21:54 +03:00
|
|
|
}
|
2018-06-06 17:36:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to update and retrieve outputs
|
|
|
|
/// Return (whether the outputs were validated against a node, OutputData)
|
|
|
|
pub fn retrieve_outputs(
|
2018-07-12 18:49:37 +03:00
|
|
|
&self,
|
2018-06-06 17:36:29 +03:00
|
|
|
include_spent: bool,
|
2018-06-13 23:58:45 +03:00
|
|
|
refresh_from_node: bool,
|
2018-06-06 17:36:29 +03:00
|
|
|
) -> Result<(bool, Vec<OutputData>), Error> {
|
2018-07-12 18:49:37 +03:00
|
|
|
|
|
|
|
let mut w = self.wallet.lock().unwrap();
|
|
|
|
w.open_with_credentials()?;
|
|
|
|
|
2018-06-13 23:58:45 +03:00
|
|
|
let mut validated = false;
|
|
|
|
if refresh_from_node {
|
2018-07-12 18:49:37 +03:00
|
|
|
validated = self.update_outputs(&mut w);
|
2018-06-13 23:58:45 +03:00
|
|
|
}
|
2018-07-12 18:49:37 +03:00
|
|
|
|
|
|
|
let res = Ok((
|
2018-06-06 17:36:29 +03:00
|
|
|
validated,
|
2018-07-12 18:49:37 +03:00
|
|
|
updater::retrieve_outputs(&mut **w, include_spent)?,
|
|
|
|
));
|
|
|
|
|
|
|
|
w.close()?;
|
|
|
|
res
|
2018-06-06 17:36:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve summary info for wallet
|
2018-06-14 19:02:05 +03:00
|
|
|
pub fn retrieve_summary_info(
|
|
|
|
&mut self,
|
|
|
|
refresh_from_node: bool,
|
|
|
|
) -> Result<(bool, WalletInfo), Error> {
|
2018-07-12 18:49:37 +03:00
|
|
|
let mut w = self.wallet.lock().unwrap();
|
|
|
|
w.open_with_credentials()?;
|
|
|
|
|
2018-06-13 23:58:45 +03:00
|
|
|
let mut validated = false;
|
|
|
|
if refresh_from_node {
|
2018-07-12 18:49:37 +03:00
|
|
|
validated = self.update_outputs(&mut w);
|
2018-06-13 23:58:45 +03:00
|
|
|
}
|
2018-07-12 18:49:37 +03:00
|
|
|
|
|
|
|
let wallet_info = updater::retrieve_info(&mut **w)?;
|
|
|
|
let res = Ok((validated, wallet_info));
|
|
|
|
|
|
|
|
w.close()?;
|
|
|
|
res
|
2018-06-06 17:36:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Issues a send transaction and sends to recipient
|
|
|
|
pub fn issue_send_tx(
|
|
|
|
&mut self,
|
|
|
|
amount: u64,
|
|
|
|
minimum_confirmations: u64,
|
|
|
|
dest: &str,
|
|
|
|
max_outputs: usize,
|
|
|
|
selection_strategy_is_use_all: bool,
|
2018-07-12 18:49:37 +03:00
|
|
|
) -> Result<Slate, Error> {
|
|
|
|
let mut w = self.wallet.lock().unwrap();
|
|
|
|
w.open_with_credentials()?;
|
|
|
|
|
|
|
|
let client;
|
|
|
|
let mut slate_out: Slate;
|
|
|
|
let lock_fn_out;
|
|
|
|
|
|
|
|
client = w.client().clone();
|
2018-06-07 17:04:21 +03:00
|
|
|
let (slate, context, lock_fn) = tx::create_send_tx(
|
2018-07-12 18:49:37 +03:00
|
|
|
&mut **w,
|
2018-06-06 17:36:29 +03:00
|
|
|
amount,
|
|
|
|
minimum_confirmations,
|
|
|
|
max_outputs,
|
|
|
|
selection_strategy_is_use_all,
|
2018-06-07 17:04:21 +03:00
|
|
|
)?;
|
|
|
|
|
2018-07-12 18:49:37 +03:00
|
|
|
lock_fn_out = lock_fn;
|
|
|
|
slate_out = match w.client().send_tx_slate(dest, &slate) {
|
2018-06-07 17:04:21 +03:00
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => {
|
|
|
|
error!(
|
2018-07-12 18:49:37 +03:00
|
|
|
LOGGER,
|
|
|
|
"Communication with receiver failed on SenderInitiation send. Aborting transaction {:?}",
|
|
|
|
e,
|
|
|
|
);
|
2018-06-07 17:04:21 +03:00
|
|
|
return Err(e)?;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-12 18:49:37 +03:00
|
|
|
tx::complete_tx(&mut **w, &mut slate_out, &context)?;
|
2018-06-07 17:04:21 +03:00
|
|
|
|
2018-07-12 18:49:37 +03:00
|
|
|
// lock our inputs
|
|
|
|
lock_fn_out(&mut **w)?;
|
|
|
|
w.close()?;
|
|
|
|
Ok(slate_out)
|
2018-06-06 17:36:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Issue a burn TX
|
|
|
|
pub fn issue_burn_tx(
|
|
|
|
&mut self,
|
|
|
|
amount: u64,
|
|
|
|
minimum_confirmations: u64,
|
|
|
|
max_outputs: usize,
|
|
|
|
) -> Result<(), Error> {
|
2018-07-12 18:49:37 +03:00
|
|
|
let mut w = self.wallet.lock().unwrap();
|
|
|
|
w.open_with_credentials()?;
|
|
|
|
let tx_burn = tx::issue_burn_tx(&mut **w, amount, minimum_confirmations, max_outputs)?;
|
2018-06-07 17:04:21 +03:00
|
|
|
let tx_hex = util::to_hex(ser::ser_vec(&tx_burn).unwrap());
|
2018-07-12 18:49:37 +03:00
|
|
|
w.client().post_tx(&TxWrapper { tx_hex: tx_hex }, false)?;
|
|
|
|
w.close()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Posts a transaction to the chain
|
|
|
|
pub fn post_tx(&self, slate: &Slate, fluff: bool) -> Result<(), Error> {
|
|
|
|
let tx_hex = util::to_hex(ser::ser_vec(&slate.tx).unwrap());
|
|
|
|
let client = {
|
|
|
|
let mut w = self.wallet.lock().unwrap();
|
|
|
|
w.client().clone()
|
|
|
|
};
|
|
|
|
client.post_tx(&TxWrapper { tx_hex: tx_hex }, fluff)?;
|
2018-06-07 17:04:21 +03:00
|
|
|
Ok(())
|
2018-06-06 17:36:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to restore contents of wallet
|
|
|
|
pub fn restore(&mut self) -> Result<(), Error> {
|
2018-07-12 18:49:37 +03:00
|
|
|
let mut w = self.wallet.lock().unwrap();
|
|
|
|
w.open_with_credentials()?;
|
|
|
|
let res = w.restore();
|
|
|
|
w.close()?;
|
|
|
|
res
|
2018-06-06 17:36:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve current height from node
|
|
|
|
pub fn node_height(&mut self) -> Result<(u64, bool), Error> {
|
2018-07-12 18:49:37 +03:00
|
|
|
let mut w = self.wallet.lock().unwrap();
|
|
|
|
w.open_with_credentials()?;
|
|
|
|
let res = w.client().get_chain_height();
|
|
|
|
match res {
|
|
|
|
Ok(height) => {
|
|
|
|
w.close()?;
|
|
|
|
Ok((height, true))
|
|
|
|
},
|
2018-06-06 17:36:29 +03:00
|
|
|
Err(_) => {
|
2018-06-13 23:58:45 +03:00
|
|
|
let outputs = self.retrieve_outputs(true, false)?;
|
2018-06-06 17:36:29 +03:00
|
|
|
let height = match outputs.1.iter().map(|out| out.height).max() {
|
|
|
|
Some(height) => height,
|
|
|
|
None => 0,
|
|
|
|
};
|
2018-07-12 18:49:37 +03:00
|
|
|
w.close()?;
|
2018-06-06 17:36:29 +03:00
|
|
|
Ok((height, false))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to update outputs in wallet, return whether it was successful
|
2018-07-12 18:49:37 +03:00
|
|
|
fn update_outputs(&self, w: &mut W ) -> bool {
|
|
|
|
match updater::refresh_outputs(&mut *w) {
|
2018-06-06 17:36:29 +03:00
|
|
|
Ok(_) => true,
|
|
|
|
Err(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wrapper around external API functions, intended to communicate
|
|
|
|
/// with other parties
|
2018-07-12 18:49:37 +03:00
|
|
|
pub struct APIForeign<W: ?Sized, C, K>
|
2018-06-06 17:36:29 +03:00
|
|
|
where
|
2018-07-12 18:49:37 +03:00
|
|
|
W: WalletBackend<C, K>,
|
2018-07-10 11:18:24 +03:00
|
|
|
C: WalletClient,
|
2018-06-08 08:21:54 +03:00
|
|
|
K: Keychain,
|
2018-06-06 17:36:29 +03:00
|
|
|
{
|
|
|
|
/// Wallet, contains its keychain (TODO: Split these up into 2 traits
|
|
|
|
/// perhaps)
|
2018-07-12 18:49:37 +03:00
|
|
|
pub wallet: Arc<Mutex<Box<W>>>,
|
2018-06-08 08:21:54 +03:00
|
|
|
phantom: PhantomData<K>,
|
2018-07-10 11:18:24 +03:00
|
|
|
phantom_c: PhantomData<C>,
|
2018-06-06 17:36:29 +03:00
|
|
|
}
|
|
|
|
|
2018-07-12 18:49:37 +03:00
|
|
|
impl<'a, W: ?Sized, C, K> APIForeign<W, C, K>
|
2018-06-06 17:36:29 +03:00
|
|
|
where
|
2018-07-10 11:18:24 +03:00
|
|
|
W: WalletBackend<C, K>,
|
|
|
|
C: WalletClient,
|
2018-06-08 08:21:54 +03:00
|
|
|
K: Keychain,
|
2018-06-06 17:36:29 +03:00
|
|
|
{
|
|
|
|
/// Create new API instance
|
2018-07-12 18:49:37 +03:00
|
|
|
pub fn new(wallet_in: Arc<Mutex<Box<W>>>) -> Box<Self> {
|
|
|
|
Box::new(APIForeign {
|
2018-06-08 08:21:54 +03:00
|
|
|
wallet: wallet_in,
|
|
|
|
phantom: PhantomData,
|
2018-07-10 11:18:24 +03:00
|
|
|
phantom_c: PhantomData,
|
2018-07-12 18:49:37 +03:00
|
|
|
})
|
2018-06-06 17:36:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Build a new (potential) coinbase transaction in the wallet
|
|
|
|
pub fn build_coinbase(&mut self, block_fees: &BlockFees) -> Result<CbData, Error> {
|
2018-07-12 18:49:37 +03:00
|
|
|
let mut w = self.wallet.lock().unwrap();
|
|
|
|
w.open_with_credentials()?;
|
|
|
|
let res = updater::build_coinbase(&mut **w, block_fees);
|
|
|
|
w.close()?;
|
|
|
|
res
|
|
|
|
|
2018-06-06 17:36:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive a transaction from a sender
|
|
|
|
pub fn receive_tx(&mut self, slate: &mut Slate) -> Result<(), Error> {
|
2018-07-12 18:49:37 +03:00
|
|
|
let mut w = self.wallet.lock().unwrap();
|
|
|
|
w.open_with_credentials()?;
|
|
|
|
let res = tx::receive_tx(&mut **w, slate);
|
|
|
|
w.close()?;
|
|
|
|
res
|
2018-06-06 17:36:29 +03:00
|
|
|
}
|
|
|
|
}
|