您正在查看: EOS-智能合约 分类下的文章

EOS 合约 hex 字符串与checksum256互转

hex to checksum256

eosio::checksum256 hex_to_checksum256(const std::string& in) {
    eosio::check(in.length() == 64, "checksum size is error");
    std::string hex{"0123456789abcdef"};
    eosio::checksum256 out;
    for (int i = 0; i < 32; i++) {
      auto d1 = hex.find(in[2 * i]);
      auto d2 = hex.find(in[2 * i + 1]);
      eosio::check(d1 != std::string::npos || d2 != std::string::npos,
                  "invalid sha256");

      // checksum256 is composed of little endian int128_t
      reinterpret_cast<char*>(out.data())[i / 16 * 16 + 15 - (i % 16)] =
          (d1 << 4) + d2;
    }
    return out;
}

checksum256 to hex string

static string to_hex(const checksum256 &hashed) {
    // Construct variables
    string result;
    const char *hex_chars = "0123456789abcdef";
    const auto bytes = hashed.extract_as_byte_array();
    // Iterate hash and build result
    for (uint32_t i = 0; i < bytes.size(); ++i) {
        (result += hex_chars[(bytes.at(i) >> 4)]) += hex_chars[(bytes.at(i) & 0x0f)];
    }
    // Return string
    return result;
}

参考

https://github.com/EOSIO/eos/issues/4012

在eosio合约中解析json

在合约中常有解析json的需求,此文章介绍使用
nlohmann/json

原版本
https://github.com/nlohmann/json
适配eosio合约后的版本源码地址
https://github.com/bcskill/eosio_json

下载完json.hpp文件后,将代码文件放到合约目录,通常放到项目合约代码同级的common目录

然后在项目合约中直接包含此json.hpp文件

#include "./common/json.hpp"

在使用的合约指名json的命名空间

using json = nlohmann::json;

然后就可以直接解析json字符串了

json memoJson = json::parse(memo);
   // 为了业务的判定,合约账户内不允许非限定交易,如有特殊需求再做变更
   eosio::check(memoJson.count("transfer_type") == 1, get_assert_msg(ASSERT_ERROR_CODE::MISSING_PARAMETERS, "Missing parameters"));
   transfer_type = memoJson["transfer_type"].get<uint8_t>();

EOS合约中获取当天本地时间的零点和第二天的零点

获取当前时间所在当天的本地零点时间

time_point contract::last_zeto_time_point(time_point last_do_time_point) {
   uint64_t days_count = (last_do_time_point.time_since_epoch().count() + (8 * one_hour_msec)) / one_day_msec;
   uint64_t days_msec = days_count * one_day_msec;
   days_msec -= (8 * one_hour_msec);
   return time_point(microseconds(days_msec));
}

获取当前时间所在当天的本地第二天的零点时间

time_point contract::next_zeto_time_point(time_point last_do_time_point) {
   return last_zeto_time_point(last_do_time_point) + microseconds(one_day_msec);
}

EOSIO SDK for Rust

用于Rust的EOSIO SDK –用于在Rust中的EOSIO区块链上构建智能合约的API
项目地址:
https://github.com/sagan-software/eosio-rust
开发文档:
https://sagan-software.github.io/eosio-rust/quick-start.html

std::string 与 abieos::public_key 互转

#include "abieos_numeric.hpp"
#include <stdio.h>
#include <cassert>

using namespace abieos;


int main() {
  std::string pubkey_str = std::string("EOS6S7kyRBUnwhKwfz41E7hCv9swCzT6AicNb7Skiz4LAeY7wG9WJ");
  abieos::public_key pubkey = string_to_public_key(pubkey_str);
  std::string decoded_pubkey_str = public_key_to_string(pubkey);
  assert(decoded_pubkey_str == pubkey_str);
  printf("Succcess!\n");
}

源代码地址:https://github.com/cppfuns/abieos-numeric-c-14/blob/master/test.cpp