miijs 2.2.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,36 +1,303 @@
1
1
  # MiiJS
2
- MiiJS is a JS library for working with Mii characters in an accessible way. Reading and writing binary representations or QR codes, converting between consoles, rendering via Studio or locally, generating instructions to recreate the Miis, making Special Miis, and more functions planned. This library reads Mii data into JSON, which is human and computer readable.
2
+ MiiJS is a complete and comprehensive Mii library for reading, converting, modifying, writing, and rendering Mii characters from an accessible coding language. Support for all Mii types, including DS, Wii, 3DS, Wii U, Amiibo, Switch 1 & 2, Amiibos, and Mii Studio. Capable of making Special Miis and 3DS QR codes. Able to generate instructions to recreate Miis from scratch.
3
3
  <hr>
4
4
 
5
5
  ## Installation
6
- `npm install miijs` | `npm i miijs`
6
+ `npm install miijs` || `npm i miijs`
7
7
 
8
8
  <hr>
9
9
 
10
10
  ## Table of Contents
11
11
  - [Functions](#functions)
12
+ - [Code Examples](#code-examples)
12
13
  - [Special Miis](#special-miis)
13
14
  - [Other Console Support](#other-console-support)
14
15
  - [`convertMii` Discrepancies](#discrepancies-in-convertmii-function)
15
16
  - [Transferring to/from the System](#transferring-miis-to-and-from-the-system)
16
17
  - [FFLResHigh.dat](#fflreshighdat)
17
18
  - [Credits](#credits)
19
+
18
20
  <hr>
19
21
 
20
22
  # Functions
21
- - **`async read3DSQR(PathToMiiQR OR BinaryDataFromQR, ReturnDecryptedBin?)`** - returns JSON by default, by specifying `true` as the secondary parameter you can receive only the decrypted Mii data from the QR.
22
- - **`async write3DSQR(MiiJSON, PathToWriteTo, fflRes)`** - writes a JPG QR of a 3DS scannable Mii to the path specified. If no fflRes is specified, the QR will render using Nintendo Studio's API. If one is, it will contain a locally rendered version. fflRes must either be passed as a buffer, or FFLResHigh.dat present in your project's root directory.
23
23
 
24
- - **`readWiiBin(PathToMii OR BinaryMiiData)`** - returns JSON.
25
- - **`writeWiiBin(MiiJSON, PathToWriteTo)`** - returns Mii binary which can then be written by default. If PathToWriteTo is specified, it will instead be written to a file.
24
+ ### Reading Miis
25
+ - **`async read3DSQR(PathToMiiQR OR BinaryDataFromQR, ReturnDecryptedBin?)`** - Returns JSON by default. By specifying `true` as the secondary parameter you can receive only the decrypted Mii data from the QR.
26
+ - **`readWiiBin(PathToMii OR BinaryMiiData)`** - Returns JSON from a Wii Mii binary file.
27
+
28
+ ### Writing Miis
29
+ - **`async write3DSQR(MiiJSON, PathToWriteTo, fflRes?)`** - Writes a JPG QR of a 3DS scannable Mii to the path specified. If no fflRes is specified, the QR will render using Nintendo Studio's API. If one is provided, it will contain a locally rendered version. fflRes must either be passed as a buffer, or FFLResHigh.dat present in your project's root directory.
30
+ - **`async writeWiiBin(MiiJSON, PathToWriteTo?)`** - Returns Mii binary which can then be written by default. If PathToWriteTo is specified, it will instead be written to a file.
31
+
32
+ ### Converting Miis
33
+ - **`convertMii(miiJson, typeTo?)`** - Converts the Mii JSON format between consoles (3DS ↔ Wii) and returns the JSON. If typeTo is not specified, converts to the opposite type.
34
+ - **`convertMiiToStudio(miiJSON)`** - Returns a Studio compatible Mii in hex format.
35
+ - **`convertStudioToMii(input)`** - Converts Studio format (hex string or Uint8Array) to 3DS Mii JSON.
36
+
37
+ ### Rendering Miis
38
+ - **`async renderMiiWithStudio(miiJSON)`** - Returns a buffer containing a PNG representation of the Mii's face using Nintendo's Studio API.
39
+ - **`async renderMii(miiJSON, fflRes?)`** - Returns a buffer containing a PNG representation of the Mii's face using local rendering. fflRes must either be passed as a buffer, or FFLResHigh.dat present in your project's root directory. Currently bodies render but are unaffected by height and weight changes, though this is planned to be changed in the future.
40
+
41
+ ### Amiibo Functions
42
+ - **`insertMiiIntoAmiibo(amiiboDump, miiData)`** - Inserts Mii data (92 or 96 bytes, decrypted 3DS format) into an Amiibo dump. Returns the modified Amiibo dump.
43
+ - **`extractMiiFromAmiibo(amiiboDump)`** - Extracts the Mii data (92 bytes, decrypted 3DS format) from an Amiibo dump. Returns a Buffer.
44
+
45
+ ### Utility Functions
46
+ - **`generateInstructions(miiJson, fullInstructions?)`** - Returns a JSON object of different instruction fields for manually recreating the Mii. If fullInstructions is not set, only the instructions that differ from a default Mii will be returned.
47
+ - **`miiHeightToFeetInches(value)`** - Converts Mii height value (0-127) to real-world feet and inches. Returns `{feet, inches, totalInches}`.
48
+ - **`inchesToMiiHeight(totalInches)`** - Converts real-world height in inches to Mii height value (0-127).
49
+ - **`heightWeightToMiiWeight(heightInches, weightLbs)`** - Converts real-world height and weight to Mii weight value (0-127). **EXPERIMENTAL**
50
+ - **`miiWeightToRealWeight(heightInches, miiWeight)`** - Converts Mii weight value to real-world pounds and BMI. Returns `{pounds, bmi}`. **EXPERIMENTAL**
51
+
52
+ <hr>
53
+
54
+ # Code Examples
55
+
56
+ ## Reading a 3DS Mii from QR Code
57
+ ```javascript
58
+ const miijs = require('miijs');
59
+
60
+ // Read from file path
61
+ const miiJson = await miijs.read3DSQR('./example3DSQR.jpg');
62
+ console.log('Mii Name:', miiJson.meta.name);
63
+ console.log('Favorite Color:', miiJson.general.favoriteColor);
64
+
65
+ // Or get just the decrypted binary data
66
+ const decryptedBin = await miijs.read3DSQR('./example3DSQR.jpg', true);
67
+ console.log('Decrypted binary length:', decryptedBin.length);
68
+ ```
69
+
70
+ ## Reading a Wii Mii from Binary File
71
+ ```javascript
72
+ const miijs = require('miijs');
73
+
74
+ // Read from file path
75
+ const miiJson = await miijs.readWiiBin('./exampleWii.bin');
76
+ console.log('Mii Name:', miiJson.meta.name);
77
+ console.log('Gender:', miiJson.general.gender === 0 ? 'Male' : 'Female');
78
+
79
+ // Or pass binary data directly
80
+ const fs = require('fs');
81
+ const binaryData = fs.readFileSync('./exampleWii.bin');
82
+ const miiJson2 = await miijs.readWiiBin(binaryData);
83
+ ```
84
+
85
+ ## Writing a 3DS Mii QR Code
86
+ ```javascript
87
+ const miijs = require('miijs');
88
+
89
+ // First, read or create a Mii JSON
90
+ const miiJson = await miijs.read3DSQR('./example3DSQR.jpg');
91
+
92
+ // Write QR code with Studio rendering (no FFLResHigh.dat needed)
93
+ await miijs.write3DSQR(miiJson, './output_qr.jpg');
94
+
95
+ // Or with local rendering (requires FFLResHigh.dat in project root or passed as buffer)
96
+ const fs = require('fs');
97
+ const fflRes = fs.readFileSync('./FFLResHigh.dat');
98
+ await miijs.write3DSQR(miiJson, './output_qr_local.jpg', fflRes);
99
+ ```
100
+
101
+ ## Writing a Wii Mii Binary
102
+ ```javascript
103
+ const miijs = require('miijs');
104
+ const fs = require('fs');
105
+
106
+ // Read a Mii (from any format)
107
+ const miiJson = await miijs.read3DSQR('./example3DSQR.jpg');
108
+
109
+ // Convert to Wii format first if needed
110
+ const wiiMii = miijs.convertMii(miiJson, 'wii');
111
+
112
+ // Write to file
113
+ await miijs.writeWiiBin(wiiMii, './output_wii.bin');
114
+
115
+ // Or get buffer without writing
116
+ const buffer = await miijs.writeWiiBin(wiiMii);
117
+ fs.writeFileSync('./manual_write.bin', buffer);
118
+ ```
119
+
120
+ ## Converting Between Formats
121
+ ```javascript
122
+ const miijs = require('miijs');
123
+
124
+ // Read a 3DS Mii
125
+ const ds3Mii = await miijs.read3DSQR('./example3DSQR.jpg');
126
+
127
+ // Convert to Wii format
128
+ const wiiMii = miijs.convertMii(ds3Mii, 'wii');
129
+
130
+ // Convert back to 3DS
131
+ const backTo3DS = miijs.convertMii(wiiMii, '3ds');
132
+
133
+ // Auto-detect and convert to opposite
134
+ const autoConverted = miijs.convertMii(ds3Mii);
135
+ ```
136
+
137
+ ## Converting to/from Studio Format
138
+ ```javascript
139
+ const miijs = require('miijs');
140
+
141
+ // Read a Mii and convert to Studio format
142
+ const miiJson = await miijs.read3DSQR('./example3DSQR.jpg');
143
+ const studioHex = miijs.convertMiiToStudio(miiJson);
144
+ console.log('Studio URL:', `https://studio.mii.nintendo.com/miis/image.png?data=${studioHex}`);
145
+
146
+ // Convert Studio format back to JSON
147
+ const studioData = '000d142a303f434b717a7b84939ba6b2bbbec5cbc9d0e2ea...';
148
+ const miiFromStudio = miijs.convertStudioToMii(studioData);
149
+ console.log('Converted Mii:', miiFromStudio.meta.name);
150
+ ```
151
+
152
+ ## Rendering Miis
153
+ ```javascript
154
+ const miijs = require('miijs');
155
+ const fs = require('fs');
156
+
157
+ // Read a Mii
158
+ const miiJson = await miijs.read3DSQR('./example3DSQR.jpg');
159
+
160
+ // Render using Studio API (simple, no setup needed)
161
+ const studioPng = await miijs.renderMiiWithStudio(miiJson);
162
+ fs.writeFileSync('./mii_studio_render.png', studioPng);
163
+
164
+ // Render locally with full body (requires FFLResHigh.dat)
165
+ const fflRes = fs.readFileSync('./FFLResHigh.dat');
166
+ const localPng = await miijs.renderMii(miiJson, fflRes);
167
+ fs.writeFileSync('./mii_local_render.png', localPng);
168
+
169
+ // Shirt color comes from miiJson.general.favoriteColor
170
+ ```
171
+
172
+ ## Working with Amiibos
173
+ ```javascript
174
+ const miijs = require('miijs');
175
+ const fs = require('fs');
176
+
177
+ // Read an Amiibo dump
178
+ const amiiboDump = fs.readFileSync('./exampleAmiiboDump.bin');
179
+
180
+ // Extract the Mii from the Amiibo (returns 92 bytes decrypted)
181
+ const miiData = miijs.extractMiiFromAmiibo(amiiboDump);
182
+
183
+ // Convert the raw Mii data to readable JSON
184
+ // (miiData is already decrypted 3DS format)
185
+ const miiJson = miijs.decode3DSMii(miiData); // Note: decode3DSMii not exported, use read3DSQR workflow
186
+
187
+ // Better workflow: Read from QR, get decrypted data, insert into Amiibo
188
+ const qrMiiJson = await miijs.read3DSQR('./example3DSQR.jpg');
189
+ const decryptedMiiData = await miijs.read3DSQR('./example3DSQR.jpg', true);
190
+
191
+ // Insert new Mii into Amiibo
192
+ const modifiedAmiibo = miijs.insertMiiIntoAmiibo(amiiboDump, decryptedMiiData);
193
+ fs.writeFileSync('./modified_amiibo.bin', modifiedAmiibo);
194
+ ```
195
+
196
+ ## Generating Recreation Instructions
197
+ ```javascript
198
+ const miijs = require('miijs');
199
+
200
+ // Read a Mii
201
+ const miiJson = await miijs.read3DSQR('./example3DSQR.jpg');
202
+
203
+ // Generate only non-default instructions (minimal)
204
+ const minimalInstructions = miijs.generateInstructions(miiJson);
205
+ console.log('Steps to recreate:');
206
+ Object.values(minimalInstructions).forEach(step => {
207
+ if (step) console.log('- ' + step);
208
+ });
209
+
210
+ // Generate complete instructions (every step)
211
+ const fullInstructions = miijs.generateInstructions(miiJson, true);
212
+ console.log('\nComplete recreation guide:');
213
+ Object.entries(fullInstructions).forEach(([field, instruction]) => {
214
+ console.log(`${field}: ${instruction}`);
215
+ });
216
+ ```
217
+
218
+ ## Height and Weight Conversions
219
+ ```javascript
220
+ const miijs = require('miijs');
221
+
222
+ // Convert Mii height (0-127) to feet/inches
223
+ const heightInfo = miijs.miiHeightToFeetInches(64); // midpoint value
224
+ console.log(`Height: ${heightInfo.feet}'${heightInfo.inches}" (${heightInfo.totalInches} inches)`);
225
+
226
+ // Convert real height to Mii value
227
+ const miiHeightValue = miijs.inchesToMiiHeight(72); // 6'0"
228
+ console.log('Mii height value for 6\'0":', miiHeightValue);
229
+
230
+ // EXPERIMENTAL: Convert real weight to Mii weight
231
+ const heightInches = 69; // 5'9"
232
+ const weightLbs = 160;
233
+ const miiWeightValue = miijs.heightWeightToMiiWeight(heightInches, weightLbs);
234
+ console.log('Mii weight value:', miiWeightValue);
235
+
236
+ // EXPERIMENTAL: Convert Mii weight to real weight
237
+ const weightInfo = miijs.miiWeightToRealWeight(heightInches, 64);
238
+ console.log(`Weight: ${weightInfo.pounds.toFixed(1)} lbs, BMI: ${weightInfo.bmi.toFixed(1)}`);
239
+ ```
240
+
241
+ ## Creating and Modifying a Mii
242
+ ```javascript
243
+ const miijs = require('miijs');
244
+
245
+ // Read an existing Mii
246
+ const miiJson = await miijs.read3DSQR('./example3DSQR.jpg');
247
+
248
+ // Modify properties
249
+ miiJson.meta.name = 'Custom Name';
250
+ miiJson.general.favoriteColor = 5; // Blue
251
+ miiJson.hair.color = 0; // Black
252
+ miiJson.eyes.color = 2; // Brown
253
+
254
+ // Make it a Special Mii (3DS only)
255
+ miiJson.meta.type = 'Special';
256
+
257
+ // Convert to Wii format
258
+ const wiiVersion = miijs.convertMii(miiJson, 'wii');
259
+
260
+ // Save as both formats
261
+ await miijs.write3DSQR(miiJson, './modified_3ds.jpg');
262
+ await miijs.writeWiiBin(wiiVersion, './modified_wii.bin');
263
+ ```
26
264
 
27
- - **`convertMii(miiJson)`** - converts the Mii JSON format to the opposite Mii type (3DS, Wii) and returns the JSON.
28
- - **`convertMiiToStudio(miiJSON)`** - returns a Studio compatible Mii in hex format.
265
+ ## Complete Workflow Example
266
+ ```javascript
267
+ const miijs = require('miijs');
268
+ const fs = require('fs');
29
269
 
30
- - **`async renderMiiWithStudio(miiJSON)`** - Returns a buffer containing a JPG representation of the Mii's face using Studio.
31
- - **`async renderMii(miiJSON,fflRes)`** - Returns a buffer containing a JPG representation of the Mii's face. fflRes must either be passed as a buffer, or FFLResHigh.dat present in your project's root directory.
270
+ async function processMyMii() {
271
+ // 1. Read from QR code
272
+ const mii = await miijs.read3DSQR('./example3DSQR.jpg');
273
+ console.log('Loaded:', mii.meta.name);
274
+
275
+ // 2. Customize the Mii
276
+ mii.general.favoriteColor = 0; // Red
277
+ mii.general.height = miijs.inchesToMiiHeight(66); // 5'6"
278
+
279
+ // 3. Render it
280
+ const renderBuffer = await miijs.renderMiiWithStudio(mii);
281
+ fs.writeFileSync('./my_mii_face.png', renderBuffer);
282
+
283
+ // 4. Generate recreation instructions
284
+ const instructions = miijs.generateInstructions(mii);
285
+ console.log('\nRecreation steps:', instructions);
286
+
287
+ // 5. Export to multiple formats
288
+ await miijs.write3DSQR(mii, './my_mii_qr.jpg');
289
+
290
+ const wiiMii = miijs.convertMii(mii, 'wii');
291
+ await miijs.writeWiiBin(wiiMii, './my_mii_wii.bin');
292
+
293
+ const studioCode = miijs.convertMiiToStudio(mii);
294
+ console.log('\nStudio URL:', `https://studio.mii.nintendo.com/miis/image.png?data=${studioCode}`);
295
+
296
+ console.log('\nDone! All formats exported.');
297
+ }
32
298
 
33
- - **`generateInstructions(miiJson, fullInstructions)`** - returns a JSON object of different instruction fields. If full is not set, only the instructions that differ from a default Mii will be returned.
299
+ processMyMii().catch(console.error);
300
+ ```
34
301
 
35
302
  <hr>
36
303
 
@@ -38,21 +305,26 @@ MiiJS is a JS library for working with Mii characters in an accessible way. Read
38
305
  Special Miis were on the Wii and 3DS, identifiable via their golden pants. They were created by Nintendo employees, and not consumers. They could not be edited, or copied. In every other instance transferring a Mii to another system would leave a copy on both systems. For Special Miis, they would delete themselves from the console sending them, and only ever be present in one place at a time per copy Nintendo sent out. When receiving them via QR code on the 3DS, it would only allow you to scan that QR once, and never again. On the Wii, these were distributed via the WiiConnect24 service, and would arrive via the Message Board. On the 3DS, these were distributed occasionally via Spotpass, Streetpass, and QR codes.
39
306
  ### Making a Special Mii
40
307
  To make a special Mii, read in the file using the appropriate function, set `mii.info.type="Special";`, and then write a new file with the appropriate function.
41
- -# The Wii U does not support Special Miis.
308
+ -# Special Miis only work on the Wii and 3DS, and no other console.
42
309
 
43
310
  <hr>
44
311
 
45
312
  ## Other Console Support
46
313
  - DS
47
- - DS and Wii Miis are interchangeable. The DS only contains Miis in a handful of games, and is not baked into the system, however every instance where it does it is based off the Wii version of Miis, and to my current knowledge always provides a way to transfer to and from the Wii, being the only way short of recreation to transfer on and off.
314
+ - DS and Wii Miis are interchangeable. The DS only contains Miis in a handful of games, and is not baked into the system, however every instance where it does it is based off the Wii version of Miis, and to my current knowledge always provides a way to transfer from the Wii, being the only way short of recreation to transfer onto the DS. There is, to my knowledge, no way to transfer Miis off of the DS short of recreation.
315
+ - Use Wii functions for DS Miis
48
316
  - Wii U
49
317
  - The Wii U and 3DS Miis are interchangeable, with one major exception. The 3DS has Special Miis, while the Wii U will not render any Mii set as a Special Mii. So since the 3DS has this one added feature, 3DS is what takes priority in the naming schemes across this project, however it is for all intents and purposes interchangeable with a Wii U Mii.
318
+ - Use 3DS functions for Wii U Miis
50
319
  - Switch/2
51
- - Miis are more isolated than they've ever been on the Switch/2. To take them on and off of the Switch/2 via direct transfer, an Amiibo _and_ one of, a 3DS with NFC Reader accessory, New 3DS, or Wii U, is **required**. The only other method is to recreate manually from scratch. Due to this limitation of direct transfer, all Miis that this library can affect will be going through the 3DS or Wii U anyway, and direct Switch/2 support is thus irrelevant.
320
+ - Miis are more isolated than they've ever been on the Switch/2. To take them on and off of the Switch/2 via direct transfer, an Amiibo _and_ one of, a 3DS with NFC Reader accessory, New 3DS, or Wii U, is **required**. The only other method is to recreate manually from scratch. When the Switch writes to an Amiibo, it converts it to a 3DS/Wii U format. Due to this limitation of direct transfer, all Miis that this library can affect will be going through the 3DS/Wii U anyway, and direct Switch/2 support is thus irrelevant. The only differences between Switch Miis and Wii U Miis (no Special Mii support on the Switch either) is a ton more hair colors anyway.
321
+ - Use 3DS, Studio, and Amiibo functions for Switch/2 Miis
52
322
  - Studio
53
323
  - Studio Miis are in essence Switch/2 Miis. Transferring directly on/off of Studio (a browser Mii Maker used purely for profile pictures across Nintendo's online logins) requires a developer console and code paste, or browser extension. I may undertake making my own version of this in the future, but for the time being [this tool](https://mii.tools/studioloader/) by HEYimHeroic serves this purpose (from what I can tell, I have not used it myself).
324
+ - Use Studio Functions for Studio Miis
54
325
  - Miitomo/Kaerutomo and Tomodachi Life
55
326
  - Both Mii formats are the same as 3DS formats, with extra info added to the end. The way the library is set up, it can already read these. My devices are too new for Kaerutomo support, but I believe it should be able to scan the 3DS format Miis. Writing specific to Tomodachi Life Miis with game data already present in the QR is more within the realm of a Tomodachi Life save editor. I may undertake this for the Miis in the future, but it would be a separate project.
327
+ - Use 3DS functions for these Miis
56
328
 
57
329
  <hr>
58
330
 
@@ -72,11 +344,17 @@ There is a reason that the Wii supports sending Miis to the 3DS, but not vice ve
72
344
  <hr>
73
345
 
74
346
  # Transferring Miis to and from the System
347
+ - DS
348
+ - If the game you would like to transfer Miis to supports it, the option to "Connect to Wii" will be found in various places and worded different ways. The main game you might want to do this for is Tomodachi Collection, which will be in Town Hall after three Mii residents are on the island. On the Wii, you then want to press the DS icon in the top right and follow the prompts from there. If the option is not present, press and _release_ **A**, press and release **B**, press and release **1**, and then press and _hold_ **2**. The option should then be visible. This option is not available on Wii U or the Wii mode of the Wii U, and can only be used to send Miis to DS and 3DS, not from. No option to retrieve Miis from the DS is available besides recreating the Mii.
75
349
  - Wii
76
350
  - Method 1 (Recommended, doesn't require homebrew): Connect the Wiimote to your PC, Dolphin seems to be the easiest way to do so though there are some more difficult ways to do so, and use [WDMLMiiTransfer](https://sourceforge.net/projects/wdml/files/WDML%20-%20MiiTransfer/). Open the `readSlotX.bat` file for the slot you're trying to read from (Array notation, 0=1, 1=2, 2=3, and so on). The Mii will be in the same directory under the name `miiX.mii`, where X is the same number as the readSlot you opened. If you used `readSlotAll.bat`, then there will be 10 Miis (0-9) in the directory. Note that if no Mii was ever present in that slot ever on the Wiimote, it will still output a `miiX.mii` file, though it will not contain the Mii data correctly. To write to the Wiimote, make sure the Mii you're writing is in the same directory and named `miiX.mii`, where X is the slot you're writing to, and open `writeSlotX.bat`, where X is the slot you're writing to (in array notation). You can transfer Miis on and off the Wiimote from the Wii by using the Wiimote icon in the top right of Mii Maker.
77
351
  - Method 2 (Requires Homebrew, is untested by me): [Mii Installer](https://wiibrew.org/wiki/Mii_Installer) for writing from the SD card to the Wii, and [Mii Extractor](https://wiibrew.org/wiki/Mii_Extractor) for reading from the Wii.
78
352
  - 3DS and Wii U
79
353
  - Open Mii Maker, select "QR Code/Image Options", and then select the respective QR Code option, be it scanning a QR code or saving a Mii as a QR code.
354
+ - Amiibo
355
+ - You can use [Tagmo](https://play.google.com/store/apps/details?id=com.hiddenramblings.tagmo.eightbit&hl=en_US&pli=1) on Android, bottom right NFC button -> Backup to retrieve an Amiibo file, or Amiibo bin in explorer -> Write: first write to blank NTAG215 tag OR Update: subsequent writes to already-an-Amiibo tags. _Reportedly_ [one of these apps](https://www.reddit.com/r/tagmo/comments/ynxonu/list_of_ios_iphone_amiibo_apps/) can be used for an equivalent on iPhone. I have not tested and cannot verify any of the iPhone apps at this time.
356
+ - Switch/2
357
+ - You have to use Amiibos as a conduit to interact with Miis on the Switch/2. To take these Miis on and off of the Switch, in System Settings under the Amiibo menu you can register or change the Owner Mii to set the Mii stored on the Amiibo, and under Miis you can select Create a Mii and then Copy from Amiibo to take a Mii from the Amiibo onto the Switch.
80
358
 
81
359
  <sub>If you are unable to transfer to the console you wish to, you can use the `generateInstructions` function provided here and manually recreate the Mii on the console using the provided instructions.</sub>
82
360
 
@@ -93,4 +371,5 @@ You can find FFLResHigh using a Wii U with an FTP program installed at `sys/titl
93
371
  # Credits
94
372
  - **[kazuki-4ys' MiiInfoEditorCTR](https://github.com/kazuki-4ys/kazuki-4ys.github.io/tree/master/web_apps/MiiInfoEditorCTR)** - I repurposed how to decrypt and reencrypt the QR codes from here, including repurposing the asmCrypto.js file in its entirety with very small modifications (it has since been stripped down to only include the functions this library uses). I believe I also modified the code for rendering the Mii using Nintendo's Mii Studio from here as well, though I do not remember for certain.
95
373
  - **[ariankordi's FFL.js](https://github.com/ariankordi/FFL.js/)** - Rendering Miis locally would not be possible without this library. Instructions for finding FFLResHigh are also learned from [ariankordi's FFL-Testing repository](https://github.com/ariankordi/FFL-Testing).
96
- - **[Models Resource](https://models.spriters-resource.com/3ds/systembios/asset/306260/)** - For the bodies used in Mii rendering
374
+ - **[Models Resource](https://models.spriters-resource.com/3ds/systembios/asset/306260/)** - For the bodies used in Mii rendering
375
+ - **[socram8888's Amiitools](https://github.com/socram8888/amiitool)** - I _think_, for the code reverse engineered to help with aspects of Amiibo dump processing. I went through so many iterations in research and coding, there may be other credits due as well but I _think_ this was the only repo actually used for the reverse engineering in the final working code.
@@ -0,0 +1,239 @@
1
+ const crypto = require('crypto');
2
+
3
+ /*This constant is provided SOLELY because I cannot find a guide online to retrieve this file from a console or Amiibo on your own that doesn't just tell you to download it from somewhere anyway.
4
+ If someone can find, or make, a guide for this, I will wipe all commits of this key from the repo and instead point to how to get this key for yourself.*/
5
+ const MASTER_KEY_BUFFER = Buffer.from('1D164B375B72A55728B91D64B6A3C205756E666978656420696E666F7300000EDB4B9E3F45278F397EFF9B4FB9930000044917DC76B49640D6F83939960FAED4EF392FAAB21428AA21FB54E5450547667F752D2873A20017FEF85C0575904B6D6C6F636B656420736563726574000010FDC8A07694B89E4C47D37DE8CE5C74C1044917DC76B49640D6F83939960FAED4EF392FAAB21428AA21FB54E545054766', 'hex');
6
+
7
+ const DATA_HMAC_KEY = MASTER_KEY_BUFFER.slice(0, 16);
8
+ const DATA_TYPE_STRING = MASTER_KEY_BUFFER.slice(16, 30);
9
+ const DATA_MAGIC_BYTES_SIZE = MASTER_KEY_BUFFER[31];
10
+ const DATA_MAGIC_BYTES = MASTER_KEY_BUFFER.slice(32, 48);
11
+ const DATA_XOR_PAD = MASTER_KEY_BUFFER.slice(48, 80);
12
+
13
+ const TAG_HMAC_KEY = MASTER_KEY_BUFFER.slice(80, 96);
14
+ const TAG_TYPE_STRING = MASTER_KEY_BUFFER.slice(96, 110);
15
+ const TAG_MAGIC_BYTES_SIZE = MASTER_KEY_BUFFER[111];
16
+ const TAG_MAGIC_BYTES = MASTER_KEY_BUFFER.slice(112, 128);
17
+ const TAG_XOR_PAD = MASTER_KEY_BUFFER.slice(128, 160);
18
+
19
+ const NFC3D_AMIIBO_SIZE = 520;
20
+ const NTAG215_SIZE = 540;
21
+ const NTAG215_SIZE_ALT = 532;
22
+
23
+ const MII_OFFSET_DECRYPTED = 0x4C;
24
+ const MII_SIZE = 96;
25
+
26
+ //Calculate CRC16 checksum for Mii data (for Amiibo format)
27
+ function calculateMiiChecksum(data) {
28
+ const checksumData = data.slice(0, 94);
29
+ let crc = 0;
30
+ for (let byteIndex = 0; byteIndex < checksumData.length; byteIndex++) {
31
+ for (let bitIndex = 7; bitIndex >= 0; bitIndex--) {
32
+ crc = (((crc << 1) | ((checksumData[byteIndex] >> bitIndex) & 0x1)) ^
33
+ (((crc & 0x8000) !== 0) ? 0x1021 : 0)) & 0xFFFF;
34
+ }
35
+ }
36
+ for (let counter = 16; counter > 0; counter--) {
37
+ crc = ((crc << 1) ^ (((crc & 0x8000) !== 0) ? 0x1021 : 0)) & 0xFFFF;
38
+ }
39
+ return crc & 0xFFFF;
40
+ }
41
+
42
+ //Validate and fix Mii checksum - for 96-byte Amiibo format
43
+ function validateAndFixMiiChecksum(miiData) {
44
+ if (miiData.length !== 92 && miiData.length !== MII_SIZE) {
45
+ throw new Error(`Invalid Mii data size: expected 92 or ${MII_SIZE} bytes, got ${miiData.length}`);
46
+ }
47
+ const fullMii = Buffer.alloc(MII_SIZE);
48
+ miiData.slice(0, Math.min(94, miiData.length)).copy(fullMii, 0);
49
+ const newChecksum = calculateMiiChecksum(fullMii);
50
+ fullMii[94] = (newChecksum >> 8) & 0xFF;
51
+ fullMii[95] = newChecksum & 0xFF;
52
+ return fullMii;
53
+ }
54
+ function calcSeed(dump) {
55
+ const seed = Buffer.alloc(64);
56
+ dump.slice(0x029, 0x02B).copy(seed, 0x00);
57
+ seed.fill(0x00, 0x02, 0x10);
58
+ dump.slice(0x1D4, 0x1DC).copy(seed, 0x10);
59
+ dump.slice(0x1D4, 0x1DC).copy(seed, 0x18);
60
+ dump.slice(0x1E8, 0x208).copy(seed, 0x20);
61
+ return seed;
62
+ }
63
+ function prepareSeed(typeString, magicBytes, magicBytesSize, xorPad, baseSeed) {
64
+ const output = Buffer.alloc(480);
65
+ let offset = 0;
66
+ const typeStringEnd = typeString.indexOf(0);
67
+ const typeLen = typeStringEnd >= 0 ? typeStringEnd + 1 : 14;
68
+ typeString.slice(0, typeLen).copy(output, offset);
69
+ offset += typeLen;
70
+ const leadingSeedBytes = 16 - magicBytesSize;
71
+ baseSeed.slice(0, leadingSeedBytes).copy(output, offset);
72
+ offset += leadingSeedBytes;
73
+ magicBytes.slice(0, magicBytesSize).copy(output, offset);
74
+ offset += magicBytesSize;
75
+ baseSeed.slice(0x10, 0x20).copy(output, offset);
76
+ offset += 16;
77
+ for (let i = 0; i < 32; i++) {
78
+ output[offset + i] = baseSeed[0x20 + i] ^ xorPad[i];
79
+ }
80
+ offset += 32;
81
+ return output.slice(0, offset);
82
+ }
83
+ function drbgGenerateBytes(hmacKey, seed, outputSize) {
84
+ const result = Buffer.alloc(outputSize);
85
+ let offset = 0;
86
+ let iteration = 0;
87
+ while (offset < outputSize) {
88
+ const iterBuffer = Buffer.alloc(2 + seed.length);
89
+ iterBuffer[0] = (iteration >> 8) & 0xFF;
90
+ iterBuffer[1] = iteration & 0xFF;
91
+ seed.copy(iterBuffer, 2);
92
+ const hmac = crypto.createHmac('sha256', hmacKey);
93
+ hmac.update(iterBuffer);
94
+ const output = hmac.digest();
95
+ const toCopy = Math.min(32, outputSize - offset);
96
+ output.copy(result, offset, 0, toCopy);
97
+ offset += toCopy;
98
+ iteration++;
99
+ }
100
+ return result;
101
+ }
102
+
103
+ function deriveKeys(typeString, magicBytes, magicBytesSize, xorPad, hmacKey, baseSeed) {
104
+ const preparedSeed = prepareSeed(typeString, magicBytes, magicBytesSize, xorPad, baseSeed);
105
+ const derived = drbgGenerateBytes(hmacKey, preparedSeed, 48);
106
+ return {
107
+ aesKey: derived.slice(0, 16),
108
+ aesIV: derived.slice(16, 32),
109
+ hmacKey: derived.slice(32, 48)
110
+ };
111
+ }
112
+
113
+ function tagToInternal(tag) {
114
+ const internal = Buffer.alloc(NFC3D_AMIIBO_SIZE);
115
+ tag.slice(0x008, 0x010).copy(internal, 0x000);
116
+ tag.slice(0x080, 0x0A0).copy(internal, 0x008);
117
+ tag.slice(0x010, 0x034).copy(internal, 0x028);
118
+ tag.slice(0x0A0, 0x208).copy(internal, 0x04C);
119
+ tag.slice(0x034, 0x054).copy(internal, 0x1B4);
120
+ tag.slice(0x000, 0x008).copy(internal, 0x1D4);
121
+ tag.slice(0x054, 0x080).copy(internal, 0x1DC);
122
+ return internal;
123
+ }
124
+
125
+ function internalToTag(internal) {
126
+ const tag = Buffer.alloc(NFC3D_AMIIBO_SIZE);
127
+ internal.slice(0x000, 0x008).copy(tag, 0x008);
128
+ internal.slice(0x008, 0x028).copy(tag, 0x080);
129
+ internal.slice(0x028, 0x04C).copy(tag, 0x010);
130
+ internal.slice(0x04C, 0x1B4).copy(tag, 0x0A0);
131
+ internal.slice(0x1B4, 0x1D4).copy(tag, 0x034);
132
+ internal.slice(0x1D4, 0x1DC).copy(tag, 0x000);
133
+ internal.slice(0x1DC, 0x208).copy(tag, 0x054);
134
+ return tag;
135
+ }
136
+
137
+ function decryptAmiibo(tag) {
138
+ const internal = tagToInternal(tag);
139
+ const seed = calcSeed(internal);
140
+ const dataKeys = deriveKeys(DATA_TYPE_STRING, DATA_MAGIC_BYTES, DATA_MAGIC_BYTES_SIZE, DATA_XOR_PAD, DATA_HMAC_KEY, seed);
141
+ const tagKeys = deriveKeys(TAG_TYPE_STRING, TAG_MAGIC_BYTES, TAG_MAGIC_BYTES_SIZE, TAG_XOR_PAD, TAG_HMAC_KEY, seed);
142
+ const plain = Buffer.alloc(NFC3D_AMIIBO_SIZE);
143
+ const cipher = crypto.createDecipheriv('aes-128-ctr', dataKeys.aesKey, dataKeys.aesIV);
144
+ cipher.setAutoPadding(false);
145
+ const decrypted = cipher.update(internal.slice(0x02C, 0x1B4));
146
+ decrypted.copy(plain, 0x02C);
147
+ internal.slice(0x000, 0x008).copy(plain, 0x000);
148
+ internal.slice(0x028, 0x02C).copy(plain, 0x028);
149
+ internal.slice(0x1D4, 0x208).copy(plain, 0x1D4);
150
+ const tagHmac = crypto.createHmac('sha256', tagKeys.hmacKey);
151
+ tagHmac.update(plain.slice(0x1D4, 0x208));
152
+ const computedTagHmac = tagHmac.digest();
153
+ computedTagHmac.copy(plain, 0x1B4);
154
+ const dataHmac = crypto.createHmac('sha256', dataKeys.hmacKey);
155
+ dataHmac.update(plain.slice(0x029, 0x208));
156
+ const computedDataHmac = dataHmac.digest();
157
+ computedDataHmac.copy(plain, 0x008);
158
+ return plain;
159
+ }
160
+
161
+ function encryptAmiibo(plain) {
162
+ const seed = calcSeed(plain);
163
+ const dataKeys = deriveKeys(DATA_TYPE_STRING, DATA_MAGIC_BYTES, DATA_MAGIC_BYTES_SIZE, DATA_XOR_PAD, DATA_HMAC_KEY, seed);
164
+ const tagKeys = deriveKeys(TAG_TYPE_STRING, TAG_MAGIC_BYTES, TAG_MAGIC_BYTES_SIZE, TAG_XOR_PAD, TAG_HMAC_KEY, seed);
165
+ const cipher_internal = Buffer.alloc(NFC3D_AMIIBO_SIZE);
166
+ const tagHmac = crypto.createHmac('sha256', tagKeys.hmacKey);
167
+ tagHmac.update(plain.slice(0x1D4, 0x208));
168
+ tagHmac.digest().copy(cipher_internal, 0x1B4);
169
+ const dataHmac = crypto.createHmac('sha256', dataKeys.hmacKey);
170
+ dataHmac.update(plain.slice(0x029, 0x1B4));
171
+ dataHmac.update(cipher_internal.slice(0x1B4, 0x1D4));
172
+ dataHmac.update(plain.slice(0x1D4, 0x208));
173
+ dataHmac.digest().copy(cipher_internal, 0x008);
174
+ const aesCipher = crypto.createCipheriv('aes-128-ctr', dataKeys.aesKey, dataKeys.aesIV);
175
+ aesCipher.setAutoPadding(false);
176
+ const encrypted = aesCipher.update(plain.slice(0x02C, 0x1B4));
177
+ encrypted.copy(cipher_internal, 0x02C);
178
+ plain.slice(0x000, 0x008).copy(cipher_internal, 0x000);
179
+ plain.slice(0x028, 0x02C).copy(cipher_internal, 0x028);
180
+ plain.slice(0x1D4, 0x208).copy(cipher_internal, 0x1D4);
181
+ return internalToTag(cipher_internal);
182
+ }
183
+
184
+ //Extract Mii data from an Amiibo dump
185
+ function extractMiiFromAmiibo(amiiboDump) {
186
+ if (!Buffer.isBuffer(amiiboDump)) {
187
+ throw new Error('Amiibo dump must be a Buffer');
188
+ }
189
+ const size = amiiboDump.length;
190
+ if (size !== NFC3D_AMIIBO_SIZE && size !== NTAG215_SIZE && size !== NTAG215_SIZE_ALT) {
191
+ throw new Error(`Invalid Amiibo dump size: ${size} (expected ${NFC3D_AMIIBO_SIZE}, ${NTAG215_SIZE_ALT}, or ${NTAG215_SIZE})`);
192
+ }
193
+ const tag = amiiboDump.slice(0, NFC3D_AMIIBO_SIZE);
194
+ const decrypted = decryptAmiibo(tag);
195
+
196
+ // Extract only the first 92 bytes (the actual Mii data, without checksum)
197
+ const miiData = decrypted.slice(MII_OFFSET_DECRYPTED, MII_OFFSET_DECRYPTED + 92);
198
+
199
+ return Buffer.from(miiData);
200
+ }
201
+
202
+ //Insert Mii data into an Amiibo dump
203
+ function insertMiiIntoAmiibo(amiiboDump, miiData) {
204
+ if (!Buffer.isBuffer(amiiboDump)) {
205
+ throw new Error('Amiibo dump must be a Buffer');
206
+ }
207
+ if (!Buffer.isBuffer(miiData)) {
208
+ throw new Error('Mii data must be a Buffer');
209
+ }
210
+ const size = amiiboDump.length;
211
+ if (size !== NFC3D_AMIIBO_SIZE && size !== NTAG215_SIZE && size !== NTAG215_SIZE_ALT) {
212
+ throw new Error(`Invalid Amiibo dump size: ${size}`);
213
+ }
214
+ if (miiData.length !== 92 && miiData.length !== MII_SIZE) {
215
+ throw new Error(`Mii data must be 92 or ${MII_SIZE} bytes, got ${miiData.length}`);
216
+ }
217
+ const tag = amiiboDump.slice(0, NFC3D_AMIIBO_SIZE);
218
+ const decrypted = decryptAmiibo(tag);
219
+
220
+ // Validate and fix Mii checksum, ensuring it's 96 bytes with correct checksum
221
+ const miiWithChecksum = validateAndFixMiiChecksum(miiData);
222
+
223
+ // Insert Mii data (96 bytes)
224
+ miiWithChecksum.copy(decrypted, MII_OFFSET_DECRYPTED);
225
+
226
+ const encrypted = encryptAmiibo(decrypted);
227
+ const result = Buffer.alloc(size);
228
+ encrypted.copy(result, 0);
229
+ if (size > NFC3D_AMIIBO_SIZE) {
230
+ amiiboDump.slice(NFC3D_AMIIBO_SIZE).copy(result, NFC3D_AMIIBO_SIZE);
231
+ }
232
+
233
+ return result;
234
+ }
235
+
236
+ module.exports = {
237
+ insertMiiIntoAmiibo,
238
+ extractMiiFromAmiibo
239
+ };