Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some apis #8

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl BitcoinRpc {
}

// net

rpc_method! {
/// Returns the number of connections to other nodes.
pub fn getconnectioncount(&self) -> RpcResult<u64>;
Expand Down Expand Up @@ -243,6 +243,72 @@ impl BitcoinRpc {
rpc_method! {
pub fn getnetworkinfo(&self) -> RpcResult<net::NetworkInfo>;
}

/// Mark a block as invalid by `block_hash`
pub fn invalidate_block(&self, block_hash: &Sha256dHash) -> RpcResult<()> {
let v: () = rpc_request!(
&self.client,
"invalidateblock".to_string(),
vec![block_hash.to_string().into()]
);
Ok(v)
}

/// Get the hex-consensus-encoded block by `block_hash`
pub fn get_block(&self, block_hash: &Sha256dHash) -> RpcResult<String> {
let v: String = rpc_request!(
&self.client,
"getblock".to_string(),
vec![block_hash.to_string().into(), 0.into()]
);
Ok(v)
}

/// Generate new address under own control
pub fn get_new_address(&self, account: String) -> RpcResult<String> {
let v: String = rpc_request!(
&self.client,
"getnewaddress".to_string(),
vec![account.into()]
);
Ok(v)
}

/// Dump private key of an `address`
pub fn dump_priv_key(&self, address: String) -> RpcResult<String> {
let v: String = rpc_request!(
&self.client,
"dumpprivkey".to_string(),
vec![address.into()]
);
Ok(v)
}

/// Mine `block_num` blocks and pay coinbase to `address`
///
/// Returns hashes of the generated blocks
pub fn generate_to_address(&self, block_num: u64, address: String) -> RpcResult<Vec<Sha256dHash>> {
let v : Vec<String> = rpc_request!(
&self.client,
"generatetoaddress".to_string(),
vec![block_num.into(), address.into()]
);


Ok(v.into_iter().map(|v| Sha256dHash::from_hex(&v).unwrap()).collect())
}


/// Get block hash at a given height
pub fn get_blockhash(&self, height: u64) -> RpcResult<Sha256dHash> {
let v: String = rpc_request!(
&self.client,
"getblockhash".to_string(),
vec![height.into()]
);

Ok(Sha256dHash::from_hex(&v).unwrap())
}
}

/// The error type for bitcoin JSON-RPC operations.
Expand Down