-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
383 lines (369 loc) · 12.1 KB
/
index.html
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script id="fxhash-snippet">
//---- do not edit the following code (you can indent as you wish)
let search = new URLSearchParams(window.location.search)
let alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
let b58dec = (str) =>
[...str].reduce(
(p, c) => (p * alphabet.length + alphabet.indexOf(c)) | 0,
0
)
// make fxrand from hash
var fxhash =
search.get("fxhash") ||
"oo" +
Array(49)
.fill(0)
.map((_) => alphabet[(Math.random() * alphabet.length) | 0])
.join("")
let fxhashTrunc = fxhash.slice(2)
let regex = new RegExp(".{" + ((fxhash.length / 4) | 0) + "}", "g")
let hashes = fxhashTrunc.match(regex).map((h) => b58dec(h))
let sfc32 = (a, b, c, d) => {
return () => {
a |= 0
b |= 0
c |= 0
d |= 0
var t = (((a + b) | 0) + d) | 0
d = (d + 1) | 0
a = b ^ (b >>> 9)
b = (c + (c << 3)) | 0
c = (c << 21) | (c >>> 11)
c = (c + t) | 0
return (t >>> 0) / 4294967296
}
}
var fxrand = sfc32(...hashes)
// make fxrandminter from minter address
var fxminter =
search.get("fxminter") ||
"tz1" +
Array(33)
.fill(0)
.map((_) => alphabet[(Math.random() * alphabet.length) | 0])
.join("")
let fxminterTrunc = fxminter.slice(3)
regex = new RegExp(".{" + ((fxminterTrunc.length / 4) | 0) + "}", "g")
hashes = fxminterTrunc.match(regex).map((h) => b58dec(h))
var fxrandminter = sfc32(...hashes)
// true if preview mode active, false otherwise
// you can append preview=1 to the URL to simulate preview active
var isFxpreview = search.get("preview") === "1"
// call this method to trigger the preview
function fxpreview() {
window.dispatchEvent(new Event("fxhash-preview"))
setTimeout(() => fxpreview(), 500)
}
// get the byte params from the URL
let fxparams = search.get("fxparams")
fxparams = fxparams ? fxparams.replace("0x", "") : fxparams
// the parameter processor, used to parse fxparams
const processors = {
number: {
deserialize: (input) => {
const view = new DataView(new ArrayBuffer(8))
for (let i = 0; i < 8; i++) {
view.setUint8(i, parseInt(input.substring(i * 2, i * 2 + 2), 16))
}
return view.getFloat64(0)
},
bytesLength: () => 8,
constrain: (value, definition) => {
let min = Number.MIN_SAFE_INTEGER
if (typeof definition.options?.min !== "undefined")
min = Number(definition.options.min)
let max = Number.MAX_SAFE_INTEGER
if (typeof definition.options?.max !== "undefined")
max = Number(definition.options.max)
max = Math.min(max, Number.MAX_SAFE_INTEGER)
min = Math.max(min, Number.MIN_SAFE_INTEGER)
const v = Math.min(Math.max(value, min), max)
return v
},
random: (definition) => {
let min = Number.MIN_SAFE_INTEGER
if (typeof definition.options?.min !== "undefined")
min = Number(definition.options.min)
let max = Number.MAX_SAFE_INTEGER
if (typeof definition.options?.max !== "undefined")
max = Number(definition.options.max)
max = Math.min(max, Number.MAX_SAFE_INTEGER)
min = Math.max(min, Number.MIN_SAFE_INTEGER)
const v = Math.random() * (max - min) + min
if (definition?.options?.step) {
const t = 1.0 / definition?.options?.step
return Math.round(v * t) / t
}
return v
},
},
bigint: {
deserialize: (input) => {
const view = new DataView(new ArrayBuffer(8))
for (let i = 0; i < 8; i++) {
view.setUint8(i, parseInt(input.substring(i * 2, i * 2 + 2), 16))
}
return view.getBigInt64(0)
},
bytesLength: () => 8,
random: (definition) => {
const MIN_SAFE_INT64 = -9223372036854775808n
const MAX_SAFE_INT64 = 9223372036854775807n
let min = MIN_SAFE_INT64
let max = MAX_SAFE_INT64
if (typeof definition.options?.min !== "undefined")
min = BigInt(definition.options.min)
if (typeof definition.options?.max !== "undefined")
max = BigInt(definition.options.max)
const range = max - min
const bits = range.toString(2).length
let random
do {
random = BigInt(
"0b" +
Array.from(
crypto.getRandomValues(new Uint8Array(Math.ceil(bits / 8)))
)
.map((b) => b.toString(2).padStart(8, "0"))
.join("")
)
} while (random > range)
return random + min
},
},
boolean: {
// if value is "00" -> 0 -> false, otherwise we consider it's 1
deserialize: (input) => {
return input === "00" ? false : true
},
bytesLength: () => 1,
random: () => Math.random() < 0.5,
},
color: {
deserialize: (input) => input,
bytesLength: () => 4,
transform: (input) => {
const r = parseInt(input.slice(0, 2), 16)
const g = parseInt(input.slice(2, 4), 16)
const b = parseInt(input.slice(4, 6), 16)
const a = parseInt(input.slice(6, 8), 16)
return {
hex: {
rgb: "#" + input.slice(0, 6),
rgba: "#" + input,
},
obj: {
rgb: { r, g, b },
rgba: { r, g, b, a },
},
arr: {
rgb: [r, g, b],
rgba: [r, g, b, a],
},
}
},
constrain: (value, definition) => {
const hex = value.replace("#", "")
return hex.slice(0, 8).padEnd(8, "f")
},
random: () =>
`${[...Array(8)]
.map(() => Math.floor(Math.random() * 16).toString(16))
.join("")}`,
},
string: {
deserialize: (input) => {
const hx = input.match(/.{1,4}/g) || []
let rtn = ""
for (let i = 0; i < hx.length; i++) {
const int = parseInt(hx[i], 16)
if (int === 0) break
rtn += String.fromCharCode(int)
}
return rtn
},
bytesLength: (options) => {
if (typeof options?.maxLength !== "undefined")
return Number(options.maxLength) * 2
return 64 * 2
},
constrain: (value, definition) => {
let min = 0
if (typeof definition.options?.minLength !== "undefined")
min = definition.options.minLength
let max = 64
if (typeof definition.options?.maxLength !== "undefined")
max = definition.options.maxLength
let v = value.slice(0, max)
if (v.length < min) {
return v.padEnd(min)
}
return v
},
random: (definition) => {
let min = 0
if (typeof definition.options?.minLength !== "undefined")
min = definition.options.minLength
let max = 64
if (typeof definition.options?.maxLength !== "undefined")
max = definition.options.maxLength
const length = Math.round(Math.random() * (max - min) + min)
return [...Array(length)]
.map((i) => (~~(Math.random() * 36)).toString(36))
.join("")
},
},
select: {
deserialize: (input, definition) => {
return (
definition.options.options[parseInt(input, 16)] || definition.default
)
},
bytesLength: () => 1,
constrain: (value, definition) => {
if (definition.options.options.includes(value)) {
return value
}
return definition.options.options[0]
},
random: (definition) => {
const index = Math.round(
Math.random() * (definition?.options?.options?.length - 1) + 0
)
return definition?.options?.options[index]
},
},
}
// takes the parameters as bytes and outputs an object with the
// deserialized parameters, identified by their id in an object
const deserializeParams = (bytes, definition) => {
const params = {}
for (const def of definition) {
const processor = processors[def.type]
// if we don't have any parameters defined in the URL, set the
// default value and move on
if (!bytes) {
let v
if (typeof def.default === "undefined") v = processor.random(def)
else v = def.default
params[def.id] = processor.constrain?.(v, def) || v
continue
}
// extract the length from the bytes & shift the initial bytes string
const valueBytes = bytes.substring(
0,
processor.bytesLength(def?.options) * 2
)
bytes = bytes.substring(processor.bytesLength(def?.options) * 2)
// deserialize the bytes into the params
const value = processor.deserialize(valueBytes, def)
params[def.id] = processor.constrain?.(value, def) || value
}
return params
}
const transformParamValues = (values, definitions) => {
const paramValues = {}
for (const def of definitions) {
const processor = processors[def.type]
const value = values[def.id]
// deserialize the bytes into the params
paramValues[def.id] = processor.transform
? processor.transform(value)
: value
}
return paramValues
}
window.$fx = {
_version: "3.0.0",
_processors: processors,
// where params def & features will be stored
_params: undefined,
_features: undefined,
// where the parameter values are stored
_paramValues: {},
hash: fxhash,
rand: fxrand,
minter: fxminter,
randminter: fxrandminter,
preview: fxpreview,
isPreview: isFxpreview,
params: function (definition) {
// todo: maybe do some validation on the dev side ?
// or maybe not ?
this._params = definition
this._rawValues = deserializeParams(fxparams, definition)
this._paramValues = transformParamValues(this._rawValues, definition)
},
features: function (features) {
this._features = features
},
getFeature: function (id) {
return this._features[id]
},
getFeatures: function () {
return this._features
},
getParam: function (id) {
return this._paramValues[id]
},
getParams: function () {
return this._paramValues
},
getRawParam: function (id) {
return this._rawValues[id]
},
getRawParams: function () {
return this._rawValues
},
getDefinitions: function () {
return this._params
},
stringifyParams: function (params) {
return JSON.stringify(
params,
(key, value) => {
if (typeof value === "bigint") return value.toString()
return value
},
2
)
},
}
window.addEventListener("message", (event) => {
if (event.data === "fxhash_getInfo") {
parent.postMessage(
{
id: "fxhash_getInfo",
data: {
version: window.$fx._version,
hash: window.$fx.hash,
features: window.$fx.getFeatures(),
params: {
definitions: window.$fx.getDefinitions(),
values: window.$fx.getRawParams(),
},
minter: window.$fx.minter,
},
},
"*"
)
}
})
// END NEW
//---- /do not edit the following code
</script>
<script src="index.js" type="module"></script>
<link rel="stylesheet" type="text/css" href="index.css">
<title>Title</title>
<!--
Title
Description
-->
</head>
<body>
</body>
</html>