-
Notifications
You must be signed in to change notification settings - Fork 14
/
hl-gen.js
174 lines (165 loc) · 4.8 KB
/
hl-gen.js
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
/**
* Highlight Generative Art Script : V0
* @version: 0.1.2
* @description The script exposes certain values and methods that
* can be used within code based generative art.
*/
const hl = (function () {
let searchParams = new URLSearchParams(window.location.search);
const generateRandomHash = () => {
const alphabet = "0123456789abcdef";
return (
"0x" +
Array(64)
.fill(0)
.map((_) => alphabet[(Math.random() * alphabet.length) | 0])
.join("")
);
};
const generateRandomAddress = () => {
const alphabet = "0123456789abcdef";
return (
"0x" +
Array(40)
.fill(0)
.map((_) => alphabet[(Math.random() * alphabet.length) | 0])
.join("")
);
};
function xmur3(str) {
for (var i = 0, h = 1779033703 ^ str.length; i < str.length; i++)
(h = Math.imul(h ^ str.charCodeAt(i), 3432918353)),
(h = (h << 13) | (h >>> 19));
return function () {
(h = Math.imul(h ^ (h >>> 16), 2246822507)),
(h = Math.imul(h ^ (h >>> 13), 3266489909));
return (h ^= h >>> 16) >>> 0;
};
}
function sfc32(a, b, c, d) {
return function () {
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;
};
}
const contractAddress = searchParams.get("a") || generateRandomAddress();
const chainId =
searchParams.get("c") ||
[1, 5, 137, 80001][Math.floor(Math.random() * 4)].toString();
const editionSize =
searchParams.get("s") || Math.floor(100 * Math.random()).toString();
const mintSize =
searchParams.get("ms") || Math.floor(Number(editionSize) * Math.random() + 1).toString();
const mintIteration =
searchParams.get("mi") ||
Math.floor((Number(mintSize) - 1) * Math.random() + 1).toString();
const hash = searchParams.get("h") || generateRandomHash();
const blockHash = searchParams.get("bh") || generateRandomHash();
const blockNumber =
searchParams.get("bn") || Math.floor(1000000 * Math.random()).toString();
const tokenId =
searchParams.get("tid") || Math.floor(Number(editionSize) * Math.random()).toString();
const walletAddress = searchParams.get("wa") || generateRandomAddress();
const timestamp =
searchParams.get("t") || Math.floor(Date.now() / 1000).toString();
const gasPrice =
searchParams.get("gp") ||
Math.floor(Math.random() * (200 - 10 + 1) + 10).toString();
const gasUsed =
searchParams.get("gu") ||
Math.floor(Math.random() * (100 - 10 + 1) + 10).toString();
const isCurated = (searchParams.get("ic") || "0") === "1";
const seed = isCurated ? xmur3(hash) : xmur3(hash + tokenId);
const customMintData = searchParams.get("cmd") || "0x";
const hl = {
tx: {
contractAddress,
chainId,
hash,
blockHash,
blockNumber,
timestamp,
walletAddress,
tokenId,
mintSize,
mintIteration,
editionSize,
gasPrice,
gasUsed,
customMintData
},
random: (...args) => {
let min = 0,
max = 1;
if (args.length === 1) max = args[0];
if (args.length === 2) {
max = args[1];
min = args[0];
}
const rand = sfc32(seed(), seed(), seed(), seed())();
return min + (max - min) * rand;
},
randomInt: (...args) => {
if (args.length === 0) args.push(100); //If not upper limit provided then set to [0,100) as safe assumption
if (args.length === 1) args[0]++;
if (args.length === 2) args[1]++;
return Math.floor(hl.random(...args));
},
randomBool: (p) => {
return hl.random() < p;
},
randomElement: (array) => {
return array[hl.randomInt(0, array.length - 1)];
},
token: {
id: tokenId,
traits: {},
name: "",
description: "",
capturePreview: function () {
window.dispatchEvent(new Event("CAPTURE_PREVIEW"));
setTimeout(() => this.capturePreview(), 500);
},
setTraits: function (traits) {
this.traits = traits;
},
getTraits: function () {
return this.traits;
},
setName: function (name) {
this.name = name;
},
getName: function () {
return this.name;
},
setDescription: function (description) {
this.description = description;
},
getDescription: function () {
return this.description;
},
},
context: {
previewMode: searchParams.get("pr") === "1",
isCurated,
scriptInfo: () => {
return {
name: "Highlight Generative Art Script",
version: "0.1.2",
framework: "js",
};
},
},
};
return hl;
})();
window.$hl = hl;