Jump to content

My Dive into Rust


Recommended Posts

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

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

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

and we get the best block hash

~/projects/tutorial/target/debug$ ./tutorial
best block hash: 000000001774afce834c68b8dca5c96724e2784569d0c213b36f264d25186145


  • Like 1
Link to comment
Share on other sites

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.


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.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| {

        // Response body
        transfer.write_function(|new_data| {

        // .. 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


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?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...