Repository: dusk-network/rusk (language: rust)

Issue:
Querying the balance of an address via the local node



Discussion / hints:

Fix patch:
diff --git a/rusk/CHANGELOG.md b/rusk/CHANGELOG.md
index f072e7cae..b303cce77 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/account:<address>/status` endpoint [#3422]
+
 ### Changed
 
 - Change dependency declaration to not require strict equal [#3405]
@@ -296,6 +300,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.
 
 <!-- Issues -->
+[#3422]: https://github.com/dusk-network/rusk/issues/3422
 [#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 0ecf80f42..4b101bdb1 100644
--- a/rusk/src/lib/http/rusk.rs
+++ b/rusk/src/lib/http/rusk.rs
@@ -7,12 +7,14 @@
 use super::event::Event;
 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;
@@ -44,6 +46,7 @@ impl HandleRequest for Rusk {
         match request.uri.inner() {
             ("contracts", Some(_), _) => true,
             ("node", _, "provisioners") => true,
+            ("account", Some(_), "status") => true,
             ("node", _, "crs") => true,
             _ => false,
         }
@@ -59,6 +62,8 @@ impl HandleRequest for Rusk {
                 self.handle_contract_query(contract_id, method, data, feeder)
             }
             ("node", _, "provisioners") => self.get_provisioners(),
+
+            ("account", Some(pk), "status") => self.get_account(pk),
             ("node", _, "crs") => self.get_crs(),
             _ => Err(anyhow::anyhow!("Unsupported")),
         }
@@ -151,6 +156,24 @@ impl Rusk {
         Ok(ResponseData::new(serde_json::to_value(prov)?))
     }
 
+    fn get_account(&self, pk: &str) -> anyhow::Result<ResponseData> {
+        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<ResponseData> {
         let crs = rusk_profile::get_common_reference_string()?;
         Ok(ResponseData::new(crs).with_header("crs-hash", CRS_17_HASH))


Test patch: