Skip to content

Commit

Permalink
fix: 0 ARKtoshi amount handling (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepdefic1t authored and faustbrian committed May 21, 2019
1 parent f88815a commit 4ea120b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

### Changed

- properly handle 0 ARKtoshi Transaction amounts.
- improved Windows support ([#83])

## [0.3.1] - 2019-02-19
Expand Down
9 changes: 9 additions & 0 deletions src/include/cpp-crypto/transactions/builder.h
Expand Up @@ -18,8 +18,17 @@ 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 recipientId, uint64_t amount, std::string vendorField,
std::string passphrase, std::string secondPassphrase = "");
/**/

static Transaction buildSecondSignatureRegistration(std::string passphrase, std::string secondPassphrase = "");
static Transaction buildDelegateRegistration(std::string username, std::string passphrase,
std::string secondPassphrase = "");
Expand Down
2 changes: 2 additions & 0 deletions src/transactions/builder.cpp
Expand Up @@ -13,6 +13,8 @@ namespace Transactions {
Transaction Builder::buildTransfer(std::string recipientId, uint64_t amount, std::string vendorField,
std::string passphrase, std::string secondPassphrase) {
Transaction transaction;
if (amount < 1ull) { return transaction; };

transaction.type = Enums::Types::TRANSFER;
transaction.fee = Configuration::Fee().get(Enums::Types::TRANSFER);
transaction.recipientId = std::move(recipientId);
Expand Down
4 changes: 4 additions & 0 deletions src/transactions/transaction.cpp
Expand Up @@ -58,6 +58,8 @@ bool Ark::Crypto::Transactions::Transaction::internalVerify(
std::string publicKey,
std::vector<uint8_t> bytes,
std::string signature) const {
if (bytes.empty()) { return false; };

const auto hash = Sha256::getHash(&bytes[0], bytes.size());
const auto key = Identities::PublicKey::fromHex(publicKey.c_str());
auto signatureBytes = HexToBytes(signature.c_str());
Expand All @@ -69,6 +71,8 @@ std::vector<uint8_t> Ark::Crypto::Transactions::Transaction::toBytes(
bool skipSecondSignature) const {
std::vector<uint8_t> bytes;

if (this->type == 0 && amount < 1ull) { return bytes; };

pack(bytes, this->type);
pack(bytes, this->timestamp);

Expand Down
10 changes: 10 additions & 0 deletions test/transactions/builder.cpp
Expand Up @@ -13,4 +13,14 @@ TEST(transactions, build_transfer) {
ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", actual.recipientId.c_str());
ASSERT_TRUE(100000000 == actual.amount);
ASSERT_STREQ("", actual.vendorField.c_str());

// test 0 ARKtoshi value
const auto shouldBeEmpty = Ark::Crypto::Transactions::Builder::buildTransfer(
"D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
0,
"",
"Secret passphrase");
ASSERT_STREQ("", shouldBeEmpty.recipientId.c_str());
ASSERT_EQ(0, shouldBeEmpty.amount);
ASSERT_FALSE(shouldBeEmpty.verify());
}

0 comments on commit 4ea120b

Please sign in to comment.