-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvanity-validation.test.js
More file actions
67 lines (59 loc) · 2 KB
/
vanity-validation.test.js
File metadata and controls
67 lines (59 loc) · 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
import { describe, it, expect } from 'vitest';
import {
validatePattern,
estimateAttempts,
formatTimeEstimate,
BASE58_ALPHABET,
MAX_PATTERN_LENGTH,
} from '../../src/solana/vanity/validation.js';
describe('vanity validation', () => {
it('accepts valid base58 prefixes', () => {
expect(validatePattern('AGNT').valid).toBe(true);
expect(validatePattern('So').valid).toBe(true);
expect(validatePattern('123').valid).toBe(true);
});
it('rejects empty string', () => {
expect(validatePattern('').valid).toBe(false);
});
it('rejects each confused character with a hint', () => {
for (const c of ['0', 'O', 'I', 'l']) {
const r = validatePattern(c);
expect(r.valid).toBe(false);
expect(r.errors.join(' ')).toMatch(c);
}
});
it('rejects patterns longer than the maximum', () => {
const tooLong = 'A'.repeat(MAX_PATTERN_LENGTH + 1);
expect(validatePattern(tooLong).valid).toBe(false);
});
it('rejects whitespace', () => {
expect(validatePattern(' AB').valid).toBe(false);
expect(validatePattern('AB ').valid).toBe(false);
});
it('alphabet excludes confused chars', () => {
expect(BASE58_ALPHABET).not.toMatch(/[0OIl]/);
expect(BASE58_ALPHABET.length).toBe(58);
});
});
describe('estimateAttempts', () => {
it('returns 58^length', () => {
expect(estimateAttempts(1)).toBe(58);
expect(estimateAttempts(2)).toBe(58 * 58);
expect(estimateAttempts(4)).toBe(Math.pow(58, 4));
});
});
describe('formatTimeEstimate', () => {
it('returns unknown for zero rate', () => {
expect(formatTimeEstimate(1000, 0)).toBe('unknown');
});
it('formats sub-second', () => {
expect(formatTimeEstimate(10, 1000)).toBe('less than a second');
});
it('formats seconds/minutes/hours/days/years', () => {
expect(formatTimeEstimate(30, 1)).toMatch(/seconds/);
expect(formatTimeEstimate(300, 1)).toMatch(/minutes/);
expect(formatTimeEstimate(7200, 1)).toMatch(/hours/);
expect(formatTimeEstimate(86400 * 3, 1)).toMatch(/days/);
expect(formatTimeEstimate(31536000 * 2, 1)).toMatch(/years/);
});
});