-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathnativeScriptUtils.test.ts
More file actions
415 lines (350 loc) · 11.2 KB
/
nativeScriptUtils.test.ts
File metadata and controls
415 lines (350 loc) · 11.2 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import { describe, it, expect } from "@jest/globals";
import { serializeNativeScript, type NativeScript } from "@meshsdk/core";
import {
type DecodedNativeScript,
decodeNativeScriptFromCbor,
decodedToNativeScript,
collectSigKeyHashes,
isHierarchicalScript,
computeRequiredSigners,
detectTypeFromSigParents,
normalizeCborHex,
countTotalSigs,
getScriptDepth,
} from "../utils/nativeScriptUtils";
// --- Test fixtures ---
const sigA: DecodedNativeScript = { type: "sig", keyHash: "aaaa" };
const sigB: DecodedNativeScript = { type: "sig", keyHash: "bbbb" };
const sigC: DecodedNativeScript = { type: "sig", keyHash: "cccc" };
const flatAll: DecodedNativeScript = {
type: "all",
scripts: [sigA, sigB, sigC],
};
const flatAny: DecodedNativeScript = {
type: "any",
scripts: [sigA, sigB],
};
const flatAtLeast: DecodedNativeScript = {
type: "atLeast",
required: 2,
scripts: [sigA, sigB, sigC],
};
// Hierarchical: all(atLeast(2, [sigA, sigB, sigC]))
const hierarchical: DecodedNativeScript = {
type: "all",
scripts: [
{
type: "atLeast",
required: 2,
scripts: [sigA, sigB, sigC],
},
],
};
// Deeply nested: all(any(all(sigA, sigB)))
const deeplyNested: DecodedNativeScript = {
type: "all",
scripts: [
{
type: "any",
scripts: [
{
type: "all",
scripts: [sigA, sigB],
},
],
},
],
};
const withTimelock: DecodedNativeScript = {
type: "all",
scripts: [
sigA,
{ type: "timelockStart", slot: "100" },
{ type: "timelockExpiry", slot: "999" },
],
};
// --- Tests ---
describe("normalizeCborHex", () => {
it("should strip 0x prefix", () => {
expect(normalizeCborHex("0xabcdef")).toBe("abcdef");
});
it("should strip 0X prefix (uppercase)", () => {
expect(normalizeCborHex("0Xabcdef")).toBe("abcdef");
});
it("should trim whitespace", () => {
expect(normalizeCborHex(" abcdef ")).toBe("abcdef");
});
it("should handle empty string", () => {
expect(normalizeCborHex("")).toBe("");
});
it("should pass through valid hex", () => {
expect(normalizeCborHex("abcdef")).toBe("abcdef");
});
});
describe("collectSigKeyHashes", () => {
it("should collect from a single sig", () => {
expect(collectSigKeyHashes(sigA)).toEqual(["aaaa"]);
});
it("should collect from flat all", () => {
const hashes = collectSigKeyHashes(flatAll);
expect(hashes).toEqual(["aaaa", "bbbb", "cccc"]);
});
it("should collect from hierarchical script", () => {
const hashes = collectSigKeyHashes(hierarchical);
expect(hashes).toEqual(["aaaa", "bbbb", "cccc"]);
});
it("should collect from deeply nested script", () => {
const hashes = collectSigKeyHashes(deeplyNested);
expect(hashes).toEqual(["aaaa", "bbbb"]);
});
it("should deduplicate repeated hashes", () => {
const dup: DecodedNativeScript = {
type: "all",
scripts: [sigA, sigA, sigB],
};
const hashes = collectSigKeyHashes(dup);
expect(hashes).toEqual(["aaaa", "bbbb"]);
});
it("should return empty for timelock nodes", () => {
expect(collectSigKeyHashes({ type: "timelockStart", slot: "0" })).toEqual(
[],
);
});
it("should skip timelocks but collect sigs in mixed scripts", () => {
const hashes = collectSigKeyHashes(withTimelock);
expect(hashes).toEqual(["aaaa"]);
});
});
describe("isHierarchicalScript", () => {
it("should return false for flat all", () => {
expect(isHierarchicalScript(flatAll)).toBe(false);
});
it("should return false for flat any", () => {
expect(isHierarchicalScript(flatAny)).toBe(false);
});
it("should return false for flat atLeast", () => {
expect(isHierarchicalScript(flatAtLeast)).toBe(false);
});
it("should return true for all(atLeast(...))", () => {
expect(isHierarchicalScript(hierarchical)).toBe(true);
});
it("should return true for deeply nested scripts", () => {
expect(isHierarchicalScript(deeplyNested)).toBe(true);
});
it("should return false for a single sig", () => {
expect(isHierarchicalScript(sigA)).toBe(false);
});
});
describe("computeRequiredSigners", () => {
it("should return 1 for a single sig", () => {
expect(computeRequiredSigners(sigA)).toBe(1);
});
it("should return count of all sigs for 'all'", () => {
expect(computeRequiredSigners(flatAll)).toBe(3);
});
it("should return 1 for 'any' with sigs", () => {
expect(computeRequiredSigners(flatAny)).toBe(1);
});
it("should return required count for flat atLeast", () => {
expect(computeRequiredSigners(flatAtLeast)).toBe(2);
});
it("should handle hierarchical: all(atLeast(2, [sig, sig, sig])) = 2", () => {
expect(computeRequiredSigners(hierarchical)).toBe(2);
});
it("should return 0 for timelockStart", () => {
expect(computeRequiredSigners({ type: "timelockStart", slot: "0" })).toBe(
0,
);
});
it("should return 0 for timelockExpiry", () => {
expect(computeRequiredSigners({ type: "timelockExpiry", slot: "0" })).toBe(
0,
);
});
it("should handle all with timelocks (only count sigs)", () => {
// all(sigA, timelockStart, timelockExpiry) -> need sigA = 1
expect(computeRequiredSigners(withTimelock)).toBe(1);
});
it("should return 0 for empty any", () => {
expect(computeRequiredSigners({ type: "any", scripts: [] })).toBe(0);
});
});
describe("detectTypeFromSigParents", () => {
it("should detect 'all' for flat all", () => {
expect(detectTypeFromSigParents(flatAll)).toBe("all");
});
it("should detect 'any' for flat any", () => {
expect(detectTypeFromSigParents(flatAny)).toBe("any");
});
it("should detect 'atLeast' for flat atLeast", () => {
expect(detectTypeFromSigParents(flatAtLeast)).toBe("atLeast");
});
it("should detect 'atLeast' for all(atLeast(...))", () => {
// sigs' parent is atLeast, so atLeast wins
expect(detectTypeFromSigParents(hierarchical)).toBe("atLeast");
});
it("should detect 'all' for deeply nested all(any(all(sig, sig)))", () => {
// sigs' parent is 'all' (innermost), but 'any' and 'all' both appear
// Priority: atLeast > all > any, so 'all' wins
expect(detectTypeFromSigParents(deeplyNested)).toBe("all");
});
});
describe("decodedToNativeScript", () => {
it("should convert sig node", () => {
expect(decodedToNativeScript(sigA)).toEqual({
type: "sig",
keyHash: "aaaa",
});
});
it("should convert flat all", () => {
const result = decodedToNativeScript(flatAll);
expect(result).toEqual({
type: "all",
scripts: [
{ type: "sig", keyHash: "aaaa" },
{ type: "sig", keyHash: "bbbb" },
{ type: "sig", keyHash: "cccc" },
],
});
});
it("should convert flat atLeast with required", () => {
const result = decodedToNativeScript(flatAtLeast);
expect(result).toEqual({
type: "atLeast",
required: 2,
scripts: [
{ type: "sig", keyHash: "aaaa" },
{ type: "sig", keyHash: "bbbb" },
{ type: "sig", keyHash: "cccc" },
],
});
});
it("should convert hierarchical script recursively", () => {
const result = decodedToNativeScript(hierarchical);
expect(result).toEqual({
type: "all",
scripts: [
{
type: "atLeast",
required: 2,
scripts: [
{ type: "sig", keyHash: "aaaa" },
{ type: "sig", keyHash: "bbbb" },
{ type: "sig", keyHash: "cccc" },
],
},
],
});
});
it("should convert timelockStart to 'after'", () => {
const result = decodedToNativeScript({
type: "timelockStart",
slot: "12345",
});
expect(result).toEqual({ type: "after", slot: "12345" });
});
it("should convert timelockExpiry to 'before'", () => {
const result = decodedToNativeScript({
type: "timelockExpiry",
slot: "99999",
});
expect(result).toEqual({ type: "before", slot: "99999" });
});
it("should handle mixed script with timelocks", () => {
const result = decodedToNativeScript(withTimelock);
expect(result).toEqual({
type: "all",
scripts: [
{ type: "sig", keyHash: "aaaa" },
{ type: "after", slot: "100" },
{ type: "before", slot: "999" },
],
});
});
});
describe("decodeNativeScriptFromCbor (integration)", () => {
it("should decode a serialized hierarchical script", () => {
const keyHashA = "11".repeat(28);
const keyHashB = "22".repeat(28);
const keyHashC = "33".repeat(28);
const original: NativeScript = {
type: "all",
scripts: [
{
type: "atLeast",
required: 2,
scripts: [
{ type: "sig", keyHash: keyHashA },
{ type: "sig", keyHash: keyHashB },
{ type: "sig", keyHash: keyHashC },
],
},
],
};
const serialized = serializeNativeScript(original, undefined, 0);
expect(serialized.scriptCbor).toBeDefined();
const scriptCbor = serialized.scriptCbor!;
expect(typeof scriptCbor).toBe("string");
expect(scriptCbor.length).toBeGreaterThan(0);
const decoded = decodeNativeScriptFromCbor(scriptCbor);
expect(decoded.type).toBe("all");
expect(isHierarchicalScript(decoded)).toBe(true);
const hashes = collectSigKeyHashes(decoded);
expect(hashes).toEqual([
keyHashA.toLowerCase(),
keyHashB.toLowerCase(),
keyHashC.toLowerCase(),
]);
const ns = decodedToNativeScript(decoded) as any;
expect(ns.type).toBe("all");
expect(Array.isArray(ns.scripts)).toBe(true);
expect(ns.scripts).toHaveLength(1);
expect(ns.scripts[0].type).toBe("atLeast");
expect(ns.scripts[0].required).toBe(2);
expect(ns.scripts[0].scripts).toHaveLength(3);
});
});
describe("countTotalSigs", () => {
it("should count 1 for a single sig", () => {
expect(countTotalSigs(sigA)).toBe(1);
});
it("should count all sigs in flat script", () => {
expect(countTotalSigs(flatAll)).toBe(3);
});
it("should count sigs in hierarchical script", () => {
expect(countTotalSigs(hierarchical)).toBe(3);
});
it("should count sigs in deeply nested script", () => {
expect(countTotalSigs(deeplyNested)).toBe(2);
});
it("should return 0 for timelocks", () => {
expect(countTotalSigs({ type: "timelockStart", slot: "0" })).toBe(0);
});
it("should count sigs including duplicates", () => {
const dup: DecodedNativeScript = {
type: "all",
scripts: [sigA, sigA, sigB],
};
// countTotalSigs counts leaves, not unique hashes
expect(countTotalSigs(dup)).toBe(3);
});
});
describe("getScriptDepth", () => {
it("should return 0 for a sig node", () => {
expect(getScriptDepth(sigA)).toBe(0);
});
it("should return 1 for flat scripts", () => {
expect(getScriptDepth(flatAll)).toBe(1);
expect(getScriptDepth(flatAny)).toBe(1);
expect(getScriptDepth(flatAtLeast)).toBe(1);
});
it("should return 2 for hierarchical script", () => {
expect(getScriptDepth(hierarchical)).toBe(2);
});
it("should return 3 for deeply nested script", () => {
expect(getScriptDepth(deeplyNested)).toBe(3);
});
it("should return 0 for timelocks", () => {
expect(getScriptDepth({ type: "timelockStart", slot: "0" })).toBe(0);
});
});