|
| 1 | +## Mnemonic Class |
| 2 | + |
| 3 | +### Namespace |
| 4 | + |
| 5 | +```php |
| 6 | +namespace MoneroIntegrations\MoneroCrypto; |
| 7 | +``` |
| 8 | + |
| 9 | +### Class: Mnemonic |
| 10 | + |
| 11 | +The `Mnemonic` class provides methods for encoding, decoding, validating checksums, and managing wordsets for Monero wallets. |
| 12 | + |
| 13 | +#### Method: `checksum` |
| 14 | + |
| 15 | +Given a mnemonic seed word list, return the seed checksum. |
| 16 | + |
| 17 | +```php |
| 18 | +/** |
| 19 | + * Given a mnemonic seed word list, return the seed checksum. |
| 20 | + * |
| 21 | + * @param array<string> $words |
| 22 | + * @param int $prefix_len |
| 23 | + * @return string |
| 24 | + */ |
| 25 | +public static function checksum(array $words, int $prefix_len): string |
| 26 | +``` |
| 27 | + |
| 28 | +##### Example: |
| 29 | + |
| 30 | +```php |
| 31 | +$seed = [ |
| 32 | + "sighting", "pavements", "mocked", "dilute", |
| 33 | + "lunar", "king", "bygones", "niece", "tonic", |
| 34 | + "noises", "ostrich", "ecstatic", "hoax", "gawk", |
| 35 | + "bays", "wiring", "total", "emulate", "update", |
| 36 | + "bypass", "asked", "pager", "geometry", "haystack", |
| 37 | + "geometry" |
| 38 | +]; |
| 39 | +$prefixLen = 3; |
| 40 | +$checksum = Mnemonic::checksum($seed, $prefixLen); // Returns "geometry" |
| 41 | +``` |
| 42 | + |
| 43 | +#### Method: `validateChecksum` |
| 44 | + |
| 45 | +Given a mnemonic seed word list, check if checksum word is valid. |
| 46 | + |
| 47 | +```php |
| 48 | +/** |
| 49 | + * Given a mnemonic seed word list, check if checksum word is valid. |
| 50 | + * |
| 51 | + * @param array<string> $words |
| 52 | + * @param int $prefix_len |
| 53 | + * @return bool |
| 54 | + */ |
| 55 | +public static function validateChecksum(array $words, int $prefix_len): bool |
| 56 | +``` |
| 57 | + |
| 58 | +Example: |
| 59 | + |
| 60 | +```php |
| 61 | +$isValid = Mnemonic::validateChecksum($seed, $prefixLen); // Returns true |
| 62 | +``` |
| 63 | + |
| 64 | +#### Method: `swapEndian` |
| 65 | + |
| 66 | +Given an 8 byte word (or shorter), pads to 8 bytes (adds 0 at left) and reverses endian byte order. |
| 67 | + |
| 68 | +```php |
| 69 | +/** |
| 70 | + * Given an 8 byte word (or shorter), pads to 8 bytes (adds 0 at left) and reverses endian byte order. |
| 71 | + * |
| 72 | + * @param string $word |
| 73 | + * @return string |
| 74 | + */ |
| 75 | +public static function swapEndian(string $word): string |
| 76 | +``` |
| 77 | + |
| 78 | +Example: |
| 79 | + |
| 80 | +```php |
| 81 | +$word = "12345678"; |
| 82 | +$swapped = Mnemonic::swapEndian($word); // Returns "78563412" |
| 83 | +``` |
| 84 | + |
| 85 | +#### Method: `encode` |
| 86 | + |
| 87 | +Given a hexadecimal key string (seed), return its mnemonic representation. |
| 88 | + |
| 89 | +```php |
| 90 | +/** |
| 91 | + * Given a hexadecimal key string (seed), return its mnemonic representation. |
| 92 | + * |
| 93 | + * @param string $seed |
| 94 | + * @param string|null $wordset_name |
| 95 | + * @return array<string> |
| 96 | + */ |
| 97 | +public static function encode(string $seed, ?string $wordset_name = null): array |
| 98 | +``` |
| 99 | + |
| 100 | +Example: |
| 101 | + |
| 102 | +```php |
| 103 | +$seedHex = "f2750ee6e1f326f485fdc34ac517a69cbd9c72c5766151626039f0eeab40e109"; |
| 104 | +$encodedMnemonic = Mnemonic::encode($seedHex, "english"); // Returns an array of mnemonic words |
| 105 | +``` |
| 106 | + |
| 107 | +#### Method: `encodeWithChecksum` |
| 108 | + |
| 109 | +Given a hexadecimal key string (seed), return its mnemonic representation plus an extra checksum word. |
| 110 | + |
| 111 | +```php |
| 112 | +/** |
| 113 | + * Given a hexadecimal key string (seed), return its mnemonic representation plus an extra checksum word. |
| 114 | + * |
| 115 | + * @param string $seed |
| 116 | + * @param string|null $wordset_name |
| 117 | + * @return array<string> |
| 118 | + */ |
| 119 | +public static function encodeWithChecksum(string $seed, ?string $wordset_name = null): array |
| 120 | +``` |
| 121 | + |
| 122 | +Example: |
| 123 | + |
| 124 | +```php |
| 125 | +$seedHex = "f2750ee6e1f326f485fdc34ac517a69cbd9c72c5766151626039f0eeab40e109"; |
| 126 | +$mnemonicWithChecksum = Mnemonic::encodeWithChecksum($seedHex, "english"); // Returns an array of mnemonic words with checksum |
| 127 | +``` |
| 128 | + |
| 129 | +#### Method: `decode` |
| 130 | + |
| 131 | +Given a mnemonic word list, return a hexadecimal encoded string (seed). |
| 132 | + |
| 133 | +```php |
| 134 | +/** |
| 135 | + * Given a mnemonic word list, return a hexadecimal encoded string (seed). |
| 136 | + * |
| 137 | + * @param array<string> $wlist |
| 138 | + * @param string|null $wordset_name |
| 139 | + * @return string |
| 140 | + * @throws Exception if decoding fails. |
| 141 | + */ |
| 142 | +public static function decode(array $wlist, ?string $wordset_name = null): string |
| 143 | +``` |
| 144 | + |
| 145 | +Example: |
| 146 | + |
| 147 | +```php |
| 148 | +$mnemonicWords = ["sighting", "pavements", "mocked", ...]; |
| 149 | +$decodedSeedHex = Mnemonic::decode($mnemonicWords, "english"); // Returns the hexadecimal seed string |
| 150 | +``` |
| 151 | + |
| 152 | +#### Method: `getWordsetByName` |
| 153 | + |
| 154 | +Given a wordset identifier, returns the full wordset. |
| 155 | + |
| 156 | +```php |
| 157 | +/** |
| 158 | + * Given a wordset identifier, returns the full wordset. |
| 159 | + * |
| 160 | + * @param string|null $name |
| 161 | + * @return array<string> |
| 162 | + * @throws Exception if the wordset name is invalid. |
| 163 | + */ |
| 164 | +public static function getWordsetByName(?string $name = null): array |
| 165 | +``` |
| 166 | + |
| 167 | +Example: |
| 168 | + |
| 169 | +```php |
| 170 | +$wordsetDetails = Mnemonic::getWordsetByName("english"); // Returns details of the English wordset |
| 171 | +``` |
| 172 | + |
| 173 | +#### Method: `findWordsetByMnemonic` |
| 174 | + |
| 175 | +Given a mnemonic array of words, returns the name of the matching wordset. |
| 176 | + |
| 177 | +```php |
| 178 | +/** |
| 179 | + * Given a mnemonic array of words, returns the name of the matching wordset. |
| 180 | + * |
| 181 | + * @param array<string> $mnemonic |
| 182 | + * @return string|null |
| 183 | + * @throws Exception if more than one wordset matches the mnemonic. |
| 184 | + */ |
| 185 | +public static function findWordsetByMnemonic(array $mnemonic): ?string |
| 186 | +``` |
| 187 | + |
| 188 | +Example: |
| 189 | + |
| 190 | +```php |
| 191 | +$matchedWordset = Mnemonic::findWordsetByMnemonic($mnemonicWords); // Returns "english" if the mnemonic matches |
| 192 | +``` |
| 193 | + |
| 194 | +#### Method: `getWordsetList` |
| 195 | + |
| 196 | +Return a list of available wordset names. |
| 197 | + |
| 198 | +```php |
| 199 | +/** |
| 200 | + * Return a list of available wordset names. |
| 201 | + * |
| 202 | + * @return array<string> |
| 203 | + */ |
| 204 | +public static function getWordsetList(): array |
| 205 | +``` |
| 206 | + |
| 207 | +Example: |
| 208 | + |
| 209 | +```php |
| 210 | +$wordsetList = Mnemonic::getWordsetList(); // Returns an array of available wordset names |
| 211 | +``` |
| 212 | + |
| 213 | +#### Method: `getWordsets` |
| 214 | + |
| 215 | +Return a list of available wordsets with details. |
| 216 | + |
| 217 | +```php |
| 218 | +/** |
| 219 | + * Return a list of available wordsets with details. |
| 220 | + * |
| 221 | + * @return array<string, array<string, string|int|array<string>>> |
| 222 | + */ |
| 223 | +public static function getWordsets(): array |
| 224 | +``` |
| 225 | + |
| 226 | +Example: |
| 227 | + |
| 228 | +```php |
| 229 | +$allWordsets = Mnemonic::getWordsets(); // Returns an array of all available wordsets with details |
| 230 | +``` |
| 231 | + |
| 232 | +### Interfaces |
| 233 | + |
| 234 | +#### Interface: `Wordset` |
| 235 | + |
| 236 | +The `Wordset` interface is implemented by classes representing different wordsets. |
| 237 | + |
| 238 | +```php |
| 239 | +interface Wordset { |
| 240 | + public static function name(): string; |
| 241 | + public static function englishName(): string; |
| 242 | + public static function prefixLength(): int; |
| 243 | + public static function words(): array; |
| 244 | +} |
| 245 | +``` |
| 246 | + |
| 247 | +This interface defines methods that must be implemented by each wordset class. It provides information about the name, English name, prefix length, and word list of a wordset. |
0 commit comments