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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#[cfg(doc)]
use casper_types::{
account::{Account, AccountHash},
Contract, HashAddr,
};
use casper_types::{Key, URef};
use crate::{cli::CliError, rpcs::DictionaryItemIdentifier};
/// Various ways of uniquely identifying a dictionary item.
pub enum DictionaryItemStrParams<'a> {
/// A dictionary item identified via an [`Account`]'s named keys.
AccountNamedKey {
/// The [`AccountHash`] as a formatted string, identifying the account whose named keys
/// contains `dictionary_name`.
account_hash: &'a str,
/// The named key under which the dictionary seed `URef` is stored.
dictionary_name: &'a str,
/// The key within the dictionary under which the item is held.
dictionary_item_key: &'a str,
},
/// A dictionary item identified via a [`Contract`]'s named keys.
ContractNamedKey {
/// The [`HashAddr`] as a formatted string, identifying the contract whose named keys
/// contains `dictionary_name`.
hash_addr: &'a str,
/// The named key under which the dictionary seed `URef` is stored.
dictionary_name: &'a str,
/// The key within the dictionary under which the item is held.
dictionary_item_key: &'a str,
},
/// A dictionary item identified via a [`casper_types::AddressableEntity`]'s named keys.
EntityNamedKey {
/// The [`casper_types::EntityAddr`] as a formatted string, identifying the entity whose named keys
/// contains `dictionary_name`.
entity_addr: &'a str,
/// The named key under which the dictionary seed `URef` is stored.
dictionary_name: &'a str,
/// The key within the dictionary under which the item is held.
dictionary_item_key: &'a str,
},
/// A dictionary item identified via its seed [`URef`].
URef {
/// The dictionary's seed `URef` as a formatted string.
seed_uref: &'a str,
/// The key within the dictionary under which the item is held.
dictionary_item_key: &'a str,
},
/// A dictionary item identified via its unique address derived from the dictionary's seed
/// `URef` and the item's key within the dictionary. The key must be a `Key::Dictionary`
/// variant as a formatted string.
Dictionary(&'a str),
}
impl<'a> TryFrom<DictionaryItemStrParams<'a>> for DictionaryItemIdentifier {
type Error = CliError;
fn try_from(
params: DictionaryItemStrParams<'a>,
) -> Result<DictionaryItemIdentifier, Self::Error> {
match params {
DictionaryItemStrParams::AccountNamedKey {
account_hash,
dictionary_name,
dictionary_item_key,
} => {
let key = Key::from_formatted_str(account_hash).map_err(|error| {
CliError::FailedToParseKey {
context: "dictionary item account named key",
error,
}
})?;
let account_hash = key.into_account().ok_or(CliError::InvalidArgument {
context: "dictionary item account named key",
error: "not an account hash".to_string(),
})?;
Ok(DictionaryItemIdentifier::new_from_account_info(
account_hash,
dictionary_name.to_string(),
dictionary_item_key.to_string(),
))
}
DictionaryItemStrParams::ContractNamedKey {
hash_addr,
dictionary_name,
dictionary_item_key,
} => {
let key = Key::from_formatted_str(hash_addr).map_err(|error| {
CliError::FailedToParseKey {
context: "dictionary item contract named key",
error,
}
})?;
let hash_addr = key.into_hash_addr().ok_or(CliError::InvalidArgument {
context: "dictionary item contract named key",
error: "not a hash-addr".to_string(),
})?;
Ok(DictionaryItemIdentifier::new_from_contract_info(
hash_addr,
dictionary_name.to_string(),
dictionary_item_key.to_string(),
))
}
DictionaryItemStrParams::EntityNamedKey {
entity_addr,
dictionary_name,
dictionary_item_key,
} => {
let key = Key::from_formatted_str(entity_addr).map_err(|error| {
CliError::FailedToParseKey {
context: "dictionary item entity named key",
error,
}
})?;
let entity_addr = if let Key::AddressableEntity(addr) = key {
addr
} else {
return Err(CliError::InvalidArgument {
context: "dictionary item entity named key",
error: "not a entity-addr".to_string(),
});
};
Ok(DictionaryItemIdentifier::new_from_entity_info(
entity_addr,
dictionary_name.to_string(),
dictionary_item_key.to_string(),
))
}
DictionaryItemStrParams::URef {
seed_uref,
dictionary_item_key,
} => {
let seed_uref = URef::from_formatted_str(seed_uref).map_err(|error| {
CliError::FailedToParseURef {
context: "dictionary item uref",
error,
}
})?;
Ok(DictionaryItemIdentifier::new_from_seed_uref(
seed_uref,
dictionary_item_key.to_string(),
))
}
DictionaryItemStrParams::Dictionary(address) => {
let key = Key::from_formatted_str(address).map_err(|error| {
CliError::FailedToParseKey {
context: "dictionary item address",
error,
}
})?;
Ok(DictionaryItemIdentifier::new_from_item_key(key)?)
}
}
}
}