2018-01-10 22:36:27 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2017-05-25 02:08:39 +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-02-06 14:42:26 +03:00
|
|
|
use uuid::Uuid;
|
2018-02-13 18:35:30 +03:00
|
|
|
|
2017-10-03 03:02:31 +03:00
|
|
|
use api;
|
2017-11-01 21:32:34 +03:00
|
|
|
use client;
|
2017-05-29 06:21:29 +03:00
|
|
|
use checker;
|
2018-05-16 15:18:09 +03:00
|
|
|
use core::core::amount_to_hr_string;
|
|
|
|
use libwallet::{aggsig, build, transaction};
|
|
|
|
use grinwallet::selection;
|
2017-10-12 06:35:40 +03:00
|
|
|
use core::ser;
|
2018-05-16 15:18:09 +03:00
|
|
|
use keychain::{Identifier, Keychain};
|
2017-10-12 06:35:40 +03:00
|
|
|
use receiver::TxWrapper;
|
2017-05-25 02:08:39 +03:00
|
|
|
use types::*;
|
2017-10-12 19:56:44 +03:00
|
|
|
use util::LOGGER;
|
2017-10-12 06:35:40 +03:00
|
|
|
use util;
|
2018-02-28 20:56:09 +03:00
|
|
|
use failure::ResultExt;
|
2017-05-25 02:08:39 +03:00
|
|
|
|
2017-06-08 04:12:15 +03:00
|
|
|
/// Issue a new transaction to the provided sender by spending some of our
|
|
|
|
/// wallet
|
2018-03-05 22:33:44 +03:00
|
|
|
/// Outputs. The destination can be "stdout" (for command line) (currently disabled) or a URL to the
|
2017-06-08 04:12:15 +03:00
|
|
|
/// recipients wallet receiver (to be implemented).
|
2017-09-29 21:44:25 +03:00
|
|
|
pub fn issue_send_tx(
|
|
|
|
config: &WalletConfig,
|
2017-10-03 03:02:31 +03:00
|
|
|
keychain: &Keychain,
|
2017-09-29 21:44:25 +03:00
|
|
|
amount: u64,
|
2017-10-18 23:47:37 +03:00
|
|
|
minimum_confirmations: u64,
|
2017-09-29 21:44:25 +03:00
|
|
|
dest: String,
|
2017-11-15 21:56:35 +03:00
|
|
|
max_outputs: usize,
|
2018-01-12 23:05:57 +03:00
|
|
|
selection_strategy_is_use_all: bool,
|
2018-03-20 06:18:54 +03:00
|
|
|
fluff: bool,
|
2017-09-29 21:44:25 +03:00
|
|
|
) -> Result<(), Error> {
|
2017-10-11 21:12:01 +03:00
|
|
|
checker::refresh_outputs(config, keychain)?;
|
2017-08-10 03:54:10 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
// Create a new aggsig context
|
|
|
|
let mut context_manager = aggsig::ContextManager::new();
|
|
|
|
let tx_id = Uuid::new_v4();
|
|
|
|
|
|
|
|
// Get lock height
|
2017-10-11 21:12:01 +03:00
|
|
|
let chain_tip = checker::get_tip_from_node(config)?;
|
2017-10-18 23:47:37 +03:00
|
|
|
let current_height = chain_tip.height;
|
2018-05-16 15:18:09 +03:00
|
|
|
// ensure outputs we're selecting are up to date
|
|
|
|
checker::refresh_outputs(config, keychain)?;
|
2017-10-18 23:47:37 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
let lock_height = current_height;
|
2017-10-11 21:12:01 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
let tx_data = selection::build_send_tx(
|
2017-10-18 23:47:37 +03:00
|
|
|
config,
|
|
|
|
keychain,
|
|
|
|
amount,
|
|
|
|
current_height,
|
|
|
|
minimum_confirmations,
|
|
|
|
lock_height,
|
2017-11-15 21:56:35 +03:00
|
|
|
max_outputs,
|
2018-01-12 23:05:57 +03:00
|
|
|
selection_strategy_is_use_all,
|
2017-10-18 23:47:37 +03:00
|
|
|
)?;
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
let partial_tx = transaction::sender_initiation(
|
|
|
|
keychain,
|
|
|
|
&tx_id,
|
|
|
|
&mut context_manager,
|
|
|
|
current_height,
|
|
|
|
tx_data,
|
|
|
|
)?;
|
2018-05-07 16:21:41 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
let context = context_manager.get_context(&tx_id);
|
2017-08-10 03:54:10 +03:00
|
|
|
|
2017-11-20 22:12:52 +03:00
|
|
|
// Closure to acquire wallet lock and lock the coins being spent
|
|
|
|
// so we avoid accidental double spend attempt.
|
2018-03-04 03:19:54 +03:00
|
|
|
let update_wallet = || {
|
|
|
|
WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
2018-05-16 15:18:09 +03:00
|
|
|
for id in context.get_outputs().clone() {
|
|
|
|
let coin = wallet_data.get_output(&id).unwrap().clone();
|
2018-03-04 03:19:54 +03:00
|
|
|
wallet_data.lock_output(&coin);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// Closure to acquire wallet lock and delete the change output in case of tx
|
|
|
|
// failure.
|
|
|
|
let rollback_wallet = || {
|
|
|
|
WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
2018-05-16 15:18:09 +03:00
|
|
|
match context.change_key.clone() {
|
2018-04-27 17:26:40 +03:00
|
|
|
Some(change) => {
|
|
|
|
info!(LOGGER, "cleaning up unused change output from wallet");
|
|
|
|
wallet_data.delete_output(&change);
|
|
|
|
}
|
|
|
|
None => info!(LOGGER, "No change output to clean from wallet"),
|
|
|
|
}
|
2018-03-04 03:19:54 +03:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: stdout option removed for now, as it won't work very will with this
|
|
|
|
// version of aggsig exchange
|
2018-01-10 22:36:27 +03:00
|
|
|
|
|
|
|
/*if dest == "stdout" {
|
2017-11-01 21:32:34 +03:00
|
|
|
let json_tx = serde_json::to_string_pretty(&partial_tx).unwrap();
|
2017-11-18 02:33:16 +03:00
|
|
|
update_wallet()?;
|
2017-06-06 23:18:16 +03:00
|
|
|
println!("{}", json_tx);
|
2018-01-10 22:36:27 +03:00
|
|
|
} else */
|
|
|
|
|
|
|
|
if &dest[..4] != "http" {
|
2018-03-28 19:36:10 +03:00
|
|
|
WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
2018-05-16 15:18:09 +03:00
|
|
|
match context.change_key.clone() {
|
2018-04-27 17:26:40 +03:00
|
|
|
Some(change) => {
|
|
|
|
info!(LOGGER, "cleaning up unused change output from wallet");
|
|
|
|
wallet_data.delete_output(&change);
|
|
|
|
}
|
|
|
|
None => info!(LOGGER, "No change output to clean from wallet"),
|
|
|
|
}
|
2018-04-06 02:31:34 +03:00
|
|
|
}).unwrap();
|
2018-03-04 03:19:54 +03:00
|
|
|
panic!(
|
|
|
|
"dest formatted as {} but send -d expected stdout or http://IP:port",
|
|
|
|
dest
|
|
|
|
);
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
2018-01-10 22:36:27 +03:00
|
|
|
|
|
|
|
let url = format!("{}/v1/receive/transaction", &dest);
|
|
|
|
debug!(LOGGER, "Posting partial transaction to {}", url);
|
2018-03-20 06:18:54 +03:00
|
|
|
let res = client::send_partial_tx(&url, &partial_tx, fluff);
|
2018-01-10 22:36:27 +03:00
|
|
|
if let Err(e) = res {
|
2018-02-28 20:56:09 +03:00
|
|
|
match e.kind() {
|
2018-03-04 03:19:54 +03:00
|
|
|
ErrorKind::FeeExceedsAmount {
|
|
|
|
sender_amount,
|
|
|
|
recipient_fee,
|
|
|
|
} => error!(
|
2018-01-17 06:03:40 +03:00
|
|
|
LOGGER,
|
2018-01-12 15:44:15 +03:00
|
|
|
"Recipient rejected the transfer because transaction fee ({}) exceeded amount ({}).",
|
|
|
|
amount_to_hr_string(recipient_fee),
|
|
|
|
amount_to_hr_string(sender_amount)
|
|
|
|
),
|
2018-03-04 03:19:54 +03:00
|
|
|
_ => error!(
|
|
|
|
LOGGER,
|
|
|
|
"Communication with receiver failed on SenderInitiation send. Aborting transaction"
|
|
|
|
),
|
2018-01-12 15:44:15 +03:00
|
|
|
}
|
2018-01-10 22:36:27 +03:00
|
|
|
rollback_wallet()?;
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
let partial_tx =
|
|
|
|
transaction::sender_confirmation(keychain, &mut context_manager, res.unwrap())?;
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
// And send again, expecting completed transaction as result this time
|
|
|
|
let res = client::send_partial_tx_final(&url, &partial_tx, fluff);
|
2018-01-10 22:36:27 +03:00
|
|
|
if let Err(e) = res {
|
2018-02-28 20:56:09 +03:00
|
|
|
match e.kind() {
|
|
|
|
ErrorKind::FeeExceedsAmount {sender_amount, recipient_fee} =>
|
2018-01-12 15:44:15 +03:00
|
|
|
error!(
|
2018-01-17 06:03:40 +03:00
|
|
|
LOGGER,
|
2018-01-12 15:44:15 +03:00
|
|
|
"Recipient rejected the transfer because transaction fee ({}) exceeded amount ({}).",
|
|
|
|
amount_to_hr_string(recipient_fee),
|
|
|
|
amount_to_hr_string(sender_amount)
|
|
|
|
),
|
|
|
|
_ => error!(LOGGER, "Communication with receiver failed on SenderConfirmation send. Aborting transaction"),
|
|
|
|
}
|
2018-01-10 22:36:27 +03:00
|
|
|
rollback_wallet()?;
|
|
|
|
return Err(e);
|
|
|
|
}
|
2018-02-13 18:35:30 +03:00
|
|
|
|
2018-05-09 12:15:58 +03:00
|
|
|
// Not really necessary here
|
2018-05-16 15:18:09 +03:00
|
|
|
context_manager.save_context(context.clone());
|
2018-05-09 12:15:58 +03:00
|
|
|
|
2018-02-13 18:35:30 +03:00
|
|
|
// All good so
|
2018-01-10 22:36:27 +03:00
|
|
|
update_wallet()?;
|
2017-05-25 02:08:39 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-10-18 23:47:37 +03:00
|
|
|
pub fn issue_burn_tx(
|
|
|
|
config: &WalletConfig,
|
|
|
|
keychain: &Keychain,
|
|
|
|
amount: u64,
|
|
|
|
minimum_confirmations: u64,
|
2017-11-15 21:56:35 +03:00
|
|
|
max_outputs: usize,
|
2017-10-18 23:47:37 +03:00
|
|
|
) -> Result<(), Error> {
|
2017-10-16 20:11:01 +03:00
|
|
|
let keychain = &Keychain::burn_enabled(keychain, &Identifier::zero());
|
|
|
|
|
2017-10-18 23:47:37 +03:00
|
|
|
let chain_tip = checker::get_tip_from_node(config)?;
|
|
|
|
let current_height = chain_tip.height;
|
|
|
|
|
2017-10-12 06:35:40 +03:00
|
|
|
let _ = checker::refresh_outputs(config, keychain);
|
2017-10-16 20:11:01 +03:00
|
|
|
|
|
|
|
let key_id = keychain.root_key_id();
|
2017-06-15 07:42:58 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
// select some spendable coins from the wallet
|
|
|
|
let coins = WalletData::read_wallet(&config.data_file_dir, |wallet_data| {
|
2018-02-28 20:56:09 +03:00
|
|
|
Ok(wallet_data.select_coins(
|
2017-11-15 21:56:35 +03:00
|
|
|
key_id.clone(),
|
|
|
|
amount,
|
|
|
|
current_height,
|
|
|
|
minimum_confirmations,
|
|
|
|
max_outputs,
|
|
|
|
false,
|
2018-02-28 20:56:09 +03:00
|
|
|
))
|
2017-10-26 00:09:34 +03:00
|
|
|
})?;
|
2017-10-13 07:45:07 +03:00
|
|
|
|
2017-11-10 22:33:36 +03:00
|
|
|
debug!(LOGGER, "selected some coins - {}", coins.len());
|
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
let fee = tx_fee(coins.len(), 2, selection::coins_proof_count(&coins), None);
|
|
|
|
let (mut parts, _) = selection::inputs_and_change(&coins, config, keychain, amount, fee)?;
|
2017-10-12 06:35:40 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
// add burn output and fees
|
2017-11-20 22:12:52 +03:00
|
|
|
parts.push(build::output(amount - fee, Identifier::zero()));
|
2017-10-12 06:35:40 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
// finalize the burn transaction and send
|
2018-02-28 20:56:09 +03:00
|
|
|
let tx_burn = build::transaction(parts, &keychain).context(ErrorKind::Keychain)?;
|
|
|
|
tx_burn.validate().context(ErrorKind::Transaction)?;
|
2017-10-12 06:35:40 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
let tx_hex = util::to_hex(ser::ser_vec(&tx_burn).unwrap());
|
|
|
|
let url = format!("{}/v1/pool/push", config.check_node_api_http_addr.as_str());
|
2017-11-01 02:32:33 +03:00
|
|
|
let _: () =
|
2018-02-28 20:56:09 +03:00
|
|
|
api::client::post(url.as_str(), &TxWrapper { tx_hex: tx_hex }).context(ErrorKind::Node)?;
|
2017-10-26 00:09:34 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-10-12 06:35:40 +03:00
|
|
|
|
2017-08-23 02:05:56 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2018-05-09 12:15:58 +03:00
|
|
|
use libwallet::build;
|
2017-10-03 03:02:31 +03:00
|
|
|
use keychain::Keychain;
|
2017-08-23 02:05:56 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
// demonstrate that input.commitment == referenced output.commitment
|
2017-11-01 21:32:34 +03:00
|
|
|
// based on the public key and amount begin spent
|
2017-08-23 02:05:56 +03:00
|
|
|
fn output_commitment_equals_input_commitment_on_spend() {
|
2017-10-03 03:02:31 +03:00
|
|
|
let keychain = Keychain::from_random_seed().unwrap();
|
2017-10-13 07:45:07 +03:00
|
|
|
let key_id1 = keychain.derive_key_id(1).unwrap();
|
2017-08-23 02:05:56 +03:00
|
|
|
|
2018-02-13 18:35:30 +03:00
|
|
|
let tx1 = build::transaction(vec![build::output(105, key_id1.clone())], &keychain).unwrap();
|
2018-03-02 23:47:27 +03:00
|
|
|
let tx2 = build::transaction(vec![build::input(105, key_id1.clone())], &keychain).unwrap();
|
2017-08-23 02:05:56 +03:00
|
|
|
|
2018-01-17 06:03:40 +03:00
|
|
|
assert_eq!(tx1.outputs[0].features, tx2.inputs[0].features);
|
2017-10-13 07:45:07 +03:00
|
|
|
assert_eq!(tx1.outputs[0].commitment(), tx2.inputs[0].commitment());
|
2017-08-23 02:05:56 +03:00
|
|
|
}
|
|
|
|
}
|