2018-03-05 22:33:44 +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.
|
|
|
|
|
|
|
|
//! Provides the JSON/HTTP API for wallets to receive payments. Because
|
|
|
|
//! receiving money in MimbleWimble requires an interactive exchange, a
|
|
|
|
//! wallet server that's running at all time is required in many cases.
|
2017-11-01 02:32:33 +03:00
|
|
|
|
2017-11-01 21:32:34 +03:00
|
|
|
use bodyparser;
|
|
|
|
use iron::prelude::*;
|
|
|
|
use iron::Handler;
|
|
|
|
use iron::status;
|
|
|
|
use serde_json;
|
2018-02-06 14:42:26 +03:00
|
|
|
use uuid::Uuid;
|
2017-05-25 02:08:39 +03:00
|
|
|
|
2017-11-01 21:32:34 +03:00
|
|
|
use api;
|
2017-10-05 10:23:04 +03:00
|
|
|
use core::consensus::reward;
|
2018-03-04 03:19:54 +03:00
|
|
|
use core::core::{amount_to_hr_string, build, Block, Committed, Output, Transaction, TxKernel};
|
2018-01-17 06:03:40 +03:00
|
|
|
use core::{global, ser};
|
2018-03-04 03:19:54 +03:00
|
|
|
use keychain::{BlindingFactor, Identifier, Keychain};
|
2017-05-25 02:08:39 +03:00
|
|
|
use types::*;
|
2018-03-04 03:19:54 +03:00
|
|
|
use util::{secp, to_hex, LOGGER};
|
|
|
|
use failure::ResultExt;
|
2017-05-25 02:08:39 +03:00
|
|
|
|
2017-06-09 02:34:27 +03:00
|
|
|
/// Dummy wrapper for the hex-encoded serialized transaction.
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2017-10-12 06:35:40 +03:00
|
|
|
pub struct TxWrapper {
|
|
|
|
pub tx_hex: String,
|
2017-06-09 02:34:27 +03:00
|
|
|
}
|
|
|
|
|
2018-01-10 22:36:27 +03:00
|
|
|
/// Receive Part 1 of interactive transactions from sender, Sender Initiation
|
|
|
|
/// Return result of part 2, Recipient Initation, to sender
|
|
|
|
/// -Receiver receives inputs, outputs xS * G and kS * G
|
|
|
|
/// -Receiver picks random blinding factors for all outputs being received, computes total blinding
|
|
|
|
/// excess xR
|
|
|
|
/// -Receiver picks random nonce kR
|
|
|
|
/// -Receiver computes Schnorr challenge e = H(M | kR * G + kS * G)
|
|
|
|
/// -Receiver computes their part of signature, sR = kR + e * xR
|
|
|
|
/// -Receiver responds with sR, blinding excess xR * G, public nonce kR * G
|
|
|
|
|
|
|
|
fn handle_sender_initiation(
|
2017-11-01 21:32:34 +03:00
|
|
|
config: &WalletConfig,
|
|
|
|
keychain: &Keychain,
|
2018-03-04 03:19:54 +03:00
|
|
|
partial_tx: &PartialTx,
|
2018-01-10 22:36:27 +03:00
|
|
|
) -> Result<PartialTx, Error> {
|
2018-03-04 03:19:54 +03:00
|
|
|
let (amount, _sender_pub_blinding, sender_pub_nonce, kernel_offset, _sig, tx) =
|
|
|
|
read_partial_tx(keychain, partial_tx)?;
|
2018-01-10 22:36:27 +03:00
|
|
|
|
|
|
|
let root_key_id = keychain.root_key_id();
|
|
|
|
|
|
|
|
// double check the fee amount included in the partial tx
|
|
|
|
// we don't necessarily want to just trust the sender
|
|
|
|
// we could just overwrite the fee here (but we won't) due to the ecdsa sig
|
|
|
|
let fee = tx_fee(tx.inputs.len(), tx.outputs.len() + 1, None);
|
2018-02-13 18:35:30 +03:00
|
|
|
if fee != tx.fee() {
|
2018-02-28 20:56:09 +03:00
|
|
|
return Err(ErrorKind::FeeDispute {
|
2018-02-13 18:35:30 +03:00
|
|
|
sender_fee: tx.fee(),
|
2018-01-10 22:36:27 +03:00
|
|
|
recipient_fee: fee,
|
2018-02-28 20:56:09 +03:00
|
|
|
})?;
|
2018-01-10 22:36:27 +03:00
|
|
|
}
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
if fee > amount {
|
2018-01-12 15:44:15 +03:00
|
|
|
info!(
|
2018-01-17 06:03:40 +03:00
|
|
|
LOGGER,
|
2018-01-12 15:44:15 +03:00
|
|
|
"Rejected the transfer because transaction fee ({}) exceeds received amount ({}).",
|
|
|
|
amount_to_hr_string(fee),
|
|
|
|
amount_to_hr_string(amount)
|
|
|
|
);
|
2018-03-04 03:19:54 +03:00
|
|
|
return Err(ErrorKind::FeeExceedsAmount {
|
|
|
|
sender_amount: amount,
|
|
|
|
recipient_fee: fee,
|
|
|
|
})?;
|
|
|
|
}
|
2018-01-12 15:44:15 +03:00
|
|
|
|
2018-01-10 22:36:27 +03:00
|
|
|
let out_amount = amount - fee;
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
// First step is just to get the excess sum of the outputs we're participating
|
|
|
|
// in Output and key needs to be stored until transaction finalisation time,
|
|
|
|
// somehow
|
2018-01-10 22:36:27 +03:00
|
|
|
|
|
|
|
let key_id = WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
|
|
|
let (key_id, derivation) = next_available_key(&wallet_data, keychain);
|
|
|
|
|
|
|
|
wallet_data.add_output(OutputData {
|
|
|
|
root_key_id: root_key_id.clone(),
|
|
|
|
key_id: key_id.clone(),
|
|
|
|
n_child: derivation,
|
|
|
|
value: out_amount,
|
|
|
|
status: OutputStatus::Unconfirmed,
|
|
|
|
height: 0,
|
|
|
|
lock_height: 0,
|
|
|
|
is_coinbase: false,
|
2018-03-02 23:47:27 +03:00
|
|
|
block: None,
|
|
|
|
merkle_proof: None,
|
2018-01-10 22:36:27 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
key_id
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// Still handy for getting the blinding sum
|
2018-03-04 03:19:54 +03:00
|
|
|
let (_, blind_sum) =
|
|
|
|
build::partial_transaction(vec![build::output(out_amount, key_id.clone())], keychain)
|
|
|
|
.context(ErrorKind::Keychain)?;
|
2018-01-10 22:36:27 +03:00
|
|
|
|
|
|
|
warn!(LOGGER, "Creating new aggsig context");
|
|
|
|
// Create a new aggsig context
|
|
|
|
// this will create a new blinding sum and nonce, and store them
|
2018-03-04 03:19:54 +03:00
|
|
|
let blind = blind_sum
|
|
|
|
.secret_key(&keychain.secp())
|
|
|
|
.context(ErrorKind::Keychain)?;
|
|
|
|
keychain
|
|
|
|
.aggsig_create_context(&partial_tx.id, blind)
|
|
|
|
.context(ErrorKind::Keychain)?;
|
2018-02-06 14:42:26 +03:00
|
|
|
keychain.aggsig_add_output(&partial_tx.id, &key_id);
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
let sig_part = keychain
|
|
|
|
.aggsig_calculate_partial_sig(&partial_tx.id, &sender_pub_nonce, fee, tx.lock_height())
|
|
|
|
.unwrap();
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
// Build the response, which should contain sR, blinding excess xR * G, public
|
|
|
|
// nonce kR * G
|
|
|
|
let mut partial_tx = build_partial_tx(
|
|
|
|
&partial_tx.id,
|
|
|
|
keychain,
|
|
|
|
amount,
|
|
|
|
kernel_offset,
|
|
|
|
Some(sig_part),
|
|
|
|
tx,
|
|
|
|
);
|
2018-01-10 22:36:27 +03:00
|
|
|
partial_tx.phase = PartialTxPhase::ReceiverInitiation;
|
|
|
|
|
|
|
|
Ok(partial_tx)
|
2017-11-01 21:32:34 +03:00
|
|
|
}
|
|
|
|
|
2018-01-10 22:36:27 +03:00
|
|
|
/// Receive Part 3 of interactive transactions from sender, Sender Confirmation
|
|
|
|
/// Return Ok/Error
|
|
|
|
/// -Receiver receives sS
|
|
|
|
/// -Receiver verifies sender's sig, by verifying that kS * G + e *xS * G = sS * G
|
|
|
|
/// -Receiver calculates final sig as s=(sS+sR, kS * G+kR * G)
|
|
|
|
/// -Receiver puts into TX kernel:
|
|
|
|
///
|
|
|
|
/// Signature S
|
|
|
|
/// pubkey xR * G+xS * G
|
|
|
|
/// fee (= M)
|
|
|
|
/// -Receiver sends completed TX to mempool. responds OK to sender
|
|
|
|
|
|
|
|
fn handle_sender_confirmation(
|
2017-10-27 20:36:03 +03:00
|
|
|
config: &WalletConfig,
|
|
|
|
keychain: &Keychain,
|
2018-03-04 03:19:54 +03:00
|
|
|
partial_tx: &PartialTx,
|
2018-01-10 22:36:27 +03:00
|
|
|
) -> Result<PartialTx, Error> {
|
2018-03-04 03:19:54 +03:00
|
|
|
let (amount, sender_pub_blinding, sender_pub_nonce, kernel_offset, sender_sig_part, tx) =
|
|
|
|
read_partial_tx(keychain, partial_tx)?;
|
2018-02-13 18:35:30 +03:00
|
|
|
let sender_sig_part = sender_sig_part.unwrap();
|
|
|
|
let res = keychain.aggsig_verify_partial_sig(
|
|
|
|
&partial_tx.id,
|
|
|
|
&sender_sig_part,
|
|
|
|
&sender_pub_nonce,
|
|
|
|
&sender_pub_blinding,
|
2018-03-04 03:19:54 +03:00
|
|
|
tx.fee(),
|
|
|
|
tx.lock_height(),
|
2018-02-13 18:35:30 +03:00
|
|
|
);
|
2018-01-10 22:36:27 +03:00
|
|
|
|
|
|
|
if !res {
|
|
|
|
error!(LOGGER, "Partial Sig from sender invalid.");
|
2018-02-28 20:56:09 +03:00
|
|
|
return Err(ErrorKind::Signature("Partial Sig from sender invalid."))?;
|
2018-01-10 22:36:27 +03:00
|
|
|
}
|
2018-01-07 06:56:27 +03:00
|
|
|
|
2018-02-13 18:35:30 +03:00
|
|
|
// Just calculate our sig part again instead of storing
|
2018-03-04 03:19:54 +03:00
|
|
|
let our_sig_part = keychain
|
|
|
|
.aggsig_calculate_partial_sig(
|
|
|
|
&partial_tx.id,
|
|
|
|
&sender_pub_nonce,
|
|
|
|
tx.fee(),
|
|
|
|
tx.lock_height(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2017-06-09 02:34:27 +03:00
|
|
|
|
2018-01-10 22:36:27 +03:00
|
|
|
// And the final signature
|
2018-03-04 03:19:54 +03:00
|
|
|
let final_sig = keychain
|
|
|
|
.aggsig_calculate_final_sig(
|
|
|
|
&partial_tx.id,
|
|
|
|
&sender_sig_part,
|
|
|
|
&our_sig_part,
|
|
|
|
&sender_pub_nonce,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-01-10 22:36:27 +03:00
|
|
|
|
|
|
|
// Calculate the final public key (for our own sanity check)
|
2018-03-04 03:19:54 +03:00
|
|
|
let final_pubkey = keychain
|
|
|
|
.aggsig_calculate_final_pubkey(&partial_tx.id, &sender_pub_blinding)
|
|
|
|
.unwrap();
|
2018-02-13 18:35:30 +03:00
|
|
|
|
|
|
|
// Check our final sig verifies
|
|
|
|
let res = keychain.aggsig_verify_final_sig_build_msg(
|
|
|
|
&final_sig,
|
|
|
|
&final_pubkey,
|
|
|
|
tx.fee(),
|
|
|
|
tx.lock_height(),
|
|
|
|
);
|
2018-01-10 22:36:27 +03:00
|
|
|
|
|
|
|
if !res {
|
|
|
|
error!(LOGGER, "Final aggregated signature invalid.");
|
2018-02-28 20:56:09 +03:00
|
|
|
return Err(ErrorKind::Signature("Final aggregated signature invalid."))?;
|
2018-01-07 06:56:27 +03:00
|
|
|
}
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-02-13 18:35:30 +03:00
|
|
|
let final_tx = build_final_transaction(
|
|
|
|
&partial_tx.id,
|
|
|
|
config,
|
|
|
|
keychain,
|
|
|
|
amount,
|
|
|
|
kernel_offset,
|
|
|
|
&final_sig,
|
|
|
|
tx.clone(),
|
|
|
|
)?;
|
|
|
|
|
2018-01-10 22:36:27 +03:00
|
|
|
let tx_hex = to_hex(ser::ser_vec(&final_tx).unwrap());
|
|
|
|
|
|
|
|
let url = format!("{}/v1/pool/push", config.check_node_api_http_addr.as_str());
|
2018-03-04 03:19:54 +03:00
|
|
|
api::client::post(url.as_str(), &TxWrapper { tx_hex: tx_hex }).context(ErrorKind::Node)?;
|
2018-01-10 22:36:27 +03:00
|
|
|
|
|
|
|
// Return what we've actually posted
|
2018-02-13 18:35:30 +03:00
|
|
|
// TODO - why build_partial_tx here? Just a naming issue?
|
2018-03-04 03:19:54 +03:00
|
|
|
let mut partial_tx = build_partial_tx(
|
|
|
|
&partial_tx.id,
|
|
|
|
keychain,
|
|
|
|
amount,
|
|
|
|
kernel_offset,
|
|
|
|
Some(final_sig),
|
|
|
|
tx,
|
|
|
|
);
|
2018-01-10 22:36:27 +03:00
|
|
|
partial_tx.phase = PartialTxPhase::ReceiverConfirmation;
|
|
|
|
Ok(partial_tx)
|
2017-06-08 04:12:15 +03:00
|
|
|
}
|
|
|
|
|
2017-05-25 02:08:39 +03:00
|
|
|
/// Component used to receive coins, implements all the receiving end of the
|
|
|
|
/// wallet REST API as well as some of the command-line operations.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct WalletReceiver {
|
2017-10-03 03:02:31 +03:00
|
|
|
pub keychain: Keychain,
|
2017-06-16 19:47:29 +03:00
|
|
|
pub config: WalletConfig,
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
impl Handler for WalletReceiver {
|
|
|
|
fn handle(&self, req: &mut Request) -> IronResult<Response> {
|
2017-11-09 23:42:19 +03:00
|
|
|
let struct_body = req.get::<bodyparser::Struct<PartialTx>>();
|
2017-11-01 02:32:33 +03:00
|
|
|
|
2017-11-01 21:32:34 +03:00
|
|
|
if let Ok(Some(partial_tx)) = struct_body {
|
2018-01-10 22:36:27 +03:00
|
|
|
match partial_tx.phase {
|
|
|
|
PartialTxPhase::SenderInitiation => {
|
2018-03-04 03:19:54 +03:00
|
|
|
let resp_tx = handle_sender_initiation(
|
|
|
|
&self.config,
|
|
|
|
&self.keychain,
|
|
|
|
&partial_tx,
|
|
|
|
).map_err(|e| {
|
2018-01-10 22:36:27 +03:00
|
|
|
error!(LOGGER, "Phase 1 Sender Initiation -> Problematic partial tx, looks like this: {:?}", partial_tx);
|
2018-03-04 03:19:54 +03:00
|
|
|
api::Error::Internal(format!(
|
|
|
|
"Error processing partial transaction: {:?}",
|
|
|
|
e
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.unwrap();
|
2018-01-10 22:36:27 +03:00
|
|
|
let json = serde_json::to_string(&resp_tx).unwrap();
|
|
|
|
Ok(Response::with((status::Ok, json)))
|
2018-03-04 03:19:54 +03:00
|
|
|
}
|
2018-01-10 22:36:27 +03:00
|
|
|
PartialTxPhase::SenderConfirmation => {
|
2018-03-04 03:19:54 +03:00
|
|
|
let resp_tx = handle_sender_confirmation(
|
|
|
|
&self.config,
|
|
|
|
&self.keychain,
|
|
|
|
&partial_tx,
|
|
|
|
).map_err(|e| {
|
2018-01-10 22:36:27 +03:00
|
|
|
error!(LOGGER, "Phase 3 Sender Confirmation -> Problematic partial tx, looks like this: {:?}", partial_tx);
|
2018-03-04 03:19:54 +03:00
|
|
|
api::Error::Internal(format!(
|
|
|
|
"Error processing partial transaction: {:?}",
|
|
|
|
e
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.unwrap();
|
2018-01-10 22:36:27 +03:00
|
|
|
let json = serde_json::to_string(&resp_tx).unwrap();
|
|
|
|
Ok(Response::with((status::Ok, json)))
|
2018-03-04 03:19:54 +03:00
|
|
|
}
|
|
|
|
_ => {
|
2018-01-10 22:36:27 +03:00
|
|
|
error!(LOGGER, "Unhandled Phase: {:?}", partial_tx);
|
|
|
|
Ok(Response::with((status::BadRequest, "Unhandled Phase")))
|
|
|
|
}
|
|
|
|
}
|
2017-11-01 21:32:34 +03:00
|
|
|
} else {
|
|
|
|
Ok(Response::with((status::BadRequest, "")))
|
2017-05-29 06:21:29 +03:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
fn retrieve_existing_key(wallet_data: &WalletData, key_id: Identifier) -> (Identifier, u32) {
|
2017-11-18 10:31:02 +03:00
|
|
|
if let Some(existing) = wallet_data.get_output(&key_id) {
|
|
|
|
let key_id = existing.key_id.clone();
|
|
|
|
let derivation = existing.n_child;
|
|
|
|
(key_id, derivation)
|
|
|
|
} else {
|
|
|
|
panic!("should never happen");
|
|
|
|
}
|
2017-10-26 00:09:34 +03:00
|
|
|
}
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
fn next_available_key(wallet_data: &WalletData, keychain: &Keychain) -> (Identifier, u32) {
|
2017-11-18 10:31:02 +03:00
|
|
|
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)
|
2017-10-26 00:09:34 +03:00
|
|
|
}
|
|
|
|
|
2017-05-25 02:08:39 +03:00
|
|
|
/// Build a coinbase output and the corresponding kernel
|
2017-10-27 20:36:03 +03:00
|
|
|
pub fn receive_coinbase(
|
2017-10-18 23:47:37 +03:00
|
|
|
config: &WalletConfig,
|
|
|
|
keychain: &Keychain,
|
2017-11-01 02:32:33 +03:00
|
|
|
block_fees: &BlockFees,
|
2017-10-18 23:47:37 +03:00
|
|
|
) -> Result<(Output, TxKernel, BlockFees), Error> {
|
2017-10-13 07:45:07 +03:00
|
|
|
let root_key_id = keychain.root_key_id();
|
2017-06-15 07:42:58 +03:00
|
|
|
|
2018-01-17 06:03:40 +03:00
|
|
|
let height = block_fees.height;
|
|
|
|
let lock_height = height + global::coinbase_maturity();
|
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
// Now acquire the wallet lock and write the new output.
|
2017-11-18 10:31:02 +03:00
|
|
|
let (key_id, derivation) = WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
|
|
|
let key_id = block_fees.key_id();
|
|
|
|
let (key_id, derivation) = match key_id {
|
|
|
|
Some(key_id) => retrieve_existing_key(&wallet_data, key_id),
|
|
|
|
None => next_available_key(&wallet_data, keychain),
|
|
|
|
};
|
|
|
|
|
2017-06-15 07:42:58 +03:00
|
|
|
// track the new output and return the stuff needed for reward
|
2017-10-06 23:10:30 +03:00
|
|
|
wallet_data.add_output(OutputData {
|
2017-10-13 07:45:07 +03:00
|
|
|
root_key_id: root_key_id.clone(),
|
|
|
|
key_id: key_id.clone(),
|
2017-10-03 03:02:31 +03:00
|
|
|
n_child: derivation,
|
2017-10-07 20:38:41 +03:00
|
|
|
value: reward(block_fees.fees),
|
2017-06-15 07:42:58 +03:00
|
|
|
status: OutputStatus::Unconfirmed,
|
2018-01-17 06:03:40 +03:00
|
|
|
height: height,
|
|
|
|
lock_height: lock_height,
|
2017-10-18 23:47:37 +03:00
|
|
|
is_coinbase: true,
|
2018-03-02 23:47:27 +03:00
|
|
|
block: None,
|
|
|
|
merkle_proof: None,
|
2017-06-15 07:42:58 +03:00
|
|
|
});
|
2017-11-18 10:31:02 +03:00
|
|
|
|
|
|
|
(key_id, derivation)
|
2017-10-26 00:09:34 +03:00
|
|
|
})?;
|
2017-10-07 20:38:41 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
debug!(
|
|
|
|
LOGGER,
|
2018-01-17 06:03:40 +03:00
|
|
|
"receive_coinbase: built candidate output - {:?}, {}",
|
2017-10-26 00:09:34 +03:00
|
|
|
key_id.clone(),
|
|
|
|
derivation,
|
|
|
|
);
|
2017-06-15 07:42:58 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
let mut block_fees = block_fees.clone();
|
|
|
|
block_fees.key_id = Some(key_id.clone());
|
2017-10-07 20:38:41 +03:00
|
|
|
|
2018-01-17 06:03:40 +03:00
|
|
|
debug!(LOGGER, "receive_coinbase: {:?}", block_fees);
|
2017-10-07 20:38:41 +03:00
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
let (out, kern) = Block::reward_output(&keychain, &key_id, block_fees.fees, block_fees.height)
|
|
|
|
.context(ErrorKind::Keychain)?;
|
2017-10-26 00:09:34 +03:00
|
|
|
Ok((out, kern, block_fees))
|
2017-06-08 04:12:15 +03:00
|
|
|
}
|
|
|
|
|
2018-01-10 22:36:27 +03:00
|
|
|
/// builds a final transaction after the aggregated sig exchange
|
|
|
|
fn build_final_transaction(
|
2018-02-06 14:42:26 +03:00
|
|
|
tx_id: &Uuid,
|
2017-09-29 21:44:25 +03:00
|
|
|
config: &WalletConfig,
|
2017-10-03 03:02:31 +03:00
|
|
|
keychain: &Keychain,
|
2017-09-29 21:44:25 +03:00
|
|
|
amount: u64,
|
2018-02-13 18:35:30 +03:00
|
|
|
kernel_offset: BlindingFactor,
|
2018-01-10 22:36:27 +03:00
|
|
|
excess_sig: &secp::Signature,
|
|
|
|
tx: Transaction,
|
|
|
|
) -> Result<Transaction, Error> {
|
2017-10-13 07:45:07 +03:00
|
|
|
let root_key_id = keychain.root_key_id();
|
2017-06-08 04:12:15 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
// double check the fee amount included in the partial tx
|
2018-01-17 06:03:40 +03:00
|
|
|
// we don't necessarily want to just trust the sender
|
|
|
|
// we could just overwrite the fee here (but we won't) due to the ecdsa sig
|
2018-01-10 22:36:27 +03:00
|
|
|
let fee = tx_fee(tx.inputs.len(), tx.outputs.len() + 1, None);
|
2018-02-13 18:35:30 +03:00
|
|
|
if fee != tx.fee() {
|
2018-02-28 20:56:09 +03:00
|
|
|
return Err(ErrorKind::FeeDispute {
|
2018-02-13 18:35:30 +03:00
|
|
|
sender_fee: tx.fee(),
|
2017-10-26 00:09:34 +03:00
|
|
|
recipient_fee: fee,
|
2018-02-28 20:56:09 +03:00
|
|
|
})?;
|
2017-10-26 00:09:34 +03:00
|
|
|
}
|
2017-10-10 20:30:34 +03:00
|
|
|
|
2018-02-13 18:35:30 +03:00
|
|
|
if fee > amount {
|
2018-01-12 15:44:15 +03:00
|
|
|
info!(
|
2018-01-17 06:03:40 +03:00
|
|
|
LOGGER,
|
2018-01-12 15:44:15 +03:00
|
|
|
"Rejected the transfer because transaction fee ({}) exceeds received amount ({}).",
|
|
|
|
amount_to_hr_string(fee),
|
|
|
|
amount_to_hr_string(amount)
|
|
|
|
);
|
2018-03-04 03:19:54 +03:00
|
|
|
return Err(ErrorKind::FeeExceedsAmount {
|
|
|
|
sender_amount: amount,
|
|
|
|
recipient_fee: fee,
|
|
|
|
})?;
|
|
|
|
}
|
2018-01-12 15:44:15 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
let out_amount = amount - fee;
|
2017-10-02 00:56:19 +03:00
|
|
|
|
2018-01-10 22:36:27 +03:00
|
|
|
// Get output we created in earlier step
|
|
|
|
// TODO: will just be one for now, support multiple later
|
2018-02-06 14:42:26 +03:00
|
|
|
let output_vec = keychain.aggsig_get_outputs(tx_id);
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2017-11-18 10:31:02 +03:00
|
|
|
// operate within a lock on wallet data
|
|
|
|
let (key_id, derivation) = WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
2018-01-10 22:36:27 +03:00
|
|
|
let (key_id, derivation) = retrieve_existing_key(&wallet_data, output_vec[0].clone());
|
2017-11-18 10:31:02 +03:00
|
|
|
|
|
|
|
wallet_data.add_output(OutputData {
|
|
|
|
root_key_id: root_key_id.clone(),
|
|
|
|
key_id: key_id.clone(),
|
|
|
|
n_child: derivation,
|
|
|
|
value: out_amount,
|
|
|
|
status: OutputStatus::Unconfirmed,
|
|
|
|
height: 0,
|
|
|
|
lock_height: 0,
|
|
|
|
is_coinbase: false,
|
2018-03-02 23:47:27 +03:00
|
|
|
block: None,
|
|
|
|
merkle_proof: None,
|
2017-11-18 10:31:02 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
(key_id, derivation)
|
|
|
|
})?;
|
|
|
|
|
2018-01-10 22:36:27 +03:00
|
|
|
// Build final transaction, the sum of which should
|
|
|
|
// be the same as the exchanged excess values
|
2018-02-13 18:35:30 +03:00
|
|
|
let mut final_tx = build::transaction(
|
2017-11-01 02:32:33 +03:00
|
|
|
vec![
|
2018-01-10 22:36:27 +03:00
|
|
|
build::initial_tx(tx),
|
2017-11-01 02:32:33 +03:00
|
|
|
build::output(out_amount, key_id.clone()),
|
2018-02-13 18:35:30 +03:00
|
|
|
build::with_offset(kernel_offset),
|
2017-11-01 02:32:33 +03:00
|
|
|
],
|
|
|
|
keychain,
|
2018-02-28 20:56:09 +03:00
|
|
|
).context(ErrorKind::Keychain)?;
|
2017-06-13 02:41:27 +03:00
|
|
|
|
2018-02-13 18:35:30 +03:00
|
|
|
// build the final excess based on final tx and offset
|
|
|
|
let final_excess = {
|
|
|
|
// sum the input/output commitments on the final tx
|
2018-02-28 20:56:09 +03:00
|
|
|
let tx_excess = final_tx.sum_commitments().context(ErrorKind::Transaction)?;
|
2018-02-13 18:35:30 +03:00
|
|
|
|
|
|
|
// subtract the kernel_excess (built from kernel_offset)
|
2018-03-04 03:19:54 +03:00
|
|
|
let offset_excess = keychain
|
|
|
|
.secp()
|
|
|
|
.commit(0, kernel_offset.secret_key(&keychain.secp()).unwrap())
|
|
|
|
.unwrap();
|
|
|
|
keychain
|
|
|
|
.secp()
|
|
|
|
.commit_sum(vec![tx_excess], vec![offset_excess])
|
|
|
|
.context(ErrorKind::Transaction)?
|
2018-02-13 18:35:30 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// update the tx kernel to reflect the offset excess and sig
|
|
|
|
assert_eq!(final_tx.kernels.len(), 1);
|
|
|
|
final_tx.kernels[0].excess = final_excess.clone();
|
|
|
|
final_tx.kernels[0].excess_sig = excess_sig.clone();
|
|
|
|
|
|
|
|
// confirm the kernel verifies successfully before proceeding
|
2018-03-04 03:19:54 +03:00
|
|
|
final_tx.kernels[0]
|
|
|
|
.verify()
|
|
|
|
.context(ErrorKind::Transaction)?;
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-02-13 18:35:30 +03:00
|
|
|
// confirm the overall transaction is valid (including the updated kernel)
|
2018-02-28 20:56:09 +03:00
|
|
|
let _ = final_tx.validate().context(ErrorKind::Transaction)?;
|
2017-06-08 04:12:15 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
debug!(
|
|
|
|
LOGGER,
|
2018-01-10 22:36:27 +03:00
|
|
|
"Finalized transaction and built output - {:?}, {:?}, {}",
|
2017-10-26 00:09:34 +03:00
|
|
|
root_key_id.clone(),
|
|
|
|
key_id.clone(),
|
|
|
|
derivation,
|
|
|
|
);
|
2017-10-13 07:45:07 +03:00
|
|
|
|
2018-01-10 22:36:27 +03:00
|
|
|
Ok(final_tx)
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|