essential_app_utils/
inputs.rs1use essential_sign::secp256k1::{ecdsa::RecoverableSignature, PublicKey};
2use essential_types::{
3 convert::word_4_from_u8_32, solution::Mutation, ContentAddress, Hash, Key, PredicateAddress,
4 Value, Word,
5};
6
7#[derive(Clone)]
8pub struct Instance {
9 pub address: PredicateAddress,
10 pub path: Word,
11}
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub struct Int(pub Word);
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct B256(pub [Word; 4]);
18
19pub fn index_key(index: Word, key: Key) -> Key {
20 let mut k = vec![index];
21 k.extend(key);
22 k
23}
24
25pub fn index_mutation(index: Word, value: Value) -> Mutation {
26 Mutation {
27 key: vec![index],
28 value,
29 }
30}
31
32impl B256 {
33 pub fn to_key(&self) -> Key {
34 self.0.to_vec()
35 }
36
37 pub fn to_value(&self) -> Value {
38 self.0.to_vec()
39 }
40}
41
42impl Int {
43 pub fn to_key(&self) -> Key {
44 vec![self.0]
45 }
46
47 pub fn to_value(&self) -> Value {
48 vec![self.0]
49 }
50}
51
52impl From<Hash> for B256 {
53 fn from(value: Hash) -> Self {
54 Self(essential_types::convert::word_4_from_u8_32(value))
55 }
56}
57
58impl From<[Word; 4]> for B256 {
59 fn from(value: [Word; 4]) -> Self {
60 Self(value)
61 }
62}
63
64impl From<Word> for Int {
65 fn from(value: Word) -> Self {
66 Self(value)
67 }
68}
69
70impl From<ContentAddress> for B256 {
71 fn from(value: ContentAddress) -> Self {
72 Self(word_4_from_u8_32(value.0))
73 }
74}
75
76trait Slots {
77 fn to_slot<I>(&mut self, iter: I)
78 where
79 I: IntoIterator<Item = Word>;
80}
81
82pub trait WriteDecVars {
83 fn write_dec_var(&self, decision_variables: &mut Vec<Value>);
84}
85
86impl WriteDecVars for B256 {
87 fn write_dec_var(&self, decision_variables: &mut Vec<Value>) {
88 decision_variables.to_slot(self.0);
89 }
90}
91
92impl WriteDecVars for Int {
93 fn write_dec_var(&self, decision_variables: &mut Vec<Value>) {
94 decision_variables.push(vec![self.0]);
95 }
96}
97
98impl WriteDecVars for PredicateAddress {
99 fn write_dec_var(&self, decision_variables: &mut Vec<Value>) {
100 let mut slot = Vec::new();
101 self.contract.write_dec_var(&mut slot);
102 self.predicate.write_dec_var(&mut slot);
103 decision_variables.to_slot(slot.into_iter().flatten());
104 }
105}
106
107impl WriteDecVars for ContentAddress {
108 fn write_dec_var(&self, decision_variables: &mut Vec<Value>) {
109 decision_variables.to_slot(word_4_from_u8_32(self.0));
110 }
111}
112
113impl WriteDecVars for Instance {
114 fn write_dec_var(&self, decision_variables: &mut Vec<Value>) {
115 let mut slot = Vec::new();
116 self.address.contract.write_dec_var(&mut slot);
117 self.address.predicate.write_dec_var(&mut slot);
118 Int::from(self.path).write_dec_var(&mut slot);
119 decision_variables.to_slot(slot.into_iter().flatten());
120 }
121}
122
123impl Slots for Vec<Value> {
124 fn to_slot<I>(&mut self, iter: I)
125 where
126 I: IntoIterator<Item = Word>,
127 {
128 self.push(iter.into_iter().collect());
129 }
130}
131
132pub trait Encode {
133 type Output;
134
135 fn encode(&self) -> Self::Output;
136}
137
138impl Encode for RecoverableSignature {
139 type Output = ([Word; 4], [Word; 4], Word);
140
141 fn encode(&self) -> Self::Output {
143 let [a0, a1, a2, a3, b0, b1, b2, b3, c1] = essential_sign::encode::signature(self);
144 ([a0, a1, a2, a3], [b0, b1, b2, b3], c1)
145 }
146}
147
148impl Encode for PublicKey {
149 type Output = ([Word; 4], Word);
150
151 fn encode(&self) -> Self::Output {
153 let [a0, a1, a2, a3, b0] = essential_sign::encode::public_key(self);
154 ([a0, a1, a2, a3], b0)
155 }
156}
157
158impl Encode for ContentAddress {
159 type Output = [Word; 4];
160
161 fn encode(&self) -> Self::Output {
162 word_4_from_u8_32(self.0)
163 }
164}
165
166impl Encode for PredicateAddress {
167 type Output = ([Word; 4], [Word; 4]);
168
169 fn encode(&self) -> Self::Output {
170 (self.contract.encode(), self.predicate.encode())
171 }
172}