From 741e3b20230c75387b4c19cec5b928bf4fc0a3e1 Mon Sep 17 00:00:00 2001 From: Robert Kornacki <11645932+robkorn@users.noreply.github.com> Date: Fri, 20 Nov 2020 13:58:05 +0100 Subject: [PATCH] Moved tx methods to own file --- src/lib.rs | 1 + src/node_interface.rs | 91 ----------------------------------------- src/transactions.rs | 95 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 91 deletions(-) create mode 100644 src/transactions.rs diff --git a/src/lib.rs b/src/lib.rs index d3f224b..f35092a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ pub mod local_config; pub mod node_interface; mod requests; pub mod scanning; +pub mod transactions; pub use node_interface::NodeInterface; pub use scanning::Scan; diff --git a/src/node_interface.rs b/src/node_interface.rs index 4aa9834..bfbe9d0 100644 --- a/src/node_interface.rs +++ b/src/node_interface.rs @@ -1,11 +1,7 @@ /// The `NodeInterface` struct is defined which allows for interacting with an /// Ergo Node via Rust. -use crate::JsonString; use crate::{BlockHeight, NanoErg, P2PKAddressString, P2SAddressString, TxId}; use ergo_lib::chain::ergo_box::ErgoBox; -use ergo_lib::chain::transaction::unsigned::UnsignedTransaction; -use ergo_lib::chain::transaction::Transaction; -use json::JsonValue; use serde_json::from_str; use thiserror::Error; @@ -61,93 +57,6 @@ impl NodeInterface { "http://".to_string() + &self.ip + ":" + &self.port } - /// Submits a Signed Transaction provided as input as JSON - /// to the Ergo Blockchain mempool. - pub fn submit_json_transaction(&self, signed_tx_json: &JsonString) -> Result { - let endpoint = "/transactions"; - let res_json = self.use_json_endpoint_and_check_errors(endpoint, signed_tx_json)?; - - // If tx is valid and is posted, return just the tx id - let tx_id = res_json.dump(); - return Ok(tx_id); - } - - /// Sign an Unsigned Transaction which is formatted in JSON - pub fn sign_json_transaction(&self, unsigned_tx_string: &JsonString) -> Result { - let endpoint = "/wallet/transaction/sign"; - let unsigned_tx_json = json::parse(&unsigned_tx_string) - .map_err(|_| NodeError::FailedParsingNodeResponse(unsigned_tx_string.to_string()))?; - - let prepared_body = object! { - tx: unsigned_tx_json - }; - - println!("Unsigned tx Json: {:?}", prepared_body.dump()); - - let res_json = self.use_json_endpoint_and_check_errors(endpoint, &prepared_body.dump())?; - - Ok(res_json) - } - - /// Sign an Unsigned Transaction which is formatted in JSON - /// and then submit it to the mempool. - pub fn sign_and_submit_json_transaction( - &self, - unsigned_tx_string: &JsonString, - ) -> Result { - let signed_tx = self.sign_json_transaction(unsigned_tx_string)?; - let signed_tx_json = json::stringify(signed_tx); - - self.submit_json_transaction(&signed_tx_json) - } - - /// Submits a Signed `Transaction` provided as input - /// to the Ergo Blockchain mempool. - pub fn submit_transaction(&self, signed_tx: &Transaction) -> Result { - let signed_tx_json = &serde_json::to_string(&signed_tx) - .map_err(|_| NodeError::Other("Failed Converting `Transaction` to json".to_string()))?; - self.submit_json_transaction(signed_tx_json) - } - - /// Sign an `UnsignedTransaction` - pub fn sign_transaction(&self, unsigned_tx: &UnsignedTransaction) -> Result { - let json_tx = - self.sign_json_transaction(&serde_json::to_string(&unsigned_tx).map_err(|_| { - NodeError::Other("Failed Converting `UnsignedTransaction` to json".to_string()) - })?)?; - - serde_json::from_str(&json_tx.dump()) - .map_err(|_| NodeError::Other("Failed Converting `Transaction` to json".to_string())) - } - - /// Sign an `UnsignedTransaction` and then submit it to the mempool. - pub fn sign_and_submit_transaction(&self, unsigned_tx: &UnsignedTransaction) -> Result { - let signed_tx = self.sign_transaction(unsigned_tx)?; - self.submit_transaction(&signed_tx) - } - - /// Generates and submits a tx using the node endpoints. Input is - /// a json formatted request with rawInputs (and rawDataInputs) - /// manually selected or inputs will be automatically selected by wallet. - /// Returns the resulting `TxId`. - pub fn generate_and_submit_transaction(&self, tx_request_json: &JsonString) -> Result { - let endpoint = "/wallet/transaction/send"; - let res_json = self.use_json_endpoint_and_check_errors(endpoint, tx_request_json)?; - // If tx is valid and is posted, return just the tx id - let tx_id = res_json.dump(); - return Ok(tx_id); - } - - /// Generates Json of an Unsigned Transaction. - /// Input must be a json formatted request with rawInputs (and rawDataInputs) - /// manually selected or will be automatically selected by wallet. - pub fn generate_json_transaction(&self, tx_request_json: &JsonString) -> Result { - let endpoint = "/wallet/transaction/generate"; - let res_json = self.use_json_endpoint_and_check_errors(endpoint, tx_request_json)?; - - Ok(res_json) - } - /// Get all addresses from the node wallet pub fn wallet_addresses(&self) -> Result> { let endpoint = "/wallet/addresses"; diff --git a/src/transactions.rs b/src/transactions.rs new file mode 100644 index 0000000..6333682 --- /dev/null +++ b/src/transactions.rs @@ -0,0 +1,95 @@ +use crate::node_interface::{NodeError, NodeInterface, Result}; +use crate::JsonString; +use crate::TxId; +use ergo_lib::chain::transaction::unsigned::UnsignedTransaction; +use ergo_lib::chain::transaction::Transaction; +use json::JsonValue; + +impl NodeInterface { + /// Submits a Signed Transaction provided as input as JSON + /// to the Ergo Blockchain mempool. + pub fn submit_json_transaction(&self, signed_tx_json: &JsonString) -> Result { + let endpoint = "/transactions"; + let res_json = self.use_json_endpoint_and_check_errors(endpoint, signed_tx_json)?; + + // If tx is valid and is posted, return just the tx id + let tx_id = res_json.dump(); + return Ok(tx_id); + } + + /// Sign an Unsigned Transaction which is formatted in JSON + pub fn sign_json_transaction(&self, unsigned_tx_string: &JsonString) -> Result { + let endpoint = "/wallet/transaction/sign"; + let unsigned_tx_json = json::parse(&unsigned_tx_string) + .map_err(|_| NodeError::FailedParsingNodeResponse(unsigned_tx_string.to_string()))?; + + let prepared_body = object! { + tx: unsigned_tx_json + }; + + println!("Unsigned tx Json: {:?}", prepared_body.dump()); + + let res_json = self.use_json_endpoint_and_check_errors(endpoint, &prepared_body.dump())?; + + Ok(res_json) + } + + /// Sign an Unsigned Transaction which is formatted in JSON + /// and then submit it to the mempool. + pub fn sign_and_submit_json_transaction( + &self, + unsigned_tx_string: &JsonString, + ) -> Result { + let signed_tx = self.sign_json_transaction(unsigned_tx_string)?; + let signed_tx_json = json::stringify(signed_tx); + + self.submit_json_transaction(&signed_tx_json) + } + + /// Submits a Signed `Transaction` provided as input + /// to the Ergo Blockchain mempool. + pub fn submit_transaction(&self, signed_tx: &Transaction) -> Result { + let signed_tx_json = &serde_json::to_string(&signed_tx) + .map_err(|_| NodeError::Other("Failed Converting `Transaction` to json".to_string()))?; + self.submit_json_transaction(signed_tx_json) + } + + /// Sign an `UnsignedTransaction` + pub fn sign_transaction(&self, unsigned_tx: &UnsignedTransaction) -> Result { + let json_tx = + self.sign_json_transaction(&serde_json::to_string(&unsigned_tx).map_err(|_| { + NodeError::Other("Failed Converting `UnsignedTransaction` to json".to_string()) + })?)?; + + serde_json::from_str(&json_tx.dump()) + .map_err(|_| NodeError::Other("Failed Converting `Transaction` to json".to_string())) + } + + /// Sign an `UnsignedTransaction` and then submit it to the mempool. + pub fn sign_and_submit_transaction(&self, unsigned_tx: &UnsignedTransaction) -> Result { + let signed_tx = self.sign_transaction(unsigned_tx)?; + self.submit_transaction(&signed_tx) + } + + /// Generates and submits a tx using the node endpoints. Input is + /// a json formatted request with rawInputs (and rawDataInputs) + /// manually selected or inputs will be automatically selected by wallet. + /// Returns the resulting `TxId`. + pub fn generate_and_submit_transaction(&self, tx_request_json: &JsonString) -> Result { + let endpoint = "/wallet/transaction/send"; + let res_json = self.use_json_endpoint_and_check_errors(endpoint, tx_request_json)?; + // If tx is valid and is posted, return just the tx id + let tx_id = res_json.dump(); + return Ok(tx_id); + } + + /// Generates Json of an Unsigned Transaction. + /// Input must be a json formatted request with rawInputs (and rawDataInputs) + /// manually selected or will be automatically selected by wallet. + pub fn generate_json_transaction(&self, tx_request_json: &JsonString) -> Result { + let endpoint = "/wallet/transaction/generate"; + let res_json = self.use_json_endpoint_and_check_errors(endpoint, tx_request_json)?; + + Ok(res_json) + } +}