1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use holochain_core_types::{self, entry::Entry, entry_type::EntryType, hash::HashString};
use holochain_dna::Dna;
use holochain_wasm_utils::api_serialization::HashEntryArgs;
use multihash::Hash as Multihash;
use nucleus::ribosome::Runtime;
use serde_json;
use std::str::FromStr;
use wasmi::{RuntimeArgs, RuntimeValue, Trap};
pub fn get_entry_type(dna: &Dna, entry_type_name: &str) -> Result<EntryType, Option<RuntimeValue>> {
let entry_type = EntryType::from_str(&entry_type_name).map_err(|_| {
Some(RuntimeValue::I32(
holochain_core_types::error::RibosomeErrorCode::UnknownEntryType as i32,
))
})?;
if entry_type.is_app() {
let result = dna.get_entry_type_def(entry_type_name);
if result.is_none() {
return Err(Some(RuntimeValue::I32(
holochain_core_types::error::RibosomeErrorCode::UnknownEntryType as i32,
)));
}
}
Ok(entry_type)
}
pub fn invoke_hash_entry(
runtime: &mut Runtime,
args: &RuntimeArgs,
) -> Result<Option<RuntimeValue>, Trap> {
let args_str = runtime.load_utf8_from_args(&args);
let input: HashEntryArgs = match serde_json::from_str(&args_str) {
Ok(input) => input,
Err(_) => return ribosome_error_code!(ArgumentDeserializationFailed),
};
let dna = runtime
.context
.state()
.unwrap()
.nucleus()
.dna()
.expect("Should have DNA");
let maybe_entry_type = get_entry_type(&dna, &input.entry_type_name);
if let Err(err) = maybe_entry_type {
return Ok(err);
}
let entry_type = maybe_entry_type.unwrap();
let entry = Entry::new(&entry_type, &input.entry_value);
let hash = HashString::encode_from_serializable(&entry, Multihash::SHA2256);
runtime.store_utf8(&String::from(hash))
}