2017-05-25 02:08:39 +03:00
|
|
|
// Copyright 2016 The Grin Developers
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//!
|
|
|
|
//! The API looks like this:
|
|
|
|
//!
|
|
|
|
//! POST /v1/wallet/receive
|
|
|
|
//! > {
|
|
|
|
//! > "amount": 10,
|
|
|
|
//! > "blind_sum": "a12b7f...",
|
|
|
|
//! > "tx": "f083de...",
|
|
|
|
//! > }
|
|
|
|
//!
|
|
|
|
//! < {
|
|
|
|
//! < "tx": "f083de...",
|
|
|
|
//! < "status": "ok"
|
|
|
|
//! < }
|
|
|
|
//!
|
|
|
|
//! POST /v1/wallet/finalize
|
|
|
|
//! > {
|
|
|
|
//! > "tx": "f083de...",
|
|
|
|
//! > }
|
|
|
|
//!
|
|
|
|
//! POST /v1/wallet/receive_coinbase
|
|
|
|
//! > {
|
|
|
|
//! > "amount": 1,
|
|
|
|
//! > }
|
|
|
|
//!
|
|
|
|
//! < {
|
|
|
|
//! < "output": "8a90bc...",
|
|
|
|
//! < "kernel": "f083de...",
|
|
|
|
//! < }
|
|
|
|
//!
|
|
|
|
//! Note that while at this point the finalize call is completely unecessary, a
|
|
|
|
//! double-exchange will be required as soon as we support Schnorr signatures.
|
|
|
|
//! So we may as well have it in place already.
|
|
|
|
|
2017-10-05 10:23:04 +03:00
|
|
|
use core::consensus::reward;
|
2017-05-25 02:08:39 +03:00
|
|
|
use core::core::{Block, Transaction, TxKernel, Output, build};
|
|
|
|
use core::ser;
|
2017-05-26 03:22:21 +03:00
|
|
|
use api::{self, ApiEndpoint, Operation, ApiResult};
|
2017-10-12 06:35:40 +03:00
|
|
|
use keychain::{BlindingFactor, 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-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-12 19:56:44 +03:00
|
|
|
pub fn receive_json_tx(config: &WalletConfig,
|
|
|
|
keychain: &Keychain,
|
|
|
|
partial_tx_str: &str)
|
|
|
|
-> Result<(), Error> {
|
2017-10-03 03:02:31 +03:00
|
|
|
let (amount, blinding, partial_tx) = partial_tx_from_json(keychain, partial_tx_str)?;
|
|
|
|
let final_tx = receive_transaction(config, keychain, amount, blinding, partial_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-09-29 21:44:25 +03:00
|
|
|
let _: () = 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
|
|
|
}
|
|
|
|
|
|
|
|
impl ApiEndpoint for WalletReceiver {
|
|
|
|
type ID = String;
|
|
|
|
type T = String;
|
2017-06-27 05:09:01 +03:00
|
|
|
type OP_IN = WalletReceiveRequest;
|
2017-05-29 06:21:29 +03:00
|
|
|
type OP_OUT = CbData;
|
2017-05-25 02:08:39 +03:00
|
|
|
|
|
|
|
fn operations(&self) -> Vec<Operation> {
|
2017-10-12 19:56:44 +03:00
|
|
|
vec![Operation::Custom("coinbase".to_string()),
|
|
|
|
Operation::Custom("receive_json_tx".to_string())]
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
|
|
|
|
2017-06-27 05:09:01 +03:00
|
|
|
fn operation(&self, op: String, input: WalletReceiveRequest) -> ApiResult<CbData> {
|
2017-05-29 06:21:29 +03:00
|
|
|
match op.as_str() {
|
2017-06-13 02:41:27 +03:00
|
|
|
"coinbase" => {
|
2017-06-27 05:09:01 +03:00
|
|
|
match input {
|
2017-10-05 10:23:04 +03:00
|
|
|
WalletReceiveRequest::Coinbase(cb_fees) => {
|
2017-10-12 19:56:44 +03:00
|
|
|
debug!(LOGGER, "Operation {} with fees {:?}", op, cb_fees);
|
|
|
|
let (out, kern, block_fees) = receive_coinbase(
|
|
|
|
&self.config,
|
|
|
|
&self.keychain,
|
|
|
|
&cb_fees,
|
|
|
|
).map_err(|e| {
|
2017-10-13 07:45:07 +03:00
|
|
|
api::Error::Internal(format!("Error building coinbase: {:?}", e))
|
|
|
|
})?;
|
|
|
|
let out_bin = ser::ser_vec(&out).map_err(|e| {
|
|
|
|
api::Error::Internal(format!("Error serializing output: {:?}", e))
|
|
|
|
})?;
|
|
|
|
let kern_bin = ser::ser_vec(&kern).map_err(|e| {
|
|
|
|
api::Error::Internal(format!("Error serializing kernel: {:?}", e))
|
|
|
|
})?;
|
|
|
|
let key_id_bin = match block_fees.key_id {
|
|
|
|
Some(key_id) => {
|
|
|
|
ser::ser_vec(&key_id).map_err(|e| {
|
|
|
|
api::Error::Internal(
|
|
|
|
format!("Error serializing kernel: {:?}", e),
|
|
|
|
)
|
|
|
|
})?
|
2017-10-12 19:56:44 +03:00
|
|
|
}
|
2017-10-07 20:38:41 +03:00
|
|
|
None => vec![],
|
|
|
|
};
|
|
|
|
|
2017-06-27 05:09:01 +03:00
|
|
|
Ok(CbData {
|
|
|
|
output: util::to_hex(out_bin),
|
|
|
|
kernel: util::to_hex(kern_bin),
|
2017-10-13 07:45:07 +03:00
|
|
|
key_id: util::to_hex(key_id_bin),
|
2017-06-27 05:09:01 +03:00
|
|
|
})
|
|
|
|
}
|
2017-10-12 19:56:44 +03:00
|
|
|
_ => Err(api::Error::Argument(format!("Incorrect request data: {}", op))),
|
2017-06-27 05:09:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"receive_json_tx" => {
|
|
|
|
match input {
|
|
|
|
WalletReceiveRequest::PartialTransaction(partial_tx_str) => {
|
2017-10-12 19:56:44 +03:00
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"Operation {} with transaction {}",
|
|
|
|
op,
|
2017-10-13 07:45:07 +03:00
|
|
|
&partial_tx_str,
|
2017-10-12 19:56:44 +03:00
|
|
|
);
|
|
|
|
receive_json_tx(&self.config, &self.keychain, &partial_tx_str)
|
|
|
|
.map_err(|e| {
|
|
|
|
api::Error::Internal(
|
|
|
|
format!("Error processing partial transaction: {:?}", e),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.unwrap();
|
2017-10-03 03:02:31 +03:00
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
// TODO: Return emptiness for now, should be a proper enum return type
|
2017-06-27 05:09:01 +03:00
|
|
|
Ok(CbData {
|
|
|
|
output: String::from(""),
|
|
|
|
kernel: String::from(""),
|
2017-10-13 07:45:07 +03:00
|
|
|
key_id: String::from(""),
|
2017-06-27 05:09:01 +03:00
|
|
|
})
|
|
|
|
}
|
2017-10-12 19:56:44 +03:00
|
|
|
_ => Err(api::Error::Argument(format!("Incorrect request data: {}", op))),
|
2017-06-27 05:09:01 +03:00
|
|
|
}
|
2017-05-29 06:21:29 +03:00
|
|
|
}
|
|
|
|
_ => Err(api::Error::Argument(format!("Unknown operation: {}", op))),
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Build a coinbase output and the corresponding kernel
|
2017-10-12 19:56:44 +03:00
|
|
|
fn receive_coinbase(config: &WalletConfig,
|
|
|
|
keychain: &Keychain,
|
|
|
|
block_fees: &BlockFees)
|
|
|
|
-> Result<(Output, TxKernel, BlockFees), Error> {
|
2017-10-13 07:45:07 +03:00
|
|
|
let root_key_id = keychain.root_key_id();
|
2017-05-25 02:08:39 +03:00
|
|
|
|
2017-06-15 07:42:58 +03:00
|
|
|
// operate within a lock on wallet data
|
2017-06-16 19:47:29 +03:00
|
|
|
WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
2017-10-13 07:45:07 +03:00
|
|
|
let key_id = block_fees.key_id();
|
|
|
|
let (key_id, derivation) = match key_id {
|
|
|
|
Some(key_id) => {
|
|
|
|
let derivation = keychain.derivation_from_key_id(&key_id)?;
|
|
|
|
(key_id.clone(), derivation)
|
|
|
|
},
|
2017-10-07 20:38:41 +03:00
|
|
|
None => {
|
2017-10-13 07:45:07 +03:00
|
|
|
let derivation = wallet_data.next_child(root_key_id.clone());
|
|
|
|
let key_id = keychain.derive_key_id(derivation)?;
|
|
|
|
(key_id, derivation)
|
2017-10-07 20:38:41 +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,
|
2017-09-22 19:44:12 +03:00
|
|
|
height: 0,
|
|
|
|
lock_height: 0,
|
2017-06-15 07:42:58 +03:00
|
|
|
});
|
2017-10-07 20:38:41 +03:00
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
debug!(
|
|
|
|
LOGGER,
|
2017-10-13 07:45:07 +03:00
|
|
|
"Received coinbase and built candidate output - {:?}, {:?}, {}",
|
|
|
|
root_key_id.clone(),
|
|
|
|
key_id.clone(),
|
|
|
|
derivation,
|
2017-10-12 19:56:44 +03:00
|
|
|
);
|
2017-06-15 07:42:58 +03:00
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
debug!(LOGGER, "block_fees - {:?}", block_fees);
|
2017-10-07 20:38:41 +03:00
|
|
|
|
|
|
|
let mut block_fees = block_fees.clone();
|
2017-10-13 07:45:07 +03:00
|
|
|
block_fees.key_id = Some(key_id.clone());
|
2017-10-07 20:38:41 +03:00
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
debug!(LOGGER, "block_fees updated - {:?}", block_fees);
|
2017-10-07 20:38:41 +03:00
|
|
|
|
2017-10-11 21:12:01 +03:00
|
|
|
let (out, kern) = Block::reward_output(
|
|
|
|
&keychain,
|
2017-10-13 07:45:07 +03:00
|
|
|
&key_id,
|
2017-10-11 21:12:01 +03:00
|
|
|
block_fees.fees,
|
|
|
|
)?;
|
2017-10-07 20:38:41 +03:00
|
|
|
Ok((out, kern, block_fees))
|
2017-06-15 07:42:58 +03:00
|
|
|
})?
|
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-06-15 07:42:58 +03:00
|
|
|
// operate within a lock on wallet data
|
2017-06-16 19:47:29 +03:00
|
|
|
WalletData::with_wallet(&config.data_file_dir, |wallet_data| {
|
2017-10-13 07:45:07 +03:00
|
|
|
let derivation = wallet_data.next_child(root_key_id.clone());
|
|
|
|
let key_id = keychain.derive_key_id(derivation)?;
|
2017-06-08 04:12:15 +03:00
|
|
|
|
2017-10-13 07:45:07 +03:00
|
|
|
// 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(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-13 07:45:07 +03:00
|
|
|
let out_amount = amount - fee;
|
2017-10-02 00:56:19 +03:00
|
|
|
|
2017-10-13 07:45:07 +03:00
|
|
|
let (tx_final, _) = build::transaction(vec![
|
|
|
|
build::initial_tx(partial),
|
|
|
|
build::with_excess(blinding),
|
|
|
|
build::output(out_amount, key_id.clone()),
|
|
|
|
// build::with_fee(fee_amount),
|
|
|
|
], keychain)?;
|
2017-06-13 02:41:27 +03:00
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
// make sure the resulting transaction is valid (could have been lied to on
|
|
|
|
// excess)
|
2017-10-03 03:02:31 +03:00
|
|
|
tx_final.validate(&keychain.secp())?;
|
2017-06-08 04:12:15 +03:00
|
|
|
|
2017-06-15 07:42:58 +03:00
|
|
|
// track the new output and return the finalized transaction to broadcast
|
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-06-15 07:42:58 +03:00
|
|
|
});
|
2017-10-12 19:56:44 +03:00
|
|
|
debug!(
|
|
|
|
LOGGER,
|
2017-10-13 07:45:07 +03:00
|
|
|
"Received txn and built output - {:?}, {:?}, {}",
|
|
|
|
root_key_id.clone(),
|
|
|
|
key_id.clone(),
|
|
|
|
derivation,
|
2017-10-12 19:56:44 +03:00
|
|
|
);
|
2017-10-13 07:45:07 +03:00
|
|
|
|
2017-06-15 07:42:58 +03:00
|
|
|
Ok(tx_final)
|
|
|
|
})?
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|