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