Sending of transaction to push to mocked up API

Once the wallet transaction is fully built, serializing it and
sending the push request to a node. Implemented the push node
API, mocked up for now (until the tx pool is integrated).
This commit is contained in:
Ignotus Peverell 2017-06-08 16:34:27 -07:00
parent d26a659a97
commit dd1339a9c3
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
5 changed files with 63 additions and 3 deletions

View file

@ -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"

View file

@ -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<Operation> {
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<chain::ChainStore>) {
@ -91,6 +128,8 @@ pub fn start_rest_apis(addr: String, chain_store: Arc<chain::ChainStore>) {
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);
});

View file

@ -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;

View file

@ -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(())
}

View file

@ -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<secp::Error> for Error {
@ -65,6 +68,12 @@ impl From<num::ParseIntError> for Error {
}
}
impl From<api::Error> for Error {
fn from(e: api::Error) -> Error {
Error::Node(e)
}
}
#[derive(Debug, Clone)]
pub struct WalletConfig {
pub api_http_addr: String,