Skip to content

Commit 4ea120b

Browse files
sleepdefic1tfaustbrian
authored andcommitted
fix: 0 ARKtoshi amount handling (#85)
1 parent f88815a commit 4ea120b

5 files changed

Lines changed: 28 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515

1616
### Changed
1717

18+
### Changed
19+
20+
- properly handle 0 ARKtoshi Transaction amounts.
1821
- improved Windows support ([#83])
1922

2023
## [0.3.1] - 2019-02-19

src/include/cpp-crypto/transactions/builder.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@ namespace Transactions {
1818

1919
class Builder {
2020
public:
21+
/**
22+
* Builder::buildTransfer()
23+
*
24+
* note: The 'Type 0'/Transfer amount must be greater than 0 ARKtoshi.
25+
* If amount is not > 0, Builder will return an empty Transaction object and
26+
* validation will fail.
27+
**/
2128
static Transaction buildTransfer(std::string recipientId, uint64_t amount, std::string vendorField,
2229
std::string passphrase, std::string secondPassphrase = "");
30+
/**/
31+
2332
static Transaction buildSecondSignatureRegistration(std::string passphrase, std::string secondPassphrase = "");
2433
static Transaction buildDelegateRegistration(std::string username, std::string passphrase,
2534
std::string secondPassphrase = "");

src/transactions/builder.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace Transactions {
1313
Transaction Builder::buildTransfer(std::string recipientId, uint64_t amount, std::string vendorField,
1414
std::string passphrase, std::string secondPassphrase) {
1515
Transaction transaction;
16+
if (amount < 1ull) { return transaction; };
17+
1618
transaction.type = Enums::Types::TRANSFER;
1719
transaction.fee = Configuration::Fee().get(Enums::Types::TRANSFER);
1820
transaction.recipientId = std::move(recipientId);

src/transactions/transaction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ bool Ark::Crypto::Transactions::Transaction::internalVerify(
5858
std::string publicKey,
5959
std::vector<uint8_t> bytes,
6060
std::string signature) const {
61+
if (bytes.empty()) { return false; };
62+
6163
const auto hash = Sha256::getHash(&bytes[0], bytes.size());
6264
const auto key = Identities::PublicKey::fromHex(publicKey.c_str());
6365
auto signatureBytes = HexToBytes(signature.c_str());
@@ -69,6 +71,8 @@ std::vector<uint8_t> Ark::Crypto::Transactions::Transaction::toBytes(
6971
bool skipSecondSignature) const {
7072
std::vector<uint8_t> bytes;
7173

74+
if (this->type == 0 && amount < 1ull) { return bytes; };
75+
7276
pack(bytes, this->type);
7377
pack(bytes, this->timestamp);
7478

test/transactions/builder.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ TEST(transactions, build_transfer) {
1313
ASSERT_STREQ("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib", actual.recipientId.c_str());
1414
ASSERT_TRUE(100000000 == actual.amount);
1515
ASSERT_STREQ("", actual.vendorField.c_str());
16+
17+
// test 0 ARKtoshi value
18+
const auto shouldBeEmpty = Ark::Crypto::Transactions::Builder::buildTransfer(
19+
"D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
20+
0,
21+
"",
22+
"Secret passphrase");
23+
ASSERT_STREQ("", shouldBeEmpty.recipientId.c_str());
24+
ASSERT_EQ(0, shouldBeEmpty.amount);
25+
ASSERT_FALSE(shouldBeEmpty.verify());
1626
}

0 commit comments

Comments
 (0)