xrpld
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/SecretKey.h>
14#include <xrpl/protocol/Serializer.h>
15#include <xrpl/protocol/TxFormats.h>
16#include <xrpl/protocol/jss.h>
17
18namespace xrpl::transactions {
19
24template <typename Derived>
26{
27public:
29
31 SF_UINT16::type::value_type transactionType,
32 SF_ACCOUNT::type::value_type account,
35 {
36 // Don't call object_.set(soTemplate) - keep object_ as a free object.
37 // This avoids creating STBase placeholders for soeDEFAULT fields,
38 // which would cause applyTemplate() to throw "may not be explicitly
39 // set to default" when building the STTx.
40 // The STTx constructor will call applyTemplate() which properly
41 // handles missing fields.
42 object_[sfTransactionType] = transactionType;
43 setAccount(account);
44
45 if (sequence)
46 {
47 setSequence(*sequence);
48 }
49 if (fee)
50 {
51 setFee(*fee);
52 }
53 }
54
60 Derived&
61 setAccount(AccountID const& value)
62 {
63 object_[sfAccount] = value;
64 return static_cast<Derived&>(*this);
65 }
66
72 Derived&
73 setFee(STAmount const& value)
74 {
75 object_[sfFee] = value;
76 return static_cast<Derived&>(*this);
77 }
78
84 Derived&
86 {
87 object_[sfSequence] = value;
88 return static_cast<Derived&>(*this);
89 }
90
97 Derived&
99 {
100 object_[sfSequence] = 0u;
101 object_[sfTicketSequence] = value;
102 return static_cast<Derived&>(*this);
103 }
104
110 Derived&
112 {
113 object_[sfFlags] = value;
114 return static_cast<Derived&>(*this);
115 }
116
122 Derived&
124 {
125 object_[sfSourceTag] = value;
126 return static_cast<Derived&>(*this);
127 }
128
134 Derived&
136 {
137 object_[sfLastLedgerSequence] = value;
138 return static_cast<Derived&>(*this);
139 }
140
146 Derived&
148 {
149 object_[sfAccountTxnID] = value;
150 return static_cast<Derived&>(*this);
151 }
152
159 Derived&
161 {
162 object_[sfPreviousTxnID] = value;
163 return static_cast<Derived&>(*this);
164 }
165
171 Derived&
173 {
174 object_[sfOperationLimit] = value;
175 return static_cast<Derived&>(*this);
176 }
177
183 Derived&
184 setMemos(STArray const& value)
185 {
186 object_.setFieldArray(sfMemos, value);
187 return static_cast<Derived&>(*this);
188 }
189
195 Derived&
196 setSigners(STArray const& value)
197 {
198 object_.setFieldArray(sfSigners, value);
199 return static_cast<Derived&>(*this);
200 }
201
207 Derived&
209 {
210 object_[sfNetworkID] = value;
211 return static_cast<Derived&>(*this);
212 }
213
219 Derived&
220 setDelegate(AccountID const& value)
221 {
222 object_[sfDelegate] = value;
223 return static_cast<Derived&>(*this);
224 }
225
230 STObject const&
232 {
233 return object_;
234 }
235
236protected:
246 Derived&
247 sign(PublicKey const& publicKey, SecretKey const& secretKey)
248 {
249 // Set the signing public key
250 object_.setFieldVL(sfSigningPubKey, publicKey.slice());
251
252 // Build the signing data: HashPrefix::TxSign + serialized object
253 // (without signing fields)
254 Serializer s;
256 object_.addWithoutSigningFields(s);
257
258 // Sign and set the signature
259 auto const sig = xrpl::sign(publicKey, secretKey, s.slice());
260 object_.setFieldVL(sfTxnSignature, sig);
261
262 return static_cast<Derived&>(*this);
263 }
264
265 STObject object_{sfTransaction};
266};
267
268} // namespace xrpl::transactions
A public key.
Definition PublicKey.h:53
Slice slice() const noexcept
Definition PublicKey.h:115
A secret key.
Definition SecretKey.h:24
Slice slice() const noexcept
Definition Serializer.h:141
Derived & setDelegate(AccountID const &value)
Set the delegate account for delegated transactions.
Derived & setFee(STAmount const &value)
Set the transaction fee.
Derived & setFlags(std::uint32_t const &value)
Set transaction flags.
Derived & setOperationLimit(std::uint32_t const &value)
Set the operation limit.
Derived & setPreviousTxnID(UInt256 const &value)
Set the previous transaction ID.
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.
Derived & setAccountTxnID(UInt256 const &value)
Set the account transaction ID.
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.
BaseUInt< 256 > UInt256
Definition base_uint.h:581
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
@ TxSign
inner transaction to sign
Definition HashPrefix.h:64
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.