2017-05-25 02:08:39 +03:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
2017-10-03 03:02:31 +03:00
|
|
|
use api;
|
2017-05-29 06:21:29 +03:00
|
|
|
use checker;
|
2017-11-01 02:32:33 +03:00
|
|
|
use core::core::{build, Transaction};
|
2017-10-12 06:35:40 +03:00
|
|
|
use core::ser;
|
2017-11-01 02:32:33 +03:00
|
|
|
use keychain::{BlindingFactor, 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;
|
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
|
|
|
|
/// UTXOs. The destination can be "stdout" (for command line) or a URL to the
|
|
|
|
/// recipients wallet receiver (to be implemented).
|
2017-10-12 19:56:44 +03:00
|
|
|
|
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,
|
|
|
|
) -> Result<(), Error> {
|
2017-10-11 21:12:01 +03:00
|
|
|
checker::refresh_outputs(config, keychain)?;
|
2017-08-10 03:54:10 +03:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
// proof of concept - set lock_height on the tx
|
2017-10-11 21:12:01 +03:00
|
|
|
let lock_height = chain_tip.height;
|
|
|
|
|
2017-10-18 23:47:37 +03:00
|
|
|
let (tx, blind_sum) = build_send_tx(
|
|
|
|
config,
|
|
|
|
keychain,
|
|
|
|
amount,
|
|
|
|
current_height,
|
|
|
|
minimum_confirmations,
|
|
|
|
lock_height,
|
|
|
|
)?;
|
2017-05-25 02:08:39 +03:00
|
|
|
let json_tx = partial_tx_to_json(amount, blind_sum, tx);
|
2017-08-10 03:54:10 +03:00
|
|
|
|
2017-05-25 02:08:39 +03:00
|
|
|
if dest == "stdout" {
|
2017-06-06 23:18:16 +03:00
|
|
|
println!("{}", json_tx);
|
2017-05-25 02:08:39 +03:00
|
|
|
} else if &dest[..4] == "http" {
|
2017-11-01 02:32:33 +03:00
|
|
|
let url = format!("{}/v1/receive/transaction", &dest);
|
2017-10-12 19:56:44 +03:00
|
|
|
debug!(LOGGER, "Posting partial transaction to {}", url);
|
2017-06-27 05:09:01 +03:00
|
|
|
let request = WalletReceiveRequest::PartialTransaction(json_tx);
|
2017-10-12 19:56:44 +03:00
|
|
|
let _: CbData = api::client::post(url.as_str(), &request).expect(&format!(
|
|
|
|
"Wallet receiver at {} unreachable, could not send transaction. Is it running?",
|
|
|
|
url
|
|
|
|
));
|
2017-10-03 03:02:31 +03:00
|
|
|
} else {
|
|
|
|
panic!("dest not in expected format: {}", dest);
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds a transaction to send to someone from the HD seed associated with the
|
|
|
|
/// wallet and the amount to send. Handles reading through the wallet data file,
|
|
|
|
/// selecting outputs to spend and building the change.
|
2017-09-29 21:44:25 +03:00
|
|
|
fn build_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
|
|
|
current_height: u64,
|
|
|
|
minimum_confirmations: u64,
|
2017-10-11 21:12:01 +03:00
|
|
|
lock_height: u64,
|
2017-10-03 03:02:31 +03:00
|
|
|
) -> Result<(Transaction, BlindingFactor), Error> {
|
2017-10-13 07:45:07 +03:00
|
|
|
let key_id = keychain.clone().root_key_id();
|
2017-05-25 02:08:39 +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| {
|
|
|
|
wallet_data.select(key_id.clone(), current_height, minimum_confirmations)
|
|
|
|
})?;
|
2017-05-25 02:08:39 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
// build transaction skeleton with inputs and change
|
|
|
|
let mut parts = inputs_and_change(&coins, config, keychain, key_id, amount)?;
|
2017-10-11 21:12:01 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
// This is more proof of concept than anything but here we set lock_height
|
2017-11-01 02:32:33 +03:00
|
|
|
// on tx being sent (based on current chain height via api).
|
2017-10-26 00:09:34 +03:00
|
|
|
parts.push(build::with_lock_height(lock_height));
|
2017-10-11 21:12:01 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
let (tx, blind) = build::transaction(parts, &keychain)?;
|
2017-10-13 07:45:07 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
Ok((tx, blind))
|
2017-10-12 06:35:40 +03:00
|
|
|
}
|
2017-10-11 21:12:01 +03:00
|
|
|
|
2017-10-18 23:47:37 +03:00
|
|
|
pub fn issue_burn_tx(
|
|
|
|
config: &WalletConfig,
|
|
|
|
keychain: &Keychain,
|
|
|
|
amount: u64,
|
|
|
|
minimum_confirmations: u64,
|
|
|
|
) -> 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| {
|
|
|
|
wallet_data.select(key_id.clone(), current_height, minimum_confirmations)
|
|
|
|
})?;
|
2017-10-13 07:45:07 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
let mut parts = inputs_and_change(&coins, config, keychain, key_id, amount)?;
|
2017-10-12 06:35:40 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
// add burn output and fees
|
|
|
|
let fee = tx_fee(coins.len(), 2, None);
|
|
|
|
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
|
|
|
|
let (tx_burn, _) = build::transaction(parts, &keychain)?;
|
|
|
|
tx_burn.validate(&keychain.secp())?;
|
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 _: () =
|
|
|
|
api::client::post(url.as_str(), &TxWrapper { tx_hex: tx_hex }).map_err(|e| Error::Node(e))?;
|
2017-10-26 00:09:34 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-10-12 06:35:40 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
fn next_available_key(
|
|
|
|
config: &WalletConfig,
|
|
|
|
keychain: &Keychain,
|
|
|
|
) -> Result<(Identifier, u32), Error> {
|
|
|
|
let res = WalletData::read_wallet(&config.data_file_dir, |wallet_data| {
|
|
|
|
let root_key_id = keychain.root_key_id();
|
|
|
|
let derivation = wallet_data.next_child(root_key_id.clone());
|
|
|
|
let key_id = keychain.derive_key_id(derivation).unwrap();
|
|
|
|
(key_id, derivation)
|
|
|
|
})?;
|
|
|
|
Ok(res)
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
2017-08-23 02:05:56 +03:00
|
|
|
|
2017-10-13 07:45:07 +03:00
|
|
|
fn inputs_and_change(
|
|
|
|
coins: &Vec<OutputData>,
|
2017-10-26 00:09:34 +03:00
|
|
|
config: &WalletConfig,
|
2017-10-13 07:45:07 +03:00
|
|
|
keychain: &Keychain,
|
|
|
|
root_key_id: Identifier,
|
|
|
|
amount: u64,
|
|
|
|
) -> Result<Vec<Box<build::Append>>, Error> {
|
2017-10-12 06:35:40 +03:00
|
|
|
let mut parts = vec![];
|
|
|
|
|
2017-10-13 07:45:07 +03:00
|
|
|
// calculate the total across all inputs, and how much is left
|
2017-10-12 06:35:40 +03:00
|
|
|
let total: u64 = coins.iter().map(|c| c.value).sum();
|
2017-10-13 07:45:07 +03:00
|
|
|
let shortage = (total as i64) - (amount as i64);
|
2017-10-12 06:35:40 +03:00
|
|
|
if shortage < 0 {
|
|
|
|
return Err(Error::NotEnoughFunds((-shortage) as u64));
|
|
|
|
}
|
2017-10-13 07:45:07 +03:00
|
|
|
|
|
|
|
// sender is responsible for setting the fee on the partial tx
|
2017-11-01 02:32:33 +03:00
|
|
|
// recipient should double check the fee calculation and not blindly trust the
|
|
|
|
// sender
|
2017-10-13 07:45:07 +03:00
|
|
|
let fee = tx_fee(coins.len(), 2, None);
|
2017-10-12 06:35:40 +03:00
|
|
|
parts.push(build::with_fee(fee));
|
|
|
|
|
2017-10-13 07:45:07 +03:00
|
|
|
// if we are spending 10,000 coins to send 1,000 then our change will be 9,000
|
2017-11-01 02:32:33 +03:00
|
|
|
// the fee will come out of the amount itself
|
|
|
|
// if the fee is 80 then the recipient will only receive 920
|
|
|
|
// but our change will still be 9,000
|
2017-10-13 07:45:07 +03:00
|
|
|
let change = total - amount;
|
|
|
|
|
|
|
|
// build inputs using the appropriate derived key_ids
|
2017-10-12 06:35:40 +03:00
|
|
|
for coin in coins {
|
2017-10-13 07:45:07 +03:00
|
|
|
let key_id = keychain.derive_key_id(coin.n_child)?;
|
|
|
|
parts.push(build::input(coin.value, key_id));
|
2017-10-12 06:35:40 +03:00
|
|
|
}
|
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
let (change_key, change_derivation) = next_available_key(config, keychain)?;
|
|
|
|
|
2017-10-12 06:35:40 +03:00
|
|
|
parts.push(build::output(change, change_key.clone()));
|
2017-10-13 07:45:07 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
// Acquire wallet lock, add the new change output and lock coins being spent.
|
|
|
|
WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
|
|
|
// we got that far, time to start tracking the output representing our change
|
|
|
|
wallet_data.add_output(OutputData {
|
|
|
|
root_key_id: root_key_id.clone(),
|
|
|
|
key_id: change_key.clone(),
|
|
|
|
n_child: change_derivation,
|
|
|
|
value: change as u64,
|
|
|
|
status: OutputStatus::Unconfirmed,
|
|
|
|
height: 0,
|
|
|
|
lock_height: 0,
|
|
|
|
is_coinbase: false,
|
|
|
|
});
|
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
// now lock the ouputs we're spending so we avoid accidental double spend
|
|
|
|
// attempt
|
2017-10-26 00:09:34 +03:00
|
|
|
for coin in coins {
|
|
|
|
wallet_data.lock_output(coin);
|
|
|
|
}
|
|
|
|
})?;
|
2017-10-12 06:35:40 +03:00
|
|
|
|
|
|
|
Ok(parts)
|
|
|
|
}
|
|
|
|
|
2017-08-23 02:05:56 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use core::core::build::{input, output, transaction};
|
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 02:32:33 +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
|
|
|
|
2017-10-13 07:45:07 +03:00
|
|
|
let (tx1, _) = transaction(vec![output(105, key_id1.clone())], &keychain).unwrap();
|
|
|
|
let (tx2, _) = transaction(vec![input(105, key_id1.clone())], &keychain).unwrap();
|
2017-08-23 02:05:56 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|