buzzkillb Posted April 12, 2020 Report Share Posted April 12, 2020 I want to take my bash scripts and turn them into Rust. I am not a coder and have no idea what I am doing. But maybe posting as I go along helps someone else try something new. The first goals are to create a Masternode monitor and a raw transaction many input deduster. But need the basic basic basics first. Install rustc on ubuntu from https://www.rust-lang.org/learn/get-started create a projects folder and run cargo new tutorial to setup a tutorial project cargo new tutorial go into the tutorial folder and edit the Cargo.toml to import rust-bitcoin-rpc cd tutorial nano Cargo.toml add bitcoincore-rpc crate to dependencies like below [package] name = "tutorial" version = "0.1.0" authors = ["buzzkillb <email@email.com>"] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] bitcoincore-rpc = "0.10.0" go into the src directory and edit main.rs cd src nano main.rs edit main.rs to show below and change your port, USERNAME and PASSWORD to your Wallet settings. Denarius RPC port is 32369 extern crate bitcoincore_rpc; use bitcoincore_rpc::{Auth, Client, RpcApi}; fn main() { let rpc = Client::new("http://localhost:32369".to_string(), Auth::UserPass("<FILL RPC USERNAME>".to_string(), "<FILL RPC PASSWORD>".to_string())).unwrap(); let best_block_hash = rpc.get_best_block_hash().unwrap(); println!("best block hash: {}", best_block_hash); } go back to the tutorial directory and build. Takes a little time to get all the dependencies for the crate we added. cd .. cargo build now we have a target directory with our binary. go to the target/debug directory to find our new binary and run tutorial to see what happens. cd target cd debug ./tutorial and we get the best block hash ~/projects/tutorial/target/debug$ ./tutorial best block hash: 000000001774afce834c68b8dca5c96724e2784569d0c213b36f264d25186145  1 Quote Link to comment Share on other sites More sharing options...
buzzkillb Posted April 17, 2020 Author Report Share Posted April 17, 2020 Reading a chapter in the book each night and then also looking at any examples I can find to mess around on so I hit what I am trying to do from both directions. I found this example that allows me to query the Denarius RPC and get the response into a vector. What I am trying to do is get each part of the getinfo RPC JSON into a variable and then do something with that. main.rs  use std::io::Read; use curl::easy::Easy; fn main() { let mut body = r#"{"jsonrpc":"2.0","method":"getinfo","params":[],"id":1337}"#.as_bytes(); let mut easy = Easy::new(); easy.url("http://127.0.0.1:32369").unwrap(); easy.username("<rpc_username>").unwrap(); easy.password("<rpc_password>").unwrap(); easy.post(true).unwrap(); easy.post_field_size(body.len() as u64).unwrap(); let mut data = Vec::new(); { // Create transfer in separate scope ... let mut transfer = easy.transfer(); // Request body transfer.read_function(|buf| { Ok(body.read(buf).unwrap_or(0)) }).unwrap(); // Response body transfer.write_function(|new_data| { data.extend_from_slice(new_data); Ok(new_data.len()) }).unwrap(); transfer.perform().unwrap(); // .. to force drop it here, so we can use easy.response_code() } if !data.is_empty() { println!("As string: {}", String::from_utf8_lossy(&data)); } } Since I am still new to how this works I was trying to find out what's inside of each vector. I added this below the println! to find out. let first_half = &data[0..10]; assert_eq!(first_half.len(), 10); println!("{:?}", String::from_utf8_lossy(first_half)); which gives "{\"result\":" So going through 0 to 9 of the vector is giving me a piece of the puzzle. Without the String::from_utf8_lossy line I get [123, 34, 114, 101, 115, 117, 108, 116, 34, 58] Now how to change this into something I can use? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.