Skip to content

Commit

Permalink
support AccountPermission
Browse files Browse the repository at this point in the history
  • Loading branch information
yinyiqian1 committed Feb 19, 2025
1 parent 43e1d44 commit b07dd3b
Show file tree
Hide file tree
Showing 92 changed files with 6,743 additions and 467 deletions.
2 changes: 1 addition & 1 deletion include/xrpl/protocol/Feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace detail {
// Feature.cpp. Because it's only used to reserve storage, and determine how
// large to make the FeatureBitset, it MAY be larger. It MUST NOT be less than
// the actual number of amendments. A LogicError on startup will verify this.
static constexpr std::size_t numFeatures = 88;
static constexpr std::size_t numFeatures = 89;

/** Amendments that this server supports and the default voting behavior.
Whether they are enabled depends on the Rules defined in the validated
Expand Down
11 changes: 11 additions & 0 deletions include/xrpl/protocol/Indexes.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@ amm(Asset const& issue1, Asset const& issue2) noexcept;
Keylet
amm(uint256 const& amm) noexcept;

/** An AccountPermission */
/** @{ */
Keylet
accountPermission(
AccountID const& account,
AccountID const& authorizedAccount) noexcept;

Keylet
accountPermission(uint256 const& key) noexcept;
/** @} */

Keylet
bridge(STXChainBridge const& bridge, STXChainBridge::ChainType chainType);

Expand Down
92 changes: 92 additions & 0 deletions include/xrpl/protocol/Permissions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//------------------------------------------------------------------------------
/*
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.
*/
//==============================================================================

#ifndef RIPPLE_PROTOCOL_PERMISSION_H_INCLUDED
#define RIPPLE_PROTOCOL_PERMISSION_H_INCLUDED

#include <xrpl/protocol/STTx.h>
#include <optional>
#include <string>
#include <unordered_map>

namespace ripple {

/**
* We have transaction type permissions and granular type
* permissions. Since we will reuse the TransactionFormats to parse the
* Transaction Permissions, we only define the GranularPermissionType here.
*/

enum GranularPermissionType : std::uint32_t {
TrustlineAuthorize = 65537,

TrustlineFreeze = 65538,

TrustlineUnfreeze = 65539,

AccountDomainSet = 65540,

AccountEmailHashSet = 65541,

AccountMessageKeySet = 65542,

AccountTransferRateSet = 65543,

AccountTickSizeSet = 65544,

PaymentMint = 65545,

PaymentBurn = 65546,

MPTokenIssuanceLock = 65547,

MPTokenIssuanceUnlock = 65548,
};

class Permission
{
private:
Permission();

std::unordered_map<std::string, GranularPermissionType>
granularPermissionMap;

std::unordered_map<GranularPermissionType, TxType> granularTxTypeMap;

public:
static Permission const&
getInstance();

Permission(const Permission&) = delete;
Permission&
operator=(const Permission&) = delete;

std::optional<std::uint32_t>
getGranularValue(std::string const& name) const;

std::optional<TxType>
getGranularTxType(GranularPermissionType const& gpType) const;

bool
isProhibited(std::string const& name) const;
};

} // namespace ripple

#endif
6 changes: 6 additions & 0 deletions include/xrpl/protocol/STTx.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ class STTx final : public STObject, public CountedObject<STTx>
SeqProxy
getSeqProxy() const;

SeqProxy
getDelegateSeqProxy() const;

boost::container::flat_set<AccountID>
getMentionedAccounts() const;

Expand Down Expand Up @@ -139,6 +142,9 @@ class STTx final : public STObject, public CountedObject<STTx>
char status,
std::string const& escapedMetaData) const;

AccountID
getEffectiveAccountID() const;

private:
Expected<void, std::string>
checkSingleSign(RequireFullyCanonicalSig requireCanonicalSig) const;
Expand Down
256 changes: 256 additions & 0 deletions include/xrpl/protocol/STTxDelegated.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
//------------------------------------------------------------------------------
/*
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.
*/
//==============================================================================

#ifndef RIPPLE_PROTOCOL_STTX_WRAPPER_H_INCLUDED
#define RIPPLE_PROTOCOL_STTX_WRAPPER_H_INCLUDED

#include <xrpl/protocol/STAccount.h>
#include <xrpl/protocol/STTx.h>
#include <functional>

namespace ripple {

// This class is a wrapper to deal with delegating in AccountPermission
// amendment. It wraps STTx, and delegates to STTx methods. The key change is
// getAccountID and operator[]. We need to first check if the transaction is
// delegated by another account by checking if the sfOnBehalfOf field is
// present. If it is present, we need to return the sfOnBehalfOf field as the
// account when calling getAccountID(sfAccount) and tx[sfAccount].
class STTxDelegated
{
private:
const STTx& tx_; // Wrap an instance of STTx
bool isDelegated_; // if the transaction is delegated by another account

public:
explicit STTxDelegated(STTx const& tx, bool isDelegated)
: tx_(tx), isDelegated_(isDelegated)
{
}

const STTx&
getSTTx() const
{
return tx_;
}

bool
isDelegated() const
{
return isDelegated_;
}

AccountID
getSenderAccount() const
{
return tx_.getAccountID(sfAccount);
}

std::uint32_t
getEffectiveSeq() const
{
return isDelegated_ ? tx_.getDelegateSeqProxy().value()
: tx_.getSeqProxy().value();
}

AccountID
getAccountID(SField const& field) const
{
if (field == sfAccount)
return tx_.isFieldPresent(sfOnBehalfOf) ? *tx_[~sfOnBehalfOf]
: tx_[sfAccount];
return tx_.getAccountID(field);
}

template <class T>
requires(!std::is_same<TypedField<T>, SF_ACCOUNT>::value)
typename T::value_type
operator[](TypedField<T> const& f) const
{
return tx_[f];
}

// When Type is SF_ACCOUNT and also field name is sfAccount, we need to
// check if the transaction is delegated by another account. If it is,
// return sfOnBehalfOf field instead.
template <class T>
requires std::is_same<TypedField<T>, SF_ACCOUNT>::value
AccountID
operator[](TypedField<T> const& f) const
{
if (f == sfAccount)
return tx_.isFieldPresent(sfOnBehalfOf) ? *tx_[~sfOnBehalfOf]
: tx_[sfAccount];
return tx_[f];
}

template <class T>
std::optional<std::decay_t<typename T::value_type>>
operator[](OptionaledField<T> const& of) const
{
return tx_[of];
}

template <class T>
requires(!std::is_same<TypedField<T>, SF_ACCOUNT>::value)
typename T::value_type
at(TypedField<T> const& f) const
{
return tx_.at(f);
}

// When Type is SF_ACCOUNT and also field name is sfAccount, we need to
// check if the transaction is delegated by another account. If it is,
// return sfOnBehalfOf field instead.
template <class T>
requires std::is_same<TypedField<T>, SF_ACCOUNT>::value
AccountID
at(TypedField<T> const& f) const
{
if (f == sfAccount)
return tx_.isFieldPresent(sfOnBehalfOf) ? *tx_[~sfOnBehalfOf]
: tx_[sfAccount];
return tx_.at(f);
}

template <class T>
std::optional<std::decay_t<typename T::value_type>>
at(OptionaledField<T> const& of) const
{
return tx_.at(of);
}

uint256
getTransactionID() const
{
return tx_.getTransactionID();
}

TxType
getTxnType() const
{
return tx_.getTxnType();
}

std::uint32_t
getFlags() const
{
return tx_.getFlags();
}

bool
isFieldPresent(SField const& field) const
{
return tx_.isFieldPresent(field);
}

Json::Value
getJson(JsonOptions options) const
{
return tx_.getJson(options);
}

void
add(Serializer& s) const
{
tx_.add(s);
}

unsigned char
getFieldU8(SField const& field) const
{
return tx_.getFieldU8(field);
}

std::uint32_t
getFieldU32(SField const& field) const
{
return tx_.getFieldU32(field);
}

uint256
getFieldH256(SField const& field) const
{
return tx_.getFieldH256(field);
}

Blob
getFieldVL(SField const& field) const
{
return tx_.getFieldVL(field);
}

STAmount const&
getFieldAmount(SField const& field) const
{
return tx_.getFieldAmount(field);
}

STPathSet const&
getFieldPathSet(SField const& field) const
{
return tx_.getFieldPathSet(field);
}

const STVector256&
getFieldV256(SField const& field) const
{
return tx_.getFieldV256(field);
}

const STArray&
getFieldArray(SField const& field) const
{
return tx_.getFieldArray(field);
}

Blob
getSigningPubKey() const
{
return tx_.getSigningPubKey();
}

Blob
getSignature() const
{
return tx_.getSignature();
}

bool
isFlag(std::uint32_t f) const
{
return tx_.isFlag(f);
}

SeqProxy
getSeqProxy() const
{
return tx_.getSeqProxy();
}

SeqProxy
getDelegateSeqProxy() const
{
return tx_.getDelegateSeqProxy();
}
};

} // namespace ripple

#endif
Loading

0 comments on commit b07dd3b

Please sign in to comment.