xrpld
Loading...
Searching...
No Matches
ConfidentialMPTHolderKeyUpdateTests.cpp
1// Auto-generated unit tests for transaction ConfidentialMPTHolderKeyUpdate
2
3
4#include <gtest/gtest.h>
5
6#include <protocol_autogen/TestHelpers.h>
7
8#include <xrpl/protocol/SecretKey.h>
9#include <xrpl/protocol/Seed.h>
10#include <xrpl/protocol/STTx.h>
11#include <xrpl/protocol_autogen/transactions/ConfidentialMPTHolderKeyUpdate.h>
12#include <xrpl/protocol_autogen/transactions/AccountSet.h>
13
14#include <string>
15
16namespace xrpl::transactions {
17
18// 1 & 4) Set fields via builder setters, build, then read them back via
19// wrapper getters. After build(), validate() should succeed.
20TEST(TransactionsConfidentialMPTHolderKeyUpdateTests, BuilderSettersRoundTrip)
21{
22 // Generate a deterministic keypair for signing
23 auto const [publicKey, secretKey] =
24 generateKeyPair(KeyType::Secp256k1, generateSeed("testConfidentialMPTHolderKeyUpdate"));
25
26 // Common transaction fields
27 auto const accountValue = calcAccountID(publicKey);
28 std::uint32_t const sequenceValue = 1;
29 auto const feeValue = canonical_AMOUNT();
30
31 // Transaction-specific field values
32 auto const mPTokenIssuanceIDValue = canonical_UINT192();
33 auto const holderEncryptionKeyValue = canonical_VL();
34 auto const confidentialBalanceSpendingValue = canonical_VL();
35 auto const confidentialBalanceInboxValue = canonical_VL();
36 auto const zKProofValue = canonical_VL();
37
39 accountValue,
40 mPTokenIssuanceIDValue,
41 sequenceValue,
42 feeValue
43 };
44
45 // Set optional fields
46 builder.setHolderEncryptionKey(holderEncryptionKeyValue);
47 builder.setConfidentialBalanceSpending(confidentialBalanceSpendingValue);
48 builder.setConfidentialBalanceInbox(confidentialBalanceInboxValue);
49 builder.setZKProof(zKProofValue);
50
51 auto tx = builder.build(publicKey, secretKey);
52
53 std::string reason;
54 EXPECT_TRUE(tx.validate(reason)) << reason;
55
56 // Verify signing was applied
57 EXPECT_FALSE(tx.getSigningPubKey().empty());
58 EXPECT_TRUE(tx.hasTxnSignature());
59
60 // Verify common fields
61 EXPECT_EQ(tx.getAccount(), accountValue);
62 EXPECT_EQ(tx.getSequence(), sequenceValue);
63 EXPECT_EQ(tx.getFee(), feeValue);
64
65 // Verify required fields
66 {
67 auto const& expected = mPTokenIssuanceIDValue;
68 auto const actual = tx.getMPTokenIssuanceID();
69 expectEqualField(expected, actual, "sfMPTokenIssuanceID");
70 }
71
72 // Verify optional fields
73 {
74 auto const& expected = holderEncryptionKeyValue;
75 auto const actualOpt = tx.getHolderEncryptionKey();
76 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfHolderEncryptionKey should be present";
77 expectEqualField(expected, *actualOpt, "sfHolderEncryptionKey");
78 EXPECT_TRUE(tx.hasHolderEncryptionKey());
79 }
80
81 {
82 auto const& expected = confidentialBalanceSpendingValue;
83 auto const actualOpt = tx.getConfidentialBalanceSpending();
84 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfConfidentialBalanceSpending should be present";
85 expectEqualField(expected, *actualOpt, "sfConfidentialBalanceSpending");
86 EXPECT_TRUE(tx.hasConfidentialBalanceSpending());
87 }
88
89 {
90 auto const& expected = confidentialBalanceInboxValue;
91 auto const actualOpt = tx.getConfidentialBalanceInbox();
92 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfConfidentialBalanceInbox should be present";
93 expectEqualField(expected, *actualOpt, "sfConfidentialBalanceInbox");
94 EXPECT_TRUE(tx.hasConfidentialBalanceInbox());
95 }
96
97 {
98 auto const& expected = zKProofValue;
99 auto const actualOpt = tx.getZKProof();
100 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfZKProof should be present";
101 expectEqualField(expected, *actualOpt, "sfZKProof");
102 EXPECT_TRUE(tx.hasZKProof());
103 }
104
105}
106
107// 2 & 4) Start from an STTx, construct a builder from it, build a new wrapper,
108// and verify all fields match.
109TEST(TransactionsConfidentialMPTHolderKeyUpdateTests, BuilderFromStTxRoundTrip)
110{
111 // Generate a deterministic keypair for signing
112 auto const [publicKey, secretKey] =
113 generateKeyPair(KeyType::Secp256k1, generateSeed("testConfidentialMPTHolderKeyUpdateFromTx"));
114
115 // Common transaction fields
116 auto const accountValue = calcAccountID(publicKey);
117 std::uint32_t const sequenceValue = 2;
118 auto const feeValue = canonical_AMOUNT();
119
120 // Transaction-specific field values
121 auto const mPTokenIssuanceIDValue = canonical_UINT192();
122 auto const holderEncryptionKeyValue = canonical_VL();
123 auto const confidentialBalanceSpendingValue = canonical_VL();
124 auto const confidentialBalanceInboxValue = canonical_VL();
125 auto const zKProofValue = canonical_VL();
126
127 // Build an initial transaction
129 accountValue,
130 mPTokenIssuanceIDValue,
131 sequenceValue,
132 feeValue
133 };
134
135 initialBuilder.setHolderEncryptionKey(holderEncryptionKeyValue);
136 initialBuilder.setConfidentialBalanceSpending(confidentialBalanceSpendingValue);
137 initialBuilder.setConfidentialBalanceInbox(confidentialBalanceInboxValue);
138 initialBuilder.setZKProof(zKProofValue);
139
140 auto initialTx = initialBuilder.build(publicKey, secretKey);
141
142 // Create builder from existing STTx
143 ConfidentialMPTHolderKeyUpdateBuilder builderFromTx{initialTx.getSTTx()};
144
145 auto rebuiltTx = builderFromTx.build(publicKey, secretKey);
146
147 std::string reason;
148 EXPECT_TRUE(rebuiltTx.validate(reason)) << reason;
149
150 // Verify common fields
151 EXPECT_EQ(rebuiltTx.getAccount(), accountValue);
152 EXPECT_EQ(rebuiltTx.getSequence(), sequenceValue);
153 EXPECT_EQ(rebuiltTx.getFee(), feeValue);
154
155 // Verify required fields
156 {
157 auto const& expected = mPTokenIssuanceIDValue;
158 auto const actual = rebuiltTx.getMPTokenIssuanceID();
159 expectEqualField(expected, actual, "sfMPTokenIssuanceID");
160 }
161
162 // Verify optional fields
163 {
164 auto const& expected = holderEncryptionKeyValue;
165 auto const actualOpt = rebuiltTx.getHolderEncryptionKey();
166 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfHolderEncryptionKey should be present";
167 expectEqualField(expected, *actualOpt, "sfHolderEncryptionKey");
168 }
169
170 {
171 auto const& expected = confidentialBalanceSpendingValue;
172 auto const actualOpt = rebuiltTx.getConfidentialBalanceSpending();
173 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfConfidentialBalanceSpending should be present";
174 expectEqualField(expected, *actualOpt, "sfConfidentialBalanceSpending");
175 }
176
177 {
178 auto const& expected = confidentialBalanceInboxValue;
179 auto const actualOpt = rebuiltTx.getConfidentialBalanceInbox();
180 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfConfidentialBalanceInbox should be present";
181 expectEqualField(expected, *actualOpt, "sfConfidentialBalanceInbox");
182 }
183
184 {
185 auto const& expected = zKProofValue;
186 auto const actualOpt = rebuiltTx.getZKProof();
187 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfZKProof should be present";
188 expectEqualField(expected, *actualOpt, "sfZKProof");
189 }
190
191}
192
193// 3) Verify wrapper throws when constructed from wrong transaction type.
194TEST(TransactionsConfidentialMPTHolderKeyUpdateTests, WrapperThrowsOnWrongTxType)
195{
196 // Build a valid transaction of a different type
197 auto const [pk, sk] =
199 auto const account = calcAccountID(pk);
200
201 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
202 auto wrongTx = wrongBuilder.build(pk, sk);
203
204 EXPECT_THROW(ConfidentialMPTHolderKeyUpdate{wrongTx.getSTTx()}, std::runtime_error);
205}
206
207// 4) Verify builder throws when constructed from wrong transaction type.
208TEST(TransactionsConfidentialMPTHolderKeyUpdateTests, BuilderThrowsOnWrongTxType)
209{
210 // Build a valid transaction of a different type
211 auto const [pk, sk] =
212 generateKeyPair(KeyType::Secp256k1, generateSeed("testWrongTypeBuilder"));
213 auto const account = calcAccountID(pk);
214
215 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
216 auto wrongTx = wrongBuilder.build(pk, sk);
217
218 EXPECT_THROW(ConfidentialMPTHolderKeyUpdateBuilder{wrongTx.getSTTx()}, std::runtime_error);
219}
220
221// 5) Build with only required fields and verify optional fields return nullopt.
222TEST(TransactionsConfidentialMPTHolderKeyUpdateTests, OptionalFieldsReturnNullopt)
223{
224 // Generate a deterministic keypair for signing
225 auto const [publicKey, secretKey] =
226 generateKeyPair(KeyType::Secp256k1, generateSeed("testConfidentialMPTHolderKeyUpdateNullopt"));
227
228 // Common transaction fields
229 auto const accountValue = calcAccountID(publicKey);
230 std::uint32_t const sequenceValue = 3;
231 auto const feeValue = canonical_AMOUNT();
232
233 // Transaction-specific required field values
234 auto const mPTokenIssuanceIDValue = canonical_UINT192();
235
237 accountValue,
238 mPTokenIssuanceIDValue,
239 sequenceValue,
240 feeValue
241 };
242
243 // Do NOT set optional fields
244
245 auto tx = builder.build(publicKey, secretKey);
246
247 // Verify optional fields are not present
248 EXPECT_FALSE(tx.hasHolderEncryptionKey());
249 EXPECT_FALSE(tx.getHolderEncryptionKey().has_value());
250 EXPECT_FALSE(tx.hasConfidentialBalanceSpending());
251 EXPECT_FALSE(tx.getConfidentialBalanceSpending().has_value());
252 EXPECT_FALSE(tx.hasConfidentialBalanceInbox());
253 EXPECT_FALSE(tx.getConfidentialBalanceInbox().has_value());
254 EXPECT_FALSE(tx.hasZKProof());
255 EXPECT_FALSE(tx.getZKProof().has_value());
256}
257
258}
AccountSet build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the AccountSet wrapper.
ConfidentialMPTHolderKeyUpdateBuilder & setConfidentialBalanceInbox(std::decay_t< typename SF_VL::type::value_type > const &value)
Set sfConfidentialBalanceInbox (SoeOptional).
ConfidentialMPTHolderKeyUpdateBuilder & setZKProof(std::decay_t< typename SF_VL::type::value_type > const &value)
Set sfZKProof (SoeOptional).
ConfidentialMPTHolderKeyUpdateBuilder & setHolderEncryptionKey(std::decay_t< typename SF_VL::type::value_type > const &value)
Set sfHolderEncryptionKey (SoeOptional).
ConfidentialMPTHolderKeyUpdateBuilder & setConfidentialBalanceSpending(std::decay_t< typename SF_VL::type::value_type > const &value)
Set sfConfidentialBalanceSpending (SoeOptional).
ConfidentialMPTHolderKeyUpdate build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the ConfidentialMPTHolderKeyUpdate wrapper.
TEST(TransactionsAccountDeleteTests, BuilderSettersRoundTrip)
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:58
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
AccountID calcAccountID(PublicKey const &pk)
void expectEqualField(T const &expected, T const &actual, char const *fieldName)