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-06-07 17:04:21 +03:00
|
|
|
//! Transaction building functions
|
2018-06-06 17:36:29 +03:00
|
|
|
|
2018-06-07 17:04:21 +03:00
|
|
|
use core::core::Transaction;
|
2018-05-16 15:18:09 +03:00
|
|
|
use keychain::{Identifier, Keychain};
|
2018-06-06 17:36:29 +03:00
|
|
|
use libtx::slate::Slate;
|
2018-05-30 19:48:32 +03:00
|
|
|
use libtx::{build, tx_fee};
|
2018-06-07 17:04:21 +03:00
|
|
|
use libwallet::internal::{selection, sigcontext, updater};
|
|
|
|
use libwallet::types::{WalletBackend, WalletClient};
|
2018-06-06 17:36:29 +03:00
|
|
|
use libwallet::{Error, ErrorKind};
|
2018-05-21 18:28:11 +03:00
|
|
|
use util::LOGGER;
|
2017-05-25 02:08:39 +03:00
|
|
|
|
2018-06-06 17:36:29 +03:00
|
|
|
/// Receive a tranaction, modifying the slate accordingly (which can then be
|
|
|
|
/// sent back to sender for posting)
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn receive_tx<T, K>(wallet: &mut T, slate: &mut Slate) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: Keychain,
|
|
|
|
{
|
2018-06-06 17:36:29 +03:00
|
|
|
// create an output using the amount in the slate
|
|
|
|
let (_, mut context, receiver_create_fn) =
|
|
|
|
selection::build_recipient_output_with_slate(wallet, slate).unwrap();
|
|
|
|
|
|
|
|
// fill public keys
|
|
|
|
let _ = slate.fill_round_1(
|
|
|
|
wallet.keychain(),
|
|
|
|
&mut context.sec_key,
|
|
|
|
&context.sec_nonce,
|
|
|
|
1,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// perform partial sig
|
|
|
|
let _ = slate.fill_round_2(wallet.keychain(), &context.sec_key, &context.sec_nonce, 1)?;
|
|
|
|
|
|
|
|
// Save output in wallet
|
|
|
|
let _ = receiver_create_fn(wallet);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-06-08 04:12:15 +03:00
|
|
|
/// Issue a new transaction to the provided sender by spending some of our
|
|
|
|
/// wallet
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn create_send_tx<T, K>(
|
2018-05-30 19:48:32 +03:00
|
|
|
wallet: &mut T,
|
2017-09-29 21:44:25 +03:00
|
|
|
amount: u64,
|
2017-10-18 23:47:37 +03:00
|
|
|
minimum_confirmations: u64,
|
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-06-07 17:04:21 +03:00
|
|
|
) -> Result<
|
|
|
|
(
|
|
|
|
Slate,
|
|
|
|
sigcontext::Context,
|
|
|
|
impl FnOnce(&mut T) -> Result<(), Error>,
|
|
|
|
),
|
|
|
|
Error,
|
2018-06-08 08:21:54 +03:00
|
|
|
>
|
|
|
|
where
|
|
|
|
T: WalletBackend<K> + WalletClient,
|
|
|
|
K: Keychain,
|
|
|
|
{
|
2018-05-16 15:18:09 +03:00
|
|
|
// Get lock height
|
2018-06-07 17:04:21 +03:00
|
|
|
let current_height = wallet.get_chain_height(wallet.node_url())?;
|
2018-05-16 15:18:09 +03:00
|
|
|
// ensure outputs we're selecting are up to date
|
2018-06-01 17:06:59 +03:00
|
|
|
updater::refresh_outputs(wallet)?;
|
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-24 18:27:26 +03:00
|
|
|
// Sender selects outputs into a new slate and save our corresponding keyss in
|
|
|
|
// a transaction context. The secret key in our transaction context will be
|
2018-05-21 18:28:11 +03:00
|
|
|
// randomly selected. This returns the public slate, and a closure that locks
|
|
|
|
// our inputs and outputs once we're convinced the transaction exchange went
|
|
|
|
// according to plan
|
|
|
|
// This function is just a big helper to do all of that, in theory
|
|
|
|
// this process can be split up in any way
|
2018-05-24 18:27:26 +03:00
|
|
|
let (mut slate, mut context, sender_lock_fn) = selection::build_send_tx_slate(
|
2018-05-30 19:48:32 +03:00
|
|
|
wallet,
|
2018-05-21 18:28:11 +03:00
|
|
|
2,
|
2017-10-18 23:47:37 +03:00
|
|
|
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,
|
2018-06-01 17:06:59 +03:00
|
|
|
)?;
|
2018-03-04 03:19:54 +03:00
|
|
|
|
2018-05-21 18:28:11 +03:00
|
|
|
// Generate a kernel offset and subtract from our context's secret key. Store
|
|
|
|
// the offset in the slate's transaction kernel, and adds our public key
|
|
|
|
// information to the slate
|
2018-06-01 17:06:59 +03:00
|
|
|
let _ = slate.fill_round_1(
|
|
|
|
wallet.keychain(),
|
|
|
|
&mut context.sec_key,
|
|
|
|
&context.sec_nonce,
|
|
|
|
0,
|
|
|
|
)?;
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-06-07 17:04:21 +03:00
|
|
|
Ok((slate, context, sender_lock_fn))
|
|
|
|
}
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-06-07 17:04:21 +03:00
|
|
|
/// Complete a transaction as the sender
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn complete_tx<T, K>(
|
2018-06-07 17:04:21 +03:00
|
|
|
wallet: &mut T,
|
|
|
|
slate: &mut Slate,
|
|
|
|
context: &sigcontext::Context,
|
2018-06-08 08:21:54 +03:00
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
T: WalletBackend<K>,
|
|
|
|
K: Keychain,
|
|
|
|
{
|
2018-06-01 17:06:59 +03:00
|
|
|
let _ = slate.fill_round_2(wallet.keychain(), &context.sec_key, &context.sec_nonce, 0)?;
|
2018-05-21 18:28:11 +03:00
|
|
|
// Final transaction can be built by anyone at this stage
|
2018-06-07 17:04:21 +03:00
|
|
|
let res = slate.finalize(wallet.keychain());
|
|
|
|
if let Err(e) = res {
|
|
|
|
Err(ErrorKind::LibTX(e.kind()))?
|
2018-05-21 18:28:11 +03:00
|
|
|
}
|
2017-05-25 02:08:39 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-06-06 17:36:29 +03:00
|
|
|
/// Issue a burn tx
|
2018-06-08 08:21:54 +03:00
|
|
|
pub fn issue_burn_tx<T, K>(
|
2018-05-30 19:48:32 +03:00
|
|
|
wallet: &mut T,
|
2017-10-18 23:47:37 +03:00
|
|
|
amount: u64,
|
|
|
|
minimum_confirmations: u64,
|
2017-11-15 21:56:35 +03:00
|
|
|
max_outputs: usize,
|
2018-06-08 08:21:54 +03:00
|
|
|
) -> Result<Transaction, Error>
|
|
|
|
where
|
|
|
|
T: WalletBackend<K> + WalletClient,
|
|
|
|
K: Keychain,
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
// let keychain = &Keychain::burn_enabled(wallet.keychain(),
|
|
|
|
// &Identifier::zero());
|
|
|
|
let keychain = wallet.keychain().clone();
|
2017-10-16 20:11:01 +03:00
|
|
|
|
2018-06-07 17:04:21 +03:00
|
|
|
let current_height = wallet.get_chain_height(wallet.node_url())?;
|
2017-10-18 23:47:37 +03:00
|
|
|
|
2018-06-01 17:06:59 +03:00
|
|
|
let _ = updater::refresh_outputs(wallet);
|
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
|
2018-05-30 19:48:32 +03:00
|
|
|
let coins = wallet.read_wallet(|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-30 19:48:32 +03:00
|
|
|
let fee = tx_fee(coins.len(), 2, selection::coins_proof_count(&coins), None);
|
|
|
|
let (mut parts, _) = selection::inputs_and_change(&coins, wallet, current_height, 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-06-01 17:06:59 +03:00
|
|
|
let tx_burn = build::transaction(parts, &keychain)?;
|
|
|
|
tx_burn.validate()?;
|
2018-06-07 17:04:21 +03:00
|
|
|
Ok(tx_burn)
|
2017-10-26 00:09:34 +03:00
|
|
|
}
|
2017-10-12 06:35:40 +03:00
|
|
|
|
2017-08-23 02:05:56 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2018-06-08 08:21:54 +03:00
|
|
|
use keychain::{ExtKeychain, Keychain};
|
2018-05-30 19:48:32 +03:00
|
|
|
use libtx::build;
|
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() {
|
2018-06-08 08:21:54 +03:00
|
|
|
let keychain = ExtKeychain::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
|
|
|
}
|
|
|
|
}
|