-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathqrdrop.js
176 lines (147 loc) · 6.39 KB
/
qrdrop.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
#!/usr/bin/env node
const { ethers } = require('hardhat');
const fs = require('fs');
const path = require('path');
const { assert } = require('console');
const qrdrop = require('./gen_qr_lib.js');
const archive = require('./zip_lib.js');
const commander = require('commander');
const { exit } = require('process');
const {DropSettings} = require("./gen_qr_lib");
const program = new commander.Command();
// Example usage: node ./src/qrdrop.js -gqlczv 33 -a 5,10,20,30,40,50 -n 40,70,80,100,70,40
// Example usage: node ./src/qrdrop.js -gqlv 28 -a 10,20,30,40,50 -n 140,140,210,140,70
// Example usage: node ./src/qrdrop.js -x -u "https://app.1inch.io/#/1/qr?d=IgA..." -r "0x347b0605206ea9851b1172ad9c2a935f"
// Example usage: node ./src/qrdrop.js -c
program
// generation mode
.option('-v, --version <version>', 'deployment instance version', false)
.option('-g, --gencodes', 'generate codes mode', false)
.option('-q, --qrs', 'generate qr: ', false)
.option('-l, --links', 'generate links: ', false)
.option('-n, --numbers <numbers>', 'codes to generate')
.option('-a, --amounts <amounts>', 'amounts to generate')
.option('-t, --testcodes <codes>', 'test codes', '10,1')
.option('-s, --nodeploy', 'test run, ignores version', false)
.option('-c, --cleanup', 'cleanup qr directories before codes generation', false)
.option('-z, --zip', 'zip qr-codes', false)
// verification mode
.option('-x, --validate', 'validation mode', false)
.option('-u, --url <url>', 'qr url')
.option('-r, --root <root>', 'merkle root')
// cleanup mode
.option('-w, --wipe', 'clean up qr directories', false)
// general parameters generation and validation
.option('-b, --chainid <chainid>', 'chain id', '1');
program.parse(process.argv);
const options = program.opts();
const VERSION = Number(options.version);
const flagGenerateCodes = options.gencodes;
const flagSaveQr = options.qrs;
const flagSaveLink = options.links;
const COUNTS = options.numbers === undefined ? [] : options.numbers.split(',').map(x => BigInt(x));
const AMOUNTS = options.amounts === undefined ? [] : options.amounts.split(',').map(x => BigInt(x));
const testCode = options.testcodes.split(',').map(x => BigInt(x));
const flagNoDeploy = options.nodeploy;
const flagCleanup = options.cleanup;
const flagZip = options.zip;
const flagValidateOnly = options.validate;
const validateUrl = options.url;
const validateRoot = options.root;
const flagWipe = options.wipe;
const chainId = Number(options.chainid);
validateArgs();
execute();
async function execute () {
if (flagGenerateCodes) {
const settings = qrdrop.createNewDropSettings(flagSaveQr, flagSaveLink, COUNTS, AMOUNTS, VERSION, chainId, flagNoDeploy);
if (!flagNoDeploy) {
validateVersion(settings.version, settings.fileLatest);
}
if (flagCleanup) {
archive.cleanDirs([settings.pathTestQr, settings.pathQr]);
}
COUNTS.unshift(testCode[0]);
AMOUNTS.unshift(testCode[1]);
AMOUNTS.forEach((element, index) => { AMOUNTS[index] = ethers.parseEther(element.toString()); });
await qrdrop.generateCodes(settings);
if (flagZip) {
const dateString = new Date().toISOString().slice(0, 7);
const productionQr = path.join(settings.pathZip, `${settings.version}-qr-drop-${dateString}.zip`);
const testQr = path.join(settings.pathZip, `${settings.version}-qr-drop-test-${dateString}.zip`);
archive.zipFolders([settings.pathQr, settings.pathTestQr], [productionQr, testQr]);
}
}
if (flagValidateOnly) {
const settings = qrdrop.createNewDropSettings(false, false, null, null, null, chainId, true);
assert(qrdrop.verifyLink(validateUrl, validateRoot, settings.prefix));
}
if (flagWipe || flagZip) {
archive.cleanDirs([DropSettings.pathTestQr, DropSettings.pathQr]);
}
}
function validateArgs () {
// Validate input
if (Number(flagGenerateCodes) + Number(flagValidateOnly) + Number(flagWipe) !== 1) {
console.error('please specify mode, either "generate codes" or "validate code" or "cleanup": -g or -x or -c, respectively');
exit(1);
}
// Validate generation mode arguments
if (flagGenerateCodes) {
// check version is an integer
if (isNaN(VERSION)) {
console.error('option \'-v, --version <version>\' is required to be an integer above zero');
exit(1);
}
if (isNotIntegerAboveZero(chainId)) {
console.error('option \'-b, --chainid <chainid>\' is required to be an integer above zero');
exit(1);
}
// check counts and amounts have the same length
if (COUNTS.length !== AMOUNTS.length) {
console.error('counts and amounts should have the same length');
exit(1);
}
// check there are elements in th array
if (COUNTS.length === 0 || AMOUNTS.length === 0) {
console.error('counts and amounts should contain at least one element');
exit(1);
}
// check non-integer elements in the array
if (COUNTS.some(isNotIntegerAboveZero) || AMOUNTS.some(isNotIntegerAboveZero)) {
console.error('counts and amounts should contain only integers above zero');
exit(1);
}
if (testCode.some(isNotIntegerAboveZero) || testCode.length !== 2) {
console.error('test codes should contain exactly two integers above zero.\nan example for 10 test codes with ether(\'1\') amount: -t 10,1');
exit(1);
}
}
if (flagValidateOnly) {
if (!validateUrl || !validateRoot) {
console.error('please specify url and root for validation: -u and -r, respectively');
exit(1);
}
}
}
function isNotIntegerAboveZero (value) {
return !(value > 0);
}
function validateVersion (version, latestFile) {
const latestVersion = getLatestVersion(latestFile);
if (version <= latestVersion) {
console.error('version should be greater than ' + latestVersion.toString());
exit(1);
}
}
function getLatestVersion (latestFile) {
if (!fs.existsSync(latestFile)) {
return 0;
}
const latestVersion = Number(fs.readFileSync(latestFile));
if (isNaN(latestVersion) || latestVersion < 0) {
console.log('WARNING! version file is corrupted');
exit(1);
}
return latestVersion;
}