2018-05-16 15:18:09 +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.
|
|
|
|
|
|
|
|
//! Selection of inputs for building transactions
|
|
|
|
|
2018-06-08 08:21:54 +03:00
|
|
|
use keychain::{Identifier, Keychain};
|
2018-06-29 20:41:28 +03:00
|
|
|
use libtx::{build, tx_fee, slate::Slate};
|
2018-06-01 17:06:59 +03:00
|
|
|
use libwallet::error::{Error, ErrorKind};
|
2018-06-06 17:36:29 +03:00
|
|
|
use libwallet::internal::{keys, sigcontext};
|
2018-05-30 19:48:32 +03:00
|
|
|
use libwallet::types::*;
|
2018-05-21 18:28:11 +03:00
|
|
|
|
2018-06-13 19:03:34 +03:00
|
|
|
/// Initialize a transaction on the sender side, returns a corresponding
|
2018-05-21 18:28:11 +03:00
|
|
|
/// libwallet transaction slate with the appropriate inputs selected,
|
|
|
|
/// and saves the private wallet identifiers of our selected outputs
|
|
|
|
/// into our transaction context
|
|
|
|
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn build_send_tx_slate<T, K>(
|
2018-05-30 19:48:32 +03:00
|
|
|
wallet: &mut T,
|
2018-05-21 18:28:11 +03:00
|
|
|
num_participants: usize,
|
|
|
|
amount: u64,
|
|
|
|
current_height: u64,
|
|
|
|
minimum_confirmations: u64,
|
|
|
|
lock_height: u64,
|
|
|
|
max_outputs: usize,
|
|
|
|
selection_strategy_is_use_all: bool,
|
2018-05-24 18:27:26 +03:00
|
|
|
) -> Result<
|
|
|
|
(
|
2018-05-30 19:48:32 +03:00
|
|
|
Slate,
|
2018-05-24 18:27:26 +03:00
|
|
|
sigcontext::Context,
|
2018-05-30 19:48:32 +03:00
|
|
|
impl FnOnce(&mut T) -> Result<(), Error>,
|
2018-05-24 18:27:26 +03:00
|
|
|
),
|
|
|
|
Error,
|
2018-05-30 19:48:32 +03:00
|
|
|
>
|
|
|
|
where
|
2018-06-08 08:21:54 +03:00
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: Keychain,
|
2018-05-30 19:48:32 +03:00
|
|
|
{
|
2018-06-14 19:02:05 +03:00
|
|
|
let (elems, inputs, change, change_derivation, amount, fee) = select_send_tx(
|
2018-05-30 19:48:32 +03:00
|
|
|
wallet,
|
2018-05-21 18:28:11 +03:00
|
|
|
amount,
|
|
|
|
current_height,
|
|
|
|
minimum_confirmations,
|
|
|
|
lock_height,
|
|
|
|
max_outputs,
|
|
|
|
selection_strategy_is_use_all,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Create public slate
|
2018-05-30 19:48:32 +03:00
|
|
|
let mut slate = Slate::blank(num_participants);
|
2018-05-21 18:28:11 +03:00
|
|
|
slate.amount = amount;
|
|
|
|
slate.height = current_height;
|
|
|
|
slate.lock_height = lock_height;
|
|
|
|
slate.fee = fee;
|
|
|
|
|
2018-05-30 19:48:32 +03:00
|
|
|
let keychain = wallet.keychain().clone();
|
|
|
|
|
2018-06-01 17:06:59 +03:00
|
|
|
let blinding = slate.add_transaction_elements(&keychain, elems)?;
|
2018-05-21 18:28:11 +03:00
|
|
|
// Create our own private context
|
2018-05-24 18:27:26 +03:00
|
|
|
let mut context = sigcontext::Context::new(
|
2018-05-30 19:48:32 +03:00
|
|
|
wallet.keychain().secp(),
|
|
|
|
blinding.secret_key(&keychain.secp()).unwrap(),
|
2018-05-21 18:28:11 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// Store our private identifiers for each input
|
|
|
|
for input in inputs {
|
|
|
|
context.add_input(&input.key_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store change output
|
2018-06-14 19:02:05 +03:00
|
|
|
if change_derivation.is_some() {
|
|
|
|
let change_id = keychain.derive_key_id(change_derivation.unwrap()).unwrap();
|
|
|
|
context.add_output(&change_id);
|
2018-05-21 18:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let lock_inputs = context.get_inputs().clone();
|
|
|
|
let _lock_outputs = context.get_outputs().clone();
|
|
|
|
|
2018-06-22 11:08:06 +03:00
|
|
|
let root_key_id = keychain.root_key_id();
|
|
|
|
|
2018-05-21 18:28:11 +03:00
|
|
|
// Return a closure to acquire wallet lock and lock the coins being spent
|
|
|
|
// so we avoid accidental double spend attempt.
|
2018-05-30 19:48:32 +03:00
|
|
|
let update_sender_wallet_fn = move |wallet: &mut T| {
|
2018-06-22 11:08:06 +03:00
|
|
|
let mut batch = wallet.batch()?;
|
|
|
|
for id in lock_inputs {
|
|
|
|
let mut coin = batch.get(&id).unwrap();
|
|
|
|
batch.lock_output(&mut coin);
|
|
|
|
}
|
|
|
|
// write the output representing our change
|
|
|
|
if let Some(d) = change_derivation {
|
|
|
|
let change_id = keychain.derive_key_id(change_derivation.unwrap()).unwrap();
|
|
|
|
|
|
|
|
batch.save(OutputData {
|
|
|
|
root_key_id: root_key_id,
|
|
|
|
key_id: change_id.clone(),
|
|
|
|
n_child: d,
|
|
|
|
value: change as u64,
|
|
|
|
status: OutputStatus::Unconfirmed,
|
|
|
|
height: current_height,
|
|
|
|
lock_height: 0,
|
|
|
|
is_coinbase: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
batch.commit()?;
|
|
|
|
Ok(())
|
2018-05-21 18:28:11 +03:00
|
|
|
};
|
|
|
|
|
2018-05-24 18:27:26 +03:00
|
|
|
Ok((slate, context, update_sender_wallet_fn))
|
2018-05-21 18:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new output in the wallet for the recipient,
|
|
|
|
/// returning the key of the fresh output and a closure
|
|
|
|
/// that actually performs the addition of the output to the
|
|
|
|
/// wallet
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn build_recipient_output_with_slate<T, K>(
|
2018-05-30 19:48:32 +03:00
|
|
|
wallet: &mut T,
|
|
|
|
slate: &mut Slate,
|
2018-05-24 18:27:26 +03:00
|
|
|
) -> Result<
|
|
|
|
(
|
|
|
|
Identifier,
|
|
|
|
sigcontext::Context,
|
2018-05-30 19:48:32 +03:00
|
|
|
impl FnOnce(&mut T) -> Result<(), Error>,
|
2018-05-24 18:27:26 +03:00
|
|
|
),
|
|
|
|
Error,
|
2018-05-30 19:48:32 +03:00
|
|
|
>
|
|
|
|
where
|
2018-06-08 08:21:54 +03:00
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: Keychain,
|
2018-05-30 19:48:32 +03:00
|
|
|
{
|
2018-05-21 18:28:11 +03:00
|
|
|
// Create a potential output for this transaction
|
2018-06-22 11:08:06 +03:00
|
|
|
let (key_id, derivation) = keys::next_available_key(wallet).unwrap();
|
2018-05-21 18:28:11 +03:00
|
|
|
|
2018-06-08 08:21:54 +03:00
|
|
|
let keychain = wallet.keychain().clone();
|
|
|
|
let root_key_id = keychain.root_key_id();
|
2018-05-21 18:28:11 +03:00
|
|
|
let key_id_inner = key_id.clone();
|
|
|
|
let amount = slate.amount;
|
2018-05-24 18:27:26 +03:00
|
|
|
let height = slate.height;
|
2018-05-21 18:28:11 +03:00
|
|
|
|
2018-06-01 17:06:59 +03:00
|
|
|
let blinding =
|
|
|
|
slate.add_transaction_elements(&keychain, vec![build::output(amount, key_id.clone())])?;
|
2018-05-21 18:28:11 +03:00
|
|
|
|
|
|
|
// Add blinding sum to our context
|
2018-05-24 18:27:26 +03:00
|
|
|
let mut context = sigcontext::Context::new(
|
2018-05-21 18:28:11 +03:00
|
|
|
keychain.secp(),
|
2018-06-08 08:21:54 +03:00
|
|
|
blinding
|
|
|
|
.secret_key(wallet.keychain().clone().secp())
|
|
|
|
.unwrap(),
|
2018-05-21 18:28:11 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
context.add_output(&key_id);
|
|
|
|
|
|
|
|
// Create closure that adds the output to recipient's wallet
|
|
|
|
// (up to the caller to decide when to do)
|
2018-05-30 19:48:32 +03:00
|
|
|
let wallet_add_fn = move |wallet: &mut T| {
|
2018-06-22 11:08:06 +03:00
|
|
|
let mut batch = wallet.batch()?;
|
|
|
|
batch.save(OutputData {
|
|
|
|
root_key_id: root_key_id,
|
|
|
|
key_id: key_id_inner,
|
|
|
|
n_child: derivation,
|
|
|
|
value: amount,
|
|
|
|
status: OutputStatus::Unconfirmed,
|
|
|
|
height: height,
|
|
|
|
lock_height: 0,
|
|
|
|
is_coinbase: false,
|
|
|
|
});
|
|
|
|
batch.commit()?;
|
|
|
|
Ok(())
|
2018-05-21 18:28:11 +03:00
|
|
|
};
|
2018-05-24 18:27:26 +03:00
|
|
|
Ok((key_id, context, wallet_add_fn))
|
2018-05-21 18:28:11 +03:00
|
|
|
}
|
2018-05-16 15:18:09 +03:00
|
|
|
|
|
|
|
/// 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.
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn select_send_tx<T, K>(
|
2018-05-30 19:48:32 +03:00
|
|
|
wallet: &mut T,
|
2018-05-16 15:18:09 +03:00
|
|
|
amount: u64,
|
|
|
|
current_height: u64,
|
|
|
|
minimum_confirmations: u64,
|
|
|
|
lock_height: u64,
|
|
|
|
max_outputs: usize,
|
|
|
|
selection_strategy_is_use_all: bool,
|
|
|
|
) -> Result<
|
|
|
|
(
|
2018-06-08 08:21:54 +03:00
|
|
|
Vec<Box<build::Append<K>>>,
|
2018-05-16 15:18:09 +03:00
|
|
|
Vec<OutputData>,
|
2018-06-14 19:02:05 +03:00
|
|
|
u64, //change
|
|
|
|
Option<u32>, //change derivation
|
|
|
|
u64, // amount
|
|
|
|
u64, // fee
|
2018-05-16 15:18:09 +03:00
|
|
|
),
|
|
|
|
Error,
|
2018-05-30 19:48:32 +03:00
|
|
|
>
|
|
|
|
where
|
2018-06-08 08:21:54 +03:00
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: Keychain,
|
2018-05-30 19:48:32 +03:00
|
|
|
{
|
2018-06-08 08:21:54 +03:00
|
|
|
let key_id = wallet.keychain().root_key_id();
|
2018-05-16 15:18:09 +03:00
|
|
|
|
|
|
|
// select some spendable coins from the wallet
|
2018-06-22 11:08:06 +03:00
|
|
|
let mut coins = wallet.select_coins(
|
|
|
|
key_id.clone(),
|
|
|
|
amount,
|
|
|
|
current_height,
|
|
|
|
minimum_confirmations,
|
|
|
|
max_outputs,
|
|
|
|
selection_strategy_is_use_all,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get the maximum number of outputs in the wallet
|
|
|
|
let max_outputs = wallet
|
|
|
|
.select_coins(
|
2018-05-16 15:18:09 +03:00
|
|
|
key_id.clone(),
|
|
|
|
amount,
|
|
|
|
current_height,
|
|
|
|
minimum_confirmations,
|
|
|
|
max_outputs,
|
2018-06-22 11:08:06 +03:00
|
|
|
true,
|
|
|
|
)
|
2018-05-30 19:48:32 +03:00
|
|
|
.len();
|
2018-05-16 15:18:09 +03:00
|
|
|
|
|
|
|
// sender is responsible for setting the fee on the partial tx
|
|
|
|
// recipient should double check the fee calculation and not blindly trust the
|
|
|
|
// sender
|
|
|
|
let mut fee;
|
|
|
|
// First attempt to spend without change
|
2018-06-29 04:56:07 +03:00
|
|
|
fee = tx_fee(coins.len(), 1, None);
|
2018-05-16 15:18:09 +03:00
|
|
|
let mut total: u64 = coins.iter().map(|c| c.value).sum();
|
|
|
|
let mut amount_with_fee = amount + fee;
|
|
|
|
|
|
|
|
if total == 0 {
|
2018-06-01 17:06:59 +03:00
|
|
|
return Err(ErrorKind::NotEnoughFunds {
|
|
|
|
available: 0,
|
|
|
|
needed: amount_with_fee as u64,
|
|
|
|
})?;
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we need to use a change address
|
|
|
|
if total > amount_with_fee {
|
2018-06-29 04:56:07 +03:00
|
|
|
fee = tx_fee(coins.len(), 2, None);
|
2018-05-16 15:18:09 +03:00
|
|
|
amount_with_fee = amount + fee;
|
|
|
|
|
|
|
|
// Here check if we have enough outputs for the amount including fee otherwise
|
|
|
|
// look for other outputs and check again
|
|
|
|
while total < amount_with_fee {
|
|
|
|
// End the loop if we have selected all the outputs and still not enough funds
|
|
|
|
if coins.len() == max_outputs {
|
2018-06-01 17:06:59 +03:00
|
|
|
return Err(ErrorKind::NotEnoughFunds {
|
|
|
|
available: total as u64,
|
|
|
|
needed: amount_with_fee as u64,
|
|
|
|
})?;
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// select some spendable coins from the wallet
|
2018-06-22 11:08:06 +03:00
|
|
|
coins = wallet.select_coins(
|
|
|
|
key_id.clone(),
|
|
|
|
amount_with_fee,
|
|
|
|
current_height,
|
|
|
|
minimum_confirmations,
|
|
|
|
max_outputs,
|
|
|
|
selection_strategy_is_use_all,
|
|
|
|
);
|
2018-06-29 04:56:07 +03:00
|
|
|
fee = tx_fee(coins.len(), 2, None);
|
2018-05-16 15:18:09 +03:00
|
|
|
total = coins.iter().map(|c| c.value).sum();
|
|
|
|
amount_with_fee = amount + fee;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// build transaction skeleton with inputs and change
|
2018-06-22 11:08:06 +03:00
|
|
|
let (mut parts, change, change_derivation) = inputs_and_change(&coins, wallet, amount, fee)?;
|
2018-05-16 15:18:09 +03:00
|
|
|
|
|
|
|
// This is more proof of concept than anything but here we set lock_height
|
|
|
|
// on tx being sent (based on current chain height via api).
|
|
|
|
parts.push(build::with_lock_height(lock_height));
|
|
|
|
|
2018-06-14 19:02:05 +03:00
|
|
|
Ok((parts, coins, change, change_derivation, amount, fee))
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Selects inputs and change for a transaction
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn inputs_and_change<T, K>(
|
2018-05-16 15:18:09 +03:00
|
|
|
coins: &Vec<OutputData>,
|
2018-05-30 19:48:32 +03:00
|
|
|
wallet: &mut T,
|
2018-05-16 15:18:09 +03:00
|
|
|
amount: u64,
|
|
|
|
fee: u64,
|
2018-06-14 19:02:05 +03:00
|
|
|
) -> Result<(Vec<Box<build::Append<K>>>, u64, Option<u32>), Error>
|
2018-05-30 19:48:32 +03:00
|
|
|
where
|
2018-06-08 08:21:54 +03:00
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: Keychain,
|
2018-05-30 19:48:32 +03:00
|
|
|
{
|
2018-05-16 15:18:09 +03:00
|
|
|
let mut parts = vec![];
|
|
|
|
|
|
|
|
// calculate the total across all inputs, and how much is left
|
|
|
|
let total: u64 = coins.iter().map(|c| c.value).sum();
|
|
|
|
|
|
|
|
parts.push(build::with_fee(fee));
|
|
|
|
|
|
|
|
// if we are spending 10,000 coins to send 1,000 then our change will be 9,000
|
|
|
|
// if the fee is 80 then the recipient will receive 1000 and our change will be
|
|
|
|
// 8,920
|
|
|
|
let change = total - amount - fee;
|
|
|
|
|
|
|
|
// build inputs using the appropriate derived key_ids
|
|
|
|
for coin in coins {
|
2018-06-01 17:06:59 +03:00
|
|
|
let key_id = wallet.keychain().derive_key_id(coin.n_child)?;
|
2018-05-16 15:18:09 +03:00
|
|
|
if coin.is_coinbase {
|
|
|
|
parts.push(build::coinbase_input(
|
|
|
|
coin.value,
|
|
|
|
key_id,
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
parts.push(build::input(coin.value, key_id));
|
|
|
|
}
|
|
|
|
}
|
2018-06-14 19:02:05 +03:00
|
|
|
let mut change_derivation = None;
|
2018-05-16 15:18:09 +03:00
|
|
|
if change != 0 {
|
2018-06-22 11:08:06 +03:00
|
|
|
let keychain = wallet.keychain().clone();
|
|
|
|
let root_key_id = keychain.root_key_id();
|
|
|
|
change_derivation = Some(wallet.next_child(root_key_id.clone()).unwrap());
|
|
|
|
let change_k = keychain.derive_key_id(change_derivation.unwrap()).unwrap();
|
2018-05-16 15:18:09 +03:00
|
|
|
|
2018-06-22 11:08:06 +03:00
|
|
|
parts.push(build::output(change, change_k.clone()));
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|
|
|
|
|
2018-06-14 19:02:05 +03:00
|
|
|
Ok((parts, change, change_derivation))
|
2018-05-16 15:18:09 +03:00
|
|
|
}
|