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
use essential_types::{
    convert::word_4_from_u8_32, solution::Mutation, ContentAddress, Hash, Key, PredicateAddress,
    Value, Word,
};

#[derive(Clone)]
pub struct Instance {
    pub address: PredicateAddress,
    pub path: Word,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Int(pub Word);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct B256(pub [Word; 4]);

pub fn index_key(index: Word, key: Key) -> Key {
    let mut k = vec![index];
    k.extend(key);
    k
}

pub fn index_mutation(index: Word, value: Value) -> Mutation {
    Mutation {
        key: vec![index],
        value,
    }
}

impl B256 {
    pub fn to_key(&self) -> Key {
        self.0.to_vec()
    }

    pub fn to_value(&self) -> Value {
        self.0.to_vec()
    }
}

impl Int {
    pub fn to_key(&self) -> Key {
        vec![self.0]
    }

    pub fn to_value(&self) -> Value {
        vec![self.0]
    }
}

impl From<Hash> for B256 {
    fn from(value: Hash) -> Self {
        Self(essential_types::convert::word_4_from_u8_32(value))
    }
}

impl From<[Word; 4]> for B256 {
    fn from(value: [Word; 4]) -> Self {
        Self(value)
    }
}

impl From<Word> for Int {
    fn from(value: Word) -> Self {
        Self(value)
    }
}

impl From<ContentAddress> for B256 {
    fn from(value: ContentAddress) -> Self {
        Self(word_4_from_u8_32(value.0))
    }
}

trait Slots {
    fn to_slot<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = Word>;
}

pub trait WriteDecVars {
    fn write_dec_var(&self, decision_variables: &mut Vec<Value>);
}

impl WriteDecVars for B256 {
    fn write_dec_var(&self, decision_variables: &mut Vec<Value>) {
        decision_variables.to_slot(self.0);
    }
}

impl WriteDecVars for Int {
    fn write_dec_var(&self, decision_variables: &mut Vec<Value>) {
        decision_variables.push(vec![self.0]);
    }
}

impl WriteDecVars for essential_signer::secp256k1::ecdsa::RecoverableSignature {
    fn write_dec_var(&self, decision_variables: &mut Vec<Value>) {
        let sig = essential_sign::encode::signature(self);
        decision_variables.to_slot(sig);
    }
}

impl WriteDecVars for essential_signer::secp256k1::PublicKey {
    fn write_dec_var(&self, decision_variables: &mut Vec<Value>) {
        let k = essential_sign::encode::public_key(self);
        decision_variables.to_slot(k);
    }
}

impl WriteDecVars for PredicateAddress {
    fn write_dec_var(&self, decision_variables: &mut Vec<Value>) {
        let mut slot = Vec::new();
        self.contract.write_dec_var(&mut slot);
        self.predicate.write_dec_var(&mut slot);
        decision_variables.to_slot(slot.into_iter().flatten());
    }
}

impl WriteDecVars for ContentAddress {
    fn write_dec_var(&self, decision_variables: &mut Vec<Value>) {
        decision_variables.to_slot(word_4_from_u8_32(self.0));
    }
}

impl WriteDecVars for Instance {
    fn write_dec_var(&self, decision_variables: &mut Vec<Value>) {
        let mut slot = Vec::new();
        self.address.contract.write_dec_var(&mut slot);
        self.address.predicate.write_dec_var(&mut slot);
        Int::from(self.path).write_dec_var(&mut slot);
        decision_variables.to_slot(slot.into_iter().flatten());
    }
}

impl Slots for Vec<Value> {
    fn to_slot<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = Word>,
    {
        self.push(iter.into_iter().collect());
    }
}