Skip to content

Commit

Permalink
add sfTxIDs and update signing
Browse files Browse the repository at this point in the history
  • Loading branch information
dangell7 committed Sep 18, 2024
1 parent 122368f commit 274660b
Show file tree
Hide file tree
Showing 14 changed files with 695 additions and 790 deletions.
37 changes: 37 additions & 0 deletions include/xrpl/protocol/Batch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2024 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#include <xrpl/protocol/HashPrefix.h>
#include <xrpl/protocol/Serializer.h>
#include <xrpl/protocol/STVector256.h>

namespace ripple {

inline void
serializeBatch(
Serializer& msg,
std::uint32_t const& flags,
STVector256 const& txids)
{
msg.add32(HashPrefix::batch);
msg.add32(flags);
msg.add32(txids.size());
for (auto const& txid : txids)
msg.addBitString(txid);
}

} // namespace ripple
3 changes: 3 additions & 0 deletions include/xrpl/protocol/HashPrefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ enum class HashPrefix : std::uint32_t {

/** Payment Channel Claim */
paymentChannelClaim = detail::make_hash_prefix('C', 'L', 'M'),

/** Batch */
batch = detail::make_hash_prefix('B', 'C', 'H'),
};

template <class Hasher>
Expand Down
1 change: 1 addition & 0 deletions include/xrpl/protocol/SField.h
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ extern SF_VECTOR256 const sfIndexes;
extern SF_VECTOR256 const sfHashes;
extern SF_VECTOR256 const sfAmendments;
extern SF_VECTOR256 const sfNFTokenOffers;
extern SF_VECTOR256 const sfTxIDs;

// inner object
// OBJECT/1 is reserved for end of object
Expand Down
1 change: 1 addition & 0 deletions src/libxrpl/protocol/SField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ CONSTRUCT_TYPED_SFIELD(sfIndexes, "Indexes", VECTOR25
CONSTRUCT_TYPED_SFIELD(sfHashes, "Hashes", VECTOR256, 2);
CONSTRUCT_TYPED_SFIELD(sfAmendments, "Amendments", VECTOR256, 3);
CONSTRUCT_TYPED_SFIELD(sfNFTokenOffers, "NFTokenOffers", VECTOR256, 4);
CONSTRUCT_TYPED_SFIELD(sfTxIDs, "TxIDs", VECTOR256, 5);

// path set
CONSTRUCT_UNTYPED_SFIELD(sfPaths, "Paths", PATHSET, 1);
Expand Down
26 changes: 8 additions & 18 deletions src/libxrpl/protocol/STTx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
#include <xrpl/basics/contract.h>
#include <xrpl/basics/safe_cast.h>
#include <xrpl/json/to_string.h>
#include <xrpl/protocol/Batch.h>
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/HashPrefix.h>
#include <xrpl/protocol/Protocol.h>
#include <xrpl/protocol/PublicKey.h>
#include <xrpl/protocol/STAccount.h>
#include <xrpl/protocol/STArray.h>
#include <xrpl/protocol/STVector256.h>
#include <xrpl/protocol/STTx.h>
#include <xrpl/protocol/Sign.h>
#include <xrpl/protocol/TxFlags.h>
Expand Down Expand Up @@ -400,27 +402,21 @@ STTx::checkBatchSingleSign(
if (batchSigner.isFieldPresent(sfSigners))
return Unexpected("Cannot both single- and multi-sign.");

Serializer const dataStart{startMultiSigningData(*this)};

auto const accountID = batchSigner.getAccountID(sfAccount);

bool validSig = false;
try
{
Serializer s = dataStart;
finishMultiSigningData(accountID, s);

Serializer msg;
serializeBatch(msg, getFlags(), getFieldV256(sfTxIDs));
bool const fullyCanonical = (getFlags() & tfFullyCanonicalSig) ||
(requireCanonicalSig == RequireFullyCanonicalSig::yes);

auto const spk = batchSigner.getFieldVL(sfSigningPubKey);
if (publicKeyType(makeSlice(spk)))
{
Blob const signature = batchSigner.getFieldVL(sfTxnSignature);

validSig = verify(
PublicKey(makeSlice(spk)),
s.slice(),
msg.slice(),
makeSlice(signature),
fullyCanonical);
}
Expand Down Expand Up @@ -459,10 +455,8 @@ STTx::checkBatchMultiSign(
signers.size() > maxMultiSigners(&rules))
return Unexpected("Invalid Signers array size.");

// We can ease the computational load inside the loop a bit by
// pre-constructing part of the data that we hash. Fill a Serializer
// with the stuff that stays constant from signature to signature.
Serializer const dataStart{startMultiSigningData(*this)};
Serializer msg;
serializeBatch(msg, getFlags(), getFieldV256(sfTxIDs));

// We also use the sfAccount field inside the loop. Get it once.
auto const txnAccountID = batchSigner.getAccountID(sfAccount);
Expand Down Expand Up @@ -497,18 +491,14 @@ STTx::checkBatchMultiSign(
bool validSig = false;
try
{
Serializer s = dataStart;
finishMultiSigningData(accountID, s);

auto spk = signer.getFieldVL(sfSigningPubKey);

if (publicKeyType(makeSlice(spk)))
{
Blob const signature = signer.getFieldVL(sfTxnSignature);

validSig = verify(
PublicKey(makeSlice(spk)),
s.slice(),
msg.slice(),
makeSlice(signature),
fullyCanonical);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libxrpl/protocol/TER.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ transResults()
MAKE_ERROR(temXCHAIN_BRIDGE_BAD_REWARD_AMOUNT, "Malformed: Bad reward amount."),
MAKE_ERROR(temARRAY_EMPTY, "Malformed: Array is empty."),
MAKE_ERROR(temARRAY_TOO_LARGE, "Malformed: Array is too large."),
MAKE_ERROR(temINVALID_BATCH, "The transaction cannot contain BatchTxn."),
MAKE_ERROR(temINVALID_BATCH, "Invalid inner batch transaction type."),

MAKE_ERROR(terRETRY, "Retry transaction."),
MAKE_ERROR(terFUNDS_SPENT, "DEPRECATED."),
Expand Down
1 change: 1 addition & 0 deletions src/libxrpl/protocol/TxFormats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ TxFormats::TxFormats()
ttBATCH,
{
{sfRawTransactions, soeREQUIRED},
{sfTxIDs, soeREQUIRED},
{sfBatchSigners, soeOPTIONAL},
},
commonFields);
Expand Down
Loading

0 comments on commit 274660b

Please sign in to comment.