-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtap-drop.mjs
154 lines (129 loc) · 3.53 KB
/
tap-drop.mjs
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
import { Address } from '@cmdcode/tapscript';
import util from "util";
import fs from 'graceful-fs';
const readFile = util.promisify(fs.readFile);
let template = {
"p" : "tap",
"op" : "token-send",
"items" : []
};
let tpls = [];
let tpl_idx = -1;
let addresses = await readFile('./drop.csv');
addresses = addresses.toString().trim().replaceAll("\r","");
let splitted = addresses.split("\n");
for(let i = 0; i < splitted.length; i++)
{
let ticker = splitted[i].split(',')[0].trim();
let address = splitted[i].split(',')[1].trim();
let amount = splitted[i].split(',')[2].trim();
if(i % 3000 === 0)
{
tpl_idx += 1;
tpls.push(JSON.parse(JSON.stringify(template)));
}
if(isValidBitcoinAddress(address))
{
tpls[tpl_idx].items.push({
"tick": ticker,
"amt": amount,
"address" : address
});
}
}
for(let i = 0; i < tpls.length; i++)
{
if(await exists('./drop/' + i + '.txt'))
{
await fs.promises.unlink('./drop/' + i + '.txt');
}
await fs.promises.writeFile('./drop/' + i + '.txt', JSON.stringify(tpls[i]));
}
function isValidNumber(strNum)
{
let validNumber = new RegExp(/^\d*\.?\d*$/);
return validNumber.test(''+strNum);
}
function resolveNumberString(number, decimals){
if(!isValidNumber(number))
{
console.log('Invalid number', number);
throw new Error('Invalid op number');
}
let splitted = number.split(".");
if(splitted.length == 1 && decimals > 0){
splitted[1] = '';
}
if(splitted.length > 1) {
let size = decimals - splitted[1].length;
for (let i = 0; i < size; i++) {
splitted[1] += "0";
}
let new_splitted = '';
for(let i = 0; i < splitted[1].length; i++)
{
if(i >= decimals)
{
break;
}
new_splitted += splitted[1][i];
}
number = "" + (splitted[0] == '0' ? '' : splitted[0]) + new_splitted;
if(BigInt(number) == 0n || number === ''){
number = "0";
}
}
try {
while (number.charAt(0) === '0') {
number = number.substring(1);
}
}catch(e){
number = '0';
}
return number === '' ? '0' : number;
}
function isValidBitcoinAddress(toAddress) {
if(toAddress.startsWith('bc1q'))
{
try {
Address.p2wpkh.decode(toAddress, 'main').hex;
return true;
} catch (e) {
console.log('invalid address, skipped:', toAddress);
}
}
else if(toAddress.startsWith('1') || toAddress.startsWith('m') || toAddress.startsWith('n'))
{
try {
Address.p2pkh.decode(toAddress, 'main').hex;
return true;
} catch (e) {
console.log('invalid address, skipped:', toAddress);
}
}
else if(toAddress.startsWith('3') || toAddress.startsWith('2'))
{
try {
Address.p2sh.decode(toAddress, 'main').hex;
return true;
} catch (e) {
console.log('invalid address, skipped:', toAddress);
}
}
else
{
try {
Address.p2tr.decode(toAddress).hex;
return true;
} catch (e) {
console.log('invalid address, skipped:', toAddress);
}
}
return false;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function exists(filename){
return !!(await fs.promises.stat(filename).catch(() => null));
}