diff --git a/rusk/CHANGELOG.md b/rusk/CHANGELOG.md index fb0bebda3..36b5789ff 100644 --- a/rusk/CHANGELOG.md +++ b/rusk/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Added + +- Add `/on/node/
/account` endpoint [#3414] + ### Changed - Change dependency declaration to not require strict equal [#3405] @@ -300,6 +304,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add build system that generates keys for circuits and caches them. +[#3414]: https://github.com/dusk-network/rusk/issues/3414 [#3405]: https://github.com/dusk-network/rusk/issues/3405 [#3359]: https://github.com/dusk-network/rusk/issues/3359 [#3206]: https://github.com/dusk-network/rusk/issues/3206 diff --git a/rusk/src/lib/http/rusk.rs b/rusk/src/lib/http/rusk.rs index 8fedb8a6e..a72351a35 100644 --- a/rusk/src/lib/http/rusk.rs +++ b/rusk/src/lib/http/rusk.rs @@ -6,12 +6,14 @@ use super::*; -use dusk_bytes::Serializable; +use dusk_bytes::{DeserializableSlice, Serializable}; use dusk_core::abi::ContractId; +use dusk_core::signatures::bls::PublicKey as BlsPublicKey; use dusk_core::stake::StakeFundOwner; use node::vm::VMExecution; use rusk_profile::CRS_17_HASH; use serde::Serialize; +use serde_json::json; use std::sync::{mpsc, Arc}; use std::thread; use tokio::task; @@ -28,6 +30,7 @@ impl HandleRequest for Rusk { match request.uri.inner() { ("contracts", Some(_), _) => true, ("node", _, "provisioners") => true, + ("node", Some(_), "account") => true, ("node", _, "crs") => true, _ => false, } @@ -43,6 +46,8 @@ impl HandleRequest for Rusk { self.handle_contract_query(contract_id, method, data, feeder) } ("node", _, "provisioners") => self.get_provisioners(), + + ("node", Some(pk), "account") => self.get_account(pk), ("node", _, "crs") => self.get_crs(), _ => Err(anyhow::anyhow!("Unsupported")), } @@ -107,6 +112,24 @@ impl Rusk { Ok(ResponseData::new(serde_json::to_value(prov)?)) } + fn get_account(&self, pk: &str) -> anyhow::Result { + let pk = bs58::decode(pk) + .into_vec() + .map_err(|_| anyhow::anyhow!("Invalid bs58 account"))?; + let pk = BlsPublicKey::from_slice(&pk) + .map_err(|_| anyhow::anyhow!("Invalid bls account"))?; + let account = self + .account(&pk) + .map(|account| { + json!({ + "balance": account.balance, + "nonce": account.nonce, + }) + }) + .map_err(|e| anyhow::anyhow!("Cannot query the state {e:?}"))?; + Ok(ResponseData::new(account)) + } + fn get_crs(&self) -> anyhow::Result { let crs = rusk_profile::get_common_reference_string()?; Ok(ResponseData::new(crs).with_header("crs-hash", CRS_17_HASH))