Skip to content

Commit

Permalink
Merge pull request #8 from shawnxie999/cft-maxamt
Browse files Browse the repository at this point in the history
Add MaxAmount guard in MPTIssuanceCreate
  • Loading branch information
gregtatcam authored Jan 9, 2024
2 parents 22909fc + b1513c2 commit d5c3288
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/ripple/app/tx/impl/MPTokenIssuanceCreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,16 @@ MPTokenIssuanceCreate::preflight(PreflightContext const& ctx)
return temMALFORMED;
}

// TODO: check if maximumAmount is within 63 bit range
// Check if maximumAmount is within 63 bit range
if (auto const maxAmt = ctx.tx[~sfMaximumAmount])
{
if (maxAmt == 0)
return temMALFORMED;

// TODO: Improve this check and move the constant elsewhere (STAmount?)
if (maxAmt > 0x7FFFFFFFFFFFFFFFull)
return temMALFORMED;
}
return preflight2(ctx);
}

Expand Down
11 changes: 10 additions & 1 deletion src/test/app/MPToken_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ class MPToken_test : public beast::unit_test::suite
// empty metadata returns error
env(mpt::create(alice, 100, 0, 0, ""), ter(temMALFORMED));
env.close();

// MaximumAmout of 0 returns error
env(mpt::create(alice, 0, 1, 1, "test"), ter(temMALFORMED));
env.close();

// MaximumAmount larger than 63 bit returns errpr
env(mpt::create(alice, 0xFFFFFFFFFFFFFFF0ull, 0, 0, "test"),
ter(temMALFORMED));
env.close();
}
}

Expand All @@ -145,7 +154,7 @@ class MPToken_test : public beast::unit_test::suite
BEAST_EXPECT(env.ownerCount(alice) == 0);

auto const id = getMptID(alice, env.seq(alice));
env(mpt::create(alice, 100, 1, 10, "123"),
env(mpt::create(alice, 0x7FFFFFFFFFFFFFFF, 1, 10, "123"),
txflags(
tfMPTCanLock | tfMPTRequireAuth | tfMPTCanEscrow |
tfMPTCanTrade | tfMPTCanTransfer | tfMPTCanClawback));
Expand Down
15 changes: 13 additions & 2 deletions src/test/jtx/impl/mpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ namespace jtx {

namespace mpt {

static std::array<std::uint8_t, 8>
uint64ToByteArray(std::uint64_t value)
{
value = boost::endian::native_to_big(value);
std::array<std::uint8_t, 8> result;
std::memcpy(result.data(), &value, sizeof(value));
return result;
}

Json::Value
create(jtx::Account const& account)
{
Expand All @@ -39,18 +48,20 @@ create(jtx::Account const& account)
Json::Value
create(
jtx::Account const& account,
std::uint32_t const maxAmt,
std::uint64_t const maxAmt,
std::uint8_t const assetScale,
std::uint16_t transferFee,
std::string metadata)
{
Json::Value jv;
jv[sfAccount.jsonName] = account.human();
jv[sfTransactionType.jsonName] = jss::MPTokenIssuanceCreate;
jv[sfMaximumAmount.jsonName] = maxAmt;
jv[sfAssetScale.jsonName] = assetScale;
jv[sfTransferFee.jsonName] = transferFee;
jv[sfMPTokenMetadata.jsonName] = strHex(metadata);

// convert maxAmt to hex string, since json doesn't accept 64-bit int
jv[sfMaximumAmount.jsonName] = strHex(uint64ToByteArray(maxAmt));
return jv;
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/jtx/mpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ create(jtx::Account const& account);
Json::Value
create(
jtx::Account const& account,
std::uint32_t const maxAmt,
std::uint64_t const maxAmt,
std::uint8_t const assetScale,
std::uint16_t transferFee,
std::string metadata);
Expand Down

0 comments on commit d5c3288

Please sign in to comment.