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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "pwasm", feature(alloc))]
#[cfg(feature = "std")]
extern crate cid;
#[cfg(feature = "std")]
extern crate integer_encoding;
#[cfg(feature = "std")]
extern crate multibase;
#[cfg(feature = "std")]
extern crate multihash;
#[cfg(feature = "std")]
extern crate prost;
#[cfg(feature = "std")]
#[macro_use]
extern crate prost_derive;
#[cfg(feature = "std")]
extern crate rustc_hex;
#[cfg(feature = "std")]
extern crate serde;
#[cfg(feature = "std")]
extern crate serde_bytes;
#[cfg(feature = "std")]
#[macro_use]
extern crate serde_derive;
#[cfg(feature = "std")]
extern crate varint;
#[cfg(feature = "pwasm")]
extern crate pwasm_std;
#[cfg(feature = "web3_compat")]
extern crate web3;
#[cfg(feature = "std")]
use cid::{Cid, Codec, Error as CidError, Version};
#[cfg(feature = "std")]
use integer_encoding::VarIntReader;
pub mod prelude {
pub use ontology::*;
#[cfg(feature = "std")]
pub use ontology::compact::*;
#[cfg(feature = "std")]
pub use ontology::v0::*;
#[cfg(feature = "web3_compat")]
pub use ontology::web3::*;
}
pub mod ontology {
#[cfg(feature = "std")]
use multihash::encode;
#[cfg(feature = "std")]
use multihash::Hash;
#[cfg(feature = "std")]
use cid::{Cid, Codec, Error as CidError, ToCid, Version};
#[cfg(feature = "std")]
use serde::de::{Deserialize, Deserializer};
#[cfg(feature = "std")]
use prost::Message;
#[cfg(feature = "web3_compat")]
use self::web3::{FromABIV2Response, FromABIV2ResponseHinted};
#[cfg(feature = "pwasm")]
use pwasm_std::*;
pub trait Canonicalize {
fn canonicalize(&mut self);
}
pub trait AssociatedCodec {
const CODEC_CODE: u64;
}
include!(concat!(env!("OUT_DIR"), "/rlay.ontology.entities.rs"));
include!("./rlay.ontology.macros.rs");
include!(concat!(env!("OUT_DIR"), "/rlay.ontology.macros_applied.rs"));
impl_canonicalize!(Annotation; annotations);
impl EntityKind {
pub fn from_event_name(event_name: &str) -> Result<Self, ()> {
let name = event_name.replace("Stored", "");
Self::from_name(&name)
}
pub fn retrieve_fn_name(&self) -> String {
format!("retrieve{}", Into::<&str>::into(self))
}
}
impl Entity {
#[cfg(feature = "std")]
pub fn to_bytes(&self) -> Vec<u8> {
self.to_cid().unwrap().to_bytes()
}
pub fn get_subject(&self) -> Option<&Vec<u8>> {
match &self {
Entity::ClassAssertion(ent) => Some(ent.get_subject()),
Entity::NegativeClassAssertion(ent) => Some(ent.get_subject()),
_ => None,
}
}
pub fn as_class_assertion(&self) -> Option<&ClassAssertion> {
match *self {
Entity::ClassAssertion(ref val) => Some(&*val),
_ => None,
}
}
pub fn as_negative_class_assertion(&self) -> Option<&NegativeClassAssertion> {
match *self {
Entity::NegativeClassAssertion(ref val) => Some(&*val),
_ => None,
}
}
}
pub use self::custom::*;
#[cfg(feature = "web3_compat")]
pub mod web3 {
use super::*;
use rustc_hex::{FromHex, ToHex};
use serde::de::{self, Deserialize, Deserializer, MapAccess, Visitor};
use web3::types::U256;
struct HexString<'a> {
pub inner: &'a [u8],
}
impl<'a> HexString<'a> {
pub fn wrap(bytes: &'a [u8]) -> Self {
HexString { inner: bytes }
}
pub fn wrap_option(bytes: Option<&'a Vec<u8>>) -> Option<Self> {
match bytes {
Some(bytes) => Some(HexString { inner: bytes }),
None => None,
}
}
}
impl<'a> ::serde::Serialize for HexString<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
let hex: String = self.inner.to_hex();
Ok(try!(serializer.serialize_str(&format!("0x{}", &hex))))
}
}
pub trait FormatWeb3<'a> {
type Formatted: serde::Deserialize<'a> + serde::Serialize;
fn to_web3_format(self) -> Self::Formatted;
fn from_web3_format(formatted: Self::Formatted) -> Self;
}
fn decode_bytes(bytes: &[u8]) -> Vec<u8> {
let length = U256::from_big_endian(&bytes[0..32]);
bytes[((32) as usize)..((length).as_u64() as usize + 32)].to_owned()
}
fn decode_bytes_array(bytes: &[u8]) -> Vec<Vec<u8>> {
let num_elements = U256::from_big_endian(&bytes[0..32]);
let element_offsets: Vec<U256> = (0..num_elements.as_u64())
.map(|element_i| {
let element_data_offset = U256::from_big_endian(
&bytes[(32 * (element_i + 1) as usize)..(32 * (element_i + 2) as usize)],
);
element_data_offset + Into::<U256>::into(32)
})
.collect();
element_offsets
.into_iter()
.map(|element_start_offset| {
decode_bytes(&bytes[(element_start_offset.as_u64() as usize)..bytes.len()])
})
.collect()
}
fn to_option_bytes(bytes: Vec<u8>) -> Option<Vec<u8>> {
match bytes.len() {
0 => None,
_ => Some(bytes),
}
}
pub trait FromABIV2Response {
fn from_abiv2(bytes: &[u8]) -> Self;
}
pub trait FromABIV2ResponseHinted {
fn from_abiv2(bytes: &[u8], kind: &EntityKind) -> Self;
}
macro_rules! decode_offset {
($bytes_var:ident, $offset_var:ident, $start:expr, $end:expr) => (
let $offset_var = U256::from_big_endian(&$bytes_var[$start..$end]);
);
}
macro_rules! decode_param {
(bytes_array; $bytes_var:ident, $param_var:ident, $start:expr, $end:expr) => (
let $param_var = decode_bytes_array(
&$bytes_var[($start.as_u64() as usize)..($end.as_u64() as usize)],
);
);
(bytes_array; $bytes_var:ident, $param_var:ident, $start:expr) => (
let $param_var = decode_bytes_array(
&$bytes_var[($start.as_u64() as usize)..$bytes_var.len()],
);
);
(bytes; $bytes_var:ident, $param_var:ident, $start:expr, $end:expr) => (
let $param_var = decode_bytes(
&$bytes_var[($start.as_u64() as usize)..($end.as_u64() as usize)],
);
);
(bytes; $bytes_var:ident, $param_var:ident, $start:expr) => (
let $param_var = decode_bytes(
&$bytes_var[($start.as_u64() as usize)..$bytes_var.len()],
);
);
}
include!(concat!(env!("OUT_DIR"), "/rlay.ontology.web3_applied.rs"));
}
#[cfg(feature = "std")]
pub mod compact {
use super::*;
pub trait FormatCompact<'a> {
type Formatted: serde::Deserialize<'a> + serde::Serialize;
fn to_compact_format(self) -> Self::Formatted;
fn from_compact_format(formatted: Self::Formatted) -> Self;
}
include!(concat!(env!("OUT_DIR"), "/rlay.ontology.compact.rs"));
}
mod custom {
use super::*;
pub trait GetAssertionComplement {
type Complement;
fn get_assertion_complement(&self) -> Self::Complement;
}
impl GetAssertionComplement for ClassAssertion {
type Complement = NegativeClassAssertion;
fn get_assertion_complement(&self) -> Self::Complement {
NegativeClassAssertion {
annotations: vec![],
subject: self.subject.clone(),
class: self.class.clone(),
}
}
}
impl GetAssertionComplement for NegativeClassAssertion {
type Complement = ClassAssertion;
fn get_assertion_complement(&self) -> Self::Complement {
ClassAssertion {
annotations: vec![],
subject: self.subject.clone(),
class: self.class.clone(),
}
}
}
impl ClassAssertion {
pub fn get_subject(&self) -> &Vec<u8> {
&self.subject
}
}
impl NegativeClassAssertion {
pub fn get_subject(&self) -> &Vec<u8> {
&self.subject
}
}
}
#[cfg(feature = "std")]
pub mod v0 {
use super::*;
use ontology::compact::FormatCompact;
use integer_encoding::VarIntWriter;
use integer_encoding::VarIntReader;
include!(concat!(env!("OUT_DIR"), "/rlay.ontology.v0.rs"));
}
}
#[cfg(feature = "std")]
pub trait ToCidUnknown {
fn to_cid_unknown(&self, permitted: Option<u64>) -> Result<Cid, CidError>;
}
#[cfg(feature = "std")]
impl ToCidUnknown for String {
fn to_cid_unknown(&self, permitted: Option<u64>) -> Result<Cid, CidError> {
let bytes = multibase::decode(self).unwrap().1;
bytes.to_cid_unknown(permitted)
}
}
#[cfg(feature = "std")]
use std::io::Cursor;
#[cfg(feature = "std")]
impl ToCidUnknown for [u8] {
fn to_cid_unknown(&self, permitted: Option<u64>) -> Result<Cid, CidError> {
let mut cur = Cursor::new(self);
let raw_version = cur.read_varint()?;
let raw_codec = cur.read_varint()?;
let version = Version::from(raw_version)?;
match permitted {
Some(permitted) => {
if raw_codec != permitted {
return Err(CidError::UnknownCodec);
}
}
None => {}
}
let codec = Codec::Unknown(raw_codec);
let hash = &self[cur.position() as usize..];
multihash::decode(hash)?;
Ok(Cid::new(codec, version, hash))
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}