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
use cosmwasm_std::{Addr, Api, BlockInfo, CanonicalAddr, ContractInfo, Empty, Env, MemoryStorage, OwnedDeps, Querier, RecoverPubkeyError, StdError, StdResult, Timestamp, VerificationError, Order, Storage};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use std::collections::HashMap;
use std::marker::PhantomData;
use blake2::Blake2bVar;
use blake2::digest::{Update, VariableOutput};
use sha2::Digest as Sha2Digest;
use sha3::{Digest};

const CANONICAL_LENGTH: usize = 54;

pub fn create_lto_env() -> Env {
    Env {
        block: BlockInfo {
            height: 0,
            time: Timestamp::from_seconds(0),
            chain_id: "lto".to_string(),
        },
        contract: ContractInfo {
            address: Addr::unchecked(""),
        },
        transaction: None,
    }
}

pub fn load_lto_deps(state_dump: Option<IdbStateDump>) -> OwnedDeps<MemoryStorage, EmptyApi, EmptyQuerier, Empty> {
    match state_dump {
        None => OwnedDeps {
            storage: MemoryStorage::default(),
            api: EmptyApi::default(),
            querier: EmptyQuerier::default(),
            custom_query_type: PhantomData,
        },
        Some(dump) => {
            let idb_storage = IdbStorage::load(dump);
            OwnedDeps {
                storage: idb_storage.storage,
                api: EmptyApi::default(),
                querier: EmptyQuerier::default(),
                custom_query_type: PhantomData,
            }
        }
    }

}

/// takes a b58 of compressed secp256k1 pk
pub fn address_eip155(public_key: String) -> Result<Addr, StdError> {
    if public_key.is_empty() {
        return Err(StdError::not_found("empty input"));
    }

    // decode b58 pk
    let pk = bs58::decode(public_key.as_bytes()).into_vec();
    let decoded_pk = match pk {
        Ok(pk) => pk,
        Err(e) => return Err(StdError::generic_err(e.to_string())),
    };

    // instantiate secp256k1 public key from input
    let public_key = secp256k1::PublicKey::from_slice(decoded_pk.as_slice()).unwrap();
    let mut uncompressed_hex_pk = hex::encode(public_key.serialize_uncompressed());
    if uncompressed_hex_pk.starts_with("04") {
        uncompressed_hex_pk = uncompressed_hex_pk.split_off(2);
    }

    // pass the raw bytes to keccak256
    let uncompressed_raw_pk = hex::decode(uncompressed_hex_pk).unwrap();

    let mut hasher = sha3::Keccak256::new();
    hasher.input(uncompressed_raw_pk.as_slice());
    let hashed_addr = hex::encode(hasher.result().as_slice()).to_string();

    let result = &hashed_addr[hashed_addr.len() - 40..];
    let checksum_addr = "0x".to_owned() + eip_55_checksum(result).as_str();

    Ok(Addr::unchecked(checksum_addr))
}

fn eip_55_checksum(addr: &str) -> String {
    let mut checksum_hasher = sha3::Keccak256::new();
    checksum_hasher.input(&addr[addr.len() - 40..].as_bytes());
    let hashed_addr = hex::encode(checksum_hasher.result()).to_string();

    let mut checksum_buff = "".to_owned();
    let result_chars: Vec<char> = addr.chars()
        .into_iter()
        .collect();
    let keccak_chars: Vec<char> = hashed_addr.chars()
        .into_iter()
        .collect();
    for i in 0..addr.len() {
        let mut char = result_chars[i];
        if char.is_alphabetic() {
            let keccak_digit = keccak_chars[i]
                .to_digit(16)
                .unwrap();
            // if the corresponding hex digit >= 8, convert to uppercase
            if keccak_digit >= 8 {
                char = char.to_ascii_uppercase();
            }
        }
        checksum_buff += char.to_string().as_str();
    }

    checksum_buff
}

pub fn address_lto(network_id: char, public_key: String) -> Result<Addr, StdError> {
    if network_id != 'L' && network_id != 'T' {
        return Err(StdError::generic_err("unrecognized network_id"));
    }
    if bs58::decode(public_key.clone()).into_vec().is_err() {
        return Err(StdError::generic_err("invalid public key"));
    }

    // decode b58 of pubkey into byte array
    let public_key = bs58::decode(public_key).into_vec().unwrap();
    // get the ascii value from network char
    let network_id = network_id as u8;
    let pub_key_secure_hash = secure_hash(public_key.as_slice());
    // get the first 20 bytes of the securehash
    let address_bytes = &pub_key_secure_hash[0..20];
    let version = &1_u8.to_be_bytes();
    let checksum_input:Vec<u8> = [version, &[network_id], address_bytes].concat();

    // checksum is the first 4 bytes of secureHash of version, chain_id, and hash
    let checksum = &secure_hash(checksum_input.as_slice())
        .to_vec()[0..4];

    let addr_fields = [
        version,
        &[network_id],
        address_bytes,
        checksum
    ];

    let address: Vec<u8> = addr_fields.concat();
    Ok(Addr::unchecked(base58(address.as_slice())))
}

fn base58(input: &[u8]) -> String {
    bs58::encode(input).into_string()
}

fn secure_hash(m: &[u8]) -> Vec<u8> {
    let mut hasher = Blake2bVar::new(32).unwrap();
    hasher.update(m);
    let mut buf = [0u8; 32];
    hasher.finalize_variable(&mut buf).unwrap();

    // get the sha256 of blake
    let mut sha256_hasher = sha2::Sha256::new();
    Update::update(&mut sha256_hasher, buf.as_slice());
    let res = sha256_hasher.finalize();
    // let mut hasher = sha2::Sha256::new();
    // hasher.update(&buf);
    // let mut buf = hasher.finalize();
    res.to_vec()
}

pub struct IdbStorage {
    pub storage: MemoryStorage,
}

impl IdbStorage {
    pub fn load(idb: IdbStateDump) -> Self {
        let mut store = IdbStorage {
            storage: MemoryStorage::new(),
        };
        store.load_to_mem_storage(idb);
        store
    }

    pub fn load_to_mem_storage(&mut self, idb_state: IdbStateDump) {
        for (k, v) in idb_state.state_dump.into_iter() {
            self.storage.set(&k, &v);
        }
    }
}

#[serde_as]
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct IdbStateDump {
    // map of the indexed db key value pairs of the state object store
    #[serde_as(as = "Vec<(_, _)>")]
    pub state_dump: HashMap<Vec<u8>, Vec<u8>>,
}

impl IdbStateDump {
    pub fn from(store: MemoryStorage) -> IdbStateDump {
        let mut state: HashMap<Vec<u8>, Vec<u8>> = HashMap::new();

        for (key, value) in store.range(None,None, Order::Ascending) {
            state.insert(key, value);
        }
        IdbStateDump {
            state_dump: state,
        }
    }
}

// EmptyApi that is meant to conform the traits by the cosmwasm standard contract syntax. The functions of this implementation are not meant to be used or produce any sensible results.
#[derive(Copy, Clone)]
pub struct EmptyApi {
    /// Length of canonical addresses created with this API. Contracts should not make any assumtions
    /// what this value is.
    canonical_length: usize,
}

impl Default for EmptyApi {
    fn default() -> Self {
        EmptyApi {
            canonical_length: CANONICAL_LENGTH,
        }
    }
}

impl Api for EmptyApi {
    fn addr_validate(&self, human: &str) -> StdResult<Addr> {
        self.addr_canonicalize(human).map(|_canonical| ())?;
        Ok(Addr::unchecked(human))
    }

    fn addr_canonicalize(&self, human: &str) -> StdResult<CanonicalAddr> {
        // Dummy input validation. This is more sophisticated for formats like bech32, where format and checksum are validated.
        if human.len() < 3 {
            return Err(StdError::generic_err(
                "Invalid input: human address too short",
            ));
        }
        if human.len() > self.canonical_length {
            return Err(StdError::generic_err(
                "Invalid input: human address too long",
            ));
        }

        let mut out = Vec::from(human);

        // pad to canonical length with NULL bytes
        out.resize(self.canonical_length, 0x00);
        // // content-dependent rotate followed by shuffle to destroy
        // // the most obvious structure (https://github.com/CosmWasm/cosmwasm/issues/552)
        // let rotate_by = digit_sum(&out) % self.canonical_length;
        // out.rotate_left(rotate_by);
        // for _ in 0..SHUFFLES_ENCODE {
        //     out = riffle_shuffle(&out);
        // }
        Ok(out.into())
    }

    fn addr_humanize(&self, canonical: &CanonicalAddr) -> StdResult<Addr> {
        if canonical.len() != self.canonical_length {
            return Err(StdError::generic_err(
                "Invalid input: canonical address length not correct",
            ));
        }

        let tmp: Vec<u8> = canonical.clone().into();
        // // Shuffle two more times which restored the original value (24 elements are back to original after 20 rounds)
        // for _ in 0..SHUFFLES_DECODE {
        //     tmp = riffle_shuffle(&tmp);
        // }
        // // Rotate back
        // let rotate_by = digit_sum(&tmp) % self.canonical_length;
        // tmp.rotate_right(rotate_by);
        // Remove NULL bytes (i.e. the padding)
        let trimmed = tmp.into_iter().filter(|&x| x != 0x00).collect();
        // decode UTF-8 bytes into string
        let human = String::from_utf8(trimmed)?;
        Ok(Addr::unchecked(human))
    }

    fn secp256k1_verify(
        &self,
        _message_hash: &[u8],
        _signature: &[u8],
        _public_key: &[u8],
    ) -> Result<bool, VerificationError> {
        Err(VerificationError::unknown_err(0))
    }

    fn secp256k1_recover_pubkey(
        &self,
        _message_hash: &[u8],
        _signature: &[u8],
        _recovery_param: u8,
    ) -> Result<Vec<u8>, RecoverPubkeyError> {
        Err(RecoverPubkeyError::unknown_err(0))
    }

    fn ed25519_verify(
        &self,
        _message: &[u8],
        _signature: &[u8],
        _public_key: &[u8],
    ) -> Result<bool, VerificationError> {
        Ok(true)
    }

    fn ed25519_batch_verify(
        &self,
        _messages: &[&[u8]],
        _signatures: &[&[u8]],
        _public_keys: &[&[u8]],
    ) -> Result<bool, VerificationError> {
        Ok(true)
    }

    fn debug(&self, message: &str) {
        println!("{}", message);
    }
}

/// Empty Querier that is meant to conform the traits expected by the cosmwasm standard contract syntax. It should not be used whatsoever
#[derive(Default)]
pub struct EmptyQuerier {}

impl Querier for EmptyQuerier {
    fn raw_query(&self, _bin_request: &[u8]) -> cosmwasm_std::QuerierResult {
        todo!()
    }
}