diff --git a/00_Enumerated-Lists/enumerate.cpp b/00_Enumerated-Lists/enumerate.cpp new file mode 100644 index 0000000..479bb49 --- /dev/null +++ b/00_Enumerated-Lists/enumerate.cpp @@ -0,0 +1,16 @@ +#include +#include + +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 + }; +}; \ No newline at end of file diff --git a/01_Hashes/checksum.cpp b/01_Hashes/checksum.cpp index 1c73483..11d5e5b 100644 --- a/01_Hashes/checksum.cpp +++ b/01_Hashes/checksum.cpp @@ -6,18 +6,68 @@ using namespace eosio; using std::string; +template + class crypto : eosio::contract { public: using contract::contract; + /** + * Hash + * @desc Hashes a given input string and prints to console + * @author Mitch Pierias + * @param str 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 + * @param str String to hash + * @return result Hash digest + * @private + */ + checksum256 create_hash(const string& str) { // Hash the string to a checksum - checksum256 sum{}; + checksum256 result{}; sha256(const_cast(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 + * @param data Checksum character set + * @param length Checksum size + * @return result 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; } }; diff --git a/02_Singletons/singletons.cpp b/02_Singletons/singletons.cpp index 8b9b003..562ca78 100644 --- a/02_Singletons/singletons.cpp +++ b/02_Singletons/singletons.cpp @@ -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() { diff --git a/11_ABI-Extensions/abiext.cpp b/11_ABI-Extensions/abiext.cpp new file mode 100644 index 0000000..1ce0c04 --- /dev/null +++ b/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);` + } +} \ No newline at end of file