-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.js
360 lines (316 loc) · 11.1 KB
/
fetcher.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
const { ethers } = require("ethers");
const fs = require("fs");
const path = require("path");
const bitcoin = require("bitcoinjs-lib");
const ecc = require("@bitcoin-js/tiny-secp256k1-asmjs");
bitcoin.initEccLib(ecc);
require("dotenv").config();
// Define available blockchains with their respective RPC URLs and chain IDs
const blockchains = {
chronicle: {
rpcUrl:
process.env.CHRONICLE_RPC_URL ||
"https://chain-rpc.litprotocol.com/replica-http",
chainId: 175177,
},
yellowstone: {
rpcUrl:
process.env.YELLOWSTONE_RPC_URL ||
"https://yellowstone-rpc.litprotocol.com/",
chainId: 175188,
},
};
// Define the contracts (networks) with their respective addresses
const networks = {
// cayenne: "0x58582b93d978F30b4c4E812A16a7b31C035A69f7",
// habanero: "0x80182Ec46E3dD7Bb8fa4f89b48d303bD769465B2",
// manzano: "0x3c3ad2d238757Ea4AF87A8624c716B11455c1F9A",
// serrano: "0x8F75a53F65e31DD0D2e40d0827becAaE2299D111",
datil_prod: "0x487A9D096BB4B7Ac1520Cb12370e31e677B175EA",
datil_dev: "0x02C4242F72d62c8fEF2b2DB088A35a9F4ec741C7",
datil_test: "0x6a0f439f064B7167A8Ea6B22AcC07ae5360ee0d1",
};
// ABI for the contract, including the PKPMinted event and getEthAddress function
const contractABI = [
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "uint256",
name: "tokenId",
type: "uint256",
},
{
indexed: false,
internalType: "bytes",
name: "pubkey",
type: "bytes",
},
],
name: "PKPMinted",
type: "event",
},
{
inputs: [
{
internalType: "uint256",
name: "tokenId",
type: "uint256",
},
],
name: "getEthAddress",
outputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
stateMutability: "view",
type: "function",
},
];
async function fetchPKPs(
startBlock,
endBlock,
_blockchain,
_network,
_provider
) {
// Retrieve configuration from environment variables
const selectedBlockchain = _blockchain;
const selectedNetwork = _network;
const provider = _provider;
const blockInterval = parseInt(process.env.BLOCK_INTERVAL, 10) || 25000;
// Ensure both blockchain and network are specified
if (!selectedBlockchain) {
throw new Error(
"Both BLOCKCHAIN must be specified in the environment variables."
);
}
let results = [];
const networksToUse =
selectedNetwork === "all" ? Object.keys(networks) : [selectedNetwork];
// Iterate over each selected network (contract)
for (const network of networksToUse) {
// Get the contract address for the specified network
const contractAddress = networks[network];
if (!contractAddress) {
throw new Error(`Invalid network specified: ${selectedNetwork}`);
}
const contract = new ethers.Contract(
contractAddress,
contractABI,
provider
);
// Iterate through the blocks in intervals to query events
for (
let fromBlock = startBlock;
fromBlock <= endBlock;
fromBlock += blockInterval
) {
const toBlock = Math.min(fromBlock + blockInterval - 1, endBlock);
const filter = {
address: contractAddress,
fromBlock: fromBlock,
toBlock: toBlock,
topics: [ethers.utils.id("PKPMinted(uint256,bytes)")],
};
try {
// Query events for the specified range of blocks
const events = await contract.queryFilter(
filter,
fromBlock,
toBlock
);
console.log(
`Found ${events.length} PKPMinted events from block ${fromBlock} to ${toBlock} on ${selectedBlockchain} blockchain and ${network} network`
);
for (const event of events) {
const tokenId = event.args.tokenId.toString();
const publicKey = event.args.pubkey;
const { p2pkh, p2wpkh, p2shP2wpkh, p2tr, p2wsh, p2sh } =
calculateBtcAddresses(publicKey);
try {
// Fetch the ETH address associated with the tokenId
const ethAddress = await contract.getEthAddress(
tokenId
);
const result = `Blockchain: ${selectedBlockchain}, Network: ${network}, Token ID: ${tokenId} -> ETH Address: ${ethAddress}`;
console.log(result);
results.push({
blockchain: selectedBlockchain,
network: network,
tokenId,
ethAddress,
p2pkh,
p2wpkh,
p2shP2wpkh,
p2tr,
p2wsh,
p2sh,
});
} catch (error) {
console.error(
`Error fetching ETH address for Token ID ${tokenId} on ${selectedBlockchain} blockchain, ${selectedNetwork} network:`,
error
);
}
}
} catch (error) {
console.error(
`Error fetching events from block ${fromBlock} to ${toBlock} on ${selectedBlockchain} blockchain, ${selectedNetwork} network:`,
error
);
}
// Add a delay between queries to avoid overloading the provider
await new Promise((res) => setTimeout(res, 2000)); // 2 second delay
}
}
// Format and return results
results = cleanArray(results);
return results;
}
function calculateBtcAddresses(publicKey) {
if (publicKey.startsWith("0x")) {
publicKey = publicKey.slice(2);
}
// Check if the public key is in uncompressed format (starts with 04)
const isUncompressed = publicKey.startsWith("04");
let pubKeyBuffer;
if (isUncompressed) {
pubKeyBuffer = Buffer.from(
ecc.pointCompress(Buffer.from(publicKey, "hex"), true)
);
} else {
pubKeyBuffer = Buffer.from(publicKey, "hex");
}
const network = bitcoin.networks.bitcoin;
// P2PKH (Legacy address)
const p2pkh = bitcoin.payments.p2pkh({
pubkey: pubKeyBuffer,
network,
}).address;
// P2WPKH (Native SegWit address)
const p2wpkh = bitcoin.payments.p2wpkh({
pubkey: pubKeyBuffer,
network,
}).address;
// P2SH-P2WPKH (Nested SegWit address)
const p2shP2wpkh = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({ pubkey: pubKeyBuffer, network }),
network,
}).address;
// P2TR (Taproot address)
const p2tr = bitcoin.payments.p2tr({
internalPubkey: pubKeyBuffer.slice(1),
network,
}).address;
// P2WSH (Pay-to-Witness-Script-Hash)
const p2wsh = bitcoin.payments.p2wsh({
redeem: bitcoin.payments.p2pk({ pubkey: pubKeyBuffer, network }),
network,
}).address;
// P2SH (Pay-to-Script-Hash) - Example with P2PK script
const p2sh = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2pk({ pubkey: pubKeyBuffer, network }),
network,
}).address;
return { p2pkh, p2wpkh, p2shP2wpkh, p2tr, p2wsh, p2sh };
}
function cleanArray(dataArray) {
// Remove rows with empty values
const cleanedData = dataArray.filter((row) =>
Object.values(row).every(
(value) => value !== "" && value !== null && value !== undefined
)
);
// Rename columns
const renamedData = cleanedData.map((row) => ({
blockchain: row["blockchain"],
network: row["network"],
token_id: row["tokenId"],
eth_address: row["ethAddress"],
btcP2PKH: row["p2pkh"],
btcP2WPKH: row["p2wpkh"],
btcP2SHP2WPKH: row["p2shP2wpkh"],
btcP2TR: row["p2tr"],
btcP2WSH: row["p2wsh"],
btcP2SH: row["p2sh"],
}));
return renamedData;
}
function convertToCSV(data) {
const headers =
"blockchain,network,token_id,eth_address,btc_p2pkh,btc_p2wpkh,btc_p2shp2wpkh,btc_p2tr,btc_p2wsh,btc_p2sh";
const rows = data
.map(
(row) =>
`${row.blockchain},${row.network},${row.token_id},${row.eth_address},${row.btcP2PKH},${row.btcP2WPKH},${row.btcP2SHP2WPKH},${row.btcP2TR},${row.btcP2WSH},${row.btcP2SH}`
)
.join("\n");
return `${headers}\n${rows}`;
}
const CSV_DIR = "csv";
const getFormattedDate = () => {
const date = new Date();
return {
day: date.getDate().toString().padStart(2, "0"),
month: (date.getMonth() + 1).toString().padStart(2, "0"),
year: date.getFullYear().toString().slice(-2),
};
};
function writePkpCSV(_data) {
if (!fs.existsSync(CSV_DIR)) {
fs.mkdirSync(CSV_DIR);
console.log("Created csv directory");
}
const { day, month, year } = getFormattedDate();
const fileName = `pkp-${day}-${month}-${year}.csv`;
const filePath = path.join(CSV_DIR, fileName);
fs.writeFileSync(filePath, _data);
console.log(`PKP CSV data exported successfully to ${filePath}`);
}
function writeBlocksCSV(_startBlock, _endBlock) {
const csvHeader = "block_type,block_number\n";
const csvData = `start,${_startBlock}\nend,${_endBlock}`;
const fullCsvContent = csvHeader + csvData;
if (!fs.existsSync(CSV_DIR)) {
fs.mkdirSync(CSV_DIR);
console.log("Created csv directory");
}
const { day, month, year } = getFormattedDate();
const fileName = `block-${day}-${month}-${year}.csv`;
const filePath = path.join(CSV_DIR, fileName);
fs.writeFileSync(filePath, fullCsvContent);
console.log(`Block CSV data exported successfully to ${filePath}`);
}
export async function scan(startBlock) {
const blockchain = process.env.BLOCKCHAIN;
const { rpcUrl, chainId } = blockchains[blockchain];
if (!rpcUrl || !chainId) {
throw new Error(`Invalid blockchain specified: ${selectedBlockchain}`);
}
const provider = new ethers.providers.JsonRpcProvider(rpcUrl, {
name: blockchain,
chainId,
});
const endBlock = await provider.getBlockNumber();
const network = process.env.NETWORK || "all";
console.log("endBlock: ", endBlock);
const PKPs = await fetchPKPs(
startBlock,
endBlock,
blockchain,
network,
provider
);
const csvData = convertToCSV(PKPs);
return { csvData, blocksData };
}
async function main() {
const {csvData} = await scan();
writePkpCSV(csvData);
writeBlocksCSV(startBlock, endBlock);
}