From 4754ad8ebf31378d1060ffde12f18dd0781a50d5 Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Fri, 24 Jan 2025 15:47:00 +0100 Subject: [PATCH] rusk: HTTP - add endpoint for query account status Resolves #3422 --- rusk/CHANGELOG.md | 5 +++++ rusk/src/lib/http/rusk.rs | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/rusk/CHANGELOG.md b/rusk/CHANGELOG.md index f072e7cae1..b303cce770 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:
/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. +[#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 0ecf80f42e..4b101bdb18 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 { + 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))