rippled
Loading...
Searching...
No Matches
SetRegularKey_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2016 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 <test/jtx.h>
21#include <xrpl/protocol/Feature.h>
22
23namespace ripple {
24
26{
27public:
28 void
30 {
31 using namespace test::jtx;
32
33 testcase("Set regular key");
34 Env env{*this, supported_amendments() - fixMasterKeyAsRegularKey};
35 Account const alice("alice");
36 Account const bob("bob");
37 env.fund(XRP(10000), alice, bob);
38
39 env(regkey(alice, bob));
40 auto const ar = env.le(alice);
41 BEAST_EXPECT(
42 ar->isFieldPresent(sfRegularKey) &&
43 (ar->getAccountID(sfRegularKey) == bob.id()));
44
45 env(noop(alice), sig(bob));
46 env(noop(alice), sig(alice));
47
48 testcase("Disable master key");
49 env(fset(alice, asfDisableMaster), sig(alice));
50 env(noop(alice), sig(bob));
51 env(noop(alice), sig(alice), ter(tefMASTER_DISABLED));
52
53 testcase("Re-enable master key");
54 env(fclear(alice, asfDisableMaster),
55 sig(alice),
57
58 env(fclear(alice, asfDisableMaster), sig(bob));
59 env(noop(alice), sig(bob));
60 env(noop(alice), sig(alice));
61
62 testcase("Revoke regular key");
63 env(regkey(alice, disabled));
64 env(noop(alice), sig(bob), ter(tefBAD_AUTH_MASTER));
65 env(noop(alice), sig(alice));
66 }
67
68 void
70 {
71 using namespace test::jtx;
72
73 testcase("Set regular key");
74 Env env{*this, supported_amendments() | fixMasterKeyAsRegularKey};
75 Account const alice("alice");
76 Account const bob("bob");
77 env.fund(XRP(10000), alice, bob);
78
79 env(regkey(alice, bob));
80 env(noop(alice), sig(bob));
81 env(noop(alice), sig(alice));
82
83 testcase("Disable master key");
84 env(fset(alice, asfDisableMaster), sig(alice));
85 env(noop(alice), sig(bob));
86 env(noop(alice), sig(alice), ter(tefMASTER_DISABLED));
87
88 testcase("Re-enable master key");
89 env(fclear(alice, asfDisableMaster),
90 sig(alice),
92
93 env(fclear(alice, asfDisableMaster), sig(bob));
94 env(noop(alice), sig(bob));
95 env(noop(alice), sig(alice));
96
97 testcase("Revoke regular key");
98 env(regkey(alice, disabled));
99 env(noop(alice), sig(bob), ter(tefBAD_AUTH));
100 env(noop(alice), sig(alice));
101 }
102
103 void
105 {
106 using namespace test::jtx;
107
108 // See https://ripplelabs.atlassian.net/browse/RIPD-1721.
109 testcase(
110 "Set regular key to master key (before fixMasterKeyAsRegularKey)");
111 Env env{*this, supported_amendments() - fixMasterKeyAsRegularKey};
112 Account const alice("alice");
113 env.fund(XRP(10000), alice);
114
115 // Must be possible unless amendment `fixMasterKeyAsRegularKey` enabled.
116 env(regkey(alice, alice), sig(alice));
117 env(fset(alice, asfDisableMaster), sig(alice));
118
119 // No way to sign...
120 env(noop(alice), ter(tefMASTER_DISABLED));
121 env(noop(alice), sig(alice), ter(tefMASTER_DISABLED));
122
123 // ... until now.
124 env.enableFeature(fixMasterKeyAsRegularKey);
125 env(noop(alice));
126 env(noop(alice), sig(alice));
127
128 env(regkey(alice, disabled), ter(tecNO_ALTERNATIVE_KEY));
129 env(fclear(alice, asfDisableMaster));
130 env(regkey(alice, disabled));
131 env(fset(alice, asfDisableMaster), ter(tecNO_ALTERNATIVE_KEY));
132 }
133
134 void
136 {
137 using namespace test::jtx;
138
139 testcase(
140 "Set regular key to master key (after fixMasterKeyAsRegularKey)");
141 Env env{*this, supported_amendments() | fixMasterKeyAsRegularKey};
142 Account const alice("alice");
143 env.fund(XRP(10000), alice);
144
145 // Must be possible unless amendment `fixMasterKeyAsRegularKey` enabled.
146 env(regkey(alice, alice), ter(temBAD_REGKEY));
147 }
148
149 void
151 {
152 using namespace test::jtx;
153
154 testcase("Password spent");
155 Env env(*this);
156 Account const alice("alice");
157 Account const bob("bob");
158 env.fund(XRP(10000), alice, bob);
159
160 auto ar = env.le(alice);
161 BEAST_EXPECT(
162 ar->isFieldPresent(sfFlags) &&
163 ((ar->getFieldU32(sfFlags) & lsfPasswordSpent) == 0));
164
165 env(regkey(alice, bob), sig(alice), fee(0));
166
167 ar = env.le(alice);
168 BEAST_EXPECT(
169 ar->isFieldPresent(sfFlags) &&
170 ((ar->getFieldU32(sfFlags) & lsfPasswordSpent) ==
172
173 // The second SetRegularKey transaction with Fee=0 should fail.
174 env(regkey(alice, bob), sig(alice), fee(0), ter(telINSUF_FEE_P));
175
176 env.trust(bob["USD"](1), alice);
177 env(pay(bob, alice, bob["USD"](1)));
178 ar = env.le(alice);
179 BEAST_EXPECT(
180 ar->isFieldPresent(sfFlags) &&
181 ((ar->getFieldU32(sfFlags) & lsfPasswordSpent) == 0));
182 }
183
184 void
186 {
187 using namespace test::jtx;
188
189 testcase("Universal mask");
190 Env env(*this);
191 Account const alice("alice");
192 Account const bob("bob");
193 env.fund(XRP(10000), alice, bob);
194
195 auto jv = regkey(alice, bob);
196 jv[sfFlags.fieldName] = tfUniversalMask;
197 env(jv, ter(temINVALID_FLAG));
198 }
199
200 void
202 {
203 using namespace test::jtx;
204
205 testcase("Ticket regular key");
206 Env env{*this};
207 Account const alice{"alice", KeyType::ed25519};
208 env.fund(XRP(1000), alice);
209 env.close();
210
211 // alice makes herself some tickets.
212 env(ticket::create(alice, 4));
213 env.close();
214 std::uint32_t ticketSeq{env.seq(alice)};
215
216 // Make sure we can give a regular key using a ticket.
217 Account const alie{"alie", KeyType::secp256k1};
218 env(regkey(alice, alie), ticket::use(--ticketSeq));
219 env.close();
220
221 // Disable alice's master key using a ticket.
222 env(fset(alice, asfDisableMaster),
223 sig(alice),
224 ticket::use(--ticketSeq));
225 env.close();
226
227 // alice should be able to sign using the regular key but not the
228 // master key.
229 std::uint32_t const aliceSeq{env.seq(alice)};
230 env(noop(alice), sig(alice), ter(tefMASTER_DISABLED));
231 env(noop(alice), sig(alie), ter(tesSUCCESS));
232 env.close();
233 BEAST_EXPECT(env.seq(alice) == aliceSeq + 1);
234
235 // Re-enable the master key using a ticket.
236 env(fclear(alice, asfDisableMaster),
237 sig(alie),
238 ticket::use(--ticketSeq));
239 env.close();
240
241 // Disable the regular key using a ticket.
242 env(regkey(alice, disabled), sig(alie), ticket::use(--ticketSeq));
243 env.close();
244
245 // alice should be able to sign using the master key but not the
246 // regular key.
247 env(noop(alice), sig(alice), ter(tesSUCCESS));
248 env(noop(alice), sig(alie), ter(tefBAD_AUTH));
249 env.close();
250 }
251
252 void
253 run() override
254 {
262 }
263};
264
265BEAST_DEFINE_TESTSUITE(SetRegularKey, app, ripple);
266
267} // namespace ripple
A testsuite class.
Definition: suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:155
void run() override
Runs the suite.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
@ telINSUF_FEE_P
Definition: TER.h:57
@ lsfPasswordSpent
constexpr std::uint32_t asfDisableMaster
Definition: TxFlags.h:79
@ tefBAD_AUTH_MASTER
Definition: TER.h:182
@ tefBAD_AUTH
Definition: TER.h:169
@ tefMASTER_DISABLED
Definition: TER.h:177
@ tecNO_ALTERNATIVE_KEY
Definition: TER.h:283
@ tesSUCCESS
Definition: TER.h:242
constexpr std::uint32_t tfUniversalMask
Definition: TxFlags.h:62
@ temBAD_REGKEY
Definition: TER.h:98
@ temINVALID_FLAG
Definition: TER.h:111