Skip to content

Commit

Permalink
Began Enumerated Lists and ABI Extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchPierias committed Nov 27, 2018
1 parent a37422f commit cfbba33
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
16 changes: 16 additions & 0 deletions 00_Enumerated-Lists/enumerate.cpp
@@ -0,0 +1,16 @@
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.h>

using namespace eosio;
using std::string;

class enumerate : eosio::contract {

private:
// Game status types
enum game_status: uint8_t {
OPEN = 0,
PAUSED = 1,
CLOSED = 2
};
};
56 changes: 53 additions & 3 deletions 01_Hashes/checksum.cpp
Expand Up @@ -6,18 +6,68 @@
using namespace eosio;
using std::string;

template <typename CharT>

class crypto : eosio::contract {

public:

using contract::contract;

/**
* Hash
* @desc Hashes a given input string and prints to console
* @author Mitch Pierias <https://github.com/MitchPierias>
* @param str <std::string> String to hash
* @public
*/
void hash(const string& str) {
// Hash the given string
checksum256 hashed = create_hash(str);
// Convert output to hexadecimal string
string result = to_hex(hashed);
// Print result
print(result);
}

private:

/**
* Hash String
* @desc Digests a hash from the provided string
* @author Mitch Pierias <https://github.com/MitchPierias>
* @param str <std::string> String to hash
* @return result <checksum256> Hash digest
* @private
*/
checksum256 create_hash(const string& str) {
// Hash the string to a checksum
checksum256 sum{};
checksum256 result{};
sha256(const_cast<char*>(str.c_str()), str.size(), &sum);
// Print output
printhex(&sum, sizeof(sum));
// Return resulting digest
return result;
}

/**
* Checksum to Hexadecimal String
* @desc Converts the provided checksum of specified length to a hexadecimal string
* @author Mitch Pierias <https://github.com/MitchPierias>
* @param data <CharT> Checksum character set
* @param length <uint32_t> Checksum size
* @return result <std::string> Hexadecimal string
* @private
*/
string to_hex(const CharT* data, uint32_t length) {
// Construct variables
string result;
const char* hex_chars = "0123456789abcdef";
uint8_t* c = (uint8_t*)data;
// Iterate bytes and build result
for (uint32_t i = 0; i < length; ++i) {
(result += hex_chars[(c[i] >> 4)]) += hex_chars[(c[i] & 0x0f)];
}
// Return string
return result;
}
};

Expand Down
5 changes: 4 additions & 1 deletion 02_Singletons/singletons.cpp
Expand Up @@ -14,7 +14,10 @@ class singletons : eosio::contract {
explicit singletons(action_name self) : contract(self), config(_self, _self) {}
// @abi action
void setclosed(bool isClosed) {
config.set(Config{isClosed}, _self);
auto state = config.get();
state.closed = isClosed;
config.set(state, _self);
print(state.closed);
}
// @abi action
void getclosed() {
Expand Down
19 changes: 19 additions & 0 deletions 11_ABI-Extensions/abiext.cpp
@@ -0,0 +1,19 @@
extern "C" void apply(uint64_t receiver, uint64_t code, uint64_t action) {

auto self = receiver;

if (code == self || code == N(eosio.token)) {

if (action == N(transfer)) {
eosio_assert(code == N(eosio.token), "EOS required for transfer");
}

TYPE thiscontract(self);

switch (action) {
EOSIO_API(TYPE, MEMBERS)
}

// Prevent contract destructor from executing `eosio_exit(0);`
}
}

0 comments on commit cfbba33

Please sign in to comment.