rippled
Loading...
Searching...
No Matches
TransactionBuilderBase.h
1#pragma once
2
3#include <xrpl/protocol/HashPrefix.h>
4#include <xrpl/protocol/PublicKey.h>
5#include <xrpl/protocol/SField.h>
6#include <xrpl/protocol/STAccount.h>
7#include <xrpl/protocol/STAmount.h>
8#include <xrpl/protocol/STArray.h>
9#include <xrpl/protocol/STBlob.h>
10#include <xrpl/protocol/STInteger.h>
11#include <xrpl/protocol/STObject.h>
12#include <xrpl/protocol/STTx.h>
13#include <xrpl/protocol/STXChainBridge.h>
14#include <xrpl/protocol/SecretKey.h>
15#include <xrpl/protocol/Serializer.h>
16#include <xrpl/protocol/TxFormats.h>
17#include <xrpl/protocol/jss.h>
18
19namespace xrpl::transactions {
20
25template <typename Derived>
27{
28public:
30
32 SF_UINT16::type::value_type transactionType,
33 SF_ACCOUNT::type::value_type account,
36 {
37 // Don't call object_.set(soTemplate) - keep object_ as a free object.
38 // This avoids creating STBase placeholders for soeDEFAULT fields,
39 // which would cause applyTemplate() to throw "may not be explicitly
40 // set to default" when building the STTx.
41 // The STTx constructor will call applyTemplate() which properly
42 // handles missing fields.
43 object_[sfTransactionType] = transactionType;
44 setAccount(account);
45
46 if (sequence)
47 {
48 setSequence(*sequence);
49 }
50 if (fee)
51 {
52 setFee(*fee);
53 }
54 }
55
61 Derived&
62 setAccount(AccountID const& value)
63 {
64 object_[sfAccount] = value;
65 return static_cast<Derived&>(*this);
66 }
67
73 Derived&
74 setFee(STAmount const& value)
75 {
76 object_[sfFee] = value;
77 return static_cast<Derived&>(*this);
78 }
79
85 Derived&
87 {
88 object_[sfSequence] = value;
89 return static_cast<Derived&>(*this);
90 }
91
98 Derived&
100 {
101 object_[sfSequence] = 0u;
102 object_[sfTicketSequence] = value;
103 return static_cast<Derived&>(*this);
104 }
105
111 Derived&
113 {
114 object_[sfFlags] = value;
115 return static_cast<Derived&>(*this);
116 }
117
123 Derived&
125 {
126 object_[sfSourceTag] = value;
127 return static_cast<Derived&>(*this);
128 }
129
135 Derived&
137 {
138 object_[sfLastLedgerSequence] = value;
139 return static_cast<Derived&>(*this);
140 }
141
147 Derived&
149 {
150 object_[sfAccountTxnID] = value;
151 return static_cast<Derived&>(*this);
152 }
153
160 Derived&
162 {
163 object_[sfPreviousTxnID] = value;
164 return static_cast<Derived&>(*this);
165 }
166
172 Derived&
174 {
175 object_[sfOperationLimit] = value;
176 return static_cast<Derived&>(*this);
177 }
178
184 Derived&
185 setMemos(STArray const& value)
186 {
187 object_.setFieldArray(sfMemos, value);
188 return static_cast<Derived&>(*this);
189 }
190
196 Derived&
197 setSigners(STArray const& value)
198 {
199 object_.setFieldArray(sfSigners, value);
200 return static_cast<Derived&>(*this);
201 }
202
208 Derived&
210 {
211 object_[sfNetworkID] = value;
212 return static_cast<Derived&>(*this);
213 }
214
220 Derived&
221 setDelegate(AccountID const& value)
222 {
223 object_[sfDelegate] = value;
224 return static_cast<Derived&>(*this);
225 }
226
231 STObject const&
233 {
234 return object_;
235 }
236
237protected:
247 Derived&
248 sign(PublicKey const& publicKey, SecretKey const& secretKey)
249 {
250 // Set the signing public key
251 object_.setFieldVL(sfSigningPubKey, publicKey.slice());
252
253 // Build the signing data: HashPrefix::txSign + serialized object
254 // (without signing fields)
255 Serializer s;
258
259 // Sign and set the signature
260 auto const sig = xrpl::sign(publicKey, secretKey, s.slice());
261 object_.setFieldVL(sfTxnSignature, sig);
262
263 return static_cast<Derived&>(*this);
264 }
265
266 STObject object_{sfTransaction};
267};
268
269} // namespace xrpl::transactions
A public key.
Definition PublicKey.h:42
Slice slice() const noexcept
Definition PublicKey.h:103
void addWithoutSigningFields(Serializer &s) const
Definition STObject.h:971
void setFieldVL(SField const &field, Blob const &)
Definition STObject.cpp:777
void setFieldArray(SField const &field, STArray const &v)
Definition STObject.cpp:819
A secret key.
Definition SecretKey.h:18
Slice slice() const noexcept
Definition Serializer.h:44
Base class for all transaction builders.
Derived & setDelegate(AccountID const &value)
Set the delegate account for delegated transactions.
Derived & setPreviousTxnID(uint256 const &value)
Set the previous transaction ID.
Derived & setFee(STAmount const &value)
Set the transaction fee.
Derived & setFlags(std::uint32_t const &value)
Set transaction flags.
Derived & setAccountTxnID(uint256 const &value)
Set the account transaction ID.
Derived & setOperationLimit(std::uint32_t const &value)
Set the operation limit.
Derived & setTicketSequence(std::uint32_t const &value)
Set the ticket sequence to use for this transaction.
Derived & setNetworkID(std::uint32_t const &value)
Set the network ID.
Derived & setSigners(STArray const &value)
Set the signers array for multi-signing.
Derived & setSequence(std::uint32_t const &value)
Set the sequence number.
STObject const & getSTObject() const
Get the underlying STObject.
Derived & setAccount(AccountID const &value)
Set the account that is sending the transaction.
Derived & setLastLedgerSequence(std::uint32_t const &value)
Set the last ledger sequence.
Derived & setMemos(STArray const &value)
Set the memos array.
Derived & sign(PublicKey const &publicKey, SecretKey const &secretKey)
Sign the transaction with the given keys.
TransactionBuilderBase(SF_UINT16::type::value_type transactionType, SF_ACCOUNT::type::value_type account, std::optional< SF_UINT32::type::value_type > sequence, std::optional< SF_AMOUNT::type::value_type > fee)
Derived & setSourceTag(std::uint32_t const &value)
Set the source tag.
@ txSign
inner transaction to sign
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.