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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use reqwest::blocking::Client;
use reqwest::StatusCode;
use serde_json::Value;
use reqwest::header::{CONTENT_TYPE, HeaderValue};
use crate::errors::{Result, ErrorKind, Error};
use crate::api_types::*;
use crate::wallet_types::Wallet;
use eosio_client_keys::EOSPublicKey;
use crate::wasm::WASM;
use libabieos_sys::{ABIEOS, AbiFiles};
//use rust_embed::RustEmbed;

use chrono::{Utc, DateTime};

pub const ERROR_TXN_SET_EXACT_CODE: usize = 3_160_008;

pub struct EOSRPC {
    pub client: Client,
    pub host: String,
    pub abi_abi_js: String,
    pub transaction_abi_js: String,
}

impl EOSRPC {
    pub fn blocking(host: String) -> Result<EOSRPC> {
        let client = reqwest::blocking::Client::new();
        let abi_f = AbiFiles::get("abi.abi.json").unwrap();
        let abi_abi_js: String = String::from_utf8(abi_f.as_ref().to_vec())?;
        let transaction_abi_js: String = String::from_utf8(AbiFiles::get("transaction.abi.json").unwrap().as_ref().to_vec())?;
        Ok(EOSRPC {
            client,
            host,
            abi_abi_js,
            transaction_abi_js,
        })
    }

    pub fn blocking_ex(host: String, abi_abi_js: &str, transaction_abi_js: &str) -> EOSRPC {
        let client = reqwest::blocking::Client::new();
        EOSRPC { client, host, abi_abi_js: abi_abi_js.to_string(), transaction_abi_js: transaction_abi_js.to_string() }
    }

    pub fn blocking_req(&self, url: &str, in_json: Value) -> Result<String> {
        let full_url = [&self.host, url].concat();
        let req = self.client.post(&full_url).json(&in_json);
        let response = req.send()?;
        let content_type = response.headers().get(CONTENT_TYPE).unwrap();
        let hv_json = HeaderValue::from_static("application/json");
        if content_type != hv_json {
            return Err(ErrorKind::InvalidResponseContentType.into());
        }
        let status = response.status();

        if status == StatusCode::OK || status == StatusCode::CREATED || status == StatusCode::ACCEPTED {
            Ok(response.text()?)
        } else {
            let tx: &str = &response.text()?;
            let msg: ErrorReply = serde_json::from_str(tx).unwrap();
            Err(ErrorKind::InvalidResponseStatus(msg.error).into())
        }
    }

    pub fn get_account(&self, account_name: &str) -> Result<GetAccount> {
        let value = serde_json::json!({ "account_name": account_name });
        let res = self.blocking_req("/v1/chain/get_account", value)?;
        let ga: GetAccount = serde_json::from_str(&res).unwrap();
        Ok(ga)
    }
    pub fn get_abi(&self, account_name: &str) -> Result<GetAbi> {
        let value = serde_json::json!({ "account_name": account_name });
        let res = self.blocking_req("/v1/chain/get_abi", value)?;
        let ga: GetAbi = serde_json::from_str(&res).unwrap();
        Ok(ga)
    }

    pub fn get_info(&self) -> Result<GetInfo> {
        let value = serde_json::json!({ "greg": "the stop sign" });

        let res = self.blocking_req("/v1/chain/get_info", value)?;
        let ga: GetInfo = serde_json::from_str(&res).unwrap();
        Ok(ga)
    }

    pub fn get_code_hash(&self, account_name: &str) -> Result<GetCodeHash> {
        let value = serde_json::json!({ "account_name": account_name });

        let res = self.blocking_req("/v1/chain/get_code_hash", value)?;
        let gc: GetCodeHash = serde_json::from_str(&res)?;
        Ok(gc)
    }

    pub fn get_raw_abi(&self, account_name: &str) -> Result<GetRawABI> {
        let value = serde_json::json!({ "account_name": account_name });

        let res = self.blocking_req("/v1/chain/get_raw_abi", value)?;
        let gr: GetRawABI = serde_json::from_str(&res)?;
        Ok(gr)
    }
    pub fn get_block_num(&self, block_num: usize) -> Result<GetBlock> {
        let value = serde_json::json!({ "block_num_or_id": block_num });

        let res = self.blocking_req("/v1/chain/get_block", value)?;
        let gb: GetBlock = serde_json::from_str(&res)?;
        Ok(gb)
    }
    pub fn get_block_id(&self, block_id: &str) -> Result<GetBlock> {
        let value = serde_json::json!({ "block_num_or_id": block_id });

        let res = self.blocking_req("/v1/chain/get_block", value)?;
        let gb: GetBlock = serde_json::from_str(&res)?;
        Ok(gb)
    }

    pub fn get_required_keys(&self, transaction: &TransactionIn, keys: Vec<EOSPublicKey>) -> Result<RequiredKeys> {
        let mut key_str: Vec<String> = vec![];
        for key in keys {
            let x = key.to_eos_string()?;
            key_str.push(x);
        }

        let value = serde_json::json!({ "transaction": transaction, "available_keys":key_str});
        let res = self.blocking_req("/v1/chain/get_required_keys", value)?;
        let rk: RequiredKeys = serde_json::from_str(&res).unwrap();
        Ok(rk)
    }

    pub fn get_abi_from_account(&self, abieos_eosio: &ABIEOS, account_name: &str) -> Result<ABIEOS> {
        let rawabi = self.get_raw_abi(account_name)?;
        let account_abi = rawabi.decode_abi()?;
        unsafe {
            let account_abi_json = abieos_eosio.bin_to_json("eosio", "abi_def", &account_abi)?;
            Ok(ABIEOS::new_with_abi(account_name, &account_abi_json)?)
        }
    }

    pub fn push_transaction(&self, abieos: &ABIEOS, wallet: &Wallet, actions: Vec<ActionIn>, ref_block: &str, exp_time: DateTime<Utc>) -> Result<TransactionResponse> {
        let ti = TransactionIn::simple(actions, ref_block, exp_time)?;

        let trx_json = serde_json::to_string(&ti)?;
        let trx = unsafe {
            abieos.json_to_hex("eosio", "transaction", &trx_json)
        }?;

        let pubkeys = wallet.keys()?;
        let required_keys = self.get_required_keys(&ti, pubkeys)?;
        let eospubs: Vec<EOSPublicKey> = EOSPublicKey::from_eos_strings(&required_keys.required_keys)?;

        let signed_transaction = wallet.sign_transaction(ti, eospubs)?;
        let pti = PackedTransactionIn {
            signatures: signed_transaction.signatures,
            compression: "none".to_string(),
            packed_context_free_data: "".to_string(),
            packed_trx: trx.to_string(),
        };

        let in_val = serde_json::json!(pti);
        let res = self.blocking_req("/v1/chain/push_transaction", in_val)?;
        let tr: TransactionResponse = serde_json::from_str(&res).unwrap();
        Ok(tr)
    }

    pub fn get_table_rows(&self, code: &str, scope: &str, table: &str, table_key: &str,
                          lower_bound: &str, upper_bound: &str, limit: usize, key_type: &str,
                          index_position: &str, encode_type: &str, reverse: bool,
                          show_payer: bool) -> Result<GetTableRows> {
        let in_j = GetTableRowsIn {
            json: false,
            code: code.parse().unwrap(),
            scope: scope.parse().unwrap(),
            table: table.parse().unwrap(),
            table_key: table_key.parse().unwrap(),
            lower_bound: lower_bound.parse().unwrap(),
            upper_bound: upper_bound.parse().unwrap(),
            limit,
            key_type: key_type.parse().unwrap(),
            index_position: index_position.parse().unwrap(),
            encode_type: encode_type.parse().unwrap(),
            reverse,
            show_payer,
        };
        let in_val = serde_json::json!(in_j);
        let res = self.blocking_req("/v1/chain/get_table_rows", in_val)?;
        let tr: GetTableRows = serde_json::from_str(&res).unwrap();
        Ok(tr)
    }
    pub fn get_table_by_scope(&self,code:&str, table:&str,
                              lower_bound:&str, upper_bound:&str,
                              limit:usize, reverse:bool) -> Result<GetTableByScope> {
        let pti = GetTableByScopeIn {
            code: code.parse().unwrap(),
            table: table.parse().unwrap(),
            lower_bound: lower_bound.parse().unwrap(),
            upper_bound: upper_bound.parse().unwrap(),
            limit,
            reverse
        };
        let in_val = serde_json::json!(pti);
        let res = self.blocking_req("/v1/chain/get_table_by_scope", in_val)?;
        let tr: GetTableByScope = serde_json::from_str(&res).unwrap();
        Ok(tr)
    }
}


pub fn create_setcode_action(acct_abieos: &ABIEOS, name: &str, code: &WASM) -> Result<ActionIn> {
    let auth = AuthorizationIn { permission: "active".to_string(), actor: String::from(name) };
    let v_auth: Vec<AuthorizationIn> = vec![auth];
    let data = ActionSetcodeData {
        account: String::from(name),
        vmtype: 0,
        vmversion: 0,
        code: vec_u8_to_hex(&code.code)?,
    }.to_hex(acct_abieos)?;

    Ok(ActionIn {
        name: "setcode".to_string(),
        account: "eosio".to_string(),
        authorization: v_auth,
        data,
    })
}

pub fn create_setcode_clear_action(acct_abieos: &ABIEOS, name: &str) -> Result<ActionIn> {
    let auth = AuthorizationIn { permission: "active".to_string(), actor: String::from(name) };
    let v_auth: Vec<AuthorizationIn> = vec![auth];
    let data = ActionSetcodeData {
        account: String::from(name),
        vmtype: 0,
        vmversion: 0,
        code: vec_u8_to_hex(&WASM::dummy())?,
    }.to_hex(acct_abieos)?;

    Ok(ActionIn {
        name: "setcode".to_string(),
        account: "eosio".to_string(),
        authorization: v_auth,
        data,
    })
}

pub struct AbiTrio {
    pub sys_abi: ABIEOS,
    pub txn_abi: ABIEOS,
    pub acct_abi: ABIEOS,
}

impl AbiTrio {
    pub fn create(sys_name: &str, sys_acct_name: &str, eos: &EOSRPC) -> Result<AbiTrio> {
        unsafe {
            let sys_abi = ABIEOS::new_with_abi(sys_name, &eos.abi_abi_js)?;
            let txn_abi: ABIEOS = ABIEOS::new_with_abi(sys_name, &eos.transaction_abi_js).map_err(|e| {
                sys_abi.destroy();
                Error::with_chain(e, "AbiTrio_txn")
            })?;
            let acct_abi: ABIEOS = eos.get_abi_from_account(&sys_abi, sys_acct_name).map_err(|e| {
                sys_abi.destroy();
                txn_abi.destroy();
                Error::with_chain(e, "AbiTrio_act")
            })?;

            Ok(AbiTrio { sys_abi, txn_abi, acct_abi })
        }
    }
    pub fn destroy(&self) {
        unsafe {
            self.acct_abi.destroy();
            self.txn_abi.destroy();
            self.sys_abi.destroy()
        }
    }
}

pub fn create_setabi_action(sys_abieos: &ABIEOS, acct_abieos: &ABIEOS, name: &str, abi: &str) -> Result<ActionIn> {
    let auth = AuthorizationIn { permission: "active".to_string(), actor: String::from(name) };
    let v_auth: Vec<AuthorizationIn> = vec![auth];
    let abi_hex = unsafe {
        sys_abieos.json_to_hex("eosio", "abi_def", abi)
    }?;
    // let abi_s = String::from(abi);
    let data = ActionSetData { account: String::from(name), abi: String::from(abi_hex) }.to_hex(acct_abieos)?;

    Ok(ActionIn {
        name: "setabi".to_string(),
        account: "eosio".to_string(),
        data,
        authorization: v_auth,
    })
}


#[cfg(test)]
mod test {
    use super::*;

    //use crate::api_types::GetAccount;
    use crate::wallet_types::{get_wallet_pass, EOSIO_CHAIN_ID};
    use chrono::{NaiveDateTime, Duration};
    use std::fs;

    const TEST_HOST: &str = "http://127.0.0.1:8888";
   // const TEST_HOST: &str = "https://api.testnet.eos.io";
    const TEST_KEOSD: &str = "http://127.0.0.1:3888";

    const TEST_WALLET_NAME: &str = "default";
    const TEST_ACCOUNT_NAME: &str = "fwonhjnefmps";

//const TEST_HOST: &str = "https://eos.greymass.com";
//const TEST_HOST: &str = "https://chain.wax.io";

    #[test]
    fn blocking_req_test() -> Result<()> {
        let eos = EOSRPC::blocking(String::from(TEST_HOST))?;
        let _ga = eos.get_account("eosio")?;

        let _abi = eos.get_abi("eosio")?;
        Ok(())
    }

    #[test]
    fn blocking_get_info() -> Result<()> {
        let eos = EOSRPC::blocking(String::from(TEST_HOST))?;
        let _gi = eos.get_info()?;
        Ok(())
    }

    #[test]
    fn datetime_format() {
        let s = "2020-05-16T05:12:03";
        const FORMAT: &'static str = "%Y-%m-%dT%H:%M:%S";
        // let _tz_offset = FixedOffset::east(0);
        match NaiveDateTime::parse_from_str(s, FORMAT) {
            Err(_e) => {
                eprintln!("{:#?}", _e);
                assert!(false)
            }
            Ok(_dt) => {
                assert!(true)
            }
        }
    }

    #[test]
    fn blocking_get_required_keys() -> Result<()> {
        //  let client = reqwest::blocking::Client::new();
        let eos = EOSRPC::blocking(String::from(TEST_HOST))?;
        let keys = vec![
            EOSPublicKey::from_eos_string("EOS6zUgp7uAV1pCTXZMGJyH3dLUSWJUkZWGA9WpWxyP2pCT3mAkNX").unwrap(),
            EOSPublicKey::from_eos_string("EOS7ctUUZhtCGHnxUnh4Rg5eethj3qNS5S9fijyLMKgRsBLh8eMBB").unwrap(),
            EOSPublicKey::from_eos_string("EOS8fdsPr1aKsmszNHeY4RrgupbabNQ5nmLgQWMEkTn2dENrPbRgP").unwrap(),
        ];
        let gi: GetInfo = eos.get_info()?;
        let exp_time = gi.set_exp_time(Duration::seconds(1800));

        let wasm = WASM::read_file("test/good-2.wasm")?;

        let name = TEST_ACCOUNT_NAME;

        let abi_trio = AbiTrio::create("eosio", "eosio", &eos)?;
        let action_r = create_setcode_action(&abi_trio.acct_abi, &name, &wasm);
        abi_trio.destroy();

        let ti = TransactionIn::simple(vec![action_r?], &gi.last_irreversible_block_id, exp_time)?;
        let rk = eos.get_required_keys(&ti, keys)?;
        assert!(rk.required_keys.len() > 0);
        let k = &rk.required_keys[0];

        // accidentally set one of chains to have 'owner' key instead of 'active'
        if k == "EOS7ctUUZhtCGHnxUnh4Rg5eethj3qNS5S9fijyLMKgRsBLh8eMBB" {
            ()
        } else {
            assert_eq!(k, "EOS8fdsPr1aKsmszNHeY4RrgupbabNQ5nmLgQWMEkTn2dENrPbRgP");
        }
        Ok(())
    }

    /// these two need some static thing which will exist over all test environments
    /// the TicTacToe example deletes ALL of it's data on successful completion, so can't really be
    /// used
    ///
    #[test]
    fn blocking_table_rows() -> Result<()> {
        let eos = EOSRPC::blocking(String::from(TEST_HOST))?;
        let _r = eos.get_table_rows(TEST_ACCOUNT_NAME,"tictactoe",
                                   "games", "", "", "",
                                   10, "", "", "dec",
                                   false, true);
        Ok(())
    }

    /// these two need some static thing which will exist over all test environments
    /// the TicTacToe example deletes ALL of it's data on successful completion, so can't really be
    /// used
    ///
    #[test]
    fn blocking_table_by_scope() -> Result<()> {
        let eos = EOSRPC::blocking(String::from(TEST_HOST))?;
        let _r = eos.get_table_by_scope("eosio.token","",
                                       TEST_ACCOUNT_NAME,"",
                                       10,false);
        Ok(())
    }

    #[test]
    fn blocking_push_txn() -> Result<()> {
        let eos = EOSRPC::blocking(String::from(TEST_HOST))?;
        let wallet = Wallet::create_with_chain_id(EOSRPC::blocking(String::from(TEST_KEOSD))?, EOSIO_CHAIN_ID);
        let wallet_pass = get_wallet_pass()?;
        wallet.unlock(&TEST_WALLET_NAME, &wallet_pass)?;
        let wasm = WASM::read_file("test/good-2.wasm")?;
        let wasm_abi = fs::read_to_string("test/good-2.abi")?;

        let name = TEST_ACCOUNT_NAME;

        let gi: GetInfo = eos.get_info()?;
        let exp_time = gi.set_exp_time(Duration::seconds(1800));
        let abi_trio = AbiTrio::create("eosio", "eosio", &eos)?;

        let action_clear = create_setcode_clear_action(&abi_trio.acct_abi, &name).map_err(|e| {
            abi_trio.destroy();
            Error::with_chain(e, "blocking_push_txn/create_setcode_clear_action")
        })?;


        let _res_clear_int = eos.push_transaction(&abi_trio.txn_abi, &wallet,
                                                 vec![action_clear],
                                                 &gi.head_block_id, exp_time).map_err(|e| {
            abi_trio.destroy();
            Error::with_chain(e, "blocking_push_txn/push_transaction(clear)")
        })?;

        let action = create_setcode_action(&abi_trio.acct_abi, &name, &wasm).map_err(|e| {
            abi_trio.destroy();
            Error::with_chain(e, "blocking_push_txn/create_setcode_action")
        })?;
        let action_abi = create_setabi_action(&abi_trio.sys_abi, &abi_trio.acct_abi, &name, &wasm_abi).map_err(|e| {
            abi_trio.destroy();
            Error::with_chain(e, "blocking_push_txn/create_setabi_action")
        })?;

        let _res_int = eos.push_transaction(&abi_trio.txn_abi, &wallet,
                                           vec![action, action_abi],
                                           &gi.head_block_id, exp_time).map_err(|e| {
            abi_trio.destroy();
            Error::with_chain(e, "blocking_push_txn/push_transaction(set-code/abi)")
        })?;

        abi_trio.destroy();
        Ok(())
    }

    #[test]
    fn blocking_get_raw_abi() -> Result<()> {
        let eos = EOSRPC::blocking(String::from(TEST_HOST))?;
        let _res = eos.get_raw_abi("eosio")?;

        Ok(())
    }

    #[test]
    fn blocking_getsetabi() -> Result<()> {
        let eos = EOSRPC::blocking(String::from(TEST_HOST))?;
        let wasm_abi = fs::read_to_string("test/good-2.abi")?;
        let wallet = Wallet::create_with_chain_id(EOSRPC::blocking(String::from(TEST_KEOSD))?, EOSIO_CHAIN_ID);
        let wallet_pass = get_wallet_pass()?;
        wallet.unlock(&TEST_WALLET_NAME, &wallet_pass)?;
        let gi = eos.get_info()?;
        let exp_time = gi.set_exp_time(Duration::seconds(1800));


        let name = TEST_ACCOUNT_NAME;
        let trio = AbiTrio::create("eosio", "eosio", &eos)?;

        let action_abi = create_setabi_action(&trio.sys_abi, &trio.acct_abi, &name, &wasm_abi).map_err(|e|
            {
                &trio.destroy();
                Error::with_chain(e, "create_setabi_action")
            })?;

        let _tr = eos.push_transaction(&trio.txn_abi, &wallet,
                                       vec![action_abi],
                                       &gi.head_block_id, exp_time).map_err(|e|
            {
                trio.destroy();
                Error::with_chain(e, "push_transaction")
            })?;
        trio.destroy();

        // if the abi is written incorrectly this will cause a server error
        let _get_abi = eos.get_abi(name)?;

        Ok(())
    }

    #[test]
    fn block_getblock() -> Result<()> {
        let eos = EOSRPC::blocking(String::from(TEST_HOST))?;
        let block = eos.get_block_num(1)?;
        let block2 = eos.get_block_id(&block.id)?;
        assert_eq!(block.block_num, block2.block_num);
        Ok(())
    }
}