Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/gen_qr_lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const { assert } = require('console');

class DropSettings {
fileLatest = './src/.latest';
pathQr = './src/qr';
pathTestQr = './src/test_qr';
static pathQr = './src/qr';
static pathTestQr = './src/test_qr';
pathZip = './src/gendata';

constructor (flagSaveQr, flagSaveLink, codeCounts, codeAmounts, version, chainId, flagNoVersionUpdate = false) {
Expand All @@ -25,6 +25,9 @@ class DropSettings {
this.chainId = chainId;
this.fileLinks = `./src/gendata/${version}-qr-links.json`;
this.prefix = `https://app.1inch.io/#/${chainId}/qr?`;
// Copy static properties to the instance
this.pathQr = DropSettings.pathQr;
this.pathTestQr = DropSettings.pathTestQr;
}
}

Expand All @@ -45,12 +48,24 @@ function saveFile(filePath, fileContent) {
fs.writeFileSync(filePath, fileContent);
}

function makeDrop (wallets, amounts) {
function makeDrop(wallets, amounts) {
// Create an array of elements by concatenating each wallet address with the corresponding amount
// in hexadecimal format, padded to 64 characters.
const elements = wallets.map((w, i) => w + BigInt(amounts[i]).toString(16).padStart(64, '0'));

// Generate a Merkle Tree leaf by hashing each element with keccak128 and converting it to a hexadecimal string.
const leaves = elements.map(keccak128).map(x => MerkleTree.bufferToHex(x));

// Create a Merkle Tree from the leaves using keccak128 as the hashing function and sort the pairs for consistency.
const tree = new MerkleTree(leaves, keccak128, { sortPairs: true });

// Obtain the Merkle root, which is the top node of the tree.
const root = tree.getHexRoot();

// Generate a proof for each leaf in the Merkle Tree. A proof is used to verify that a leaf is part of the tree.
const proofs = leaves.map(tree.getProof, tree);

// Return an object containing the elements, leaves, Merkle root, and proofs.
return { elements, leaves, root, proofs };
}

Expand Down Expand Up @@ -138,7 +153,8 @@ async function main (settings) {
const COUNTS = settings.codeCounts;
const AMOUNTS = settings.codeAmounts;

const privs = await genPrivs(Number(COUNTS.reduce((s, a) => s + a, 0n)));
const totalCodes = Number(COUNTS.reduce((s, a) => s + a, 0n));
const privs = await genPrivs(totalCodes);
const accounts = privs.map(p => Wallet.fromPrivateKey(Buffer.from(p, 'hex')).getAddressString());
let amounts = [];
for (let i = 0; i < COUNTS.length; i++) {
Expand Down Expand Up @@ -196,13 +212,13 @@ function verifyLink (url, root, prefix) {
return uriDecode(url, root, prefix, true);
}

function createNewDropSettings (flagSaveQr, flagSaveLink, codeCounts, codeAmounts, version, flagNoDeploy, chainId) {
const settings = new DropSettings(flagSaveQr, flagSaveLink, codeCounts, codeAmounts, version, flagNoDeploy, chainId);
return settings;
function createNewDropSettings (flagSaveQr, flagSaveLink, codeCounts, codeAmounts, version, chainId, flagNoDeploy) {
return new DropSettings(flagSaveQr, flagSaveLink, codeCounts, codeAmounts, version, chainId, flagNoDeploy);
}

module.exports = {
generateCodes: main,
verifyLink,
createNewDropSettings,
DropSettings,
};
4 changes: 2 additions & 2 deletions src/qrdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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
Expand Down Expand Up @@ -92,8 +93,7 @@ async function execute () {
}

if (flagWipe || flagZip) {
const settings = qrdrop.createNewDropSettings();
archive.cleanDirs([settings.pathTestQr, settings.pathQr]);
archive.cleanDirs([DropSettings.pathTestQr, DropSettings.pathQr]);
}
}

Expand Down