-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendByFile.js
196 lines (177 loc) · 5.18 KB
/
sendByFile.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
const {
ContainerType,
ByteVectorType,
NumberUintType,
} = require("@chainsafe/ssz");
const { BigNumber } = require("bignumber.js");
const { ethers } = require("ethers");
const fs = require("fs");
const path = require("path");
require("dotenv").config();
const providerUrl = process.env.PROVIDER_URL;
const privateKey = process.env.PRIVATE_KEY;
const contractAddress = process.env.CONTRACT_ADDRESS;
const contractABI = [
{ inputs: [], stateMutability: "nonpayable", type: "constructor" },
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "bytes",
name: "pubkey",
type: "bytes",
},
{
indexed: false,
internalType: "bytes",
name: "withdrawal_credentials",
type: "bytes",
},
{
indexed: false,
internalType: "bytes",
name: "amount",
type: "bytes",
},
{
indexed: false,
internalType: "bytes",
name: "signature",
type: "bytes",
},
{ indexed: false, internalType: "bytes", name: "index", type: "bytes" },
],
name: "DepositEvent",
type: "event",
},
{
inputs: [
{ internalType: "bytes", name: "pubkey", type: "bytes" },
{
internalType: "bytes",
name: "withdrawal_credentials",
type: "bytes",
},
{ internalType: "bytes", name: "signature", type: "bytes" },
{ internalType: "bytes32", name: "deposit_data_root", type: "bytes32" },
],
name: "deposit",
outputs: [],
stateMutability: "payable",
type: "function",
},
{
inputs: [],
name: "get_deposit_count",
outputs: [{ internalType: "bytes", name: "", type: "bytes" }],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "get_deposit_root",
outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
stateMutability: "view",
type: "function",
},
{
inputs: [{ internalType: "bytes4", name: "interfaceId", type: "bytes4" }],
name: "supportsInterface",
outputs: [{ internalType: "bool", name: "", type: "bool" }],
stateMutability: "pure",
type: "function",
},
];
const depositDataContainer = new ContainerType({
fields: {
pubkey: new ByteVectorType({ length: 48 }),
withdrawalCredentials: new ByteVectorType({ length: 32 }),
amount: new NumberUintType({ byteLength: 8 }),
signature: new ByteVectorType({ length: 96 }),
},
});
function buf2hex(buffer) {
return Array.prototype.map
.call(new Uint8Array(buffer), (x) => `00${x.toString(16)}`.slice(-2))
.join("");
}
function createLockDirectory(filePath) {
const lockDir = `${filePath}-locks`;
if (!fs.existsSync(lockDir)) {
fs.mkdirSync(lockDir);
}
return lockDir;
}
function checkLockFile(lockDir, pubkey) {
const lockFilePath = path.join(lockDir, `${pubkey}.lock`);
return fs.existsSync(lockFilePath);
}
function createLockFile(lockDir, pubkey) {
const lockFilePath = path.join(lockDir, `${pubkey}.lock`);
fs.writeFileSync(lockFilePath, "");
}
async function processFile(filePath) {
const provider = new ethers.providers.JsonRpcProvider(providerUrl);
const wallet = new ethers.Wallet(privateKey, provider);
const contract = new ethers.Contract(contractAddress, contractABI, wallet);
const depositData = JSON.parse(fs.readFileSync(filePath, "utf8"));
const lockDir = createLockDirectory(filePath);
for (const data of depositData) {
const pubkeyHex = data.pubkey;
if (checkLockFile(lockDir, pubkeyHex)) {
console.log(
`Transaction for pubkey ${pubkeyHex} already processed. Skipping.`
);
continue;
}
const pubkeyBytes = Buffer.from(pubkeyHex, "hex");
const withdrawalCredentialsBytes = Buffer.from(
data.withdrawal_credentials,
"hex"
);
const amount = data.amount;
const signatureBytes = Buffer.from(data.signature, "hex");
const transactionParameters = {
value: ethers.utils.parseEther(String(amount / 1e9)),
gasLimit: process.env.GAS_LIMIT,
gasPrice: ethers.utils.parseUnits(process.env.GAS_PRICE, "gwei"),
};
const reconstructedKeyFile = {
pubkey: pubkeyBytes,
withdrawalCredentials: withdrawalCredentialsBytes,
amount: amount,
signature: signatureBytes,
};
try {
const tx = await contract.deposit(
reconstructedKeyFile.pubkey,
reconstructedKeyFile.withdrawalCredentials,
reconstructedKeyFile.signature,
`0x${buf2hex(depositDataContainer.hashTreeRoot(reconstructedKeyFile))}`,
transactionParameters
);
console.log("Transaction sent. Hash:", tx.hash);
createLockFile(lockDir, pubkeyHex);
} catch (error) {
console.error("Error sending transaction:", error);
}
}
}
async function main() {
// Check if a file path argument is provided
const args = process.argv.slice(2);
if (args.length !== 1) {
console.error("Usage: node script.js <file_path>");
process.exit(1);
}
const filePath = args[0];
// Check if the file exists
if (!fs.existsSync(filePath)) {
console.error(`File not found: ${filePath}`);
process.exit(1);
}
// Process the specified file
await processFile(filePath);
}
main();