-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
238 lines (207 loc) · 9.65 KB
/
index.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
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
const {Command, InvalidArgumentError} = require('commander');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const querystring = require('querystring');
const qrcode = require('qrcode');
const {Buffer} = require("buffer");
const {QRCodeCanvas} = require('@loskir/styled-qr-code-node');
const program = new Command();
function intArg(value, prev) {
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new InvalidArgumentError('Must be an integer value.');
}
return parsedValue;
}
program
.name('index.js')
.description(`A simple CLI utility to generate random codes and QR images from them`)
.version('1.0.0');
program
.command('generate-codes')
.argument('[amount]', 'Number of codes you wish to generate', intArg, 10)
.option('-u, --uuid', 'Generate UUID codes', false)
.option('-b, --bytes <int>', 'Length of random bytes codes generated', intArg, 8)
.option('-l, --lovelace <int>', 'Number of Lovelace to attach to each code', intArg, 1000000)
.option('-t, --tokens "[policy_id].[asset_id][#?]|[quantity],[policy_id].[asset_id][#?]|[quantity]"', 'Provide details of a token that will be added to each code. Assets should be provided in hex-based format i.e. (698a6ea0ca99f315034072af31eaac6ec11fe8558d3f48e9775aab9d.7444524950) If a number symbol (#) is present at the end of the asset ID, a serial number will be added to the end of each asset. Add a pipe character (|) followed by the integer amount of the token to send.', null, false)
.option('-o, --offset <int>', "How much to offset token number generation by.")
.description('Generate [amount] of random codes and save to a local text file')
.action((amount, options) => {
console.log(`Generating ${parseInt(amount)} codes!`);
let offset = options.offset || 0;
console.log(`Offset: ${offset}`);
if (options.uuid) {
console.log(`Using UUIDv4`);
} else {
console.log(`Using hex strings of ${options.bytes} random bytes.`)
}
console.log(`Codes will be written out to: ./codes.json`);
const codes = {};
for (let i = 0; i < parseInt(amount); i++) {
let code;
if (options.uuid) {
code = crypto.randomUUID();
} else {
code = crypto.randomBytes(options.bytes).toString('hex');
}
const code_contents = {
lovelaces: options.lovelace
};
if (options.tokens) {
const tokens = options.tokens.split(',');
if (tokens) {
tokens.forEach((token) => {
const [asset, quantity] = token.split('|');
let [policy_id, asset_id] = asset.split('.');
if (asset_id.slice(-1) === '#') {
const serial = parseInt(i) + parseInt(offset) + 1;
console.log(`Serial #${serial}`);
asset_id = asset_id.substring(0, asset_id.length - 1) + serial.toString().padStart(amount.toString().length, '0');
asset_id = Buffer.from(asset_id, "ascii").toString('hex');
}
code_contents[policy_id + "." + asset_id] = quantity;
});
}
}
codes[code] = code_contents
}
fs.writeFileSync('./codes.json', JSON.stringify(codes));
console.log('Finished!');
});
program
.command('generate-qr')
.argument('<faucet_url>', 'The faucet URL that will be embedded into the QR code')
.argument('[code_source]', 'JSON file containing the codes to generate. The file format should be a JSON array with each code being a new entry in the array.', './codes.json')
.option('-o, --output <path>', 'The output path to store generated QR Codes in', './qr')
.option('-f, --format <type>', "The output format", "svg")
.option('-u, --uri', 'Generate URIs only and write them to a file')
.option('-e, --ecl <level>', 'Error-correction Level: L, M, Q, H', 'H')
.option('-w, --width <int>', 'Size of the generated code', intArg, 512)
.option('-d, --dark <rgba>', 'Hex RGBA color code for dark portions of the QR code', '#000000ff')
.option('-l, --light <rgba>', 'Hex RGBA color code for light portions of the QR code', '#ffffffff')
.option('-m, --margin <int>', 'Size of the margin to place around the image', intArg, 0)
.description('Generate SVG QR codes from the provided JSON codes file')
.action((faucet_url, code_source, options) => {
console.log(faucet_url, querystring.escape(faucet_url));
console.log(code_source);
console.log(options);
// Create the output directory if it does not exist
if (!fs.existsSync(options.output)) {
console.log("Creating the output directory...");
fs.mkdirSync(options.output);
}
// Empty the output directory
const files = fs.readdirSync(options.output);
for (const file of files) {
fs.unlinkSync(path.join(options.output, file))
}
let codes;
try {
codes = JSON.parse(fs.readFileSync(code_source, 'utf8'));
} catch (e) {
console.error(e);
throw new Error("Could not read the codes file! Are you sure it is valid JSON?");
}
const uris = [];
Object.keys(codes).forEach((code) => {
const uri = 'web+cardano://claim/v1?' + querystring.stringify({
faucet_url: faucet_url,
code: code
});
uris.push(uri);
qrcode.toFile(path.join(options.output, code + '.' + options.format), uri, {
errorCorrectionLevel: options.ecl,
width: options.width,
margin: options.margin,
color: {
dark: options.dark,
light: options.light
}
})
});
fs.writeFileSync(path.join(options.output, 'code_uris.json'), JSON.stringify(uris));
})
program
.command('generate-claim-qr')
.argument('[code_source]', 'JSON file containing the codes to generate. The file format should be a JSON object with each code being the keys of the object.', './codes.json')
.option('-o, --output <path>', 'The output path to store the generated QR codes in', './claim_qr')
.option('-f, --format <type>', 'The output format (SVG|PNG)', 'svg')
.option('-u, --uri', 'Generate URIs only and write them to a file')
.option('-e, --ecl <level>', 'Error-correction Level: L, M, Q, H', 'H')
.option('-w, --width <int>', 'Size of the generated code', intArg, 512)
.option('-d, --dark <rgba>', 'Hex RGBA color code for dark portions of the QR code', '#000000ff')
.option('-l, --light <rgba>', 'Hex RGBA color code for light portions of the QR code', '#ffffffff')
.description('Generate QR codes for NMKR claim from the provided JSON codes file')
.action((code_source, options) => {
// console.log(faucet_url, querystring.escape(faucet_url));
console.log(code_source);
console.log(options);
// Create the output directory if it does not exist
if (!fs.existsSync(options.output)) {
console.log("Creating the output directory...");
fs.mkdirSync(options.output);
}
// Empty the output directory
const files = fs.readdirSync(options.output);
for (const file of files) {
fs.unlinkSync(path.join(options.output, file))
}
let codes;
try {
codes = JSON.parse(fs.readFileSync(code_source, 'utf8'));
} catch (e) {
console.error(e);
throw new Error("Could not read the codes file! Are you sure it is valid JSON?");
}
const uris = [];
const width = options.width;
const height = width;
const config = {
data: null, // This is the URL
// image: null, // This is a URI to the image to layer over the center
label: null, // This is a label shown beneath the QR
width: width,
height: height,
margin: 0,
type: "svg",
qrOptions: {
typeNumber: 0, mode: "Byte", errorCorrectionLevel: options.ecl
},
dotsOptions: {
type: "rounded", // type: "extra-rounded",
color: options.dark,
// gradient: {
// "type": "linear",
// "rotation": -0.7853981633974483,
// "colorStops": [{"offset": 0, "color": "#fdf64b"}, {"offset": 1, "color": "#02c9ee"}]
// }
},
cornersDotOptions: {
type: "dot",
color: options.dark,
},
cornersSquareOptions: {
type: "rounded",
color: options.dark,
},
backgroundOptions: {
color: options.light
},
imageOptions: {
margin: 10, crossOrigin: 'anonymous', saveAsBlob: true
}
};
Object.keys(codes).forEach((code) => {
// https://nmkr.io/claim?coupon=
const uri = 'https://nmkr.io/claim?' + querystring.stringify({
coupon: code
});
uris.push(uri);
config.data = uri;
const qr_code = new QRCodeCanvas(config);
qr_code.toFile(path.join(options.output, code + '.' + options.format), options.format);
config.data = null;
});
})
program.parse();