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-05-25 02:08:39 +03:00
|
|
|
use types::*;
|
|
|
|
use util;
|
2017-10-03 03:02:31 +03:00
|
|
|
use keychain::{BlindingFactor, Keychain};
|
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)]
|
|
|
|
struct TxWrapper {
|
|
|
|
tx_hex: String,
|
|
|
|
}
|
|
|
|
|
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-09-24 07:40:31 +03:00
|
|
|
pub fn receive_json_tx(
|
|
|
|
config: &WalletConfig,
|
2017-10-03 03:02:31 +03:00
|
|
|
keychain: &Keychain,
|
2017-09-29 21:44:25 +03:00
|
|
|
partial_tx_str: &str,
|
2017-09-24 07:40:31 +03:00
|
|
|
) -> 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-06-27 05:09:01 +03:00
|
|
|
vec![
|
|
|
|
Operation::Custom("coinbase".to_string()),
|
2017-09-29 21:44:25 +03:00
|
|
|
Operation::Custom("receive_json_tx".to_string()),
|
2017-06-27 05:09:01 +03:00
|
|
|
]
|
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-06 23:10:30 +03:00
|
|
|
debug!("Operation {} with fees {:?}", op, cb_fees);
|
2017-10-07 20:38:41 +03:00
|
|
|
let (out, kern, block_fees) =
|
2017-10-03 03:02:31 +03:00
|
|
|
receive_coinbase(
|
|
|
|
&self.config,
|
|
|
|
&self.keychain,
|
2017-10-07 20:38:41 +03:00
|
|
|
&cb_fees,
|
2017-10-03 03:02:31 +03:00
|
|
|
).map_err(|e| {
|
|
|
|
api::Error::Internal(format!("Error building coinbase: {:?}", e))
|
|
|
|
})?;
|
|
|
|
let out_bin =
|
|
|
|
ser::ser_vec(&out).map_err(|e| {
|
2017-10-07 20:38:41 +03:00
|
|
|
api::Error::Internal(format!("Error serializing output: {:?}", e))
|
|
|
|
})?;
|
2017-10-03 03:02:31 +03:00
|
|
|
let kern_bin =
|
|
|
|
ser::ser_vec(&kern).map_err(|e| {
|
2017-10-07 20:38:41 +03:00
|
|
|
api::Error::Internal(format!("Error serializing kernel: {:?}", e))
|
|
|
|
})?;
|
|
|
|
let pubkey_bin = match block_fees.pubkey {
|
|
|
|
Some(pubkey) => {
|
|
|
|
ser::ser_vec(&pubkey).map_err(|e| {
|
2017-10-03 03:02:31 +03:00
|
|
|
api::Error::Internal(format!("Error serializing kernel: {:?}", e))
|
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-07 20:38:41 +03:00
|
|
|
pubkey: util::to_hex(pubkey_bin),
|
2017-06-27 05:09:01 +03:00
|
|
|
})
|
|
|
|
}
|
2017-09-29 21:44:25 +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) => {
|
|
|
|
debug!("Operation {} with transaction {}", op, &partial_tx_str);
|
2017-10-03 03:02:31 +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();
|
|
|
|
|
|
|
|
//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-07 20:38:41 +03:00
|
|
|
pubkey: String::from(""),
|
2017-06-27 05:09:01 +03:00
|
|
|
})
|
|
|
|
}
|
2017-09-29 21:44:25 +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-09-29 21:44:25 +03:00
|
|
|
fn receive_coinbase(
|
|
|
|
config: &WalletConfig,
|
2017-10-03 03:02:31 +03:00
|
|
|
keychain: &Keychain,
|
2017-10-07 20:38:41 +03:00
|
|
|
block_fees: &BlockFees,
|
|
|
|
) -> Result<(Output, TxKernel, BlockFees), Error> {
|
|
|
|
let fingerprint = keychain.fingerprint();
|
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-07 20:38:41 +03:00
|
|
|
let pubkey = block_fees.pubkey();
|
|
|
|
let (pubkey, derivation) = match pubkey {
|
|
|
|
Some(pubkey) => {
|
|
|
|
let derivation = keychain.derivation_from_pubkey(&pubkey)?;
|
|
|
|
(pubkey.clone(), derivation)
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
let derivation = wallet_data.next_child(fingerprint.clone());
|
|
|
|
let pubkey = keychain.derive_pubkey(derivation)?;
|
|
|
|
(pubkey, derivation)
|
|
|
|
}
|
|
|
|
};
|
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-03 03:02:31 +03:00
|
|
|
fingerprint: fingerprint.clone(),
|
2017-10-06 23:10:30 +03:00
|
|
|
identifier: pubkey.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
|
|
|
|
|
|
|
debug!("Received coinbase and built candidate output - {}, {}, {}",
|
2017-10-03 03:02:31 +03:00
|
|
|
fingerprint.clone(), pubkey.fingerprint(), derivation);
|
2017-06-15 07:42:58 +03:00
|
|
|
|
2017-10-07 20:38:41 +03:00
|
|
|
debug!("block_fees - {:?}", block_fees);
|
|
|
|
|
|
|
|
let mut block_fees = block_fees.clone();
|
|
|
|
block_fees.pubkey = Some(pubkey.clone());
|
|
|
|
|
|
|
|
debug!("block_fees updated - {:?}", block_fees);
|
|
|
|
|
|
|
|
let (out, kern) = Block::reward_output(&keychain, &pubkey, block_fees.fees)?;
|
|
|
|
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-05 10:23:04 +03:00
|
|
|
|
2017-10-03 03:02:31 +03:00
|
|
|
let fingerprint = keychain.clone().fingerprint();
|
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-03 03:02:31 +03:00
|
|
|
let derivation = wallet_data.next_child(fingerprint.clone());
|
|
|
|
let pubkey = keychain.derive_pubkey(derivation)?;
|
2017-06-08 04:12:15 +03:00
|
|
|
|
2017-10-10 20:30:34 +03:00
|
|
|
// from pool.rs
|
|
|
|
// (-1 * num_inputs) + (4 * num_outputs) + 1
|
|
|
|
// then multiply by accept_fee_base==10
|
|
|
|
// so 80 is basically the minimum fee for a basic transaction
|
|
|
|
// so lets use 100 for now (revisit this)
|
|
|
|
|
|
|
|
let fee_amount = 100;
|
2017-10-02 00:56:19 +03:00
|
|
|
let out_amount = amount - fee_amount;
|
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
let (tx_final, _) = build::transaction(vec![
|
|
|
|
build::initial_tx(partial),
|
|
|
|
build::with_excess(blinding),
|
2017-10-03 03:02:31 +03:00
|
|
|
build::output(out_amount, pubkey.clone()),
|
2017-10-02 00:56:19 +03:00
|
|
|
build::with_fee(fee_amount),
|
2017-10-03 03:02:31 +03:00
|
|
|
], keychain)?;
|
2017-06-13 02:41:27 +03:00
|
|
|
|
2017-10-03 03:02:31 +03:00
|
|
|
// make sure the resulting transaction is valid (could have been lied to on excess)
|
|
|
|
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-03 03:02:31 +03:00
|
|
|
fingerprint: fingerprint.clone(),
|
2017-10-06 23:10:30 +03:00
|
|
|
identifier: pubkey.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-03 03:02:31 +03:00
|
|
|
debug!("Received txn and built output - {}, {}, {}",
|
|
|
|
fingerprint.clone(), pubkey.fingerprint(), derivation);
|
2017-06-15 07:42:58 +03:00
|
|
|
|
|
|
|
Ok(tx_final)
|
|
|
|
})?
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|