-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiiIdentityHash.js
More file actions
202 lines (166 loc) · 5.43 KB
/
Copy pathmiiIdentityHash.js
File metadata and controls
202 lines (166 loc) · 5.43 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
import crypto from "crypto";
import miijs from "miijs";
const MII_IDENTITY_HASH_FIELDS = [
"hair",
"face",
"eyes",
"eyebrows",
"nose",
"mouth",
"beard",
"glasses",
"mole"
];
const MII_IDENTITY_HASH_VERSION = "mii-face-v4";
const MII_OFFICIAL_IDENTITY_HASH_VERSION = "mii-face-general-v2";
const MII_IDENTITY_HASH_PREFIX = `${MII_IDENTITY_HASH_VERSION}:`;
const MOUTH_TYPES_WITH_HASHED_COLOR = new Set([
1,
5,
0,
13,
7,
4,
11,
27,
24,
29
]);
const EYE_TYPES_WITH_IGNORED_COLOR = new Set([
0x1a,
0x17,
0x22,
0x15,
0x0d,
0x0e,
0x2f
]);
function getFeatureType(feature) {
const type = Number(feature?.type);
return Number.isFinite(type) ? type : null;
}
function areEyebrowsOffForMiiIdentityHash(eyebrows) {
if (!eyebrows || typeof eyebrows !== "object") return false;
if (eyebrows.on === false) return true;
return eyebrows.type === 23;
}
function areGlassesOffForMiiIdentityHash(glasses) {
if (!glasses || typeof glasses !== "object") return false;
return glasses.type === 0;
}
function isMoleOffForMiiIdentityHash(mole) {
if (!mole || typeof mole !== "object") return false;
return mole.on === false;
}
function isHairIgnoredForMiiIdentityHash(hair) {
if (!hair || typeof hair !== "object") return false;
return hair.type === 30;
}
function normalizeFeatureForMiiIdentityHash(field, value) {
const normalized = normalizeValueForMiiIdentityHash(value);
if (normalized === null || typeof normalized !== "object") return normalized;
if (field === "eyebrows" && areEyebrowsOffForMiiIdentityHash(normalized)) {
return null;
}
if (field === "glasses" && areGlassesOffForMiiIdentityHash(normalized)) {
return null;
}
if (field === "mole" && isMoleOffForMiiIdentityHash(normalized)) {
return null;
}
if (field === "hair" && isHairIgnoredForMiiIdentityHash(normalized)) {
return null;
}
if (field === "mouth" && !MOUTH_TYPES_WITH_HASHED_COLOR.has(getFeatureType(normalized))) {
delete normalized.color;
}
if (field === "eyes" && EYE_TYPES_WITH_IGNORED_COLOR.has(getFeatureType(normalized))) {
delete normalized.color;
}
return normalized;
}
function hasCurrentMiiIdentityHashVersion(value) {
return typeof value === "string" && value.startsWith(MII_IDENTITY_HASH_PREFIX);
}
function getComparableMiiSource(mii) {
if (!mii || typeof mii !== "object") return mii;
if (mii.fields && typeof mii.fields === "object") return mii.fields;
return mii;
}
function getPlainComparableMiiSource(mii) {
const source = getComparableMiiSource(mii);
if (!source || typeof source !== "object") return source;
return typeof source.toObject === "function"
? source.toObject({ depopulate: true, virtuals: false, getters: false, minimize: false })
: source;
}
function normalizeMiiForIdentityHash(mii) {
const plain = getPlainComparableMiiSource(mii);
if (!plain || typeof plain !== "object") return plain;
try {
const mnmsBuffer = miijs.encodeMii(plain, miijs.MiiFormats.MNMS);
return miijs.decodeMii(mnmsBuffer);
} catch (error) {
console.warn("[miiHash] Failed to normalize Mii through MNMS before hashing; falling back to stored fields:", error.message);
return plain;
}
}
function normalizeValueForMiiIdentityHash(value) {
if (value === undefined) return null;
if (value === null || typeof value !== "object") return value;
if (Array.isArray(value)) return value.map(normalizeValueForMiiIdentityHash);
const out = {};
for (const key of Object.keys(value).sort()) {
const normalized = normalizeValueForMiiIdentityHash(value[key]);
if (normalized !== undefined) {
out[key] = normalized;
}
}
return out;
}
function getMiiIdentityHashVersion({ includeGeneral = false } = {}) {
return includeGeneral ? MII_OFFICIAL_IDENTITY_HASH_VERSION : MII_IDENTITY_HASH_VERSION;
}
function getMiiIdentityHashPayload(mii, { includeGeneral = false } = {}) {
const source = normalizeMiiForIdentityHash(mii);
if (!source || typeof source !== "object") return source;
const payload = Object.fromEntries(
MII_IDENTITY_HASH_FIELDS.map((field) => [
field,
normalizeFeatureForMiiIdentityHash(field, source[field])
])
);
if (includeGeneral) {
payload.general = normalizeValueForMiiIdentityHash(source.general);
}
return payload;
}
function getMiiIdentityHash(mii, options = {}) {
const payload = getMiiIdentityHashPayload(mii, options);
if (!payload || typeof payload !== "object") return "";
const hashVersion = getMiiIdentityHashVersion(options);
const digest = crypto
.createHash("sha256")
.update(`${hashVersion}:${JSON.stringify(payload)}`)
.digest("hex");
return `${hashVersion}:${digest}`;
}
function setMiiIdentityHash(mii) {
if (!mii || typeof mii !== "object") return mii;
mii.miiHash = getMiiIdentityHash(mii);
return mii;
}
function areMiisTheSame(miiA, miiB) {
const hashA = getMiiIdentityHash(miiA);
const hashB = getMiiIdentityHash(miiB);
return Boolean(hashA && hashB && hashA === hashB);
}
export {
MII_IDENTITY_HASH_PREFIX,
areMiisTheSame,
getMiiIdentityHash,
getMiiIdentityHashPayload,
getMiiIdentityHashVersion,
hasCurrentMiiIdentityHashVersion,
setMiiIdentityHash
};