-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash.ts
174 lines (167 loc) · 4.98 KB
/
hash.ts
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
import type { Handler, Context, Callback } from "aws-lambda";
import { PublicKey, Poseidon, Mina, AccountUpdate, PrivateKey } from "o1js";
import { getDeployer } from "./src/mina/deployers";
import { MinaNFT, sleep, fetchMinaAccount, initBlockchain } from "minanft";
import { minaInit } from "./src/mina/init";
import { GASTANKS } from "./src/mina/gastanks";
import { rateLimit, initializeRateLimiter } from "./src/api/rate-limit";
initializeRateLimiter({
name: "hash",
points: 180,
duration: 60,
});
const FAUCET_AMOUNT = 50_000_000_000n;
const MINIMUM_BALANCE = 70;
const calculate: Handler = async (
event: any,
context: Context,
callback: Callback
) => {
const ip = event?.requestContext?.identity?.sourceIp ?? "no-ip";
if (
await rateLimit({
name: "hash",
key: ip,
})
) {
console.log("rate limit", ip);
callback(null, {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
},
body: "error: rate limit exceeded",
});
return;
}
try {
console.time("hash");
//console.log("event", event);
const body = JSON.parse(event.body);
console.log("hash started", ip, body);
if (
body.auth === undefined ||
body.auth !== process.env.BOTAPIAUTH! ||
body.publicKey === undefined ||
body.publicKey === ""
) {
console.error("Wrong call format");
callback(null, {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify({
hash: "",
isCalculated: false,
reason: "Wrong call format",
}),
});
return;
}
const publicKey = PublicKey.fromBase58(body.publicKey);
console.log("publicKey", publicKey.toBase58());
if (body.faucet === "true") {
let deployer: PrivateKey | undefined;
let amount = FAUCET_AMOUNT;
if (body.chain === "zeko") {
await initBlockchain("zeko");
deployer = PrivateKey.fromBase58(
GASTANKS[Math.floor(Math.random() * (GASTANKS.length - 1))]
);
amount = 1_000_000_000_000n;
} else {
await minaInit();
deployer = await getDeployer(MINIMUM_BALANCE);
}
if (deployer === undefined) {
console.error("Faucet: No deployer available");
callback(null, {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify({
hash: "",
isCalculated: false,
reason: "Grahql endpoint error",
}),
});
return;
}
const sender = deployer.toPublicKey();
await fetchMinaAccount({ publicKey: sender });
await fetchMinaAccount({ publicKey });
const hasAccount = Mina.hasAccount(publicKey);
const hasFunds = Mina.hasAccount(sender);
if (!hasFunds) {
callback(null, {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify({
hash: "",
isCalculated: false,
reason: "Grahql endpoint error",
}),
});
}
const transaction = await Mina.transaction(
{ sender, fee: "100000000", memo: "minanft.io faucet" },
async () => {
const senderUpdate = AccountUpdate.createSigned(sender);
if (!hasAccount) senderUpdate.balance.subInPlace(1_000_000_000n);
senderUpdate.send({ to: publicKey, amount });
}
);
transaction.sign([deployer]);
const tx = await transaction.safeSend();
console.log("tx", tx);
const hash = tx.hash;
console.log("tx hash", hash);
await sleep(1000);
console.timeEnd("hash");
callback(null, {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify({ hash: hash ?? "", isCalculated: true }),
});
return;
} else {
const hash = Poseidon.hash(publicKey.toFields());
console.log("hash", hash.toJSON());
console.timeEnd("hash");
callback(null, {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify({ hash: hash.toJSON(), isCalculated: true }),
});
}
} catch (error) {
console.error("catch", (error as any).toString());
callback(null, {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify({
hash: "",
isCalculated: false,
reason: (error as any).toString(),
}),
});
}
};
export { calculate };