rippled
SetRegularKey.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/tx/impl/SetRegularKey.h>
21 #include <ripple/basics/Log.h>
22 #include <ripple/protocol/Feature.h>
23 #include <ripple/protocol/TxFlags.h>
24 
25 namespace ripple {
26 
27 XRPAmount
29 {
30  auto const id = tx.getAccountID(sfAccount);
31  auto const spk = tx.getSigningPubKey();
32 
33  if (publicKeyType(makeSlice(spk)))
34  {
35  if (calcAccountID(PublicKey(makeSlice(spk))) == id)
36  {
37  auto const sle = view.read(keylet::account(id));
38 
39  if (sle && (!(sle->getFlags() & lsfPasswordSpent)))
40  {
41  // flag is armed and they signed with the right account
42  return XRPAmount{0};
43  }
44  }
45  }
46 
48 }
49 
50 NotTEC
52 {
53  if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
54  return ret;
55 
56  std::uint32_t const uTxFlags = ctx.tx.getFlags();
57 
58  if (uTxFlags & tfUniversalMask)
59  {
60  JLOG(ctx.j.trace()) << "Malformed transaction: Invalid flags set.";
61 
62  return temINVALID_FLAG;
63  }
64 
68  {
69  return temBAD_REGKEY;
70  }
71 
72  return preflight2(ctx);
73 }
74 
75 TER
77 {
78  auto const sle = view().peek(keylet::account(account_));
79  if (!sle)
80  return tefINTERNAL;
81 
82  if (!minimumFee(ctx_.app, ctx_.baseFee, view().fees(), view().flags()))
83  sle->setFlag(lsfPasswordSpent);
84 
86  {
87  sle->setAccountID(sfRegularKey, ctx_.tx.getAccountID(sfRegularKey));
88  }
89  else
90  {
91  // Account has disabled master key and no multi-signer signer list.
92  if (sle->isFlag(lsfDisableMaster) &&
93  !view().peek(keylet::signers(account_)))
94  return tecNO_ALTERNATIVE_KEY;
95 
96  sle->makeFieldAbsent(sfRegularKey);
97  }
98 
99  ctx_.view().update(sle);
100 
101  return tesSUCCESS;
102 }
103 
104 } // namespace ripple
ripple::preflight2
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:133
ripple::lsfPasswordSpent
@ lsfPasswordSpent
Definition: LedgerFormats.h:254
ripple::makeSlice
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition: Slice.h:241
ripple::Transactor::minimumFee
static XRPAmount minimumFee(Application &app, XRPAmount baseFee, Fees const &fees, ApplyFlags flags)
Compute the minimum fee required to process a transaction with a given baseFee based on the current s...
Definition: Transactor.cpp:183
ripple::tefINTERNAL
@ tefINTERNAL
Definition: TER.h:167
ripple::Rules::enabled
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:94
beast::Journal::trace
Stream trace() const
Severity stream access functions.
Definition: Journal.h:308
ripple::ApplyView::peek
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
ripple::lsfDisableMaster
@ lsfDisableMaster
Definition: LedgerFormats.h:260
ripple::isTesSuccess
bool isTesSuccess(TER x)
Definition: TER.h:637
ripple::ApplyContext::baseFee
const XRPAmount baseFee
Definition: ApplyContext.h:50
ripple::sfRegularKey
const SF_ACCOUNT sfRegularKey
ripple::temBAD_REGKEY
@ temBAD_REGKEY
Definition: TER.h:97
ripple::ApplyView::update
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
ripple::PreflightContext::j
const beast::Journal j
Definition: Transactor.h:38
ripple::preflight1
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:81
ripple::ApplyContext::app
Application & app
Definition: ApplyContext.h:47
ripple::publicKeyType
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:207
ripple::temINVALID_FLAG
@ temINVALID_FLAG
Definition: TER.h:110
ripple::Transactor::calculateBaseFee
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Definition: Transactor.cpp:165
ripple::tecNO_ALTERNATIVE_KEY
@ tecNO_ALTERNATIVE_KEY
Definition: TER.h:277
ripple::PublicKey
A public key.
Definition: PublicKey.h:61
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:142
ripple::STObject::getAccountID
AccountID getAccountID(SField const &field) const
Definition: STObject.cpp:589
ripple::TERSubset
Definition: TER.h:380
ripple::calcAccountID
AccountID calcAccountID(PublicKey const &pk)
Definition: AccountID.cpp:158
ripple::STObject::getFlags
std::uint32_t getFlags() const
Definition: STObject.cpp:481
ripple::fixMasterKeyAsRegularKey
const uint256 fixMasterKeyAsRegularKey
ripple::STTx
Definition: STTx.h:45
ripple::STTx::getSigningPubKey
Blob getSigningPubKey() const
Definition: STTx.h:185
std::uint32_t
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::ApplyContext::view
ApplyView & view()
Definition: ApplyContext.h:54
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:54
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SetRegularKey::preflight
static NotTEC preflight(PreflightContext const &ctx)
Definition: SetRegularKey.cpp:51
ripple::Transactor::view
ApplyView & view()
Definition: Transactor.h:107
ripple::STObject::isFieldPresent
bool isFieldPresent(SField const &field) const
Definition: STObject.cpp:428
ripple::Transactor::ctx_
ApplyContext & ctx_
Definition: Transactor.h:88
ripple::sfAccount
const SF_ACCOUNT sfAccount
ripple::SetRegularKey::doApply
TER doApply() override
Definition: SetRegularKey.cpp:76
ripple::PreflightContext::tx
STTx const & tx
Definition: Transactor.h:35
ripple::PreflightContext
State information when preflighting a tx.
Definition: Transactor.h:31
ripple::keylet::signers
static Keylet signers(AccountID const &account, std::uint32_t page) noexcept
Definition: Indexes.cpp:277
ripple::PreflightContext::rules
const Rules rules
Definition: Transactor.h:36
ripple::tfUniversalMask
constexpr std::uint32_t tfUniversalMask
Definition: TxFlags.h:60
ripple::SetRegularKey::calculateBaseFee
static XRPAmount calculateBaseFee(ReadView const &view, STTx const &tx)
Definition: SetRegularKey.cpp:28
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:236
ripple::Transactor::account_
const AccountID account_
Definition: Transactor.h:91
ripple::ApplyContext::tx
STTx const & tx
Definition: ApplyContext.h:48
ripple::XRPAmount
Definition: XRPAmount.h:46