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
// Copyright (c) Mysten Labs, Inc.
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashSet;

use rand::{Rng, rngs::ThreadRng, thread_rng};

/// This library provides two functions to generate
/// a random combination of an adjective
/// and a precious stone name as a well formatted
/// string, or a list of these strings.

/// A list of adjectives
const LEFT_NAMES: [&str; 108] = [
    "admiring",
    "adoring",
    "affectionate",
    "agitated",
    "amazing",
    "angry",
    "awesome",
    "beautiful",
    "blissful",
    "bold",
    "boring",
    "brave",
    "busy",
    "charming",
    "clever",
    "compassionate",
    "competent",
    "condescending",
    "confident",
    "cool",
    "cranky",
    "crazy",
    "dazzling",
    "determined",
    "distracted",
    "dreamy",
    "eager",
    "ecstatic",
    "elastic",
    "elated",
    "elegant",
    "eloquent",
    "epic",
    "exciting",
    "fervent",
    "festive",
    "flamboyant",
    "focused",
    "friendly",
    "frosty",
    "funny",
    "gallant",
    "gifted",
    "goofy",
    "gracious",
    "great",
    "happy",
    "hardcore",
    "heuristic",
    "hopeful",
    "hungry",
    "infallible",
    "inspiring",
    "intelligent",
    "interesting",
    "jolly",
    "jovial",
    "keen",
    "kind",
    "laughing",
    "loving",
    "lucid",
    "magical",
    "modest",
    "musing",
    "mystifying",
    "naughty",
    "nervous",
    "nice",
    "nifty",
    "nostalgic",
    "objective",
    "optimistic",
    "peaceful",
    "pedantic",
    "pensive",
    "practical",
    "priceless",
    "quirky",
    "quizzical",
    "recursing",
    "relaxed",
    "reverent",
    "romantic",
    "sad",
    "serene",
    "sharp",
    "silly",
    "sleepy",
    "stoic",
    "strange",
    "stupefied",
    "suspicious",
    "sweet",
    "tender",
    "thirsty",
    "trusting",
    "unruffled",
    "upbeat",
    "vibrant",
    "vigilant",
    "vigorous",
    "wizardly",
    "wonderful",
    "xenodochial",
    "youthful",
    "zealous",
    "zen",
];

const LEFT_LENGTH: usize = LEFT_NAMES.len();

/// A list of precious stones
const RIGHT_NAMES: [&str; 53] = [
    "agates",
    "alexandrite",
    "amber",
    "amethyst",
    "apatite",
    "avanturine",
    "axinite",
    "beryl",
    "beryl",
    "carnelian",
    "chalcedony",
    "chrysoberyl",
    "chrysolite",
    "chrysoprase",
    "coral",
    "corundum",
    "crocidolite",
    "cyanite",
    "cymophane",
    "diamond",
    "dichroite",
    "emerald",
    "epidote",
    "euclase",
    "felspar",
    "garnet",
    "heliotrope",
    "hematite",
    "hiddenite",
    "hypersthene",
    "idocrase",
    "jasper",
    "jet",
    "labradorite",
    "malachite",
    "moonstone",
    "obsidian",
    "opal",
    "pearl",
    "phenacite",
    "plasma",
    "prase",
    "quartz",
    "ruby",
    "sapphire",
    "sphene",
    "spinel",
    "spodumene",
    "sunstone",
    "topaz",
    "tourmaline",
    "turquois",
    "zircon",
];
const RIGHT_LENGTH: usize = RIGHT_NAMES.len();

/// Return a random name formatted as first-second from a list of strings.
///
/// The main purpose of this function is to generate random aliases for
/// addresses.
pub fn random_name(conflicts: &HashSet<String>) -> String {
    let mut rng = thread_rng();
    // as long as the generated name is in the list of conflicts,
    // we try to find a different name that is not in the list yet
    loop {
        let output = generate(&mut rng);
        if !conflicts.contains(&output) {
            return output;
        }
    }
}

/// Return a unique collection of names.
pub fn random_names(mut conflicts: HashSet<String>, output_size: usize) -> Vec<String> {
    let mut names = Vec::with_capacity(output_size);
    names.resize_with(output_size, || {
        let name = random_name(&conflicts);
        conflicts.insert(name.clone());
        name
    });
    names
}

// Generate a random name as a pair from left and right string arrays
fn generate(rng: &mut ThreadRng) -> String {
    let left_idx = rng.gen_range(0..LEFT_LENGTH);
    let right_idx = rng.gen_range(0..RIGHT_LENGTH);
    format!(
        "{}-{}",
        LEFT_NAMES.get(left_idx).unwrap(),
        RIGHT_NAMES.get(right_idx).unwrap()
    )
}