From 8fcd0ae05c99464746e4f9468f996891538995e0 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 18 Oct 2019 00:25:36 -0700 Subject: [PATCH 01/21] refactor: CodeClimate Improvements (#166) --- src/crypto/hash.cpp | 12 ++--- src/transactions/serializer.cpp | 42 +++++++-------- src/transactions/transaction.cpp | 93 ++++++++++++++++---------------- src/utils/crypto_helpers.h | 6 +-- test/utils/crypto_helpers.cpp | 25 ++++++--- 5 files changed, 94 insertions(+), 84 deletions(-) diff --git a/src/crypto/hash.cpp b/src/crypto/hash.cpp index 53548d71..8c843e42 100644 --- a/src/crypto/hash.cpp +++ b/src/crypto/hash.cpp @@ -9,6 +9,8 @@ #include "crypto/hash.hpp" +#include + #include "interfaces/identities.hpp" #include "bcl/Ripemd160.hpp" @@ -32,13 +34,11 @@ PubkeyHash Hash::ripemd160(const uint8_t*publicKeyBytes) { // Returns a 32-byte SHA256 hash of the input vector. Hash32 Hash::sha256(const uint8_t* inputBytes, size_t size) { - auto result = bcl::Sha256::getHash(inputBytes, size); + Hash32 hash32 {}; + memmove(hash32.data(), + bcl::Sha256::getHash(inputBytes, size).value, + hash32.size()); - Hash32 hash32; - uint8_t* ptr = result.value; - for (auto& e : hash32) { - e = *ptr++; - }; return hash32; } diff --git a/src/transactions/serializer.cpp b/src/transactions/serializer.cpp index 88c8ff22..40fec6d8 100644 --- a/src/transactions/serializer.cpp +++ b/src/transactions/serializer.cpp @@ -2,6 +2,7 @@ #include "transactions/serializer.h" #include +#include #include #include @@ -146,21 +147,22 @@ void Serializer::serializeDelegateRegistration( /**/ -void Serializer::serializeVote( - std::vector& bytes) const { +void Serializer::serializeVote(std::vector& bytes) const { std::string votes; - for (const auto& vote : _transaction.asset.votes) { - votes += (vote[0] == '+' ? "01" : "00") + vote.substr(1); - }; + votes = std::accumulate(_transaction.asset.votes.begin(), + _transaction.asset.votes.end(), + std::string(), + [&](const std::string& a, const std::string& b) + -> std::string { + return a + (b.at(0) == '+' ? "01" : "00") + b.substr(1); + }); std::vector voteBytes = HexToBytes(votes.c_str()); - bytes.push_back(static_cast( - _transaction.asset.votes.size())); - bytes.insert( - bytes.end(), - voteBytes.begin(), - voteBytes.end()); + + bytes.push_back(static_cast(_transaction.asset.votes.size())); + + bytes.insert(bytes.end(), voteBytes.begin(), voteBytes.end()); } /**/ @@ -168,24 +170,20 @@ void Serializer::serializeVote( void Serializer::serializeMultiSignatureRegistration( std::vector& bytes) const { std::string keysgroup; - if (_transaction.version == 1) { - for (const auto& kg : _transaction.asset.multiSignature.keysgroup) { - keysgroup += kg.substr(1); - }; - } else { - keysgroup = join(_transaction.asset.multiSignature.keysgroup); - }; + + keysgroup = join(_transaction.asset.multiSignature.keysgroup, + _transaction.version == 1U ? 1U : 0U); bytes.push_back(_transaction.asset.multiSignature.min); + bytes.push_back(static_cast( _transaction.asset.multiSignature.keysgroup.size())); + bytes.push_back(_transaction.asset.multiSignature.lifetime); std::vector keysgroupBytes = HexToBytes(keysgroup.c_str()); - bytes.insert( - bytes.end(), - keysgroupBytes.begin(), - keysgroupBytes.end()); + + bytes.insert(bytes.end(), keysgroupBytes.begin(), keysgroupBytes.end()); } /**/ diff --git a/src/transactions/transaction.cpp b/src/transactions/transaction.cpp index 71106199..0e10446d 100644 --- a/src/transactions/transaction.cpp +++ b/src/transactions/transaction.cpp @@ -212,15 +212,15 @@ std::vector Ark::Crypto::Transactions::Transaction::toBytes( std::map Ark::Crypto::Transactions::Transaction::toArray() const { // buffers for variable and non-string type-values. - char amount[24]; - char assetName[16]; - char assetValue[512]; - char fee[24]; - char network[8]; - char signatures[512]; - char timestamp[36]; - char type[8]; - char version[8]; + char amount[24] = {}; + char assetName[16] = {}; + char assetValue[512] = {}; + char fee[24] = {}; + char network[8] = {}; + char signatures[512] = {}; + char timestamp[36] = {}; + char type[8] = {}; + char version[8] = {}; // Amount snprintf(amount, sizeof(amount), "%" PRIu64, this->amount); @@ -229,33 +229,33 @@ std::map Ark::Crypto::Transactions::Transaction::toArr if (this->type == 0) { // Transfer // do nothing - } else if (this->type == 1) { + } + else if (this->type == 1) { // Second Signature Registration - strncpy(assetName, "publicKey", sizeof(assetName)); - strncpy( - assetValue, - this->asset.signature.publicKey.c_str(), - this->asset.signature.publicKey.length() + 1); - } else if (this->type == 2) { + memmove(assetName, "publicKey", 10); + memmove(assetValue, + this->asset.signature.publicKey.c_str(), + this->asset.signature.publicKey.length() + 1); + } + else if (this->type == 2) { // Delegate Registration - strncpy(assetName, "username", sizeof(assetName)); - strncpy( - assetValue, - this->asset.delegate.username.c_str(), - this->asset.delegate.username.length() + 1); - } else if (this->type == 3) { + memmove(assetName, "username", 9); + memmove(assetValue, + this->asset.delegate.username.c_str(), + this->asset.delegate.username.length() + 1); + } + else if (this->type == 3) { // Vote - strncpy(assetName, "votes", sizeof(assetName)); - strncpy(assetValue, "", 1); - for (unsigned int i = 0; i < this->asset.votes.size(); ++i) { - strncat( - assetValue, - this->asset.votes[i].c_str(), - this->asset.votes[i].length() + 1); - if (i < this->asset.votes.size() - 1) { - strncat(assetValue, ",", 1); - }; - }; + memmove(assetName, "votes", 6); + for (unsigned int i = 0U; i < this->asset.votes.size(); ++i) { + uint8_t offset = i * this->asset.votes[i].length(); + memmove(assetValue + offset + (i == 0U ? 0U : 1U), + this->asset.votes[i].c_str(), + this->asset.votes[i].length()); + if (i > 0 && i < this->asset.votes.size()) { + memmove(assetValue + offset, ",", 1); + } + } // } else if (this->type == 4) { // Multisignature Registration // // TODO @@ -273,22 +273,21 @@ std::map Ark::Crypto::Transactions::Transaction::toArr snprintf(fee, sizeof(fee), "%" PRIu64, this->fee); // Signatures - strcpy(signatures, ""); - for (unsigned int i = 0; i < this->signatures.size(); ++i) { - strncat( - signatures, - this->signatures[i].c_str(), - this->signatures[i].length() + 1); - if (i < this->signatures.size() - 1) { - strncpy(signatures, ",", 1); - }; - }; + for (unsigned int i = 0U; i < this->signatures.size(); ++i) { + uint8_t offset = i * this->signatures[i].length(); + memmove(signatures + offset + (i == 0U ? 0U : 1U), + this->signatures[i].c_str(), + this->signatures[i].length()); + if (i > 1U && i < this->signatures.size()) { + memmove(signatures + i * this->signatures[i].length(), ",", 1); + } + } // Network snprintf(network, sizeof(network), "%d", this->network); // Timestamp - snprintf(timestamp, sizeof(timestamp), "%d", this->timestamp); + snprintf(timestamp, sizeof(timestamp), "%" PRIu32, this->timestamp); // Type snprintf(type, sizeof(type), "%u", this->type); @@ -384,7 +383,7 @@ std::string Ark::Crypto::Transactions::Transaction::toJson() const { doc["recipient"] = txArray["recipient"]; // SecondSignature - if (std::strlen(txArray["secondSignature"].c_str()) > 0) { + if (txArray["secondSignature"].length() > 0U) { doc["secondSignature"] = txArray["secondSignature"]; }; @@ -407,7 +406,7 @@ std::string Ark::Crypto::Transactions::Transaction::toJson() const { }; // SignSignature - if (std::strlen(txArray["signSignature"].c_str()) > 0) { + if (txArray["signSignature"].length() > 0U) { doc["signSignature"] = txArray["signSignature"]; }; @@ -418,7 +417,7 @@ std::string Ark::Crypto::Transactions::Transaction::toJson() const { doc["type"] = atoi(txArray["type"].c_str()); // VendorField - if (std::strlen(txArray["vendorField"].c_str()) > 0) { + if (txArray["vendorField"].length() > 0U) { doc["vendorField"] = txArray["vendorField"]; }; diff --git a/src/utils/crypto_helpers.h b/src/utils/crypto_helpers.h index 2982f712..d7e44c63 100644 --- a/src/utils/crypto_helpers.h +++ b/src/utils/crypto_helpers.h @@ -50,13 +50,13 @@ inline void unpack(T* dst, uint8_t* src, size_t size = -1) { } // Join string vector -inline std::string join(const std::vector& strings) { +inline std::string join(const std::vector& strings, size_t offset = 0U) { return std::accumulate( strings.begin(), strings.end(), std::string(), - [](const std::string& a, const std::string& b) - -> std::string { return a + b; }); + [&](const std::string& a, const std::string& b) + -> std::string { return a + b.substr(offset); }); } #endif diff --git a/test/utils/crypto_helpers.cpp b/test/utils/crypto_helpers.cpp index 8ded3fbb..693f227f 100644 --- a/test/utils/crypto_helpers.cpp +++ b/test/utils/crypto_helpers.cpp @@ -19,12 +19,25 @@ TEST(utils, pack_unpack) { /**/ TEST(utils, join) { - const auto strBuffer = "123"; - std::vector vstr(3); - vstr[0] = strBuffer[0]; - vstr[1] = strBuffer[1]; - vstr[2] = strBuffer[2]; + const std::string strBuffer = "123"; + const std::string strBuffer2 = "456"; + std::vector vstr { strBuffer, strBuffer2 }; std::string joined = join(vstr); - ASSERT_STREQ(joined.c_str(), strBuffer); + + ASSERT_STREQ(joined.c_str(), std::string(strBuffer + strBuffer2).c_str()); } + +/**/ + +TEST(utils, join_offset) { + const std::string strBuffer = "654"; + const std::string strBuffer2 = "321"; + std::vector vstr { strBuffer, strBuffer2 }; + + std::string joined = join(vstr, 1); + + ASSERT_STREQ(joined.c_str(), + std::string(strBuffer.substr(1) + + strBuffer2.substr(1)).c_str()); +} \ No newline at end of file From aa487a160c37837c8a6d58eb69dc4478aee58840 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 18 Oct 2019 07:15:01 -0700 Subject: [PATCH 02/21] fix: platformio tests src_filter (#164) --- test/platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/platformio.ini b/test/platformio.ini index 75350f86..eccb7229 100644 --- a/test/platformio.ini +++ b/test/platformio.ini @@ -17,7 +17,7 @@ libdeps_dir = ../extern/.piolibdeps [base] lib_deps = ArduinoJson@6.12.0, BIP66@0.2.1, micro-ecc@1.0.0, googletest@1.8.1 build_flags = -I../test -I../test/iot/ -I../src -I../src/lib -I../src/include/cpp-crypto -DUNIT_TEST -src_filter = +<../src> +<../test/iot> +src_filter = +<../src> +<../test> +<../test/iot> upload_speed = 921600 [common_tests] From 66790cc11e5d7e0bdf983d85e948fbb305328a85 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 18 Oct 2019 22:43:58 -0700 Subject: [PATCH 03/21] fix: cmake warnings (#167) --- cmake/extern/ArduinoJson.txt.in | 2 ++ cmake/extern/BIP66.txt.in | 2 ++ cmake/extern/GTest.txt.in | 2 ++ cmake/extern/uECC.txt.in | 2 ++ 4 files changed, 8 insertions(+) diff --git a/cmake/extern/ArduinoJson.txt.in b/cmake/extern/ArduinoJson.txt.in index abb0f7e7..76703973 100644 --- a/cmake/extern/ArduinoJson.txt.in +++ b/cmake/extern/ArduinoJson.txt.in @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 3.2) +project(arduinojson-download) + include(ExternalProject) # ------------------------------------------------------------------------------ diff --git a/cmake/extern/BIP66.txt.in b/cmake/extern/BIP66.txt.in index 73cdcfd4..2516f897 100644 --- a/cmake/extern/BIP66.txt.in +++ b/cmake/extern/BIP66.txt.in @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 3.2) +project(bip66-download) + include(ExternalProject) # ------------------------------------------------------------------------------ diff --git a/cmake/extern/GTest.txt.in b/cmake/extern/GTest.txt.in index 4d4372eb..1f7aa71c 100644 --- a/cmake/extern/GTest.txt.in +++ b/cmake/extern/GTest.txt.in @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 3.2) +project(googletest-download) + include(ExternalProject) # ------------------------------------------------------------------------------ diff --git a/cmake/extern/uECC.txt.in b/cmake/extern/uECC.txt.in index 8e68a5c2..e3a7cf08 100644 --- a/cmake/extern/uECC.txt.in +++ b/cmake/extern/uECC.txt.in @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 3.2) +project(uecc-download) + include(ExternalProject) # ------------------------------------------------------------------------------ From e42f707e16acbf2515023222fa4c90b8840f7448 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 27 Oct 2019 22:00:20 -0700 Subject: [PATCH 04/21] ci: split coverage workflows (#171) CodeClimate Coverage apparently attempts to use the `CC_TEST_REPORTER_ID` of the PR base for reporting to the upstream parent, which fails. This PR splits workflows so that CodeClimate Coverage onlys runs on Pushes to master/devleop. CodeCov will continue to run on PR's and Pushes. --- .github/workflows/codeclimate.yml | 35 +++++++++++++++++++++++++++++++ .github/workflows/coverage.yml | 27 ------------------------ 2 files changed, 35 insertions(+), 27 deletions(-) create mode 100644 .github/workflows/codeclimate.yml diff --git a/.github/workflows/codeclimate.yml b/.github/workflows/codeclimate.yml new file mode 100644 index 00000000..6ae03f8d --- /dev/null +++ b/.github/workflows/codeclimate.yml @@ -0,0 +1,35 @@ +name: CodeClimate Coverage + +on: + push: + branches: + - 'master' + - 'develop' + +jobs: + codeclimate: + runs-on: macOS-latest + + steps: + - uses: actions/checkout@v1 + - name: Set compiler to clang++ + run: COMPILER=clang++ + - name: Install Dependencies + run: brew install cmake lcov + - name: Make scripts executable + run: sudo chmod +x ./.github/workflows/test/clang_tidy.sh + - name: Setup Code Climate test-reporter + run: | + curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-darwin-amd64 > ./cc-test-reporter + chmod +x ./cc-test-reporter + - name: Build & Run Coverage Tests + run: | + ./cc-test-reporter before-build + cmake -DCMAKE_BUILD_TYPE=Coverage -DUNIT_TEST=ON . + cmake --build . + ./test/ark_cpp_crypto_tests + lcov --directory . --include "*/src/*" --include "*/test/*" --exclude "*/src/lib/*" --exclude "*/extern/*" --capture --output-file coverage.info --ignore-errors gcov + ./cc-test-reporter format-coverage --input-type lcov coverage.info + ./cc-test-reporter upload-coverage + env: + CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index c93cba72..ec1cbe5e 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -9,33 +9,6 @@ on: types: [ready_for_review, synchronize, opened] jobs: - codeclimate: - runs-on: macOS-latest - - steps: - - uses: actions/checkout@v1 - - name: Set compiler to clang++ - run: COMPILER=clang++ - - name: Install Dependencies - run: brew install cmake lcov - - name: Make scripts executable - run: sudo chmod +x ./.github/workflows/test/clang_tidy.sh - - name: Setup Code Climate test-reporter - run: | - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-darwin-amd64 > ./cc-test-reporter - chmod +x ./cc-test-reporter - - name: Build & Run Coverage Tests - run: | - ./cc-test-reporter before-build - cmake -DCMAKE_BUILD_TYPE=Coverage -DUNIT_TEST=ON . - cmake --build . - ./test/ark_cpp_crypto_tests - lcov --directory . --include "*/src/*" --include "*/test/*" --exclude "*/src/lib/*" --exclude "*/extern/*" --capture --output-file coverage.info --ignore-errors gcov - ./cc-test-reporter format-coverage --input-type lcov coverage.info - ./cc-test-reporter upload-coverage - env: - CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} - codecov: runs-on: ubuntu-latest From abab250c3625ca063906ceb85c8a1f4f7d541539 Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Mon, 28 Oct 2019 00:47:13 -0500 Subject: [PATCH 05/21] ci: call correct EXE for windows tests (#170) --- .github/workflows/test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b37dd239..6ebcd7f2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -115,4 +115,5 @@ jobs: shell: cmd run: msbuild "%GITHUB_WORKSPACE%\ark_cpp_crypto.sln" - name: Run Tests - run: call "%GITHUB_WORKSPACE%\test\Debug\ark_cpp_crypto_tests" + shell: cmd + run: "%GITHUB_WORKSPACE%\\test\\Debug\\ark_cpp_crypto_tests.exe" From 1230e47f6413332d2274f75bc5f779279612146f Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Mon, 28 Oct 2019 00:56:29 -0500 Subject: [PATCH 06/21] test: break up unit tests to support platforms with limited RAM (#172) Broke up unit tests into smaller parts to better support devices with limited RAM. This allows for the creation of smaller unit test firmware images. --- test/CMakeLists.txt | 20 +- test/crypto/curve.cpp | 124 ------- test/crypto/curve_ecdsa_sign.cpp | 22 ++ ...urve_ecdsa_sign_null_hash32_privatekey.cpp | 22 ++ .../curve_ecdsa_sign_null_hash32_sha256.cpp | 20 ++ test/crypto/curve_ecdsa_sign_null_pk.cpp | 20 ++ test/crypto/curve_publickey.cpp | 60 ++++ test/crypto/curve_verify_invalid.cpp | 19 + test/crypto/curve_verify_valid.cpp | 17 + test/crypto/message.cpp | 20 -- test/crypto/message_sign.cpp | 22 ++ test/crypto/message_verify.cpp | 21 ++ test/fixtures/identity.hpp | 6 +- test/identities/address.cpp | 8 +- test/identities/keys.cpp | 16 +- test/identities/keys_publickey.cpp | 16 + test/identities/privatekey.cpp | 5 +- test/identities/publickey.cpp | 5 +- test/identities/wif.cpp | 4 +- test/transactions/builder.cpp | 35 +- .../builder_transfer_custom_network.cpp | 34 ++ test/transactions/deserializer.cpp | 214 ------------ .../deserializer_delegate_registration.cpp | 41 +++ ...erializer_multi_signature_registration.cpp | 69 ++++ ...rializer_second_signature_registration.cpp | 40 +++ test/transactions/deserializer_transfer.cpp | 34 ++ test/transactions/deserializer_vote.cpp | 38 ++ test/transactions/transaction.cpp | 330 ------------------ test/transactions/transaction_to_array.cpp | 180 ++++++++++ test/transactions/transaction_to_json.cpp | 169 +++++++++ 30 files changed, 889 insertions(+), 742 deletions(-) delete mode 100644 test/crypto/curve.cpp create mode 100644 test/crypto/curve_ecdsa_sign.cpp create mode 100644 test/crypto/curve_ecdsa_sign_null_hash32_privatekey.cpp create mode 100644 test/crypto/curve_ecdsa_sign_null_hash32_sha256.cpp create mode 100644 test/crypto/curve_ecdsa_sign_null_pk.cpp create mode 100644 test/crypto/curve_publickey.cpp create mode 100644 test/crypto/curve_verify_invalid.cpp create mode 100644 test/crypto/curve_verify_valid.cpp create mode 100644 test/crypto/message_sign.cpp create mode 100644 test/crypto/message_verify.cpp create mode 100644 test/identities/keys_publickey.cpp create mode 100644 test/transactions/builder_transfer_custom_network.cpp delete mode 100644 test/transactions/deserializer.cpp create mode 100644 test/transactions/deserializer_delegate_registration.cpp create mode 100644 test/transactions/deserializer_multi_signature_registration.cpp create mode 100644 test/transactions/deserializer_second_signature_registration.cpp create mode 100644 test/transactions/deserializer_transfer.cpp create mode 100644 test/transactions/deserializer_vote.cpp create mode 100644 test/transactions/transaction_to_array.cpp create mode 100644 test/transactions/transaction_to_json.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 09a5e8e5..a12ab4a7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -42,8 +42,16 @@ set(COMMON_TEST_SOURCE # Crypto set(CRYPTO_TEST_SOURCE - ${PROJECT_SOURCE_DIR}/crypto/curve.cpp + ${PROJECT_SOURCE_DIR}/crypto/curve_ecdsa_sign_null_hash32_privatekey.cpp + ${PROJECT_SOURCE_DIR}/crypto/curve_ecdsa_sign_null_hash32_sha256.cpp + ${PROJECT_SOURCE_DIR}/crypto/curve_ecdsa_sign_null_pk.cpp + ${PROJECT_SOURCE_DIR}/crypto/curve_ecdsa_sign.cpp + ${PROJECT_SOURCE_DIR}/crypto/curve_publickey.cpp + ${PROJECT_SOURCE_DIR}/crypto/curve_verify_invalid.cpp + ${PROJECT_SOURCE_DIR}/crypto/curve_verify_valid.cpp ${PROJECT_SOURCE_DIR}/crypto/hash.cpp + ${PROJECT_SOURCE_DIR}/crypto/message_sign.cpp + ${PROJECT_SOURCE_DIR}/crypto/message_verify.cpp ${PROJECT_SOURCE_DIR}/crypto/message.cpp ${PROJECT_SOURCE_DIR}/crypto/slot.cpp) @@ -59,6 +67,7 @@ set(DEFAULTS_TEST_SOURCE set(IDENTITIES_TEST_SOURCE ${PROJECT_SOURCE_DIR}/identities/address.cpp + ${PROJECT_SOURCE_DIR}/identities/keys_publickey.cpp ${PROJECT_SOURCE_DIR}/identities/keys.cpp ${PROJECT_SOURCE_DIR}/identities/privatekey.cpp ${PROJECT_SOURCE_DIR}/identities/publickey.cpp @@ -83,9 +92,16 @@ set(NETWORKS_TEST_SOURCE # Transactions set(TRANSACTIONS_TEST_SOURCE + ${PROJECT_SOURCE_DIR}/transactions/builder_transfer_custom_network.cpp ${PROJECT_SOURCE_DIR}/transactions/builder.cpp - ${PROJECT_SOURCE_DIR}/transactions/deserializer.cpp + ${PROJECT_SOURCE_DIR}/transactions/deserializer_delegate_registration.cpp + ${PROJECT_SOURCE_DIR}/transactions/deserializer_multi_signature_registration.cpp + ${PROJECT_SOURCE_DIR}/transactions/deserializer_second_signature_registration.cpp + ${PROJECT_SOURCE_DIR}/transactions/deserializer_transfer.cpp + ${PROJECT_SOURCE_DIR}/transactions/deserializer_vote.cpp ${PROJECT_SOURCE_DIR}/transactions/serializer.cpp + ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array.cpp + ${PROJECT_SOURCE_DIR}/transactions/transaction_to_json.cpp ${PROJECT_SOURCE_DIR}/transactions/transaction.cpp) # ------------------------------------------------------------------------------ diff --git a/test/crypto/curve.cpp b/test/crypto/curve.cpp deleted file mode 100644 index 1b3632d8..00000000 --- a/test/crypto/curve.cpp +++ /dev/null @@ -1,124 +0,0 @@ - -#include "gtest/gtest.h" - -#include "crypto/curve.hpp" - -#include "fixtures/identity.hpp" -#include "fixtures/message.hpp" -using namespace Ark::Crypto; -using namespace fixtures::identity; -using namespace fixtures::message; - -/**/ - -TEST(crypto, curve_ecdsa_sign) { - std::vector signature; - Curve::Ecdsa::sign(tMessageSha256Bytes.data(), - tPrivateKeyBytes.data(), - signature); - - for (auto i = 0U; i < signature.size(); ++i) { - ASSERT_EQ(signature.at(i), tMessageSignatureBytes.at(i)); - }; -} - -/**/ - -TEST(crypto, curve_ecdsa_sign_null_hash32) { - std::vector signature; - Curve::Ecdsa::sign(nullptr, - tPrivateKeyBytes.data(), - signature); - ASSERT_FALSE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), - tPublicKeyBytes.data(), - signature)); - - signature.clear(); - - Curve::Ecdsa::sign(tMessageSha256Bytes.data(), - nullptr, - signature); - ASSERT_FALSE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), - tPublicKeyBytes.data(), - signature)); -} - -/**/ - -TEST(crypto, curve_ecdsa_sign_null_pk) { - std::vector signature; - Curve::Ecdsa::sign(tMessageSha256Bytes.data(), - nullptr, - signature); - ASSERT_FALSE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), - tPublicKeyBytes.data(), - signature)); -} - -/**/ - -TEST(crypto, crypto_verify_valid) { - ASSERT_TRUE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), - tPublicKeyBytes.data(), - { tMessageSignatureBytes.begin(), - tMessageSignatureBytes.end() })); -} - -/**/ - -TEST(crypto, crypto_verify_invalid) { - ASSERT_FALSE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), - invalid::tPublicKeyBytes.data(), - { tMessageSignatureBytes.begin(), - tMessageSignatureBytes.end() })); -} - -/**/ - -TEST(crypto, curve_publickey_compute) { - const auto publicKey = Curve::PublicKey::compute(tPrivateKeyBytes.data()); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; ++i) { - ASSERT_EQ(publicKey.at(i), tPublicKeyBytes.at(i)); - }; -} - -/**/ - -TEST(crypto, curve_publickey_compute_invalid) { - const auto publicKey = - Curve::PublicKey::compute(invalid::tPrivateKeyBytes.data()); - for (auto& e : publicKey) { - ASSERT_EQ(e, 0); - }; -} - -/**/ - -TEST(crypto, curve_publickey_compress) { - const auto compressed = - Curve::PublicKey::compress(tUncompressedPublicKeyBytes.data()); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; ++i) { - ASSERT_EQ(compressed.at(i), tPublicKeyBytes.at(i)); - }; -} - -/**/ - -TEST(crypto, curve_publickey_decompress) { - const auto decompressed = Curve::PublicKey::decompress(tPublicKeyBytes.data()); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; ++i) { - ASSERT_EQ(decompressed.at(i), tUncompressedPublicKeyBytes.at(i)); - }; -} - -/**/ - -TEST(crypto, curve_publickey_validate) { - ASSERT_TRUE(Curve::PublicKey::validate(tPublicKeyBytes.data())); -} - -/**/ - -TEST(crypto, curve_publickey_validate_invalid) { - ASSERT_FALSE(Curve::PublicKey::validate(invalid::tPublicKeyBytes.data())); -} diff --git a/test/crypto/curve_ecdsa_sign.cpp b/test/crypto/curve_ecdsa_sign.cpp new file mode 100644 index 00000000..04e2f916 --- /dev/null +++ b/test/crypto/curve_ecdsa_sign.cpp @@ -0,0 +1,22 @@ + +#include "gtest/gtest.h" + +#include "crypto/curve.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/message.hpp" +using namespace Ark::Crypto; +using namespace fixtures::identity; +using namespace fixtures::message; + +/**/ +TEST(crypto, curve_ecdsa_sign) { + std::vector signature; + Curve::Ecdsa::sign(tMessageSha256Bytes.data(), + tPrivateKeyBytes.data(), + signature); + + for (auto i = 0U; i < signature.size(); ++i) { + ASSERT_EQ(signature.at(i), tMessageSignatureBytes.at(i)); + }; +} diff --git a/test/crypto/curve_ecdsa_sign_null_hash32_privatekey.cpp b/test/crypto/curve_ecdsa_sign_null_hash32_privatekey.cpp new file mode 100644 index 00000000..640f76ff --- /dev/null +++ b/test/crypto/curve_ecdsa_sign_null_hash32_privatekey.cpp @@ -0,0 +1,22 @@ + +#include "gtest/gtest.h" + +#include "crypto/curve.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/message.hpp" +using namespace Ark::Crypto; +using namespace fixtures::identity; +using namespace fixtures::message; + +/**/ +TEST(crypto, curve_ecdsa_sign_null_hash32_privatekey) { + std::vector signature; + Curve::Ecdsa::sign(nullptr, + tPrivateKeyBytes.data(), + signature); + ASSERT_FALSE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), + tPublicKeyBytes.data(), + signature)); + +} diff --git a/test/crypto/curve_ecdsa_sign_null_hash32_sha256.cpp b/test/crypto/curve_ecdsa_sign_null_hash32_sha256.cpp new file mode 100644 index 00000000..3d571aa7 --- /dev/null +++ b/test/crypto/curve_ecdsa_sign_null_hash32_sha256.cpp @@ -0,0 +1,20 @@ + +#include "gtest/gtest.h" + +#include "crypto/curve.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/message.hpp" +using namespace Ark::Crypto; +using namespace fixtures::identity; +using namespace fixtures::message; + +TEST(crypto, curve_ecdsa_sign_null_hash32_sha256) { + std::vector signature; + Curve::Ecdsa::sign(tMessageSha256Bytes.data(), + nullptr, + signature); + ASSERT_FALSE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), + tPublicKeyBytes.data(), + signature)); +} diff --git a/test/crypto/curve_ecdsa_sign_null_pk.cpp b/test/crypto/curve_ecdsa_sign_null_pk.cpp new file mode 100644 index 00000000..d3ee0deb --- /dev/null +++ b/test/crypto/curve_ecdsa_sign_null_pk.cpp @@ -0,0 +1,20 @@ + +#include "gtest/gtest.h" + +#include "crypto/curve.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/message.hpp" +using namespace Ark::Crypto; +using namespace fixtures::identity; +using namespace fixtures::message; + +TEST(crypto, curve_ecdsa_sign_null_pk) { + std::vector signature; + Curve::Ecdsa::sign(tMessageSha256Bytes.data(), + nullptr, + signature); + ASSERT_FALSE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), + tPublicKeyBytes.data(), + signature)); +} diff --git a/test/crypto/curve_publickey.cpp b/test/crypto/curve_publickey.cpp new file mode 100644 index 00000000..a705d7cb --- /dev/null +++ b/test/crypto/curve_publickey.cpp @@ -0,0 +1,60 @@ + +#include "gtest/gtest.h" + +#include "crypto/curve.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/message.hpp" +using namespace Ark::Crypto; +using namespace fixtures::identity; +using namespace fixtures::message; + +/**/ + +TEST(crypto, curve_publickey_compute) { + const auto publicKey = Curve::PublicKey::compute(tPrivateKeyBytes.data()); + for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; ++i) { + ASSERT_EQ(publicKey.at(i), tPublicKeyBytes.at(i)); + }; +} + +/**/ + +TEST(crypto, curve_publickey_compute_invalid) { + const auto publicKey = + Curve::PublicKey::compute(invalid::tPrivateKeyBytes.data()); + for (auto& e : publicKey) { + ASSERT_EQ(e, 0); + }; +} + +/**/ + +TEST(crypto, curve_publickey_compress) { + const auto compressed = + Curve::PublicKey::compress(tUncompressedPublicKeyBytes.data()); + for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; ++i) { + ASSERT_EQ(compressed.at(i), tPublicKeyBytes.at(i)); + }; +} + +/**/ + +TEST(crypto, curve_publickey_decompress) { + const auto decompressed = Curve::PublicKey::decompress(tPublicKeyBytes.data()); + for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; ++i) { + ASSERT_EQ(decompressed.at(i), tUncompressedPublicKeyBytes.at(i)); + }; +} + +/**/ + +TEST(crypto, curve_publickey_validate) { + ASSERT_TRUE(Curve::PublicKey::validate(tPublicKeyBytes.data())); +} + +/**/ + +TEST(crypto, curve_publickey_validate_invalid) { + ASSERT_FALSE(Curve::PublicKey::validate(invalid::tPublicKeyBytes.data())); +} diff --git a/test/crypto/curve_verify_invalid.cpp b/test/crypto/curve_verify_invalid.cpp new file mode 100644 index 00000000..7b4c7839 --- /dev/null +++ b/test/crypto/curve_verify_invalid.cpp @@ -0,0 +1,19 @@ + +#include "gtest/gtest.h" + +#include "crypto/curve.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/message.hpp" +using namespace Ark::Crypto; +using namespace fixtures::identity; +using namespace fixtures::message; + +/**/ + +TEST(crypto, crypto_verify_invalid) { + ASSERT_FALSE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), + invalid::tPublicKeyBytes.data(), + { tMessageSignatureBytes.begin(), + tMessageSignatureBytes.end() })); +} diff --git a/test/crypto/curve_verify_valid.cpp b/test/crypto/curve_verify_valid.cpp new file mode 100644 index 00000000..4099ca16 --- /dev/null +++ b/test/crypto/curve_verify_valid.cpp @@ -0,0 +1,17 @@ + +#include "gtest/gtest.h" + +#include "crypto/curve.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/message.hpp" +using namespace Ark::Crypto; +using namespace fixtures::identity; +using namespace fixtures::message; + +TEST(crypto, crypto_verify_valid) { + ASSERT_TRUE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), + tPublicKeyBytes.data(), + { tMessageSignatureBytes.begin(), + tMessageSignatureBytes.end() })); +} diff --git a/test/crypto/message.cpp b/test/crypto/message.cpp index 0fb4d2a3..84975de2 100644 --- a/test/crypto/message.cpp +++ b/test/crypto/message.cpp @@ -12,26 +12,6 @@ using namespace Ark::Crypto; using namespace fixtures::identity; using namespace fixtures::message; -TEST(crypto, message_sign) { - Message message; - message.sign(tMessageString, tPassphrase); - - ASSERT_STREQ(BytesToHex(message.signature.begin(), - message.signature.end()).c_str(), - tSignatureString); -} - -/**/ - -TEST(crypto, message_verify) { - Message message(tMessageString, - tPublicKeyBytes, - { tMessageSignatureBytes.begin(), - tMessageSignatureBytes.end() }); - ASSERT_TRUE(message.verify()); -} - -/**/ TEST(crypto, message_to_array) { Message message; diff --git a/test/crypto/message_sign.cpp b/test/crypto/message_sign.cpp new file mode 100644 index 00000000..9539a6d7 --- /dev/null +++ b/test/crypto/message_sign.cpp @@ -0,0 +1,22 @@ + +#include "gtest/gtest.h" + +#include + +#include "crypto/message.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/message.hpp" +#include "utils/hex.hpp" +using namespace Ark::Crypto; +using namespace fixtures::identity; +using namespace fixtures::message; + +TEST(crypto, message_sign) { + Message message; + message.sign(tMessageString, tPassphrase); + + ASSERT_STREQ(BytesToHex(message.signature.begin(), + message.signature.end()).c_str(), + tSignatureString); +} diff --git a/test/crypto/message_verify.cpp b/test/crypto/message_verify.cpp new file mode 100644 index 00000000..78086ae7 --- /dev/null +++ b/test/crypto/message_verify.cpp @@ -0,0 +1,21 @@ + +#include "gtest/gtest.h" + +#include + +#include "crypto/message.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/message.hpp" +#include "utils/hex.hpp" +using namespace Ark::Crypto; +using namespace fixtures::identity; +using namespace fixtures::message; + +TEST(crypto, message_verify) { + Message message(tMessageString, + tPublicKeyBytes, + { tMessageSignatureBytes.begin(), + tMessageSignatureBytes.end() }); + ASSERT_TRUE(message.verify()); +} diff --git a/test/fixtures/identity.hpp b/test/fixtures/identity.hpp index 1cb17c57..78053ae7 100644 --- a/test/fixtures/identity.hpp +++ b/test/fixtures/identity.hpp @@ -7,8 +7,8 @@ * file that was distributed with this source code. **/ -// #ifndef CRYPTO_FIXTURES_IDENTITY_HPP -// #define CRYPTO_FIXTURES_IDENTITY_HPP +#ifndef CRYPTO_FIXTURES_IDENTITY_HPP +#define CRYPTO_FIXTURES_IDENTITY_HPP #include @@ -93,4 +93,4 @@ namespace identity { // NOLINT } // Crypto } // Ark -// #endif +#endif diff --git a/test/identities/address.cpp b/test/identities/address.cpp index 112824c8..783bbe9c 100644 --- a/test/identities/address.cpp +++ b/test/identities/address.cpp @@ -1,10 +1,14 @@ #include "gtest/gtest.h" -#include +#include "identities/address.hpp" +#include "identities/keys.hpp" +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; #include "fixtures/identity.hpp" -using namespace fixtures::identity; +using namespace Ark::Crypto::fixtures::identity; + TEST(identities, address_construct_bytes) { Address address(tAddressBytes, tAddressVersion); diff --git a/test/identities/keys.cpp b/test/identities/keys.cpp index 1a3e81e4..0d605caa 100644 --- a/test/identities/keys.cpp +++ b/test/identities/keys.cpp @@ -1,10 +1,13 @@ #include "gtest/gtest.h" -#include +#include "identities/keys.hpp" +#include "interfaces/identities.hpp" #include "fixtures/identity.hpp" -using namespace fixtures::identity; +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; +using namespace Ark::Crypto::fixtures::identity; TEST(identities, keys_from_passphrase) { auto keys = Keys::fromPassphrase(tPassphrase); @@ -59,12 +62,3 @@ TEST(identities, keys_privatekey_from_wif) { }; ASSERT_EQ(outVersion, tWifVersion); } - -/**/ - -TEST(identities, keys_publickey_from_privatekey) { - auto publicKey = Keys::PublicKey::fromPrivateKey(tPrivateKeyBytes.data()); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; i++) { - ASSERT_EQ(publicKey.at(i), tPublicKeyBytes.at(i)); - }; -} diff --git a/test/identities/keys_publickey.cpp b/test/identities/keys_publickey.cpp new file mode 100644 index 00000000..466866fb --- /dev/null +++ b/test/identities/keys_publickey.cpp @@ -0,0 +1,16 @@ +#include "gtest/gtest.h" + +#include "identities/keys.hpp" +#include "interfaces/identities.hpp" + +#include "fixtures/identity.hpp" +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; +using namespace Ark::Crypto::fixtures::identity; + +TEST(identities, keys_publickey_from_privatekey) { + auto publicKey = Keys::PublicKey::fromPrivateKey(tPrivateKeyBytes.data()); + for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; i++) { + ASSERT_EQ(publicKey.at(i), tPublicKeyBytes.at(i)); + }; +} diff --git a/test/identities/privatekey.cpp b/test/identities/privatekey.cpp index db61001a..7fe6f22a 100644 --- a/test/identities/privatekey.cpp +++ b/test/identities/privatekey.cpp @@ -1,7 +1,8 @@ - #include "gtest/gtest.h" -#include +#include "identities/privatekey.hpp" +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; #include "fixtures/identity.hpp" using namespace fixtures::identity; diff --git a/test/identities/publickey.cpp b/test/identities/publickey.cpp index f89e3a65..bb5e6f33 100644 --- a/test/identities/publickey.cpp +++ b/test/identities/publickey.cpp @@ -1,7 +1,8 @@ - #include "gtest/gtest.h" -#include +#include "identities/publickey.hpp" +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; #include "fixtures/identity.hpp" using namespace fixtures::identity; diff --git a/test/identities/wif.cpp b/test/identities/wif.cpp index 699549d9..52825d24 100644 --- a/test/identities/wif.cpp +++ b/test/identities/wif.cpp @@ -1,7 +1,9 @@ #include "gtest/gtest.h" -#include +#include "identities/wif.hpp" +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; #include "fixtures/identity.hpp" using namespace fixtures::identity; diff --git a/test/transactions/builder.cpp b/test/transactions/builder.cpp index 53fb81ac..b8ba150d 100644 --- a/test/transactions/builder.cpp +++ b/test/transactions/builder.cpp @@ -1,7 +1,10 @@ #include "gtest/gtest.h" -#include +#include "transactions/builder.h" +#include "common/fee_policy.hpp" +using namespace Ark::Crypto::Transactions; +using namespace Ark::Crypto; TEST(transactions, build_transfer) { const auto actual = @@ -18,36 +21,6 @@ TEST(transactions, build_transfer) { ASSERT_TRUE(actual.vendorField.empty()); } -/**/ - -TEST(transactions, build_transfer_custom_network) { - static const Network MyCustomNetwork = { - "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", - 1, 0x53, 0xaa, - "2019-04-12T13:00:00.000Z" - }; - - const Configuration myCustomConfiguration(MyCustomNetwork); - - const auto transaction = Builder::buildTransfer( - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 100000000ULL, - "this is a custom bridgechain transaction", - "this is a top secret passphrase", - "this is a top secret passphrase too", - myCustomConfiguration); - - ASSERT_EQ(0, transaction.type); - ASSERT_EQ(myCustomConfiguration.getFee(transaction.type), transaction.fee); - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - transaction.recipient.c_str()); - ASSERT_EQ(100000000ULL, transaction.amount); - ASSERT_STREQ("this is a custom bridgechain transaction", - transaction.vendorField.c_str()); -} - -/**/ - TEST(transactions, build_empty_transaction) { // test 0 ARKtoshi value const auto shouldBeEmpty = Ark::Crypto::Transactions::Builder::buildTransfer( diff --git a/test/transactions/builder_transfer_custom_network.cpp b/test/transactions/builder_transfer_custom_network.cpp new file mode 100644 index 00000000..5a88a4ef --- /dev/null +++ b/test/transactions/builder_transfer_custom_network.cpp @@ -0,0 +1,34 @@ + +#include "gtest/gtest.h" + +#include "transactions/builder.h" +using namespace Ark::Crypto::Transactions; + +#include "common/network.hpp" +using namespace Ark::Crypto; + +TEST(transactions, build_transfer_custom_network) { + static const Network MyCustomNetwork = { + "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", + 1, 0x53, 0xaa, + "2019-04-12T13:00:00.000Z" + }; + + const Configuration myCustomConfiguration(MyCustomNetwork); + + const auto transaction = Builder::buildTransfer( + "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", + 100000000ULL, + "this is a custom bridgechain transaction", + "this is a top secret passphrase", + "this is a top secret passphrase too", + myCustomConfiguration); + + ASSERT_EQ(0, transaction.type); + ASSERT_EQ(myCustomConfiguration.getFee(transaction.type), transaction.fee); + ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", + transaction.recipient.c_str()); + ASSERT_EQ(100000000ULL, transaction.amount); + ASSERT_STREQ("this is a custom bridgechain transaction", + transaction.vendorField.c_str()); +} diff --git a/test/transactions/deserializer.cpp b/test/transactions/deserializer.cpp deleted file mode 100644 index 5e262d8e..00000000 --- a/test/transactions/deserializer.cpp +++ /dev/null @@ -1,214 +0,0 @@ - -#include "gtest/gtest.h" - -#include - -TEST(transactions, deserialize_transfer) { // NOLINT - // transfer/passphrase-with-vendor-field.json - Ark::Crypto::Transactions::Deserializer deserializer( - "ff011e0007627802034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19280969800000000000b48656c6c6f" - "20576f726c6400c2eb0b00000000000000001e0995750207ecaf0ccf251c1265b92ad84f553662304402205616d6e361439d67a5c2067b" - "bfc8fce61b93061a4fa113315a1c5cf965ff6f3202200a1d99caaa98aeebcec04edd5365352500addb830c79f49b9de484ec616bb1e1"); - auto actual = deserializer.deserialize(); - - ASSERT_EQ(0xFF, actual.header); - ASSERT_EQ(1, actual.version); - ASSERT_EQ(30, actual.network); - ASSERT_EQ(TransactionTypes::Transfer, actual.type); - ASSERT_EQ(41443847UL, actual.timestamp); - ASSERT_STREQ( - "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", - actual.senderPublicKey.c_str()); - ASSERT_TRUE(10000000ULL == actual.fee); - ASSERT_STREQ("48656c6c6f20576f726c64", actual.vendorFieldHex.c_str()); - ASSERT_STREQ("Hello World", actual.vendorField.c_str()); - ASSERT_TRUE(200000000ULL == actual.amount); - ASSERT_STREQ( - "ecf558fbddd62ae42edcfcba02f402d987a94b72a7636ef1121e8625487e2a1e", - actual.id.c_str()); - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", actual.recipient.c_str()); - ASSERT_STREQ( - "304402205616d6e361439d67a5c2067bbfc8fce61b93061a4fa113315a1c5cf965ff6f3202200a1d99caaa98aeebcec04edd5365352500addb830c79f49b9de484ec616bb1e1", - actual.signature.c_str()); - ASSERT_TRUE(actual.verify()); -} - -/**/ - -TEST(transactions, deserialize_second_signature_registration) { // NOLINT - // second_signature_registration/second-passphrase.json - Ark::Crypto::Transactions::Deserializer deserializer( - "ff011e013bc27502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed1920065cd1d000000000003" - "699e966b2525f9088a6941d8d94f7869964a000efe65783d78ac82e1199fe609304402202aab49477dd3531e4473196d08fbd7" - "c00ebb79223d5eaaeaf02c52c4041a86cf02201a7d82655f9b1d22af3ea94e6f183649bb4610cdeca3b9e20d6c8773f869831c"); - auto actual = deserializer.deserialize(); - - ASSERT_EQ(0xFF, actual.header); - ASSERT_EQ(1, actual.version); - ASSERT_EQ(30, actual.network); - ASSERT_EQ(TransactionTypes::SecondSignatureRegistration, actual.type); - ASSERT_EQ(41271867UL, actual.timestamp); - ASSERT_STREQ( - "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", - actual.senderPublicKey.c_str()); - ASSERT_TRUE(500000000ULL == actual.fee); - ASSERT_TRUE(0UL == actual.amount); - ASSERT_EQ(0, actual.expiration); - ASSERT_STREQ( - "6d1615924d172d352c8f44d4ded84cbbece3c03ebb3e4fc3f3334784ae332590", - actual.id.c_str()); - ASSERT_STREQ( - "03699e966b2525f9088a6941d8d94f7869964a000efe65783d78ac82e1199fe609", - actual.asset.signature.publicKey.c_str()); - ASSERT_STREQ( - "304402202aab49477dd3531e4473196d08fbd7c00ebb79223d5eaaeaf02c52c4041a86cf02201a7d82655f9b1d22af3ea94e6f183649bb4610cdeca3b9e20d6c8773f869831c", - actual.signature.c_str()); - ASSERT_TRUE(actual.verify()); - - // special case as the type 1 transaction itself has no recipient - const auto publicKey = Ark::Crypto::identities::PublicKey::fromHex(actual.senderPublicKey.c_str()); - const auto address = Ark::Crypto::identities::Address::fromPublicKey(publicKey.toBytes().data(), actual.network); - ASSERT_STREQ(address.toString().c_str(), actual.recipient.c_str()); -} - -/**/ - -#if 0 -TEST(transactions, deserialize_delegate_registration) { // NOLINT - // delegate_registration/second-passphrase.json - Ark::Crypto::Transactions::Deserializer deserializer( - "ff011e02b0b87502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200f902950000000000" - "09626f6c646e696e6a613045022100f21b742fa052cd18de43328e1d068539ba7cbe9d33a9dcbd862a82871383955d022005" - "3b06d22ed3e3ad6168c6b27aa0ec68e7e40958c7709aec0e1555087ea9ad94304402207da580da4feec955edcb8e8eb36947" - "867b439de3d28d38e58c844fd8c45b564302200e6741b6ad11c2588a57b3afd180df1e9b345d48a9c2ae98be57dced869cf38c" - ); - auto actual = deserializer.deserialize(); - - ASSERT_EQ(0xFF, actual.header); - ASSERT_EQ(2, actual.type); - ASSERT_EQ(1, actual.version); - ASSERT_EQ(30, actual.network); - ASSERT_EQ(TransactionTypes::DelegateRegistration, actual.type); - ASSERT_EQ(41269424UL, actual.timestamp); - ASSERT_STREQ( - "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", - actual.senderPublicKey.c_str()); - ASSERT_EQ(2500000000ULL, actual.fee); - ASSERT_EQ(0ULL, actual.amount); - ASSERT_EQ(0, actual.expiration); - ASSERT_STREQ( - "bf7e018ff9c0066f7a9f51e95d3f78c08cad5dd8581325d630d64350181a91bb", - actual.id.c_str()); - ASSERT_STREQ("boldninja", actual.asset.delegate.username.c_str()); - ASSERT_STREQ( - "3045022100f21b742fa052cd18de43328e1d068539ba7cbe9d33a9dcbd862a82871383955d0220053b06d22ed3e3ad6168c6b27aa0ec68e7e40958c7709aec0e1555087ea9ad94", - actual.signature.c_str()); - ASSERT_STREQ( - "304402207da580da4feec955edcb8e8eb36947867b439de3d28d38e58c844fd8c45b564302200e6741b6ad11c2588a57b3afd180df1e9b345d48a9c2ae98be57dced869cf38c", - actual.secondSignature.c_str()); - ASSERT_TRUE(actual.verify())); -} -#endif - -/**/ - -TEST(transactions, deserialize_vote) { // NOLINT - // vote/second-passphrase.json - Ark::Crypto::Transactions::Deserializer deserializer( - "ff011e0376b87502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200e1f50500000000000101022cca95" - "29ec97a772156c152a00aad155ee6708243e65c9d211a589cb5d43234d304402204b8bb403e2db7f9599d46d0f5d39f8bb1d0663d875af7e" - "c1154448e98466e86302201e92fb57e13fb729b07e1027fa3d6e3f28e0d5828ed2d7c53a5e8db08cb6d068304402201329882762a42d1af9" - "079c822a9e3feefa47b7476b0afe61440637408958a64402206da179b08e31d9c784fbb23abe2c9b50353ed7881dc29787a5e8ecbee2dfda66"); - auto actual = deserializer.deserialize(); - - ASSERT_EQ(0xFF, actual.header); - ASSERT_EQ(1, actual.version); - ASSERT_EQ(30, actual.network); - ASSERT_EQ(TransactionTypes::Vote, actual.type); - ASSERT_EQ(41269366UL, actual.timestamp); - ASSERT_STREQ("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", actual.senderPublicKey.c_str()); - ASSERT_TRUE(100000000ULL == actual.fee); - ASSERT_TRUE(0ULL == actual.amount); - ASSERT_EQ(0, actual.expiration); - ASSERT_STREQ("16f28a180cd6f3ea46c10f358a457989e956e9d355258230d0c7b07acec10b73", actual.id.c_str()); - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", actual.recipient.c_str()); - - std::vector votes = {std::string("+022cca9529ec97a772156c152a00aad155ee6708243e65c9d211a589cb5d43234d")}; - ASSERT_EQ(1, actual.asset.votes.size()); - ASSERT_STREQ(votes[0].c_str(), actual.asset.votes[0].c_str()); - - ASSERT_STREQ( - "304402204b8bb403e2db7f9599d46d0f5d39f8bb1d0663d875af7ec1154448e98466e86302201e92fb57e13fb729b07e1027fa3d6e3f28e0d5828ed2d7c53a5e8db08cb6d068", - actual.signature.c_str()); - ASSERT_STREQ( - "304402201329882762a42d1af9079c822a9e3feefa47b7476b0afe61440637408958a64402206da179b08e31d9c784fbb23abe2c9b50353ed7881dc29787a5e8ecbee2dfda66", - actual.secondSignature.c_str()); - ASSERT_TRUE(actual.verify()); -} - -/**/ - -TEST(transactions, deserialize_multi_signature_registration) { // NOLINT - // multi_signature_registration/passphrase.json - Ark::Crypto::Transactions::Deserializer deserializer( - "ff011704724c9a00036928c98ee53a1f52ed01dd87db10ffe1980eb47cd7c0a7d688321f47b5d7d76000943577000000000002031803543c" - "6cc3545be6bac09c82721973a052c690658283472e88f24d14739f75acc80276dc5b8706a85ca9fdc46e571ac84e52fbb48e13ec7a165a80" - "731b44ae89f1fc02e8d5d17eb17bbc8d7bf1001d29a2d25d1249b7bb7a5b7ad8b7422063091f4b3130440220324d89c5792e4a54ae70b4f1" - "e27e2f87a8b7169cc6f2f7b2c83dba894960f987022053b8d0ae23ff9d1769364db7b6fd03216d93753c82a711c3558045e787bc01a53044" - "02201fcd54a9ac9c0269b8cec213566ddf43207798e2cf9ca1ce3c5d315d66321c6902201aa94c4ed3e5e479a12220aa886b259e488eb89b" - "697c711f91e8c03b9620e0b1ff304502210097f17c8eecf36f86a967cc52a83fa661e4ffc70cc4ea08df58673669406d424c0220798f5710" - "897b75dda42f6548f841afbe4ed1fa262097112cf5a1b3f7dade60e4304402201a4a4c718bfdc699bbb891b2e89be018027d2dcd10640b5d" - "df07802424dab78e02204ec7c7d505d2158c3b51fdd3843d16aecd2eaaa4c6c7a555ef123c5e59fd41fb304402207e660489bced5ce80c33" - "d45c86781b63898775ab4a231bb48780f97b40073a63022026f0cefd0d83022d822522ab4366a82e3b89085c328817919939f2efeabd913d"); - auto actual = deserializer.deserialize(); - - ASSERT_EQ(0xFF, actual.header); - ASSERT_EQ(1, actual.version); - ASSERT_EQ(23, actual.network); - ASSERT_EQ(TransactionTypes::MultiSignatureRegistration, actual.type); - ASSERT_EQ(10112114UL, actual.timestamp); - ASSERT_STREQ("036928c98ee53a1f52ed01dd87db10ffe1980eb47cd7c0a7d688321f47b5d7d760", actual.senderPublicKey.c_str()); - ASSERT_TRUE(2000000000ULL == actual.fee); - ASSERT_TRUE(0ULL == actual.amount); - ASSERT_EQ(0, actual.expiration); - - std::vector keysgroup = { - "+03543c6cc3545be6bac09c82721973a052c690658283472e88f24d14739f75acc8", - "+0276dc5b8706a85ca9fdc46e571ac84e52fbb48e13ec7a165a80731b44ae89f1fc", - "+02e8d5d17eb17bbc8d7bf1001d29a2d25d1249b7bb7a5b7ad8b7422063091f4b31" - }; - - ASSERT_EQ(3, actual.asset.multiSignature.keysgroup.size()); - ASSERT_EQ(2, actual.asset.multiSignature.min); - ASSERT_EQ(24, actual.asset.multiSignature.lifetime); - - for (uint8_t i = 0; i < keysgroup.size(); i++) { - ASSERT_STREQ(keysgroup[i].c_str(), actual.asset.multiSignature.keysgroup[i].c_str()); - }; - - ASSERT_STREQ("cbd6862966bb1b03ba742397b7e5a88d6eefb393a362ead0d605723b840db2af", actual.id.c_str()); - - ASSERT_STREQ( - "30440220324d89c5792e4a54ae70b4f1e27e2f87a8b7169cc6f2f7b2c83dba894960f987022053b8d0ae23ff9d1769364db7b6fd03216d93753c82a711c3558045e787bc01a5", - actual.signature.c_str()); - ASSERT_STREQ( - "304402201fcd54a9ac9c0269b8cec213566ddf43207798e2cf9ca1ce3c5d315d66321c6902201aa94c4ed3e5e479a12220aa886b259e488eb89b697c711f91e8c03b9620e0b1", - actual.secondSignature.c_str()); - - std::vector signatures = { - "304502210097f17c8eecf36f86a967cc52a83fa661e4ffc70cc4ea08df58673669406d424c0220798f5710897b75dda42f6548f841afbe4ed1fa262097112cf5a1b3f7dade60e4", - "304402201a4a4c718bfdc699bbb891b2e89be018027d2dcd10640b5ddf07802424dab78e02204ec7c7d505d2158c3b51fdd3843d16aecd2eaaa4c6c7a555ef123c5e59fd41fb", - "304402207e660489bced5ce80c33d45c86781b63898775ab4a231bb48780f97b40073a63022026f0cefd0d83022d822522ab4366a82e3b89085c328817919939f2efeabd913d"}; - - ASSERT_EQ(3, actual.signatures.size()); - for (uint8_t i = 0; i < signatures.size(); i++) { - ASSERT_STREQ(signatures[i].c_str(), actual.signatures[i].c_str()); - } - - ASSERT_TRUE(actual.verify()); - - // special case as the type 4 transaction itself has no recipient - const auto publicKey = Ark::Crypto::identities::PublicKey::fromHex(actual.senderPublicKey.c_str()); - const auto address = Ark::Crypto::identities::Address::fromPublicKey(publicKey.toBytes().data(), actual.network); - ASSERT_STREQ(address.toString().c_str(), actual.recipient.c_str()); -} diff --git a/test/transactions/deserializer_delegate_registration.cpp b/test/transactions/deserializer_delegate_registration.cpp new file mode 100644 index 00000000..680ce442 --- /dev/null +++ b/test/transactions/deserializer_delegate_registration.cpp @@ -0,0 +1,41 @@ + +#include "gtest/gtest.h" + +#include + +#if 0 +TEST(transactions, deserialize_delegate_registration) { // NOLINT + // delegate_registration/second-passphrase.json + Ark::Crypto::Transactions::Deserializer deserializer( + "ff011e02b0b87502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200f902950000000000" + "09626f6c646e696e6a613045022100f21b742fa052cd18de43328e1d068539ba7cbe9d33a9dcbd862a82871383955d022005" + "3b06d22ed3e3ad6168c6b27aa0ec68e7e40958c7709aec0e1555087ea9ad94304402207da580da4feec955edcb8e8eb36947" + "867b439de3d28d38e58c844fd8c45b564302200e6741b6ad11c2588a57b3afd180df1e9b345d48a9c2ae98be57dced869cf38c" + ); + auto actual = deserializer.deserialize(); + + ASSERT_EQ(0xFF, actual.header); + ASSERT_EQ(2, actual.type); + ASSERT_EQ(1, actual.version); + ASSERT_EQ(30, actual.network); + ASSERT_EQ(TransactionTypes::DelegateRegistration, actual.type); + ASSERT_EQ(41269424UL, actual.timestamp); + ASSERT_STREQ( + "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", + actual.senderPublicKey.c_str()); + ASSERT_EQ(2500000000ULL, actual.fee); + ASSERT_EQ(0ULL, actual.amount); + ASSERT_EQ(0, actual.expiration); + ASSERT_STREQ( + "bf7e018ff9c0066f7a9f51e95d3f78c08cad5dd8581325d630d64350181a91bb", + actual.id.c_str()); + ASSERT_STREQ("boldninja", actual.asset.delegate.username.c_str()); + ASSERT_STREQ( + "3045022100f21b742fa052cd18de43328e1d068539ba7cbe9d33a9dcbd862a82871383955d0220053b06d22ed3e3ad6168c6b27aa0ec68e7e40958c7709aec0e1555087ea9ad94", + actual.signature.c_str()); + ASSERT_STREQ( + "304402207da580da4feec955edcb8e8eb36947867b439de3d28d38e58c844fd8c45b564302200e6741b6ad11c2588a57b3afd180df1e9b345d48a9c2ae98be57dced869cf38c", + actual.secondSignature.c_str()); + ASSERT_TRUE(actual.verify())); +} +#endif diff --git a/test/transactions/deserializer_multi_signature_registration.cpp b/test/transactions/deserializer_multi_signature_registration.cpp new file mode 100644 index 00000000..c44faf1e --- /dev/null +++ b/test/transactions/deserializer_multi_signature_registration.cpp @@ -0,0 +1,69 @@ + +#include "gtest/gtest.h" + +#include + +TEST(transactions, deserialize_multi_signature_registration) { // NOLINT + // multi_signature_registration/passphrase.json + Ark::Crypto::Transactions::Deserializer deserializer( + "ff011704724c9a00036928c98ee53a1f52ed01dd87db10ffe1980eb47cd7c0a7d688321f47b5d7d76000943577000000000002031803543c" + "6cc3545be6bac09c82721973a052c690658283472e88f24d14739f75acc80276dc5b8706a85ca9fdc46e571ac84e52fbb48e13ec7a165a80" + "731b44ae89f1fc02e8d5d17eb17bbc8d7bf1001d29a2d25d1249b7bb7a5b7ad8b7422063091f4b3130440220324d89c5792e4a54ae70b4f1" + "e27e2f87a8b7169cc6f2f7b2c83dba894960f987022053b8d0ae23ff9d1769364db7b6fd03216d93753c82a711c3558045e787bc01a53044" + "02201fcd54a9ac9c0269b8cec213566ddf43207798e2cf9ca1ce3c5d315d66321c6902201aa94c4ed3e5e479a12220aa886b259e488eb89b" + "697c711f91e8c03b9620e0b1ff304502210097f17c8eecf36f86a967cc52a83fa661e4ffc70cc4ea08df58673669406d424c0220798f5710" + "897b75dda42f6548f841afbe4ed1fa262097112cf5a1b3f7dade60e4304402201a4a4c718bfdc699bbb891b2e89be018027d2dcd10640b5d" + "df07802424dab78e02204ec7c7d505d2158c3b51fdd3843d16aecd2eaaa4c6c7a555ef123c5e59fd41fb304402207e660489bced5ce80c33" + "d45c86781b63898775ab4a231bb48780f97b40073a63022026f0cefd0d83022d822522ab4366a82e3b89085c328817919939f2efeabd913d"); + auto actual = deserializer.deserialize(); + + ASSERT_EQ(0xFF, actual.header); + ASSERT_EQ(1, actual.version); + ASSERT_EQ(23, actual.network); + ASSERT_EQ(TransactionTypes::MultiSignatureRegistration, actual.type); + ASSERT_EQ(10112114UL, actual.timestamp); + ASSERT_STREQ("036928c98ee53a1f52ed01dd87db10ffe1980eb47cd7c0a7d688321f47b5d7d760", actual.senderPublicKey.c_str()); + ASSERT_TRUE(2000000000ULL == actual.fee); + ASSERT_TRUE(0ULL == actual.amount); + ASSERT_EQ(0, actual.expiration); + + std::vector keysgroup = { + "+03543c6cc3545be6bac09c82721973a052c690658283472e88f24d14739f75acc8", + "+0276dc5b8706a85ca9fdc46e571ac84e52fbb48e13ec7a165a80731b44ae89f1fc", + "+02e8d5d17eb17bbc8d7bf1001d29a2d25d1249b7bb7a5b7ad8b7422063091f4b31" + }; + + ASSERT_EQ(3, actual.asset.multiSignature.keysgroup.size()); + ASSERT_EQ(2, actual.asset.multiSignature.min); + ASSERT_EQ(24, actual.asset.multiSignature.lifetime); + + for (uint8_t i = 0; i < keysgroup.size(); i++) { + ASSERT_STREQ(keysgroup[i].c_str(), actual.asset.multiSignature.keysgroup[i].c_str()); + }; + + ASSERT_STREQ("cbd6862966bb1b03ba742397b7e5a88d6eefb393a362ead0d605723b840db2af", actual.id.c_str()); + + ASSERT_STREQ( + "30440220324d89c5792e4a54ae70b4f1e27e2f87a8b7169cc6f2f7b2c83dba894960f987022053b8d0ae23ff9d1769364db7b6fd03216d93753c82a711c3558045e787bc01a5", + actual.signature.c_str()); + ASSERT_STREQ( + "304402201fcd54a9ac9c0269b8cec213566ddf43207798e2cf9ca1ce3c5d315d66321c6902201aa94c4ed3e5e479a12220aa886b259e488eb89b697c711f91e8c03b9620e0b1", + actual.secondSignature.c_str()); + + std::vector signatures = { + "304502210097f17c8eecf36f86a967cc52a83fa661e4ffc70cc4ea08df58673669406d424c0220798f5710897b75dda42f6548f841afbe4ed1fa262097112cf5a1b3f7dade60e4", + "304402201a4a4c718bfdc699bbb891b2e89be018027d2dcd10640b5ddf07802424dab78e02204ec7c7d505d2158c3b51fdd3843d16aecd2eaaa4c6c7a555ef123c5e59fd41fb", + "304402207e660489bced5ce80c33d45c86781b63898775ab4a231bb48780f97b40073a63022026f0cefd0d83022d822522ab4366a82e3b89085c328817919939f2efeabd913d"}; + + ASSERT_EQ(3, actual.signatures.size()); + for (uint8_t i = 0; i < signatures.size(); i++) { + ASSERT_STREQ(signatures[i].c_str(), actual.signatures[i].c_str()); + } + + ASSERT_TRUE(actual.verify()); + + // special case as the type 4 transaction itself has no recipient + const auto publicKey = Ark::Crypto::identities::PublicKey::fromHex(actual.senderPublicKey.c_str()); + const auto address = Ark::Crypto::identities::Address::fromPublicKey(publicKey.toBytes().data(), actual.network); + ASSERT_STREQ(address.toString().c_str(), actual.recipient.c_str()); +} diff --git a/test/transactions/deserializer_second_signature_registration.cpp b/test/transactions/deserializer_second_signature_registration.cpp new file mode 100644 index 00000000..6d042be6 --- /dev/null +++ b/test/transactions/deserializer_second_signature_registration.cpp @@ -0,0 +1,40 @@ + +#include "gtest/gtest.h" + +#include + +TEST(transactions, deserialize_second_signature_registration) { // NOLINT + // second_signature_registration/second-passphrase.json + Ark::Crypto::Transactions::Deserializer deserializer( + "ff011e013bc27502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed1920065cd1d000000000003" + "699e966b2525f9088a6941d8d94f7869964a000efe65783d78ac82e1199fe609304402202aab49477dd3531e4473196d08fbd7" + "c00ebb79223d5eaaeaf02c52c4041a86cf02201a7d82655f9b1d22af3ea94e6f183649bb4610cdeca3b9e20d6c8773f869831c"); + auto actual = deserializer.deserialize(); + + ASSERT_EQ(0xFF, actual.header); + ASSERT_EQ(1, actual.version); + ASSERT_EQ(30, actual.network); + ASSERT_EQ(TransactionTypes::SecondSignatureRegistration, actual.type); + ASSERT_EQ(41271867UL, actual.timestamp); + ASSERT_STREQ( + "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", + actual.senderPublicKey.c_str()); + ASSERT_TRUE(500000000ULL == actual.fee); + ASSERT_TRUE(0UL == actual.amount); + ASSERT_EQ(0, actual.expiration); + ASSERT_STREQ( + "6d1615924d172d352c8f44d4ded84cbbece3c03ebb3e4fc3f3334784ae332590", + actual.id.c_str()); + ASSERT_STREQ( + "03699e966b2525f9088a6941d8d94f7869964a000efe65783d78ac82e1199fe609", + actual.asset.signature.publicKey.c_str()); + ASSERT_STREQ( + "304402202aab49477dd3531e4473196d08fbd7c00ebb79223d5eaaeaf02c52c4041a86cf02201a7d82655f9b1d22af3ea94e6f183649bb4610cdeca3b9e20d6c8773f869831c", + actual.signature.c_str()); + ASSERT_TRUE(actual.verify()); + + // special case as the type 1 transaction itself has no recipient + const auto publicKey = Ark::Crypto::identities::PublicKey::fromHex(actual.senderPublicKey.c_str()); + const auto address = Ark::Crypto::identities::Address::fromPublicKey(publicKey.toBytes().data(), actual.network); + ASSERT_STREQ(address.toString().c_str(), actual.recipient.c_str()); +} diff --git a/test/transactions/deserializer_transfer.cpp b/test/transactions/deserializer_transfer.cpp new file mode 100644 index 00000000..f222ffce --- /dev/null +++ b/test/transactions/deserializer_transfer.cpp @@ -0,0 +1,34 @@ + +#include "gtest/gtest.h" + +#include + +TEST(transactions, deserialize_transfer) { // NOLINT + // transfer/passphrase-with-vendor-field.json + Ark::Crypto::Transactions::Deserializer deserializer( + "ff011e0007627802034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19280969800000000000b48656c6c6f" + "20576f726c6400c2eb0b00000000000000001e0995750207ecaf0ccf251c1265b92ad84f553662304402205616d6e361439d67a5c2067b" + "bfc8fce61b93061a4fa113315a1c5cf965ff6f3202200a1d99caaa98aeebcec04edd5365352500addb830c79f49b9de484ec616bb1e1"); + auto actual = deserializer.deserialize(); + + ASSERT_EQ(0xFF, actual.header); + ASSERT_EQ(1, actual.version); + ASSERT_EQ(30, actual.network); + ASSERT_EQ(TransactionTypes::Transfer, actual.type); + ASSERT_EQ(41443847UL, actual.timestamp); + ASSERT_STREQ( + "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", + actual.senderPublicKey.c_str()); + ASSERT_TRUE(10000000ULL == actual.fee); + ASSERT_STREQ("48656c6c6f20576f726c64", actual.vendorFieldHex.c_str()); + ASSERT_STREQ("Hello World", actual.vendorField.c_str()); + ASSERT_TRUE(200000000ULL == actual.amount); + ASSERT_STREQ( + "ecf558fbddd62ae42edcfcba02f402d987a94b72a7636ef1121e8625487e2a1e", + actual.id.c_str()); + ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", actual.recipient.c_str()); + ASSERT_STREQ( + "304402205616d6e361439d67a5c2067bbfc8fce61b93061a4fa113315a1c5cf965ff6f3202200a1d99caaa98aeebcec04edd5365352500addb830c79f49b9de484ec616bb1e1", + actual.signature.c_str()); + ASSERT_TRUE(actual.verify()); +} diff --git a/test/transactions/deserializer_vote.cpp b/test/transactions/deserializer_vote.cpp new file mode 100644 index 00000000..41f8ceae --- /dev/null +++ b/test/transactions/deserializer_vote.cpp @@ -0,0 +1,38 @@ + +#include "gtest/gtest.h" + +#include + +TEST(transactions, deserialize_vote) { // NOLINT + // vote/second-passphrase.json + Ark::Crypto::Transactions::Deserializer deserializer( + "ff011e0376b87502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200e1f50500000000000101022cca95" + "29ec97a772156c152a00aad155ee6708243e65c9d211a589cb5d43234d304402204b8bb403e2db7f9599d46d0f5d39f8bb1d0663d875af7e" + "c1154448e98466e86302201e92fb57e13fb729b07e1027fa3d6e3f28e0d5828ed2d7c53a5e8db08cb6d068304402201329882762a42d1af9" + "079c822a9e3feefa47b7476b0afe61440637408958a64402206da179b08e31d9c784fbb23abe2c9b50353ed7881dc29787a5e8ecbee2dfda66"); + auto actual = deserializer.deserialize(); + + ASSERT_EQ(0xFF, actual.header); + ASSERT_EQ(1, actual.version); + ASSERT_EQ(30, actual.network); + ASSERT_EQ(TransactionTypes::Vote, actual.type); + ASSERT_EQ(41269366UL, actual.timestamp); + ASSERT_STREQ("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", actual.senderPublicKey.c_str()); + ASSERT_TRUE(100000000ULL == actual.fee); + ASSERT_TRUE(0ULL == actual.amount); + ASSERT_EQ(0, actual.expiration); + ASSERT_STREQ("16f28a180cd6f3ea46c10f358a457989e956e9d355258230d0c7b07acec10b73", actual.id.c_str()); + ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", actual.recipient.c_str()); + + std::vector votes = {std::string("+022cca9529ec97a772156c152a00aad155ee6708243e65c9d211a589cb5d43234d")}; + ASSERT_EQ(1, actual.asset.votes.size()); + ASSERT_STREQ(votes[0].c_str(), actual.asset.votes[0].c_str()); + + ASSERT_STREQ( + "304402204b8bb403e2db7f9599d46d0f5d39f8bb1d0663d875af7ec1154448e98466e86302201e92fb57e13fb729b07e1027fa3d6e3f28e0d5828ed2d7c53a5e8db08cb6d068", + actual.signature.c_str()); + ASSERT_STREQ( + "304402201329882762a42d1af9079c822a9e3feefa47b7476b0afe61440637408958a64402206da179b08e31d9c784fbb23abe2c9b50353ed7881dc29787a5e8ecbee2dfda66", + actual.secondSignature.c_str()); + ASSERT_TRUE(actual.verify()); +} diff --git a/test/transactions/transaction.cpp b/test/transactions/transaction.cpp index 29af3876..20f752ea 100644 --- a/test/transactions/transaction.cpp +++ b/test/transactions/transaction.cpp @@ -17,336 +17,6 @@ TEST(transactions, transaction_default) { /**/ -TEST(transactions, transaction_to_array) { // NOLINT - // Type 0 // - auto transfer = Builder::buildTransfer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 1, - "", - "Secret passphrase"); - - auto tArray = transfer.toArray(); - - // Amount - ASSERT_STREQ("1", tArray["amount"].c_str()); - - // Fee - ASSERT_STREQ("10000000", tArray["fee"].c_str()); - - // Id - ASSERT_FALSE(tArray["id"].empty()); - - // Recipient - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", tArray["recipient"].c_str()); - - // SenderPublicKey - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - tArray["senderPublicKey"].c_str()); - - // Signature - ASSERT_FALSE(tArray["signature"].empty()); - - // Timestamp - ASSERT_FALSE(tArray["timestamp"].empty()); - - // Type - ASSERT_STREQ("0", tArray["type"].c_str()); - - // Version - ASSERT_STREQ("1", tArray["version"].c_str()); - - // Type 1 // - auto secondSignatureRegistration = Builder::buildSecondSignatureRegistration( - "Secret passphrase", - "Second Secret passphrase"); - - auto ssArray = secondSignatureRegistration.toArray(); - - // Amount - ASSERT_STREQ("0", ssArray["amount"].c_str()); - - // Asset - ASSERT_STREQ("02e1684d8990c0a5625aec85977fcf22204884bc08d45dbc71b2859e5fa4f45104", - ssArray["publicKey"].c_str()); - - // Fee - ASSERT_STREQ("500000000", ssArray["fee"].c_str()); - - // Id - ASSERT_FALSE(ssArray["id"].empty()); - - // Recipient - ASSERT_TRUE(ssArray["recipient"].empty()); - - // SecondSignature - ASSERT_FALSE(ssArray["secondSignature"].empty()); - - // SenderPublicKey - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - ssArray["senderPublicKey"].c_str()); - - // Signature - ASSERT_FALSE(ssArray["signature"].empty()); - - // Timestamp - ASSERT_FALSE(ssArray["timestamp"].empty()); - - // Type - ASSERT_STREQ("1", ssArray["type"].c_str()); - - // Version - ASSERT_STREQ("1", ssArray["version"].c_str()); - - // Type 2 // - auto delegateRegistration = Builder::buildDelegateRegistration( - "testName", - "Secret passphrase"); - - auto dArray = delegateRegistration.toArray(); - - // Amount - ASSERT_STREQ("0", dArray["amount"].c_str()); - - // Asset - ASSERT_STREQ("testName", dArray["username"].c_str()); - - // Fee - ASSERT_STREQ("2500000000", dArray["fee"].c_str()); - - // Id - ASSERT_FALSE(dArray["id"].empty()); - - // Recipient - ASSERT_TRUE(dArray["recipient"].empty()); - - // SecondSignature - ASSERT_TRUE(dArray["secondSignature"].empty()); - - // SenderPublicKey - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - dArray["senderPublicKey"].c_str()); - - // Signature - ASSERT_FALSE(dArray["signature"].empty()); - - // ASSERT_FALSE - ASSERT_FALSE(dArray["timestamp"].empty()); - - // Type - ASSERT_STREQ("2", dArray["type"].c_str()); - - // Version - ASSERT_STREQ("1", dArray["version"].c_str()); - - // Type 3 // - std::vector votes = { - "-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", - "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6" - }; - - auto vote = Builder::buildVote(votes, "Secret passphrase"); - - auto vArray = vote.toArray(); - - // Amount - ASSERT_STREQ("0", vArray["amount"].c_str()); - - // Asset - ASSERT_STREQ("-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6," - "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", - vArray["votes"].c_str()); - - // Fee - ASSERT_STREQ("100000000", vArray["fee"].c_str()); - - // Id - ASSERT_FALSE(vArray["id"].empty()); - - // Recipient - ASSERT_STREQ("DPgZq5MK6rm5yVks9b7TrA22F8FwRvkCtF", - vArray["recipient"].c_str()); - - // SecondSignature - ASSERT_TRUE(vArray["secondSignature"].empty()); - - // SenderPublicKey - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - vArray["senderPublicKey"].c_str()); - - // Signature - ASSERT_FALSE(vArray["signature"].empty()); - - // Timestamp - ASSERT_FALSE(vArray["timestamp"].empty()); - - // Type - ASSERT_STREQ("3", vArray["type"].c_str()); - - // Version - ASSERT_STREQ("1", vArray["version"].c_str()); -} - -/**/ - -TEST(transactions, transaction_to_json) { // NOLINT - // Type 0 // - auto transfer = Builder::buildTransfer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 1, - "", - "Secret passphrase"); - - auto tJson = transfer.toJson(); - - const size_t tCapacity = JSON_OBJECT_SIZE(8) + 450; - DynamicJsonDocument tDoc(tCapacity); - - DeserializationError tError = deserializeJson(tDoc, tJson); - ASSERT_FALSE(tError); - - ASSERT_STREQ("1", tDoc["amount"].as()); - - ASSERT_STREQ("10000000", tDoc["fee"].as()); - - ASSERT_STRNE("", tDoc["id"].as()); - - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - tDoc["recipient"].as()); - - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - tDoc["senderPublicKey"].as()); - - ASSERT_STRNE("", tDoc["signature"].as()); - - ASSERT_GT(tDoc["timestamp"].as(), 50000000UL); - - ASSERT_LT(tDoc["timestamp"].as(), 1000000000UL); - - ASSERT_EQ(tDoc["type"].as(), 0); - - ASSERT_EQ(1, tDoc["version"].as()); - - // Type 1 // - auto secondSignatureRegistration = Builder::buildSecondSignatureRegistration( - "Secret passphrase", - "Second Secret passphrase"); - - auto ssJson = secondSignatureRegistration.toJson(); - - const size_t ssCapacity = 2 * JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(10) + 690; - DynamicJsonDocument ssDoc(ssCapacity); - - DeserializationError ssError = deserializeJson(ssDoc, ssJson); - ASSERT_FALSE(ssError); - - ASSERT_STREQ("0", ssDoc["amount"].as()); - - ASSERT_STREQ("02e1684d8990c0a5625aec85977fcf22204884bc08d45dbc71b2859e5fa4f45104", - ssDoc["asset"]["signature"]["publicKey"].as()); - - ASSERT_STREQ("500000000", ssDoc["fee"].as()); - - ASSERT_STRNE("", ssDoc["id"].as()); - - ASSERT_STREQ("", ssDoc["recipient"].as()); - - ASSERT_STRNE("", ssDoc["secondSignature"].as()); - - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - ssDoc["senderPublicKey"].as()); - - ASSERT_STRNE("", ssDoc["signature"].as()); - - ASSERT_GT(ssDoc["timestamp"].as(), 50000000UL); - ASSERT_LT(ssDoc["timestamp"].as(), 1000000000UL); - - ASSERT_EQ(ssDoc["type"].as(), 1); - - ASSERT_EQ(1, ssDoc["version"].as()); - - // Type 2 // - auto delegateRegistration = Builder::buildDelegateRegistration( - "testName", - "Secret passphrase"); - - auto dJson = delegateRegistration.toJson(); - - const size_t dCapacity = 2*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(9) + 450; - DynamicJsonDocument dDoc(dCapacity); - - DeserializationError dError = deserializeJson(dDoc, dJson); - ASSERT_FALSE(dError); - - ASSERT_STREQ("0", dDoc["amount"].as()); - - ASSERT_STREQ("testName", - dDoc["asset"]["delegate"]["username"].as()); - - ASSERT_STREQ("2500000000", dDoc["fee"].as()); - - ASSERT_STRNE("", dDoc["id"].as()); - - ASSERT_STREQ("", dDoc["recipient"].as()); - - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - dDoc["senderPublicKey"].as()); - - ASSERT_STRNE("", dDoc["signature"].as()); - - ASSERT_GT(dDoc["timestamp"].as(), 50000000UL); - ASSERT_LT(dDoc["timestamp"].as(), 1000000000UL); - - ASSERT_EQ(dDoc["type"].as(), 2); - - ASSERT_EQ(1, dDoc["version"].as()); - - // Type 3 // - std::vector votes = { - "-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6," - "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6" - }; - - auto vote = Builder::buildVote(votes, "Secret passphrase"); - - auto vJson = vote.toJson(); - - const size_t vCapacity = JSON_ARRAY_SIZE(1) - + JSON_OBJECT_SIZE(1) - + JSON_OBJECT_SIZE(9) - + 700; - DynamicJsonDocument vDoc(vCapacity); - - DeserializationError vError = deserializeJson(vDoc, vJson); - ASSERT_FALSE(vError); - - ASSERT_STREQ("0", vDoc["amount"].as()); - - ASSERT_STREQ("-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", - vDoc["asset"]["votes"][0].as()); - - ASSERT_STREQ("+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", - vDoc["asset"]["votes"][1].as()); - - ASSERT_STREQ("100000000", vDoc["fee"].as()); - - ASSERT_STRNE("", vDoc["id"].as()); - - ASSERT_STREQ("DPgZq5MK6rm5yVks9b7TrA22F8FwRvkCtF", - vDoc["recipient"].as()); - - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - vDoc["senderPublicKey"].as()); - - ASSERT_STRNE("", vDoc["signature"].as()); - - ASSERT_GT(vDoc["timestamp"].as(), 50000000UL); - ASSERT_LT(vDoc["timestamp"].as(), 1000000000UL); - - ASSERT_EQ(vDoc["type"].as(), 3); - - ASSERT_EQ(1, vDoc["version"].as()); -}; - -/**/ - TEST(transactions, transaction_empty) { Ark::Crypto::Transactions::Transaction transaction; bool isValid = transaction.verify(); diff --git a/test/transactions/transaction_to_array.cpp b/test/transactions/transaction_to_array.cpp new file mode 100644 index 00000000..c8efe575 --- /dev/null +++ b/test/transactions/transaction_to_array.cpp @@ -0,0 +1,180 @@ + +#include "gtest/gtest.h" + +#include +#include + + +#include "transactions/builder.h" +#include "transactions/transaction.h" +#include "utils/json.h" + +using namespace Ark::Crypto::Transactions; + +TEST(transactions, transaction_to_array) { // NOLINT + // Type 0 // + auto transfer = Builder::buildTransfer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", + 1, + "", + "Secret passphrase"); + + auto tArray = transfer.toArray(); + + // Amount + ASSERT_STREQ("1", tArray["amount"].c_str()); + + // Fee + ASSERT_STREQ("10000000", tArray["fee"].c_str()); + + // Id + ASSERT_FALSE(tArray["id"].empty()); + + // Recipient + ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", tArray["recipient"].c_str()); + + // SenderPublicKey + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + tArray["senderPublicKey"].c_str()); + + // Signature + ASSERT_FALSE(tArray["signature"].empty()); + + // Timestamp + ASSERT_FALSE(tArray["timestamp"].empty()); + + // Type + ASSERT_STREQ("0", tArray["type"].c_str()); + + // Version + ASSERT_STREQ("1", tArray["version"].c_str()); + + // Type 1 // + auto secondSignatureRegistration = Builder::buildSecondSignatureRegistration( + "Secret passphrase", + "Second Secret passphrase"); + + auto ssArray = secondSignatureRegistration.toArray(); + + // Amount + ASSERT_STREQ("0", ssArray["amount"].c_str()); + + // Asset + ASSERT_STREQ("02e1684d8990c0a5625aec85977fcf22204884bc08d45dbc71b2859e5fa4f45104", + ssArray["publicKey"].c_str()); + + // Fee + ASSERT_STREQ("500000000", ssArray["fee"].c_str()); + + // Id + ASSERT_FALSE(ssArray["id"].empty()); + + // Recipient + ASSERT_TRUE(ssArray["recipient"].empty()); + + // SecondSignature + ASSERT_FALSE(ssArray["secondSignature"].empty()); + + // SenderPublicKey + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + ssArray["senderPublicKey"].c_str()); + + // Signature + ASSERT_FALSE(ssArray["signature"].empty()); + + // Timestamp + ASSERT_FALSE(ssArray["timestamp"].empty()); + + // Type + ASSERT_STREQ("1", ssArray["type"].c_str()); + + // Version + ASSERT_STREQ("1", ssArray["version"].c_str()); + + // Type 2 // + auto delegateRegistration = Builder::buildDelegateRegistration( + "testName", + "Secret passphrase"); + + auto dArray = delegateRegistration.toArray(); + + // Amount + ASSERT_STREQ("0", dArray["amount"].c_str()); + + // Asset + ASSERT_STREQ("testName", dArray["username"].c_str()); + + // Fee + ASSERT_STREQ("2500000000", dArray["fee"].c_str()); + + // Id + ASSERT_FALSE(dArray["id"].empty()); + + // Recipient + ASSERT_TRUE(dArray["recipient"].empty()); + + // SecondSignature + ASSERT_TRUE(dArray["secondSignature"].empty()); + + // SenderPublicKey + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + dArray["senderPublicKey"].c_str()); + + // Signature + ASSERT_FALSE(dArray["signature"].empty()); + + // ASSERT_FALSE + ASSERT_FALSE(dArray["timestamp"].empty()); + + // Type + ASSERT_STREQ("2", dArray["type"].c_str()); + + // Version + ASSERT_STREQ("1", dArray["version"].c_str()); + + // Type 3 // + std::vector votes = { + "-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", + "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6" + }; + + auto vote = Builder::buildVote(votes, "Secret passphrase"); + + auto vArray = vote.toArray(); + + // Amount + ASSERT_STREQ("0", vArray["amount"].c_str()); + + // Asset + ASSERT_STREQ("-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6," + "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", + vArray["votes"].c_str()); + + // Fee + ASSERT_STREQ("100000000", vArray["fee"].c_str()); + + // Id + ASSERT_FALSE(vArray["id"].empty()); + + // Recipient + ASSERT_STREQ("DPgZq5MK6rm5yVks9b7TrA22F8FwRvkCtF", + vArray["recipient"].c_str()); + + // SecondSignature + ASSERT_TRUE(vArray["secondSignature"].empty()); + + // SenderPublicKey + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + vArray["senderPublicKey"].c_str()); + + // Signature + ASSERT_FALSE(vArray["signature"].empty()); + + // Timestamp + ASSERT_FALSE(vArray["timestamp"].empty()); + + // Type + ASSERT_STREQ("3", vArray["type"].c_str()); + + // Version + ASSERT_STREQ("1", vArray["version"].c_str()); +} diff --git a/test/transactions/transaction_to_json.cpp b/test/transactions/transaction_to_json.cpp new file mode 100644 index 00000000..68800aaa --- /dev/null +++ b/test/transactions/transaction_to_json.cpp @@ -0,0 +1,169 @@ + +#include "gtest/gtest.h" + +#include +#include + +#include + +#include "utils/json.h" + +using namespace Ark::Crypto::Transactions; + +TEST(transactions, transaction_to_json) { // NOLINT + // Type 0 // + auto transfer = Builder::buildTransfer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", + 1, + "", + "Secret passphrase"); + + auto tJson = transfer.toJson(); + + const size_t tCapacity = JSON_OBJECT_SIZE(8) + 450; + DynamicJsonDocument tDoc(tCapacity); + + DeserializationError tError = deserializeJson(tDoc, tJson); + ASSERT_FALSE(tError); + + ASSERT_STREQ("1", tDoc["amount"].as()); + + ASSERT_STREQ("10000000", tDoc["fee"].as()); + + ASSERT_STRNE("", tDoc["id"].as()); + + ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", + tDoc["recipient"].as()); + + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + tDoc["senderPublicKey"].as()); + + ASSERT_STRNE("", tDoc["signature"].as()); + + ASSERT_GT(tDoc["timestamp"].as(), 50000000UL); + + ASSERT_LT(tDoc["timestamp"].as(), 1000000000UL); + + ASSERT_EQ(tDoc["type"].as(), 0); + + ASSERT_EQ(1, tDoc["version"].as()); + + // Type 1 // + auto secondSignatureRegistration = Builder::buildSecondSignatureRegistration( + "Secret passphrase", + "Second Secret passphrase"); + + auto ssJson = secondSignatureRegistration.toJson(); + + const size_t ssCapacity = 2 * JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(10) + 690; + DynamicJsonDocument ssDoc(ssCapacity); + + DeserializationError ssError = deserializeJson(ssDoc, ssJson); + ASSERT_FALSE(ssError); + + ASSERT_STREQ("0", ssDoc["amount"].as()); + + ASSERT_STREQ("02e1684d8990c0a5625aec85977fcf22204884bc08d45dbc71b2859e5fa4f45104", + ssDoc["asset"]["signature"]["publicKey"].as()); + + ASSERT_STREQ("500000000", ssDoc["fee"].as()); + + ASSERT_STRNE("", ssDoc["id"].as()); + + ASSERT_STREQ("", ssDoc["recipient"].as()); + + ASSERT_STRNE("", ssDoc["secondSignature"].as()); + + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + ssDoc["senderPublicKey"].as()); + + ASSERT_STRNE("", ssDoc["signature"].as()); + + ASSERT_GT(ssDoc["timestamp"].as(), 50000000UL); + ASSERT_LT(ssDoc["timestamp"].as(), 1000000000UL); + + ASSERT_EQ(ssDoc["type"].as(), 1); + + ASSERT_EQ(1, ssDoc["version"].as()); + + // Type 2 // + auto delegateRegistration = Builder::buildDelegateRegistration( + "testName", + "Secret passphrase"); + + auto dJson = delegateRegistration.toJson(); + + const size_t dCapacity = 2*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(9) + 450; + DynamicJsonDocument dDoc(dCapacity); + + DeserializationError dError = deserializeJson(dDoc, dJson); + ASSERT_FALSE(dError); + + ASSERT_STREQ("0", dDoc["amount"].as()); + + ASSERT_STREQ("testName", + dDoc["asset"]["delegate"]["username"].as()); + + ASSERT_STREQ("2500000000", dDoc["fee"].as()); + + ASSERT_STRNE("", dDoc["id"].as()); + + ASSERT_STREQ("", dDoc["recipient"].as()); + + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + dDoc["senderPublicKey"].as()); + + ASSERT_STRNE("", dDoc["signature"].as()); + + ASSERT_GT(dDoc["timestamp"].as(), 50000000UL); + ASSERT_LT(dDoc["timestamp"].as(), 1000000000UL); + + ASSERT_EQ(dDoc["type"].as(), 2); + + ASSERT_EQ(1, dDoc["version"].as()); + + // Type 3 // + std::vector votes = { + "-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6," + "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6" + }; + + auto vote = Builder::buildVote(votes, "Secret passphrase"); + + auto vJson = vote.toJson(); + + const size_t vCapacity = JSON_ARRAY_SIZE(1) + + JSON_OBJECT_SIZE(1) + + JSON_OBJECT_SIZE(9) + + 700; + DynamicJsonDocument vDoc(vCapacity); + + DeserializationError vError = deserializeJson(vDoc, vJson); + ASSERT_FALSE(vError); + + ASSERT_STREQ("0", vDoc["amount"].as()); + + ASSERT_STREQ("-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", + vDoc["asset"]["votes"][0].as()); + + ASSERT_STREQ("+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", + vDoc["asset"]["votes"][1].as()); + + ASSERT_STREQ("100000000", vDoc["fee"].as()); + + ASSERT_STRNE("", vDoc["id"].as()); + + ASSERT_STREQ("DPgZq5MK6rm5yVks9b7TrA22F8FwRvkCtF", + vDoc["recipient"].as()); + + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + vDoc["senderPublicKey"].as()); + + ASSERT_STRNE("", vDoc["signature"].as()); + + ASSERT_GT(vDoc["timestamp"].as(), 50000000UL); + ASSERT_LT(vDoc["timestamp"].as(), 1000000000UL); + + ASSERT_EQ(vDoc["type"].as(), 3); + + ASSERT_EQ(1, vDoc["version"].as()); +} From d9a6c85aedce126c652bb30fefb986bb7056303a Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Mon, 28 Oct 2019 01:15:09 -0500 Subject: [PATCH 07/21] test: fix ESP8266 Unit Tests (#169) --- .gitignore | 1 + test/iot/test_main.cpp | 6 +- test/platformio.ini | 512 +++++++++++++++++++++++++++++++++++------ 3 files changed, 444 insertions(+), 75 deletions(-) diff --git a/.gitignore b/.gitignore index cc93e2ea..aa9f2108 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ /lib/ /bin/ build +extern /src/ark_cpp_crypto.vcxproj.filters /src/ark_cpp_crypto.vcxproj /src/ark_cpp_crypto.sln diff --git a/test/iot/test_main.cpp b/test/iot/test_main.cpp index 0db83130..9851d3b7 100644 --- a/test/iot/test_main.cpp +++ b/test/iot/test_main.cpp @@ -20,12 +20,12 @@ void setup() { setDummyTime(); testing::InitGoogleTest(); - - auto __attribute__((unused)) run = RUN_ALL_TESTS(); } void loop() { - // do nothing + // loop the tests. + auto __attribute__((unused)) run = RUN_ALL_TESTS(); + delay(1000); } #endif diff --git a/test/platformio.ini b/test/platformio.ini index eccb7229..449f03fa 100644 --- a/test/platformio.ini +++ b/test/platformio.ini @@ -15,94 +15,452 @@ build_dir = ../build/.pioenvs libdeps_dir = ../extern/.piolibdeps [base] -lib_deps = ArduinoJson@6.12.0, BIP66@0.2.1, micro-ecc@1.0.0, googletest@1.8.1 +# Replace googletest library with proper release once ESP8266 support PR is merged to googletest +lib_deps = ArduinoJson@6.12.0, BIP66@0.2.1, micro-ecc@1.0.0, https://github.com/ciband/googletest.git#pre_release build_flags = -I../test -I../test/iot/ -I../src -I../src/lib -I../src/include/cpp-crypto -DUNIT_TEST -src_filter = +<../src> +<../test> +<../test/iot> +src_filter = +<../src> +<../test/iot> upload_speed = 921600 +[esp8266] +build_flags = -D_GLIBCXX_USE_C99 + [common_tests] -src_filter = + +src_filter = +<../test/common> [crypto_tests] -src_filter = + +src_filter = +<../test/crypto> + +[crypto_curve_ecdsa_sign_tests] +src_filter = +<../test/crypto/curve_ecdsa_sign.cpp> + +[crypto_curve_ecdsa_sign_null_pk_tests] +src_filter = +<../test/crypto/curve_ecdsa_sign_null_pk.cpp> + +[crypto_curve_ecdsa_sign_null_hash32_sha256_tests] +src_filter = +<../test/crypto/curve_ecdsa_sign_null_hash32_sha256.cpp> + +[crypto_curve_ecdsa_sign_null_hash32_privatekey_tests] +src_filter = +<../test/crypto/curve_ecdsa_sign_null_hash32_privatekey.cpp> + +[crypto_curve_publickey_tests] +src_filter = +<../test/crypto/curve_publickey.cpp> + +[crypto_curve_verify_invalid_tests] +src_filter = +<../test/crypto/curve_verify_invalid.cpp> + +[crypto_curve_verify_valid_tests] +src_filter = +<../test/crypto/curve_verify_valid.cpp> + +[crypto_hash_tests] +src_filter = +<../test/crypto/hash.cpp> + +[crypto_message_tests] +src_filter = +<../test/crypto/message.cpp> + +[crypto_message_sign_tests] +src_filter = +<../test/crypto/message_sign.cpp> + +[crypto_message_verify_tests] +src_filter = +<../test/crypto/message_verify.cpp> + +[crypto_slot_tests] +src_filter = +<../test/crypto/slot.cpp> [defaults_tests] -src_filter = + +src_filter = +<../test/defaults> [identities_tests] -src_filter = + +src_filter = +<../test/identities> + +[identities_address_tests] +src_filter = +<../test/identities/address.cpp> + +[identities_keys_tests] +src_filter = +<../test/identities/keys.cpp> + +[identities_keys_publickey_tests] +src_filter = +<../test/identities/keys_publickey.cpp> + +[identities_privatekey_tests] +src_filter = +<../test/identities/privatekey.cpp> + +[identities_publickey_tests] +src_filter = +<../test/identities/publickey.cpp> [managers_tests] -src_filter = + +src_filter = +<../test/managers> [networks_tests] -src_filter = + +src_filter = +<../test/networks> [transactions_tests] -src_filter = + - -; [env:esp8266_common_tests] -; platform = espressif8266 -; board = huzzah -; framework = arduino -; lib_deps = ${base.lib_deps} -; build_flags = ${base.build_flags} -; src_filter = ${base.src_filter} ${common_tests.src_filter} -; upload_speed = ${base.upload_speed} - -; [env:esp8266_crypto_tests] -; platform = espressif8266 -; board = huzzah -; framework = arduino -; lib_deps = ${base.lib_deps} -; build_flags = ${base.build_flags} -; src_filter = ${base.src_filter} ${crypto_tests.src_filter} -; upload_speed = ${base.upload_speed} - -; [env:esp8266_defaults_tests] -; platform = espressif8266 -; board = huzzah -; framework = arduino -; lib_deps = ${base.lib_deps} -; build_flags = ${base.build_flags} -; src_filter = ${base.src_filter} ${defaults_tests.src_filter} -; upload_speed = ${base.upload_speed} - -; [env:esp8266_identities_tests] -; platform = espressif8266 -; board = huzzah -; framework = arduino -; lib_deps = ${base.lib_deps} -; build_flags = ${base.build_flags} -; src_filter = ${base.src_filter} ${identities_tests.src_filter} -; upload_speed = ${base.upload_speed} - -; [env:esp8266_managers_tests] -; platform = espressif8266 -; board = huzzah -; framework = arduino -; lib_deps = ${base.lib_deps} -; build_flags = ${base.build_flags} -; src_filter = ${base.src_filter} ${managers_tests.src_filter} -; upload_speed = ${base.upload_speed} - -; [env:esp8266_networks_tests] -; platform = espressif8266 -; board = huzzah -; framework = arduino -; lib_deps = ${base.lib_deps} -; build_flags = ${base.build_flags} -; src_filter = ${base.src_filter} ${networks_tests.src_filter} -; upload_speed = ${base.upload_speed} - -; [env:esp8266_transactions_tests] -; platform = espressif8266 -; board = huzzah -; framework = arduino -; lib_deps = ${base.lib_deps} -; build_flags = ${base.build_flags} -; src_filter = ${base.src_filter} ${transactions_tests.src_filter} -; upload_speed = ${base.upload_speed} +src_filter = +<../test/transactions> + +[transactions_builder_tests] +src_filter = +<../test/transactions/builder.cpp> + +[transactions_builder_transfer_custom_network_tests] +src_filter = +<../test/transactions/builder_transfer_custom_network.cpp> + +[transactions_deserializer_delegate_registration_tests] +src_filter = +<../test/transactions/deserializer_delegate_registration.cpp> + +[transactions_deserializer_multi_signature_registration_tests] +src_filter = +<../test/transactions/deserializer_multi_signature_registration.cpp> + +[transactions_deserializer_second_signature_registration_tests] +src_filter = +<../test/transactions/deserializer_second_signature_registration.cpp> + +[transactions_deserializer_transfer_tests] +src_filter = +<../test/transactions/deserializer_transfer.cpp> + +[transactions_deserializer_vote_tests] +src_filter = +<../test/transactions/deserializer_vote.cpp> + +[transactions_serializer_tests] +src_filter = +<../test/transactions/serializer.cpp> + +[transactions_transaction_tests] +src_filter = +<../test/transactions/transaction.cpp> + +[transactions_transaction_to_array_tests] +src_filter = +<../test/transactions/transaction_to_array.cpp> + +[transactions_transaction_to_json_tests] +src_filter = +<../test/transactions/transaction_to_json.cpp> + +[utils_tests] +src_filter = +<../test/utils> + +[env:esp8266_common_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${common_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_crypto_curve_ecdsa_sign_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_crypto_curve_ecdsa_sign_null_pk_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_pk_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_crypto_curve_ecdsa_sign_null_hash32_sha256_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_hash32_sha256_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_crypto_curve_ecdsa_sign_null_hash32_privatekey_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_hash32_privatekey_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_crypto_curve_publickey_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${crypto_curve_publickey_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_crypto_curve_verify_invalid_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${crypto_curve_verify_invalid_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_crypto_curve_verify_valid_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${crypto_curve_verify_valid_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_crypto_hash_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${crypto_hash_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_crypto_message_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${crypto_message_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_crypto_message_sign_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${crypto_message_sign_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_crypto_message_verify_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${crypto_message_verify_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_crypto_slot_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${crypto_slot_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_defaults_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${defaults_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_identities_address_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${identities_address_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_identities_keys_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${identities_keys_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_identities_keys_publickey_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${identities_keys_publickey_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_identities_privatekey_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${identities_privatekey_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_identities_publickey_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${identities_publickey_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_managers_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${managers_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_networks_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${networks_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_transactions_builder_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_builder_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_transactions_builder_transfer_custom_network_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_builder_transfer_custom_network_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_transactions_deserializer_delegate_registration_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_deserializer_delegate_registration_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_transactions_deserializer_multi_signature_registration_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_deserializer_multi_signature_registration_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_transactions_deserializer_second_signature_registration_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_deserializer_second_signature_registration_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_transactions_deserializer_transfer_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_deserializer_transfer_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_transactions_deserializer_vote_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_deserializer_vote_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_transactions_serializer_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_serializer_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_transactions_transaction_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_transaction_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_transactions_transaction_to_array_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_transaction_to_array_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_transactions_transaction_to_json_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_transaction_to_json_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 + +[env:esp8266_utils_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${utils_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 [env:esp32_common_tests] platform = espressif32 @@ -166,3 +524,13 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} src_filter = ${base.src_filter} ${transactions_tests.src_filter} upload_speed = ${base.upload_speed} + +[env:esp32_utils_tests] +platform = espressif32 +board = esp32dev +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} +src_filter = ${base.src_filter} ${utils_tests.src_filter} +upload_speed = ${base.upload_speed} +upload_port = /dev/ttyUSB4 From fc6b4059c5ffaa14d5d1f288f5afb203a98cf519 Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Mon, 28 Oct 2019 16:48:39 -0500 Subject: [PATCH 08/21] chore: Remove debug port config (#175) --- test/platformio.ini | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/test/platformio.ini b/test/platformio.ini index 449f03fa..fb556a81 100644 --- a/test/platformio.ini +++ b/test/platformio.ini @@ -140,7 +140,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${common_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_crypto_curve_ecdsa_sign_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -150,7 +149,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_crypto_curve_ecdsa_sign_null_pk_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -160,7 +158,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_pk_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_crypto_curve_ecdsa_sign_null_hash32_sha256_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -170,7 +167,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_hash32_sha256_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_crypto_curve_ecdsa_sign_null_hash32_privatekey_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -180,7 +176,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_hash32_privatekey_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_crypto_curve_publickey_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -190,7 +185,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_publickey_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_crypto_curve_verify_invalid_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -200,7 +194,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_verify_invalid_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_crypto_curve_verify_valid_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -210,7 +203,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_verify_valid_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_crypto_hash_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -220,7 +212,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_hash_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_crypto_message_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -230,7 +221,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_message_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_crypto_message_sign_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -240,7 +230,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_message_sign_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_crypto_message_verify_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -250,7 +239,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_message_verify_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_crypto_slot_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -260,7 +248,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_slot_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_defaults_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -270,7 +257,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${defaults_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_identities_address_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -280,7 +266,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${identities_address_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_identities_keys_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -290,7 +275,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${identities_keys_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_identities_keys_publickey_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -300,7 +284,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${identities_keys_publickey_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_identities_privatekey_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -310,7 +293,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${identities_privatekey_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_identities_publickey_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -320,7 +302,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${identities_publickey_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_managers_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -330,7 +311,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${managers_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_networks_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -340,7 +320,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${networks_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_transactions_builder_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -350,7 +329,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_builder_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_transactions_builder_transfer_custom_network_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -360,7 +338,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_builder_transfer_custom_network_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_transactions_deserializer_delegate_registration_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -370,7 +347,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_deserializer_delegate_registration_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_transactions_deserializer_multi_signature_registration_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -380,7 +356,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_deserializer_multi_signature_registration_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_transactions_deserializer_second_signature_registration_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -390,7 +365,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_deserializer_second_signature_registration_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_transactions_deserializer_transfer_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -400,7 +374,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_deserializer_transfer_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_transactions_deserializer_vote_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -410,7 +383,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_deserializer_vote_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_transactions_serializer_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -420,7 +392,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_serializer_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_transactions_transaction_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -430,7 +401,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_transactions_transaction_to_array_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -440,7 +410,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_to_array_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_transactions_transaction_to_json_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -450,7 +419,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_to_json_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp8266_utils_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -460,7 +428,6 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${utils_tests.src_filter} upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 [env:esp32_common_tests] platform = espressif32 From a7fef39f38636ed7976a05fb80b8b33df5071e1a Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Wed, 30 Oct 2019 05:32:54 -0500 Subject: [PATCH 09/21] fix: fix transaction::to_array tests on esp8266 (#178) Broke up the transaction::to_array tests to reduce stack usage and allow for support on esp8266. --- test/CMakeLists.txt | 5 +- test/platformio.ini | 44 ++++- test/transactions/transaction_to_array.cpp | 180 ------------------ .../transaction_to_array_type_0.cpp | 54 ++++++ .../transaction_to_array_type_1.cpp | 59 ++++++ .../transaction_to_array_type_2.cpp | 57 ++++++ .../transaction_to_array_type_3.cpp | 64 +++++++ 7 files changed, 278 insertions(+), 185 deletions(-) delete mode 100644 test/transactions/transaction_to_array.cpp create mode 100644 test/transactions/transaction_to_array_type_0.cpp create mode 100644 test/transactions/transaction_to_array_type_1.cpp create mode 100644 test/transactions/transaction_to_array_type_2.cpp create mode 100644 test/transactions/transaction_to_array_type_3.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a12ab4a7..494b941a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -100,7 +100,10 @@ set(TRANSACTIONS_TEST_SOURCE ${PROJECT_SOURCE_DIR}/transactions/deserializer_transfer.cpp ${PROJECT_SOURCE_DIR}/transactions/deserializer_vote.cpp ${PROJECT_SOURCE_DIR}/transactions/serializer.cpp - ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array.cpp + ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array_type_0.cpp + ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array_type_1.cpp + ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array_type_2.cpp + ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array_type_3.cpp ${PROJECT_SOURCE_DIR}/transactions/transaction_to_json.cpp ${PROJECT_SOURCE_DIR}/transactions/transaction.cpp) diff --git a/test/platformio.ini b/test/platformio.ini index fb556a81..d0779da2 100644 --- a/test/platformio.ini +++ b/test/platformio.ini @@ -123,8 +123,17 @@ src_filter = +<../test/transactions/serializer.cpp> [transactions_transaction_tests] src_filter = +<../test/transactions/transaction.cpp> -[transactions_transaction_to_array_tests] -src_filter = +<../test/transactions/transaction_to_array.cpp> +[transactions_transaction_to_array_type_0_tests] +src_filter = +<../test/transactions/transaction_to_array_type_0.cpp> + +[transactions_transaction_to_array_type_1_tests] +src_filter = +<../test/transactions/transaction_to_array_type_1.cpp> + +[transactions_transaction_to_array_type_2_tests] +src_filter = +<../test/transactions/transaction_to_array_type_2.cpp> + +[transactions_transaction_to_array_type_3_tests] +src_filter = +<../test/transactions/transaction_to_array_type_3.cpp> [transactions_transaction_to_json_tests] src_filter = +<../test/transactions/transaction_to_json.cpp> @@ -402,13 +411,40 @@ build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_tests.src_filter} upload_speed = ${base.upload_speed} -[env:esp8266_transactions_transaction_to_array_tests] +[env:esp8266_transactions_transaction_to_array_type_0_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_0_tests.src_filter} +upload_speed = ${base.upload_speed} + +[env:esp8266_transactions_transaction_to_array_type_1_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_1_tests.src_filter} +upload_speed = ${base.upload_speed} + +[env:esp8266_transactions_transaction_to_array_type_2_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_2_tests.src_filter} +upload_speed = ${base.upload_speed} + +[env:esp8266_transactions_transaction_to_array_type_3_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage board = huzzah framework = arduino lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} -src_filter = ${base.src_filter} ${transactions_transaction_to_array_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_3_tests.src_filter} upload_speed = ${base.upload_speed} [env:esp8266_transactions_transaction_to_json_tests] diff --git a/test/transactions/transaction_to_array.cpp b/test/transactions/transaction_to_array.cpp deleted file mode 100644 index c8efe575..00000000 --- a/test/transactions/transaction_to_array.cpp +++ /dev/null @@ -1,180 +0,0 @@ - -#include "gtest/gtest.h" - -#include -#include - - -#include "transactions/builder.h" -#include "transactions/transaction.h" -#include "utils/json.h" - -using namespace Ark::Crypto::Transactions; - -TEST(transactions, transaction_to_array) { // NOLINT - // Type 0 // - auto transfer = Builder::buildTransfer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 1, - "", - "Secret passphrase"); - - auto tArray = transfer.toArray(); - - // Amount - ASSERT_STREQ("1", tArray["amount"].c_str()); - - // Fee - ASSERT_STREQ("10000000", tArray["fee"].c_str()); - - // Id - ASSERT_FALSE(tArray["id"].empty()); - - // Recipient - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", tArray["recipient"].c_str()); - - // SenderPublicKey - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - tArray["senderPublicKey"].c_str()); - - // Signature - ASSERT_FALSE(tArray["signature"].empty()); - - // Timestamp - ASSERT_FALSE(tArray["timestamp"].empty()); - - // Type - ASSERT_STREQ("0", tArray["type"].c_str()); - - // Version - ASSERT_STREQ("1", tArray["version"].c_str()); - - // Type 1 // - auto secondSignatureRegistration = Builder::buildSecondSignatureRegistration( - "Secret passphrase", - "Second Secret passphrase"); - - auto ssArray = secondSignatureRegistration.toArray(); - - // Amount - ASSERT_STREQ("0", ssArray["amount"].c_str()); - - // Asset - ASSERT_STREQ("02e1684d8990c0a5625aec85977fcf22204884bc08d45dbc71b2859e5fa4f45104", - ssArray["publicKey"].c_str()); - - // Fee - ASSERT_STREQ("500000000", ssArray["fee"].c_str()); - - // Id - ASSERT_FALSE(ssArray["id"].empty()); - - // Recipient - ASSERT_TRUE(ssArray["recipient"].empty()); - - // SecondSignature - ASSERT_FALSE(ssArray["secondSignature"].empty()); - - // SenderPublicKey - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - ssArray["senderPublicKey"].c_str()); - - // Signature - ASSERT_FALSE(ssArray["signature"].empty()); - - // Timestamp - ASSERT_FALSE(ssArray["timestamp"].empty()); - - // Type - ASSERT_STREQ("1", ssArray["type"].c_str()); - - // Version - ASSERT_STREQ("1", ssArray["version"].c_str()); - - // Type 2 // - auto delegateRegistration = Builder::buildDelegateRegistration( - "testName", - "Secret passphrase"); - - auto dArray = delegateRegistration.toArray(); - - // Amount - ASSERT_STREQ("0", dArray["amount"].c_str()); - - // Asset - ASSERT_STREQ("testName", dArray["username"].c_str()); - - // Fee - ASSERT_STREQ("2500000000", dArray["fee"].c_str()); - - // Id - ASSERT_FALSE(dArray["id"].empty()); - - // Recipient - ASSERT_TRUE(dArray["recipient"].empty()); - - // SecondSignature - ASSERT_TRUE(dArray["secondSignature"].empty()); - - // SenderPublicKey - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - dArray["senderPublicKey"].c_str()); - - // Signature - ASSERT_FALSE(dArray["signature"].empty()); - - // ASSERT_FALSE - ASSERT_FALSE(dArray["timestamp"].empty()); - - // Type - ASSERT_STREQ("2", dArray["type"].c_str()); - - // Version - ASSERT_STREQ("1", dArray["version"].c_str()); - - // Type 3 // - std::vector votes = { - "-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", - "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6" - }; - - auto vote = Builder::buildVote(votes, "Secret passphrase"); - - auto vArray = vote.toArray(); - - // Amount - ASSERT_STREQ("0", vArray["amount"].c_str()); - - // Asset - ASSERT_STREQ("-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6," - "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", - vArray["votes"].c_str()); - - // Fee - ASSERT_STREQ("100000000", vArray["fee"].c_str()); - - // Id - ASSERT_FALSE(vArray["id"].empty()); - - // Recipient - ASSERT_STREQ("DPgZq5MK6rm5yVks9b7TrA22F8FwRvkCtF", - vArray["recipient"].c_str()); - - // SecondSignature - ASSERT_TRUE(vArray["secondSignature"].empty()); - - // SenderPublicKey - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - vArray["senderPublicKey"].c_str()); - - // Signature - ASSERT_FALSE(vArray["signature"].empty()); - - // Timestamp - ASSERT_FALSE(vArray["timestamp"].empty()); - - // Type - ASSERT_STREQ("3", vArray["type"].c_str()); - - // Version - ASSERT_STREQ("1", vArray["version"].c_str()); -} diff --git a/test/transactions/transaction_to_array_type_0.cpp b/test/transactions/transaction_to_array_type_0.cpp new file mode 100644 index 00000000..ac5c700d --- /dev/null +++ b/test/transactions/transaction_to_array_type_0.cpp @@ -0,0 +1,54 @@ + +#include "gtest/gtest.h" + +#include +#include + + +#include "transactions/builder.h" +#include "transactions/transaction.h" + +using namespace Ark::Crypto::Transactions; + +TEST(transactions, transaction_to_array_type_0) { // NOLINT + std::map tArray; + // Type 0 // + + { // force dtor on transfer to clean up stack space + auto transfer = Builder::buildTransfer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", + 1, + "", + "Secret passphrase"); + + tArray = transfer.toArray(); + } + + // Amount + ASSERT_STREQ("1", tArray["amount"].c_str()); + + // Fee + ASSERT_STREQ("10000000", tArray["fee"].c_str()); + + // Id + ASSERT_FALSE(tArray["id"].empty()); + + // Recipient + ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", tArray["recipient"].c_str()); + + // SenderPublicKey + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + tArray["senderPublicKey"].c_str()); + + // Signature + ASSERT_FALSE(tArray["signature"].empty()); + + // Timestamp + ASSERT_FALSE(tArray["timestamp"].empty()); + + // Type + ASSERT_STREQ("0", tArray["type"].c_str()); + + // Version + ASSERT_STREQ("1", tArray["version"].c_str()); + +} diff --git a/test/transactions/transaction_to_array_type_1.cpp b/test/transactions/transaction_to_array_type_1.cpp new file mode 100644 index 00000000..cf1218cc --- /dev/null +++ b/test/transactions/transaction_to_array_type_1.cpp @@ -0,0 +1,59 @@ + +#include "gtest/gtest.h" + +#include +#include + + +#include "transactions/builder.h" +#include "transactions/transaction.h" + +using namespace Ark::Crypto::Transactions; + +TEST(transactions, transaction_to_array_type_1) { // NOLINT + std::map ssArray; + + { // force dtor on transfer to clean up stack space + // Type 1 // + auto secondSignatureRegistration = Builder::buildSecondSignatureRegistration( + "Secret passphrase", + "Second Secret passphrase"); + + ssArray = secondSignatureRegistration.toArray(); + } + + // Amount + ASSERT_STREQ("0", ssArray["amount"].c_str()); + + // Asset + ASSERT_STREQ("02e1684d8990c0a5625aec85977fcf22204884bc08d45dbc71b2859e5fa4f45104", + ssArray["publicKey"].c_str()); + + // Fee + ASSERT_STREQ("500000000", ssArray["fee"].c_str()); + + // Id + ASSERT_FALSE(ssArray["id"].empty()); + + // Recipient + ASSERT_TRUE(ssArray["recipient"].empty()); + + // SecondSignature + ASSERT_FALSE(ssArray["secondSignature"].empty()); + + // SenderPublicKey + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + ssArray["senderPublicKey"].c_str()); + + // Signature + ASSERT_FALSE(ssArray["signature"].empty()); + + // Timestamp + ASSERT_FALSE(ssArray["timestamp"].empty()); + + // Type + ASSERT_STREQ("1", ssArray["type"].c_str()); + + // Version + ASSERT_STREQ("1", ssArray["version"].c_str()); +} diff --git a/test/transactions/transaction_to_array_type_2.cpp b/test/transactions/transaction_to_array_type_2.cpp new file mode 100644 index 00000000..18e8e2af --- /dev/null +++ b/test/transactions/transaction_to_array_type_2.cpp @@ -0,0 +1,57 @@ + +#include "gtest/gtest.h" + +#include +#include + + +#include "transactions/builder.h" +#include "transactions/transaction.h" + +using namespace Ark::Crypto::Transactions; + +TEST(transactions, transaction_to_array_type_2) { // NOLINT + std::map dArray; + + { // force dtor on transfer to clean up stack space + // Type 2 // + auto delegateRegistration = Builder::buildDelegateRegistration( + "testName", + "Secret passphrase"); + + dArray = delegateRegistration.toArray(); + } + // Amount + ASSERT_STREQ("0", dArray["amount"].c_str()); + + // Asset + ASSERT_STREQ("testName", dArray["username"].c_str()); + + // Fee + ASSERT_STREQ("2500000000", dArray["fee"].c_str()); + + // Id + ASSERT_FALSE(dArray["id"].empty()); + + // Recipient + ASSERT_TRUE(dArray["recipient"].empty()); + + // SecondSignature + ASSERT_TRUE(dArray["secondSignature"].empty()); + + // SenderPublicKey + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + dArray["senderPublicKey"].c_str()); + + // Signature + ASSERT_FALSE(dArray["signature"].empty()); + + // ASSERT_FALSE + ASSERT_FALSE(dArray["timestamp"].empty()); + + // Type + ASSERT_STREQ("2", dArray["type"].c_str()); + + // Version + ASSERT_STREQ("1", dArray["version"].c_str()); +} diff --git a/test/transactions/transaction_to_array_type_3.cpp b/test/transactions/transaction_to_array_type_3.cpp new file mode 100644 index 00000000..42ece945 --- /dev/null +++ b/test/transactions/transaction_to_array_type_3.cpp @@ -0,0 +1,64 @@ + +#include "gtest/gtest.h" + +#include +#include + + +#include "transactions/builder.h" +#include "transactions/transaction.h" + +using namespace Ark::Crypto::Transactions; + +TEST(transactions, transaction_to_array_type_3) { // NOLINT + std::map vArray; + + { // force dtor on transfer to clean up stack space + // Type 3 // + std::vector votes = { + "-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", + "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6" + }; + + auto vote = Builder::buildVote(votes, "Secret passphrase"); + + vArray = vote.toArray(); + } + + // Amount + ASSERT_STREQ("0", vArray["amount"].c_str()); + + // Asset + ASSERT_STREQ("-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6," + "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", + vArray["votes"].c_str()); + + // Fee + ASSERT_STREQ("100000000", vArray["fee"].c_str()); + + // Id + ASSERT_FALSE(vArray["id"].empty()); + + // Recipient + ASSERT_STREQ("DPgZq5MK6rm5yVks9b7TrA22F8FwRvkCtF", + vArray["recipient"].c_str()); + + // SecondSignature + ASSERT_TRUE(vArray["secondSignature"].empty()); + + // SenderPublicKey + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + vArray["senderPublicKey"].c_str()); + + // Signature + ASSERT_FALSE(vArray["signature"].empty()); + + // Timestamp + ASSERT_FALSE(vArray["timestamp"].empty()); + + // Type + ASSERT_STREQ("3", vArray["type"].c_str()); + + // Version + ASSERT_STREQ("1", vArray["version"].c_str()); +} From 0ccfb9e876c1ef0cdab952d392bc03fae738fd9d Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Wed, 30 Oct 2019 06:08:49 -0500 Subject: [PATCH 10/21] fix: Fix transaction::to_json tests on ESP8266 (#180) * fix: fix transaction::to_json tests on esp8266 Broke up the transaction::to_json tests to reduce stack usage and allow for support on esp8266. --- test/CMakeLists.txt | 5 +- test/platformio.ini | 44 ++++- test/transactions/transaction_to_json.cpp | 169 ------------------ .../transaction_to_json_type_0.cpp | 53 ++++++ .../transaction_to_json_type_1.cpp | 55 ++++++ .../transaction_to_json_type_2.cpp | 53 ++++++ .../transaction_to_json_type_3.cpp | 59 ++++++ 7 files changed, 264 insertions(+), 174 deletions(-) delete mode 100644 test/transactions/transaction_to_json.cpp create mode 100644 test/transactions/transaction_to_json_type_0.cpp create mode 100644 test/transactions/transaction_to_json_type_1.cpp create mode 100644 test/transactions/transaction_to_json_type_2.cpp create mode 100644 test/transactions/transaction_to_json_type_3.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 494b941a..365dbc4a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -104,7 +104,10 @@ set(TRANSACTIONS_TEST_SOURCE ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array_type_1.cpp ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array_type_2.cpp ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array_type_3.cpp - ${PROJECT_SOURCE_DIR}/transactions/transaction_to_json.cpp + ${PROJECT_SOURCE_DIR}/transactions/transaction_to_json_type_0.cpp + ${PROJECT_SOURCE_DIR}/transactions/transaction_to_json_type_1.cpp + ${PROJECT_SOURCE_DIR}/transactions/transaction_to_json_type_2.cpp + ${PROJECT_SOURCE_DIR}/transactions/transaction_to_json_type_3.cpp ${PROJECT_SOURCE_DIR}/transactions/transaction.cpp) # ------------------------------------------------------------------------------ diff --git a/test/platformio.ini b/test/platformio.ini index d0779da2..96b67c18 100644 --- a/test/platformio.ini +++ b/test/platformio.ini @@ -135,8 +135,17 @@ src_filter = +<../test/transactions/transaction_to_array_type_2.cpp> [transactions_transaction_to_array_type_3_tests] src_filter = +<../test/transactions/transaction_to_array_type_3.cpp> -[transactions_transaction_to_json_tests] -src_filter = +<../test/transactions/transaction_to_json.cpp> +[transactions_transaction_to_json_type_0_tests] +src_filter = +<../test/transactions/transaction_to_json_type_0.cpp> + +[transactions_transaction_to_json_type_1_tests] +src_filter = +<../test/transactions/transaction_to_json_type_1.cpp> + +[transactions_transaction_to_json_type_2_tests] +src_filter = +<../test/transactions/transaction_to_json_type_2.cpp> + +[transactions_transaction_to_json_type_3_tests] +src_filter = +<../test/transactions/transaction_to_json_type_3.cpp> [utils_tests] src_filter = +<../test/utils> @@ -447,13 +456,40 @@ build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_3_tests.src_filter} upload_speed = ${base.upload_speed} -[env:esp8266_transactions_transaction_to_json_tests] +[env:esp8266_transactions_transaction_to_json_type_0_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_0_tests.src_filter} +upload_speed = ${base.upload_speed} + +[env:esp8266_transactions_transaction_to_json_type_1_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_1_tests.src_filter} +upload_speed = ${base.upload_speed} + +[env:esp8266_transactions_transaction_to_json_type_2_tests] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah +framework = arduino +lib_deps = ${base.lib_deps} +build_flags = ${base.build_flags} ${esp8266.build_flags} +src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_2_tests.src_filter} +upload_speed = ${base.upload_speed} + +[env:esp8266_transactions_transaction_to_json_type_3_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage board = huzzah framework = arduino lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} -src_filter = ${base.src_filter} ${transactions_transaction_to_json_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_3_tests.src_filter} upload_speed = ${base.upload_speed} [env:esp8266_utils_tests] diff --git a/test/transactions/transaction_to_json.cpp b/test/transactions/transaction_to_json.cpp deleted file mode 100644 index 68800aaa..00000000 --- a/test/transactions/transaction_to_json.cpp +++ /dev/null @@ -1,169 +0,0 @@ - -#include "gtest/gtest.h" - -#include -#include - -#include - -#include "utils/json.h" - -using namespace Ark::Crypto::Transactions; - -TEST(transactions, transaction_to_json) { // NOLINT - // Type 0 // - auto transfer = Builder::buildTransfer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 1, - "", - "Secret passphrase"); - - auto tJson = transfer.toJson(); - - const size_t tCapacity = JSON_OBJECT_SIZE(8) + 450; - DynamicJsonDocument tDoc(tCapacity); - - DeserializationError tError = deserializeJson(tDoc, tJson); - ASSERT_FALSE(tError); - - ASSERT_STREQ("1", tDoc["amount"].as()); - - ASSERT_STREQ("10000000", tDoc["fee"].as()); - - ASSERT_STRNE("", tDoc["id"].as()); - - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - tDoc["recipient"].as()); - - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - tDoc["senderPublicKey"].as()); - - ASSERT_STRNE("", tDoc["signature"].as()); - - ASSERT_GT(tDoc["timestamp"].as(), 50000000UL); - - ASSERT_LT(tDoc["timestamp"].as(), 1000000000UL); - - ASSERT_EQ(tDoc["type"].as(), 0); - - ASSERT_EQ(1, tDoc["version"].as()); - - // Type 1 // - auto secondSignatureRegistration = Builder::buildSecondSignatureRegistration( - "Secret passphrase", - "Second Secret passphrase"); - - auto ssJson = secondSignatureRegistration.toJson(); - - const size_t ssCapacity = 2 * JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(10) + 690; - DynamicJsonDocument ssDoc(ssCapacity); - - DeserializationError ssError = deserializeJson(ssDoc, ssJson); - ASSERT_FALSE(ssError); - - ASSERT_STREQ("0", ssDoc["amount"].as()); - - ASSERT_STREQ("02e1684d8990c0a5625aec85977fcf22204884bc08d45dbc71b2859e5fa4f45104", - ssDoc["asset"]["signature"]["publicKey"].as()); - - ASSERT_STREQ("500000000", ssDoc["fee"].as()); - - ASSERT_STRNE("", ssDoc["id"].as()); - - ASSERT_STREQ("", ssDoc["recipient"].as()); - - ASSERT_STRNE("", ssDoc["secondSignature"].as()); - - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - ssDoc["senderPublicKey"].as()); - - ASSERT_STRNE("", ssDoc["signature"].as()); - - ASSERT_GT(ssDoc["timestamp"].as(), 50000000UL); - ASSERT_LT(ssDoc["timestamp"].as(), 1000000000UL); - - ASSERT_EQ(ssDoc["type"].as(), 1); - - ASSERT_EQ(1, ssDoc["version"].as()); - - // Type 2 // - auto delegateRegistration = Builder::buildDelegateRegistration( - "testName", - "Secret passphrase"); - - auto dJson = delegateRegistration.toJson(); - - const size_t dCapacity = 2*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(9) + 450; - DynamicJsonDocument dDoc(dCapacity); - - DeserializationError dError = deserializeJson(dDoc, dJson); - ASSERT_FALSE(dError); - - ASSERT_STREQ("0", dDoc["amount"].as()); - - ASSERT_STREQ("testName", - dDoc["asset"]["delegate"]["username"].as()); - - ASSERT_STREQ("2500000000", dDoc["fee"].as()); - - ASSERT_STRNE("", dDoc["id"].as()); - - ASSERT_STREQ("", dDoc["recipient"].as()); - - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - dDoc["senderPublicKey"].as()); - - ASSERT_STRNE("", dDoc["signature"].as()); - - ASSERT_GT(dDoc["timestamp"].as(), 50000000UL); - ASSERT_LT(dDoc["timestamp"].as(), 1000000000UL); - - ASSERT_EQ(dDoc["type"].as(), 2); - - ASSERT_EQ(1, dDoc["version"].as()); - - // Type 3 // - std::vector votes = { - "-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6," - "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6" - }; - - auto vote = Builder::buildVote(votes, "Secret passphrase"); - - auto vJson = vote.toJson(); - - const size_t vCapacity = JSON_ARRAY_SIZE(1) - + JSON_OBJECT_SIZE(1) - + JSON_OBJECT_SIZE(9) - + 700; - DynamicJsonDocument vDoc(vCapacity); - - DeserializationError vError = deserializeJson(vDoc, vJson); - ASSERT_FALSE(vError); - - ASSERT_STREQ("0", vDoc["amount"].as()); - - ASSERT_STREQ("-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", - vDoc["asset"]["votes"][0].as()); - - ASSERT_STREQ("+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", - vDoc["asset"]["votes"][1].as()); - - ASSERT_STREQ("100000000", vDoc["fee"].as()); - - ASSERT_STRNE("", vDoc["id"].as()); - - ASSERT_STREQ("DPgZq5MK6rm5yVks9b7TrA22F8FwRvkCtF", - vDoc["recipient"].as()); - - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - vDoc["senderPublicKey"].as()); - - ASSERT_STRNE("", vDoc["signature"].as()); - - ASSERT_GT(vDoc["timestamp"].as(), 50000000UL); - ASSERT_LT(vDoc["timestamp"].as(), 1000000000UL); - - ASSERT_EQ(vDoc["type"].as(), 3); - - ASSERT_EQ(1, vDoc["version"].as()); -} diff --git a/test/transactions/transaction_to_json_type_0.cpp b/test/transactions/transaction_to_json_type_0.cpp new file mode 100644 index 00000000..c25692a1 --- /dev/null +++ b/test/transactions/transaction_to_json_type_0.cpp @@ -0,0 +1,53 @@ + +#include "gtest/gtest.h" + +#include +#include + +#include + +#include "utils/json.h" + +using namespace Ark::Crypto::Transactions; + +TEST(transactions, transaction_to_json_type_0) { // NOLINT + std::string tJson; + + { // force dtor on transfer to clean up stack space + auto transfer = Builder::buildTransfer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", + 1, + "", + "Secret passphrase"); + + tJson = transfer.toJson(); + } + + const size_t tCapacity = JSON_OBJECT_SIZE(8) + 450; + DynamicJsonDocument tDoc(tCapacity); + + DeserializationError tError = deserializeJson(tDoc, tJson); + ASSERT_FALSE(tError); + + ASSERT_STREQ("1", tDoc["amount"].as()); + + ASSERT_STREQ("10000000", tDoc["fee"].as()); + + ASSERT_STRNE("", tDoc["id"].as()); + + ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", + tDoc["recipient"].as()); + + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + tDoc["senderPublicKey"].as()); + + ASSERT_STRNE("", tDoc["signature"].as()); + + ASSERT_GT(tDoc["timestamp"].as(), 50000000UL); + + ASSERT_LT(tDoc["timestamp"].as(), 1000000000UL); + + ASSERT_EQ(tDoc["type"].as(), 0); + + ASSERT_EQ(1, tDoc["version"].as()); + +} diff --git a/test/transactions/transaction_to_json_type_1.cpp b/test/transactions/transaction_to_json_type_1.cpp new file mode 100644 index 00000000..22b4e237 --- /dev/null +++ b/test/transactions/transaction_to_json_type_1.cpp @@ -0,0 +1,55 @@ + +#include "gtest/gtest.h" + +#include +#include + +#include + +#include "utils/json.h" + +using namespace Ark::Crypto::Transactions; + +TEST(transactions, transaction_to_json_type_1) { // NOLINT + std::string ssJson; + + { // force dtor on transfer to clean up stack space + auto secondSignatureRegistration = Builder::buildSecondSignatureRegistration( + "Secret passphrase", + "Second Secret passphrase"); + + ssJson = secondSignatureRegistration.toJson(); + } + + const size_t ssCapacity = 2 * JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(10) + 690; + DynamicJsonDocument ssDoc(ssCapacity); + + DeserializationError ssError = deserializeJson(ssDoc, ssJson); + ASSERT_FALSE(ssError); + + ASSERT_STREQ("0", ssDoc["amount"].as()); + + ASSERT_STREQ("02e1684d8990c0a5625aec85977fcf22204884bc08d45dbc71b2859e5fa4f45104", + ssDoc["asset"]["signature"]["publicKey"].as()); + + ASSERT_STREQ("500000000", ssDoc["fee"].as()); + + ASSERT_STRNE("", ssDoc["id"].as()); + + ASSERT_STREQ("", ssDoc["recipient"].as()); + + ASSERT_STRNE("", ssDoc["secondSignature"].as()); + + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + ssDoc["senderPublicKey"].as()); + + ASSERT_STRNE("", ssDoc["signature"].as()); + + ASSERT_GT(ssDoc["timestamp"].as(), 50000000UL); + ASSERT_LT(ssDoc["timestamp"].as(), 1000000000UL); + + ASSERT_EQ(ssDoc["type"].as(), 1); + + ASSERT_EQ(1, ssDoc["version"].as()); + +} diff --git a/test/transactions/transaction_to_json_type_2.cpp b/test/transactions/transaction_to_json_type_2.cpp new file mode 100644 index 00000000..4ca07eef --- /dev/null +++ b/test/transactions/transaction_to_json_type_2.cpp @@ -0,0 +1,53 @@ + +#include "gtest/gtest.h" + +#include +#include + +#include + +#include "utils/json.h" + +using namespace Ark::Crypto::Transactions; + +TEST(transactions, transaction_to_json_type_2) { // NOLINT + std::string dJson; + + { // force dtor on transfer to clean up stack space + auto delegateRegistration = Builder::buildDelegateRegistration( + "testName", + "Secret passphrase"); + + dJson = delegateRegistration.toJson(); + } + + const size_t dCapacity = 2*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(9) + 450; + DynamicJsonDocument dDoc(dCapacity); + + DeserializationError dError = deserializeJson(dDoc, dJson); + ASSERT_FALSE(dError); + + ASSERT_STREQ("0", dDoc["amount"].as()); + + ASSERT_STREQ("testName", + dDoc["asset"]["delegate"]["username"].as()); + + ASSERT_STREQ("2500000000", dDoc["fee"].as()); + + ASSERT_STRNE("", dDoc["id"].as()); + + ASSERT_STREQ("", dDoc["recipient"].as()); + + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + dDoc["senderPublicKey"].as()); + + ASSERT_STRNE("", dDoc["signature"].as()); + + ASSERT_GT(dDoc["timestamp"].as(), 50000000UL); + ASSERT_LT(dDoc["timestamp"].as(), 1000000000UL); + + ASSERT_EQ(dDoc["type"].as(), 2); + + ASSERT_EQ(1, dDoc["version"].as()); + +} diff --git a/test/transactions/transaction_to_json_type_3.cpp b/test/transactions/transaction_to_json_type_3.cpp new file mode 100644 index 00000000..3d3dad47 --- /dev/null +++ b/test/transactions/transaction_to_json_type_3.cpp @@ -0,0 +1,59 @@ + +#include "gtest/gtest.h" + +#include +#include + +#include + +#include "utils/json.h" + +using namespace Ark::Crypto::Transactions; + +TEST(transactions, transaction_to_json_type_3) { // NOLINT + std::string vJson; + { // force dtor on transfer to clean up stack space + std::vector votes = { + "-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6," + "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6" + }; + auto vote = Builder::buildVote(votes, "Secret passphrase"); + vJson = vote.toJson(); + } + + const size_t vCapacity = JSON_ARRAY_SIZE(1) + + JSON_OBJECT_SIZE(1) + + JSON_OBJECT_SIZE(9) + + 700; + DynamicJsonDocument vDoc(vCapacity); + + DeserializationError vError = deserializeJson(vDoc, vJson); + ASSERT_FALSE(vError); + + ASSERT_STREQ("0", vDoc["amount"].as()); + + ASSERT_STREQ("-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", + vDoc["asset"]["votes"][0].as()); + + ASSERT_STREQ("+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", + vDoc["asset"]["votes"][1].as()); + + ASSERT_STREQ("100000000", vDoc["fee"].as()); + + ASSERT_STRNE("", vDoc["id"].as()); + + ASSERT_STREQ("DPgZq5MK6rm5yVks9b7TrA22F8FwRvkCtF", + vDoc["recipient"].as()); + + ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", + vDoc["senderPublicKey"].as()); + + ASSERT_STRNE("", vDoc["signature"].as()); + + ASSERT_GT(vDoc["timestamp"].as(), 50000000UL); + ASSERT_LT(vDoc["timestamp"].as(), 1000000000UL); + + ASSERT_EQ(vDoc["type"].as(), 3); + + ASSERT_EQ(1, vDoc["version"].as()); +} From 524c443441bece65e943a4dac61ec1f12b93eb93 Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Thu, 31 Oct 2019 11:17:58 -0500 Subject: [PATCH 11/21] ci: add esp8266 arduino cli configs back (#182) --- .github/workflows/test/install_arduino.sh | 4 +++- .github/workflows/test/script_arduino.sh | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test/install_arduino.sh b/.github/workflows/test/install_arduino.sh index efccff77..ac086c66 100644 --- a/.github/workflows/test/install_arduino.sh +++ b/.github/workflows/test/install_arduino.sh @@ -14,8 +14,10 @@ sudo ln -s /usr/local/share/arduino-cli /usr/local/bin/arduino-cli printf "board_manager: additional_urls: - - https://dl.espressif.com/dl/package_esp32_index.json" >> .cli-config.yml + - https://dl.espressif.com/dl/package_esp32_index.json + - http://arduino.esp8266.com/stable/package_esp8266com_index.json" >> .cli-config.yml sudo mv .cli-config.yml /usr/local/share/ arduino-cli core update-index arduino-cli core install esp32:esp32 +arduino-cli core install esp8266:esp8266 diff --git a/.github/workflows/test/script_arduino.sh b/.github/workflows/test/script_arduino.sh index 5f3b2abf..74554488 100644 --- a/.github/workflows/test/script_arduino.sh +++ b/.github/workflows/test/script_arduino.sh @@ -10,3 +10,5 @@ arduino-cli lib install "BIP66@0.2.0" arduino-cli lib install "micro-ecc@1.0.0" arduino-cli compile --output temp.bin -b esp32:esp32:esp32 ~/Arduino/libraries/cpp-crypto/examples/arduino/ESP32/ESP32.ino --debug + +arduino-cli compile --output temp.bin -b esp8266:esp8266:nodemcu ~/Arduino/libraries/cpp-crypto/examples/arduino/ESP8266/ESP8266.ino --debug From c558a732ae69e45a294cb04628f0b4080f083adf Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Thu, 31 Oct 2019 14:03:14 -0500 Subject: [PATCH 12/21] feat: disable wifi on esp8266 during tests (#181) * Disabled WiFi radio during tests to lower current draw. * Increased CPU frequency to execute tests faster to improve stability. Stability has been improved but is not perfect. There is ~15% failure rate when running complex tests where the board goes through a DMT reset. --- test/iot/platform.hpp | 19 +++++++++++++++++++ test/iot/test_main.cpp | 6 +++++- test/platformio.ini | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 test/iot/platform.hpp diff --git a/test/iot/platform.hpp b/test/iot/platform.hpp new file mode 100644 index 00000000..e0eec0ae --- /dev/null +++ b/test/iot/platform.hpp @@ -0,0 +1,19 @@ +#ifndef CRYPTO_TEST_IOT_PLATFORM_HPP +#define CRYPTO_TEST_IOT_PLATFORM_HPP + +#ifdef ESP8266 + +#include "ESP8266WiFi.h" + +#endif + +void optimize_for_testing() { + +#ifdef ESP8266 + WiFi.forceSleepBegin(); // turn off WiFi to save power and processing time + delay(1); // give WiFi time to power down +#endif + +} + +#endif diff --git a/test/iot/test_main.cpp b/test/iot/test_main.cpp index 9851d3b7..8de7140a 100644 --- a/test/iot/test_main.cpp +++ b/test/iot/test_main.cpp @@ -4,6 +4,8 @@ #include +#include "platform.hpp" + // Set Dummy Time for testing board #include void setDummyTime() { @@ -17,6 +19,8 @@ void setDummyTime() { void setup() { Serial.begin(115200); + optimize_for_testing(); + setDummyTime(); testing::InitGoogleTest(); @@ -25,7 +29,7 @@ void setup() { void loop() { // loop the tests. auto __attribute__((unused)) run = RUN_ALL_TESTS(); - delay(1000); + delay(1); } #endif diff --git a/test/platformio.ini b/test/platformio.ini index 96b67c18..8a5b76b8 100644 --- a/test/platformio.ini +++ b/test/platformio.ini @@ -23,6 +23,7 @@ upload_speed = 921600 [esp8266] build_flags = -D_GLIBCXX_USE_C99 +board_build.f_cpu = 160000000L [common_tests] src_filter = +<../test/common> @@ -158,6 +159,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${common_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_ecdsa_sign_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -167,6 +169,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_ecdsa_sign_null_pk_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -176,6 +179,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_pk_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_ecdsa_sign_null_hash32_sha256_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -185,6 +189,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_hash32_sha256_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_ecdsa_sign_null_hash32_privatekey_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -194,6 +199,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_hash32_privatekey_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_publickey_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -203,6 +209,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_publickey_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_verify_invalid_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -212,6 +219,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_verify_invalid_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_verify_valid_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -221,6 +229,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_curve_verify_valid_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_hash_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -230,6 +239,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_hash_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_message_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -239,6 +249,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_message_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_message_sign_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -248,6 +259,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_message_sign_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_message_verify_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -257,6 +269,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_message_verify_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_slot_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -266,6 +279,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${crypto_slot_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_defaults_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -275,6 +289,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${defaults_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_identities_address_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -284,6 +299,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${identities_address_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_identities_keys_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -293,6 +309,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${identities_keys_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_identities_keys_publickey_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -302,6 +319,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${identities_keys_publickey_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_identities_privatekey_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -311,6 +329,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${identities_privatekey_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_identities_publickey_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -320,6 +339,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${identities_publickey_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_managers_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -329,6 +349,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${managers_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_networks_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -338,6 +359,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${networks_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_builder_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -347,6 +369,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_builder_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_builder_transfer_custom_network_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -356,6 +379,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_builder_transfer_custom_network_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_deserializer_delegate_registration_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -365,6 +389,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_deserializer_delegate_registration_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_deserializer_multi_signature_registration_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -374,6 +399,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_deserializer_multi_signature_registration_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_deserializer_second_signature_registration_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -383,6 +409,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_deserializer_second_signature_registration_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_deserializer_transfer_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -392,6 +419,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_deserializer_transfer_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_deserializer_vote_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -401,6 +429,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_deserializer_vote_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_serializer_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -410,6 +439,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_serializer_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -419,6 +449,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_array_type_0_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -428,6 +459,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_0_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_array_type_1_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -437,6 +469,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_1_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_array_type_2_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -446,6 +479,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_2_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_array_type_3_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -455,6 +489,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_3_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_json_type_0_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -464,6 +499,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_0_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_json_type_1_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -473,6 +509,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_1_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_json_type_2_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -482,6 +519,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_2_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_json_type_3_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -491,6 +529,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_3_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_utils_tests] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage @@ -500,6 +539,7 @@ lib_deps = ${base.lib_deps} build_flags = ${base.build_flags} ${esp8266.build_flags} src_filter = ${base.src_filter} ${utils_tests.src_filter} upload_speed = ${base.upload_speed} +board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp32_common_tests] platform = espressif32 From e30531fa6c68549d17ceff15f90c681340666108 Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Mon, 4 Nov 2019 10:18:44 -0600 Subject: [PATCH 13/21] chore: add +x to scripts (#185) --- .github/workflows/test/clang_format.sh | 0 .github/workflows/test/clang_tidy.sh | 0 .github/workflows/test/install_arduino.sh | 0 .github/workflows/test/install_platform_io.sh | 0 .github/workflows/test/script_arduino.sh | 0 .github/workflows/test/script_desktop.sh | 0 .github/workflows/test/script_platform_io.sh | 0 extras/ARDUINO_IDE.sh | 0 8 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 .github/workflows/test/clang_format.sh mode change 100644 => 100755 .github/workflows/test/clang_tidy.sh mode change 100644 => 100755 .github/workflows/test/install_arduino.sh mode change 100644 => 100755 .github/workflows/test/install_platform_io.sh mode change 100644 => 100755 .github/workflows/test/script_arduino.sh mode change 100644 => 100755 .github/workflows/test/script_desktop.sh mode change 100644 => 100755 .github/workflows/test/script_platform_io.sh mode change 100644 => 100755 extras/ARDUINO_IDE.sh diff --git a/.github/workflows/test/clang_format.sh b/.github/workflows/test/clang_format.sh old mode 100644 new mode 100755 diff --git a/.github/workflows/test/clang_tidy.sh b/.github/workflows/test/clang_tidy.sh old mode 100644 new mode 100755 diff --git a/.github/workflows/test/install_arduino.sh b/.github/workflows/test/install_arduino.sh old mode 100644 new mode 100755 diff --git a/.github/workflows/test/install_platform_io.sh b/.github/workflows/test/install_platform_io.sh old mode 100644 new mode 100755 diff --git a/.github/workflows/test/script_arduino.sh b/.github/workflows/test/script_arduino.sh old mode 100644 new mode 100755 diff --git a/.github/workflows/test/script_desktop.sh b/.github/workflows/test/script_desktop.sh old mode 100644 new mode 100755 diff --git a/.github/workflows/test/script_platform_io.sh b/.github/workflows/test/script_platform_io.sh old mode 100644 new mode 100755 diff --git a/extras/ARDUINO_IDE.sh b/extras/ARDUINO_IDE.sh old mode 100644 new mode 100755 From 8cdd1e72df58ff57b12372c98be53174a06a65f4 Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Thu, 14 Nov 2019 06:33:40 -0600 Subject: [PATCH 14/21] refactor: remove usage of monolithic arkCrypto.h (#190) * refactor: remove usage of monolithic arkCrypto.h * Updated arkCrypto.h header to just be a dummy header for the sole purpose of forcing the cpp-crypto library to be linked into an Arduino sketch. * Updated tests to use specific headers instead of arkCrypto.h * Updated Arduino sample sketches to use only the headers required for the sketch. * chore: update comments - add note on ESP8266 Arduino CPU speed. - reword comment in `arkCrypto.h` to be less than 80 characters. --- examples/arduino/ESP32/ESP32.ino | 55 ++++++++++++------- examples/arduino/ESP32/arkCrypto_esp32.h | 22 ++++++++ examples/arduino/ESP8266/ESP8266.ino | 49 +++++++++++------ examples/arduino/ESP8266/arkCrypto_esp8266.h | 22 ++++++++ src/include/cpp-crypto/arkCrypto.h | 29 +--------- test/common/configuration.cpp | 6 +- test/common/fee_policy.cpp | 3 +- test/common/network.cpp | 4 +- test/defaults/static_fees.cpp | 3 +- test/defaults/transaction_types.cpp | 5 +- ...erializer_multi_signature_registration.cpp | 12 +++- ...rializer_second_signature_registration.cpp | 6 +- test/transactions/deserializer_transfer.cpp | 4 +- test/transactions/deserializer_vote.cpp | 7 ++- test/transactions/serializer.cpp | 4 +- test/transactions/transaction.cpp | 5 +- .../transaction_to_json_type_0.cpp | 5 +- .../transaction_to_json_type_1.cpp | 5 +- .../transaction_to_json_type_2.cpp | 5 +- .../transaction_to_json_type_3.cpp | 5 +- 20 files changed, 163 insertions(+), 93 deletions(-) create mode 100644 examples/arduino/ESP32/arkCrypto_esp32.h create mode 100644 examples/arduino/ESP8266/arkCrypto_esp8266.h diff --git a/examples/arduino/ESP32/ESP32.ino b/examples/arduino/ESP32/ESP32.ino index 9b16a8eb..9f48f287 100644 --- a/examples/arduino/ESP32/ESP32.ino +++ b/examples/arduino/ESP32/ESP32.ino @@ -6,7 +6,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. **/ - + /** * ESP32 Cpp-Crypto Usage Example Sketch * @@ -24,16 +24,29 @@ /** * This is where you include the 'arkCrypto.h' header. * This allows your project to use Ark Cpp-Crypto. + * This header is empty and is just to force the inclusion + * of the library into the Arduino sketch */ #include /**/ +/** + * This is a helper header that includes all the required Ark + * headers required for this sketch. +*/ +#include "arkCrypto_esp32.h" +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; +using namespace Ark::Crypto::Transactions; + /** * This is a small hex helper header included in ARK Cpp-Crypto */ #include "utils/hex.hpp" /**/ +#include + /****************************************/ void checkCrypto() { @@ -66,23 +79,23 @@ void checkCrypto() { /** * This is how you can check the default 'Network' "Transaction 'Fees' by type. - * In this example, it should return a 'uint64_t' integer of '10000000' as the default 'Fee' for a 'Transaction' of 'Type' '0'. + * In this example, it should return a 'uint64_t' integer of '10000000' as the default 'Fee' for a 'Transaction' of 'Type' '0'. */ Configuration config; unsigned long typeZeroTransactionFee = config.getFee(0); Serial.print("\nType 0 default Transaction Fee: "); Serial.println(typeZeroTransactionFee); // The response is a 'uint64_t' integer. - + /**/ /********************/ /** - * The following methods allows you to create an ARK address. + * The following methods allows you to create an ARK address. * This is done by passing a 12-word 'Passphrase' and the 'Network' 'Version' "byte". - * The 'Version" "byte" is a BASE58 P2PKH byte. Ark Devnet is '0x1E'; Ark Mainnet is '0x17'. - * - * Given the passphrase "this is a top secret passphrase", + * The 'Version" "byte" is a BASE58 P2PKH byte. Ark Devnet is '0x1E'; Ark Mainnet is '0x17'. + * + * Given the passphrase "this is a top secret passphrase", * and the 'Devnet' 'Version' byte (0x1E); the ARK Address should be "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib" */ const auto passphrase = "this is a top secret passphrase"; @@ -97,10 +110,10 @@ void checkCrypto() { /********************/ /** - * The following methods allows create a 'PrivateKey'. + * The following methods allows create a 'PrivateKey'. * This is done by passing a 12-word 'Passphrase'. - * - * Given the passphrase "this is a top secret passphrase", + * + * Given the passphrase "this is a top secret passphrase", * the 'PrivateKey" should be "d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712". * This is a 'SHA256' of your "Passphrase". */ @@ -113,10 +126,10 @@ void checkCrypto() { /********************/ /** - * The following methods allows create a 'PublicKey'. + * The following methods allows create a 'PublicKey'. * This is done by passing a 12-word 'Passphrase'. - * - * Given the passphrase "this is a top secret passphrase", + * + * Given the passphrase "this is a top secret passphrase", * the 'PublicKey" should be "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192". */ const auto passphrase3 = "this is a top secret passphrase"; @@ -128,13 +141,13 @@ void checkCrypto() { /********************/ /** - * The following methods allows create a 'WIF'-style "PrivateKey". + * The following methods allows create a 'WIF'-style "PrivateKey". * 'WIF' stands for "Wallet Import Format" * This is done by passing a 12-word 'Passphrase' and the 'Network' 'WIF' "byte". - * The 'WIF" "byte" is a BASE58 WIF byte. Ark Devnet is '0xaa'; Ark Mainnet is also '0xaa'. + * The 'WIF" "byte" is a BASE58 WIF byte. Ark Devnet is '0xaa'; Ark Mainnet is also '0xaa'. - * - * Given the passphrase "this is a top secret passphrase", + * + * Given the passphrase "this is a top secret passphrase", * and the 'Devnet' 'WIF' byte (0xaa); * The 'WIF" should be "SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA". */ @@ -148,11 +161,11 @@ void checkCrypto() { /********************/ /** - * The following methods allows you to 'Sign' a text 'Message'. + * The following methods allows you to 'Sign' a text 'Message'. * This is done by passing the text to be signed, and a 12-word 'Passphrase'. - * - * Given the text "Hello World", - * and the passphrase "this is a top secret passphrase", + * + * Given the text "Hello World", + * and the passphrase "this is a top secret passphrase", * The 'Signature" should be "304402200fb4adddd1f1d652b544ea6ab62828a0a65b712ed447e2538db0caebfa68929e02205ecb2e1c63b29879c2ecf1255db506d671c8b3fa6017f67cfd1bf07e6edd1cc8". */ const auto text = "Hello World"; diff --git a/examples/arduino/ESP32/arkCrypto_esp32.h b/examples/arduino/ESP32/arkCrypto_esp32.h new file mode 100644 index 00000000..d7a9e252 --- /dev/null +++ b/examples/arduino/ESP32/arkCrypto_esp32.h @@ -0,0 +1,22 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_CRYPTO_ESP32_H +#define ARK_CRYPTO_ESP32_H + +#include "common/configuration.hpp" +#include "common/network.hpp" +#include "crypto/message.hpp" +#include "identities/address.hpp" +#include "identities/privatekey.hpp" +#include "identities/publickey.hpp" +#include "identities/wif.hpp" +#include "transactions/builder.h" + +#endif diff --git a/examples/arduino/ESP8266/ESP8266.ino b/examples/arduino/ESP8266/ESP8266.ino index a35022cb..6b90ef26 100644 --- a/examples/arduino/ESP8266/ESP8266.ino +++ b/examples/arduino/ESP8266/ESP8266.ino @@ -6,7 +6,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. **/ - + /** * ESP8266 Cpp-Crypto Usage Example Sketch * @@ -18,16 +18,29 @@ * NOTE: At the time of this writing, the Cpp-Crypto library requires running the 'ARDUINO_IDE.sh' bash script located in the 'extras' folder. * This converts our library to be compatible with the Arduino IDE. */ - + /****************************************/ /** * This is where you include the 'arkCrypto.h' header. * This allows your project to use Ark Cpp-Crypto. + * This header is empty and is just to force the inclusion + * of the library into the Arduino sketch */ #include /**/ +/** + * This is a helper header that includes all the required Ark + * headers required for this sketch. + * + * Also set the CPU frequency to 160MHz using the Arduino IDE's 'Tools' menu. +*/ +#include "arkCrypto_esp8266.h" +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; +using namespace Ark::Crypto::Transactions; + /****************************************/ void checkCrypto() { @@ -59,23 +72,23 @@ void checkCrypto() { /** * This is how you can check the default 'Network' "Transaction 'Fees' by type. - * In this example, it should return a 'uint64_t' integer of '10000000' as the default 'Fee' for a 'Transaction' of 'Type' '0'. + * In this example, it should return a 'uint64_t' integer of '10000000' as the default 'Fee' for a 'Transaction' of 'Type' '0'. */ Configuration config; unsigned long typeZeroTransactionFee = config.getFee(0); Serial.print("\nType 0 default Transaction Fee: "); Serial.println(typeZeroTransactionFee); // The response is a 'uint64_t' integer. - + /**/ /********************/ /** - * The following methods allows you to create an ARK address. + * The following methods allows you to create an ARK address. * This is done by passing a 12-word 'Passphrase' and the 'Network' 'Version' "byte". - * The 'Version" "byte" is a BASE58 P2PKH byte. Ark Devnet is '0x1E'; Ark Mainnet is '0x17'. - * - * Given the passphrase "this is a top secret passphrase", + * The 'Version" "byte" is a BASE58 P2PKH byte. Ark Devnet is '0x1E'; Ark Mainnet is '0x17'. + * + * Given the passphrase "this is a top secret passphrase", * and the 'Devnet' 'Version' byte (0x1E); the ARK Address should be "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib" */ const auto passphrase = "this is a top secret passphrase"; @@ -90,10 +103,10 @@ void checkCrypto() { /********************/ /** - * The following methods allows create a 'PrivateKey'. + * The following methods allows create a 'PrivateKey'. * This is done by passing a 12-word 'Passphrase'. - * - * Given the passphrase "this is a top secret passphrase", + * + * Given the passphrase "this is a top secret passphrase", * the 'PrivateKey" should be "d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712". * This is a 'SHA256' of your "Passphrase". */ @@ -106,10 +119,10 @@ void checkCrypto() { /********************/ /** - * The following methods allows create a 'PublicKey'. + * The following methods allows create a 'PublicKey'. * This is done by passing a 12-word 'Passphrase'. - * - * Given the passphrase "this is a top secret passphrase", + * + * Given the passphrase "this is a top secret passphrase", * the 'PublicKey" should be "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192". */ const auto passphrase3 = "this is a top secret passphrase"; @@ -121,13 +134,13 @@ void checkCrypto() { /********************/ /** - * The following methods allows create a 'WIF'-style "PrivateKey". + * The following methods allows create a 'WIF'-style "PrivateKey". * 'WIF' stands for "Wallet Import Format" * This is done by passing a 12-word 'Passphrase' and the 'Network' 'WIF' "byte". - * The 'WIF" "byte" is a BASE58 WIF byte. Ark Devnet is '0xaa'; Ark Mainnet is also '0xaa'. + * The 'WIF" "byte" is a BASE58 WIF byte. Ark Devnet is '0xaa'; Ark Mainnet is also '0xaa'. - * - * Given the passphrase "this is a top secret passphrase", + * + * Given the passphrase "this is a top secret passphrase", * and the 'Devnet' 'WIF' byte (0xaa); * The 'WIF" should be "SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA". */ diff --git a/examples/arduino/ESP8266/arkCrypto_esp8266.h b/examples/arduino/ESP8266/arkCrypto_esp8266.h new file mode 100644 index 00000000..404d1bc9 --- /dev/null +++ b/examples/arduino/ESP8266/arkCrypto_esp8266.h @@ -0,0 +1,22 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_CRYPTO_ESP8266_H +#define ARK_CRYPTO_ESP8266_H + +#include "common/configuration.hpp" +#include "common/network.hpp" +#include "crypto/message.hpp" +#include "identities/address.hpp" +#include "identities/privatekey.hpp" +#include "identities/publickey.hpp" +#include "identities/wif.hpp" +#include "transactions/builder.h" + +#endif diff --git a/src/include/cpp-crypto/arkCrypto.h b/src/include/cpp-crypto/arkCrypto.h index cd87b85e..0b6088bc 100644 --- a/src/include/cpp-crypto/arkCrypto.h +++ b/src/include/cpp-crypto/arkCrypto.h @@ -10,33 +10,6 @@ #ifndef ARK_CRYPTO_H #define ARK_CRYPTO_H -#include "common/configuration.hpp" -#include "common/fee_policy.hpp" -#include "common/network.hpp" - -#include "crypto/message.hpp" -#include "crypto/slot.hpp" - -#include "defaults/static_fees.hpp" -#include "defaults/transaction_types.hpp" - -#include "identities/address.hpp" -#include "identities/keys.hpp" -#include "identities/privatekey.hpp" -#include "identities/publickey.hpp" -#include "identities/wif.hpp" - -#include "networks/devnet.hpp" -#include "networks/mainnet.hpp" -#include "networks/testnet.hpp" - -#include "transactions/builder.h" -#include "transactions/deserializer.h" -#include "transactions/serializer.h" -#include "transactions/transaction.h" - -using namespace Ark::Crypto; -using namespace Ark::Crypto::identities; -using namespace Ark::Crypto::Transactions; +// This header forces the Arduino IDE to include ARK Crypto in its search path. #endif diff --git a/test/common/configuration.cpp b/test/common/configuration.cpp index ea0b0695..f5d06935 100644 --- a/test/common/configuration.cpp +++ b/test/common/configuration.cpp @@ -3,7 +3,11 @@ #include -#include +#include "common/configuration.hpp" +#include "common/fee_policy.hpp" +#include "networks/devnet.hpp" +#include "networks/testnet.hpp" +using namespace Ark::Crypto; namespace { const FeePolicy CustomFeePolicy = { diff --git a/test/common/fee_policy.cpp b/test/common/fee_policy.cpp index 045d0045..1e6a8cef 100644 --- a/test/common/fee_policy.cpp +++ b/test/common/fee_policy.cpp @@ -1,7 +1,8 @@ #include "gtest/gtest.h" -#include +#include "common/fee_policy.hpp" +using namespace Ark::Crypto; namespace { const FeePolicy CustomFeePolicy = { diff --git a/test/common/network.cpp b/test/common/network.cpp index eec7dbe5..ae0dd149 100644 --- a/test/common/network.cpp +++ b/test/common/network.cpp @@ -1,7 +1,9 @@ #include "gtest/gtest.h" -#include +#include "common/network.hpp" +#include "networks/devnet.hpp" +using namespace Ark::Crypto; namespace { const Network CustomNetwork { diff --git a/test/defaults/static_fees.cpp b/test/defaults/static_fees.cpp index 5fb13576..db2f519c 100644 --- a/test/defaults/static_fees.cpp +++ b/test/defaults/static_fees.cpp @@ -3,7 +3,8 @@ #include -#include +#include "defaults/static_fees.hpp" +using namespace Ark::Crypto; namespace { constexpr const uint64_t kTransfer = 10000000ULL; diff --git a/test/defaults/transaction_types.cpp b/test/defaults/transaction_types.cpp index 1365888a..976f7bd0 100644 --- a/test/defaults/transaction_types.cpp +++ b/test/defaults/transaction_types.cpp @@ -1,7 +1,10 @@ #include "gtest/gtest.h" -#include +#include + +#include "defaults/transaction_types.hpp" +using namespace Ark::Crypto; namespace { constexpr const uint8_t TransferType = 0; diff --git a/test/transactions/deserializer_multi_signature_registration.cpp b/test/transactions/deserializer_multi_signature_registration.cpp index c44faf1e..9d0521de 100644 --- a/test/transactions/deserializer_multi_signature_registration.cpp +++ b/test/transactions/deserializer_multi_signature_registration.cpp @@ -1,11 +1,19 @@ #include "gtest/gtest.h" -#include +#include +#include +#include + +#include "defaults/transaction_types.hpp" +#include "identities/address.hpp" +#include "identities/publickey.hpp" +#include "transactions/deserializer.h" +using namespace Ark::Crypto; TEST(transactions, deserialize_multi_signature_registration) { // NOLINT // multi_signature_registration/passphrase.json - Ark::Crypto::Transactions::Deserializer deserializer( + Transactions::Deserializer deserializer( "ff011704724c9a00036928c98ee53a1f52ed01dd87db10ffe1980eb47cd7c0a7d688321f47b5d7d76000943577000000000002031803543c" "6cc3545be6bac09c82721973a052c690658283472e88f24d14739f75acc80276dc5b8706a85ca9fdc46e571ac84e52fbb48e13ec7a165a80" "731b44ae89f1fc02e8d5d17eb17bbc8d7bf1001d29a2d25d1249b7bb7a5b7ad8b7422063091f4b3130440220324d89c5792e4a54ae70b4f1" diff --git a/test/transactions/deserializer_second_signature_registration.cpp b/test/transactions/deserializer_second_signature_registration.cpp index 6d042be6..2de4d86e 100644 --- a/test/transactions/deserializer_second_signature_registration.cpp +++ b/test/transactions/deserializer_second_signature_registration.cpp @@ -1,7 +1,11 @@ #include "gtest/gtest.h" -#include +#include "defaults/transaction_types.hpp" +#include "identities/address.hpp" +#include "identities/publickey.hpp" +#include "transactions/deserializer.h" +using namespace Ark::Crypto; TEST(transactions, deserialize_second_signature_registration) { // NOLINT // second_signature_registration/second-passphrase.json diff --git a/test/transactions/deserializer_transfer.cpp b/test/transactions/deserializer_transfer.cpp index f222ffce..ddd631d5 100644 --- a/test/transactions/deserializer_transfer.cpp +++ b/test/transactions/deserializer_transfer.cpp @@ -1,7 +1,9 @@ #include "gtest/gtest.h" -#include +#include "defaults/transaction_types.hpp" +#include "transactions/deserializer.h" +using namespace Ark::Crypto; TEST(transactions, deserialize_transfer) { // NOLINT // transfer/passphrase-with-vendor-field.json diff --git a/test/transactions/deserializer_vote.cpp b/test/transactions/deserializer_vote.cpp index 41f8ceae..61282810 100644 --- a/test/transactions/deserializer_vote.cpp +++ b/test/transactions/deserializer_vote.cpp @@ -1,7 +1,12 @@ #include "gtest/gtest.h" -#include +#include +#include + +#include "defaults/transaction_types.hpp" +#include "transactions/deserializer.h" +using namespace Ark::Crypto; TEST(transactions, deserialize_vote) { // NOLINT // vote/second-passphrase.json diff --git a/test/transactions/serializer.cpp b/test/transactions/serializer.cpp index 1c457cb4..e158b408 100644 --- a/test/transactions/serializer.cpp +++ b/test/transactions/serializer.cpp @@ -1,7 +1,9 @@ #include "gtest/gtest.h" -#include +#include + +#include "transactions/serializer.h" TEST(transactions, serialize_transfer) { // NOLINT // transfer/passphrase-with-vendor-field.json diff --git a/test/transactions/transaction.cpp b/test/transactions/transaction.cpp index 20f752ea..59309b77 100644 --- a/test/transactions/transaction.cpp +++ b/test/transactions/transaction.cpp @@ -4,10 +4,9 @@ #include #include -#include - +#include "transactions/transaction.h" #include "utils/json.h" - +using namespace Ark::Crypto; using namespace Ark::Crypto::Transactions; TEST(transactions, transaction_default) { diff --git a/test/transactions/transaction_to_json_type_0.cpp b/test/transactions/transaction_to_json_type_0.cpp index c25692a1..378ba7d8 100644 --- a/test/transactions/transaction_to_json_type_0.cpp +++ b/test/transactions/transaction_to_json_type_0.cpp @@ -4,10 +4,9 @@ #include #include -#include - +#include "transactions/builder.h" #include "utils/json.h" - +using namespace Ark::Crypto; using namespace Ark::Crypto::Transactions; TEST(transactions, transaction_to_json_type_0) { // NOLINT diff --git a/test/transactions/transaction_to_json_type_1.cpp b/test/transactions/transaction_to_json_type_1.cpp index 22b4e237..4b58cf3c 100644 --- a/test/transactions/transaction_to_json_type_1.cpp +++ b/test/transactions/transaction_to_json_type_1.cpp @@ -4,10 +4,9 @@ #include #include -#include - +#include "transactions/builder.h" #include "utils/json.h" - +using namespace Ark::Crypto; using namespace Ark::Crypto::Transactions; TEST(transactions, transaction_to_json_type_1) { // NOLINT diff --git a/test/transactions/transaction_to_json_type_2.cpp b/test/transactions/transaction_to_json_type_2.cpp index 4ca07eef..5bacf291 100644 --- a/test/transactions/transaction_to_json_type_2.cpp +++ b/test/transactions/transaction_to_json_type_2.cpp @@ -4,10 +4,9 @@ #include #include -#include - +#include "transactions/builder.h" #include "utils/json.h" - +using namespace Ark::Crypto; using namespace Ark::Crypto::Transactions; TEST(transactions, transaction_to_json_type_2) { // NOLINT diff --git a/test/transactions/transaction_to_json_type_3.cpp b/test/transactions/transaction_to_json_type_3.cpp index 3d3dad47..ce0a51dc 100644 --- a/test/transactions/transaction_to_json_type_3.cpp +++ b/test/transactions/transaction_to_json_type_3.cpp @@ -4,10 +4,9 @@ #include #include -#include - +#include "transactions/builder.h" #include "utils/json.h" - +using namespace Ark::Crypto; using namespace Ark::Crypto::Transactions; TEST(transactions, transaction_to_json_type_3) { // NOLINT From 4539d34d3c45e46fd5fe6bb1a835caa04b4ce8c1 Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Thu, 14 Nov 2019 20:39:24 -0600 Subject: [PATCH 15/21] chore: move global data to function local (#191) Moved global const data to function local to reduce global memory footprint on embedded platforms. --- src/crypto/message.cpp | 8 ++++---- src/crypto/slot.cpp | 22 +++++++++++----------- src/lib/rfc6979/rfc6979.h | 8 ++++---- src/utils/hex.hpp | 2 +- src/utils/str.hpp | 12 +++++------- 5 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/crypto/message.cpp b/src/crypto/message.cpp index f2e05f43..b81a1f4f 100644 --- a/src/crypto/message.cpp +++ b/src/crypto/message.cpp @@ -27,7 +27,6 @@ namespace { constexpr const char* MESSAGE_KEY = "message"; constexpr const char* PUBLICKEY_KEY = "publickey"; constexpr const char* SIGNATURE_KEY = "signature"; -constexpr const size_t MAGIC_JSON_SIZE = 120U; } // namespace // Create an empty Message object for building and signing. @@ -83,12 +82,13 @@ Message::toArray() const { // Create a Json'ified string of the Signed Message. std::string Message::toJson() const { + const size_t MAGIC_JSON_SIZE = 120U; std::map messageArray = this->toArray(); const size_t docLength = this->message.length() + - (PUBLICKEY_COMPRESSED_BYTE_LEN + 1) + // + `/0` - (Curve::Ecdsa::MAX_SIG_LEN + 1); // + `/0` - + (PUBLICKEY_COMPRESSED_BYTE_LEN + 1) + // + `/0` + (Curve::Ecdsa::MAX_SIG_LEN + 1); // + `/0` + const size_t docCapacity = JSON_OBJECT_SIZE(3) + docLength + MAGIC_JSON_SIZE; DynamicJsonDocument doc(docCapacity); diff --git a/src/crypto/slot.cpp b/src/crypto/slot.cpp index 3f65c1a2..0cdf543e 100644 --- a/src/crypto/slot.cpp +++ b/src/crypto/slot.cpp @@ -63,20 +63,20 @@ namespace Ark { namespace Crypto { - namespace { - constexpr const uint8_t TIMESTAMP_LEN = 24U; - constexpr const uint8_t OFFSET_YEAR = 0U; - constexpr const uint8_t OFFSET_MONTH = 5U; - constexpr const uint8_t OFFSET_DAY = 8U; - constexpr const uint8_t OFFSET_HOUR = 11U; - constexpr const uint8_t OFFSET_MINUTE = 14U; - constexpr const uint8_t OFFSET_SECOND = 17U; - constexpr const uint8_t BASE10 = 10U; - constexpr const uint16_t YEAR_1900 = 1900U; - } // namespace + // Get the Epoch of a given Network. uint64_t Slot::epoch(const Ark::Crypto::Network& network) { + const uint8_t TIMESTAMP_LEN = 24U; + const uint8_t OFFSET_YEAR = 0U; + const uint8_t OFFSET_MONTH = 5U; + const uint8_t OFFSET_DAY = 8U; + const uint8_t OFFSET_HOUR = 11U; + const uint8_t OFFSET_MINUTE = 14U; + const uint8_t OFFSET_SECOND = 17U; + const uint8_t BASE10 = 10U; + const uint16_t YEAR_1900 = 1900U; + // If unexpected ISO 8601 date/time length if (network.epoch.length() != TIMESTAMP_LEN) { return 0ULL; diff --git a/src/lib/rfc6979/rfc6979.h b/src/lib/rfc6979/rfc6979.h index e45f3945..7bff4f8d 100644 --- a/src/lib/rfc6979/rfc6979.h +++ b/src/lib/rfc6979/rfc6979.h @@ -164,7 +164,7 @@ inline void secp256k1_sha256_write(secp256k1_sha256_t *hash, const unsigned char } inline void secp256k1_sha256_finalize(secp256k1_sha256_t *hash, unsigned char *out32) { - static const unsigned char pad[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + const unsigned char pad[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; uint32_t sizedesc[2]; @@ -224,8 +224,8 @@ inline void secp256k1_hmac_sha256_finalize(secp256k1_hmac_sha256_t *hash, unsign inline void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha256_t *rng, const unsigned char *key, size_t keylen) { secp256k1_hmac_sha256_t hmac; - static const unsigned char zero[1] = {0x00}; - static const unsigned char one[1] = {0x01}; + const unsigned char zero[1] = {0x00}; + const unsigned char one[1] = {0x01}; memset(rng->v, 0x01, 32); /* RFC6979 3.2.b. */ memset(rng->k, 0x00, 32); /* RFC6979 3.2.c. */ @@ -255,7 +255,7 @@ inline void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha2 inline void secp256k1_rfc6979_hmac_sha256_generate(secp256k1_rfc6979_hmac_sha256_t *rng, unsigned char *out, size_t outlen) { /* RFC6979 3.2.h. */ - static const unsigned char zero[1] = {0x00}; + const unsigned char zero[1] = {0x00}; if (rng->retry != 0) { secp256k1_hmac_sha256_t hmac; secp256k1_hmac_sha256_initialize(&hmac, rng->k, 32); diff --git a/src/utils/hex.hpp b/src/utils/hex.hpp index 3c6f2967..1c99a148 100644 --- a/src/utils/hex.hpp +++ b/src/utils/hex.hpp @@ -21,7 +21,7 @@ template inline std::string BytesToHex( const T itbegin, const T itend) { - static const char hexmap[16] = { + const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; diff --git a/src/utils/str.hpp b/src/utils/str.hpp index 775f7b14..5388764b 100644 --- a/src/utils/str.hpp +++ b/src/utils/str.hpp @@ -14,18 +14,16 @@ #include #include -namespace { -const size_t ALPHANUM_TABLE_LEN = 128U; -constexpr std::array AlphaNumericTable = {{ - #include "utils/str.table" -}}; -} // namespace - /**/ // Checks string length with bounds and char checking. // returned length excludes the null terminator. inline size_t strlenSafe(const char* str) { + const size_t ALPHANUM_TABLE_LEN = 128U; + const std::array AlphaNumericTable = {{ + #include "utils/str.table" + }}; + size_t count = 0; while (str[count] != 0 || isspace(str[count]) || From 986aa86577c5df36f2c8f6d51cc8438ba6a97c70 Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Thu, 14 Nov 2019 20:46:42 -0600 Subject: [PATCH 16/21] ci: reduce pio build times for ci (#192) --- .github/workflows/test/script_platform_io.sh | 6 +- platformio.ini | 11 +- test/platformio.ini | 402 ++++--------------- 3 files changed, 87 insertions(+), 332 deletions(-) diff --git a/.github/workflows/test/script_platform_io.sh b/.github/workflows/test/script_platform_io.sh index da9305f3..bd89913d 100755 --- a/.github/workflows/test/script_platform_io.sh +++ b/.github/workflows/test/script_platform_io.sh @@ -1,3 +1,7 @@ +#!/usr/bin/env bash + # run PlatformIO builds platformio run -platformio run -d ./test + +platformio run -e esp8266_tests -d test +platformio run -e esp32_tests -d test diff --git a/platformio.ini b/platformio.ini index adcf3495..785382f5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -21,20 +21,13 @@ src_filter = +<*> upload_speed = 921600 [env:esp8266] +extends = common platform = espressif8266 board = huzzah framework = arduino -lib_deps = ${common.lib_deps} -build_flags = ${common.build_flags} -src_filter = ${common.src_filter} -upload_speed = ${common.upload_speed} [env:esp32] +extends = common platform = espressif32 board = esp32dev framework = arduino -lib_deps = ${common.lib_deps} -build_flags = ${common.build_flags} -src_filter = ${common.src_filter} -upload_speed = ${common.upload_speed} - diff --git a/test/platformio.ini b/test/platformio.ini index 8a5b76b8..b78df8db 100644 --- a/test/platformio.ini +++ b/test/platformio.ini @@ -21,10 +21,22 @@ build_flags = -I../test -I../test/iot/ -I../src -I../src/lib -I../src/include/cp src_filter = +<../src> +<../test/iot> upload_speed = 921600 +[arduino] +framework = arduino + [esp8266] +platform = https://github.com/platformio/platform-espressif8266.git#feature/stage +board = huzzah build_flags = -D_GLIBCXX_USE_C99 board_build.f_cpu = 160000000L +[esp32] +platform = espressif32 +board = esp32dev + +[all_tests] +src_filter = +<../test> + [common_tests] src_filter = +<../test/common> @@ -151,465 +163,211 @@ src_filter = +<../test/transactions/transaction_to_json_type_3.cpp> [utils_tests] src_filter = +<../test/utils> +########################################################################### +# CI Configurations to build all tests without rebuilding framworks +# for every flashable test +# +# These tests will generate binaries that will either be too big to +# flash or will use too much memory and crash at runtime +# +# Use the individual test targets to test specific features on hardware +########################################################################### + +[env:esp8266_tests] +extends = base, arduino, esp8266 +src_filter = ${base.src_filter} ${all_tests.src_filter} + +[env:esp32_tests] +extends = base, arduino, esp32 +src_filter = ${base.src_filter} ${all_tests.src_filter} + +########################################################################### +########################################################################### + [env:esp8266_common_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${common_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_ecdsa_sign_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_ecdsa_sign_null_pk_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_pk_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_ecdsa_sign_null_hash32_sha256_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_hash32_sha256_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_ecdsa_sign_null_hash32_privatekey_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_hash32_privatekey_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_publickey_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_curve_publickey_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_verify_invalid_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_curve_verify_invalid_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_curve_verify_valid_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_curve_verify_valid_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_hash_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_hash_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_message_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_message_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_message_sign_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_message_sign_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_message_verify_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_message_verify_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_crypto_slot_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_slot_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_defaults_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${defaults_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_identities_address_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${identities_address_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_identities_keys_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${identities_keys_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_identities_keys_publickey_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${identities_keys_publickey_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_identities_privatekey_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${identities_privatekey_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_identities_publickey_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${identities_publickey_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_managers_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${managers_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_networks_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${networks_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_builder_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_builder_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_builder_transfer_custom_network_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_builder_transfer_custom_network_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_deserializer_delegate_registration_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_deserializer_delegate_registration_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_deserializer_multi_signature_registration_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_deserializer_multi_signature_registration_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_deserializer_second_signature_registration_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_deserializer_second_signature_registration_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_deserializer_transfer_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_deserializer_transfer_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_deserializer_vote_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_deserializer_vote_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_serializer_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_serializer_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_transaction_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_array_type_0_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_0_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_array_type_1_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_1_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_array_type_2_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_2_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_array_type_3_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_3_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_json_type_0_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_0_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_json_type_1_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_1_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_json_type_2_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_2_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_transactions_transaction_to_json_type_3_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_3_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp8266_utils_tests] -platform = https://github.com/platformio/platform-espressif8266.git#feature/stage -board = huzzah -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} ${esp8266.build_flags} +extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${utils_tests.src_filter} -upload_speed = ${base.upload_speed} -board_build.f_cpu = ${esp8266.board_build.f_cpu} [env:esp32_common_tests] -platform = espressif32 -board = esp32dev -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} +extends = base, arduino, esp32 src_filter = ${base.src_filter} ${common_tests.src_filter} -upload_speed = ${base.upload_speed} [env:esp32_crypto_tests] -platform = espressif32 -board = esp32dev -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} +extends = base, arduino, esp32 src_filter = ${base.src_filter} ${crypto_tests.src_filter} -upload_speed = ${base.upload_speed} [env:esp32_defaults_tests] -platform = espressif32 -board = esp32dev -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} +extends = base, arduino, esp32 src_filter = ${base.src_filter} ${defaults_tests.src_filter} -upload_speed = ${base.upload_speed} [env:esp32_identities_tests] -platform = espressif32 -board = esp32dev -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} +extends = base, arduino, esp32 src_filter = ${base.src_filter} ${identities_tests.src_filter} -upload_speed = ${base.upload_speed} [env:esp32_managers_tests] -platform = espressif32 -board = esp32dev -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} +extends = base, arduino, esp32 src_filter = ${base.src_filter} ${managers_tests.src_filter} -upload_speed = ${base.upload_speed} [env:esp32_networks_tests] -platform = espressif32 -board = esp32dev -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} +extends = base, arduino, esp32 src_filter = ${base.src_filter} ${networks_tests.src_filter} -upload_speed = ${base.upload_speed} [env:esp32_transactions_tests] -platform = espressif32 -board = esp32dev -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} +extends = base, arduino, esp32 src_filter = ${base.src_filter} ${transactions_tests.src_filter} -upload_speed = ${base.upload_speed} [env:esp32_utils_tests] -platform = espressif32 -board = esp32dev -framework = arduino -lib_deps = ${base.lib_deps} -build_flags = ${base.build_flags} +extends = base, arduino, esp32 src_filter = ${base.src_filter} ${utils_tests.src_filter} -upload_speed = ${base.upload_speed} -upload_port = /dev/ttyUSB4 From 98d585fb0e03d436f9e3337061880f6d57c41b64 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 29 Nov 2019 08:22:58 -0800 Subject: [PATCH 17/21] build: hide cmake warnings (#194) - ignores detached head warnings when cloning external libs from CMake. --- cmake/extern/ArduinoJson.txt.in | 1 + cmake/extern/BIP66.txt.in | 1 + cmake/extern/GTest.txt.in | 1 + cmake/extern/uECC.txt.in | 1 + 4 files changed, 4 insertions(+) diff --git a/cmake/extern/ArduinoJson.txt.in b/cmake/extern/ArduinoJson.txt.in index 76703973..6245050b 100644 --- a/cmake/extern/ArduinoJson.txt.in +++ b/cmake/extern/ArduinoJson.txt.in @@ -12,6 +12,7 @@ include(ExternalProject) ExternalProject_Add(ArduinoJson GIT_REPOSITORY https://github.com/bblanchon/ArduinoJson GIT_TAG v6.12.0 + GIT_CONFIG advice.detachedHead=false SOURCE_DIR "${EXTERNAL_LIBRARY_DIR}/arduinojson/src" BINARY_DIR "${EXTERNAL_LIBRARY_DIR}/arduinojson/build" CONFIGURE_COMMAND "" diff --git a/cmake/extern/BIP66.txt.in b/cmake/extern/BIP66.txt.in index 2516f897..10ae75c2 100644 --- a/cmake/extern/BIP66.txt.in +++ b/cmake/extern/BIP66.txt.in @@ -12,6 +12,7 @@ include(ExternalProject) ExternalProject_Add(BIP66 GIT_REPOSITORY https://github.com/sleepdefic1t/BIP66 GIT_TAG 0.2.1 + GIT_CONFIG advice.detachedHead=false SOURCE_DIR "${EXTERNAL_LIBRARY_DIR}/bip66/src" BINARY_DIR "${EXTERNAL_LIBRARY_DIR}/bip66/build" CONFIGURE_COMMAND "" diff --git a/cmake/extern/GTest.txt.in b/cmake/extern/GTest.txt.in index 1f7aa71c..e3aee00b 100644 --- a/cmake/extern/GTest.txt.in +++ b/cmake/extern/GTest.txt.in @@ -12,6 +12,7 @@ include(ExternalProject) ExternalProject_Add(GoogleTest GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG v1.10.x + GIT_CONFIG advice.detachedHead=false SOURCE_DIR "${EXTERNAL_LIBRARY_DIR}/googletest/src" BINARY_DIR "${EXTERNAL_LIBRARY_DIR}/googletest/build" CONFIGURE_COMMAND "" diff --git a/cmake/extern/uECC.txt.in b/cmake/extern/uECC.txt.in index e3a7cf08..57ff2ff8 100644 --- a/cmake/extern/uECC.txt.in +++ b/cmake/extern/uECC.txt.in @@ -12,6 +12,7 @@ include(ExternalProject) ExternalProject_Add(UECC GIT_REPOSITORY https://github.com/kmackay/micro-ecc GIT_TAG v1.0 + GIT_CONFIG advice.detachedHead=false SOURCE_DIR "${EXTERNAL_LIBRARY_DIR}/uecc/src" BINARY_DIR "${EXTERNAL_LIBRARY_DIR}/uecc/build" CONFIGURE_COMMAND "" From 09714c5f6f473ccced5f7b97e474c5293f02185f Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 30 Jan 2020 20:39:00 -0800 Subject: [PATCH 18/21] feat: implement AIP 11 (#198) --- .editorconfig | 2 +- .github/workflows/test/script_arduino.sh | 3 +- .github/workflows/test/script_desktop.sh | 2 + .github/workflows/test/script_platform_io.sh | 2 +- .gitignore | 4 +- cmake/External.cmake | 80 ++- cmake/extern/BCL.txt.in | 24 + cmake/extern/BIP66.txt.in | 2 +- docs/INSTALL_ARDUINO.MD | 276 ++++++-- docs/INSTALL_OS.md | 50 +- docs/INSTALL_PLATFORMIO.md | 52 +- docs/cpp.md | 626 +++++++++++++----- examples/arduino/ESP32/ESP32.ino | 371 ++++++----- examples/arduino/ESP32/arkCrypto_esp32.h | 6 +- examples/arduino/ESP8266/ESP8266.ino | 325 ++++----- examples/arduino/ESP8266/arkCrypto_esp8266.h | 6 +- extras/ARDUINO_IDE.sh | 51 +- library.json | 9 +- platformio.ini | 2 +- src/CMakeLists.txt | 81 ++- src/common/configuration.cpp | 39 +- src/common/network.cpp | 32 +- src/crypto/curve.cpp | 245 ++++--- src/crypto/curve.hpp | 54 +- src/crypto/hash.cpp | 36 +- src/crypto/hash.hpp | 13 +- src/crypto/message.cpp | 135 ++-- src/crypto/slot.cpp | 131 ++-- src/identities/address.cpp | 77 ++- src/identities/keys.cpp | 56 +- src/identities/privatekey.cpp | 39 +- src/identities/publickey.cpp | 53 +- src/identities/wif.cpp | 46 +- .../cpp-crypto/common/configuration.hpp | 24 +- src/include/cpp-crypto/common/fee_policy.hpp | 10 +- src/include/cpp-crypto/common/network.hpp | 22 +- src/include/cpp-crypto/crypto/message.hpp | 40 +- src/include/cpp-crypto/crypto/slot.hpp | 11 +- .../cpp-crypto/defaults/static_fees.hpp | 33 - .../cpp-crypto/defaults/transaction_types.hpp | 33 - src/include/cpp-crypto/identities/address.hpp | 42 +- src/include/cpp-crypto/identities/keys.hpp | 31 +- .../cpp-crypto/identities/privatekey.hpp | 25 +- .../cpp-crypto/identities/publickey.hpp | 26 +- src/include/cpp-crypto/identities/wif.hpp | 35 +- src/include/cpp-crypto/interfaces/constants.h | 91 +++ .../cpp-crypto/interfaces/identities.hpp | 83 ++- src/include/cpp-crypto/networks/devnet.hpp | 5 +- src/include/cpp-crypto/networks/mainnet.hpp | 5 +- src/include/cpp-crypto/networks/testnet.hpp | 5 +- src/include/cpp-crypto/transactions/builder.h | 95 --- .../transactions/builders/builder.hpp | 24 + .../builders/delegate_registration.hpp | 76 +++ .../transactions/builders/htlc_claim.hpp | 63 ++ .../transactions/builders/htlc_lock.hpp | 101 +++ .../transactions/builders/htlc_refund.hpp | 53 ++ .../cpp-crypto/transactions/builders/ipfs.hpp | 53 ++ .../transactions/builders/multi_payment.hpp | 80 +++ .../builders/second_signature.hpp | 55 ++ .../transactions/builders/transfer.hpp | 85 +++ .../cpp-crypto/transactions/builders/vote.hpp | 55 ++ .../cpp-crypto/transactions/deserializer.h | 52 -- .../cpp-crypto/transactions/serializer.h | 45 -- .../cpp-crypto/transactions/transaction.h | 94 --- .../cpp-crypto/transactions/transaction.hpp | 65 ++ .../transactions/transaction_data.hpp | 64 ++ src/lib/bcl/Base58Check.cpp | 248 ------- src/lib/bcl/Base58Check.hpp | 112 ---- src/lib/bcl/CurvePoint.cpp | 293 -------- src/lib/bcl/CurvePoint.hpp | 134 ---- src/lib/bcl/Ecdsa.cpp | 152 ----- src/lib/bcl/Ecdsa.hpp | 53 -- src/lib/bcl/FieldInt.cpp | 186 ------ src/lib/bcl/FieldInt.hpp | 111 ---- src/lib/bcl/Ripemd160.cpp | 143 ---- src/lib/bcl/Ripemd160.hpp | 55 -- src/lib/bcl/Sha256.cpp | 173 ----- src/lib/bcl/Sha256.hpp | 81 --- src/lib/bcl/Sha256Hash.cpp | 51 -- src/lib/bcl/Sha256Hash.hpp | 61 -- src/lib/bcl/Sha512.cpp | 167 ----- src/lib/bcl/Sha512.hpp | 78 --- src/lib/bcl/Uint256.cpp | 220 ------ src/lib/bcl/Uint256.hpp | 145 ---- src/lib/bcl/Utils.cpp | 47 -- src/lib/bcl/Utils.hpp | 47 -- src/managers/fee_manager.cpp | 47 -- src/managers/fee_manager.hpp | 44 +- src/managers/network_manager.cpp | 30 - src/managers/network_manager.hpp | 26 +- src/transactions/builder.cpp | 170 ----- src/transactions/builders/common.hpp | 198 ++++++ src/transactions/defaults/fees.hpp | 53 ++ src/transactions/defaults/offsets.hpp | 52 ++ src/transactions/defaults/types.hpp | 39 ++ src/transactions/deserializer.cpp | 533 ++++++++------- src/transactions/deserializer.hpp | 35 + src/transactions/mapping/json.cpp | 395 +++++++++++ src/transactions/mapping/json.hpp | 33 + src/transactions/mapping/labels.hpp | 61 ++ src/transactions/mapping/mapping.cpp | 200 ++++++ src/transactions/mapping/mapping.hpp | 30 + src/transactions/serializer.cpp | 498 ++++++++------ src/transactions/serializer.hpp | 45 ++ src/transactions/transaction.cpp | 534 ++++----------- src/transactions/types/assets.hpp | 51 ++ .../types/delegate_registration.cpp | 135 ++++ .../types/delegate_registration.hpp | 67 ++ .../types/delegate_resignation.hpp | 26 + src/transactions/types/htlc_claim.cpp | 136 ++++ src/transactions/types/htlc_claim.hpp | 60 ++ src/transactions/types/htlc_lock.cpp | 240 +++++++ src/transactions/types/htlc_lock.hpp | 66 ++ src/transactions/types/htlc_refund.cpp | 114 ++++ src/transactions/types/htlc_refund.hpp | 60 ++ src/transactions/types/ipfs.cpp | 122 ++++ src/transactions/types/ipfs.hpp | 61 ++ src/transactions/types/multi_payment.cpp | 263 ++++++++ src/transactions/types/multi_payment.hpp | 89 +++ src/transactions/types/second_signature.cpp | 117 ++++ src/transactions/types/second_signature.hpp | 59 ++ src/transactions/types/transfer.cpp | 152 +++++ src/transactions/types/transfer.hpp | 65 ++ src/transactions/types/vote.cpp | 128 ++++ src/transactions/types/vote.hpp | 64 ++ src/utils/base58.cpp | 187 ++++-- src/utils/base58.hpp | 35 +- src/utils/crypto_helpers.h | 51 +- src/utils/hex.hpp | 145 ++-- src/utils/json.h | 4 +- src/utils/packing.h | 58 ++ src/utils/platform.h | 31 + src/utils/str.hpp | 66 +- test/CMakeLists.txt | 138 ++-- test/common/configuration.cpp | 87 +-- test/common/fee_policy.cpp | 31 +- test/common/network.cpp | 61 +- test/crypto/curve_ecdsa_sign.cpp | 37 +- test/crypto/curve_ecdsa_sign_null_hash.cpp | 27 + ...urve_ecdsa_sign_null_hash32_privatekey.cpp | 22 - .../curve_ecdsa_sign_null_hash32_sha256.cpp | 20 - test/crypto/curve_ecdsa_sign_null_pk.cpp | 20 - .../curve_ecdsa_sign_null_privatekey.cpp | 27 + test/crypto/curve_publickey.cpp | 81 ++- test/crypto/curve_verify_invalid.cpp | 32 +- test/crypto/curve_verify_valid.cpp | 25 +- test/crypto/hash.cpp | 40 +- test/crypto/message.cpp | 38 +- test/crypto/message_sign.cpp | 26 +- test/crypto/message_verify.cpp | 27 +- test/crypto/slot.cpp | 33 +- test/defaults/static_fees.cpp | 41 -- test/defaults/transaction_types.cpp | 33 - test/fixtures/identity.hpp | 138 ++-- test/fixtures/message.hpp | 57 +- test/identities/address.cpp | 137 ++-- test/identities/keys.cpp | 103 +-- test/identities/keys_publickey.cpp | 26 +- test/identities/privatekey.cpp | 96 +-- test/identities/publickey.cpp | 110 +-- test/identities/wif.cpp | 115 ++-- test/managers/fee_manager.cpp | 95 +-- test/managers/network_manager.cpp | 37 +- test/networks/devnet.cpp | 32 +- test/networks/mainnet.cpp | 34 +- test/networks/testnet.cpp | 34 +- test/platformio.ini | 618 +++++++++++++---- test/test_helpers.h | 44 ++ test/transactions/builder.cpp | 35 - .../builder_transfer_custom_network.cpp | 34 - .../builders/delegate_registration.cpp | 50 ++ test/transactions/builders/htlc_claim.cpp | 37 ++ test/transactions/builders/htlc_lock.cpp | 58 ++ test/transactions/builders/htlc_refund.cpp | 36 + test/transactions/builders/ipfs.cpp | 36 + test/transactions/builders/multi_payment.cpp | 43 ++ .../builders/second_signature.cpp | 36 + test/transactions/builders/transfer.cpp | 147 ++++ test/transactions/builders/vote.cpp | 37 ++ test/transactions/defaults/fees_types.cpp | 33 + test/transactions/deserializer.cpp | 50 ++ .../deserializer_delegate_registration.cpp | 41 -- ...erializer_multi_signature_registration.cpp | 77 --- ...rializer_second_signature_registration.cpp | 44 -- test/transactions/deserializer_transfer.cpp | 36 - test/transactions/deserializer_vote.cpp | 43 -- test/transactions/serializer.cpp | 191 ++---- test/transactions/transaction.cpp | 305 +++++++-- .../transaction_to_array_type_0.cpp | 54 -- .../transaction_to_array_type_1.cpp | 59 -- .../transaction_to_array_type_2.cpp | 57 -- .../transaction_to_array_type_3.cpp | 64 -- .../transaction_to_json_type_0.cpp | 52 -- .../transaction_to_json_type_1.cpp | 54 -- .../transaction_to_json_type_2.cpp | 52 -- .../transaction_to_json_type_3.cpp | 58 -- test/transactions/transaction_v1.cpp | 29 + .../types/delegate_registration.cpp | 123 ++++ .../types/delegate_registration_v1.cpp | 95 +++ .../types/delegate_resignation.cpp | 75 +++ test/transactions/types/fixtures/common.hpp | 32 + .../types/fixtures/delegate_registration.hpp | 78 +++ .../fixtures/delegate_registration_v1.hpp | 69 ++ .../types/fixtures/delegate_resignation.hpp | 62 ++ .../types/fixtures/htlc_claim.hpp | 86 +++ .../transactions/types/fixtures/htlc_lock.hpp | 99 +++ .../types/fixtures/htlc_refund.hpp | 76 +++ test/transactions/types/fixtures/ipfs.hpp | 78 +++ .../types/fixtures/multi_payment.hpp | 518 +++++++++++++++ .../types/fixtures/second_signature.hpp | 78 +++ .../types/fixtures/second_signature_v1.hpp | 58 ++ test/transactions/types/fixtures/transfer.hpp | 142 ++++ .../types/fixtures/transfer_v1.hpp | 126 ++++ test/transactions/types/fixtures/vote.hpp | 78 +++ test/transactions/types/fixtures/vote_v1.hpp | 73 ++ test/transactions/types/htlc_claim.cpp | 111 ++++ test/transactions/types/htlc_lock.cpp | 139 ++++ test/transactions/types/htlc_refund.cpp | 97 +++ test/transactions/types/ipfs.cpp | 117 ++++ test/transactions/types/multi_payment.cpp | 160 +++++ test/transactions/types/second_signature.cpp | 100 +++ .../types/second_signature_v1.cpp | 78 +++ test/transactions/types/transfer.cpp | 187 ++++++ test/transactions/types/transfer_v1.cpp | 152 +++++ test/transactions/types/vote.cpp | 101 +++ test/transactions/types/vote_v1.cpp | 89 +++ test/utils/base58.cpp | 138 ++-- test/utils/crypto_helpers.cpp | 59 +- test/utils/hex.cpp | 91 +-- test/utils/packing.cpp | 72 ++ test/utils/str.cpp | 45 +- 231 files changed, 13306 insertions(+), 7489 deletions(-) create mode 100644 cmake/extern/BCL.txt.in delete mode 100644 src/include/cpp-crypto/defaults/static_fees.hpp delete mode 100644 src/include/cpp-crypto/defaults/transaction_types.hpp create mode 100644 src/include/cpp-crypto/interfaces/constants.h delete mode 100644 src/include/cpp-crypto/transactions/builder.h create mode 100644 src/include/cpp-crypto/transactions/builders/builder.hpp create mode 100644 src/include/cpp-crypto/transactions/builders/delegate_registration.hpp create mode 100644 src/include/cpp-crypto/transactions/builders/htlc_claim.hpp create mode 100644 src/include/cpp-crypto/transactions/builders/htlc_lock.hpp create mode 100644 src/include/cpp-crypto/transactions/builders/htlc_refund.hpp create mode 100644 src/include/cpp-crypto/transactions/builders/ipfs.hpp create mode 100644 src/include/cpp-crypto/transactions/builders/multi_payment.hpp create mode 100644 src/include/cpp-crypto/transactions/builders/second_signature.hpp create mode 100644 src/include/cpp-crypto/transactions/builders/transfer.hpp create mode 100644 src/include/cpp-crypto/transactions/builders/vote.hpp delete mode 100644 src/include/cpp-crypto/transactions/deserializer.h delete mode 100644 src/include/cpp-crypto/transactions/serializer.h delete mode 100644 src/include/cpp-crypto/transactions/transaction.h create mode 100644 src/include/cpp-crypto/transactions/transaction.hpp create mode 100644 src/include/cpp-crypto/transactions/transaction_data.hpp delete mode 100644 src/lib/bcl/Base58Check.cpp delete mode 100644 src/lib/bcl/Base58Check.hpp delete mode 100644 src/lib/bcl/CurvePoint.cpp delete mode 100644 src/lib/bcl/CurvePoint.hpp delete mode 100644 src/lib/bcl/Ecdsa.cpp delete mode 100644 src/lib/bcl/Ecdsa.hpp delete mode 100644 src/lib/bcl/FieldInt.cpp delete mode 100644 src/lib/bcl/FieldInt.hpp delete mode 100644 src/lib/bcl/Ripemd160.cpp delete mode 100644 src/lib/bcl/Ripemd160.hpp delete mode 100644 src/lib/bcl/Sha256.cpp delete mode 100644 src/lib/bcl/Sha256.hpp delete mode 100644 src/lib/bcl/Sha256Hash.cpp delete mode 100644 src/lib/bcl/Sha256Hash.hpp delete mode 100644 src/lib/bcl/Sha512.cpp delete mode 100644 src/lib/bcl/Sha512.hpp delete mode 100644 src/lib/bcl/Uint256.cpp delete mode 100644 src/lib/bcl/Uint256.hpp delete mode 100644 src/lib/bcl/Utils.cpp delete mode 100644 src/lib/bcl/Utils.hpp delete mode 100644 src/managers/fee_manager.cpp delete mode 100644 src/managers/network_manager.cpp delete mode 100644 src/transactions/builder.cpp create mode 100644 src/transactions/builders/common.hpp create mode 100644 src/transactions/defaults/fees.hpp create mode 100644 src/transactions/defaults/offsets.hpp create mode 100644 src/transactions/defaults/types.hpp create mode 100644 src/transactions/deserializer.hpp create mode 100644 src/transactions/mapping/json.cpp create mode 100644 src/transactions/mapping/json.hpp create mode 100644 src/transactions/mapping/labels.hpp create mode 100644 src/transactions/mapping/mapping.cpp create mode 100644 src/transactions/mapping/mapping.hpp create mode 100644 src/transactions/serializer.hpp create mode 100644 src/transactions/types/assets.hpp create mode 100644 src/transactions/types/delegate_registration.cpp create mode 100644 src/transactions/types/delegate_registration.hpp create mode 100644 src/transactions/types/delegate_resignation.hpp create mode 100644 src/transactions/types/htlc_claim.cpp create mode 100644 src/transactions/types/htlc_claim.hpp create mode 100644 src/transactions/types/htlc_lock.cpp create mode 100644 src/transactions/types/htlc_lock.hpp create mode 100644 src/transactions/types/htlc_refund.cpp create mode 100644 src/transactions/types/htlc_refund.hpp create mode 100644 src/transactions/types/ipfs.cpp create mode 100644 src/transactions/types/ipfs.hpp create mode 100644 src/transactions/types/multi_payment.cpp create mode 100644 src/transactions/types/multi_payment.hpp create mode 100644 src/transactions/types/second_signature.cpp create mode 100644 src/transactions/types/second_signature.hpp create mode 100644 src/transactions/types/transfer.cpp create mode 100644 src/transactions/types/transfer.hpp create mode 100644 src/transactions/types/vote.cpp create mode 100644 src/transactions/types/vote.hpp create mode 100644 src/utils/packing.h create mode 100644 src/utils/platform.h create mode 100644 test/crypto/curve_ecdsa_sign_null_hash.cpp delete mode 100644 test/crypto/curve_ecdsa_sign_null_hash32_privatekey.cpp delete mode 100644 test/crypto/curve_ecdsa_sign_null_hash32_sha256.cpp delete mode 100644 test/crypto/curve_ecdsa_sign_null_pk.cpp create mode 100644 test/crypto/curve_ecdsa_sign_null_privatekey.cpp delete mode 100644 test/defaults/static_fees.cpp delete mode 100644 test/defaults/transaction_types.cpp create mode 100644 test/test_helpers.h delete mode 100644 test/transactions/builder.cpp delete mode 100644 test/transactions/builder_transfer_custom_network.cpp create mode 100644 test/transactions/builders/delegate_registration.cpp create mode 100644 test/transactions/builders/htlc_claim.cpp create mode 100644 test/transactions/builders/htlc_lock.cpp create mode 100644 test/transactions/builders/htlc_refund.cpp create mode 100644 test/transactions/builders/ipfs.cpp create mode 100644 test/transactions/builders/multi_payment.cpp create mode 100644 test/transactions/builders/second_signature.cpp create mode 100644 test/transactions/builders/transfer.cpp create mode 100644 test/transactions/builders/vote.cpp create mode 100644 test/transactions/defaults/fees_types.cpp create mode 100644 test/transactions/deserializer.cpp delete mode 100644 test/transactions/deserializer_delegate_registration.cpp delete mode 100644 test/transactions/deserializer_multi_signature_registration.cpp delete mode 100644 test/transactions/deserializer_second_signature_registration.cpp delete mode 100644 test/transactions/deserializer_transfer.cpp delete mode 100644 test/transactions/deserializer_vote.cpp delete mode 100644 test/transactions/transaction_to_array_type_0.cpp delete mode 100644 test/transactions/transaction_to_array_type_1.cpp delete mode 100644 test/transactions/transaction_to_array_type_2.cpp delete mode 100644 test/transactions/transaction_to_array_type_3.cpp delete mode 100644 test/transactions/transaction_to_json_type_0.cpp delete mode 100644 test/transactions/transaction_to_json_type_1.cpp delete mode 100644 test/transactions/transaction_to_json_type_2.cpp delete mode 100644 test/transactions/transaction_to_json_type_3.cpp create mode 100644 test/transactions/transaction_v1.cpp create mode 100644 test/transactions/types/delegate_registration.cpp create mode 100644 test/transactions/types/delegate_registration_v1.cpp create mode 100644 test/transactions/types/delegate_resignation.cpp create mode 100644 test/transactions/types/fixtures/common.hpp create mode 100644 test/transactions/types/fixtures/delegate_registration.hpp create mode 100644 test/transactions/types/fixtures/delegate_registration_v1.hpp create mode 100644 test/transactions/types/fixtures/delegate_resignation.hpp create mode 100644 test/transactions/types/fixtures/htlc_claim.hpp create mode 100644 test/transactions/types/fixtures/htlc_lock.hpp create mode 100644 test/transactions/types/fixtures/htlc_refund.hpp create mode 100644 test/transactions/types/fixtures/ipfs.hpp create mode 100644 test/transactions/types/fixtures/multi_payment.hpp create mode 100644 test/transactions/types/fixtures/second_signature.hpp create mode 100644 test/transactions/types/fixtures/second_signature_v1.hpp create mode 100644 test/transactions/types/fixtures/transfer.hpp create mode 100644 test/transactions/types/fixtures/transfer_v1.hpp create mode 100644 test/transactions/types/fixtures/vote.hpp create mode 100644 test/transactions/types/fixtures/vote_v1.hpp create mode 100644 test/transactions/types/htlc_claim.cpp create mode 100644 test/transactions/types/htlc_lock.cpp create mode 100644 test/transactions/types/htlc_refund.cpp create mode 100644 test/transactions/types/ipfs.cpp create mode 100644 test/transactions/types/multi_payment.cpp create mode 100644 test/transactions/types/second_signature.cpp create mode 100644 test/transactions/types/second_signature_v1.cpp create mode 100644 test/transactions/types/transfer.cpp create mode 100644 test/transactions/types/transfer_v1.cpp create mode 100644 test/transactions/types/vote.cpp create mode 100644 test/transactions/types/vote_v1.cpp create mode 100644 test/utils/packing.cpp diff --git a/.editorconfig b/.editorconfig index ec3ed6f4..72adc943 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,5 +6,5 @@ charset = utf-8 end_of_line = lf insert_final_newline = true indent_style = space -indent_size = 2 +indent_size = 4 trim_trailing_whitespace = true diff --git a/.github/workflows/test/script_arduino.sh b/.github/workflows/test/script_arduino.sh index 74554488..6c3ff57b 100755 --- a/.github/workflows/test/script_arduino.sh +++ b/.github/workflows/test/script_arduino.sh @@ -6,7 +6,8 @@ mkdir -p ~/Arduino/libraries/cpp-crypto/ mv ${GITHUB_WORKSPACE}/* ~/Arduino/libraries/cpp-crypto/ arduino-cli lib install "ArduinoJson@6.12.0" -arduino-cli lib install "BIP66@0.2.0" +arduino-cli lib install "bcl@0.0.5" +arduino-cli lib install "BIP66@0.3.2" arduino-cli lib install "micro-ecc@1.0.0" arduino-cli compile --output temp.bin -b esp32:esp32:esp32 ~/Arduino/libraries/cpp-crypto/examples/arduino/ESP32/ESP32.ino --debug diff --git a/.github/workflows/test/script_desktop.sh b/.github/workflows/test/script_desktop.sh index 1b353277..782b4d05 100755 --- a/.github/workflows/test/script_desktop.sh +++ b/.github/workflows/test/script_desktop.sh @@ -1,3 +1,5 @@ +#!/usr/bin/env bash + # run desktop builds cmake . -DCMAKE_BUILD_TYPE=Coverage cmake --build . diff --git a/.github/workflows/test/script_platform_io.sh b/.github/workflows/test/script_platform_io.sh index bd89913d..99cc21e0 100755 --- a/.github/workflows/test/script_platform_io.sh +++ b/.github/workflows/test/script_platform_io.sh @@ -3,5 +3,5 @@ # run PlatformIO builds platformio run -platformio run -e esp8266_tests -d test +# platformio run -e esp8266_tests -d test platformio run -e esp32_tests -d test diff --git a/.gitignore b/.gitignore index aa9f2108..33476db1 100644 --- a/.gitignore +++ b/.gitignore @@ -12,8 +12,8 @@ /include/uECC /lib/ /bin/ -build -extern +./build +./extern /src/ark_cpp_crypto.vcxproj.filters /src/ark_cpp_crypto.vcxproj /src/ark_cpp_crypto.sln diff --git a/cmake/External.cmake b/cmake/External.cmake index bf288e0c..d6745d58 100644 --- a/cmake/External.cmake +++ b/cmake/External.cmake @@ -3,26 +3,26 @@ # ArduinoJson: https://github.com/bblanchon/ArduinoJson # ------------------------------------------------------------------------------ -# Set the configuration +# Copy the configuration configure_file(${CMAKE_SOURCE_DIR}/cmake/extern/ArduinoJson.txt.in ${EXTERNAL_LIBRARY_DIR}/arduinojson/CMakeLists.txt) -# Execute Git Clone and run Cmake +# Configure the Cmake build execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . RESULT_VARIABLE result WORKING_DIRECTORY ${EXTERNAL_LIBRARY_DIR}/arduinojson) if(result) - message(FATAL_ERROR "ArduinoJson: CMake Failed: ${result}") + message(FATAL_ERROR "ArduinoJson: CMake Configuration Failed: ${result}") endif() -# Build the Cloned Repo +# Execute Git Clone and run Cmake execute_process(COMMAND ${CMAKE_COMMAND} --build . RESULT_VARIABLE result WORKING_DIRECTORY ${EXTERNAL_LIBRARY_DIR}/arduinojson) if(result) - message(FATAL_ERROR "ArduinoJson: Build Failed: ${result}") + message(FATAL_ERROR "ArduinoJson: Clone and Build Step Failed: ${result}") endif() set(ARDUINO_JSON_SOURCE_DIR @@ -33,41 +33,80 @@ include_directories(${ARDUINO_JSON_SOURCE_DIR}) # ------------------------------------------------------------------------------ +# ------------------------------------------------------------------------------ +# BCL: https://github.com/sleepdefic1t/bcl +# ------------------------------------------------------------------------------ + +# Copy the configuration +configure_file(${CMAKE_SOURCE_DIR}/cmake/extern/BCL.txt.in + ${EXTERNAL_LIBRARY_DIR}/bcl/CMakeLists.txt) + +# Configure the Cmake build +execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE result + WORKING_DIRECTORY ${EXTERNAL_LIBRARY_DIR}/bcl) + +if(result) + message(FATAL_ERROR "BCL: CMake Configuration Failed: ${result}") +endif() + +# Execute Git Clone and run Cmake +execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE result + WORKING_DIRECTORY ${EXTERNAL_LIBRARY_DIR}/bcl) + +if(result) + message(FATAL_ERROR "BIP66: Clone and Build Step Failed: ${result}") +endif() + +set(BCL_SOURCE + ${EXTERNAL_LIBRARY_DIR}/bcl/src/src/Base58Check.cpp + ${EXTERNAL_LIBRARY_DIR}/bcl/src/src/CurvePoint.cpp + ${EXTERNAL_LIBRARY_DIR}/bcl/src/src/Ecdsa.cpp + ${EXTERNAL_LIBRARY_DIR}/bcl/src/src/FieldInt.cpp + ${EXTERNAL_LIBRARY_DIR}/bcl/src/src/Ripemd160.cpp + ${EXTERNAL_LIBRARY_DIR}/bcl/src/src/Sha256.cpp + ${EXTERNAL_LIBRARY_DIR}/bcl/src/src/Sha256Hash.cpp + ${EXTERNAL_LIBRARY_DIR}/bcl/src/src/Uint256.cpp + ${EXTERNAL_LIBRARY_DIR}/bcl/src/src/Utils.cpp + CACHE INTERNAL "BCL: Source Files" +) + +include_directories(${EXTERNAL_LIBRARY_DIR}/bcl/src/src) + +# ------------------------------------------------------------------------------ + # ------------------------------------------------------------------------------ # BIP66: https://github.com/sleepdefic1t/BIP66 # ------------------------------------------------------------------------------ -# Set the configuration +# Copy the configuration configure_file(${CMAKE_SOURCE_DIR}/cmake/extern/BIP66.txt.in ${EXTERNAL_LIBRARY_DIR}/bip66/CMakeLists.txt) -# Execute Git Clone and run Cmake +# Configure the Cmake build execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . RESULT_VARIABLE result WORKING_DIRECTORY ${EXTERNAL_LIBRARY_DIR}/bip66) if(result) - message(FATAL_ERROR "BIP66: CMake Failed: ${result}") + message(FATAL_ERROR "BIP66: CMake Configuration Failed: ${result}") endif() -# Build the Cloned Repo +# Execute Git Clone and run Cmake execute_process(COMMAND ${CMAKE_COMMAND} --build . RESULT_VARIABLE result WORKING_DIRECTORY ${EXTERNAL_LIBRARY_DIR}/bip66) if(result) - message(FATAL_ERROR "BIP66: Build Failed: ${result}") + message(FATAL_ERROR "BIP66: Clone and Build Step Failed: ${result}") endif() - -add_subdirectory("${EXTERNAL_LIBRARY_DIR}/bip66/src/src" - "${EXTERNAL_LIBRARY_DIR}/bip66/build" - EXCLUDE_FROM_ALL) set(BIP66_SOURCE ${EXTERNAL_LIBRARY_DIR}/bip66/src/src/bip66.cpp CACHE INTERNAL "BIP66: Source Files") -include_directories(${BIP66_SOURCE_DIR}) +include_directories(${EXTERNAL_LIBRARY_DIR}/bip66/src/) # ------------------------------------------------------------------------------ @@ -75,26 +114,26 @@ include_directories(${BIP66_SOURCE_DIR}) # uECC: https://github.com/kmackay/micro-ecc # ------------------------------------------------------------------------------ -# Set the configuration +# Copy the configuration configure_file(${CMAKE_SOURCE_DIR}/cmake/extern/uECC.txt.in ${EXTERNAL_LIBRARY_DIR}/uecc/CMakeLists.txt) -# Execute Git Clone and run Cmake +# Configure the Cmake build execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . RESULT_VARIABLE result WORKING_DIRECTORY ${EXTERNAL_LIBRARY_DIR}/uecc) if(result) - message(FATAL_ERROR "uECC: CMake Failed: ${result}") + message(FATAL_ERROR "uECC: CMake Configuration Failed: ${result}") endif() -# Build the Cloned Repo +# Execute Git Clone and run Cmake execute_process(COMMAND ${CMAKE_COMMAND} --build . RESULT_VARIABLE result WORKING_DIRECTORY ${EXTERNAL_LIBRARY_DIR}/uecc) if(result) - message(FATAL_ERROR "uECC: Build Failed: ${result}") + message(FATAL_ERROR "uECC: Clone and Build Step Failed: ${result}") endif() set(UECC_SOURCE_DIR @@ -114,6 +153,7 @@ include_directories(${UECC_SOURCE_DIR}) # ------------------------------------------------------------------------------ set(EXTERNAL_LIBRARY_SOURCE + ${BCL_SOURCE} ${BIP66_SOURCE} ${UECC_SOURCE} CACHE INTERNAL "External Lib Binary Files") diff --git a/cmake/extern/BCL.txt.in b/cmake/extern/BCL.txt.in new file mode 100644 index 00000000..0ef3a7c4 --- /dev/null +++ b/cmake/extern/BCL.txt.in @@ -0,0 +1,24 @@ + +cmake_minimum_required(VERSION 3.2) + +project(bcl-download) + +include(ExternalProject) + +# ------------------------------------------------------------------------------ +# BCL: https://github.com/sleepdefic1t/bcl +# ------------------------------------------------------------------------------ + +ExternalProject_Add(BCL + GIT_REPOSITORY https://github.com/sleepdefic1t/bcl + GIT_TAG 0.0.5 + GIT_CONFIG advice.detachedHead=false + SOURCE_DIR "${EXTERNAL_LIBRARY_DIR}/bcl/src" + BINARY_DIR "${EXTERNAL_LIBRARY_DIR}/bcl/build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) + +# ------------------------------------------------------------------------------ diff --git a/cmake/extern/BIP66.txt.in b/cmake/extern/BIP66.txt.in index 10ae75c2..dbd5aea1 100644 --- a/cmake/extern/BIP66.txt.in +++ b/cmake/extern/BIP66.txt.in @@ -11,7 +11,7 @@ include(ExternalProject) ExternalProject_Add(BIP66 GIT_REPOSITORY https://github.com/sleepdefic1t/BIP66 - GIT_TAG 0.2.1 + GIT_TAG 0.3.2 GIT_CONFIG advice.detachedHead=false SOURCE_DIR "${EXTERNAL_LIBRARY_DIR}/bip66/src" BINARY_DIR "${EXTERNAL_LIBRARY_DIR}/bip66/build" diff --git a/docs/INSTALL_ARDUINO.MD b/docs/INSTALL_ARDUINO.MD index 8de30f86..dd80ba9e 100644 --- a/docs/INSTALL_ARDUINO.MD +++ b/docs/INSTALL_ARDUINO.MD @@ -1,68 +1,268 @@ +# Arduino IDE -# Arduino Builds +Download and install the Arduino IDE (>=1.8.10) from the following link: +* `https://www.arduino.cc/en/Main/Software` -### requirements: +## Dependencies -**Arduino IDE:** -Download and install the Arduino IDE (>=1.8.5) from the following link: -```https://www.arduino.cc/en/Main/Software``` +Using the Arduino IDE's built in Library Manager, install the following Libraries: -# +* [`ArduinoJson@6.12.0`](https://github.com/bblanchon/ArduinoJson) +* [`bcl@0.0.5`](https://github.com/sleepdefic1t/bcl) +* [`BIP66@0.3.2`](https://github.com/sleepdefic1t/BIP66) +* [`micro-ecc@1.0.0`](https://github.com/kmackay/micro-ecc) -### dependencies: +## Arduino Example using the Adafruit ESP32 Feather -Using the Arduino IDE's built in Library Manager, -install the following Libraries -- micro-ecc -- AUnit -- ArduinoJson +--- -# +> `extras/ESP32/arkCrypto_esp32.h` -### Using with the Arduino IDE -> include the following header in your Arduino Sketch: ```cpp -#include +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_CRYPTO_ESP32_H +#define ARK_CRYPTO_ESP32_H + +#include "common/configuration.hpp" +#include "common/network.hpp" + +#include "crypto/message.hpp" + +#include "identities/address.hpp" +#include "identities/privatekey.hpp" +#include "identities/publickey.hpp" +#include "identities/wif.hpp" + +#include "transactions/builders/transfer.hpp" +#include "transactions/transaction.hpp" + +#endif ``` -# +--- -### Arduino Example using the Adafruit Feather ESP8266 +> `extras/ESP32/ESP32.ino` ```cpp -#include "arkCrypto.h" +/** + * This file is part of Ark C++ Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +//////////////////////////////////////////////////////////////////////////////// +// ESP32 C++ Crypto Usage Example Sketch +// +// This sketch covers how to use the C++ Crypto library. +// It allows your ESP32 use Ark Ecosystem cryptographic protocols. + +//////////////////////////////////////////////////////////////////////////////// +// NOTE: +// +// If this Repo was Cloned, run the 'ARDUINO_IDE.sh' script first. +// It's in the 'extras/' folder and extends compatability to the Arduino IDE. + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// This is where you include the 'arkCrypto.h' header. +// This allows your project to use Ark C++ Crypto. +// This header is empty and is just to force the inclusion +// of the library into the Arduino sketch + +#include + +//////////////////////////////////////////////////////////////////////////////// +// This is a helper header that includes all the required Ark +// headers required for this sketch. + +#include "arkCrypto_esp32.h" + +//////////////////////////////////////////////////////////////////////////////// +// Misc ARK C++ Crypto headers + +#include "networks/devnet.hpp" + +#include "utils/hex.hpp" +#include "utils/str.hpp" -void setup() -{ - Serial.begin(115200); +//////////////////////////////////////////////////////////////////////////////// +// The Main Arduino Header - Address address = Address::fromPassphrase(passphrase, networkVersion); - Serial.println(address.toString().c_str()); +#include - PrivateKey privateKey("950981ce17df662dbc1d25305f8597a71309fb8f7232203a0944477e2534b021"); - Serial.println(privateKey.toString().c_str()); +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Namespaces - const auto passphrase = "bullet parade snow bacon mutual deposit brass floor staff list concert ask"; +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; +using namespace Ark::Crypto::transactions; - PublicKey publicKey = PublicKey::fromPassphrase(passphrase); - Serial.println(publicKey.toString().c_str()); +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Sketch Constants - const uint8_t wifByte = 0xaa; - WIF wif = WIF::fromPassphrase(passphrase, wifByte); - Serial.println(wif.toString().c_str()); +constexpr auto Passphrase = "this is a top secret passphrase"; +constexpr auto SecondPassphrase = "this is a top secret passphrase too"; +constexpr auto MessageText = "Hello World"; +constexpr auto WifByte = 0xaa; // Devnet - const auto actual = Ark::Crypto::Transactions::Builder::buildTransfer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", 100000000, "", "Secret passphrase"); - Serial.println(actual.type); +//////////////////////////////////////////////////////////////////////////////// +// Create a PrivateKey using a 12-word Passphrase. +// +// Given the passphrase "this is a top secret passphrase", +// the computed PrivateKey is: +// - "d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712". +// +// --- +void createPrivateKey() { + const auto privateKey = PrivateKey::fromPassphrase(Passphrase); + const auto privateKeyString = privateKey.toString(); + + printf("\n\nPrivateKey from Passphrase: %s\n\n", privateKeyString.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Create a Wif(Wallet Import Format) PrivateKey. +// +// Uses a 12-word Passphrase and a Network Wif-byte. +// +// Given the passphrase "this is a top secret passphrase", +// and the Network byte 0xaa(Devnet), +// the computed Wif is: "SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA". +// +// --- +void createWif() { + const auto wif = Wif::fromPassphrase(Passphrase, WifByte); + const auto wifString = wif.toString(); + + printf("\n\nWIF from Passphrase: %s\n\n", wifString.c_str()); +} +//////////////////////////////////////////////////////////////////////////////// +// Create a PublicKey using a 12-word Passphrase. +// +// Given the passphrase "this is a top secret passphrase", +// the computed PublicKey is: +// - "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192". +// +// --- +void createPublicKey() { + const auto publicKey = PublicKey::fromPassphrase(Passphrase); + const auto publicKeyString = publicKey.toString(); - const auto text = "Computer science is no more about computers than astronomy is about telescopes."; - Ark::Crypto::Message message; - message.sign(text, passphrase); - Serial.println(BytesToHex(message.signature).c_str()); + printf("\n\nPublicKey from Passphrase: %s\n\n", publicKeyString.c_str()); } +//////////////////////////////////////////////////////////////////////////////// +// Create a Wallet Address using a 12-word Passphrase and a Network Version. +// +// - Ark Devnet is '0x1E' +// - Ark Mainnet is '0x17'. +// +// Given the passphrase "this is a top secret passphrase" using Devnet(0x1E), +// the computed ARK Address is: +// - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib" +// +// --- +void createAddress() { + const auto address = Address::fromPassphrase(Passphrase, Devnet.version); + const auto addressString = address.toString(); + + printf("\n\nARK Address: %s\n\n", addressString.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Check the Transactions Fees +// --- +void checkFees() { + Configuration cfg; + + auto i = 0; + for (auto &fee : cfg.getPolicy()) { + printf("\n\nType %d fee: %s\n", i, UintToString(fee).c_str()); + ++i; + } + + // or get the Fee by Transaction Type + printf("\n\nType 0 default fee: %s\n\n", UintToString(cfg.getFee(0)).c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Sign a Message using a 12-word Passphrase and Verify it. +// +// Given the text "Hello World", +// and the passphrase "this is a top secret passphrase", +// the computed 'Signature" is: +// - "304402200fb4adddd1f1d652b544ea6ab62828a0a65b712ed447e2538db0caebfa68929e02205ecb2e1c63b29879c2ecf1255db506d671c8b3fa6017f67cfd1bf07e6edd1cc8". +// +// --- +void signMessage() { + Message message; + message.sign(MessageText, Passphrase); + + const auto signatureString = BytesToHex(message.signature); + printf("\n\nSignature from Signed Message: %s\n", signatureString.c_str()); + + const bool isValid = message.verify(); + printf("\nMessage Signature is valid: %s\n\n", isValid ? "true" : "false"); +} + +//////////////////////////////////////////////////////////////////////////////// +// Create a BridgeChain transaction, tailored for a custom network. +void createBridgechainTransaction() { + + // Custom Bridgechain Network + const Network BridgechainNetwork = { + "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", + 1, 0x53, 0xaa, + "2019-04-12T13:00:00.000Z" + }; + + // Load the Custom Network Configuration + const Configuration cfg(BridgechainNetwork); + + // Use the Transaction Builder to make a transaction. + const auto bridgechainTransaction = builder::Transfer() + .recipientId("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib") + .vendorField("this is a custom bridgechain transaction") + .sign(Passphrase) + .secondSign(SecondPassphrase) + .build(cfg); + + // Create and Print the Json representation of the Transaction. + const auto transactionJson = bridgechainTransaction.toJson(); + printf("\n\nBridgechain Transaction: %s\n\n", transactionJson.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +void setup() { + Serial.begin(115200); + + createPrivateKey(); + createWif(); + createPublicKey(); + createAddress(); + checkFees(); + signMessage(); + createBridgechainTransaction(); +} + +//////////////////////////////////////////////////////////////////////////////// void loop() {} + ``` diff --git a/docs/INSTALL_OS.md b/docs/INSTALL_OS.md index 3d91d542..d8b3345f 100644 --- a/docs/INSTALL_OS.md +++ b/docs/INSTALL_OS.md @@ -1,35 +1,43 @@ +# Desktop/Server -# OS Builds +Linux, macOS and Windows. -### dependencies +## Requirements -**CMake:** -> Use an installer package from the following link: -> ```https://www.cmake.org/download/``` +**CMake:** -or -**Homebrew:** -> `brew install cmake` +Installing Cmake: -# +* Linux: + * `sudo apt-get -y install cmake` +* macOS: + * `brew install cmake` +* Windows Installer Pkg: + * `https://www.cmake.org/download/` -> note: all other dependencies will be automatically installed via CMake and Hunter Package Manager. +## Dependencies -# +The following external libraries are cloned during the build process: + +* [`ArduinoJson@6.12.0`](https://github.com/bblanchon/ArduinoJson) +* [`bcl@0.0.5`](https://github.com/sleepdefic1t/bcl) +* [`BIP66@0.3.2`](https://github.com/sleepdefic1t/BIP66) +* [`micro-ecc@1.0.0`](https://github.com/kmackay/micro-ecc) + +## Building ### make and build -**For Linux/Mac** -> `./build.sh` -**For Windows** -> `./build.cmd` +* `cd cpp-crypto` +* `mkdir build && cd build` +* `cmake -DUNIT_TEST=ON ..` +* `cmake --build .` + +### run tests (Linux, macOS) -# +* `./test/ark_cpp_crypto_tests` -### run tests -**For Linux/Mac** -> `./run_tests.sh` +### run tests (Windows) -**For Windows** -> `./run_tests.cmd` +* `.\test\Debug\ark_cpp_crypto_tests.exe` diff --git a/docs/INSTALL_PLATFORMIO.md b/docs/INSTALL_PLATFORMIO.md index f1eab693..0ca22dd7 100644 --- a/docs/INSTALL_PLATFORMIO.md +++ b/docs/INSTALL_PLATFORMIO.md @@ -1,39 +1,35 @@ +# PlatformIO IDE -# PlatformIO +## Requirements -### dependencies +### Python -**Python:** -Use an installer package from the following link: -> ```https://www.python.org/downloads/``` +* Linux: + * `sudo apt-get install python` +* macOS: + * `brew install python` +* Windows Installer Pkg: + * `https://www.python.org/downloads/windows/` -# +### PlatformIO -**PlatformIO:** -install PlatformIO if not already installed -> ```pip install -U platformio``` -or -> ```python -c "$(curl -fsSL > https://raw.githubusercontent.com/platformio/platformio/develop/scripts/get-platformio.py)"``` +* Linux/macOS/Windows using PIP: + * `pip install -U platformio` +* Linux/macOS/Windows using cURL: + * `python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/develop/scripts/get-platformio.py)"` +## Dependencies -also install platformio dependencies: -> install AUnit (2778), micro-ecc (1665) ArduinoJson@6.10.0 libraries ->```platformio lib -g install 2778 1665 ArduinoJson@6.10.0``` +The following external libraries are fetched during the build process: -# +* [`ArduinoJson@6.12.0`](https://github.com/bblanchon/ArduinoJson) +* [`bcl@0.0.5`](https://github.com/sleepdefic1t/bcl) +* [`BIP66@0.3.2`](https://github.com/sleepdefic1t/BIP66) +* [`micro-ecc@1.0.0`](https://github.com/kmackay/micro-ecc) -### running the tests on an Arduino board using PlatformIO +## Build and Run Live Tests on the ESP32 -**`cd` into this directory "*.../Cpp-Crypto/test*"** -> ```cd test``` - -**execute the following command to upload test to your board** - ->| board | command | ->|:-- |:-- | ->| ESP8266 | ```pio run -e esp8266 -t upload``` | ->| ESP32 | ```pio run -e esp32 -t upload``` | - -> ^ the above runs the tests rooted in the following file: -> "*.../cpp-crypto/test/IoT/test_main.cpp*" +> | board | command | +> | :---- | :------ | +> | ESP32 | `pio run -e esp32_tests -d test/ -t upload` | diff --git a/docs/cpp.md b/docs/cpp.md index a2db207f..b3fa67c6 100644 --- a/docs/cpp.md +++ b/docs/cpp.md @@ -2,7 +2,7 @@ title: "Cpp" --- -# Cpp +# C++ ::: warning This project is still under development. This page will get more content as the project evolves. In the meantime you can view its source on [Github](https://github.com/ArkEcosystem/cpp-crypto/). @@ -10,111 +10,200 @@ This project is still under development. This page will get more content as the [[toc]] -## Installation -* [Arduino](#Arduino) -* [Linux >= 16.04](#OS) -* [macOS >= 10.10](#OS) -* [Windows >= 7](#OS) +## Installation -### ARK Transactions +* [Arduino IDE](#Arduino-IDE) +* [PlatformIO IDE](#PlatformIO-IDE) +* [Desktop/Server](#Desktop-Server) + * **Linux >= 16.04** + * **macOS >= 10.10** + * **Windows >= 7** -#### Devnet +## ARK Transactions + +### Devnet ```cpp -const auto DevnetTransaction = Builder::buildTransfer( - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 100000000ULL, - "this is a devnet transaction", - "this is a top secret passphrase", - "this is a top secret passphrase too"); +// Default Configuration is Devnet +const auto transaction = builder::Transfer() + .type(0) + .nonce(1) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .vendorField("this is a devnet transaction") + .amount(100000000ULL) + .expiration(0) + .recipientId("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib") + .sign("this is a top secret passphrase") + .secondSign("this is a top secret passphrase too") + .build(); ``` -#### Mainnet +### Mainnet ```cpp -const Configuration MainnetConfiguration(Mainnet); - -const auto MainnetTransaction = Builder::buildTransfer( - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 100000000ULL, - "this is a mainnet transaction", - "this is a top secret passphrase", - "this is a top secret passphrase too", - MainnetConfiguration); +// Use the Configuration Class to create a Mainnet Transaction +const Configuration mainnetCfg(Mainnet); + +const auto transaction = builder::Transfer() + .type(0) + .nonce(1) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .vendorField("this is a devnet transaction") + .amount(100000000ULL) + .expiration(0) + .recipientId("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib") + .sign("this is a top secret passphrase") + .secondSign("this is a top secret passphrase too") + .build(mainnetCfg); ``` -#### BridgeChain Transaction +### BridgeChain Transaction ```cpp -static const Network MyBridgechainNetwork = { - "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", - 1, 0x53, 0xaa, - "2019-04-12T13:00:00.000Z" +const Network Radians = { + "f39a61f04d6136a690a0b675ef6eedbd053665bd343b4e4f03311f12065fb875", + 1, 0xCE, 0x41, + "2019-10-25T09:05:40.856Z" }; -const Configuration MyBridgechainConfiguration(MyBridgechainNetwork); - -const auto MyBridgechainTransaction = Builder::buildTransfer( - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 100000000ULL, - "this is a custom bridgechain transaction", - "this is a top secret passphrase", - "this is a top secret passphrase too", - MyBridgechainConfiguration); +const Configuration radiansCfg(Radians); + +const std::array senderPublicKeyBytes = { + { 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, 57, 79, 134, 53, + 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, 231, 240, 174, 209, + 146 }}; + +const uint8_t radiansRecipient[] = { + 65, 29, 252, 105, 181, 76, 127, 233, 1, 233, 29, + 90, 154, 183, 131, 136, 100, 94, 36, 39, 234 }; + +auto transaction = builder::Transfer() + .type(0) + .nonce(1) + .senderPublicKey(senderPublicKeyBytes.data()) + .vendorField("this is a Radians transaction") + .amount(100000000ULL) + .expiration(0) + .recipientId(radiansRecipient) + .sign("this is a top secret passphrase") + .secondSign("this is a top secret passphrase too") + .build(radiansCfg); ``` - -#### With custom Fees +### With custom Fees ```cpp -static const Network MyBridgechainNetwork = { - "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", - 1, 0x53, 0xaa, - "2019-04-12T13:00:00.000Z" +const Network Radians = { + "f39a61f04d6136a690a0b675ef6eedbd053665bd343b4e4f03311f12065fb875", + 1, 0xCE, 0x41, + "2019-10-25T09:05:40.856Z" }; -const FeePolicy MyCustomFees = { +const FeePolicy customFees = { 900000000ULL, 800000000ULL, 700000000ULL, 600000000ULL, 500000000ULL, 400000000ULL, 300000000ULL, 200000000ULL, 100000000ULL, 0ULL }; -ßconst Configuration MyBridgechainConfiguration(MyBridgechainNetwork, - MyCustomFees); - -const auto MyBridgechainTransaction = Builder::buildTransfer( - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 100000000ULL, - "", - "this is a top secret passphrase", - "", - MyBridgechainConfiguration); +const Configuration customCfg(Radians, MyCustomFees); + +auto transaction = builder::Transfer() + .type(0) + .nonce(1) + .senderPublicKey(senderPublicKeyBytes.data()) + .vendorField("this is a Radians transaction") + .amount(100000000ULL) + .expiration(0) + .recipientId(radiansRecipient) + .sign("this is a top secret passphrase") + .secondSign("this is a top secret passphrase too") + .build(customCfg); ``` -### Sign +### Sign a Transactions ```cpp -const auto text = "Computer science is no more about computers than astronomy is about telescopes."; -const auto passphrase = "viable weasel wage promote praise inflict jaguar tackle color unusual exclude direct"; - -Ark::Crypto::Message message; -message.sign(text, passphrase); +// ff02170100000000000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192809698000000000000010000000000000000000000171dfc69b54c7fe901e91d5a9ab78388645e2427ea3045022100d55051f5aec7d894afb987d582e63e0157b7a6e0cb8eff1d70ac81cbbb1382e90220294b89acb106e811b48a072a555915bea4aa0fc63c4210ad90115d1220b31b7d +const std::vector transferBytes = { + 255, 2, 23, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 128, 150, 152, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 29, 252, 105, + 181, 76, 127, 233, 1, 233, 29, 90, 154, 183, 131, 136, 100, 94, 36, + 39, 234, 48, 69, 2, 33, 0, 213, 80, 81, 245, 174, 199, 216, 148, + 175, 185, 135, 213, 130, 230, 62, 1, 87, 183, 166, 224, 203, 142, 255, + 29, 112, 172, 129, 203, 187, 19, 130, 233, 2, 32, 41, 75, 137, 172, + 177, 6, 232, 17, 180, 138, 7, 42, 85, 89, 21, 190, 164, 170, 15, + 198, 60, 66, 16, 173, 144, 17, 93, 18, 32, 179, 27, 125 }; + +TransactionData data; + +Deserializer::deserialize(&data, transferBytes); + +transaction.sign("this is a top secret passphrase"); ``` ### Serialize (AIP11) ```cpp - +// ff02170100000000000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192809698000000000000010000000000000000000000171dfc69b54c7fe901e91d5a9ab78388645e2427ea3045022100d55051f5aec7d894afb987d582e63e0157b7a6e0cb8eff1d70ac81cbbb1382e90220294b89acb106e811b48a072a555915bea4aa0fc63c4210ad90115d1220b31b7d +const std::vector transferBytes = { + 255, 2, 23, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 128, 150, 152, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 29, 252, 105, + 181, 76, 127, 233, 1, 233, 29, 90, 154, 183, 131, 136, 100, 94, 36, + 39, 234, 48, 69, 2, 33, 0, 213, 80, 81, 245, 174, 199, 216, 148, + 175, 185, 135, 213, 130, 230, 62, 1, 87, 183, 166, 224, 203, 142, 255, + 29, 112, 172, 129, 203, 187, 19, 130, 233, 2, 32, 41, 75, 137, 172, + 177, 6, 232, 17, 180, 138, 7, 42, 85, 89, 21, 190, 164, 170, 15, + 198, 60, 66, 16, 173, 144, 17, 93, 18, 32, 179, 27, 125 }; + +TransactionData data; + +Deserializer::deserialize(&data, transferBytes); ``` ### Deserialize (AIP11) ```cpp +const std::array senderPublicKeyBytes = { + { 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, 57, 79, 134, 53, + 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, 231, 240, 174, 209, + 146 }}; + +// base58Decode(AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff) +constexpr uint8_t recipientId[] = { + 23, 29, 252, 105, 181, 76, 127, 233, 1, 233, 29, + 90, 154, 183, 131, 136, 100, 94, 36, 39, 234 }; + +TransactionData data; + +data.network = 23U; +data.typeGroup = 1UL; +data.type = 0UL; +data.nonce = 1ULL; + +std::copy(senderPublicKeyBytes, + senderPublicKeyBytes.end(), + data.senderPublicKey.begin()); + +data.fee = 10000000ULL; + +data.asset.transfer.amount = 100000ULL; +data.asset.transfer.expiration = 0UL; + +std::copy_n(recipientId, + ADDRESS_HASH_LEN, + data.asset.transfer.recipientId.begin()); +const auto serialized = Serializer::serialize(data); ``` ## Message -### Sign +### Sign a Message ```cpp const auto text = "Computer science is no more about computers than astronomy is about telescopes."; @@ -130,11 +219,7 @@ const auto text = "Computer science is no more about computers than astronomy is PublicKey publicKey = PublicKey::fromHex("0275776018638e5c40f1b922901e96cac2caa734585ef302b4a2801ee9a338a456"); std::vector signature = HexToBytes("3044022021704f2adb2e4a10a3ddc1d7d64552b8061c05f6d12a168c69091c75581d611402200edf37689d2786fc690af9f0f6fa1f629c95695039f648a6d455484302402e93"); -Ark::Crypto::Message message( - text, - publicKey, - signature -); +const auto message = Ark::Crypto::Message(text, publicKey, signature); message.verify(); ``` @@ -144,6 +229,7 @@ message.verify(); ### Address #### Get an address from a passphrase + ```cpp const auto passphrase = "bullet parade snow bacon mutual deposit brass floor staff list concert ask"; const uint8_t networkVersion = 0x1E; @@ -151,6 +237,7 @@ Address address = Address::fromPassphrase(passphrase, networkVersion); ``` #### Get an address from a public key + ```cpp PublicKey publicKey("029fdf41a7d69d8efc7b236c21b9509a23d862ea4ed8b13a56e31eee58dbfd97b4"); const uint8_t networkVersion = 0x1E; @@ -158,6 +245,7 @@ Address address = Address::fromPublicKey(publicKey, networkVersion); ``` #### Get an address from a private key + ```cpp PrivateKey privateKey("950981ce17df662dbc1d25305f8597a71309fb8f7232203a0944477e2534b021"); const uint8_t networkVersion = 0x1E; @@ -165,6 +253,7 @@ Address address = Address::fromPrivateKey(privateKey, networkVersion); ``` #### Validate an address + ```cpp Address address("DStZXkgpEjxbG355nQ26vnkp95p24U9tsV"); bool isValidAddress = Address::validate(address, networkVersion); @@ -173,158 +262,391 @@ bool isValidAddress = Address::validate(address, networkVersion); ### Private Key #### Get a private key from a passphrase + ```cpp const auto passphrase = "bullet parade snow bacon mutual deposit brass floor staff list concert ask"; -PrivateKey privateKey = PrivateKey::fromPassphrase(passphrase); +const auto privateKey = PrivateKey::fromPassphrase(passphrase); ``` #### Get a private key instance object from hex + ```cpp -PrivateKey privateKey = PrivateKey::fromHex("950981ce17df662dbc1d25305f8597a71309fb8f7232203a0944477e2534b021"); +const auto privateKey = PrivateKey::fromHex("950981ce17df662dbc1d25305f8597a71309fb8f7232203a0944477e2534b021"); ``` ### Public Key #### Get a public key from a passphrase + ```cpp const auto passphrase = "bullet parade snow bacon mutual deposit brass floor staff list concert ask"; -PublicKey publicKey = PublicKey::fromPassphrase(passphrase); +const auto publicKey = PublicKey::fromPassphrase(passphrase); ``` #### Get a public key instance object from hex + ```cpp -PublicKey publicKey = PublicKey::fromHex("029fdf41a7d69d8efc7b236c21b9509a23d862ea4ed8b13a56e31eee58dbfd97b4"); +const auto publicKey = PublicKey::fromHex("029fdf41a7d69d8efc7b236c21b9509a23d862ea4ed8b13a56e31eee58dbfd97b4"); ``` ### WIF #### Get a WIF from a passphrase + ```cpp const auto passphrase = "bullet parade snow bacon mutual deposit brass floor staff list concert ask"; const uint8_t wifByte = 0xaa; -WIF wif = WIF::fromPassphrase(passphrase, wifByte); +const auto wif = WIF::fromPassphrase(passphrase, wifByte); ``` -# +--- + +## Arduino IDE + +Download and install the Arduino IDE (>=1.8.10) from the following link: -# Arduino -**Arduino IDE:** -Download and install the Arduino IDE (>=1.8.5) from the following link: -```https://www.arduino.cc/en/Main/Software``` +* `https://www.arduino.cc/en/Main/Software` -Using the Arduino IDE's built in Library Manager, -install the following Libraries: -```micro-ecc``` -```AUnit``` +### Dependencies -#### Ensure all git submodules have been cloned -##### Note that future goals include ensuring that all dependent libaries are registered in the PIO Library Manager to avoid the use of git submodules for PIO builds +Using the Arduino IDE's built in Library Manager, install the following Libraries: - cd Cpp-Crypto - git submodule update --init --recursive +* [`ArduinoJson@6.12.0`](https://github.com/bblanchon/ArduinoJson) +* [`bcl@0.0.5`](https://github.com/sleepdefic1t/bcl) +* [`BIP66@0.3.2`](https://github.com/sleepdefic1t/BIP66) +* [`micro-ecc@1.0.0`](https://github.com/kmackay/micro-ecc) + +### Arduino Example using the Adafruit ESP32 Feather + +--- -#### Arduino Example using the Adafruit Feather ESP8266 +> `extras/ESP32/arkCrypto_esp32.h` ```cpp -#include "arkCrypto.h" +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ -void setup() -{ - Serial.begin(115200); +#ifndef ARK_CRYPTO_ESP32_H +#define ARK_CRYPTO_ESP32_H + +#include "common/configuration.hpp" +#include "common/network.hpp" + +#include "crypto/message.hpp" + +#include "identities/address.hpp" +#include "identities/privatekey.hpp" +#include "identities/publickey.hpp" +#include "identities/wif.hpp" + +#include "transactions/builders/transfer.hpp" +#include "transactions/transaction.hpp" + +#endif +``` + +--- + +> `extras/ESP32/ESP32.ino` + +```cpp +/** + * This file is part of Ark C++ Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +//////////////////////////////////////////////////////////////////////////////// +// ESP32 C++ Crypto Usage Example Sketch +// +// This sketch covers how to use the C++ Crypto library. +// It allows your ESP32 use Ark Ecosystem cryptographic protocols. + +//////////////////////////////////////////////////////////////////////////////// +// NOTE: +// +// If this Repo was Cloned, run the 'ARDUINO_IDE.sh' script first. +// It's in the 'extras/' folder and extends compatability to the Arduino IDE. + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// This is where you include the 'arkCrypto.h' header. +// This allows your project to use Ark C++ Crypto. +// This header is empty and is just to force the inclusion +// of the library into the Arduino sketch + +#include + +//////////////////////////////////////////////////////////////////////////////// +// This is a helper header that includes all the required Ark +// headers required for this sketch. + +#include "arkCrypto_esp32.h" + +//////////////////////////////////////////////////////////////////////////////// +// Misc ARK C++ Crypto headers + +#include "networks/devnet.hpp" + +#include "utils/hex.hpp" +#include "utils/str.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// The Main Arduino Header + +#include + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Namespaces + +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Sketch Constants + +constexpr auto Passphrase = "this is a top secret passphrase"; +constexpr auto SecondPassphrase = "this is a top secret passphrase too"; +constexpr auto MessageText = "Hello World"; + +constexpr auto WifByte = 0xaa; // Devnet + +//////////////////////////////////////////////////////////////////////////////// +// Create a PrivateKey using a 12-word Passphrase. +// +// Given the passphrase "this is a top secret passphrase", +// the computed PrivateKey is: +// - "d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712". +// +// --- +void createPrivateKey() { + const auto privateKey = PrivateKey::fromPassphrase(Passphrase); + const auto privateKeyString = privateKey.toString(); + + printf("\n\nPrivateKey from Passphrase: %s\n\n", privateKeyString.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Create a Wif(Wallet Import Format) PrivateKey. +// +// Uses a 12-word Passphrase and a Network Wif-byte. +// +// Given the passphrase "this is a top secret passphrase", +// and the Network byte 0xaa(Devnet), +// the computed Wif is: "SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA". +// +// --- +void createWif() { + const auto wif = Wif::fromPassphrase(Passphrase, WifByte); + const auto wifString = wif.toString(); + + printf("\n\nWIF from Passphrase: %s\n\n", wifString.c_str()); +} - const auto passphrase = "bullet parade snow bacon mutual deposit brass floor staff list concert ask"; - const uint8_t networkVersion = 0x1E; - Address address = Address::fromPassphrase(passphrase, networkVersion); - .println(address.toString().c_str()); +//////////////////////////////////////////////////////////////////////////////// +// Create a PublicKey using a 12-word Passphrase. +// +// Given the passphrase "this is a top secret passphrase", +// the computed PublicKey is: +// - "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192". +// +// --- +void createPublicKey() { + const auto publicKey = PublicKey::fromPassphrase(Passphrase); + const auto publicKeyString = publicKey.toString(); + + printf("\n\nPublicKey from Passphrase: %s\n\n", publicKeyString.c_str()); +} - PrivateKey privateKey("950981ce17df662dbc1d25305f8597a71309fb8f7232203a0944477e2534b021"); - Serial.println(privateKey.toString().c_str()); +//////////////////////////////////////////////////////////////////////////////// +// Create a Wallet Address using a 12-word Passphrase and a Network Version. +// +// - Ark Devnet is '0x1E' +// - Ark Mainnet is '0x17'. +// +// Given the passphrase "this is a top secret passphrase" using Devnet(0x1E), +// the computed ARK Address is: +// - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib" +// +// --- +void createAddress() { + const auto address = Address::fromPassphrase(Passphrase, Devnet.version); + const auto addressString = address.toString(); + + printf("\n\nARK Address: %s\n\n", addressString.c_str()); +} - const auto passphrase = "bullet parade snow bacon mutual deposit brass floor staff list concert ask"; +//////////////////////////////////////////////////////////////////////////////// +// Check the Transactions Fees +// --- +void checkFees() { + Configuration cfg; - PublicKey publicKey = PublicKey::fromPassphrase(passphrase); - Serial.println(publicKey.toString().c_str()); + auto i = 0; + for (auto &fee : cfg.getPolicy()) { + printf("\n\nType %d fee: %s\n", i, UintToString(fee).c_str()); + ++i; + } - const uint8_t wifByte = 0xaa; - WIF wif = WIF::fromPassphrase(passphrase, wifByte); - Serial.println(wif.toString().c_str()); + // or get the Fee by Transaction Type + printf("\n\nType 0 default fee: %s\n\n", UintToString(cfg.getFee(0)).c_str()); +} +//////////////////////////////////////////////////////////////////////////////// +// Sign a Message using a 12-word Passphrase and Verify it. +// +// Given the text "Hello World", +// and the passphrase "this is a top secret passphrase", +// the computed 'Signature" is: +// - "304402200fb4adddd1f1d652b544ea6ab62828a0a65b712ed447e2538db0caebfa68929e02205ecb2e1c63b29879c2ecf1255db506d671c8b3fa6017f67cfd1bf07e6edd1cc8". +// +// --- +void signMessage() { + Message message; + message.sign(MessageText, Passphrase); + + const auto signatureString = BytesToHex(message.signature); + printf("\n\nSignature from Signed Message: %s\n", signatureString.c_str()); + + const bool isValid = message.verify(); + printf("\nMessage Signature is valid: %s\n\n", isValid ? "true" : "false"); +} - const auto actual = Ark::Crypto::Transactions::Builder::buildTransfer( - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 100000000, - "", - "Secret passphrase" - ); - Serial.println(actual.type); +//////////////////////////////////////////////////////////////////////////////// +// Create a BridgeChain transaction, tailored for a custom network. +void createBridgechainTransaction() { + + // Custom Bridgechain Network + const Network BridgechainNetwork = { + "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", + 1, 0x53, 0xaa, + "2019-04-12T13:00:00.000Z" + }; + + // Load the Custom Network Configuration + const Configuration cfg(BridgechainNetwork); + + // Use the Transaction Builder to make a transaction. + const auto bridgechainTransaction = builder::Transfer() + .recipientId("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib") + .vendorField("this is a custom bridgechain transaction") + .sign(Passphrase) + .secondSign(SecondPassphrase) + .build(cfg); + + // Create and Print the Json representation of the Transaction. + const auto transactionJson = bridgechainTransaction.toJson(); + printf("\n\nBridgechain Transaction: %s\n\n", transactionJson.c_str()); +} +//////////////////////////////////////////////////////////////////////////////// +void setup() { + Serial.begin(115200); - const auto text = "Computer science is no more about computers than astronomy is about telescopes."; - Ark::Crypto::Message message; - message.sign(text, passphrase); - Serial.println(BytesToHex(message.signature).c_str()); + createPrivateKey(); + createWif(); + createPublicKey(); + createAddress(); + checkFees(); + signMessage(); + createBridgechainTransaction(); } +//////////////////////////////////////////////////////////////////////////////// void loop() {} + ``` -**PlatformIO IDE:** +--- -#### Python: -Use an installer package from the following link or use your preferred method to install Python: -```https://www.python.org/downloads/``` +## PlatformIO IDE -Install PlatformIO: +### Requirements - pip install -U platformio -or +#### Python - python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/develop/scripts/get-platformio.py)" +* Linux: + * `sudo apt-get install python` +* macOS: + * `brew install python` +* Windows Installer Pkg: + * `https://www.python.org/downloads/windows/` -Install AUnit (2778), micro-ecc (1665) +#### PlatformIO - platformio lib -g install 2778 1665 +* Linux/macOS/Windows using PIP: + * `pip install -U platformio` +* Linux/macOS/Windows using cURL: + * `python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/develop/scripts/get-platformio.py)"` + +### Dependencies + +The following external libraries are fetched during the build process: + +* [`ArduinoJson@6.12.0`](https://github.com/bblanchon/ArduinoJson) +* [`bcl@0.0.5`](https://github.com/sleepdefic1t/bcl) +* [`BIP66@0.3.2`](https://github.com/sleepdefic1t/BIP66) +* [`micro-ecc@1.0.0`](https://github.com/kmackay/micro-ecc) + +### Build and Run Live Tests on the ESP32 + +>| board | command | +>| :---- | :------ | +>| ESP32 | `pio run -e esp32_tests -d test/ -t upload` | + +--- -#### Ensure all git submodules have been cloned -##### Note that future goals include ensuring that all dependent libaries are registered in the PIO Library Manager to avoid the use of git submodules for PIO builds +## Desktop/Server - cd Cpp-Crypto - git submodule update --init --recursive +Linux, macOS and Windows. -#### running the tests on an Arduino board +### Requirements - cd test +**CMake:** -#### execute the following command to upload test to your board +Installing Cmake: ->| board | command | ->|:-- |:-- | ->| ESP8266 | ```pio run -e esp8266 -t upload``` | ->| ESP32 | ```pio run -e esp32 -t upload``` | +* Linux: + * `sudo apt-get -y install cmake` +* macOS: + * `brew install cmake` +* Windows Installer Pkg: + * `https://www.cmake.org/download/` -# +### Dependencies -# OS -## Linux, macOS and Windows +The following external libraries are cloned during the build process: -**CMake:** +* [`ArduinoJson@6.12.0`](https://github.com/bblanchon/ArduinoJson) +* [`bcl@0.0.5`](https://github.com/sleepdefic1t/bcl) +* [`BIP66@0.3.2`](https://github.com/sleepdefic1t/BIP66) +* [`micro-ecc@1.0.0`](https://github.com/kmackay/micro-ecc) -Use an installer package from the following link, Homebrew, or use your preferred method: -```https://www.cmake.org/download/``` +### Building -using -**Homebrew:** +#### make and build - brew install cmake +* `cd cpp-crypto` +* `mkdir build && cd build` +* `cmake -DUNIT_TEST=ON ..` +* `cmake --build .` -> note: all other dependencies will be automatically installed via CMake and Hunter Package Manager. +#### run tests (Linux, macOS) -### make and build - cd cpp-crypto - cmake . && cmake --build . +* `./test/ark_cpp_crypto_tests` -### run tests (Linux, macOS) - ./test/Ark-Cpp-Crypto-tests +#### run tests (Windows) -### run tests (Windows) - .\test\Debug\Ark-Cpp-Crypto-tests.exe +* `.\test\Debug\ark_cpp_crypto_tests.exe` diff --git a/examples/arduino/ESP32/ESP32.ino b/examples/arduino/ESP32/ESP32.ino index 9f48f287..d7eff74f 100644 --- a/examples/arduino/ESP32/ESP32.ino +++ b/examples/arduino/ESP32/ESP32.ino @@ -1,5 +1,5 @@ /** - * This file is part of Ark Cpp Crypto. + * This file is part of Ark C++ Crypto. * * (c) Ark Ecosystem * @@ -7,189 +7,204 @@ * file that was distributed with this source code. **/ -/** - * ESP32 Cpp-Crypto Usage Example Sketch - * - * This sketch covers how to use the Cpp-Crypto library. - * It allows your ESP32 use Ark Ecosystem cryptographic protocols. - */ - - /** - * NOTE: At the time of this writing, the Cpp-Crypto library requires running the 'ARDUINO_IDE.sh' bash script located in the 'extras' folder. - * This converts our library to be compatible with the Arduino IDE. - */ - -/****************************************/ +//////////////////////////////////////////////////////////////////////////////// +// ESP32 C++ Crypto Usage Example Sketch +// +// This sketch covers how to use the C++ Crypto library. +// It allows your ESP32 use Ark Ecosystem cryptographic protocols. + +//////////////////////////////////////////////////////////////////////////////// +// NOTE: +// +// If this Repo was Cloned, run the 'ARDUINO_IDE.sh' script first. +// It's in the 'extras/' folder and extends compatability to the Arduino IDE. + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// This is where you include the 'arkCrypto.h' header. +// This allows your project to use Ark C++ Crypto. +// This header is empty and is just to force the inclusion +// of the library into the Arduino sketch -/** - * This is where you include the 'arkCrypto.h' header. - * This allows your project to use Ark Cpp-Crypto. - * This header is empty and is just to force the inclusion - * of the library into the Arduino sketch - */ #include -/**/ -/** - * This is a helper header that includes all the required Ark - * headers required for this sketch. -*/ +//////////////////////////////////////////////////////////////////////////////// +// This is a helper header that includes all the required Ark +// headers required for this sketch. + #include "arkCrypto_esp32.h" -using namespace Ark::Crypto; -using namespace Ark::Crypto::identities; -using namespace Ark::Crypto::Transactions; -/** - * This is a small hex helper header included in ARK Cpp-Crypto - */ +//////////////////////////////////////////////////////////////////////////////// +// Misc ARK C++ Crypto headers + +#include "networks/devnet.hpp" + #include "utils/hex.hpp" -/**/ +#include "utils/str.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// The Main Arduino Header #include -/****************************************/ - -void checkCrypto() { - - /** - * Create a BridgeChain transaction, tailored for your custom network. - */ - static const Network MyBridgechainNetwork = { - "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", - 1, 0x53, 0xaa, - "2019-04-12T13:00:00.000Z" - }; - - const Configuration MyBridgechainConfiguration(MyBridgechainNetwork); - - auto myBridgechainTransaction = Builder::buildTransfer( - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 100000000ULL, - "this is a custom bridgechain transaction", - "this is a top secret passphrase", - "this is a top secret passphrase too", - MyBridgechainConfiguration); - - Serial.print("\nBridgechain Transaction: "); - Serial.println(myBridgechainTransaction.toJson().c_str()); - - /**/ - - /********************/ - - /** - * This is how you can check the default 'Network' "Transaction 'Fees' by type. - * In this example, it should return a 'uint64_t' integer of '10000000' as the default 'Fee' for a 'Transaction' of 'Type' '0'. - */ - Configuration config; - unsigned long typeZeroTransactionFee = config.getFee(0); - Serial.print("\nType 0 default Transaction Fee: "); - Serial.println(typeZeroTransactionFee); // The response is a 'uint64_t' integer. - - /**/ - - /********************/ - - /** - * The following methods allows you to create an ARK address. - * This is done by passing a 12-word 'Passphrase' and the 'Network' 'Version' "byte". - * The 'Version" "byte" is a BASE58 P2PKH byte. Ark Devnet is '0x1E'; Ark Mainnet is '0x17'. - * - * Given the passphrase "this is a top secret passphrase", - * and the 'Devnet' 'Version' byte (0x1E); the ARK Address should be "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib" - */ - const auto passphrase = "this is a top secret passphrase"; - const uint8_t networkVersion = 0x1E; - - Address arkAddress = Address::fromPassphrase(passphrase, networkVersion); - Serial.print("\nARK Address: "); - Serial.println(arkAddress.toString().c_str()); // The 'Address' object is a type. Use 'toString()' to view the output. Arduino requires a 'c_str()' to 'print'. - /**/ - - - /********************/ - - /** - * The following methods allows create a 'PrivateKey'. - * This is done by passing a 12-word 'Passphrase'. - * - * Given the passphrase "this is a top secret passphrase", - * the 'PrivateKey" should be "d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712". - * This is a 'SHA256' of your "Passphrase". - */ - const auto passphrase2 = "this is a top secret passphrase"; - PrivateKey privateKeyFromPassphrase = PrivateKey::fromPassphrase(passphrase2); - Serial.print("\nPrivateKey from Passphrase: "); - Serial.println(privateKeyFromPassphrase.toString().c_str()); // The 'PrivateKey' object is a type. Use 'toString()' to view the output. Arduino requires a 'c_str()' to 'print'. - /**/ - - /********************/ - - /** - * The following methods allows create a 'PublicKey'. - * This is done by passing a 12-word 'Passphrase'. - * - * Given the passphrase "this is a top secret passphrase", - * the 'PublicKey" should be "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192". - */ - const auto passphrase3 = "this is a top secret passphrase"; - PublicKey publicKeyFromPassphrase = PublicKey::fromPassphrase(passphrase3); - Serial.print("\nPublicKey from Passphrase: "); - Serial.println(publicKeyFromPassphrase.toString().c_str()); // the 'PublicKey' object is a type. Use 'toString()' to view the output. Arduino requires a 'c_str()' to 'print'. - /**/ - - /********************/ - - /** - * The following methods allows create a 'WIF'-style "PrivateKey". - * 'WIF' stands for "Wallet Import Format" - * This is done by passing a 12-word 'Passphrase' and the 'Network' 'WIF' "byte". - * The 'WIF" "byte" is a BASE58 WIF byte. Ark Devnet is '0xaa'; Ark Mainnet is also '0xaa'. - - * - * Given the passphrase "this is a top secret passphrase", - * and the 'Devnet' 'WIF' byte (0xaa); - * The 'WIF" should be "SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA". - */ - const auto passphrase4 = "this is a top secret passphrase"; - const uint8_t wifByte = 0xaa; - Wif wifFromPassphrase = Wif::fromPassphrase(passphrase4, wifByte); - Serial.print("\nWIF from Passphrase: "); - Serial.println(wifFromPassphrase.toString().c_str()); // the 'WIF' object is a type. Use 'toString()' to view the output. Arduino requires a 'c_str()' to 'print'. - /**/ - - /********************/ - - /** - * The following methods allows you to 'Sign' a text 'Message'. - * This is done by passing the text to be signed, and a 12-word 'Passphrase'. - * - * Given the text "Hello World", - * and the passphrase "this is a top secret passphrase", - * The 'Signature" should be "304402200fb4adddd1f1d652b544ea6ab62828a0a65b712ed447e2538db0caebfa68929e02205ecb2e1c63b29879c2ecf1255db506d671c8b3fa6017f67cfd1bf07e6edd1cc8". - */ - const auto text = "Hello World"; - const auto passphrase5 = "this is a top secret passphrase"; - Ark::Crypto::Message message; - message.sign(text, passphrase5); - Serial.print("\nSignature from Signed Message: "); - Serial.println(BytesToHex(message.signature).c_str()); - // Additionally, you can verify the message. - bool isValid = message.verify(); - Serial.print("\nSigned Message Signature is Verified: "); - Serial.println(isValid); - /**/ -}; - -/****************************************/ - -void setup() -{ - Serial.begin(115200); // Begin your Serial Connection. This allows you to monitor your boards output. - - checkCrypto(); // Begin Crypto example usage. -}; - -/****************************************/ - -void loop() {}; // We can leave this empty, as we don't want to repeat anything in this example. +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Namespaces + +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Sketch Constants + +constexpr auto Passphrase = "this is a top secret passphrase"; +constexpr auto SecondPassphrase = "this is a top secret passphrase too"; +constexpr auto MessageText = "Hello World"; + +constexpr auto WifByte = 0xaa; // Devnet + +//////////////////////////////////////////////////////////////////////////////// +// Create a PrivateKey using a 12-word Passphrase. +// +// Given the passphrase "this is a top secret passphrase", +// the computed PrivateKey is: +// - "d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712". +// +// --- +void createPrivateKey() { + const auto privateKey = PrivateKey::fromPassphrase(Passphrase); + const auto privateKeyString = privateKey.toString(); + + printf("\n\nPrivateKey from Passphrase: %s\n\n", privateKeyString.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Create a Wif(Wallet Import Format) PrivateKey. +// +// Uses a 12-word Passphrase and a Network Wif-byte. +// +// Given the passphrase "this is a top secret passphrase", +// and the Network byte 0xaa(Devnet), +// the computed Wif is: "SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA". +// +// --- +void createWif() { + const auto wif = Wif::fromPassphrase(Passphrase, WifByte); + const auto wifString = wif.toString(); + + printf("\n\nWIF from Passphrase: %s\n\n", wifString.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Create a PublicKey using a 12-word Passphrase. +// +// Given the passphrase "this is a top secret passphrase", +// the computed PublicKey is: +// - "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192". +// +// --- +void createPublicKey() { + const auto publicKey = PublicKey::fromPassphrase(Passphrase); + const auto publicKeyString = publicKey.toString(); + + printf("\n\nPublicKey from Passphrase: %s\n\n", publicKeyString.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Create a Wallet Address using a 12-word Passphrase and a Network Version. +// +// - Ark Devnet is '0x1E' +// - Ark Mainnet is '0x17'. +// +// Given the passphrase "this is a top secret passphrase" using Devnet(0x1E), +// the computed ARK Address is: +// - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib" +// +// --- +void createAddress() { + const auto address = Address::fromPassphrase(Passphrase, Devnet.version); + const auto addressString = address.toString(); + + printf("\n\nARK Address: %s\n\n", addressString.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Check the Transactions Fees +// --- +void checkFees() { + Configuration cfg; + + auto i = 0; + for (auto &fee : cfg.getPolicy()) { + printf("\n\nType %d fee: %s\n", i, UintToString(fee).c_str()); + ++i; + } + + // or get the Fee by Transaction Type + printf("\n\nType 0 default fee: %s\n\n", UintToString(cfg.getFee(0)).c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Sign a Message using a 12-word Passphrase and Verify it. +// +// Given the text "Hello World", +// and the passphrase "this is a top secret passphrase", +// the computed 'Signature" is: +// - "304402200fb4adddd1f1d652b544ea6ab62828a0a65b712ed447e2538db0caebfa68929e02205ecb2e1c63b29879c2ecf1255db506d671c8b3fa6017f67cfd1bf07e6edd1cc8". +// +// --- +void signMessage() { + Message message; + message.sign(MessageText, Passphrase); + + const auto signatureString = BytesToHex(message.signature); + printf("\n\nSignature from Signed Message: %s\n", signatureString.c_str()); + + const bool isValid = message.verify(); + printf("\nMessage Signature is valid: %s\n\n", isValid ? "true" : "false"); +} + +//////////////////////////////////////////////////////////////////////////////// +// Create a BridgeChain transaction, tailored for a custom network. +void createBridgechainTransaction() { + + // Custom Bridgechain Network + const Network BridgechainNetwork = { + "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", + 1, 0x53, 0xaa, + "2019-04-12T13:00:00.000Z" + }; + + // Load the Custom Network Configuration + const Configuration cfg(BridgechainNetwork); + + // Use the Transaction Builder to make a transaction. + const auto bridgechainTransaction = builder::Transfer() + .recipientId("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib") + .vendorField("this is a custom bridgechain transaction") + .sign(Passphrase) + .secondSign(SecondPassphrase) + .build(cfg); + + // Create and Print the Json representation of the Transaction. + const auto transactionJson = bridgechainTransaction.toJson(); + printf("\n\nBridgechain Transaction: %s\n\n", transactionJson.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +void setup() { + Serial.begin(115200); + + createPrivateKey(); + createWif(); + createPublicKey(); + createAddress(); + checkFees(); + signMessage(); + createBridgechainTransaction(); +} + +//////////////////////////////////////////////////////////////////////////////// +void loop() {} diff --git a/examples/arduino/ESP32/arkCrypto_esp32.h b/examples/arduino/ESP32/arkCrypto_esp32.h index d7a9e252..c3eca1eb 100644 --- a/examples/arduino/ESP32/arkCrypto_esp32.h +++ b/examples/arduino/ESP32/arkCrypto_esp32.h @@ -12,11 +12,15 @@ #include "common/configuration.hpp" #include "common/network.hpp" + #include "crypto/message.hpp" + #include "identities/address.hpp" #include "identities/privatekey.hpp" #include "identities/publickey.hpp" #include "identities/wif.hpp" -#include "transactions/builder.h" + +#include "transactions/builders/transfer.hpp" +#include "transactions/transaction.hpp" #endif diff --git a/examples/arduino/ESP8266/ESP8266.ino b/examples/arduino/ESP8266/ESP8266.ino index 6b90ef26..6a7288b1 100644 --- a/examples/arduino/ESP8266/ESP8266.ino +++ b/examples/arduino/ESP8266/ESP8266.ino @@ -1,5 +1,5 @@ /** - * This file is part of Ark Cpp Crypto. + * This file is part of Ark C++ Crypto. * * (c) Ark Ecosystem * @@ -7,160 +7,181 @@ * file that was distributed with this source code. **/ -/** - * ESP8266 Cpp-Crypto Usage Example Sketch - * - * This sketch covers how to use the Cpp-Crypto library. - * It allows your ESP8266 use Ark Ecosystem cryptographic protocols. - */ - - /** - * NOTE: At the time of this writing, the Cpp-Crypto library requires running the 'ARDUINO_IDE.sh' bash script located in the 'extras' folder. - * This converts our library to be compatible with the Arduino IDE. - */ - -/****************************************/ +//////////////////////////////////////////////////////////////////////////////// +// ESP8266 C++ Crypto Usage Example Sketch +// +// This sketch covers how to use the C++ Crypto library. +// It allows your ESP8266 use Ark Ecosystem cryptographic protocols. + +//////////////////////////////////////////////////////////////////////////////// +// NOTE: +// +// If this Repo was Cloned, run the 'ARDUINO_IDE.sh' script first. +// It's in the 'extras/' folder and extends compatability to the Arduino IDE. + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// This is where you include the 'arkCrypto.h' header. +// This allows your project to use Ark C++ Crypto. +// This header is empty and is just to force the inclusion +// of the library into the Arduino sketch -/** - * This is where you include the 'arkCrypto.h' header. - * This allows your project to use Ark Cpp-Crypto. - * This header is empty and is just to force the inclusion - * of the library into the Arduino sketch - */ #include -/**/ -/** - * This is a helper header that includes all the required Ark - * headers required for this sketch. - * - * Also set the CPU frequency to 160MHz using the Arduino IDE's 'Tools' menu. -*/ +//////////////////////////////////////////////////////////////////////////////// +// This is a helper header that includes all the required Ark +// headers required for this sketch. +// +// Also set the CPU frequency to 160MHz using the Arduino IDE's 'Tools' menu. + #include "arkCrypto_esp8266.h" + +//////////////////////////////////////////////////////////////////////////////// +// Misc ARK C++ Crypto headers + +#include "networks/devnet.hpp" + +#include "utils/hex.hpp" +#include "utils/str.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// The Main Arduino Header + +#include + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Namespaces + using namespace Ark::Crypto; using namespace Ark::Crypto::identities; -using namespace Ark::Crypto::Transactions; - -/****************************************/ - -void checkCrypto() { - /** - * Create a BridgeChain transaction, tailored for your custom network. - */ - static const Network MyBridgechainNetwork = { - "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", - 1, 0x53, 0xaa, - "2019-04-12T13:00:00.000Z" - }; - - const Configuration MyBridgechainConfiguration(MyBridgechainNetwork); - - auto myBridgechainTransaction = Builder::buildTransfer( - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 100000000ULL, - "this is a custom bridgechain transaction", - "this is a top secret passphrase", - "this is a top secret passphrase too", - MyBridgechainConfiguration); - - Serial.print("\nBridgechain Transaction: "); - Serial.println(myBridgechainTransaction.toJson().c_str()); - - /**/ - - /********************/ - - /** - * This is how you can check the default 'Network' "Transaction 'Fees' by type. - * In this example, it should return a 'uint64_t' integer of '10000000' as the default 'Fee' for a 'Transaction' of 'Type' '0'. - */ - Configuration config; - unsigned long typeZeroTransactionFee = config.getFee(0); - Serial.print("\nType 0 default Transaction Fee: "); - Serial.println(typeZeroTransactionFee); // The response is a 'uint64_t' integer. - - /**/ - - /********************/ - - /** - * The following methods allows you to create an ARK address. - * This is done by passing a 12-word 'Passphrase' and the 'Network' 'Version' "byte". - * The 'Version" "byte" is a BASE58 P2PKH byte. Ark Devnet is '0x1E'; Ark Mainnet is '0x17'. - * - * Given the passphrase "this is a top secret passphrase", - * and the 'Devnet' 'Version' byte (0x1E); the ARK Address should be "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib" - */ - const auto passphrase = "this is a top secret passphrase"; - const uint8_t networkVersion = 0x1E; - - Address arkAddress = Address::fromPassphrase(passphrase, networkVersion); - Serial.print("\nARK Address: "); - Serial.println(arkAddress.toString().c_str()); // The 'Address' object is a type. Use 'toString()' to view the output. Arduino requires a 'c_str()' to 'print'. - /**/ - - - /********************/ - - /** - * The following methods allows create a 'PrivateKey'. - * This is done by passing a 12-word 'Passphrase'. - * - * Given the passphrase "this is a top secret passphrase", - * the 'PrivateKey" should be "d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712". - * This is a 'SHA256' of your "Passphrase". - */ - const auto passphrase2 = "this is a top secret passphrase"; - PrivateKey privateKeyFromPassphrase = PrivateKey::fromPassphrase(passphrase2); - Serial.print("\nPrivateKey from Passphrase: "); - Serial.println(privateKeyFromPassphrase.toString().c_str()); // The 'PrivateKey' object is a type. Use 'toString()' to view the output. Arduino requires a 'c_str()' to 'print'. - /**/ - - /********************/ - - /** - * The following methods allows create a 'PublicKey'. - * This is done by passing a 12-word 'Passphrase'. - * - * Given the passphrase "this is a top secret passphrase", - * the 'PublicKey" should be "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192". - */ - const auto passphrase3 = "this is a top secret passphrase"; - PublicKey publicKeyFromPassphrase = PublicKey::fromPassphrase(passphrase3); - Serial.print("\nPublicKey from Passphrase: "); - Serial.println(publicKeyFromPassphrase.toString().c_str()); // the 'PublicKey' object is a type. Use 'toString()' to view the output. Arduino requires a 'c_str()' to 'print'. - /**/ - - /********************/ - - /** - * The following methods allows create a 'WIF'-style "PrivateKey". - * 'WIF' stands for "Wallet Import Format" - * This is done by passing a 12-word 'Passphrase' and the 'Network' 'WIF' "byte". - * The 'WIF" "byte" is a BASE58 WIF byte. Ark Devnet is '0xaa'; Ark Mainnet is also '0xaa'. - - * - * Given the passphrase "this is a top secret passphrase", - * and the 'Devnet' 'WIF' byte (0xaa); - * The 'WIF" should be "SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA". - */ - const auto passphrase4 = "this is a top secret passphrase"; - const uint8_t wifByte = 0xaa; - Wif wifFromPassphrase = Wif::fromPassphrase(passphrase4, wifByte); - Serial.print("\nWIF from Passphrase: "); - Serial.println(wifFromPassphrase.toString().c_str()); // the 'WIF' object is a type. Use 'toString()' to view the output. Arduino requires a 'c_str()' to 'print'. - /**/ -}; - -/****************************************/ - -void setup() -{ - Serial.begin(115200); // Begin your Serial Connection. This allows you to monitor your boards output. - - checkCrypto(); // Begin Crypto example usage. -}; - -/****************************************/ - -void loop() {}; // We can leave this empty, as we don't want to repeat anything in this example. +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Sketch Constants + +constexpr auto Passphrase = "this is a top secret passphrase"; +constexpr auto WifByte = 0xaa; // Devnet + +//////////////////////////////////////////////////////////////////////////////// +// Create a PrivateKey using a 12-word Passphrase. +// +// Given the passphrase "this is a top secret passphrase", +// the computed PrivateKey is: +// - "d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712". +// +// --- +void createPrivateKey() { + const auto privateKey = PrivateKey::fromPassphrase(Passphrase); + const auto privateKeyString = privateKey.toString(); + + printf("\n\nPrivateKey from Passphrase: %s\n\n", privateKeyString.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Create a Wif(Wallet Import Format) PrivateKey. +// +// Uses a 12-word Passphrase and a Network Wif-byte. +// +// Given the passphrase "this is a top secret passphrase", +// and the Network byte 0xaa(Devnet), +// the computed Wif is: "SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA". +// +// --- +void createWif() { + const auto wif = Wif::fromPassphrase(Passphrase, WifByte); + const auto wifString = wif.toString(); + + printf("\n\nWIF from Passphrase: %s\n\n", wifString.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Create a PublicKey using a 12-word Passphrase. +// +// Given the passphrase "this is a top secret passphrase", +// the computed PublicKey is: +// - "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192". +// +// --- +void createPublicKey() { + const auto publicKey = PublicKey::fromPassphrase(Passphrase); + const auto publicKeyString = publicKey.toString(); + + printf("\n\nPublicKey from Passphrase: %s\n\n", publicKeyString.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Create a Wallet Address using a 12-word Passphrase and a Network Version. +// +// - Ark Devnet is '0x1E' +// - Ark Mainnet is '0x17'. +// +// Given the passphrase "this is a top secret passphrase" using Devnet(0x1E), +// the computed ARK Address is: +// - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib" +// +// --- +void createAddress() { + const auto address = Address::fromPassphrase(Passphrase, Devnet.version); + const auto addressString = address.toString(); + + printf("\n\nARK Address: %s\n\n", addressString.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Check the Transactions Fees +// --- +void checkFees() { + Configuration cfg; + + auto i = 0; + for (auto &fee : cfg.getPolicy()) { + printf("\n\nType %d fee: %s\n", i, UintToString(fee).c_str()); + ++i; + } + + // or get the Fee by Transaction Type + printf("\n\nType 0 default fee: %s\n\n", UintToString(cfg.getFee(0)).c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +// Create a BridgeChain transaction, tailored for a custom network. +void createBridgechainTransaction() { + + // Custom Bridgechain Network + const Network BridgechainNetwork = { + "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", + 1, 0x53, 0xaa, + "2019-04-12T13:00:00.000Z" + }; + + // Load the Custom Network Configuration + const Configuration cfg(BridgechainNetwork); + + // Use the Transaction Builder to make a transaction. + const auto bridgechainTransaction = builder::Transfer() + .recipientId("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib") + .vendorField("this is a custom bridgechain transaction") + .sign(Passphrase) + .build(cfg); + + // Create and Print the Json representation of the Transaction. + const auto transactionJson = bridgechainTransaction.toJson(); + printf("\n\nBridgechain Transaction: %s\n\n", transactionJson.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////// +void setup() { + Serial.begin(115200); + + createPrivateKey(); + createWif(); + createPublicKey(); + createAddress(); + checkFees(); + createBridgechainTransaction(); +} + +//////////////////////////////////////////////////////////////////////////////// +void loop() {} diff --git a/examples/arduino/ESP8266/arkCrypto_esp8266.h b/examples/arduino/ESP8266/arkCrypto_esp8266.h index 404d1bc9..1dcc04f4 100644 --- a/examples/arduino/ESP8266/arkCrypto_esp8266.h +++ b/examples/arduino/ESP8266/arkCrypto_esp8266.h @@ -12,11 +12,15 @@ #include "common/configuration.hpp" #include "common/network.hpp" + #include "crypto/message.hpp" + #include "identities/address.hpp" #include "identities/privatekey.hpp" #include "identities/publickey.hpp" #include "identities/wif.hpp" -#include "transactions/builder.h" + +#include "transactions/builders/transfer.hpp" +#include "transactions/transaction.hpp" #endif diff --git a/extras/ARDUINO_IDE.sh b/extras/ARDUINO_IDE.sh index 4abf6255..1ae27a54 100755 --- a/extras/ARDUINO_IDE.sh +++ b/extras/ARDUINO_IDE.sh @@ -45,9 +45,6 @@ INCLUDE_COMMON_DIR=${INCLUDE_CPP_CRYPTO_DIR}/common SRC_CRYPTO_DIR=${SRC_DIR}/crypto INCLUDE_CRYPTO_DIR=${INCLUDE_CPP_CRYPTO_DIR}/crypto -SRC_DEFAULTS_DIR=${SRC_DIR}/defaults -INCLUDE_DEFAULTS_DIR=${INCLUDE_CPP_CRYPTO_DIR}/defaults - SRC_IDENTITIES_DIR=${SRC_DIR}/identities INCLUDE_IDENTITIES_DIR=${INCLUDE_CPP_CRYPTO_DIR}/identities @@ -60,6 +57,9 @@ SRC_NETWORKS_DIR=${SRC_DIR}/networks INCLUDE_TRANSACTIONS_DIR=${INCLUDE_CPP_CRYPTO_DIR}/transactions SRC_TRANSACTIONS_DIR=${SRC_DIR}/transactions +INCLUDE_TRANSACTIONS_BUILDERS_DIR=${INCLUDE_TRANSACTIONS_DIR}/builders +SRC_TRANSACTIONS_BUILDERS_DIR=${SRC_TRANSACTIONS_DIR}/builders + if [[ $AUTO == '0' ]]; then # Interface @@ -108,11 +108,6 @@ if [[ -d ${INCLUDE_DIR} ]]; then mv ${INCLUDE_CRYPTO_DIR}/message.hpp ${SRC_CRYPTO_DIR} mv ${INCLUDE_CRYPTO_DIR}/slot.hpp ${SRC_CRYPTO_DIR} - echo -e "Moving 'defaults' headers.\n" - mkdir ${SRC_DEFAULTS_DIR} - mv ${INCLUDE_DEFAULTS_DIR}/static_fees.hpp ${SRC_DEFAULTS_DIR} - mv ${INCLUDE_DEFAULTS_DIR}/transaction_types.hpp ${SRC_DEFAULTS_DIR} - echo -e "Moving 'identites' headers.\n" mv ${INCLUDE_IDENTITIES_DIR}/address.hpp ${SRC_IDENTITIES_DIR} mv ${INCLUDE_IDENTITIES_DIR}/keys.hpp ${SRC_IDENTITIES_DIR} @@ -122,6 +117,7 @@ if [[ -d ${INCLUDE_DIR} ]]; then echo -e "Moving 'interfaces' headers.\n" mkdir ${SRC_INTERFACES_DIR} + mv ${INCLUDE_INTERFACES_DIR}/constants.h ${SRC_INTERFACES_DIR} mv ${INCLUDE_INTERFACES_DIR}/identities.hpp ${SRC_INTERFACES_DIR} echo -e "Moving 'networks' headers.\n" @@ -131,10 +127,19 @@ if [[ -d ${INCLUDE_DIR} ]]; then mv ${INCLUDE_NETWORKS_DIR}/testnet.hpp ${SRC_NETWORKS_DIR} echo -e "Moving 'transactions' headers.\n" - mv ${INCLUDE_TRANSACTIONS_DIR}/builder.h ${SRC_TRANSACTIONS_DIR} - mv ${INCLUDE_TRANSACTIONS_DIR}/deserializer.h ${SRC_TRANSACTIONS_DIR} - mv ${INCLUDE_TRANSACTIONS_DIR}/serializer.h ${SRC_TRANSACTIONS_DIR} - mv ${INCLUDE_TRANSACTIONS_DIR}/transaction.h ${SRC_TRANSACTIONS_DIR} + mv ${INCLUDE_TRANSACTIONS_BUILDERS_DIR}/builder.hpp ${SRC_TRANSACTIONS_BUILDERS_DIR} + mv ${INCLUDE_TRANSACTIONS_BUILDERS_DIR}/transfer.hpp ${SRC_TRANSACTIONS_BUILDERS_DIR} + mv ${INCLUDE_TRANSACTIONS_BUILDERS_DIR}/second_signature.hpp ${SRC_TRANSACTIONS_BUILDERS_DIR} + mv ${INCLUDE_TRANSACTIONS_BUILDERS_DIR}/delegate_registration.hpp ${SRC_TRANSACTIONS_BUILDERS_DIR} + mv ${INCLUDE_TRANSACTIONS_BUILDERS_DIR}/vote.hpp ${SRC_TRANSACTIONS_BUILDERS_DIR} + mv ${INCLUDE_TRANSACTIONS_BUILDERS_DIR}/ipfs.hpp ${SRC_TRANSACTIONS_BUILDERS_DIR} + mv ${INCLUDE_TRANSACTIONS_BUILDERS_DIR}/multi_payment.hpp ${SRC_TRANSACTIONS_BUILDERS_DIR} + mv ${INCLUDE_TRANSACTIONS_BUILDERS_DIR}/delegate_resignation.hpp ${SRC_TRANSACTIONS_BUILDERS_DIR} + mv ${INCLUDE_TRANSACTIONS_BUILDERS_DIR}/htlc_lock.hpp ${SRC_TRANSACTIONS_BUILDERS_DIR} + mv ${INCLUDE_TRANSACTIONS_BUILDERS_DIR}/htlc_claim.hpp ${SRC_TRANSACTIONS_BUILDERS_DIR} + mv ${INCLUDE_TRANSACTIONS_BUILDERS_DIR}/htlc_refund.hpp ${SRC_TRANSACTIONS_BUILDERS_DIR} + mv ${INCLUDE_TRANSACTIONS_DIR}/transaction_data.hpp ${SRC_TRANSACTIONS_DIR} + mv ${INCLUDE_TRANSACTIONS_DIR}/transaction.hpp ${SRC_TRANSACTIONS_DIR} echo -e "Backing up, moving, and removing dependencies from the 'src/lib' directory.\n" mkdir ${EXTRAS_BACKUP_DIR} @@ -170,6 +175,7 @@ else mkdir ${INCLUDE_INTERFACES_DIR} mkdir ${INCLUDE_NETWORKS_DIR} mkdir ${INCLUDE_TRANSACTIONS_DIR} + mkdir ${INCLUDE_TRANSACTIONS_BUILDERS_DIR} echo -e "Moving 'arkCrypto.h' back to the 'include/cpp-crypto/' directory.\n" mv ${SRC_DIR}/arkCrypto.h ${INCLUDE_CPP_CRYPTO_DIR} @@ -190,12 +196,14 @@ else echo -e "Moving 'identities' headers.\n" mv ${SRC_IDENTITIES_DIR}/address.hpp ${INCLUDE_IDENTITIES_DIR} + mv ${SRC_IDENTITIES_DIR}/keys.hpp ${INCLUDE_IDENTITIES_DIR} mv ${SRC_IDENTITIES_DIR}/privatekey.hpp ${INCLUDE_IDENTITIES_DIR} mv ${SRC_IDENTITIES_DIR}/publickey.hpp ${INCLUDE_IDENTITIES_DIR} mv ${SRC_IDENTITIES_DIR}/wif.hpp ${INCLUDE_IDENTITIES_DIR} echo -e "Moving 'interfaces' headers.\n" - mv ${SRC_INTERFACES_DIR}/identities.hpp ${INCLUDE_INTERFACES_DIR} + mv ${SRC_INTERFACES_DIR}/constants.h ${INCLUDE_INTERFACES_DIR} + mv ${SRC_INTERFACES_DIR}/identities.hpp ${INCLUDE_INTERFACES_DIR} rm -rf ${SRC_INTERFACES_DIR} echo -e "Moving 'networks' headers.\n" @@ -205,10 +213,19 @@ else rm -rf ${SRC_NETWORKS_DIR} echo -e "Moving 'transactions' headers.\n" - mv ${SRC_TRANSACTIONS_DIR}/builder.h ${INCLUDE_TRANSACTIONS_DIR} - mv ${SRC_TRANSACTIONS_DIR}/deserializer.h ${INCLUDE_TRANSACTIONS_DIR} - mv ${SRC_TRANSACTIONS_DIR}/serializer.h ${INCLUDE_TRANSACTIONS_DIR} - mv ${SRC_TRANSACTIONS_DIR}/transaction.h ${INCLUDE_TRANSACTIONS_DIR} + mv ${SRC_TRANSACTIONS_BUILDERS_DIR}/builder.hpp ${INCLUDE_TRANSACTIONS_BUILDERS_DIR} + mv ${SRC_TRANSACTIONS_BUILDERS_DIR}/transfer.hpp ${INCLUDE_TRANSACTIONS_BUILDERS_DIR} + mv ${SRC_TRANSACTIONS_BUILDERS_DIR}/second_signature.hpp ${INCLUDE_TRANSACTIONS_BUILDERS_DIR} + mv ${SRC_TRANSACTIONS_BUILDERS_DIR}/delegate_registration.hpp ${INCLUDE_TRANSACTIONS_BUILDERS_DIR} + mv ${SRC_TRANSACTIONS_BUILDERS_DIR}/vote.hpp ${INCLUDE_TRANSACTIONS_BUILDERS_DIR} + mv ${SRC_TRANSACTIONS_BUILDERS_DIR}/ipfs.hpp ${INCLUDE_TRANSACTIONS_BUILDERS_DIR} + mv ${SRC_TRANSACTIONS_BUILDERS_DIR}/multi_payment.hpp ${INCLUDE_TRANSACTIONS_BUILDERS_DIR} + mv ${SRC_TRANSACTIONS_BUILDERS_DIR}/delegate_resignation.hpp ${INCLUDE_TRANSACTIONS_BUILDERS_DIR} + mv ${SRC_TRANSACTIONS_BUILDERS_DIR}/htlc_lock.hpp ${INCLUDE_TRANSACTIONS_BUILDERS_DIR} + mv ${SRC_TRANSACTIONS_BUILDERS_DIR}/htlc_claim.hpp ${SRC_TINCLUDE_TRANSACTIONS_BUILDERS_DIRRANSACTIONS_BUILDERS_DIR} + mv ${SRC_TRANSACTIONS_BUILDERS_DIR}/htlc_refund.hpp ${SRC_TRANSACTIONS_BUILDERS_DIR} + mv ${INCLUDE_TRANSACTIONS_DIR}/transaction_data.hpp ${SRC_TRANSACTIONS_DIR} + mv ${INCLUDE_TRANSACTIONS_DIR}/transaction.hpp ${SRC_TRANSACTIONS_DIR} echo -e "Restoring the 'lib' directory.\n" mkdir ${SRC_LIB_DIR} diff --git a/library.json b/library.json index 4308868a..b6cefc54 100644 --- a/library.json +++ b/library.json @@ -22,13 +22,16 @@ ], "dependencies": [ { - "name": "micro-ecc@1.0.0" + "name": "ArduinoJson@6.12.0" }, { - "name": "ArduinoJson@6.12.0" + "name": "bcl@0.0.5" }, { - "name": "BIP66@0.2.0" + "name": "BIP66@0.3.2" + }, + { + "name": "micro-ecc@1.0.0" } ], "export": { diff --git a/platformio.ini b/platformio.ini index 785382f5..7563bb3a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,7 +15,7 @@ build_dir = build/.pioenvs libdeps_dir = extern/.piolibdeps [common] -lib_deps = ArduinoJson@6.12.0, BIP66@0.2.1, micro-ecc@1.0.0 +lib_deps = ArduinoJson@6.12.0, bcl@0.0.5, BIP66@0.3.2, micro-ecc@1.0.0 build_flags = -I./src/ -I./src/lib -I./src/include/cpp-crypto src_filter = +<*> upload_speed = 921600 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4c9b0b01..2c6c02ea 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,29 +16,13 @@ include(${CMAKE_SOURCE_DIR}/cmake/External.cmake) # ------------------------------------------------------------------------------ set(INTERNAL_LIBRARY_SOURCE_DIRS - ${PROJECT_SOURCE_DIR}/lib/bcl - ${PROJECT_SOURCE_DIR}/lib/rfc6979) + ${PROJECT_SOURCE_DIR}/lib/rfc6979 +) include_directories(${INTERNAL_LIBRARY_SOURCE_DIRS}) # ------------------------------------------------------------------------------ -set(BCL_SOURCE - lib/bcl/Base58Check.cpp - lib/bcl/CurvePoint.cpp - lib/bcl/Ecdsa.cpp - lib/bcl/FieldInt.cpp - lib/bcl/Ripemd160.cpp - lib/bcl/Sha256Hash.cpp - lib/bcl/Sha256.cpp - lib/bcl/Sha512.cpp - lib/bcl/Uint256.cpp - lib/bcl/Utils.cpp) - -set(INTERNAL_LIBRARY_SOURCE ${BCL_SOURCE}) - -# ------------------------------------------------------------------------------ - # ------------------------------------------------------------------------------ # ARK C++ Crypto Source # ------------------------------------------------------------------------------ @@ -48,7 +32,8 @@ set(INTERNAL_LIBRARY_SOURCE ${BCL_SOURCE}) set(COMMON_SOURCE common/configuration.cpp - common/network.cpp) + common/network.cpp +) # ------------------------------------------------------------------------------ # Crypto @@ -57,7 +42,8 @@ set(CRYPTO_SOURCE crypto/curve.cpp crypto/hash.cpp crypto/message.cpp - crypto/slot.cpp) + crypto/slot.cpp +) # ------------------------------------------------------------------------------ # Identities @@ -67,24 +53,45 @@ set(IDENTITIES_SOURCE identities/keys.cpp identities/privatekey.cpp identities/publickey.cpp - identities/wif.cpp) - -# ------------------------------------------------------------------------------ -# Managers - -set(MANAGERS_SOURCE - managers/fee_manager.cpp - managers/network_manager.cpp + identities/wif.cpp ) # ------------------------------------------------------------------------------ # Transactions -set(TRANSACTIONS_SOURCE - transactions/builder.cpp +# Types +set(TRANSACTIONS_TYPES_SOURCE + transactions/types/transfer.cpp + transactions/types/second_signature.cpp + transactions/types/delegate_registration.cpp + transactions/types/vote.cpp + # transactions/types/multi_signature.cpp + transactions/types/ipfs.cpp + transactions/types/multi_payment.cpp + transactions/types/htlc_lock.cpp + transactions/types/htlc_claim.cpp + transactions/types/htlc_refund.cpp +) + +# Serialization/Deserialization +set(TRANSACTIONS_SERDE_SOURCE transactions/deserializer.cpp transactions/serializer.cpp - transactions/transaction.cpp) +) + +# Mapping +set(TRANSACTIONS_MAPPING_SOURCE + transactions/mapping/json.cpp + transactions/mapping/mapping.cpp +) + +# Transaction Source Files +set(TRANSACTIONS_SOURCE + ${TRANSACTIONS_TYPES_SOURCE} + ${TRANSACTIONS_SERDE_SOURCE} + ${TRANSACTIONS_MAPPING_SOURCE} + transactions/transaction.cpp +) # ------------------------------------------------------------------------------ # Utils @@ -94,13 +101,13 @@ set(UTILS_SOURCE utils/base58.cpp) # ------------------------------------------------------------------------------ # ARK C++ Crypto Library Source -set(ARK_SOURCE +set(ARK_LIBRARY_SOURCE ${COMMON_SOURCE} ${CRYPTO_SOURCE} ${IDENTITIES_SOURCE} - ${MANAGERS_SOURCE} ${TRANSACTIONS_SOURCE} - ${UTILS_SOURCE}) + ${UTILS_SOURCE} +) # ------------------------------------------------------------------------------ @@ -111,7 +118,8 @@ set(ARK_SOURCE add_library(${PROJECT_NAME} STATIC ${EXTERNAL_LIBRARY_SOURCE} ${INTERNAL_LIBRARY_SOURCE} - ${ARK_SOURCE}) + ${ARK_LIBRARY_SOURCE} +) # ------------------------------------------------------------------------------ @@ -122,7 +130,8 @@ add_library(${PROJECT_NAME} STATIC set(ARK_CPP_CRYPTO_INCLUDE_DIRS ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/include/cpp-crypto - ${PROJECT_SOURCE_DIR}/lib/) + ${PROJECT_SOURCE_DIR}/lib/ +) include_directories(${ARK_CPP_CRYPTO_INCLUDE_DIRS}) diff --git a/src/common/configuration.cpp b/src/common/configuration.cpp index 47492e12..996a4ff5 100644 --- a/src/common/configuration.cpp +++ b/src/common/configuration.cpp @@ -15,40 +15,37 @@ namespace Ark { namespace Crypto { +//////////////////////////////////////////////////////////////////////////////// // Network initialization: Custom Network w/StaticFees -Configuration::Configuration(const Network& network) { - setNetwork(network); +Configuration::Configuration(const Network &network) { + setNetwork(network); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // FeePolicy initialization: ARK Devnet w/Custom Fees -Configuration::Configuration(const FeePolicy& policy) { - setPolicy(policy); +Configuration::Configuration(const FeePolicy &policy) { + setPolicy(policy); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Network & Fee initialization: Custom Network w/Custom Fees -Configuration::Configuration(const Network& network, const FeePolicy& policy) { - setNetwork(network); - setPolicy(policy); +Configuration::Configuration(const Network &network, const FeePolicy &policy) { + setNetwork(network); + setPolicy(policy); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Compares the Network and FeePolicy for equality. -bool Configuration::operator==(const Configuration& other) const { - return this->network_ == other.network_ && - this->feePolicy_ == other.feePolicy_; +auto Configuration::operator==(const Configuration &other) const -> bool { + return this->network_ == other.network_ && + this->feePolicy_ == other.feePolicy_; } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Compares the Network and FeePolicy for inequality. -bool Configuration::operator!=(const Configuration& other) const { - return this->network_ != other.network_ || - this->feePolicy_ != other.feePolicy_; +auto Configuration::operator!=(const Configuration &other) const -> bool { + return this->network_ != other.network_ || + this->feePolicy_ != other.feePolicy_; } } // namespace Crypto diff --git a/src/common/network.cpp b/src/common/network.cpp index 6f3485c6..11f2ceb8 100644 --- a/src/common/network.cpp +++ b/src/common/network.cpp @@ -14,24 +14,26 @@ namespace Ark { namespace Crypto { -bool Network::operator==(const Network& other) const { - bool numbersMatch = (this->slip44 == other.slip44) && - (this->wif == other.wif) && - (this->version == other.version); - bool stringsMatch = (this->nethash == other.nethash) && - (this->epoch == other.epoch); - return numbersMatch && stringsMatch; +//////////////////////////////////////////////////////////////////////////////// +auto Network::operator==(const Network& other) const -> bool { + bool numbersMatch = (this->slip44 == other.slip44) && + (this->wif == other.wif) && + (this->version == other.version); + bool stringsMatch = (this->nethash == other.nethash) && + (this->epoch == other.epoch); + + return numbersMatch && stringsMatch; } -/**/ +//////////////////////////////////////////////////////////////////////////////// +auto Network::operator!=(const Network& other) const -> bool { + bool numbersMatch = (this->slip44 != other.slip44) || + (this->wif != other.wif) || + (this->version != other.version); + bool stringsMatch = (this->nethash != other.nethash) || + (this->epoch != other.epoch); -bool Network::operator!=(const Network& other) const { - bool numbersMatch = (this->slip44 != other.slip44) || - (this->wif != other.wif) || - (this->version != other.version); - bool stringsMatch = (this->nethash != other.nethash) || - (this->epoch != other.epoch); - return numbersMatch || stringsMatch; + return numbersMatch || stringsMatch; } } // namespace Crypto diff --git a/src/crypto/curve.cpp b/src/crypto/curve.cpp index 8aa92ea5..c2aa2f88 100644 --- a/src/crypto/curve.cpp +++ b/src/crypto/curve.cpp @@ -1,4 +1,3 @@ - /** * This file is part of Ark Cpp Crypto. * @@ -7,6 +6,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. **/ + #include "crypto/curve.hpp" #include @@ -14,25 +14,26 @@ #include #include "interfaces/identities.hpp" -#include "utils/crypto_helpers.h" + #include "utils/hex.hpp" +#include "utils/platform.h" -#include "rfc6979/rfc6979.h" // Nonce function +#include "rfc6979/rfc6979.h" // Nonce function -#include "bcl/Ecdsa.hpp" // Signing operations -#include "bcl/Sha256.hpp" -#include "bcl/Sha256Hash.hpp" -#include "bcl/Uint256.hpp" +#include "Ecdsa.hpp" // BCL Crypto Lib +#include "Sha256.hpp" +#include "Sha256Hash.hpp" +#include "Uint256.hpp" -#include "bip66.h" // ECDSA DER encoding +#include "bip66.hpp" // ECDSA DER encoding -#include "uECC.h" // publicKey operations +#include "uECC.h" // publicKey operations namespace Ark { namespace Crypto { -////////// Curve::Ecdsa ////////// - +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// // Curve::Ecdsa::sign // // Sign the input hash with a privateKey using SECP256K1 ECDSA. @@ -40,47 +41,60 @@ namespace Crypto { // Signing result is copied to the outgoing signature. // Returns true if signing and encoding were both successful. // -// const uint8_t* hash32 -// | 32-byte array of the message hash to sign. +// const uint8_t *hash32 +// - 32-byte array of the message hash to sign. // -// const uint8_t* privateKeyBytes -// | 32-byte array of the privateKey. +// const uint8_t *privateKeyBytes +// - 32-byte array of the privateKey. // // std::vector& outSignature -// | Outgoing signature filled with the DER-encoded signing result. -bool Curve::Ecdsa::sign(const uint8_t* hash32, - const uint8_t* privateKeyBytes, - std::vector& outSignature) { - if (hash32 == nullptr || privateKeyBytes == nullptr) { - return false; - }; - - // Create the Signing nonce - Hash32 nonce32; - nonce_function_rfc6979(nonce32.data(), - hash32, - privateKeyBytes, - nullptr, nullptr, 0); - - // Sign the hash using PrivateKey bytes and the Signing Nonce. - // Outputs Signature values to the R and S elements. - bcl::Uint256 r; - bcl::Uint256 s; - bcl::Ecdsa::sign(bcl::Uint256(privateKeyBytes), - bcl::Sha256Hash(hash32, HASH_32_BYTE_LEN), - bcl::Uint256(nonce32.data()), - r, s); - - // Copy the big-endian bytes into and R and S element byte-buffers. - std::array rsBuffer = {}; - r.getBigEndianBytes(&rsBuffer[0]); - s.getBigEndianBytes(&rsBuffer[HASH_32_BYTE_LEN]); - - // Encode r & s-values into a BIP66-encoded signature. - // returns 'true' if DER encoding was successful. - return BIP66::encode(rsBuffer.data(), outSignature); +// - Outgoing signature filled with the DER-encoded signing result. +// +// --- +auto Curve::Ecdsa::sign(const uint8_t *hash32, + const uint8_t *privateKeyBytes, + std::vector *outSignature) -> bool { + if (hash32 == nullptr || privateKeyBytes == nullptr) { + return false; + } + + // Create the Signing nonce + Hash32 nonce32 {}; + nonce_function_rfc6979(nonce32.data(), + hash32, + privateKeyBytes, + nullptr, nullptr, 0); + + // Sign the hash using PrivateKey bytes and the Signing Nonce. + // Outputs Signature values to the R and S elements. + bcl::Uint256 r; + bcl::Uint256 s; + bcl::Ecdsa::sign(bcl::Uint256(privateKeyBytes), + bcl::Sha256Hash(hash32, HASH_32_LEN), + bcl::Uint256(nonce32.data()), + r, s); + + // Copy the big-endian bytes into and R/S element buffer. + std::array rsBuffer; + r.getBigEndianBytes(&rsBuffer[0]); + s.getBigEndianBytes(&rsBuffer[HASH_32_LEN]); + + // Encode R & S Elements into a BIP66-encoded signature. + const auto success = bip66::encode( + rsBuffer.data(), HASH_32_LEN, + rsBuffer.data() + HASH_32_LEN, HASH_32_LEN, + outSignature->data()); + + // Calculate and set the outgoing signature's size. + const auto outSigSize = outSignature->at(1) == 0UL + ? 0UL + : 2U + outSignature->at(1); + outSignature->resize(outSigSize); + + return success; } +//////////////////////////////////////////////////////////////////////////////// // Curve::Ecdsa::verify // // Verify a hash and publicKey against a signature using SECP256K1 ECDSA. @@ -88,42 +102,48 @@ bool Curve::Ecdsa::sign(const uint8_t* hash32, // Signing result is copied to the outgoing signature. // Returns true if signing and encoding were both successful. // -// const uint8_t* hash32 -// | 32-byte array of the message hash to to verify. +// const uint8_t *hash32 +// - 32-byte array of the message hash to to verify. // -// const uint8_t* publicKeyBytes -// | 33-byte array of the compressed-type publicKey. +// const uint8_t *publicKeyBytes +// - 33-byte array of the compressed-type publicKey. // // std::vector& signature -// | DER-encoded Signature by which the hash and publicKey are being verified. -bool Curve::Ecdsa::verify(const uint8_t* hash32, - const uint8_t* publicKeyBytes, - const std::vector& signature) { - // Decompressed the publicKey - const auto decompressed = Curve::PublicKey::decompress(publicKeyBytes); - - // Build the curvePoint - bcl::FieldInt x(BytesToHex(&decompressed[0], - &decompressed[HASH_32_BYTE_LEN]).c_str()); - bcl::FieldInt y(BytesToHex(&decompressed[HASH_32_BYTE_LEN], - &decompressed[HASH_64_BYTE_LEN]).c_str()); - - /// Decode signature from DER into R and S element buffers. - std::vector rBuffer; - rBuffer.reserve(HASH_32_BYTE_LEN); - std::vector sBuffer; - sBuffer.reserve(HASH_32_BYTE_LEN); - BIP66::decode(signature, rBuffer, sBuffer); - - // Verify - return bcl::Ecdsa::verify(bcl::CurvePoint(x, y), - bcl::Sha256Hash(hash32, HASH_32_BYTE_LEN), - bcl::Uint256(rBuffer.data()), - bcl::Uint256(sBuffer.data())); +// - DER-encoded Signature by which the hash and publicKey are being verified. +// +// --- +auto Curve::Ecdsa::verify(const uint8_t *hash32, + const uint8_t *publicKeyBytes, + const std::vector &signature) -> bool { + if (hash32 == nullptr || publicKeyBytes == nullptr) { + return false; + } + + // Decompressed the publicKey + const auto decompressed = Curve::PublicKey::decompress(publicKeyBytes); + + // Build the curvePoint + bcl::FieldInt x(BytesToHex(&decompressed[0], + &decompressed[HASH_32_LEN]).c_str()); + bcl::FieldInt y(BytesToHex(&decompressed[HASH_32_LEN], + &decompressed[HASH_64_LEN]).c_str()); + + /// Decode signature from DER into R and S element buffers. + std::vector r(HASH_32_LEN, 0); + std::vector s(HASH_32_LEN, 0); + + bip66::decode(signature.data(), signature.size(), r.data(), s.data()); + + // Verify + return bcl::Ecdsa::verify( + bcl::CurvePoint(x, y), + bcl::Sha256Hash(hash32, HASH_32_LEN), + bcl::Uint256(&r.at(0)), + bcl::Uint256(&s.at(0))); } -////////// Curve::PublicKey ////////// - +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// // Curve::PublicKey::compute // // Compute a compressed-type publicKey from a privateKey byte-array. @@ -131,34 +151,41 @@ bool Curve::Ecdsa::verify(const uint8_t* hash32, // Returns a 33-byte compressed-type publicKey array. // If computation was not successful, an empty byte-array is returned. // -// const uint8_t* privateKeyBytes -// | 32-byte privateKey byte array. -PublicKeyBytes Curve::PublicKey::compute(const uint8_t* privateKeyBytes) { - const struct uECC_Curve_t* curve = uECC_secp256k1(); +// const uint8_t *privateKeyBytes +// - 32-byte privateKey byte array. +// +// --- +auto Curve::PublicKey::compute(const uint8_t *privateKeyBytes) -> PublicKeyBytes { + const struct uECC_Curve_t *curve = uECC_secp256k1(); - PublicKeyPoint uncompressed {}; - if (uECC_compute_public_key(privateKeyBytes, &uncompressed[0], curve) == 0) { - return {}; - }; + PublicKeyPoint uncompressed {}; + if (uECC_compute_public_key(privateKeyBytes, &uncompressed[0], curve) == 0) { + return {}; + }; - return compress(uncompressed.data()); + return compress(uncompressed.data()); } +//////////////////////////////////////////////////////////////////////////////// // Curve::PublicKey::compress // // Compresses an uncompressed publicKey curvepoint. // // Returns a 33-byte compressed-type publicKey array. // -// const uint8_t* uncompressed -// | 64-byte uncompressed-type publicKey byte array of (x,y) elements -PublicKeyBytes Curve::PublicKey::compress(const uint8_t* uncompressed) { - const struct uECC_Curve_t* curve = uECC_secp256k1(); - PublicKeyBytes compressed {}; - uECC_compress(uncompressed, compressed.data(), curve); - return compressed; +// const uint8_t *uncompressed +// - 64-byte uncompressed-type publicKey byte array of (x,y) elements +// +// --- +auto Curve::PublicKey::compress(const uint8_t *uncompressed) -> PublicKeyBytes { + const struct uECC_Curve_t* curve = uECC_secp256k1(); + PublicKeyBytes compressed {}; + uECC_compress(uncompressed, compressed.data(), curve); + + return compressed; } +//////////////////////////////////////////////////////////////////////////////// // Curve::PublicKey::decompress // // Decompresses a compressed publicKey. @@ -166,26 +193,32 @@ PublicKeyBytes Curve::PublicKey::compress(const uint8_t* uncompressed) { // // Returns a 64-byte uncompressed-type publicKey array of (x,y) elements. // -// const uint8_t* compressed -// | 33-byte compressed-type publicKey byte array. -PublicKeyPoint Curve::PublicKey::decompress(const uint8_t* compressed) { - const struct uECC_Curve_t* curve = uECC_secp256k1(); - PublicKeyPoint decompressed {}; - uECC_decompress(compressed, decompressed.data(), curve); - return decompressed; +// const uint8_t *compressed +// - 33-byte compressed-type publicKey byte array. +// +// --- +auto Curve::PublicKey::decompress(const uint8_t *compressed) -> PublicKeyPoint { + const struct uECC_Curve_t* curve = uECC_secp256k1(); + PublicKeyPoint decompressed {}; + uECC_decompress(compressed, decompressed.data(), curve); + + return decompressed; } +//////////////////////////////////////////////////////////////////////////////// // Curve::PublicKey::validate // // Validate a compressed publicKey. // // Returns true if the 33-byte publicKey array was successfully validated. // -// const uint8_t* publicKeyBytes -// | 33-byte compressed-type publicKey byte array. -bool Curve::PublicKey::validate(const uint8_t* publicKeyBytes) { - const struct uECC_Curve_t* curve = uECC_secp256k1(); - return uECC_valid_public_key(decompress(publicKeyBytes).data(), curve) != 0; +// const uint8_t *publicKeyBytes +// - 33-byte compressed-type publicKey byte array. +// +// --- +auto Curve::PublicKey::validate(const uint8_t *publicKeyBytes) -> bool { + const struct uECC_Curve_t* curve = uECC_secp256k1(); + return uECC_valid_public_key(decompress(publicKeyBytes).data(), curve) != 0; } } // namespace Crypto diff --git a/src/crypto/curve.hpp b/src/crypto/curve.hpp index ab6a2227..7aeaaab4 100644 --- a/src/crypto/curve.hpp +++ b/src/crypto/curve.hpp @@ -1,4 +1,3 @@ - /** * This file is part of Ark Cpp Crypto. * @@ -8,8 +7,8 @@ * file that was distributed with this source code. **/ -#ifndef CRYPTO_CURVE_HPP -#define CRYPTO_CURVE_HPP +#ifndef ARK_CRYPTO_CURVE_HPP +#define ARK_CRYPTO_CURVE_HPP #include @@ -19,26 +18,35 @@ namespace Ark { namespace Crypto { struct Curve { - // Generate Signatures based in ECDSA Cryptography. - struct Ecdsa { - static const size_t MAX_SIG_LEN = 72U; - - static bool sign(const uint8_t* hash32, - const uint8_t* privateKeyBytes, - std::vector& outSignature); - - static bool verify(const uint8_t* hash32, - const uint8_t* publicKeyBytes, - const std::vector& signature); - }; - - // Primitive PublicKey operations. - struct PublicKey { - static PublicKeyBytes compute(const uint8_t* privateKeyBytes); - static PublicKeyBytes compress(const uint8_t* uncompressed); - static PublicKeyPoint decompress(const uint8_t* compressed); - static bool validate(const uint8_t* publicKeyBytes); - }; + //////////////////////////////////////////////////////////////////////////// + // Generate Signatures based in ECDSA Cryptography. + struct Ecdsa { + //////////////////////////////////////////////////////////////////////// + static bool sign(const uint8_t *hash32, + const uint8_t *privateKeyBytes, + std::vector *outSignature); + + //////////////////////////////////////////////////////////////////////// + static bool verify(const uint8_t *hash32, + const uint8_t *publicKeyBytes, + const std::vector &signature); + }; + + //////////////////////////////////////////////////////////////////////////// + // Primitive PublicKey operations. + struct PublicKey { + //////////////////////////////////////////////////////////////////////// + static PublicKeyBytes compute(const uint8_t *privateKeyBytes); + + //////////////////////////////////////////////////////////////////////// + static PublicKeyBytes compress(const uint8_t *uncompressed); + + //////////////////////////////////////////////////////////////////////// + static PublicKeyPoint decompress(const uint8_t *compressed); + + //////////////////////////////////////////////////////////////////////// + static bool validate(const uint8_t *publicKeyBytes); + }; }; } // namespace Crypto diff --git a/src/crypto/hash.cpp b/src/crypto/hash.cpp index 8c843e42..388db9bb 100644 --- a/src/crypto/hash.cpp +++ b/src/crypto/hash.cpp @@ -9,37 +9,37 @@ #include "crypto/hash.hpp" -#include +#include #include "interfaces/identities.hpp" -#include "bcl/Ripemd160.hpp" -#include "bcl/Sha256.hpp" -#include "bcl/Sha256Hash.hpp" +#include "Ripemd160.hpp" // BCL Crypto Lib +#include "Sha256.hpp" +#include "Sha256Hash.hpp" namespace Ark { namespace Crypto { +//////////////////////////////////////////////////////////////////////////////// // Returns the RIPEMD160 hash of a publicKey bytes. // Expects a 33-byte compressed PublicKey array. -PubkeyHash Hash::ripemd160(const uint8_t*publicKeyBytes) { - PubkeyHash hash20; - bcl::Ripemd160::getHash(publicKeyBytes, - PUBLICKEY_COMPRESSED_BYTE_LEN, - hash20.data()); - return hash20; +auto Hash::ripemd160(const uint8_t *publicKeyBytes) -> PubkeyHash { + PubkeyHash hash20 {}; + bcl::Ripemd160::getHash(publicKeyBytes, + PUBLICKEY_COMPRESSED_LEN, + hash20.data()); + return hash20; } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns a 32-byte SHA256 hash of the input vector. -Hash32 Hash::sha256(const uint8_t* inputBytes, size_t size) { - Hash32 hash32 {}; - memmove(hash32.data(), - bcl::Sha256::getHash(inputBytes, size).value, - hash32.size()); +auto Hash::sha256(const uint8_t *inputBytes, const size_t &size) -> Hash32 { + auto result = bcl::Sha256::getHash(inputBytes, size); + + Hash32 hash32 {}; + std::copy_n(result.value, HASH_32_LEN, hash32.begin()); - return hash32; + return hash32; } } // namespace Crypto diff --git a/src/crypto/hash.hpp b/src/crypto/hash.hpp index 473983e7..483afdbf 100644 --- a/src/crypto/hash.hpp +++ b/src/crypto/hash.hpp @@ -8,17 +8,22 @@ * file that was distributed with this source code. **/ -#ifndef CRYPTO_HASH_HPP -#define CRYPTO_HASH_HPP +#ifndef ARK_CRYPTO_HASH_HPP +#define ARK_CRYPTO_HASH_HPP #include "interfaces/identities.hpp" namespace Ark { namespace Crypto { +//////////////////////////////////////////////////////////////////////////////// +// Hash Operations struct Hash { - static PubkeyHash ripemd160(const uint8_t* publicKeyBytes); - static Hash32 sha256(const uint8_t* inputBytes, size_t size); + //////////////////////////////////////////////////////////////////////////// + static PubkeyHash ripemd160(const uint8_t *publicKeyBytes); + + //////////////////////////////////////////////////////////////////////////// + static Hash32 sha256(const uint8_t *inputBytes, const size_t &size); }; } // namespace Crypto diff --git a/src/crypto/message.cpp b/src/crypto/message.cpp index b81a1f4f..80e7e898 100644 --- a/src/crypto/message.cpp +++ b/src/crypto/message.cpp @@ -9,98 +9,113 @@ #include "crypto/message.hpp" +#include #include #include #include #include "interfaces/identities.hpp" + #include "crypto/curve.hpp" #include "crypto/hash.hpp" + #include "identities/keys.hpp" + #include "utils/hex.hpp" #include "utils/json.h" namespace Ark { namespace Crypto { -namespace { -constexpr const char* MESSAGE_KEY = "message"; -constexpr const char* PUBLICKEY_KEY = "publickey"; -constexpr const char* SIGNATURE_KEY = "signature"; -} // namespace +//////////////////////////////////////////////////////////////////////////////// +constexpr auto MESSAGE_KEY = "message"; +constexpr auto PUBLICKEY_KEY = "publickey"; +constexpr auto SIGNATURE_KEY = "signature"; + +constexpr size_t MAGIC_JSON_SIZE = 120; +constexpr size_t MAGIC_JSON_OBJ_SIZE = 3; +//////////////////////////////////////////////////////////////////////////////// // Create an empty Message object for building and signing. -Message::Message() : publicKey({}), signature(Curve::Ecdsa::MAX_SIG_LEN) {} +Message::Message() : publicKey() { + signature.resize(SIGNATURE_ECDSA_MAX); +} +//////////////////////////////////////////////////////////////////////////////// // Create a Signed Message object for verification. Message::Message(std::string message, - const PublicKeyBytes& publicKeyBytes, - std::vector signature) - : message(std::move(message)), - publicKey(publicKeyBytes), - signature(std::move(signature)) {}; - -bool Message::sign(const std::string& message, const std::string& passphrase) { - this->message = message; - - const auto keys = identities::Keys::fromPassphrase(passphrase.c_str()); - this->publicKey = keys.publicKey; + const uint8_t *publicKeyBytes, + const uint8_t *signature) : message(std::move(message)) { + std::copy_n(publicKeyBytes, + PUBLICKEY_COMPRESSED_LEN, + this->publicKey.begin()); + + this->signature.resize(signature[1] == 0U ? 0U : 2U + signature[1]); + std::copy_n(signature, + this->signature.size(), + this->signature.begin()); +} - const auto messageBytes = - reinterpret_cast(message.c_str()); - const auto hash = Hash::sha256(messageBytes, this->message.size()); +//////////////////////////////////////////////////////////////////////////////// +auto Message::sign(const std::string &message, const std::string &passphrase) + -> bool { + this->message = message; - std::vector buffer(Curve::Ecdsa::MAX_SIG_LEN); - Curve::Ecdsa::sign(hash.data(), keys.privateKey.data(), buffer); + const auto keys = identities::Keys::fromPassphrase(passphrase.c_str()); + this->publicKey = keys.publicKey; - buffer.resize(buffer[1] + 2); - this->signature = std::move(buffer); + const auto hash = Hash::sha256((uint8_t *)message.data(), + this->message.size()); - return true; + return Curve::Ecdsa::sign(hash.data(), + keys.privateKey.data(), + &this->signature); } +//////////////////////////////////////////////////////////////////////////////// // Verify a Signed Message object. -bool Message::verify() const { - const auto messageBytes = - reinterpret_cast(this->message.c_str()); - const auto hash = Hash::sha256(messageBytes, this->message.size()); - - return Curve::Ecdsa::verify(hash.data(), - this->publicKey.data(), - this->signature); +auto Message::verify() const -> bool { + const auto hash = Hash::sha256((uint8_t *)this->message.data(), + this->message.size()); + + return Curve::Ecdsa::verify(hash.data(), + this->publicKey.data(), + this->signature); } +//////////////////////////////////////////////////////////////////////////////// // Create a string map of the Signed Message objects. -std::map -Message::toArray() const { - return { - { MESSAGE_KEY, this->message }, - { PUBLICKEY_KEY, BytesToHex(this->publicKey.begin(), this->publicKey.end()) }, - { SIGNATURE_KEY, BytesToHex(this->signature.begin(), this->signature.end()) } - }; +auto Message::toMap() const -> std::map { + return { + { MESSAGE_KEY, this->message }, + { PUBLICKEY_KEY, BytesToHex(this->publicKey) }, + { SIGNATURE_KEY, BytesToHex(this->signature) } + }; } +//////////////////////////////////////////////////////////////////////////////// // Create a Json'ified string of the Signed Message. -std::string Message::toJson() const { - const size_t MAGIC_JSON_SIZE = 120U; - std::map messageArray = this->toArray(); - - const size_t docLength = this->message.length() + - (PUBLICKEY_COMPRESSED_BYTE_LEN + 1) + // + `/0` - (Curve::Ecdsa::MAX_SIG_LEN + 1); // + `/0` - - const size_t docCapacity = JSON_OBJECT_SIZE(3) + docLength + MAGIC_JSON_SIZE; - DynamicJsonDocument doc(docCapacity); - - doc[MESSAGE_KEY] = messageArray[MESSAGE_KEY]; - doc[PUBLICKEY_KEY] = messageArray[PUBLICKEY_KEY]; - doc[SIGNATURE_KEY] = messageArray[SIGNATURE_KEY]; - - std::string jsonStr; - jsonStr.reserve(docCapacity); - serializeJson(doc, &jsonStr[0], docCapacity); - - return jsonStr; +auto Message::toJson() const -> std::string { + auto messageArray = this->toMap(); + + const size_t docLength = this->message.length() + + (PUBLICKEY_COMPRESSED_LEN + 1U) + // + `/0` + (this->signature.size() + 1U); // + `/0` + + const size_t docCapacity = JSON_OBJECT_SIZE(MAGIC_JSON_OBJ_SIZE) + + docLength + + MAGIC_JSON_SIZE; + DynamicJsonDocument doc(docCapacity); + + doc[MESSAGE_KEY] = messageArray[MESSAGE_KEY]; + doc[PUBLICKEY_KEY] = messageArray[PUBLICKEY_KEY]; + doc[SIGNATURE_KEY] = messageArray[SIGNATURE_KEY]; + + std::string jsonStr; + jsonStr.reserve(docCapacity); + serializeJson(doc, jsonStr); + + return jsonStr; } } // namespace Crypto diff --git a/src/crypto/slot.cpp b/src/crypto/slot.cpp index 0cdf543e..ad4b1a15 100644 --- a/src/crypto/slot.cpp +++ b/src/crypto/slot.cpp @@ -11,97 +11,104 @@ #include +#include "interfaces/constants.h" + #include "common/network.hpp" -#include "utils/crypto_helpers.h" -#ifndef USE_IOT // Desktop and Server +#include "utils/platform.h" - #include - #include +//////////////////////////////////////////////////////////////////////////////// +#ifndef USE_IOT // Server/Desktop - #undef round - #include "date/date.h" +#include +#include - namespace Ark { - namespace Crypto { +#undef round +#include "date/date.h" - namespace { - constexpr const uint64_t SECONDS_DIVISOR = 1000ULL; - } // namespace +namespace Ark { +namespace Crypto { - // Get the Epoch of a given Network. - uint64_t Slot::epoch(const Network& network) { +//////////////////////////////////////////////////////////////////////////////// +// Get the Epoch of a given Network. +auto Slot::epoch(const Network &network) -> uint64_t { // NOLINT std::istringstream in(network.epoch); std::chrono::system_clock::time_point timePoint; + in >> date::parse("%FT%TZ", timePoint); - // cast milliseconds as uint64_t in seconds(/1000) - return std::chrono::duration_cast( - timePoint.time_since_epoch()) - .count() / SECONDS_DIVISOR; - } // src: https://stackoverflow.com/questions/33421450/c-c-time-zone-correct-time-conversion-to-seconds-since-epoch/33438989#33438989 + const uint64_t SECONDS_DIVISOR = 1000ULL; - // Get the current Network time. - uint64_t Slot::time(const Ark::Crypto::Network& network) { - const auto systemTime = - std::chrono::duration_cast( + // cast milliseconds as uint64_t in seconds(/1000) + // src: https://stackoverflow.com/questions/33421450/c-c-time-zone-correct-time-conversion-to-seconds-since-epoch/33438989#33438989 + return std::chrono::duration_cast( + timePoint.time_since_epoch()) + .count() / SECONDS_DIVISOR; +} + +//////////////////////////////////////////////////////////////////////////////// +// Get the current Network time. +auto Slot::time(const Ark::Crypto::Network &network) -> uint64_t { + const auto systemTime = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) - .count(); + .count(); + return systemTime - epoch(network); - } +} - } // namespace Crypto - } // namespace Ark +} // namespace Crypto +} // namespace Ark -#endif +#endif // Server/Desktop Builds #if defined(ESP32) || defined(ESP8266) - #include - #include /* strtol */ - #include - - namespace Ark { - namespace Crypto { - - - // Get the Epoch of a given Network. - uint64_t Slot::epoch(const Ark::Crypto::Network& network) { - const uint8_t TIMESTAMP_LEN = 24U; - const uint8_t OFFSET_YEAR = 0U; - const uint8_t OFFSET_MONTH = 5U; - const uint8_t OFFSET_DAY = 8U; - const uint8_t OFFSET_HOUR = 11U; - const uint8_t OFFSET_MINUTE = 14U; - const uint8_t OFFSET_SECOND = 17U; - const uint8_t BASE10 = 10U; - const uint16_t YEAR_1900 = 1900U; +//////////////////////////////////////////////////////////////////////////////// +#include +#include /* strtol */ +#include + +namespace Ark { +namespace Crypto { + +//////////////////////////////////////////////////////////////////////////////// +// Get the Epoch of a given Network. +auto Slot::epoch(const Ark::Crypto::Network& network) -> uint64_t { + const uint8_t TIMESTAMP_LEN = 24U; + const uint8_t OFFSET_YEAR = 0U; + const uint8_t OFFSET_MONTH = 5U; + const uint8_t OFFSET_DAY = 8U; + const uint8_t OFFSET_HOUR = 11U; + const uint8_t OFFSET_MINUTE = 14U; + const uint8_t OFFSET_SECOND = 17U; + const uint16_t YEAR_1900 = 1900U; // If unexpected ISO 8601 date/time length if (network.epoch.length() != TIMESTAMP_LEN) { - return 0ULL; - }; + return 0ULL; + } std::string input(network.epoch); struct tm time; - time.tm_year = strtol(&input[OFFSET_YEAR], nullptr, BASE10) - YEAR_1900; - time.tm_mon = strtol(&input[OFFSET_MONTH], nullptr, BASE10) - 1; - time.tm_mday = strtol(&input[OFFSET_DAY], nullptr, BASE10); - time.tm_hour = strtol(&input[OFFSET_HOUR], nullptr, BASE10); - time.tm_min = strtol(&input[OFFSET_MINUTE], nullptr, BASE10); - time.tm_sec = strtol(&input[OFFSET_SECOND], nullptr, BASE10); - time.tm_isdst = 0; + time.tm_year = strtol(&input[OFFSET_YEAR], nullptr, BASE_10) - YEAR_1900; + time.tm_mon = strtol(&input[OFFSET_MONTH], nullptr, BASE_10) - 1; + time.tm_mday = strtol(&input[OFFSET_DAY], nullptr, BASE_10); + time.tm_hour = strtol(&input[OFFSET_HOUR], nullptr, BASE_10); + time.tm_min = strtol(&input[OFFSET_MINUTE], nullptr, BASE_10); + time.tm_sec = strtol(&input[OFFSET_SECOND], nullptr, BASE_10); + time.tm_isdst = 0; return mktime(&time); - } +} - // Get the current Network time. - uint64_t Slot::time(const Ark::Crypto::Network& network) { +//////////////////////////////////////////////////////////////////////////////// +// Get the current Network time. +auto Slot::time(const Ark::Crypto::Network& network) -> uint64_t { return std::time(0) - epoch(network); - } +} - } // namespace Crypto - } // namespace Ark +} // namespace Crypto +} // namespace Ark -#endif +#endif // #if defined(ESP32) || defined(ESP8266) diff --git a/src/identities/address.cpp b/src/identities/address.cpp index dce87f79..b418b6ed 100644 --- a/src/identities/address.cpp +++ b/src/identities/address.cpp @@ -13,7 +13,9 @@ #include "interfaces/identities.hpp" #include "identities/keys.hpp" + #include "crypto/hash.hpp" + #include "utils/base58.hpp" #include "utils/str.hpp" @@ -21,69 +23,66 @@ namespace Ark { namespace Crypto { namespace identities { +//////////////////////////////////////////////////////////////////////////////// // Constructs an Address from a 20-byte PubkeyHash and Address Version. -Address::Address(const PubkeyHash& pubkeyHash, uint8_t version) - : pubkeyHash_(pubkeyHash), version_(version) {} - -/**/ +Address::Address(const PubkeyHash &pubkeyHash, uint8_t version) + : pubkeyHash_(pubkeyHash), version_(version) {} +//////////////////////////////////////////////////////////////////////////////// // Constructs an Address from a 34-character Address string. Address::Address(const char* addressString) : pubkeyHash_(), version_() { - if (Base58::validate(addressString, ADDRESS_STRING_LEN)) { - const auto hashPair = Base58::getHashPair(addressString); - this->pubkeyHash_ = hashPair.pubkeyHash; - this->version_ = hashPair.version; - }; + if (Base58::validate(addressString, ADDRESS_STR_LEN)) { + const auto hashPair = Base58::getHashPair(addressString); + this->pubkeyHash_ = hashPair.pubkeyHash; + this->version_ = hashPair.version; + } } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns the Base58 Address Version-byte. -uint8_t Address::version() const noexcept { return this->version_; } - -/**/ +auto Address::version() const noexcept -> uint8_t { return this->version_; } +//////////////////////////////////////////////////////////////////////////////// // Returns the internal 20-byte Ripemd160 PublicKey Hash. -PubkeyHash Address::toBytes() const noexcept { return this->pubkeyHash_; } - -/**/ +auto Address::toBytes() const noexcept -> PubkeyHash { + return this->pubkeyHash_; +} +//////////////////////////////////////////////////////////////////////////////// // Returns a 34-character formatted Address string. -std::string Address::toString() const { - return Base58::parseHash(this->pubkeyHash_.data(), this->version_); +auto Address::toString() const -> std::string { + return Base58::parsePubkeyHash(this->pubkeyHash_.data(), this->version_); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns an Address object from a given Passphrase and Address Version. -Address Address::fromPassphrase(const char* passphrase, uint8_t version) { - return fromPublicKey(Keys::fromPassphrase(passphrase).publicKey.data(), - version); +auto Address::fromPassphrase(const char *passphrase, uint8_t version) + -> Address { + return fromPublicKey(Keys::fromPassphrase(passphrase).publicKey.data(), + version); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns an Address object from PublicKey-bytes and an Address Version. -Address Address::fromPublicKey(const uint8_t* publicKeyBytes, - uint8_t version) { - return { Hash::ripemd160(publicKeyBytes), version }; +auto Address::fromPublicKey(const uint8_t *publicKeyBytes, uint8_t version) + -> Address { + return { Hash::ripemd160(publicKeyBytes), version }; } -/**/ +//////////////////////////////////////////////////////////////////////////////// // Returns an Address object from PrivateKey-bytes and an Address Version. -Address Address::fromPrivateKey(const uint8_t* privateKeyBytes, - uint8_t version) { - return fromPublicKey(Keys::fromPrivateKey(privateKeyBytes).publicKey.data(), - version); +auto Address::fromPrivateKey(const uint8_t *privateKeyBytes, uint8_t version) + -> Address { + return fromPublicKey(Keys::fromPrivateKey(privateKeyBytes).publicKey.data(), + version); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Validates an Address object. -bool Address::validate(const Address& address, uint8_t version) { - const auto hashPair = Base58::getHashPair(address.toString().c_str()); - return hashPair.version == version; +auto Address::validate(const Address &address, uint8_t version) -> bool { + const auto hashPair = Base58::getHashPair(address.toString().c_str()); + return hashPair.version == version; } } // namespace identities diff --git a/src/identities/keys.cpp b/src/identities/keys.cpp index 13ecd160..ef610ab3 100644 --- a/src/identities/keys.cpp +++ b/src/identities/keys.cpp @@ -9,10 +9,11 @@ #include "identities/keys.hpp" -#include +#include #include "crypto/curve.hpp" #include "crypto/hash.hpp" + #include "utils/base58.hpp" #include "utils/hex.hpp" #include "utils/str.hpp" @@ -21,49 +22,48 @@ namespace Ark { namespace Crypto { namespace identities { +//////////////////////////////////////////////////////////////////////////////// // Returns KeyPair from a given passphrase. -KeyPair Keys::fromPassphrase(const char* passphrase) { - return fromPrivateKey(PrivateKey::fromPassphrase(passphrase).data()); +auto Keys::fromPassphrase(const char *passphrase) -> KeyPair { + return fromPrivateKey(PrivateKey::fromPassphrase(passphrase).data()); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns KeyPair from PrivateKey bytes. -KeyPair Keys::fromPrivateKey(const uint8_t* privateKeyBytes) { - PrivateKeyBytes byteArray {}; - std::copy_n(privateKeyBytes, PRIVATEKEY_BYTE_LEN, byteArray.begin()); - return { byteArray, PublicKey::fromPrivateKey(privateKeyBytes) }; -} +auto Keys::fromPrivateKey(const uint8_t *privateKeyBytes) -> KeyPair { + PrivateKeyBytes privateKey {}; + std::copy_n(privateKeyBytes, + PRIVATEKEY_BYTE_LEN, + privateKey.begin()); -/**/ + return { privateKey, PublicKey::fromPrivateKey(privateKeyBytes) }; +} +//////////////////////////////////////////////////////////////////////////////// // Returns KeyPair from a given WIF string. -KeyPair Keys::fromWif(const char* wif) { - return fromPrivateKey(Keys::PrivateKey::fromWif(wif, nullptr).data()); +auto Keys::fromWif(const char *wif) -> KeyPair { + return fromPrivateKey(Keys::PrivateKey::fromWif(wif, nullptr).data()); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns a PrivateKey 32-byte array from a passphrase. -PrivateKeyBytes Keys::PrivateKey::fromPassphrase( - const char* passphrase) { - return Hash::sha256(StringToBytes(passphrase).data(), strlenSafe(passphrase)); +auto Keys::PrivateKey::fromPassphrase(const char *passphrase) + -> PrivateKeyBytes { + return Hash::sha256(StringToBytes(passphrase).data(), strlenSafe(passphrase)); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns a PrivateKey 32-byte array from a 52-char WIF string. -PrivateKeyBytes Keys::PrivateKey::fromWif(const char* wif, - uint8_t* outVersion) { - return Base58::parseWif(wif, outVersion); +auto Keys::PrivateKey::fromWif(const char *wif, + uint8_t *outVersion) -> PrivateKeyBytes { + return Base58::parseWif(wif, outVersion); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns a 33-byte PublicKey array from a 32-byte PrivateKey. -PublicKeyBytes Keys::PublicKey::fromPrivateKey( - const uint8_t* privateKeyBytes) { - return Curve::PublicKey::compute(privateKeyBytes); +auto Keys::PublicKey::fromPrivateKey(const uint8_t *privateKeyBytes) + -> PublicKeyBytes { + return Curve::PublicKey::compute(privateKeyBytes); } } // namespace identities diff --git a/src/identities/privatekey.cpp b/src/identities/privatekey.cpp index 142bebf2..3880a12f 100644 --- a/src/identities/privatekey.cpp +++ b/src/identities/privatekey.cpp @@ -12,7 +12,9 @@ #include #include "interfaces/identities.hpp" + #include "identities/keys.hpp" + #include "utils/hex.hpp" #include "utils/str.hpp" @@ -20,37 +22,36 @@ namespace Ark { namespace Crypto { namespace identities { +//////////////////////////////////////////////////////////////////////////////// // Construct a PrivateKey object directly from a 32-byte PrivateKey array. -PrivateKey::PrivateKey(const PrivateKeyBytes& privateKeyBytes) - : privateKeyBytes_(privateKeyBytes) {} - -/**/ +PrivateKey::PrivateKey(const PrivateKeyBytes &privateKeyBytes) + : privateKeyBytes_(privateKeyBytes) {} +//////////////////////////////////////////////////////////////////////////////// // Returns the 32-byte PrivateKey representation. -PrivateKeyBytes PrivateKey::toBytes() const noexcept{ return this->privateKeyBytes_; } - -/**/ +auto PrivateKey::toBytes() const noexcept -> PrivateKeyBytes { + return this->privateKeyBytes_; +} +//////////////////////////////////////////////////////////////////////////////// // Returns a 64-character Hex-represented-string of a PrivateKey. -std::string PrivateKey::toString() const { - return BytesToHex(this->privateKeyBytes_.begin(), - this->privateKeyBytes_.end()); +auto PrivateKey::toString() const -> std::string { + return BytesToHex(this->privateKeyBytes_.begin(), + this->privateKeyBytes_.end()); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns a PrivateKey object from a Passphrase. -PrivateKey PrivateKey::fromPassphrase(const char* passphrase) { - return PrivateKey(Keys::fromPassphrase(passphrase).privateKey); +auto PrivateKey::fromPassphrase(const char *passphrase) -> PrivateKey { + return PrivateKey(Keys::fromPassphrase(passphrase).privateKey); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns a PrivateKey object from a 64-char PrivateKey Hex string. -PrivateKey PrivateKey::fromHex(const char* privateKeyHex) { - return strlenSafe(privateKeyHex) == PRIVATEKEY_STRING_LEN +auto PrivateKey::fromHex(const char *privateKeyHex) -> PrivateKey { + return strlenSafe(privateKeyHex) == PRIVATEKEY_STRING_LEN ? PrivateKey(HexToBytesArray<>(privateKeyHex)) - : PrivateKey({}); + : PrivateKey(PrivateKeyBytes()); } } // namespace identities diff --git a/src/identities/publickey.cpp b/src/identities/publickey.cpp index 89408e43..39a4abfd 100644 --- a/src/identities/publickey.cpp +++ b/src/identities/publickey.cpp @@ -12,8 +12,11 @@ #include #include "interfaces/identities.hpp" + #include "identities/keys.hpp" + #include "crypto/curve.hpp" + #include "utils/hex.hpp" #include "utils/str.hpp" @@ -21,45 +24,45 @@ namespace Ark { namespace Crypto { namespace identities { +//////////////////////////////////////////////////////////////////////////////// // Construct a PublicKey object from a 33-byte PublicKey array. -PublicKey::PublicKey(const PublicKeyBytes& publicKeyBytes) - : publicKeyBytes_(publicKeyBytes) {} - -/**/ +PublicKey::PublicKey(const PublicKeyBytes &publicKeyBytes) + : publicKeyBytes_(publicKeyBytes) {} +//////////////////////////////////////////////////////////////////////////////// // Returns a 33-byte Compressed PublicKey array. -PublicKeyBytes PublicKey::toBytes() const noexcept { return this->publicKeyBytes_; } - -/**/ +auto PublicKey::toBytes() const noexcept -> PublicKeyBytes { + return this->publicKeyBytes_; +} +//////////////////////////////////////////////////////////////////////////////// // Returns a 32-character Hex-represented-string of a PublicKey. -std::string PublicKey::toString() const { - return BytesToHex(this->publicKeyBytes_.begin(), - this->publicKeyBytes_.end()); +auto PublicKey::toString() const -> std::string { + return BytesToHex(this->publicKeyBytes_.begin(), + this->publicKeyBytes_.end()); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns a PublicKey object from a given Passphrase string. -PublicKey PublicKey::fromPassphrase(const char* passphrase) { - return PublicKey(Keys::PublicKey::fromPrivateKey( - Keys::PrivateKey::fromPassphrase(passphrase).data())); +auto PublicKey::fromPassphrase(const char *passphrase) -> PublicKey { + return PublicKey( + Keys::PublicKey::fromPrivateKey( + Keys::PrivateKey::fromPassphrase(passphrase).data())); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns a PublicKey object from a 64-char PublicKey Hex string. -PublicKey PublicKey::fromHex(const char* publicKeyHex) { - if (strlenSafe(publicKeyHex) != PUBLICKEY_COMPRESSED_STRING_LEN) { - return PublicKey({}); - }; +auto PublicKey::fromHex(const char *publicKeyHex) -> PublicKey { + if (strlenSafe(publicKeyHex) != PUBLICKEY_COMPRESSED_STR_LEN) { + return PublicKey(PublicKeyBytes()); + } - const auto publicKeyBytes = - HexToBytesArray(publicKeyHex); + const auto publicKeyBytes = + HexToBytesArray(publicKeyHex); - return Curve::PublicKey::validate(publicKeyBytes.data()) + return Curve::PublicKey::validate(publicKeyBytes.data()) ? PublicKey(publicKeyBytes) - : PublicKey({}); + : PublicKey(PublicKeyBytes()); } } // namespace identities diff --git a/src/identities/wif.cpp b/src/identities/wif.cpp index 05efc366..64d01228 100644 --- a/src/identities/wif.cpp +++ b/src/identities/wif.cpp @@ -12,50 +12,48 @@ #include #include "interfaces/identities.hpp" + #include "identities/keys.hpp" + #include "utils/base58.hpp" namespace Ark { namespace Crypto { namespace identities { +//////////////////////////////////////////////////////////////////////////////// // Constructs a Wif object using a PrivateKey and Wif Version-byte. -Wif::Wif(const PrivateKeyBytes& privateKeyBytes, uint8_t version) - : privateKeyBytes_(privateKeyBytes), version_(version) {} - -/**/ +Wif::Wif(const PrivateKeyBytes &privateKeyBytes, uint8_t version) + : privateKeyBytes_(privateKeyBytes), version_(version) {} +//////////////////////////////////////////////////////////////////////////////// // Constructs a Wif object from a 52(+ 1)-char string and Wif Version-byte. -Wif::Wif(const char* wif) : privateKeyBytes_(), version_() { - if (Base58::validate(wif, WIF_STRING_LEN)) { - this->privateKeyBytes_ = Keys::PrivateKey::fromWif(wif, &this->version_); - }; +Wif::Wif(const char *wif) : privateKeyBytes_(), version_() { + if (Base58::validate(wif, WIF_STRING_LEN)) { + this->privateKeyBytes_ = Keys::PrivateKey::fromWif(wif, &this->version_); + } } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns the Wif Network Version-byte. -uint8_t Wif::version() const noexcept { return this->version_; }; +auto Wif::version() const noexcept -> uint8_t { return this->version_; }; -/**/ - -// Returns the Wifs internal privateKey -PrivateKeyBytes Wif::toBytes() const noexcept { - return this->privateKeyBytes_; +//////////////////////////////////////////////////////////////////////////////// +// Returns the Wif's internal privateKey +auto Wif::toBytes() const noexcept -> PrivateKeyBytes { + return this->privateKeyBytes_; } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns the 52(+ 1)-char Base58-encoded Wif string. -std::string Wif::toString() const { - return Base58::getWif(this->privateKeyBytes_.data(), this->version_); +auto Wif::toString() const -> std::string { + return Base58::getWif(this->privateKeyBytes_.data(), this->version_); } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns a Wif object from a Passphrase and Wif Version-byte. -Wif Wif::fromPassphrase(const char* passphrase, uint8_t version) { - return { Keys::PrivateKey::fromPassphrase(passphrase), version }; +auto Wif::fromPassphrase(const char *passphrase, uint8_t version) -> Wif { + return { Keys::PrivateKey::fromPassphrase(passphrase), version }; } } // namespace identities diff --git a/src/include/cpp-crypto/common/configuration.hpp b/src/include/cpp-crypto/common/configuration.hpp index 32e6bd77..b35284f4 100644 --- a/src/include/cpp-crypto/common/configuration.hpp +++ b/src/include/cpp-crypto/common/configuration.hpp @@ -7,8 +7,8 @@ * file that was distributed with this source code. **/ -#ifndef COMMON_CONFIGURATION_HPP -#define COMMON_CONFIGURATION_HPP +#ifndef ARK_COMMON_CONFIGURATION_HPP +#define ARK_COMMON_CONFIGURATION_HPP #include "managers/network_manager.hpp" #include "managers/fee_manager.hpp" @@ -16,17 +16,21 @@ namespace Ark { namespace Crypto { +//////////////////////////////////////////////////////////////////////////////// +// Network Configuration Class class Configuration : public managers::NetworkManager, public managers::FeeManager { - public: - // Default initialization: ARK Devnet w/StaticFees - Configuration() = default; - explicit Configuration(const Network& network); - explicit Configuration(const FeePolicy& policy); - Configuration(const Network& network, const FeePolicy& policy); + public: + //////////////////////////////////////////////////////////////////////////// + // Default initialization: ARK Devnet w/StaticFees + Configuration() = default; + explicit Configuration(const Network &network); + explicit Configuration(const FeePolicy &policy); + Configuration(const Network &network, const FeePolicy &policy); - bool operator==(const Configuration& other) const; - bool operator!=(const Configuration& other) const; + //////////////////////////////////////////////////////////////////////////// + bool operator==(const Configuration &other) const; + bool operator!=(const Configuration &other) const; }; } // namespace Crypto diff --git a/src/include/cpp-crypto/common/fee_policy.hpp b/src/include/cpp-crypto/common/fee_policy.hpp index 7b29ba20..b70dab12 100644 --- a/src/include/cpp-crypto/common/fee_policy.hpp +++ b/src/include/cpp-crypto/common/fee_policy.hpp @@ -7,8 +7,8 @@ * file that was distributed with this source code. **/ -#ifndef COMMON_FEE_POLICY_HPP -#define COMMON_FEE_POLICY_HPP +#ifndef ARK_COMMON_FEE_POLICY_HPP +#define ARK_COMMON_FEE_POLICY_HPP #include #include @@ -16,9 +16,9 @@ namespace Ark { namespace Crypto { -static const uint64_t AMOUNT_ZERO = 0ULL; - -typedef std::vector FeePolicy; +//////////////////////////////////////////////////////////////////////////////// +// Fee Policy Container +using FeePolicy = std::vector; } // namespace Crypto } // namespace Ark diff --git a/src/include/cpp-crypto/common/network.hpp b/src/include/cpp-crypto/common/network.hpp index 237db098..35ad69b4 100644 --- a/src/include/cpp-crypto/common/network.hpp +++ b/src/include/cpp-crypto/common/network.hpp @@ -7,23 +7,27 @@ * file that was distributed with this source code. **/ -#ifndef COMMON_NETWORK_H -#define COMMON_NETWORK_H +#ifndef ARK_COMMON_NETWORK_HPP +#define ARK_COMMON_NETWORK_HPP #include namespace Ark { namespace Crypto { +//////////////////////////////////////////////////////////////////////////////// +// Network Data Model struct Network { - std::string nethash; - uint8_t slip44; - uint8_t wif; - uint8_t version; - std::string epoch; + //////////////////////////////////////////////////////////////////////////// + std::string nethash; + uint8_t slip44; + uint8_t wif; + uint8_t version; + std::string epoch; - bool operator==(const Network& other) const; - bool operator!=(const Network& other) const; + //////////////////////////////////////////////////////////////////////////// + bool operator==(const Network &other) const; + bool operator!=(const Network &other) const; }; } // namespace Crypto diff --git a/src/include/cpp-crypto/crypto/message.hpp b/src/include/cpp-crypto/crypto/message.hpp index 1a4054d5..8dd27f7e 100644 --- a/src/include/cpp-crypto/crypto/message.hpp +++ b/src/include/cpp-crypto/crypto/message.hpp @@ -7,8 +7,8 @@ * file that was distributed with this source code. **/ -#ifndef CRYPTO_MESSAGE_HPP -#define CRYPTO_MESSAGE_HPP +#ifndef ARK_CRYPTO_MESSAGE_HPP +#define ARK_CRYPTO_MESSAGE_HPP #include #include @@ -19,24 +19,36 @@ namespace Ark { namespace Crypto { +//////////////////////////////////////////////////////////////////////////////// +// Message Class class Message { - public: - std::string message; - PublicKeyBytes publicKey; - std::vector signature; + public: + //////////////////////////////////////////////////////////////////////////// + std::string message; + PublicKeyBytes publicKey; + std::vector signature; - Message(); - Message(std::string message, - const PublicKeyBytes& publicKeyBytes, - std::vector signature); + //////////////////////////////////////////////////////////////////////////// + Message(); + Message(std::string message, + const uint8_t *publicKeyBytes, + const uint8_t *signature); - bool sign(const std::string& message, const std::string& passphrase); - bool verify() const; + //////////////////////////////////////////////////////////////////////////// + bool sign(const std::string &message, const std::string &passphrase); - std::map toArray() const; - std::string toJson() const; + //////////////////////////////////////////////////////////////////////////// + bool verify() const; + + //////////////////////////////////////////////////////////////////////////// + std::map toMap() const; + + //////////////////////////////////////////////////////////////////////////// + std::string toJson() const; }; +//////////////////////////////////////////////////////////////////////////////// + } // namespace Crypto } // namespace Ark diff --git a/src/include/cpp-crypto/crypto/slot.hpp b/src/include/cpp-crypto/crypto/slot.hpp index 3f0e1d1e..8804e3e8 100644 --- a/src/include/cpp-crypto/crypto/slot.hpp +++ b/src/include/cpp-crypto/crypto/slot.hpp @@ -7,8 +7,8 @@ * file that was distributed with this source code. **/ -#ifndef CRYPTO_SLOTS_HPP -#define CRYPTO_SLOTS_HPP +#ifndef ARK_CRYPTO_SLOTS_HPP +#define ARK_CRYPTO_SLOTS_HPP #include @@ -17,10 +17,11 @@ namespace Ark { namespace Crypto { +//////////////////////////////////////////////////////////////////////////////// class Slot { - public: - static uint64_t epoch(const Network& network); - static uint64_t time(const Network& network); + public: + static uint64_t epoch(const Network &network); + static uint64_t time(const Network &network); }; } // namespace Crypto diff --git a/src/include/cpp-crypto/defaults/static_fees.hpp b/src/include/cpp-crypto/defaults/static_fees.hpp deleted file mode 100644 index 70d1ab88..00000000 --- a/src/include/cpp-crypto/defaults/static_fees.hpp +++ /dev/null @@ -1,33 +0,0 @@ -/** - * This file is part of Ark Cpp Crypto. - * - * (c) Ark Ecosystem - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - **/ - -#ifndef DEFAULTS_TRANSACTIONS_STATIC_FEES_HPP -#define DEFAULTS_TRANSACTIONS_STATIC_FEES_HPP - -#include "common/fee_policy.hpp" - -namespace Ark { -namespace Crypto { - -const FeePolicy StaticFeePolicy { - 10000000ULL, // Transfer - 500000000ULL, // SecondSignatureRegistration - 2500000000ULL, // DelegateRegistration - 100000000ULL, // Vote - 500000000ULL, // MultiSignatureRegistration - 0ULL, // Ipfs - 0ULL, // TimelockTransfer - 0ULL, // MultiPayment - 2500000000ULL // DelegateResignation -}; - -} // namespace Crypto -} // namespace Ark - -#endif diff --git a/src/include/cpp-crypto/defaults/transaction_types.hpp b/src/include/cpp-crypto/defaults/transaction_types.hpp deleted file mode 100644 index eb757ea3..00000000 --- a/src/include/cpp-crypto/defaults/transaction_types.hpp +++ /dev/null @@ -1,33 +0,0 @@ -/** - * This file is part of Ark Cpp Crypto. - * - * (c) Ark Ecosystem - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - **/ - -#ifndef DEFAULTS_TRANSACTIONS_TYPES_HPP -#define DEFAULTS_TRANSACTIONS_TYPES_HPP - -#include - -namespace Ark { -namespace Crypto { - -enum TransactionTypes : uint8_t { - Transfer = 0, - SecondSignatureRegistration = 1, - DelegateRegistration = 2, - Vote = 3, - MultiSignatureRegistration = 4, - Ipfs = 5, - TimelockTransfer = 6, - MultiPayment = 7, - DelegateResignation = 8 -}; - -} // namespace Crypto -} // namespace Ark - -#endif diff --git a/src/include/cpp-crypto/identities/address.hpp b/src/include/cpp-crypto/identities/address.hpp index d644b98b..af9b7551 100644 --- a/src/include/cpp-crypto/identities/address.hpp +++ b/src/include/cpp-crypto/identities/address.hpp @@ -7,8 +7,8 @@ * file that was distributed with this source code. **/ -#ifndef IDENTITIES_ADDRESS_HPP -#define IDENTITIES_ADDRESS_HPP +#ifndef ARK_IDENTITIES_ADDRESS_HPP +#define ARK_IDENTITIES_ADDRESS_HPP #include @@ -18,25 +18,37 @@ namespace Ark { namespace Crypto { namespace identities { +//////////////////////////////////////////////////////////////////////////////// class Address { - public: - Address(const PubkeyHash& pubkeyHash, uint8_t version); - explicit Address(const char* addressString); + public: + //////////////////////////////////////////////////////////////////////////// + Address(const PubkeyHash &pubkeyHash, uint8_t version); + explicit Address(const char *addressString); - uint8_t version() const noexcept; + //////////////////////////////////////////////////////////////////////////// + uint8_t version() const noexcept; - PubkeyHash toBytes() const noexcept; - std::string toString() const; + //////////////////////////////////////////////////////////////////////////// + PubkeyHash toBytes() const noexcept; + std::string toString() const; - static Address fromPassphrase(const char* passphrase, uint8_t version); - static Address fromPublicKey(const uint8_t* publicKeyBytes, uint8_t version); - static Address fromPrivateKey(const uint8_t* privateKeyBytes, uint8_t version); + //////////////////////////////////////////////////////////////////////////// + static Address fromPassphrase(const char *passphrase, + uint8_t version); - static bool validate(const Address& address, uint8_t version); + static Address fromPublicKey(const uint8_t *publicKeyBytes, + uint8_t version); - private: - PubkeyHash pubkeyHash_; - uint8_t version_; + static Address fromPrivateKey(const uint8_t *privateKeyBytes, + uint8_t version); + + //////////////////////////////////////////////////////////////////////////// + static bool validate(const Address& address, uint8_t version); + + private: + //////////////////////////////////////////////////////////////////////////// + PubkeyHash pubkeyHash_; + uint8_t version_; }; } // namespace identities diff --git a/src/include/cpp-crypto/identities/keys.hpp b/src/include/cpp-crypto/identities/keys.hpp index 72007416..7027a305 100644 --- a/src/include/cpp-crypto/identities/keys.hpp +++ b/src/include/cpp-crypto/identities/keys.hpp @@ -7,8 +7,8 @@ * file that was distributed with this source code. **/ -#ifndef IDENTITIES_KEYS_HPP -#define IDENTITIES_KEYS_HPP +#ifndef ARK_IDENTITIES_KEYS_HPP +#define ARK_IDENTITIES_KEYS_HPP #include "interfaces/identities.hpp" @@ -16,19 +16,22 @@ namespace Ark { namespace Crypto { namespace identities { +//////////////////////////////////////////////////////////////////////////////// struct Keys { - static KeyPair fromPassphrase(const char* passphrase); - static KeyPair fromPrivateKey(const uint8_t* privateKeyBytes); - static KeyPair fromWif(const char* wif); - - struct PrivateKey { - static PrivateKeyBytes fromPassphrase(const char* passphrase); - static PrivateKeyBytes fromWif(const char* wif, uint8_t* outVersion); - }; - - struct PublicKey { - static PublicKeyBytes fromPrivateKey(const uint8_t* privateKeyBytes); - }; + static KeyPair fromPassphrase(const char *passphrase); + static KeyPair fromPrivateKey(const uint8_t *privateKeyBytes); + static KeyPair fromWif(const char *wif); + + //////////////////////////////////////////////////////////////////////////// + struct PrivateKey { + static PrivateKeyBytes fromPassphrase(const char *passphrase); + static PrivateKeyBytes fromWif(const char *wif, uint8_t *outVersion); + }; + + //////////////////////////////////////////////////////////////////////////// + struct PublicKey { + static PublicKeyBytes fromPrivateKey(const uint8_t *privateKeyBytes); + }; }; } // namespace identities diff --git a/src/include/cpp-crypto/identities/privatekey.hpp b/src/include/cpp-crypto/identities/privatekey.hpp index e381fe91..afc6a1e7 100644 --- a/src/include/cpp-crypto/identities/privatekey.hpp +++ b/src/include/cpp-crypto/identities/privatekey.hpp @@ -7,8 +7,8 @@ * file that was distributed with this source code. **/ -#ifndef IDENTITIES_PRIVATEKEY_HPP -#define IDENTITIES_PRIVATEKEY_HPP +#ifndef ARK_IDENTITIES_PRIVATEKEY_HPP +#define ARK_IDENTITIES_PRIVATEKEY_HPP #include @@ -18,18 +18,23 @@ namespace Ark { namespace Crypto { namespace identities { +//////////////////////////////////////////////////////////////////////////////// class PrivateKey { - public: - explicit PrivateKey(const PrivateKeyBytes& privateKeyBytes); + public: + //////////////////////////////////////////////////////////////////////////// + explicit PrivateKey(const PrivateKeyBytes &privateKeyBytes); - PrivateKeyBytes toBytes() const noexcept; - std::string toString() const; + //////////////////////////////////////////////////////////////////////////// + PrivateKeyBytes toBytes() const noexcept; + std::string toString() const; - static PrivateKey fromPassphrase(const char* passphrase); - static PrivateKey fromHex(const char* privateKeyHex); + //////////////////////////////////////////////////////////////////////////// + static PrivateKey fromPassphrase(const char *passphrase); + static PrivateKey fromHex(const char *privateKeyHex); - private: - PrivateKeyBytes privateKeyBytes_; + private: + //////////////////////////////////////////////////////////////////////////// + PrivateKeyBytes privateKeyBytes_; }; } // namespace identities diff --git a/src/include/cpp-crypto/identities/publickey.hpp b/src/include/cpp-crypto/identities/publickey.hpp index 2f94ca5a..1a4be021 100644 --- a/src/include/cpp-crypto/identities/publickey.hpp +++ b/src/include/cpp-crypto/identities/publickey.hpp @@ -7,8 +7,8 @@ * file that was distributed with this source code. **/ -#ifndef IDENTITIES_PUBLICKEY_HPP -#define IDENTITIES_PUBLICKEY_HPP +#ifndef ARK_IDENTITIES_PUBLICKEY_HPP +#define ARK_IDENTITIES_PUBLICKEY_HPP #include @@ -18,20 +18,26 @@ namespace Ark { namespace Crypto { namespace identities { +//////////////////////////////////////////////////////////////////////////////// class PublicKey { - public: - explicit PublicKey(const PublicKeyBytes& publicKeyBytes); + public: + //////////////////////////////////////////////////////////////////////////// + explicit PublicKey(const PublicKeyBytes &publicKeyBytes); - PublicKeyBytes toBytes() const noexcept; - std::string toString() const; + //////////////////////////////////////////////////////////////////////////// + PublicKeyBytes toBytes() const noexcept; + std::string toString() const; - static PublicKey fromPassphrase(const char* passphrase); - static PublicKey fromHex(const char* publicKeyHex); + //////////////////////////////////////////////////////////////////////////// + static PublicKey fromPassphrase(const char *passphrase); + static PublicKey fromHex(const char *publicKeyHex); - private: - PublicKeyBytes publicKeyBytes_; + private: + //////////////////////////////////////////////////////////////////////////// + PublicKeyBytes publicKeyBytes_; }; + } // namespace identities } // namespace Crypto } // namespace Ark diff --git a/src/include/cpp-crypto/identities/wif.hpp b/src/include/cpp-crypto/identities/wif.hpp index 23339fe0..4e05be03 100644 --- a/src/include/cpp-crypto/identities/wif.hpp +++ b/src/include/cpp-crypto/identities/wif.hpp @@ -7,8 +7,8 @@ * file that was distributed with this source code. **/ -#ifndef IDENTITIES_WIF_HPP -#define IDENTITIES_WIF_HPP +#ifndef ARK_IDENTITIES_WIF_HPP +#define ARK_IDENTITIES_WIF_HPP #include @@ -18,20 +18,25 @@ namespace Ark { namespace Crypto { namespace identities { +//////////////////////////////////////////////////////////////////////////////// class Wif { - public: - Wif(const PrivateKeyBytes& privateKeyBytes, uint8_t version); - explicit Wif(const char* wif); - - uint8_t version() const noexcept; - PrivateKeyBytes toBytes() const noexcept; - std::string toString() const; - - static Wif fromPassphrase(const char* passphrase, uint8_t version); - - private: - PrivateKeyBytes privateKeyBytes_; - uint8_t version_; + public: + //////////////////////////////////////////////////////////////////////////// + Wif(const PrivateKeyBytes &privateKeyBytes, uint8_t version); + explicit Wif(const char *wif); + + //////////////////////////////////////////////////////////////////////////// + uint8_t version() const noexcept; + PrivateKeyBytes toBytes() const noexcept; + std::string toString() const; + + //////////////////////////////////////////////////////////////////////////// + static Wif fromPassphrase(const char *passphrase, uint8_t version); + + private: + //////////////////////////////////////////////////////////////////////////// + PrivateKeyBytes privateKeyBytes_; + uint8_t version_; }; } // namespace identities diff --git a/src/include/cpp-crypto/interfaces/constants.h b/src/include/cpp-crypto/interfaces/constants.h new file mode 100644 index 00000000..b9eeff46 --- /dev/null +++ b/src/include/cpp-crypto/interfaces/constants.h @@ -0,0 +1,91 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_INTERFACES_CONSTANTS_H +#define ARK_INTERFACES_CONSTANTS_H + +#include +#include + +#include "utils/platform.h" + +//////////////////////////////////////////////////////////////////////////////// +// Hash +constexpr size_t BASE58_ALPHABET_LEN = 58U; +constexpr size_t HASH_20_LEN = 20U; +constexpr size_t HASH_32_LEN = 32U; +constexpr size_t HASH_64_LEN = 64U; + +//////////////////////////////////////////////////////////////////////////////// +// Address +constexpr uint8_t ADDRESS_DEFAULT_VERSION = 0x1E; // (30) Base58 'D' | Devnet +constexpr size_t ADDRESS_HASH_LEN = 21U; +constexpr size_t ADDRESS_STR_LEN = 34U; + +//////////////////////////////////////////////////////////////////////////////// +// Privatekey +constexpr size_t PRIVATEKEY_BYTE_LEN = 32U; +constexpr size_t PRIVATEKEY_STRING_LEN = 64U; + +//////////////////////////////////////////////////////////////////////////////// +// PublicKey +constexpr size_t PUBLICKEY_COMPRESSED_LEN = 33U; +constexpr size_t PUBLICKEY_COMPRESSED_STR_LEN = 66U; +constexpr size_t PUBLICKEY_UNCOMPRESSED_LEN = 65U; + +//////////////////////////////////////////////////////////////////////////////// +// Signatures +constexpr size_t SIGNATURE_ECDSA_MIN = 70U; +constexpr size_t SIGNATURE_ECDSA_MAX = 72U; + +//////////////////////////////////////////////////////////////////////////////// +// Transactions +constexpr uint8_t TRANSACTION_VERSION_TYPE_1 = 0x01; +constexpr uint8_t TRANSACTION_VERSION_TYPE_2 = 0x02; + +constexpr uint32_t TRANSACTION_DEFAULT_HEADER = 0xFF; +constexpr uint32_t TRANSACTION_DEFAULT_TYPEGROUP = 1UL; + +// The Default and Max Tx Sizes aid in limiting the impact of dynamic allocation. +// Particularly in embedded environments, this also helps avoid overflow errors. +// +// These are currently used by Serializer and the MultiPayment Tx-type. +// They can be tweaked to suit whichever environment were developing for. +constexpr size_t TX_DEFAULT_SIZE = 512UL; + +#if defined(USE_IOT) + constexpr size_t TX_MAX_SIZE = 768UL; +#else + constexpr size_t TX_MAX_SIZE = 20480UL; +#endif + +//////////////////////////////////////////////////////////////////////////////// +// Wif +constexpr uint8_t WIF_DEFAULT_VERSION = 0xAA; // (170) Base58 'S' +constexpr size_t WIF_STRING_LEN = 52U; + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Map/Json Constants +constexpr auto KEY_ASSET_LABEL = "asset"; +const auto KEY_ASSET_SIZE = strlen(KEY_ASSET_LABEL); + +//////////////////////////////////////////////////////////////////////////////// +// Misc +constexpr uint8_t BASE_10 = 10U; + +constexpr size_t UINT64_MAX_CHARS = 20U; +constexpr size_t UINT32_MAX_CHARS = 10U; +constexpr size_t UINT16_MAX_CHARS = 5U; +constexpr size_t UINT8_MAX_CHARS = 3U; + +constexpr size_t HASH_32_MAX_CHARS = 2U * HASH_32_LEN; +constexpr size_t SIGNATURE_ECDSA_MAX_CHARS = 2U * SIGNATURE_ECDSA_MAX; + +#endif diff --git a/src/include/cpp-crypto/interfaces/identities.hpp b/src/include/cpp-crypto/interfaces/identities.hpp index b62114e1..d8c145ce 100644 --- a/src/include/cpp-crypto/interfaces/identities.hpp +++ b/src/include/cpp-crypto/interfaces/identities.hpp @@ -7,64 +7,81 @@ * file that was distributed with this source code. **/ -#ifndef INTERFACE_IDENTITIES_HPP -#define INTERFACE_IDENTITIES_HPP +#ifndef ARK_INTERFACE_IDENTITIES_HPP +#define ARK_INTERFACE_IDENTITIES_HPP #include +#include +#include + +#include "interfaces/constants.h" namespace Ark { namespace Crypto { -////////// Misc ////////// - -const size_t BASE58_ALPHABET_LEN = 58U; -const size_t HASH_20_BYTE_LEN = 20U; -const size_t HASH_32_BYTE_LEN = 32U; -const size_t HASH_64_BYTE_LEN = 64U; - -typedef std::array Hash32; - -////////// Identities ////////// - -// Address -const uint8_t ADDRESS_DEFAULT_VERSION = 0x1e; // (30) Base58 'D' | Devnet -const size_t ADDRESS_STRING_LEN = 34U; +//////////////////////////////////////////////////////////////////////////////// +// Hash +using Hash32 = std::array; +//////////////////////////////////////////////////////////////////////////////// // PubkeyHash // A 20 Byte Array. // Used by Ripemd160 as a hash of the PublicKey. // An Address is derived using a Network Version-byte and a PubkeyHash. -typedef std::array PubkeyHash; +using PubkeyHash = std::array; +//////////////////////////////////////////////////////////////////////////////// // PubkeyHashPair // // Base58 version byte. // Ripemd160 PubkeyHash. struct PubkeyHashPair { - uint8_t version; - PubkeyHash pubkeyHash; + uint8_t version { }; + PubkeyHash pubkeyHash { }; + + PubkeyHashPair() = default; + PubkeyHashPair(uint8_t version, const PubkeyHash &pubkeyHash) + : version(version) { + std::copy(pubkeyHash.begin(), + pubkeyHash.end(), + this->pubkeyHash.begin()); + } }; +//////////////////////////////////////////////////////////////////////////////// +// AddressHash - 21 Bytes +// NetworkVersion + PubkeyHash +// +// ex: 171dfc69b54c7fe901e91d5a9ab78388645e2427ea +using AddressHash = std::array; + +//////////////////////////////////////////////////////////////////////////////// // Privatekey -const size_t PRIVATEKEY_BYTE_LEN = 32U; -const size_t PRIVATEKEY_STRING_LEN = 64U; -typedef std::array PrivateKeyBytes; +using PrivateKeyBytes = std::array; +//////////////////////////////////////////////////////////////////////////////// // PublicKey -const size_t PUBLICKEY_COMPRESSED_BYTE_LEN = 33U; -const size_t PUBLICKEY_COMPRESSED_STRING_LEN = 66U; -const size_t PUBLICKEY_UNCOMPRESSED_BYTE_LEN = 65U; -typedef std::array PublicKeyBytes; -typedef std::array PublicKeyPoint; - -// Wif -const size_t WIF_DEFAULT_VERSION = 0xaa; // (170) Base58 'S' -const size_t WIF_STRING_LEN = 52U; +using PublicKeyBytes = std::array; +using PublicKeyPoint = std::array; +//////////////////////////////////////////////////////////////////////////////// // KeyPair struct KeyPair { - PrivateKeyBytes privateKey; - PublicKeyBytes publicKey; + //////////////////////////////////////////////////////////////////////////// + PrivateKeyBytes privateKey { }; + PublicKeyBytes publicKey { }; + + //////////////////////////////////////////////////////////////////////////// + KeyPair() = default; + KeyPair(const PrivateKeyBytes &privateKey, const PublicKeyBytes &publicKey) { + std::copy(privateKey.begin(), + privateKey.end(), + this->privateKey.begin()); + + std::copy(publicKey.begin(), + publicKey.end(), + this->publicKey.begin()); + } }; } // namespace Crypto diff --git a/src/include/cpp-crypto/networks/devnet.hpp b/src/include/cpp-crypto/networks/devnet.hpp index d39d1968..95fb7044 100644 --- a/src/include/cpp-crypto/networks/devnet.hpp +++ b/src/include/cpp-crypto/networks/devnet.hpp @@ -7,14 +7,15 @@ * file that was distributed with this source code. **/ -#ifndef NETWORKS_DEVNET_HPP -#define NETWORKS_DEVNET_HPP +#ifndef ARK_NETWORKS_DEVNET_HPP +#define ARK_NETWORKS_DEVNET_HPP #include "common/network.hpp" namespace Ark { namespace Crypto { +//////////////////////////////////////////////////////////////////////////////// // Devnet // Default ARK Development Network const Network Devnet { diff --git a/src/include/cpp-crypto/networks/mainnet.hpp b/src/include/cpp-crypto/networks/mainnet.hpp index 0b4b7ae6..f336546d 100644 --- a/src/include/cpp-crypto/networks/mainnet.hpp +++ b/src/include/cpp-crypto/networks/mainnet.hpp @@ -7,14 +7,15 @@ * file that was distributed with this source code. **/ -#ifndef NETWORKS_MAINNET_HPP -#define NETWORKS_MAINNET_HPP +#ifndef ARK_NETWORKS_MAINNET_HPP +#define ARK_NETWORKS_MAINNET_HPP #include "common/network.hpp" namespace Ark { namespace Crypto { +//////////////////////////////////////////////////////////////////////////////// // Mainnet // ARK Public Network const Network Mainnet { diff --git a/src/include/cpp-crypto/networks/testnet.hpp b/src/include/cpp-crypto/networks/testnet.hpp index ea182045..c2416c68 100644 --- a/src/include/cpp-crypto/networks/testnet.hpp +++ b/src/include/cpp-crypto/networks/testnet.hpp @@ -7,14 +7,15 @@ * file that was distributed with this source code. **/ -#ifndef NETWORKS_TESTNET_HPP -#define NETWORKS_TESTNET_HPP +#ifndef ARK_NETWORKS_TESTNET_HPP +#define ARK_NETWORKS_TESTNET_HPP #include "common/network.hpp" namespace Ark { namespace Crypto { +//////////////////////////////////////////////////////////////////////////////// // Testnet // ARK Test Network const Network Testnet { diff --git a/src/include/cpp-crypto/transactions/builder.h b/src/include/cpp-crypto/transactions/builder.h deleted file mode 100644 index 7d9401e3..00000000 --- a/src/include/cpp-crypto/transactions/builder.h +++ /dev/null @@ -1,95 +0,0 @@ -/** - * This file is part of Ark Cpp Crypto. - * - * (c) Ark Ecosystem - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - **/ - -#ifndef BUILDER_H -#define BUILDER_H - -#include -#include -#include - -#include "transactions/transaction.h" -#include "common/configuration.hpp" - -namespace Ark { -namespace Crypto { -namespace Transactions { -/**/ -class Builder { -public: - /** - * Builder::buildTransfer() - * - * note: The 'Type 0'/Transfer amount must be greater than 0 ARKtoshi. - * If amount is not > 0, Builder will return an empty Transaction object and - * validation will fail. - **/ - static Transaction buildTransfer( - std::string recipient, - uint64_t amount, - std::string vendorField, - const std::string& passphrase, - const std::string& secondPassphrase = "", - const Configuration& configuration = {}); - /**/ - - static Transaction buildSecondSignatureRegistration( - const std::string& passphrase, - const std::string& secondPassphrase = "", - const Configuration& configuration = {}); - - static Transaction buildDelegateRegistration( - std::string username, - const std::string& passphrase, - const std::string& secondPassphrase = "", - const Configuration& configuration = {}); - - static Transaction buildVote( - std::vector votes, - const std::string& passphrase, - const std::string& secondPassphrase = "", - const Configuration& configuration = {}); - - static Transaction buildMultiSignatureRegistration( - uint8_t min, - uint8_t lifetime, - std::vector& keysgroup, - const std::string& passphrase, - const std::string& secondPassphrase = "", - const Configuration& configuration = {}); - -private: - Builder(); - - static void sign( - Transaction& transaction, - const std::string& passphrase, - const std::string& secondPassphrase = "", - const Configuration& configuration = {}); - - Builder sign(const std::string& passphrase); - -private: - void serializeVendorField(const std::vector& bytes); - void serializeType(const std::vector& bytes); - void serializeTransfer(const std::vector& bytes); - void serializeSecondSignatureRegistration(const std::vector& bytes); - void serializeDelegateRegistration(const std::vector& bytes); - void serializeVote(const std::vector& bytes); - void serializeMultiSignatureRegistration(const std::vector& bytes); - void serializeSignatures(const std::vector& bytes); - - Transaction _transaction; -}; -/**/ -}; // namespace Transactions -}; // namespace Crypto -}; // namespace Ark - -#endif diff --git a/src/include/cpp-crypto/transactions/builders/builder.hpp b/src/include/cpp-crypto/transactions/builders/builder.hpp new file mode 100644 index 00000000..6226868a --- /dev/null +++ b/src/include/cpp-crypto/transactions/builders/builder.hpp @@ -0,0 +1,24 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_BUILDERS_HPP +#define ARK_TRANSACTIONS_BUILDERS_HPP + +#include "transactions/builders/transfer.hpp" +#include "transactions/builders/second_signature.hpp" +#include "transactions/builders/delegate_registration.hpp" +#include "transactions/builders/vote.hpp" +// #include "transactions/builders/multi_signature.hpp" +#include "transactions/builders/ipfs.hpp" +#include "transactions/builders/multi_payment.hpp" +#include "transactions/builders/htlc_lock.hpp" +#include "transactions/builders/htlc_claim.hpp" +#include "transactions/builders/htlc_refund.hpp" + +#endif diff --git a/src/include/cpp-crypto/transactions/builders/delegate_registration.hpp b/src/include/cpp-crypto/transactions/builders/delegate_registration.hpp new file mode 100644 index 00000000..5a22903e --- /dev/null +++ b/src/include/cpp-crypto/transactions/builders/delegate_registration.hpp @@ -0,0 +1,76 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_BUILDERS_DELEGATE_REGISTRATION_HPP +#define ARK_TRANSACTIONS_BUILDERS_DELEGATE_REGISTRATION_HPP + +#include +#include +#include + +#include "transactions/builders/common.hpp" + +#include "interfaces/constants.h" + +#include "utils/str.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { +namespace builder { + +//////////////////////////////////////////////////////////////////////////////// +// Forward Declaration +class DelegateRegistration; + +//////////////////////////////////////////////////////////////////////////////// +class DelegateRegistration : public Common { + public: + //////////////////////////////////////////////////////////////////////////// + // Username - uint8_t[] 3 <=> 20 Bytes + DelegateRegistration &username(const uint8_t *username, + const size_t &length) { + this->transaction.data.asset + .delegateRegistration.length = static_cast(length); + + std::copy_n(username, length, this->transaction.data.asset + .delegateRegistration + .username.begin()); + + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + // Username - std::string + DelegateRegistration &username(const std::string &username) { + this->transaction.data.asset + .delegateRegistration.length = + static_cast(username.length()); + + std::copy_n(StringToBytes(username).begin(), + username.length(), + this->transaction.data.asset + .delegateRegistration + .username.begin()); + + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + DelegateRegistration() { + this->transaction.data.type = DELEGATE_REGISTRATION_TYPE; + } +}; + +} // namespace builder +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/include/cpp-crypto/transactions/builders/htlc_claim.hpp b/src/include/cpp-crypto/transactions/builders/htlc_claim.hpp new file mode 100644 index 00000000..c29e0ba6 --- /dev/null +++ b/src/include/cpp-crypto/transactions/builders/htlc_claim.hpp @@ -0,0 +1,63 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_BUILDERS_HTLC_CLAIM_HPP +#define ARK_TRANSACTIONS_BUILDERS_HTLC_CLAIM_HPP + +#include +#include + +#include "transactions/builders/common.hpp" + +#include "interfaces/constants.h" + +namespace Ark { +namespace Crypto { +namespace transactions { +namespace builder { + +//////////////////////////////////////////////////////////////////////////////// +// Forward Declaration +class HtlcClaim; + +//////////////////////////////////////////////////////////////////////////////// +class HtlcClaim : public Common { + public: + //////////////////////////////////////////////////////////////////////////// + // Lock Transaction Id + HtlcClaim &lockTransactionId(const uint8_t *lockTransactionId) { + std::copy_n(lockTransactionId, + HASH_32_LEN, + this->transaction.data.asset.htlcClaim.id.begin()); + + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + // Unlock Secret + HtlcClaim &unlockSecret(const uint8_t *unlockSecret) { + std::copy_n(unlockSecret, + HASH_32_LEN, + this->transaction.data.asset.htlcClaim.secret.begin()); + + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + HtlcClaim() { + this->transaction.data.type = HTLC_CLAIM_TYPE; + } +}; + +} // namespace builder +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/include/cpp-crypto/transactions/builders/htlc_lock.hpp b/src/include/cpp-crypto/transactions/builders/htlc_lock.hpp new file mode 100644 index 00000000..0ed9ce70 --- /dev/null +++ b/src/include/cpp-crypto/transactions/builders/htlc_lock.hpp @@ -0,0 +1,101 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_BUILDERS_HTLC_LOCK_HPP +#define ARK_TRANSACTIONS_BUILDERS_HTLC_LOCK_HPP + +#include +#include +#include + +#include "transactions/builders/common.hpp" + +#include "interfaces/constants.h" + +#include "utils/base58.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { +namespace builder { + +//////////////////////////////////////////////////////////////////////////////// +// Forward Declaration +class HtlcLock; + +//////////////////////////////////////////////////////////////////////////////// +class HtlcLock : public Common { + public: + //////////////////////////////////////////////////////////////////////////// + // Amount + HtlcLock &amount(uint64_t amount) { + this->transaction.data.asset.htlcLock.amount = amount; + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + // Secret Hash + HtlcLock &secretHash(const uint8_t *secretHash) { + std::copy_n(secretHash, + HASH_32_LEN, + this->transaction.data.asset.htlcLock.secretHash.begin()); + + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + // Expiration Type + HtlcLock &expirationType(uint8_t expirationType) { + this->transaction.data.asset.htlcLock.expirationType = expirationType; + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + // Expiration + HtlcLock &expiration(uint32_t expiration) { + this->transaction.data.asset.htlcLock.expiration = expiration; + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + // RecipientId - uint8_t[21] (network.version + pubkeyHash) + HtlcLock &recipientId(const uint8_t *addressHash) { + std::copy_n(addressHash, + ADDRESS_HASH_LEN, + this->transaction.data.asset.htlcLock.recipientId.begin()); + + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + // RecipientId - std::string + HtlcLock &recipientId(const std::string &recipientId) { + const auto hashPair = Base58::getHashPair(recipientId.c_str()); + this->transaction.data.asset + .htlcLock.recipientId.at(0) = hashPair.version; + + std::copy(hashPair.pubkeyHash.begin(), + hashPair.pubkeyHash.end(), + &this->transaction.data.asset.htlcLock.recipientId.at(1)); + + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + HtlcLock() { + this->transaction.data.type = HTLC_LOCK_TYPE; + } +}; + +} // namespace builder +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/include/cpp-crypto/transactions/builders/htlc_refund.hpp b/src/include/cpp-crypto/transactions/builders/htlc_refund.hpp new file mode 100644 index 00000000..1b6ee16d --- /dev/null +++ b/src/include/cpp-crypto/transactions/builders/htlc_refund.hpp @@ -0,0 +1,53 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_BUILDERS_HTLC_REFUND_HPP +#define ARK_TRANSACTIONS_BUILDERS_HTLC_REFUND_HPP + +#include +#include + +#include "transactions/builders/common.hpp" + +#include "interfaces/constants.h" + +namespace Ark { +namespace Crypto { +namespace transactions { +namespace builder { + +//////////////////////////////////////////////////////////////////////////////// +// Forward Declaration +class HtlcRefund; + +//////////////////////////////////////////////////////////////////////////////// +class HtlcRefund : public Common { + public: + //////////////////////////////////////////////////////////////////////////// + // Lock Transaction Id + HtlcRefund &lockTransactionId(const uint8_t *lockTransactionId) { + std::copy_n(lockTransactionId, + HASH_32_LEN, + this->transaction.data.asset.htlcRefund.id.begin()); + + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + HtlcRefund() { + this->transaction.data.type = HTLC_REFUND_TYPE; + } +}; + +} // namespace builder +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/include/cpp-crypto/transactions/builders/ipfs.hpp b/src/include/cpp-crypto/transactions/builders/ipfs.hpp new file mode 100644 index 00000000..cd1863c0 --- /dev/null +++ b/src/include/cpp-crypto/transactions/builders/ipfs.hpp @@ -0,0 +1,53 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_BUILDERS_IPFS_HPP +#define ARK_TRANSACTIONS_BUILDERS_IPFS_HPP + +#include +#include + +#include "transactions/builders/common.hpp" + +#include "interfaces/constants.h" + +namespace Ark { +namespace Crypto { +namespace transactions { +namespace builder { + +//////////////////////////////////////////////////////////////////////////////// +// Forward Declaration +class Ipfs; + +//////////////////////////////////////////////////////////////////////////////// +class Ipfs : public Common { + public: + //////////////////////////////////////////////////////////////////////////// + // Ipfs hash + Ipfs &ipfs(const uint8_t *ipfsHash, const size_t &length) { + this->transaction.data.asset.ipfs.dag.insert( + this->transaction.data.asset.ipfs.dag.begin(), + ipfsHash, + ipfsHash + length); + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + Ipfs() { + this->transaction.data.type = IPFS_TYPE; + } +}; + +} // namespace builder +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/include/cpp-crypto/transactions/builders/multi_payment.hpp b/src/include/cpp-crypto/transactions/builders/multi_payment.hpp new file mode 100644 index 00000000..1374d082 --- /dev/null +++ b/src/include/cpp-crypto/transactions/builders/multi_payment.hpp @@ -0,0 +1,80 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_BUILDERS_MULTI_PAYMENT_HPP +#define ARK_TRANSACTIONS_BUILDERS_MULTI_PAYMENT_HPP + +#include +#include + +#include "transactions/builders/common.hpp" + +#include "interfaces/constants.h" + +namespace Ark { +namespace Crypto { +namespace transactions { +namespace builder { + +//////////////////////////////////////////////////////////////////////////////// +// Forward Declaration +class MultiPayment; + +//////////////////////////////////////////////////////////////////////////////// +class MultiPayment : public Common { + public: + //////////////////////////////////////////////////////////////////////////// + // Number of Payments + // + // - Should correspond to the 'Amounts' and 'Addresses' builder inputs counts. + // + // --- + MultiPayment &n_payments(uint16_t n_payments) { + this->transaction.data.asset.multiPayment.n_payments = n_payments; + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + // Amounts + // + // - Must be a std::vector -type. + // - Count of amounts should match the 'n_payments' builder input. + // - Must be ordered to match the corresponding 'Addresses' builder input. + // + // --- + MultiPayment &amounts(const std::vector &amounts) { + this->transaction.data.asset.multiPayment.amounts = amounts; + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + // Addresses + // + // - Must be a std::vector -type. + // - Count of amounts should match the 'n_payments' builder input. + // - Must be ordered to match the corresponding 'Amounts' builder input. + // + // --- + MultiPayment &addresses(const std::vector &addresses) { + this->transaction.data.asset.multiPayment.addresses = addresses; + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + MultiPayment() { + this->transaction.data.type = MULTI_PAYMENT_TYPE; + } +}; + +} // namespace builder +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/include/cpp-crypto/transactions/builders/second_signature.hpp b/src/include/cpp-crypto/transactions/builders/second_signature.hpp new file mode 100644 index 00000000..d31f432b --- /dev/null +++ b/src/include/cpp-crypto/transactions/builders/second_signature.hpp @@ -0,0 +1,55 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_BUILDERS_SECOND_SIGNATURE_HPP +#define ARK_TRANSACTIONS_BUILDERS_SECOND_SIGNATURE_HPP + +#include +#include + +#include "transactions/builders/common.hpp" + +#include "interfaces/constants.h" + +namespace Ark { +namespace Crypto { +namespace transactions { +namespace builder { + +//////////////////////////////////////////////////////////////////////////////// +// Forward Declaration +class SecondSignature; + +//////////////////////////////////////////////////////////////////////////////// +class SecondSignature : public Common { + public: + //////////////////////////////////////////////////////////////////////////// + // Second Signature PublicKey + SecondSignature &publicKey(const uint8_t *secondPublicKey) { + std::copy_n(secondPublicKey, + PUBLICKEY_COMPRESSED_LEN, + this->transaction.data.asset + .secondSignature + .publicKey.begin()); + + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + SecondSignature() { + this->transaction.data.type = SECOND_SIGNATURE_TYPE; + } +}; + +} // namespace builder +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/include/cpp-crypto/transactions/builders/transfer.hpp b/src/include/cpp-crypto/transactions/builders/transfer.hpp new file mode 100644 index 00000000..b3b4b052 --- /dev/null +++ b/src/include/cpp-crypto/transactions/builders/transfer.hpp @@ -0,0 +1,85 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_BUILDERS_TRANSFER_HPP +#define ARK_TRANSACTIONS_BUILDERS_TRANSFER_HPP + +#include +#include +#include +#include + +#include "transactions/builders/common.hpp" + +#include "interfaces/constants.h" + +#include "utils/base58.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { +namespace builder { + +//////////////////////////////////////////////////////////////////////////////// +// Forward Declaration +class Transfer; + +//////////////////////////////////////////////////////////////////////////////// +class Transfer : public Common { + public: + //////////////////////////////////////////////////////////////////////////// + // Amount + Transfer &amount(uint64_t amount) { + this->transaction.data.asset.transfer.amount = amount; + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + // Expiration + Transfer &expiration(uint32_t expiration) { + this->transaction.data.asset.transfer.expiration = expiration; + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + // RecipientId - uint8_t[21] (network.version + pubkeyHash) + Transfer &recipientId(const uint8_t *addressHash) { + std::copy_n(addressHash, + ADDRESS_HASH_LEN, + this->transaction.data.asset.transfer.recipientId.begin()); + + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + // RecipientId - std::string + Transfer &recipientId(const std::string &recipientId) { + const auto hashPair = Base58::getHashPair(recipientId.c_str()); + this->transaction.data.asset + .transfer.recipientId.at(0) = hashPair.version; + + std::copy(hashPair.pubkeyHash.begin(), + hashPair.pubkeyHash.end(), + &this->transaction.data.asset.transfer.recipientId.at(1)); + + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + Transfer() { + this->transaction.data.type = TRANSFER_TYPE; + } +}; + +} // namespace builder +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/include/cpp-crypto/transactions/builders/vote.hpp b/src/include/cpp-crypto/transactions/builders/vote.hpp new file mode 100644 index 00000000..7a2a975c --- /dev/null +++ b/src/include/cpp-crypto/transactions/builders/vote.hpp @@ -0,0 +1,55 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_BUILDERS_VOTE_HPP +#define ARK_TRANSACTIONS_BUILDERS_VOTE_HPP + +#include +#include + +#include "transactions/builders/common.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { +namespace builder { + +//////////////////////////////////////////////////////////////////////////////// +// Forward Declaration +class Vote; + +//////////////////////////////////////////////////////////////////////////////// +class Vote : public Common { + public: + //////////////////////////////////////////////////////////////////////////// + // Votes + // + // Should be in the format: + // - { 1(n_votes), 00/01('-'/'+'), publicKeyBytes } + Vote &votes(const uint8_t *votes) { + this->transaction.data.asset.vote.count = 1U; + std::copy_n(votes, VOTES_LEN, this->transaction.data.asset + .vote + .votes.begin()); + + return *this; + } + + //////////////////////////////////////////////////////////////////////////// + Vote() { + this->transaction.data.type = VOTE_TYPE; + } +}; + +} // namespace builder +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/include/cpp-crypto/transactions/deserializer.h b/src/include/cpp-crypto/transactions/deserializer.h deleted file mode 100644 index ccaf217d..00000000 --- a/src/include/cpp-crypto/transactions/deserializer.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * This file is part of Ark Cpp Crypto. - * - * (c) Ark Ecosystem - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - **/ - -#ifndef DESERIALIZER_H -#define DESERIALIZER_H - -#include "transactions/transaction.h" - -#include -#include -#include - -namespace Ark { -namespace Crypto { -namespace Transactions { -/**/ -class Deserializer { -public: - Deserializer(const std::string& serialized); - Transaction deserialize(); - -private: - void deserializeHeader(Transaction& transaction); - void deserializeType(Transaction& transaction); - void deserializeTransfer(Transaction& transaction); - void deserializeSecondSignatureRegistration(Transaction& transaction); - void deserializeDelegateRegistration(Transaction& transaction); - void deserializeVote(Transaction& transaction); - void deserializeMultiSignatureRegistration(Transaction& transaction); - void deserializeIPFS(Transaction& transaction); - void deserializeTimelock(Transaction& transaction); - void deserializeMultiPayment(Transaction& transaction); - void deserializeDelegateResignation(Transaction& transaction); - void deserializeSignatures(Transaction& transaction); - void handleVersionOne(Transaction& transaction); - - std::string _serialized; - std::vector _binary; - uint32_t _assetOffset; -}; -/**/ -}; // namespace Transactions -}; // namespace Crypto -}; // namespace Ark - -#endif diff --git a/src/include/cpp-crypto/transactions/serializer.h b/src/include/cpp-crypto/transactions/serializer.h deleted file mode 100644 index e0099e27..00000000 --- a/src/include/cpp-crypto/transactions/serializer.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * This file is part of Ark Cpp Crypto. - * - * (c) Ark Ecosystem - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - **/ - -#ifndef SERIALIZER_H -#define SERIALIZER_H - -#include "transactions/transaction.h" - -#include -#include -#include - -namespace Ark { -namespace Crypto { -namespace Transactions { -/**/ -class Serializer { -public: - Serializer(Transaction transaction); - std::string serialize() const; - -private: - void serializeVendorField(std::vector& bytes) const; - void serializeType(std::vector& bytes) const; - void serializeTransfer(std::vector& bytes) const; - void serializeSecondSignatureRegistration(std::vector& bytes) const; - void serializeDelegateRegistration(std::vector& bytes) const; - void serializeVote(std::vector& bytes) const; - void serializeMultiSignatureRegistration(std::vector& bytes) const; - void serializeSignatures(std::vector& bytes) const; - - Transaction _transaction; -}; -/**/ -} // namespace Transactions -} // namespace Crypto -} // namespace Ark - -#endif diff --git a/src/include/cpp-crypto/transactions/transaction.h b/src/include/cpp-crypto/transactions/transaction.h deleted file mode 100644 index fce18726..00000000 --- a/src/include/cpp-crypto/transactions/transaction.h +++ /dev/null @@ -1,94 +0,0 @@ -/** - * This file is part of Ark Cpp Crypto. - * - * (c) Ark Ecosystem - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - **/ - -#ifndef TRANSACTION_H -#define TRANSACTION_H - -#include "identities/privatekey.hpp" -#include "identities/publickey.hpp" - -#include -#include -#include - -namespace Ark { -namespace Crypto { -namespace Transactions { -/**/ -struct TransactionAsset { - struct { - std::string publicKey; - } signature; - - struct { - std::string username; - } delegate; - - std::vector votes; - - struct { - uint8_t min; - uint8_t lifetime; - std::vector keysgroup; - } multiSignature; -}; -/**/ -class Transaction { -public: - Transaction() = default; - Transaction(const Transaction&) = default; - Transaction& operator=(const Transaction&) = default; - Transaction(Transaction&&) = default; - Transaction& operator=(Transaction&&) = default; - - std::string getId() const; - - std::string sign(const char* passphrase); - std::string secondSign(const char* passphrase); - - bool verify() const; - bool secondVerify(const char* secondPublicKey) const; - - std::vector toBytes(bool skipSignature = true, bool skipSecondSignature = true) const; - std::map toArray() const; - std::string toJson() const; - - uint8_t header = 0; - uint8_t network = 0; - uint8_t type = 0; - uint8_t version = 0; - TransactionAsset asset = {}; - uint32_t timelock_type = 0; - std::vector signatures = {}; - std::string id = ""; - std::string recipient = ""; - std::string senderPublicKey = ""; - std::string signature = ""; - std::string secondSignature = ""; - std::string signSignature = ""; - std::string vendorField = ""; - std::string vendorFieldHex = ""; - uint32_t expiration = 0; - uint32_t timestamp = 0; - uint64_t amount = 0; - uint64_t fee = 0; - uint64_t timelock = 0; - -private: - bool internalVerify( - const std::string& publicKey, - std::vector bytes, - const std::string& signature) const; -}; -/**/ -}; // namespace Transactions -}; // namespace Crypto -}; // namespace Ark - -#endif diff --git a/src/include/cpp-crypto/transactions/transaction.hpp b/src/include/cpp-crypto/transactions/transaction.hpp new file mode 100644 index 00000000..78e225fa --- /dev/null +++ b/src/include/cpp-crypto/transactions/transaction.hpp @@ -0,0 +1,65 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTION_HPP +#define ARK_TRANSACTION_HPP + +#include +#include +#include +#include + +#include "interfaces/identities.hpp" + +#include "transactions/transaction_data.hpp" + +#include "transactions/serializer.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +class Transaction { + public: + //////////////////////////////////////////////////////////////////////////// + Transaction() = default; + + //////////////////////////////////////////////////////////////////////////// + Hash32 getId() const; + + //////////////////////////////////////////////////////////////////////////// + bool sign(const std::string &passphrase); + bool secondSign(const std::string &secondPassphrase); + + //////////////////////////////////////////////////////////////////////////// + bool verify() const; + bool secondVerify(const uint8_t *secondPublicKey) const; + + //////////////////////////////////////////////////////////////////////////// + bool deserialize(const std::vector &serialized); + std::vector serialize(); + + //////////////////////////////////////////////////////////////////////////// + std::vector toBytes( + const SerializerOptions &options = { false, false }) const; + + //////////////////////////////////////////////////////////////////////////// + std::map toMap() const; + std::string toJson() const; + + //////////////////////////////////////////////////////////////////////////// + TransactionData data; +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/include/cpp-crypto/transactions/transaction_data.hpp b/src/include/cpp-crypto/transactions/transaction_data.hpp new file mode 100644 index 00000000..0c2ebf16 --- /dev/null +++ b/src/include/cpp-crypto/transactions/transaction_data.hpp @@ -0,0 +1,64 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TRANSACTION_DATA_HPP +#define ARK_TRANSACTIONS_TRANSACTION_DATA_HPP + +#include +#include + +#include "interfaces/constants.h" +#include "interfaces/identities.hpp" + +#include "networks/devnet.hpp" + +#include "transactions/types/assets.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Transaction Data Model +struct TransactionData { + uint8_t header { TRANSACTION_DEFAULT_HEADER }; + uint8_t version { TRANSACTION_VERSION_TYPE_2 }; + uint8_t network { Devnet.version }; + + uint32_t typeGroup { TRANSACTION_DEFAULT_TYPEGROUP }; // v2 + uint16_t type { 0U }; // v1: 1 Byte | v2: 2 Bytes + + uint64_t nonce { 0ULL }; // v2 only + uint32_t timestamp { 0UL }; // v1 only + + PublicKeyBytes senderPublicKey { }; + + uint64_t fee { 0ULL }; + + std::vector vendorField; + + Asset asset; + + Hash32 id { }; + + std::vector signature; + std::vector secondSignature; + + TransactionData() { + signature.resize(SIGNATURE_ECDSA_MAX); + secondSignature.resize(SIGNATURE_ECDSA_MAX); + }; +}; + + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/lib/bcl/Base58Check.cpp b/src/lib/bcl/Base58Check.cpp deleted file mode 100644 index 3e7c6ec3..00000000 --- a/src/lib/bcl/Base58Check.cpp +++ /dev/null @@ -1,248 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - * - * adapted 2018 simon downey - * - removed ExtendedPrivatKey references - */ - -#include -#include -#include "Base58Check.hpp" -#include "Sha256.hpp" -#include "Sha256Hash.hpp" -#include "Utils.hpp" - -namespace bcl { - - -using std::uint8_t; -using std::uint32_t; -using std::size_t; - - - -/*---- Public and private functions for bytes-to-Base58 conversion ----*/ - -void Base58Check::pubkeyHashToBase58Check(const uint8_t pubkeyHash[Ripemd160::HASH_LEN], uint8_t version, char outStr[36]) { - assert(pubkeyHash != nullptr && outStr != nullptr); - constexpr size_t arrayLen = 1 + static_cast(Ripemd160::HASH_LEN) + 4; - uint8_t toEncode[arrayLen]; - toEncode[0] = version; - std::memcpy(&toEncode[1], pubkeyHash, Ripemd160::HASH_LEN); - uint8_t temp[arrayLen]; - bytesToBase58Check(toEncode, temp, arrayLen - 4, outStr); -} - - -void Base58Check::privateKeyToBase58Check(const Uint256 &privKey, uint8_t version, bool isCompressed, char outStr[53]) { - assert(outStr != nullptr); - constexpr size_t arrayLen = 1 + 32 + 1 + 4; - uint8_t toEncode[arrayLen]; - toEncode[0] = version; - privKey.getBigEndianBytes(&toEncode[1]); - toEncode[33] = 0x01; // Compressed marker - uint8_t temp[arrayLen]; - bytesToBase58Check(toEncode, temp, arrayLen - (isCompressed ? 4 : 5), outStr); -} - - -void Base58Check::bytesToBase58Check(uint8_t data[], uint8_t temp[], size_t dataLen, char *outStr) { - // Append 4-byte hash - assert(data != nullptr && temp != nullptr && outStr != nullptr); - const Sha256Hash sha256Hash = Sha256::getDoubleHash(data, dataLen); - for (int i = 0; i < 4; i++, dataLen++) - data[dataLen] = sha256Hash.value[i]; - - // Count leading zero bytes - size_t leadingZeros = 0; - while (leadingZeros < dataLen && data[leadingZeros] == 0) - leadingZeros++; - - // Encode to Base 58 - size_t outLen = 0; - while (!isZero(data, dataLen)) { // Extract digits in little-endian - outStr[outLen] = ALPHABET[mod58(data, dataLen)]; - outLen++; - divide58(data, temp, dataLen); // temp = floor(data / 58) - Utils::copyBytes(data, temp, dataLen); // data = temp - } - for (size_t i = 0; i < leadingZeros; i++) { // Append leading zeros - outStr[outLen] = ALPHABET[0]; - outLen++; - } - outStr[outLen] = '\0'; - - // Reverse the string - if (outLen == 0) - return; // Exit early to ensure that j does not overflow - for (size_t i = 0, j = outLen - 1; i < j; i++, j--) { - char temp = outStr[i]; - outStr[i] = outStr[j]; - outStr[j] = temp; - } -} - - -bool Base58Check::isZero(const uint8_t x[], size_t len) { - assert(len == 0 || x != nullptr); - for (size_t i = 0; i < len; i++) { - if (x[i] != 0) - return false; - } - return true; -} - - -uint8_t Base58Check::mod58(const uint8_t x[], size_t len) { - assert(len == 0 || x != nullptr); - unsigned int sum = 0; - for (size_t i = 0; i < len; i++) - sum = ((sum * 24) + x[i]) % 58; // Note: 256 % 58 = 24 - return static_cast(sum); -} - - -void Base58Check::divide58(const uint8_t x[], uint8_t y[], size_t len) { - assert(x != nullptr && y != nullptr); - std::memset(y, 0, len); - unsigned int dividend = 0; - for (size_t i = 0; i < len; i++) { // For each input and output byte - assert(dividend < 58); - dividend = (dividend << 8) | x[i]; // Shift next byte into right side - assert(dividend < 14848); - y[i] = static_cast(dividend / 58); - dividend %= 58; - } -} - - - -/*---- Public and private functions for Base58-to-bytes conversion ----*/ - -bool Base58Check::pubkeyHashFromBase58Check(const char *addrStr, uint8_t outPubkeyHash[Ripemd160::HASH_LEN], uint8_t *outVersion) { - // Preliminary checks - assert(addrStr != nullptr && outPubkeyHash != nullptr); - if (std::strlen(addrStr) < 25 || std::strlen(addrStr) > 35) - return false; - - // Perform Base58 decoding - uint8_t decoded[1 + Ripemd160::HASH_LEN + 4]; - if (!base58CheckToBytes(addrStr, decoded, sizeof(decoded) / sizeof(decoded[0]))) - return false; - - // Successfully set the output and version - std::memcpy(outPubkeyHash, &decoded[1], Ripemd160::HASH_LEN * sizeof(uint8_t)); - if (outVersion != nullptr) - *outVersion = decoded[0]; - return true; -} - - -bool Base58Check::privateKeyFromBase58Check(const char wifStr[53], Uint256 &outPrivKey, uint8_t *outVersion, bool *outIsCompressed) { - // Preliminary checks - assert(wifStr != nullptr); - if (std::strlen(wifStr) < 38 || std::strlen(wifStr) > 52) - return false; - - // Perform Base58 decoding - constexpr size_t arrayLen = 1 + 32 + 1 + 4; - uint8_t decoded[arrayLen]; - if (base58CheckToBytes(wifStr, decoded, arrayLen - 1)) { // Try decoding uncompressed - if (outIsCompressed != nullptr) - *outIsCompressed = false; - } else if (base58CheckToBytes(wifStr, decoded, arrayLen)) { // Try decoding compressed - if (decoded[33] != 0x01) // Check compressed marker byte - return false; - if (outIsCompressed != nullptr) - *outIsCompressed = true; - } else - return false; - - // Successfully set the value and version - outPrivKey = Uint256(&decoded[1]); - if (outVersion != nullptr) - *outVersion = decoded[0]; - return true; -} - - -bool Base58Check::base58CheckToBytes(const char *inStr, uint8_t outData[], size_t outDataLen) { - assert(inStr != nullptr && outData != nullptr && outDataLen >= 4); - - // Convert from Base 58 to base 256 - std::memset(outData, 0, outDataLen * sizeof(outData[0])); - for (size_t i = 0; inStr[i] != '\0'; i++) { - if (multiply58(outData, outDataLen)) - return false; - const char *p = std::strchr(ALPHABET, inStr[i]); - if (p == nullptr) - return false; - if (addUint8(outData, p - &ALPHABET[0], outDataLen)) - return false; - } - - // Verify number of leading zeros - for (size_t i = 0; ; i++) { - if (inStr[i] != '1' && (i >= outDataLen || outData[i] != 0)) - break; // Success - else if (inStr[i] == '1' && i < outDataLen && outData[i] == 0) - continue; // Keep scanning - else - return false; // Mismatch - } - - // Compute and check hash - const Sha256Hash sha256Hash = Sha256::getDoubleHash(outData, outDataLen - 4); - for (int i = 0; i < 4; i++) { - if (outData[outDataLen - 4 + i] != sha256Hash.value[i]) - return false; - } - return true; -} - - -bool Base58Check::addUint8(uint8_t x[], uint8_t y, size_t len) { - assert(len >= 1 && x != nullptr); - int carry = 0; - for (size_t i = len - 1; ; i--) { - int sum = x[i] + carry; - assert(0 <= sum && sum <= 256); - if (i == len - 1) - sum += y; - x[i] = static_cast(sum); - carry = sum >> 8; - assert((carry >> 1) == 0); - if (i == 0) - break; - } - return carry > 0; -} - - -bool Base58Check::multiply58(uint8_t x[], size_t len) { - assert(len >= 1 && x != nullptr); - int carry = 0; - for (size_t i = len - 1; ; i--) { - int temp = x[i] * 58 + carry; - x[i] = static_cast(temp); - carry = temp >> 8; - assert(0 <= carry && carry < 58); - if (i == 0) - break; - } - return carry > 0; -} - - - -/*---- Miscellaneous definitions ----*/ - -// Static initializers -const char *Base58Check::ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; - - -} // namespace bcl diff --git a/src/lib/bcl/Base58Check.hpp b/src/lib/bcl/Base58Check.hpp deleted file mode 100644 index 9e8b100b..00000000 --- a/src/lib/bcl/Base58Check.hpp +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - * - * adapted 2018 simon downey - * - removed ExtendedPrivatKey references - */ - -#pragma once - -#include -#include -#include "Ripemd160.hpp" -#include "Uint256.hpp" - -namespace bcl { - - -/* - * Converts a pubkey hash, private key, or extended private key to and from a Base58Check ASCII string. - */ -class Base58Check final { - - /*---- Public export-to-string functions ----*/ - - // Exports the given 20-byte public key hash with the given version prefix byte as a public address. - // The outStr array must have length >= 36 (including null terminator). - // The output text length is between 25 and 35 characters, inclusive. Not constant-time. - public: static void pubkeyHashToBase58Check(const std::uint8_t pubkeyHash[Ripemd160::HASH_LEN], std::uint8_t version, char outStr[36]); - - - // Exports the given private key with the given version prefix byte as WIF. - // The isCompressed parameter controls whether the private key should generate a compressed - // public curve point, and should almost always be set to true (except for legacy applications). - // The outStr array must have length >= 53 (including null terminator). - // The output text length is between 38 and 52 characters, inclusive. Not constant-time. - public: static void privateKeyToBase58Check(const Uint256 &privKey, std::uint8_t version, bool isCompressed, char outStr[53]); - - - /*---- Public import-from-string functions ----*/ - - // Parses the given public address string. If the syntax and check digits are correct, then the - // output array is set to the decoded value, the version byte is set if not null, and true is returned. - // Otherwise the output array and version are unchanged, and false is returned. Not constant-time. - public: static bool pubkeyHashFromBase58Check(const char *addrStr, std::uint8_t outPubkeyHash[Ripemd160::HASH_LEN], std::uint8_t *outVersion); - - - // Parses the given WIF string. If the syntax, optional compressed marker, and check digits are correct, - // then the private key Uint256 is set to the decoded value, the version byte is set if not null, - // the compressed status is set if not null, and true is returned. Otherwise the Uint256, - // version, and compressed status are unchanged, and false is returned. Not constant-time. - // Note that the decoded integer may be outside the normal private key range of [1, CurvePoint::ORDER). - public: static bool privateKeyFromBase58Check(const char wifStr[53], Uint256 &outPrivKey, std::uint8_t *outVersion, bool *outIsCompressed); - - - /*---- Private high-level Base58Check functions ----*/ - - // Computes the 4-byte hash of the given byte array, concatenates it, and converts it to Base58Check. - // This overwrites data and temp for indices 0 <= i < len+4. The caller is responsible for leaving 4 free bytes - // starting at data[len], allocating len+4 bytes for temp, and allocating enough space in outStr. Not constant-time. - public: static void bytesToBase58Check(std::uint8_t data[], std::uint8_t temp[], std::size_t dataLen, char *outStr); - - - // Converts the given Base58Check string to an array of bytes. Returns true if the conversion succeeded; - // otherwise returns false if the string contains non-Base58 characters, decodes to - // shorter or longer data than the output array length, or fails the hash check. - // The output array elements may be changed even if false is returned. Not constant-time. - private: static bool base58CheckToBytes(const char *inStr, std::uint8_t outData[], std::size_t outDataLen); - - - /*---- Private low-level arithmetic functions ----*/ - - // These functions perform unsigned big-endian arbitrary-precision arithmetic on byte arrays that represent numbers. - // These algorithms differ from Uint256 because Uint256 is fixed-width, little-endian, and 32-bit-word-oriented. - - // Tests whether the given bigint is zero. Not constant-time. - private: static bool isZero(const std::uint8_t x[], std::size_t len); - - - // Returns the given bigint modulo 58. Not constant-time. - private: static std::uint8_t mod58(const std::uint8_t x[], std::size_t len); - - - // Computes the quotient y = floor(x / 58). Not constant-time. - private: static void divide58(const std::uint8_t x[], std::uint8_t y[], std::size_t len); - - - // Computes the sum (x = (x + y) mod 256^len) in place. Returns whether the - // carry-out is non-zero. Constant-time with respect to x's values and the value of y. - private: static bool addUint8(std::uint8_t x[], std::uint8_t y, std::size_t len); - - - // Computes the product (x = (x * 58) mod 256^len) in place. Returns whether - // the carry-out is non-zero. Constant-time with respect to x's values. - private: static bool multiply58(std::uint8_t x[], std::size_t len); - - - - /*---- Miscellaneous ----*/ - - Base58Check() = delete; // Not instantiable - - - public: static const char *ALPHABET; - -}; - - -} // namespace bcl diff --git a/src/lib/bcl/CurvePoint.cpp b/src/lib/bcl/CurvePoint.cpp deleted file mode 100644 index 416f4c64..00000000 --- a/src/lib/bcl/CurvePoint.cpp +++ /dev/null @@ -1,293 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#include -#include "CurvePoint.hpp" - -namespace bcl { - - -using std::uint8_t; -using std::uint32_t; - - -CurvePoint::CurvePoint(const FieldInt &x_, const FieldInt &y_) : - x(x_), y(y_), z(FI_ONE) {} - - -CurvePoint::CurvePoint(const char *xStr, const char *yStr) : - x(xStr), y(yStr), z(FI_ONE) {} - - -CurvePoint::CurvePoint() : - x(FI_ZERO), y(FI_ONE), z(FI_ZERO) {} - - -void CurvePoint::add(const CurvePoint &other) { - /* - * (See https://www.nayuki.io/page/elliptic-curve-point-addition-in-projective-coordinates) - * Algorithm pseudocode: - * if (this == ZERO) - * this = other - * else if (other == ZERO) - * this = this - * else { - * t0 = y0 * z1 - * t1 = y1 * z0 - * u0 = x0 * z1 - * u1 = x1 * z0 - * if (u0 == u1) { // Same x coordinates - * if (t0 == t1) // Same y coordinates - * this = twice() - * else - * this = ZERO - * } else { - * t = t0 - t1 - * u = u0 - u1 - * u2 = u^2 - * v = z0 * z1 - * w = t^2 * v - u2 * (u0 + u1) - * x' = u * w - * u3 = u2 * u - * y' = t * (u0 * u2 - w) - t0 * u3 - * z' = u3 * v - * } - * } - */ - bool thisZero = this->isZero(); - bool otherZero = other.isZero(); - CurvePoint temp = *this; - temp.twice(); - temp.replace(*this, static_cast(otherZero)); - temp.replace(other, static_cast(thisZero )); - - FieldInt u0 = this->x; - FieldInt u1 = other.x; - FieldInt t0 = this->y; - FieldInt &t1 = x; // Reuse memory - t1 = other.y; - u0.multiply(other.z); - u1.multiply(this->z); - t0.multiply(other.z); - t1.multiply(this->z); - bool sameX = u0 == u1; - bool sameY = t0 == t1; - temp.replace(ZERO, static_cast(!thisZero & !otherZero & sameX & !sameY)); - - FieldInt &t = y; // Reuse memory - t = t0; - t.subtract(t1); - FieldInt u = u0; - u.subtract(u1); - FieldInt u2 = u; - u2.square(); - FieldInt &v = z; // Reuse memory - v.multiply(other.z); - - FieldInt w = t; - w.square(); - w.multiply(v); - u1.add(u0); - u1.multiply(u2); - w.subtract(u1); - - x = u; - x.multiply(w); - - FieldInt &u3 = u1; // Reuse memory - u3 = u; - u3.multiply(u2); - - u0.multiply(u2); - u0.subtract(w); - t.multiply(u0); - t0.multiply(u3); - t.subtract(t0); // Assigns to y - - v.multiply(u3); // Assigns to z - - this->replace(temp, static_cast(thisZero | otherZero | sameX)); -} - - -void CurvePoint::twice() { - /* - * (See https://www.nayuki.io/page/elliptic-curve-point-addition-in-projective-coordinates) - * Algorithm pseudocode: - * if (this == ZERO || y == 0) - * this = ZERO - * else { - * a = 0 (curve parameter) - * t = 3 * x^2 + a * z^2 - * u = 2 * y * z - * v = 2 * u * x * y - * w = t^2 - 2 * v - * x' = u * w - * y' = t * (v - w) - 2 * (u * y)^2 - * z' = u^3 - * } - */ - bool zeroResult = isZero() | (y == FI_ZERO); - - FieldInt u = y; - u.multiply(z); - u.multiply2(); - - FieldInt v = u; - v.multiply(x); - v.multiply(y); - v.multiply2(); - - x.square(); - FieldInt t = x; - t.multiply2(); - t.add(x); - - FieldInt &w = z; // Reuse memory - w = t; - w.square(); - x = v; - x.multiply2(); - w.subtract(x); - - x = v; - x.subtract(w); - x.multiply(t); - y.multiply(u); - y.square(); - y.multiply2(); - x.subtract(y); - y = x; - - x = u; - x.multiply(w); - - z = u; - z.square(); - z.multiply(u); - - this->replace(ZERO, static_cast(zeroResult)); -} - - -void CurvePoint::multiply(const Uint256 &n) { - // Precompute [this*0, this*1, ..., this*15] - constexpr int tableBits = 4; // Do not modify - constexpr int tableLen = 1 << tableBits; - CurvePoint table[tableLen]; // Default-initialized with ZERO - table[1] = *this; - table[2] = *this; - table[2].twice(); - for (int i = 3; i < tableLen; i++) { - table[i] = table[i - 1]; - table[i].add(*this); - } - - // Process tableBits per iteration (windowed method) - *this = ZERO; - for (int i = Uint256::NUM_WORDS * 32 - tableBits; i >= 0; i -= tableBits) { - unsigned int inc = (n.value[i >> 5] >> (i & 31)) & (tableLen - 1); - CurvePoint q = ZERO; // Dummy initial value - for (unsigned int j = 0; j < tableLen; j++) - q.replace(table[j], static_cast(j == inc)); - this->add(q); - if (i != 0) { - for (int j = 0; j < tableBits; j++) - this->twice(); - } - } -} - - -void CurvePoint::normalize() { - /* - * Algorithm pseudocode: - * if (z != 0) { - * x /= z - * y /= z - * z = 1 - * } else { - * x = x != 0 ? 1 : 0 - * y = y != 0 ? 1 : 0 - * z = 0 - * } - */ - CurvePoint norm = *this; - norm.z.reciprocal(); - norm.x.multiply(norm.z); - norm.y.multiply(norm.z); - norm.z = FI_ONE; - x.replace(FI_ONE, static_cast(x != FI_ZERO)); - y.replace(FI_ONE, static_cast(y != FI_ZERO)); - this->replace(norm, static_cast(z != FI_ZERO)); -} - - -void CurvePoint::replace(const CurvePoint &other, uint32_t enable) { - assert((enable >> 1) == 0); - this->x.replace(other.x, enable); - this->y.replace(other.y, enable); - this->z.replace(other.z, enable); -} - - -bool CurvePoint::isOnCurve() const { - FieldInt left = y; - left.square(); - FieldInt right = x; - right.square(); - right.add(A); - right.multiply(x); - right.add(B); - return (left == right) & !isZero(); -} - - -bool CurvePoint::isZero() const { - return (x == FI_ZERO) & (y != FI_ZERO) & (z == FI_ZERO); -} - - -bool CurvePoint::operator==(const CurvePoint &other) const { - return (x == other.x) & (y == other.y) & (z == other.z); -} - -bool CurvePoint::operator!=(const CurvePoint &other) const { - return !(*this == other); -} - - -void CurvePoint::toCompressedPoint(uint8_t output[33]) const { - assert(output != nullptr); - output[0] = (y.value[0] & 1) + 0x02; - x.getBigEndianBytes(&output[1]); -} - - -CurvePoint CurvePoint::privateExponentToPublicPoint(const Uint256 &privExp) { - assert((Uint256::ZERO < privExp) & (privExp < CurvePoint::ORDER)); - CurvePoint result = CurvePoint::G; - result.multiply(privExp); - result.normalize(); - return result; -} - - -// Static initializers -const FieldInt CurvePoint::FI_ZERO("0000000000000000000000000000000000000000000000000000000000000000"); -const FieldInt CurvePoint::FI_ONE ("0000000000000000000000000000000000000000000000000000000000000001"); -const FieldInt CurvePoint::A ("0000000000000000000000000000000000000000000000000000000000000000"); -const FieldInt CurvePoint::B ("0000000000000000000000000000000000000000000000000000000000000007"); -const Uint256 CurvePoint::ORDER("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"); -const CurvePoint CurvePoint::G( - FieldInt("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"), - FieldInt("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8")); -const CurvePoint CurvePoint::ZERO; // Default constructor - - -} // namespace bcl diff --git a/src/lib/bcl/CurvePoint.hpp b/src/lib/bcl/CurvePoint.hpp deleted file mode 100644 index cf1705dc..00000000 --- a/src/lib/bcl/CurvePoint.hpp +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#pragma once - -#include -#include "FieldInt.hpp" -#include "Uint256.hpp" - -namespace bcl { - - -/* - * A point on the secp256k1 elliptic curve for Bitcoin use, in projective coordinates. - * Contains methods for computing point addition, doubling, and multiplication, and testing equality. - * The ordinary affine coordinates of a point is (x/z, y/z). Instances of this class are mutable. - * - * Points MUST be normalized before comparing for equality. Example of correct usage: - * CurvePoint a(...); - * CurvePoint b(...); - * CurvePoint c(...); - * - * a.add(b); - * a.multiply(50); - * - * a.normalize(); - * c.normalize(); - * if (a == c) { ... } - */ -class CurvePoint final { - - /*---- Fields ----*/ - - public: FieldInt x; - public: FieldInt y; - public: FieldInt z; // The point is normalized iff (z = 1 OR (x,y,z)=(0,1,0)) - - - - /*---- Constructors ----*/ - - // Constructs a normalized point (z=1) from the given coordinates. Constant-time with respect to the values. - public: explicit CurvePoint(const FieldInt &x_, const FieldInt &y_); - - - // Constructs a normalized point (z=1) from the given string coordinates. Not constant-time. - public: explicit CurvePoint(const char *xStr, const char *yStr); - - - // Constructs the special "point at infinity" (normalized), which is used by ZERO and in multiply(). - private: CurvePoint(); - - - - /*---- Arithmetic methods ----*/ - - // Adds the given curve point to this point. The resulting state is - // usually not normalized. Constant-time with respect to both values. - public: void add(const CurvePoint &other); - - - // Doubles this curve point. The resulting state is usually - // not normalized. Constant-time with respect to this value. - public: void twice(); - - - // Multiplies this point by the given unsigned integer. The resulting state - // is usually not normalized. Constant-time with respect to both values. - public: void multiply(const Uint256 &n); - - - // Normalizes the coordinates of this point. Idempotent operation. - // Constant-time with respect to this value. - public: void normalize(); - - - // Copies the given point into this point if enable is 1, or does nothing if enable is 0. - // Constant-time with respect to both values and the enable. - public: void replace(const CurvePoint &other, std::uint32_t enable); - - - // Tests whether this point is on the elliptic curve. - // This point needs to be normalized before the method is called. - // Zero is considered to be off the curve. Constant-time with respect to this value. - public: bool isOnCurve() const; - - - // Tests whether this point is equal to the special zero point. - // This point need not be normalized. Constant-time with respect to this value. - // This method is equivalent to, but more convenient than: - // { CurvePoint temp(*this); temp.normalize(); return temp == ZERO; } - public: bool isZero() const; - - - // Tests whether this point equals the given point in all 3 coordinates. This comparison is - // meaningful only if both points are normalized. Constant-time with respect to both values. - public: bool operator==(const CurvePoint &other) const; - - // Tests whether this point mismatches the given point in any of the 3 coordinates. This comparison - // is meaningful only if both points are normalized. Constant-time with respect to both values. - public: bool operator!=(const CurvePoint &other) const; - - - // Serializes this point in compressed format (header byte, x-coordinate in big-endian). - // This point needs to be normalized before the method is called. Constant-time with respect to this value. - public: void toCompressedPoint(std::uint8_t output[33]) const; - - - /*---- Static functions ----*/ - - // Returns a normalized public curve point for the given private exponent key. - // Requires 0 < privExp < ORDER. Constant-time with respect to the value. - public: static CurvePoint privateExponentToPublicPoint(const Uint256 &privExp); - - - /*---- Class constants ----*/ - - public: static const FieldInt FI_ZERO; // These FieldInt constants are declared here because they are only needed in this class, - public: static const FieldInt FI_ONE; // and because of C++'s lack of guarantee of static initialization order. - public: static const FieldInt A; // Curve equation parameter - public: static const FieldInt B; // Curve equation parameter - public: static const Uint256 ORDER; // Order of base point, which is a prime number - public: static const CurvePoint G; // Base point (normalized) - public: static const CurvePoint ZERO; // Dummy point at infinity (normalized) - -}; - - -} // namespace bcl diff --git a/src/lib/bcl/Ecdsa.cpp b/src/lib/bcl/Ecdsa.cpp deleted file mode 100644 index 9b383fe3..00000000 --- a/src/lib/bcl/Ecdsa.cpp +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#include -#include -#include -#include "Ecdsa.hpp" -#include "FieldInt.hpp" -#include "Sha256.hpp" - -namespace bcl { - - -using std::uint8_t; -using std::uint32_t; - - -bool Ecdsa::sign(const Uint256 &privateKey, const Sha256Hash &msgHash, const Uint256 &nonce, Uint256 &outR, Uint256 &outS) { - /* - * Algorithm pseudocode: - * if (nonce outside range [1, order-1]) return false - * p = nonce * G - * r = p.x % order - * if (r == 0) return false - * s = nonce^-1 * (msgHash + r * privateKey) % order - * if (s == 0) return false - * s = min(s, order - s) - */ - - const Uint256 &order = CurvePoint::ORDER; - const Uint256 &zero = Uint256::ZERO; - if (nonce == zero || nonce >= order) - return false; - - const CurvePoint p = CurvePoint::privateExponentToPublicPoint(nonce); - Uint256 r(p.x); - r.subtract(order, static_cast(r >= order)); - if (r == zero) - return false; - assert(r < order); - - Uint256 s = r; - const Uint256 z(msgHash.value); - multiplyModOrder(s, privateKey); - uint32_t carry = s.add(z, 1); - s.subtract(order, carry | static_cast(s >= order)); - - Uint256 kInv = nonce; - kInv.reciprocal(order); - multiplyModOrder(s, kInv); - if (s == zero) - return false; - - Uint256 negS = order; - negS.subtract(s); - s.replace(negS, static_cast(negS < s)); // To ensure low S values for BIP 62 - outR = r; - outS = s; - return true; -} - - -bool Ecdsa::signWithHmacNonce(const Uint256 &privateKey, const Sha256Hash &msgHash, Uint256 &outR, Uint256 &outS) { - uint8_t privkeyBytes[Uint256::NUM_WORDS * 4]; - privateKey.getBigEndianBytes(privkeyBytes); - const Sha256Hash hmac = Sha256::getHmac(privkeyBytes, sizeof(privkeyBytes), msgHash.value, Sha256Hash::HASH_LEN); - const Uint256 nonce(hmac.value); - return sign(privateKey, msgHash, nonce, outR, outS); -} - - -bool Ecdsa::verify(const CurvePoint &publicKey, const Sha256Hash &msgHash, const Uint256 &r, const Uint256 &s) { - /* - * Algorithm pseudocode: - * if (pubKey == zero || !(pubKey is normalized) || - * !(pubKey on curve) || n * pubKey != zero) - * return false - * if (!(0 < r, s < order)) - * return false - * w = s^-1 % order - * u1 = (msgHash * w) % order - * u2 = (r * w) % order - * p = u1 * G + u2 * pubKey - * return r == p.x % order - */ - - const Uint256 &order = CurvePoint::ORDER; - const Uint256 &zero = Uint256::ZERO; - CurvePoint q = publicKey; - q.multiply(CurvePoint::ORDER); - if (!(zero < r && r < order && zero < s && s < order)) - return false; - if (publicKey.isZero() || publicKey.z != CurvePoint::FI_ONE || !publicKey.isOnCurve() || !q.isZero()) - return false; - - Uint256 w = s; - w.reciprocal(order); - const Uint256 z(msgHash.value); - Uint256 u1 = w; - Uint256 u2 = w; - multiplyModOrder(u1, z); - multiplyModOrder(u2, r); - - CurvePoint p = CurvePoint::G; - q = publicKey; - p.multiply(u1); - q.multiply(u2); - p.add(q); - p.normalize(); - - Uint256 px(p.x); - if (px >= order) - px.subtract(order); - return r == px; -} - - -void Ecdsa::multiplyModOrder(Uint256 &x, const Uint256 &y) { - /* - * Russian peasant multiplication with modular reduction at each step. Algorithm pseudocode: - * z = 0 - * for (i = 255 .. 0) { - * z = (z * 2) % order - * if (y.bit[i] == 1) - * z = (z + x) % order - * } - * x = z - */ - const Uint256 &mod = CurvePoint::ORDER; - assert(&x != &y && x < mod); - Uint256 z = Uint256::ZERO; - - for (int i = Uint256::NUM_WORDS * 32 - 1; i >= 0; i--) { - // Multiply by 2 - uint32_t c = z.shiftLeft1(); - z.subtract(mod, c | static_cast(z >= mod)); - // Conditionally add x - uint32_t enable = (y.value[i >> 5] >> (i & 31)) & 1; - c = z.add(x, enable); - z.subtract(mod, c | static_cast(z >= mod)); - assert(z < mod); - } - x = z; -} - - -} // namespace bcl diff --git a/src/lib/bcl/Ecdsa.hpp b/src/lib/bcl/Ecdsa.hpp deleted file mode 100644 index cd353b8e..00000000 --- a/src/lib/bcl/Ecdsa.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#pragma once - -#include "CurvePoint.hpp" -#include "Sha256Hash.hpp" -#include "Uint256.hpp" - -namespace bcl { - - -/* - * Performs ECDSA signature generation and verification. Provides just three static functions. - */ -class Ecdsa final { - - // Computes the signature (deterministically) when given the private key, message hash, and random nonce. - // Returns true if signing was successful (overwhelming probability), or false if a new nonce must be chosen - // (vanishing probability). Both privateKey and nonce must be in the range [1, CurvePoint::ORDER). - // outR and outS will be in the same range too; their values are assigned iff signing is successful. - // Note: The nonce must be unique, unpredictable, and secret. Otherwise the signature may leak the private key. - // All successful executions are constant-time with respect to the input values; in order words - // one successful execution is indistinguishable from another one based on side channel information. - public: static bool sign(const Uint256 &privateKey, const Sha256Hash &msgHash, const Uint256 &nonce, Uint256 &outR, Uint256 &outS); - - - // Computes a deterministic nonce based on the HMAC-SHA-256 of the message hash with the private key, - // and then performs ECDSA signing. Returns true iff signing is successful (with overwhelming probability). - // This has the same constant-time behavior as sign(). - public: static bool signWithHmacNonce(const Uint256 &privateKey, const Sha256Hash &msgHash, Uint256 &outR, Uint256 &outS); - - - // Checks whether the given signature, message, and public key are valid together. The public key point - // must be normalized. This function does not need to be constant-time because all inputs are public. - public: static bool verify(const CurvePoint &publicKey, const Sha256Hash &msgHash, const Uint256 &r, const Uint256 &s); - - - // Computes x = (x * y) % CurvePoint::ORDER. Requires x < CurvePoint::ORDER, but y is unrestricted. - private: static void multiplyModOrder(Uint256 &x, const Uint256 &y); - - - Ecdsa() = delete; // Not instantiable - -}; - - -} // namespace bcl diff --git a/src/lib/bcl/FieldInt.cpp b/src/lib/bcl/FieldInt.cpp deleted file mode 100644 index f852a049..00000000 --- a/src/lib/bcl/FieldInt.cpp +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#include -#include -#include "FieldInt.hpp" - -namespace bcl { - - -using std::uint32_t; -using std::uint64_t; - - -FieldInt::FieldInt(const char *str) : - Uint256(str) { - // C++ does not guarantee the order of initialization of static variables. If another class is - // initializing a FieldInt constant, this class's modulus might not have been initialized yet. - // Thus the assertion must be exempted in this situation. This logic relies on the fact that uninitialized - // static variables are set to zero. Hence, only do the assertion if the modulus has been initialized already. - if (MODULUS.value[0] != 0) - assert(*this < MODULUS); -} - - -FieldInt::FieldInt(const Uint256 &val) : - Uint256(val) { - Uint256::subtract(MODULUS, static_cast(*this >= MODULUS)); - assert(*this < MODULUS); -} - - -void FieldInt::add(const FieldInt &other) { - uint32_t c = Uint256::add(other, 1); // Perform addition - assert((c >> 1) == 0); - Uint256::subtract(MODULUS, c | static_cast(*this >= MODULUS)); // Conditionally subtract modulus -} - - -void FieldInt::subtract(const FieldInt &other) { - uint32_t b = Uint256::subtract(other, 1); // Perform subtraction - assert((b >> 1) == 0); - Uint256::add(MODULUS, b); // Conditionally add modulus -} - - -void FieldInt::multiply2() { - uint32_t c = shiftLeft1(); - assert((c >> 1) == 0); - Uint256::subtract(MODULUS, c | static_cast(*this >= MODULUS)); // Conditionally subtract modulus -} - - -void FieldInt::square() { - multiply(*this); -} - - -void FieldInt::multiply(const FieldInt &other) { - // Compute raw product of (uint256 this->value) * (uint256 other.value) = (uint512 product0), via long multiplication - uint32_t product0[NUM_WORDS * 2] = {}; - for (int i = 0; i < NUM_WORDS; i++) { - uint32_t carry = 0; - for (int j = 0; j < NUM_WORDS; j++) { - uint64_t sum = static_cast(this->value[i]) * other.value[j]; - sum += static_cast(product0[i + j]) + carry; // Does not overflow - product0[i + j] = static_cast(sum); - carry = static_cast(sum >> 32); - } - product0[i + NUM_WORDS] = carry; - } - - // Barrett reduction algorithm begins here (see https://www.nayuki.io/page/barrett-reduction-algorithm). - // Multiply by floor(2^512 / MODULUS), which is 2^256 + 2^32 + 0x3D1. Guaranteed to fit in a uint768. - uint32_t product1[NUM_WORDS * 3]; - { - uint32_t carry = 0; - for (int i = 0; i < NUM_WORDS * 3; i++) { - uint64_t sum = carry; - if (i < NUM_WORDS * 2) - sum += static_cast(product0[i]) * 0x3D1; - if (1 <= i && i < NUM_WORDS * 2 + 1) - sum += product0[i - 1]; - if (i >= NUM_WORDS) - sum += product0[i - NUM_WORDS]; - product1[i] = static_cast(sum); - carry = static_cast(sum >> 32); - assert(carry <= 0x3D3); - } - assert(carry == 0); - } - - // Virtually shift right by 512 bits, then multiply by MODULUS. - // Note that MODULUS = 2^256 - 2^32 - 0x3D1. Result fits in a uint512. - uint32_t *product1Shifted = &product1[NUM_WORDS * 2]; // Length NUM_WORDS - uint32_t product2[NUM_WORDS * 2]; - { - uint32_t borrow = 0; - for (int i = 0; i < NUM_WORDS * 2; i++) { - uint64_t diff = -static_cast(borrow); - if (i < NUM_WORDS) - diff -= static_cast(product1Shifted[i]) * 0x3D1; - if (1 <= i && i < NUM_WORDS + 1) - diff -= product1Shifted[i - 1]; - if (i >= NUM_WORDS) - diff += product1Shifted[i - NUM_WORDS]; - product2[i] = static_cast(diff); - borrow = -static_cast(diff >> 32); - assert(borrow <= 0x3D3); - } - assert(borrow == 0); - } - - // Compute product0 - product2, which fits in a uint257 (sic) - uint32_t difference[NUM_WORDS + 1]; - { - uint32_t borrow = 0; - for (int i = 0; i < NUM_WORDS + 1; i++) { - uint64_t diff = static_cast(product0[i]) - product2[i] - borrow; - difference[i] = static_cast(diff); - borrow = -static_cast(diff >> 32); - assert((borrow >> 1) == 0); - } - } - - // Final conditional subtraction to yield a FieldInt value - std::memcpy(this->value, difference, sizeof(value)); - uint32_t dosub = static_cast((difference[NUM_WORDS] != 0) | (*this >= MODULUS)); - Uint256::subtract(MODULUS, dosub); -} - - -void FieldInt::reciprocal() { - Uint256::reciprocal(MODULUS); -} - - -void FieldInt::replace(const FieldInt &other, uint32_t enable) { - Uint256::replace(other, enable); -} - - -bool FieldInt::operator==(const FieldInt &other) const { - return Uint256::operator==(other); -} - -bool FieldInt::operator!=(const FieldInt &other) const { - return Uint256::operator!=(other); -} - -bool FieldInt::operator<(const FieldInt &other) const { - return Uint256::operator<(other); -} - -bool FieldInt::operator<=(const FieldInt &other) const { - return Uint256::operator<=(other); -} - -bool FieldInt::operator>(const FieldInt &other) const { - return Uint256::operator>(other); -} - -bool FieldInt::operator>=(const FieldInt &other) const { - return Uint256::operator>=(other); -} - - -bool FieldInt::operator<(const Uint256 &other) const { - return Uint256::operator<(other); -} - -bool FieldInt::operator>=(const Uint256 &other) const { - return Uint256::operator>=(other); -} - - -// Static initializers -const Uint256 FieldInt::MODULUS("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"); - - -} // namespace bcl diff --git a/src/lib/bcl/FieldInt.hpp b/src/lib/bcl/FieldInt.hpp deleted file mode 100644 index 1b4a5cee..00000000 --- a/src/lib/bcl/FieldInt.hpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#pragma once - -#include -#include "Uint256.hpp" - -namespace bcl { - - -/* - * An unsigned 256-bit integer modulo a specific prime number, for Bitcoin and secp256k1. - * The input and output values of each method are always in the range [0, MODULUS). - * - * Some behaviors are specific to FieldInt (such as reciprocal()), while others are - * the same as Uint256 (such as comparisons). The number representation format is - * the same as Uint256. It is illegal to set the value to be greater than or equal - * to MODULUS; undefined behavior will result. Instances of this class are mutable. - */ -class FieldInt final : private Uint256 { - - public: using Uint256::NUM_WORDS; - - /*---- Fields ----*/ - - public: using Uint256::value; - - - - /*---- Constructors ----*/ - - // Constructs a FieldInt from the given 64-character hexadecimal string. Not constant-time. - // If the syntax of the string is invalid, then an assertion will fail. - public: explicit FieldInt(const char *str); - - - // Constructs a FieldInt from the given Uint256, reducing it as necessary. - // Constant-time with respect to the given value. - public: explicit FieldInt(const Uint256 &val); - - - - /*---- Arithmetic methods ----*/ - - // Adds the given number into this number, modulo the prime. Constant-time with respect to both values. - public: void add(const FieldInt &other); - - - // Subtracts the given number from this number, modulo the prime. Constant-time with respect to both values. - public: void subtract(const FieldInt &other); - - - // Doubles this number, modulo the prime. Constant-time with respect to this value. - public: void multiply2(); - - - // Squares this number, modulo the prime. Constant-time with respect to this value. - public: void square(); - - - // Multiplies the given number into this number, modulo the prime. Constant-time with respect to both values. - public: void multiply(const FieldInt &other); - - - // Computes the multiplicative inverse of this number with respect to the modulus. - // If this number is zero, the reciprocal is zero. Constant-time with respect to this value. - public: void reciprocal(); - - - /*---- Miscellaneous methods ----*/ - - public: void replace(const FieldInt &other, std::uint32_t enable); - - public: using Uint256::getBigEndianBytes; - - - /*---- Equality and inequality operators ----*/ - - public: bool operator==(const FieldInt &other) const; - - public: bool operator!=(const FieldInt &other) const; - - public: bool operator<(const FieldInt &other) const; - - public: bool operator<=(const FieldInt &other) const; - - public: bool operator>(const FieldInt &other) const; - - public: bool operator>=(const FieldInt &other) const; - - - private: bool operator<(const Uint256 &other) const; - - private: bool operator>=(const Uint256 &other) const; - - - - /*---- Class constants ----*/ - - private: static const Uint256 MODULUS; // Prime number - -}; - - -} // namespace bcl diff --git a/src/lib/bcl/Ripemd160.cpp b/src/lib/bcl/Ripemd160.cpp deleted file mode 100644 index c3a9fed2..00000000 --- a/src/lib/bcl/Ripemd160.cpp +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#include -#include -#include "Ripemd160.hpp" -#include "Utils.hpp" - -namespace bcl { - - -using std::uint8_t; -using std::uint32_t; -using std::size_t; - - -void Ripemd160::getHash(const uint8_t msg[], size_t len, uint8_t hashResult[HASH_LEN]) { - // Compress whole message blocks - assert((msg != nullptr || len == 0) && hashResult != nullptr); - uint32_t state[5] = {UINT32_C(0x67452301), UINT32_C(0xEFCDAB89), UINT32_C(0x98BADCFE), UINT32_C(0x10325476), UINT32_C(0xC3D2E1F0)}; - size_t off = len & ~static_cast(BLOCK_LEN - 1); - compress(state, msg, off); - - // Final blocks, padding, and length - uint8_t block[BLOCK_LEN] = {}; - Utils::copyBytes(block, &msg[off], len - off); - off = len & (BLOCK_LEN - 1); - block[off] = 0x80; - off++; - if (off + 8 > BLOCK_LEN) { - compress(state, block, BLOCK_LEN); - std::memset(block, 0, BLOCK_LEN); - } - block[BLOCK_LEN - 8] = static_cast((len & 0x1FU) << 3); - len >>= 5; - for (int i = 1; i < 8; i++, len >>= 8) - block[BLOCK_LEN - 8 + i] = static_cast(len); - compress(state, block, BLOCK_LEN); - - // Uint32 array to bytes in little endian - for (int i = 0; i < HASH_LEN; i++) - hashResult[i] = static_cast(state[i >> 2] >> ((i & 3) << 3)); -} - - -void Ripemd160::compress(uint32_t state[5], const uint8_t blocks[], size_t len) { - assert(len % BLOCK_LEN == 0); - uint32_t schedule[16]; - for (size_t i = 0; i < len; ) { - - // Message schedule - for (int j = 0; j < 16; j++, i += 4) { - schedule[j] = static_cast(blocks[i + 0]) << 0 - | static_cast(blocks[i + 1]) << 8 - | static_cast(blocks[i + 2]) << 16 - | static_cast(blocks[i + 3]) << 24; - } - - // The 80 rounds - uint32_t al = state[0], ar = state[0]; - uint32_t bl = state[1], br = state[1]; - uint32_t cl = state[2], cr = state[2]; - uint32_t dl = state[3], dr = state[3]; - uint32_t el = state[4], er = state[4]; - for (int j = 0; j < NUM_ROUNDS; j++) { - uint32_t temp; - temp = 0U + rotl32(0U + al + f(j, bl, cl, dl) + schedule[RL[j]] + KL[j >> 4], SL[j]) + el; - al = el; - el = dl; - dl = rotl32(cl, 10); - cl = bl; - bl = temp; - temp = 0U + rotl32(0U + ar + f(NUM_ROUNDS - 1 - j, br, cr, dr) + schedule[RR[j]] + KR[j >> 4], SR[j]) + er; - ar = er; - er = dr; - dr = rotl32(cr, 10); - cr = br; - br = temp; - } - uint32_t temp = 0U + state[1] + cl + dr; - state[1] = 0U + state[2] + dl + er; - state[2] = 0U + state[3] + el + ar; - state[3] = 0U + state[4] + al + br; - state[4] = 0U + state[0] + bl + cr; - state[0] = temp; - } -} - - -uint32_t Ripemd160::f(int i, uint32_t x, uint32_t y, uint32_t z) { - switch (i >> 4) { - case 0: return x ^ y ^ z; - case 1: return (x & y) | (~x & z); - case 2: return (x | ~y) ^ z; - case 3: return (x & z) | (y & ~z); - case 4: return x ^ (y | ~z); - default: assert(false); return 0; // Dummy value to please the compiler - } -} - - -uint32_t Ripemd160::rotl32(uint32_t x, int i) { - return ((0U + x) << i) | (x >> (32 - i)); -} - - -// Static initializers -const uint32_t Ripemd160::KL[5] = { - UINT32_C(0x00000000), UINT32_C(0x5A827999), UINT32_C(0x6ED9EBA1), UINT32_C(0x8F1BBCDC), UINT32_C(0xA953FD4E)}; -const uint32_t Ripemd160::KR[5] = { - UINT32_C(0x50A28BE6), UINT32_C(0x5C4DD124), UINT32_C(0x6D703EF3), UINT32_C(0x7A6D76E9), UINT32_C(0x00000000)}; -const unsigned char Ripemd160::RL[NUM_ROUNDS] = { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, - 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, - 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, - 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13}; -const unsigned char Ripemd160::RR[NUM_ROUNDS] = { - 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, - 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, - 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, - 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, - 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11}; -const unsigned char Ripemd160::SL[NUM_ROUNDS] = { - 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, - 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, - 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, - 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, - 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6}; -const unsigned char Ripemd160::SR[NUM_ROUNDS] = { - 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, - 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, - 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, - 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, - 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11}; - - -} // namespace bcl diff --git a/src/lib/bcl/Ripemd160.hpp b/src/lib/bcl/Ripemd160.hpp deleted file mode 100644 index 7bfc7d2b..00000000 --- a/src/lib/bcl/Ripemd160.hpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#pragma once - -#include -#include - -namespace bcl { - - -/* - * Computes the RIPEMD-160 hash of a sequence of bytes. The hash value is 20 bytes long. - * Provides just one static method. - */ -class Ripemd160 final { - - public: static constexpr int HASH_LEN = 20; - private: static constexpr int BLOCK_LEN = 64; - private: static constexpr int NUM_ROUNDS = 80; - - /*---- Static functions ----*/ - - public: static void getHash(const std::uint8_t msg[], std::size_t len, std::uint8_t hashResult[HASH_LEN]); - - - private: static void compress(std::uint32_t state[5], const std::uint8_t blocks[], std::size_t len); - - private: static std::uint32_t f(int i, std::uint32_t x, std::uint32_t y, std::uint32_t z); - - // Requires 1 <= i <= 31 - private: static std::uint32_t rotl32(std::uint32_t x, int i); - - Ripemd160() = delete; // Not instantiable - - - - /*---- Class constants ----*/ - - private: static const std::uint32_t KL[5]; // Round constants for left line - private: static const std::uint32_t KR[5]; // Round constants for right line - private: static const unsigned char RL[NUM_ROUNDS]; // Message schedule for left line - private: static const unsigned char RR[NUM_ROUNDS]; // Message schedule for right line - private: static const unsigned char SL[NUM_ROUNDS]; // Left-rotation for left line - private: static const unsigned char SR[NUM_ROUNDS]; // Left-rotation for right line - -}; - - -} // namespace bcl diff --git a/src/lib/bcl/Sha256.cpp b/src/lib/bcl/Sha256.cpp deleted file mode 100644 index 3792b59b..00000000 --- a/src/lib/bcl/Sha256.cpp +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#include -#include -#include "Sha256.hpp" -#include "Utils.hpp" - -namespace bcl { - - -using std::uint8_t; -using std::uint32_t; -using std::uint64_t; -using std::size_t; - - -Sha256::Sha256() : - length(0), - bufferLen(0) {} - - -Sha256 &Sha256::append(const uint8_t bytes[], size_t len) { - assert(bytes != nullptr || len == 0); - for (size_t i = 0; i < len; i++) { - buffer[bufferLen] = bytes[i]; - bufferLen++; - if (bufferLen == BLOCK_LEN) { - compress(state, buffer); - bufferLen = 0; - } - } - length += len; - return *this; -} - - -Sha256Hash Sha256::getHash() { - uint64_t bitLength = length << 3; - uint8_t temp = 0x80; - append(&temp, 1); - temp = 0x00; - while (bufferLen != 56) - append(&temp, 1); - for (int i = 0; i < 8; i++) { - temp = static_cast(bitLength >> ((7 - i) << 3)); - append(&temp, 1); - } - uint8_t result[Sha256Hash::HASH_LEN]; - for (size_t i = 0; i < sizeof(state) / sizeof(state[0]); i++) - Utils::storeBigUint32(state[i], &result[i * 4]); - return Sha256Hash(result, Sha256Hash::HASH_LEN); -} - - -Sha256Hash Sha256::getHash(const uint8_t msg[], size_t len) { - return Sha256().append(msg, len).getHash(); -} - - -Sha256Hash Sha256::getDoubleHash(const uint8_t msg[], size_t len) { - const Sha256Hash innerHash = getHash(msg, len); - return getHash(innerHash.value, Sha256Hash::HASH_LEN); -} - - -Sha256Hash Sha256::getHmac(const uint8_t key[], size_t keyLen, const uint8_t msg[], size_t msgLen) { - assert(key != nullptr || keyLen == 0); - - // Preprocess key - uint8_t tempKey[BLOCK_LEN] = {}; - if (keyLen <= BLOCK_LEN) - Utils::copyBytes(tempKey, key, keyLen); - else { - const Sha256Hash keyHash = getHash(key, keyLen); - std::memcpy(tempKey, keyHash.value, Sha256Hash::HASH_LEN); - } - - // Compute inner hash - for (int i = 0; i < BLOCK_LEN; i++) - tempKey[i] ^= 0x36; - const Sha256Hash innerHash = Sha256() - .append(tempKey, BLOCK_LEN) - .append(msg, msgLen) - .getHash(); - - // Compute outer hash - for (int i = 0; i < BLOCK_LEN; i++) - tempKey[i] ^= 0x36 ^ 0x5C; - return Sha256() - .append(tempKey, BLOCK_LEN) - .append(innerHash.value, Sha256Hash::HASH_LEN) - .getHash(); -} - - -void Sha256::compress(uint32_t state[8], const uint8_t block[BLOCK_LEN]) { - assert(state != nullptr && block != nullptr); - - // Message schedule - uint32_t schedule[NUM_ROUNDS] = {}; - for (int i = 0; i < 64; i++) - schedule[i >> 2] |= static_cast(block[i]) << ((3 - (i & 3)) << 3); - - for (int i = 16; i < NUM_ROUNDS; i++) { - schedule[i] = 0U + schedule[i - 16] + schedule[i - 7] - + (rotr32(schedule[i - 15], 7) ^ rotr32(schedule[i - 15], 18) ^ (schedule[i - 15] >> 3)) - + (rotr32(schedule[i - 2], 17) ^ rotr32(schedule[i - 2], 19) ^ (schedule[i - 2] >> 10)); - } - - // The 64 rounds - uint32_t a = state[0]; - uint32_t b = state[1]; - uint32_t c = state[2]; - uint32_t d = state[3]; - uint32_t e = state[4]; - uint32_t f = state[5]; - uint32_t g = state[6]; - uint32_t h = state[7]; - for (int i = 0; i < NUM_ROUNDS; i++) { - uint32_t t1 = 0U + h + (rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25)) + (g ^ (e & (f ^ g))) + ROUND_CONSTANTS[i] + schedule[i]; - uint32_t t2 = 0U + (rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22)) + ((a & (b | c)) | (b & c)); - h = g; - g = f; - f = e; - e = 0U + d + t1; - d = c; - c = b; - b = a; - a = 0U + t1 + t2; - } - state[0] = 0U + state[0] + a; - state[1] = 0U + state[1] + b; - state[2] = 0U + state[2] + c; - state[3] = 0U + state[3] + d; - state[4] = 0U + state[4] + e; - state[5] = 0U + state[5] + f; - state[6] = 0U + state[6] + g; - state[7] = 0U + state[7] + h; -} - - -uint32_t Sha256::rotr32(uint32_t x, int i) { - return ((0U + x) << (32 - i)) | (x >> i); -} - - -const uint32_t Sha256::ROUND_CONSTANTS[NUM_ROUNDS] = { - UINT32_C(0x428A2F98), UINT32_C(0x71374491), UINT32_C(0xB5C0FBCF), UINT32_C(0xE9B5DBA5), - UINT32_C(0x3956C25B), UINT32_C(0x59F111F1), UINT32_C(0x923F82A4), UINT32_C(0xAB1C5ED5), - UINT32_C(0xD807AA98), UINT32_C(0x12835B01), UINT32_C(0x243185BE), UINT32_C(0x550C7DC3), - UINT32_C(0x72BE5D74), UINT32_C(0x80DEB1FE), UINT32_C(0x9BDC06A7), UINT32_C(0xC19BF174), - UINT32_C(0xE49B69C1), UINT32_C(0xEFBE4786), UINT32_C(0x0FC19DC6), UINT32_C(0x240CA1CC), - UINT32_C(0x2DE92C6F), UINT32_C(0x4A7484AA), UINT32_C(0x5CB0A9DC), UINT32_C(0x76F988DA), - UINT32_C(0x983E5152), UINT32_C(0xA831C66D), UINT32_C(0xB00327C8), UINT32_C(0xBF597FC7), - UINT32_C(0xC6E00BF3), UINT32_C(0xD5A79147), UINT32_C(0x06CA6351), UINT32_C(0x14292967), - UINT32_C(0x27B70A85), UINT32_C(0x2E1B2138), UINT32_C(0x4D2C6DFC), UINT32_C(0x53380D13), - UINT32_C(0x650A7354), UINT32_C(0x766A0ABB), UINT32_C(0x81C2C92E), UINT32_C(0x92722C85), - UINT32_C(0xA2BFE8A1), UINT32_C(0xA81A664B), UINT32_C(0xC24B8B70), UINT32_C(0xC76C51A3), - UINT32_C(0xD192E819), UINT32_C(0xD6990624), UINT32_C(0xF40E3585), UINT32_C(0x106AA070), - UINT32_C(0x19A4C116), UINT32_C(0x1E376C08), UINT32_C(0x2748774C), UINT32_C(0x34B0BCB5), - UINT32_C(0x391C0CB3), UINT32_C(0x4ED8AA4A), UINT32_C(0x5B9CCA4F), UINT32_C(0x682E6FF3), - UINT32_C(0x748F82EE), UINT32_C(0x78A5636F), UINT32_C(0x84C87814), UINT32_C(0x8CC70208), - UINT32_C(0x90BEFFFA), UINT32_C(0xA4506CEB), UINT32_C(0xBEF9A3F7), UINT32_C(0xC67178F2), -}; - - -} // namespace bcl diff --git a/src/lib/bcl/Sha256.hpp b/src/lib/bcl/Sha256.hpp deleted file mode 100644 index 5a77fc33..00000000 --- a/src/lib/bcl/Sha256.hpp +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#pragma once - -#include -#include -#include "Sha256Hash.hpp" - -namespace bcl { - - -/* - * Computes the SHA-256 hash of a sequence of bytes, returning a Sha256Hash object. - * Provides three static methods, and an instantiable stateful hasher. - */ -class Sha256 final { - - /*---- Public constants ----*/ - - public: static constexpr int BLOCK_LEN = 64; // In bytes - - - - /*---- Instance members ----*/ - - private: std::uint32_t state[8] = { - UINT32_C(0x6A09E667), UINT32_C(0xBB67AE85), UINT32_C(0x3C6EF372), UINT32_C(0xA54FF53A), - UINT32_C(0x510E527F), UINT32_C(0x9B05688C), UINT32_C(0x1F83D9AB), UINT32_C(0x5BE0CD19), - }; - private: std::uint64_t length; - private: std::uint8_t buffer[BLOCK_LEN]; - private: int bufferLen; - - - // Constructs a new SHA-256 hasher with an initially blank message. - public: explicit Sha256(); - - - // Appends message bytes to this ongoing hasher, and returns this object itself. - public: Sha256 &append(const std::uint8_t bytes[], std::size_t len); - - - // Returns the SHA-256 hash of all the bytes seen. Destroys the state so that no further append() or getHash() will be valid. - public: Sha256Hash getHash(); - - - - /*---- Static functions ----*/ - - public: static Sha256Hash getHash(const std::uint8_t msg[], std::size_t len); - - - public: static Sha256Hash getDoubleHash(const std::uint8_t msg[], std::size_t len); - - - public: static Sha256Hash getHmac(const std::uint8_t key[], std::size_t keyLen, const std::uint8_t msg[], std::size_t msgLen); - - - public: static void compress(std::uint32_t state[8], const std::uint8_t block[BLOCK_LEN]); - - - // Requires 1 <= i <= 31 - private: static std::uint32_t rotr32(std::uint32_t x, int i); - - - - /*---- Private constants ----*/ - - private: static constexpr int NUM_ROUNDS = 64; - private: static const std::uint32_t ROUND_CONSTANTS[NUM_ROUNDS]; - -}; - - -} // namespace bcl diff --git a/src/lib/bcl/Sha256Hash.cpp b/src/lib/bcl/Sha256Hash.cpp deleted file mode 100644 index 47b65bf8..00000000 --- a/src/lib/bcl/Sha256Hash.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#include -#include -#include "Sha256Hash.hpp" -#include "Utils.hpp" - -namespace bcl { - - -using std::uint8_t; -using std::size_t; - - -Sha256Hash::Sha256Hash(const uint8_t hash[HASH_LEN], size_t len) { - assert(hash != nullptr && len == HASH_LEN); - std::memcpy(value, hash, sizeof(value)); -} - - -Sha256Hash::Sha256Hash(const char *str) : - value() { - assert(str != nullptr && std::strlen(str) == HASH_LEN * 2); - for (int i = 0; i < HASH_LEN * 2; i++) { - int digit = Utils::parseHexDigit(str[HASH_LEN * 2 - 1 - i]); - assert(digit != -1); - value[i >> 1] |= digit << ((i & 1) << 2); - } -} - - -bool Sha256Hash::operator==(const Sha256Hash &other) const { - int diff = 0; - for (int i = 0; i < HASH_LEN; i++) - diff |= value[i] ^ other.value[i]; - return diff == 0; -} - - -bool Sha256Hash::operator!=(const Sha256Hash &other) const { - return !(*this == other); -} - - -} // namespace bcl diff --git a/src/lib/bcl/Sha256Hash.hpp b/src/lib/bcl/Sha256Hash.hpp deleted file mode 100644 index cc322b75..00000000 --- a/src/lib/bcl/Sha256Hash.hpp +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#pragma once - -#include -#include - -namespace bcl { - - -/* - * Represents a 32-byte SHA-256 hash value. - * - * Note that by Bitcoin convention, SHA-256 hash strings are serialized in byte-reversed order. - * For example, these three lines all represent the same hash value: - * - Bigint: 0x0102030405060708091011121314151617181920212223242526272829303132. - * - Byte array: {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16, - * 0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32}. - * - Hex string: "3231302928272625242322212019181716151413121110090807060504030201". - */ -class Sha256Hash final { - - public: static constexpr int HASH_LEN = 32; - - /*---- Fields ----*/ - - public: std::uint8_t value[HASH_LEN]; - - - - /*---- Constructors ----*/ - - // Constructs a Sha256Hash from the given array of 32 bytes (len is a dummy parameter that must equal 32). - // Constant-time with respect to the given array of values. - public: explicit Sha256Hash(const std::uint8_t hash[HASH_LEN], std::size_t len); - - - // Constructs a Sha256Hash from the given 64-character byte-reversed hexadecimal string. Not constant-time. - public: explicit Sha256Hash(const char *str); - - - - /*---- Instance methods ----*/ - - // Tests whether the given hash is equal to this one. Constant-time with respect to both values. - public: bool operator==(const Sha256Hash &other) const; - - - // Tests whether the given hash is unequal to this one. Constant-time with respect to both values. - public: bool operator!=(const Sha256Hash &other) const; - -}; - - -} // namespace bcl diff --git a/src/lib/bcl/Sha512.cpp b/src/lib/bcl/Sha512.cpp deleted file mode 100644 index a8b039b6..00000000 --- a/src/lib/bcl/Sha512.cpp +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#include -#include "Sha512.hpp" -#include "Utils.hpp" - -namespace bcl { - - -using std::uint8_t; -using std::uint64_t; -using std::size_t; - - -Sha512::Sha512() : - length(0), - bufferLen(0) {} - - -Sha512 &Sha512::append(const uint8_t bytes[], size_t len) { - assert(bytes != nullptr || len == 0); - for (size_t i = 0; i < len; i++) { - buffer[bufferLen] = bytes[i]; - bufferLen++; - if (bufferLen == BLOCK_LEN) { - compress(); - bufferLen = 0; - } - } - length += len; - return *this; -} - - -void Sha512::getHash(uint8_t result[HASH_LEN]) { - assert(result != nullptr); - uint64_t bitLength = length << 3; - uint8_t temp = 0x80; - append(&temp, 1); - temp = 0x00; - while (bufferLen != 112) - append(&temp, 1); - for (int i = 0; i < 8; i++) - append(&temp, 1); - for (int i = 0; i < 8; i++) { - temp = static_cast(bitLength >> ((7 - i) << 3)); - append(&temp, 1); - } - for (int i = 0; i < HASH_LEN; i++) - result[i] = static_cast(state[i >> 3] >> ((7 - (i & 7)) << 3)); -} - - -void Sha512::compress() { - // Message schedule - uint64_t schedule[NUM_ROUNDS] = {}; - for (int i = 0; i < 128; i++) - schedule[i >> 3] |= static_cast(buffer[i]) << ((7 - (i & 7)) << 3); - - for (int i = 16; i < NUM_ROUNDS; i++) { - schedule[i] = 0U + schedule[i - 16] + schedule[i - 7] - + (rotr64(schedule[i - 15], 1) ^ rotr64(schedule[i - 15], 8) ^ (schedule[i - 15] >> 7)) - + (rotr64(schedule[i - 2], 19) ^ rotr64(schedule[i - 2], 61) ^ (schedule[i - 2] >> 6)); - } - - // The 80 rounds - uint64_t a = state[0]; - uint64_t b = state[1]; - uint64_t c = state[2]; - uint64_t d = state[3]; - uint64_t e = state[4]; - uint64_t f = state[5]; - uint64_t g = state[6]; - uint64_t h = state[7]; - for (int i = 0; i < NUM_ROUNDS; i++) { - uint64_t t1 = 0U + h + (rotr64(e, 14) ^ rotr64(e, 18) ^ rotr64(e, 41)) + (g ^ (e & (f ^ g))) + ROUND_CONSTANTS[i] + schedule[i]; - uint64_t t2 = 0U + (rotr64(a, 28) ^ rotr64(a, 34) ^ rotr64(a, 39)) + ((a & (b | c)) | (b & c)); - h = g; - g = f; - f = e; - e = 0U + d + t1; - d = c; - c = b; - b = a; - a = 0U + t1 + t2; - } - state[0] = 0U + state[0] + a; - state[1] = 0U + state[1] + b; - state[2] = 0U + state[2] + c; - state[3] = 0U + state[3] + d; - state[4] = 0U + state[4] + e; - state[5] = 0U + state[5] + f; - state[6] = 0U + state[6] + g; - state[7] = 0U + state[7] + h; -} - - -void Sha512::getHash(const uint8_t msg[], size_t len, uint8_t hashResult[HASH_LEN]) { - Sha512().append(msg, len).getHash(hashResult); -} - - -void Sha512::getHmac(const uint8_t key[], size_t keyLen, const uint8_t msg[], size_t msgLen, uint8_t result[HASH_LEN]) { - assert(key != nullptr || keyLen == 0); - - // Preprocess key - uint8_t tempKey[BLOCK_LEN] = {}; - if (keyLen <= BLOCK_LEN) - Utils::copyBytes(tempKey, key, keyLen); - else - getHash(key, keyLen, tempKey); - - // Compute inner hash - for (int i = 0; i < BLOCK_LEN; i++) - tempKey[i] ^= 0x36; - uint8_t innerHash[HASH_LEN] = {}; - Sha512() - .append(tempKey, BLOCK_LEN) - .append(msg, msgLen) - .getHash(innerHash); - - // Compute outer hash - for (int i = 0; i < BLOCK_LEN; i++) - tempKey[i] ^= 0x36 ^ 0x5C; - Sha512() - .append(tempKey, BLOCK_LEN) - .append(innerHash, HASH_LEN) - .getHash(result); -} - - -uint64_t Sha512::rotr64(uint64_t x, int i) { - return ((0U + x) << (64 - i)) | (x >> i); -} - - -const uint64_t Sha512::ROUND_CONSTANTS[NUM_ROUNDS] = { - UINT64_C(0x428A2F98D728AE22), UINT64_C(0x7137449123EF65CD), UINT64_C(0xB5C0FBCFEC4D3B2F), UINT64_C(0xE9B5DBA58189DBBC), - UINT64_C(0x3956C25BF348B538), UINT64_C(0x59F111F1B605D019), UINT64_C(0x923F82A4AF194F9B), UINT64_C(0xAB1C5ED5DA6D8118), - UINT64_C(0xD807AA98A3030242), UINT64_C(0x12835B0145706FBE), UINT64_C(0x243185BE4EE4B28C), UINT64_C(0x550C7DC3D5FFB4E2), - UINT64_C(0x72BE5D74F27B896F), UINT64_C(0x80DEB1FE3B1696B1), UINT64_C(0x9BDC06A725C71235), UINT64_C(0xC19BF174CF692694), - UINT64_C(0xE49B69C19EF14AD2), UINT64_C(0xEFBE4786384F25E3), UINT64_C(0x0FC19DC68B8CD5B5), UINT64_C(0x240CA1CC77AC9C65), - UINT64_C(0x2DE92C6F592B0275), UINT64_C(0x4A7484AA6EA6E483), UINT64_C(0x5CB0A9DCBD41FBD4), UINT64_C(0x76F988DA831153B5), - UINT64_C(0x983E5152EE66DFAB), UINT64_C(0xA831C66D2DB43210), UINT64_C(0xB00327C898FB213F), UINT64_C(0xBF597FC7BEEF0EE4), - UINT64_C(0xC6E00BF33DA88FC2), UINT64_C(0xD5A79147930AA725), UINT64_C(0x06CA6351E003826F), UINT64_C(0x142929670A0E6E70), - UINT64_C(0x27B70A8546D22FFC), UINT64_C(0x2E1B21385C26C926), UINT64_C(0x4D2C6DFC5AC42AED), UINT64_C(0x53380D139D95B3DF), - UINT64_C(0x650A73548BAF63DE), UINT64_C(0x766A0ABB3C77B2A8), UINT64_C(0x81C2C92E47EDAEE6), UINT64_C(0x92722C851482353B), - UINT64_C(0xA2BFE8A14CF10364), UINT64_C(0xA81A664BBC423001), UINT64_C(0xC24B8B70D0F89791), UINT64_C(0xC76C51A30654BE30), - UINT64_C(0xD192E819D6EF5218), UINT64_C(0xD69906245565A910), UINT64_C(0xF40E35855771202A), UINT64_C(0x106AA07032BBD1B8), - UINT64_C(0x19A4C116B8D2D0C8), UINT64_C(0x1E376C085141AB53), UINT64_C(0x2748774CDF8EEB99), UINT64_C(0x34B0BCB5E19B48A8), - UINT64_C(0x391C0CB3C5C95A63), UINT64_C(0x4ED8AA4AE3418ACB), UINT64_C(0x5B9CCA4F7763E373), UINT64_C(0x682E6FF3D6B2B8A3), - UINT64_C(0x748F82EE5DEFB2FC), UINT64_C(0x78A5636F43172F60), UINT64_C(0x84C87814A1F0AB72), UINT64_C(0x8CC702081A6439EC), - UINT64_C(0x90BEFFFA23631E28), UINT64_C(0xA4506CEBDE82BDE9), UINT64_C(0xBEF9A3F7B2C67915), UINT64_C(0xC67178F2E372532B), - UINT64_C(0xCA273ECEEA26619C), UINT64_C(0xD186B8C721C0C207), UINT64_C(0xEADA7DD6CDE0EB1E), UINT64_C(0xF57D4F7FEE6ED178), - UINT64_C(0x06F067AA72176FBA), UINT64_C(0x0A637DC5A2C898A6), UINT64_C(0x113F9804BEF90DAE), UINT64_C(0x1B710B35131C471B), - UINT64_C(0x28DB77F523047D84), UINT64_C(0x32CAAB7B40C72493), UINT64_C(0x3C9EBE0A15C9BEBC), UINT64_C(0x431D67C49C100D4C), - UINT64_C(0x4CC5D4BECB3E42B6), UINT64_C(0x597F299CFC657E2A), UINT64_C(0x5FCB6FAB3AD6FAEC), UINT64_C(0x6C44198C4A475817), -}; - - -} // namespace bcl diff --git a/src/lib/bcl/Sha512.hpp b/src/lib/bcl/Sha512.hpp deleted file mode 100644 index e257b214..00000000 --- a/src/lib/bcl/Sha512.hpp +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#pragma once - -#include -#include - -namespace bcl { - - -/* - * Computes the SHA-512 hash of a sequence of bytes. The hash value is 64 bytes long. - * Provides two static methods, and an instantiable stateful hasher. - */ -class Sha512 final { - - /*---- Scalar constants ----*/ - - public: static constexpr int HASH_LEN = 64; - private: static constexpr int BLOCK_LEN = 128; - private: static constexpr int NUM_ROUNDS = 80; - - - - /*---- Instance members ----*/ - - private: std::uint64_t state[8] = { - UINT64_C(0x6A09E667F3BCC908), UINT64_C(0xBB67AE8584CAA73B), UINT64_C(0x3C6EF372FE94F82B), UINT64_C(0xA54FF53A5F1D36F1), - UINT64_C(0x510E527FADE682D1), UINT64_C(0x9B05688C2B3E6C1F), UINT64_C(0x1F83D9ABFB41BD6B), UINT64_C(0x5BE0CD19137E2179), - }; - private: std::uint64_t length; - private: std::uint8_t buffer[BLOCK_LEN]; - private: int bufferLen; - - - // Constructs a new SHA-512 hasher with an initially blank message. - public: explicit Sha512(); - - - // Appends message bytes to this ongoing hasher, and returns this object itself. - public: Sha512 &append(const std::uint8_t bytes[], std::size_t len); - - - // Returns the SHA-512 hash of all the bytes seen. Destroys the state so that no further append() or getHash() will be valid. - public: void getHash(std::uint8_t result[HASH_LEN]); - - - private: void compress(); - - - - /*---- Static functions ----*/ - - public: static void getHash(const std::uint8_t msg[], std::size_t len, std::uint8_t hashResult[HASH_LEN]); - - - public: static void getHmac(const std::uint8_t key[], std::size_t keyLen, const std::uint8_t msg[], std::size_t msgLen, std::uint8_t result[HASH_LEN]); - - - // Requires 1 <= i <= 63 - private: static std::uint64_t rotr64(std::uint64_t x, int i); - - - - /*---- Array constants ----*/ - - private: static const std::uint64_t ROUND_CONSTANTS[NUM_ROUNDS]; - -}; - - -} // namespace bcl diff --git a/src/lib/bcl/Uint256.cpp b/src/lib/bcl/Uint256.cpp deleted file mode 100644 index bb6f5f43..00000000 --- a/src/lib/bcl/Uint256.cpp +++ /dev/null @@ -1,220 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#include -#include -#include "Uint256.hpp" -#include "Utils.hpp" - -namespace bcl { - - -using std::uint8_t; -using std::uint32_t; -using std::uint64_t; - - -Uint256::Uint256() : - value() {} - - -Uint256::Uint256(const char *str) : - value() { - assert(str != nullptr && std::strlen(str) == NUM_WORDS * 8); - for (int i = 0; i < NUM_WORDS * 8; i++) { - int digit = Utils::parseHexDigit(str[NUM_WORDS * 8 - 1 - i]); - assert(digit != -1); - value[i >> 3] |= static_cast(digit) << ((i & 7) << 2); - } -} - - -Uint256::Uint256(const uint8_t b[NUM_WORDS * 4]) : - value() { - assert(b != nullptr); - for (int i = 0; i < NUM_WORDS * 4; i++) - value[i >> 2] |= static_cast(b[NUM_WORDS * 4 - 1 - i]) << ((i & 3) << 3); -} - - -Uint256::Uint256(const FieldInt &val) { - std::memcpy(this->value, val.value, sizeof(value)); -} - - -uint32_t Uint256::add(const Uint256 &other, uint32_t enable) { - assert(&other != this && (enable >> 1) == 0); - uint32_t mask = -enable; - uint32_t carry = 0; - for (int i = 0; i < NUM_WORDS; i++) { - uint64_t sum = static_cast(value[i]) + (other.value[i] & mask) + carry; - value[i] = static_cast(sum); - carry = static_cast(sum >> 32); - assert((carry >> 1) == 0); - } - return carry; -} - - -uint32_t Uint256::subtract(const Uint256 &other, uint32_t enable) { - assert(&other != this && (enable >> 1) == 0); - uint32_t mask = -enable; - uint32_t borrow = 0; - for (int i = 0; i < NUM_WORDS; i++) { - uint64_t diff = static_cast(value[i]) - (other.value[i] & mask) - borrow; - value[i] = static_cast(diff); - borrow = -static_cast(diff >> 32); - assert((borrow >> 1) == 0); - } - return borrow; -} - - -uint32_t Uint256::shiftLeft1() { - uint32_t prev = 0; - for (int i = 0; i < NUM_WORDS; i++) { - uint32_t cur = value[i]; - value[i] = (0U + cur) << 1 | prev >> 31; - prev = cur; - } - return prev >> 31; -} - - -void Uint256::shiftRight1(uint32_t enable) { - assert((enable >> 1) == 0); - uint32_t mask = -enable; - uint32_t cur = value[0]; - for (int i = 0; i < NUM_WORDS - 1; i++) { - uint32_t next = value[i + 1]; - value[i] = ((cur >> 1 | (0U + next) << 31) & mask) | (cur & ~mask); - cur = next; - } - value[NUM_WORDS - 1] = ((cur >> 1) & mask) | (cur & ~mask); -} - - -void Uint256::reciprocal(const Uint256 &modulus) { - // Extended binary GCD algorithm - assert(&modulus != this && (modulus.value[0] & 1) == 1 && modulus > ONE && *this < modulus); - Uint256 x = modulus; - Uint256 y = *this; - Uint256 a = ZERO; - Uint256 b = ONE; - Uint256 halfModulus = modulus; - halfModulus.add(ONE); - halfModulus.shiftRight1(); - - // Loop invariant: x = a*this mod modulus, and y = b*this mod modulus - for (int i = 0; i < NUM_WORDS * 32 * 2; i++) { - // Try to reduce a trailing zero of y. Pseudocode: - // if (y % 2 == 0) { - // y /= 2 - // b = b % 2 == 0 ? b / 2 : modulus - (modulus - b) / 2 - // } - assert((x.value[0] & 1) == 1); - uint32_t yEven = (y.value[0] & 1) ^ 1; - uint32_t bOdd = b.value[0] & 1; - y.shiftRight1(yEven); - b.shiftRight1(yEven); - b.add(halfModulus, yEven & bOdd); - - // If allowed, try to swap so that y >= x and then do y -= x. Pseudocode: - // if (y % 2 == 1) { - // if (x > y) { - // x, y = y, x - // a, b = b, a - // } - // y -= x - // b -= a - // b %= modulus - // } - uint32_t enable = y.value[0] & 1; - uint32_t doswap = enable & static_cast(x > y); - x.swap(y, doswap); - y.subtract(x, enable); - a.swap(b, doswap); - uint32_t borrow = b.subtract(a, enable); - b.add(modulus, borrow); - } - assert((x == ONE) | (x == modulus)); // Either gcd(this, modulus) = 1 or this = 0 - this->replace(a, static_cast(*this != ZERO)); -} - - -void Uint256::replace(const Uint256 &other, uint32_t enable) { - assert((enable >> 1) == 0); - uint32_t mask = -enable; - for (int i = 0; i < NUM_WORDS; i++) - value[i] = (other.value[i] & mask) | (value[i] & ~mask); -} - - -void Uint256::swap(Uint256 &other, uint32_t enable) { - assert((enable >> 1) == 0); - uint32_t mask = -enable; - for (int i = 0; i < NUM_WORDS; i++) { - uint32_t x = this->value[i]; - uint32_t y = other.value[i]; - this->value[i] = (y & mask) | (x & ~mask); - other.value[i] = (x & mask) | (y & ~mask); - } -} - - -void Uint256::getBigEndianBytes(uint8_t b[NUM_WORDS * 4]) const { - assert(b != nullptr); - for (int i = 0; i < NUM_WORDS; i++) - Utils::storeBigUint32(value[i], &b[(NUM_WORDS - 1 - i) * 4]); -} - - -bool Uint256::operator==(const Uint256 &other) const { - uint32_t diff = 0; - for (int i = 0; i < NUM_WORDS; i++) - diff |= value[i] ^ other.value[i]; - return diff == 0; -} - - -bool Uint256::operator!=(const Uint256 &other) const { - return !(*this == other); -} - - -bool Uint256::operator<(const Uint256 &other) const { - bool result = false; - for (int i = 0; i < NUM_WORDS; i++) { - bool eq = value[i] == other.value[i]; - result = (eq & result) | (!eq & (value[i] < other.value[i])); - } - return result; -} - - -bool Uint256::operator<=(const Uint256 &other) const { - return !(other < *this); -} - - -bool Uint256::operator>(const Uint256 &other) const { - return other < *this; -} - - -bool Uint256::operator>=(const Uint256 &other) const { - return !(*this < other); -} - - -// Static initializers -const Uint256 Uint256::ZERO; -const Uint256 Uint256::ONE("0000000000000000000000000000000000000000000000000000000000000001"); - - -} // namespace bcl diff --git a/src/lib/bcl/Uint256.hpp b/src/lib/bcl/Uint256.hpp deleted file mode 100644 index 6ae0950b..00000000 --- a/src/lib/bcl/Uint256.hpp +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#pragma once - -#include - -namespace bcl { - - -class FieldInt; // Forward declaration - - -/* - * An unsigned 256-bit integer, represented as eight unsigned 32-bit words in little endian. - * All arithmetic operations are performed modulo 2^256 (the standard unsigned overflow behavior). - * Instances of this class are mutable. All possible values are valid. - * - * For example, the integer 0x0123456789ABCDEF000000001111111122222222333333334444444455555555 is represented by - * the array {0x55555555, 0x44444444, 0x33333333, 0x22222222, 0x11111111, 0x00000000, 0x89ABCDEF, 0x01234567}. - */ -class Uint256 { - - public: static constexpr int NUM_WORDS = 8; - - /*---- Fields ----*/ - - // The mutable words representing this number in little endian, conceptually like this: - // actualValue = value[0] << 0 | value[1] << 32 | ... | value[7] << 224. - public: std::uint32_t value[NUM_WORDS]; - - - - /*---- Constructors ----*/ - - // Constructs a Uint256 initialized to zero. Constant-time. - // For clarity, only use this constructor if the variable will be overwritten immediately - // (pretend that this constructor leaves the value array uninitialized). - // For actual zero values, please explicitly initialize them with: Uint256 num(Uint256::ZERO); - public: explicit Uint256(); - - - // Constructs a Uint256 from the given 64-character hexadecimal string. Not constant-time. - // If the syntax of the string is invalid, then an assertion will fail. - public: explicit Uint256(const char *str); - - - // Constructs a Uint256 from the given 32 bytes encoded in big-endian. - // Constant-time with respect to the input array values. All possible values are valid. - public: explicit Uint256(const std::uint8_t b[NUM_WORDS * 4]); - - - // Constructs a Uint256 from the given FieldInt. Constant-time with respect to the given value. - // All possible FieldInt values are valid. - public: explicit Uint256(const FieldInt &val); - - - - /*---- Arithmetic methods ----*/ - - // Adds the given number into this number, modulo 2^256. The other number must be a distinct object. - // Enable must be 1 to perform the operation or 0 to do nothing. Returns the carry-out bit, which is 0 or 1. - // Constant-time with respect to both values and the enable. - public: std::uint32_t add(const Uint256 &other, std::uint32_t enable=1); - - - // Subtracts the given number from this number, modulo 2^256. The other number must be a distinct object. - // Enable must be 1 to perform the operation or 0 to do nothing. Returns the borrow-out bit, which is 0 or 1. - // Constant-time with respect to both values and the enable. - public: std::uint32_t subtract(const Uint256 &other, std::uint32_t enable=1); - - - // Shifts this number left by 1 bit (same as multiplying by 2), modulo 2^256. - // Returns the old leftmost bit, which is 0 or 1. Constant-time with respect to this value. - public: std::uint32_t shiftLeft1(); - - - // Shifts this number right by 1 bit (same as dividing by 2 and flooring). - // Enable must be 1 to perform the operation or 0 to do nothing. - // Constant-time with respect to this value and the enable. - public: void shiftRight1(std::uint32_t enable=1); - - - // Computes the multiplicative inverse of this number with respect to the given modulus. - // The modulus must be odd and coprime to this number. This number must be less than the modulus. - // If this number is zero, the reciprocal is zero. Constant-time with respect to this value. - public: void reciprocal(const Uint256 &modulus); - - - /*---- Miscellaneous methods ----*/ - - // Copies the given number into this number if enable is 1, or does nothing if enable is 0. - // Constant-time with respect to both values and the enable. - public: void replace(const Uint256 &other, std::uint32_t enable); - - - // Swaps the value of this number with the given number if enable is 1, or does nothing if enable is 0. - // Constant-time with respect to both values and the enable. - public: void swap(Uint256 &other, std::uint32_t enable); - - - // Writes this 256-bit integer as 32 bytes encoded in big endian to the given array. - // Constant-time with respect to this value. - public: void getBigEndianBytes(std::uint8_t b[NUM_WORDS * 4]) const; - - - /*---- Equality/inequality operators ----*/ - - // Tests whether this number is equal to the given number. Constant-time with respect to both values. - public: bool operator==(const Uint256 &other) const; - - // Tests whether this number is unequal to the given number. Constant-time with respect to both values. - public: bool operator!=(const Uint256 &other) const; - - - // Tests whether this number is less than the given number. Constant-time with respect to both values. - public: bool operator<(const Uint256 &other) const; - - // Tests whether this number is less than or equal to the given number. Constant-time with respect to both values. - public: bool operator<=(const Uint256 &other) const; - - // Tests whether this number is greater than the given number. Constant-time with respect to both values. - public: bool operator>(const Uint256 &other) const; - - // Tests whether this number is greater than or equal to the given number. Constant-time with respect to both values. - public: bool operator>=(const Uint256 &other) const; - - - - /*---- Class constants ----*/ - - public: static const Uint256 ZERO; - public: static const Uint256 ONE; - -}; - - -} // namespace bcl - -#include "FieldInt.hpp" diff --git a/src/lib/bcl/Utils.cpp b/src/lib/bcl/Utils.cpp deleted file mode 100644 index 9c86bd57..00000000 --- a/src/lib/bcl/Utils.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#include -#include "Utils.hpp" - -namespace bcl { - - -using std::uint8_t; - - -int Utils::parseHexDigit(int ch) { - if ('0' <= ch && ch <= '9') - return ch - '0'; - else if ('a' <= ch && ch <= 'f') - return ch - 'a' + 10; - else if ('A' <= ch && ch <= 'F') - return ch - 'A' + 10; - else - return -1; -} - - -void Utils::copyBytes(void *dest, const void *src, std::size_t count) { - if (count > 0) - std::memmove(dest, src, count); -} - - -void Utils::storeBigUint32(std::uint32_t x, uint8_t arr[4]) { - arr[0] = static_cast(x >> 24); - arr[1] = static_cast(x >> 16); - arr[2] = static_cast(x >> 8); - arr[3] = static_cast(x >> 0); -} - - -const char *Utils::HEX_DIGITS = "0123456789abcdef"; - - -} // namespace bcl diff --git a/src/lib/bcl/Utils.hpp b/src/lib/bcl/Utils.hpp deleted file mode 100644 index 2ee3fd55..00000000 --- a/src/lib/bcl/Utils.hpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Bitcoin cryptography library - * Copyright (c) Project Nayuki - * - * https://www.nayuki.io/page/bitcoin-cryptography-library - * https://github.com/nayuki/Bitcoin-Cryptography-Library - */ - -#pragma once - -#include -#include - -namespace bcl { - - - -/* - * Miscellaneous utilities used in a variety of places. - */ -class Utils final { - - public: static const char *HEX_DIGITS; - - - // Returns the numerical value of a hexadecimal digit character - // (e.g. '9' -> 9, 'a' -> 10, 'B' -> 11), or -1 if the character is invalid. - public: static int parseHexDigit(int ch); - - - // A safe wrapper over memmove() to avoid undefined behavior. This function can be a drop-in replacement - // for both memcpy() and memmove(). It is useful when count is a variable number that is sometimes zero. - // Note that src and dest can overlap. - // The function returns immediately if count is 0. This is safer than calling memmove() with a count of 0, because - // it would be undefined behavior if src or dest is null, or if either one is pointing to the end of an array. - // The function is not helpful for code that calls memcpy/memmove with a known positive constant count value. - public: static void copyBytes(void *dest, const void *src, std::size_t count); - - - public: static void storeBigUint32(std::uint32_t x, std::uint8_t arr[4]); - - - Utils() = delete; // Not instantiable - -}; - -} // namespace bcl diff --git a/src/managers/fee_manager.cpp b/src/managers/fee_manager.cpp deleted file mode 100644 index 949cde56..00000000 --- a/src/managers/fee_manager.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/** - * This file is part of Ark Cpp Crypto. - * - * (c) Ark Ecosystem - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - **/ - -#include "managers/fee_manager.hpp" - -#include "common/fee_policy.hpp" - -namespace Ark { -namespace Crypto { -namespace managers { - -uint64_t FeeManager::getFee(uint8_t type) const { - return type <= this->feePolicy_.size() - ? this->feePolicy_.at(type) - : AMOUNT_ZERO; -} - -/**/ - -void FeeManager::setFee(uint8_t type, uint64_t amount) { - if (type > this->feePolicy_.size()) { - this->feePolicy_.resize(type + 1); - }; - this->feePolicy_.at(type) = amount; -} - -/**/ - -FeePolicy FeeManager::getPolicy() const { - return this->feePolicy_; -} - -/**/ - -void FeeManager::setPolicy(const FeePolicy& policy) { - this->feePolicy_ = policy; -} - -} // namespace managers -} // namespace Crypto -} // namespace Ark diff --git a/src/managers/fee_manager.hpp b/src/managers/fee_manager.hpp index 51b8e917..8d07cda0 100644 --- a/src/managers/fee_manager.hpp +++ b/src/managers/fee_manager.hpp @@ -1,3 +1,4 @@ + /** * This file is part of Ark Cpp Crypto. * @@ -7,26 +8,45 @@ * file that was distributed with this source code. **/ -#ifndef MANAGERS_FEE_MANAGER_HPP -#define MANAGERS_FEE_MANAGER_HPP +#ifndef ARK_MANAGERS_FEE_MANAGER_HPP +#define ARK_MANAGERS_FEE_MANAGER_HPP #include "common/fee_policy.hpp" -#include "defaults/static_fees.hpp" + +#include "transactions/defaults/fees.hpp" namespace Ark { namespace Crypto { namespace managers { +//////////////////////////////////////////////////////////////////////////////// +// FeeManager +// Fee Getter/Setter for the common/Configuration-type. class FeeManager { - public: - uint64_t getFee(uint8_t type) const ; - void setFee(uint8_t type, uint64_t amount); - - FeePolicy getPolicy() const; - void setPolicy(const FeePolicy& policy); - - protected: - FeePolicy feePolicy_ = StaticFeePolicy; + public: + //////////////////////////////////////////////////////////////////////////// + auto getFee(uint16_t type) const -> uint64_t { + return type < this->feePolicy_.size() ? this->feePolicy_.at(type) : 0ULL; + } + + //////////////////////////////////////////////////////////////////////////// + void setFee(uint16_t type, uint64_t amount) { + this->feePolicy_.insert(this->feePolicy_.begin() + type, amount); + } + + //////////////////////////////////////////////////////////////////////////// + auto getPolicy() const -> FeePolicy { + return this->feePolicy_; + } + + //////////////////////////////////////////////////////////////////////////// + void setPolicy(const FeePolicy &policy){ + this->feePolicy_ = policy; + } + + protected: + //////////////////////////////////////////////////////////////////////////// + FeePolicy feePolicy_ = transactions::StaticFeePolicy; }; } // namespace managers diff --git a/src/managers/network_manager.cpp b/src/managers/network_manager.cpp deleted file mode 100644 index 10e3692c..00000000 --- a/src/managers/network_manager.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/** - * This file is part of Ark Cpp Crypto. - * - * (c) Ark Ecosystem - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - **/ - -#include "managers/network_manager.hpp" - -#include "common/network.hpp" - -namespace Ark { -namespace Crypto { -namespace managers { - -Network NetworkManager::getNetwork() const { - return this->network_; -} - -/**/ - -void NetworkManager::setNetwork(const Network& network) { - this->network_ = network; -} - -} // namespace managers -} // namespace Crypto -} // namespace Ark diff --git a/src/managers/network_manager.hpp b/src/managers/network_manager.hpp index 462a8223..c2547a0b 100644 --- a/src/managers/network_manager.hpp +++ b/src/managers/network_manager.hpp @@ -7,23 +7,35 @@ * file that was distributed with this source code. **/ -#ifndef MANAGERS_NETWORK_MANAGER_HPP -#define MANAGERS_NETWORK_MANAGER_HPP +#ifndef ARK_MANAGERS_NETWORK_MANAGER_HPP +#define ARK_MANAGERS_NETWORK_MANAGER_HPP #include "common/network.hpp" + #include "networks/devnet.hpp" namespace Ark { namespace Crypto { namespace managers { +//////////////////////////////////////////////////////////////////////////////// +// Network Manager +// Network Getter/Setter for the common/Configuration-type. class NetworkManager { - public: - Network getNetwork() const; - void setNetwork(const Network& network); + public: + //////////////////////////////////////////////////////////////////////////// + auto getNetwork() const -> Network { + return this->network_; + } + + //////////////////////////////////////////////////////////////////////////// + void setNetwork(const Network &network) { + this->network_ = network; + } - protected: - Network network_ = Devnet; + protected: + //////////////////////////////////////////////////////////////////////////// + Network network_ = Devnet; }; } // namespace managers diff --git a/src/transactions/builder.cpp b/src/transactions/builder.cpp deleted file mode 100644 index 0322c5d7..00000000 --- a/src/transactions/builder.cpp +++ /dev/null @@ -1,170 +0,0 @@ - -#include "transactions/builder.h" - -#include -#include -#include -#include - -#include "common/configuration.hpp" -#include "defaults/transaction_types.hpp" -#include "identities/address.hpp" -#include "crypto/slot.hpp" -#include "utils/crypto_helpers.h" -#include "identities/publickey.hpp" - -namespace Ark { -namespace Crypto { -namespace Transactions { - -Transaction Builder::buildTransfer( - std::string recipient, - uint64_t amount, - std::string vendorField, - const std::string& passphrase, - const std::string&secondPassphrase, - const Configuration& configuration) { - Transaction transaction; - if (amount < 1ULL) { return transaction; } - - transaction.version = 0x01; - transaction.type = TransactionTypes::Transfer; - transaction.fee = configuration.getFee(TransactionTypes::Transfer); - transaction.recipient = std::move(recipient); - transaction.amount = amount; - transaction.vendorField = std::move(vendorField); - - sign( - transaction, - std::move(passphrase), - std::move(secondPassphrase)); - - return transaction; -} - -/**/ - -Transaction Builder::buildSecondSignatureRegistration( - const std::string& passphrase, - const std::string& secondPassphrase, - const Configuration& configuration) { - Transaction transaction; - - transaction.version = 0x01; - transaction.type = TransactionTypes::SecondSignatureRegistration; - transaction.fee = configuration.getFee( - TransactionTypes::SecondSignatureRegistration); - - transaction.asset.signature.publicKey = - identities::PublicKey::fromPassphrase(secondPassphrase.c_str()) - .toString(); - - sign( - transaction, - std::move(passphrase), - std::move(secondPassphrase)); - - return transaction; -} - -/**/ - -Transaction Builder::buildDelegateRegistration( - std::string username, - const std::string& passphrase, - const std::string& secondPassphrase, - const Configuration& configuration) { - Transaction transaction; - - transaction.version = 0x01; - transaction.type = TransactionTypes::DelegateRegistration; - transaction.fee = configuration.getFee(TransactionTypes::DelegateRegistration); - transaction.asset.delegate.username = std::move(username); - - sign(transaction, - std::move(passphrase), - std::move(secondPassphrase)); - - return transaction; -} - -/**/ - -Transaction Builder::buildVote( - std::vector votes, - const std::string& passphrase, - const std::string& secondPassphrase, - const Configuration& configuration) { - Transaction transaction; - - transaction.version = 0x01; - transaction.type = TransactionTypes::Vote; - transaction.fee = configuration.getFee(TransactionTypes::Vote); - transaction.asset.votes = std::move(votes); - - const auto recipient = identities::Address::fromPassphrase( - passphrase.c_str(), - configuration.getNetwork().version); - transaction.recipient = recipient.toString(); - - sign(transaction, - std::move(passphrase), - std::move(secondPassphrase)); - - return transaction; -} - -/**/ - -Transaction Builder::buildMultiSignatureRegistration( - uint8_t min, - uint8_t lifetime, - std::vector& keysgroup, - const std::string& passphrase, - const std::string& secondPassphrase, - const Configuration& configuration) { - Transaction transaction; - - transaction.version = 0x01; - transaction.type = TransactionTypes::MultiSignatureRegistration; - transaction.fee = - (keysgroup.size() + 1) * - configuration.getFee(TransactionTypes::MultiSignatureRegistration); - transaction.asset.multiSignature.min = min; - transaction.asset.multiSignature.lifetime = lifetime; - transaction.asset.multiSignature.keysgroup = keysgroup; - - const auto recipient = identities::Address::fromPassphrase( - passphrase.c_str(), - configuration.getNetwork().version); - transaction.recipient = recipient.toString(); - - sign( - transaction, - std::move(passphrase), - std::move(secondPassphrase)); - - return transaction; -} - -/**/ - -void Builder::sign( - Transaction& transaction, - const std::string& passphrase, - const std::string& secondPassphrase, - const Configuration& configuration) { - transaction.timestamp = static_cast( - Slot::time(configuration.getNetwork())); - transaction.sign(passphrase.c_str()); - - if (secondPassphrase.length() > 0) { - transaction.secondSign(secondPassphrase.c_str()); - }; - - transaction.id = transaction.getId(); -} - -} // namespace Transactions -} // namespace Crypto -} // namespace Ark diff --git a/src/transactions/builders/common.hpp b/src/transactions/builders/common.hpp new file mode 100644 index 00000000..a2a09206 --- /dev/null +++ b/src/transactions/builders/common.hpp @@ -0,0 +1,198 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_BUILDER_COMMON_HPP +#define ARK_TRANSACTIONS_BUILDER_COMMON_HPP + +#include +#include +#include +#include + +#include "common/configuration.hpp" + +#include "identities/keys.hpp" + +#include "transactions/transaction.hpp" + +#include "interfaces/identities.hpp" + +#include "utils/str.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { +namespace builder { + +//////////////////////////////////////////////////////////////////////////////// +// Internal Common Transaction Builder Class +template class Common { + public: + //////////////////////////////////////////////////////////////////////////// + // Version + T &version(uint8_t version) { + this->transaction.data.version = version; + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // Network + // + // - Devnet: 30 + // - Mainnet: 23 + // + // --- + T &network(uint8_t network) { + this->transaction.data.network = network; + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // TypeGroup + T &typeGroup(uint16_t typeGroup) { + this->transaction.data.typeGroup = typeGroup; + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // Transaction Type + T &type(uint16_t type) { + this->transaction.data.type = type; + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // Nonce - v2 Only + T &nonce(uint64_t nonce) { + this->transaction.data.nonce = nonce; + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // Timestamp - v1 Only + T ×tamp(uint32_t timestamp) { + this->transaction.data.timestamp = timestamp; + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // Sender PublicKey + // + // This builder item is unecessary when signing the builder inline. + // - e.g `.sign(passphrase)` + // + // `Transaction::sign(passphrase)` adds this automatically. + // + // --- + T &senderPublicKey(const uint8_t *senderPublicKey) { + std::copy_n(senderPublicKey, + PUBLICKEY_COMPRESSED_LEN, + this->transaction.data.senderPublicKey.begin()); + + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // Fee + T &fee(uint64_t fee) { + this->transaction.data.fee = fee; + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // VendorField - std::vector + T &vendorFieldHex(const uint8_t *vendorField, const size_t &length) { + this->transaction.data.vendorField.insert( + this->transaction.data.vendorField.begin(), + vendorField, + vendorField + length); + + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // VendorField - std::string + T &vendorField(const std::string &vendorField) { + const auto vf = StringToBytes(vendorField); + + this->transaction.data.vendorField.insert( + this->transaction.data.vendorField.begin(), + vf.begin(), + vf.end()); + + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // Signature + T &signature(const uint8_t *signature, const size_t &length) { + this->transaction.data.signature.resize(length); + std::copy_n(signature, + length, + this->transaction.data.signature.begin()); + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // Second Signature + T &secondSignature(const uint8_t *secondSignature, const size_t &length) { + this->transaction.data.signature.resize(length); + std::copy_n(secondSignature, + length, + this->transaction.data.signature.begin()); + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // Sign + T &sign(const std::string &passphrase) { + this->transaction.sign(passphrase); + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // Second Sign + T &secondSign(const std::string &secondPassphrase) { + this->transaction.secondSign(secondPassphrase); + return reinterpret_cast(*this); + } + + //////////////////////////////////////////////////////////////////////////// + // Build + // + // If calling 'builder::sign()' before calling 'build()', + // ensure the Fees and Network are properly configured first. + // + // --- + Transaction &build(const Configuration &config = {}) { + // if the transaction fee is the default of '0', use the config fees. + if (this->transaction.data.fee == 0ULL) { + this->transaction.data.fee = config.getFee(this->transaction.data.type); + } + + // Use the configuration network version if it's different. + if (this->transaction.data.network == Devnet.version && + config.getNetwork().version != Devnet.version) { + this->transaction.data.network = config.getNetwork().version; + } + + return this->transaction; + } + + protected: + //////////////////////////////////////////////////////////////////////////// + Transaction transaction; +}; + +} // namespace builder +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/defaults/fees.hpp b/src/transactions/defaults/fees.hpp new file mode 100644 index 00000000..af08481d --- /dev/null +++ b/src/transactions/defaults/fees.hpp @@ -0,0 +1,53 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_DEFAULTS_FEES_HPP +#define ARK_TRANSACTIONS_DEFAULTS_FEES_HPP + +#include "common/fee_policy.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Default Transaction Fees +constexpr uint64_t TRANSFER_FEE = 10000000ULL; +constexpr uint64_t SECOND_SIGNATURE_FEE = 500000000ULL; +constexpr uint64_t DELEGATE_REGISTRATION_FEE = 2500000000ULL; +constexpr uint64_t VOTE_FEE = 100000000ULL; +constexpr uint64_t MULTI_SIGNATURE_FEE = 500000000ULL; +constexpr uint64_t IPFS_FEE = 500000000ULL; +constexpr uint64_t MULTI_PAYMENT_FEE = 10000000ULL; +constexpr uint64_t DELEGATE_RESIGNATION_FEE = 2500000000ULL; +constexpr uint64_t HTLC_LOCK_FEE = 10000000ULL; +constexpr uint64_t HTLC_CLAIM_FEE = 0ULL; +constexpr uint64_t HTLC_REFUND_FEE = 0ULL; + +//////////////////////////////////////////////////////////////////////////////// +// The Default Fee Policy +const FeePolicy StaticFeePolicy { + TRANSFER_FEE, // 10000000ULL + SECOND_SIGNATURE_FEE, // 500000000ULL + DELEGATE_REGISTRATION_FEE, // 2500000000ULL + VOTE_FEE, // 100000000ULL + MULTI_SIGNATURE_FEE, // 500000000ULL + IPFS_FEE, // 500000000ULL + MULTI_PAYMENT_FEE, // 10000000ULL + DELEGATE_RESIGNATION_FEE, // 2500000000ULL + HTLC_LOCK_FEE, // 10000000ULL + HTLC_CLAIM_FEE, // 0ULL + HTLC_REFUND_FEE // 0ULL +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/defaults/offsets.hpp b/src/transactions/defaults/offsets.hpp new file mode 100644 index 00000000..0eef8bbb --- /dev/null +++ b/src/transactions/defaults/offsets.hpp @@ -0,0 +1,52 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_DEFAULTS_OFFSETS_HPP +#define ARK_TRANSACTIONS_DEFAULTS_OFFSETS_HPP + +#include + +#include "interfaces/identities.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// v2 Transaction Offsets +constexpr uint8_t HEADER_OFFSET = 0U; +constexpr uint8_t VERSION_OFFSET = 1U; +constexpr uint8_t NETWORK_OFFSET = 2U; +constexpr uint8_t TYPEGROUP_OFFSET = 3U; +constexpr uint8_t TYPE_OFFSET = 7U; +constexpr uint8_t NONCE_OFFSET = 9U; +constexpr uint8_t SENDER_PUBLICKEY_OFFSET = 17U; +constexpr uint8_t FEE_OFFSET = 50U; +constexpr uint8_t VF_LEN_OFFSET = 58U; +constexpr uint8_t VF_OFFSET = 59U; + +//////////////////////////////////////////////////////////////////////////////// +// v1 Transaction Offsets +namespace v1 { +constexpr uint8_t HEADER_OFFSET = 0U; +constexpr uint8_t VERSION_OFFSET = 1U; +constexpr uint8_t NETWORK_OFFSET = 2U; +constexpr uint8_t TYPE_OFFSET = 3U; +constexpr uint8_t TIMESTAMP_OFFSET = 4U; +constexpr uint8_t SENDER_PUBLICKEY_OFFSET = 8U; +constexpr uint8_t FEE_OFFSET = 41U; +constexpr uint8_t VF_LEN_OFFSET = 49U; +constexpr uint8_t VF_OFFSET = 50U; +} // namespace v1 + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/defaults/types.hpp b/src/transactions/defaults/types.hpp new file mode 100644 index 00000000..dc9cd272 --- /dev/null +++ b/src/transactions/defaults/types.hpp @@ -0,0 +1,39 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_DEFAULTS_TYPES_HPP +#define ARK_TRANSACTIONS_DEFAULTS_TYPES_HPP + +#include + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// ARK Transaction Types +enum TransactionTypes : uint8_t { + TRANSFER_TYPE = 0U, + SECOND_SIGNATURE_TYPE = 1U, + DELEGATE_REGISTRATION_TYPE = 2U, + VOTE_TYPE = 3U, + MULTI_SIGNATURE_TYPE = 4U, + IPFS_TYPE = 5U, + MULTI_PAYMENT_TYPE = 6U, + DELEGATE_RESIGNATION_TYPE = 7U, + HTLC_LOCK_TYPE = 8U, + HTLC_CLAIM_TYPE = 9U, + HTLC_REFUND_TYPE = 10U +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/deserializer.cpp b/src/transactions/deserializer.cpp index b5c3355c..8e6260bd 100644 --- a/src/transactions/deserializer.cpp +++ b/src/transactions/deserializer.cpp @@ -1,292 +1,287 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ -#include "transactions/deserializer.h" +#include "transactions/deserializer.hpp" #include #include -#include -#include -#include #include -#include "defaults/transaction_types.hpp" -#include "identities/address.hpp" -#include "identities/privatekey.hpp" -#include "identities/publickey.hpp" -#include "utils/base58.hpp" -#include "utils/crypto_helpers.h" -#include "utils/hex.hpp" +#include "interfaces/constants.h" -namespace Ark { -namespace Crypto { -namespace Transactions { - -Deserializer::Deserializer(const std::string& serialized) - : _serialized(serialized), - _binary(HexToBytes(serialized.c_str())), - _assetOffset(0) {} - -Transaction Deserializer::deserialize() { - Transaction transaction; - - deserializeHeader(transaction); - deserializeType(transaction); - deserializeSignatures(transaction); - - if (transaction.version == 1) { - handleVersionOne(transaction); - }; - - return transaction; -} - -/**/ +#include "transactions/defaults/offsets.hpp" -void Deserializer::deserializeHeader(Transaction& transaction) { - unpack(&transaction.header, &this->_binary[0]); // 1 Byte - unpack(&transaction.version, &this->_binary[1]); // 1 Byte - unpack(&transaction.network, &this->_binary[2]); // 1 Byte - unpack(&transaction.type, &this->_binary[3]); // 1 Byte - unpack(&transaction.timestamp, &this->_binary[4]); // 4 Byte +#include "transactions/transaction_data.hpp" - // 33 Byte - transaction.senderPublicKey = BytesToHex( - this->_binary.begin() + 8, - this->_binary.begin() + 33 + 8); +#include "utils/packing.h" - unpack(&transaction.fee, &this->_binary[41]); // 8 Byte - - uint8_t vendorFieldOffset = (41 + 8 + 1) * 2; - uint8_t vendorFieldLength = 0; - unpack(&vendorFieldLength, &this->_binary[49]); // 1 Byte - if (vendorFieldLength > 0) { - transaction.vendorFieldHex = this->_serialized.substr( - vendorFieldOffset, - vendorFieldLength * 2); - }; - - _assetOffset = vendorFieldOffset + vendorFieldLength * 2; -} - -/**/ - -void Deserializer::deserializeType( - Transaction& transaction) { - switch (transaction.type) { - case TransactionTypes::Transfer: { - deserializeTransfer(transaction); - break; - }; - case TransactionTypes::SecondSignatureRegistration: { - deserializeSecondSignatureRegistration(transaction); - break; - }; - case TransactionTypes::DelegateRegistration: { - deserializeDelegateRegistration(transaction); - break; - }; - case TransactionTypes::Vote: { - deserializeVote(transaction); - break; - }; - case TransactionTypes::MultiSignatureRegistration: { - deserializeMultiSignatureRegistration(transaction); - break; +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Deserialize Common +// +// @param TransactionData *data: The Transaction Data destination. +// @param const std::vector &buffer +// +// --- +// Internals: +// +// Header - 1 Byte: +// - data->header = buffer.at(0); +// +// Transaction Version - 1 Byte: +// - data->version = buffer.at(1); +// +// Network Version - 1 Byte: +// - data->network = buffer.at(2); +// +// TypeGroup - 4 Bytes: +// - data->typeGroup = unpack4LE(&buffer, 3); +// +// Transaction Type - 2 Bytes: +// - data->type = unpack2LE(&buffer, 7); +// +// Nonce - 8 Bytes: +// - data->nonce = unpack8LE(&buffer, 9); +// +// SenderPublicKey - 33 Bytes: +// - std::copy_n(&buffer.at(Z) 33, data->senderPublicKey.begin()); +// +// Fee - 8 bytes +// - data->fee = unpack8LE(&buffer, 50); +// +// VendorField Length - 1 Byte: +// - buffer.at(58); +// +// VendorField - 0 - 255 Bytes: +// - data->vendorField.insert(data->vendorField.begin(), +// &buffer.at(59), +// &buffer.at(59 + data->vendorField.size())); +// +// --- +static void deserializeCommon(TransactionData *data, + const std::vector &buffer) { + data->header = buffer.at(HEADER_OFFSET); // 1 Byte + data->version = buffer.at(VERSION_OFFSET); // 1 Byte + data->network = buffer.at(NETWORK_OFFSET); // 1 Byte + + data->typeGroup = unpack4LE(buffer, TYPEGROUP_OFFSET); // 4 Bytes + + data->type = unpack2LE(buffer, TYPE_OFFSET); // 2 Bytes + + data->nonce = unpack8LE(buffer, NONCE_OFFSET); // 8 Bytes + + std::copy_n(&buffer.at(SENDER_PUBLICKEY_OFFSET), // 21 Bytes + PUBLICKEY_COMPRESSED_LEN, + data->senderPublicKey.begin()); + + data->fee = unpack8LE(buffer, FEE_OFFSET); // 8 Bytes + + if (buffer.at(VF_LEN_OFFSET) != 0U) { + data->vendorField.insert( // 0 <=> 255 Bytes + data->vendorField.begin(), + &buffer.at(VF_OFFSET), + &buffer.at(VF_OFFSET + buffer.at(VF_LEN_OFFSET))); }; - case TransactionTypes::Ipfs: { break; }; - case TransactionTypes::TimelockTransfer: { break; }; - case TransactionTypes::MultiPayment: { break; }; - case TransactionTypes::DelegateResignation: { break; }; - }; -} - -/**/ - -void Deserializer::deserializeTransfer( - Transaction& transaction) { - unpack(&transaction.amount, &this->_binary[_assetOffset / 2]); - unpack(&transaction.expiration, &this->_binary[_assetOffset / 2 + 8]); - - transaction.recipient = Base58::parseHash( - &this->_binary[(_assetOffset / 2) + 13], - this->_binary[(_assetOffset / 2) + 12]); - - _assetOffset += (8 + 4 + 21) * 2; -}; - -void Deserializer::deserializeSecondSignatureRegistration( - Transaction& transaction) { - transaction.asset.signature.publicKey = this->_serialized.substr( - _assetOffset, - 66); - _assetOffset += 66; -}; - -void Deserializer::deserializeDelegateRegistration( - Transaction& transaction) { - uint8_t usernameLength = 0; - unpack(&usernameLength, &this->_binary[_assetOffset / 2]); - usernameLength &= 0xff; - - std::string username = this->_serialized.substr( - (_assetOffset / 2 + 1) * 2, usernameLength * 2); - - std::vector bytes = HexToBytes(username.c_str()); - transaction.asset.delegate.username = std::string( - bytes.begin(), - bytes.end()); - _assetOffset += (usernameLength + 1) * 2; -}; - -void Deserializer::deserializeVote( - Transaction& transaction) { - uint8_t voteLength = 0; - unpack(&voteLength, &this->_binary[_assetOffset / 2]); - voteLength &= 0xff; - - for (uint8_t i = 0; i < voteLength; i++) { - std::string vote = this->_serialized.substr( - _assetOffset + 2 + i * 2 * 32, - 68); - vote = (vote[1] == '1' ? "+" : "-") + vote.substr(2); - transaction.asset.votes.push_back(vote); - }; - - _assetOffset += 2 + voteLength * 34 * 2; } -/**/ - -void Deserializer::deserializeMultiSignatureRegistration( - Transaction& transaction) { - uint8_t min = 0; - unpack(&min, &this->_binary[_assetOffset / 2]); - min &= 0xff; - - uint8_t count = 0; - unpack(&count, &this->_binary[_assetOffset / 2 + 1]); - count &= 0xff; - - uint8_t lifetime = 0; - unpack(&lifetime, &this->_binary[_assetOffset / 2 + 2]); - lifetime &= 0xff; - - transaction.asset.multiSignature.min = min; - transaction.asset.multiSignature.lifetime = lifetime; - - for (uint8_t i = 0; i < count; i++) { - std::string key = this->_serialized.substr( - _assetOffset + 6 + i * 66, - 66); - transaction.asset.multiSignature.keysgroup.push_back(key); - }; - - _assetOffset += 6 + count * 66; +//////////////////////////////////////////////////////////////////////////////// +// Deserialize Common v1 +// +// @param TransactionData *data: The Transaction Data destination. +// @param const std::vector &buffer +// +// --- +// Internals: +// +// Header - 1 Byte: +// - data->header = buffer.at(0); +// +// Transaction Version - 1 Byte: +// - data->version = buffer.at(1); +// +// Network Version - 1 Byte: +// - data->network = buffer.at(2); +// +// Transaction Type - 1 Byte: +// - data->type = buffer.at(3); +// +// Timestamp - 4 Bytes +// - data->timestamp = unpack4LE(buffer, 4); +// +// SenderPublicKey - 33 Bytes: +// - std::copy_n(&buffer.at(8), 33, data->senderPublicKey.begin()); +// +// Fee - 8 bytes +// - data->fee = unpack8LE(buffer, 41); +// +// VendorField Length - 1 Byte: +// - buffer.at(49) +// +// VendorField - 0 - 255 Bytes: +// - data->vendorField.insert(data->vendorField.begin(), +// &buffer.at(50), +// &buffer.at(50 + buffer.at(49))); +// +// --- +static void deserializeCommonV1(TransactionData *data, + const std::vector &buffer) { + data->header = buffer.at(HEADER_OFFSET); // 1 Byte + data->version = buffer.at(VERSION_OFFSET); // 1 Byte + data->network = buffer.at(NETWORK_OFFSET); // 1 Byte + + data->type = buffer.at(v1::TYPE_OFFSET); // 1 Bytes + + data->timestamp = unpack4LE(buffer, v1::TIMESTAMP_OFFSET); // 4 Bytes + + std::copy_n(&buffer.at(v1::SENDER_PUBLICKEY_OFFSET), // 33 Bytes + PUBLICKEY_COMPRESSED_LEN, + data->senderPublicKey.begin()); + + data->fee = unpack8LE(buffer, v1::FEE_OFFSET); // 8 Bytes + + if (buffer.at(v1::VF_LEN_OFFSET) != 0U) { + data->vendorField.insert( // 0 <=> 255 Bytes + data->vendorField.begin(), + &buffer.at(v1::VF_OFFSET), + &buffer.at(v1::VF_OFFSET + buffer.at(v1::VF_LEN_OFFSET))); + }; } -/**/ - -static uint8_t parseSignatureLength(const std::string& hex) { - if (hex.length() > 2) { return 0; }; - unsigned int length; - sscanf(hex.c_str(), "%x", &length); - return static_cast(length + 2); +//////////////////////////////////////////////////////////////////////////////// +static auto deserializeAsset(TransactionData *transaction, + const std::vector &buffer, + const size_t &offset) -> size_t { + switch (transaction->type) { + // Transfer + case TRANSFER_TYPE: + return Transfer::Deserialize( + &transaction->asset.transfer, + &buffer.at(offset)); + + // Second Signature Registration + case SECOND_SIGNATURE_TYPE: + return SecondSignature::Deserialize( + &transaction->asset.secondSignature, + &buffer.at(offset)); + + // Delegate Registration + case DELEGATE_REGISTRATION_TYPE: + return DelegateRegistration::Deserialize( + &transaction->asset.delegateRegistration, + &buffer.at(offset)); + + // Vote + case VOTE_TYPE: + return Vote::Deserialize( + &transaction->asset.vote, + &buffer.at(offset)); + + // // MultiSignature Registration + // case MULTI_SIGNATURE_TYPE: // TODO + + // Ipfs + case IPFS_TYPE: + return Ipfs::Deserialize( + &transaction->asset.ipfs, + &buffer.at(offset)); + + // MultiPayment + case MULTI_PAYMENT_TYPE: + return MultiPayment::Deserialize( + &transaction->asset.multiPayment, + &buffer.at(offset)); + + // Delegate Resignation + // No Asset Needed. Return Default of '0'. + // case DELEGATE_RESIGNATION_TYPE: + + // Htlc Lock + case HTLC_LOCK_TYPE: + return HtlcLock::Deserialize( + &transaction->asset.htlcLock, + &buffer.at(offset)); + + // Htlc Claim + case HTLC_CLAIM_TYPE: + return HtlcClaim::Deserialize( + &transaction->asset.htlcClaim, + &buffer.at(offset)); + + // Htlc Claim + case HTLC_REFUND_TYPE: + return HtlcRefund::Deserialize( + &transaction->asset.htlcRefund, + &buffer.at(offset)); + + default: return 0UL; + } } -/**/ - -void Deserializer::deserializeSignatures( - Transaction& transaction) { - std::string signature = this->_serialized.substr(_assetOffset); - - if (!signature.empty()) { - size_t multiSignatureOffset = 0; - - size_t signatureLength = parseSignatureLength(signature.substr(2, 2)); - transaction.signature = this->_serialized.substr( - _assetOffset, - signatureLength * 2); - multiSignatureOffset += signatureLength * 2; - transaction.secondSignature = this->_serialized.substr( - (_assetOffset + signatureLength * 2)); - - if (!transaction.secondSignature.empty()) { - if (transaction.secondSignature.substr(0, 2) == "ff") { - transaction.secondSignature = ""; - } else { - size_t secondSignatureLength = parseSignatureLength( - transaction.secondSignature.substr(2, 2)); - transaction.secondSignature = transaction.secondSignature.substr( - 0, - secondSignatureLength * 2); - multiSignatureOffset += secondSignatureLength * 2; - }; - }; - - std::string signatures = this->_serialized.substr( - _assetOffset + multiSignatureOffset); - if (!signatures.empty() && signatures.substr(0, 2) == "ff") { - signatures = signatures.substr(2); - - while (!signatures.empty()) { - size_t multiSignatureLength = parseSignatureLength( - signatures.substr(2, 2)); - if (multiSignatureLength > 0) { - transaction.signatures.push_back( - signatures.substr(0, - multiSignatureLength * 2)); - }; - - signatures = signatures.substr(multiSignatureLength * 2); - }; - }; - }; +//////////////////////////////////////////////////////////////////////////////// +static void deserializeSignatures(TransactionData *transaction, + const uint8_t *buffer, + const size_t &size) { + size_t signatureLength = buffer[1] == 0U ? 0U : 2U + buffer[1]; + if (signatureLength >= SIGNATURE_ECDSA_MIN && + signatureLength <= SIGNATURE_ECDSA_MAX) { + transaction->signature.resize(signatureLength); + std::copy_n(buffer, + transaction->signature.size(), + transaction->signature.begin()); + } + + size_t secondSignatureLength = size - signatureLength; + if (secondSignatureLength >= SIGNATURE_ECDSA_MIN && + secondSignatureLength <= SIGNATURE_ECDSA_MAX) { + transaction->secondSignature.resize(secondSignatureLength); + std::copy_n(buffer + signatureLength, + transaction->secondSignature.size(), + transaction->secondSignature.begin()); + } } -/**/ - -void Deserializer::handleVersionOne( - Transaction& transaction) { - transaction.signSignature = transaction.secondSignature; - - if (transaction.type == TransactionTypes::Vote) { - const auto address = identities::Address::fromPublicKey( - HexToBytesArray( - transaction.senderPublicKey.c_str()) - .data(), - transaction.network); - - transaction.recipient = address.toString(); - }; - - if (transaction.type == TransactionTypes::MultiSignatureRegistration) { - std::for_each( - transaction.asset.multiSignature.keysgroup.begin(), - transaction.asset.multiSignature.keysgroup.end(), - [](std::string& key) { key.insert(0, "+"); } - ); - }; - - if (!transaction.vendorFieldHex.empty()) { - const auto bytes = HexToBytes(transaction.vendorFieldHex.c_str()); - transaction.vendorField = std::string(bytes.begin(), bytes.end()); - }; - - if (transaction.id.empty()) { - transaction.id = transaction.getId(); - }; - - if (transaction.type == TransactionTypes::SecondSignatureRegistration - || transaction.type == TransactionTypes::MultiSignatureRegistration) { - const auto address = identities::Address::fromPublicKey( - HexToBytesArray( - transaction.senderPublicKey.c_str()) - .data(), - transaction.network); - - transaction.recipient = address.toString(); - }; +//////////////////////////////////////////////////////////////////////////////// +// Deserialize Transaction Data +// +// @param TransactionData *data +// @param const std::vector &buffer +// +// --- +auto Deserializer::deserialize(TransactionData *data, + const std::vector &buffer) -> bool { + size_t assetOffset = 0UL; + + // Use v2 or v1, otherwise return with no changes to the Tx Data. + // v2 + if (buffer.at(VERSION_OFFSET) == TRANSACTION_VERSION_TYPE_2) { + deserializeCommon(data, buffer); + assetOffset = VF_OFFSET + data->vendorField.size(); + } + // v1 + else if(buffer.at(VERSION_OFFSET) == TRANSACTION_VERSION_TYPE_1) { + deserializeCommonV1(data, buffer); + assetOffset = v1::VF_OFFSET + data->vendorField.size(); + } + else { return false; } + + size_t assetSize = deserializeAsset(data, buffer, assetOffset); + + deserializeSignatures(data, + &buffer.at(assetOffset + assetSize), + buffer.size() - assetOffset - assetSize); + + return true; } -} // namespace Transactions +} // namespace transactions } // namespace Crypto } // namespace Ark diff --git a/src/transactions/deserializer.hpp b/src/transactions/deserializer.hpp new file mode 100644 index 00000000..0ee4c484 --- /dev/null +++ b/src/transactions/deserializer.hpp @@ -0,0 +1,35 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_DESERIALIZER_HPP +#define ARK_TRANSACTIONS_DESERIALIZER_HPP + +#include +#include + +#include "transactions/transaction_data.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +class Deserializer { + public: + //////////////////////////////////////////////////////////////////////////// + // Deserialize Transaction Data + static bool deserialize(TransactionData *data, + const std::vector &buffer); +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/mapping/json.cpp b/src/transactions/mapping/json.cpp new file mode 100644 index 00000000..aa770d64 --- /dev/null +++ b/src/transactions/mapping/json.cpp @@ -0,0 +1,395 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "transactions/mapping/json.hpp" + +#include +#include +#include +#include + +#include "interfaces/constants.h" + +#include "transactions/defaults/types.hpp" + +#include "transactions/mapping/labels.hpp" + +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Calculate the ArduinoJson Capacity of the Common Transaction Data. +// +// Json Sizes are calculated using 'https://arduinojson.org/v6/assistant/' +// +// - Version +// - Network +// - TypeGroup // v2 +// - Type +// - Nonce // v2 +// - Timestamp // v1 +// - Sender PublicKey +// - Fee +// - VendorField +// +// --- +static auto getCommonJsonCapacity( + const std::map &map) -> size_t { + auto commonCapacity = + JSON_OBJECT_SIZE(1) + + KEY_VERSION_SIZE + UINT8_MAX_CHARS + + JSON_OBJECT_SIZE(1) + + KEY_NETWORK_SIZE + UINT8_MAX_CHARS + + JSON_OBJECT_SIZE(1) + + KEY_SENDER_PUBLICKEY_SIZE + PUBLICKEY_COMPRESSED_LEN + + JSON_OBJECT_SIZE(1) + + KEY_FEE_SIZE + UINT64_MAX_CHARS; + + const uint16_t transactionVersion = static_cast( + strtoul(map.at(KEY_VERSION_LABEL).c_str(), nullptr, BASE_10)); + + // v2 + if (transactionVersion == TRANSACTION_VERSION_TYPE_2) { + commonCapacity += JSON_OBJECT_SIZE(1) + + KEY_TYPEGROUP_SIZE + UINT32_MAX_CHARS + + JSON_OBJECT_SIZE(1) + + KEY_TYPE_SIZE + UINT16_MAX_CHARS + + JSON_OBJECT_SIZE(1) + + KEY_NONCE_SIZE + UINT64_MAX_CHARS; + } + // v1 + else if (transactionVersion == TRANSACTION_VERSION_TYPE_1) { + commonCapacity += JSON_OBJECT_SIZE(1) + + KEY_TYPE_SIZE + UINT8_MAX_CHARS + + JSON_OBJECT_SIZE(1) + + KEY_TIMESTAMP_SIZE + UINT32_MAX_CHARS; + } + + // Vendorfield + if (map.find(KEY_VENDORFIELD_LABEL) != map.end()) { + commonCapacity += JSON_OBJECT_SIZE(1) + + KEY_VENDORFIELD_SIZE + + map.at(KEY_VENDORFIELD_LABEL).size(); + } + + return commonCapacity; +} + +//////////////////////////////////////////////////////////////////////////////// +// Calculate the ArduinoJson Capacity of the Transaction Asset Data. +// +// Json Sizes are calculated using 'https://arduinojson.org/v6/assistant/' +// +// - n_payments only needed for MultiPayment Capacity calculation. +// +// - Transfer +// - SecondSignature +// - Delegate Registration +// - Vote +// - MultiSignature Registration // TODO +// - Ipfs +// - MultiPayment +// - Delegate Resignation // No Asset Needed +// - Htlc Lock +// - Htlc Claim +// - Htlc Refund +// +// --- +static auto getAssetJsonCapacity(uint16_t type, const size_t &n_payments = 0UL) + -> size_t { + switch(type) { + // Transfer + case TRANSFER_TYPE: + return Transfer::getJsonCapacity(); + + // Second Signature Registration + case SECOND_SIGNATURE_TYPE: + return SecondSignature::getJsonCapacity(); + + // Delegate Registration + case DELEGATE_REGISTRATION_TYPE: + return DelegateRegistration::getJsonCapacity(); + + // Vote + case VOTE_TYPE: + return Vote::getJsonCapacity(); + + // // MultiSignature Registration + // case MULTI_SIGNATURE_TYPE: // TODO + + // Ipfs + case IPFS_TYPE: + return Ipfs::getJsonCapacity(); + + // MultiPayment + case MULTI_PAYMENT_TYPE: + return MultiPayment::getJsonCapacity(n_payments); + + // Delegate Resignation + // No Asset Needed. Return Default of '0'. + // case DELEGATE_RESIGNATION_TYPE: + + // Htlc Lock + case HTLC_LOCK_TYPE: + return HtlcLock::getJsonCapacity(); + + // Htlc Claim + case HTLC_CLAIM_TYPE: + return HtlcClaim::getJsonCapacity(); + + // Htlc Refund + case HTLC_REFUND_TYPE: + return HtlcRefund::getJsonCapacity(); + + default: return 0UL; + } +} + +//////////////////////////////////////////////////////////////////////////////// +// Calculate the ArduinoJson Capacity of the Extra Transaction Data. +// +// Json Sizes are calculated using 'https://arduinojson.org/v6/assistant/' +// +// - Transaction Id +// - Signature (if present) +// - Second Signature (if present) +// +// --- +static auto getExtrasJsonCapacity( + const std::map &map) -> size_t { + // start with the txId json size + size_t commonExtrasCapacity = KEY_ID_SIZE + HASH_32_MAX_CHARS; + + // Signature + if (map.find(KEY_SIGNATURE_LABEL) != map.end()) { + commonExtrasCapacity += + JSON_OBJECT_SIZE(1) + + KEY_SIGNATURE_SIZE + SIGNATURE_ECDSA_MAX_CHARS; + } + + // Second Signature + if (map.find(KEY_SECOND_SIGNATURE_LABEL) != map.end()) { + commonExtrasCapacity += + JSON_OBJECT_SIZE(1) + + KEY_SECOND_SIGNATURE_SIZE + SIGNATURE_ECDSA_MAX_CHARS; + } + + return commonExtrasCapacity; +} + +//////////////////////////////////////////////////////////////////////////////// +// Add Common Transaction data to a 'DynamicJsonDocument' +// +// - Version +// - Network +// - TypeGroup // v2 +// - Type +// - Nonce // v2 +// - Timestamp // v1 +// - Sender PublicKey +// - Fee +// - VendorField +// +// --- +static void mapCommonJson(DynamicJsonDocument &jsonDoc, + const std::map &map) { + const uint16_t transactionVersion = + static_cast(strtoul(map.at(KEY_VERSION_LABEL).c_str(), + nullptr, + BASE_10)); + + // Version + jsonDoc[KEY_VERSION_LABEL] = transactionVersion; + + // Network + jsonDoc[KEY_NETWORK_LABEL] = strtoul( + map.at(KEY_NETWORK_LABEL).c_str(), nullptr, BASE_10); + + // v2 + if (transactionVersion == TRANSACTION_VERSION_TYPE_2) { + // TypeGroup + jsonDoc[KEY_TYPEGROUP_LABEL] = + strtoul(map.at(KEY_TYPEGROUP_LABEL).c_str(), + nullptr, + BASE_10); + } + + // Type + jsonDoc[KEY_TYPE_LABEL] = strtoul( + map.at(KEY_TYPE_LABEL).c_str(), nullptr, BASE_10); + + // v2 + if (transactionVersion == TRANSACTION_VERSION_TYPE_2) { + // Nonce + jsonDoc[KEY_NONCE_LABEL] = map.at(KEY_NONCE_LABEL); + } + // v1 + else if (transactionVersion == TRANSACTION_VERSION_TYPE_1) { + // Timestamp + jsonDoc[KEY_TIMESTAMP_LABEL] = map.at(KEY_TIMESTAMP_LABEL); + } + + // Sender PublicKey + jsonDoc[KEY_SENDER_PUBLICKEY_LABEL] = map.at(KEY_SENDER_PUBLICKEY_LABEL); + + // Fee + jsonDoc[KEY_FEE_LABEL] = map.at(KEY_FEE_LABEL); + + // VendorField + if (map.find(KEY_VENDORFIELD_LABEL) != map.end()) { + jsonDoc[KEY_VENDORFIELD_LABEL] = map.at(KEY_VENDORFIELD_LABEL); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// Add Transaction Asset data to a 'DynamicJsonDocument' +// +// - Transfer +// - SecondSignature +// - Delegate Registration +// - Vote +// - MultiSignature Registration // TODO +// - Ipfs +// - MultiPayment +// - Delegate Resignation // No Asset Needed +// - Htlc Lock +// - Htlc Claim +// - Htlc Refund +// +// --- +static void mapAssetJson(uint16_t type, + DynamicJsonDocument &jsonDoc, + const std::map &map) { + switch (type) { + // Transfer + case TRANSFER_TYPE: + Transfer::addToJson(jsonDoc, map); + break; + + // SecondSignature + case SECOND_SIGNATURE_TYPE: + SecondSignature::addToJson(jsonDoc, map); + break; + + // Delegate Registration + case DELEGATE_REGISTRATION_TYPE: + DelegateRegistration::addToJson(jsonDoc, map); + break; + + // Vote + case VOTE_TYPE: + Vote::addToJson(jsonDoc, map); + break; + + // // MultiSignature Registration + // case MULTI_SIGNATURE_TYPE: // TODO + + // Ipfs + case IPFS_TYPE: + Ipfs::addToJson(jsonDoc, map); + break; + + // MultiPayment + case MULTI_PAYMENT_TYPE: + MultiPayment::addToJson(jsonDoc, map); + break; + + // Delegate Resignation + // No Asset Needed + // case DELEGATE_RESIGNATION_TYPE + + // Htlc Lock + case HTLC_LOCK_TYPE: + HtlcLock::addToJson(jsonDoc, map); + break; + + // Htlc Claim + case HTLC_CLAIM_TYPE: + HtlcClaim::addToJson(jsonDoc, map); + break; + + // Htlc Refund + case HTLC_REFUND_TYPE: + HtlcRefund::addToJson(jsonDoc, map); + break; + + default: break; + } +} + +//////////////////////////////////////////////////////////////////////////////// +// Add a Transcations Extra data to a 'DynamicJsonDocument' +// +// - Transaction Id +// - Signature (if present) +// - Second Signature (if present) +// +// --- +static void mapExtrasJson(DynamicJsonDocument &jsonDoc, + const std::map &map) { + // Transaction Id + jsonDoc[KEY_ID_LABEL] = map.at(KEY_ID_LABEL); + + // Signature + if (map.find(KEY_SIGNATURE_LABEL) != map.end()) { + jsonDoc[KEY_SIGNATURE_LABEL] = map.at(KEY_SIGNATURE_LABEL); + } + + // Second Signature + if (map.find(KEY_SECOND_SIGNATURE_LABEL) != map.end()) { + jsonDoc[KEY_SECOND_SIGNATURE_LABEL] = map.at(KEY_SECOND_SIGNATURE_LABEL); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// Extract a Json String from a given Transaction Map. +// +// +// - Add the Common Transaction data. +// - Add the Transaction Asset data. +// - Add the Transactions Extra data. (id, signatures) +// +// --- +auto Json::fromTransactionMap(const std::map &map) + -> std::string { + // Get the Type from the Mapped Transaction. + const uint16_t transactionType = static_cast( + strtoul(map.at(KEY_TYPE_LABEL).c_str(), nullptr, BASE_10)); + + // Non-Zero only if Map contains MultiPayment data + const auto multiPaymentCount = map.find(KEY_N_PAYMENTS_LABEL) != map.end() + ? strtol(map.at(KEY_N_PAYMENTS_LABEL).c_str(), nullptr, BASE_10) + : 0UL; + + // Calculate the Capacity needed for Json Serialization. + size_t jsonCapacity = + getCommonJsonCapacity(map) + + getAssetJsonCapacity(transactionType, multiPaymentCount) + + getExtrasJsonCapacity(map); + + DynamicJsonDocument jsonDoc(jsonCapacity); + + mapCommonJson(jsonDoc, map); + mapAssetJson(transactionType, jsonDoc, map); + mapExtrasJson(jsonDoc, map); + + std::string jsonStr; + jsonStr.reserve(jsonCapacity); + + serializeJson(jsonDoc, jsonStr); + + return jsonStr; +} + +} // namespace transactions +} // namespace Crypto +} // namespace Ark diff --git a/src/transactions/mapping/json.hpp b/src/transactions/mapping/json.hpp new file mode 100644 index 00000000..a8b6cc70 --- /dev/null +++ b/src/transactions/mapping/json.hpp @@ -0,0 +1,33 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_MAPPING_JSON_HPP +#define ARK_TRANSACTIONS_MAPPING_JSON_HPP + +#include +#include + +#include "transactions/transaction_data.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Transaction Mapping +// Extract a Json-serialized string of a given Transaction map. +struct Json { // NOLINT + static std::string fromTransactionMap(const std::map &map); +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif // #define ARK_TRANSACTIONS_MAPPING_JSON_HPP diff --git a/src/transactions/mapping/labels.hpp b/src/transactions/mapping/labels.hpp new file mode 100644 index 00000000..28913a95 --- /dev/null +++ b/src/transactions/mapping/labels.hpp @@ -0,0 +1,61 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_MAPPING_LABELS_HPP +#define ARK_TRANSACTIONS_MAPPING_LABELS_HPP + +#include + +namespace Ark { +namespace Crypto { +namespace transactions { // NOLINT + +//////////////////////////////////////////////////////////////////////////////// +// Mapping Label Constants +constexpr auto KEY_VERSION_LABEL = "version"; +const auto KEY_VERSION_SIZE = strlen(KEY_VERSION_LABEL); + +constexpr auto KEY_NETWORK_LABEL = "network"; +const auto KEY_NETWORK_SIZE = strlen(KEY_NETWORK_LABEL); + +constexpr auto KEY_TYPEGROUP_LABEL = "typeGroup"; +const auto KEY_TYPEGROUP_SIZE = strlen(KEY_TYPEGROUP_LABEL); + +constexpr auto KEY_TYPE_LABEL = "type"; +const auto KEY_TYPE_SIZE = strlen(KEY_TYPE_LABEL); + +constexpr auto KEY_NONCE_LABEL = "nonce"; +const auto KEY_NONCE_SIZE = strlen(KEY_NONCE_LABEL); + +constexpr auto KEY_TIMESTAMP_LABEL = "timestamp"; +const auto KEY_TIMESTAMP_SIZE = strlen(KEY_TIMESTAMP_LABEL); + +constexpr auto KEY_SENDER_PUBLICKEY_LABEL = "senderPublicKey"; +const auto KEY_SENDER_PUBLICKEY_SIZE = strlen(KEY_SENDER_PUBLICKEY_LABEL); + +constexpr auto KEY_FEE_LABEL = "fee"; +const auto KEY_FEE_SIZE = strlen(KEY_FEE_LABEL); + +constexpr auto KEY_VENDORFIELD_LABEL = "vendorField"; +const auto KEY_VENDORFIELD_SIZE = strlen(KEY_VENDORFIELD_LABEL); + +constexpr auto KEY_SIGNATURE_LABEL = "signature"; +const auto KEY_SIGNATURE_SIZE = strlen(KEY_SIGNATURE_LABEL); + +constexpr auto KEY_SECOND_SIGNATURE_LABEL = "secondSignature"; +const auto KEY_SECOND_SIGNATURE_SIZE = strlen(KEY_SECOND_SIGNATURE_LABEL); + +constexpr auto KEY_ID_LABEL = "id"; +const auto KEY_ID_SIZE = strlen(KEY_ID_LABEL); + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif // #define ARK_TRANSACTIONS_MAPPING_LABELS_HPP diff --git a/src/transactions/mapping/mapping.cpp b/src/transactions/mapping/mapping.cpp new file mode 100644 index 00000000..4e96892e --- /dev/null +++ b/src/transactions/mapping/mapping.cpp @@ -0,0 +1,200 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "transactions/mapping/mapping.hpp" + +#include +#include +#include +#include + +#include "interfaces/constants.h" + +#include "transactions/defaults/types.hpp" + +#include "transactions/transaction_data.hpp" + +#include "transactions/mapping/labels.hpp" + +#include "utils/hex.hpp" +#include "utils/str.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Start with the Common Map data +// +// - Version +// - Network +// - TypeGroup // v2 +// - Type +// - Nonce // v2 +// - Timestamp // v1 +// - Sender PublicKey +// - Fee +// - VendorField +// +// --- +static void mapCommon(std::map &map, + const TransactionData &data) { + // Version + map.emplace(KEY_VERSION_LABEL, UintToString(data.version)); + + // Network + map.emplace(KEY_NETWORK_LABEL, UintToString(data.network)); + + // v2 + if (data.version == TRANSACTION_VERSION_TYPE_2) { + // TypeGroup + map.emplace(KEY_TYPEGROUP_LABEL, + UintToString(data.typeGroup)); + } + + // Type + map.emplace(KEY_TYPE_LABEL, UintToString(data.type)); + + // v2 + if (data.version == TRANSACTION_VERSION_TYPE_2) { + // Nonce + map.emplace(KEY_NONCE_LABEL, UintToString(data.nonce)); + } + // v1 + else if (data.version == TRANSACTION_VERSION_TYPE_1) { + // Timestamp + map.emplace(KEY_TIMESTAMP_LABEL, + UintToString(data.timestamp)); + } + + // Sender PublicKey + map.emplace(KEY_SENDER_PUBLICKEY_LABEL, + BytesToHex(data.senderPublicKey)); + + // Fee + map.emplace(KEY_FEE_LABEL, UintToString(data.fee)); + + // VendorField + if (!data.vendorField.empty()) { + const auto vf = reinterpret_cast( + data.vendorField.data()); + + map.emplace(KEY_VENDORFIELD_LABEL, + std::string(vf, vf + data.vendorField.size())); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// Add Asset data +// +// - Transfer +// - SecondSignature +// - Delegate Registration +// - Vote +// - MultiSignature Registration // TODO +// - Ipfs +// - MultiPayment +// - Delegate Resignation // No Asset Needed +// - Htlc Lock +// - Htlc Claim +// - Htlc Refund +// +// --- +static void mapAsset(std::map &map, + const TransactionData &data) { + switch (data.type) { + // Tranfer + case TRANSFER_TYPE: + Transfer::addToMap(data.asset.transfer, map); + + // Second Signature Registration + case SECOND_SIGNATURE_TYPE: + SecondSignature::addToMap(data.asset.secondSignature, map); + + // Delegate Registration + case DELEGATE_REGISTRATION_TYPE: + DelegateRegistration::addToMap(data.asset.delegateRegistration, map); + + // Vote + case VOTE_TYPE: + Vote::addToMap(data.asset.vote, map); + + // MultiSignature Registration + // case MULTI_SIGNATURE_TYPE: // TODO + + // Ipfs + case IPFS_TYPE: + Ipfs::addToMap(data.asset.ipfs, map); + + // MultiPayment + case MULTI_PAYMENT_TYPE: + MultiPayment::addToMap(data.asset.multiPayment, map); + + // Delegate Resignation + // No Asset Needed + // case DELEGATE_RESIGNATION_TYPE: + + // Htlc Lock + case HTLC_LOCK_TYPE: + HtlcLock::addToMap(data.asset.htlcLock, map); + + // Htlc Claim + case HTLC_CLAIM_TYPE: + HtlcClaim::addToMap(data.asset.htlcClaim, map); + + // Htlc Refund + case HTLC_REFUND_TYPE: + HtlcRefund::addToMap(data.asset.htlcRefund, map); + + default: break; + } +} + +//////////////////////////////////////////////////////////////////////////////// +// Add Common Extra Map data +// +// - TransactionId +// - Signature (if present) +// - Second Signature (if present) +// +// --- +static void mapExtras(std::map &map, + const TransactionData &data) { + // Transaction Id + map.emplace(KEY_ID_LABEL, BytesToHex(data.id)); + + // Signature + if (!data.signature.empty() && data.signature.at(1) != 0U) { + map.emplace(KEY_SIGNATURE_LABEL, + BytesToHex(data.signature)); + } + + // Second Signature + if (!data.secondSignature.empty() && data.secondSignature.at(1) != 0U) { + map.emplace(KEY_SECOND_SIGNATURE_LABEL, + BytesToHex(data.secondSignature)); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// Extract a standard string-Map from a Transaction's data. +auto Mapping::fromTransactionData(const TransactionData &data) + -> std::map { + std::map map; + + mapCommon(map, data); + mapAsset(map, data); + mapExtras(map, data); + + return map; +} + +} // namespace transactions +} // namespace Crypto +} // namespace Ark diff --git a/src/transactions/mapping/mapping.hpp b/src/transactions/mapping/mapping.hpp new file mode 100644 index 00000000..2419c271 --- /dev/null +++ b/src/transactions/mapping/mapping.hpp @@ -0,0 +1,30 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_MAPPING_HPP +#define ARK_TRANSACTIONS_MAPPING_HPP + +#include +#include + +#include "transactions/transaction_data.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +struct Mapping { // NOLINT + static std::map fromTransactionData(const TransactionData &data); +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif // #define ARK_TRANSACTIONS_MAPPING_HPP diff --git a/src/transactions/serializer.cpp b/src/transactions/serializer.cpp index 40fec6d8..d4b2f6f4 100644 --- a/src/transactions/serializer.cpp +++ b/src/transactions/serializer.cpp @@ -1,232 +1,310 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ -#include "transactions/serializer.h" +#include "transactions/serializer.hpp" #include -#include -#include +#include #include -#include "defaults/transaction_types.hpp" -#include "identities/address.hpp" -#include "common/configuration.hpp" -#include "utils/base58.hpp" -#include "utils/crypto_helpers.h" -#include "utils/hex.hpp" +#include "interfaces/constants.h" -namespace Ark { -namespace Crypto { -namespace Transactions { - -// TODO: remove class ? -Serializer::Serializer(Transaction transaction) - : _transaction(std::move(transaction)) {} - -std::string Serializer::serialize() const { - std::vector bytes; - bytes.push_back(0xff); - bytes.push_back( - _transaction.version > 0 - ? _transaction.version - : 0x01); - bytes.push_back( - _transaction.network > 0 - ? _transaction.network - : Configuration().getNetwork().version); - bytes.push_back(_transaction.type); - - pack(bytes, _transaction.timestamp); - - std::vector senderPublicKeyBytes = HexToBytes( - _transaction.senderPublicKey.c_str()); - bytes.insert( - bytes.end(), - senderPublicKeyBytes.begin(), - senderPublicKeyBytes.end()); - - pack(bytes, _transaction.fee); - - serializeVendorField(bytes); - serializeType(bytes); - serializeSignatures(bytes); - - return BytesToHex(bytes); -} - -/**/ - -void Serializer::serializeVendorField( - std::vector& bytes) const { - if (_transaction.vendorField.length() > 0) { - auto vendorFieldLength = static_cast( - _transaction.vendorField.length()); - bytes.push_back(vendorFieldLength); - bytes.insert( - bytes.end(), - std::begin(_transaction.vendorField), - std::end(_transaction.vendorField)); - } else if (_transaction.vendorFieldHex.length() > 0) { - auto vendorFieldHexLength = static_cast( - _transaction.vendorFieldHex.length() / 2); - bytes.push_back(vendorFieldHexLength); - bytes.insert( - bytes.end(), - std::begin(_transaction.vendorFieldHex), - std::end(_transaction.vendorFieldHex)); - } else { - bytes.push_back(0x00); - }; -} - -/**/ - -void Serializer::serializeType( - std::vector& bytes) const { - switch (_transaction.type) { - case TransactionTypes::Transfer: { - serializeTransfer(bytes); - break; - }; - case TransactionTypes::SecondSignatureRegistration: { - serializeSecondSignatureRegistration(bytes); - break; - }; - case TransactionTypes::DelegateRegistration: { - serializeDelegateRegistration(bytes); - break; - }; - case TransactionTypes::Vote: { - serializeVote(bytes); - break; - }; - case TransactionTypes::MultiSignatureRegistration: { - serializeMultiSignatureRegistration(bytes); - break; - }; - case TransactionTypes::Ipfs: { break; }; - case TransactionTypes::TimelockTransfer: { break; }; - case TransactionTypes::MultiPayment: { break; }; - case TransactionTypes::DelegateResignation: { break; }; - }; -} - -/**/ +#include "crypto/hash.hpp" -void Serializer::serializeTransfer( - std::vector& bytes) const { - pack(bytes, _transaction.amount); - pack(bytes, _transaction.expiration); +#include "transactions/defaults/offsets.hpp" - const auto hashPair = Base58::getHashPair(_transaction.recipient.c_str()); - bytes.push_back(hashPair.version); - bytes.insert(bytes.end(), hashPair.pubkeyHash.begin(), hashPair.pubkeyHash.end()); -} +#include "transactions/transaction_data.hpp" -/**/ +#include "utils/packing.h" -void Serializer::serializeSecondSignatureRegistration( - std::vector& bytes) const { - std::vector publicKeyBytes = HexToBytes( - _transaction.asset.signature.publicKey.c_str()); - bytes.insert( - bytes.end(), - publicKeyBytes.begin(), - publicKeyBytes.end()); +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Serialize Common +// +// @param const TransactionData &data +// @param std::vector &buffer: The serialized transactions buffer. +// +// --- +// Internals: +// +// Header - 1 Byte: +// - buffer.at(0) = transaction.header; +// +// Transaction Version - 1 Byte: +// - buffer.at(1) = transaction.version; +// +// Network Version - 1 Byte: +// - buffer.at(2) = transaction.network; +// +// TypeGroup - 4 Bytes: +// - pack4LE(&buffer.at(TYPEGROUP_OFFSET), &transaction.typeGroup); +// +// Transaction Type - 2 Bytes: +// - pack2LE(&buffer.at(TYPE_OFFSET), &transaction.type); +// +// Nonce - 8 Bytes: +// - pack8LE(&buffer[NONCE_OFFSET], &transaction.nonce); +// +// SenderPublicKey - 33 Bytes: +// - buffer.insert(buffer.begin() + 17, +// transaction.senderPublicKey.begin(), +// transaction.senderPublicKey.end()); +// +// Fee - 8 bytes +// - pack8LE(&buffer[FEE_OFFSET], &transaction.fee); +// +// VendorField Length - 1 Byte: +// - buffer.at(58) = transaction.vendorField.size(); +// +// VendorField - 0 <=> 255 Bytes: +// - buffer.insert(buffer.begin() + 59, +// transaction.vendorField.begin(), +// transaction.vendorField.end()); +// +// --- +static void serializeCommon(const TransactionData &transaction, + std::vector &buffer) { + buffer.at(HEADER_OFFSET) = transaction.header; // 1 Byte + buffer.at(VERSION_OFFSET) = transaction.version; // 1 Byte + buffer.at(NETWORK_OFFSET) = transaction.network; // 1 Byte + + pack4LE(&buffer.at(TYPEGROUP_OFFSET), &transaction.typeGroup); // 4 Bytes + pack2LE(&buffer.at(TYPE_OFFSET), &transaction.type); // 2 Bytes + pack8LE(&buffer[NONCE_OFFSET], &transaction.nonce); // 8 Bytes + + buffer.insert(buffer.begin() + SENDER_PUBLICKEY_OFFSET, // 21 Bytes + transaction.senderPublicKey.begin(), + transaction.senderPublicKey.end()); + + pack8LE(&buffer[FEE_OFFSET], &transaction.fee); // 8 Bytes + + buffer.at(VF_LEN_OFFSET) = // 1 Byte + static_cast(transaction.vendorField.size()); + + if (!transaction.vendorField.empty()) { + buffer.insert(buffer.begin() + VF_OFFSET, // 0 <=> 255 Bytes + transaction.vendorField.begin(), + transaction.vendorField.end()); + } } -/**/ - -void Serializer::serializeDelegateRegistration( - std::vector& bytes) const { - const auto username = _transaction.asset.delegate.username; - bytes.push_back(static_cast(username.size())); - bytes.insert( - bytes.end(), - username.begin(), - username.end()); +//////////////////////////////////////////////////////////////////////////////// +// Serialize Common V1 +// +// @param const TransactionData &data +// @param std::vector &buffer: The serialized transactions buffer. +// +// --- +// Internals: +// +// Header - 1 Byte: +// - buffer.at(0) = transaction.header; +// +// Transaction Version - 1 Byte: +// - buffer.at(1) = transaction.version; +// +// Network Version - 1 Byte: +// - buffer.at(2) = transaction.network; +// +// Transaction Type - 1 Byte: +// - buffer.at(3) = transaction.type; +// +// Timestamp - 4 Bytes +// - pack4LE(&buffer.at(v1::TIMESTAMP_OFFSET), &transaction.timestamp); +// +// SenderPublicKey - 33 Bytes: +// - buffer.insert(buffer.begin() + 8, +// transaction.senderPublicKey.begin(), +// transaction.senderPublicKey.end()); +// +// Fee - 8 bytes +// - pack8LE(&buffer.at(v1::FEE_OFFSET), &transaction.fee); +// +// VendorField Length - 1 Byte: +// - buffer.at(49) = transaction.vendorField.size(); +// +// VendorField - 0 <=> 255 Bytes: +// - buffer.insert(buffer.begin() + 50, +// transaction.vendorField.begin(), +// transaction.vendorField.end()); +// +// --- +static void serializeCommonV1(const TransactionData &transaction, + std::vector &buffer) { + buffer.at(v1::HEADER_OFFSET) = transaction.header; // 1 Byte + buffer.at(v1::VERSION_OFFSET) = transaction.version; // 1 Byte + buffer.at(v1::NETWORK_OFFSET) = transaction.network; // 1 Byte + + buffer.at(v1::TYPE_OFFSET) = // 1 Byte + static_cast(transaction.type); + + pack4LE(&buffer.at(v1::TIMESTAMP_OFFSET), // 4 Bytes + &transaction.timestamp); + + buffer.insert(buffer.begin() + v1::SENDER_PUBLICKEY_OFFSET, // 21 Bytes + transaction.senderPublicKey.begin(), + transaction.senderPublicKey.end()); + + pack8LE(&buffer.at(v1::FEE_OFFSET), &transaction.fee); // 8 Bytes + + buffer.at(v1::VF_LEN_OFFSET) = // 1 Byte + static_cast(transaction.vendorField.size()); + + if (!transaction.vendorField.empty()) { + buffer.insert(buffer.begin() + v1::VF_OFFSET, // 0 <=> 255 Bytes + transaction.vendorField.begin(), + transaction.vendorField.end()); + } } -/**/ - -void Serializer::serializeVote(std::vector& bytes) const { - std::string votes; - - votes = std::accumulate(_transaction.asset.votes.begin(), - _transaction.asset.votes.end(), - std::string(), - [&](const std::string& a, const std::string& b) - -> std::string { - return a + (b.at(0) == '+' ? "01" : "00") + b.substr(1); - }); - - std::vector voteBytes = HexToBytes(votes.c_str()); - - bytes.push_back(static_cast(_transaction.asset.votes.size())); - - bytes.insert(bytes.end(), voteBytes.begin(), voteBytes.end()); +//////////////////////////////////////////////////////////////////////////////// +static auto serializeAsset(const TransactionData &transaction, + std::vector &buffer, + const size_t &offset) -> size_t { + switch (transaction.type) { + // Transfer + case TRANSFER_TYPE: + return Transfer::Serialize( + transaction.asset.transfer, + &buffer.at(offset)); + + // Second Signature + case SECOND_SIGNATURE_TYPE: + return SecondSignature::Serialize( + transaction.asset.secondSignature, + &buffer.at(offset)); + + // Delegate Registration + case DELEGATE_REGISTRATION_TYPE: + return DelegateRegistration::Serialize( + transaction.asset.delegateRegistration, + &buffer.at(offset)); + + // Vote + case VOTE_TYPE: + return Vote::Serialize( + transaction.asset.vote, + &buffer.at(offset)); + + // // MultiSignature + // case MULTI_SIGNATURE_TYPE: // TODO + + // Ipfs + case IPFS_TYPE: + return Ipfs::Serialize( + transaction.asset.ipfs, + &buffer.at(offset)); + + // MultiPayment + case MULTI_PAYMENT_TYPE: + return MultiPayment::Serialize( + transaction.asset.multiPayment, + buffer, + offset); + + // Delegate Resignation + // No Asset Needed. Return Default of '0'. + // case DELEGATE_RESIGNATION_TYPE: + + // Htlc Lock + case HTLC_LOCK_TYPE: + return HtlcLock::Serialize( + transaction.asset.htlcLock, + &buffer.at(offset)); + + // Htlc Claim + case HTLC_CLAIM_TYPE: + return HtlcClaim::Serialize( + transaction.asset.htlcClaim, + &buffer.at(offset)); + + // Htlc Refund + case HTLC_REFUND_TYPE: + return HtlcRefund::Serialize( + transaction.asset.htlcRefund, + &buffer.at(offset)); + + default: return 0UL; + }; } -/**/ - -void Serializer::serializeMultiSignatureRegistration( - std::vector& bytes) const { - std::string keysgroup; - - keysgroup = join(_transaction.asset.multiSignature.keysgroup, - _transaction.version == 1U ? 1U : 0U); - - bytes.push_back(_transaction.asset.multiSignature.min); - - bytes.push_back(static_cast( - _transaction.asset.multiSignature.keysgroup.size())); - - bytes.push_back(_transaction.asset.multiSignature.lifetime); - - std::vector keysgroupBytes = HexToBytes(keysgroup.c_str()); - - bytes.insert(bytes.end(), keysgroupBytes.begin(), keysgroupBytes.end()); +//////////////////////////////////////////////////////////////////////////////// +static auto serializeSignatures(const TransactionData &data, + std::vector &buffer, + const size_t &offset, + const SerializerOptions &options) -> size_t { + if (!options.excludeSignature && + data.signature.at(0) != 0 && + data.signature.size() >= SIGNATURE_ECDSA_MIN && + data.signature.size() <= SIGNATURE_ECDSA_MAX) { + buffer.insert(buffer.begin() + offset, + data.signature.begin(), + data.signature.end()); + } + else { return 0UL; } + + if (!options.excludeSecondSignature && + data.secondSignature.at(0) != 0 && + data.secondSignature.size() >= SIGNATURE_ECDSA_MIN && + data.secondSignature.size() <= SIGNATURE_ECDSA_MAX) { + buffer.insert(buffer.begin() + offset + data.signature.size(), + data.secondSignature.begin(), + data.secondSignature.end()); + } + else { return data.signature.size(); } + + return data.signature.size() + data.secondSignature.size(); } -/**/ - -void Serializer::serializeSignatures( - std::vector& bytes) const { - if (_transaction.signature.length() > 0) { - std::vector signatureBytes = HexToBytes( - _transaction.signature.c_str()); - bytes.insert( - bytes.end(), - signatureBytes.begin(), - signatureBytes.end()); - }; - - if (_transaction.secondSignature.length() > 0) { - std::vector secondSignatureBytes = HexToBytes( - _transaction.secondSignature.c_str()); - bytes.insert( - bytes.end(), - secondSignatureBytes.begin(), - secondSignatureBytes.end()); - } else if (_transaction.signSignature.length() > 0) { - std::vector signSignatureBytes = HexToBytes( - _transaction.signSignature.c_str()); - bytes.insert( - bytes.end(), - signSignatureBytes.begin(), - signSignatureBytes.end()); - }; - - if (!_transaction.signatures.empty()) { - bytes.push_back(0xff); - for (const auto& signature : _transaction.signatures) { - std::vector signatureBytes = HexToBytes(signature.c_str()); - bytes.insert( - bytes.end(), - std::begin(signatureBytes), - std::end(signatureBytes)); - }; - }; +//////////////////////////////////////////////////////////////////////////////// +// Serialize Transaction Data +// +// @param const TransactionData &data +// @param const SerializerOptions &options +// +// @return std::vector +// +// --- +auto Serializer::serialize(const TransactionData &data, + const SerializerOptions &options) + -> std::vector{ + std::vector buffer; + buffer.resize(TX_DEFAULT_SIZE); + + size_t assetOffset = 0UL; + + // Use v2 or v1, otherwise return an empty object. + if (data.version == TRANSACTION_VERSION_TYPE_2) { + serializeCommon(data, buffer); + assetOffset = VF_OFFSET + data.vendorField.size(); + } + else if (data.version == TRANSACTION_VERSION_TYPE_1) { + serializeCommonV1(data, buffer); + assetOffset = v1::VF_OFFSET + data.vendorField.size(); + } + else { return {}; } + + size_t assetSize = serializeAsset(data, buffer, assetOffset); + + size_t signaturesSize = serializeSignatures(data, + buffer, + assetOffset + assetSize, + options); + + buffer.resize(assetOffset + assetSize + signaturesSize); + + return buffer; } -} // namespace Transactions +} // namespace transactions } // namespace Crypto } // namespace Ark diff --git a/src/transactions/serializer.hpp b/src/transactions/serializer.hpp new file mode 100644 index 00000000..a379d295 --- /dev/null +++ b/src/transactions/serializer.hpp @@ -0,0 +1,45 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_SERIALIZER_HPP +#define ARK_TRANSACTIONS_SERIALIZER_HPP + +#include +#include + +#include "transactions/transaction_data.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Serializer Options +// Configure whether Signatures should be included during serialization. +struct SerializerOptions { + //////////////////////////////////////////////////////////////////////////// + bool excludeSignature; + bool excludeSecondSignature; +}; + +//////////////////////////////////////////////////////////////////////////////// +// Transaction Serializer Class +class Serializer { + public: + //////////////////////////////////////////////////////////////////////////// + static std::vector serialize( + const TransactionData &data, + const SerializerOptions &options = { false, false }); +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/transaction.cpp b/src/transactions/transaction.cpp index 0e10446d..739aede9 100644 --- a/src/transactions/transaction.cpp +++ b/src/transactions/transaction.cpp @@ -1,433 +1,177 @@ - -#include "transactions/transaction.h" - -#include +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "transactions/transaction.hpp" + +#include +#include +#include #include #include #include #include "crypto/curve.hpp" #include "crypto/hash.hpp" -#include "defaults/transaction_types.hpp" -#include "identities/address.hpp" + #include "identities/keys.hpp" -#include "identities/privatekey.hpp" -#include "utils/base58.hpp" -#include "utils/crypto_helpers.h" + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "transactions/defaults/offsets.hpp" + +#include "transactions/transaction_data.hpp" + +#include "transactions/mapping/json.hpp" +#include "transactions/mapping/mapping.hpp" + #include "utils/hex.hpp" -#include "utils/json.h" -using namespace Ark::Crypto::identities; +namespace Ark { +namespace Crypto { +namespace transactions { -std::string Ark::Crypto::Transactions::Transaction::getId() const { - auto bytes = this->toBytes(false, false); - const auto hash = Ark::Crypto::Hash::sha256(bytes.data(), bytes.size()); - memcpy(bytes.data(), hash.data(), HASH_32_BYTE_LEN); - return BytesToHex(bytes.begin(), bytes.begin() + HASH_32_BYTE_LEN); +//////////////////////////////////////////////////////////////////////////////// +// Compute the unique transaction ID. +auto Transaction::getId() const -> Hash32 { + const auto serialized = this->toBytes(); + return Hash::sha256(serialized.data(), serialized.size()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +// Sign the transaction using a passphrase. +auto Transaction::sign(const std::string &passphrase) -> bool { + if (passphrase.empty()) { + return false; + } + + auto keys = identities::Keys::fromPassphrase(passphrase.c_str()); + + std::copy(keys.publicKey.begin(), + keys.publicKey.end(), + this->data.senderPublicKey.begin()); + + const auto serialized = this->toBytes({ true, true }); + const auto hash32 = Hash::sha256(serialized.data(), serialized.size()); -std::string Ark::Crypto::Transactions::Transaction::sign( - const char* passphrase) { - auto keys = Keys::fromPassphrase(passphrase); - this->senderPublicKey = BytesToHex(keys.publicKey); + const auto success = Curve::Ecdsa::sign(hash32.data(), + keys.privateKey.data(), + &this->data.signature); - const auto bytes = this->toBytes(); - const auto hash = Ark::Crypto::Hash::sha256(&bytes[0], bytes.size()); - const auto pk = Keys::PrivateKey::fromPassphrase(passphrase); + memset(&keys, 0, sizeof(keys)); - std::vector buffer(Curve::Ecdsa::MAX_SIG_LEN); - Ark::Crypto::Curve::Ecdsa::sign(hash.data(), pk.data(), buffer); + if (success) { + std::copy_n(this->getId().begin(), HASH_32_LEN, this->data.id.begin()); + } - this->signature = BytesToHex(buffer.begin(), buffer.end()); - return this->signature; + return success; } -/**/ +//////////////////////////////////////////////////////////////////////////////// +// Sign the Transaction using a Second Passphrase. +auto Transaction::secondSign(const std::string &secondPassphrase) -> bool { + if (this->data.signature.empty() || this->data.signature.at(0) == 0 || + secondPassphrase.empty()) { + return false; + } -std::string Ark::Crypto::Transactions::Transaction::secondSign( - const char* passphrase) { - const auto bytes = this->toBytes(false); - const auto hash = Ark::Crypto::Hash::sha256(&bytes[0], bytes.size()); - const auto pk = Keys::PrivateKey::fromPassphrase(passphrase); + auto keys = identities::Keys::fromPassphrase(secondPassphrase.c_str()); - std::vector buffer(Curve::Ecdsa::MAX_SIG_LEN); - Ark::Crypto::Curve::Ecdsa::sign(hash.data(), pk.data(), buffer); + const auto serialized = this->toBytes({ false, true }); + const auto hash32 = Hash::sha256(serialized.data(), serialized.size()); - this->secondSignature = BytesToHex(buffer.begin(), buffer.end()); - return this->secondSignature; -} + const auto success = Curve::Ecdsa::sign(hash32.data(), + keys.privateKey.data(), + &this->data.secondSignature); -/**/ + memset(&keys, 0, sizeof(keys)); -bool Ark::Crypto::Transactions::Transaction::verify() const { - return this->internalVerify( - this->senderPublicKey, - this->toBytes(), - this->signature); + return success; } -/**/ +//////////////////////////////////////////////////////////////////////////////// +// Verify the Transaction. +auto Transaction::verify() const -> bool { + // skip both signatures, + // neither should be present in the signing hash. + const auto serialized = this->toBytes({ true, true }); + const auto hash32 = Hash::sha256(serialized.data(), serialized.size()); + + return Curve::Ecdsa::verify(hash32.data(), + this->data.senderPublicKey.data(), + this->data.signature); +} -bool Ark::Crypto::Transactions::Transaction::secondVerify( - const char* secondPublicKey) const { - std::string secondPublicKeyString = secondPublicKey; - return this->internalVerify( - secondPublicKeyString, - this->toBytes(false), - this->secondSignature); +//////////////////////////////////////////////////////////////////////////////// +// Verify the Transaction using a Second PublicKey. +auto Transaction::secondVerify(const uint8_t *secondPublicKey) const -> bool { + // include only the first signature, + // that should be present signing hash for a second signing. + const auto serialized = this->toBytes({ false, true }); + const auto hash32 = Hash::sha256(serialized.data(), serialized.size()); + + return Curve::Ecdsa::verify(hash32.data(), + secondPublicKey, + this->data.secondSignature); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +// Deserialize the given Hex string via AIP11. +auto Transaction::deserialize(const std::vector &serialized) -> bool { + if (!Deserializer::deserialize(&this->data, serialized)) { + return false; + } -bool Ark::Crypto::Transactions::Transaction::internalVerify( - const std::string& publicKey, - std::vector bytes, - const std::string& signature) const { - if (bytes.empty()) { return false; }; - const auto hash = Ark::Crypto::Hash::sha256(bytes.data(), bytes.size()); - const auto key = identities::PublicKey::fromHex(publicKey.c_str()); - auto signatureBytes = HexToBytes(signature.c_str()); + std::copy_n(this->getId().begin(), HASH_32_LEN, this->data.id.begin()); - return Ark::Crypto::Curve::Ecdsa::verify(hash.data(), - key.toBytes().data(), - signatureBytes); + return true; } -/**/ - -std::vector Ark::Crypto::Transactions::Transaction::toBytes( - bool skipSignature, - bool skipSecondSignature) const { - std::vector bytes; - - if (this->type == 0 && amount < 1ULL) { return bytes; }; - - pack(bytes, this->type); - pack(bytes, this->timestamp); - - const auto senderKeyBytes = HexToBytes( - this->senderPublicKey.c_str()); - bytes.insert( - std::end(bytes), - std::begin(senderKeyBytes), - std::end(senderKeyBytes)); - - const auto skiprecipient = - type == TransactionTypes::SecondSignatureRegistration - || type ==TransactionTypes::MultiSignatureRegistration; - - if (!this->recipient.empty() && !skiprecipient) { - const auto hashPair = Base58::getHashPair(this->recipient.c_str()); - pack(bytes, hashPair.version); - bytes.insert(std::end(bytes), - hashPair.pubkeyHash.begin(), - hashPair.pubkeyHash.end()); - } else { - std::vector filler(21, 0); - bytes.insert( - std::end(bytes), - std::begin(filler), - std::end(filler)); - }; - - if (!this->vendorField.empty() && vendorField.length() <= 255) { - bytes.insert( - std::end(bytes), - std::begin(this->vendorField), - std::end(this->vendorField)); - size_t diff = 64 - vendorField.length(); - if (diff > 0) { - std::vector filler(diff, 0); - bytes.insert( - std::end(bytes), - std::begin(filler), - std::end(filler)); - }; - } else { - std::vector filler(64, 0); - bytes.insert( - std::end(bytes), - std::begin(filler), - std::end(filler)); - }; - - pack(bytes, this->amount); - pack(bytes, this->fee); - - if (type == TransactionTypes::SecondSignatureRegistration) { - // SECOND_SIGNATURE_REGISTRATION - const auto publicKeyBytes = HexToBytes( - this->asset.signature.publicKey.c_str()); - bytes.insert( - std::end(bytes), - std::begin(publicKeyBytes), - std::end(publicKeyBytes)); - } else if (type == TransactionTypes::DelegateRegistration) { - // DELEGATE_REGISTRATION - bytes.insert( - std::end(bytes), - std::begin(this->asset.delegate.username), - std::end(this->asset.delegate.username)); - } else if (type == TransactionTypes::Vote) { - // VOTE - const auto joined = join(this->asset.votes); - bytes.insert( - std::end(bytes), - std::begin(joined), - std::end(joined)); - } else if (type == TransactionTypes::MultiSignatureRegistration) { - // MULTI_SIGNATURE_REGISTRATION - pack(bytes, this->asset.multiSignature.min); - pack(bytes, this->asset.multiSignature.lifetime); - const auto joined = join(this->asset.multiSignature.keysgroup); - bytes.insert( - std::end(bytes), - std::begin(joined), - std::end(joined)); - }; - - if (!skipSignature && !this->signature.empty()) { - const auto signatureBytes = HexToBytes(this->signature.c_str()); - bytes.insert( - std::end(bytes), - std::begin(signatureBytes), - std::end(signatureBytes)); - }; - - if (!skipSecondSignature && !this->secondSignature.empty()) { - const auto secondSignatureBytes = HexToBytes( - this->secondSignature.c_str()); - bytes.insert( - std::end(bytes), - std::begin(secondSignatureBytes), - std::end(secondSignatureBytes)); - }; - - return bytes; +//////////////////////////////////////////////////////////////////////////////// +// Serialize the object via AIP11. +auto Transaction::serialize() -> std::vector { + const auto serialized = Serializer::serialize(this->data); + if (serialized.empty()) { + return {}; + } + + std::copy_n(this->getId().begin(), HASH_32_LEN, this->data.id.begin()); + + return serialized; } -/**/ - -std::map Ark::Crypto::Transactions::Transaction::toArray() const { - // buffers for variable and non-string type-values. - char amount[24] = {}; - char assetName[16] = {}; - char assetValue[512] = {}; - char fee[24] = {}; - char network[8] = {}; - char signatures[512] = {}; - char timestamp[36] = {}; - char type[8] = {}; - char version[8] = {}; - - // Amount - snprintf(amount, sizeof(amount), "%" PRIu64, this->amount); - - // Asset - if (this->type == 0) { - // Transfer - // do nothing - } - else if (this->type == 1) { - // Second Signature Registration - memmove(assetName, "publicKey", 10); - memmove(assetValue, - this->asset.signature.publicKey.c_str(), - this->asset.signature.publicKey.length() + 1); - } - else if (this->type == 2) { - // Delegate Registration - memmove(assetName, "username", 9); - memmove(assetValue, - this->asset.delegate.username.c_str(), - this->asset.delegate.username.length() + 1); - } - else if (this->type == 3) { - // Vote - memmove(assetName, "votes", 6); - for (unsigned int i = 0U; i < this->asset.votes.size(); ++i) { - uint8_t offset = i * this->asset.votes[i].length(); - memmove(assetValue + offset + (i == 0U ? 0U : 1U), - this->asset.votes[i].c_str(), - this->asset.votes[i].length()); - if (i > 0 && i < this->asset.votes.size()) { - memmove(assetValue + offset, ",", 1); - } - } +//////////////////////////////////////////////////////////////////////////////// +// Turn the Transaction into its byte representation. +auto Transaction::toBytes(const SerializerOptions &options) const + -> std::vector { + return Serializer::serialize(this->data, options); +} - // } else if (this->type == 4) { // Multisignature Registration - // // TODO - // } else if (this->type == 5) { // IPFS - // // TBD - // } else if (this->type == 6) { // Timelock Registration - // // TBD - // } else if (this->type == 7) { // Multi-Payment - // // TBD - // } else if (this->type == 8) { // Delegate Resignation - // // TBD - }; - - // Fee - snprintf(fee, sizeof(fee), "%" PRIu64, this->fee); - - // Signatures - for (unsigned int i = 0U; i < this->signatures.size(); ++i) { - uint8_t offset = i * this->signatures[i].length(); - memmove(signatures + offset + (i == 0U ? 0U : 1U), - this->signatures[i].c_str(), - this->signatures[i].length()); - if (i > 1U && i < this->signatures.size()) { - memmove(signatures + i * this->signatures[i].length(), ",", 1); - } - } - - // Network - snprintf(network, sizeof(network), "%d", this->network); - - // Timestamp - snprintf(timestamp, sizeof(timestamp), "%" PRIu32, this->timestamp); - - // Type - snprintf(type, sizeof(type), "%u", this->type); - - // Version - snprintf(version, sizeof(version), "%u", this->version); - - return { - { "amount", amount }, - { assetName, assetValue }, - { "fee", fee }, - { "id", this->id }, - { "network", network }, - { "recipient", this->recipient }, - { "secondSignature", this->secondSignature }, - { "senderPublicKey", this->senderPublicKey }, - { "signature", this->signature }, - { "signatures", signatures }, - { "signSignature", this->signSignature }, - { "timestamp", timestamp }, - { "type", type }, - { "vendorField", this->vendorField }, - { "version", version } - }; +//////////////////////////////////////////////////////////////////////////////// +// Turn the transaction into a standardized array. +// +// This concept of an array in is quite different compared to other ARK SDKs. +// C++11 doesn't have an 'Any' type, so we'll need to use a string map here. +// +// Json Sizes are approximated using 'https://arduinojson.org/v6/assistant/' +// +// -- +auto Transaction::toMap() const -> std::map { + return Mapping::fromTransactionData(this->data); } -/**/ - -std::string Ark::Crypto::Transactions::Transaction::toJson() const { - std::map txArray = this->toArray(); - - // Update this value if the size of the JSON document changes - static const size_t docCapacity = 913; - - DynamicJsonDocument doc(docCapacity); - - // Amount - // >= Core v.2.5 'amount' json is string-type - doc["amount"] = txArray["amount"]; - - // Asset - if (this->type == 0) { - // Transfer - //do nothing - } else if (this->type == 1) { - // Second Signature Registration - JsonObject tAsset = doc.createNestedObject("asset"); - JsonObject signature = tAsset.createNestedObject("signature"); - signature["publicKey"] = txArray["publicKey"]; - } else if (this->type == 2) { - // Delegate Registration - JsonObject dAsset = doc.createNestedObject("asset"); - JsonObject delegate = dAsset.createNestedObject("delegate"); - delegate["username"] = txArray["username"]; - } else if (this->type == 3) { - // Vote - JsonObject vAsset = doc.createNestedObject("asset"); - JsonArray votes = vAsset.createNestedArray("votes"); - - std::string::size_type lastPos = txArray["votes"].find_first_not_of(',', 0); - std::string::size_type pos = txArray["votes"].find_first_of(',', lastPos); - while (std::string::npos != pos || std::string::npos != lastPos) { - votes.add(txArray["votes"].substr(lastPos, pos - lastPos)); - lastPos = txArray["votes"].find_first_not_of(',', pos); - pos = txArray["votes"].find_first_of(',', lastPos); - }; - - // } else if (this->type == 4) { // Multisignature Registration - // // TODO - // } else if (this->type == 5) { // IPFS - // // TBD - // } else if (this->type == 6) { // Timelock Registration - // // TBD - // } else if (this->type == 7) { // Multi-Payment - // // TBD - // } else if (this->type == 8) { // Delegate Resignation - // // TBD - }; - - // Fee - // >= Core v.2.5 'fee' json is string-type - doc["fee"] = txArray["fee"]; - - // Id - doc["id"] = txArray["id"]; - - // Network - if (txArray["network"] != "0") { - doc["network"] = atoi(txArray["network"].c_str()); - }; - - // recipient - doc["recipient"] = txArray["recipient"]; - - // SecondSignature - if (txArray["secondSignature"].length() > 0U) { - doc["secondSignature"] = txArray["secondSignature"]; - }; - - // SenderPublicKey - doc["senderPublicKey"] = txArray["senderPublicKey"]; - - // Signature - doc["signature"] = txArray["signature"]; - - // Signatures - if (!this->signatures.empty()) { - JsonArray signatures = doc.createNestedArray("signatures"); - std::string::size_type lastPos = txArray["signatures"].find_first_not_of(',', 0); - std::string::size_type pos = txArray["signatures"].find_first_of(',', lastPos); - while (std::string::npos != pos || std::string::npos != lastPos) { - signatures.add(txArray["signatures"].substr(lastPos, pos - lastPos)); - lastPos = txArray["signatures"].find_first_not_of(',', pos); - pos = txArray["signatures"].find_first_of(',', lastPos); - }; - }; - - // SignSignature - if (txArray["signSignature"].length() > 0U) { - doc["signSignature"] = txArray["signSignature"]; - }; - - // Timestamp - doc["timestamp"] = strtoul(txArray["timestamp"].c_str(), nullptr, 10); - - // Type - doc["type"] = atoi(txArray["type"].c_str()); - - // VendorField - if (txArray["vendorField"].length() > 0U) { - doc["vendorField"] = txArray["vendorField"]; - }; - - // Version - if (txArray["version"] != "0") { - doc["version"] = atoi(txArray["version"].c_str()); - }; - - char jsonChar[docCapacity]; - serializeJson(doc, jsonChar, docCapacity); - - return jsonChar; +//////////////////////////////////////////////////////////////////////////////// +// Turn the Transaction into a JSON string using `toMap()` as the source. +auto Transaction::toJson() const -> std::string { + return Json::fromTransactionMap(this->toMap()); } + +} // namespace transactions +} // namespace Crypto +} // namespace Ark diff --git a/src/transactions/types/assets.hpp b/src/transactions/types/assets.hpp new file mode 100644 index 00000000..30800280 --- /dev/null +++ b/src/transactions/types/assets.hpp @@ -0,0 +1,51 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_ASSETS_HPP +#define ARK_TRANSACTIONS_TYPES_ASSETS_HPP + +#include "transactions/defaults/types.hpp" + +#include "transactions/types/transfer.hpp" +#include "transactions/types/second_signature.hpp" +#include "transactions/types/delegate_registration.hpp" +#include "transactions/types/vote.hpp" +// #include "transactions/types/multi_signature.hpp" +#include "transactions/types/ipfs.hpp" +#include "transactions/types/multi_payment.hpp" +#include "transactions/types/delegate_resignation.hpp" +#include "transactions/types/htlc_lock.hpp" +#include "transactions/types/htlc_claim.hpp" +#include "transactions/types/htlc_refund.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// AIP-11 Transaction Assets +struct Asset { + Transfer transfer; // Type 0 + SecondSignature secondSignature; // Type 1 + DelegateRegistration delegateRegistration; // Type 2 + Vote vote; // Type 3 + // MultiSignature multiSignature; // Type 4 + Ipfs ipfs; // Type 5 + MultiPayment multiPayment; // Type 6 + DelegateResignation delegateResignation; // Type 7 + HtlcLock htlcLock; // Type 8 + HtlcClaim htlcClaim; // Type 9 + HtlcRefund htlcRefund; // Type 10 +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/types/delegate_registration.cpp b/src/transactions/types/delegate_registration.cpp new file mode 100644 index 00000000..beeb32d2 --- /dev/null +++ b/src/transactions/types/delegate_registration.cpp @@ -0,0 +1,135 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "transactions/types/delegate_registration.hpp" + +#include +#include +#include +#include + +#include "utils/json.h" +#include "utils/str.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Deserialize Delegate Registration (Type 2) - 4 <=> 21 Bytes +// +// @param DelegateRegistration *registration. +// @param const uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Username Length - 1 Byte: +// - registration.length = buffer[0]; +// +// Username - 3 <=> 20 Bytes: +// - std::copy_n(&buffer[1], registration->length, registration->username.begin()); +// +// --- +auto DelegateRegistration::Deserialize(DelegateRegistration *registration, + const uint8_t *buffer) -> size_t { + if (buffer[0] < USERNAME_MIN || buffer[0] > USERNAME_MAX) { + return 0UL; + } + + registration->length = buffer[0]; // 1 Byte + + std::copy_n(&buffer[sizeof(uint8_t)], // 3 <=> 20 Bytes + registration->length, + registration->username.begin()); + + return sizeof(uint8_t) + registration->length; // 4 <=> 21 Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +// Serialize Delegate Registration (Type 2) - 4 <=> 21 Bytes +// +// @param const DelegateRegistration ®istration. +// @param uint8_t *buffer: The buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Username Length - 1 Byte: +// - buffer[0] = registration.length; +// +// Username - 3 <=> 20 Bytes: +// - std::copy_n(registration.username.begin(), registration.length, &buffer[1]); +// +// --- +auto DelegateRegistration::Serialize(const DelegateRegistration ®istration, + uint8_t *buffer) -> size_t { + if (registration.length < USERNAME_MIN || + registration.length > USERNAME_MAX) { + return 0UL; + } + + buffer[0] = registration.length; // 1 Byte + + std::copy_n(registration.username.begin(), // 3 <=> 20 Bytes + registration.length, + &buffer[sizeof(uint8_t)]); + + return sizeof(uint8_t) + registration.length; // 4 <=> 21 Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Map/Json Constants +constexpr auto OBJECT_DELEGATE_LABEL = "delegate"; +const auto KEY_DELEGATE_SIZE = strlen(OBJECT_DELEGATE_LABEL); + +constexpr auto KEY_USERNAME_LABEL = "username"; +const auto KEY_USERNAME_SIZE = strlen(KEY_USERNAME_LABEL); + +//////////////////////////////////////////////////////////////////////////////// +// Add Delegate Registration Asset data to a Transaction Map. +void DelegateRegistration::addToMap(const DelegateRegistration ®istration, + std::map &map) { + map.emplace(KEY_USERNAME_LABEL, + std::string(registration.username.begin(), + registration.username.begin() + registration.length)); +} + +//////////////////////////////////////////////////////////////////////////////// +// Return the Json Capacity of a Delegate Registration-type Transaction. +auto DelegateRegistration::getJsonCapacity() -> size_t { + return JSON_OBJECT_SIZE(1) + KEY_ASSET_SIZE + + JSON_OBJECT_SIZE(1) + KEY_DELEGATE_SIZE + + JSON_OBJECT_SIZE(1) + KEY_USERNAME_SIZE + USERNAME_MAX; +} + +//////////////////////////////////////////////////////////////////////////////// +// Add Delegate Registration data to a `DynamicJsonDocument` using a std::map. +// +// The std::map must already contain Delegate Registration data. +// +// --- +void DelegateRegistration::addToJson( + DynamicJsonDocument &jsonDoc, + const std::map &map) { + const auto asset = jsonDoc.createNestedObject(KEY_ASSET_LABEL); + const auto registration = asset.createNestedObject(OBJECT_DELEGATE_LABEL); + + // Username + registration[KEY_USERNAME_LABEL] = map.at(KEY_USERNAME_LABEL); +} + +} // namespace transactions +} // namespace Crypto +} // namespace Ark diff --git a/src/transactions/types/delegate_registration.hpp b/src/transactions/types/delegate_registration.hpp new file mode 100644 index 00000000..c2ad8a8f --- /dev/null +++ b/src/transactions/types/delegate_registration.hpp @@ -0,0 +1,67 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_DELEGATE_REGISTRATION_HPP +#define ARK_TRANSACTIONS_TYPES_DELEGATE_REGISTRATION_HPP + +#include +#include +#include +#include + +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t USERNAME_MIN = 3U; +constexpr uint8_t USERNAME_MAX = 20U; + +//////////////////////////////////////////////////////////////////////////////// +// Type 2 - Delegate Registration +// +// Min Length: 3 +// Max Length: 20 +// +// --- +struct DelegateRegistration { + //////////////////////////////////////////////////////////////////////////// + uint8_t length { 0U }; + std::array username { }; + + //////////////////////////////////////////////////////////////////////////// + static size_t Deserialize(DelegateRegistration *registration, + const uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + static size_t Serialize(const DelegateRegistration ®istration, + uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + static void addToMap(const DelegateRegistration ®istration, + std::map &map); + + //////////////////////////////////////////////////////////////////////////// + static size_t getJsonCapacity(); + + //////////////////////////////////////////////////////////////////////////// + static void addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map); + + //////////////////////////////////////////////////////////////////////////// + DelegateRegistration() = default; +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/types/delegate_resignation.hpp b/src/transactions/types/delegate_resignation.hpp new file mode 100644 index 00000000..83a3df2f --- /dev/null +++ b/src/transactions/types/delegate_resignation.hpp @@ -0,0 +1,26 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_DELEGATE_RESIGNATION_HPP +#define ARK_TRANSACTIONS_TYPES_DELEGATE_RESIGNATION_HPP + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Type 7 - Delegate Resignation +// No Asset data needed. +struct DelegateResignation {}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/types/htlc_claim.cpp b/src/transactions/types/htlc_claim.cpp new file mode 100644 index 00000000..9d18ee49 --- /dev/null +++ b/src/transactions/types/htlc_claim.cpp @@ -0,0 +1,136 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "transactions/types/htlc_claim.hpp" + +#include +#include +#include +#include + +#include "interfaces/constants.h" + +#include "utils/hex.hpp" +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Deserialize Htlc Claim (Type 8) - 64 Bytes +// +// @param HtlcClaim *claim +// @param const uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Lock Transaction Id - 32 Bytes: +// - std::copy_n(buffer, 32, claim->id.begin()); +// +// Unlock Secret - UTF-8 encoded - 32 Bytes +// - std::copy_n(&buffer[32], 32, claim->secret.begin()); +// +// --- +auto HtlcClaim::Deserialize(HtlcClaim *claim, const uint8_t *buffer) -> size_t { + std::copy_n(buffer, HASH_32_LEN, claim->id.begin()); // 32 Bytes + + std::copy_n(&buffer[HASH_32_LEN], // 32 Bytes + HASH_32_LEN, + claim->secret.begin()); + + return HASH_64_LEN; // 64 Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +// Serialize Htlc Claim (Type 8) - 64 Bytes +// +// @param const HtlcClaim &claim +// @param uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Lock Transaction Id - 32 Bytes: +// - std::copy(claim.id.begin(), claim.id.end(), buffer); +// +// Unlock Secret - UTF-8 encoded - 32 Bytes +// - std::copy(claim.secret.begin(), claim.secret.end(), &buffer[32]); +// +// --- +auto HtlcClaim::Serialize(const HtlcClaim &claim, uint8_t *buffer) -> size_t { + std::copy(claim.id.begin(), claim.id.end(), buffer); // 32 Bytes + + std::copy(claim.secret.begin(), // 32 Bytes + claim.secret.end(), + &buffer[HASH_32_LEN]); + + return HASH_64_LEN; // 64 Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Map/Json Constants +constexpr auto OBJECT_HTLC_CLAIM_LABEL = "claim"; +const auto OBJECT_HTLC_CLAIM_SIZE = strlen(OBJECT_HTLC_CLAIM_LABEL); + +constexpr auto KEY_CLAIM_TX_ID_LABEL = "lockTransactionId"; +const auto KEY_CLAIM_TX_ID_SIZE = strlen(KEY_CLAIM_TX_ID_LABEL); + +constexpr auto KEY_CLAIM_SECRET_LABEL = "unlockSecret"; +const auto KEY_CLAIM_SECRET_SIZE = strlen(KEY_CLAIM_SECRET_LABEL); + +//////////////////////////////////////////////////////////////////////////////// +// Add Htlc Claim Asset data to a Transaction Map. +void HtlcClaim::addToMap(const HtlcClaim &claim, + std::map &map) { + // Id + map.emplace(KEY_CLAIM_TX_ID_LABEL, BytesToHex(claim.id)); + + // Secret + std::string secret(claim.secret.size(), '\0'); + secret.insert(secret.begin(), claim.secret.begin(), claim.secret.end()); + map.emplace(KEY_CLAIM_SECRET_LABEL, secret); +} + +//////////////////////////////////////////////////////////////////////////////// +// Return the Json Capacity of a Htlc Claim-type Transaction. +auto HtlcClaim::getJsonCapacity() -> size_t { + return JSON_OBJECT_SIZE(1) + KEY_ASSET_SIZE + + JSON_OBJECT_SIZE(1) + OBJECT_HTLC_CLAIM_SIZE + + JSON_OBJECT_SIZE(1) + KEY_CLAIM_TX_ID_SIZE + HASH_32_MAX_CHARS + + JSON_OBJECT_SIZE(1) + KEY_CLAIM_SECRET_SIZE + HASH_32_MAX_CHARS; +} + +//////////////////////////////////////////////////////////////////////////////// +// Add Htlc Claim data to a `DynamicJsonDocument` using a std::map. +// +// The std::map must already contain Htlc Claim data. +// +// --- +void HtlcClaim::addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map) { + const auto asset = jsonDoc.createNestedObject(KEY_ASSET_LABEL); + const auto claim = asset.createNestedObject(OBJECT_HTLC_CLAIM_LABEL); + + // Lock Transaction Id + claim[KEY_CLAIM_TX_ID_LABEL] = map.at(KEY_CLAIM_TX_ID_LABEL); + + // Unlock Secret + claim[KEY_CLAIM_SECRET_LABEL] = map.at(KEY_CLAIM_SECRET_LABEL); +} + +} // namespace transactions +} // namespace Crypto +} // namespace Ark diff --git a/src/transactions/types/htlc_claim.hpp b/src/transactions/types/htlc_claim.hpp new file mode 100644 index 00000000..4c765146 --- /dev/null +++ b/src/transactions/types/htlc_claim.hpp @@ -0,0 +1,60 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_HTLC_CLAIM_HPP +#define ARK_TRANSACTIONS_TYPES_HTLC_CLAIM_HPP + +#include +#include +#include +#include + +#include "interfaces/constants.h" +#include "interfaces/identities.hpp" + +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Type 9 - Htlc Claim +struct HtlcClaim { + //////////////////////////////////////////////////////////////////////////// + Hash32 id { }; + Hash32 secret { }; + + //////////////////////////////////////////////////////////////////////////// + static size_t Deserialize(HtlcClaim *claim, const uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + static size_t Serialize(const HtlcClaim &claim, uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// + static void addToMap(const HtlcClaim &claim, + std::map &map); + + //////////////////////////////////////////////////////////////////////////// + static size_t getJsonCapacity(); + + //////////////////////////////////////////////////////////////////////////// + static void addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map); + + //////////////////////////////////////////////////////////////////////////// + HtlcClaim() = default; +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/types/htlc_lock.cpp b/src/transactions/types/htlc_lock.cpp new file mode 100644 index 00000000..7b82e711 --- /dev/null +++ b/src/transactions/types/htlc_lock.cpp @@ -0,0 +1,240 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "transactions/types/htlc_lock.hpp" + +#include +#include +#include +#include + +#include "interfaces/constants.h" +#include "interfaces/identities.hpp" + +#include "transactions/defaults/offsets.hpp" + +#include "utils/base58.hpp" +#include "utils/hex.hpp" +#include "utils/json.h" +#include "utils/packing.h" +#include "utils/str.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Deserialize Htlc Lock Transaction (Type 8) - 66 Bytes +// +// @param HtlcLock *lock +// @param const uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Amount - 8 Bytes: +// - lock.amount = unpack8LE(buffer, 0); +// +// Secret Hash - 32 Bytes +// - std::copy_n(&buffer[offset], 32, lock->secretHash.begin()); +// +// Expiration Type- 1 Byte +// - lock.expirationType = buffer[40]; +// +// Expiration Value - 4 Bytes +// - lock.expirationValue = unpack4LE(buffer, 41); +// +// Recipient - 21 Bytes +// - std::copy_n(&buffer[offset], 21, lock->recipientId.begin()); +// +// --- +auto HtlcLock::Deserialize(HtlcLock *lock, const uint8_t *buffer) -> size_t { + size_t offset = 0UL; + + lock->amount = unpack8LE(buffer, offset); // 8 Bytes + + offset += sizeof(uint64_t); + + std::copy_n(&buffer[offset], // 32 Bytes + HASH_32_LEN, + lock->secretHash.begin()); + + offset += HASH_32_LEN; + + lock->expirationType = buffer[offset]; // 1 Byte + + offset += sizeof(uint8_t); + + lock->expiration = unpack4LE(buffer, offset); // 4 Bytes + + offset += sizeof(uint32_t); + + std::copy_n(&buffer[offset], // 21 Bytes + ADDRESS_HASH_LEN, + lock->recipientId.begin()); + + offset += ADDRESS_HASH_LEN; + + return offset; // 66 Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +// Serialize Htlc Lock Transaction (Type 8) - 70 Bytes +// +// @param const HtlcLock &lock +// @param uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Amount - 8 Bytes: +// - pack8LE(buffer, &lock.amount); +// +// Secret Hash - 32 Bytes +// - std::copy(lock.secretHash.begin(), lock.secretHash.end(), &buffer[8]);; +// +// Expiration Type- 1 Byte +// - buffer[40] = lock.expirationType; +// +// Expiration Value - 4 Bytes +// - pack4LE(&buffer[offset], &lock.expiration); +// +// Recipient - 21 Bytes +// - std::copy(lock.recipientId.begin(), lock.recipientId.end(), &buffer[45]); +// +// --- +auto HtlcLock::Serialize(const HtlcLock &lock, uint8_t *buffer) -> size_t { + size_t offset = 0; + + pack8LE(buffer, &lock.amount); // 8 Bytes + + offset += sizeof(uint64_t); + + std::copy(lock.secretHash.begin(), // 32 Bytes + lock.secretHash.end(), + &buffer[offset]); + + offset += HASH_32_LEN; + + buffer[offset] = lock.expirationType; // 1 Byte + + offset += sizeof(uint8_t); + + pack4LE(&buffer[offset], &lock.expiration); // 4 Bytes + + offset += sizeof(uint32_t); + + std::copy(lock.recipientId.begin(), // 21 Bytes + lock.recipientId.end(), + &buffer[offset]); + + offset += ADDRESS_HASH_LEN; + + return offset; +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Map/Json Constants +constexpr auto OBJECT_HTLC_LOCK_LABEL = "lock"; +const auto OBJECT_HTLC_LOCK_SIZE = strlen(OBJECT_HTLC_LOCK_LABEL) - 1U; + +constexpr auto OBJECT_HTLC_LOCK_TYPE_LABEL = "type"; +const auto OBJECT_HTLC_LOCK_VALUE_LABEL = "value"; + +constexpr auto KEY_AMOUNT_LABEL = "amount"; +const auto KEY_AMOUNT_SIZE = strlen(KEY_AMOUNT_LABEL); + +constexpr auto KEY_SECRET_HASH_LABEL = "secretHash"; +const auto KEY_SECRET_HASH_SIZE = strlen(KEY_SECRET_HASH_LABEL); + +constexpr auto KEY_EXPIRATION_TYPE_LABEL = "expirationType"; +const auto KEY_EXPIRATION_TYPE_SIZE = strlen(KEY_EXPIRATION_TYPE_LABEL); + +constexpr auto KEY_EXPIRATION_LABEL = "expiration"; +const auto KEY_EXPIRATION_SIZE = strlen(KEY_EXPIRATION_LABEL); + +constexpr auto KEY_RECIPIENT_ID_LABEL = "recipientId"; +const auto KEY_RECIPIENT_ID_SIZE = strlen(KEY_RECIPIENT_ID_LABEL); + +//////////////////////////////////////////////////////////////////////////////// +// Add Htlc Lock Asset data to a Transaction Map. +void HtlcLock::addToMap(const HtlcLock &lock, + std::map &map) { + // Amount + map.emplace(KEY_AMOUNT_LABEL, UintToString(lock.amount)); + + // Secret Hash + map.emplace(KEY_SECRET_HASH_LABEL, BytesToHex(lock.secretHash)); + + // Expiration Type + map.emplace(KEY_EXPIRATION_TYPE_LABEL, UintToString(lock.expirationType)); + + // Expiration + map.emplace(KEY_EXPIRATION_LABEL, UintToString(lock.expiration)); + + // RecipientId + map.emplace(KEY_RECIPIENT_ID_LABEL, Base58::parseAddressHash(lock.recipientId)); +} + +//////////////////////////////////////////////////////////////////////////////// +// Return the Json Capacity of a Htlc Lock-type Transaction. +auto HtlcLock::getJsonCapacity() -> size_t { + return JSON_OBJECT_SIZE(1) + KEY_AMOUNT_SIZE + UINT64_MAX_CHARS + + JSON_OBJECT_SIZE(1) + KEY_RECIPIENT_ID_SIZE + ADDRESS_STR_LEN + + JSON_OBJECT_SIZE(1) + KEY_ASSET_SIZE + + JSON_OBJECT_SIZE(1) + OBJECT_HTLC_LOCK_SIZE + + JSON_OBJECT_SIZE(1) + KEY_SECRET_HASH_SIZE + HASH_32_MAX_CHARS + + JSON_OBJECT_SIZE(1) + KEY_EXPIRATION_SIZE + + JSON_OBJECT_SIZE(1) + KEY_EXPIRATION_TYPE_SIZE + UINT8_MAX_CHARS + + JSON_OBJECT_SIZE(1) + KEY_EXPIRATION_SIZE + UINT32_MAX_CHARS; +} + +//////////////////////////////////////////////////////////////////////////////// +// Add Htlc Lock data to a `DynamicJsonDocument` using a std::map. +// +// The std::map must already contain Htlc Lock data. +// +// --- +void HtlcLock::addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map) { + // Amount + jsonDoc[KEY_AMOUNT_LABEL] = map.at(KEY_AMOUNT_LABEL); + + // RecipientId + jsonDoc[KEY_RECIPIENT_ID_LABEL] = map.at(KEY_RECIPIENT_ID_LABEL); + + const auto asset = jsonDoc.createNestedObject(KEY_ASSET_LABEL); + const auto lock = asset.createNestedObject(OBJECT_HTLC_LOCK_LABEL); + + // Secret Hash + lock[KEY_SECRET_HASH_LABEL] = map.at(KEY_SECRET_HASH_LABEL); + + const auto expiration = lock.createNestedObject(KEY_EXPIRATION_LABEL); + + // Expiration Type + expiration[OBJECT_HTLC_LOCK_TYPE_LABEL] = + strtoul(map.at(KEY_EXPIRATION_TYPE_LABEL).c_str(), + nullptr, + BASE_10); + + // Expiration Value + expiration[OBJECT_HTLC_LOCK_VALUE_LABEL] = + strtoul(map.at(KEY_EXPIRATION_LABEL).c_str(), + nullptr, + BASE_10); +} + +} // namespace transactions +} // namespace Crypto +} // namespace Ark diff --git a/src/transactions/types/htlc_lock.hpp b/src/transactions/types/htlc_lock.hpp new file mode 100644 index 00000000..18e24d6f --- /dev/null +++ b/src/transactions/types/htlc_lock.hpp @@ -0,0 +1,66 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_HTLC_LOCK_HPP +#define ARK_TRANSACTIONS_TYPES_HTLC_LOCK_HPP + +#include +#include +#include +#include + +#include "interfaces/constants.h" +#include "interfaces/identities.hpp" + +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +constexpr size_t HTLC_LOCK_SIZE = 66U; + +//////////////////////////////////////////////////////////////////////////////// +// Type 8 - Htlc Lock +struct HtlcLock { + //////////////////////////////////////////////////////////////////////////// + uint64_t amount { 0ULL }; + Hash32 secretHash { }; + uint8_t expirationType { 0U }; + uint32_t expiration { 0UL }; + AddressHash recipientId { }; + + //////////////////////////////////////////////////////////////////////////// + static size_t Deserialize(HtlcLock *lock, const uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + static size_t Serialize(const HtlcLock &lock, uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// + static void addToMap(const HtlcLock &lock, + std::map &map); + + //////////////////////////////////////////////////////////////////////////// + static size_t getJsonCapacity(); + + //////////////////////////////////////////////////////////////////////////// + static void addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map); + + //////////////////////////////////////////////////////////////////////////// + HtlcLock() = default; +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/types/htlc_refund.cpp b/src/transactions/types/htlc_refund.cpp new file mode 100644 index 00000000..6b421836 --- /dev/null +++ b/src/transactions/types/htlc_refund.cpp @@ -0,0 +1,114 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "transactions/types/htlc_refund.hpp" + +#include +#include +#include +#include + +#include "interfaces/constants.h" + +#include "utils/hex.hpp" +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Deserialize Htlc Refund (Type 10) - 32 Bytes +// +// @param HtlcRefund *refund +// @param const uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Lock Transaction Id - 32 Bytes: +// - std::copy_n(buffer, 32, refund->id.begin()); +// --- +auto HtlcRefund::Deserialize(HtlcRefund *refund, const uint8_t *buffer) + -> size_t { + refund->id = {}; + std::copy_n(buffer, HASH_32_LEN, refund->id.begin()); // 32 Bytes + + return HASH_32_LEN; +} + +//////////////////////////////////////////////////////////////////////////////// +// Serialize Htlc Refund (Type 10) - 32 Bytes +// +// @param const HtlcRefund &refund +// @param uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Lock Transaction Id - 32 Bytes: +// - std::copy(refund.id.begin(), refund.id.end(), buffer); +// +// --- +auto HtlcRefund::Serialize(const HtlcRefund &refund, uint8_t *buffer) + -> size_t { + std::copy(refund.id.begin(), refund.id.end(), buffer); // 32 Bytes + + return HASH_32_LEN; +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Map/Json Constants +constexpr auto OBJECT_HTLC_REFUND_LABEL = "refund"; +const auto OBJECT_HTLC_REFUND_SIZE = strlen(OBJECT_HTLC_REFUND_LABEL); + +constexpr auto KEY_REFUND_TX_ID_LABEL = "lockTransactionId"; +const auto KEY_REFUND_TX_ID_SIZE = strlen(KEY_REFUND_TX_ID_LABEL); + +constexpr auto HTLC_JSON_OBJECT_SIZE = 1U; + +//////////////////////////////////////////////////////////////////////////////// +// Add Htlc Refund Asset data to a Transaction Map. +void HtlcRefund::addToMap(const HtlcRefund &refund, + std::map &map) { + // Id + map.emplace(KEY_REFUND_TX_ID_LABEL, BytesToHex(refund.id)); +} + +//////////////////////////////////////////////////////////////////////////////// +// Return the Json Capacity of a Htlc Refund-type Transaction. +auto HtlcRefund::getJsonCapacity() -> size_t { + return JSON_OBJECT_SIZE(1) + KEY_ASSET_SIZE + + JSON_OBJECT_SIZE(1) + OBJECT_HTLC_REFUND_SIZE + + JSON_OBJECT_SIZE(1) + KEY_REFUND_TX_ID_SIZE + HASH_32_MAX_CHARS; +} + +//////////////////////////////////////////////////////////////////////////////// +// Add Htlc Refund data to a `DynamicJsonDocument` using a std::map. +// +// The std::map must already contain Htlc Refund data. +// +// --- +void HtlcRefund::addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map) { + const auto asset = jsonDoc.createNestedObject(KEY_ASSET_LABEL); + const auto refund = asset.createNestedObject(OBJECT_HTLC_REFUND_LABEL); + + // Lock Transaction Id + refund[KEY_REFUND_TX_ID_LABEL] = map.at(KEY_REFUND_TX_ID_LABEL); +} + +} // namespace transactions +} // namespace Crypto +} // namespace Ark diff --git a/src/transactions/types/htlc_refund.hpp b/src/transactions/types/htlc_refund.hpp new file mode 100644 index 00000000..88490391 --- /dev/null +++ b/src/transactions/types/htlc_refund.hpp @@ -0,0 +1,60 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_HTLC_REFUND_HPP +#define ARK_TRANSACTIONS_TYPES_HTLC_REFUND_HPP + +#include +#include +#include +#include + +#include "interfaces/constants.h" +#include "interfaces/identities.hpp" + +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Type 10 - Htlc Refund +struct HtlcRefund { + //////////////////////////////////////////////////////////////////////////// + Hash32 id { }; + + //////////////////////////////////////////////////////////////////////////// + static size_t Deserialize(HtlcRefund *refund, const uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + static size_t Serialize(const HtlcRefund &refund, uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// + static void addToMap(const HtlcRefund &refund, + std::map &map); + + //////////////////////////////////////////////////////////////////////////// + static size_t getJsonCapacity(); + + //////////////////////////////////////////////////////////////////////////// + static void addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map); + + //////////////////////////////////////////////////////////////////////////// + HtlcRefund() = default; +}; + + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/types/ipfs.cpp b/src/transactions/types/ipfs.cpp new file mode 100644 index 00000000..c1995c8f --- /dev/null +++ b/src/transactions/types/ipfs.cpp @@ -0,0 +1,122 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "transactions/types/ipfs.hpp" + +#include +#include +#include +#include + +#include "interfaces/constants.h" + +#include "utils/base58.hpp" +#include "utils/hex.hpp" +#include "utils/json.h" +#include "utils/str.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Deserialize Ipfs Transaction (Type 5) - 0 <=> 255 Bytes +// +// @param Ipfs *ipfs +// @param const uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Dag - UTF-8 encoded -0 <=> 255 Bytes +// - ipfs->dag.insert(ipfs->dag.begin(), buffer, buffer + ipfs->dag.size()); +// +// --- +auto Ipfs::Deserialize(Ipfs *ipfs, const uint8_t *buffer) -> size_t { + const size_t ipfsLen = buffer[1] + 2U; + + if (buffer[1] == 0U || ipfsLen <= IPFS_MIN || ipfsLen > IPFS_MAX) { + ipfs->dag.resize(0); + return 0UL; + } + + ipfs->dag.insert(ipfs->dag.begin(), buffer, buffer + ipfsLen); + + return ipfsLen; // 0 <=> 255 Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +// Serialize Ipfs Transaction (Type 5) - 0 <=> 255 Bytes +// +// @param const Ipfs &ipfs +// @param uint8_t *buffer: The buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Dag - UTF-8 encoded - 0 <=> 255 Bytes +// - std::copy(ipfs.dag.begin(), ipfs.dag.end(), buffer); +// +// --- +auto Ipfs::Serialize(const Ipfs &ipfs, uint8_t *buffer) -> size_t { + if (ipfs.dag.empty() || + ipfs.dag.size() <= IPFS_MIN || + ipfs.dag.size() > IPFS_MAX) { + return 0UL; + } + + std::copy(ipfs.dag.begin(), ipfs.dag.end(), buffer); // 0 <=> 255 Bytes + + return ipfs.dag.size(); +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Map/Json Constants +constexpr auto KEY_IPFS_LABEL = "ipfs"; +const auto KEY_IPFS_SIZE = strlen(KEY_IPFS_LABEL); + +//////////////////////////////////////////////////////////////////////////////// +// Add Ipfs Asset data to a Transaction Map. +void Ipfs::addToMap(const Ipfs &ipfs, + std::map &map) { + map.emplace(KEY_IPFS_LABEL, + Base58::checkEncode(ipfs.dag.data(), + ipfs.dag.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +// Return the Json Capacity of a Ipfs-type Transaction. +auto Ipfs::getJsonCapacity() -> size_t { + return JSON_OBJECT_SIZE(1) + KEY_ASSET_SIZE + + JSON_OBJECT_SIZE(1) + KEY_IPFS_SIZE + + IPFS_MAX; +} + +//////////////////////////////////////////////////////////////////////////////// +// Add Ipfs data to a `DynamicJsonDocument` using a std::map. +// +// The std::map must already contain Ipfs data. +// +// --- +void Ipfs::addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map) { + JsonObject asset = jsonDoc.createNestedObject(KEY_ASSET_LABEL); + + // Ipfs Dag + asset[KEY_IPFS_LABEL] = map.at(KEY_IPFS_LABEL); +} + +} // namespace transactions +} // namespace Crypto +} // namespace Ark diff --git a/src/transactions/types/ipfs.hpp b/src/transactions/types/ipfs.hpp new file mode 100644 index 00000000..93d72415 --- /dev/null +++ b/src/transactions/types/ipfs.hpp @@ -0,0 +1,61 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_IPFS_HPP +#define ARK_TRANSACTIONS_TYPES_IPFS_HPP + +#include +#include +#include +#include + +#include "interfaces/constants.h" + +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t IPFS_MIN = 2U; +constexpr uint8_t IPFS_MAX = 90U; + +//////////////////////////////////////////////////////////////////////////////// +// Type 5 - Ipfs +struct Ipfs { + //////////////////////////////////////////////////////////////////////////// + std::vector dag; + + //////////////////////////////////////////////////////////////////////////// + static size_t Deserialize(Ipfs *ipfs, const uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + static size_t Serialize(const Ipfs &ipfs, uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + static void addToMap(const Ipfs &ipfs, + std::map &map); + + //////////////////////////////////////////////////////////////////////////// + static size_t getJsonCapacity(); + + //////////////////////////////////////////////////////////////////////////// + static void addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map); + + //////////////////////////////////////////////////////////////////////////// + Ipfs() = default; +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/types/multi_payment.cpp b/src/transactions/types/multi_payment.cpp new file mode 100644 index 00000000..1bea3432 --- /dev/null +++ b/src/transactions/types/multi_payment.cpp @@ -0,0 +1,263 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "transactions/types/multi_payment.hpp" + +#include +#include +#include +#include +#include // std::accumulate +#include +#include + +#include "interfaces/constants.h" +#include "interfaces/identities.hpp" + +#include "identities/address.hpp" + +#include "utils/base58.hpp" +#include "utils/hex.hpp" +#include "utils/json.h" +#include "utils/packing.h" +#include "utils/str.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Deserialize MultiPayment (Type 6) - 2 + 29N Bytes +// +// @param MultiPayment *multipayment +// @param const uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Number of Payments - 2 Bytes: +// - payments.n_payments = unpack2LE(buffer, 0U); +// +// Amounts - n_payments * 8 Bytes +// - payments.amounts.at(i) = unpack8LE(buffer, offset); +// +// Addresses - n_payments * vector Bytes +// - std::copy_n(&buffer[pos], 21, payments->addresses.at(i).begin()); +// +// --- +auto MultiPayment::Deserialize(MultiPayment *payments, const uint8_t *buffer) + -> size_t { + payments->n_payments = unpack2LE(buffer, 0U); // 2 Bytes + + if (buffer[0] == 0U || payments->n_payments > MULTI_PAYMENT_NETWORK_LIMIT) { + payments->n_payments = 0UL; + return 0UL; + } + + payments->amounts.resize(payments->n_payments); + payments->addresses.resize(payments->n_payments); + + size_t pos = sizeof(uint16_t); + + for (size_t i = 0UL; i < payments->n_payments; ++i) { + payments->amounts.at(i) = unpack8LE(buffer, pos); // 8 Bytes + + pos += sizeof(uint64_t); + + std::copy_n(&buffer[pos], // 21 Bytes + ADDRESS_HASH_LEN, + payments->addresses.at(i).begin()); + + pos += ADDRESS_HASH_LEN; + } + + return pos; // 2 + 29N Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +// Check the final size of the transaction. +// +// - Do nothing if the buffer size is okay: return true. +// - Resize the buffer if it's too small: return true. +// - Clear the buffer if the transaction is too large: return false. +// +// --- +static inline auto checkBuffer(const MultiPayment &payments, + std::vector &buffer, + const size_t &offset) -> bool { + const size_t assetSize = + sizeof(uint16_t) + + ((sizeof(uint64_t) + ADDRESS_HASH_LEN) * payments.n_payments); + + const size_t fullTxSize = offset + assetSize; + + if (fullTxSize > TX_MAX_SIZE || + payments.n_payments > MULTI_PAYMENT_NETWORK_LIMIT) { + buffer.clear(); + return false; + } + + if (fullTxSize > buffer.size()) { + buffer.resize(fullTxSize); + } + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +// Serialize MultiPayment (Type 6) - 2 + 29N Bytes +// +// Resizes the outgoing buffer as-needed. +// +// @param const MultiPayment &multipayment +// @param uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Number of Payments - 2 Bytes: +// - pack2LE(&buffer.at(offset), &payments.n_payments); +// +// Amounts[] - 8 Bytes * n_payments +// - pack8LE(&buffer.at(pos), &payments.amounts.at(i)); +// +// Addresses - n_payments * vector Bytes +// - std::copy(payments.addresses.at(i).begin(), payments.addresses.at(i).end(), &buffer[pos]); +// +// --- +auto MultiPayment::Serialize(const MultiPayment &payments, + std::vector &buffer, + const size_t &offset) -> size_t { + if (!checkBuffer(payments, buffer, offset)) { + return 0UL; + } + + pack2LE(&buffer.at(offset), &payments.n_payments); // 2 Bytes + + size_t pos = offset + sizeof(uint16_t); + + for (size_t i = 0U; i < payments.n_payments; ++i) { + pack8LE(&buffer.at(pos), &payments.amounts.at(i)); // 8 Bytes + + pos += sizeof(uint64_t); + + std::copy(payments.addresses.at(i).begin(), // 21 Bytes + payments.addresses.at(i).end(), + &buffer[pos]); + + pos += ADDRESS_HASH_LEN; + } + + return pos - offset; // 2 + 29N Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Map/Json Constants +constexpr auto KEY_PAYMENTS_LABEL = "payments"; +const auto KEY_PAYMENTS_SIZE = strlen(KEY_PAYMENTS_LABEL); + +const auto KEY_N_PAYMENTS_SIZE = strlen(KEY_N_PAYMENTS_LABEL); + +constexpr auto KEY_AMOUNTS_LABEL = "amounts"; +const auto KEY_AMOUNTS_SIZE = strlen(KEY_AMOUNTS_LABEL); + +constexpr auto KEY_ADDRESSES_LABEL = "addresses"; +const auto KEY_ADDRESSES_SIZE = strlen(KEY_ADDRESSES_LABEL); + +constexpr auto KEY_AMOUNT_LABEL = "amount"; +const auto KEY_AMOUNT_SIZE = strlen(KEY_AMOUNT_LABEL); + +constexpr auto KEY_RECIPIENT_ID_LABEL = "recipientId"; +const auto KEY_RECIPIENT_ID_SIZE = strlen(KEY_RECIPIENT_ID_LABEL); + +constexpr auto MULTIPAYMENT_JSON_OBJECT_SIZE = 2U; + +//////////////////////////////////////////////////////////////////////////////// +// Join Array and Vectors of a given 'T' to a comma separated string. +// +// ex: +// - JoinWithOp(vector(amounts), UintToStringOp)); +// +// --- +template +static inline auto JoinWithOp(const InputType &a, Operation op) -> std::string { + const auto joinOp = [op](const std::string &a, + const T &b) -> std::string { + return a + (a.empty() ? a : ",") + op(b); + }; + return std::accumulate(a.begin(), a.end(), std::string(), joinOp); +} + +//////////////////////////////////////////////////////////////////////////////// +// Add MultiPayment Asset data to a Transaction Map. +// Key strings are comma-separated. +void MultiPayment::addToMap(const MultiPayment &payments, + std::map &map) { + // Number of Payments + map.emplace(KEY_N_PAYMENTS_LABEL, UintToString(payments.n_payments)); + + // Amounts + map.emplace(KEY_AMOUNTS_LABEL, + JoinWithOp(payments.amounts, UintToString)); + + // Addresses + map.emplace(KEY_ADDRESSES_LABEL, + JoinWithOp(payments.addresses, + Base58::parseAddressHash)); +} + +//////////////////////////////////////////////////////////////////////////////// +// Return the Json Capacity of a MultiPayment-type Transaction. +auto MultiPayment::getJsonCapacity(const size_t &n_payments) -> size_t { + return JSON_OBJECT_SIZE(1) + KEY_ASSET_SIZE + + JSON_OBJECT_SIZE(1) + KEY_PAYMENTS_SIZE + + JSON_ARRAY_SIZE(n_payments) + + (n_payments * JSON_OBJECT_SIZE(MULTIPAYMENT_JSON_OBJECT_SIZE)) + + (n_payments * (KEY_AMOUNTS_SIZE + UINT64_MAX_CHARS + + KEY_RECIPIENT_ID_SIZE + ADDRESS_STR_LEN)); +} + +//////////////////////////////////////////////////////////////////////////////// +// Add MultiPayment data to a `DynamicJsonDocument` using a std::map. +// +// The std::map must already contain MultiPayment data. +// +// --- +void MultiPayment::addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map) { + JsonObject asset = jsonDoc.createNestedObject(KEY_ASSET_LABEL); + JsonArray payments = asset.createNestedArray(KEY_PAYMENTS_LABEL); + + const auto paymentsStr = map.at(KEY_AMOUNTS_LABEL); + const auto addressesStr = map.at(KEY_ADDRESSES_LABEL); + const auto n_payments = strtol(map.at(KEY_N_PAYMENTS_LABEL).c_str(), + nullptr, + BASE_10); + + for (uint8_t i = 0U; i < n_payments; ++i) { + JsonObject payment_n = payments.createNestedObject(); + + // Amount(N) + payment_n[KEY_AMOUNT_LABEL] = paymentsStr + .substr(0, paymentsStr.find(',', 0)); + + // RecipientId(N) + payment_n[KEY_RECIPIENT_ID_LABEL] = addressesStr + .substr(i + (i * ADDRESS_STR_LEN), ADDRESS_STR_LEN); + } +} + +} // namespace transactions +} // namespace Crypto +} // namespace Ark diff --git a/src/transactions/types/multi_payment.hpp b/src/transactions/types/multi_payment.hpp new file mode 100644 index 00000000..dec61110 --- /dev/null +++ b/src/transactions/types/multi_payment.hpp @@ -0,0 +1,89 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_MULTI_PAYMENT_HPP +#define ARK_TRANSACTIONS_TYPES_MULTI_PAYMENT_HPP + +#include +#include +#include +#include +#include + +#include "interfaces/constants.h" +#include "interfaces/identities.hpp" + +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint16_t MULTI_PAYMENT_NETWORK_LIMIT = 100UL; + +constexpr auto KEY_N_PAYMENTS_LABEL = "n_payments"; + +//////////////////////////////////////////////////////////////////////////////// +// Type 6 - MultiPayment +// +// MultiPayment Transaction-types can get fairly large. +// +// Typically, we avoid using dynamic allocation as much as possible. +// +// In this case, we're using vectors. +// Otherwise, we'd need to allocate upwards of ~65K on the stack.. +// +// Extra precaution should always be used when dealing with large Txs. +// Particularly in embedded environments. +// +// --- +struct MultiPayment { + //////////////////////////////////////////////////////////////////////////// + // Network Limit: 100 + uint16_t n_payments { 0U }; + + //////////////////////////////////////////////////////////////////////////// + // n_payments * vector + std::vector amounts { 0ULL }; + + //////////////////////////////////////////////////////////////////////////// + // n_payments * vector Bytes + // std::vector > addresses; + std::vector addresses; + + //////////////////////////////////////////////////////////////////////////// + static size_t Deserialize(MultiPayment *payments, const uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + static size_t Serialize(const MultiPayment &payments, + std::vector &buffer, + const size_t &offset); + + //////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// + static void addToMap(const MultiPayment &payments, + std::map &map); + + //////////////////////////////////////////////////////////////////////////// + static size_t getJsonCapacity(const size_t &n_payments); + + //////////////////////////////////////////////////////////////////////////// + static void addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map); + + //////////////////////////////////////////////////////////////////////////// + MultiPayment() = default; +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/types/second_signature.cpp b/src/transactions/types/second_signature.cpp new file mode 100644 index 00000000..2a8017af --- /dev/null +++ b/src/transactions/types/second_signature.cpp @@ -0,0 +1,117 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "transactions/types/second_signature.hpp" + +#include +#include +#include +#include + +#include "interfaces/constants.h" + +#include "utils/hex.hpp" +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Deserialize Second Signature Registration (Type 1) - 33 Bytes +// +// @param SecondSignature *registration +// @param const uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Second PublicKey - 33 Bytes: +// - std::copy_n(buffer, 33, registration->publicKey.begin()); +// +// --- +auto SecondSignature::Deserialize(SecondSignature *registration, + const uint8_t *buffer) -> size_t { + std::copy_n(buffer, + PUBLICKEY_COMPRESSED_LEN, + registration->publicKey.begin()); + + return PUBLICKEY_COMPRESSED_LEN; // 33 Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +// Serialize Second Signature Registration (Type 1) - 33 Bytes +// +// @param const SecondSignature ®istration +// @param uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Second PublicKey - 33 Bytes: +// - std::copy(registration.publicKey.begin(), registration.publicKey.end(), buffer); +// +// --- +auto SecondSignature::Serialize(const SecondSignature ®istration, + uint8_t *buffer) -> size_t { + std::copy(registration.publicKey.begin(), + registration.publicKey.end(), + buffer); + + return PUBLICKEY_COMPRESSED_LEN; // 33 Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Map/Json Constants +constexpr auto KEY_SIGNATURE_LABEL = "signature"; +const auto KEY_SIGNATURE_SIZE = strlen(KEY_SIGNATURE_LABEL); + +constexpr auto KEY_PUBLICKEY_LABEL = "publicKey"; +const auto KEY_PUBLICKEY_SIZE = strlen(KEY_PUBLICKEY_LABEL); + +//////////////////////////////////////////////////////////////////////////////// +// Add SecondSignature Asset data to a Transaction Map. +void SecondSignature::addToMap(const SecondSignature ®istration, + std::map &map) { + // Second Signature PublicKey + map.emplace(KEY_PUBLICKEY_LABEL, BytesToHex(registration.publicKey)); +} + +//////////////////////////////////////////////////////////////////////////////// +// Return the Json Capacity of a SecondSignature-type Transaction. +auto SecondSignature::getJsonCapacity() -> size_t { + return JSON_OBJECT_SIZE(1) + KEY_ASSET_SIZE + + JSON_OBJECT_SIZE(1) + KEY_SIGNATURE_SIZE + + JSON_OBJECT_SIZE(1) + + KEY_PUBLICKEY_SIZE + PUBLICKEY_COMPRESSED_STR_LEN; +} + +//////////////////////////////////////////////////////////////////////////////// +// Add SecondSignature data to a `DynamicJsonDocument` using a std::map. +// +// The std::map must already contain SecondSignature data. +// +// --- +void SecondSignature::addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map) { + const auto asset = jsonDoc.createNestedObject(KEY_ASSET_LABEL); + const auto secondSig = asset.createNestedObject(KEY_SIGNATURE_LABEL); + + // PublicKey + secondSig[KEY_PUBLICKEY_LABEL] = map.at(KEY_PUBLICKEY_LABEL); +} + +} // namespace transactions +} // namespace Crypto +} // namespace Ark diff --git a/src/transactions/types/second_signature.hpp b/src/transactions/types/second_signature.hpp new file mode 100644 index 00000000..99d0b990 --- /dev/null +++ b/src/transactions/types/second_signature.hpp @@ -0,0 +1,59 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_SECOND_SIGNATURE_REGISTRATION_HPP +#define ARK_TRANSACTIONS_TYPES_SECOND_SIGNATURE_REGISTRATION_HPP + +#include +#include +#include + +#include "interfaces/identities.hpp" + +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Type 1 - Second Signature Registration +struct SecondSignature { + //////////////////////////////////////////////////////////////////////////// + PublicKeyBytes publicKey { }; + + //////////////////////////////////////////////////////////////////////////// + static size_t Deserialize(SecondSignature *registration, + const uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + static size_t Serialize(const SecondSignature ®istration, + uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// + static void addToMap(const SecondSignature ®istration, + std::map &map); + + //////////////////////////////////////////////////////////////////////////// + static size_t getJsonCapacity(); + + //////////////////////////////////////////////////////////////////////////// + static void addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map); + + //////////////////////////////////////////////////////////////////////////// + SecondSignature() = default; +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/types/transfer.cpp b/src/transactions/types/transfer.cpp new file mode 100644 index 00000000..3f884684 --- /dev/null +++ b/src/transactions/types/transfer.cpp @@ -0,0 +1,152 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "transactions/types/transfer.hpp" + +#include +#include +#include +#include +#include + +#include "interfaces/constants.h" + +#include "utils/base58.hpp" +#include "utils/hex.hpp" +#include "utils/json.h" +#include "utils/packing.h" +#include "utils/str.hpp" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Deserialize Transfer (Type 0) - 33 bytes +// +// @param Transfer *transfer +// @param uint8_t *buffer: The serialized buffer beginning at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Amount - 8 Bytes: +// - transfer.amount = unpack8LE(buffer, 0); +// +// Expiration - 4 Bytes: +// - transfer.expiration = unpack4LE(buffer, sizeof(uint64_t)); +// +// Recipient - 21 Bytes: +// - std::copy_n(&buffer[12], 21, transfer->recipientId.begin()); +// +// --- +auto Transfer::Deserialize(Transfer *transfer, const uint8_t *buffer) + -> size_t { + transfer->amount = unpack8LE(buffer, 0U); // 8 Bytes + transfer->expiration = unpack4LE(buffer, sizeof(uint64_t)); // 4 Bytes + + std::copy_n(&buffer[sizeof(uint64_t) + sizeof(uint32_t)], // 21 Bytes + ADDRESS_HASH_LEN, + transfer->recipientId.begin()); + + return TRANSACTION_TYPE_TRANSFER_SIZE; // 33 Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +// Serialize Transfer (Type 0) - 33 bytes +// +// @param const Transfer &transfer +// @param uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Amount - 8 Bytes: +// - pack8LE(buffer, &transfer.amount); +// +// Expiration - 4 Bytes: +// - pack4LE(&buffer[sizeof(uint64_t)], &transfer.expiration); +// +// Recipient - 21 Bytes: +// - std::copy(transfer.recipientId.begin(), transfer.recipientId.end(), &buffer[12]); +// +// --- +auto Transfer::Serialize(const Transfer &transfer, uint8_t *buffer) -> size_t { + pack8LE(buffer, &transfer.amount); // 8 Bytes + + pack4LE(&buffer[sizeof(uint64_t)], &transfer.expiration); // 4 Bytes + + std::copy(transfer.recipientId.begin(), // 21 Bytes + transfer.recipientId.end(), + &buffer[sizeof(uint64_t) + sizeof(uint32_t)]); + + return TRANSACTION_TYPE_TRANSFER_SIZE; // 33 Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Map/Json Constants +constexpr auto KEY_AMOUNT_LABEL = "amount"; +const auto KEY_AMOUNT_SIZE = strlen(KEY_AMOUNT_LABEL); + +constexpr auto KEY_EXPIRATION_LABEL = "expiration"; +const auto KEY_EXPIRATION_SIZE = strlen(KEY_EXPIRATION_LABEL); + +constexpr auto KEY_RECIPIENT_ID_LABEL = "recipientId"; +const auto KEY_RECIPIENT_ID_SIZE = strlen(KEY_RECIPIENT_ID_LABEL); + +//////////////////////////////////////////////////////////////////////////////// +// Add Transfer Asset data to a Transaction Map. +void Transfer::addToMap(const Transfer &transfer, + std::map &map) { + // Amount + map.emplace(KEY_AMOUNT_LABEL, UintToString(transfer.amount)); + + // Expiration + map.emplace(KEY_EXPIRATION_LABEL, UintToString(transfer.expiration)); + + // RecipientId + map.emplace(KEY_RECIPIENT_ID_LABEL, Base58::parseAddressHash(transfer.recipientId)); +} + +//////////////////////////////////////////////////////////////////////////////// +// Return the Json Capacity of a Transfer-type Transaction. +auto Transfer::getJsonCapacity() -> size_t { + return JSON_OBJECT_SIZE(1) + KEY_AMOUNT_SIZE + UINT64_MAX_CHARS + + JSON_OBJECT_SIZE(1) + KEY_EXPIRATION_SIZE + UINT32_MAX_CHARS + + JSON_OBJECT_SIZE(1) + KEY_RECIPIENT_ID_SIZE + ADDRESS_STR_LEN; +} + +//////////////////////////////////////////////////////////////////////////////// +// Add Transfer data to a `DynamicJsonDocument` using a std::map. +// +// The std::map must already contain Transfer data. +// +// --- +void Transfer::addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map) { + // Amount + jsonDoc[KEY_AMOUNT_LABEL] = map.at(KEY_AMOUNT_LABEL); + + // Expiration + jsonDoc[KEY_EXPIRATION_LABEL] = strtol(map.at(KEY_EXPIRATION_LABEL).c_str(), + nullptr, + BASE_10); + + // RecipientId + jsonDoc[KEY_RECIPIENT_ID_LABEL] = map.at(KEY_RECIPIENT_ID_LABEL); +} + +} // namespace transactions +} // namespace Crypto +} // namespace Ark diff --git a/src/transactions/types/transfer.hpp b/src/transactions/types/transfer.hpp new file mode 100644 index 00000000..d0d6be17 --- /dev/null +++ b/src/transactions/types/transfer.hpp @@ -0,0 +1,65 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_TRANSFER_HPP +#define ARK_TRANSACTIONS_TYPES_TRANSFER_HPP + +#include +#include +#include +#include +#include + +#include "interfaces/constants.h" +#include "interfaces/identities.hpp" + +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +constexpr size_t TRANSACTION_TYPE_TRANSFER_SIZE = 33UL; + +//////////////////////////////////////////////////////////////////////////////// +// Type 0 - Transfer +struct Transfer { + //////////////////////////////////////////////////////////////////////////// + uint64_t amount { 0ULL }; + uint32_t expiration { 0UL }; + AddressHash recipientId { }; + + //////////////////////////////////////////////////////////////////////////// + static size_t Deserialize(Transfer *transfer, const uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + static size_t Serialize(const Transfer &transfer, uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// + static void addToMap(const Transfer &transfer, + std::map &map); + + //////////////////////////////////////////////////////////////////////////// + static size_t getJsonCapacity(); + + //////////////////////////////////////////////////////////////////////////// + static void addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map); + + //////////////////////////////////////////////////////////////////////////// + Transfer() = default; +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/transactions/types/vote.cpp b/src/transactions/types/vote.cpp new file mode 100644 index 00000000..00b0f107 --- /dev/null +++ b/src/transactions/types/vote.cpp @@ -0,0 +1,128 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "transactions/types/vote.hpp" + +#include +#include +#include +#include + +#include "interfaces/constants.h" + +#include "utils/hex.hpp" +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +// Deserialize Vote Transaction (Type 3) - 35 Bytes +// +// @param Vote *vote +// @param const uint8_t *buffer: The serialized buffer at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Vote - 1 + 33 Bytes: +// - std::copy_n(&buffer[1], 34, vote->votes.begin()); +// +// --- +auto Vote::Deserialize(Vote *vote, const uint8_t *buffer) -> size_t { + vote->count = buffer[0]; // 1 Byte + + std::copy_n(&buffer[sizeof(uint8_t)], // 34 Bytes + VOTES_LEN, + vote->votes.begin()); + + return sizeof(uint8_t) + VOTE_LEN; // 35 Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +// Serialize Vote Transaction (Type 3) - 35 Bytes +// +// @param const Vote &vote +// @param uint8_t *buffer: The buffer beginning at the Assets offset. +// +// @return Asset Length +// +// --- +// Internals: +// +// Count - 1 Byte: 1-Vote supported +// - buffer[0] = 1; +// +// Vote - 2 + 33 Bytes: +// - std::copy(vote.votes.begin(), vote.votes.end(), &buffer[1]) +// +// --- +auto Vote::Serialize(const Vote &vote, uint8_t *buffer) -> size_t { + buffer[0] = vote.count; // 1 Byte + + std::copy(vote.votes.begin(), // 34 Bytes + vote.votes.end(), + &buffer[sizeof(uint8_t)]); + + return sizeof(uint8_t) + vote.count * VOTE_LEN; // 35 Bytes +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Map/Json Constants +constexpr auto KEY_VOTE_LABEL = "vote"; +const auto KEY_VOTE_SIZE = strlen(KEY_VOTE_LABEL); + +constexpr auto KEY_VOTES_LABEL = "votes"; +const auto KEY_VOTES_SIZE = strlen(KEY_VOTES_LABEL); + +//////////////////////////////////////////////////////////////////////////////// +// Add Vote Asset data to a Transaction Map. +void Vote::addToMap(const Vote &vote, std::map &map) { + // Vote + std::string votes; + votes.reserve(VOTES_LEN); + + // 1 Vote currently supported. + votes.push_back(vote.votes[0] == 0x01 ? '+' : '-'); + votes.append(BytesToHex(vote.votes.begin() + sizeof(uint8_t), + vote.votes.end())); + + map.emplace(KEY_VOTES_LABEL, votes); +} + +//////////////////////////////////////////////////////////////////////////////// +// Return the Json Capacity of a Vote-type Transaction. +auto Vote::getJsonCapacity() -> size_t { + return JSON_OBJECT_SIZE(1) + KEY_ASSET_SIZE + + JSON_ARRAY_SIZE(1) + KEY_VOTES_SIZE + + JSON_OBJECT_SIZE(1) + 1U + PUBLICKEY_COMPRESSED_STR_LEN; +} + +//////////////////////////////////////////////////////////////////////////////// +// Add Vote data to a `DynamicJsonDocument` using a std::map. +// +// The std::map must already contain Vote data. +// +// --- +void Vote::addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map) { + JsonObject asset = jsonDoc.createNestedObject(KEY_ASSET_LABEL); + JsonArray votes = asset.createNestedArray(KEY_VOTES_LABEL); + + // Votes + votes.add(map.at(KEY_VOTES_LABEL)); +} + +} // namespace transactions +} // namespace Crypto +} // namespace Ark diff --git a/src/transactions/types/vote.hpp b/src/transactions/types/vote.hpp new file mode 100644 index 00000000..87b5c9f5 --- /dev/null +++ b/src/transactions/types/vote.hpp @@ -0,0 +1,64 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_VOTE_HPP +#define ARK_TRANSACTIONS_TYPES_VOTE_HPP + +#include +#include +#include +#include + +#include "interfaces/constants.h" + +#include "utils/json.h" + +namespace Ark { +namespace Crypto { +namespace transactions { + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t VOTE_LIMIT = 1U; +constexpr uint8_t VOTE_LEN = 1U + PUBLICKEY_COMPRESSED_LEN; +constexpr uint8_t VOTES_LEN = VOTE_LIMIT * VOTE_LEN; + +//////////////////////////////////////////////////////////////////////////////// +// Type 3 - Vote +struct Vote { + //////////////////////////////////////////////////////////////////////////// + uint8_t count { 0U }; + std::array votes { }; // (1 + 33)N + + //////////////////////////////////////////////////////////////////////////// + static size_t Deserialize(Vote *vote, const uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + static size_t Serialize(const Vote &vote, uint8_t *buffer); + + //////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////// + static void addToMap(const Vote &vote, + std::map &map); + + //////////////////////////////////////////////////////////////////////////// + static size_t getJsonCapacity(); + + //////////////////////////////////////////////////////////////////////////// + static void addToJson(DynamicJsonDocument &jsonDoc, + const std::map &map); + + //////////////////////////////////////////////////////////////////////////// + Vote() = default; +}; + +} // namespace transactions +} // namespace Crypto +} // namespace Ark + +#endif diff --git a/src/utils/base58.cpp b/src/utils/base58.cpp index 3e856304..5525622b 100644 --- a/src/utils/base58.cpp +++ b/src/utils/base58.cpp @@ -9,90 +9,171 @@ #include "utils/base58.hpp" +#include #include +#include #include "interfaces/identities.hpp" #include "utils/str.hpp" -#include "bcl/Base58Check.hpp" +#include "Base58Check.hpp" // BCL Crypto Lib namespace Ark { namespace Crypto { +//////////////////////////////////////////////////////////////////////////////// +auto Base58::checkEncode(const uint8_t *data, const size_t &length) + -> std::string { + const std::array BASE58ALPHABET = { + '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', + 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', + 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', + 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p', + 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' + }; + + const size_t tmpSize = 164UL; + const size_t limit = 256UL; + const auto limitFlag = 0xFF; + + std::vector tmp; + tmp.resize(tmpSize); + + std::vector buffer; + buffer.resize(tmpSize); + + size_t j; + size_t startAt; + int zeroCount = 0; + + if (length > tmp.size()) { + return {}; + } + + std::copy_n(data, length, tmp.begin()); + while ((zeroCount < length) && (tmp.at(zeroCount) == 0U)) { + ++zeroCount; + } + + j = 2U * length; + startAt = zeroCount; + while (startAt < length) { + size_t remainder = 0U; + size_t divLoop; + + for (divLoop = startAt; divLoop < length; divLoop++) { + auto digit256 = (uint16_t)(tmp.at(divLoop) & limitFlag); + auto tmpDiv = remainder * limit + digit256; + tmp.at(divLoop) = (uint8_t)(tmpDiv / BASE58_ALPHABET_LEN); + remainder = (tmpDiv % BASE58_ALPHABET_LEN); + } + + if (tmp.at(startAt) == 0U) { + ++startAt; + } + + buffer.at(--j) = (uint8_t)BASE58ALPHABET.at(remainder); + } + + while (zeroCount-- > 0) { + buffer.at(--j) = BASE58ALPHABET.at(0); + } + + auto outLength = 2U * length - j; + + return std::string((buffer.begin() + j), buffer.begin() + j + outLength); +} + +//////////////////////////////////////////////////////////////////////////////// // Returns a Version-byte and Ripemd160 Hash as a pair. // Expects a c_string address as input. -PubkeyHashPair Base58::getHashPair(const char* address) { - PubkeyHashPair out {}; - bcl::Base58Check::pubkeyHashFromBase58Check(address, - out.pubkeyHash.data(), - &out.version); - return out; +auto Base58::getHashPair(const char *address) -> PubkeyHashPair { + PubkeyHashPair out {}; + bcl::Base58Check::pubkeyHashFromBase58Check(address, + out.pubkeyHash.data(), + &out.version); + return out; } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns a formatted Address string. // Expects a 20-byte Ripemd160 Address hash a Network version-byte. -std::string Base58::parseHash(const uint8_t* pubkeyHash, uint8_t version) { - std::string out(ADDRESS_STRING_LEN + 1, '\0'); // (+1, null terminator) - bcl::Base58Check::pubkeyHashToBase58Check(pubkeyHash, version, &out[0]); - return out; +auto Base58::parseAddressHash(const AddressHash &addressHash) -> std::string { + std::string out(ADDRESS_STR_LEN, '\0'); + bcl::Base58Check::pubkeyHashToBase58Check(&addressHash.at(1), + addressHash.at(0), + &out[0]); + return out; } -/**/ +//////////////////////////////////////////////////////////////////////////////// +// Returns a formatted Address string. +// Expects a 20-byte Ripemd160 Address hash a Network version-byte. +auto Base58::parsePubkeyHash(const uint8_t *pubkeyHash, uint8_t version) + -> std::string { + std::string out(ADDRESS_STR_LEN, '\0'); + bcl::Base58Check::pubkeyHashToBase58Check(pubkeyHash, version, &out[0]); + return out; +} +//////////////////////////////////////////////////////////////////////////////// // Returns a Wif string from PrivateKey-bytes and a Wif version-byte. -std::string Base58::getWif(const uint8_t* privateKeyBytes, uint8_t version) { - std::string out(WIF_STRING_LEN + 1, '\0'); - bcl::Base58Check::privateKeyToBase58Check(bcl::Uint256(privateKeyBytes), - version, - true, - &out[0]); - return out; +auto Base58::getWif(const uint8_t *privateKeyBytes, uint8_t version) + -> std::string { + std::string out(WIF_STRING_LEN, '\0'); + bcl::Base58Check::privateKeyToBase58Check(bcl::Uint256(privateKeyBytes), + version, + true, + &out[0]); + return out; } -/**/ - +//////////////////////////////////////////////////////////////////////////////// // Returns PrivateKey bytes from a Wif string and a Wif version-byte. -PrivateKeyBytes Base58::parseWif(const char* wif, uint8_t* outVersion) { - if (!Base58::validate(wif, WIF_STRING_LEN)) { return {}; }; - - bcl::Uint256 num; - bool compressed; - bcl::Base58Check::privateKeyFromBase58Check(wif, - num, - outVersion, - &compressed); - - PrivateKeyBytes privateKey; - num.getBigEndianBytes(privateKey.data()); - return privateKey; +auto Base58::parseWif(const char *wif, uint8_t* outVersion) -> PrivateKeyBytes { + if (!Base58::validate(wif, WIF_STRING_LEN)) { + return {}; + } + + bcl::Uint256 num; + bool compressed; + bcl::Base58Check::privateKeyFromBase58Check(wif, + num, + outVersion, + &compressed); + + PrivateKeyBytes privateKey {}; + num.getBigEndianBytes(privateKey.data()); + + return privateKey; } -/**/ +//////////////////////////////////////////////////////////////////////////////// +constexpr size_t BASE58_TABLE_LEN = 128U; -namespace { // NOLINT -const size_t BASE58_TABLE_LEN = 128U; -const std::array Base58Table = {{ +//////////////////////////////////////////////////////////////////////////////// +constexpr std::array Base58Table = {{ #include "base58.table" }}; -} // namespace - -/**/ +//////////////////////////////////////////////////////////////////////////////// // Validates that a given string is Base58 encoded. // An Address will be 34-chars. // A Wif will be 52-chars. -bool Base58::validate(const char* str, size_t size) { - auto length = strlenSafe(str); - if (size == 0 || length != size) { return false; }; - - for (auto i = 0U; i < length; ++i) { - if (Base58Table.at(str[i]) == 0) { return false; }; - }; - - return true; -}; +auto Base58::validate(const char *str, const size_t &size) -> bool { + auto length = strlenSafe(str); + if (size == 0 || length != size) { + return false; + } + + for (auto i = 0U; i < length; ++i) { + if (Base58Table.at(str[i]) == 0) { + return false; + } + } + + return true; +} } // namespace Crypto } // namespace Ark diff --git a/src/utils/base58.hpp b/src/utils/base58.hpp index 8c1801cb..3b202a35 100644 --- a/src/utils/base58.hpp +++ b/src/utils/base58.hpp @@ -7,23 +7,44 @@ * file that was distributed with this source code. **/ -#ifndef UTILS_BASE58_HPP -#define UTILS_BASE58_HPP +#ifndef ARK_UTILS_BASE58_HPP +#define ARK_UTILS_BASE58_HPP + +#include +#include #include "interfaces/identities.hpp" namespace Ark { namespace Crypto { +//////////////////////////////////////////////////////////////////////////////// struct Base58 { - static PubkeyHashPair getHashPair(const char* address); - static std::string parseHash(const uint8_t* pubkeyHash, uint8_t version); - static std::string getWif(const uint8_t* privateKeyBytes, uint8_t version); - static PrivateKeyBytes parseWif(const char* wif, uint8_t* outVersion); + //////////////////////////////////////////////////////////////////////////// + static std::string checkEncode(const uint8_t *data, const size_t &length); + + //////////////////////////////////////////////////////////////////////////// + static PubkeyHashPair getHashPair(const char *address); + + //////////////////////////////////////////////////////////////////////////// + static std::string parseAddressHash(const AddressHash &addressHash); + + //////////////////////////////////////////////////////////////////////////// + static std::string parsePubkeyHash(const uint8_t *pubkeyHash, + uint8_t version); - static bool validate(const char* str, size_t size); + //////////////////////////////////////////////////////////////////////////// + static std::string getWif(const uint8_t *privateKeyBytes, uint8_t version); + + //////////////////////////////////////////////////////////////////////////// + static PrivateKeyBytes parseWif(const char *wif, uint8_t *outVersion); + + //////////////////////////////////////////////////////////////////////////// + static bool validate(const char *str, const size_t &size); }; +//////////////////////////////////////////////////////////////////////////////// + } // namespace Crypto } // namespace Ark diff --git a/src/utils/crypto_helpers.h b/src/utils/crypto_helpers.h index d7e44c63..33a94afb 100644 --- a/src/utils/crypto_helpers.h +++ b/src/utils/crypto_helpers.h @@ -7,56 +7,37 @@ * file that was distributed with this source code. **/ -#ifndef CRYPTO_HELPERS_H -#define CRYPTO_HELPERS_H +#ifndef ARK_UTILS_CRYPTO_HELPERS_H +#define ARK_UTILS_CRYPTO_HELPERS_H #include #include #include #include -const auto ADDRESS_LENGTH = 34U; -const auto COMPRESSED_PUBLICKEY_SIZE = 33U; -const auto PRIVATEKEY_SIZE = 32U; -const auto WIF_SIZE = 52U; - -#if (defined ARDUINO || defined ESP8266 || defined ESP32) - -#define USE_IOT - -#include -#include - -#endif - -#ifndef USE_IOT - -#define __STDC_FORMAT_MACROS 1 -#include - -#endif - +//////////////////////////////////////////////////////////////////////////////// // Write data into dst template -inline void pack(std::vector& dst, T& data) { - const auto src = reinterpret_cast(&data); - dst.insert(dst.end(), src, src + sizeof(T)); +inline void pack(std::vector &dst, T &data) { + const auto src = reinterpret_cast(&data); + dst.insert(dst.end(), src, src + sizeof(T)); } +//////////////////////////////////////////////////////////////////////////////// // Read size bytes into dst from src template -inline void unpack(T* dst, uint8_t* src, size_t size = -1) { - memcpy(dst, src, size == -1 ? sizeof(*dst) : size); +inline void unpack(T *dst, uint8_t *src, size_t size = -1) { + memcpy(dst, src, size == -1 ? sizeof(*dst) : size); } +//////////////////////////////////////////////////////////////////////////////// // Join string vector -inline std::string join(const std::vector& strings, size_t offset = 0U) { - return std::accumulate( - strings.begin(), - strings.end(), - std::string(), - [&](const std::string& a, const std::string& b) - -> std::string { return a + b.substr(offset); }); +inline auto join(const std::vector &strings) -> std::string { + return std::accumulate(strings.begin(), + strings.end(), + std::string(), + [](const std::string &a, const std::string &b) + -> std::string { return a + b; }); } #endif diff --git a/src/utils/hex.hpp b/src/utils/hex.hpp index 1c99a148..139a06d0 100644 --- a/src/utils/hex.hpp +++ b/src/utils/hex.hpp @@ -7,78 +7,78 @@ * file that was distributed with this source code. **/ -#ifndef UTILS_HEX_HPP -#define UTILS_HEX_HPP +#ifndef ARK_UTILS_HEX_HPP +#define ARK_UTILS_HEX_HPP #include #include #include -/** - * Hex Helpers - **/ -template -inline std::string BytesToHex( - const T itbegin, - const T itend) { - const char hexmap[16] = { - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'a', 'b', 'c', 'd', 'e', 'f' - }; - - std::string rv; - rv.reserve((itend - itbegin) * 3); - for (T it = itbegin; it < itend; ++it) { - const auto val = static_cast(*it); - rv.push_back(hexmap[val >> 4]); - rv.push_back(hexmap[val & 15]); - } - - return rv; -}; - -/**/ +#include "interfaces/constants.h" +//////////////////////////////////////////////////////////////////////////////// +// Bytes to Hex template -inline std::string BytesToHex(const T& vch) { - return BytesToHex(vch.begin(), vch.end()); -}; - -/****/ - -namespace { +inline std::string BytesToHex(const T itbegin, const T itend) { + const char hexmap[16] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f' + }; + + std::string rv; + rv.reserve((itend - itbegin) * 3); + + for (T it = itbegin; it < itend; ++it) { + const auto val = static_cast(*it); + rv.push_back(hexmap[val >> 4]); + rv.push_back(hexmap[val & 15]); + } + + return rv; +} + +//////////////////////////////////////////////////////////////////////////////// +// Bytes to Hex +template inline std::string BytesToHex(const T& vch) { + return BytesToHex(vch.begin(), vch.end()); +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// constexpr size_t HEX_TABLE_LEN = 256U; + +//////////////////////////////////////////////////////////////////////////////// constexpr std::array HexTable = {{ #include "utils/hex.table" }}; -} // namespace - -/**/ +//////////////////////////////////////////////////////////////////////////////// inline int8_t HexDigit(char c) { - return HexTable[static_cast(c)]; -}; - -/**/ + return HexTable[static_cast(c)]; +} +//////////////////////////////////////////////////////////////////////////////// inline std::vector HexToBytes(const char* psz) { - // convert hex dump to vector - std::vector vch; - for (;;) { - while (isspace(*psz) != 0) { psz++; }; - auto c = HexDigit(*psz++); - if (c == static_cast(-1)) { break; }; - int8_t n = (c << 4); - c = HexDigit(*psz++); - if (c == static_cast(-1)) { break; }; - n |= c; - vch.push_back(n); - }; - return vch; -}; - -/**/ + // convert hex dump to vector + std::vector vch; + + for (;;) { + while (isspace(*psz) != 0) { psz++; }; + auto a = HexDigit(*psz++); + if (a < 0) { break; } + + auto b = HexDigit(*psz++); + if (b < 0) { break; } + + vch.push_back(b |= a << 4); + } + + return vch; +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// // Hex string to Byte Array. // Same as HexToBytes, but using std::array. // This is a bit lighter and faster than using the vector method. @@ -90,19 +90,24 @@ inline std::vector HexToBytes(const char* psz) { // Examples: // auto privateKeyFromHex = HexToBytesArray<>(my_privatekey_hex_string); // auto publicKeyFromHex = HexToBytesArray<33>(my_publickey_hex_string); -template -inline std::array HexToBytesArray(const char* psz) { - if (psz == nullptr) { return {0}; }; - - std::array arr; - for (auto& e : arr) { - while (isspace(*psz) != 0) { psz++; }; - auto a = HexTable[ static_cast(*psz++) ] << 4; - auto b = HexTable[ static_cast(*psz++) ]; - if ((a | b) < 0) { return {0}; }; - e = a |= b; - }; - return arr; -}; +template +inline std::array HexToBytesArray(const char *psz) { + if (psz == nullptr) { return std::array(); }; + + std::array arr {}; + for (auto &e : arr) { + while (isspace(*psz) != 0) { psz++; }; + + auto a = HexDigit(*psz++); + if (a < 0) { return {}; } + + auto b = HexDigit(*psz++); + if (b < 0) { return {}; } + + e = b |= a << 4; + } + + return arr; +} #endif diff --git a/src/utils/json.h b/src/utils/json.h index 12912745..43e92eb3 100644 --- a/src/utils/json.h +++ b/src/utils/json.h @@ -1,6 +1,6 @@ -#ifndef JSON_H -#define JSON_H +#ifndef ARK_JSON_H +#define ARK_JSON_H // ArduinoJson Presets #define ARDUINOJSON_USE_LONG_LONG 1 diff --git a/src/utils/packing.h b/src/utils/packing.h new file mode 100644 index 00000000..7229db2e --- /dev/null +++ b/src/utils/packing.h @@ -0,0 +1,58 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_UTILS_PACKING_H +#define ARK_UTILS_PACKING_H + +#include +#include + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t U1_MAX = 0xFF; +constexpr uint16_t U2_MAX = 0xFFFF; +constexpr uint32_t U4_MAX = 0xFFFFFFFF; +constexpr uint64_t U8_MAX = 0xFFFFFFFFFFFFFFFF; + +constexpr auto U1_SIZE = sizeof(uint8_t); +constexpr auto U2_SIZE = sizeof(uint16_t); +constexpr auto U4_SIZE = sizeof(uint32_t); +constexpr auto U8_SIZE = sizeof(uint64_t); + +constexpr uint16_t U1_SHIFT = 8U; +constexpr uint32_t U2_SHIFT = 16U; +constexpr uint64_t U4_SHIFT = 32U; + +//////////////////////////////////////////////////////////////////////////////// +#define pack2LE(dst, src) memcpy (dst, src, U2_SIZE) + +//////////////////////////////////////////////////////////////////////////////// +#define pack4LE(dst, src) memcpy (dst, src, U4_SIZE) + +//////////////////////////////////////////////////////////////////////////////// +#define pack8LE(dst, src) memcpy (dst, src, U8_SIZE) + +//////////////////////////////////////////////////////////////////////////////// +#define unpack2LE(src, offset) \ + ((((uint16_t)(((src)[(offset)]) & U1_MAX)) | \ + (((uint16_t)(((src)[((offset) + U1_SIZE)]) & U1_MAX) << U1_SHIFT))) \ + & U2_MAX) + +//////////////////////////////////////////////////////////////////////////////// +#define unpack4LE(src, offset) \ + ((((uint32_t)((unpack2LE((src), (offset))) & U2_MAX)) | \ + (((uint32_t)((unpack2LE((src), ((offset) + U2_SIZE))) & U2_MAX) << U2_SHIFT))) \ + & U4_MAX) + +//////////////////////////////////////////////////////////////////////////////// +#define unpack8LE(src, offset) \ + ((((uint64_t)((unpack4LE((src), (offset))) & U4_MAX)) | \ + (((uint64_t)((unpack4LE((src), ((offset) + U4_SIZE))) & U4_MAX) << U4_SHIFT))) \ + & U8_MAX) + +#endif diff --git a/src/utils/platform.h b/src/utils/platform.h new file mode 100644 index 00000000..68011e91 --- /dev/null +++ b/src/utils/platform.h @@ -0,0 +1,31 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_UTILS_PLATFORM_H +#define ARK_UTILS_PLATFORM_H + +//////////////////////////////////////////////////////////////////////////////// +#if (defined ARDUINO || defined ESP8266 || defined ESP32) + +#define USE_IOT + +#include +#include + +#endif // (defined ARDUINO || defined ESP8266 || defined ESP32) + +//////////////////////////////////////////////////////////////////////////////// +#ifndef USE_IOT + +#define __STDC_FORMAT_MACROS 1 +#include + +#endif // ifndef USE_IOT + +#endif diff --git a/src/utils/str.hpp b/src/utils/str.hpp index 5388764b..f375f5da 100644 --- a/src/utils/str.hpp +++ b/src/utils/str.hpp @@ -7,35 +7,69 @@ * file that was distributed with this source code. **/ -#ifndef UTILS_STR_HPP -#define UTILS_STR_HPP +#ifndef ARK_UTILS_STR_HPP +#define ARK_UTILS_STR_HPP +#include #include #include #include -/**/ +#include "interfaces/constants.h" +//////////////////////////////////////////////////////////////////////////////// +constexpr size_t ALPHANUM_TABLE_LEN = 128U; + +//////////////////////////////////////////////////////////////////////////////// +constexpr std::array AlphaNumericTable = {{ + #include "utils/str.table" +}}; + +//////////////////////////////////////////////////////////////////////////////// // Checks string length with bounds and char checking. // returned length excludes the null terminator. inline size_t strlenSafe(const char* str) { - const size_t ALPHANUM_TABLE_LEN = 128U; - const std::array AlphaNumericTable = {{ - #include "utils/str.table" - }}; - - size_t count = 0; - while (str[count] != 0 || - isspace(str[count]) || - AlphaNumericTable.at(str[count] > 0)) { count++; }; - return count; -} + size_t count = 0; + while (str[count] != 0 || + isspace(static_cast(str[count])) != 0 || + AlphaNumericTable.at(str[count]) > 0) { + count++; + }; -/**/ + return count; +} +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// // Converts an ASCII string to a uint8_t/bytes vector representation. inline const std::vector StringToBytes(const std::string& str) { - return { str.begin(), str.end() }; + return { str.begin(), str.end() }; }; +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Convert an Unsigned Integer to its string representation. +inline const std::string UintToString(uint64_t amount) { + if (amount == 0ULL) { + return "0"; + } + + const uint8_t STRING_SIZE = 20U; + const char *STRING_TABLE = "0123456789"; + + std::string result; + result.reserve(STRING_SIZE); + + uint64_t q = amount; + + while (q != 0U) { + result += STRING_TABLE[q % BASE_10]; + q /= BASE_10; + } + + std::reverse(result.begin(), result.end()); + + return result; +} + #endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 365dbc4a..c8d41d8e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -36,15 +36,15 @@ include_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) set(COMMON_TEST_SOURCE ${PROJECT_SOURCE_DIR}/common/configuration.cpp ${PROJECT_SOURCE_DIR}/common/fee_policy.cpp - ${PROJECT_SOURCE_DIR}/common/network.cpp) + ${PROJECT_SOURCE_DIR}/common/network.cpp +) # ------------------------------------------------------------------------------ # Crypto set(CRYPTO_TEST_SOURCE - ${PROJECT_SOURCE_DIR}/crypto/curve_ecdsa_sign_null_hash32_privatekey.cpp - ${PROJECT_SOURCE_DIR}/crypto/curve_ecdsa_sign_null_hash32_sha256.cpp - ${PROJECT_SOURCE_DIR}/crypto/curve_ecdsa_sign_null_pk.cpp + ${PROJECT_SOURCE_DIR}/crypto/curve_ecdsa_sign_null_hash.cpp + ${PROJECT_SOURCE_DIR}/crypto/curve_ecdsa_sign_null_privatekey.cpp ${PROJECT_SOURCE_DIR}/crypto/curve_ecdsa_sign.cpp ${PROJECT_SOURCE_DIR}/crypto/curve_publickey.cpp ${PROJECT_SOURCE_DIR}/crypto/curve_verify_invalid.cpp @@ -53,32 +53,28 @@ set(CRYPTO_TEST_SOURCE ${PROJECT_SOURCE_DIR}/crypto/message_sign.cpp ${PROJECT_SOURCE_DIR}/crypto/message_verify.cpp ${PROJECT_SOURCE_DIR}/crypto/message.cpp - ${PROJECT_SOURCE_DIR}/crypto/slot.cpp) - -# ------------------------------------------------------------------------------ -# Defaults - -set(DEFAULTS_TEST_SOURCE - ${PROJECT_SOURCE_DIR}/defaults/static_fees.cpp - ${PROJECT_SOURCE_DIR}/defaults/transaction_types.cpp) + ${PROJECT_SOURCE_DIR}/crypto/slot.cpp +) # ------------------------------------------------------------------------------ # Identities set(IDENTITIES_TEST_SOURCE ${PROJECT_SOURCE_DIR}/identities/address.cpp - ${PROJECT_SOURCE_DIR}/identities/keys_publickey.cpp ${PROJECT_SOURCE_DIR}/identities/keys.cpp + ${PROJECT_SOURCE_DIR}/identities/keys_publickey.cpp ${PROJECT_SOURCE_DIR}/identities/privatekey.cpp ${PROJECT_SOURCE_DIR}/identities/publickey.cpp - ${PROJECT_SOURCE_DIR}/identities/wif.cpp) + ${PROJECT_SOURCE_DIR}/identities/wif.cpp +) # ------------------------------------------------------------------------------ # Managers -set(MANAGERS_SOURCE +set(MANAGERS_TEST_SOURCE ${PROJECT_SOURCE_DIR}/managers/fee_manager.cpp - ${PROJECT_SOURCE_DIR}/managers/network_manager.cpp) + ${PROJECT_SOURCE_DIR}/managers/network_manager.cpp +) # ------------------------------------------------------------------------------ # Networks @@ -86,29 +82,85 @@ set(MANAGERS_SOURCE set(NETWORKS_TEST_SOURCE ${PROJECT_SOURCE_DIR}/networks/devnet.cpp ${PROJECT_SOURCE_DIR}/networks/mainnet.cpp - ${PROJECT_SOURCE_DIR}/networks/testnet.cpp) + ${PROJECT_SOURCE_DIR}/networks/testnet.cpp +) + +# ------------------------------------------------------------------------------ +# Transactions Test Sources +# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ -# Transactions +# Builders -set(TRANSACTIONS_TEST_SOURCE - ${PROJECT_SOURCE_DIR}/transactions/builder_transfer_custom_network.cpp - ${PROJECT_SOURCE_DIR}/transactions/builder.cpp - ${PROJECT_SOURCE_DIR}/transactions/deserializer_delegate_registration.cpp - ${PROJECT_SOURCE_DIR}/transactions/deserializer_multi_signature_registration.cpp - ${PROJECT_SOURCE_DIR}/transactions/deserializer_second_signature_registration.cpp - ${PROJECT_SOURCE_DIR}/transactions/deserializer_transfer.cpp - ${PROJECT_SOURCE_DIR}/transactions/deserializer_vote.cpp +set(TRANSACTIONS_BUILDERS_TEST_SOURCE + ${PROJECT_SOURCE_DIR}/transactions/builders/transfer.cpp + ${PROJECT_SOURCE_DIR}/transactions/builders/second_signature.cpp + ${PROJECT_SOURCE_DIR}/transactions/builders/delegate_registration.cpp + ${PROJECT_SOURCE_DIR}/transactions/builders/vote.cpp + ${PROJECT_SOURCE_DIR}/transactions/builders/ipfs.cpp + ${PROJECT_SOURCE_DIR}/transactions/builders/multi_payment.cpp + ${PROJECT_SOURCE_DIR}/transactions/builders/htlc_lock.cpp + ${PROJECT_SOURCE_DIR}/transactions/builders/htlc_claim.cpp + ${PROJECT_SOURCE_DIR}/transactions/builders/htlc_refund.cpp +) + +# ------------------------------------------------------------------------------ +# Defaults + +set(TRANSACTIONS_DEFAULTS_TEST_SOURCE + ${PROJECT_SOURCE_DIR}/transactions/defaults/fees_types.cpp +) + + +# ------------------------------------------------------------------------------ +# Serialization/Deserialization + +set(TRANSACTIONS_SERDE_TEST_SOURCE + ${PROJECT_SOURCE_DIR}/transactions/deserializer.cpp ${PROJECT_SOURCE_DIR}/transactions/serializer.cpp - ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array_type_0.cpp - ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array_type_1.cpp - ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array_type_2.cpp - ${PROJECT_SOURCE_DIR}/transactions/transaction_to_array_type_3.cpp - ${PROJECT_SOURCE_DIR}/transactions/transaction_to_json_type_0.cpp - ${PROJECT_SOURCE_DIR}/transactions/transaction_to_json_type_1.cpp - ${PROJECT_SOURCE_DIR}/transactions/transaction_to_json_type_2.cpp - ${PROJECT_SOURCE_DIR}/transactions/transaction_to_json_type_3.cpp - ${PROJECT_SOURCE_DIR}/transactions/transaction.cpp) +) + +# ------------------------------------------------------------------------------ +# v1 Types + +set(TRANSACTIONS_TYPES_V1_TEST_SOURCE + ${PROJECT_SOURCE_DIR}/transactions/types/transfer_v1.cpp + ${PROJECT_SOURCE_DIR}/transactions/types/second_signature_v1.cpp + ${PROJECT_SOURCE_DIR}/transactions/types/delegate_registration_v1.cpp + ${PROJECT_SOURCE_DIR}/transactions/types/vote_v1.cpp +) + +# ------------------------------------------------------------------------------ +# Types + +set(TRANSACTIONS_TYPES_TEST_SOURCE + ${TRANSACTIONS_TYPES_V1_SOURCE} + ${PROJECT_SOURCE_DIR}/transactions/types/transfer.cpp + ${PROJECT_SOURCE_DIR}/transactions/types/second_signature.cpp + ${PROJECT_SOURCE_DIR}/transactions/types/delegate_registration.cpp + ${PROJECT_SOURCE_DIR}/transactions/types/vote.cpp + # ${PROJECT_SOURCE_DIR}/transactions/types/multi_signature.cpp + ${PROJECT_SOURCE_DIR}/transactions/types/ipfs.cpp + ${PROJECT_SOURCE_DIR}/transactions/types/multi_payment.cpp + ${PROJECT_SOURCE_DIR}/transactions/types/delegate_resignation.cpp + ${PROJECT_SOURCE_DIR}/transactions/types/htlc_lock.cpp + ${PROJECT_SOURCE_DIR}/transactions/types/htlc_claim.cpp + ${PROJECT_SOURCE_DIR}/transactions/types/htlc_refund.cpp +) + +# ------------------------------------------------------------------------------ +# Transactions Test Source Files + +set(TRANSACTIONS_TEST_SOURCE + ${TRANSACTIONS_BUILDERS_TEST_SOURCE} + ${TRANSACTIONS_DEFAULTS_TEST_SOURCE} + ${TRANSACTIONS_SERDE_TEST_SOURCE} + ${TRANSACTIONS_TYPES_V1_TEST_SOURCE} + ${TRANSACTIONS_TYPES_TEST_SOURCE} + ${TRANSACTIONS_TRANSACTION_TEST_SOURCE} + ${PROJECT_SOURCE_DIR}/transactions/transaction.cpp + ${PROJECT_SOURCE_DIR}/transactions/transaction_v1.cpp +) # ------------------------------------------------------------------------------ # Utils @@ -117,20 +169,22 @@ set(UTILS_TEST_SOURCE ${PROJECT_SOURCE_DIR}/utils/base58.cpp ${PROJECT_SOURCE_DIR}/utils/crypto_helpers.cpp ${PROJECT_SOURCE_DIR}/utils/hex.cpp - ${PROJECT_SOURCE_DIR}/utils/str.cpp) + ${PROJECT_SOURCE_DIR}/utils/packing.cpp + ${PROJECT_SOURCE_DIR}/utils/str.cpp +) # ------------------------------------------------------------------------------ -# ARK C++ Library Source +# ARK C++ Library Test Source set(ARK_TEST_SOURCE ${COMMON_TEST_SOURCE} ${CRYPTO_TEST_SOURCE} - ${DEFAULTS_TEST_SOURCE} ${IDENTITIES_TEST_SOURCE} ${MANAGERS_TEST_SOURCE} ${NETWORKS_TEST_SOURCE} ${TRANSACTIONS_TEST_SOURCE} - ${UTILS_TEST_SOURCE}) + ${UTILS_TEST_SOURCE} +) # ------------------------------------------------------------------------------ @@ -149,16 +203,16 @@ add_test(NAME test COMMAND ${PROJECT_NAME}) # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ -# Coverage 2:2 +# Coverage # ------------------------------------------------------------------------------ if (CMAKE_BUILD_TYPE STREQUAL "Coverage") include("${CMAKE_SOURCE_DIR}/cmake/CodeCoverage.cmake") - setup_target_for_coverage(${PROJECT_NAME}_coverage ${PROJECT_NAME} coverage) + setup_target_for_coverage(${PROJECT_NAME}_coverage ${PROJECT_NAME}_tests coverage) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage") -endif() #CMAKE_BUILD_TYPE STREQUAL "Coverage" +endif() #CMAKE_BUILD_TYPE STREQUAL "Coverage" # ------------------------------------------------------------------------------ diff --git a/test/common/configuration.cpp b/test/common/configuration.cpp index f5d06935..5567ceb2 100644 --- a/test/common/configuration.cpp +++ b/test/common/configuration.cpp @@ -1,65 +1,70 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include #include "common/configuration.hpp" -#include "common/fee_policy.hpp" + +#include "transactions/defaults/fees.hpp" + #include "networks/devnet.hpp" #include "networks/testnet.hpp" + using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; -namespace { +//////////////////////////////////////////////////////////////////////////////// const FeePolicy CustomFeePolicy = { - 900000000ULL, 800000000ULL, 700000000ULL, 600000000ULL, 500000000ULL, - 400000000ULL, 300000000ULL, 200000000ULL, 100000000ULL, 0ULL + 900000000ULL, 800000000ULL, 700000000ULL, 600000000ULL, 500000000ULL, + 400000000ULL, 300000000ULL, 200000000ULL, 100000000ULL, 0ULL }; -} //namespace -/**/ - -TEST(common, configuration_constructor_default) { - Configuration config; - Configuration custom(Devnet, StaticFeePolicy); - ASSERT_TRUE(config == custom); +//////////////////////////////////////////////////////////////////////////////// +TEST(common_configuration, constructor_default) { + Configuration config; + Configuration custom(Devnet, StaticFeePolicy); + ASSERT_TRUE(config == custom); } -/**/ - -TEST(common, configuration_constructor_network) { - Configuration config(Testnet); - ASSERT_TRUE(config.getNetwork() == Testnet); - ASSERT_TRUE(config.getPolicy() == StaticFeePolicy); +//////////////////////////////////////////////////////////////////////////////// +TEST(common_configuration, constructor_network) { + Configuration config(Testnet); + ASSERT_TRUE(config.getNetwork() == Testnet); + ASSERT_TRUE(config.getPolicy() == StaticFeePolicy); } -/**/ - -TEST(common, configuration_constructor_fee_policy) { - Configuration config(CustomFeePolicy); - ASSERT_TRUE(config.getNetwork() == Devnet); - ASSERT_TRUE(config.getPolicy() == CustomFeePolicy); +//////////////////////////////////////////////////////////////////////////////// +TEST(common_configuration, constructor_fee_policy) { + Configuration config(CustomFeePolicy); + ASSERT_TRUE(config.getNetwork() == Devnet); + ASSERT_TRUE(config.getPolicy() == CustomFeePolicy); } -/**/ - -TEST(common, configuration_constructor_network_and_fee_policy) { - Configuration config(Testnet, CustomFeePolicy); - ASSERT_TRUE(config.getNetwork() == Testnet); - ASSERT_TRUE(config.getPolicy() == CustomFeePolicy); +//////////////////////////////////////////////////////////////////////////////// +TEST(common_configuration, constructor_network_and_fee_policy) { + Configuration config(Testnet, CustomFeePolicy); + ASSERT_TRUE(config.getNetwork() == Testnet); + ASSERT_TRUE(config.getPolicy() == CustomFeePolicy); } -/**/ - -TEST(common, configuration_comparison_equals) { - Configuration config; - Configuration custom(Devnet, StaticFeePolicy); - ASSERT_TRUE(config == custom); +//////////////////////////////////////////////////////////////////////////////// +TEST(common_configuration, comparison_equivalent) { + Configuration config; + Configuration custom(Devnet, StaticFeePolicy); + ASSERT_TRUE(config == custom); } -/**/ - -TEST(common, configuration_comparison_not_equal) { - Configuration config; - Configuration custom(Testnet); - ASSERT_TRUE(config != custom); +//////////////////////////////////////////////////////////////////////////////// +TEST(common_configuration, comparison_not_equivalent) { + Configuration config; + Configuration custom(Testnet); + ASSERT_TRUE(config != custom); } diff --git a/test/common/fee_policy.cpp b/test/common/fee_policy.cpp index 1e6a8cef..a2825e80 100644 --- a/test/common/fee_policy.cpp +++ b/test/common/fee_policy.cpp @@ -1,21 +1,32 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include "common/fee_policy.hpp" + + using namespace Ark::Crypto; -namespace { +//////////////////////////////////////////////////////////////////////////////// const FeePolicy CustomFeePolicy = { - 900000000ULL, 800000000ULL, 700000000ULL, 600000000ULL, 500000000ULL, - 400000000ULL, 300000000ULL, 200000000ULL, 100000000ULL, 0ULL + 900000000ULL, 800000000ULL, 700000000ULL, 600000000ULL, 500000000ULL, + 400000000ULL, 300000000ULL, 200000000ULL, 100000000ULL, 0ULL }; -} //namespace -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(common_fee_policy, custom) { + const FeePolicy feePolicy(CustomFeePolicy); -TEST(common, fee_policy_custom) { - const FeePolicy feePolicy(CustomFeePolicy); - for (auto i = 0U; i < CustomFeePolicy.size(); ++i) { - ASSERT_EQ(feePolicy[i], CustomFeePolicy[i]); - }; + uint8_t i = 0U; + for (auto &fee : feePolicy) { + ASSERT_EQ(feePolicy.at(i), fee); + ++i; + } } diff --git a/test/common/network.cpp b/test/common/network.cpp index ae0dd149..d966eff1 100644 --- a/test/common/network.cpp +++ b/test/common/network.cpp @@ -1,16 +1,28 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include "common/network.hpp" + #include "networks/devnet.hpp" + using namespace Ark::Crypto; -namespace { +//////////////////////////////////////////////////////////////////////////////// const Network CustomNetwork { - "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", - 1, 0x53, 0xaa, - "2019-04-12T13:00:00.000Z" + "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", + 1, 0x53, 0xaa, + "2019-04-12T13:00:00.000Z" }; + +//////////////////////////////////////////////////////////////////////////////// const Network InvalidNetwork { "2a44f340d76ffc3df204c5f38cd355b7496c9065a1ade2ef92071436bd72e867", 1, @@ -18,39 +30,34 @@ const Network InvalidNetwork { 0x00, "2017-03-21T13:00:00.000Z" }; -} // namespace - -/**/ -TEST(common, network_constructor) { +//////////////////////////////////////////////////////////////////////////////// +TEST(common_network, copy_constructor) { Network network(CustomNetwork); ASSERT_TRUE(network == CustomNetwork); } -/**/ - -TEST(common, network_get_parameters) { - Network network(Devnet); - ASSERT_STREQ(network.nethash.c_str(), Devnet.nethash.c_str()); - ASSERT_EQ(network.slip44, Devnet.slip44); - ASSERT_EQ(network.wif, Devnet.wif); - ASSERT_EQ(network.version, Devnet.version); - ASSERT_STREQ(network.epoch.c_str(), Devnet.epoch.c_str()); +//////////////////////////////////////////////////////////////////////////////// +TEST(common_network, members) { + Network network(Devnet); + ASSERT_STREQ(Devnet.nethash.c_str(), network.nethash.c_str()); + ASSERT_EQ(Devnet.slip44, network.slip44); + ASSERT_EQ(Devnet.wif, network.wif); + ASSERT_EQ(Devnet.version, network.version); + ASSERT_STREQ(Devnet.epoch.c_str(), network.epoch.c_str()); } -/**/ - -TEST(common, network_comparison_equals) { +//////////////////////////////////////////////////////////////////////////////// +TEST(common_network, comparison_equivalent) { Network network(CustomNetwork); ASSERT_TRUE(network == CustomNetwork); } -/**/ - -TEST(common, network_comparison_not_equal) { - Network network(CustomNetwork); - ASSERT_TRUE(network != Devnet); +//////////////////////////////////////////////////////////////////////////////// +TEST(common_network, comparison_not_equivalent) { + Network network(CustomNetwork); + ASSERT_TRUE(network != Devnet); - Network invalid(InvalidNetwork); - ASSERT_TRUE(invalid != Devnet); + Network invalid(InvalidNetwork); + ASSERT_TRUE(invalid != Devnet); } diff --git a/test/crypto/curve_ecdsa_sign.cpp b/test/crypto/curve_ecdsa_sign.cpp index 04e2f916..dbe13f80 100644 --- a/test/crypto/curve_ecdsa_sign.cpp +++ b/test/crypto/curve_ecdsa_sign.cpp @@ -1,3 +1,11 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" @@ -5,18 +13,21 @@ #include "fixtures/identity.hpp" #include "fixtures/message.hpp" + +#include "test_helpers.h" + using namespace Ark::Crypto; -using namespace fixtures::identity; -using namespace fixtures::message; - -/**/ -TEST(crypto, curve_ecdsa_sign) { - std::vector signature; - Curve::Ecdsa::sign(tMessageSha256Bytes.data(), - tPrivateKeyBytes.data(), - signature); - - for (auto i = 0U; i < signature.size(); ++i) { - ASSERT_EQ(signature.at(i), tMessageSignatureBytes.at(i)); - }; + +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_curve, ecdsa_sign) { + std::vector signature = {}; + signature.resize(SIGNATURE_ECDSA_MAX); + + ASSERT_TRUE(Curve::Ecdsa::sign(fixtures::MessageSha256Bytes.data(), + fixtures::PrivateKeyBytes.data(), + &signature)); + + ASSERT_TRUE(array_cmp(fixtures::MessageSignatureBytes.data(), + signature.data(), + signature.size())); } diff --git a/test/crypto/curve_ecdsa_sign_null_hash.cpp b/test/crypto/curve_ecdsa_sign_null_hash.cpp new file mode 100644 index 00000000..e3414866 --- /dev/null +++ b/test/crypto/curve_ecdsa_sign_null_hash.cpp @@ -0,0 +1,27 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "crypto/curve.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/message.hpp" + +using namespace Ark::Crypto; + +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_curve, ecdsa_sign_null_hash) { + std::vector signature = {}; + Curve::Ecdsa::sign(nullptr, fixtures::PrivateKeyBytes.data(), &signature); + + ASSERT_FALSE(Curve::Ecdsa::verify(fixtures::MessageSha256Bytes.data(), + fixtures::PublicKeyBytes.data(), + signature)); +} diff --git a/test/crypto/curve_ecdsa_sign_null_hash32_privatekey.cpp b/test/crypto/curve_ecdsa_sign_null_hash32_privatekey.cpp deleted file mode 100644 index 640f76ff..00000000 --- a/test/crypto/curve_ecdsa_sign_null_hash32_privatekey.cpp +++ /dev/null @@ -1,22 +0,0 @@ - -#include "gtest/gtest.h" - -#include "crypto/curve.hpp" - -#include "fixtures/identity.hpp" -#include "fixtures/message.hpp" -using namespace Ark::Crypto; -using namespace fixtures::identity; -using namespace fixtures::message; - -/**/ -TEST(crypto, curve_ecdsa_sign_null_hash32_privatekey) { - std::vector signature; - Curve::Ecdsa::sign(nullptr, - tPrivateKeyBytes.data(), - signature); - ASSERT_FALSE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), - tPublicKeyBytes.data(), - signature)); - -} diff --git a/test/crypto/curve_ecdsa_sign_null_hash32_sha256.cpp b/test/crypto/curve_ecdsa_sign_null_hash32_sha256.cpp deleted file mode 100644 index 3d571aa7..00000000 --- a/test/crypto/curve_ecdsa_sign_null_hash32_sha256.cpp +++ /dev/null @@ -1,20 +0,0 @@ - -#include "gtest/gtest.h" - -#include "crypto/curve.hpp" - -#include "fixtures/identity.hpp" -#include "fixtures/message.hpp" -using namespace Ark::Crypto; -using namespace fixtures::identity; -using namespace fixtures::message; - -TEST(crypto, curve_ecdsa_sign_null_hash32_sha256) { - std::vector signature; - Curve::Ecdsa::sign(tMessageSha256Bytes.data(), - nullptr, - signature); - ASSERT_FALSE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), - tPublicKeyBytes.data(), - signature)); -} diff --git a/test/crypto/curve_ecdsa_sign_null_pk.cpp b/test/crypto/curve_ecdsa_sign_null_pk.cpp deleted file mode 100644 index d3ee0deb..00000000 --- a/test/crypto/curve_ecdsa_sign_null_pk.cpp +++ /dev/null @@ -1,20 +0,0 @@ - -#include "gtest/gtest.h" - -#include "crypto/curve.hpp" - -#include "fixtures/identity.hpp" -#include "fixtures/message.hpp" -using namespace Ark::Crypto; -using namespace fixtures::identity; -using namespace fixtures::message; - -TEST(crypto, curve_ecdsa_sign_null_pk) { - std::vector signature; - Curve::Ecdsa::sign(tMessageSha256Bytes.data(), - nullptr, - signature); - ASSERT_FALSE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), - tPublicKeyBytes.data(), - signature)); -} diff --git a/test/crypto/curve_ecdsa_sign_null_privatekey.cpp b/test/crypto/curve_ecdsa_sign_null_privatekey.cpp new file mode 100644 index 00000000..7cdb8081 --- /dev/null +++ b/test/crypto/curve_ecdsa_sign_null_privatekey.cpp @@ -0,0 +1,27 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "crypto/curve.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/message.hpp" + +using namespace Ark::Crypto; + +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_curve, ecdsa_sign_null_privatekey) { + std::vector signature = {}; + Curve::Ecdsa::sign(fixtures::MessageSha256Bytes.data(), nullptr, &signature); + + ASSERT_FALSE(Curve::Ecdsa::verify(fixtures::MessageSha256Bytes.data(), + fixtures::PublicKeyBytes.data(), + signature)); +} diff --git a/test/crypto/curve_publickey.cpp b/test/crypto/curve_publickey.cpp index a705d7cb..9227c382 100644 --- a/test/crypto/curve_publickey.cpp +++ b/test/crypto/curve_publickey.cpp @@ -1,3 +1,11 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" @@ -5,56 +13,57 @@ #include "fixtures/identity.hpp" #include "fixtures/message.hpp" -using namespace Ark::Crypto; -using namespace fixtures::identity; -using namespace fixtures::message; -/**/ +#include "test_helpers.h" -TEST(crypto, curve_publickey_compute) { - const auto publicKey = Curve::PublicKey::compute(tPrivateKeyBytes.data()); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; ++i) { - ASSERT_EQ(publicKey.at(i), tPublicKeyBytes.at(i)); - }; -} +using namespace Ark::Crypto; -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_curve, publickey_compute) { + const auto publicKey = Curve::PublicKey::compute(fixtures::PrivateKeyBytes.data()); -TEST(crypto, curve_publickey_compute_invalid) { - const auto publicKey = - Curve::PublicKey::compute(invalid::tPrivateKeyBytes.data()); - for (auto& e : publicKey) { - ASSERT_EQ(e, 0); - }; + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + publicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_curve, publickey_compute_invalid) { + const auto publicKey = Curve::PublicKey::compute( + fixtures::invalid::PrivateKeyBytes.data()); -TEST(crypto, curve_publickey_compress) { - const auto compressed = - Curve::PublicKey::compress(tUncompressedPublicKeyBytes.data()); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; ++i) { - ASSERT_EQ(compressed.at(i), tPublicKeyBytes.at(i)); - }; + for (auto &e : publicKey) { + ASSERT_EQ(0U, e); + } } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_curve, publickey_compress) { + const auto compressed = Curve::PublicKey::compress( + fixtures::PublicKeyUncompressedBytes.data()); -TEST(crypto, curve_publickey_decompress) { - const auto decompressed = Curve::PublicKey::decompress(tPublicKeyBytes.data()); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; ++i) { - ASSERT_EQ(decompressed.at(i), tUncompressedPublicKeyBytes.at(i)); - }; + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + compressed.data(), + compressed.size())); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_curve, publickey_decompress) { + const auto decompressed = Curve::PublicKey::decompress( + fixtures::PublicKeyBytes.data()); -TEST(crypto, curve_publickey_validate) { - ASSERT_TRUE(Curve::PublicKey::validate(tPublicKeyBytes.data())); + ASSERT_TRUE(array_cmp(fixtures::PublicKeyUncompressedBytes.data(), + decompressed.data(), + decompressed.size())); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_curve, publickey_validate) { + ASSERT_TRUE(Curve::PublicKey::validate(fixtures::PublicKeyBytes.data())); +} -TEST(crypto, curve_publickey_validate_invalid) { - ASSERT_FALSE(Curve::PublicKey::validate(invalid::tPublicKeyBytes.data())); +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_curve, publickey_validate_invalid) { + ASSERT_FALSE(Curve::PublicKey::validate( + fixtures::invalid::PublicKeyBytes.data())); } diff --git a/test/crypto/curve_verify_invalid.cpp b/test/crypto/curve_verify_invalid.cpp index 7b4c7839..33686ed6 100644 --- a/test/crypto/curve_verify_invalid.cpp +++ b/test/crypto/curve_verify_invalid.cpp @@ -1,3 +1,11 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" @@ -5,15 +13,23 @@ #include "fixtures/identity.hpp" #include "fixtures/message.hpp" + using namespace Ark::Crypto; -using namespace fixtures::identity; -using namespace fixtures::message; -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_curve, verify_invalid) { + std::vector temp = { + fixtures::MessageSignatureBytes.begin(), + fixtures::MessageSignatureBytes.end() + }; + + // Invalid PublicKey + ASSERT_FALSE(Curve::Ecdsa::verify(fixtures::MessageSha256Bytes.data(), + fixtures::invalid::PublicKeyBytes.data(), + temp)); -TEST(crypto, crypto_verify_invalid) { - ASSERT_FALSE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), - invalid::tPublicKeyBytes.data(), - { tMessageSignatureBytes.begin(), - tMessageSignatureBytes.end() })); + // Null Hash + ASSERT_FALSE(Curve::Ecdsa::verify(nullptr, + fixtures::invalid::PublicKeyBytes.data(), + temp)); } diff --git a/test/crypto/curve_verify_valid.cpp b/test/crypto/curve_verify_valid.cpp index 4099ca16..ae50fce0 100644 --- a/test/crypto/curve_verify_valid.cpp +++ b/test/crypto/curve_verify_valid.cpp @@ -1,3 +1,11 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" @@ -5,13 +13,16 @@ #include "fixtures/identity.hpp" #include "fixtures/message.hpp" + using namespace Ark::Crypto; -using namespace fixtures::identity; -using namespace fixtures::message; +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_curve, verify_valid) { + std::vector temp = { + fixtures::MessageSignatureBytes.begin(), + fixtures::MessageSignatureBytes.end() + }; -TEST(crypto, crypto_verify_valid) { - ASSERT_TRUE(Curve::Ecdsa::verify(tMessageSha256Bytes.data(), - tPublicKeyBytes.data(), - { tMessageSignatureBytes.begin(), - tMessageSignatureBytes.end() })); + ASSERT_TRUE(Curve::Ecdsa::verify(fixtures::MessageSha256Bytes.data(), + fixtures::PublicKeyBytes.data(), + temp)); } diff --git a/test/crypto/hash.cpp b/test/crypto/hash.cpp index 5c063204..500d64c8 100644 --- a/test/crypto/hash.cpp +++ b/test/crypto/hash.cpp @@ -1,3 +1,11 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" @@ -5,23 +13,25 @@ #include "fixtures/identity.hpp" #include "fixtures/message.hpp" + +#include "test_helpers.h" + using namespace Ark::Crypto; -using namespace fixtures::identity; -using namespace fixtures::message; - -TEST(crypto, hash_ripemd_hash_160) { - auto ripeHash = Hash::ripemd160(tPublicKeyBytes.data()); - for (auto i = 0U; i < HASH_20_BYTE_LEN; ++i) { - ASSERT_EQ(ripeHash.at(i), tAddressBytes.at(i)); - }; + +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_hash_ripemd, hash_160) { + auto ripeHash = Hash::ripemd160(fixtures::PublicKeyBytes.data()); + ASSERT_TRUE(array_cmp(fixtures::AddressBytes.data(), + ripeHash.data(), + HASH_20_LEN)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_hash_sha256, hash_256) { + auto shaHash = Hash::sha256(fixtures::MessageBytes.data(), + fixtures::MessageBytes.size()); -TEST(crypto, hash_sha_hash_256) { - auto shaHash = Hash::sha256(tMessageBytes.data(), - tMessageBytes.size()); - for (auto i = 0U; i < HASH_32_BYTE_LEN; ++i) { - ASSERT_EQ(shaHash.at(i), tMessageSha256Bytes.at(i)); - }; + ASSERT_TRUE(array_cmp(fixtures::MessageSha256Bytes.data(), + shaHash.data(), + HASH_32_LEN)); } diff --git a/test/crypto/message.cpp b/test/crypto/message.cpp index 84975de2..a555f628 100644 --- a/test/crypto/message.cpp +++ b/test/crypto/message.cpp @@ -1,3 +1,11 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" @@ -7,26 +15,26 @@ #include "fixtures/identity.hpp" #include "fixtures/message.hpp" + #include "utils/hex.hpp" -using namespace Ark::Crypto; -using namespace fixtures::identity; -using namespace fixtures::message; +using namespace Ark::Crypto; -TEST(crypto, message_to_array) { - Message message; - message.sign(tMessageString, tPassphrase); - auto messageArray = message.toArray(); +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_message, to_map) { + Message message; + message.sign(fixtures::MessageString, fixtures::Passphrase); + auto map = message.toMap(); - ASSERT_STREQ(tMessageString, messageArray["message"].c_str()); - ASSERT_STREQ(tPublicKeyHex, messageArray["publickey"].c_str()); - ASSERT_STREQ(tSignatureString, messageArray["signature"].c_str()); + ASSERT_STREQ(fixtures::MessageString, map["message"].c_str()); + ASSERT_STREQ(fixtures::PublicKeyHex, map["publickey"].c_str()); + ASSERT_STREQ(fixtures::MessageSignatureString, map["signature"].c_str()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_message, to_json) { + Message message; + message.sign(fixtures::MessageString, fixtures::Passphrase); -TEST(crypto, message_to_json) { - Message message; - message.sign(tMessageString, tPassphrase); - ASSERT_STREQ(message.toJson().c_str(), tMessageJsonString); + ASSERT_STREQ(fixtures::MessageJsonString, message.toJson().c_str()); } diff --git a/test/crypto/message_sign.cpp b/test/crypto/message_sign.cpp index 9539a6d7..316f1bce 100644 --- a/test/crypto/message_sign.cpp +++ b/test/crypto/message_sign.cpp @@ -1,3 +1,11 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" @@ -7,16 +15,18 @@ #include "fixtures/identity.hpp" #include "fixtures/message.hpp" + #include "utils/hex.hpp" + using namespace Ark::Crypto; -using namespace fixtures::identity; -using namespace fixtures::message; -TEST(crypto, message_sign) { - Message message; - message.sign(tMessageString, tPassphrase); +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_message, sign) { + Message message; + message.sign(fixtures::MessageString, fixtures::Passphrase); + + const auto signatureString = BytesToHex(message.signature); - ASSERT_STREQ(BytesToHex(message.signature.begin(), - message.signature.end()).c_str(), - tSignatureString); + ASSERT_STREQ(fixtures::MessageSignatureString, + signatureString.c_str()); } diff --git a/test/crypto/message_verify.cpp b/test/crypto/message_verify.cpp index 78086ae7..922f0a07 100644 --- a/test/crypto/message_verify.cpp +++ b/test/crypto/message_verify.cpp @@ -1,3 +1,11 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" @@ -7,15 +15,16 @@ #include "fixtures/identity.hpp" #include "fixtures/message.hpp" + #include "utils/hex.hpp" + using namespace Ark::Crypto; -using namespace fixtures::identity; -using namespace fixtures::message; - -TEST(crypto, message_verify) { - Message message(tMessageString, - tPublicKeyBytes, - { tMessageSignatureBytes.begin(), - tMessageSignatureBytes.end() }); - ASSERT_TRUE(message.verify()); + +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_message, verify) { + Message message(fixtures::MessageString, + fixtures::PublicKeyBytes.data(), + fixtures::MessageSignatureBytes.data()); + + ASSERT_TRUE(message.verify()); } diff --git a/test/crypto/slot.cpp b/test/crypto/slot.cpp index ca5a013e..f35894ee 100644 --- a/test/crypto/slot.cpp +++ b/test/crypto/slot.cpp @@ -1,3 +1,11 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" @@ -7,20 +15,21 @@ using namespace Ark::Crypto; -TEST(crypto, slots_epoch) { - const auto arkEpoch = 1490101200ULL; - ASSERT_EQ(arkEpoch, Slot::epoch(Devnet)); +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_slots, epoch) { + const auto arkEpoch = 1490101200ULL; + ASSERT_EQ(arkEpoch, Slot::epoch(Devnet)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(crypto_slots, time) { + // 28 Jan 2019(in seconds) - Ark Epoch (seconds) + const auto testTime = (1548725761ULL - 1490101200ULL); + const auto slotTime = Slot::time(Devnet); -TEST(utilities, slots_time) { - // 28 Jan 2019(in seconds) - Ark Epoch (seconds) - const auto testTime = (1548725761ULL - 1490101200ULL); - const auto slotTime = Slot::time(Devnet); + // test that slot-time is more recent than 'testTime', + ASSERT_GT(slotTime , testTime); - // test that slot-time is more recent than 'testTime', - ASSERT_GT(slotTime , testTime); - // also check that the 'slotTime' is not too large (e.g. from an overflow). - ASSERT_LT(slotTime, (testTime) + 315360000ULL); // 315360000s = 10yrs + // also check that the 'slotTime' is not too large (e.g. from an overflow). + ASSERT_LT(slotTime, (testTime) + 315360000ULL); // 315360000s = 10yrs } diff --git a/test/defaults/static_fees.cpp b/test/defaults/static_fees.cpp deleted file mode 100644 index db2f519c..00000000 --- a/test/defaults/static_fees.cpp +++ /dev/null @@ -1,41 +0,0 @@ - -#include "gtest/gtest.h" - -#include - -#include "defaults/static_fees.hpp" -using namespace Ark::Crypto; - -namespace { -constexpr const uint64_t kTransfer = 10000000ULL; -constexpr const uint64_t kSecondSignatureRegistration = 500000000ULL; -constexpr const uint64_t kDelegateRegistration = 2500000000ULL; -constexpr const uint64_t kVote = 100000000ULL; -constexpr const uint64_t kMultiSignatureRegistration = 500000000ULL; -constexpr const uint64_t kIpfs = 0ULL; -constexpr const uint64_t kTimelockTransfer = 0ULL; -constexpr const uint64_t kMultiPayment = 0ULL; -constexpr const uint64_t kDelegateResignation = 2500000000ULL; -constexpr const uint8_t TransferType = 0; -constexpr const uint8_t SecondSignatureRegistrationType = 1; -constexpr const uint8_t DelegateRegistrationType = 2; -constexpr const uint8_t VoteType = 3; -constexpr const uint8_t MultiSignatureRegistrationType = 4; -constexpr const uint8_t IpfsType = 5; -constexpr const uint8_t TimelockTransferType = 6; -constexpr const uint8_t MultiPaymentType = 7; -constexpr const uint8_t DelegateResignationType = 8; -} //namespace - -TEST(defaults, fees_static) { // NOLINT - const auto feePolicy = StaticFeePolicy; - ASSERT_EQ(kTransfer, feePolicy.at(TransferType)); - ASSERT_EQ(kSecondSignatureRegistration, feePolicy.at(SecondSignatureRegistrationType)); - ASSERT_EQ(kDelegateRegistration, feePolicy.at(DelegateRegistrationType)); - ASSERT_EQ(kVote, feePolicy.at(VoteType)); - ASSERT_EQ(kMultiSignatureRegistration, feePolicy.at(MultiSignatureRegistrationType)); - ASSERT_EQ(kIpfs, feePolicy.at(IpfsType)); - ASSERT_EQ(kTimelockTransfer, feePolicy.at(TimelockTransferType)); - ASSERT_EQ(kMultiPayment, feePolicy.at(MultiPaymentType)); - ASSERT_EQ(kDelegateResignation, feePolicy.at(DelegateResignationType)); -} diff --git a/test/defaults/transaction_types.cpp b/test/defaults/transaction_types.cpp deleted file mode 100644 index 976f7bd0..00000000 --- a/test/defaults/transaction_types.cpp +++ /dev/null @@ -1,33 +0,0 @@ - -#include "gtest/gtest.h" - -#include - -#include "defaults/transaction_types.hpp" -using namespace Ark::Crypto; - -namespace { -constexpr const uint8_t TransferType = 0; -constexpr const uint8_t SecondSignatureRegistrationType = 1; -constexpr const uint8_t DelegateRegistrationType = 2; -constexpr const uint8_t VoteType = 3; -constexpr const uint8_t MultiSignatureRegistrationType = 4; -constexpr const uint8_t IpfsType = 5; -constexpr const uint8_t TimelockTransferType = 6; -constexpr const uint8_t MultiPaymentType = 7; -constexpr const uint8_t DelegateResignationType = 8; -} //namespace - -/**/ - -TEST(defaults, transaction_types) { // NOLINT - ASSERT_EQ(TransferType, TransactionTypes::Transfer); - ASSERT_EQ(SecondSignatureRegistrationType, TransactionTypes::SecondSignatureRegistration); - ASSERT_EQ(DelegateRegistrationType, TransactionTypes::DelegateRegistration); - ASSERT_EQ(VoteType, TransactionTypes::Vote); - ASSERT_EQ(MultiSignatureRegistrationType, TransactionTypes::MultiSignatureRegistration); - ASSERT_EQ(IpfsType, TransactionTypes::Ipfs); - ASSERT_EQ(TimelockTransferType, TransactionTypes::TimelockTransfer); - ASSERT_EQ(MultiPaymentType, TransactionTypes::MultiPayment); - ASSERT_EQ(DelegateResignationType, TransactionTypes::DelegateResignation); -} diff --git a/test/fixtures/identity.hpp b/test/fixtures/identity.hpp index 78053ae7..2e2285f4 100644 --- a/test/fixtures/identity.hpp +++ b/test/fixtures/identity.hpp @@ -7,15 +7,18 @@ * file that was distributed with this source code. **/ -#ifndef CRYPTO_FIXTURES_IDENTITY_HPP -#define CRYPTO_FIXTURES_IDENTITY_HPP +#ifndef ARK_FIXTURES_IDENTITY_HPP +#define ARK_FIXTURES_IDENTITY_HPP #include +#include "interfaces/constants.h" + namespace Ark { namespace Crypto { -namespace fixtures { +namespace fixtures { // NOLINT +//////////////////////////////////////////////////////////////////////////////// // ARK Identity Fixtures // // { @@ -31,64 +34,79 @@ namespace fixtures { // } // https://github.com/ARKEcosystem/core/blob/develop/__tests__/unit/crypto/identities/fixture.json // -namespace identity { // NOLINT - // ARK Identities: Hex Strings - constexpr static const char* tPrivateKeyHex = "d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712"; - constexpr static const char* tPublicKeyHex = "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192"; - constexpr static const char* tAddressString = "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib"; - constexpr static const char* tWifString = "SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA"; - constexpr static const char* tPassphrase = "this is a top secret passphrase"; - - // ARK Identities: Byte Arrays - static constexpr const std::array tPrivateKeyBytes = {{ - 216, 131, 156, 36, 50, 191, 208, 166, 126, 241, 10, 128, 75, 169, 145, 234, - 187, 161, 159, 21, 74, 61, 112, 121, 23, 104, 29, 69, 130, 42, 87, 18 - }}; - - static constexpr const std::array tPublicKeyBytes = {{ - 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, 57, 79, 134, 53, - 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, 231, 240, 174, 209, 146 - }}; - - static constexpr const std::array tUncompressedPublicKeyBytes = {{ - 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, 57, 79, 134, 53, - 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, 231, 240, 174, 209, 146, - 250, 41, 206, 0, 251, 142, 217, 47, 52, 246, 140, 0, 24, 241, 45, 91, - 104, 193, 96, 144, 128, 171, 175, 51, 23, 70, 214, 216, 108, 96, 57, 229 - }}; - - static constexpr const std::array tAddressBytes = {{ // RIPEMD160 Hash bytes - 9, 149, 117, 2, 7, 236, 175, 12, 207, 37, 28, 18, 101, 185, 42, 216, 79, 85, 54, 98 - }}; - - static constexpr const std::array tPassphraseBytes = {{ // RIPEMD160 Hash bytes - 116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 111, 112, 32, 115, - 101, 99, 114, 101, 116, 32, 112, 97, 115, 115, 112, 104, 114, 97, 115, 101 - }}; - - // ARK Identities: Base58 Versions - const uint8_t tAddressVersion = 0x1E; // Base58: 'D' character - const uint8_t tWifVersion = 0xaa; // Base58: 'S' character - - // ARK Identities: Invalid Fixtures - namespace invalid { - // Invalid characters - - // 'h' and 'z' are not valid Hex characters. - constexpr static const char* tPrivateKeyHex = "d8839c2432bfd0a67ef10ah04ba991eabba19f154a3d707917681d45822a5712"; - constexpr static const char* tPublicKeyHex = "034151a3ec46b5670a682b0a63394f863587d1bcz7483b1b6c70eb58e7f0aed192"; - - // '0', 'O', 'l', and 'I' are not valid Base58 characters. - constexpr static const char* tAddressString = "D61mfSggzbvOgTUe6JhYKH2doHaqJ3Dyib"; - constexpr static const char* tWifString = "SGq4xLgZKCGxslbjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA"; - - // Empty arrays - static constexpr const std::array tPrivateKeyBytes = {{}}; - static constexpr const std::array tPublicKeyBytes = {{}}; - } // namespace invalid - -} // namespace identity +//////////////////////////////////////////////////////////////////////////////// +// ARK Identities: Hex Strings +constexpr auto PrivateKeyHex = "d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712"; +constexpr auto PublicKeyHex = "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192"; +constexpr auto AddressString = "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib"; +constexpr auto WifString = "SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA"; +constexpr auto Passphrase = "this is a top secret passphrase"; +constexpr auto SecondPassphrase = "this is a top secret passphrase too"; + +//////////////////////////////////////////////////////////////////////////////// +// ARK Identities: Byte Arrays + +//////////////////////////////////////////////////////////////////////////////// +constexpr std::array PrivateKeyBytes = { + { 216, 131, 156, 36, 50, 191, 208, 166, 126, 241, 10, 128, 75, 169, 145, 234, + 187, 161, 159, 21, 74, 61, 112, 121, 23, 104, 29, 69, 130, 42, 87, 18 }}; + +//////////////////////////////////////////////////////////////////////////////// +// PublicKeyBytes +constexpr std::array PublicKeyBytes = { + { 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, 57, 79, 134, 53, + 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, 231, 240, 174, 209, + 146 }}; + +//////////////////////////////////////////////////////////////////////////////// +// Uncompressed PublicKey Bytes +constexpr std::array PublicKeyUncompressedBytes = { + { 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, 57, 79, 134, 53, 135, + 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, 231, 240, 174, 209, 146, 250, + 41, 206, 0, 251, 142, 217, 47, 52, 246, 140, 0, 24, 241, 45, 91, 104, 193, + 96, 144, 128, 171, 175, 51, 23, 70, 214, 216, 108, 96, 57, 229 }}; + +//////////////////////////////////////////////////////////////////////////////// +// RIPEMD160 Hash bytes +constexpr std::array AddressBytes = { + { 9, 149, 117, 2, 7, 236, 175, 12, 207, 37, 28, 18, 101, 185, 42, 216, 79, + 85, 54, 98 }}; + +//////////////////////////////////////////////////////////////////////////////// +// RIPEMD160 Hash bytes +constexpr std::array PassphraseBytes = {{ + 116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 111, 112, 32, 115, + 101, 99, 114, 101, 116, 32, 112, 97, 115, 115, 112, 104, 114, 97, 115, 101 +}}; + +//////////////////////////////////////////////////////////////////////////////// +// ARK Identities: Base58 Versions +constexpr uint8_t AddressVersion = 0x1E; // Base58: 'D' character +constexpr uint8_t WifVersion = 0xaa; // Base58: 'S' character + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// ARK Identities: Invalid Fixtures +namespace invalid { + +//////////////////////////////////////////////////////////////////////////////// +// Invalid characters +// 'h' and 'z' are not valid Hex characters. +constexpr auto PrivateKeyHex = "d8839c2432bfd0a67ef10ah04ba991eabba19f154a3d707917681d45822a5712"; +constexpr auto PublicKeyHex = "034151a3ec46b5670a682b0a63394f863587d1bcz7483b1b6c70eb58e7f0aed192"; + +//////////////////////////////////////////////////////////////////////////////// +// '0', 'O', 'l', and 'I' are not valid Base58 characters. +constexpr auto AddressString = "D61mfSggzbvOgTUe6JhYKH2doHaqJ3Dyib"; +constexpr auto WifString = "SGq4xLgZKCGxslbjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA"; + +//////////////////////////////////////////////////////////////////////////////// +// Empty arrays +constexpr std::array PrivateKeyBytes = {{}}; +constexpr std::array PublicKeyBytes = {{}}; + +} // namespace invalid } // fixtures } // Crypto } // Ark diff --git a/test/fixtures/message.hpp b/test/fixtures/message.hpp index 7a408f2d..16d8e6fa 100644 --- a/test/fixtures/message.hpp +++ b/test/fixtures/message.hpp @@ -11,12 +11,14 @@ #define CRYPTO_FIXTURES_MESSAGE_HPP #include -#include + +#include "interfaces/constants.h" namespace Ark { namespace Crypto { -namespace fixtures { +namespace fixtures { // NOLINT +//////////////////////////////////////////////////////////////////////////////// // ARK Message Fixtures // // { @@ -29,31 +31,32 @@ namespace fixtures { // } // https://raw.githubusercontent.com/ArkEcosystem/swift-crypto/develop/Crypto/CryptoTests/Fixtures/message.json // -namespace message { // NOLINT - // ARK Message: Strings - constexpr static const char* tMessageString = "Hello World"; - constexpr static const char* tSignatureString = "304402200fb4adddd1f1d652b544ea6ab62828a0a65b712ed447e2538db0caebfa68929e02205ecb2e1c63b29879c2ecf1255db506d671c8b3fa6017f67cfd1bf07e6edd1cc8"; - constexpr static const char* tSignatureStringSpaces = "30 44 02 20 0f b4 ad dd d1 f1 d6 52 b5 44 ea 6a b6 28 28 a0 a6 5b 71 2e d4 47 e2 53 8d b0 ca eb fa 68 92 9e 02 20 5e cb 2e 1c 63 b2 98 79 c2 ec f1 25 5d b5 06 d6 71 c8 b3 fa 60 17 f6 7c fd 1b f0 7e 6e dd 1c c8"; - constexpr static const char* tMessageJsonString = R"({"message":"Hello World","publickey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","signature":"304402200fb4adddd1f1d652b544ea6ab62828a0a65b712ed447e2538db0caebfa68929e02205ecb2e1c63b29879c2ecf1255db506d671c8b3fa6017f67cfd1bf07e6edd1cc8"})"; - - // ARK Message: Bytes - static constexpr const std::array tMessageBytes = {{ - 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 - }}; - - static constexpr const std::array tMessageSha256Bytes = {{ - 165, 145, 166, 212, 11, 244, 32, 64, 74, 1, 23, 51, 207, 183, 177, 144, - 214, 44, 101, 191, 11, 205, 163, 43, 87, 178, 119, 217, 173, 159, 20, 110 - }}; - - static constexpr const std::array tMessageSignatureBytes = {{ - 48, 68, 2, 32, 15, 180, 173, 221, 209, 241, 214, 82, 181, 68, 234, 106, 182, 40, - 40, 160, 166, 91, 113, 46, 212, 71, 226, 83, 141, 176, 202, 235, 250, 104, 146, 158, - 2, 32, 94, 203, 46, 28, 99, 178, 152, 121, 194, 236, 241, 37, 93, 181, 6, 214, - 113, 200, 179, 250, 96, 23, 246, 124, 253, 27, 240, 126, 110, 221, 28, 200 - }}; - -} // namespace message + +//////////////////////////////////////////////////////////////////////////////// +// ARK Message: Strings +constexpr auto MessageString = "Hello World"; +constexpr auto MessageSignatureString = "304402200fb4adddd1f1d652b544ea6ab62828a0a65b712ed447e2538db0caebfa68929e02205ecb2e1c63b29879c2ecf1255db506d671c8b3fa6017f67cfd1bf07e6edd1cc8"; +constexpr auto MessageSignatureStringSpaces = "30 44 02 20 0f b4 ad dd d1 f1 d6 52 b5 44 ea 6a b6 28 28 a0 a6 5b 71 2e d4 47 e2 53 8d b0 ca eb fa 68 92 9e 02 20 5e cb 2e 1c 63 b2 98 79 c2 ec f1 25 5d b5 06 d6 71 c8 b3 fa 60 17 f6 7c fd 1b f0 7e 6e dd 1c c8"; +constexpr auto MessageJsonString = R"({"message":"Hello World","publickey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","signature":"304402200fb4adddd1f1d652b544ea6ab62828a0a65b712ed447e2538db0caebfa68929e02205ecb2e1c63b29879c2ecf1255db506d671c8b3fa6017f67cfd1bf07e6edd1cc8"})"; + +//////////////////////////////////////////////////////////////////////////////// +// ARK Message: Bytes +const std::array MessageBytes = { + { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 }}; + +//////////////////////////////////////////////////////////////////////////////// +const std::array MessageSha256Bytes = { + { 165, 145, 166, 212, 11, 244, 32, 64, 74, 1, 23, 51, 207, 183, 177, 144, + 214, 44, 101, 191, 11, 205, 163, 43, 87, 178, 119, 217, 173, 159, 20, + 110 }}; + +//////////////////////////////////////////////////////////////////////////////// +const std::array MessageSignatureBytes = { + { 48, 68, 2, 32, 15, 180, 173, 221, 209, 241, 214, 82, 181, 68, 234, 106, + 182, 40, 40, 160, 166, 91, 113, 46, 212, 71, 226, 83, 141, 176, 202, 235, + 250, 104, 146, 158, 2, 32, 94, 203, 46, 28, 99, 178, 152, 121, 194, 236, + 241, 37, 93, 181, 6, 214, 113, 200, 179, 250, 96, 23, 246, 124, 253, 27, + 240, 126, 110, 221, 28, 200 }}; } // fixtures } // Crypto diff --git a/test/identities/address.cpp b/test/identities/address.cpp index 783bbe9c..3f431183 100644 --- a/test/identities/address.cpp +++ b/test/identities/address.cpp @@ -1,102 +1,105 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include "identities/address.hpp" #include "identities/keys.hpp" -using namespace Ark::Crypto; -using namespace Ark::Crypto::identities; #include "fixtures/identity.hpp" -using namespace Ark::Crypto::fixtures::identity; +#include "test_helpers.h" -TEST(identities, address_construct_bytes) { - Address address(tAddressBytes, tAddressVersion); - ASSERT_TRUE(Address::validate(address, tAddressVersion)); -} - -/**/ +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; -TEST(identities, address_construct_string) { - Address address(tAddressString); - ASSERT_TRUE(Address::validate(address, tAddressVersion)); +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_address, construct_bytes) { + Address address(fixtures::AddressBytes, fixtures::AddressVersion); + ASSERT_TRUE(Address::validate(address, fixtures::AddressVersion)); } -/**/ - -TEST(identities, address_construct_string_invalid_chars) { - Address address(invalid::tAddressString); - ASSERT_FALSE(Address::validate(address, tAddressVersion)); +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_address, construct_string) { + Address address(fixtures::AddressString); + ASSERT_TRUE(Address::validate(address, fixtures::AddressVersion)); } -/**/ - -TEST(identities, address_construct_string_invalid_length) { - Address address(&tAddressString[1]); - ASSERT_FALSE(Address::validate(address, tAddressVersion)); +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_address,construct_string_invalid_chars) { + Address address(fixtures::invalid::AddressString); + ASSERT_FALSE(Address::validate(address, fixtures::AddressVersion)); } -/**/ - -TEST(identities, address_get_version) { - Address address(tAddressString); - auto version = address.version(); - ASSERT_EQ(version, tAddressVersion); +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_address, construct_string_invalid_length) { + Address address(&fixtures::AddressString[1]); + ASSERT_FALSE(Address::validate(address, fixtures::AddressVersion)); } -/**/ - -TEST(identities, address_to_bytes) { - Address address(tAddressString); - auto addressBytes = address.toBytes(); - for (auto i = 0U; i < HASH_20_BYTE_LEN; i++) { - ASSERT_EQ(addressBytes.at(i), tAddressBytes.at(i)); - }; - auto version = address.version(); - ASSERT_EQ(version, tAddressVersion); +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_address, get_version) { + Address address(fixtures::AddressString); + ASSERT_EQ(fixtures::AddressVersion, address.version()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_address, to_bytes) { + Address address(fixtures::AddressString); -TEST(identities, address_to_string) { - Address address(tAddressBytes, tAddressVersion); - auto addressString = address.toString(); - ASSERT_STREQ(addressString.c_str(), tAddressString); -} + ASSERT_TRUE(array_cmp(fixtures::AddressBytes.data(), + address.toBytes().data(), + HASH_20_LEN)); -/**/ - -TEST(identities, address_from_passphrase) { - auto address = Address::fromPassphrase(tPassphrase, tAddressVersion); - ASSERT_TRUE(Address::validate(address, tAddressVersion)); + ASSERT_EQ(fixtures::AddressVersion, address.version()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_address, to_string) { + Address address(fixtures::AddressBytes, fixtures::AddressVersion); + ASSERT_STREQ(fixtures::AddressString, address.toString().c_str()); +} -TEST(identities, address_from_publickey) { - auto publicKey = Keys::fromPassphrase(tPassphrase).publicKey.data(); - Address address = Address::fromPublicKey(publicKey, tAddressVersion); - ASSERT_TRUE(Address::validate(address, tAddressVersion)); +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_address, from_passphrase) { + auto address = Address::fromPassphrase(fixtures::Passphrase, + fixtures::AddressVersion); + ASSERT_TRUE(Address::validate(address, fixtures::AddressVersion)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_address, from_publickey) { + auto publicKey = Keys::fromPassphrase(fixtures::Passphrase).publicKey; + const auto address = Address::fromPublicKey(publicKey.data(), + fixtures::AddressVersion); -TEST(identities, address_from_privatekey) { - auto privateKey = Keys::fromPassphrase(tPassphrase).privateKey.data(); - auto address = Address::fromPrivateKey(privateKey, tAddressVersion); - ASSERT_TRUE(Address::validate(address, tAddressVersion)); + ASSERT_TRUE(Address::validate(address, fixtures::AddressVersion)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_address, from_privatekey) { + auto privateKey = Keys::fromPassphrase( + fixtures::Passphrase).privateKey; + auto address = Address::fromPrivateKey(privateKey.data(), + fixtures::AddressVersion); -TEST(identities, address_validate) { - Address address(tAddressString); - ASSERT_TRUE(Address::validate(address, tAddressVersion)); + ASSERT_TRUE(Address::validate(address, fixtures::AddressVersion)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_address, validate) { + Address address(fixtures::AddressString); + ASSERT_TRUE(Address::validate(address, fixtures::AddressVersion)); +} -TEST(identities, address_validate_invalid) { - Address address(invalid::tAddressString); - ASSERT_FALSE(Address::validate(address, tAddressVersion)); +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_address, validate_invalid) { + Address address(fixtures::invalid::AddressString); + ASSERT_FALSE(Address::validate(address, fixtures::AddressVersion)); } diff --git a/test/identities/keys.cpp b/test/identities/keys.cpp index 0d605caa..7c7c2298 100644 --- a/test/identities/keys.cpp +++ b/test/identities/keys.cpp @@ -1,64 +1,81 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" -#include "identities/keys.hpp" #include "interfaces/identities.hpp" +#include "identities/keys.hpp" + #include "fixtures/identity.hpp" + +#include "test_helpers.h" + using namespace Ark::Crypto; using namespace Ark::Crypto::identities; -using namespace Ark::Crypto::fixtures::identity; - -TEST(identities, keys_from_passphrase) { - auto keys = Keys::fromPassphrase(tPassphrase); - for (auto i = 0U; i < PRIVATEKEY_BYTE_LEN; i++) { - ASSERT_EQ(keys.privateKey.at(i), tPrivateKeyBytes.at(i)); - }; - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; i++) { - ASSERT_EQ(keys.publicKey.at(i), tPublicKeyBytes.at(i)); - }; + +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_keys, from_passphrase) { + auto keys = Keys::fromPassphrase(fixtures::Passphrase); + + ASSERT_TRUE(array_cmp(fixtures::PrivateKeyBytes.data(), + keys.privateKey.data(), + PRIVATEKEY_BYTE_LEN)); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + keys.publicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_keys, from_privatekey) { + auto keys = Keys::fromPrivateKey(fixtures::PrivateKeyBytes.data()); + + ASSERT_TRUE(array_cmp(fixtures::PrivateKeyBytes.data(), + keys.privateKey.data(), + PRIVATEKEY_BYTE_LEN)); -TEST(identities, keys_from_privatekey) { - auto keys = Keys::fromPrivateKey(tPrivateKeyBytes.data()); - for (auto i = 0U; i < PRIVATEKEY_BYTE_LEN; i++) { - ASSERT_EQ(keys.privateKey.at(i), tPrivateKeyBytes.at(i)); - }; - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; i++) { - ASSERT_EQ(keys.publicKey.at(i), tPublicKeyBytes.at(i)); - }; + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + keys.publicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_keys, from_wif) { + auto keys = Keys::fromWif(fixtures::WifString); -TEST(identities, keys_from_wif) { - auto keys = Keys::fromWif(tWifString); - for (auto i = 0U; i < PRIVATEKEY_BYTE_LEN; i++) { - ASSERT_EQ(keys.privateKey.at(i), tPrivateKeyBytes.at(i)); - }; - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; i++) { - ASSERT_EQ(keys.publicKey.at(i), tPublicKeyBytes.at(i)); - }; + ASSERT_TRUE(array_cmp(fixtures::PrivateKeyBytes.data(), + keys.privateKey.data(), + PRIVATEKEY_BYTE_LEN)); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + keys.publicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_keys, privatekey_from_passphrase) { + auto privateKey = Keys::PrivateKey::fromPassphrase(fixtures::Passphrase); -TEST(identities, keys_privatekey_from_passphrase) { - auto privateKey = Keys::PrivateKey::fromPassphrase(tPassphrase); - for (auto i = 0U; i < PRIVATEKEY_BYTE_LEN; i++) { - ASSERT_EQ(privateKey.at(i), tPrivateKeyBytes.at(i)); - }; + ASSERT_TRUE(array_cmp(fixtures::PrivateKeyBytes.data(), + privateKey.data(), + PRIVATEKEY_BYTE_LEN)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_keys, privatekey_from_wif) { + uint8_t outVersion; + auto privateKey = Keys::PrivateKey::fromWif(fixtures::WifString, &outVersion); + + ASSERT_TRUE(array_cmp(fixtures::PrivateKeyBytes.data(), + privateKey.data(), + PRIVATEKEY_BYTE_LEN)); -TEST(identities, keys_privatekey_from_wif) { - uint8_t outVersion; - auto privateKey = Keys::PrivateKey::fromWif(tWifString, &outVersion); - for (auto i = 0U; i < PRIVATEKEY_BYTE_LEN; i++) { - ASSERT_EQ(privateKey.at(i), tPrivateKeyBytes.at(i)); - }; - ASSERT_EQ(outVersion, tWifVersion); + ASSERT_EQ(fixtures::WifVersion, outVersion); } diff --git a/test/identities/keys_publickey.cpp b/test/identities/keys_publickey.cpp index 466866fb..e8b8d1dd 100644 --- a/test/identities/keys_publickey.cpp +++ b/test/identities/keys_publickey.cpp @@ -1,16 +1,30 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + #include "gtest/gtest.h" #include "identities/keys.hpp" #include "interfaces/identities.hpp" #include "fixtures/identity.hpp" + +#include "test_helpers.h" + using namespace Ark::Crypto; using namespace Ark::Crypto::identities; -using namespace Ark::Crypto::fixtures::identity; -TEST(identities, keys_publickey_from_privatekey) { - auto publicKey = Keys::PublicKey::fromPrivateKey(tPrivateKeyBytes.data()); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; i++) { - ASSERT_EQ(publicKey.at(i), tPublicKeyBytes.at(i)); - }; +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_keys, publickey_from_privatekey) { + auto publicKey = Keys::PublicKey::fromPrivateKey( + fixtures::PrivateKeyBytes.data()); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + publicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); } diff --git a/test/identities/privatekey.cpp b/test/identities/privatekey.cpp index 7fe6f22a..5b1aa266 100644 --- a/test/identities/privatekey.cpp +++ b/test/identities/privatekey.cpp @@ -1,70 +1,72 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + #include "gtest/gtest.h" #include "identities/privatekey.hpp" -using namespace Ark::Crypto; -using namespace Ark::Crypto::identities; #include "fixtures/identity.hpp" -using namespace fixtures::identity; -TEST(identities, privatekey_construct_bytes) { - PrivateKey privateKey(tPrivateKeyBytes); - const auto privateKeyString = privateKey.toString(); - ASSERT_STREQ(privateKeyString.c_str(), tPrivateKeyHex); -} +#include "test_helpers.h" -/**/ +using namespace Ark::Crypto; +using namespace Ark::Crypto::identities; -TEST(identities, privatekey_to_bytes) { - auto privateKey = PrivateKey::fromHex(tPrivateKeyHex); - const auto privateKeyBytes = privateKey.toBytes(); - for (auto i = 0U; i < PRIVATEKEY_BYTE_LEN; i++) { - ASSERT_EQ(privateKeyBytes.at(i), tPrivateKeyBytes.at(i)); - }; +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_privatekey, construct_bytes) { + PrivateKey privateKey(fixtures::PrivateKeyBytes); + ASSERT_STREQ(fixtures::PrivateKeyHex, privateKey.toString().c_str()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_privatekey, to_bytes) { + auto privateKey = PrivateKey::fromHex(fixtures::PrivateKeyHex); -TEST(identities, privatekey_to_string) { - PrivateKey privateKey(tPrivateKeyBytes); - const auto privateKeyString = privateKey.toString(); - ASSERT_STREQ(privateKeyString.c_str(), tPrivateKeyHex); + ASSERT_TRUE(array_cmp(fixtures::PrivateKeyBytes.data(), + privateKey.toBytes().data(), + PRIVATEKEY_BYTE_LEN)); } -/**/ - -TEST(identities, privatekey_from_passphrase) { - PrivateKey privateKey = PrivateKey::fromPassphrase(tPassphrase); - const auto privateKeyString = privateKey.toString(); - ASSERT_STREQ(privateKeyString.c_str(), tPrivateKeyHex); +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_privatekey, to_string) { + PrivateKey privateKey(fixtures::PrivateKeyBytes); + ASSERT_STREQ(fixtures::PrivateKeyHex, privateKey.toString().c_str()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_privatekey, from_passphrase) { + PrivateKey privateKey = PrivateKey::fromPassphrase(fixtures::Passphrase); + ASSERT_STREQ(fixtures::PrivateKeyHex, privateKey.toString().c_str()); +} -TEST(identities, privatekey_from_hex) { - auto privateKey = PrivateKey::fromHex(tPrivateKeyHex); - const auto privateKeyBytes = privateKey.toBytes(); - for (auto i = 0U; i < PRIVATEKEY_BYTE_LEN; i++) { - ASSERT_EQ(privateKeyBytes.at(i), tPrivateKeyBytes.at(i)); - }; +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_privatekey, from_hex) { + auto privateKey = PrivateKey::fromHex(fixtures::PrivateKeyHex); + ASSERT_TRUE(array_cmp(fixtures::PrivateKeyBytes.data(), + privateKey.toBytes().data(), + PRIVATEKEY_BYTE_LEN)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_privatekey, from_hex_invalid_chars) { + auto privateKey = PrivateKey::fromHex(fixtures::invalid::PrivateKeyHex); -TEST(identities, privatekey_from_hex_invalid_chars) { - auto privateKey = PrivateKey::fromHex(invalid::tPrivateKeyHex); - const auto privateKeyBytes = privateKey.toBytes(); - for (auto i = 0U; i < PRIVATEKEY_BYTE_LEN; i++) { - ASSERT_EQ(privateKeyBytes.at(i), 0); - }; + for (auto &e : privateKey.toBytes()) { + ASSERT_EQ(0U, e); + } } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_privatekey, from_hex_invalid_length) { + auto privateKey = PrivateKey::fromHex(&fixtures::PrivateKeyHex[1]); -TEST(identities, privatekey_from_hex_invalid_length) { - auto privateKey = PrivateKey::fromHex(&tPrivateKeyHex[1]); - const auto privateKeyBytes = privateKey.toBytes(); - for (auto i = 0U; i < PRIVATEKEY_BYTE_LEN; i++) { - ASSERT_EQ(privateKeyBytes.at(i), 0); - }; + for (auto &e : privateKey.toBytes()) { + ASSERT_EQ(0U, e); + } } diff --git a/test/identities/publickey.cpp b/test/identities/publickey.cpp index bb5e6f33..e0d9d079 100644 --- a/test/identities/publickey.cpp +++ b/test/identities/publickey.cpp @@ -1,73 +1,81 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + #include "gtest/gtest.h" #include "identities/publickey.hpp" + +#include "fixtures/identity.hpp" + +#include "test_helpers.h" + using namespace Ark::Crypto; using namespace Ark::Crypto::identities; -#include "fixtures/identity.hpp" -using namespace fixtures::identity; - -TEST(identities, publickey_construct_bytes) { - PublicKey publicKey(tPublicKeyBytes); - const auto publicKeyBytes = publicKey.toBytes(); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; i++) { - ASSERT_EQ(publicKeyBytes.at(i), tPublicKeyBytes.at(i)); - };} - -/**/ - -TEST(identities, publickey_to_bytes) { - auto publicKey = PublicKey::fromHex(tPublicKeyHex); - const auto publicKeyBytes = publicKey.toBytes(); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; i++) { - ASSERT_EQ(publicKeyBytes.at(i), tPublicKeyBytes.at(i)); - }; +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_publickey, construct_bytes) { + PublicKey publicKey(fixtures::PublicKeyBytes); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + publicKey.toBytes().data(), + PUBLICKEY_COMPRESSED_LEN)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_publickey, to_bytes) { + auto publicKey = PublicKey::fromHex(fixtures::PublicKeyHex); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + publicKey.toBytes().data(), + PUBLICKEY_COMPRESSED_LEN)); +} -TEST(identities, publickey_to_string) { - PublicKey publicKey(tPublicKeyBytes); - const auto publicKeyString = publicKey.toString(); - ASSERT_STREQ(publicKeyString.c_str(), tPublicKeyHex); +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_publickey, to_string) { + PublicKey publicKey(fixtures::PublicKeyBytes); + ASSERT_STREQ(fixtures::PublicKeyHex, publicKey.toString().c_str()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_publickey, from_passphrase) { + auto publicKey = PublicKey::fromPassphrase(fixtures::Passphrase); + const auto publicKeyBytes = publicKey.toBytes(); -TEST(identities, publickey_from_passphrase) { - auto publicKey = PublicKey::fromPassphrase(tPassphrase); - const auto publicKeyBytes = publicKey.toBytes(); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; i++) { - ASSERT_EQ(publicKeyBytes.at(i), tPublicKeyBytes.at(i)); - }; + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + publicKeyBytes.data(), + PUBLICKEY_COMPRESSED_LEN)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_publickey, from_hex) { + auto publicKey = PublicKey::fromHex(fixtures::PublicKeyHex); + const auto publicKeyBytes = publicKey.toBytes(); -TEST(identities, publickey_from_hex) { - auto publicKey = PublicKey::fromHex(tPublicKeyHex); - const auto publicKeyBytes = publicKey.toBytes(); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; i++) { - ASSERT_EQ(publicKeyBytes.at(i), tPublicKeyBytes.at(i)); - }; + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + publicKeyBytes.data(), + PUBLICKEY_COMPRESSED_LEN)); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_publickey, from_hex_invalid_chars) { + const auto publicKey = PublicKey::fromHex(fixtures::invalid::PublicKeyHex); -TEST(identities, publickey_from_hex_invalid_chars) { - auto publicKey = PublicKey::fromHex(invalid::tPublicKeyHex); - const auto publicKeyBytes = publicKey.toBytes(); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; i++) { - ASSERT_NE(publicKeyBytes.at(i), tPublicKeyBytes.at(i)); - }; + for (auto &e : publicKey.toBytes()) { + ASSERT_EQ(0U, e); + } } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_publickey, from_hex_invalid_length) { + const auto publicKey = PublicKey::fromHex(&fixtures::PublicKeyHex[1]); -TEST(identities, publickey_from_hex_invalid_length) { - auto publicKey = PublicKey::fromHex(&tPublicKeyHex[1]); - const auto publicKeyBytes = publicKey.toBytes(); - for (auto i = 0U; i < PUBLICKEY_COMPRESSED_BYTE_LEN; i++) { - ASSERT_NE(publicKeyBytes.at(i), tPublicKeyBytes.at(i)); - }; + for (auto &e : publicKey.toBytes()) { + ASSERT_EQ(0U, e); + } } diff --git a/test/identities/wif.cpp b/test/identities/wif.cpp index 52825d24..f7bf14e0 100644 --- a/test/identities/wif.cpp +++ b/test/identities/wif.cpp @@ -1,77 +1,88 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include "identities/wif.hpp" + +#include "fixtures/identity.hpp" + +#include "test_helpers.h" + using namespace Ark::Crypto; using namespace Ark::Crypto::identities; -#include "fixtures/identity.hpp" -using namespace fixtures::identity; - -TEST(identities, wif_construct_bytes) { - Wif wif(tPrivateKeyBytes, tWifVersion); - const auto wifBytes = wif.toBytes(); - for (auto i = 0U; i < HASH_20_BYTE_LEN; i++) { - ASSERT_EQ(wifBytes.at(i), tPrivateKeyBytes.at(i)); - }; - ASSERT_EQ(wif.version(), tWifVersion); -} +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_wif, construct_bytes) { + Wif wif(fixtures::PrivateKeyBytes, fixtures::WifVersion); + ASSERT_TRUE(array_cmp(fixtures::PrivateKeyBytes.data(), + wif.toBytes().data(), + HASH_20_LEN)); -TEST(identities, wif_construct_string) { - Wif wif(tWifString); - const auto wifBytes = wif.toBytes(); - for (auto i = 0U; i < HASH_20_BYTE_LEN; i++) { - ASSERT_EQ(wifBytes.at(i), tPrivateKeyBytes.at(i)); - }; - ASSERT_EQ(wif.version(), tWifVersion); + ASSERT_EQ(fixtures::WifVersion, wif.version()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_wif, construct_string) { + Wif wif(fixtures::WifString); -TEST(identities, wif_construct_string_invlaid) { - Wif wif(invalid::tWifString); - const auto wifBytes = wif.toBytes(); - for (auto i = 0U; i < HASH_20_BYTE_LEN; i++) { - ASSERT_EQ(wifBytes.at(i), 0); - }; - ASSERT_EQ(wif.version(), 0); + ASSERT_TRUE(array_cmp(fixtures::PrivateKeyBytes.data(), + wif.toBytes().data(), + HASH_20_LEN)); + + ASSERT_EQ(fixtures::WifVersion, wif.version()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_wif, construct_string_invlaid) { + Wif wif(fixtures::invalid::WifString); + + for (auto &e : wif.toBytes()) { + ASSERT_EQ(0U, e); + } + + ASSERT_EQ(0U, wif.version()); +} -TEST(identities, wif_get_version) { - Wif wif(tWifString); - ASSERT_EQ(wif.version(), tWifVersion); +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_wif, get_version) { + Wif wif(fixtures::WifString); + ASSERT_EQ(fixtures::WifVersion, wif.version()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_wif, to_bytes) { + Wif wif(fixtures::WifString); + + ASSERT_TRUE(array_cmp(fixtures::PrivateKeyBytes.data(), + wif.toBytes().data(), + HASH_20_LEN)); -TEST(identities, wif_to_bytes) { - Wif wif(tWifString); - const auto wifBytes = wif.toBytes(); - for (auto i = 0U; i < HASH_20_BYTE_LEN; i++) { - ASSERT_EQ(wifBytes.at(i), tPrivateKeyBytes.at(i)); - }; - ASSERT_EQ(wif.version(), tWifVersion); + ASSERT_EQ(fixtures::WifVersion, wif.version()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_wif, to_string) { + Wif wif(fixtures::PrivateKeyBytes, fixtures::WifVersion); -TEST(identities, wif_to_string) { - Wif wif(tPrivateKeyBytes, tWifVersion); - const auto wifString = wif.toString(); - ASSERT_STREQ(wifString.c_str(), tWifString); - ASSERT_EQ(wif.version(), tWifVersion); + ASSERT_STREQ(fixtures::WifString, wif.toString().c_str()); + ASSERT_EQ(fixtures::WifVersion, wif.version()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(identities_wif, from_passphrase) { + Wif wif = Wif::fromPassphrase(fixtures::Passphrase, fixtures::WifVersion); + + ASSERT_TRUE(array_cmp(fixtures::PrivateKeyBytes.data(), + wif.toBytes().data(), + HASH_20_LEN)); -TEST(identities, wif_from_passphrase) { - Wif wif = Wif::fromPassphrase(tPassphrase, tWifVersion); - const auto wifBytes = wif.toBytes(); - for (auto i = 0U; i < HASH_20_BYTE_LEN; i++) { - ASSERT_EQ(wifBytes.at(i), tPrivateKeyBytes.at(i)); - }; - ASSERT_EQ(wif.version(), tWifVersion); + ASSERT_EQ(fixtures::WifVersion, wif.version()); } diff --git a/test/managers/fee_manager.cpp b/test/managers/fee_manager.cpp index ba338e26..bbd3549a 100644 --- a/test/managers/fee_manager.cpp +++ b/test/managers/fee_manager.cpp @@ -1,65 +1,72 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include "managers/fee_manager.hpp" -#include "defaults/static_fees.hpp" + +#include "transactions/defaults/fees.hpp" using namespace Ark::Crypto; using namespace Ark::Crypto::managers; +using namespace Ark::Crypto::transactions; -namespace { -const FeePolicy CustomFeePolicy { - 900000000ULL, 800000000ULL, 700000000ULL, 600000000ULL, 500000000ULL, - 400000000ULL, 300000000ULL, 200000000ULL, 100000000ULL, 0ULL +//////////////////////////////////////////////////////////////////////////////// +static FeePolicy CustomFeePolicy { + 900000000ULL, 800000000ULL, 700000000ULL, 600000000ULL, 500000000ULL, + 400000000ULL, 300000000ULL, 200000000ULL, 100000000ULL, 0ULL }; -constexpr const uint8_t InBounds = 5U; -constexpr const uint8_t OutOfBounds = 11; -constexpr const uint64_t CustomFee = 1000ULL; -} //namespace - -/**/ -TEST(managers, fee_manager_fee_get) { - FeeManager feeManager; - for (auto i = 0u; i < StaticFeePolicy.size(); ++i) { - ASSERT_EQ(feeManager.getFee(i), StaticFeePolicy.at(i)); - }; +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t InBounds = 5U; +constexpr uint8_t OutOfBounds = 11; +constexpr uint64_t CustomFee = 1000ULL; + +//////////////////////////////////////////////////////////////////////////////// +TEST(managers_fee_manager, get) { + FeeManager feeManager; + uint8_t i = 0; + for (auto &fee : StaticFeePolicy) { + ASSERT_EQ(fee, feeManager.getFee(i)); + ++i; + } } -/**/ - -TEST(managers, fee_manager_fee_get_oob) { - FeeManager feeManager; - ASSERT_EQ(feeManager.getFee(OutOfBounds), AMOUNT_ZERO); +//////////////////////////////////////////////////////////////////////////////// +TEST(managers_fee_manager, get_out_of_bounds) { + FeeManager feeManager; + ASSERT_EQ(0ULL, feeManager.getFee(OutOfBounds)); } -/**/ - -TEST(managers, fee_manager_fee_set) { - FeeManager feeManager; - feeManager.setFee(InBounds, CustomFee); - ASSERT_EQ(feeManager.getFee(InBounds), CustomFee); +//////////////////////////////////////////////////////////////////////////////// +TEST(managers_fee_manager, set) { + FeeManager feeManager; + feeManager.setFee(InBounds, CustomFee); + ASSERT_EQ(CustomFee, feeManager.getFee(InBounds)); } -/**/ - -TEST(managers, fee_manager_fee_set_oob) { - FeeManager feeManager; - feeManager.setFee(OutOfBounds, CustomFee); - ASSERT_EQ(feeManager.getFee(OutOfBounds), CustomFee); +//////////////////////////////////////////////////////////////////////////////// +TEST(managers_fee_manager, set_out_of_bounds) { + FeeManager feeManager; + feeManager.setFee(OutOfBounds, CustomFee); + ASSERT_EQ(CustomFee, feeManager.getFee(OutOfBounds)); } -/**/ - -TEST(managers, fee_manager_policy_get) { - FeeManager feeManager; - ASSERT_TRUE(feeManager.getPolicy() == StaticFeePolicy); +//////////////////////////////////////////////////////////////////////////////// +TEST(managers_fee_manager, policy_get) { + FeeManager feeManager; + ASSERT_TRUE(StaticFeePolicy == feeManager.getPolicy()); } -/**/ - -TEST(managers, fee_manager_policy_set) { - FeeManager feeManager; - feeManager.setPolicy(CustomFeePolicy); - ASSERT_TRUE(feeManager.getPolicy() == CustomFeePolicy); +//////////////////////////////////////////////////////////////////////////////// +TEST(managers_fee_manager, policy_set) { + FeeManager feeManager; + feeManager.setPolicy(CustomFeePolicy); + ASSERT_TRUE(CustomFeePolicy == feeManager.getPolicy()); } diff --git a/test/managers/network_manager.cpp b/test/managers/network_manager.cpp index 9455f940..45491e00 100644 --- a/test/managers/network_manager.cpp +++ b/test/managers/network_manager.cpp @@ -1,31 +1,38 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include "managers/network_manager.hpp" + #include "common/network.hpp" using namespace Ark::Crypto; using namespace managers; -namespace { +//////////////////////////////////////////////////////////////////////////////// const Network CustomNetwork { - "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", - 1, 0x53, 0xaa, - "2019-04-12T13:00:00.000Z" + "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", + 1, 0x53, 0xaa, + "2019-04-12T13:00:00.000Z" }; -} //namespace - -/**/ -TEST(managers, network_manager_network_get) { - NetworkManager manager; - ASSERT_TRUE(manager.getNetwork() == Devnet); +//////////////////////////////////////////////////////////////////////////////// +TEST(managers_network_manager, get) { + NetworkManager manager; + ASSERT_TRUE(Devnet == manager.getNetwork()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(managers_network_manager, set) { + NetworkManager manager; + manager.setNetwork(CustomNetwork); -TEST(managers, network_manager_network_set) { - NetworkManager manager; - manager.setNetwork(CustomNetwork); - ASSERT_TRUE(manager.getNetwork() == CustomNetwork); + ASSERT_TRUE(CustomNetwork == manager.getNetwork()); } diff --git a/test/networks/devnet.cpp b/test/networks/devnet.cpp index cc4d5db5..dcf9d5c2 100644 --- a/test/networks/devnet.cpp +++ b/test/networks/devnet.cpp @@ -1,21 +1,29 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include "networks/devnet.hpp" -namespace { // NOLINT using namespace Ark::Crypto; -constexpr const char* kNethash = "2a44f340d76ffc3df204c5f38cd355b7496c9065a1ade2ef92071436bd72e867"; -constexpr const uint8_t kSlip44 = 1; -constexpr const uint8_t kWif = 0xaa; -constexpr const uint8_t kVersion = 0x1E; -constexpr const char* kEpoch = "2017-03-21T13:00:00.000Z"; -}; // namespace +//////////////////////////////////////////////////////////////////////////////// TEST(networks, devnet) { - ASSERT_STREQ(Devnet.nethash.c_str(), kNethash); - ASSERT_EQ(Devnet.slip44, kSlip44); - ASSERT_EQ(Devnet.wif, kWif); - ASSERT_EQ(Devnet.version, kVersion); - ASSERT_STREQ(Devnet.epoch.c_str(), kEpoch); + const char* NETHASH = "2a44f340d76ffc3df204c5f38cd355b7496c9065a1ade2ef92071436bd72e867"; + const uint8_t SLIP44 = 1U; + const uint8_t WIF = 0xaa; + const uint8_t VERSION = 0x1E; + const char* EPOCH = "2017-03-21T13:00:00.000Z"; + + ASSERT_STREQ(NETHASH, Devnet.nethash.c_str()); + ASSERT_EQ(SLIP44, Devnet.slip44); + ASSERT_EQ(WIF, Devnet.wif); + ASSERT_EQ(VERSION, Devnet.version); + ASSERT_STREQ(EPOCH, Devnet.epoch.c_str()); } diff --git a/test/networks/mainnet.cpp b/test/networks/mainnet.cpp index b0fb925a..3571b1b7 100644 --- a/test/networks/mainnet.cpp +++ b/test/networks/mainnet.cpp @@ -1,21 +1,29 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include "networks/mainnet.hpp" -namespace { // NOLINT using namespace Ark::Crypto; -constexpr const char* kNethash = "6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988"; -constexpr const uint8_t kSlip44 = 111; -constexpr const uint8_t kWif = 0xaa; -constexpr const uint8_t kVersion = 0x17; -constexpr const char* kEpoch = "2017-03-21T13:00:00.000Z"; -}; // namespace -TEST(networks, mainnet) { - ASSERT_STREQ(Mainnet.nethash.c_str(), kNethash); - ASSERT_EQ(Mainnet.slip44, kSlip44); - ASSERT_EQ(Mainnet.wif, kWif); - ASSERT_EQ(Mainnet.version, kVersion); - ASSERT_STREQ(Mainnet.epoch.c_str(), kEpoch); +//////////////////////////////////////////////////////////////////////////////// +TEST(networks, mainnet) { // NOLINT + const char* NETHASH = "6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988"; + const uint8_t SLIP44 = 111U; + const uint8_t WIF = 0xaa; + const uint8_t VERSION = 0x17; + const char* EPOCH = "2017-03-21T13:00:00.000Z"; + + ASSERT_STREQ(NETHASH, Mainnet.nethash.c_str()); + ASSERT_EQ(SLIP44, Mainnet.slip44); + ASSERT_EQ(WIF, Mainnet.wif); + ASSERT_EQ(VERSION, Mainnet.version); + ASSERT_STREQ(EPOCH, Mainnet.epoch.c_str()); } diff --git a/test/networks/testnet.cpp b/test/networks/testnet.cpp index 8d546507..4b4123ce 100644 --- a/test/networks/testnet.cpp +++ b/test/networks/testnet.cpp @@ -1,21 +1,29 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include "networks/testnet.hpp" -namespace { // NOLINT using namespace Ark::Crypto; -constexpr const char* kNethash = "d9acd04bde4234a81addb8482333b4ac906bed7be5a9970ce8ada428bd083192"; -constexpr const uint8_t kSlip44 = 1; -constexpr const uint8_t kWif = 0xba; -constexpr const uint8_t kVersion = 0x17; -constexpr const char* kEpoch = "2017-03-21T13:00:00.000Z"; -}; // namespace -TEST(networks, testnet) { - ASSERT_STREQ(Testnet.nethash.c_str(), kNethash); - ASSERT_EQ(Testnet.slip44, kSlip44); - ASSERT_EQ(Testnet.wif, kWif); - ASSERT_EQ(Testnet.version, kVersion); - ASSERT_STREQ(Testnet.epoch.c_str(), kEpoch); +//////////////////////////////////////////////////////////////////////////////// +TEST(networks, testnet) { // NOLINT + const char* NETHASH = "d9acd04bde4234a81addb8482333b4ac906bed7be5a9970ce8ada428bd083192"; + const uint8_t SLIP44 = 1U; + const uint8_t WIF = 0xba; + const uint8_t VERSION = 0x17; + const char* EPOCH = "2017-03-21T13:00:00.000Z"; + + ASSERT_STREQ(NETHASH, Testnet.nethash.c_str()); + ASSERT_EQ(SLIP44, Testnet.slip44); + ASSERT_EQ(WIF, Testnet.wif); + ASSERT_EQ(VERSION, Testnet.version); + ASSERT_STREQ(EPOCH, Testnet.epoch.c_str()); } diff --git a/test/platformio.ini b/test/platformio.ini index b78df8db..172e216f 100644 --- a/test/platformio.ini +++ b/test/platformio.ini @@ -8,162 +8,350 @@ ; Please visit documentation for the other options and examples ; http://docs.platformio.org/page/projectconf.html +################################################################################ +; Project Settings +################################################################################ + +########################### +; PIO Environment Settings [platformio] description = "Unit Tests for Ark-Cpp-Crypto" src_dir = ../src build_dir = ../build/.pioenvs libdeps_dir = ../extern/.piolibdeps +################## +; Base Parameters [base] -# Replace googletest library with proper release once ESP8266 support PR is merged to googletest -lib_deps = ArduinoJson@6.12.0, BIP66@0.2.1, micro-ecc@1.0.0, https://github.com/ciband/googletest.git#pre_release +; Replace googletest library with proper release once ESP8266 support PR is merged to googletest +lib_deps = ArduinoJson@6.12.0, bcl@0.0.5, BIP66@0.3.2, micro-ecc@1.0.0, https://github.com/ciband/googletest.git#pre_release build_flags = -I../test -I../test/iot/ -I../src -I../src/lib -I../src/include/cpp-crypto -DUNIT_TEST src_filter = +<../src> +<../test/iot> upload_speed = 921600 +################################################################################ +; Frameworks +################################################################################ + +########## +; Arduino [arduino] framework = arduino +################################################################################ +; Boards +################################################################################ + +########## +; ESP8266 [esp8266] platform = https://github.com/platformio/platform-espressif8266.git#feature/stage board = huzzah build_flags = -D_GLIBCXX_USE_C99 board_build.f_cpu = 160000000L +######## +; ESP32 + [esp32] platform = espressif32 board = esp32dev +################################################################################ +; Test Sources +################################################################################ + +################################################################################ +################################################################################ +; All Tests + [all_tests] src_filter = +<../test> +################################################################################ +################################################################################ +; Common Tests + [common_tests] src_filter = +<../test/common> +################################################################################ +################################################################################ +; Crypto + +################### +; All Crypto Tests [crypto_tests] src_filter = +<../test/crypto> +################################################################################ +; Crypto - Curve + +############# +; Ecdsa Sign [crypto_curve_ecdsa_sign_tests] src_filter = +<../test/crypto/curve_ecdsa_sign.cpp> -[crypto_curve_ecdsa_sign_null_pk_tests] -src_filter = +<../test/crypto/curve_ecdsa_sign_null_pk.cpp> - -[crypto_curve_ecdsa_sign_null_hash32_sha256_tests] -src_filter = +<../test/crypto/curve_ecdsa_sign_null_hash32_sha256.cpp> +######################### +; Ecdsa Sign - Null Hash +[crypto_curve_ecdsa_sign_null_hash_tests] +src_filter = +<../test/crypto/curve_ecdsa_sign_null_hash.cpp> -[crypto_curve_ecdsa_sign_null_hash32_privatekey_tests] -src_filter = +<../test/crypto/curve_ecdsa_sign_null_hash32_privatekey.cpp> +############################### +; Ecdsa Sign - Null PrivateKey +[crypto_curve_ecdsa_sign_null_privatekey_tests] +src_filter = +<../test/crypto/curve_ecdsa_sign_null_privatekey.cpp> -[crypto_curve_publickey_tests] -src_filter = +<../test/crypto/curve_publickey.cpp> +################# +; Ecdsa - Verify +[crypto_curve_verify_valid_tests] +src_filter = +<../test/crypto/curve_verify_valid.cpp> +######################### +; Ecdsa - Verify Invalid [crypto_curve_verify_invalid_tests] src_filter = +<../test/crypto/curve_verify_invalid.cpp> -[crypto_curve_verify_valid_tests] -src_filter = +<../test/crypto/curve_verify_valid.cpp> +############ +; PublicKey +[crypto_curve_publickey_tests] +src_filter = +<../test/crypto/curve_publickey.cpp> + +################################################################################ +; Crypto - Hash [crypto_hash_tests] src_filter = +<../test/crypto/hash.cpp> +################################################################################ +; Crypto - Message + +############## +; Basic Tests +; - 'toJson()' +; - 'toMap()' [crypto_message_tests] src_filter = +<../test/crypto/message.cpp> +################# +; Message - Sign [crypto_message_sign_tests] src_filter = +<../test/crypto/message_sign.cpp> +################### +; Message - Verify [crypto_message_verify_tests] src_filter = +<../test/crypto/message_verify.cpp> +################################################################################ +; Crypto - Slot + [crypto_slot_tests] src_filter = +<../test/crypto/slot.cpp> -[defaults_tests] -src_filter = +<../test/defaults> +################################################################################ +################################################################################ +; Identities +####################### +; All Identities Tests [identities_tests] src_filter = +<../test/identities> +####################### +; Identities - Address [identities_address_tests] src_filter = +<../test/identities/address.cpp> +#################### +; Identities - Keys [identities_keys_tests] src_filter = +<../test/identities/keys.cpp> +############################### +; Identities - Keys: PublicKey [identities_keys_publickey_tests] src_filter = +<../test/identities/keys_publickey.cpp> +########################## +; Identities - PrivateKey [identities_privatekey_tests] src_filter = +<../test/identities/privatekey.cpp> +######################### +; Identities - PublicKey [identities_publickey_tests] src_filter = +<../test/identities/publickey.cpp> +################### +; Identities - Wif +[identities_wif_tests] +src_filter = +<../test/identities/wif.cpp> + +################################################################################ +################################################################################ +; Managers + [managers_tests] src_filter = +<../test/managers> +################################################################################ +################################################################################ +; Networks + [networks_tests] src_filter = +<../test/networks> -[transactions_tests] -src_filter = +<../test/transactions> - -[transactions_builder_tests] -src_filter = +<../test/transactions/builder.cpp> +################################################################################ +################################################################################ +; Transactions -[transactions_builder_transfer_custom_network_tests] -src_filter = +<../test/transactions/builder_transfer_custom_network.cpp> - -[transactions_deserializer_delegate_registration_tests] -src_filter = +<../test/transactions/deserializer_delegate_registration.cpp> - -[transactions_deserializer_multi_signature_registration_tests] -src_filter = +<../test/transactions/deserializer_multi_signature_registration.cpp> - -[transactions_deserializer_second_signature_registration_tests] -src_filter = +<../test/transactions/deserializer_second_signature_registration.cpp> - -[transactions_deserializer_transfer_tests] -src_filter = +<../test/transactions/deserializer_transfer.cpp> - -[transactions_deserializer_vote_tests] -src_filter = +<../test/transactions/deserializer_vote.cpp> +######################### +; All Transactions Tests +[transactions_tests] +src_filter = +<../test/transactions/*> + +################################################################################ +; Transactions - Builders + +###################### +; Builders - Transfer +[transactions_builders_transfer_tests] +src_filter = +<../test/transactions/builders/transfer.cpp> + +############################## +; Builders - Second Signature +[transactions_builders_second_signature_tests] +src_filter = +<../test/transactions/builders/second_signature.cpp> + +################################### +; Builders - Delegate Registration +[transactions_builders_delegate_registration_tests] +src_filter = +<../test/transactions/builders/delegate_registration.cpp> + +################## +; Builders - Vote +[transactions_builders_vote_tests] +src_filter = +<../test/transactions/builders/vote.cpp> + +################## +; Builders - Ipfs +[transactions_builders_ipfs_tests] +src_filter = +<../test/transactions/builders/ipfs.cpp> + +########################## +; Builders - MultiPayment +[transactions_builders_multi_payment_tests] +src_filter = +<../test/transactions/builders/multi_payment.cpp> + +################################## +; Builders - Delegate Resignation +[transactions_builders_delegate_resignation_tests] +src_filter = +<../test/transactions/builders/delegate_resignation.cpp> + +####################### +; Builders - Htlc Lock +[transactions_builders_htlc_lock_tests] +src_filter = +<../test/transactions/builders/htlc_lock.cpp> + +######################## +; Builders - Htlc Claim +[transactions_builders_htlc_claim_tests] +src_filter = +<../test/transactions/builders/htlc_claim.cpp> + +######################### +; Builders - Htlc Refund +[transactions_builders_htlc_refund_tests] +src_filter = +<../test/transactions/builders/htlc_refund.cpp> + +################################################################################ +; Transactions - Defaults + +[transactions_defaults_tests] +src_filter = +<../test/transactions/defaults/fees_types.cpp> + +################################################################################ +; Transactions - Types + +################### +; Types - Transfer +[transactions_types_transfer_tests] +src_filter = +<../test/transactions/types/transfer.cpp> + +############################ +; Types - Second Signature +[transactions_types_second_signature_tests] +src_filter = +<../test/transactions/types/second_signature.cpp> + +################################ +; Types - Delegate Registration +[transactions_types_delegate_registration_tests] +src_filter = +<../test/transactions/types/delegate_registration.cpp> + +############### +; Types - Vote +[transactions_types_vote_tests] +src_filter = +<../test/transactions/types/vote.cpp> + +############### +; Types - Ipfs +[transactions_types_ipfs_tests] +src_filter = +<../test/transactions/types/ipfs.cpp> + +####################### +; Types - MultiPayment +[transactions_types_multi_payment_tests] +src_filter = +<../test/transactions/types/multi_payment.cpp> + +############################### +; Types - Delegate Resignation +[transactions_types_delegate_resignation_tests] +src_filter = +<../test/transactions/types/delegate_resignation.cpp> + +############################ +; Types - Types - Htlc Lock +[transactions_types_htlc_lock_tests] +src_filter = +<../test/transactions/types/htlc_lock.cpp> + +############################# +; Types - Types - Htlc Claim +[transactions_types_htlc_claim_tests] +src_filter = +<../test/transactions/types/htlc_claim.cpp> + +############################## +; Types - Types - Htlc Refund +[transactions_types_htlc_refund_tests] +src_filter = +<../test/transactions/types/htlc_refund.cpp> + +################################################################################ +; Transactions - Deserializer + +[transactions_deserializer_tests] +src_filter = +<../test/transactions/deserializer.cpp> + +################################################################################ +; Transactions - Serializer [transactions_serializer_tests] src_filter = +<../test/transactions/serializer.cpp> -[transactions_transaction_tests] -src_filter = +<../test/transactions/transaction.cpp> - -[transactions_transaction_to_array_type_0_tests] -src_filter = +<../test/transactions/transaction_to_array_type_0.cpp> - -[transactions_transaction_to_array_type_1_tests] -src_filter = +<../test/transactions/transaction_to_array_type_1.cpp> - -[transactions_transaction_to_array_type_2_tests] -src_filter = +<../test/transactions/transaction_to_array_type_2.cpp> - -[transactions_transaction_to_array_type_3_tests] -src_filter = +<../test/transactions/transaction_to_array_type_3.cpp> - -[transactions_transaction_to_json_type_0_tests] -src_filter = +<../test/transactions/transaction_to_json_type_0.cpp> - -[transactions_transaction_to_json_type_1_tests] -src_filter = +<../test/transactions/transaction_to_json_type_1.cpp> +################################################################################ +; Transactions - Transaction -[transactions_transaction_to_json_type_2_tests] -src_filter = +<../test/transactions/transaction_to_json_type_2.cpp> +[transactions_transaction] +src_filter = +<../test/transactions/transaction.cpp> -[transactions_transaction_to_json_type_3_tests] -src_filter = +<../test/transactions/transaction_to_json_type_3.cpp> +################################################################################ +################################################################################ +; Utils [utils_tests] src_filter = +<../test/utils> -########################################################################### +################################################################################ +################################################################################ + +################################################################################ # CI Configurations to build all tests without rebuilding framworks # for every flashable test # @@ -171,203 +359,393 @@ src_filter = +<../test/utils> # flash or will use too much memory and crash at runtime # # Use the individual test targets to test specific features on hardware -########################################################################### +################################################################################ -[env:esp8266_tests] -extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${all_tests.src_filter} +; [env:esp8266_tests] +; extends = base, arduino, esp8266 +; src_filter = ${base.src_filter} ${all_tests.src_filter} [env:esp32_tests] extends = base, arduino, esp32 src_filter = ${base.src_filter} ${all_tests.src_filter} -########################################################################### -########################################################################### +################################################################################ +################################################################################ + +################################################################################ +################################################################################ +; Environments +################################################################################ +################################################################################ + +################################################################################ +; ESP8266 +################################################################################ + +################################################################################ +################################################################################ +; ESP8266 - Common +########################### +; All ESP8266 Common Tests [env:esp8266_common_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${common_tests.src_filter} +################################################################################ +################################################################################ +; ESP8266 - Crypto + +################################################################################ +; ESP8266 - Crypto - Curve + +################################# +; ESP8266 - Curve - Ecdsa - Sign [env:esp8266_crypto_curve_ecdsa_sign_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_tests.src_filter} -[env:esp8266_crypto_curve_ecdsa_sign_null_pk_tests] -extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_pk_tests.src_filter} - -[env:esp8266_crypto_curve_ecdsa_sign_null_hash32_sha256_tests] +############################################# +; ESP8266 - Curve - Ecdsa - Sign - Null Hash +[env:esp8266_crypto_curve_ecdsa_sign_null_hash_tests] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_hash32_sha256_tests.src_filter} +src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_hash_tests.src_filter} -[env:esp8266_crypto_curve_ecdsa_sign_null_hash32_privatekey_tests] +################################################### +; ESP8266 - Curve - Ecdsa - Sign - Null PrivateKey +[env:esp8266_crypto_curve_ecdsa_sign_null_privatekey_tests] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_hash32_privatekey_tests.src_filter} +src_filter = ${base.src_filter} ${crypto_curve_ecdsa_sign_null_privatekey_tests.src_filter} -[env:esp8266_crypto_curve_publickey_tests] +################################### +; ESP8266 - Curve - Ecdsa - Verify +[env:esp8266_crypto_curve_verify_valid_tests] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${crypto_curve_publickey_tests.src_filter} +src_filter = ${base.src_filter} ${crypto_curve_verify_valid_tests.src_filter} +################################### +; ESP8266 - Curve - Ecdsa - Verify [env:esp8266_crypto_curve_verify_invalid_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_curve_verify_invalid_tests.src_filter} -[env:esp8266_crypto_curve_verify_valid_tests] +############################### +; ESP8266 - Curve - PublicKey +[env:esp8266_crypto_curve_publickey_tests] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${crypto_curve_verify_valid_tests.src_filter} +src_filter = ${base.src_filter} ${crypto_curve_publickey_tests.src_filter} +########################## +; ESP8266 - Crypto - Hash [env:esp8266_crypto_hash_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_hash_tests.src_filter} +################################################################################ +; ESP8266 - Crypto - Message + +############################# +; ESP8266 - Crypto - Message [env:esp8266_crypto_message_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_message_tests.src_filter} +#################################### +; ESP8266 - Crypto - Message - Sign [env:esp8266_crypto_message_sign_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_message_sign_tests.src_filter} +###################################### +; ESP8266 - Crypto - Message - Verify [env:esp8266_crypto_message_verify_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_message_verify_tests.src_filter} +################################################################################ +; ESP8266 - Crypto - Slot + [env:esp8266_crypto_slot_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${crypto_slot_tests.src_filter} -[env:esp8266_defaults_tests] -extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${defaults_tests.src_filter} +################################################################################ +################################################################################ +; ESP8266 - Identities +################################# +; ESP8266 - Identities - Address [env:esp8266_identities_address_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${identities_address_tests.src_filter} +############################## +; ESP8266 - Identities - Keys [env:esp8266_identities_keys_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${identities_keys_tests.src_filter} +########################################## +; ESP8266 - Identities - Keys - PublicKey [env:esp8266_identities_keys_publickey_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${identities_keys_publickey_tests.src_filter} +#################################### +; ESP8266 - Identities - PrivateKey [env:esp8266_identities_privatekey_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${identities_privatekey_tests.src_filter} +################################### +; ESP8266 - Identities - PublicKey [env:esp8266_identities_publickey_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${identities_publickey_tests.src_filter} +############################# +; ESP8266 - Identities - Wif +[env:esp8266_identities_wif_tests] +extends = base, arduino, esp8266 +src_filter = ${base.src_filter} ${identities_wif_tests.src_filter} + +################################################################################ +################################################################################ +; ESP8266 - Managers + [env:esp8266_managers_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${managers_tests.src_filter} +################################################################################ +################################################################################ +; ESP8266 - Networks + [env:esp8266_networks_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${networks_tests.src_filter} -[env:esp8266_transactions_builder_tests] +################################################################################ +################################################################################ +; ESP8266 - Transactions + +################################################################################ +; ESP8266 - Transactions - Builders + +############################################### +; ESP8266 - Transactions - Builders - Transfer +[env:esp8266_transactions_builder_transfer] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_builder_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_builders_transfer_tests.src_filter} -[env:esp8266_transactions_builder_transfer_custom_network_tests] +####################################################### +; ESP8266 - Transactions - Builders - Second Signature +[env:esp8266_transactions_builder_second_signature] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_builder_transfer_custom_network_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_builders_second_signature_tests.src_filter} -[env:esp8266_transactions_deserializer_delegate_registration_tests] +############################################################ +; ESP8266 - Transactions - Builders - Delegate Registration +[env:esp8266_transactions_builder_delegate_registration] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_deserializer_delegate_registration_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_builders_delegate_registration_tests.src_filter} -[env:esp8266_transactions_deserializer_multi_signature_registration_tests] +############################################ +; ESP8266 - Transactions - Builders - Vote +[env:esp8266_transactions_builder_vote] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_deserializer_multi_signature_registration_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_builders_vote_tests.src_filter} -[env:esp8266_transactions_deserializer_second_signature_registration_tests] +########################################### +; ESP8266 - Transactions - Builders - Ipfs +[env:esp8266_transactions_builder_ipfs] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_deserializer_second_signature_registration_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_builders_ipfs_tests.src_filter} -[env:esp8266_transactions_deserializer_transfer_tests] +################################################### +; ESP8266 - Transactions - Builders - MultiPayment +[env:esp8266_transactions_builder_multi_payment] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_deserializer_transfer_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_builders_multi_payment_tests.src_filter} -[env:esp8266_transactions_deserializer_vote_tests] +########################################################### +; ESP8266 - Transactions - Builders - Delegate Resignation +[env:esp8266_transactions_builder_delegate_resignation] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_deserializer_vote_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_builders_delegate_resignation_tests.src_filter} -[env:esp8266_transactions_serializer_tests] +################################################ +; ESP8266 - Transactions - Builders - Htlc Lock +[env:esp8266_transactions_builder_htlc_lock] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_serializer_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_builders_htlc_lock_tests.src_filter} -[env:esp8266_transactions_transaction_tests] +################################################# +; ESP8266 - Transactions - Builders - Htlc Claim +[env:esp8266_transactions_builder_htlc_claim] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_transaction_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_builders_htlc_claim_tests.src_filter} -[env:esp8266_transactions_transaction_to_array_type_0_tests] +################################################## +; ESP8266 - Transactions - Builders - Htlc Refund +[env:esp8266_transactions_builder_htlc_refund] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_0_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_builders_htlc_refund_tests.src_filter} + +################################################################################ +; ESP8266 - Transactions - Defaults -[env:esp8266_transactions_transaction_to_array_type_1_tests] +[env:esp8266_transactions_defaults_tests] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_1_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_defaults_tests.src_filter} -[env:esp8266_transactions_transaction_to_array_type_2_tests] +################################################################################ +; ESP8266 - Transactions - Types + +############################################ +; ESP8266 - Transactions - Types - Transfer +[env:esp8266_transactions_types_transfer_tests] +extends = base, arduino, esp8266 +src_filter = ${base.src_filter} ${transactions_types_transfer_tests.src_filter} + +#################################################### +; ESP8266 - Transactions - Types - Second Signature +[env:esp8266_transactions_types_second_signature_tests] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_2_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_types_second_signature_tests.src_filter} -[env:esp8266_transactions_transaction_to_array_type_3_tests] +######################################################### +; ESP8266 - Transactions - Types - Delegate Registration +[env:esp8266_transactions_types_delegate_registration_tests] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_transaction_to_array_type_3_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_types_delegate_registration_tests.src_filter} -[env:esp8266_transactions_transaction_to_json_type_0_tests] +######################################## +; ESP8266 - Transactions - Types - Vote +[env:esp8266_transactions_types_vote_tests] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_0_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_types_vote_tests.src_filter} -[env:esp8266_transactions_transaction_to_json_type_1_tests] +######################################## +; ESP8266 - Transactions - Types - Ipfs +[env:esp8266_transactions_types_ipfs_tests] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_1_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_types_ipfs_tests.src_filter} -[env:esp8266_transactions_transaction_to_json_type_2_tests] +################################################ +; ESP8266 - Transactions - Types - MultiPayment +[env:esp8266_transactions_types_multi_payment_tests] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_2_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_types_multi_payment_tests.src_filter} -[env:esp8266_transactions_transaction_to_json_type_3_tests] +######################################################## +; ESP8266 - Transactions - Types - Delegate Resignation +[env:esp8266_transactions_types_delegate_resignation_tests] extends = base, arduino, esp8266 -src_filter = ${base.src_filter} ${transactions_transaction_to_json_type_3_tests.src_filter} +src_filter = ${base.src_filter} ${transactions_types_delegate_resignation_tests.src_filter} + +############################################# +; ESP8266 - Transactions - Types - Htlc Lock +[env:esp8266_transactions_types_htlc_lock_tests] +extends = base, arduino, esp8266 +src_filter = ${base.src_filter} ${transactions_types_htlc_lock_tests.src_filter} + +############################################## +; ESP8266 - Transactions - Types - Htlc Claim +[env:esp8266_transactions_types_htlc_claim_tests] +extends = base, arduino, esp8266 +src_filter = ${base.src_filter} ${transactions_types_htlc_claim_tests.src_filter} + +################################################ +; ESP8266 - Transactions - Types - Htlc Refund +[env:esp8266_transactions_types_htlc_refund_tests] +extends = base, arduino, esp8266 +src_filter = ${base.src_filter} ${transactions_types_htlc_refund_tests.src_filter} + +######################################## +; ESP8266 - Transactions - Deserializer +[env:esp8266_transactions_deserializer_tests] +extends = base, arduino, esp8266 +src_filter = ${base.src_filter} ${transactions_deserializer_tests.src_filter} + +####################################### +; ESP8266 - Transactions - Serializer +[env:esp8266_transactions_serializer_tests] +extends = base, arduino, esp8266 +src_filter = ${base.src_filter} ${transactions_serializer_tests.src_filter} + +################################################################################ +; ESP8266 - Transactions - Transaction + +[env:esp8266_transactions_transaction_tests] +extends = base, arduino, esp8266 +src_filter = ${base.src_filter} ${transactions_transaction_tests.src_filter} + +################################################################################ +################################################################################ +; ESP8266 - Utils [env:esp8266_utils_tests] extends = base, arduino, esp8266 src_filter = ${base.src_filter} ${utils_tests.src_filter} +################################################################################ +################################################################################ + +################################################################################ +; ESP32 +################################################################################ + +################################################################################ +; ESP32 - Common + [env:esp32_common_tests] extends = base, arduino, esp32 src_filter = ${base.src_filter} ${common_tests.src_filter} +################################################################################ +################################################################################ +; ESP32 - Crypto + [env:esp32_crypto_tests] extends = base, arduino, esp32 src_filter = ${base.src_filter} ${crypto_tests.src_filter} -[env:esp32_defaults_tests] -extends = base, arduino, esp32 -src_filter = ${base.src_filter} ${defaults_tests.src_filter} +################################################################################ +################################################################################ +; ESP32 - Identities [env:esp32_identities_tests] extends = base, arduino, esp32 src_filter = ${base.src_filter} ${identities_tests.src_filter} +################################################################################ +; ESP32 - Managers + [env:esp32_managers_tests] extends = base, arduino, esp32 src_filter = ${base.src_filter} ${managers_tests.src_filter} +################################################################################ +; ESP32 - Networks + [env:esp32_networks_tests] extends = base, arduino, esp32 src_filter = ${base.src_filter} ${networks_tests.src_filter} +################################################################################ +; ESP32 - Transactions + [env:esp32_transactions_tests] extends = base, arduino, esp32 src_filter = ${base.src_filter} ${transactions_tests.src_filter} +################################################################################ +; ESP32 - Utils + [env:esp32_utils_tests] extends = base, arduino, esp32 src_filter = ${base.src_filter} ${utils_tests.src_filter} + +################################################################################ +################################################################################ diff --git a/test/test_helpers.h b/test/test_helpers.h new file mode 100644 index 00000000..02f64f68 --- /dev/null +++ b/test/test_helpers.h @@ -0,0 +1,44 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TEST_HELPERS_H +#define ARK_TEST_HELPERS_H + +#include +#include + +//////////////////////////////////////////////////////////////////////////////// +// Array comparison differs across architectures and environments. +// This helper method avoids have to loop each array for comparison. +// +// @param: const uint8_t *expected +// @param: const uint8_t *actual +// @param: const uint32_t length +// +// @return: bool +// - 'true' if the arrays match. +// - 'false' on mismatch. Also prints info to the console. +// +// --- +static inline bool array_cmp(const uint8_t *expected, + const uint8_t *actual, + const size_t &length) { + for (size_t i = 0UL; i < length; ++i) { + if (expected[i] != actual[i]) { + printf("\n\nMismatch at element: %zd", i); + printf("\nExpected value: %d", (int)expected[i]); + printf("\nActual value: %d\n\n", (int)actual[i]); + return false; + } + } + + return true; +} + +#endif diff --git a/test/transactions/builder.cpp b/test/transactions/builder.cpp deleted file mode 100644 index b8ba150d..00000000 --- a/test/transactions/builder.cpp +++ /dev/null @@ -1,35 +0,0 @@ - -#include "gtest/gtest.h" - -#include "transactions/builder.h" -#include "common/fee_policy.hpp" -using namespace Ark::Crypto::Transactions; -using namespace Ark::Crypto; - -TEST(transactions, build_transfer) { - const auto actual = - Builder::buildTransfer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 100000000ULL, - "", - "Secret passphrase"); - - ASSERT_EQ(0, actual.type); - ASSERT_EQ(StaticFeePolicy[actual.type], actual.fee); - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - actual.recipient.c_str()); - ASSERT_EQ(100000000ULL, actual.amount); - ASSERT_TRUE(actual.vendorField.empty()); -} - -TEST(transactions, build_empty_transaction) { - // test 0 ARKtoshi value - const auto shouldBeEmpty = Ark::Crypto::Transactions::Builder::buildTransfer( - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 0, - "", - "Secret passphrase"); - - ASSERT_TRUE(shouldBeEmpty.recipient.empty()); - ASSERT_EQ(0ULL, shouldBeEmpty.amount); - ASSERT_FALSE(shouldBeEmpty.verify()); -} diff --git a/test/transactions/builder_transfer_custom_network.cpp b/test/transactions/builder_transfer_custom_network.cpp deleted file mode 100644 index 5a88a4ef..00000000 --- a/test/transactions/builder_transfer_custom_network.cpp +++ /dev/null @@ -1,34 +0,0 @@ - -#include "gtest/gtest.h" - -#include "transactions/builder.h" -using namespace Ark::Crypto::Transactions; - -#include "common/network.hpp" -using namespace Ark::Crypto; - -TEST(transactions, build_transfer_custom_network) { - static const Network MyCustomNetwork = { - "16c891512149d6d3ff1b70e65900936140bf853a4ae79b5515157981dcc706df", - 1, 0x53, 0xaa, - "2019-04-12T13:00:00.000Z" - }; - - const Configuration myCustomConfiguration(MyCustomNetwork); - - const auto transaction = Builder::buildTransfer( - "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 100000000ULL, - "this is a custom bridgechain transaction", - "this is a top secret passphrase", - "this is a top secret passphrase too", - myCustomConfiguration); - - ASSERT_EQ(0, transaction.type); - ASSERT_EQ(myCustomConfiguration.getFee(transaction.type), transaction.fee); - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - transaction.recipient.c_str()); - ASSERT_EQ(100000000ULL, transaction.amount); - ASSERT_STREQ("this is a custom bridgechain transaction", - transaction.vendorField.c_str()); -} diff --git a/test/transactions/builders/delegate_registration.cpp b/test/transactions/builders/delegate_registration.cpp new file mode 100644 index 00000000..1ff9c31e --- /dev/null +++ b/test/transactions/builders/delegate_registration.cpp @@ -0,0 +1,50 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "transactions/builders/delegate_registration.hpp" + +#include "interfaces/constants.h" + +#include "fixtures/identity.hpp" +#include "transactions/types/fixtures/delegate_registration.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builders, delegate_registration) { + const auto transaction = builder::DelegateRegistration() + .network(COMMON_MAINNET) + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .username(TYPE_2_USERNAME, sizeof(TYPE_2_USERNAME)) + .fee(TYPE_2_FEE) + .signature(TYPE_2_SIGNATURE, sizeof(TYPE_2_SIGNATURE)) + .build(); + + ASSERT_TRUE(transaction.verify()); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builders, delegate_registration_string) { + const auto transaction = builder::DelegateRegistration() + .network(COMMON_MAINNET) + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .username(TYPE_2_USERNAME_STRING) + .fee(TYPE_2_FEE) + .signature(TYPE_2_SIGNATURE, sizeof(TYPE_2_SIGNATURE)) + .build(); + + ASSERT_TRUE(transaction.verify()); +} diff --git a/test/transactions/builders/htlc_claim.cpp b/test/transactions/builders/htlc_claim.cpp new file mode 100644 index 00000000..1e281131 --- /dev/null +++ b/test/transactions/builders/htlc_claim.cpp @@ -0,0 +1,37 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "transactions/builders/htlc_claim.hpp" + +#include "interfaces/constants.h" + +#include "fixtures/identity.hpp" +#include "transactions/types/fixtures/htlc_claim.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builders, htlc_claim) { + const auto transaction = builder::HtlcClaim() + .network(COMMON_MAINNET) + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .fee(TYPE_9_FEE) + .lockTransactionId(TYPE_9_LOCK_TX_ID) + .unlockSecret(TYPE_9_UNLOCK_SECRET) + .signature(TYPE_9_SIGNATURE, sizeof(TYPE_9_SIGNATURE)) + .build(); + + ASSERT_TRUE(transaction.verify()); +} diff --git a/test/transactions/builders/htlc_lock.cpp b/test/transactions/builders/htlc_lock.cpp new file mode 100644 index 00000000..3d796c7b --- /dev/null +++ b/test/transactions/builders/htlc_lock.cpp @@ -0,0 +1,58 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "transactions/builders/htlc_lock.hpp" + +#include "interfaces/constants.h" + +#include "fixtures/identity.hpp" +#include "transactions/types/fixtures/htlc_lock.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builders, htlc_lock) { + const auto transaction = builder::HtlcLock() + .network(COMMON_MAINNET) + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .fee(TYPE_8_FEE) + .amount(TYPE_8_AMOUNT) + .secretHash(TYPE_8_SECRET_HASH) + .expirationType(TYPE_8_EXPIRATION_TYPE) + .expiration(TYPE_8_EXPIRATION_VALUE) + .recipientId(TYPE_8_RECIPIENT) + .signature(TYPE_8_SIGNATURE, sizeof(TYPE_8_SIGNATURE)) + .build(); + + ASSERT_TRUE(transaction.verify()); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builders, htlc_lock_string) { + const auto transaction = builder::HtlcLock() + .network(COMMON_MAINNET) + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .fee(TYPE_8_FEE) + .amount(TYPE_8_AMOUNT) + .secretHash(TYPE_8_SECRET_HASH) + .expirationType(TYPE_8_EXPIRATION_TYPE) + .expiration(TYPE_8_EXPIRATION_VALUE) + .recipientId(TYPE_8_RECIPIENT_STRING) + .signature(TYPE_8_SIGNATURE, sizeof(TYPE_8_SIGNATURE)) + .build(); + + ASSERT_TRUE(transaction.verify()); +} diff --git a/test/transactions/builders/htlc_refund.cpp b/test/transactions/builders/htlc_refund.cpp new file mode 100644 index 00000000..26dba3d7 --- /dev/null +++ b/test/transactions/builders/htlc_refund.cpp @@ -0,0 +1,36 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "transactions/builders/htlc_refund.hpp" + +#include "interfaces/constants.h" + +#include "fixtures/identity.hpp" +#include "transactions/types/fixtures/htlc_refund.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builders, htlc_refund) { + const auto transaction = builder::HtlcRefund() + .network(COMMON_MAINNET) + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .fee(TYPE_10_FEE) + .lockTransactionId(TYPE_10_LOCK_TX_ID) + .signature(TYPE_10_SIGNATURE, sizeof(TYPE_10_SIGNATURE)) + .build(); + + ASSERT_TRUE(transaction.verify()); +} diff --git a/test/transactions/builders/ipfs.cpp b/test/transactions/builders/ipfs.cpp new file mode 100644 index 00000000..6c1b7934 --- /dev/null +++ b/test/transactions/builders/ipfs.cpp @@ -0,0 +1,36 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "transactions/builders/ipfs.hpp" + +#include "interfaces/constants.h" + +#include "fixtures/identity.hpp" +#include "transactions/types/fixtures/ipfs.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builders, ipfs) { + const auto transaction = builder::Ipfs() + .network(COMMON_MAINNET) + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .fee(TYPE_5_FEE) + .ipfs(TYPE_5_DAG, sizeof(TYPE_5_DAG)) + .signature(TYPE_5_SIGNATURE, sizeof(TYPE_5_SIGNATURE)) + .build(); + + ASSERT_TRUE(transaction.verify()); +} diff --git a/test/transactions/builders/multi_payment.cpp b/test/transactions/builders/multi_payment.cpp new file mode 100644 index 00000000..eae9e19c --- /dev/null +++ b/test/transactions/builders/multi_payment.cpp @@ -0,0 +1,43 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "transactions/builders/multi_payment.hpp" + +#include "interfaces/constants.h" + +#include "fixtures/identity.hpp" +#include "transactions/types/fixtures/multi_payment.hpp" + +#include "test_helpers.h" + +#include "utils/platform.h" + +#ifndef USE_IOT + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builders, multi_payment) { + const auto transaction = builder::MultiPayment() + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .fee(TYPE_6_FEE) + .n_payments(TYPE_6_N_PAYMENTS) + .amounts(TYPE_6_AMOUNTS) + .addresses(TYPE_6_ADDRESSES) + .signature(TYPE_6_SIGNATURE, sizeof(TYPE_6_SIGNATURE)) + .build(); + + ASSERT_TRUE(transaction.verify()); +} + +#endif // #ifndef USE_IOT diff --git a/test/transactions/builders/second_signature.cpp b/test/transactions/builders/second_signature.cpp new file mode 100644 index 00000000..32d55d12 --- /dev/null +++ b/test/transactions/builders/second_signature.cpp @@ -0,0 +1,36 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "transactions/builders/second_signature.hpp" + +#include "interfaces/constants.h" + +#include "fixtures/identity.hpp" +#include "transactions/types/fixtures/second_signature.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builders, second_signature) { + const auto transaction = builder::SecondSignature() + .network(COMMON_MAINNET) + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .fee(TYPE_1_FEE) + .publicKey(TYPE_1_SECOND_PUBLICKEY) + .signature(TYPE_1_SIGNATURE, sizeof(TYPE_1_SIGNATURE)) + .build(); + + ASSERT_TRUE(transaction.verify()); +} diff --git a/test/transactions/builders/transfer.cpp b/test/transactions/builders/transfer.cpp new file mode 100644 index 00000000..ff6a2839 --- /dev/null +++ b/test/transactions/builders/transfer.cpp @@ -0,0 +1,147 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "transactions/builders/transfer.hpp" + +#include "interfaces/constants.h" + +#include "common/configuration.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/message.hpp" +#include "transactions/types/fixtures/common.hpp" +#include "transactions/types/fixtures/transfer.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builders, transfer_address_hash) { + const auto transaction = builder::Transfer() + .network(COMMON_MAINNET) + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .fee(TYPE_0_FEE) + .amount(TYPE_0_AMOUNT) + .expiration(TYPE_0_EXPIRATION) + .recipientId(TYPE_0_RECIPIENT) + .signature(TYPE_0_SIGNATURE, sizeof(TYPE_0_SIGNATURE)) + .build(); + + ASSERT_EQ(COMMON_VERSION_V2, transaction.data.version); + ASSERT_EQ(COMMON_MAINNET, transaction.data.network); + ASSERT_EQ(COMMON_TYPEGROUP, transaction.data.typeGroup); + ASSERT_EQ(TYPE_0_TYPE, transaction.data.type); + ASSERT_EQ(COMMON_NONCE, transaction.data.nonce); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + transaction.data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(TYPE_0_FEE, transaction.data.fee); + + ASSERT_EQ(TYPE_0_AMOUNT, transaction.data.asset.transfer.amount); + + ASSERT_EQ(TYPE_0_EXPIRATION, transaction.data.asset.transfer.expiration); + + ASSERT_TRUE(array_cmp(TYPE_0_RECIPIENT, + transaction.data.asset.transfer.recipientId.data(), + ADDRESS_HASH_LEN)); + + ASSERT_TRUE(array_cmp(TYPE_0_SIGNATURE, + transaction.data.signature.data(), + sizeof(TYPE_0_SIGNATURE))); + + ASSERT_TRUE(transaction.verify()); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builder, transfer_address_string) { + const std::vector signature = { + TYPE_0_SIGNATURE, + TYPE_0_SIGNATURE + sizeof(TYPE_0_SIGNATURE) + }; + + const auto transaction = builder::Transfer() + .network(COMMON_MAINNET) + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .fee(TYPE_0_FEE) + .amount(TYPE_0_AMOUNT) + .expiration(TYPE_0_EXPIRATION) + .recipientId(TYPE_0_RECIPIENT_ID_STRING) + .signature(TYPE_0_SIGNATURE, sizeof(TYPE_0_SIGNATURE)) + .build(); + + ASSERT_TRUE(transaction.verify()); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builder, transfer_sign) { + const auto transaction = builder::Transfer() + .network(COMMON_MAINNET) + .nonce(COMMON_NONCE) + .fee(TYPE_0_FEE) + .amount(TYPE_0_AMOUNT) + .expiration(TYPE_0_EXPIRATION) + .recipientId(TYPE_0_RECIPIENT) + .sign(fixtures::Passphrase) + .build(); + + ASSERT_TRUE(transaction.verify()); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builder, transfer_sign_configuration) { + const Network Radians = { + "f39a61f04d6136a690a0b675ef6eedbd053665bd343b4e4f03311f12065fb875", + 1, 0xCE, 0x41, + "2019-10-25T09:05:40.856Z" + }; + + const Configuration radiansCfg(Radians); + + const AddressHash radiansRecipient = { + 65, 29, 252, 105, 181, 76, 127, 233, 1, 233, 29, + 90, 154, 183, 131, 136, 100, 94, 36, 39, 234 }; + + auto transaction = builder::Transfer() + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .fee(TYPE_0_FEE) + .amount(TYPE_0_AMOUNT) + .expiration(TYPE_0_EXPIRATION) + .recipientId(radiansRecipient.data()) + .build(radiansCfg); + + transaction.sign(fixtures::Passphrase); + + ASSERT_TRUE(transaction.verify()); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builder, transfer_sign_vendorfield_second_signature) { + const auto transaction = builder::Transfer() + .network(COMMON_MAINNET) + .nonce(COMMON_NONCE) + .fee(TYPE_0_FEE) + .vendorField(fixtures::MessageString) + .amount(TYPE_0_AMOUNT) + .expiration(TYPE_0_EXPIRATION) + .recipientId(TYPE_0_RECIPIENT) + .sign(fixtures::Passphrase) + .secondSign(fixtures::SecondPassphrase) + .build(); + + ASSERT_TRUE(transaction.verify()); +} diff --git a/test/transactions/builders/vote.cpp b/test/transactions/builders/vote.cpp new file mode 100644 index 00000000..a5b06d8e --- /dev/null +++ b/test/transactions/builders/vote.cpp @@ -0,0 +1,37 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "transactions/builders/vote.hpp" + +#include "interfaces/constants.h" + +#include "fixtures/identity.hpp" +#include "transactions/types/fixtures/vote.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_builders, vote) { + const auto transaction = builder::Vote() + .network(COMMON_MAINNET) + .type(TYPE_3_TYPE) + .nonce(COMMON_NONCE) + .senderPublicKey(fixtures::PublicKeyBytes.data()) + .fee(TYPE_3_FEE) + .votes(TYPE_3_VOTE) + .signature(TYPE_3_SIGNATURE, sizeof(TYPE_3_SIGNATURE)) + .build(); + + ASSERT_TRUE(transaction.verify()); +} diff --git a/test/transactions/defaults/fees_types.cpp b/test/transactions/defaults/fees_types.cpp new file mode 100644 index 00000000..f88b68fb --- /dev/null +++ b/test/transactions/defaults/fees_types.cpp @@ -0,0 +1,33 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "transactions/defaults/fees.hpp" +#include "transactions/defaults/types.hpp" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_defaults, fees_and_types) { // NOLINT + const auto feePolicy = StaticFeePolicy; + + ASSERT_EQ(TRANSFER_FEE, feePolicy.at(TRANSFER_TYPE)); + ASSERT_EQ(SECOND_SIGNATURE_FEE, feePolicy.at(SECOND_SIGNATURE_TYPE)); + ASSERT_EQ(DELEGATE_REGISTRATION_FEE, feePolicy.at(DELEGATE_REGISTRATION_TYPE)); + ASSERT_EQ(VOTE_FEE, feePolicy.at(VOTE_TYPE)); + ASSERT_EQ(MULTI_SIGNATURE_FEE, feePolicy.at(MULTI_SIGNATURE_TYPE)); + ASSERT_EQ(IPFS_FEE, feePolicy.at(IPFS_TYPE)); + ASSERT_EQ(MULTI_PAYMENT_FEE, feePolicy.at(MULTI_PAYMENT_TYPE)); + ASSERT_EQ(DELEGATE_RESIGNATION_FEE, feePolicy.at(DELEGATE_RESIGNATION_TYPE)); + ASSERT_EQ(HTLC_LOCK_FEE, feePolicy.at(HTLC_LOCK_TYPE)); + ASSERT_EQ(HTLC_CLAIM_FEE, feePolicy.at(HTLC_CLAIM_TYPE)); + ASSERT_EQ(HTLC_REFUND_FEE, feePolicy.at(HTLC_REFUND_TYPE)); +} diff --git a/test/transactions/deserializer.cpp b/test/transactions/deserializer.cpp new file mode 100644 index 00000000..fc925526 --- /dev/null +++ b/test/transactions/deserializer.cpp @@ -0,0 +1,50 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "transactions/deserializer.hpp" +#include "transactions/transaction_data.hpp" + +#include "transactions/defaults/offsets.hpp" + +#include "types/fixtures/transfer.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +// Break deserialization with an invalid version. +// All other cases are tested via transaction types. +TEST(transactions_deserializer, deserialize_invalid_version) { + TransactionData data; + + // break serialization by changing the Tx version to 0. + // the API only accepts v1 or v2. + auto invalidBytes = TYPE_0_BYTES; + invalidBytes.at(1) = 0U; + + ASSERT_FALSE(Deserializer::deserialize(&data, invalidBytes)); + +} + +//////////////////////////////////////////////////////////////////////////////// +// Deserialize with an unknown type. +// For compatiblity with future Tx types, this should not fail. +// All other cases are tested via transaction types. +TEST(transactions_deserializer, deserialize_unknown_type_no_fail) { + TransactionData data; + + auto unknownTypeBytes = TYPE_0_BYTES; + const auto unknowntype = 100U; + unknownTypeBytes.at(TYPE_OFFSET) = unknowntype; + + ASSERT_TRUE(Deserializer::deserialize(&data, unknownTypeBytes)); +} diff --git a/test/transactions/deserializer_delegate_registration.cpp b/test/transactions/deserializer_delegate_registration.cpp deleted file mode 100644 index 680ce442..00000000 --- a/test/transactions/deserializer_delegate_registration.cpp +++ /dev/null @@ -1,41 +0,0 @@ - -#include "gtest/gtest.h" - -#include - -#if 0 -TEST(transactions, deserialize_delegate_registration) { // NOLINT - // delegate_registration/second-passphrase.json - Ark::Crypto::Transactions::Deserializer deserializer( - "ff011e02b0b87502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200f902950000000000" - "09626f6c646e696e6a613045022100f21b742fa052cd18de43328e1d068539ba7cbe9d33a9dcbd862a82871383955d022005" - "3b06d22ed3e3ad6168c6b27aa0ec68e7e40958c7709aec0e1555087ea9ad94304402207da580da4feec955edcb8e8eb36947" - "867b439de3d28d38e58c844fd8c45b564302200e6741b6ad11c2588a57b3afd180df1e9b345d48a9c2ae98be57dced869cf38c" - ); - auto actual = deserializer.deserialize(); - - ASSERT_EQ(0xFF, actual.header); - ASSERT_EQ(2, actual.type); - ASSERT_EQ(1, actual.version); - ASSERT_EQ(30, actual.network); - ASSERT_EQ(TransactionTypes::DelegateRegistration, actual.type); - ASSERT_EQ(41269424UL, actual.timestamp); - ASSERT_STREQ( - "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", - actual.senderPublicKey.c_str()); - ASSERT_EQ(2500000000ULL, actual.fee); - ASSERT_EQ(0ULL, actual.amount); - ASSERT_EQ(0, actual.expiration); - ASSERT_STREQ( - "bf7e018ff9c0066f7a9f51e95d3f78c08cad5dd8581325d630d64350181a91bb", - actual.id.c_str()); - ASSERT_STREQ("boldninja", actual.asset.delegate.username.c_str()); - ASSERT_STREQ( - "3045022100f21b742fa052cd18de43328e1d068539ba7cbe9d33a9dcbd862a82871383955d0220053b06d22ed3e3ad6168c6b27aa0ec68e7e40958c7709aec0e1555087ea9ad94", - actual.signature.c_str()); - ASSERT_STREQ( - "304402207da580da4feec955edcb8e8eb36947867b439de3d28d38e58c844fd8c45b564302200e6741b6ad11c2588a57b3afd180df1e9b345d48a9c2ae98be57dced869cf38c", - actual.secondSignature.c_str()); - ASSERT_TRUE(actual.verify())); -} -#endif diff --git a/test/transactions/deserializer_multi_signature_registration.cpp b/test/transactions/deserializer_multi_signature_registration.cpp deleted file mode 100644 index 9d0521de..00000000 --- a/test/transactions/deserializer_multi_signature_registration.cpp +++ /dev/null @@ -1,77 +0,0 @@ - -#include "gtest/gtest.h" - -#include -#include -#include - -#include "defaults/transaction_types.hpp" -#include "identities/address.hpp" -#include "identities/publickey.hpp" -#include "transactions/deserializer.h" -using namespace Ark::Crypto; - -TEST(transactions, deserialize_multi_signature_registration) { // NOLINT - // multi_signature_registration/passphrase.json - Transactions::Deserializer deserializer( - "ff011704724c9a00036928c98ee53a1f52ed01dd87db10ffe1980eb47cd7c0a7d688321f47b5d7d76000943577000000000002031803543c" - "6cc3545be6bac09c82721973a052c690658283472e88f24d14739f75acc80276dc5b8706a85ca9fdc46e571ac84e52fbb48e13ec7a165a80" - "731b44ae89f1fc02e8d5d17eb17bbc8d7bf1001d29a2d25d1249b7bb7a5b7ad8b7422063091f4b3130440220324d89c5792e4a54ae70b4f1" - "e27e2f87a8b7169cc6f2f7b2c83dba894960f987022053b8d0ae23ff9d1769364db7b6fd03216d93753c82a711c3558045e787bc01a53044" - "02201fcd54a9ac9c0269b8cec213566ddf43207798e2cf9ca1ce3c5d315d66321c6902201aa94c4ed3e5e479a12220aa886b259e488eb89b" - "697c711f91e8c03b9620e0b1ff304502210097f17c8eecf36f86a967cc52a83fa661e4ffc70cc4ea08df58673669406d424c0220798f5710" - "897b75dda42f6548f841afbe4ed1fa262097112cf5a1b3f7dade60e4304402201a4a4c718bfdc699bbb891b2e89be018027d2dcd10640b5d" - "df07802424dab78e02204ec7c7d505d2158c3b51fdd3843d16aecd2eaaa4c6c7a555ef123c5e59fd41fb304402207e660489bced5ce80c33" - "d45c86781b63898775ab4a231bb48780f97b40073a63022026f0cefd0d83022d822522ab4366a82e3b89085c328817919939f2efeabd913d"); - auto actual = deserializer.deserialize(); - - ASSERT_EQ(0xFF, actual.header); - ASSERT_EQ(1, actual.version); - ASSERT_EQ(23, actual.network); - ASSERT_EQ(TransactionTypes::MultiSignatureRegistration, actual.type); - ASSERT_EQ(10112114UL, actual.timestamp); - ASSERT_STREQ("036928c98ee53a1f52ed01dd87db10ffe1980eb47cd7c0a7d688321f47b5d7d760", actual.senderPublicKey.c_str()); - ASSERT_TRUE(2000000000ULL == actual.fee); - ASSERT_TRUE(0ULL == actual.amount); - ASSERT_EQ(0, actual.expiration); - - std::vector keysgroup = { - "+03543c6cc3545be6bac09c82721973a052c690658283472e88f24d14739f75acc8", - "+0276dc5b8706a85ca9fdc46e571ac84e52fbb48e13ec7a165a80731b44ae89f1fc", - "+02e8d5d17eb17bbc8d7bf1001d29a2d25d1249b7bb7a5b7ad8b7422063091f4b31" - }; - - ASSERT_EQ(3, actual.asset.multiSignature.keysgroup.size()); - ASSERT_EQ(2, actual.asset.multiSignature.min); - ASSERT_EQ(24, actual.asset.multiSignature.lifetime); - - for (uint8_t i = 0; i < keysgroup.size(); i++) { - ASSERT_STREQ(keysgroup[i].c_str(), actual.asset.multiSignature.keysgroup[i].c_str()); - }; - - ASSERT_STREQ("cbd6862966bb1b03ba742397b7e5a88d6eefb393a362ead0d605723b840db2af", actual.id.c_str()); - - ASSERT_STREQ( - "30440220324d89c5792e4a54ae70b4f1e27e2f87a8b7169cc6f2f7b2c83dba894960f987022053b8d0ae23ff9d1769364db7b6fd03216d93753c82a711c3558045e787bc01a5", - actual.signature.c_str()); - ASSERT_STREQ( - "304402201fcd54a9ac9c0269b8cec213566ddf43207798e2cf9ca1ce3c5d315d66321c6902201aa94c4ed3e5e479a12220aa886b259e488eb89b697c711f91e8c03b9620e0b1", - actual.secondSignature.c_str()); - - std::vector signatures = { - "304502210097f17c8eecf36f86a967cc52a83fa661e4ffc70cc4ea08df58673669406d424c0220798f5710897b75dda42f6548f841afbe4ed1fa262097112cf5a1b3f7dade60e4", - "304402201a4a4c718bfdc699bbb891b2e89be018027d2dcd10640b5ddf07802424dab78e02204ec7c7d505d2158c3b51fdd3843d16aecd2eaaa4c6c7a555ef123c5e59fd41fb", - "304402207e660489bced5ce80c33d45c86781b63898775ab4a231bb48780f97b40073a63022026f0cefd0d83022d822522ab4366a82e3b89085c328817919939f2efeabd913d"}; - - ASSERT_EQ(3, actual.signatures.size()); - for (uint8_t i = 0; i < signatures.size(); i++) { - ASSERT_STREQ(signatures[i].c_str(), actual.signatures[i].c_str()); - } - - ASSERT_TRUE(actual.verify()); - - // special case as the type 4 transaction itself has no recipient - const auto publicKey = Ark::Crypto::identities::PublicKey::fromHex(actual.senderPublicKey.c_str()); - const auto address = Ark::Crypto::identities::Address::fromPublicKey(publicKey.toBytes().data(), actual.network); - ASSERT_STREQ(address.toString().c_str(), actual.recipient.c_str()); -} diff --git a/test/transactions/deserializer_second_signature_registration.cpp b/test/transactions/deserializer_second_signature_registration.cpp deleted file mode 100644 index 2de4d86e..00000000 --- a/test/transactions/deserializer_second_signature_registration.cpp +++ /dev/null @@ -1,44 +0,0 @@ - -#include "gtest/gtest.h" - -#include "defaults/transaction_types.hpp" -#include "identities/address.hpp" -#include "identities/publickey.hpp" -#include "transactions/deserializer.h" -using namespace Ark::Crypto; - -TEST(transactions, deserialize_second_signature_registration) { // NOLINT - // second_signature_registration/second-passphrase.json - Ark::Crypto::Transactions::Deserializer deserializer( - "ff011e013bc27502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed1920065cd1d000000000003" - "699e966b2525f9088a6941d8d94f7869964a000efe65783d78ac82e1199fe609304402202aab49477dd3531e4473196d08fbd7" - "c00ebb79223d5eaaeaf02c52c4041a86cf02201a7d82655f9b1d22af3ea94e6f183649bb4610cdeca3b9e20d6c8773f869831c"); - auto actual = deserializer.deserialize(); - - ASSERT_EQ(0xFF, actual.header); - ASSERT_EQ(1, actual.version); - ASSERT_EQ(30, actual.network); - ASSERT_EQ(TransactionTypes::SecondSignatureRegistration, actual.type); - ASSERT_EQ(41271867UL, actual.timestamp); - ASSERT_STREQ( - "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", - actual.senderPublicKey.c_str()); - ASSERT_TRUE(500000000ULL == actual.fee); - ASSERT_TRUE(0UL == actual.amount); - ASSERT_EQ(0, actual.expiration); - ASSERT_STREQ( - "6d1615924d172d352c8f44d4ded84cbbece3c03ebb3e4fc3f3334784ae332590", - actual.id.c_str()); - ASSERT_STREQ( - "03699e966b2525f9088a6941d8d94f7869964a000efe65783d78ac82e1199fe609", - actual.asset.signature.publicKey.c_str()); - ASSERT_STREQ( - "304402202aab49477dd3531e4473196d08fbd7c00ebb79223d5eaaeaf02c52c4041a86cf02201a7d82655f9b1d22af3ea94e6f183649bb4610cdeca3b9e20d6c8773f869831c", - actual.signature.c_str()); - ASSERT_TRUE(actual.verify()); - - // special case as the type 1 transaction itself has no recipient - const auto publicKey = Ark::Crypto::identities::PublicKey::fromHex(actual.senderPublicKey.c_str()); - const auto address = Ark::Crypto::identities::Address::fromPublicKey(publicKey.toBytes().data(), actual.network); - ASSERT_STREQ(address.toString().c_str(), actual.recipient.c_str()); -} diff --git a/test/transactions/deserializer_transfer.cpp b/test/transactions/deserializer_transfer.cpp deleted file mode 100644 index ddd631d5..00000000 --- a/test/transactions/deserializer_transfer.cpp +++ /dev/null @@ -1,36 +0,0 @@ - -#include "gtest/gtest.h" - -#include "defaults/transaction_types.hpp" -#include "transactions/deserializer.h" -using namespace Ark::Crypto; - -TEST(transactions, deserialize_transfer) { // NOLINT - // transfer/passphrase-with-vendor-field.json - Ark::Crypto::Transactions::Deserializer deserializer( - "ff011e0007627802034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19280969800000000000b48656c6c6f" - "20576f726c6400c2eb0b00000000000000001e0995750207ecaf0ccf251c1265b92ad84f553662304402205616d6e361439d67a5c2067b" - "bfc8fce61b93061a4fa113315a1c5cf965ff6f3202200a1d99caaa98aeebcec04edd5365352500addb830c79f49b9de484ec616bb1e1"); - auto actual = deserializer.deserialize(); - - ASSERT_EQ(0xFF, actual.header); - ASSERT_EQ(1, actual.version); - ASSERT_EQ(30, actual.network); - ASSERT_EQ(TransactionTypes::Transfer, actual.type); - ASSERT_EQ(41443847UL, actual.timestamp); - ASSERT_STREQ( - "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", - actual.senderPublicKey.c_str()); - ASSERT_TRUE(10000000ULL == actual.fee); - ASSERT_STREQ("48656c6c6f20576f726c64", actual.vendorFieldHex.c_str()); - ASSERT_STREQ("Hello World", actual.vendorField.c_str()); - ASSERT_TRUE(200000000ULL == actual.amount); - ASSERT_STREQ( - "ecf558fbddd62ae42edcfcba02f402d987a94b72a7636ef1121e8625487e2a1e", - actual.id.c_str()); - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", actual.recipient.c_str()); - ASSERT_STREQ( - "304402205616d6e361439d67a5c2067bbfc8fce61b93061a4fa113315a1c5cf965ff6f3202200a1d99caaa98aeebcec04edd5365352500addb830c79f49b9de484ec616bb1e1", - actual.signature.c_str()); - ASSERT_TRUE(actual.verify()); -} diff --git a/test/transactions/deserializer_vote.cpp b/test/transactions/deserializer_vote.cpp deleted file mode 100644 index 61282810..00000000 --- a/test/transactions/deserializer_vote.cpp +++ /dev/null @@ -1,43 +0,0 @@ - -#include "gtest/gtest.h" - -#include -#include - -#include "defaults/transaction_types.hpp" -#include "transactions/deserializer.h" -using namespace Ark::Crypto; - -TEST(transactions, deserialize_vote) { // NOLINT - // vote/second-passphrase.json - Ark::Crypto::Transactions::Deserializer deserializer( - "ff011e0376b87502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200e1f50500000000000101022cca95" - "29ec97a772156c152a00aad155ee6708243e65c9d211a589cb5d43234d304402204b8bb403e2db7f9599d46d0f5d39f8bb1d0663d875af7e" - "c1154448e98466e86302201e92fb57e13fb729b07e1027fa3d6e3f28e0d5828ed2d7c53a5e8db08cb6d068304402201329882762a42d1af9" - "079c822a9e3feefa47b7476b0afe61440637408958a64402206da179b08e31d9c784fbb23abe2c9b50353ed7881dc29787a5e8ecbee2dfda66"); - auto actual = deserializer.deserialize(); - - ASSERT_EQ(0xFF, actual.header); - ASSERT_EQ(1, actual.version); - ASSERT_EQ(30, actual.network); - ASSERT_EQ(TransactionTypes::Vote, actual.type); - ASSERT_EQ(41269366UL, actual.timestamp); - ASSERT_STREQ("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", actual.senderPublicKey.c_str()); - ASSERT_TRUE(100000000ULL == actual.fee); - ASSERT_TRUE(0ULL == actual.amount); - ASSERT_EQ(0, actual.expiration); - ASSERT_STREQ("16f28a180cd6f3ea46c10f358a457989e956e9d355258230d0c7b07acec10b73", actual.id.c_str()); - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", actual.recipient.c_str()); - - std::vector votes = {std::string("+022cca9529ec97a772156c152a00aad155ee6708243e65c9d211a589cb5d43234d")}; - ASSERT_EQ(1, actual.asset.votes.size()); - ASSERT_STREQ(votes[0].c_str(), actual.asset.votes[0].c_str()); - - ASSERT_STREQ( - "304402204b8bb403e2db7f9599d46d0f5d39f8bb1d0663d875af7ec1154448e98466e86302201e92fb57e13fb729b07e1027fa3d6e3f28e0d5828ed2d7c53a5e8db08cb6d068", - actual.signature.c_str()); - ASSERT_STREQ( - "304402201329882762a42d1af9079c822a9e3feefa47b7476b0afe61440637408958a64402206da179b08e31d9c784fbb23abe2c9b50353ed7881dc29787a5e8ecbee2dfda66", - actual.secondSignature.c_str()); - ASSERT_TRUE(actual.verify()); -} diff --git a/test/transactions/serializer.cpp b/test/transactions/serializer.cpp index e158b408..8cd2e60e 100644 --- a/test/transactions/serializer.cpp +++ b/test/transactions/serializer.cpp @@ -1,147 +1,80 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" -#include +#include -#include "transactions/serializer.h" +#include "transactions/serializer.hpp" +#include "transactions/transaction_data.hpp" -TEST(transactions, serialize_transfer) { // NOLINT - // transfer/passphrase-with-vendor-field.json - Ark::Crypto::Transactions::Transaction transaction; +#include "transactions/defaults/offsets.hpp" - transaction.type = 0; - transaction.fee = 10000000ULL; - transaction.amount = 200000000ULL; - transaction.timestamp = 41443847UL; - transaction.recipient = "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib"; - transaction.senderPublicKey = "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192"; - transaction.vendorField = "Hello World"; - transaction.signature = "304402205616d6e361439d67a5c2067bbfc8fce61b93061a4fa113315a1c5cf965ff6f3202200a1d99caaa98aeebcec04edd5365352500addb830c79f49b9de484ec616bb1e1"; +#include "fixtures/identity.hpp" +#include "types/fixtures/transfer.hpp" +#include "types/fixtures/common.hpp" - Ark::Crypto::Transactions::Serializer serializer(transaction); - std::string actual = serializer.serialize(); +#include "test_helpers.h" - ASSERT_STREQ( - "ff011e0007627802034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19280969800000000000b48656c6c6f" - "20576f726c6400c2eb0b00000000000000001e0995750207ecaf0ccf251c1265b92ad84f553662304402205616d6e361439d67a5c2067b" - "bfc8fce61b93061a4fa113315a1c5cf965ff6f3202200a1d99caaa98aeebcec04edd5365352500addb830c79f49b9de484ec616bb1e1", - actual.c_str()); -} +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; -/**/ +//////////////////////////////////////////////////////////////////////////////// +// Break serialization with an invalid version. +// All other cases are tested via transaction types. +TEST(transactions_serializer, serialize_invalid_version) { + TransactionData data; -TEST(transactions, serialize_second_signature_registration) { // NOLINT - // second_signature_registration/second-passphrase.json - Ark::Crypto::Transactions::Transaction transaction; + // break serialization. + // the API only accepts v1 or v2. + data.version = 0U; - transaction.type = 1; - transaction.fee = 500000000ULL; - transaction.timestamp = 41271867UL; - transaction.senderPublicKey = "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192"; - transaction.signature = "304402202aab49477dd3531e4473196d08fbd7c00ebb79223d5eaaeaf02c52c4041a86cf02201a7d82655f9b1d22af3ea94e6f183649bb4610cdeca3b9e20d6c8773f869831c"; - transaction.asset.signature.publicKey = "03699e966b2525f9088a6941d8d94f7869964a000efe65783d78ac82e1199fe609"; + data.network = COMMON_MAINNET; + data.typeGroup = COMMON_TYPEGROUP; + data.type = TYPE_0_TYPE; + data.nonce = COMMON_NONCE; - Ark::Crypto::Transactions::Serializer serializer(transaction); - std::string actual = serializer.serialize(); + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); - ASSERT_STREQ( - "ff011e013bc27502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed1920065cd1d000000000003" - "699e966b2525f9088a6941d8d94f7869964a000efe65783d78ac82e1199fe609304402202aab49477dd3531e4473196d08fbd7" - "c00ebb79223d5eaaeaf02c52c4041a86cf02201a7d82655f9b1d22af3ea94e6f183649bb4610cdeca3b9e20d6c8773f869831c", - actual.c_str()); -} + data.fee = TYPE_0_FEE; -/**/ - -TEST(transactions, serialize_delegate_registration) { // NOLINT - // delegate_registration/second-passphrase.json - Ark::Crypto::Transactions::Transaction transaction; - - transaction.type = 2; - transaction.fee = 2500000000ULL; - transaction.timestamp = 41269424UL; - transaction.senderPublicKey = "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192"; - transaction.signature = "3045022100f21b742fa052cd18de43328e1d068539ba7cbe9d33a9dcbd862a82871383955d0220053b06d22ed3e3ad6168c6b27aa0ec68e7e40958c7709aec0e1555087ea9ad94"; - transaction.secondSignature = "304402207da580da4feec955edcb8e8eb36947867b439de3d28d38e58c844fd8c45b564302200e6741b6ad11c2588a57b3afd180df1e9b345d48a9c2ae98be57dced869cf38c"; - transaction.asset.delegate.username = "boldninja"; - - Ark::Crypto::Transactions::Serializer serializer(transaction); - std::string actual = serializer.serialize(); - - ASSERT_STREQ( - "ff011e02b0b87502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200f902950000000000" - "09626f6c646e696e6a613045022100f21b742fa052cd18de43328e1d068539ba7cbe9d33a9dcbd862a82871383955d022005" - "3b06d22ed3e3ad6168c6b27aa0ec68e7e40958c7709aec0e1555087ea9ad94304402207da580da4feec955edcb8e8eb36947" - "867b439de3d28d38e58c844fd8c45b564302200e6741b6ad11c2588a57b3afd180df1e9b345d48a9c2ae98be57dced869cf38c", - actual.c_str()); -} + data.asset.transfer.amount = TYPE_0_AMOUNT; + data.asset.transfer.expiration = TYPE_0_EXPIRATION; + + std::copy_n(TYPE_0_RECIPIENT, + ADDRESS_HASH_LEN, + data.asset.transfer.recipientId.begin()); + + data.signature.insert(data.signature.begin(), + TYPE_0_SIGNATURE, + TYPE_0_SIGNATURE + sizeof(TYPE_0_SIGNATURE)); -/**/ - -TEST(transactions, serialize_vote) { // NOLINT - // vote/second-passphrase.json - Ark::Crypto::Transactions::Transaction transaction; - - transaction.type = 3; - transaction.fee = 100000000ULL; - transaction.timestamp = 41269366UL; - transaction.recipient = "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib"; - transaction.senderPublicKey = "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192"; - transaction.signature = "304402204b8bb403e2db7f9599d46d0f5d39f8bb1d0663d875af7ec1154448e98466e86302201e92fb57e13fb729b07e1027fa3d6e3f28e0d5828ed2d7c53a5e8db08cb6d068"; - transaction.secondSignature = "304402201329882762a42d1af9079c822a9e3feefa47b7476b0afe61440637408958a64402206da179b08e31d9c784fbb23abe2c9b50353ed7881dc29787a5e8ecbee2dfda66"; - transaction.asset.votes = { "+022cca9529ec97a772156c152a00aad155ee6708243e65c9d211a589cb5d43234d" }; - - Ark::Crypto::Transactions::Serializer serializer(transaction); - std::string actual = serializer.serialize(); - - ASSERT_STREQ( - "ff011e0376b87502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200e1f50500000000000101022cca95" - "29ec97a772156c152a00aad155ee6708243e65c9d211a589cb5d43234d304402204b8bb403e2db7f9599d46d0f5d39f8bb1d0663d875af7e" - "c1154448e98466e86302201e92fb57e13fb729b07e1027fa3d6e3f28e0d5828ed2d7c53a5e8db08cb6d068304402201329882762a42d1af9" - "079c822a9e3feefa47b7476b0afe61440637408958a64402206da179b08e31d9c784fbb23abe2c9b50353ed7881dc29787a5e8ecbee2dfda66", - actual.c_str()); + const auto serialized = Serializer::serialize(data); + + ASSERT_TRUE(serialized.empty()); } -/**/ - -TEST(transactions, serialize_multi_signature_registration) { // NOLINT - // multi_signature_registration/passphrase.json - Ark::Crypto::Transactions::Transaction transaction; - - transaction.type = 4; - transaction.version = 1; - transaction.network = 23; - transaction.fee = 2000000000ULL; - transaction.timestamp = 10112114UL; - transaction.senderPublicKey = "036928c98ee53a1f52ed01dd87db10ffe1980eb47cd7c0a7d688321f47b5d7d760"; - transaction.signature = "30440220324d89c5792e4a54ae70b4f1e27e2f87a8b7169cc6f2f7b2c83dba894960f987022053b8d0ae23ff9d1769364db7b6fd03216d93753c82a711c3558045e787bc01a5"; - transaction.secondSignature = "304402201fcd54a9ac9c0269b8cec213566ddf43207798e2cf9ca1ce3c5d315d66321c6902201aa94c4ed3e5e479a12220aa886b259e488eb89b697c711f91e8c03b9620e0b1"; - transaction.signatures = { - "304502210097f17c8eecf36f86a967cc52a83fa661e4ffc70cc4ea08df58673669406d424c0220798f5710897b75dda42f6548f841afbe4ed1fa262097112cf5a1b3f7dade60e4", - "304402201a4a4c718bfdc699bbb891b2e89be018027d2dcd10640b5ddf07802424dab78e02204ec7c7d505d2158c3b51fdd3843d16aecd2eaaa4c6c7a555ef123c5e59fd41fb", - "304402207e660489bced5ce80c33d45c86781b63898775ab4a231bb48780f97b40073a63022026f0cefd0d83022d822522ab4366a82e3b89085c328817919939f2efeabd913d" - }; - - transaction.asset.multiSignature.keysgroup = { - "+03543c6cc3545be6bac09c82721973a052c690658283472e88f24d14739f75acc8", - "+0276dc5b8706a85ca9fdc46e571ac84e52fbb48e13ec7a165a80731b44ae89f1fc", - "+02e8d5d17eb17bbc8d7bf1001d29a2d25d1249b7bb7a5b7ad8b7422063091f4b31" - }; - transaction.asset.multiSignature.lifetime = 24; - transaction.asset.multiSignature.min = 2; - - Ark::Crypto::Transactions::Serializer serializer(transaction); - std::string actual = serializer.serialize(); - - ASSERT_STREQ( - "ff011704724c9a00036928c98ee53a1f52ed01dd87db10ffe1980eb47cd7c0a7d688321f47b5d7d76000943577000000000002031803543c" - "6cc3545be6bac09c82721973a052c690658283472e88f24d14739f75acc80276dc5b8706a85ca9fdc46e571ac84e52fbb48e13ec7a165a80" - "731b44ae89f1fc02e8d5d17eb17bbc8d7bf1001d29a2d25d1249b7bb7a5b7ad8b7422063091f4b3130440220324d89c5792e4a54ae70b4f1" - "e27e2f87a8b7169cc6f2f7b2c83dba894960f987022053b8d0ae23ff9d1769364db7b6fd03216d93753c82a711c3558045e787bc01a53044" - "02201fcd54a9ac9c0269b8cec213566ddf43207798e2cf9ca1ce3c5d315d66321c6902201aa94c4ed3e5e479a12220aa886b259e488eb89b" - "697c711f91e8c03b9620e0b1ff304502210097f17c8eecf36f86a967cc52a83fa661e4ffc70cc4ea08df58673669406d424c0220798f5710" - "897b75dda42f6548f841afbe4ed1fa262097112cf5a1b3f7dade60e4304402201a4a4c718bfdc699bbb891b2e89be018027d2dcd10640b5d" - "df07802424dab78e02204ec7c7d505d2158c3b51fdd3843d16aecd2eaaa4c6c7a555ef123c5e59fd41fb304402207e660489bced5ce80c33" - "d45c86781b63898775ab4a231bb48780f97b40073a63022026f0cefd0d83022d822522ab4366a82e3b89085c328817919939f2efeabd913d", - actual.c_str()); +//////////////////////////////////////////////////////////////////////////////// +// Serialize with an unknown type. +// For compatiblity with future Tx types, this should not fail. +// All other cases are tested via transaction types. +TEST(transactions_serializer, serialize_unknown_type_no_fail) { + TransactionData data; + + const auto unknownType = 100U; + data.type = unknownType; + // auto unknownTypeBytes = TYPE_0_BYTES; + // unknownTypeBytes.at(TYPE_OFFSET) = unknowntype; + + const auto serialized = Serializer::serialize(data); + + ASSERT_FALSE(serialized.empty()); } diff --git a/test/transactions/transaction.cpp b/test/transactions/transaction.cpp index 59309b77..ecc4bce2 100644 --- a/test/transactions/transaction.cpp +++ b/test/transactions/transaction.cpp @@ -1,63 +1,282 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include -#include +#include + +#include "fixtures/identity.hpp" + +#include "identities/keys.hpp" + +#include "types/fixtures/common.hpp" +#include "types/fixtures/transfer.hpp" +#include "types/fixtures/second_signature.hpp" +#include "types/fixtures/delegate_registration.hpp" +#include "types/fixtures/vote.hpp" +#include "types/fixtures/ipfs.hpp" +#include "types/fixtures/multi_payment.hpp" +#include "types/fixtures/delegate_resignation.hpp" +#include "types/fixtures/htlc_lock.hpp" +#include "types/fixtures/htlc_claim.hpp" +#include "types/fixtures/htlc_refund.hpp" + +#include "transactions/transaction_data.hpp" +#include "transactions/transaction.hpp" + +#include "utils/hex.hpp" +#include "utils/platform.h" + +#include "test_helpers.h" -#include "transactions/transaction.h" -#include "utils/json.h" using namespace Ark::Crypto; -using namespace Ark::Crypto::Transactions; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transaction, constructor_default) { + Transaction transaction; + ASSERT_FALSE(transaction.verify()); +} -TEST(transactions, transaction_default) { - Transaction transaction; - ASSERT_FALSE(transaction.verify()); -}; +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transaction, get_id) { + Transaction transaction; -/**/ + transaction.data.network = COMMON_MAINNET; + transaction.data.typeGroup = COMMON_TYPEGROUP; + transaction.data.type = TYPE_0_TYPE; + transaction.data.nonce = COMMON_NONCE; -TEST(transactions, transaction_empty) { - Ark::Crypto::Transactions::Transaction transaction; - bool isValid = transaction.verify(); - ASSERT_FALSE(isValid); + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + transaction.data.senderPublicKey.begin()); + + transaction.data.fee = TYPE_0_FEE; + + transaction.data.asset.transfer.amount = TYPE_0_AMOUNT; + transaction.data.asset.transfer.expiration = TYPE_0_EXPIRATION; + + std::copy_n(TYPE_0_RECIPIENT, + ADDRESS_HASH_LEN, + transaction.data.asset.transfer.recipientId.begin()); + + transaction.data.signature.resize(sizeof(TYPE_0_SIGNATURE)); + std::copy_n(TYPE_0_SIGNATURE, + sizeof(TYPE_0_SIGNATURE), + transaction.data.signature.begin()); + + ASSERT_TRUE(array_cmp(TYPE_0_TX_ID, + transaction.getId().data(), + sizeof(TYPE_0_TX_ID))); + + ASSERT_TRUE(transaction.verify()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transaction, sign) { + Transaction transaction; + + transaction.data.network = COMMON_MAINNET; + transaction.data.typeGroup = COMMON_TYPEGROUP; + transaction.data.type = TYPE_0_TYPE; + transaction.data.nonce = COMMON_NONCE; -TEST(transactions, transaction_asset_signature) { // NOLINT - const auto publicKeyString = "02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699"; - TransactionAsset asset; - asset.signature.publicKey = publicKeyString; - ASSERT_STREQ(asset.signature.publicKey.c_str(), publicKeyString); + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + transaction.data.senderPublicKey.begin()); + + transaction.data.fee = TYPE_0_FEE; + + transaction.data.asset.transfer.amount = TYPE_0_AMOUNT; + transaction.data.asset.transfer.expiration = TYPE_0_EXPIRATION; + + std::copy_n(TYPE_0_RECIPIENT, + ADDRESS_HASH_LEN, + transaction.data.asset.transfer.recipientId.begin()); + + // Test with an Empty Passphrase. + transaction.sign(""); + + // Sign with a Valid Passphrase. + transaction.sign(fixtures::Passphrase); + + ASSERT_TRUE(array_cmp(TYPE_0_TX_ID, + transaction.getId().data(), + sizeof(TYPE_0_TX_ID))); + + ASSERT_TRUE(array_cmp(TYPE_0_SIGNATURE, + transaction.data.signature.data(), + sizeof(TYPE_0_SIGNATURE))); + + ASSERT_TRUE(transaction.verify()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transaction, sign_second) { + Transaction transaction; + + transaction.data.network = COMMON_MAINNET; + transaction.data.typeGroup = COMMON_TYPEGROUP; + transaction.data.type = TYPE_0_TYPE; + transaction.data.nonce = COMMON_NONCE; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + transaction.data.senderPublicKey.begin()); + + transaction.data.fee = TYPE_0_FEE; + transaction.data.asset.transfer.amount = TYPE_0_AMOUNT; + transaction.data.asset.transfer.expiration = TYPE_0_EXPIRATION; + + std::copy_n(TYPE_0_RECIPIENT, + ADDRESS_HASH_LEN, + transaction.data.asset.transfer.recipientId.begin()); + + ASSERT_TRUE(transaction.sign(fixtures::Passphrase)); + + // Test with an Empty Second Passphrase. + ASSERT_FALSE(transaction.secondSign("")); + + // Sign with a Valid SecondPassphrase. + ASSERT_TRUE(transaction.secondSign(fixtures::SecondPassphrase)); -TEST(transactions, transaction_asset_delegate) { - const auto testUsername = "testUsername"; - TransactionAsset asset; - asset.delegate.username = "testUsername"; - ASSERT_STREQ(asset.delegate.username.c_str(), testUsername); + const auto keys = identities::Keys::fromPassphrase(fixtures::SecondPassphrase); + + ASSERT_TRUE(transaction.verify()); + + ASSERT_TRUE(transaction.secondVerify(keys.publicKey.data())); + + // Check whether the secondSignature is present in 'toJson()', + // and internally, 'toMap()'. + auto txJson = transaction.toJson(); + + const auto secondSigJsonPos = 474; + ASSERT_EQ(secondSigJsonPos, txJson.find("secondSignature")); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transaction, deserialize) { + Transaction transaction; + ASSERT_TRUE(transaction.deserialize(TYPE_0_BYTES)); + ASSERT_TRUE(transaction.verify()); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transaction, deserialize_invalid) { + const uint8_t badSerialized[] = { 0xFF, 0x0a }; + Transaction transaction; + + ASSERT_FALSE(transaction.deserialize( + { badSerialized, badSerialized + sizeof(badSerialized) })); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transaction, serialize) { + Transaction transaction; + + ASSERT_TRUE(transaction.deserialize(TYPE_0_BYTES)); + + ASSERT_TRUE(array_cmp(TYPE_0_BYTES.data(), + transaction.serialize().data(), + TYPE_0_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transaction, serialize_invalid) { + const auto badVersion = 0x0a; + Transaction transaction; + + transaction.data.version = badVersion; + + ASSERT_TRUE(transaction.serialize().empty()); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transaction, to_bytes) { + Transaction transaction; + + ASSERT_TRUE(transaction.deserialize(TYPE_0_BYTES)); + + ASSERT_TRUE(array_cmp(TYPE_0_BYTES.data(), + transaction.toBytes().data(), + TYPE_0_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transaction, to_map_to_json) { // NOLINT + Transaction transferTx; + transferTx.deserialize(TYPE_0_BYTES); + const auto transferJson = transferTx.toJson(); + const std::string transferResponse = R"({"version":2,"network":23,"typeGroup":1,"type":0,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"10000000","amount":"1","expiration":0,"recipientId":"AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff","id":"aafbacad9b75fa664c9cecae6de552833d4c2ab6f857573cf492eafcebfd1551","signature":"3045022100d55051f5aec7d894afb987d582e63e0157b7a6e0cb8eff1d70ac81cbbb1382e90220294b89acb106e811b48a072a555915bea4aa0fc63c4210ad90115d1220b31b7d"})"; + ASSERT_STREQ(transferResponse.c_str(), transferJson.c_str()); + + Transaction secondSignatureTx; + secondSignatureTx.deserialize(TYPE_1_BYTES); + const auto secondSignatureJson = secondSignatureTx.toJson(); + const std::string secondSignatureResponse = R"({"version":2,"network":23,"typeGroup":1,"type":1,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"500000000","asset":{"signature":{"publicKey":"02877e4f35c76abaeb152b128670db0a7ae10b3999afcd28a42938b653fbf87ae9"}},"id":"6934f7a73846b2daeb94e70b3b8a23118410cb552ada5993bf1b47b10e168916","signature":"304402201786b482215f6f63e3a9414324283decffc64b829209640024a4e78d7ed557f50220197e53c8e8b7f8a645eb82712587926ad930695a8ca43a2308ca0a9dbeabb65b"})"; + ASSERT_STREQ(secondSignatureResponse.c_str(), secondSignatureJson.c_str()); + + Transaction delegateRegistrationTx; + delegateRegistrationTx.deserialize(TYPE_2_BYTES); + const auto delegateRegistrationJson = delegateRegistrationTx.toJson(); + const std::string delegateRegistrationResponse = R"({"version":2,"network":23,"typeGroup":1,"type":2,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"2500000000","asset":{"delegate":{"username":"arkdelegate"}},"id":"668f5db0d99189ee7dd85c991360ca19372302a13e3046511a60d115dbf1864e","signature":"30440220281812ca558b3b032aaf724aeaba5e20733e87f20fefe1c78536cc523de7e475022038a2454ca2665f6c3d910b04ecdd5995a7d82904f9f4bf878402a31262d2db61"})"; + ASSERT_STREQ(delegateRegistrationResponse.c_str(), delegateRegistrationJson.c_str()); + + Transaction voteTx; + voteTx.deserialize(TYPE_3_BYTES); + const auto voteJson = voteTx.toJson(); + const std::string voteResponse = R"({"version":2,"network":23,"typeGroup":1,"type":3,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"100000000","asset":{"votes":["+034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192"]},"id":"ca544581927728ae03a63f088e76053f357dc8fb0422512bb8a7c6a0f7ae16cf","signature":"3045022100dfa4edd1fb7bc587eb277dd996ab0dde73dc4942a293e5634bd48be9c7d25880022025c91f30c88f49bc3ed77614749c0c439f0759457a4bf22ed5c97f338659ccee"})"; + ASSERT_STREQ(voteResponse.c_str(), voteJson.c_str()); + + // Transaction multiSignatureTx; // TODO + + Transaction ipfsTx; + ipfsTx.deserialize(TYPE_5_BYTES); + const auto ipfsJson = ipfsTx.toJson(); + const std::string ipfsResponse = R"({"version":2,"network":23,"typeGroup":1,"type":5,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"500000000","asset":{"ipfs":"QmYSK2JyM3RyDyB52caZCTKFR3HKniEcMnNJYdk8DQ6KKB"},"id":"f68875f1fa80091ef42dec05c5c6a0ea77f4b83934f7eed0d1c72b910ddf6d03","signature":"30440220636aa75a15bcc59c9cf2db11ae7f9ed73f112b6ca14cc2aca1b6ed3d2daadaad02207434f5c6d17c98350942afafbaa57b6518ea4e78e8443f414398190183235787"})"; + ASSERT_STREQ(ipfsResponse.c_str(), ipfsJson.c_str()); + +// This MultiPayment is currently way too big for IoT. +// The MultiPayment type is supported on IoT; +// however, the limit is considerably less than the Network supports +// and is ultimately dependent on your working environment. +#ifndef USE_IOT + Transaction multiPaymentTx; + multiPaymentTx.deserialize(TYPE_6_BYTES); + const auto multiPaymentJson = multiPaymentTx.toJson(); + + const std::string multiPaymentResponse = R"({"version":2,"network":30,"typeGroup":1,"type":6,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"10000000","asset":{"payments":[{"amount":"1","recipientId":"DHKxXag9PjfjHBbPg3HQS5WCaQZdgDf6yi"},{"amount":"1","recipientId":"DBzGiUk8UVjB2dKCfGRixknB7Ki3Zhqthp"},{"amount":"1","recipientId":"DFa7vn1LvWAyTuVDrQUr5NKaM73cfjx2Cp"},{"amount":"1","recipientId":"DSGsxX84gif4ipAxZjjCE2k2YpHmsNTJeY"},{"amount":"1","recipientId":"DQhzMRvVoCYCiZH2iSyuqCTcayz7z4XTKx"},{"amount":"1","recipientId":"DMSD6fFT1Xcxh4ErYExr5MtGnEuDcYu22m"},{"amount":"1","recipientId":"D7HCZG8hJuJqu9rANRdyNr6N1vpH2vbyx8"},{"amount":"1","recipientId":"DQy6ny2bkvWiDLboQMZ1cxnmoNC5JM228w"},{"amount":"1","recipientId":"D7EUgmA78qUaSfsNedfgKs2ALq28FhL3zo"},{"amount":"1","recipientId":"DEHyKHdtzHqTghfpwaBcvTzLpgPP5AAUgE"},{"amount":"1","recipientId":"DBgA92a616rwVi9GsgYUwBq9Y7dgvZiC41"},{"amount":"1","recipientId":"DPXaJv1GcVpZPvxw5T4fXebqTVhFpfqyrC"},{"amount":"1","recipientId":"D6JpPhN7BehrhNy7AbSQ2u9mkSZb1k7Ens"},{"amount":"1","recipientId":"D9sdJ42YtJpXeL7Fa1cTLiciW7FpGYqms4"},{"amount":"1","recipientId":"DJq86RdmTMKC257szcXRXKpbuoYPaL8KgL"},{"amount":"1","recipientId":"DMsQKioFXniGH3vHDNfkQLRRqsQsAS46Cy"},{"amount":"1","recipientId":"DHwPoAP8cMP9ZeKrhh5c99WzaoJqFKW2qi"},{"amount":"1","recipientId":"DAwN6Pp4ErGf69EypErrbtuWFfEMtuSzmE"},{"amount":"1","recipientId":"DQ6sE3jE9rTFC13e2ndooRdy5YCYinLbPm"},{"amount":"1","recipientId":"DFuo2NGezzHGwHjzG6c21JuJ9WpntLGFER"},{"amount":"1","recipientId":"D6qzeJEGG7rEBem5bNCCZqHtPCBtzsUZpP"},{"amount":"1","recipientId":"DNVhLKTPh4LnqhcnkogNS8iSxmsnFG17tC"},{"amount":"1","recipientId":"D8EPxx42Dr4bd8xXRbMHi8LHefFLaT2VaM"},{"amount":"1","recipientId":"DBK4VPsUQHUYkGc47FqF69PEyPjjqvnGzu"},{"amount":"1","recipientId":"D7XtDDKh2VrRtz5rtbBicfgSEoEQzEZiNx"},{"amount":"1","recipientId":"D9gQjhu2tDUstXfrbK85zHi23VtAk72qsS"},{"amount":"1","recipientId":"DKhfkyY4RZyxR7CFjQAeNtGKXAaVEBa9HK"},{"amount":"1","recipientId":"DMCBerfV13HBuJEwJTZRVTWzrYDxgb3QSy"},{"amount":"1","recipientId":"DLCoxbHdf9LMhEavEnj8mGv4AwVk8eEiKd"},{"amount":"1","recipientId":"D5taz6B4xDk1LD3jV4fYrUhaKC8DnTtziW"},{"amount":"1","recipientId":"DDb3EXY3refv2f5ymMME3hp2DXFqMPzGah"},{"amount":"1","recipientId":"D5HydybffvfuwdbBKQ1dnhiXzNnWq6CgQz"},{"amount":"1","recipientId":"D9DMKvx8fDyWyAP1EUGs5McBwwv3y5E1Yn"},{"amount":"1","recipientId":"DHXqndno9dBvGabhc7NdZWuoj6nRUdSaP7"},{"amount":"1","recipientId":"DJAmJiuLQGnzXWmH7KvosVLks7hhfxQ8np"},{"amount":"1","recipientId":"D752ZwkQZKm5gYYMUZV2tKFFZZaD35MtRa"},{"amount":"1","recipientId":"D6Xe5kVsK7axaPZ1tP2fVWaLFubyCajkVq"},{"amount":"1","recipientId":"D9yDJNK4xHP9Gx27s187Z5XHcNF5YFA94h"},{"amount":"1","recipientId":"DJuZC2smL8j86bUNrZiNceAubad3zs3drS"},{"amount":"1","recipientId":"DRUVFo5MjNrMHHQCwpVPH6AwLL2AULpgbH"},{"amount":"1","recipientId":"DNM5wLmqVUz6UgY14mt6BsMndy8JGcFwct"},{"amount":"1","recipientId":"DHMd77xyB8f6DnSCgxaRWfuM86cwyH76EH"},{"amount":"1","recipientId":"DFmg9q2KqGyretLazvRjWdynJAZbPkZPG2"},{"amount":"1","recipientId":"DMnY8QWLAmsb4wNEjRVvtQsNWF4SbXntxM"},{"amount":"1","recipientId":"DBMn94FxVB36nXgzbmtmfu6jVEGwwHNyNA"},{"amount":"1","recipientId":"DUD3r46LtArk4msu6jFrwn1hjxbZoXzX9t"},{"amount":"1","recipientId":"DFUNVTBd5zFexBaHkymr4UJqsHeXhPLKUF"},{"amount":"1","recipientId":"DFtCxvMSsF9qfw2mH1aNHxusXGJ2QzCahP"},{"amount":"1","recipientId":"D8LiYnmH4DLDyxCLTV7RrqxtCA21pfGkb9"},{"amount":"1","recipientId":"DASUgX1U7yvp8WDQ57QoTEUim6bqTYzxGw"},{"amount":"1","recipientId":"D5iNaf5ZckhdZivPfy6vFvBLeBDJtvDoGo"},{"amount":"1","recipientId":"DPrdeuFDcfMujvYK6n18RBAWgh7hYeiDeZ"},{"amount":"1","recipientId":"D9oaC7bd2YaJYHDdGkUdyAnfpkBrFFKZHy"},{"amount":"1","recipientId":"DUTUfseKR6qJRjqCuxeH3oRxMr6EFLUxRW"},{"amount":"1","recipientId":"DTYv1v3YdUNy81kzD1VhRx6e8jkDYnvCoh"},{"amount":"1","recipientId":"DE1BK8iL17PiBwo9TUCfkkm1vnUkobBwj8"},{"amount":"1","recipientId":"D7Ba6DnbpPJgqVnQNdN7baRsZs1DKvptMM"},{"amount":"1","recipientId":"DUBcre2e5KMykYr6xK56T5BrwkKMZkUF8r"},{"amount":"1","recipientId":"DPeqoTgBbRhyuEJtMnhqhSAeK32ymMNvjd"},{"amount":"1","recipientId":"DGmToX3GrCEUC8EJdZrXWTYFNWqQz1VVhX"},{"amount":"1","recipientId":"DArHKTMXf3F5zXS1i3GSwni9aA8TX1yQvh"},{"amount":"1","recipientId":"DTgcCvAYR2XdzUHv4WuEB6aShYEbh2MYp6"},{"amount":"1","recipientId":"DFLY9huetM6GMrt9EGp6sXDEiC7r3oSYHg"},{"amount":"1","recipientId":"DEszKKqdRipXiF7BDKS2Q4iJwwfzLdwADK"},{"amount":"1","recipientId":"DF45FRKUYcyUGeZyyTF3sCYJ8VFNXymzhJ"},{"amount":"1","recipientId":"DJb6jvhSvw2RmxCBEQSAzG6tjUs5rK3a5m"},{"amount":"1","recipientId":"DCqqT4x4on1dsbgUKRWkuZsdZaoohYK6NV"},{"amount":"1","recipientId":"D9SAVjqkxwWQmb82iqAedJPccFjDUnMSi9"},{"amount":"1","recipientId":"DBvXesEgzrAsm9YrzRb2jithR2hg7SZpuq"},{"amount":"1","recipientId":"DF5ZYcQsmgDvH6cVQR87xvXapyTUFB1a5R"},{"amount":"1","recipientId":"DQEfNNsJ6PQTA9abwWdiunPuebLZAhxbpZ"},{"amount":"1","recipientId":"DP6k5YTtaeNJUHZ72H6QtugFaHVB5vEBQe"},{"amount":"1","recipientId":"DJBTrPo6sDMGr2kcswTwDWtQyYV5adqnAp"},{"amount":"1","recipientId":"DMHtTBMyG5qGYgcZNRkb36XaCT3TUSKmYE"},{"amount":"1","recipientId":"DTbCpyVgTeJw4idpqbY5jwrEi7SxSid9GU"},{"amount":"1","recipientId":"D75g1ztcaHi46eUFRnakRryqG7GV9xsgGC"},{"amount":"1","recipientId":"DSkMiPrEx3YF6ijDjxhwCnbAbriC8sWKEW"},{"amount":"1","recipientId":"D7BHGU3UedoxpZko4nBLcz5oRtSmUmRfy6"},{"amount":"1","recipientId":"DQZUzueTvUJb5tnhBCziPYaMnzaoui4F57"},{"amount":"1","recipientId":"DGCCpnJ86YvxJAkHRPhC5jTBNGsy5PEDRh"},{"amount":"1","recipientId":"DHSW3vi66L63xnzRt9PadwSVrb9bCKhgvJ"},{"amount":"1","recipientId":"D6eAmMh6FFynorCSjHS1Qx75rXiN89soa7"},{"amount":"1","recipientId":"DGPoaSg15fb6As8bPKBAQrK3nCDpgfYow8"},{"amount":"1","recipientId":"DKPmC4G1ZEwxb5hZro2sJwRWJ1pQixcK6N"},{"amount":"1","recipientId":"DFpBcXzcJdFaN9rfVD8Nc5yWFvJ3DnePwa"},{"amount":"1","recipientId":"DKxQ9FDTBDQaLLV24sWc625w2Kycw2RoqR"},{"amount":"1","recipientId":"DNZr7NxGm97r8hV6Jg4rv3S5MgJrEVUWNQ"},{"amount":"1","recipientId":"DBBbtnKKDAW84bDjywEmQFLgwf36DxJHZJ"},{"amount":"1","recipientId":"DA7GfDKG5zYFeiZ1C4FPJBeTajpYNvXxcC"},{"amount":"1","recipientId":"DPAsWQyuxYkiMRzhngKBPTpWb6WWqxdd1T"},{"amount":"1","recipientId":"DTv6qvhUK1jks58gAA5YFzShHf3YX9sJVo"},{"amount":"1","recipientId":"DDwTm5FbgvWugYekvjE1dzantAVKxtGbNo"},{"amount":"1","recipientId":"DTno4QZdEyAokZHxQZcYrErqMLVE19PgCb"},{"amount":"1","recipientId":"D5xRcwzEGSN83nyuGN74Sw8f353vDmm2tt"},{"amount":"1","recipientId":"DC1hKDKyFbtMhiTc79mmPS99SJnQLsvvH3"},{"amount":"1","recipientId":"DM1pVjbHA3Q4dezcwGBjmT54cLYqpx1NtZ"},{"amount":"1","recipientId":"DFEMw6jihEKRJ9CT3k8Rj73PLKDGDyQLU1"},{"amount":"1","recipientId":"D5nTPSQFkt9W6mNzdy5ks5PiHkhDqswxDY"},{"amount":"1","recipientId":"DMca1DGMxj8w59dWYmji1YC1xLP7AmL6rA"},{"amount":"1","recipientId":"DM6emC4WnxyP2RzrNW5VMS2n3sNfz6Xpci"}]},"id":"c9d689827cd6c6b741ebcad601ce5218bd7df96d2bf592aa5105116b89e09f80","signature":"3045022100a6c327cb6798cb3dc8b282b64358dc4c24a72bff61e347009c1e0ad2a1cc6d8c022028cf62c3d0ea8c3ca6113f63b4e827bd8db8201a478f5ff40c309bbb9987a9b4"})"; + ASSERT_STREQ(multiPaymentResponse.c_str(), multiPaymentJson.c_str()); +#endif // #ifndef USE_IOT + + Transaction delegateResignationTx; + delegateResignationTx.deserialize(TYPE_7_BYTES); + const auto delegateResignationJson = delegateResignationTx.toJson(); + const std::string delegateResignationResponse = R"({"version":2,"network":23,"typeGroup":1,"type":7,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"2500000000","id":"a8cf10c6a7d2e5c82bff0860f83f917f426bdd1ee25bd8d5dad2358973ce4ecb","signature":"3045022100aa113ddefb502d868219339d16b8d448c8e69c8e320664e12903cc84420c159d02201b6206c7cb9442f6ef07dd0824aeb0cc393d8f9f9909988da6687c44ef897070"})"; + ASSERT_STREQ(delegateResignationResponse.c_str(), delegateResignationJson.c_str()); + + Transaction htlcLockTx; + htlcLockTx.deserialize(TYPE_8_BYTES); + const auto htlcLockJson = htlcLockTx.toJson(); + const std::string htlcLockResponse = R"({"version":2,"network":23,"typeGroup":1,"type":8,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"10000000","amount":"1","recipientId":"AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff","asset":{"lock":{"secretHash":"09b9a28393efd02fcd76a21b0f0f55ba2aad8f3640ff8cae86de033a9cfbd78c","expiration":{"type":1,"value":81242895}}},"id":"50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e","signature":"3044022034491f6ee88278248c3b0ddd62b7ee0cb68e0ff93a51008899240b32d111b90a022043b10dd604f2d6256c75e50b5423612824a971c1876528af983ebb8145403e86"})"; + ASSERT_STREQ(htlcLockResponse.c_str(), htlcLockJson.c_str()); -TEST(transactions, transaction_asset_multisignature) { - const auto min = 2; - const auto lifetime = 24; - std::vector keysgroup = { - "+03543c6cc3545be6bac09c82721973a052c690658283472e88f24d14739f75acc8", - "+0276dc5b8706a85ca9fdc46e571ac84e52fbb48e13ec7a165a80731b44ae89f1fc", - "+02e8d5d17eb17bbc8d7bf1001d29a2d25d1249b7bb7a5b7ad8b7422063091f4b31" - }; + Transaction htlcClaimTx; + htlcClaimTx.deserialize(TYPE_9_BYTES); + const auto htlcClaimJson = htlcClaimTx.toJson(); + const std::string htlcClaimResponse = R"({"version":2,"network":23,"typeGroup":1,"type":9,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"0","asset":{"claim":{"lockTransactionId":"50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e","unlockSecret":"f5ea877a311ced90cf4524cb489e972f"}},"id":"88b21734d289fa48801b2fed39e23b89f021df4670e8d43e703b776e5a5333a8","signature":"3045022100d47a071dadebfae4e9115f35ae29429d0ada7bcb1668dc9babde9613108ecc8302201d97dd366a8816f81581dc5be46f3a859b66e48532c80fb7fa19da63dfc3c01f"})"; + ASSERT_STREQ(htlcClaimResponse.c_str(), htlcClaimJson.c_str()); - TransactionAsset asset; - asset.multiSignature.min = min; - asset.multiSignature.lifetime = lifetime; - asset.multiSignature.keysgroup = keysgroup; - ASSERT_EQ(asset.multiSignature.min, min); - ASSERT_EQ(asset.multiSignature.lifetime, lifetime); - ASSERT_TRUE(asset.multiSignature.keysgroup[0] == keysgroup[0]); - ASSERT_TRUE(asset.multiSignature.keysgroup[1] == keysgroup[1]); - ASSERT_TRUE(asset.multiSignature.keysgroup[2] == keysgroup[2]); + Transaction htlcRefundTx; + htlcRefundTx.deserialize(TYPE_10_BYTES); + const auto htlcRefundJson = htlcRefundTx.toJson(); + const std::string htlcRefundResponse = R"({"version":2,"network":23,"typeGroup":1,"type":10,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"0","asset":{"refund":{"lockTransactionId":"50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e"}},"id":"ff58b3f891b8395b407d21ee7c2c10aa3fe1303e815677f1fa88b399ff185ff2","signature":"30450221009b2b4d9070095d566b2570ba723c5c1935877e669eaf88f188f0fcac51fa5a4a022022e46ad51939c78b719b6ab37536d5331ea3e414a25d24776b056346fe6feb3c"})"; + ASSERT_STREQ(htlcRefundResponse.c_str(), htlcRefundJson.c_str()); } diff --git a/test/transactions/transaction_to_array_type_0.cpp b/test/transactions/transaction_to_array_type_0.cpp deleted file mode 100644 index ac5c700d..00000000 --- a/test/transactions/transaction_to_array_type_0.cpp +++ /dev/null @@ -1,54 +0,0 @@ - -#include "gtest/gtest.h" - -#include -#include - - -#include "transactions/builder.h" -#include "transactions/transaction.h" - -using namespace Ark::Crypto::Transactions; - -TEST(transactions, transaction_to_array_type_0) { // NOLINT - std::map tArray; - // Type 0 // - - { // force dtor on transfer to clean up stack space - auto transfer = Builder::buildTransfer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 1, - "", - "Secret passphrase"); - - tArray = transfer.toArray(); - } - - // Amount - ASSERT_STREQ("1", tArray["amount"].c_str()); - - // Fee - ASSERT_STREQ("10000000", tArray["fee"].c_str()); - - // Id - ASSERT_FALSE(tArray["id"].empty()); - - // Recipient - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", tArray["recipient"].c_str()); - - // SenderPublicKey - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - tArray["senderPublicKey"].c_str()); - - // Signature - ASSERT_FALSE(tArray["signature"].empty()); - - // Timestamp - ASSERT_FALSE(tArray["timestamp"].empty()); - - // Type - ASSERT_STREQ("0", tArray["type"].c_str()); - - // Version - ASSERT_STREQ("1", tArray["version"].c_str()); - -} diff --git a/test/transactions/transaction_to_array_type_1.cpp b/test/transactions/transaction_to_array_type_1.cpp deleted file mode 100644 index cf1218cc..00000000 --- a/test/transactions/transaction_to_array_type_1.cpp +++ /dev/null @@ -1,59 +0,0 @@ - -#include "gtest/gtest.h" - -#include -#include - - -#include "transactions/builder.h" -#include "transactions/transaction.h" - -using namespace Ark::Crypto::Transactions; - -TEST(transactions, transaction_to_array_type_1) { // NOLINT - std::map ssArray; - - { // force dtor on transfer to clean up stack space - // Type 1 // - auto secondSignatureRegistration = Builder::buildSecondSignatureRegistration( - "Secret passphrase", - "Second Secret passphrase"); - - ssArray = secondSignatureRegistration.toArray(); - } - - // Amount - ASSERT_STREQ("0", ssArray["amount"].c_str()); - - // Asset - ASSERT_STREQ("02e1684d8990c0a5625aec85977fcf22204884bc08d45dbc71b2859e5fa4f45104", - ssArray["publicKey"].c_str()); - - // Fee - ASSERT_STREQ("500000000", ssArray["fee"].c_str()); - - // Id - ASSERT_FALSE(ssArray["id"].empty()); - - // Recipient - ASSERT_TRUE(ssArray["recipient"].empty()); - - // SecondSignature - ASSERT_FALSE(ssArray["secondSignature"].empty()); - - // SenderPublicKey - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - ssArray["senderPublicKey"].c_str()); - - // Signature - ASSERT_FALSE(ssArray["signature"].empty()); - - // Timestamp - ASSERT_FALSE(ssArray["timestamp"].empty()); - - // Type - ASSERT_STREQ("1", ssArray["type"].c_str()); - - // Version - ASSERT_STREQ("1", ssArray["version"].c_str()); -} diff --git a/test/transactions/transaction_to_array_type_2.cpp b/test/transactions/transaction_to_array_type_2.cpp deleted file mode 100644 index 18e8e2af..00000000 --- a/test/transactions/transaction_to_array_type_2.cpp +++ /dev/null @@ -1,57 +0,0 @@ - -#include "gtest/gtest.h" - -#include -#include - - -#include "transactions/builder.h" -#include "transactions/transaction.h" - -using namespace Ark::Crypto::Transactions; - -TEST(transactions, transaction_to_array_type_2) { // NOLINT - std::map dArray; - - { // force dtor on transfer to clean up stack space - // Type 2 // - auto delegateRegistration = Builder::buildDelegateRegistration( - "testName", - "Secret passphrase"); - - dArray = delegateRegistration.toArray(); - } - // Amount - ASSERT_STREQ("0", dArray["amount"].c_str()); - - // Asset - ASSERT_STREQ("testName", dArray["username"].c_str()); - - // Fee - ASSERT_STREQ("2500000000", dArray["fee"].c_str()); - - // Id - ASSERT_FALSE(dArray["id"].empty()); - - // Recipient - ASSERT_TRUE(dArray["recipient"].empty()); - - // SecondSignature - ASSERT_TRUE(dArray["secondSignature"].empty()); - - // SenderPublicKey - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - dArray["senderPublicKey"].c_str()); - - // Signature - ASSERT_FALSE(dArray["signature"].empty()); - - // ASSERT_FALSE - ASSERT_FALSE(dArray["timestamp"].empty()); - - // Type - ASSERT_STREQ("2", dArray["type"].c_str()); - - // Version - ASSERT_STREQ("1", dArray["version"].c_str()); -} diff --git a/test/transactions/transaction_to_array_type_3.cpp b/test/transactions/transaction_to_array_type_3.cpp deleted file mode 100644 index 42ece945..00000000 --- a/test/transactions/transaction_to_array_type_3.cpp +++ /dev/null @@ -1,64 +0,0 @@ - -#include "gtest/gtest.h" - -#include -#include - - -#include "transactions/builder.h" -#include "transactions/transaction.h" - -using namespace Ark::Crypto::Transactions; - -TEST(transactions, transaction_to_array_type_3) { // NOLINT - std::map vArray; - - { // force dtor on transfer to clean up stack space - // Type 3 // - std::vector votes = { - "-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", - "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6" - }; - - auto vote = Builder::buildVote(votes, "Secret passphrase"); - - vArray = vote.toArray(); - } - - // Amount - ASSERT_STREQ("0", vArray["amount"].c_str()); - - // Asset - ASSERT_STREQ("-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6," - "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", - vArray["votes"].c_str()); - - // Fee - ASSERT_STREQ("100000000", vArray["fee"].c_str()); - - // Id - ASSERT_FALSE(vArray["id"].empty()); - - // Recipient - ASSERT_STREQ("DPgZq5MK6rm5yVks9b7TrA22F8FwRvkCtF", - vArray["recipient"].c_str()); - - // SecondSignature - ASSERT_TRUE(vArray["secondSignature"].empty()); - - // SenderPublicKey - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - vArray["senderPublicKey"].c_str()); - - // Signature - ASSERT_FALSE(vArray["signature"].empty()); - - // Timestamp - ASSERT_FALSE(vArray["timestamp"].empty()); - - // Type - ASSERT_STREQ("3", vArray["type"].c_str()); - - // Version - ASSERT_STREQ("1", vArray["version"].c_str()); -} diff --git a/test/transactions/transaction_to_json_type_0.cpp b/test/transactions/transaction_to_json_type_0.cpp deleted file mode 100644 index 378ba7d8..00000000 --- a/test/transactions/transaction_to_json_type_0.cpp +++ /dev/null @@ -1,52 +0,0 @@ - -#include "gtest/gtest.h" - -#include -#include - -#include "transactions/builder.h" -#include "utils/json.h" -using namespace Ark::Crypto; -using namespace Ark::Crypto::Transactions; - -TEST(transactions, transaction_to_json_type_0) { // NOLINT - std::string tJson; - - { // force dtor on transfer to clean up stack space - auto transfer = Builder::buildTransfer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - 1, - "", - "Secret passphrase"); - - tJson = transfer.toJson(); - } - - const size_t tCapacity = JSON_OBJECT_SIZE(8) + 450; - DynamicJsonDocument tDoc(tCapacity); - - DeserializationError tError = deserializeJson(tDoc, tJson); - ASSERT_FALSE(tError); - - ASSERT_STREQ("1", tDoc["amount"].as()); - - ASSERT_STREQ("10000000", tDoc["fee"].as()); - - ASSERT_STRNE("", tDoc["id"].as()); - - ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", - tDoc["recipient"].as()); - - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - tDoc["senderPublicKey"].as()); - - ASSERT_STRNE("", tDoc["signature"].as()); - - ASSERT_GT(tDoc["timestamp"].as(), 50000000UL); - - ASSERT_LT(tDoc["timestamp"].as(), 1000000000UL); - - ASSERT_EQ(tDoc["type"].as(), 0); - - ASSERT_EQ(1, tDoc["version"].as()); - -} diff --git a/test/transactions/transaction_to_json_type_1.cpp b/test/transactions/transaction_to_json_type_1.cpp deleted file mode 100644 index 4b58cf3c..00000000 --- a/test/transactions/transaction_to_json_type_1.cpp +++ /dev/null @@ -1,54 +0,0 @@ - -#include "gtest/gtest.h" - -#include -#include - -#include "transactions/builder.h" -#include "utils/json.h" -using namespace Ark::Crypto; -using namespace Ark::Crypto::Transactions; - -TEST(transactions, transaction_to_json_type_1) { // NOLINT - std::string ssJson; - - { // force dtor on transfer to clean up stack space - auto secondSignatureRegistration = Builder::buildSecondSignatureRegistration( - "Secret passphrase", - "Second Secret passphrase"); - - ssJson = secondSignatureRegistration.toJson(); - } - - const size_t ssCapacity = 2 * JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(10) + 690; - DynamicJsonDocument ssDoc(ssCapacity); - - DeserializationError ssError = deserializeJson(ssDoc, ssJson); - ASSERT_FALSE(ssError); - - ASSERT_STREQ("0", ssDoc["amount"].as()); - - ASSERT_STREQ("02e1684d8990c0a5625aec85977fcf22204884bc08d45dbc71b2859e5fa4f45104", - ssDoc["asset"]["signature"]["publicKey"].as()); - - ASSERT_STREQ("500000000", ssDoc["fee"].as()); - - ASSERT_STRNE("", ssDoc["id"].as()); - - ASSERT_STREQ("", ssDoc["recipient"].as()); - - ASSERT_STRNE("", ssDoc["secondSignature"].as()); - - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - ssDoc["senderPublicKey"].as()); - - ASSERT_STRNE("", ssDoc["signature"].as()); - - ASSERT_GT(ssDoc["timestamp"].as(), 50000000UL); - ASSERT_LT(ssDoc["timestamp"].as(), 1000000000UL); - - ASSERT_EQ(ssDoc["type"].as(), 1); - - ASSERT_EQ(1, ssDoc["version"].as()); - -} diff --git a/test/transactions/transaction_to_json_type_2.cpp b/test/transactions/transaction_to_json_type_2.cpp deleted file mode 100644 index 5bacf291..00000000 --- a/test/transactions/transaction_to_json_type_2.cpp +++ /dev/null @@ -1,52 +0,0 @@ - -#include "gtest/gtest.h" - -#include -#include - -#include "transactions/builder.h" -#include "utils/json.h" -using namespace Ark::Crypto; -using namespace Ark::Crypto::Transactions; - -TEST(transactions, transaction_to_json_type_2) { // NOLINT - std::string dJson; - - { // force dtor on transfer to clean up stack space - auto delegateRegistration = Builder::buildDelegateRegistration( - "testName", - "Secret passphrase"); - - dJson = delegateRegistration.toJson(); - } - - const size_t dCapacity = 2*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(9) + 450; - DynamicJsonDocument dDoc(dCapacity); - - DeserializationError dError = deserializeJson(dDoc, dJson); - ASSERT_FALSE(dError); - - ASSERT_STREQ("0", dDoc["amount"].as()); - - ASSERT_STREQ("testName", - dDoc["asset"]["delegate"]["username"].as()); - - ASSERT_STREQ("2500000000", dDoc["fee"].as()); - - ASSERT_STRNE("", dDoc["id"].as()); - - ASSERT_STREQ("", dDoc["recipient"].as()); - - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - dDoc["senderPublicKey"].as()); - - ASSERT_STRNE("", dDoc["signature"].as()); - - ASSERT_GT(dDoc["timestamp"].as(), 50000000UL); - ASSERT_LT(dDoc["timestamp"].as(), 1000000000UL); - - ASSERT_EQ(dDoc["type"].as(), 2); - - ASSERT_EQ(1, dDoc["version"].as()); - -} diff --git a/test/transactions/transaction_to_json_type_3.cpp b/test/transactions/transaction_to_json_type_3.cpp deleted file mode 100644 index ce0a51dc..00000000 --- a/test/transactions/transaction_to_json_type_3.cpp +++ /dev/null @@ -1,58 +0,0 @@ - -#include "gtest/gtest.h" - -#include -#include - -#include "transactions/builder.h" -#include "utils/json.h" -using namespace Ark::Crypto; -using namespace Ark::Crypto::Transactions; - -TEST(transactions, transaction_to_json_type_3) { // NOLINT - std::string vJson; - { // force dtor on transfer to clean up stack space - std::vector votes = { - "-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6," - "+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6" - }; - auto vote = Builder::buildVote(votes, "Secret passphrase"); - vJson = vote.toJson(); - } - - const size_t vCapacity = JSON_ARRAY_SIZE(1) - + JSON_OBJECT_SIZE(1) - + JSON_OBJECT_SIZE(9) - + 700; - DynamicJsonDocument vDoc(vCapacity); - - DeserializationError vError = deserializeJson(vDoc, vJson); - ASSERT_FALSE(vError); - - ASSERT_STREQ("0", vDoc["amount"].as()); - - ASSERT_STREQ("-0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", - vDoc["asset"]["votes"][0].as()); - - ASSERT_STREQ("+0250b742256f9321bd7d46f3ed9769b215a7c2fb02be951acf43bc51eb57ceadf6", - vDoc["asset"]["votes"][1].as()); - - ASSERT_STREQ("100000000", vDoc["fee"].as()); - - ASSERT_STRNE("", vDoc["id"].as()); - - ASSERT_STREQ("DPgZq5MK6rm5yVks9b7TrA22F8FwRvkCtF", - vDoc["recipient"].as()); - - ASSERT_STREQ("02f21aca9b6d224ea86a1689f57910534af21c3cc9f80602fed252c13e275f0699", - vDoc["senderPublicKey"].as()); - - ASSERT_STRNE("", vDoc["signature"].as()); - - ASSERT_GT(vDoc["timestamp"].as(), 50000000UL); - ASSERT_LT(vDoc["timestamp"].as(), 1000000000UL); - - ASSERT_EQ(vDoc["type"].as(), 3); - - ASSERT_EQ(1, vDoc["version"].as()); -} diff --git a/test/transactions/transaction_v1.cpp b/test/transactions/transaction_v1.cpp new file mode 100644 index 00000000..782ea54d --- /dev/null +++ b/test/transactions/transaction_v1.cpp @@ -0,0 +1,29 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "types/fixtures/transfer_v1.hpp" + +#include "transactions/transaction.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transaction, v1_vendorField) { // NOLINT + + Transaction transaction; + transaction.deserialize(v1::TYPE_0_BYTES_VF); + + ASSERT_STREQ(R"({"version":1,"network":23,"type":0,"timestamp":"83506245","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"10000000","vendorField":"Hello World","amount":"1","expiration":0,"recipientId":"AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff","id":"b410f29636882f747608f784be2adf181f30db08a93341df57e9415cfd5b023a","signature":"304402204209261f274cb4d71b0504a3827d9670ac8cca1cb4aab6b3fc55b0b63a9f03e502205cbb25435d7279e536958cf8fa588c86ebe2a00571073a96a79084906ee0d509"})", + transaction.toJson().c_str()); +} diff --git a/test/transactions/types/delegate_registration.cpp b/test/transactions/types/delegate_registration.cpp new file mode 100644 index 00000000..94e5c803 --- /dev/null +++ b/test/transactions/types/delegate_registration.cpp @@ -0,0 +1,123 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include +#include +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/delegate_registration.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_delegate_registration, deserialize_ecdsa) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, TYPE_2_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V2, data.version); + ASSERT_EQ(COMMON_MAINNET, data.network); + ASSERT_EQ(COMMON_TYPEGROUP, data.typeGroup); + ASSERT_EQ(TYPE_2_TYPE, data.type); + ASSERT_EQ(COMMON_NONCE, data.nonce); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(TYPE_2_FEE, data.fee); + + ASSERT_EQ(TYPE_2_USERNAME_LENGTH, data.asset.delegateRegistration.length); + + ASSERT_TRUE(array_cmp(TYPE_2_USERNAME, + data.asset.delegateRegistration.username.data(), + data.asset.delegateRegistration.length)); + + ASSERT_TRUE(array_cmp(TYPE_2_SIGNATURE, + data.signature.data(), + sizeof(TYPE_2_SIGNATURE))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_delegate_registration, serialize_ecdsa) { + TransactionData data; + + data.network = COMMON_MAINNET; + data.typeGroup = COMMON_TYPEGROUP; + data.type = TYPE_2_TYPE; + data.nonce = COMMON_NONCE; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.fee = TYPE_2_FEE; + + data.asset.delegateRegistration.length = TYPE_2_USERNAME_LENGTH; + + std::copy_n(TYPE_2_USERNAME, + TYPE_2_USERNAME_LENGTH, + data.asset.delegateRegistration.username.begin()); + + data.signature.resize(sizeof(TYPE_2_SIGNATURE)); + std::copy_n(TYPE_2_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(TYPE_2_BYTES.data(), + Serializer::serialize(data).data(), + TYPE_2_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_delegate_registration, invalid_len) { + DelegateRegistration registration; + + // Invalid Username Length on Deserialization. + DelegateRegistration::Deserialize( + ®istration, + reinterpret_cast(TYPE_2_INVALID_USERNAME_STRING)); + + ASSERT_EQ(0U, registration.length); + + // Invalid Username Length on Serialization. + std::array buffer {}; + DelegateRegistration::Serialize(registration, buffer.data()); + + ASSERT_EQ(0U, buffer.at(0)); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_delegate_registration, add_to_map) { + DelegateRegistration delegateRegistration; + + delegateRegistration.length = TYPE_2_USERNAME_LENGTH; + + std::copy_n(TYPE_2_USERNAME, + TYPE_2_USERNAME_LENGTH, + delegateRegistration.username.begin()); + + std::map delegateRegistrationMap; + + DelegateRegistration::addToMap(delegateRegistration, delegateRegistrationMap); + + ASSERT_STREQ(TYPE_2_USERNAME_STRING, + delegateRegistrationMap.find("username")->second.c_str()); +} diff --git a/test/transactions/types/delegate_registration_v1.cpp b/test/transactions/types/delegate_registration_v1.cpp new file mode 100644 index 00000000..2def505d --- /dev/null +++ b/test/transactions/types/delegate_registration_v1.cpp @@ -0,0 +1,95 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/delegate_registration_v1.hpp" + +#include "test_helpers.h" + +#include "utils/hex.hpp" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_delegate_registration_v1, deserialize) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, v1::TYPE_2_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V1, data.version); + ASSERT_EQ(COMMON_DEVNET, data.network); + + ASSERT_EQ(v1::TYPE_2_TYPE, data.type); + ASSERT_EQ(v1::TYPE_2_TIMESTAMP, data.timestamp); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(v1::TYPE_2_FEE, data.fee); + + ASSERT_TRUE(array_cmp(v1::TYPE_2_USERNAME, + data.asset.delegateRegistration.username.data(), + data.asset.delegateRegistration.length)); + + ASSERT_TRUE(array_cmp(v1::TYPE_2_SIGNATURE, + data.signature.data(), + data.signature.size())); + + ASSERT_TRUE(array_cmp(v1::TYPE_2_SECOND_SIG, + data.secondSignature.data(), + data.secondSignature.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_delegate_registration_v1, serialize) { + TransactionData data; + + data.version = COMMON_VERSION_V1; + data.type = v1::TYPE_2_TYPE; + data.fee = v1::TYPE_2_FEE; + data.timestamp = v1::TYPE_2_TIMESTAMP; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.asset.delegateRegistration.length = v1::TYPE_2_USERNAME_LENGTH; + + std::copy_n(v1::TYPE_2_USERNAME, + v1::TYPE_2_USERNAME_LENGTH, + data.asset.delegateRegistration.username.begin()); + + data.signature.resize(sizeof(v1::TYPE_2_SIGNATURE)); + std::copy_n(v1::TYPE_2_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + data.secondSignature.resize(sizeof(v1::TYPE_2_SECOND_SIG)); + std::copy_n(v1::TYPE_2_SECOND_SIG, + data.secondSignature.size(), + data.secondSignature.begin()); + + const auto serialized = Serializer::serialize(data); + + ASSERT_TRUE(array_cmp(v1::TYPE_2_BYTES.data(), + serialized.data(), + serialized.size())); +} diff --git a/test/transactions/types/delegate_resignation.cpp b/test/transactions/types/delegate_resignation.cpp new file mode 100644 index 00000000..90804105 --- /dev/null +++ b/test/transactions/types/delegate_resignation.cpp @@ -0,0 +1,75 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include +#include +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/delegate_resignation.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_delegate_resignation, deserialize_ecdsa) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, TYPE_7_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V2, data.version); + ASSERT_EQ(COMMON_MAINNET, data.network); + ASSERT_EQ(COMMON_TYPEGROUP, data.typeGroup); + ASSERT_EQ(TYPE_7_TYPE, data.type); + ASSERT_EQ(COMMON_NONCE, data.nonce); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(TYPE_7_FEE, data.fee); + + ASSERT_TRUE(array_cmp(TYPE_7_SIGNATURE, + data.signature.data(), + sizeof(TYPE_7_SIGNATURE))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_delegate_resignation, serialize_ecdsa) { + TransactionData data; + + data.network = COMMON_MAINNET; + data.typeGroup = COMMON_TYPEGROUP; + data.type = TYPE_7_TYPE; + data.nonce = COMMON_NONCE; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.fee = TYPE_7_FEE; + + data.signature.resize(sizeof(TYPE_7_SIGNATURE)); + std::copy_n(TYPE_7_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(TYPE_7_BYTES.data(), + Serializer::serialize(data).data(), + TYPE_7_BYTES.size())); +} diff --git a/test/transactions/types/fixtures/common.hpp b/test/transactions/types/fixtures/common.hpp new file mode 100644 index 00000000..e6de908f --- /dev/null +++ b/test/transactions/types/fixtures/common.hpp @@ -0,0 +1,32 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_COMMON_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_COMMON_HPP + +#include + +//////////////////////////////////////////////////////////////////////////////// +// Common Test Numbers +constexpr uint8_t COMMON_HEADER = 0xFF; +constexpr uint8_t COMMON_VERSION_V1 = 1U; +constexpr uint8_t COMMON_VERSION_V2 = 2U; +constexpr uint8_t COMMON_MAINNET = 23U; +constexpr uint8_t COMMON_DEVNET = 30U; +constexpr uint16_t COMMON_TYPEGROUP = 1U; +constexpr uint64_t COMMON_NONCE = 1ULL; + +//////////////////////////////////////////////////////////////////////////////// +// Common Test Strings +constexpr auto COMMON_VERSION_V2_STRING = "2"; +constexpr auto COMMON_MAINNET_STRING = "23"; +constexpr auto COMMON_TYPEGROUP_STRING = "1"; +constexpr auto COMMON_NONCE_STRING = "1"; + +#endif diff --git a/test/transactions/types/fixtures/delegate_registration.hpp b/test/transactions/types/fixtures/delegate_registration.hpp new file mode 100644 index 00000000..e432ea36 --- /dev/null +++ b/test/transactions/types/fixtures/delegate_registration.hpp @@ -0,0 +1,78 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_DELEGATE_REGISTRATION_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_DELEGATE_REGISTRATION_HPP + +#include +#include + +#include "common.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 2, +// "network": 23, +// "typeGroup": 1, +// "type": 2, +// "nonce": "1", +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "2500000000", +// "asset": { +// "delegate": { +// "username": "arkdelegate" +// } +// }, +// "signature": "30440220281812ca558b3b032aaf724aeaba5e20733e87f20fefe1c78536cc523de7e475022038a2454ca2665f6c3d910b04ecdd5995a7d82904f9f4bf878402a31262d2db61", +// "id": "668f5db0d99189ee7dd85c991360ca19372302a13e3046511a60d115dbf1864e" +// }, +// 'serialized': "ff02170100000002000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200f9029500000000000b61726b64656c656761746530440220281812ca558b3b032aaf724aeaba5e20733e87f20fefe1c78536cc523de7e475022038a2454ca2665f6c3d910b04ecdd5995a7d82904f9f4bf878402a31262d2db61" +// } + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_2_TYPE = 2U; +constexpr uint64_t TYPE_2_FEE = 2500000000ULL; +constexpr uint64_t TYPE_2_VF_LENGTH = 0U; +constexpr uint64_t TYPE_2_USERNAME_LENGTH = 11U; + +//////////////////////////////////////////////////////////////////////////////// +constexpr auto TYPE_2_USERNAME_LEN_STRING = "11"; +constexpr auto TYPE_2_USERNAME_STRING = "arkdelegate"; +constexpr auto TYPE_2_INVALID_USERNAME_STRING = "arkdelegatearkdelegate"; + +//////////////////////////////////////////////////////////////////////////////// +// ff02170100000002000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200f9029500000000000b61726b64656c656761746530440220281812ca558b3b032aaf724aeaba5e20733e87f20fefe1c78536cc523de7e475022038a2454ca2665f6c3d910b04ecdd5995a7d82904f9f4bf878402a31262d2db61 +const std::vector TYPE_2_BYTES = { + 255, 2, 23, 1, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 0, 249, 2, 149, 0, 0, 0, 0, 0, 11, + 97, 114, 107, 100, 101, 108, 101, 103, 97, 116, 101, 48, 68, 2, 32, + 40, 24, 18, 202, 85, 139, 59, 3, 42, 175, 114, 74, 234, 186, 94, + 32, 115, 62, 135, 242, 15, 239, 225, 199, 133, 54, 204, 82, 61, 231, + 228, 117, 2, 32, 56, 162, 69, 76, 162, 102, 95, 108, 61, 145, 11, + 4, 236, 221, 89, 149, 167, 216, 41, 4, 249, 244, 191, 135, 132, 2, + 163, 18, 98, 210, 219, 97 }; + +//////////////////////////////////////////////////////////////////////////////// +// "arkdelegate" +constexpr uint8_t TYPE_2_USERNAME[] = { + 97, 114, 107, 100, 101, 108, 101, 103, 97, 116, 101 }; + +//////////////////////////////////////////////////////////////////////////////// +// 30440220281812ca558b3b032aaf724aeaba5e20733e87f20fefe1c78536cc523de7e475022038a2454ca2665f6c3d910b04ecdd5995a7d82904f9f4bf878402a31262d2db61 +constexpr uint8_t TYPE_2_SIGNATURE[] = { + 48, 68, 2, 32, 40, 24, 18, 202, 85, 139, 59, 3, 42, 175, + 114, 74, 234, 186, 94, 32, 115, 62, 135, 242, 15, 239, 225, 199, + 133, 54, 204, 82, 61, 231, 228, 117, 2, 32, 56, 162, 69, 76, + 162, 102, 95, 108, 61, 145, 11, 4, 236, 221, 89, 149, 167, 216, + 41, 4, 249, 244, 191, 135, 132, 2, 163, 18, 98, 210, 219, 97 }; + +#endif diff --git a/test/transactions/types/fixtures/delegate_registration_v1.hpp b/test/transactions/types/fixtures/delegate_registration_v1.hpp new file mode 100644 index 00000000..8e8484e9 --- /dev/null +++ b/test/transactions/types/fixtures/delegate_registration_v1.hpp @@ -0,0 +1,69 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_DELEGATE_REGISTRATION_V1_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_DELEGATE_REGISTRATION_V1_HPP + +#include +#include + +#include "common.hpp" + +namespace v1 { + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_2_TYPE = 2U; +constexpr uint32_t TYPE_2_TIMESTAMP = 41269424UL; +constexpr uint64_t TYPE_2_FEE = 2500000000ULL; +constexpr uint64_t TYPE_2_USERNAME_LENGTH = 9U; + +//////////////////////////////////////////////////////////////////////////////// +// ff011e02b0b87502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200f90295000000000009626f6c646e696e6a613045022100f21b742fa052cd18de43328e1d068539ba7cbe9d33a9dcbd862a82871383955d0220053b06d22ed3e3ad6168c6b27aa0ec68e7e40958c7709aec0e1555087ea9ad94304402207da580da4feec955edcb8e8eb36947867b439de3d28d38e58c844fd8c45b564302200e6741b6ad11c2588a57b3afd180df1e9b345d48a9c2ae98be57dced869cf38c +const std::vector TYPE_2_BYTES = { + 255, 1, 30, 2, 176, 184, 117, 2, 3, 65, 81, 163, 236, 70, 181, + 103, 10, 104, 43, 10, 99, 57, 79, 134, 53, 135, 209, 188, 151, 72, + 59, 27, 108, 112, 235, 88, 231, 240, 174, 209, 146, 0, 249, 2, 149, + 0, 0, 0, 0, 0, 9, 98, 111, 108, 100, 110, 105, 110, 106, 97, + 48, 69, 2, 33, 0, 242, 27, 116, 47, 160, 82, 205, 24, 222, 67, + 50, 142, 29, 6, 133, 57, 186, 124, 190, 157, 51, 169, 220, 189, 134, + 42, 130, 135, 19, 131, 149, 93, 2, 32, 5, 59, 6, 210, 46, 211, + 227, 173, 97, 104, 198, 178, 122, 160, 236, 104, 231, 228, 9, 88, 199, + 112, 154, 236, 14, 21, 85, 8, 126, 169, 173, 148, 48, 68, 2, 32, + 125, 165, 128, 218, 79, 238, 201, 85, 237, 203, 142, 142, 179, 105, 71, + 134, 123, 67, 157, 227, 210, 141, 56, 229, 140, 132, 79, 216, 196, 91, + 86, 67, 2, 32, 14, 103, 65, 182, 173, 17, 194, 88, 138, 87, 179, + 175, 209, 128, 223, 30, 155, 52, 93, 72, 169, 194, 174, 152, 190, 87, + 220, 237, 134, 156, 243, 140 }; + +//////////////////////////////////////////////////////////////////////////////// +// boldninja +constexpr uint8_t TYPE_2_USERNAME[] = { + 98, 111, 108, 100, 110, 105, 110, 106, 97 }; + +//////////////////////////////////////////////////////////////////////////////// +// 3045022100f21b742fa052cd18de43328e1d068539ba7cbe9d33a9dcbd862a82871383955d0220053b06d22ed3e3ad6168c6b27aa0ec68e7e40958c7709aec0e1555087ea9ad94 +constexpr uint8_t TYPE_2_SIGNATURE[] = { + 48, 69, 2, 33, 0, 242, 27, 116, 47, 160, 82, 205, 24, 222, 67, + 50, 142, 29, 6, 133, 57, 186, 124, 190, 157, 51, 169, 220, 189, 134, + 42, 130, 135, 19, 131, 149, 93, 2, 32, 5, 59, 6, 210, 46, 211, + 227, 173, 97, 104, 198, 178, 122, 160, 236, 104, 231, 228, 9, 88, 199, + 112, 154, 236, 14, 21, 85, 8, 126, 169, 173, 148 }; + +//////////////////////////////////////////////////////////////////////////////// +// 304402207da580da4feec955edcb8e8eb36947867b439de3d28d38e58c844fd8c45b564302200e6741b6ad11c2588a57b3afd180df1e9b345d48a9c2ae98be57dced869cf38c +constexpr uint8_t TYPE_2_SECOND_SIG[] = { + 48, 68, 2, 32, 125, 165, 128, 218, 79, 238, 201, 85, 237, 203, + 142, 142, 179, 105, 71, 134, 123, 67, 157, 227, 210, 141, 56, 229, + 140, 132, 79, 216, 196, 91, 86, 67, 2, 32, 14, 103, 65, 182, + 173, 17, 194, 88, 138, 87, 179, 175, 209, 128, 223, 30, 155, 52, + 93, 72, 169, 194, 174, 152, 190, 87, 220, 237, 134, 156, 243, 140 }; + +} // namespace v1 + +#endif diff --git a/test/transactions/types/fixtures/delegate_resignation.hpp b/test/transactions/types/fixtures/delegate_resignation.hpp new file mode 100644 index 00000000..aab698f5 --- /dev/null +++ b/test/transactions/types/fixtures/delegate_resignation.hpp @@ -0,0 +1,62 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_DELEGATE_RESIGNATION_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_DELEGATE_RESIGNATION_HPP + +#include +#include + +#include "common.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 2, +// "network": 23, +// "typeGroup": 1, +// "type": 7, +// "nonce": "1", +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "2500000000", +// "amount": "0", +// "signature": "3045022100aa113ddefb502d868219339d16b8d448c8e69c8e320664e12903cc84420c159d02201b6206c7cb9442f6ef07dd0824aeb0cc393d8f9f9909988da6687c44ef897070", +// "id": "a8cf10c6a7d2e5c82bff0860f83f917f426bdd1ee25bd8d5dad2358973ce4ecb" +// }, +// "serialized": "ff02170100000007000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200f9029500000000003045022100aa113ddefb502d868219339d16b8d448c8e69c8e320664e12903cc84420c159d02201b6206c7cb9442f6ef07dd0824aeb0cc393d8f9f9909988da6687c44ef897070" +// } + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_7_TYPE = 7U; +constexpr uint64_t TYPE_7_FEE = 2500000000ULL; +constexpr uint64_t TYPE_7_VF_LENGTH = 0U; + +//////////////////////////////////////////////////////////////////////////////// +// ff02170100000007000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200f9029500000000003045022100aa113ddefb502d868219339d16b8d448c8e69c8e320664e12903cc84420c159d02201b6206c7cb9442f6ef07dd0824aeb0cc393d8f9f9909988da6687c44ef897070 +const std::vector TYPE_7_BYTES = { + 255, 2, 23, 1, 0, 0, 0, 7, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 0, 249, 2, 149, 0, 0, 0, 0, 0, 48, + 69, 2, 33, 0, 170, 17, 61, 222, 251, 80, 45, 134, 130, 25, 51, + 157, 22, 184, 212, 72, 200, 230, 156, 142, 50, 6, 100, 225, 41, 3, + 204, 132, 66, 12, 21, 157, 2, 32, 27, 98, 6, 199, 203, 148, 66, + 246, 239, 7, 221, 8, 36, 174, 176, 204, 57, 61, 143, 159, 153, 9, + 152, 141, 166, 104, 124, 68, 239, 137, 112, 112 }; + +//////////////////////////////////////////////////////////////////////////////// +// 3045022100aa113ddefb502d868219339d16b8d448c8e69c8e320664e12903cc84420c159d02201b6206c7cb9442f6ef07dd0824aeb0cc393d8f9f9909988da6687c44ef897070 +constexpr uint8_t TYPE_7_SIGNATURE[] = { + 48, 69, 2, 33, 0, 170, 17, 61, 222, 251, 80, 45, 134, 130, 25, + 51, 157, 22, 184, 212, 72, 200, 230, 156, 142, 50, 6, 100, 225, 41, + 3, 204, 132, 66, 12, 21, 157, 2, 32, 27, 98, 6, 199, 203, 148, + 66, 246, 239, 7, 221, 8, 36, 174, 176, 204, 57, 61, 143, 159, 153, + 9, 152, 141, 166, 104, 124, 68, 239, 137, 112, 112 }; + +#endif diff --git a/test/transactions/types/fixtures/htlc_claim.hpp b/test/transactions/types/fixtures/htlc_claim.hpp new file mode 100644 index 00000000..4f3c1b67 --- /dev/null +++ b/test/transactions/types/fixtures/htlc_claim.hpp @@ -0,0 +1,86 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_HTLC_CLAIM_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_HTLC_CLAIM_HPP + +#include +#include + +#include "common.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 2, +// "network": 23, +// "typeGroup": 1, +// "type": 9, +// "nonce": "1", +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "0", +// "asset": { +// "claim": { +// "lockTransactionId": "50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e", +// "unlockSecret": "f5ea877a311ced90cf4524cb489e972f" +// } +// }, +// "signature": "3045022100d47a071dadebfae4e9115f35ae29429d0ada7bcb1668dc9babde9613108ecc8302201d97dd366a8816f81581dc5be46f3a859b66e48532c80fb7fa19da63dfc3c01f", +// "id": "88b21734d289fa48801b2fed39e23b89f021df4670e8d43e703b776e5a5333a8" +// }, +// "serialized": "ff02170100000009000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200000000000000000050f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e66356561383737613331316365643930636634353234636234383965393732663045022100d47a071dadebfae4e9115f35ae29429d0ada7bcb1668dc9babde9613108ecc8302201d97dd366a8816f81581dc5be46f3a859b66e48532c80fb7fa19da63dfc3c01f" +// } + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_9_TYPE = 9U; +constexpr uint64_t TYPE_9_FEE = 0ULL; + +//////////////////////////////////////////////////////////////////////////////// +constexpr auto TYPE_9_LOCK_ID_STRING = "50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e"; +constexpr auto TYPE_9_SECRET_STRING = "f5ea877a311ced90cf4524cb489e972f"; + +//////////////////////////////////////////////////////////////////////////////// +// ff02170100000009000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200000000000000000050f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e66356561383737613331316365643930636634353234636234383965393732663045022100d47a071dadebfae4e9115f35ae29429d0ada7bcb1668dc9babde9613108ecc8302201d97dd366a8816f81581dc5be46f3a859b66e48532c80fb7fa19da63dfc3c01f +const std::vector TYPE_9_BYTES = { + 255, 2, 23, 1, 0, 0, 0, 9, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, + 243, 1, 48, 109, 141, 165, 141, 98, 51, 44, 227, 163, 97, 217, 253, + 74, 1, 176, 168, 156, 169, 20, 81, 123, 104, 94, 45, 55, 20, 226, + 78, 102, 53, 101, 97, 56, 55, 55, 97, 51, 49, 49, 99, 101, 100, + 57, 48, 99, 102, 52, 53, 50, 52, 99, 98, 52, 56, 57, 101, 57, + 55, 50, 102, 48, 69, 2, 33, 0, 212, 122, 7, 29, 173, 235, 250, + 228, 233, 17, 95, 53, 174, 41, 66, 157, 10, 218, 123, 203, 22, 104, + 220, 155, 171, 222, 150, 19, 16, 142, 204, 131, 2, 32, 29, 151, 221, + 54, 106, 136, 22, 248, 21, 129, 220, 91, 228, 111, 58, 133, 155, 102, + 228, 133, 50, 200, 15, 183, 250, 25, 218, 99, 223, 195, 192, 31 }; + +//////////////////////////////////////////////////////////////////////////////// +// 50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e +constexpr uint8_t TYPE_9_LOCK_TX_ID[] = { + 80, 243, 1, 48, 109, 141, 165, 141, 98, 51, 44, 227, 163, 97, 217, 253, + 74, 1, 176, 168, 156, 169, 20, 81, 123, 104, 94, 45, 55, 20, 226, 78 }; + +//////////////////////////////////////////////////////////////////////////////// +// f5ea877a311ced90cf4524cb489e972f | UTF-8 +constexpr uint8_t TYPE_9_UNLOCK_SECRET[] = { + 102, 53, 101, 97, 56, 55, 55, 97, 51, 49, 49, 99, 101, 100, 57, 48, + 99, 102, 52, 53, 50, 52, 99, 98, 52, 56, 57, 101, 57, 55, 50, 102 }; + +//////////////////////////////////////////////////////////////////////////////// +// 3045022100d47a071dadebfae4e9115f35ae29429d0ada7bcb1668dc9babde9613108ecc8302201d97dd366a8816f81581dc5be46f3a859b66e48532c80fb7fa19da63dfc3c01f +constexpr uint8_t TYPE_9_SIGNATURE[] = { + 48, 69, 2, 33, 0, 212, 122, 7, 29, 173, 235, 250, 228, 233, 17, + 95, 53, 174, 41, 66, 157, 10, 218, 123, 203, 22, 104, 220, 155, 171, + 222, 150, 19, 16, 142, 204, 131, 2, 32, 29, 151, 221, 54, 106, 136, + 22, 248, 21, 129, 220, 91, 228, 111, 58, 133, 155, 102, 228, 133, 50, + 200, 15, 183, 250, 25, 218, 99, 223, 195, 192, 31 }; + +#endif diff --git a/test/transactions/types/fixtures/htlc_lock.hpp b/test/transactions/types/fixtures/htlc_lock.hpp new file mode 100644 index 00000000..6b1f9401 --- /dev/null +++ b/test/transactions/types/fixtures/htlc_lock.hpp @@ -0,0 +1,99 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_HTLC_LOCK_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_HTLC_LOCK_HPP + +#include +#include + +#include "common.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 2, +// "network": 23, +// "typeGroup": 1, +// "type": 8, +// "nonce": "1", +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "10000000", +// "amount": "1", +// "recipientId": "AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff", +// "asset": { +// "lock": { +// "secretHash": "09b9a28393efd02fcd76a21b0f0f55ba2aad8f3640ff8cae86de033a9cfbd78c", +// "expiration": { +// "type": 1, +// "value": 81242895 +// } +// } +// }, +// "signature": "3044022034491f6ee88278248c3b0ddd62b7ee0cb68e0ff93a51008899240b32d111b90a022043b10dd604f2d6256c75e50b5423612824a971c1876528af983ebb8145403e86", +// "id": "50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e" +// }, +// "serialized": "ff02170100000008000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192809698000000000000010000000000000009b9a28393efd02fcd76a21b0f0f55ba2aad8f3640ff8cae86de033a9cfbd78c010fabd704171dfc69b54c7fe901e91d5a9ab78388645e2427ea3044022034491f6ee88278248c3b0ddd62b7ee0cb68e0ff93a51008899240b32d111b90a022043b10dd604f2d6256c75e50b5423612824a971c1876528af983ebb8145403e86" +// } + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_8_TYPE = 8U; +constexpr uint64_t TYPE_8_FEE = 10000000ULL; +constexpr uint64_t TYPE_8_AMOUNT = 1ULL; +constexpr uint8_t TYPE_8_EXPIRATION_TYPE = 1ULL; +constexpr uint32_t TYPE_8_EXPIRATION_VALUE = 81242895UL; +constexpr uint64_t TYPE_8_VF_LENGTH = 0U; + +//////////////////////////////////////////////////////////////////////////////// +constexpr auto TYPE_8_AMOUNT_STRING = "1"; +constexpr auto TYPE_8_SECRET_STRING = "09b9a28393efd02fcd76a21b0f0f55ba2aad8f3640ff8cae86de033a9cfbd78c"; +constexpr auto TYPE_8_EXPIRATION_TYPE_STRING = "1"; +constexpr auto TYPE_8_EXPIRATION_VALUE_STRING = "81242895"; +constexpr auto TYPE_8_RECIPIENT_STRING = "AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff"; + +//////////////////////////////////////////////////////////////////////////////// +// ff02170100000008000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192809698000000000000010000000000000009b9a28393efd02fcd76a21b0f0f55ba2aad8f3640ff8cae86de033a9cfbd78c010fabd704171dfc69b54c7fe901e91d5a9ab78388645e2427ea3044022034491f6ee88278248c3b0ddd62b7ee0cb68e0ff93a51008899240b32d111b90a022043b10dd604f2d6256c75e50b5423612824a971c1876528af983ebb8145403e86 +const std::vector TYPE_8_BYTES = { + 255, 2, 23, 1, 0, 0, 0, 8, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 128, 150, 152, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 9, 185, 162, 131, 147, 239, 208, 47, + 205, 118, 162, 27, 15, 15, 85, 186, 42, 173, 143, 54, 64, 255, 140, + 174, 134, 222, 3, 58, 156, 251, 215, 140, 1, 15, 171, 215, 4, 23, + 29, 252, 105, 181, 76, 127, 233, 1, 233, 29, 90, 154, 183, 131, 136, + 100, 94, 36, 39, 234, 48, 68, 2, 32, 52, 73, 31, 110, 232, 130, + 120, 36, 140, 59, 13, 221, 98, 183, 238, 12, 182, 142, 15, 249, 58, + 81, 0, 136, 153, 36, 11, 50, 209, 17, 185, 10, 2, 32, 67, 177, + 13, 214, 4, 242, 214, 37, 108, 117, 229, 11, 84, 35, 97, 40, 36, + 169, 113, 193, 135, 101, 40, 175, 152, 62, 187, 129, 69, 64, 62, 134 }; + +//////////////////////////////////////////////////////////////////////////////// +// 09b9a28393efd02fcd76a21b0f0f55ba2aad8f3640ff8cae86de033a9cfbd78c +constexpr uint8_t TYPE_8_SECRET_HASH[] = { + 9, 185, 162, 131, 147, 239, 208, 47, 205, 118, 162, + 27, 15, 15, 85, 186, 42, 173, 143, 54, 64, 255, + 140, 174, 134, 222, 3, 58, 156, 251, 215, 140 }; + +//////////////////////////////////////////////////////////////////////////////// +// base58Decode(AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff) +constexpr uint8_t TYPE_8_RECIPIENT[] = { + 23, 29, 252, 105, 181, 76, 127, 233, 1, 233, 29, + 90, 154, 183, 131, 136, 100, 94, 36, 39, 234 }; + +//////////////////////////////////////////////////////////////////////////////// +// 3044022034491f6ee88278248c3b0ddd62b7ee0cb68e0ff93a51008899240b32d111b90a022043b10dd604f2d6256c75e50b5423612824a971c1876528af983ebb8145403e86 +constexpr uint8_t TYPE_8_SIGNATURE[] = { + 48, 68, 2, 32, 52, 73, 31, 110, 232, 130, 120, 36, 140, 59, + 13, 221, 98, 183, 238, 12, 182, 142, 15, 249, 58, 81, 0, 136, + 153, 36, 11, 50, 209, 17, 185, 10, 2, 32, 67, 177, 13, 214, + 4, 242, 214, 37, 108, 117, 229, 11, 84, 35, 97, 40, 36, 169, + 113, 193, 135, 101, 40, 175, 152, 62, 187, 129, 69, 64, 62, 134 }; + +#endif diff --git a/test/transactions/types/fixtures/htlc_refund.hpp b/test/transactions/types/fixtures/htlc_refund.hpp new file mode 100644 index 00000000..1d396d61 --- /dev/null +++ b/test/transactions/types/fixtures/htlc_refund.hpp @@ -0,0 +1,76 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_HTLC_REFUND_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_HTLC_REFUND_HPP + +#include +#include + +#include "common.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 2, +// "network": 23, +// "typeGroup": 1, +// "type": 10, +// "nonce": "1", +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "0", +// "asset": { +// "refund": { +// "lockTransactionId": "50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e" +// } +// }, +// "signature": "30450221009b2b4d9070095d566b2570ba723c5c1935877e669eaf88f188f0fcac51fa5a4a022022e46ad51939c78b719b6ab37536d5331ea3e414a25d24776b056346fe6feb3c", +// "id": "ff58b3f891b8395b407d21ee7c2c10aa3fe1303e815677f1fa88b399ff185ff2" +// }, +// "serialized": "ff0217010000000a000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200000000000000000050f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e30450221009b2b4d9070095d566b2570ba723c5c1935877e669eaf88f188f0fcac51fa5a4a022022e46ad51939c78b719b6ab37536d5331ea3e414a25d24776b056346fe6feb3c" +// } + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_10_TYPE = 10U; +constexpr uint64_t TYPE_10_FEE = 0ULL; + +//////////////////////////////////////////////////////////////////////////////// +constexpr auto TYPE_10_LOCK_ID_STRING = "50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e"; + +//////////////////////////////////////////////////////////////////////////////// +// ff0217010000000a000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200000000000000000050f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e30450221009b2b4d9070095d566b2570ba723c5c1935877e669eaf88f188f0fcac51fa5a4a022022e46ad51939c78b719b6ab37536d5331ea3e414a25d24776b056346fe6feb3c +const std::vector TYPE_10_BYTES = { + 255, 2, 23, 1, 0, 0, 0, 10, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, + 243, 1, 48, 109, 141, 165, 141, 98, 51, 44, 227, 163, 97, 217, 253, + 74, 1, 176, 168, 156, 169, 20, 81, 123, 104, 94, 45, 55, 20, 226, + 78, 48, 69, 2, 33, 0, 155, 43, 77, 144, 112, 9, 93, 86, 107, + 37, 112, 186, 114, 60, 92, 25, 53, 135, 126, 102, 158, 175, 136, 241, + 136, 240, 252, 172, 81, 250, 90, 74, 2, 32, 34, 228, 106, 213, 25, + 57, 199, 139, 113, 155, 106, 179, 117, 54, 213, 51, 30, 163, 228, 20, + 162, 93, 36, 119, 107, 5, 99, 70, 254, 111, 235, 60 }; + +//////////////////////////////////////////////////////////////////////////////// +// 50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e +constexpr uint8_t TYPE_10_LOCK_TX_ID[] = { + 80, 243, 1, 48, 109, 141, 165, 141, 98, 51, 44, 227, 163, 97, 217, 253, + 74, 1, 176, 168, 156, 169, 20, 81, 123, 104, 94, 45, 55, 20, 226, 78 }; + +//////////////////////////////////////////////////////////////////////////////// +// 30450221009b2b4d9070095d566b2570ba723c5c1935877e669eaf88f188f0fcac51fa5a4a022022e46ad51939c78b719b6ab37536d5331ea3e414a25d24776b056346fe6feb3c +constexpr uint8_t TYPE_10_SIGNATURE[] = { + 48, 69, 2, 33, 0, 155, 43, 77, 144, 112, 9, 93, 86, 107, 37, + 112, 186, 114, 60, 92, 25, 53, 135, 126, 102, 158, 175, 136, 241, 136, + 240, 252, 172, 81, 250, 90, 74, 2, 32, 34, 228, 106, 213, 25, 57, + 199, 139, 113, 155, 106, 179, 117, 54, 213, 51, 30, 163, 228, 20, 162, + 93, 36, 119, 107, 5, 99, 70, 254, 111, 235, 60 }; + +#endif diff --git a/test/transactions/types/fixtures/ipfs.hpp b/test/transactions/types/fixtures/ipfs.hpp new file mode 100644 index 00000000..b58c2f68 --- /dev/null +++ b/test/transactions/types/fixtures/ipfs.hpp @@ -0,0 +1,78 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_IPFS_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_IPFS_HPP + +#include +#include + +#include "common.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 2, +// "network": 23, +// "typeGroup": 1, +// "type": 5, +// "nonce": "1", +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "500000000", +// "asset": { +// "ipfs": "QmYSK2JyM3RyDyB52caZCTKFR3HKniEcMnNJYdk8DQ6KKB" +// }, +// "signature": "30440220636aa75a15bcc59c9cf2db11ae7f9ed73f112b6ca14cc2aca1b6ed3d2daadaad02207434f5c6d17c98350942afafbaa57b6518ea4e78e8443f414398190183235787", +// "id": "f68875f1fa80091ef42dec05c5c6a0ea77f4b83934f7eed0d1c72b910ddf6d03" +// }, +// "serialized": "ff02170100000005000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed1920065cd1d000000000012209608184d6cee2b9af8e6c2a46fc9318adf73329aeb8a86cf8472829fff5bb89e30440220636aa75a15bcc59c9cf2db11ae7f9ed73f112b6ca14cc2aca1b6ed3d2daadaad02207434f5c6d17c98350942afafbaa57b6518ea4e78e8443f414398190183235787" +// } + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_5_TYPE = 5U; +constexpr uint64_t TYPE_5_FEE = 500000000ULL; +constexpr uint64_t TYPE_5_VF_LENGTH = 0U; +constexpr uint64_t TYPE_5_IPFS_LENGTH = 34U; + +//////////////////////////////////////////////////////////////////////////////// +constexpr auto TYPE_5_IPFS_LEN_STRING = "34"; +constexpr auto TYPE_5_IPFS_STRING = "QmYSK2JyM3RyDyB52caZCTKFR3HKniEcMnNJYdk8DQ6KKB"; + +//////////////////////////////////////////////////////////////////////////////// +// ff02170100000005000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed1920065cd1d000000000012209608184d6cee2b9af8e6c2a46fc9318adf73329aeb8a86cf8472829fff5bb89e30440220636aa75a15bcc59c9cf2db11ae7f9ed73f112b6ca14cc2aca1b6ed3d2daadaad02207434f5c6d17c98350942afafbaa57b6518ea4e78e8443f414398190183235787 +const std::vector TYPE_5_BYTES = { + 255, 2, 23, 1, 0, 0, 0, 5, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 0, 101, 205, 29, 0, 0, 0, 0, 0, 18, + 32, 150, 8, 24, 77, 108, 238, 43, 154, 248, 230, 194, 164, 111, 201, + 49, 138, 223, 115, 50, 154, 235, 138, 134, 207, 132, 114, 130, 159, 255, + 91, 184, 158, 48, 68, 2, 32, 99, 106, 167, 90, 21, 188, 197, 156, + 156, 242, 219, 17, 174, 127, 158, 215, 63, 17, 43, 108, 161, 76, 194, + 172, 161, 182, 237, 61, 45, 170, 218, 173, 2, 32, 116, 52, 245, 198, + 209, 124, 152, 53, 9, 66, 175, 175, 186, 165, 123, 101, 24, 234, 78, + 120, 232, 68, 63, 65, 67, 152, 25, 1, 131, 35, 87, 135 }; + +//////////////////////////////////////////////////////////////////////////////// +// QmYSK2JyM3RyDyB52caZCTKFR3HKniEcMnNJYdk8DQ6KKB +constexpr uint8_t TYPE_5_DAG[] = { + 18, 32, 150, 8, 24, 77, 108, 238, 43, 154, 248, 230, + 194, 164, 111, 201, 49, 138, 223, 115, 50, 154, 235, 138, + 134, 207, 132, 114, 130, 159, 255, 91, 184, 158 }; + +//////////////////////////////////////////////////////////////////////////////// +// 30440220636aa75a15bcc59c9cf2db11ae7f9ed73f112b6ca14cc2aca1b6ed3d2daadaad02207434f5c6d17c98350942afafbaa57b6518ea4e78e8443f414398190183235787 +constexpr uint8_t TYPE_5_SIGNATURE[] = { + 48, 68, 2, 32, 99, 106, 167, 90, 21, 188, 197, 156, 156, 242, + 219, 17, 174, 127, 158, 215, 63, 17, 43, 108, 161, 76, 194, 172, + 161, 182, 237, 61, 45, 170, 218, 173, 2, 32, 116, 52, 245, 198, + 209, 124, 152, 53, 9, 66, 175, 175, 186, 165, 123, 101, 24, 234, + 78, 120, 232, 68, 63, 65, 67, 152, 25, 1, 131, 35, 87, 135 }; + +#endif diff --git a/test/transactions/types/fixtures/multi_payment.hpp b/test/transactions/types/fixtures/multi_payment.hpp new file mode 100644 index 00000000..ba15ceed --- /dev/null +++ b/test/transactions/types/fixtures/multi_payment.hpp @@ -0,0 +1,518 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_MULTI_PAYMENT_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_MULTI_PAYMENT_HPP + +#include +#include +#include + +#include "interfaces/constants.h" + +#include "common.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 2, +// "network": 30, +// "typeGroup": 1, +// "type": 6, +// "nonce": "1", +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "10000000", +// "amount": "0", +// "asset": { +// "payments": [ +// { +// "amount": "1", +// "recipientId": "DHKxXag9PjfjHBbPg3HQS5WCaQZdgDf6yi" +// }, +// ... +// { +// "amount": "1", +// "recipientId": "DM6emC4WnxyP2RzrNW5VMS2n3sNfz6Xpci" +// } +// ] +// }, +// "signature": "3045022100a6c327cb6798cb3dc8b282b64358dc4c24a72bff61e347009c1e0ad2a1cc6d8c022028cf62c3d0ea8c3ca6113f63b4e827bd8db8201a478f5ff40c309bbb9987a9b4", +// "id": "c9d689827cd6c6b741ebcad601ce5218bd7df96d2bf592aa5105116b89e09f80" +// }, +// "serialized": "ff021e0100000006000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192809698000000000000640001000000000000001e85af65ad6aa3a1df5bc9a6f59b9f3d394f643a2301000000000000001e4b1da1aaa9bcaec5aaf676047801070c4c1247c101000000000000001e726cd0d3e02920bcc14f6d57a5f72f64d270cee301000000000000001ee7d3776b7e6f1619aada38a5fc71a7b7f3b9bac801000000000000001ed6a2f3c639982c0059ce5fe21e5e74212d6a8c3f01000000000000001eb2be8f0c175bac6622d87ed4d8495d71bfbc49f301000000000000001e17789a2d02a50d67c84a65e2ddf6448939d20b7c01000000000000001ed97e9157cebfc74786e3315ba4aa7ba40adf6b5001000000000000001e16f4d1854c0a3f22fb2cf22e1c8b892e6c9e530e01000000000000001e6466d8815b0221778d2424d6378e659018b2af3e01000000000000001e47b0a77a1eed842c4d04c69e9f015575d9f912f001000000000000001ec9b298b06c31962b78d1e6a7865a764f740cc9cb01000000000000001e0ccece595e34853433af4f5441b00a662a825c8f01000000000000001e33eba586129729a11903c52612baec3d7099146601000000000000001e962b9245f0fd71c31bbae5a012541532a48f7cd501000000000000001eb78254011cd21f910e2e6a955204ad65c43391ef01000000000000001e8c630c8dcf60ac9a2bb47cff8ea8ed20036ef26201000000000000001e3f98bf3df9f33914e268d8ac1b40019e9c0c9d4f01000000000000001ecffe71ab5c93df97b617e5e9b720f34df8cbedfa01000000000000001e76255ae35d01b7689a2ff28588e2cb1e0418952c01000000000000001e12b44233059fb4f506aa98b743cd148f34b5374501000000000000001ebe5f8093937e60789fcacd7ef027519625c0328201000000000000001e21e900dfdb5a6095b641f9a7f6ae1dd394b6378801000000000000001e43b3341eca1ddf64ebc696c26ee029feb083a98501000000000000001e1a3f870971cff9d2a015b2138adb8c0b9013c73b01000000000000001e31cc969e25d4f20e71fc263e4470345d6a8c605d01000000000000001e9fbacd4247903118829fb46f05d3feec0da77bc501000000000000001eb0178778b898c5da79e2230df9494443bc35063001000000000000001ea53db5be5a9f2a0c861e837642247a96dbaeae6101000000000000001e0839a1a0a8a71d78c04837095cbe211d5faed8d001000000000000001e5ca90fd2bdce51e7ad1574783608dd1aa5abcce901000000000000001e01adfb8ee042147c982ac263be5cc7c55f64f2ac01000000000000001e2cae82cc5758430ddf5ee37776078ed9227c186701000000000000001e87eec317213c528fb6dc6331cb6884ccd7ac900f01000000000000001e8eea6972cfbca3b8c4bffa0165b03c58fe12494001000000000000001e152b45e09dedc366c75975252a71158102ab7fa001000000000000001e0f3b9b918bd9da5f48cef13bdb67030bb4b2b2e001000000000000001e34fa1cd982e09c4c8688d222008a9deadcc8f97b01000000000000001e97022f547a07c324524fc330f4129f0d0321510a01000000000000001edf0d61a5e6fa4bcbe4c08c824032ca25bd3632f901000000000000001ebcbea098c71502bca11e3cfd7885bb12abdc2c6601000000000000001e860004437afd98ce7287a642a4b2716d96ea4c4c01000000000000001e749c4a9a4da5540be28bc5d0a736fc686edd66c801000000000000001eb696c37775d855c5f1f5383c207f5fbc66a9825401000000000000001e4436cd79b7d8a297ad97c8278fa25cfbc856411601000000000000001efd0a4f086f6201bfbf49c1a608855ab5572bbe9b01000000000000001e71567a12ef5279edf6ea900ab95f9ee6affd84ca01000000000000001e75d881f807e5142da117a2325c75b208b54ec80e01000000000000001e231b0309a2972157f5cbc13c34562b3eb4f67bc601000000000000001e3a222f0ef5d4d27b2d6a6f3b102ee5c1ff3e75f401000000000000001e064b1e0d6d9ae7af3e03c2ca55fadbd6276714c601000000000000001ecd4d4aa981f760a9cad81733c8951164c908961301000000000000001e3327649ad69e18dbaca72f6153b30a17ac548af401000000000000001effc4dac85fff001f2b4a54d89339686e16fc565a01000000000000001ef5d3f74e3ab2b324469ae2bc5040648ba6fdae8201000000000000001e6139ca0c96c3e1d52650e9884875d95155963e0e01000000000000001e166814fc75ef2e6317115f5cee5563690584c10d01000000000000001efcc5084066ca4530c32c576648a79d7f7b4abc6901000000000000001ecb1270bd5e044ee1596a45a0169b07829c6fb13a01000000000000001e7f8a1b9875b37b00f98d74ff746adf4c19b0e12f01000000000000001e3ea2ae7196083204d515ab83fd0c6bad822c8ee401000000000000001ef7486d2ad0421421794d8a4c473d3d382d22c84201000000000000001e6fdb366937d4310a403763aae68ff627350d1b3601000000000000001e6ad5d2dfe8cfbe489c6491f16220ddcf2d60706f01000000000000001e6cbe19d65c58da5538d3ccfba3d8a06c4d45ac3f01000000000000001e93849e3dae9b3cfb372af82d7bdcf337248adab801000000000000001e547d56bf932a82cc6a9f8fdc7473fc8867f925d901000000000000001e2f1ae094509e1f3cb8539063330d1b2a6dd6eccb01000000000000001e4a686fe225caa978864d50857d8bd7249f3f6d5301000000000000001e6d0623357e34d8aa5d026abd3a983f11b45c850901000000000000001ed177dfdfc304b4995422a41bb0d33cee6454856601000000000000001ec5005a3f733d8964fbfdec22863a40e1983e405501000000000000001e8f0c420efed85f8bbde89d51a6c04ccebcebfabc01000000000000001eb12baa7f233b569698f17c299b692eeaa919fa2201000000000000001ef642d4d5be03840ba70f164aabff4920f63016da01000000000000001e154a88b33732d23e35d971c5246c9546e795c2d701000000000000001eed05de0c6ed0f7c255a801345956a662571dc89101000000000000001e165a09dea1d7bf52e0da06d95f6aa3c59ed7605c01000000000000001ed5071f07780660be988d474e5746d9ff2df9e38601000000000000001e793fe0bbbdbbb67f25e9172a8d0864aa42c5c8e501000000000000001e86ec350a3d519da4875c33e21801656b2018a0de01000000000000001e1077b75b2e00bf9e1401e3e8ec416add4bc968c301000000000000001e7b7176af2cef35079c5cc2c756a2f4da87b323d701000000000000001e9c57d9688f92567233b962a632e3c17981e2e48501000000000000001e7515b73773b8714d767c6ceabef46a791e32d8e201000000000000001ea284016ffd570bdd33e1e24246453c582df317a001000000000000001ebf287e20c8497bf114ea24976e55369446239e4201000000000000001e424a1709b337a97c400ac08c0bb403ad8e0054dc01000000000000001e36803ea2ab3e4fbe420dcb6d127d22a47e8e553501000000000000001ec5c837b703a5476d84b1b289552c501fb53b2da001000000000000001ef9d5bd6441fc3f3b09166dd70e776c239f5e9a8a01000000000000001e6085dad946d02e81527ea43d26900c814d73444e01000000000000001ef873fb682b47bc8ac37944e390ce2867ef86bd1201000000000000001e08f37a6587b5c09a7b90aad5586d9e79a3dbdca501000000000000001e4b6294e1d5a1a2a448a36ba6f32f19eba279355701000000000000001eae21b5575d04e7c2a192f8a41533e7ea4201e6b901000000000000001e6eb03010d0160f82a80e064140d33b386b985ac001000000000000001e0710cb2739dda5521955b96b4309324a1182f60601000000000000001eb4b42b7ecd576bdfac4886c2a11bfceecc31d83201000000000000001eaf0ba9bd7cb2a23220eac8310f793fd9e846bf693045022100a6c327cb6798cb3dc8b282b64358dc4c24a72bff61e347009c1e0ad2a1cc6d8c022028cf62c3d0ea8c3ca6113f63b4e827bd8db8201a478f5ff40c309bbb9987a9b4" +// } + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_6_TYPE = 6U; +constexpr uint64_t TYPE_6_FEE = 10000000ULL; +constexpr uint8_t TYPE_6_ASSET_OFFSET = 59U; +constexpr uint16_t TYPE_6_N_PAYMENTS = 100U; +constexpr uint64_t TYPE_6_AMOUNT = 1ULL; + +//////////////////////////////////////////////////////////////////////////////// +constexpr auto TYPE_6_N_PAYMENTS_STRING = "100"; + +//////////////////////////////////////////////////////////////////////////////// +constexpr auto TYPE_6_AMOUNTS_STRING = + "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1," + "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1," + "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1"; + +//////////////////////////////////////////////////////////////////////////////// +constexpr auto TYPE_6_ADDRESSES_STRING = + "DHKxXag9PjfjHBbPg3HQS5WCaQZdgDf6yi,DBzGiUk8UVjB2dKCfGRixknB7Ki3Zhqthp," + "DFa7vn1LvWAyTuVDrQUr5NKaM73cfjx2Cp,DSGsxX84gif4ipAxZjjCE2k2YpHmsNTJeY," + "DQhzMRvVoCYCiZH2iSyuqCTcayz7z4XTKx,DMSD6fFT1Xcxh4ErYExr5MtGnEuDcYu22m," + "D7HCZG8hJuJqu9rANRdyNr6N1vpH2vbyx8,DQy6ny2bkvWiDLboQMZ1cxnmoNC5JM228w," + "D7EUgmA78qUaSfsNedfgKs2ALq28FhL3zo,DEHyKHdtzHqTghfpwaBcvTzLpgPP5AAUgE," + "DBgA92a616rwVi9GsgYUwBq9Y7dgvZiC41,DPXaJv1GcVpZPvxw5T4fXebqTVhFpfqyrC," + "D6JpPhN7BehrhNy7AbSQ2u9mkSZb1k7Ens,D9sdJ42YtJpXeL7Fa1cTLiciW7FpGYqms4," + "DJq86RdmTMKC257szcXRXKpbuoYPaL8KgL,DMsQKioFXniGH3vHDNfkQLRRqsQsAS46Cy," + "DHwPoAP8cMP9ZeKrhh5c99WzaoJqFKW2qi,DAwN6Pp4ErGf69EypErrbtuWFfEMtuSzmE," + "DQ6sE3jE9rTFC13e2ndooRdy5YCYinLbPm,DFuo2NGezzHGwHjzG6c21JuJ9WpntLGFER," + "D6qzeJEGG7rEBem5bNCCZqHtPCBtzsUZpP,DNVhLKTPh4LnqhcnkogNS8iSxmsnFG17tC," + "D8EPxx42Dr4bd8xXRbMHi8LHefFLaT2VaM,DBK4VPsUQHUYkGc47FqF69PEyPjjqvnGzu," + "D7XtDDKh2VrRtz5rtbBicfgSEoEQzEZiNx,D9gQjhu2tDUstXfrbK85zHi23VtAk72qsS," + "DKhfkyY4RZyxR7CFjQAeNtGKXAaVEBa9HK,DMCBerfV13HBuJEwJTZRVTWzrYDxgb3QSy," + "DLCoxbHdf9LMhEavEnj8mGv4AwVk8eEiKd,D5taz6B4xDk1LD3jV4fYrUhaKC8DnTtziW," + "DDb3EXY3refv2f5ymMME3hp2DXFqMPzGah,D5HydybffvfuwdbBKQ1dnhiXzNnWq6CgQz," + "D9DMKvx8fDyWyAP1EUGs5McBwwv3y5E1Yn,DHXqndno9dBvGabhc7NdZWuoj6nRUdSaP7," + "DJAmJiuLQGnzXWmH7KvosVLks7hhfxQ8np,D752ZwkQZKm5gYYMUZV2tKFFZZaD35MtRa," + "D6Xe5kVsK7axaPZ1tP2fVWaLFubyCajkVq,D9yDJNK4xHP9Gx27s187Z5XHcNF5YFA94h," + "DJuZC2smL8j86bUNrZiNceAubad3zs3drS,DRUVFo5MjNrMHHQCwpVPH6AwLL2AULpgbH," + "DNM5wLmqVUz6UgY14mt6BsMndy8JGcFwct,DHMd77xyB8f6DnSCgxaRWfuM86cwyH76EH," + "DFmg9q2KqGyretLazvRjWdynJAZbPkZPG2,DMnY8QWLAmsb4wNEjRVvtQsNWF4SbXntxM," + "DBMn94FxVB36nXgzbmtmfu6jVEGwwHNyNA,DUD3r46LtArk4msu6jFrwn1hjxbZoXzX9t," + "DFUNVTBd5zFexBaHkymr4UJqsHeXhPLKUF,DFtCxvMSsF9qfw2mH1aNHxusXGJ2QzCahP," + "D8LiYnmH4DLDyxCLTV7RrqxtCA21pfGkb9,DASUgX1U7yvp8WDQ57QoTEUim6bqTYzxGw," + "D5iNaf5ZckhdZivPfy6vFvBLeBDJtvDoGo,DPrdeuFDcfMujvYK6n18RBAWgh7hYeiDeZ," + "D9oaC7bd2YaJYHDdGkUdyAnfpkBrFFKZHy,DUTUfseKR6qJRjqCuxeH3oRxMr6EFLUxRW," + "DTYv1v3YdUNy81kzD1VhRx6e8jkDYnvCoh,DE1BK8iL17PiBwo9TUCfkkm1vnUkobBwj8," + "D7Ba6DnbpPJgqVnQNdN7baRsZs1DKvptMM,DUBcre2e5KMykYr6xK56T5BrwkKMZkUF8r," + "DPeqoTgBbRhyuEJtMnhqhSAeK32ymMNvjd,DGmToX3GrCEUC8EJdZrXWTYFNWqQz1VVhX," + "DArHKTMXf3F5zXS1i3GSwni9aA8TX1yQvh,DTgcCvAYR2XdzUHv4WuEB6aShYEbh2MYp6," + "DFLY9huetM6GMrt9EGp6sXDEiC7r3oSYHg,DEszKKqdRipXiF7BDKS2Q4iJwwfzLdwADK," + "DF45FRKUYcyUGeZyyTF3sCYJ8VFNXymzhJ,DJb6jvhSvw2RmxCBEQSAzG6tjUs5rK3a5m," + "DCqqT4x4on1dsbgUKRWkuZsdZaoohYK6NV,D9SAVjqkxwWQmb82iqAedJPccFjDUnMSi9," + "DBvXesEgzrAsm9YrzRb2jithR2hg7SZpuq,DF5ZYcQsmgDvH6cVQR87xvXapyTUFB1a5R," + "DQEfNNsJ6PQTA9abwWdiunPuebLZAhxbpZ,DP6k5YTtaeNJUHZ72H6QtugFaHVB5vEBQe," + "DJBTrPo6sDMGr2kcswTwDWtQyYV5adqnAp,DMHtTBMyG5qGYgcZNRkb36XaCT3TUSKmYE," + "DTbCpyVgTeJw4idpqbY5jwrEi7SxSid9GU,D75g1ztcaHi46eUFRnakRryqG7GV9xsgGC," + "DSkMiPrEx3YF6ijDjxhwCnbAbriC8sWKEW,D7BHGU3UedoxpZko4nBLcz5oRtSmUmRfy6," + "DQZUzueTvUJb5tnhBCziPYaMnzaoui4F57,DGCCpnJ86YvxJAkHRPhC5jTBNGsy5PEDRh," + "DHSW3vi66L63xnzRt9PadwSVrb9bCKhgvJ,D6eAmMh6FFynorCSjHS1Qx75rXiN89soa7," + "DGPoaSg15fb6As8bPKBAQrK3nCDpgfYow8,DKPmC4G1ZEwxb5hZro2sJwRWJ1pQixcK6N," + "DFpBcXzcJdFaN9rfVD8Nc5yWFvJ3DnePwa,DKxQ9FDTBDQaLLV24sWc625w2Kycw2RoqR," + "DNZr7NxGm97r8hV6Jg4rv3S5MgJrEVUWNQ,DBBbtnKKDAW84bDjywEmQFLgwf36DxJHZJ," + "DA7GfDKG5zYFeiZ1C4FPJBeTajpYNvXxcC,DPAsWQyuxYkiMRzhngKBPTpWb6WWqxdd1T," + "DTv6qvhUK1jks58gAA5YFzShHf3YX9sJVo,DDwTm5FbgvWugYekvjE1dzantAVKxtGbNo," + "DTno4QZdEyAokZHxQZcYrErqMLVE19PgCb,D5xRcwzEGSN83nyuGN74Sw8f353vDmm2tt," + "DC1hKDKyFbtMhiTc79mmPS99SJnQLsvvH3,DM1pVjbHA3Q4dezcwGBjmT54cLYqpx1NtZ," + "DFEMw6jihEKRJ9CT3k8Rj73PLKDGDyQLU1,D5nTPSQFkt9W6mNzdy5ks5PiHkhDqswxDY," + "DMca1DGMxj8w59dWYmji1YC1xLP7AmL6rA,DM6emC4WnxyP2RzrNW5VMS2n3sNfz6Xpci"; + +//////////////////////////////////////////////////////////////////////////////// +// 1e85af65ad6aa3a1df5bc9a6f59b9f3d394f643a23 +constexpr uint8_t TYPE_6_FIRST_ADDRESS[] = { + 30, 133, 175, 101, 173, 106, 163, 161, 223, 91, 201, + 166, 245, 155, 159, 61, 57, 79, 100, 58, 35 }; + +//////////////////////////////////////////////////////////////////////////////// +// 1eaf0ba9bd7cb2a23220eac8310f793fd9e846bf69 +constexpr uint8_t TYPE_6_LAST_ADDRESS[] = { + 30, 175, 11, 169, 189, 124, 178, 162, 50, 32, 234, + 200, 49, 15, 121, 63, 217, 232, 70, 191, 105 }; + +//////////////////////////////////////////////////////////////////////////////// +// 3045022100a6c327cb6798cb3dc8b282b64358dc4c24a72bff61e347009c1e0ad2a1cc6d8c022028cf62c3d0ea8c3ca6113f63b4e827bd8db8201a478f5ff40c309bbb9987a9b4 +constexpr uint8_t TYPE_6_SIGNATURE[] = { + 48, 69, 2, 33, 0, 166, 195, 39, 203, 103, 152, 203, 61, 200, 178, + 130, 182, 67, 88, 220, 76, 36, 167, 43, 255, 97, 227, 71, 0, 156, + 30, 10, 210, 161, 204, 109, 140, 2, 32, 40, 207, 98, 195, 208, 234, + 140, 60, 166, 17, 63, 99, 180, 232, 39, 189, 141, 184, 32, 26, 71, + 143, 95, 244, 12, 48, 155, 187, 153, 135, 169, 180 }; + +//////////////////////////////////////////////////////////////////////////////// +const std::vector TYPE_6_AMOUNTS = { + 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, + 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, + 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, + 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, + 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, + 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, + 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, + 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, + 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, + 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL +}; + +//////////////////////////////////////////////////////////////////////////////// +const std::vector > TYPE_6_ADDRESSES = { + { 30, 133, 175, 101, 173, 106, 163, 161, 223, 91, 201, 166, 245, 155, 159, 61, 57, 79, 100, 58, 35 }, + { 30, 75, 29, 161, 170, 169, 188, 174, 197, 170, 246, 118, 4, 120, 1, 7, 12, 76, 18, 71, 193 }, + { 30, 114, 108, 208, 211, 224, 41, 32, 188, 193, 79, 109, 87, 165, 247, 47, 100, 210, 112, 206, 227 }, + { 30, 231, 211, 119, 107, 126, 111, 22, 25, 170, 218, 56, 165, 252, 113, 167, 183, 243, 185, 186, 200 }, + { 30, 214, 162, 243, 198, 57, 152, 44, 0, 89, 206, 95, 226, 30, 94, 116, 33, 45, 106, 140, 63 }, + { 30, 178, 190, 143, 12, 23, 91, 172, 102, 34, 216, 126, 212, 216, 73, 93, 113, 191, 188, 73, 243 }, + { 30, 23, 120, 154, 45, 2, 165, 13, 103, 200, 74, 101, 226, 221, 246, 68, 137, 57, 210, 11, 124 }, + { 30, 217, 126, 145, 87, 206, 191, 199, 71, 134, 227, 49, 91, 164, 170, 123, 164, 10, 223, 107, 80 }, + { 30, 22, 244, 209, 133, 76, 10, 63, 34, 251, 44, 242, 46, 28, 139, 137, 46, 108, 158, 83, 14 }, + { 30, 100, 102, 216, 129, 91, 2, 33, 119, 141, 36, 36, 214, 55, 142, 101, 144, 24, 178, 175, 62 }, + { 30, 71, 176, 167, 122, 30, 237, 132, 44, 77, 4, 198, 158, 159, 1, 85, 117, 217, 249, 18, 240 }, + { 30, 201, 178, 152, 176, 108, 49, 150, 43, 120, 209, 230, 167, 134, 90, 118, 79, 116, 12, 201, 203 }, + { 30, 12, 206, 206, 89, 94, 52, 133, 52, 51, 175, 79, 84, 65, 176, 10, 102, 42, 130, 92, 143 }, + { 30, 51, 235, 165, 134, 18, 151, 41, 161, 25, 3, 197, 38, 18, 186, 236, 61, 112, 153, 20, 102 }, + { 30, 150, 43, 146, 69, 240, 253, 113, 195, 27, 186, 229, 160, 18, 84, 21, 50, 164, 143, 124, 213 }, + { 30, 183, 130, 84, 1, 28, 210, 31, 145, 14, 46, 106, 149, 82, 4, 173, 101, 196, 51, 145, 239 }, + { 30, 140, 99, 12, 141, 207, 96, 172, 154, 43, 180, 124, 255, 142, 168, 237, 32, 3, 110, 242, 98 }, + { 30, 63, 152, 191, 61, 249, 243, 57, 20, 226, 104, 216, 172, 27, 64, 1, 158, 156, 12, 157, 79 }, + { 30, 207, 254, 113, 171, 92, 147, 223, 151, 182, 23, 229, 233, 183, 32, 243, 77, 248, 203, 237, 250 }, + { 30, 118, 37, 90, 227, 93, 1, 183, 104, 154, 47, 242, 133, 136, 226, 203, 30, 4, 24, 149, 44 }, + { 30, 18, 180, 66, 51, 5, 159, 180, 245, 6, 170, 152, 183, 67, 205, 20, 143, 52, 181, 55, 69 }, + { 30, 190, 95, 128, 147, 147, 126, 96, 120, 159, 202, 205, 126, 240, 39, 81, 150, 37, 192, 50, 130 }, + { 30, 33, 233, 0, 223, 219, 90, 96, 149, 182, 65, 249, 167, 246, 174, 29, 211, 148, 182, 55, 136 }, + { 30, 67, 179, 52, 30, 202, 29, 223, 100, 235, 198, 150, 194, 110, 224, 41, 254, 176, 131, 169, 133 }, + { 30, 26, 63, 135, 9, 113, 207, 249, 210, 160, 21, 178, 19, 138, 219, 140, 11, 144, 19, 199, 59 }, + { 30, 49, 204, 150, 158, 37, 212, 242, 14, 113, 252, 38, 62, 68, 112, 52, 93, 106, 140, 96, 93 }, + { 30, 159, 186, 205, 66, 71, 144, 49, 24, 130, 159, 180, 111, 5, 211, 254, 236, 13, 167, 123, 197 }, + { 30, 176, 23, 135, 120, 184, 152, 197, 218, 121, 226, 35, 13, 249, 73, 68, 67, 188, 53, 6, 48 }, + { 30, 165, 61, 181, 190, 90, 159, 42, 12, 134, 30, 131, 118, 66, 36, 122, 150, 219, 174, 174, 97 }, + { 30, 8, 57, 161, 160, 168, 167, 29, 120, 192, 72, 55, 9, 92, 190, 33, 29, 95, 174, 216, 208 }, + { 30, 92, 169, 15, 210, 189, 206, 81, 231, 173, 21, 116, 120, 54, 8, 221, 26, 165, 171, 204, 233 }, + { 30, 1, 173, 251, 142, 224, 66, 20, 124, 152, 42, 194, 99, 190, 92, 199, 197, 95, 100, 242, 172 }, + { 30, 44, 174, 130, 204, 87, 88, 67, 13, 223, 94, 227, 119, 118, 7, 142, 217, 34, 124, 24, 103 }, + { 30, 135, 238, 195, 23, 33, 60, 82, 143, 182, 220, 99, 49, 203, 104, 132, 204, 215, 172, 144, 15 }, + { 30, 142, 234, 105, 114, 207, 188, 163, 184, 196, 191, 250, 1, 101, 176, 60, 88, 254, 18, 73, 64 }, + { 30, 21, 43, 69, 224, 157, 237, 195, 102, 199, 89, 117, 37, 42, 113, 21, 129, 2, 171, 127, 160 }, + { 30, 15, 59, 155, 145, 139, 217, 218, 95, 72, 206, 241, 59, 219, 103, 3, 11, 180, 178, 178, 224 }, + { 30, 52, 250, 28, 217, 130, 224, 156, 76, 134, 136, 210, 34, 0, 138, 157, 234, 220, 200, 249, 123 }, + { 30, 151, 2, 47, 84, 122, 7, 195, 36, 82, 79, 195, 48, 244, 18, 159, 13, 3, 33, 81, 10 }, + { 30, 223, 13, 97, 165, 230, 250, 75, 203, 228, 192, 140, 130, 64, 50, 202, 37, 189, 54, 50, 249 }, + { 30, 188, 190, 160, 152, 199, 21, 2, 188, 161, 30, 60, 253, 120, 133, 187, 18, 171, 220, 44, 102 }, + { 30, 134, 0, 4, 67, 122, 253, 152, 206, 114, 135, 166, 66, 164, 178, 113, 109, 150, 234, 76, 76 }, + { 30, 116, 156, 74, 154, 77, 165, 84, 11, 226, 139, 197, 208, 167, 54, 252, 104, 110, 221, 102, 200 }, + { 30, 182, 150, 195, 119, 117, 216, 85, 197, 241, 245, 56, 60, 32, 127, 95, 188, 102, 169, 130, 84 }, + { 30, 68, 54, 205, 121, 183, 216, 162, 151, 173, 151, 200, 39, 143, 162, 92, 251, 200, 86, 65, 22 }, + { 30, 253, 10, 79, 8, 111, 98, 1, 191, 191, 73, 193, 166, 8, 133, 90, 181, 87, 43, 190, 155 }, + { 30, 113, 86, 122, 18, 239, 82, 121, 237, 246, 234, 144, 10, 185, 95, 158, 230, 175, 253, 132, 202 }, + { 30, 117, 216, 129, 248, 7, 229, 20, 45, 161, 23, 162, 50, 92, 117, 178, 8, 181, 78, 200, 14 }, + { 30, 35, 27, 3, 9, 162, 151, 33, 87, 245, 203, 193, 60, 52, 86, 43, 62, 180, 246, 123, 198 }, + { 30, 58, 34, 47, 14, 245, 212, 210, 123, 45, 106, 111, 59, 16, 46, 229, 193, 255, 62, 117, 244 }, + { 30, 6, 75, 30, 13, 109, 154, 231, 175, 62, 3, 194, 202, 85, 250, 219, 214, 39, 103, 20, 198 }, + { 30, 205, 77, 74, 169, 129, 247, 96, 169, 202, 216, 23, 51, 200, 149, 17, 100, 201, 8, 150, 19 }, + { 30, 51, 39, 100, 154, 214, 158, 24, 219, 172, 167, 47, 97, 83, 179, 10, 23, 172, 84, 138, 244 }, + { 30, 255, 196, 218, 200, 95, 255, 0, 31, 43, 74, 84, 216, 147, 57, 104, 110, 22, 252, 86, 90 }, + { 30, 245, 211, 247, 78, 58, 178, 179, 36, 70, 154, 226, 188, 80, 64, 100, 139, 166, 253, 174, 130 }, + { 30, 97, 57, 202, 12, 150, 195, 225, 213, 38, 80, 233, 136, 72, 117, 217, 81, 85, 150, 62, 14 }, + { 30, 22, 104, 20, 252, 117, 239, 46, 99, 23, 17, 95, 92, 238, 85, 99, 105, 5, 132, 193, 13 }, + { 30, 252, 197, 8, 64, 102, 202, 69, 48, 195, 44, 87, 102, 72, 167, 157, 127, 123, 74, 188, 105 }, + { 30, 203, 18, 112, 189, 94, 4, 78, 225, 89, 106, 69, 160, 22, 155, 7, 130, 156, 111, 177, 58 }, + { 30, 127, 138, 27, 152, 117, 179, 123, 0, 249, 141, 116, 255, 116, 106, 223, 76, 25, 176, 225, 47 }, + { 30, 62, 162, 174, 113, 150, 8, 50, 4, 213, 21, 171, 131, 253, 12, 107, 173, 130, 44, 142, 228 }, + { 30, 247, 72, 109, 42, 208, 66, 20, 33, 121, 77, 138, 76, 71, 61, 61, 56, 45, 34, 200, 66 }, + { 30, 111, 219, 54, 105, 55, 212, 49, 10, 64, 55, 99, 170, 230, 143, 246, 39, 53, 13, 27, 54 }, + { 30, 106, 213, 210, 223, 232, 207, 190, 72, 156, 100, 145, 241, 98, 32, 221, 207, 45, 96, 112, 111 }, + { 30, 108, 190, 25, 214, 92, 88, 218, 85, 56, 211, 204, 251, 163, 216, 160, 108, 77, 69, 172, 63 }, + { 30, 147, 132, 158, 61, 174, 155, 60, 251, 55, 42, 248, 45, 123, 220, 243, 55, 36, 138, 218, 184 }, + { 30, 84, 125, 86, 191, 147, 42, 130, 204, 106, 159, 143, 220, 116, 115, 252, 136, 103, 249, 37, 217 }, + { 30, 47, 26, 224, 148, 80, 158, 31, 60, 184, 83, 144, 99, 51, 13, 27, 42, 109, 214, 236, 203 }, + { 30, 74, 104, 111, 226, 37, 202, 169, 120, 134, 77, 80, 133, 125, 139, 215, 36, 159, 63, 109, 83 }, + { 30, 109, 6, 35, 53, 126, 52, 216, 170, 93, 2, 106, 189, 58, 152, 63, 17, 180, 92, 133, 9 }, + { 30, 209, 119, 223, 223, 195, 4, 180, 153, 84, 34, 164, 27, 176, 211, 60, 238, 100, 84, 133, 102 }, + { 30, 197, 0, 90, 63, 115, 61, 137, 100, 251, 253, 236, 34, 134, 58, 64, 225, 152, 62, 64, 85 }, + { 30, 143, 12, 66, 14, 254, 216, 95, 139, 189, 232, 157, 81, 166, 192, 76, 206, 188, 235, 250, 188 }, + { 30, 177, 43, 170, 127, 35, 59, 86, 150, 152, 241, 124, 41, 155, 105, 46, 234, 169, 25, 250, 34 }, + { 30, 246, 66, 212, 213, 190, 3, 132, 11, 167, 15, 22, 74, 171, 255, 73, 32, 246, 48, 22, 218 }, + { 30, 21, 74, 136, 179, 55, 50, 210, 62, 53, 217, 113, 197, 36, 108, 149, 70, 231, 149, 194, 215 }, + { 30, 237, 5, 222, 12, 110, 208, 247, 194, 85, 168, 1, 52, 89, 86, 166, 98, 87, 29, 200, 145 }, + { 30, 22, 90, 9, 222, 161, 215, 191, 82, 224, 218, 6, 217, 95, 106, 163, 197, 158, 215, 96, 92 }, + { 30, 213, 7, 31, 7, 120, 6, 96, 190, 152, 141, 71, 78, 87, 70, 217, 255, 45, 249, 227, 134 }, + { 30, 121, 63, 224, 187, 189, 187, 182, 127, 37, 233, 23, 42, 141, 8, 100, 170, 66, 197, 200, 229 }, + { 30, 134, 236, 53, 10, 61, 81, 157, 164, 135, 92, 51, 226, 24, 1, 101, 107, 32, 24, 160, 222 }, + { 30, 16, 119, 183, 91, 46, 0, 191, 158, 20, 1, 227, 232, 236, 65, 106, 221, 75, 201, 104, 195 }, + { 30, 123, 113, 118, 175, 44, 239, 53, 7, 156, 92, 194, 199, 86, 162, 244, 218, 135, 179, 35, 215 }, + { 30, 156, 87, 217, 104, 143, 146, 86, 114, 51, 185, 98, 166, 50, 227, 193, 121, 129, 226, 228, 133 }, + { 30, 117, 21, 183, 55, 115, 184, 113, 77, 118, 124, 108, 234, 190, 244, 106, 121, 30, 50, 216, 226 }, + { 30, 162, 132, 1, 111, 253, 87, 11, 221, 51, 225, 226, 66, 70, 69, 60, 88, 45, 243, 23, 160 }, + { 30, 191, 40, 126, 32, 200, 73, 123, 241, 20, 234, 36, 151, 110, 85, 54, 148, 70, 35, 158, 66 }, + { 30, 66, 74, 23, 9, 179, 55, 169, 124, 64, 10, 192, 140, 11, 180, 3, 173, 142, 0, 84, 220 }, + { 30, 54, 128, 62, 162, 171, 62, 79, 190, 66, 13, 203, 109, 18, 125, 34, 164, 126, 142, 85, 53 }, + { 30, 197, 200, 55, 183, 3, 165, 71, 109, 132, 177, 178, 137, 85, 44, 80, 31, 181, 59, 45, 160 }, + { 30, 249, 213, 189, 100, 65, 252, 63, 59, 9, 22, 109, 215, 14, 119, 108, 35, 159, 94, 154, 138 }, + { 30, 96, 133, 218, 217, 70, 208, 46, 129, 82, 126, 164, 61, 38, 144, 12, 129, 77, 115, 68, 78 }, + { 30, 248, 115, 251, 104, 43, 71, 188, 138, 195, 121, 68, 227, 144, 206, 40, 103, 239, 134, 189, 18 }, + { 30, 8, 243, 122, 101, 135, 181, 192, 154, 123, 144, 170, 213, 88, 109, 158, 121, 163, 219, 220, 165 }, + { 30, 75, 98, 148, 225, 213, 161, 162, 164, 72, 163, 107, 166, 243, 47, 25, 235, 162, 121, 53, 87 }, + { 30, 174, 33, 181, 87, 93, 4, 231, 194, 161, 146, 248, 164, 21, 51, 231, 234, 66, 1, 230, 185 }, + { 30, 110, 176, 48, 16, 208, 22, 15, 130, 168, 14, 6, 65, 64, 211, 59, 56, 107, 152, 90, 192 }, + { 30, 7, 16, 203, 39, 57, 221, 165, 82, 25, 85, 185, 107, 67, 9, 50, 74, 17, 130, 246, 6 }, + { 30, 180, 180, 43, 126, 205, 87, 107, 223, 172, 72, 134, 194, 161, 27, 252, 238, 204, 49, 216, 50 }, + { 30, 175, 11, 169, 189, 124, 178, 162, 50, 32, 234, 200, 49, 15, 121, 63, 217, 232, 70, 191, 105 } +}; + +//////////////////////////////////////////////////////////////////////////////// +// ff021e0100000006000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed1928096980000000000006400 +// 01000000000000001e85af65ad6aa3a1df5bc9a6f59b9f3d394f643a2301000000000000001e4b1da1aaa9bcaec5aaf676047801070c4c1247c1 +// 01000000000000001e726cd0d3e02920bcc14f6d57a5f72f64d270cee301000000000000001ee7d3776b7e6f1619aada38a5fc71a7b7f3b9bac8 +// 01000000000000001ed6a2f3c639982c0059ce5fe21e5e74212d6a8c3f01000000000000001eb2be8f0c175bac6622d87ed4d8495d71bfbc49f3 +// 01000000000000001e17789a2d02a50d67c84a65e2ddf6448939d20b7c01000000000000001ed97e9157cebfc74786e3315ba4aa7ba40adf6b50 +// 01000000000000001e16f4d1854c0a3f22fb2cf22e1c8b892e6c9e530e01000000000000001e6466d8815b0221778d2424d6378e659018b2af3e +// 01000000000000001e47b0a77a1eed842c4d04c69e9f015575d9f912f001000000000000001ec9b298b06c31962b78d1e6a7865a764f740cc9cb +// 01000000000000001e0ccece595e34853433af4f5441b00a662a825c8f01000000000000001e33eba586129729a11903c52612baec3d70991466 +// 01000000000000001e962b9245f0fd71c31bbae5a012541532a48f7cd501000000000000001eb78254011cd21f910e2e6a955204ad65c43391ef +// 01000000000000001e8c630c8dcf60ac9a2bb47cff8ea8ed20036ef26201000000000000001e3f98bf3df9f33914e268d8ac1b40019e9c0c9d4f +// 01000000000000001ecffe71ab5c93df97b617e5e9b720f34df8cbedfa01000000000000001e76255ae35d01b7689a2ff28588e2cb1e0418952c +// 01000000000000001e12b44233059fb4f506aa98b743cd148f34b5374501000000000000001ebe5f8093937e60789fcacd7ef027519625c03282 +// 01000000000000001e21e900dfdb5a6095b641f9a7f6ae1dd394b6378801000000000000001e43b3341eca1ddf64ebc696c26ee029feb083a985 +// 01000000000000001e1a3f870971cff9d2a015b2138adb8c0b9013c73b01000000000000001e31cc969e25d4f20e71fc263e4470345d6a8c605d +// 01000000000000001e9fbacd4247903118829fb46f05d3feec0da77bc501000000000000001eb0178778b898c5da79e2230df9494443bc350630 +// 01000000000000001ea53db5be5a9f2a0c861e837642247a96dbaeae6101000000000000001e0839a1a0a8a71d78c04837095cbe211d5faed8d0 +// 01000000000000001e5ca90fd2bdce51e7ad1574783608dd1aa5abcce901000000000000001e01adfb8ee042147c982ac263be5cc7c55f64f2ac +// 01000000000000001e2cae82cc5758430ddf5ee37776078ed9227c186701000000000000001e87eec317213c528fb6dc6331cb6884ccd7ac900f +// 01000000000000001e8eea6972cfbca3b8c4bffa0165b03c58fe12494001000000000000001e152b45e09dedc366c75975252a71158102ab7fa0 +// 01000000000000001e0f3b9b918bd9da5f48cef13bdb67030bb4b2b2e001000000000000001e34fa1cd982e09c4c8688d222008a9deadcc8f97b +// 01000000000000001e97022f547a07c324524fc330f4129f0d0321510a01000000000000001edf0d61a5e6fa4bcbe4c08c824032ca25bd3632f9 +// 01000000000000001ebcbea098c71502bca11e3cfd7885bb12abdc2c6601000000000000001e860004437afd98ce7287a642a4b2716d96ea4c4c +// 01000000000000001e749c4a9a4da5540be28bc5d0a736fc686edd66c801000000000000001eb696c37775d855c5f1f5383c207f5fbc66a98254 +// 01000000000000001e4436cd79b7d8a297ad97c8278fa25cfbc856411601000000000000001efd0a4f086f6201bfbf49c1a608855ab5572bbe9b +// 01000000000000001e71567a12ef5279edf6ea900ab95f9ee6affd84ca01000000000000001e75d881f807e5142da117a2325c75b208b54ec80e +// 01000000000000001e231b0309a2972157f5cbc13c34562b3eb4f67bc601000000000000001e3a222f0ef5d4d27b2d6a6f3b102ee5c1ff3e75f4 +// 01000000000000001e064b1e0d6d9ae7af3e03c2ca55fadbd6276714c601000000000000001ecd4d4aa981f760a9cad81733c8951164c9089613 +// 01000000000000001e3327649ad69e18dbaca72f6153b30a17ac548af401000000000000001effc4dac85fff001f2b4a54d89339686e16fc565a +// 01000000000000001ef5d3f74e3ab2b324469ae2bc5040648ba6fdae8201000000000000001e6139ca0c96c3e1d52650e9884875d95155963e0e +// 01000000000000001e166814fc75ef2e6317115f5cee5563690584c10d01000000000000001efcc5084066ca4530c32c576648a79d7f7b4abc69 +// 01000000000000001ecb1270bd5e044ee1596a45a0169b07829c6fb13a01000000000000001e7f8a1b9875b37b00f98d74ff746adf4c19b0e12f +// 01000000000000001e3ea2ae7196083204d515ab83fd0c6bad822c8ee401000000000000001ef7486d2ad0421421794d8a4c473d3d382d22c842 +// 01000000000000001e6fdb366937d4310a403763aae68ff627350d1b3601000000000000001e6ad5d2dfe8cfbe489c6491f16220ddcf2d60706f +// 01000000000000001e6cbe19d65c58da5538d3ccfba3d8a06c4d45ac3f01000000000000001e93849e3dae9b3cfb372af82d7bdcf337248adab8 +// 01000000000000001e547d56bf932a82cc6a9f8fdc7473fc8867f925d901000000000000001e2f1ae094509e1f3cb8539063330d1b2a6dd6eccb +// 01000000000000001e4a686fe225caa978864d50857d8bd7249f3f6d5301000000000000001e6d0623357e34d8aa5d026abd3a983f11b45c8509 +// 01000000000000001ed177dfdfc304b4995422a41bb0d33cee6454856601000000000000001ec5005a3f733d8964fbfdec22863a40e1983e4055 +// 01000000000000001e8f0c420efed85f8bbde89d51a6c04ccebcebfabc01000000000000001eb12baa7f233b569698f17c299b692eeaa919fa22 +// 01000000000000001ef642d4d5be03840ba70f164aabff4920f63016da01000000000000001e154a88b33732d23e35d971c5246c9546e795c2d7 +// 01000000000000001eed05de0c6ed0f7c255a801345956a662571dc89101000000000000001e165a09dea1d7bf52e0da06d95f6aa3c59ed7605c +// 01000000000000001ed5071f07780660be988d474e5746d9ff2df9e38601000000000000001e793fe0bbbdbbb67f25e9172a8d0864aa42c5c8e5 +// 01000000000000001e86ec350a3d519da4875c33e21801656b2018a0de01000000000000001e1077b75b2e00bf9e1401e3e8ec416add4bc968c3 +// 01000000000000001e7b7176af2cef35079c5cc2c756a2f4da87b323d701000000000000001e9c57d9688f92567233b962a632e3c17981e2e485 +// 01000000000000001e7515b73773b8714d767c6ceabef46a791e32d8e201000000000000001ea284016ffd570bdd33e1e24246453c582df317a0 +// 01000000000000001ebf287e20c8497bf114ea24976e55369446239e4201000000000000001e424a1709b337a97c400ac08c0bb403ad8e0054dc +// 01000000000000001e36803ea2ab3e4fbe420dcb6d127d22a47e8e553501000000000000001ec5c837b703a5476d84b1b289552c501fb53b2da0 +// 01000000000000001ef9d5bd6441fc3f3b09166dd70e776c239f5e9a8a01000000000000001e6085dad946d02e81527ea43d26900c814d73444e +// 01000000000000001ef873fb682b47bc8ac37944e390ce2867ef86bd1201000000000000001e08f37a6587b5c09a7b90aad5586d9e79a3dbdca5 +// 01000000000000001e4b6294e1d5a1a2a448a36ba6f32f19eba279355701000000000000001eae21b5575d04e7c2a192f8a41533e7ea4201e6b9 +// 01000000000000001e6eb03010d0160f82a80e064140d33b386b985ac001000000000000001e0710cb2739dda5521955b96b4309324a1182f606 +// 01000000000000001eb4b42b7ecd576bdfac4886c2a11bfceecc31d83201000000000000001eaf0ba9bd7cb2a23220eac8310f793fd9e846bf69 +// 3045022100a6c327cb6798cb3dc8b282b64358dc4c24a72bff61e347009c1e0ad2a1cc6d8c022028cf62c3d0ea8c3ca6113f63b4e827bd8db820 +// 1a478f5ff40c309bbb9987a9b4 +const std::vector TYPE_6_BYTES = { + 255, 2, 30, 1, 0, 0, 0, 6, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 128, 150, 152, 0, 0, 0, 0, 0, 0, 100, + 0, 1, 0, 0, 0, 0, 0, 0, 0, 30, 133, 175, 101, 173, 106, + 163, 161, 223, 91, 201, 166, 245, 155, 159, 61, 57, 79, 100, 58, 35, + 1, 0, 0, 0, 0, 0, 0, 0, 30, 75, 29, 161, 170, 169, 188, + 174, 197, 170, 246, 118, 4, 120, 1, 7, 12, 76, 18, 71, 193, 1, + 0, 0, 0, 0, 0, 0, 0, 30, 114, 108, 208, 211, 224, 41, 32, + 188, 193, 79, 109, 87, 165, 247, 47, 100, 210, 112, 206, 227, 1, 0, + 0, 0, 0, 0, 0, 0, 30, 231, 211, 119, 107, 126, 111, 22, 25, + 170, 218, 56, 165, 252, 113, 167, 183, 243, 185, 186, 200, 1, 0, 0, + 0, 0, 0, 0, 0, 30, 214, 162, 243, 198, 57, 152, 44, 0, 89, + 206, 95, 226, 30, 94, 116, 33, 45, 106, 140, 63, 1, 0, 0, 0, + 0, 0, 0, 0, 30, 178, 190, 143, 12, 23, 91, 172, 102, 34, 216, + 126, 212, 216, 73, 93, 113, 191, 188, 73, 243, 1, 0, 0, 0, 0, + 0, 0, 0, 30, 23, 120, 154, 45, 2, 165, 13, 103, 200, 74, 101, + 226, 221, 246, 68, 137, 57, 210, 11, 124, 1, 0, 0, 0, 0, 0, + 0, 0, 30, 217, 126, 145, 87, 206, 191, 199, 71, 134, 227, 49, 91, + 164, 170, 123, 164, 10, 223, 107, 80, 1, 0, 0, 0, 0, 0, 0, + 0, 30, 22, 244, 209, 133, 76, 10, 63, 34, 251, 44, 242, 46, 28, + 139, 137, 46, 108, 158, 83, 14, 1, 0, 0, 0, 0, 0, 0, 0, + 30, 100, 102, 216, 129, 91, 2, 33, 119, 141, 36, 36, 214, 55, 142, + 101, 144, 24, 178, 175, 62, 1, 0, 0, 0, 0, 0, 0, 0, 30, + 71, 176, 167, 122, 30, 237, 132, 44, 77, 4, 198, 158, 159, 1, 85, + 117, 217, 249, 18, 240, 1, 0, 0, 0, 0, 0, 0, 0, 30, 201, + 178, 152, 176, 108, 49, 150, 43, 120, 209, 230, 167, 134, 90, 118, 79, + 116, 12, 201, 203, 1, 0, 0, 0, 0, 0, 0, 0, 30, 12, 206, + 206, 89, 94, 52, 133, 52, 51, 175, 79, 84, 65, 176, 10, 102, 42, + 130, 92, 143, 1, 0, 0, 0, 0, 0, 0, 0, 30, 51, 235, 165, + 134, 18, 151, 41, 161, 25, 3, 197, 38, 18, 186, 236, 61, 112, 153, + 20, 102, 1, 0, 0, 0, 0, 0, 0, 0, 30, 150, 43, 146, 69, + 240, 253, 113, 195, 27, 186, 229, 160, 18, 84, 21, 50, 164, 143, 124, + 213, 1, 0, 0, 0, 0, 0, 0, 0, 30, 183, 130, 84, 1, 28, + 210, 31, 145, 14, 46, 106, 149, 82, 4, 173, 101, 196, 51, 145, 239, + 1, 0, 0, 0, 0, 0, 0, 0, 30, 140, 99, 12, 141, 207, 96, + 172, 154, 43, 180, 124, 255, 142, 168, 237, 32, 3, 110, 242, 98, 1, + 0, 0, 0, 0, 0, 0, 0, 30, 63, 152, 191, 61, 249, 243, 57, + 20, 226, 104, 216, 172, 27, 64, 1, 158, 156, 12, 157, 79, 1, 0, + 0, 0, 0, 0, 0, 0, 30, 207, 254, 113, 171, 92, 147, 223, 151, + 182, 23, 229, 233, 183, 32, 243, 77, 248, 203, 237, 250, 1, 0, 0, + 0, 0, 0, 0, 0, 30, 118, 37, 90, 227, 93, 1, 183, 104, 154, + 47, 242, 133, 136, 226, 203, 30, 4, 24, 149, 44, 1, 0, 0, 0, + 0, 0, 0, 0, 30, 18, 180, 66, 51, 5, 159, 180, 245, 6, 170, + 152, 183, 67, 205, 20, 143, 52, 181, 55, 69, 1, 0, 0, 0, 0, + 0, 0, 0, 30, 190, 95, 128, 147, 147, 126, 96, 120, 159, 202, 205, + 126, 240, 39, 81, 150, 37, 192, 50, 130, 1, 0, 0, 0, 0, 0, + 0, 0, 30, 33, 233, 0, 223, 219, 90, 96, 149, 182, 65, 249, 167, + 246, 174, 29, 211, 148, 182, 55, 136, 1, 0, 0, 0, 0, 0, 0, + 0, 30, 67, 179, 52, 30, 202, 29, 223, 100, 235, 198, 150, 194, 110, + 224, 41, 254, 176, 131, 169, 133, 1, 0, 0, 0, 0, 0, 0, 0, + 30, 26, 63, 135, 9, 113, 207, 249, 210, 160, 21, 178, 19, 138, 219, + 140, 11, 144, 19, 199, 59, 1, 0, 0, 0, 0, 0, 0, 0, 30, + 49, 204, 150, 158, 37, 212, 242, 14, 113, 252, 38, 62, 68, 112, 52, + 93, 106, 140, 96, 93, 1, 0, 0, 0, 0, 0, 0, 0, 30, 159, + 186, 205, 66, 71, 144, 49, 24, 130, 159, 180, 111, 5, 211, 254, 236, + 13, 167, 123, 197, 1, 0, 0, 0, 0, 0, 0, 0, 30, 176, 23, + 135, 120, 184, 152, 197, 218, 121, 226, 35, 13, 249, 73, 68, 67, 188, + 53, 6, 48, 1, 0, 0, 0, 0, 0, 0, 0, 30, 165, 61, 181, + 190, 90, 159, 42, 12, 134, 30, 131, 118, 66, 36, 122, 150, 219, 174, + 174, 97, 1, 0, 0, 0, 0, 0, 0, 0, 30, 8, 57, 161, 160, + 168, 167, 29, 120, 192, 72, 55, 9, 92, 190, 33, 29, 95, 174, 216, + 208, 1, 0, 0, 0, 0, 0, 0, 0, 30, 92, 169, 15, 210, 189, + 206, 81, 231, 173, 21, 116, 120, 54, 8, 221, 26, 165, 171, 204, 233, + 1, 0, 0, 0, 0, 0, 0, 0, 30, 1, 173, 251, 142, 224, 66, + 20, 124, 152, 42, 194, 99, 190, 92, 199, 197, 95, 100, 242, 172, 1, + 0, 0, 0, 0, 0, 0, 0, 30, 44, 174, 130, 204, 87, 88, 67, + 13, 223, 94, 227, 119, 118, 7, 142, 217, 34, 124, 24, 103, 1, 0, + 0, 0, 0, 0, 0, 0, 30, 135, 238, 195, 23, 33, 60, 82, 143, + 182, 220, 99, 49, 203, 104, 132, 204, 215, 172, 144, 15, 1, 0, 0, + 0, 0, 0, 0, 0, 30, 142, 234, 105, 114, 207, 188, 163, 184, 196, + 191, 250, 1, 101, 176, 60, 88, 254, 18, 73, 64, 1, 0, 0, 0, + 0, 0, 0, 0, 30, 21, 43, 69, 224, 157, 237, 195, 102, 199, 89, + 117, 37, 42, 113, 21, 129, 2, 171, 127, 160, 1, 0, 0, 0, 0, + 0, 0, 0, 30, 15, 59, 155, 145, 139, 217, 218, 95, 72, 206, 241, + 59, 219, 103, 3, 11, 180, 178, 178, 224, 1, 0, 0, 0, 0, 0, + 0, 0, 30, 52, 250, 28, 217, 130, 224, 156, 76, 134, 136, 210, 34, + 0, 138, 157, 234, 220, 200, 249, 123, 1, 0, 0, 0, 0, 0, 0, + 0, 30, 151, 2, 47, 84, 122, 7, 195, 36, 82, 79, 195, 48, 244, + 18, 159, 13, 3, 33, 81, 10, 1, 0, 0, 0, 0, 0, 0, 0, + 30, 223, 13, 97, 165, 230, 250, 75, 203, 228, 192, 140, 130, 64, 50, + 202, 37, 189, 54, 50, 249, 1, 0, 0, 0, 0, 0, 0, 0, 30, + 188, 190, 160, 152, 199, 21, 2, 188, 161, 30, 60, 253, 120, 133, 187, + 18, 171, 220, 44, 102, 1, 0, 0, 0, 0, 0, 0, 0, 30, 134, + 0, 4, 67, 122, 253, 152, 206, 114, 135, 166, 66, 164, 178, 113, 109, + 150, 234, 76, 76, 1, 0, 0, 0, 0, 0, 0, 0, 30, 116, 156, + 74, 154, 77, 165, 84, 11, 226, 139, 197, 208, 167, 54, 252, 104, 110, + 221, 102, 200, 1, 0, 0, 0, 0, 0, 0, 0, 30, 182, 150, 195, + 119, 117, 216, 85, 197, 241, 245, 56, 60, 32, 127, 95, 188, 102, 169, + 130, 84, 1, 0, 0, 0, 0, 0, 0, 0, 30, 68, 54, 205, 121, + 183, 216, 162, 151, 173, 151, 200, 39, 143, 162, 92, 251, 200, 86, 65, + 22, 1, 0, 0, 0, 0, 0, 0, 0, 30, 253, 10, 79, 8, 111, + 98, 1, 191, 191, 73, 193, 166, 8, 133, 90, 181, 87, 43, 190, 155, + 1, 0, 0, 0, 0, 0, 0, 0, 30, 113, 86, 122, 18, 239, 82, + 121, 237, 246, 234, 144, 10, 185, 95, 158, 230, 175, 253, 132, 202, 1, + 0, 0, 0, 0, 0, 0, 0, 30, 117, 216, 129, 248, 7, 229, 20, + 45, 161, 23, 162, 50, 92, 117, 178, 8, 181, 78, 200, 14, 1, 0, + 0, 0, 0, 0, 0, 0, 30, 35, 27, 3, 9, 162, 151, 33, 87, + 245, 203, 193, 60, 52, 86, 43, 62, 180, 246, 123, 198, 1, 0, 0, + 0, 0, 0, 0, 0, 30, 58, 34, 47, 14, 245, 212, 210, 123, 45, + 106, 111, 59, 16, 46, 229, 193, 255, 62, 117, 244, 1, 0, 0, 0, + 0, 0, 0, 0, 30, 6, 75, 30, 13, 109, 154, 231, 175, 62, 3, + 194, 202, 85, 250, 219, 214, 39, 103, 20, 198, 1, 0, 0, 0, 0, + 0, 0, 0, 30, 205, 77, 74, 169, 129, 247, 96, 169, 202, 216, 23, + 51, 200, 149, 17, 100, 201, 8, 150, 19, 1, 0, 0, 0, 0, 0, + 0, 0, 30, 51, 39, 100, 154, 214, 158, 24, 219, 172, 167, 47, 97, + 83, 179, 10, 23, 172, 84, 138, 244, 1, 0, 0, 0, 0, 0, 0, + 0, 30, 255, 196, 218, 200, 95, 255, 0, 31, 43, 74, 84, 216, 147, + 57, 104, 110, 22, 252, 86, 90, 1, 0, 0, 0, 0, 0, 0, 0, + 30, 245, 211, 247, 78, 58, 178, 179, 36, 70, 154, 226, 188, 80, 64, + 100, 139, 166, 253, 174, 130, 1, 0, 0, 0, 0, 0, 0, 0, 30, + 97, 57, 202, 12, 150, 195, 225, 213, 38, 80, 233, 136, 72, 117, 217, + 81, 85, 150, 62, 14, 1, 0, 0, 0, 0, 0, 0, 0, 30, 22, + 104, 20, 252, 117, 239, 46, 99, 23, 17, 95, 92, 238, 85, 99, 105, + 5, 132, 193, 13, 1, 0, 0, 0, 0, 0, 0, 0, 30, 252, 197, + 8, 64, 102, 202, 69, 48, 195, 44, 87, 102, 72, 167, 157, 127, 123, + 74, 188, 105, 1, 0, 0, 0, 0, 0, 0, 0, 30, 203, 18, 112, + 189, 94, 4, 78, 225, 89, 106, 69, 160, 22, 155, 7, 130, 156, 111, + 177, 58, 1, 0, 0, 0, 0, 0, 0, 0, 30, 127, 138, 27, 152, + 117, 179, 123, 0, 249, 141, 116, 255, 116, 106, 223, 76, 25, 176, 225, + 47, 1, 0, 0, 0, 0, 0, 0, 0, 30, 62, 162, 174, 113, 150, + 8, 50, 4, 213, 21, 171, 131, 253, 12, 107, 173, 130, 44, 142, 228, + 1, 0, 0, 0, 0, 0, 0, 0, 30, 247, 72, 109, 42, 208, 66, + 20, 33, 121, 77, 138, 76, 71, 61, 61, 56, 45, 34, 200, 66, 1, + 0, 0, 0, 0, 0, 0, 0, 30, 111, 219, 54, 105, 55, 212, 49, + 10, 64, 55, 99, 170, 230, 143, 246, 39, 53, 13, 27, 54, 1, 0, + 0, 0, 0, 0, 0, 0, 30, 106, 213, 210, 223, 232, 207, 190, 72, + 156, 100, 145, 241, 98, 32, 221, 207, 45, 96, 112, 111, 1, 0, 0, + 0, 0, 0, 0, 0, 30, 108, 190, 25, 214, 92, 88, 218, 85, 56, + 211, 204, 251, 163, 216, 160, 108, 77, 69, 172, 63, 1, 0, 0, 0, + 0, 0, 0, 0, 30, 147, 132, 158, 61, 174, 155, 60, 251, 55, 42, + 248, 45, 123, 220, 243, 55, 36, 138, 218, 184, 1, 0, 0, 0, 0, + 0, 0, 0, 30, 84, 125, 86, 191, 147, 42, 130, 204, 106, 159, 143, + 220, 116, 115, 252, 136, 103, 249, 37, 217, 1, 0, 0, 0, 0, 0, + 0, 0, 30, 47, 26, 224, 148, 80, 158, 31, 60, 184, 83, 144, 99, + 51, 13, 27, 42, 109, 214, 236, 203, 1, 0, 0, 0, 0, 0, 0, + 0, 30, 74, 104, 111, 226, 37, 202, 169, 120, 134, 77, 80, 133, 125, + 139, 215, 36, 159, 63, 109, 83, 1, 0, 0, 0, 0, 0, 0, 0, + 30, 109, 6, 35, 53, 126, 52, 216, 170, 93, 2, 106, 189, 58, 152, + 63, 17, 180, 92, 133, 9, 1, 0, 0, 0, 0, 0, 0, 0, 30, + 209, 119, 223, 223, 195, 4, 180, 153, 84, 34, 164, 27, 176, 211, 60, + 238, 100, 84, 133, 102, 1, 0, 0, 0, 0, 0, 0, 0, 30, 197, + 0, 90, 63, 115, 61, 137, 100, 251, 253, 236, 34, 134, 58, 64, 225, + 152, 62, 64, 85, 1, 0, 0, 0, 0, 0, 0, 0, 30, 143, 12, + 66, 14, 254, 216, 95, 139, 189, 232, 157, 81, 166, 192, 76, 206, 188, + 235, 250, 188, 1, 0, 0, 0, 0, 0, 0, 0, 30, 177, 43, 170, + 127, 35, 59, 86, 150, 152, 241, 124, 41, 155, 105, 46, 234, 169, 25, + 250, 34, 1, 0, 0, 0, 0, 0, 0, 0, 30, 246, 66, 212, 213, + 190, 3, 132, 11, 167, 15, 22, 74, 171, 255, 73, 32, 246, 48, 22, + 218, 1, 0, 0, 0, 0, 0, 0, 0, 30, 21, 74, 136, 179, 55, + 50, 210, 62, 53, 217, 113, 197, 36, 108, 149, 70, 231, 149, 194, 215, + 1, 0, 0, 0, 0, 0, 0, 0, 30, 237, 5, 222, 12, 110, 208, + 247, 194, 85, 168, 1, 52, 89, 86, 166, 98, 87, 29, 200, 145, 1, + 0, 0, 0, 0, 0, 0, 0, 30, 22, 90, 9, 222, 161, 215, 191, + 82, 224, 218, 6, 217, 95, 106, 163, 197, 158, 215, 96, 92, 1, 0, + 0, 0, 0, 0, 0, 0, 30, 213, 7, 31, 7, 120, 6, 96, 190, + 152, 141, 71, 78, 87, 70, 217, 255, 45, 249, 227, 134, 1, 0, 0, + 0, 0, 0, 0, 0, 30, 121, 63, 224, 187, 189, 187, 182, 127, 37, + 233, 23, 42, 141, 8, 100, 170, 66, 197, 200, 229, 1, 0, 0, 0, + 0, 0, 0, 0, 30, 134, 236, 53, 10, 61, 81, 157, 164, 135, 92, + 51, 226, 24, 1, 101, 107, 32, 24, 160, 222, 1, 0, 0, 0, 0, + 0, 0, 0, 30, 16, 119, 183, 91, 46, 0, 191, 158, 20, 1, 227, + 232, 236, 65, 106, 221, 75, 201, 104, 195, 1, 0, 0, 0, 0, 0, + 0, 0, 30, 123, 113, 118, 175, 44, 239, 53, 7, 156, 92, 194, 199, + 86, 162, 244, 218, 135, 179, 35, 215, 1, 0, 0, 0, 0, 0, 0, + 0, 30, 156, 87, 217, 104, 143, 146, 86, 114, 51, 185, 98, 166, 50, + 227, 193, 121, 129, 226, 228, 133, 1, 0, 0, 0, 0, 0, 0, 0, + 30, 117, 21, 183, 55, 115, 184, 113, 77, 118, 124, 108, 234, 190, 244, + 106, 121, 30, 50, 216, 226, 1, 0, 0, 0, 0, 0, 0, 0, 30, + 162, 132, 1, 111, 253, 87, 11, 221, 51, 225, 226, 66, 70, 69, 60, + 88, 45, 243, 23, 160, 1, 0, 0, 0, 0, 0, 0, 0, 30, 191, + 40, 126, 32, 200, 73, 123, 241, 20, 234, 36, 151, 110, 85, 54, 148, + 70, 35, 158, 66, 1, 0, 0, 0, 0, 0, 0, 0, 30, 66, 74, + 23, 9, 179, 55, 169, 124, 64, 10, 192, 140, 11, 180, 3, 173, 142, + 0, 84, 220, 1, 0, 0, 0, 0, 0, 0, 0, 30, 54, 128, 62, + 162, 171, 62, 79, 190, 66, 13, 203, 109, 18, 125, 34, 164, 126, 142, + 85, 53, 1, 0, 0, 0, 0, 0, 0, 0, 30, 197, 200, 55, 183, + 3, 165, 71, 109, 132, 177, 178, 137, 85, 44, 80, 31, 181, 59, 45, + 160, 1, 0, 0, 0, 0, 0, 0, 0, 30, 249, 213, 189, 100, 65, + 252, 63, 59, 9, 22, 109, 215, 14, 119, 108, 35, 159, 94, 154, 138, + 1, 0, 0, 0, 0, 0, 0, 0, 30, 96, 133, 218, 217, 70, 208, + 46, 129, 82, 126, 164, 61, 38, 144, 12, 129, 77, 115, 68, 78, 1, + 0, 0, 0, 0, 0, 0, 0, 30, 248, 115, 251, 104, 43, 71, 188, + 138, 195, 121, 68, 227, 144, 206, 40, 103, 239, 134, 189, 18, 1, 0, + 0, 0, 0, 0, 0, 0, 30, 8, 243, 122, 101, 135, 181, 192, 154, + 123, 144, 170, 213, 88, 109, 158, 121, 163, 219, 220, 165, 1, 0, 0, + 0, 0, 0, 0, 0, 30, 75, 98, 148, 225, 213, 161, 162, 164, 72, + 163, 107, 166, 243, 47, 25, 235, 162, 121, 53, 87, 1, 0, 0, 0, + 0, 0, 0, 0, 30, 174, 33, 181, 87, 93, 4, 231, 194, 161, 146, + 248, 164, 21, 51, 231, 234, 66, 1, 230, 185, 1, 0, 0, 0, 0, + 0, 0, 0, 30, 110, 176, 48, 16, 208, 22, 15, 130, 168, 14, 6, + 65, 64, 211, 59, 56, 107, 152, 90, 192, 1, 0, 0, 0, 0, 0, + 0, 0, 30, 7, 16, 203, 39, 57, 221, 165, 82, 25, 85, 185, 107, + 67, 9, 50, 74, 17, 130, 246, 6, 1, 0, 0, 0, 0, 0, 0, + 0, 30, 180, 180, 43, 126, 205, 87, 107, 223, 172, 72, 134, 194, 161, + 27, 252, 238, 204, 49, 216, 50, 1, 0, 0, 0, 0, 0, 0, 0, + 30, 175, 11, 169, 189, 124, 178, 162, 50, 32, 234, 200, 49, 15, 121, + 63, 217, 232, 70, 191, 105, 48, 69, 2, 33, 0, 166, 195, 39, 203, + 103, 152, 203, 61, 200, 178, 130, 182, 67, 88, 220, 76, 36, 167, 43, + 255, 97, 227, 71, 0, 156, 30, 10, 210, 161, 204, 109, 140, 2, 32, + 40, 207, 98, 195, 208, 234, 140, 60, 166, 17, 63, 99, 180, 232, 39, + 189, 141, 184, 32, 26, 71, 143, 95, 244, 12, 48, 155, 187, 153, 135, + 169, 180 }; + +#endif diff --git a/test/transactions/types/fixtures/second_signature.hpp b/test/transactions/types/fixtures/second_signature.hpp new file mode 100644 index 00000000..6f138182 --- /dev/null +++ b/test/transactions/types/fixtures/second_signature.hpp @@ -0,0 +1,78 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_SECOND_SIGNATURE_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_SECOND_SIGNATURE_HPP + +#include +#include + +#include "common.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 2, +// "network": 23, +// "typeGroup": 1, +// "type": 1, +// "nonce": "1", +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "500000000", +// "asset": { +// "signature": { +// "publicKey": "02877e4f35c76abaeb152b128670db0a7ae10b3999afcd28a42938b653fbf87ae9" +// } +// }, +// "signature": "304402201786b482215f6f63e3a9414324283decffc64b829209640024a4e78d7ed557f50220197e53c8e8b7f8a645eb82712587926ad930695a8ca43a2308ca0a9dbeabb65b", +// "id": "6934f7a73846b2daeb94e70b3b8a23118410cb552ada5993bf1b47b10e168916" +// }, +// "serialized": "ff02170100000001000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed1920065cd1d000000000002877e4f35c76abaeb152b128670db0a7ae10b3999afcd28a42938b653fbf87ae9304402201786b482215f6f63e3a9414324283decffc64b829209640024a4e78d7ed557f50220197e53c8e8b7f8a645eb82712587926ad930695a8ca43a2308ca0a9dbeabb65b" +// } + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_1_TYPE = 1U; +constexpr uint64_t TYPE_1_FEE = 500000000ULL; +constexpr uint64_t TYPE_1_VF_LENGTH = 0U; + +//////////////////////////////////////////////////////////////////////////////// +constexpr auto TYPE_1_PUBLICKEY_STRING = "02877e4f35c76abaeb152b128670db0a7ae10b3999afcd28a42938b653fbf87ae9"; + +//////////////////////////////////////////////////////////////////////////////// +// ff02170100000001000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed1920065cd1d000000000002877e4f35c76abaeb152b128670db0a7ae10b3999afcd28a42938b653fbf87ae9304402201786b482215f6f63e3a9414324283decffc64b829209640024a4e78d7ed557f50220197e53c8e8b7f8a645eb82712587926ad930695a8ca43a2308ca0a9dbeabb65b +const std::vector TYPE_1_BYTES = { + 255, 2, 23, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 0, 101, 205, 29, 0, 0, 0, 0, 0, 2, + 135, 126, 79, 53, 199, 106, 186, 235, 21, 43, 18, 134, 112, 219, 10, + 122, 225, 11, 57, 153, 175, 205, 40, 164, 41, 56, 182, 83, 251, 248, + 122, 233, 48, 68, 2, 32, 23, 134, 180, 130, 33, 95, 111, 99, 227, + 169, 65, 67, 36, 40, 61, 236, 255, 198, 75, 130, 146, 9, 100, 0, + 36, 164, 231, 141, 126, 213, 87, 245, 2, 32, 25, 126, 83, 200, 232, + 183, 248, 166, 69, 235, 130, 113, 37, 135, 146, 106, 217, 48, 105, 90, + 140, 164, 58, 35, 8, 202, 10, 157, 190, 171, 182, 91 }; + +//////////////////////////////////////////////////////////////////////////////// +// 02877e4f35c76abaeb152b128670db0a7ae10b3999afcd28a42938b653fbf87ae9 +constexpr uint8_t TYPE_1_SECOND_PUBLICKEY[] = { + 2, 135, 126, 79, 53, 199, 106, 186, 235, 21, 43, + 18, 134, 112, 219, 10, 122, 225, 11, 57, 153, 175, + 205, 40, 164, 41, 56, 182, 83, 251, 248, 122, 233 }; + +//////////////////////////////////////////////////////////////////////////////// +// 304402201786b482215f6f63e3a9414324283decffc64b829209640024a4e78d7ed557f50220197e53c8e8b7f8a645eb82712587926ad930695a8ca43a2308ca0a9dbeabb65b +constexpr uint8_t TYPE_1_SIGNATURE[] = { + 48, 68, 2, 32, 23, 134, 180, 130, 33, 95, 111, 99, 227, 169, + 65, 67, 36, 40, 61, 236, 255, 198, 75, 130, 146, 9, 100, 0, + 36, 164, 231, 141, 126, 213, 87, 245, 2, 32, 25, 126, 83, 200, + 232, 183, 248, 166, 69, 235, 130, 113, 37, 135, 146, 106, 217, 48, + 105, 90, 140, 164, 58, 35, 8, 202, 10, 157, 190, 171, 182, 91 }; + +#endif diff --git a/test/transactions/types/fixtures/second_signature_v1.hpp b/test/transactions/types/fixtures/second_signature_v1.hpp new file mode 100644 index 00000000..3345f007 --- /dev/null +++ b/test/transactions/types/fixtures/second_signature_v1.hpp @@ -0,0 +1,58 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_SECOND_SIGNATURE_V1_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_SECOND_SIGNATURE_V1_HPP + +#include +#include + +#include "common.hpp" + +namespace v1 { + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_1_TYPE = 1U; +constexpr uint32_t TYPE_1_TIMESTAMP = 41271867UL; +constexpr uint64_t TYPE_1_FEE = 500000000ULL; + +//////////////////////////////////////////////////////////////////////////////// +// ff011e013bc27502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed1920065cd1d000000000003699e966b2525f9088a6941d8d94f7869964a000efe65783d78ac82e1199fe609304402202aab49477dd3531e4473196d08fbd7c00ebb79223d5eaaeaf02c52c4041a86cf02201a7d82655f9b1d22af3ea94e6f183649bb4610cdeca3b9e20d6c8773f869831c +const std::vector TYPE_1_BYTES = { + 255, 1, 30, 1, 59, 194, 117, 2, 3, 65, 81, 163, 236, 70, + 181, 103, 10, 104, 43, 10, 99, 57, 79, 134, 53, 135, 209, 188, + 151, 72, 59, 27, 108, 112, 235, 88, 231, 240, 174, 209, 146, 0, + 101, 205, 29, 0, 0, 0, 0, 0, 3, 105, 158, 150, 107, 37, + 37, 249, 8, 138, 105, 65, 216, 217, 79, 120, 105, 150, 74, 0, + 14, 254, 101, 120, 61, 120, 172, 130, 225, 25, 159, 230, 9, 48, + 68, 2, 32, 42, 171, 73, 71, 125, 211, 83, 30, 68, 115, 25, + 109, 8, 251, 215, 192, 14, 187, 121, 34, 61, 94, 170, 234, 240, + 44, 82, 196, 4, 26, 134, 207, 2, 32, 26, 125, 130, 101, 95, + 155, 29, 34, 175, 62, 169, 78, 111, 24, 54, 73, 187, 70, 16, + 205, 236, 163, 185, 226, 13, 108, 135, 115, 248, 105, 131, 28 }; + +//////////////////////////////////////////////////////////////////////////////// +// 03699e966b2525f9088a6941d8d94f7869964a000efe65783d78ac82e1199fe609 +constexpr uint8_t TYPE_1_SECOND_PUBLICKEY[] = { + 3, 105, 158, 150, 107, 37, 37, 249, 8, 138, 105, + 65, 216, 217, 79, 120, 105, 150, 74, 0, 14, 254, + 101, 120, 61, 120, 172, 130, 225, 25, 159, 230, 9 }; + +//////////////////////////////////////////////////////////////////////////////// +// 304402202aab49477dd3531e4473196d08fbd7c00ebb79223d5eaaeaf02c52c4041a86cf02201a7d82655f9b1d22af3ea94e6f183649bb4610cdeca3b9e20d6c8773f869831c +constexpr uint8_t TYPE_1_SIGNATURE[] = { + 48, 68, 2, 32, 42, 171, 73, 71, 125, 211, 83, 30, 68, 115, + 25, 109, 8, 251, 215, 192, 14, 187, 121, 34, 61, 94, 170, 234, + 240, 44, 82, 196, 4, 26, 134, 207, 2, 32, 26, 125, 130, 101, + 95, 155, 29, 34, 175, 62, 169, 78, 111, 24, 54, 73, 187, 70, + 16, 205, 236, 163, 185, 226, 13, 108, 135, 115, 248, 105, 131, 28 }; + +} // namespace v1 + +#endif diff --git a/test/transactions/types/fixtures/transfer.hpp b/test/transactions/types/fixtures/transfer.hpp new file mode 100644 index 00000000..82225f52 --- /dev/null +++ b/test/transactions/types/fixtures/transfer.hpp @@ -0,0 +1,142 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_TRANSFER_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_TRANSFER_HPP + +#include +#include + +#include "common.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 2, +// "network": 23, +// "typeGroup": 1, +// "type": 0, +// "nonce": "1", +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "10000000", +// "amount": "1", +// "expiration": 0, +// "recipientId": "AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff", +// "signature": "3045022100d55051f5aec7d894afb987d582e63e0157b7a6e0cb8eff1d70ac81cbbb1382e90220294b89acb106e811b48a072a555915bea4aa0fc63c4210ad90115d1220b31b7d", +// "id": "aafbacad9b75fa664c9cecae6de552833d4c2ab6f857573cf492eafcebfd1551" +// }, +// "serialized": "ff02170100000000000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192809698000000000000010000000000000000000000171dfc69b54c7fe901e91d5a9ab78388645e2427ea3045022100d55051f5aec7d894afb987d582e63e0157b7a6e0cb8eff1d70ac81cbbb1382e90220294b89acb106e811b48a072a555915bea4aa0fc63c4210ad90115d1220b31b7d" +// } + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 2, +// "network": 23, +// "typeGroup": 1, +// "type": 0, +// "nonce": "1", +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "10000000", +// "amount": "1", +// "vendorFieldHex": "48656c6c6f20576f726c64", +// "vendorField": "Hello World", +// "expiration": 0, +// "recipientId": "AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff", +// "signature": "3045022100a4d816cee480025ff9ae75fd31a8a6abb749a647c50220516e37cd8f79efa1c90220651060f6521b3d0a66b07dc2d0a943f96c9c3c2c6ad42c4396efbe42e6a6823b", +// "id": "e35888115fccca7b850cd6e47bc67c55d4ad70245953825210ca90c3ae361153", +// }, +// "serialized": "ff02170100000000000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19280969800000000000b48656c6c6f20576f726c64010000000000000000000000171dfc69b54c7fe901e91d5a9ab78388645e2427ea3045022100a4d816cee480025ff9ae75fd31a8a6abb749a647c50220516e37cd8f79efa1c90220651060f6521b3d0a66b07dc2d0a943f96c9c3c2c6ad42c4396efbe42e6a6823b" +// } + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_0_TYPE = 0U; +constexpr uint64_t TYPE_0_FEE = 10000000ULL; +constexpr uint64_t TYPE_0_NVF_LENGTH = 0U; +constexpr uint8_t TYPE_0_VF_LENGTH = 11U; +constexpr uint64_t TYPE_0_AMOUNT = 1ULL; +constexpr uint32_t TYPE_0_EXPIRATION = 0UL; + +//////////////////////////////////////////////////////////////////////////////// +constexpr auto TYPE_0_TYPE_STRING = "0"; +constexpr auto TYPE_0_FEE_STRING = "10000000"; +constexpr auto TYPE_0_AMOUNT_STRING = "1"; +constexpr auto TYPE_0_EXPIRATION_STRING = "0"; +constexpr auto TYPE_0_RECIPIENT_ID_STRING = "AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff"; +constexpr auto TYPE_0_SENDER_SIGNATURE_STRING = "3045022100d55051f5aec7d894afb987d582e63e0157b7a6e0cb8eff1d70ac81cbbb1382e90220294b89acb106e811b48a072a555915bea4aa0fc63c4210ad90115d1220b31b7d"; +constexpr auto TYPE_0_ID_STRING = "aafbacad9b75fa664c9cecae6de552833d4c2ab6f857573cf492eafcebfd1551"; + +//////////////////////////////////////////////////////////////////////////////// +// ff02170100000000000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192809698000000000000010000000000000000000000171dfc69b54c7fe901e91d5a9ab78388645e2427ea3045022100d55051f5aec7d894afb987d582e63e0157b7a6e0cb8eff1d70ac81cbbb1382e90220294b89acb106e811b48a072a555915bea4aa0fc63c4210ad90115d1220b31b7d +const std::vector TYPE_0_BYTES = { + 255, 2, 23, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 128, 150, 152, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 29, 252, 105, + 181, 76, 127, 233, 1, 233, 29, 90, 154, 183, 131, 136, 100, 94, 36, + 39, 234, 48, 69, 2, 33, 0, 213, 80, 81, 245, 174, 199, 216, 148, + 175, 185, 135, 213, 130, 230, 62, 1, 87, 183, 166, 224, 203, 142, 255, + 29, 112, 172, 129, 203, 187, 19, 130, 233, 2, 32, 41, 75, 137, 172, + 177, 6, 232, 17, 180, 138, 7, 42, 85, 89, 21, 190, 164, 170, 15, + 198, 60, 66, 16, 173, 144, 17, 93, 18, 32, 179, 27, 125 }; + +//////////////////////////////////////////////////////////////////////////////// +// ff02170100000000000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19280969800000000000b48656c6c6f20576f726c64010000000000000000000000171dfc69b54c7fe901e91d5a9ab78388645e2427ea3045022100a4d816cee480025ff9ae75fd31a8a6abb749a647c50220516e37cd8f79efa1c90220651060f6521b3d0a66b07dc2d0a943f96c9c3c2c6ad42c4396efbe42e6a6823b +const std::vector TYPE_0_VF_BYTES = { + 255, 2, 23, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 128, 150, 152, 0, 0, 0, 0, 0, 11, 72, + 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 23, 29, 252, 105, 181, 76, 127, 233, + 1, 233, 29, 90, 154, 183, 131, 136, 100, 94, 36, 39, 234, 48, 69, + 2, 33, 0, 164, 216, 22, 206, 228, 128, 2, 95, 249, 174, 117, 253, + 49, 168, 166, 171, 183, 73, 166, 71, 197, 2, 32, 81, 110, 55, 205, + 143, 121, 239, 161, 201, 2, 32, 101, 16, 96, 246, 82, 27, 61, 10, + 102, 176, 125, 194, 208, 169, 67, 249, 108, 156, 60, 44, 106, 212, 44, + 67, 150, 239, 190, 66, 230, 166, 130, 59 }; + +//////////////////////////////////////////////////////////////////////////////// +// base58Decode(AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff) +constexpr uint8_t TYPE_0_RECIPIENT[] = { + 23, 29, 252, 105, 181, 76, 127, 233, 1, 233, 29, + 90, 154, 183, 131, 136, 100, 94, 36, 39, 234 }; + +//////////////////////////////////////////////////////////////////////////////// +// "Hello World" +constexpr uint8_t TYPE_0_VF[] = { + 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 }; + +//////////////////////////////////////////////////////////////////////////////// +// 3045022100d55051f5aec7d894afb987d582e63e0157b7a6e0cb8eff1d70ac81cbbb1382e90220294b89acb106e811b48a072a555915bea4aa0fc63c4210ad90115d1220b31b7d +constexpr uint8_t TYPE_0_SIGNATURE[] = { + 48, 69, 2, 33, 0, 213, 80, 81, 245, 174, 199, 216, 148, 175, 185, + 135, 213, 130, 230, 62, 1, 87, 183, 166, 224, 203, 142, 255, 29, 112, + 172, 129, 203, 187, 19, 130, 233, 2, 32, 41, 75, 137, 172, 177, 6, + 232, 17, 180, 138, 7, 42, 85, 89, 21, 190, 164, 170, 15, 198, 60, + 66, 16, 173, 144, 17, 93, 18, 32, 179, 27, 125 }; + +//////////////////////////////////////////////////////////////////////////////// +// 3045022100a4d816cee480025ff9ae75fd31a8a6abb749a647c50220516e37cd8f79efa1c90220651060f6521b3d0a66b07dc2d0a943f96c9c3c2c6ad42c4396efbe42e6a6823b +constexpr uint8_t TYPE_0_VF_SIGNATURE[] = { + 48, 69, 2, 33, 0, 164, 216, 22, 206, 228, 128, 2, 95, 249, 174, + 117, 253, 49, 168, 166, 171, 183, 73, 166, 71, 197, 2, 32, 81, 110, + 55, 205, 143, 121, 239, 161, 201, 2, 32, 101, 16, 96, 246, 82, 27, + 61, 10, 102, 176, 125, 194, 208, 169, 67, 249, 108, 156, 60, 44, 106, + 212, 44, 67, 150, 239, 190, 66, 230, 166, 130, 59 }; + +//////////////////////////////////////////////////////////////////////////////// +// aafbacad9b75fa664c9cecae6de552833d4c2ab6f857573cf492eafcebfd1551 +constexpr uint8_t TYPE_0_TX_ID[] = { + 170, 251, 172, 173, 155, 117, 250, 102, 76, 156, 236, + 174, 109, 229, 82, 131, 61, 76, 42, 182, 248, 87, + 87, 60, 244, 146, 234, 252, 235, 253, 21, 81 }; + +#endif diff --git a/test/transactions/types/fixtures/transfer_v1.hpp b/test/transactions/types/fixtures/transfer_v1.hpp new file mode 100644 index 00000000..ab8bc7bd --- /dev/null +++ b/test/transactions/types/fixtures/transfer_v1.hpp @@ -0,0 +1,126 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_TRANSFER_V1_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_TRANSFER_V1_HPP + +#include +#include + +#include "common.hpp" + +namespace v1 { + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 1, +// "network": 23, +// "type": 0, +// "timestamp": 83506126, +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "10000000", +// "amount": "1", +// "expiration": 0, +// "recipientId": "AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff", +// "signature": "3045022100f8783a2d1b58271a444e3f39e9f58343c622c15e24dcf982c22a656b6d20a701022052501b5af702bdc59cb90ffea4546e767ca01c4c8f1e07a3483ed96a57eea250", +// "id": "bf89cf279289ca668d29fbb15ccedb24933f4a4a4c4381a7ef7760634c3a5b89" +// }, +// "serialized": "ff011700ce33fa04034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192809698000000000000010000000000000000000000171dfc69b54c7fe901e91d5a9ab78388645e2427ea3045022100f8783a2d1b58271a444e3f39e9f58343c622c15e24dcf982c22a656b6d20a701022052501b5af702bdc59cb90ffea4546e767ca01c4c8f1e07a3483ed96a57eea250" +// } + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 1, +// "network": 23, +// "type": 0, +// "timestamp": 83506245, +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "10000000", +// "amount": "1", +// "vendorField": "Hello World", +// "expiration": 0, +// "recipientId": "AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff", +// "signature": "304402204209261f274cb4d71b0504a3827d9670ac8cca1cb4aab6b3fc55b0b63a9f03e502205cbb25435d7279e536958cf8fa588c86ebe2a00571073a96a79084906ee0d509", +// "id": "374ecda42eadb02839ccb7d8fa9d73df8b0b4159dca29248bc38deae0cfd0c69" +// }, +// "serialized": "ff0117004534fa04034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19280969800000000000b48656c6c6f20576f726c64010000000000000000000000171dfc69b54c7fe901e91d5a9ab78388645e2427ea304402204209261f274cb4d71b0504a3827d9670ac8cca1cb4aab6b3fc55b0b63a9f03e502205cbb25435d7279e536958cf8fa588c86ebe2a00571073a96a79084906ee0d509" +// } + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_0_TYPE = 0U; +constexpr uint32_t TYPE_0_TIMESTAMP = 83506126UL; +constexpr uint32_t TYPE_0_VF_TIMESTAMP = 83506245; +constexpr uint64_t TYPE_0_FEE = 10000000ULL; +constexpr uint64_t TYPE_0_AMOUNT = 1ULL; +constexpr uint32_t TYPE_0_EXPIRATION = 0UL; + +//////////////////////////////////////////////////////////////////////////////// +// ff011700ce33fa04034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192809698000000000000010000000000000000000000171dfc69b54c7fe901e91d5a9ab78388645e2427ea3045022100f8783a2d1b58271a444e3f39e9f58343c622c15e24dcf982c22a656b6d20a701022052501b5af702bdc59cb90ffea4546e767ca01c4c8f1e07a3483ed96a57eea250 +const std::vector TYPE_0_BYTES = { + 255, 1, 23, 0, 206, 51, 250, 4, 3, 65, 81, 163, 236, 70, + 181, 103, 10, 104, 43, 10, 99, 57, 79, 134, 53, 135, 209, 188, + 151, 72, 59, 27, 108, 112, 235, 88, 231, 240, 174, 209, 146, 128, + 150, 152, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 23, 29, 252, 105, 181, 76, 127, 233, + 1, 233, 29, 90, 154, 183, 131, 136, 100, 94, 36, 39, 234, 48, + 69, 2, 33, 0, 248, 120, 58, 45, 27, 88, 39, 26, 68, 78, + 63, 57, 233, 245, 131, 67, 198, 34, 193, 94, 36, 220, 249, 130, + 194, 42, 101, 107, 109, 32, 167, 1, 2, 32, 82, 80, 27, 90, + 247, 2, 189, 197, 156, 185, 15, 254, 164, 84, 110, 118, 124, 160, + 28, 76, 143, 30, 7, 163, 72, 62, 217, 106, 87, 238, 162, 80 }; + +//////////////////////////////////////////////////////////////////////////////// +// ff0117004534fa04034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19280969800000000000b48656c6c6f20576f726c64010000000000000000000000171dfc69b54c7fe901e91d5a9ab78388645e2427ea304402204209261f274cb4d71b0504a3827d9670ac8cca1cb4aab6b3fc55b0b63a9f03e502205cbb25435d7279e536958cf8fa588c86ebe2a00571073a96a79084906ee0d509 +const std::vector TYPE_0_BYTES_VF = { + 255, 1, 23, 0, 69, 52, 250, 4, 3, 65, 81, 163, 236, 70, 181, + 103, 10, 104, 43, 10, 99, 57, 79, 134, 53, 135, 209, 188, 151, 72, + 59, 27, 108, 112, 235, 88, 231, 240, 174, 209, 146, 128, 150, 152, 0, + 0, 0, 0, 0, 11, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, + 100, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 29, + 252, 105, 181, 76, 127, 233, 1, 233, 29, 90, 154, 183, 131, 136, 100, + 94, 36, 39, 234, 48, 68, 2, 32, 66, 9, 38, 31, 39, 76, 180, + 215, 27, 5, 4, 163, 130, 125, 150, 112, 172, 140, 202, 28, 180, 170, + 182, 179, 252, 85, 176, 182, 58, 159, 3, 229, 2, 32, 92, 187, 37, + 67, 93, 114, 121, 229, 54, 149, 140, 248, 250, 88, 140, 134, 235, 226, + 160, 5, 113, 7, 58, 150, 167, 144, 132, 144, 110, 224, 213, 9 }; + +//////////////////////////////////////////////////////////////////////////////// +// "Hello World" +constexpr uint8_t TYPE_0_VF[] = { + 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100}; + +//////////////////////////////////////////////////////////////////////////////// +// base58Decode(AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff) | 171dfc69b54c7fe901e91d5a9ab78388645e2427ea +constexpr uint8_t TYPE_0_RECIPIENT[] = { + 23, 29, 252, 105, 181, 76, 127, 233, 1, 233, 29, + 90, 154, 183, 131, 136, 100, 94, 36, 39, 234 }; + +//////////////////////////////////////////////////////////////////////////////// +// 3045022100f8783a2d1b58271a444e3f39e9f58343c622c15e24dcf982c22a656b6d20a701022052501b5af702bdc59cb90ffea4546e767ca01c4c8f1e07a3483ed96a57eea250 +constexpr uint8_t TYPE_0_SIGNATURE[] = { + 48, 69, 2, 33, 0, 248, 120, 58, 45, 27, 88, 39, 26, 68, 78, + 63, 57, 233, 245, 131, 67, 198, 34, 193, 94, 36, 220, 249, 130, 194, + 42, 101, 107, 109, 32, 167, 1, 2, 32, 82, 80, 27, 90, 247, 2, + 189, 197, 156, 185, 15, 254, 164, 84, 110, 118, 124, 160, 28, 76, 143, + 30, 7, 163, 72, 62, 217, 106, 87, 238, 162, 80 }; + +//////////////////////////////////////////////////////////////////////////////// +// 3045022100f8783a2d1b58271a444e3f39e9f58343c622c15e24dcf982c22a656b6d20a701022052501b5af702bdc59cb90ffea4546e767ca01c4c8f1e07a3483ed96a57eea250 +constexpr uint8_t TYPE_0_VF_SIGNATURE[] = { + 48, 68, 2, 32, 66, 9, 38, 31, 39, 76, 180, 215, 27, 5, + 4, 163, 130, 125, 150, 112, 172, 140, 202, 28, 180, 170, 182, 179, + 252, 85, 176, 182, 58, 159, 3, 229, 2, 32, 92, 187, 37, 67, + 93, 114, 121, 229, 54, 149, 140, 248, 250, 88, 140, 134, 235, 226, + 160, 5, 113, 7, 58, 150, 167, 144, 132, 144, 110, 224, 213, 9 }; + +} // namespace v1 + +#endif diff --git a/test/transactions/types/fixtures/vote.hpp b/test/transactions/types/fixtures/vote.hpp new file mode 100644 index 00000000..c3d94ccd --- /dev/null +++ b/test/transactions/types/fixtures/vote.hpp @@ -0,0 +1,78 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_VOTE_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_VOTE_HPP + +#include +#include + +#include "common.hpp" + +//////////////////////////////////////////////////////////////////////////////// +// { +// "data": { +// "version": 2, +// "network": 23, +// "typeGroup": 1, +// "type": 3, +// "nonce": "1", +// "senderPublicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192", +// "fee": "100000000", +// "asset": { +// "votes": [ +// "+034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192" +// ] +// }, +// "signature": "3045022100dfa4edd1fb7bc587eb277dd996ab0dde73dc4942a293e5634bd48be9c7d25880022025c91f30c88f49bc3ed77614749c0c439f0759457a4bf22ed5c97f338659ccee", +// "id": "ca544581927728ae03a63f088e76053f357dc8fb0422512bb8a7c6a0f7ae16cf" +// }, +// "serialized": "ff02170100000003000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200e1f50500000000000101034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed1923045022100dfa4edd1fb7bc587eb277dd996ab0dde73dc4942a293e5634bd48be9c7d25880022025c91f30c88f49bc3ed77614749c0c439f0759457a4bf22ed5c97f338659ccee" +// } + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_3_TYPE = 3U; +constexpr uint64_t TYPE_3_FEE = 100000000ULL; +constexpr uint8_t TYPE_3_VOTE_COUNT = 1U; + +//////////////////////////////////////////////////////////////////////////////// +constexpr auto TYPE_3_VOTES_STRING = "+034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192"; + +//////////////////////////////////////////////////////////////////////////////// +// ff02170100000003000100000000000000034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200e1f50500000000000101034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed1923045022100dfa4edd1fb7bc587eb277dd996ab0dde73dc4942a293e5634bd48be9c7d25880022025c91f30c88f49bc3ed77614749c0c439f0759457a4bf22ed5c97f338659ccee +const std::vector TYPE_3_BYTES = { + 255, 2, 23, 1, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, + 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, + 231, 240, 174, 209, 146, 0, 225, 245, 5, 0, 0, 0, 0, 0, 1, + 1, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, 10, 99, 57, + 79, 134, 53, 135, 209, 188, 151, 72, 59, 27, 108, 112, 235, 88, 231, + 240, 174, 209, 146, 48, 69, 2, 33, 0, 223, 164, 237, 209, 251, 123, + 197, 135, 235, 39, 125, 217, 150, 171, 13, 222, 115, 220, 73, 66, 162, + 147, 229, 99, 75, 212, 139, 233, 199, 210, 88, 128, 2, 32, 37, 201, + 31, 48, 200, 143, 73, 188, 62, 215, 118, 20, 116, 156, 12, 67, 159, + 7, 89, 69, 122, 75, 242, 46, 213, 201, 127, 51, 134, 89, 204, 238 }; + +//////////////////////////////////////////////////////////////////////////////// +// +034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192 +constexpr uint8_t TYPE_3_VOTE[] = { + 1, 3, 65, 81, 163, 236, 70, 181, 103, 10, 104, 43, + 10, 99, 57, 79, 134, 53, 135, 209, 188, 151, 72, 59, + 27, 108, 112, 235, 88, 231, 240, 174, 209, 146 }; + +//////////////////////////////////////////////////////////////////////////////// +// 3045022100dfa4edd1fb7bc587eb277dd996ab0dde73dc4942a293e5634bd48be9c7d25880022025c91f30c88f49bc3ed77614749c0c439f0759457a4bf22ed5c97f338659ccee +constexpr uint8_t TYPE_3_SIGNATURE[] = { + 48, 69, 2, 33, 0, 223, 164, 237, 209, 251, 123, 197, 135, 235, 39, + 125, 217, 150, 171, 13, 222, 115, 220, 73, 66, 162, 147, 229, 99, 75, + 212, 139, 233, 199, 210, 88, 128, 2, 32, 37, 201, 31, 48, 200, 143, + 73, 188, 62, 215, 118, 20, 116, 156, 12, 67, 159, 7, 89, 69, 122, + 75, 242, 46, 213, 201, 127, 51, 134, 89, 204, 238 }; + +#endif diff --git a/test/transactions/types/fixtures/vote_v1.hpp b/test/transactions/types/fixtures/vote_v1.hpp new file mode 100644 index 00000000..de4bc2ac --- /dev/null +++ b/test/transactions/types/fixtures/vote_v1.hpp @@ -0,0 +1,73 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#ifndef ARK_TRANSACTIONS_TYPES_FIXTURES_VOTE_V1_HPP +#define ARK_TRANSACTIONS_TYPES_FIXTURES_VOTE_V1_HPP + +#include +#include + +#include "common.hpp" + +namespace v1 { + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint8_t TYPE_3_TYPE = 3U; +constexpr uint32_t TYPE_3_TIMESTAMP = 41269366UL; +constexpr uint64_t TYPE_3_FEE = 100000000ULL; +constexpr uint8_t TYPE_3_VOTE_COUNT = 1U; + +//////////////////////////////////////////////////////////////////////////////// +// ff011e0376b87502034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed19200e1f50500000000000101022cca9529ec97a772156c152a00aad155ee6708243e65c9d211a589cb5d43234d304402204b8bb403e2db7f9599d46d0f5d39f8bb1d0663d875af7ec1154448e98466e86302201e92fb57e13fb729b07e1027fa3d6e3f28e0d5828ed2d7c53a5e8db08cb6d068304402201329882762a42d1af9079c822a9e3feefa47b7476b0afe61440637408958a64402206da179b08e31d9c784fbb23abe2c9b50353ed7881dc29787a5e8ecbee2dfda66 +const std::vector TYPE_3_BYTES = { + 255, 1, 30, 3, 118, 184, 117, 2, 3, 65, 81, 163, 236, 70, 181, + 103, 10, 104, 43, 10, 99, 57, 79, 134, 53, 135, 209, 188, 151, 72, + 59, 27, 108, 112, 235, 88, 231, 240, 174, 209, 146, 0, 225, 245, 5, + 0, 0, 0, 0, 0, 1, 1, 2, 44, 202, 149, 41, 236, 151, 167, + 114, 21, 108, 21, 42, 0, 170, 209, 85, 238, 103, 8, 36, 62, 101, + 201, 210, 17, 165, 137, 203, 93, 67, 35, 77, 48, 68, 2, 32, 75, + 139, 180, 3, 226, 219, 127, 149, 153, 212, 109, 15, 93, 57, 248, 187, + 29, 6, 99, 216, 117, 175, 126, 193, 21, 68, 72, 233, 132, 102, 232, + 99, 2, 32, 30, 146, 251, 87, 225, 63, 183, 41, 176, 126, 16, 39, + 250, 61, 110, 63, 40, 224, 213, 130, 142, 210, 215, 197, 58, 94, 141, + 176, 140, 182, 208, 104, 48, 68, 2, 32, 19, 41, 136, 39, 98, 164, + 45, 26, 249, 7, 156, 130, 42, 158, 63, 238, 250, 71, 183, 71, 107, + 10, 254, 97, 68, 6, 55, 64, 137, 88, 166, 68, 2, 32, 109, 161, + 121, 176, 142, 49, 217, 199, 132, 251, 178, 58, 190, 44, 155, 80, 53, + 62, 215, 136, 29, 194, 151, 135, 165, 232, 236, 190, 226, 223, 218, 102 }; + +//////////////////////////////////////////////////////////////////////////////// +// (01) + pubKey... +// +022cca9529ec97a772156c152a00aad155ee6708243e65c9d211a589cb5d43234d +constexpr uint8_t TYPE_3_VOTE[] = { + 1, 2, 44, 202, 149, 41, 236, 151, 167, 114, 21, 108, + 21, 42, 0, 170, 209, 85, 238, 103, 8, 36, 62, 101, + 201, 210, 17, 165, 137, 203, 93, 67, 35, 77 }; + +//////////////////////////////////////////////////////////////////////////////// +// 304402204b8bb403e2db7f9599d46d0f5d39f8bb1d0663d875af7ec1154448e98466e86302201e92fb57e13fb729b07e1027fa3d6e3f28e0d5828ed2d7c53a5e8db08cb6d068 +constexpr uint8_t TYPE_3_SIGNATURE[] = { + 48, 68, 2, 32, 75, 139, 180, 3, 226, 219, 127, 149, 153, 212, + 109, 15, 93, 57, 248, 187, 29, 6, 99, 216, 117, 175, 126, 193, + 21, 68, 72, 233, 132, 102, 232, 99, 2, 32, 30, 146, 251, 87, + 225, 63, 183, 41, 176, 126, 16, 39, 250, 61, 110, 63, 40, 224, + 213, 130, 142, 210, 215, 197, 58, 94, 141, 176, 140, 182, 208, 104 }; + +//////////////////////////////////////////////////////////////////////////////// +// 304402201329882762a42d1af9079c822a9e3feefa47b7476b0afe61440637408958a64402206da179b08e31d9c784fbb23abe2c9b50353ed7881dc29787a5e8ecbee2dfda66 +constexpr uint8_t TYPE_3_SECOND_SIG[] = { + 48, 68, 2, 32, 19, 41, 136, 39, 98, 164, 45, 26, 249, 7, + 156, 130, 42, 158, 63, 238, 250, 71, 183, 71, 107, 10, 254, 97, + 68, 6, 55, 64, 137, 88, 166, 68, 2, 32, 109, 161, 121, 176, + 142, 49, 217, 199, 132, 251, 178, 58, 190, 44, 155, 80, 53, 62, + 215, 136, 29, 194, 151, 135, 165, 232, 236, 190, 226, 223, 218, 102 }; + +} // namespace v1 + +#endif diff --git a/test/transactions/types/htlc_claim.cpp b/test/transactions/types/htlc_claim.cpp new file mode 100644 index 00000000..391927ab --- /dev/null +++ b/test/transactions/types/htlc_claim.cpp @@ -0,0 +1,111 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include +#include +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/htlc_claim.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_htlc_claim, deserialize_ecdsa) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, TYPE_9_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V2, data.version); + ASSERT_EQ(COMMON_MAINNET, data.network); + ASSERT_EQ(COMMON_TYPEGROUP, data.typeGroup); + ASSERT_EQ(TYPE_9_TYPE, data.type); + ASSERT_EQ(COMMON_NONCE, data.nonce); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(TYPE_9_FEE, data.fee); + + ASSERT_TRUE(array_cmp(TYPE_9_LOCK_TX_ID, + data.asset.htlcClaim.id.data(), + HASH_32_LEN)); + + ASSERT_TRUE(array_cmp(TYPE_9_UNLOCK_SECRET, + data.asset.htlcClaim.secret.data(), + HASH_32_LEN)); + + ASSERT_TRUE(array_cmp(TYPE_9_SIGNATURE, + data.signature.data(), + sizeof(TYPE_9_SIGNATURE))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_htlc_claim, serialize_ecdsa) { + TransactionData data; + + data.network = COMMON_MAINNET; + data.typeGroup = COMMON_TYPEGROUP; + data.type = TYPE_9_TYPE; + data.nonce = COMMON_NONCE; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.fee = TYPE_9_FEE; + + std::copy_n(TYPE_9_LOCK_TX_ID, + HASH_32_LEN, + data.asset.htlcClaim.id.begin()); + + + std::copy_n(TYPE_9_UNLOCK_SECRET, + HASH_32_LEN, + data.asset.htlcClaim.secret.begin()); + + data.signature.resize(sizeof(TYPE_9_SIGNATURE)); + std::copy_n(TYPE_9_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(TYPE_9_BYTES.data(), + Serializer::serialize(data).data(), + TYPE_9_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_htlc_claim, add_to_map) { + HtlcClaim claim; + + std::copy_n(TYPE_9_LOCK_TX_ID, HASH_32_LEN, claim.id.begin()); + + std::copy_n(TYPE_9_UNLOCK_SECRET, HASH_32_LEN, claim.secret.begin()); + + std::map claimMap; + + HtlcClaim::addToMap(claim, claimMap); + + ASSERT_STREQ(TYPE_9_LOCK_ID_STRING, + claimMap.find("lockTransactionId")->second.c_str()); + + ASSERT_STREQ(TYPE_9_SECRET_STRING, + claimMap.find("unlockSecret")->second.c_str()); +} diff --git a/test/transactions/types/htlc_lock.cpp b/test/transactions/types/htlc_lock.cpp new file mode 100644 index 00000000..b7267cdd --- /dev/null +++ b/test/transactions/types/htlc_lock.cpp @@ -0,0 +1,139 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include +#include +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/htlc_lock.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_htlc_lock, deserialize_ecdsa) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, TYPE_8_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V2, data.version); + ASSERT_EQ(COMMON_MAINNET, data.network); + ASSERT_EQ(COMMON_TYPEGROUP, data.typeGroup); + ASSERT_EQ(TYPE_8_TYPE, data.type); + ASSERT_EQ(COMMON_NONCE, data.nonce); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(TYPE_8_FEE, data.fee); + + ASSERT_EQ(TYPE_8_AMOUNT, data.asset.htlcLock.amount); + + ASSERT_TRUE(array_cmp(TYPE_8_SECRET_HASH, + data.asset.htlcLock.secretHash.data(), + ADDRESS_HASH_LEN)); + + ASSERT_EQ(TYPE_8_EXPIRATION_TYPE, data.asset.htlcLock.expirationType); + + ASSERT_EQ(TYPE_8_EXPIRATION_VALUE, data.asset.htlcLock.expiration); + + ASSERT_TRUE(array_cmp(TYPE_8_RECIPIENT, + data.asset.htlcLock.recipientId.data(), + ADDRESS_HASH_LEN)); + + ASSERT_TRUE(array_cmp(TYPE_8_SIGNATURE, + data.signature.data(), + sizeof(TYPE_8_SIGNATURE))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_htlc_lock, serialize_ecdsa) { + TransactionData data; + + data.network = COMMON_MAINNET; + data.typeGroup = COMMON_TYPEGROUP; + data.type = TYPE_8_TYPE; + data.nonce = COMMON_NONCE; + + std::copy_n(fixtures::PublicKeyBytes.begin(), + PUBLICKEY_COMPRESSED_LEN, + data.senderPublicKey.begin()); + + data.fee = TYPE_8_FEE; + + data.asset.htlcLock.amount = TYPE_8_AMOUNT; + + std::copy_n(TYPE_8_SECRET_HASH, + HASH_32_LEN, + data.asset.htlcLock.secretHash.begin()); + + data.asset.htlcLock.expirationType = TYPE_8_EXPIRATION_TYPE; + data.asset.htlcLock.expiration = TYPE_8_EXPIRATION_VALUE; + + std::copy_n(TYPE_8_RECIPIENT, + ADDRESS_HASH_LEN, + data.asset.htlcLock.recipientId.begin()); + + data.signature.resize(sizeof(TYPE_8_SIGNATURE)); + std::copy_n(TYPE_8_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(TYPE_8_BYTES.data(), + Serializer::serialize(data).data(), + TYPE_8_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_htlc_lock, add_to_map) { + HtlcLock lock; + + lock.amount = TYPE_8_AMOUNT; + + std::copy_n(TYPE_8_SECRET_HASH, + HASH_32_LEN, + lock.secretHash.begin()); + + lock.expirationType = TYPE_8_EXPIRATION_TYPE; + lock.expiration = TYPE_8_EXPIRATION_VALUE; + + std::copy_n(TYPE_8_RECIPIENT, + ADDRESS_HASH_LEN, + lock.recipientId.begin()); + + std::map lockMap; + + HtlcLock::addToMap(lock, lockMap); + + ASSERT_STREQ(TYPE_8_AMOUNT_STRING, + lockMap.find("amount")->second.c_str()); + + ASSERT_STREQ(TYPE_8_SECRET_STRING, + lockMap.find("secretHash")->second.c_str()); + + ASSERT_STREQ(TYPE_8_EXPIRATION_TYPE_STRING, + lockMap.find("expirationType")->second.c_str()); + + ASSERT_STREQ(TYPE_8_EXPIRATION_VALUE_STRING, + lockMap.find("expiration")->second.c_str()); + + ASSERT_STREQ(TYPE_8_RECIPIENT_STRING, + lockMap.find("recipientId")->second.c_str()); +} diff --git a/test/transactions/types/htlc_refund.cpp b/test/transactions/types/htlc_refund.cpp new file mode 100644 index 00000000..5feb683e --- /dev/null +++ b/test/transactions/types/htlc_refund.cpp @@ -0,0 +1,97 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include +#include +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/htlc_refund.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_htlc_refund, deserialize_ecdsa) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, TYPE_10_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V2, data.version); + ASSERT_EQ(COMMON_MAINNET, data.network); + ASSERT_EQ(COMMON_TYPEGROUP, data.typeGroup); + ASSERT_EQ(TYPE_10_TYPE, data.type); + ASSERT_EQ(COMMON_NONCE, data.nonce); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(TYPE_10_FEE, data.fee); + + ASSERT_TRUE(array_cmp(TYPE_10_LOCK_TX_ID, + data.asset.htlcRefund.id.data(), + HASH_32_LEN)); + + ASSERT_TRUE(array_cmp(TYPE_10_SIGNATURE, + data.signature.data(), + sizeof(TYPE_10_SIGNATURE))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_htlc_refund, serialize_ecdsa) { + TransactionData data; + + data.network = COMMON_MAINNET; + data.typeGroup = COMMON_TYPEGROUP; + data.type = TYPE_10_TYPE; + data.nonce = COMMON_NONCE; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.fee = TYPE_10_FEE; + + std::copy_n(TYPE_10_LOCK_TX_ID, + HASH_32_LEN, + data.asset.htlcRefund.id.begin()); + + data.signature.resize(sizeof(TYPE_10_SIGNATURE)); + std::copy_n(TYPE_10_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(TYPE_10_BYTES.data(), + Serializer::serialize(data).data(), + TYPE_10_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_htlc_refund, add_to_map) { + HtlcRefund refund; + + std::copy_n(TYPE_10_LOCK_TX_ID, HASH_32_LEN, refund.id.begin()); + + std::map refundMap; + + HtlcRefund::addToMap(refund, refundMap); + + ASSERT_STREQ(TYPE_10_LOCK_ID_STRING, + refundMap.find("lockTransactionId")->second.c_str()); +} diff --git a/test/transactions/types/ipfs.cpp b/test/transactions/types/ipfs.cpp new file mode 100644 index 00000000..9147e5c8 --- /dev/null +++ b/test/transactions/types/ipfs.cpp @@ -0,0 +1,117 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include +#include +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/ipfs.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_ipfs, deserialize_ecdsa) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, TYPE_5_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V2, data.version); + ASSERT_EQ(COMMON_MAINNET, data.network); + ASSERT_EQ(COMMON_TYPEGROUP, data.typeGroup); + ASSERT_EQ(TYPE_5_TYPE, data.type); + ASSERT_EQ(COMMON_NONCE, data.nonce); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(TYPE_5_FEE, data.fee); + + ASSERT_TRUE(array_cmp(TYPE_5_DAG, + data.asset.ipfs.dag.data(), + sizeof(TYPE_5_DAG))); + + ASSERT_TRUE(array_cmp(TYPE_5_SIGNATURE, + data.signature.data(), + sizeof(TYPE_5_SIGNATURE))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_ipfs, serialize_ecdsa) { + TransactionData data; + + data.network = COMMON_MAINNET; + data.typeGroup = COMMON_TYPEGROUP; + data.type = TYPE_5_TYPE; + data.nonce = COMMON_NONCE; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.fee = TYPE_5_FEE; + + data.asset.ipfs.dag.insert(data.asset.ipfs.dag.begin(), + TYPE_5_DAG, + TYPE_5_DAG + sizeof(TYPE_5_DAG)); + + data.signature.resize(sizeof(TYPE_5_SIGNATURE)); + std::copy_n(TYPE_5_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(TYPE_5_BYTES.data(), + Serializer::serialize(data).data(), + TYPE_5_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_ipfs, invalid_len) { + Ipfs ipfs; + + // Invalid Ipfs on Deserialization. + Ipfs::Deserialize( + &ipfs, + reinterpret_cast("\0")); + + ASSERT_EQ(0UL, ipfs.dag.size()); + + // Invalid Ipfs on Serialization. + std::array buffer {}; + Ipfs::Serialize(ipfs, buffer.data()); + + ASSERT_EQ(0U, buffer.at(0)); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_ipfs, add_to_map) { + Ipfs ipfs; + + ipfs.dag.insert(ipfs.dag.begin(), + TYPE_5_DAG, + TYPE_5_DAG + sizeof(TYPE_5_DAG)); + + std::map ipfsMap; + + Ipfs::addToMap(ipfs, ipfsMap); + + ASSERT_STREQ(TYPE_5_IPFS_STRING, + ipfsMap.find("ipfs")->second.c_str()); +} diff --git a/test/transactions/types/multi_payment.cpp b/test/transactions/types/multi_payment.cpp new file mode 100644 index 00000000..bba2b2ec --- /dev/null +++ b/test/transactions/types/multi_payment.cpp @@ -0,0 +1,160 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include +#include +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/multi_payment.hpp" + +#include "test_helpers.h" + +#include "utils/platform.h" + +#ifndef USE_IOT + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_multi_payment, deserialize_ecdsa) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, TYPE_6_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V2, data.version); + ASSERT_EQ(COMMON_DEVNET, data.network); + ASSERT_EQ(COMMON_TYPEGROUP, data.typeGroup); + ASSERT_EQ(TYPE_6_TYPE, data.type); + ASSERT_EQ(COMMON_NONCE, data.nonce); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(TYPE_6_FEE, data.fee); + + ASSERT_EQ(TYPE_6_AMOUNT, data.asset.multiPayment.amounts.at(0)); + + ASSERT_EQ(TYPE_6_FIRST_ADDRESS[0], + data.asset.multiPayment.addresses.at(0).at(0)); + + ASSERT_TRUE(array_cmp(&TYPE_6_FIRST_ADDRESS[1], + &data.asset.multiPayment.addresses.at(0).at(1), + HASH_20_LEN)); + + ASSERT_EQ(TYPE_6_AMOUNT, data.asset.multiPayment.amounts.back()); + + ASSERT_EQ(TYPE_6_LAST_ADDRESS[0], + data.asset.multiPayment.addresses.back().at(0)); + + ASSERT_TRUE(array_cmp(&TYPE_6_LAST_ADDRESS[1], + &data.asset.multiPayment.addresses.back().at(1), + HASH_20_LEN)); + + ASSERT_TRUE(array_cmp(TYPE_6_SIGNATURE, + data.signature.data(), + sizeof(TYPE_6_SIGNATURE))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_multi_payment, serialize_ecdsa) { + TransactionData data; + + data.network = COMMON_DEVNET; + data.typeGroup = COMMON_TYPEGROUP; + data.type = TYPE_6_TYPE; + data.nonce = COMMON_NONCE; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.fee = TYPE_6_FEE; + + data.asset.multiPayment.n_payments = TYPE_6_N_PAYMENTS; + + data.asset.multiPayment.amounts.resize(TYPE_6_N_PAYMENTS); + data.asset.multiPayment.addresses.resize(TYPE_6_N_PAYMENTS); + + size_t offset = TYPE_6_ASSET_OFFSET + sizeof(uint16_t); + + for (uint8_t i = 0U; i < TYPE_6_N_PAYMENTS; ++i) { + data.asset.multiPayment.amounts.at(i) = TYPE_6_AMOUNT; + + offset += sizeof(uint64_t); + + data.asset.multiPayment.addresses.at(i).at(0) = TYPE_6_BYTES[offset]; + + std::copy_n(&TYPE_6_BYTES[offset + 1U], + HASH_20_LEN, + &data.asset.multiPayment.addresses.at(i).at(1)); + + offset += ADDRESS_HASH_LEN; + } + + data.signature.resize(sizeof(TYPE_6_SIGNATURE)); + std::copy_n(TYPE_6_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(TYPE_6_BYTES.data(), + Serializer::serialize(data).data(), + TYPE_6_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_multi_payment, invalid_len) { + MultiPayment payments; + + // Invalid MultiPayment on Deserialization. + MultiPayment::Deserialize( + &payments, + reinterpret_cast("badlen")); + + ASSERT_EQ(0UL, payments.n_payments); + + // Invalid MultiPayment on Serialization. + payments.n_payments = MULTI_PAYMENT_NETWORK_LIMIT + 1; + std::vector buffer {}; + MultiPayment::Serialize(payments, buffer, 0UL); + + ASSERT_TRUE(buffer.empty()); +} + +//////////////////////////////////////////////////////////////////////////////// +// MultiPayment Map setup can be quite a bit of overhead. +// Let's save some trade a little space for Deserialization overhead. +TEST(transactions_multi_payment, add_to_map) { + TransactionData data; + Deserializer::deserialize(&data, TYPE_6_BYTES); + + std::map multiPaymentMap; + + MultiPayment::addToMap(data.asset.multiPayment, multiPaymentMap); + + ASSERT_STREQ(TYPE_6_N_PAYMENTS_STRING, + multiPaymentMap.find("n_payments")->second.c_str()); + + ASSERT_STREQ(TYPE_6_AMOUNTS_STRING, + multiPaymentMap.find("amounts")->second.c_str()); + + ASSERT_STREQ(TYPE_6_ADDRESSES_STRING, + multiPaymentMap.find("addresses")->second.c_str()); +} + +#endif // #ifndef USE_IOT diff --git a/test/transactions/types/second_signature.cpp b/test/transactions/types/second_signature.cpp new file mode 100644 index 00000000..fd814db9 --- /dev/null +++ b/test/transactions/types/second_signature.cpp @@ -0,0 +1,100 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include +#include +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/second_signature.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_second_signature, deserialize_ecdsa) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, TYPE_1_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V2, data.version); + ASSERT_EQ(COMMON_MAINNET, data.network); + ASSERT_EQ(COMMON_TYPEGROUP, data.typeGroup); + ASSERT_EQ(TYPE_1_TYPE, data.type); + ASSERT_EQ(COMMON_NONCE, data.nonce); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(TYPE_1_FEE, data.fee); + ASSERT_EQ(TYPE_1_VF_LENGTH, data.vendorField.size()); + + ASSERT_TRUE(array_cmp(TYPE_1_SECOND_PUBLICKEY, + data.asset.secondSignature.publicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_TRUE(array_cmp(TYPE_1_SIGNATURE, + data.signature.data(), + sizeof(TYPE_1_SIGNATURE))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_second_signature, serialize_ecdsa) { + TransactionData data; + + data.network = COMMON_MAINNET; + data.typeGroup = COMMON_TYPEGROUP; + data.type = TYPE_1_TYPE; + data.nonce = COMMON_NONCE; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.fee = TYPE_1_FEE; + + std::copy_n(TYPE_1_SECOND_PUBLICKEY, + PUBLICKEY_COMPRESSED_LEN, + data.asset.secondSignature.publicKey.begin()); + + data.signature.resize(sizeof(TYPE_1_SIGNATURE)); + std::copy_n(TYPE_1_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(TYPE_1_BYTES.data(), + Serializer::serialize(data).data(), + TYPE_1_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_second_signature, add_to_map) { + SecondSignature secondSignature; + + std::copy_n(TYPE_1_SECOND_PUBLICKEY, + PUBLICKEY_COMPRESSED_LEN, + secondSignature.publicKey.begin()); + + std::map secondSignatureMap; + + SecondSignature::addToMap(secondSignature, secondSignatureMap); + + ASSERT_STREQ(TYPE_1_PUBLICKEY_STRING, + secondSignatureMap.find("publicKey")->second.c_str()); +} diff --git a/test/transactions/types/second_signature_v1.cpp b/test/transactions/types/second_signature_v1.cpp new file mode 100644 index 00000000..49ba24eb --- /dev/null +++ b/test/transactions/types/second_signature_v1.cpp @@ -0,0 +1,78 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/second_signature_v1.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_v1, deserialize_second_signature_registration) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, v1::TYPE_1_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V1, data.version); + ASSERT_EQ(COMMON_DEVNET, data.network); + ASSERT_EQ(v1::TYPE_1_TYPE, data.type); + ASSERT_EQ(v1::TYPE_1_TIMESTAMP, data.timestamp); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(v1::TYPE_1_FEE, data.fee); + + ASSERT_TRUE(array_cmp(v1::TYPE_1_SECOND_PUBLICKEY, + data.asset.secondSignature.publicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_TRUE(array_cmp(v1::TYPE_1_SIGNATURE, + data.signature.data(), + data.signature.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_v1, serialize_second_signature_registration) { + TransactionData data; + + data.version = COMMON_VERSION_V1; + data.type = v1::TYPE_1_TYPE; + data.fee = v1::TYPE_1_FEE; + data.timestamp = v1::TYPE_1_TIMESTAMP; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + std::copy_n(v1::TYPE_1_SECOND_PUBLICKEY, + PUBLICKEY_COMPRESSED_LEN, + data.asset.secondSignature.publicKey.begin()); + + data.signature.resize(sizeof(v1::TYPE_1_SIGNATURE)); + std::copy_n(v1::TYPE_1_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(v1::TYPE_1_BYTES.data(), + Serializer::serialize(data).data(), + v1::TYPE_1_BYTES.size())); +} diff --git a/test/transactions/types/transfer.cpp b/test/transactions/types/transfer.cpp new file mode 100644 index 00000000..2a6d53fc --- /dev/null +++ b/test/transactions/types/transfer.cpp @@ -0,0 +1,187 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include +#include +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/transfer.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transfer, deserialize_ecdsa) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, TYPE_0_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V2, data.version); + ASSERT_EQ(COMMON_MAINNET, data.network); + ASSERT_EQ(COMMON_TYPEGROUP, data.typeGroup); + ASSERT_EQ(TYPE_0_TYPE, data.type); + ASSERT_EQ(COMMON_NONCE, data.nonce); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(TYPE_0_FEE, data.fee); + ASSERT_EQ(TYPE_0_NVF_LENGTH, data.vendorField.size()); + + ASSERT_EQ(TYPE_0_AMOUNT, data.asset.transfer.amount); + ASSERT_EQ(TYPE_0_EXPIRATION, data.asset.transfer.expiration); + + ASSERT_TRUE(array_cmp(TYPE_0_RECIPIENT, + data.asset.transfer.recipientId.data(), + ADDRESS_HASH_LEN)); + + ASSERT_TRUE(array_cmp(TYPE_0_SIGNATURE, + data.signature.data(), + sizeof(TYPE_0_SIGNATURE))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transfer, deserialize_vendorfield_ecdsa) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, TYPE_0_VF_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V2, data.version); + ASSERT_EQ(COMMON_MAINNET, data.network); + ASSERT_EQ(COMMON_TYPEGROUP, data.typeGroup); + ASSERT_EQ(TYPE_0_TYPE, data.type); + ASSERT_EQ(COMMON_NONCE, data.nonce); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(TYPE_0_FEE, data.fee); + ASSERT_EQ(TYPE_0_VF_LENGTH, data.vendorField.size()); + + ASSERT_TRUE(array_cmp(TYPE_0_VF, + data.vendorField.data(), + sizeof(TYPE_0_VF))); + + ASSERT_EQ(TYPE_0_AMOUNT, data.asset.transfer.amount); + ASSERT_EQ(TYPE_0_EXPIRATION, data.asset.transfer.expiration); + + ASSERT_TRUE(array_cmp(TYPE_0_RECIPIENT, + data.asset.transfer.recipientId.data(), + ADDRESS_HASH_LEN)); + + ASSERT_TRUE(array_cmp(TYPE_0_VF_SIGNATURE, + data.signature.data(), + sizeof(TYPE_0_VF_SIGNATURE))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transfer, serialize_ecdsa) { + TransactionData data; + + data.network = COMMON_MAINNET; + data.typeGroup = COMMON_TYPEGROUP; + data.type = TYPE_0_TYPE; + data.nonce = COMMON_NONCE; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.fee = TYPE_0_FEE; + + data.asset.transfer.amount = TYPE_0_AMOUNT; + data.asset.transfer.expiration = TYPE_0_EXPIRATION; + + std::copy_n(TYPE_0_RECIPIENT, + ADDRESS_HASH_LEN, + data.asset.transfer.recipientId.begin()); + + data.signature.resize(sizeof(TYPE_0_SIGNATURE)); + std::copy_n(TYPE_0_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(TYPE_0_BYTES.data(), + Serializer::serialize(data).data(), + TYPE_0_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transfer, serialize_vendorfield_ecdsa) { + TransactionData data; + + data.network = COMMON_MAINNET; + data.typeGroup = COMMON_TYPEGROUP; + data.type = TYPE_0_TYPE; + data.nonce = COMMON_NONCE; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.fee = TYPE_0_FEE; + + data.vendorField.insert(data.vendorField.begin(), + TYPE_0_VF, + TYPE_0_VF + sizeof(TYPE_0_VF)); + + data.asset.transfer.amount = TYPE_0_AMOUNT; + data.asset.transfer.expiration = TYPE_0_EXPIRATION; + + std::copy_n(TYPE_0_RECIPIENT, + ADDRESS_HASH_LEN, + data.asset.transfer.recipientId.begin()); + + data.signature.resize(sizeof(TYPE_0_VF_SIGNATURE)); + std::copy_n(TYPE_0_VF_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(TYPE_0_VF_BYTES.data(), + Serializer::serialize(data).data(), + TYPE_0_VF_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transfer, add_to_map) { + Transfer transfer; + + transfer.amount = TYPE_0_AMOUNT; + transfer.expiration = TYPE_0_EXPIRATION; + + std::copy_n(TYPE_0_RECIPIENT, + ADDRESS_HASH_LEN, + transfer.recipientId.begin()); + + std::map transferMap; + + Transfer::addToMap(transfer, transferMap); + + ASSERT_STREQ(TYPE_0_AMOUNT_STRING, + transferMap.find("amount")->second.c_str()); + + ASSERT_STREQ(TYPE_0_EXPIRATION_STRING, + transferMap.find("expiration")->second.c_str()); + + ASSERT_STREQ(TYPE_0_RECIPIENT_ID_STRING, + transferMap.find("recipientId")->second.c_str()); +} diff --git a/test/transactions/types/transfer_v1.cpp b/test/transactions/types/transfer_v1.cpp new file mode 100644 index 00000000..bae13221 --- /dev/null +++ b/test/transactions/types/transfer_v1.cpp @@ -0,0 +1,152 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/transfer_v1.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transfer_v1, deserialize) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, v1::TYPE_0_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V1, data.version); + ASSERT_EQ(COMMON_MAINNET, data.network); + ASSERT_EQ(v1::TYPE_0_TYPE, data.type); + ASSERT_EQ(v1::TYPE_0_TIMESTAMP, data.timestamp); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(v1::TYPE_0_FEE, data.fee); + + ASSERT_EQ(v1::TYPE_0_AMOUNT, data.asset.transfer.amount); + ASSERT_EQ(v1::TYPE_0_EXPIRATION, data.asset.transfer.expiration); + + ASSERT_TRUE(array_cmp(v1::TYPE_0_RECIPIENT, + data.asset.transfer.recipientId.data(), + ADDRESS_HASH_LEN)); + + ASSERT_TRUE(array_cmp(v1::TYPE_0_SIGNATURE, + data.signature.data(), + data.signature.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transfer_v1, deserialize_vendorfield) { + TransactionData data; + + Deserializer::deserialize(&data, v1::TYPE_0_BYTES_VF); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V1, data.version); + ASSERT_EQ(COMMON_MAINNET, data.network); + ASSERT_EQ(v1::TYPE_0_TYPE, data.type); + ASSERT_EQ(v1::TYPE_0_VF_TIMESTAMP, data.timestamp); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(v1::TYPE_0_FEE, data.fee); + + ASSERT_TRUE(array_cmp(v1::TYPE_0_VF, + data.vendorField.data(), + sizeof(v1::TYPE_0_VF))); + + ASSERT_EQ(v1::TYPE_0_AMOUNT, data.asset.transfer.amount); + ASSERT_EQ(v1::TYPE_0_EXPIRATION, data.asset.transfer.expiration); + + ASSERT_TRUE(array_cmp(v1::TYPE_0_RECIPIENT, + data.asset.transfer.recipientId.data(), + ADDRESS_HASH_LEN)); + + ASSERT_TRUE(array_cmp(v1::TYPE_0_VF_SIGNATURE, + data.signature.data(), + data.signature.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transfer_v1, serialize) { + TransactionData data; + + data.version = COMMON_VERSION_V1; + data.network = COMMON_MAINNET; + data.type = v1::TYPE_0_TYPE; + data.fee = v1::TYPE_0_FEE; + data.timestamp = v1::TYPE_0_TIMESTAMP; + + data.asset.transfer.amount = v1::TYPE_0_AMOUNT; + + std::copy_n(v1::TYPE_0_RECIPIENT, + ADDRESS_HASH_LEN, + data.asset.transfer.recipientId.begin()); + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.signature.resize(sizeof(v1::TYPE_0_SIGNATURE)); + std::copy_n(v1::TYPE_0_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(v1::TYPE_0_BYTES.data(), + Serializer::serialize(data).data(), + v1::TYPE_0_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_transfer_v1, serialize_vendorfield) { + TransactionData data; + + data.version = COMMON_VERSION_V1; + data.network = COMMON_MAINNET; + data.type = v1::TYPE_0_TYPE; + data.fee = v1::TYPE_0_FEE; + data.timestamp = v1::TYPE_0_VF_TIMESTAMP; + + data.asset.transfer.amount = v1::TYPE_0_AMOUNT; + + std::copy(v1::TYPE_0_RECIPIENT, + v1::TYPE_0_RECIPIENT + ADDRESS_HASH_LEN, + data.asset.transfer.recipientId.begin()); + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.vendorField.insert(data.vendorField.begin(), + v1::TYPE_0_VF, + v1::TYPE_0_VF + sizeof(v1::TYPE_0_VF)); + + data.signature.resize(sizeof(v1::TYPE_0_VF_SIGNATURE)); + std::copy_n(v1::TYPE_0_VF_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(v1::TYPE_0_BYTES_VF.data(), + Serializer::serialize(data).data(), + v1::TYPE_0_BYTES.size())); +} diff --git a/test/transactions/types/vote.cpp b/test/transactions/types/vote.cpp new file mode 100644 index 00000000..651c8927 --- /dev/null +++ b/test/transactions/types/vote.cpp @@ -0,0 +1,101 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include +#include +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/vote.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_vote, deserialize_ecdsa) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, TYPE_3_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V2, data.version); + ASSERT_EQ(COMMON_MAINNET, data.network); + ASSERT_EQ(COMMON_TYPEGROUP, data.typeGroup); + ASSERT_EQ(TYPE_3_TYPE, data.type); + ASSERT_EQ(COMMON_NONCE, data.nonce); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(TYPE_3_FEE, data.fee); + + ASSERT_EQ(TYPE_3_VOTE_COUNT, data.asset.vote.count); + + ASSERT_TRUE(array_cmp(TYPE_3_VOTE, + data.asset.vote.votes.data(), + sizeof(TYPE_3_VOTE))); + + ASSERT_TRUE(array_cmp(TYPE_3_SIGNATURE, + data.signature.data(), + sizeof(TYPE_3_SIGNATURE))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_vote, serialize_ecdsa) { + TransactionData data; + + data.network = COMMON_MAINNET; + data.typeGroup = COMMON_TYPEGROUP; + data.type = TYPE_3_TYPE; + data.nonce = COMMON_NONCE; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.fee = TYPE_3_FEE; + + data.asset.vote.count = 1U; + + std::copy_n(TYPE_3_VOTE, VOTE_LEN, data.asset.vote.votes.begin()); + + data.signature.resize(sizeof(TYPE_3_SIGNATURE)); + std::copy_n(TYPE_3_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + ASSERT_TRUE(array_cmp(TYPE_3_BYTES.data(), + Serializer::serialize(data).data(), + TYPE_3_BYTES.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_vote, add_to_map) { + Vote vote; + + vote.count = TYPE_3_VOTE_COUNT; + + std::copy_n(TYPE_3_VOTE, VOTE_LEN, vote.votes.begin()); + + std::map voteMap; + + Vote::addToMap(vote, voteMap); + + ASSERT_STREQ(TYPE_3_VOTES_STRING, + voteMap.find("votes")->second.c_str()); +} diff --git a/test/transactions/types/vote_v1.cpp b/test/transactions/types/vote_v1.cpp new file mode 100644 index 00000000..149cb42f --- /dev/null +++ b/test/transactions/types/vote_v1.cpp @@ -0,0 +1,89 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include + +#include "transactions/deserializer.hpp" +#include "transactions/serializer.hpp" + +#include "fixtures/identity.hpp" +#include "fixtures/common.hpp" +#include "fixtures/vote_v1.hpp" + +#include "test_helpers.h" + +using namespace Ark::Crypto; +using namespace Ark::Crypto::transactions; + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_vote_v1, deserialize) { + TransactionData data; + + ASSERT_TRUE(Deserializer::deserialize(&data, v1::TYPE_3_BYTES)); + + ASSERT_EQ(COMMON_HEADER, data.header); + ASSERT_EQ(COMMON_VERSION_V1, data.version); + ASSERT_EQ(COMMON_DEVNET, data.network); + ASSERT_EQ(v1::TYPE_3_TYPE, data.type); + ASSERT_EQ(v1::TYPE_3_TIMESTAMP, data.timestamp); + + ASSERT_TRUE(array_cmp(fixtures::PublicKeyBytes.data(), + data.senderPublicKey.data(), + PUBLICKEY_COMPRESSED_LEN)); + + ASSERT_EQ(v1::TYPE_3_FEE, data.fee); + + ASSERT_EQ(v1::TYPE_3_VOTE_COUNT, data.asset.vote.count); + + ASSERT_TRUE(array_cmp(v1::TYPE_3_VOTE, + data.asset.vote.votes.data(), + sizeof(v1::TYPE_3_VOTE))); + + ASSERT_TRUE(array_cmp(v1::TYPE_3_SIGNATURE, + data.signature.data(), + data.signature.size())); + + ASSERT_TRUE(array_cmp(v1::TYPE_3_SECOND_SIG, + data.secondSignature.data(), + data.secondSignature.size())); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(transactions_vote_v1, serialize) { + TransactionData data; + + data.version = COMMON_VERSION_V1; + data.type = v1::TYPE_3_TYPE; + data.fee = v1::TYPE_3_FEE; + data.timestamp = v1::TYPE_3_TIMESTAMP; + + std::copy(fixtures::PublicKeyBytes.begin(), + fixtures::PublicKeyBytes.end(), + data.senderPublicKey.begin()); + + data.asset.vote.count = 1U; + + std::copy_n(v1::TYPE_3_VOTE, VOTES_LEN, data.asset.vote.votes.begin()); + + data.signature.resize(sizeof(v1::TYPE_3_SIGNATURE)); + std::copy_n(v1::TYPE_3_SIGNATURE, + data.signature.size(), + data.signature.begin()); + + data.secondSignature.resize(sizeof(v1::TYPE_3_SECOND_SIG)); + std::copy_n(v1::TYPE_3_SECOND_SIG, + data.secondSignature.size(), + data.secondSignature.begin()); + + ASSERT_TRUE(array_cmp(v1::TYPE_3_BYTES.data(), + Serializer::serialize(data).data(), + v1::TYPE_3_BYTES.size())); +} diff --git a/test/utils/base58.cpp b/test/utils/base58.cpp index 3182f47d..57d87509 100644 --- a/test/utils/base58.cpp +++ b/test/utils/base58.cpp @@ -1,77 +1,125 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" +#include + #include "utils/base58.hpp" #include "fixtures/identity.hpp" + +#include "transactions/types/fixtures/ipfs.hpp" + +#include "test_helpers.h" + using namespace Ark::Crypto; -using namespace fixtures::identity; - -TEST(utils, base58_get_hash_pair) { - auto hashPair = Base58::getHashPair(tAddressString); - for (auto i = 0U; i < HASH_20_BYTE_LEN; ++i) { - ASSERT_EQ(hashPair.pubkeyHash.at(i), tAddressBytes.at(i)); - }; - ASSERT_EQ(hashPair.version, tAddressVersion); -} -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_base58, check_encode) { + const auto ipfsHash = Base58::checkEncode(TYPE_5_DAG, sizeof(TYPE_5_DAG)); + ASSERT_STREQ(TYPE_5_IPFS_STRING, ipfsHash.c_str()); +} -TEST(utils, base58_parse_hash) { - auto address = Base58::parseHash(tAddressBytes.data(), tAddressVersion); - ASSERT_STREQ(address.c_str(), tAddressString); +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_base58, check_encode_invalid_len) { + const auto base58Threshold = 164; + const auto invalidHash = Base58::checkEncode(nullptr, base58Threshold + 1); + ASSERT_TRUE(invalidHash.empty()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_base58, check_encode_zeros) { + const std::array zero = { 0 }; + const auto zeroHash = Base58::checkEncode(zero.data(), zero.size()); -TEST(utils, base58_get_wif) { - auto wif = Base58::getWif(tPrivateKeyBytes.data(), tWifVersion); - ASSERT_STREQ(wif.c_str(), tWifString); + ASSERT_FALSE(zeroHash.empty()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_base58, get_hash_pair) { + auto hashPair = Base58::getHashPair(fixtures::AddressString); -TEST(utils, base58_parse_wif) { - uint8_t outVersion; - auto privateKey = Base58::parseWif(tWifString, &outVersion); - for (auto i = 0U; i < HASH_20_BYTE_LEN; ++i) { - ASSERT_EQ(privateKey[i], tPrivateKeyBytes.at(i)); - }; - ASSERT_EQ(outVersion, tWifVersion); + ASSERT_TRUE(array_cmp(fixtures::AddressBytes.data(), + hashPair.pubkeyHash.data(), + HASH_20_LEN)); + + ASSERT_EQ(fixtures::AddressVersion, hashPair.version); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_base58, parse_address_hash) { + // 1e0995750207ecaf0ccf251c1265b92ad84f553662 + const AddressHash addressHash = { 30, 9, 149, 117, 2, 7, 236, + 175, 12, 207, 37, 28, 18, 101, + 185, 42, 216, 79, 85, 54, 98 }; -TEST(utils, base58_parse_wif_invalid) { - uint8_t outVersion; - auto privateKey = Base58::parseWif(invalid::tWifString, &outVersion); - for (auto i = 0U; i < HASH_20_BYTE_LEN; ++i) { - ASSERT_NE(privateKey.at(i), tPrivateKeyBytes.at(i)); - }; - ASSERT_NE(outVersion, tWifVersion); + // D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib + auto address = Base58::parseAddressHash(addressHash); + ASSERT_STREQ(fixtures::AddressString, address.c_str()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_base58, parse_hash) { + auto address = Base58::parsePubkeyHash(fixtures::AddressBytes.data(), + fixtures::AddressVersion); + ASSERT_STREQ(fixtures::AddressString, address.c_str()); +} -TEST(utils, base58_validate) { - ASSERT_TRUE(Base58::validate(tWifString, WIF_STRING_LEN)); +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_base58, get_wif) { + auto wif = Base58::getWif(fixtures::PrivateKeyBytes.data(), + fixtures::WifVersion); + ASSERT_STREQ(fixtures::WifString, wif.c_str()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_base58, parse_wif) { + uint8_t outVersion; + auto privateKey = Base58::parseWif(fixtures::WifString, &outVersion); -TEST(utils, base58_validate_zero_size) { - ASSERT_FALSE(Base58::validate(tWifString, 0)); + ASSERT_TRUE(array_cmp(fixtures::PrivateKeyBytes.data(), + privateKey.data(), + PRIVATEKEY_BYTE_LEN)); + + ASSERT_EQ(fixtures::WifVersion, outVersion); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_base58, parse_wif_invalid) { + uint8_t outVersion; + auto privateKey = Base58::parseWif(fixtures::invalid::WifString, &outVersion); + + for (auto &e : privateKey) { + ASSERT_EQ(0U, e); + } -TEST(utils, base58_validate_invalid_size) { - ASSERT_FALSE(Base58::validate(tWifString, WIF_STRING_LEN - 1)); + ASSERT_NE(fixtures::WifVersion, outVersion); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_base58, validate) { + ASSERT_TRUE(Base58::validate(fixtures::WifString, WIF_STRING_LEN)); +} -TEST(utils, base58_validate_invalid_char) { - ASSERT_FALSE(Base58::validate(invalid::tWifString, WIF_STRING_LEN)); +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_base58, validate_zero_size) { + ASSERT_FALSE(Base58::validate(fixtures::WifString, 0)); } +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_base58, validate_invalid_size) { + ASSERT_FALSE(Base58::validate(fixtures::WifString, WIF_STRING_LEN - 1)); +} + +//////////////////////////////////////////////////////////////////////////////// + +TEST(utils_base58, validate_invalid_char) { + ASSERT_FALSE(Base58::validate(fixtures::invalid::WifString, WIF_STRING_LEN)); +} diff --git a/test/utils/crypto_helpers.cpp b/test/utils/crypto_helpers.cpp index 693f227f..b50f411b 100644 --- a/test/utils/crypto_helpers.cpp +++ b/test/utils/crypto_helpers.cpp @@ -1,43 +1,38 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" -#include "utils/crypto_helpers.h" - #include #include -TEST(utils, pack_unpack) { - std::vector packBuffer; - const uint8_t packValue = 23U; - pack(packBuffer, packValue); - - uint8_t unpackValue; - unpack(&unpackValue, &packBuffer[0]); - ASSERT_EQ(packValue, unpackValue); -} - -/**/ - -TEST(utils, join) { - const std::string strBuffer = "123"; - const std::string strBuffer2 = "456"; - std::vector vstr { strBuffer, strBuffer2 }; +#include "utils/crypto_helpers.h" - std::string joined = join(vstr); +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_crypto_helpers, unpack) { + std::vector packBuffer; + const uint8_t packValue = 23U; + pack(packBuffer, packValue); - ASSERT_STREQ(joined.c_str(), std::string(strBuffer + strBuffer2).c_str()); + uint8_t unpackValue; + unpack(&unpackValue, &packBuffer[0]); + ASSERT_EQ(packValue, unpackValue); } -/**/ - -TEST(utils, join_offset) { - const std::string strBuffer = "654"; - const std::string strBuffer2 = "321"; - std::vector vstr { strBuffer, strBuffer2 }; +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_crypto_helpers, join) { + const auto strBuffer = "123"; + std::vector vstr(3); + vstr.at(0) = strBuffer[0]; + vstr.at(1) = strBuffer[1]; + vstr.at(2) = strBuffer[2]; - std::string joined = join(vstr, 1); - - ASSERT_STREQ(joined.c_str(), - std::string(strBuffer.substr(1) + - strBuffer2.substr(1)).c_str()); -} \ No newline at end of file + std::string joined = join(vstr); + ASSERT_STREQ(strBuffer, joined.c_str()); +} diff --git a/test/utils/hex.cpp b/test/utils/hex.cpp index 7cb16ab2..b42241c0 100644 --- a/test/utils/hex.cpp +++ b/test/utils/hex.cpp @@ -1,59 +1,74 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include "utils/hex.hpp" #include "fixtures/message.hpp" -using namespace Ark::Crypto; -using namespace fixtures::message; -TEST(utils, hex_bytes_to_hex) { - const auto result = BytesToHex(tMessageSignatureBytes.begin(), - tMessageSignatureBytes.end()); - ASSERT_STREQ(result.c_str(), tSignatureString); -} +#include "test_helpers.h" -/**/ +using namespace Ark::Crypto; -TEST(utils, hex_hex_to_bytes) { - const auto result = HexToBytes(tSignatureString); - for (auto i = 0U; i < result.size(); ++i) { - ASSERT_TRUE(result.at(i) == tMessageSignatureBytes.at(i)); - }; +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_hex, bytes_to_hex) { + const auto result = BytesToHex(fixtures::MessageSignatureBytes.begin(), + fixtures::MessageSignatureBytes.end()); + ASSERT_STREQ(fixtures::MessageSignatureString, result.c_str()); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_hex, hex_to_bytes) { + const auto result = HexToBytes(fixtures::MessageSignatureString); + ASSERT_TRUE(array_cmp(fixtures::MessageSignatureBytes.data(), + result.data(), + result.size())); +} -TEST(utils, hex_hex_to_bytes_spaces) { - const auto result = HexToBytes(tSignatureStringSpaces); - for (auto i = 0U; i < result.size(); ++i) { - ASSERT_TRUE(result.at(i) == tMessageSignatureBytes.at(i)); - }; +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_hex, hex_to_bytes_spaces) { + const auto result = HexToBytes(fixtures::MessageSignatureStringSpaces); + ASSERT_TRUE(array_cmp(fixtures::MessageSignatureBytes.data(), + result.data(), + result.size())); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_hex, hex_to_bytes_invalid) { + const auto result = HexToBytes(fixtures::MessageSignatureStringSpaces + 1); + ASSERT_TRUE(array_cmp(fixtures::MessageSignatureBytes.data(), + result.data(), + result.size())); -TEST(utils, hex_hex_to_bytes_array) { - const auto result = HexToBytesArray<>(tSignatureString); - for (auto i = 0U; i < result.size(); ++i) { - ASSERT_TRUE(result.at(i) == tMessageSignatureBytes[i]); - }; } -/**/ - -TEST(utils, hex_hex_to_bytes_array_spaces) { - const auto result = HexToBytesArray<>(tSignatureStringSpaces); - for (auto i = 0U; i < result.size(); ++i) { - ASSERT_TRUE(result.at(i) == tMessageSignatureBytes.at(i)); - }; +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_hex, hex_to_bytes_array) { + const auto result = HexToBytesArray<>(fixtures::MessageSignatureString); + ASSERT_TRUE(array_cmp(fixtures::MessageSignatureBytes.data(), + result.data(), + result.size())); } -/**/ +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_hex, hex_to_bytes_array_spaces) { + const auto result = HexToBytesArray<>(fixtures::MessageSignatureStringSpaces); + ASSERT_TRUE(array_cmp(fixtures::MessageSignatureBytes.data(), + result.data(), + result.size())); +} -TEST(utils, hex_hex_to_bytes_array_null_input) { - const auto result = HexToBytesArray<>(nullptr); - for (unsigned char i : result) { - ASSERT_EQ(result.at(i), 0); - }; +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_hex, hex_to_bytes_array_null_input) { + const auto result = HexToBytesArray<>(nullptr); + for (auto &e : result) { + ASSERT_EQ(0U, e); + } } diff --git a/test/utils/packing.cpp b/test/utils/packing.cpp new file mode 100644 index 00000000..a5d050d7 --- /dev/null +++ b/test/utils/packing.cpp @@ -0,0 +1,72 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ + +#include "gtest/gtest.h" + +#include "utils/packing.h" + +#include +#include + +#include "test_helpers.h" + +//////////////////////////////////////////////////////////////////////////////// +constexpr uint16_t U16 = 1U; +constexpr uint32_t U32 = 1UL; +constexpr uint64_t U64 = 1ULL; + +constexpr std::array Packed2LE = { 1U, 0U }; +constexpr std::array Packed4LE = { 1U, 0U, 0U, 0U }; +constexpr std::array Packed8LE = { 1U, 0U, 0U, 0U, + 0U, 0U, 0U, 0U }; + +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_packing, pack_2_le) { + std::array packed2LE {}; + + pack2LE(packed2LE.data(), &U16); + + ASSERT_TRUE(array_cmp(Packed2LE.data(), packed2LE.data(), sizeof(uint16_t))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_packing, pack_4_le) { + std::array packed4LE {}; + + pack4LE(packed4LE.data(), &U32); + + ASSERT_TRUE(array_cmp(Packed4LE.data(), packed4LE.data(), sizeof(uint32_t))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_packing, pack_8_le) { + std::array packed8LE {}; + + pack8LE(packed8LE.data(), &U64); + + ASSERT_TRUE(array_cmp(Packed4LE.data(), packed8LE.data(), sizeof(uint32_t))); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_packing, unpack_2_le) { + const auto unpacked2 = unpack2LE(Packed2LE, 0); + ASSERT_EQ(U16, unpacked2); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_packing, unpack_4_le) { + const auto unpacked4 = unpack4LE(Packed4LE, 0); + ASSERT_EQ(U32, unpacked4); +} + +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_packing, unpack_8_le) { + const auto unpacked8 = unpack8LE(Packed8LE, 0); + ASSERT_EQ(U64, unpacked8); +} diff --git a/test/utils/str.cpp b/test/utils/str.cpp index 1706d7e4..79c3f52b 100644 --- a/test/utils/str.cpp +++ b/test/utils/str.cpp @@ -1,28 +1,39 @@ +/** + * This file is part of Ark Cpp Crypto. + * + * (c) Ark Ecosystem + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + **/ #include "gtest/gtest.h" #include "utils/str.hpp" #include "fixtures/identity.hpp" + +#include "test_helpers.h" + using namespace Ark::Crypto; -using namespace fixtures::identity; -static constexpr const size_t PassphraseLength = 31U; - -TEST(utils, str_strlen_safe) { - const auto passphrase = tPassphrase; - for (auto i = 0U; i < PassphraseLength; ++i) { - ASSERT_EQ(strlenSafe(&passphrase[i]), PassphraseLength - i); - }; -} -/**/ +//////////////////////////////////////////////////////////////////////////////// +constexpr auto PassphraseLength = 31U; -TEST(utils, str_string_to_bytes) { - const auto passphraseBytes = StringToBytes(tPassphrase); - for (auto i = 0U; i < PassphraseLength; ++i) { - ASSERT_EQ(passphraseBytes.at(i), tPassphraseBytes.at(i)); - }; - ASSERT_EQ(passphraseBytes.size(), PassphraseLength); +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_str, strlen_safe) { + const auto passphrase = fixtures::Passphrase; + for (auto i = 0U; i < PassphraseLength; ++i) { + ASSERT_EQ(strlenSafe(&passphrase[i]), PassphraseLength - i); + } } -/**/ \ No newline at end of file +//////////////////////////////////////////////////////////////////////////////// +TEST(utils_str, string_to_bytes) { + const auto passphraseBytes = StringToBytes(fixtures::Passphrase); + ASSERT_TRUE(array_cmp(fixtures::PassphraseBytes.data(), + passphraseBytes.data(), + passphraseBytes.size())); + + ASSERT_EQ(passphraseBytes.size(), PassphraseLength); +} From b96d24cc224349cb23c388d3479c4c179a61fb58 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 10 Feb 2020 06:10:51 -0800 Subject: [PATCH 19/21] fix: always include amount in transaction payloads (#202) --- src/include/cpp-crypto/interfaces/constants.h | 3 +++ src/transactions/mapping/json.cpp | 12 ++++++++++++ src/transactions/types/multi_payment.cpp | 3 --- test/transactions/transaction.cpp | 16 ++++++++-------- test/transactions/transaction_v1.cpp | 2 +- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/include/cpp-crypto/interfaces/constants.h b/src/include/cpp-crypto/interfaces/constants.h index b9eeff46..e265ed01 100644 --- a/src/include/cpp-crypto/interfaces/constants.h +++ b/src/include/cpp-crypto/interfaces/constants.h @@ -76,6 +76,9 @@ constexpr size_t WIF_STRING_LEN = 52U; constexpr auto KEY_ASSET_LABEL = "asset"; const auto KEY_ASSET_SIZE = strlen(KEY_ASSET_LABEL); +constexpr auto KEY_AMOUNT_LABEL = "amount"; +const auto KEY_AMOUNT_SIZE = strlen(KEY_AMOUNT_LABEL); + //////////////////////////////////////////////////////////////////////////////// // Misc constexpr uint8_t BASE_10 = 10U; diff --git a/src/transactions/mapping/json.cpp b/src/transactions/mapping/json.cpp index aa770d64..f6e89fd8 100644 --- a/src/transactions/mapping/json.cpp +++ b/src/transactions/mapping/json.cpp @@ -74,6 +74,12 @@ static auto getCommonJsonCapacity( KEY_TIMESTAMP_SIZE + UINT32_MAX_CHARS; } + // Amount + if (transactionVersion == TRANSACTION_VERSION_TYPE_2) { + commonCapacity += JSON_OBJECT_SIZE(1) + + KEY_AMOUNT_SIZE + UINT64_MAX_CHARS; + } + // Vendorfield if (map.find(KEY_VENDORFIELD_LABEL) != map.end()) { commonCapacity += JSON_OBJECT_SIZE(1) + @@ -244,6 +250,12 @@ static void mapCommonJson(DynamicJsonDocument &jsonDoc, // Fee jsonDoc[KEY_FEE_LABEL] = map.at(KEY_FEE_LABEL); + // Amount + jsonDoc[KEY_AMOUNT_LABEL] = map.find(KEY_AMOUNT_LABEL) == map.end() && + transactionVersion == TRANSACTION_VERSION_TYPE_2 + ? "0" + : map.at(KEY_AMOUNT_LABEL); + // VendorField if (map.find(KEY_VENDORFIELD_LABEL) != map.end()) { jsonDoc[KEY_VENDORFIELD_LABEL] = map.at(KEY_VENDORFIELD_LABEL); diff --git a/src/transactions/types/multi_payment.cpp b/src/transactions/types/multi_payment.cpp index 1bea3432..89c91d67 100644 --- a/src/transactions/types/multi_payment.cpp +++ b/src/transactions/types/multi_payment.cpp @@ -175,9 +175,6 @@ const auto KEY_AMOUNTS_SIZE = strlen(KEY_AMOUNTS_LABEL); constexpr auto KEY_ADDRESSES_LABEL = "addresses"; const auto KEY_ADDRESSES_SIZE = strlen(KEY_ADDRESSES_LABEL); -constexpr auto KEY_AMOUNT_LABEL = "amount"; -const auto KEY_AMOUNT_SIZE = strlen(KEY_AMOUNT_LABEL); - constexpr auto KEY_RECIPIENT_ID_LABEL = "recipientId"; const auto KEY_RECIPIENT_ID_SIZE = strlen(KEY_RECIPIENT_ID_LABEL); diff --git a/test/transactions/transaction.cpp b/test/transactions/transaction.cpp index ecc4bce2..b89a8efe 100644 --- a/test/transactions/transaction.cpp +++ b/test/transactions/transaction.cpp @@ -220,19 +220,19 @@ TEST(transactions_transaction, to_map_to_json) { // NOLINT Transaction secondSignatureTx; secondSignatureTx.deserialize(TYPE_1_BYTES); const auto secondSignatureJson = secondSignatureTx.toJson(); - const std::string secondSignatureResponse = R"({"version":2,"network":23,"typeGroup":1,"type":1,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"500000000","asset":{"signature":{"publicKey":"02877e4f35c76abaeb152b128670db0a7ae10b3999afcd28a42938b653fbf87ae9"}},"id":"6934f7a73846b2daeb94e70b3b8a23118410cb552ada5993bf1b47b10e168916","signature":"304402201786b482215f6f63e3a9414324283decffc64b829209640024a4e78d7ed557f50220197e53c8e8b7f8a645eb82712587926ad930695a8ca43a2308ca0a9dbeabb65b"})"; + const std::string secondSignatureResponse = R"({"version":2,"network":23,"typeGroup":1,"type":1,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"500000000","amount":"0","asset":{"signature":{"publicKey":"02877e4f35c76abaeb152b128670db0a7ae10b3999afcd28a42938b653fbf87ae9"}},"id":"6934f7a73846b2daeb94e70b3b8a23118410cb552ada5993bf1b47b10e168916","signature":"304402201786b482215f6f63e3a9414324283decffc64b829209640024a4e78d7ed557f50220197e53c8e8b7f8a645eb82712587926ad930695a8ca43a2308ca0a9dbeabb65b"})"; ASSERT_STREQ(secondSignatureResponse.c_str(), secondSignatureJson.c_str()); Transaction delegateRegistrationTx; delegateRegistrationTx.deserialize(TYPE_2_BYTES); const auto delegateRegistrationJson = delegateRegistrationTx.toJson(); - const std::string delegateRegistrationResponse = R"({"version":2,"network":23,"typeGroup":1,"type":2,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"2500000000","asset":{"delegate":{"username":"arkdelegate"}},"id":"668f5db0d99189ee7dd85c991360ca19372302a13e3046511a60d115dbf1864e","signature":"30440220281812ca558b3b032aaf724aeaba5e20733e87f20fefe1c78536cc523de7e475022038a2454ca2665f6c3d910b04ecdd5995a7d82904f9f4bf878402a31262d2db61"})"; + const std::string delegateRegistrationResponse = R"({"version":2,"network":23,"typeGroup":1,"type":2,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"2500000000","amount":"0","asset":{"delegate":{"username":"arkdelegate"}},"id":"668f5db0d99189ee7dd85c991360ca19372302a13e3046511a60d115dbf1864e","signature":"30440220281812ca558b3b032aaf724aeaba5e20733e87f20fefe1c78536cc523de7e475022038a2454ca2665f6c3d910b04ecdd5995a7d82904f9f4bf878402a31262d2db61"})"; ASSERT_STREQ(delegateRegistrationResponse.c_str(), delegateRegistrationJson.c_str()); Transaction voteTx; voteTx.deserialize(TYPE_3_BYTES); const auto voteJson = voteTx.toJson(); - const std::string voteResponse = R"({"version":2,"network":23,"typeGroup":1,"type":3,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"100000000","asset":{"votes":["+034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192"]},"id":"ca544581927728ae03a63f088e76053f357dc8fb0422512bb8a7c6a0f7ae16cf","signature":"3045022100dfa4edd1fb7bc587eb277dd996ab0dde73dc4942a293e5634bd48be9c7d25880022025c91f30c88f49bc3ed77614749c0c439f0759457a4bf22ed5c97f338659ccee"})"; + const std::string voteResponse = R"({"version":2,"network":23,"typeGroup":1,"type":3,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"100000000","amount":"0","asset":{"votes":["+034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192"]},"id":"ca544581927728ae03a63f088e76053f357dc8fb0422512bb8a7c6a0f7ae16cf","signature":"3045022100dfa4edd1fb7bc587eb277dd996ab0dde73dc4942a293e5634bd48be9c7d25880022025c91f30c88f49bc3ed77614749c0c439f0759457a4bf22ed5c97f338659ccee"})"; ASSERT_STREQ(voteResponse.c_str(), voteJson.c_str()); // Transaction multiSignatureTx; // TODO @@ -240,7 +240,7 @@ TEST(transactions_transaction, to_map_to_json) { // NOLINT Transaction ipfsTx; ipfsTx.deserialize(TYPE_5_BYTES); const auto ipfsJson = ipfsTx.toJson(); - const std::string ipfsResponse = R"({"version":2,"network":23,"typeGroup":1,"type":5,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"500000000","asset":{"ipfs":"QmYSK2JyM3RyDyB52caZCTKFR3HKniEcMnNJYdk8DQ6KKB"},"id":"f68875f1fa80091ef42dec05c5c6a0ea77f4b83934f7eed0d1c72b910ddf6d03","signature":"30440220636aa75a15bcc59c9cf2db11ae7f9ed73f112b6ca14cc2aca1b6ed3d2daadaad02207434f5c6d17c98350942afafbaa57b6518ea4e78e8443f414398190183235787"})"; + const std::string ipfsResponse = R"({"version":2,"network":23,"typeGroup":1,"type":5,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"500000000","amount":"0","asset":{"ipfs":"QmYSK2JyM3RyDyB52caZCTKFR3HKniEcMnNJYdk8DQ6KKB"},"id":"f68875f1fa80091ef42dec05c5c6a0ea77f4b83934f7eed0d1c72b910ddf6d03","signature":"30440220636aa75a15bcc59c9cf2db11ae7f9ed73f112b6ca14cc2aca1b6ed3d2daadaad02207434f5c6d17c98350942afafbaa57b6518ea4e78e8443f414398190183235787"})"; ASSERT_STREQ(ipfsResponse.c_str(), ipfsJson.c_str()); // This MultiPayment is currently way too big for IoT. @@ -252,14 +252,14 @@ TEST(transactions_transaction, to_map_to_json) { // NOLINT multiPaymentTx.deserialize(TYPE_6_BYTES); const auto multiPaymentJson = multiPaymentTx.toJson(); - const std::string multiPaymentResponse = R"({"version":2,"network":30,"typeGroup":1,"type":6,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"10000000","asset":{"payments":[{"amount":"1","recipientId":"DHKxXag9PjfjHBbPg3HQS5WCaQZdgDf6yi"},{"amount":"1","recipientId":"DBzGiUk8UVjB2dKCfGRixknB7Ki3Zhqthp"},{"amount":"1","recipientId":"DFa7vn1LvWAyTuVDrQUr5NKaM73cfjx2Cp"},{"amount":"1","recipientId":"DSGsxX84gif4ipAxZjjCE2k2YpHmsNTJeY"},{"amount":"1","recipientId":"DQhzMRvVoCYCiZH2iSyuqCTcayz7z4XTKx"},{"amount":"1","recipientId":"DMSD6fFT1Xcxh4ErYExr5MtGnEuDcYu22m"},{"amount":"1","recipientId":"D7HCZG8hJuJqu9rANRdyNr6N1vpH2vbyx8"},{"amount":"1","recipientId":"DQy6ny2bkvWiDLboQMZ1cxnmoNC5JM228w"},{"amount":"1","recipientId":"D7EUgmA78qUaSfsNedfgKs2ALq28FhL3zo"},{"amount":"1","recipientId":"DEHyKHdtzHqTghfpwaBcvTzLpgPP5AAUgE"},{"amount":"1","recipientId":"DBgA92a616rwVi9GsgYUwBq9Y7dgvZiC41"},{"amount":"1","recipientId":"DPXaJv1GcVpZPvxw5T4fXebqTVhFpfqyrC"},{"amount":"1","recipientId":"D6JpPhN7BehrhNy7AbSQ2u9mkSZb1k7Ens"},{"amount":"1","recipientId":"D9sdJ42YtJpXeL7Fa1cTLiciW7FpGYqms4"},{"amount":"1","recipientId":"DJq86RdmTMKC257szcXRXKpbuoYPaL8KgL"},{"amount":"1","recipientId":"DMsQKioFXniGH3vHDNfkQLRRqsQsAS46Cy"},{"amount":"1","recipientId":"DHwPoAP8cMP9ZeKrhh5c99WzaoJqFKW2qi"},{"amount":"1","recipientId":"DAwN6Pp4ErGf69EypErrbtuWFfEMtuSzmE"},{"amount":"1","recipientId":"DQ6sE3jE9rTFC13e2ndooRdy5YCYinLbPm"},{"amount":"1","recipientId":"DFuo2NGezzHGwHjzG6c21JuJ9WpntLGFER"},{"amount":"1","recipientId":"D6qzeJEGG7rEBem5bNCCZqHtPCBtzsUZpP"},{"amount":"1","recipientId":"DNVhLKTPh4LnqhcnkogNS8iSxmsnFG17tC"},{"amount":"1","recipientId":"D8EPxx42Dr4bd8xXRbMHi8LHefFLaT2VaM"},{"amount":"1","recipientId":"DBK4VPsUQHUYkGc47FqF69PEyPjjqvnGzu"},{"amount":"1","recipientId":"D7XtDDKh2VrRtz5rtbBicfgSEoEQzEZiNx"},{"amount":"1","recipientId":"D9gQjhu2tDUstXfrbK85zHi23VtAk72qsS"},{"amount":"1","recipientId":"DKhfkyY4RZyxR7CFjQAeNtGKXAaVEBa9HK"},{"amount":"1","recipientId":"DMCBerfV13HBuJEwJTZRVTWzrYDxgb3QSy"},{"amount":"1","recipientId":"DLCoxbHdf9LMhEavEnj8mGv4AwVk8eEiKd"},{"amount":"1","recipientId":"D5taz6B4xDk1LD3jV4fYrUhaKC8DnTtziW"},{"amount":"1","recipientId":"DDb3EXY3refv2f5ymMME3hp2DXFqMPzGah"},{"amount":"1","recipientId":"D5HydybffvfuwdbBKQ1dnhiXzNnWq6CgQz"},{"amount":"1","recipientId":"D9DMKvx8fDyWyAP1EUGs5McBwwv3y5E1Yn"},{"amount":"1","recipientId":"DHXqndno9dBvGabhc7NdZWuoj6nRUdSaP7"},{"amount":"1","recipientId":"DJAmJiuLQGnzXWmH7KvosVLks7hhfxQ8np"},{"amount":"1","recipientId":"D752ZwkQZKm5gYYMUZV2tKFFZZaD35MtRa"},{"amount":"1","recipientId":"D6Xe5kVsK7axaPZ1tP2fVWaLFubyCajkVq"},{"amount":"1","recipientId":"D9yDJNK4xHP9Gx27s187Z5XHcNF5YFA94h"},{"amount":"1","recipientId":"DJuZC2smL8j86bUNrZiNceAubad3zs3drS"},{"amount":"1","recipientId":"DRUVFo5MjNrMHHQCwpVPH6AwLL2AULpgbH"},{"amount":"1","recipientId":"DNM5wLmqVUz6UgY14mt6BsMndy8JGcFwct"},{"amount":"1","recipientId":"DHMd77xyB8f6DnSCgxaRWfuM86cwyH76EH"},{"amount":"1","recipientId":"DFmg9q2KqGyretLazvRjWdynJAZbPkZPG2"},{"amount":"1","recipientId":"DMnY8QWLAmsb4wNEjRVvtQsNWF4SbXntxM"},{"amount":"1","recipientId":"DBMn94FxVB36nXgzbmtmfu6jVEGwwHNyNA"},{"amount":"1","recipientId":"DUD3r46LtArk4msu6jFrwn1hjxbZoXzX9t"},{"amount":"1","recipientId":"DFUNVTBd5zFexBaHkymr4UJqsHeXhPLKUF"},{"amount":"1","recipientId":"DFtCxvMSsF9qfw2mH1aNHxusXGJ2QzCahP"},{"amount":"1","recipientId":"D8LiYnmH4DLDyxCLTV7RrqxtCA21pfGkb9"},{"amount":"1","recipientId":"DASUgX1U7yvp8WDQ57QoTEUim6bqTYzxGw"},{"amount":"1","recipientId":"D5iNaf5ZckhdZivPfy6vFvBLeBDJtvDoGo"},{"amount":"1","recipientId":"DPrdeuFDcfMujvYK6n18RBAWgh7hYeiDeZ"},{"amount":"1","recipientId":"D9oaC7bd2YaJYHDdGkUdyAnfpkBrFFKZHy"},{"amount":"1","recipientId":"DUTUfseKR6qJRjqCuxeH3oRxMr6EFLUxRW"},{"amount":"1","recipientId":"DTYv1v3YdUNy81kzD1VhRx6e8jkDYnvCoh"},{"amount":"1","recipientId":"DE1BK8iL17PiBwo9TUCfkkm1vnUkobBwj8"},{"amount":"1","recipientId":"D7Ba6DnbpPJgqVnQNdN7baRsZs1DKvptMM"},{"amount":"1","recipientId":"DUBcre2e5KMykYr6xK56T5BrwkKMZkUF8r"},{"amount":"1","recipientId":"DPeqoTgBbRhyuEJtMnhqhSAeK32ymMNvjd"},{"amount":"1","recipientId":"DGmToX3GrCEUC8EJdZrXWTYFNWqQz1VVhX"},{"amount":"1","recipientId":"DArHKTMXf3F5zXS1i3GSwni9aA8TX1yQvh"},{"amount":"1","recipientId":"DTgcCvAYR2XdzUHv4WuEB6aShYEbh2MYp6"},{"amount":"1","recipientId":"DFLY9huetM6GMrt9EGp6sXDEiC7r3oSYHg"},{"amount":"1","recipientId":"DEszKKqdRipXiF7BDKS2Q4iJwwfzLdwADK"},{"amount":"1","recipientId":"DF45FRKUYcyUGeZyyTF3sCYJ8VFNXymzhJ"},{"amount":"1","recipientId":"DJb6jvhSvw2RmxCBEQSAzG6tjUs5rK3a5m"},{"amount":"1","recipientId":"DCqqT4x4on1dsbgUKRWkuZsdZaoohYK6NV"},{"amount":"1","recipientId":"D9SAVjqkxwWQmb82iqAedJPccFjDUnMSi9"},{"amount":"1","recipientId":"DBvXesEgzrAsm9YrzRb2jithR2hg7SZpuq"},{"amount":"1","recipientId":"DF5ZYcQsmgDvH6cVQR87xvXapyTUFB1a5R"},{"amount":"1","recipientId":"DQEfNNsJ6PQTA9abwWdiunPuebLZAhxbpZ"},{"amount":"1","recipientId":"DP6k5YTtaeNJUHZ72H6QtugFaHVB5vEBQe"},{"amount":"1","recipientId":"DJBTrPo6sDMGr2kcswTwDWtQyYV5adqnAp"},{"amount":"1","recipientId":"DMHtTBMyG5qGYgcZNRkb36XaCT3TUSKmYE"},{"amount":"1","recipientId":"DTbCpyVgTeJw4idpqbY5jwrEi7SxSid9GU"},{"amount":"1","recipientId":"D75g1ztcaHi46eUFRnakRryqG7GV9xsgGC"},{"amount":"1","recipientId":"DSkMiPrEx3YF6ijDjxhwCnbAbriC8sWKEW"},{"amount":"1","recipientId":"D7BHGU3UedoxpZko4nBLcz5oRtSmUmRfy6"},{"amount":"1","recipientId":"DQZUzueTvUJb5tnhBCziPYaMnzaoui4F57"},{"amount":"1","recipientId":"DGCCpnJ86YvxJAkHRPhC5jTBNGsy5PEDRh"},{"amount":"1","recipientId":"DHSW3vi66L63xnzRt9PadwSVrb9bCKhgvJ"},{"amount":"1","recipientId":"D6eAmMh6FFynorCSjHS1Qx75rXiN89soa7"},{"amount":"1","recipientId":"DGPoaSg15fb6As8bPKBAQrK3nCDpgfYow8"},{"amount":"1","recipientId":"DKPmC4G1ZEwxb5hZro2sJwRWJ1pQixcK6N"},{"amount":"1","recipientId":"DFpBcXzcJdFaN9rfVD8Nc5yWFvJ3DnePwa"},{"amount":"1","recipientId":"DKxQ9FDTBDQaLLV24sWc625w2Kycw2RoqR"},{"amount":"1","recipientId":"DNZr7NxGm97r8hV6Jg4rv3S5MgJrEVUWNQ"},{"amount":"1","recipientId":"DBBbtnKKDAW84bDjywEmQFLgwf36DxJHZJ"},{"amount":"1","recipientId":"DA7GfDKG5zYFeiZ1C4FPJBeTajpYNvXxcC"},{"amount":"1","recipientId":"DPAsWQyuxYkiMRzhngKBPTpWb6WWqxdd1T"},{"amount":"1","recipientId":"DTv6qvhUK1jks58gAA5YFzShHf3YX9sJVo"},{"amount":"1","recipientId":"DDwTm5FbgvWugYekvjE1dzantAVKxtGbNo"},{"amount":"1","recipientId":"DTno4QZdEyAokZHxQZcYrErqMLVE19PgCb"},{"amount":"1","recipientId":"D5xRcwzEGSN83nyuGN74Sw8f353vDmm2tt"},{"amount":"1","recipientId":"DC1hKDKyFbtMhiTc79mmPS99SJnQLsvvH3"},{"amount":"1","recipientId":"DM1pVjbHA3Q4dezcwGBjmT54cLYqpx1NtZ"},{"amount":"1","recipientId":"DFEMw6jihEKRJ9CT3k8Rj73PLKDGDyQLU1"},{"amount":"1","recipientId":"D5nTPSQFkt9W6mNzdy5ks5PiHkhDqswxDY"},{"amount":"1","recipientId":"DMca1DGMxj8w59dWYmji1YC1xLP7AmL6rA"},{"amount":"1","recipientId":"DM6emC4WnxyP2RzrNW5VMS2n3sNfz6Xpci"}]},"id":"c9d689827cd6c6b741ebcad601ce5218bd7df96d2bf592aa5105116b89e09f80","signature":"3045022100a6c327cb6798cb3dc8b282b64358dc4c24a72bff61e347009c1e0ad2a1cc6d8c022028cf62c3d0ea8c3ca6113f63b4e827bd8db8201a478f5ff40c309bbb9987a9b4"})"; + const std::string multiPaymentResponse = R"({"version":2,"network":30,"typeGroup":1,"type":6,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"10000000","amount":"0","asset":{"payments":[{"amount":"1","recipientId":"DHKxXag9PjfjHBbPg3HQS5WCaQZdgDf6yi"},{"amount":"1","recipientId":"DBzGiUk8UVjB2dKCfGRixknB7Ki3Zhqthp"},{"amount":"1","recipientId":"DFa7vn1LvWAyTuVDrQUr5NKaM73cfjx2Cp"},{"amount":"1","recipientId":"DSGsxX84gif4ipAxZjjCE2k2YpHmsNTJeY"},{"amount":"1","recipientId":"DQhzMRvVoCYCiZH2iSyuqCTcayz7z4XTKx"},{"amount":"1","recipientId":"DMSD6fFT1Xcxh4ErYExr5MtGnEuDcYu22m"},{"amount":"1","recipientId":"D7HCZG8hJuJqu9rANRdyNr6N1vpH2vbyx8"},{"amount":"1","recipientId":"DQy6ny2bkvWiDLboQMZ1cxnmoNC5JM228w"},{"amount":"1","recipientId":"D7EUgmA78qUaSfsNedfgKs2ALq28FhL3zo"},{"amount":"1","recipientId":"DEHyKHdtzHqTghfpwaBcvTzLpgPP5AAUgE"},{"amount":"1","recipientId":"DBgA92a616rwVi9GsgYUwBq9Y7dgvZiC41"},{"amount":"1","recipientId":"DPXaJv1GcVpZPvxw5T4fXebqTVhFpfqyrC"},{"amount":"1","recipientId":"D6JpPhN7BehrhNy7AbSQ2u9mkSZb1k7Ens"},{"amount":"1","recipientId":"D9sdJ42YtJpXeL7Fa1cTLiciW7FpGYqms4"},{"amount":"1","recipientId":"DJq86RdmTMKC257szcXRXKpbuoYPaL8KgL"},{"amount":"1","recipientId":"DMsQKioFXniGH3vHDNfkQLRRqsQsAS46Cy"},{"amount":"1","recipientId":"DHwPoAP8cMP9ZeKrhh5c99WzaoJqFKW2qi"},{"amount":"1","recipientId":"DAwN6Pp4ErGf69EypErrbtuWFfEMtuSzmE"},{"amount":"1","recipientId":"DQ6sE3jE9rTFC13e2ndooRdy5YCYinLbPm"},{"amount":"1","recipientId":"DFuo2NGezzHGwHjzG6c21JuJ9WpntLGFER"},{"amount":"1","recipientId":"D6qzeJEGG7rEBem5bNCCZqHtPCBtzsUZpP"},{"amount":"1","recipientId":"DNVhLKTPh4LnqhcnkogNS8iSxmsnFG17tC"},{"amount":"1","recipientId":"D8EPxx42Dr4bd8xXRbMHi8LHefFLaT2VaM"},{"amount":"1","recipientId":"DBK4VPsUQHUYkGc47FqF69PEyPjjqvnGzu"},{"amount":"1","recipientId":"D7XtDDKh2VrRtz5rtbBicfgSEoEQzEZiNx"},{"amount":"1","recipientId":"D9gQjhu2tDUstXfrbK85zHi23VtAk72qsS"},{"amount":"1","recipientId":"DKhfkyY4RZyxR7CFjQAeNtGKXAaVEBa9HK"},{"amount":"1","recipientId":"DMCBerfV13HBuJEwJTZRVTWzrYDxgb3QSy"},{"amount":"1","recipientId":"DLCoxbHdf9LMhEavEnj8mGv4AwVk8eEiKd"},{"amount":"1","recipientId":"D5taz6B4xDk1LD3jV4fYrUhaKC8DnTtziW"},{"amount":"1","recipientId":"DDb3EXY3refv2f5ymMME3hp2DXFqMPzGah"},{"amount":"1","recipientId":"D5HydybffvfuwdbBKQ1dnhiXzNnWq6CgQz"},{"amount":"1","recipientId":"D9DMKvx8fDyWyAP1EUGs5McBwwv3y5E1Yn"},{"amount":"1","recipientId":"DHXqndno9dBvGabhc7NdZWuoj6nRUdSaP7"},{"amount":"1","recipientId":"DJAmJiuLQGnzXWmH7KvosVLks7hhfxQ8np"},{"amount":"1","recipientId":"D752ZwkQZKm5gYYMUZV2tKFFZZaD35MtRa"},{"amount":"1","recipientId":"D6Xe5kVsK7axaPZ1tP2fVWaLFubyCajkVq"},{"amount":"1","recipientId":"D9yDJNK4xHP9Gx27s187Z5XHcNF5YFA94h"},{"amount":"1","recipientId":"DJuZC2smL8j86bUNrZiNceAubad3zs3drS"},{"amount":"1","recipientId":"DRUVFo5MjNrMHHQCwpVPH6AwLL2AULpgbH"},{"amount":"1","recipientId":"DNM5wLmqVUz6UgY14mt6BsMndy8JGcFwct"},{"amount":"1","recipientId":"DHMd77xyB8f6DnSCgxaRWfuM86cwyH76EH"},{"amount":"1","recipientId":"DFmg9q2KqGyretLazvRjWdynJAZbPkZPG2"},{"amount":"1","recipientId":"DMnY8QWLAmsb4wNEjRVvtQsNWF4SbXntxM"},{"amount":"1","recipientId":"DBMn94FxVB36nXgzbmtmfu6jVEGwwHNyNA"},{"amount":"1","recipientId":"DUD3r46LtArk4msu6jFrwn1hjxbZoXzX9t"},{"amount":"1","recipientId":"DFUNVTBd5zFexBaHkymr4UJqsHeXhPLKUF"},{"amount":"1","recipientId":"DFtCxvMSsF9qfw2mH1aNHxusXGJ2QzCahP"},{"amount":"1","recipientId":"D8LiYnmH4DLDyxCLTV7RrqxtCA21pfGkb9"},{"amount":"1","recipientId":"DASUgX1U7yvp8WDQ57QoTEUim6bqTYzxGw"},{"amount":"1","recipientId":"D5iNaf5ZckhdZivPfy6vFvBLeBDJtvDoGo"},{"amount":"1","recipientId":"DPrdeuFDcfMujvYK6n18RBAWgh7hYeiDeZ"},{"amount":"1","recipientId":"D9oaC7bd2YaJYHDdGkUdyAnfpkBrFFKZHy"},{"amount":"1","recipientId":"DUTUfseKR6qJRjqCuxeH3oRxMr6EFLUxRW"},{"amount":"1","recipientId":"DTYv1v3YdUNy81kzD1VhRx6e8jkDYnvCoh"},{"amount":"1","recipientId":"DE1BK8iL17PiBwo9TUCfkkm1vnUkobBwj8"},{"amount":"1","recipientId":"D7Ba6DnbpPJgqVnQNdN7baRsZs1DKvptMM"},{"amount":"1","recipientId":"DUBcre2e5KMykYr6xK56T5BrwkKMZkUF8r"},{"amount":"1","recipientId":"DPeqoTgBbRhyuEJtMnhqhSAeK32ymMNvjd"},{"amount":"1","recipientId":"DGmToX3GrCEUC8EJdZrXWTYFNWqQz1VVhX"},{"amount":"1","recipientId":"DArHKTMXf3F5zXS1i3GSwni9aA8TX1yQvh"},{"amount":"1","recipientId":"DTgcCvAYR2XdzUHv4WuEB6aShYEbh2MYp6"},{"amount":"1","recipientId":"DFLY9huetM6GMrt9EGp6sXDEiC7r3oSYHg"},{"amount":"1","recipientId":"DEszKKqdRipXiF7BDKS2Q4iJwwfzLdwADK"},{"amount":"1","recipientId":"DF45FRKUYcyUGeZyyTF3sCYJ8VFNXymzhJ"},{"amount":"1","recipientId":"DJb6jvhSvw2RmxCBEQSAzG6tjUs5rK3a5m"},{"amount":"1","recipientId":"DCqqT4x4on1dsbgUKRWkuZsdZaoohYK6NV"},{"amount":"1","recipientId":"D9SAVjqkxwWQmb82iqAedJPccFjDUnMSi9"},{"amount":"1","recipientId":"DBvXesEgzrAsm9YrzRb2jithR2hg7SZpuq"},{"amount":"1","recipientId":"DF5ZYcQsmgDvH6cVQR87xvXapyTUFB1a5R"},{"amount":"1","recipientId":"DQEfNNsJ6PQTA9abwWdiunPuebLZAhxbpZ"},{"amount":"1","recipientId":"DP6k5YTtaeNJUHZ72H6QtugFaHVB5vEBQe"},{"amount":"1","recipientId":"DJBTrPo6sDMGr2kcswTwDWtQyYV5adqnAp"},{"amount":"1","recipientId":"DMHtTBMyG5qGYgcZNRkb36XaCT3TUSKmYE"},{"amount":"1","recipientId":"DTbCpyVgTeJw4idpqbY5jwrEi7SxSid9GU"},{"amount":"1","recipientId":"D75g1ztcaHi46eUFRnakRryqG7GV9xsgGC"},{"amount":"1","recipientId":"DSkMiPrEx3YF6ijDjxhwCnbAbriC8sWKEW"},{"amount":"1","recipientId":"D7BHGU3UedoxpZko4nBLcz5oRtSmUmRfy6"},{"amount":"1","recipientId":"DQZUzueTvUJb5tnhBCziPYaMnzaoui4F57"},{"amount":"1","recipientId":"DGCCpnJ86YvxJAkHRPhC5jTBNGsy5PEDRh"},{"amount":"1","recipientId":"DHSW3vi66L63xnzRt9PadwSVrb9bCKhgvJ"},{"amount":"1","recipientId":"D6eAmMh6FFynorCSjHS1Qx75rXiN89soa7"},{"amount":"1","recipientId":"DGPoaSg15fb6As8bPKBAQrK3nCDpgfYow8"},{"amount":"1","recipientId":"DKPmC4G1ZEwxb5hZro2sJwRWJ1pQixcK6N"},{"amount":"1","recipientId":"DFpBcXzcJdFaN9rfVD8Nc5yWFvJ3DnePwa"},{"amount":"1","recipientId":"DKxQ9FDTBDQaLLV24sWc625w2Kycw2RoqR"},{"amount":"1","recipientId":"DNZr7NxGm97r8hV6Jg4rv3S5MgJrEVUWNQ"},{"amount":"1","recipientId":"DBBbtnKKDAW84bDjywEmQFLgwf36DxJHZJ"},{"amount":"1","recipientId":"DA7GfDKG5zYFeiZ1C4FPJBeTajpYNvXxcC"},{"amount":"1","recipientId":"DPAsWQyuxYkiMRzhngKBPTpWb6WWqxdd1T"},{"amount":"1","recipientId":"DTv6qvhUK1jks58gAA5YFzShHf3YX9sJVo"},{"amount":"1","recipientId":"DDwTm5FbgvWugYekvjE1dzantAVKxtGbNo"},{"amount":"1","recipientId":"DTno4QZdEyAokZHxQZcYrErqMLVE19PgCb"},{"amount":"1","recipientId":"D5xRcwzEGSN83nyuGN74Sw8f353vDmm2tt"},{"amount":"1","recipientId":"DC1hKDKyFbtMhiTc79mmPS99SJnQLsvvH3"},{"amount":"1","recipientId":"DM1pVjbHA3Q4dezcwGBjmT54cLYqpx1NtZ"},{"amount":"1","recipientId":"DFEMw6jihEKRJ9CT3k8Rj73PLKDGDyQLU1"},{"amount":"1","recipientId":"D5nTPSQFkt9W6mNzdy5ks5PiHkhDqswxDY"},{"amount":"1","recipientId":"DMca1DGMxj8w59dWYmji1YC1xLP7AmL6rA"},{"amount":"1","recipientId":"DM6emC4WnxyP2RzrNW5VMS2n3sNfz6Xpci"}]},"id":"c9d689827cd6c6b741ebcad601ce5218bd7df96d2bf592aa5105116b89e09f80","signature":"3045022100a6c327cb6798cb3dc8b282b64358dc4c24a72bff61e347009c1e0ad2a1cc6d8c022028cf62c3d0ea8c3ca6113f63b4e827bd8db8201a478f5ff40c309bbb9987a9b4"})"; ASSERT_STREQ(multiPaymentResponse.c_str(), multiPaymentJson.c_str()); #endif // #ifndef USE_IOT Transaction delegateResignationTx; delegateResignationTx.deserialize(TYPE_7_BYTES); const auto delegateResignationJson = delegateResignationTx.toJson(); - const std::string delegateResignationResponse = R"({"version":2,"network":23,"typeGroup":1,"type":7,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"2500000000","id":"a8cf10c6a7d2e5c82bff0860f83f917f426bdd1ee25bd8d5dad2358973ce4ecb","signature":"3045022100aa113ddefb502d868219339d16b8d448c8e69c8e320664e12903cc84420c159d02201b6206c7cb9442f6ef07dd0824aeb0cc393d8f9f9909988da6687c44ef897070"})"; + const std::string delegateResignationResponse = R"({"version":2,"network":23,"typeGroup":1,"type":7,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"2500000000","amount":"0","id":"a8cf10c6a7d2e5c82bff0860f83f917f426bdd1ee25bd8d5dad2358973ce4ecb","signature":"3045022100aa113ddefb502d868219339d16b8d448c8e69c8e320664e12903cc84420c159d02201b6206c7cb9442f6ef07dd0824aeb0cc393d8f9f9909988da6687c44ef897070"})"; ASSERT_STREQ(delegateResignationResponse.c_str(), delegateResignationJson.c_str()); Transaction htlcLockTx; @@ -271,12 +271,12 @@ TEST(transactions_transaction, to_map_to_json) { // NOLINT Transaction htlcClaimTx; htlcClaimTx.deserialize(TYPE_9_BYTES); const auto htlcClaimJson = htlcClaimTx.toJson(); - const std::string htlcClaimResponse = R"({"version":2,"network":23,"typeGroup":1,"type":9,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"0","asset":{"claim":{"lockTransactionId":"50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e","unlockSecret":"f5ea877a311ced90cf4524cb489e972f"}},"id":"88b21734d289fa48801b2fed39e23b89f021df4670e8d43e703b776e5a5333a8","signature":"3045022100d47a071dadebfae4e9115f35ae29429d0ada7bcb1668dc9babde9613108ecc8302201d97dd366a8816f81581dc5be46f3a859b66e48532c80fb7fa19da63dfc3c01f"})"; + const std::string htlcClaimResponse = R"({"version":2,"network":23,"typeGroup":1,"type":9,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"0","amount":"0","asset":{"claim":{"lockTransactionId":"50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e","unlockSecret":"f5ea877a311ced90cf4524cb489e972f"}},"id":"88b21734d289fa48801b2fed39e23b89f021df4670e8d43e703b776e5a5333a8","signature":"3045022100d47a071dadebfae4e9115f35ae29429d0ada7bcb1668dc9babde9613108ecc8302201d97dd366a8816f81581dc5be46f3a859b66e48532c80fb7fa19da63dfc3c01f"})"; ASSERT_STREQ(htlcClaimResponse.c_str(), htlcClaimJson.c_str()); Transaction htlcRefundTx; htlcRefundTx.deserialize(TYPE_10_BYTES); const auto htlcRefundJson = htlcRefundTx.toJson(); - const std::string htlcRefundResponse = R"({"version":2,"network":23,"typeGroup":1,"type":10,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"0","asset":{"refund":{"lockTransactionId":"50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e"}},"id":"ff58b3f891b8395b407d21ee7c2c10aa3fe1303e815677f1fa88b399ff185ff2","signature":"30450221009b2b4d9070095d566b2570ba723c5c1935877e669eaf88f188f0fcac51fa5a4a022022e46ad51939c78b719b6ab37536d5331ea3e414a25d24776b056346fe6feb3c"})"; + const std::string htlcRefundResponse = R"({"version":2,"network":23,"typeGroup":1,"type":10,"nonce":"1","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"0","amount":"0","asset":{"refund":{"lockTransactionId":"50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e"}},"id":"ff58b3f891b8395b407d21ee7c2c10aa3fe1303e815677f1fa88b399ff185ff2","signature":"30450221009b2b4d9070095d566b2570ba723c5c1935877e669eaf88f188f0fcac51fa5a4a022022e46ad51939c78b719b6ab37536d5331ea3e414a25d24776b056346fe6feb3c"})"; ASSERT_STREQ(htlcRefundResponse.c_str(), htlcRefundJson.c_str()); } diff --git a/test/transactions/transaction_v1.cpp b/test/transactions/transaction_v1.cpp index 782ea54d..d8f21449 100644 --- a/test/transactions/transaction_v1.cpp +++ b/test/transactions/transaction_v1.cpp @@ -24,6 +24,6 @@ TEST(transactions_transaction, v1_vendorField) { // NOLINT Transaction transaction; transaction.deserialize(v1::TYPE_0_BYTES_VF); - ASSERT_STREQ(R"({"version":1,"network":23,"type":0,"timestamp":"83506245","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"10000000","vendorField":"Hello World","amount":"1","expiration":0,"recipientId":"AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff","id":"b410f29636882f747608f784be2adf181f30db08a93341df57e9415cfd5b023a","signature":"304402204209261f274cb4d71b0504a3827d9670ac8cca1cb4aab6b3fc55b0b63a9f03e502205cbb25435d7279e536958cf8fa588c86ebe2a00571073a96a79084906ee0d509"})", + ASSERT_STREQ(R"({"version":1,"network":23,"type":0,"timestamp":"83506245","senderPublicKey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","fee":"10000000","amount":"1","vendorField":"Hello World","expiration":0,"recipientId":"AJWRd23HNEhPLkK1ymMnwnDBX2a7QBZqff","id":"b410f29636882f747608f784be2adf181f30db08a93341df57e9415cfd5b023a","signature":"304402204209261f274cb4d71b0504a3827d9670ac8cca1cb4aab6b3fc55b0b63a9f03e502205cbb25435d7279e536958cf8fa588c86ebe2a00571073a96a79084906ee0d509"})", transaction.toJson().c_str()); } From b08f1f12b60bea1f48133a7132ab758a262d86c6 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 13 Feb 2020 08:18:01 -0800 Subject: [PATCH 20/21] release: v1.0.0 --- CHANGELOG.md | 24 +++++++++++++++++++++++- library.json | 2 +- library.properties | 2 +- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65276d6a..4eee352b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [1.0.0] - 2020-02-13 + +## [1.0.0-arduino] - 2020-02-13 + +### Changed +- break up unit tests to support platforms with limited RAM ([#172]) +- removed use of monolithic `arkCrypto.h` header ([#190]) + +### Added +- added AIP-11 support for Core v.2.6 Transactions ([#198]) + +### Fixed +- fixed `transaction::to_array` tests on ESP8266 ([#178]) +- fixed `transaction::to_json` tests on ESP8266 ([#180]) + ## [0.7.0] - 2019-10-08 ## [0.7.0-arduino] - 2019-10-08 @@ -121,5 +136,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. [#121]: https://github.com/ArkEcosystem/cpp-crypto/pull/121 [#133]: https://github.com/ArkEcosystem/cpp-crypto/pull/133 [#156]: https://github.com/ArkEcosystem/cpp-crypto/pull/156 +[0.7.0]: https://github.com/ArkEcosystem/cpp-crypto/compare/0.6.0...0.7.0 [0.7.0-arduino]: https://github.com/ArkEcosystem/cpp-crypto/compare/0.6.0-arduino...0.7.0-arduino -[0.7.0]: https://github.com/ArkEcosystem/cpp-crypto/compare/0.6.0-arduino...0.7.0 +[#172]: https://github.com/ArkEcosystem/cpp-crypto/pull/172 +[#178]: https://github.com/ArkEcosystem/cpp-crypto/pull/178 +[#180]: https://github.com/ArkEcosystem/cpp-crypto/pull/180 +[#190]: https://github.com/ArkEcosystem/cpp-crypto/pull/190 +[#198]: https://github.com/ArkEcosystem/cpp-crypto/pull/198 +[1.0.0]: https://github.com/ArkEcosystem/cpp-crypto/compare/0.7.0-arduino...1.0.0 +[1.0.0-arduino]: https://github.com/ArkEcosystem/cpp-crypto/compare/0.7.0-arduino...1.0.0-arduino diff --git a/library.json b/library.json index b6cefc54..5dbc584c 100644 --- a/library.json +++ b/library.json @@ -7,7 +7,7 @@ "type": "git", "url": "https://github.com/ArkEcosystem/Cpp-Crypto.git" }, - "version": "0.7.0", + "version": "1.0.0", "authors": [ { "name": "Ark Ecosystem", diff --git a/library.properties b/library.properties index 7c55d317..71787bb4 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Ark-Cpp-Crypto -version=0.7.0 +version=1.0.0 author=Ark Ecosystem maintainer=Ark Ecosystem sentence=A simple Cryptography Implementation in C++ for the ARK Blockchain. From 95b72bd053c8934aac06b642ab3239360a7fa499 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 13 Feb 2020 09:24:04 -0800 Subject: [PATCH 21/21] Update platformio.ini --- test/platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/platformio.ini b/test/platformio.ini index 172e216f..18bf7177 100644 --- a/test/platformio.ini +++ b/test/platformio.ini @@ -338,7 +338,7 @@ src_filter = +<../test/transactions/serializer.cpp> ################################################################################ ; Transactions - Transaction -[transactions_transaction] +[transactions_transaction_tests] src_filter = +<../test/transactions/transaction.cpp> ################################################################################