2017-10-26 00:09:34 +03:00
|
|
|
// Copyright 2017 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;
|
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;
|
2017-11-01 02:32:33 +03:00
|
|
|
use core::core::{build, Block, Output, Transaction, TxKernel};
|
2017-05-25 02:08:39 +03:00
|
|
|
use core::ser;
|
2017-10-26 00:09:34 +03:00
|
|
|
use keychain::{BlindingFactor, Identifier, Keychain};
|
2017-05-25 02:08:39 +03:00
|
|
|
use types::*;
|
|
|
|
use util;
|
2017-10-12 19:56:44 +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-11-01 21:32:34 +03:00
|
|
|
pub fn receive_json_tx_str(
|
|
|
|
config: &WalletConfig,
|
|
|
|
keychain: &Keychain,
|
|
|
|
json_tx: &str,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let partial_tx = serde_json::from_str(json_tx).unwrap();
|
|
|
|
receive_json_tx(config, keychain, &partial_tx)
|
|
|
|
}
|
|
|
|
|
2017-06-08 04:12:15 +03:00
|
|
|
/// Receive an already well formed JSON transaction issuance and finalize the
|
|
|
|
/// transaction, adding our receiving output, to broadcast to the rest of the
|
|
|
|
/// network.
|
2017-10-27 20:36:03 +03:00
|
|
|
pub fn receive_json_tx(
|
|
|
|
config: &WalletConfig,
|
|
|
|
keychain: &Keychain,
|
2017-11-01 21:32:34 +03:00
|
|
|
partial_tx: &JSONPartialTx,
|
2017-10-27 20:36:03 +03:00
|
|
|
) -> Result<(), Error> {
|
2017-11-01 21:32:34 +03:00
|
|
|
let (amount, blinding, tx) = read_partial_tx(keychain, partial_tx)?;
|
|
|
|
let final_tx = receive_transaction(config, keychain, amount, blinding, tx)?;
|
2017-06-09 02:34:27 +03:00
|
|
|
let tx_hex = util::to_hex(ser::ser_vec(&final_tx).unwrap());
|
|
|
|
|
2017-06-27 05:09:01 +03:00
|
|
|
let url = format!("{}/v1/pool/push", config.check_node_api_http_addr.as_str());
|
2017-11-01 21:32:34 +03:00
|
|
|
api::client::post(url.as_str(), &TxWrapper { tx_hex: tx_hex })
|
|
|
|
.map_err(|e| Error::Node(e))?;
|
2017-06-08 04:12:15 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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-01 21:32:34 +03:00
|
|
|
let struct_body = req.get::<bodyparser::Struct<JSONPartialTx>>();
|
2017-11-01 02:32:33 +03:00
|
|
|
|
2017-11-01 21:32:34 +03:00
|
|
|
if let Ok(Some(partial_tx)) = struct_body {
|
|
|
|
receive_json_tx(&self.config, &self.keychain, &partial_tx)
|
|
|
|
.map_err(|e| {
|
|
|
|
api::Error::Internal(
|
|
|
|
format!("Error processing partial transaction: {:?}", e),
|
|
|
|
)})
|
|
|
|
.unwrap();
|
|
|
|
Ok(Response::with(status::Ok))
|
|
|
|
} else {
|
|
|
|
Ok(Response::with((status::BadRequest, "")))
|
2017-05-29 06:21:29 +03:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
// Read wallet data without acquiring the write lock.
|
|
|
|
fn retrieve_existing_key(
|
|
|
|
config: &WalletConfig,
|
|
|
|
key_id: Identifier,
|
|
|
|
) -> Result<(Identifier, u32), Error> {
|
|
|
|
let res = WalletData::read_wallet(&config.data_file_dir, |wallet_data| {
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_available_key(
|
|
|
|
config: &WalletConfig,
|
|
|
|
keychain: &Keychain,
|
|
|
|
) -> Result<(Identifier, u32), Error> {
|
|
|
|
let res = WalletData::read_wallet(&config.data_file_dir, |wallet_data| {
|
|
|
|
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)
|
|
|
|
})?;
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
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-10-26 00:09:34 +03:00
|
|
|
let key_id = block_fees.key_id();
|
2017-05-25 02:08:39 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
let (key_id, derivation) = match key_id {
|
|
|
|
Some(key_id) => retrieve_existing_key(config, key_id)?,
|
|
|
|
None => next_available_key(config, keychain)?,
|
|
|
|
};
|
2017-06-15 07:42:58 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
// Now acquire the wallet lock and write the new output.
|
|
|
|
WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
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,
|
2017-09-22 19:44:12 +03:00
|
|
|
height: 0,
|
|
|
|
lock_height: 0,
|
2017-10-18 23:47:37 +03:00
|
|
|
is_coinbase: true,
|
2017-06-15 07:42:58 +03:00
|
|
|
});
|
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,
|
|
|
|
"Received coinbase and built candidate output - {:?}, {:?}, {}",
|
|
|
|
root_key_id.clone(),
|
|
|
|
key_id.clone(),
|
|
|
|
derivation,
|
|
|
|
);
|
2017-06-15 07:42:58 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
debug!(LOGGER, "block_fees - {:?}", block_fees);
|
2017-10-07 20:38:41 +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
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
debug!(LOGGER, "block_fees updated - {:?}", block_fees);
|
2017-10-07 20:38:41 +03:00
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
let (out, kern) = Block::reward_output(&keychain, &key_id, block_fees.fees)?;
|
2017-10-26 00:09:34 +03:00
|
|
|
Ok((out, kern, block_fees))
|
2017-06-08 04:12:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds a full transaction from the partial one sent to us for transfer
|
2017-09-29 21:44:25 +03:00
|
|
|
fn receive_transaction(
|
|
|
|
config: &WalletConfig,
|
2017-10-03 03:02:31 +03:00
|
|
|
keychain: &Keychain,
|
2017-09-29 21:44:25 +03:00
|
|
|
amount: u64,
|
2017-10-03 03:02:31 +03:00
|
|
|
blinding: BlindingFactor,
|
2017-09-29 21:44:25 +03:00
|
|
|
partial: 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
|
|
|
let (key_id, derivation) = next_available_key(config, keychain)?;
|
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
|
2017-11-01 02:32:33 +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
|
2017-10-26 00:09:34 +03:00
|
|
|
let fee = tx_fee(partial.inputs.len(), partial.outputs.len() + 1, None);
|
|
|
|
if fee != partial.fee {
|
|
|
|
return Err(Error::FeeDispute {
|
|
|
|
sender_fee: partial.fee,
|
|
|
|
recipient_fee: fee,
|
|
|
|
});
|
|
|
|
}
|
2017-10-10 20:30:34 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
let out_amount = amount - fee;
|
2017-10-02 00:56:19 +03:00
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
let (tx_final, _) = build::transaction(
|
|
|
|
vec![
|
|
|
|
build::initial_tx(partial),
|
|
|
|
build::with_excess(blinding),
|
|
|
|
build::output(out_amount, key_id.clone()),
|
2017-10-26 00:09:34 +03:00
|
|
|
// build::with_fee(fee_amount),
|
2017-11-01 02:32:33 +03:00
|
|
|
],
|
|
|
|
keychain,
|
|
|
|
)?;
|
2017-06-13 02:41:27 +03:00
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
// make sure the resulting transaction is valid (could have been lied to on
|
|
|
|
// excess).
|
2017-10-26 00:09:34 +03:00
|
|
|
tx_final.validate(&keychain.secp())?;
|
2017-06-08 04:12:15 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
// operate within a lock on wallet data
|
|
|
|
WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
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-02 00:56:19 +03:00
|
|
|
value: out_amount,
|
2017-06-15 07:42:58 +03:00
|
|
|
status: OutputStatus::Unconfirmed,
|
2017-09-22 19:44:12 +03:00
|
|
|
height: 0,
|
|
|
|
lock_height: 0,
|
2017-10-18 23:47:37 +03:00
|
|
|
is_coinbase: false,
|
2017-06-15 07:42:58 +03:00
|
|
|
});
|
2017-10-26 00:09:34 +03:00
|
|
|
})?;
|
|
|
|
|
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"Received txn and built output - {:?}, {:?}, {}",
|
|
|
|
root_key_id.clone(),
|
|
|
|
key_id.clone(),
|
|
|
|
derivation,
|
|
|
|
);
|
2017-10-13 07:45:07 +03:00
|
|
|
|
2017-10-26 00:09:34 +03:00
|
|
|
Ok(tx_final)
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|