diff --git a/api/Cargo.toml b/api/Cargo.toml index 7df87e79b..9465f880e 100644 --- a/api/Cargo.toml +++ b/api/Cargo.toml @@ -15,4 +15,5 @@ iron = "~0.5.1" log = "~0.3" router = "~0.5.1" serde = "~1.0.8" +serde_derive = "~1.0.8" serde_json = "~1.0.2" diff --git a/api/src/endpoints.rs b/api/src/endpoints.rs index 7806242f4..e9105775e 100644 --- a/api/src/endpoints.rs +++ b/api/src/endpoints.rs @@ -24,8 +24,9 @@ use std::sync::Arc; use std::thread; -use core::core::Output; +use core::core::{Transaction, Output}; use core::core::hash::Hash; +use core::ser; use chain::{self, Tip}; use rest::*; use secp::pedersen::Commitment; @@ -81,6 +82,42 @@ impl ApiEndpoint for OutputApi { } } +/// ApiEndpoint implementation for the transaction pool, to check its status +/// and size as well as push new transactions. +#[derive(Clone)] +pub struct PoolApi { +} + +impl ApiEndpoint for PoolApi { + type ID = String; + type T = (); + type OP_IN = TxWrapper; + type OP_OUT = (); + + fn operations(&self) -> Vec { + vec![Operation::Custom("push".to_string())] + } + + fn operation(&self, op: String, input: TxWrapper) -> ApiResult<()> { + let tx_bin = util::from_hex(input.tx_hex) + .map_err(|e| Error::Argument(format!("Invalid hex in transaction wrapper.")))?; + + let tx: Transaction = ser::deserialize(&mut &tx_bin[..]).map_err(|_| { + Error::Argument("Could not deserialize transaction, invalid format.".to_string()) + })?; + + println!("Fake push of transaction:"); + println!("{:?}", tx); + Ok(()) + } +} + +/// Dummy wrapper for the hex-encoded serialized transaction. +#[derive(Serialize, Deserialize)] +struct TxWrapper { + tx_hex: String, +} + /// Start all server REST APIs. Just register all of them on a ApiServer /// instance and runs the corresponding HTTP server. pub fn start_rest_apis(addr: String, chain_store: Arc) { @@ -91,6 +128,8 @@ pub fn start_rest_apis(addr: String, chain_store: Arc) { ChainApi { chain_store: chain_store.clone() }); apis.register_endpoint("/chain/output".to_string(), OutputApi { chain_store: chain_store.clone() }); + apis.register_endpoint("/pool".to_string(), PoolApi {}); + apis.start(&addr[..]).unwrap_or_else(|e| { error!("Failed to start API HTTP server: {}.", e); }); diff --git a/api/src/lib.rs b/api/src/lib.rs index f6e8dce6a..9492d16e2 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -23,6 +23,8 @@ extern crate log; extern crate iron; extern crate router; extern crate serde; +#[macro_use] +extern crate serde_derive; extern crate serde_json; pub mod client; diff --git a/wallet/src/receiver.rs b/wallet/src/receiver.rs index 2ebf1c9f8..cbc17367a 100644 --- a/wallet/src/receiver.rs +++ b/wallet/src/receiver.rs @@ -60,14 +60,23 @@ use extkey::{self, ExtendedKey}; use types::*; use util; +/// Dummy wrapper for the hex-encoded serialized transaction. +#[derive(Serialize, Deserialize)] +struct TxWrapper { + tx_hex: String, +} + /// 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)?; - // TODO send to a node to broadcast - println!("TX OK!"); + let tx_hex = util::to_hex(ser::ser_vec(&final_tx).unwrap()); + + let config = WalletConfig::default(); + let url = format!("{}/v1/receive_coinbase", config.api_http_addr.as_str()); + api::client::post(url.as_str(), &TxWrapper { tx_hex: tx_hex })?; Ok(()) } diff --git a/wallet/src/types.rs b/wallet/src/types.rs index f5a60b4ff..d95990eb4 100644 --- a/wallet/src/types.rs +++ b/wallet/src/types.rs @@ -23,6 +23,7 @@ use serde_json; use secp; use secp::key::SecretKey; +use api; use core::core::Transaction; use core::ser; use extkey; @@ -39,6 +40,8 @@ pub enum Error { WalletData(String), /// An error in the format of the JSON structures exchanged by the wallet Format(String), + /// Error when contacting a node through its API + Node(api::Error), } impl From for Error { @@ -65,6 +68,12 @@ impl From for Error { } } +impl From for Error { + fn from(e: api::Error) -> Error { + Error::Node(e) + } +} + #[derive(Debug, Clone)] pub struct WalletConfig { pub api_http_addr: String,