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
|
|
|
|
2018-05-30 19:48:32 +03:00
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
|
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;
|
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-21 18:28:11 +03:00
|
|
|
use core::core::{Output, TxKernel};
|
|
|
|
use core::global;
|
2018-05-30 19:48:32 +03:00
|
|
|
use failure::{Fail, ResultExt};
|
|
|
|
use libtx::{reward, slate::Slate};
|
|
|
|
use libwallet::types::*;
|
|
|
|
use libwallet::{keys, selection};
|
2018-05-21 18:28:11 +03:00
|
|
|
use util::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
|
|
|
}
|
|
|
|
|
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)]
|
2018-05-30 19:48:32 +03:00
|
|
|
pub struct WalletReceiver<T>
|
|
|
|
where
|
|
|
|
T: WalletBackend,
|
|
|
|
{
|
|
|
|
pub wallet: Arc<RwLock<T>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> WalletReceiver<T>
|
|
|
|
where
|
|
|
|
T: WalletBackend,
|
|
|
|
{
|
|
|
|
fn handle_send(&self, wallet: &mut T, slate: &mut Slate) -> Result<(), Error> {
|
|
|
|
// 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,
|
|
|
|
)
|
|
|
|
.context(ErrorKind::LibWalletError)?;
|
|
|
|
|
|
|
|
// perform partial sig
|
|
|
|
let _ = slate
|
|
|
|
.fill_round_2(wallet.keychain(), &context.sec_key, &context.sec_nonce, 1)
|
|
|
|
.context(ErrorKind::LibWalletError)?;
|
|
|
|
|
|
|
|
// Save output in wallet
|
|
|
|
let _ = receiver_create_fn(wallet);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 19:48:32 +03:00
|
|
|
impl<T> Handler for WalletReceiver<T>
|
|
|
|
where
|
|
|
|
T: WalletBackend + Send + Sync + 'static,
|
|
|
|
{
|
2017-11-01 02:32:33 +03:00
|
|
|
fn handle(&self, req: &mut Request) -> IronResult<Response> {
|
2018-05-30 19:48:32 +03:00
|
|
|
let struct_body = req.get::<bodyparser::Struct<Slate>>();
|
|
|
|
let mut wallet = self.wallet.write().unwrap();
|
2018-03-20 06:18:54 +03:00
|
|
|
|
2018-05-21 18:28:11 +03:00
|
|
|
if let Ok(Some(mut slate)) = struct_body {
|
2018-05-30 19:48:32 +03:00
|
|
|
let _ = self.handle_send(&mut wallet, &mut slate)
|
2018-05-21 18:28:11 +03:00
|
|
|
.map_err(|e| {
|
|
|
|
error!(
|
|
|
|
LOGGER,
|
|
|
|
"Handling send -> Problematic slate, looks like this: {:?}", slate
|
|
|
|
);
|
|
|
|
e.context(api::ErrorKind::Internal(
|
|
|
|
"Error processing partial transaction".to_owned(),
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
let json = serde_json::to_string(&slate).unwrap();
|
|
|
|
Ok(Response::with((status::Ok, json)))
|
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
|
2018-05-30 19:48:32 +03:00
|
|
|
pub fn receive_coinbase<T>(
|
|
|
|
wallet: &mut T,
|
2017-11-01 02:32:33 +03:00
|
|
|
block_fees: &BlockFees,
|
2018-05-30 19:48:32 +03:00
|
|
|
) -> Result<(Output, TxKernel, BlockFees), Error>
|
|
|
|
where
|
|
|
|
T: WalletBackend,
|
|
|
|
{
|
|
|
|
let root_key_id = wallet.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.
|
2018-05-30 19:48:32 +03:00
|
|
|
let (key_id, derivation) = wallet.with_wallet(|wallet_data| {
|
2017-11-18 10:31:02 +03:00
|
|
|
let key_id = block_fees.key_id();
|
|
|
|
let (key_id, derivation) = match key_id {
|
2018-05-30 19:48:32 +03:00
|
|
|
Some(key_id) => keys::retrieve_existing_key(wallet_data, key_id),
|
|
|
|
None => keys::next_available_key(wallet_data),
|
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-30 19:48:32 +03:00
|
|
|
let (out, kern) = reward::output(
|
|
|
|
&wallet.keychain(),
|
|
|
|
&key_id,
|
|
|
|
block_fees.fees,
|
|
|
|
block_fees.height,
|
|
|
|
).unwrap();
|
2018-05-09 12:15:58 +03:00
|
|
|
/* .context(ErrorKind::Keychain)?; */
|
2017-10-26 00:09:34 +03:00
|
|
|
Ok((out, kern, block_fees))
|
2017-06-08 04:12:15 +03:00
|
|
|
}
|