Skip to content

Commit

Permalink
[fix] #159: Register domain and query all domains
Browse files Browse the repository at this point in the history
Signed-off-by: Sam H. Smith <sam.henning.smith@protonmail.com>
  • Loading branch information
SamHSmith committed Feb 12, 2024
1 parent 280422d commit 158350a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs-recipes/3.register-domain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import iroha

key_pair = iroha.KeyPair.from_json("""
{
"public_key": "ed01207233BFC89DCBD68C19FDE6CE6158225298EC1131B6A130D1AEB454C1AB5183C0",
"private_key": {
"digest_function": "ed25519",
"payload": "9ac47abf59b356e0bd7dcbbbb4dec080e302156a48ca907e47cb6aea1d32719e7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"
}
}
""")

account_id = "alice@wonderland"
web_login = "mad_hatter"
password = "ilovetea"
api_url = "http://127.0.0.1:8080/"
telemetry_url = "http://127.0.0.1:8180/"

client = iroha.Client.create(
key_pair,
account_id,
web_login,
password,
api_url)

domains = client.query_all_domains()

print("Listing all domains...")
for d in domains:
print(" - ", d,)

if "looking_glass" in domains:
print("'looking_glass' domain already exists.")

register = iroha.Instruction.register_domain("looking_glass")

client.submit_executable([register])

while True:
domains = client.query_all_domains()

if "looking_glass" in domains:
break

print("Domain 'looking_glass' has been registered.")
print("Listing all domains...")
for d in domains:
print(" - ", d,)
19 changes: 19 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ impl Client {
.map(|hash| hash.to_string())
.map_err(|e| PyRuntimeError::new_err(format!("Error submitting instruction: {}", e)))
}

fn query_all_domains(&self) -> PyResult<Vec<String>> {
let query = iroha_data_model::query::prelude::FindAllDomains {};

let val = self.client.request(query)
.map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?;

let mut items = Vec::new();
for item in val {
items.push(
item
.map(|d| d.id.to_string())
.map_err(|e|
PyRuntimeError::new_err(format!("{e:?}"))
)?
);
}
Ok(items)
}
}

macro_rules! register_query {
Expand Down
15 changes: 15 additions & 0 deletions src/isi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use iroha_data_model::isi::{
InstructionExpr, MintExpr, RegisterExpr, TransferExpr, UnregisterExpr,
};
use iroha_data_model::domain::NewDomain;
use iroha_data_model::prelude::DomainId;
use iroha_data_model::NumericValue;
use pyo3::{exceptions::PyValueError, prelude::*};

use std::str::FromStr;

use crate::data_model::account::{PyAccountId, PyNewAccount};
use crate::data_model::asset::{PyAssetDefinitionId, PyAssetId, PyNewAssetDefinition};
use crate::data_model::domain::{PyDomainId, PyNewDomain};
Expand Down Expand Up @@ -40,6 +44,17 @@ impl PyInstruction {
"Only registration of accounts, asset definitions and domains is supported",
))
}

#[staticmethod]
/// Create an instruction for registering a new domain.
fn register_domain(domain_id: &str) -> PyResult<PyInstruction> {
let new_domain_object = NewDomain {
id: DomainId::from_str(domain_id).map_err(|e| PyValueError::new_err(e.to_string()))?,
logo: None,
metadata: Default::default(),
};
return Ok(PyInstruction(InstructionExpr::Register(RegisterExpr::new(new_domain_object))));
}

#[staticmethod]
/// Create an instruction for un-registering an object.
Expand Down

0 comments on commit 158350a

Please sign in to comment.