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.
|
|
|
|
|
|
|
|
use std::convert::From;
|
|
|
|
use secp::{self, Secp256k1};
|
|
|
|
use secp::key::SecretKey;
|
|
|
|
|
|
|
|
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 extkey::{self, ExtendedKey};
|
|
|
|
use types::*;
|
|
|
|
use util;
|
|
|
|
|
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.
|
|
|
|
pub fn receive_json_tx(ext_key: &ExtendedKey, partial_tx_str: &str) -> Result<(), Error> {
|
|
|
|
let (amount, blinding, partial_tx) = partial_tx_from_json(partial_tx_str)?;
|
|
|
|
let final_tx = receive_transaction(ext_key, amount, blinding, partial_tx)?;
|
2017-06-09 02:34:27 +03:00
|
|
|
let tx_hex = util::to_hex(ser::ser_vec(&final_tx).unwrap());
|
|
|
|
|
|
|
|
let config = WalletConfig::default();
|
2017-06-13 02:41:27 +03:00
|
|
|
let url = format!("{}/v1/pool/push", config.api_http_addr.as_str());
|
2017-06-09 02:34:27 +03:00
|
|
|
api::client::post(url.as_str(), &TxWrapper { tx_hex: tx_hex })?;
|
2017-06-08 04:12:15 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-05-25 02:08:39 +03:00
|
|
|
/// Amount in request to build a coinbase output.
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
pub struct CbAmount {
|
2017-05-29 06:21:29 +03:00
|
|
|
amount: u64,
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Response to build a coinbase output.
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
pub struct CbData {
|
2017-05-29 06:21:29 +03:00
|
|
|
output: String,
|
|
|
|
kernel: String,
|
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-05-29 06:21:29 +03:00
|
|
|
pub key: ExtendedKey,
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ApiEndpoint for WalletReceiver {
|
|
|
|
type ID = String;
|
|
|
|
type T = String;
|
2017-05-29 06:21:29 +03:00
|
|
|
type OP_IN = CbAmount;
|
|
|
|
type OP_OUT = CbData;
|
2017-05-25 02:08:39 +03:00
|
|
|
|
|
|
|
fn operations(&self) -> Vec<Operation> {
|
2017-06-13 02:41:27 +03:00
|
|
|
vec![Operation::Custom("coinbase".to_string())]
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn operation(&self, op: String, input: CbAmount) -> ApiResult<CbData> {
|
2017-05-29 06:21:29 +03:00
|
|
|
debug!("Operation {} with amount {}", op, input.amount);
|
|
|
|
if input.amount == 0 {
|
|
|
|
return Err(api::Error::Argument(format!("Zero amount not allowed.")));
|
|
|
|
}
|
|
|
|
match op.as_str() {
|
2017-06-13 02:41:27 +03:00
|
|
|
"coinbase" => {
|
2017-05-29 06:21:29 +03:00
|
|
|
let (out, kern) =
|
|
|
|
receive_coinbase(&self.key, input.amount).map_err(|e| {
|
|
|
|
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))
|
|
|
|
})?;
|
|
|
|
Ok(CbData {
|
|
|
|
output: util::to_hex(out_bin),
|
|
|
|
kernel: util::to_hex(kern_bin),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => Err(api::Error::Argument(format!("Unknown operation: {}", op))),
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Build a coinbase output and the corresponding kernel
|
|
|
|
fn receive_coinbase(ext_key: &ExtendedKey, amount: u64) -> Result<(Output, TxKernel), Error> {
|
|
|
|
let secp = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);
|
|
|
|
|
|
|
|
// derive a new private for the reward
|
|
|
|
let mut wallet_data = WalletData::read_or_create()?;
|
|
|
|
let next_child = wallet_data.next_child(ext_key.fingerprint);
|
|
|
|
let coinbase_key = ext_key.derive(&secp, next_child).map_err(|e| Error::Key(e))?;
|
|
|
|
|
|
|
|
// track the new output and return the stuff needed for reward
|
|
|
|
wallet_data.append_output(OutputData {
|
|
|
|
fingerprint: coinbase_key.fingerprint,
|
|
|
|
n_child: coinbase_key.n_child,
|
|
|
|
value: amount,
|
|
|
|
status: OutputStatus::Unconfirmed,
|
|
|
|
});
|
|
|
|
wallet_data.write()?;
|
|
|
|
|
2017-06-13 02:41:27 +03:00
|
|
|
debug!("Using child {} for a new coinbase output.",
|
|
|
|
coinbase_key.n_child);
|
2017-05-26 03:22:21 +03:00
|
|
|
|
2017-06-08 04:12:15 +03:00
|
|
|
Block::reward_output(coinbase_key.key, &secp).map_err(&From::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds a full transaction from the partial one sent to us for transfer
|
|
|
|
fn receive_transaction(ext_key: &ExtendedKey,
|
|
|
|
amount: u64,
|
|
|
|
blinding: SecretKey,
|
|
|
|
partial: Transaction)
|
|
|
|
-> Result<Transaction, Error> {
|
|
|
|
|
|
|
|
let secp = secp::Secp256k1::with_caps(secp::ContextFlag::Commit);
|
|
|
|
|
|
|
|
// derive a new private for the receiving output
|
|
|
|
let mut wallet_data = WalletData::read_or_create()?;
|
|
|
|
let next_child = wallet_data.next_child(ext_key.fingerprint);
|
|
|
|
let out_key = ext_key.derive(&secp, next_child).map_err(|e| Error::Key(e))?;
|
|
|
|
|
|
|
|
let (tx_final, _) = build::transaction(vec![build::initial_tx(partial),
|
|
|
|
build::with_excess(blinding),
|
|
|
|
build::output(amount, out_key.key)])?;
|
|
|
|
|
2017-06-13 02:41:27 +03:00
|
|
|
// make sure the resulting transaction is valid (could have been lied to
|
|
|
|
// on excess)
|
|
|
|
tx_final.validate(&secp)?;
|
|
|
|
|
2017-06-08 04:12:15 +03:00
|
|
|
// track the new output and return the finalized transaction to broadcast
|
|
|
|
wallet_data.append_output(OutputData {
|
|
|
|
fingerprint: out_key.fingerprint,
|
|
|
|
n_child: out_key.n_child,
|
|
|
|
value: amount,
|
|
|
|
status: OutputStatus::Unconfirmed,
|
|
|
|
});
|
|
|
|
wallet_data.write()?;
|
|
|
|
|
2017-06-13 02:41:27 +03:00
|
|
|
debug!("Using child {} for a new transaction output.",
|
|
|
|
out_key.n_child);
|
2017-06-08 04:12:15 +03:00
|
|
|
|
|
|
|
Ok(tx_final)
|
2017-05-25 02:08:39 +03:00
|
|
|
}
|