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::Handler;
|
2018-04-16 12:00:32 +03:00
|
|
|
use iron::prelude::*;
|
2017-11-01 21:32:34 +03:00
|
|
|
use iron::status;
|
|
|
|
use serde_json;
|
2018-05-09 12:15:58 +03:00
|
|
|
use std::sync::{Arc, RwLock};
|
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-05-16 15:18:09 +03:00
|
|
|
use core::core::{Output, Transaction, TxKernel};
|
|
|
|
use libwallet::{aggsig, reward, transaction};
|
|
|
|
use grinwallet::keys;
|
2018-01-17 06:03:40 +03:00
|
|
|
use core::{global, ser};
|
2018-04-16 12:00:32 +03:00
|
|
|
use failure::{Fail, ResultExt};
|
2018-05-16 15:18:09 +03:00
|
|
|
use keychain::Keychain;
|
2017-05-25 02:08:39 +03:00
|
|
|
use types::*;
|
2018-03-20 06:18:54 +03:00
|
|
|
use urlencoded::UrlEncodedQuery;
|
2018-05-16 15:18:09 +03:00
|
|
|
use util::{to_hex, LOGGER};
|
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-05-09 12:15:58 +03:00
|
|
|
lazy_static! {
|
|
|
|
/// Static reference to aggsig context (temporary while wallet is being refactored)
|
|
|
|
pub static ref AGGSIG_CONTEXT_MANAGER:Arc<RwLock<aggsig::ContextManager>>
|
|
|
|
= Arc::new(RwLock::new(aggsig::ContextManager::new()));
|
|
|
|
}
|
|
|
|
|
2018-01-10 22:36:27 +03:00
|
|
|
fn handle_sender_initiation(
|
2017-11-01 21:32:34 +03:00
|
|
|
config: &WalletConfig,
|
2018-05-09 12:15:58 +03:00
|
|
|
context_manager: &mut aggsig::ContextManager,
|
2017-11-01 21:32:34 +03:00
|
|
|
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-05-16 15:18:09 +03:00
|
|
|
// Create a potential output for this transaction
|
|
|
|
let (key_id, derivation) = WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
|
|
|
keys::next_available_key(&wallet_data, keychain)
|
|
|
|
})?;
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
let partial_tx =
|
|
|
|
transaction::recipient_initiation(keychain, context_manager, partial_tx, &key_id)?;
|
|
|
|
let mut context = context_manager.get_context(&partial_tx.id);
|
|
|
|
context.add_output(&key_id);
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
// Add the output to our wallet
|
|
|
|
let _ = WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
2018-01-10 22:36:27 +03:00
|
|
|
wallet_data.add_output(OutputData {
|
2018-05-16 15:18:09 +03:00
|
|
|
root_key_id: keychain.root_key_id(),
|
2018-01-10 22:36:27 +03:00
|
|
|
key_id: key_id.clone(),
|
|
|
|
n_child: derivation,
|
2018-05-16 15:18:09 +03:00
|
|
|
value: partial_tx.amount - context.fee,
|
2018-01-10 22:36:27 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
})?;
|
|
|
|
|
2018-05-09 12:15:58 +03:00
|
|
|
context_manager.save_context(context);
|
2018-01-10 22:36:27 +03:00
|
|
|
Ok(partial_tx)
|
2017-11-01 21:32:34 +03:00
|
|
|
}
|
|
|
|
|
2018-01-10 22:36:27 +03:00
|
|
|
fn handle_sender_confirmation(
|
2017-10-27 20:36:03 +03:00
|
|
|
config: &WalletConfig,
|
2018-05-09 12:15:58 +03:00
|
|
|
context_manager: &mut aggsig::ContextManager,
|
2017-10-27 20:36:03 +03:00
|
|
|
keychain: &Keychain,
|
2018-03-04 03:19:54 +03:00
|
|
|
partial_tx: &PartialTx,
|
2018-03-20 06:18:54 +03:00
|
|
|
fluff: bool,
|
2018-05-16 15:18:09 +03:00
|
|
|
) -> Result<Transaction, Error> {
|
|
|
|
let context = context_manager.get_context(&partial_tx.id);
|
|
|
|
// Get output we created in earlier step
|
|
|
|
// TODO: will just be one for now, support multiple later
|
|
|
|
let output_vec = context.get_outputs();
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
let root_key_id = keychain.root_key_id();
|
|
|
|
// operate within a lock on wallet data
|
|
|
|
let (key_id, derivation) = WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
|
|
|
let (key_id, derivation) = keys::retrieve_existing_key(&wallet_data, output_vec[0].clone());
|
2018-02-13 18:35:30 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
wallet_data.add_output(OutputData {
|
|
|
|
root_key_id: root_key_id.clone(),
|
|
|
|
key_id: key_id.clone(),
|
|
|
|
n_child: derivation,
|
|
|
|
value: partial_tx.amount - context.fee,
|
|
|
|
status: OutputStatus::Unconfirmed,
|
|
|
|
height: 0,
|
|
|
|
lock_height: 0,
|
|
|
|
is_coinbase: false,
|
|
|
|
block: None,
|
|
|
|
merkle_proof: None,
|
|
|
|
});
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
(key_id, derivation)
|
|
|
|
})?;
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
// In this case partial_tx contains other party's pubkey info
|
|
|
|
let final_tx = transaction::finalize_transaction(
|
2018-02-13 18:35:30 +03:00
|
|
|
keychain,
|
2018-05-16 15:18:09 +03:00
|
|
|
context_manager,
|
|
|
|
partial_tx,
|
|
|
|
partial_tx,
|
|
|
|
&key_id,
|
|
|
|
derivation,
|
2018-02-13 18:35:30 +03:00
|
|
|
)?;
|
|
|
|
|
2018-01-10 22:36:27 +03:00
|
|
|
let tx_hex = to_hex(ser::ser_vec(&final_tx).unwrap());
|
|
|
|
|
2018-03-20 06:18:54 +03:00
|
|
|
let url;
|
|
|
|
if fluff {
|
|
|
|
url = format!(
|
|
|
|
"{}/v1/pool/push?fluff",
|
|
|
|
config.check_node_api_http_addr.as_str()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
2018-05-16 15:18:09 +03:00
|
|
|
Ok(final_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
|
|
|
|
2018-03-20 06:18:54 +03:00
|
|
|
let mut fluff = false;
|
|
|
|
if let Ok(params) = req.get_ref::<UrlEncodedQuery>() {
|
|
|
|
if let Some(_) = params.get("fluff") {
|
|
|
|
fluff = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 21:32:34 +03:00
|
|
|
if let Ok(Some(partial_tx)) = struct_body {
|
2018-05-09 12:15:58 +03:00
|
|
|
let mut acm = AGGSIG_CONTEXT_MANAGER.write().unwrap();
|
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,
|
2018-05-09 12:15:58 +03:00
|
|
|
&mut acm,
|
2018-03-04 03:19:54 +03:00
|
|
|
&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-04-16 12:00:32 +03:00
|
|
|
e.context(api::ErrorKind::Internal(
|
|
|
|
"Error processing partial transaction".to_owned(),
|
2018-03-04 03:19:54 +03:00
|
|
|
))
|
|
|
|
})
|
|
|
|
.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,
|
2018-05-09 12:15:58 +03:00
|
|
|
&mut acm,
|
2018-03-04 03:19:54 +03:00
|
|
|
&self.keychain,
|
|
|
|
&partial_tx,
|
2018-03-20 06:18:54 +03:00
|
|
|
fluff,
|
2018-03-04 03:19:54 +03:00
|
|
|
).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-04-16 12:00:32 +03:00
|
|
|
e.context(api::ErrorKind::Internal(
|
|
|
|
"Error processing partial transaction".to_owned(),
|
2018-03-04 03:19:54 +03:00
|
|
|
))
|
|
|
|
})
|
|
|
|
.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-05-16 15:18:09 +03:00
|
|
|
//TODO: Split up the output creation and the wallet insertion
|
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 {
|
2018-05-16 15:18:09 +03:00
|
|
|
Some(key_id) => keys::retrieve_existing_key(&wallet_data, key_id),
|
|
|
|
None => keys::next_available_key(&wallet_data, keychain),
|
2017-11-18 10:31:02 +03:00
|
|
|
};
|
|
|
|
|
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-05-09 12:15:58 +03:00
|
|
|
let (out, kern) =
|
|
|
|
reward::output(&keychain, &key_id, block_fees.fees, block_fees.height).unwrap();
|
|
|
|
/* .context(ErrorKind::Keychain)?; */
|
2017-10-26 00:09:34 +03:00
|
|
|
Ok((out, kern, block_fees))
|
2017-06-08 04:12:15 +03:00
|
|
|
}
|