forked from digibyte/digibyte
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathdigidollar_scripts.cpp
More file actions
368 lines (308 loc) · 13.6 KB
/
Copy pathdigidollar_scripts.cpp
File metadata and controls
368 lines (308 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright (c) 2025 The DigiByte Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <chainparams.h>
#include <consensus/amount.h>
#include <digidollar/scripts.h>
#include <digidollar/validation.h>
#include <key.h>
#include <primitives/transaction.h>
#include <pubkey.h>
#include <script/script.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <uint256.h>
#include <util/chaintype.h>
#include <cassert>
#include <cstdint>
#include <vector>
namespace {
void initialize_dd_scripts()
{
ECC_Start();
SelectParams(ChainType::REGTEST);
}
CKey MakeValidKey(FuzzedDataProvider& fdp)
{
CKey key;
auto bytes = fdp.ConsumeBytes<uint8_t>(32);
if (bytes.size() == 32) {
key.Set(bytes.begin(), bytes.end(), true);
}
return key;
}
} // namespace
FUZZ_TARGET(dd_create_scripts, .init = initialize_dd_scripts)
{
FuzzedDataProvider fdp(buffer.data(), buffer.size());
// =========================================================================
// 1. CreateCollateralP2TR with fuzzed MintParams
// =========================================================================
{
DigiDollar::MintParams params;
params.ddAmount = fdp.ConsumeIntegral<CAmount>();
params.lockHeight = fdp.ConsumeIntegral<int64_t>();
CKey ownerKey = MakeValidKey(fdp);
if (ownerKey.IsValid()) {
params.ownerKey = XOnlyPubKey(ownerKey.GetPubKey());
}
// Use NUMS key as internal key (standard for collateral)
params.internalKey = DigiDollar::GetCollateralNUMSKey();
// Add fuzzed oracle keys
int numOracles = fdp.ConsumeIntegralInRange<int>(0, 20);
for (int i = 0; i < numOracles; ++i) {
CKey oKey = MakeValidKey(fdp);
if (oKey.IsValid()) {
params.oracleKeys.push_back(XOnlyPubKey(oKey.GetPubKey()));
}
}
CScript collateralScript = DigiDollar::CreateCollateralP2TR(params);
// If we got a valid script, verify properties
if (!collateralScript.empty()) {
// Should be 34 bytes: OP_1 + 32-byte pubkey
assert(collateralScript.size() == 34);
assert(collateralScript[0] == OP_1);
// Roundtrip: IdentifyScriptType should recognize it
DigiDollar::ScriptType type = DigiDollar::IdentifyScriptType(collateralScript);
// It may or may not be identified depending on metadata registration
(void)type;
// Script metadata roundtrip
DigiDollar::ScriptMetadata meta;
if (DigiDollar::GetScriptMetadata(collateralScript, meta)) {
assert(meta.type == DigiDollar::ScriptType::COLLATERAL_LOCK);
assert(meta.ddAmount == params.ddAmount);
assert(meta.lockHeight == params.lockHeight);
}
}
}
// =========================================================================
// 2. CreateDigiDollarP2TR with fuzzed pubkeys and amounts
// =========================================================================
{
CKey key = MakeValidKey(fdp);
CAmount ddAmount = fdp.ConsumeIntegral<CAmount>();
CScript ddScript;
if (key.IsValid()) {
XOnlyPubKey xonly(key.GetPubKey());
ddScript = DigiDollar::CreateDigiDollarP2TR(xonly, ddAmount);
if (!ddScript.empty()) {
// Should be 34 bytes P2TR
assert(ddScript.size() == 34);
assert(ddScript[0] == OP_1);
// Metadata check
DigiDollar::ScriptMetadata meta;
if (DigiDollar::GetScriptMetadata(ddScript, meta)) {
assert(meta.type == DigiDollar::ScriptType::DD_TOKEN_OUTPUT);
assert(meta.ddAmount == ddAmount);
}
}
}
// Invalid amounts should produce empty scripts
CKey goodKey;
goodKey.MakeNewKey(true);
CScript emptyScript = DigiDollar::CreateDigiDollarP2TR(
XOnlyPubKey(goodKey.GetPubKey()), 0);
assert(emptyScript.empty()); // 0 amount → empty
CScript negScript = DigiDollar::CreateDigiDollarP2TR(
XOnlyPubKey(goodKey.GetPubKey()), -100);
assert(negScript.empty()); // Negative amount → empty
}
// =========================================================================
// 3. CreateNormalRedemptionPath / CreateERRPath
// =========================================================================
{
DigiDollar::MintParams params;
params.ddAmount = fdp.ConsumeIntegral<CAmount>();
params.lockHeight = fdp.ConsumeIntegral<int64_t>();
CKey key = MakeValidKey(fdp);
if (key.IsValid()) {
params.ownerKey = XOnlyPubKey(key.GetPubKey());
}
CScript normalPath = DigiDollar::CreateNormalRedemptionPath(params);
CScript errPath = DigiDollar::CreateERRPath(params);
// Both return empty for invalid params (ddAmount <= 0 or lockHeight < 0)
if (params.ddAmount > 0 && params.lockHeight >= 0 && key.IsValid()) {
// Should have non-empty scripts
if (!normalPath.empty()) {
assert(normalPath.size() > 0);
}
if (!errPath.empty()) {
assert(errPath.size() > 0);
}
}
}
// =========================================================================
// 4. GetOracleKeys
// =========================================================================
{
size_t count = fdp.ConsumeIntegralInRange<size_t>(0, 30);
auto keys = DigiDollar::GetOracleKeys(count);
assert(keys.size() == count);
// All keys should be valid
for (const auto& k : keys) {
assert(k.IsFullyValid());
}
}
// =========================================================================
// 5. GetCollateralNUMSKey
// =========================================================================
{
XOnlyPubKey nums = DigiDollar::GetCollateralNUMSKey();
// NUMS key should be fully valid
assert(nums.IsFullyValid());
}
// =========================================================================
// 6. Script roundtrip: create → metadata → verify
// =========================================================================
{
CKey key;
key.MakeNewKey(true);
CAmount amount = fdp.ConsumeIntegralInRange<CAmount>(1, 1'000'000'000);
// Create DD token script
XOnlyPubKey xonly(key.GetPubKey());
CScript script = DigiDollar::CreateDigiDollarP2TR(xonly, amount);
if (!script.empty()) {
// Extract amount from metadata
DigiDollar::ScriptMetadata meta;
bool found = DigiDollar::GetScriptMetadata(script, meta);
if (found) {
assert(meta.ddAmount == amount);
assert(meta.type == DigiDollar::ScriptType::DD_TOKEN_OUTPUT);
}
}
}
}
FUZZ_TARGET(dd_script_parsing, .init = initialize_dd_scripts)
{
FuzzedDataProvider fdp(buffer.data(), buffer.size());
// =========================================================================
// 1. Feed completely random bytes as scripts to identification functions
// =========================================================================
{
auto raw = fdp.ConsumeBytes<uint8_t>(fdp.ConsumeIntegralInRange<size_t>(0, 200));
CScript script(raw.begin(), raw.end());
// None of these should crash
DigiDollar::ScriptType type = DigiDollar::IdentifyScriptType(script);
(void)type;
CAmount amount = 0;
(void)DigiDollar::ExtractDDAmount(script, amount);
(void)DigiDollar::IsCollateralScript(script);
(void)DigiDollar::IsDDTokenScript(script);
(void)DigiDollar::ExtractLockTime(script);
}
// =========================================================================
// 2. Almost-valid P2TR: OP_1 + fuzzed 32 bytes
// =========================================================================
{
auto fuzzBytes = fdp.ConsumeBytes<uint8_t>(32);
if (fuzzBytes.size() == 32) {
CScript almostP2TR;
almostP2TR << OP_1;
almostP2TR.insert(almostP2TR.end(), 0x20); // push 32 bytes
almostP2TR.insert(almostP2TR.end(), fuzzBytes.begin(), fuzzBytes.end());
DigiDollar::ScriptType type = DigiDollar::IdentifyScriptType(almostP2TR);
(void)type;
CAmount amount = 0;
(void)DigiDollar::ExtractDDAmount(almostP2TR, amount);
(void)DigiDollar::IsCollateralScript(almostP2TR);
(void)DigiDollar::IsDDTokenScript(almostP2TR);
}
}
// =========================================================================
// 3. Truncated scripts - test scripts that are too short
// =========================================================================
{
for (int len = 0; len < 40; ++len) {
auto bytes = fdp.ConsumeBytes<uint8_t>(len);
if (static_cast<int>(bytes.size()) < len) break; // not enough fuzz data
CScript script(bytes.begin(), bytes.end());
// Must not crash on any length
(void)DigiDollar::IdentifyScriptType(script);
CAmount amt = 0;
(void)DigiDollar::ExtractDDAmount(script, amt);
(void)DigiDollar::IsCollateralScript(script);
(void)DigiDollar::IsDDTokenScript(script);
(void)DigiDollar::ExtractLockTime(script);
}
}
// =========================================================================
// 4. Oversized scripts
// =========================================================================
{
size_t bigSize = fdp.ConsumeIntegralInRange<size_t>(100, 10'000);
auto bigBytes = fdp.ConsumeBytes<uint8_t>(bigSize);
CScript bigScript(bigBytes.begin(), bigBytes.end());
(void)DigiDollar::IdentifyScriptType(bigScript);
CAmount amt = 0;
(void)DigiDollar::ExtractDDAmount(bigScript, amt);
(void)DigiDollar::IsCollateralScript(bigScript);
(void)DigiDollar::IsDDTokenScript(bigScript);
}
// =========================================================================
// 5. OP_RETURN metadata scripts (DD format)
// =========================================================================
{
CScript metaScript;
metaScript << OP_RETURN;
// Fuzz the rest of the OP_RETURN content
auto payload = fdp.ConsumeBytes<uint8_t>(fdp.ConsumeIntegralInRange<size_t>(0, 80));
for (const auto& b : payload) {
metaScript << std::vector<unsigned char>{b};
}
CAmount amt = 0;
(void)DigiDollar::ExtractDDAmount(metaScript, amt);
(void)DigiDollar::IdentifyScriptType(metaScript);
}
// =========================================================================
// 6. Transaction-level DD marker checks with fuzzed versions
// =========================================================================
{
CMutableTransaction mtx;
mtx.nVersion = fdp.ConsumeIntegral<int32_t>();
mtx.nLockTime = fdp.ConsumeIntegral<uint32_t>();
CTransaction tx(mtx);
(void)DigiDollar::HasDigiDollarMarker(tx);
try {
(void)DigiDollar::GetDigiDollarTxType(tx);
} catch (...) {
// May throw for non-DD transactions — that's fine
}
}
// =========================================================================
// 7. ValidateNormalRedemption / ValidateERRRedemption with random data
// =========================================================================
{
auto raw = fdp.ConsumeBytes<uint8_t>(fdp.ConsumeIntegralInRange<size_t>(0, 100));
CScript script(raw.begin(), raw.end());
int currentHeight = fdp.ConsumeIntegralInRange<int>(0, 10'000'000);
(void)DigiDollar::ValidateNormalRedemption(script, currentHeight);
int systemCollateral = fdp.ConsumeIntegralInRange<int>(0, 500);
(void)DigiDollar::ValidateERRRedemption(script, systemCollateral);
}
// =========================================================================
// 8. ValidateMintAmount / ValidateOutputAmount with fuzzed values
// =========================================================================
{
const auto& chainParams = Params();
CAmount amount = fdp.ConsumeIntegral<CAmount>();
int nHeight = fdp.ConsumeIntegralInRange<int>(0, 10'000'000);
(void)DigiDollar::ValidateMintAmount(amount, chainParams, nHeight);
(void)DigiDollar::ValidateOutputAmount(amount, chainParams);
}
// =========================================================================
// 9. RegisterScriptMetadata / GetScriptMetadata stress
// =========================================================================
{
auto raw = fdp.ConsumeBytes<uint8_t>(fdp.ConsumeIntegralInRange<size_t>(1, 50));
CScript script(raw.begin(), raw.end());
CAmount ddAmt = fdp.ConsumeIntegral<CAmount>();
int64_t lockH = fdp.ConsumeIntegral<int64_t>();
DigiDollar::RegisterScriptMetadata(script, DigiDollar::ScriptType::COLLATERAL_LOCK, ddAmt, lockH);
DigiDollar::ScriptMetadata meta;
bool found = DigiDollar::GetScriptMetadata(script, meta);
if (found) {
assert(meta.ddAmount == ddAmt);
assert(meta.lockHeight == lockH);
}
}
}