forked from HermanL02/Koii-Task-Funder-Express
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (117 loc) · 5.23 KB
/
index.js
File metadata and controls
137 lines (117 loc) · 5.23 KB
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
const express = require('express');
const { FundTask, KPLEstablishConnection, KPLFundTask, getTaskStateInfo, KPLCheckProgram } = require('@_koii/create-task-cli');
const { establishConnection, checkProgram } = require('@_koii/create-task-cli');
const {PublicKey, Connection,Keypair} = require('@_koii/web3.js');
const crypto = require('crypto');
const { parse } = require('path');
const axios = require('axios');
const app = express();
const port = 3000;
const SIGNING_SECRET = process.env.SIGNING_SECRET
const funder_keypair = process.env.funder_keypair
const user_id_list = ['U06NM9A2VC1', 'U02QTSK9R3N', 'U02QNL3PPFF']
app.use(express.raw({ type: 'application/x-www-form-urlencoded' }));
function verifySlackRequest(req) {
const slackSignature = req.headers['x-slack-signature'];
const timestamp = req.headers['x-slack-request-timestamp'];
const fiveMinutesAgo = Math.floor(Date.now() / 1000) - (60 * 5);
// Prevent replay attacks by checking timestamp
if (timestamp < fiveMinutesAgo) {
return false; // Request is too old
}
const sigBasestring = `v0:${timestamp}:${req.body.toString()}`;
const hmac = crypto.createHmac('sha256', SIGNING_SECRET);
const mySignature = 'v0=' + hmac.update(sigBasestring).digest('hex');
// Constant time comparison to prevent timing attacks
return crypto.timingSafeEqual(Buffer.from(mySignature, 'utf8'), Buffer.from(slackSignature, 'utf8'));
}
// Route to handle funding task
app.post('/fundtask', async (req, res) => {
if (!verifySlackRequest(req)) {
return res.status(400).send('Invalid request signature');
}
// Required
res.send('Request received and verified integrity.');
const rawBody = req.body.toString('utf8');
console.log('Raw Body:', rawBody);
const bodyParams = new URLSearchParams(rawBody);
const parsedBody = Object.fromEntries(bodyParams.entries());
console.log('Parsed Body:', parsedBody);
const text = parsedBody.text;
const response_url = parsedBody.response_url;
const user_id = parsedBody.user_id;
if (!user_id || !user_id_list.includes(user_id)) {
await axios.post(response_url, {
response_type: "in_channel",
text: 'Sorry, please tag <@U06NM9A2VC1> to add you to the list! '
})
}
let parts = text.split(' ').filter(part => part.trim() !== '');
let TASK_ID = parts[0].trim();
let AMOUNT = parts[1].trim();
try{
await generic_fund_task(TASK_ID, AMOUNT)
await axios.post(response_url, {
response_type: "in_channel",
text: `Congrats! <@${user_id}> You funded ${AMOUNT} to task ${TASK_ID} successfully. `
})
}catch(e){
await axios.post(response_url, {
response_type: "in_channel",
text: `Failed to fund ${AMOUNT} to ${TASK_ID}. ${e}`
})
}
});
app.listen(port, () => {
console.log(`App running on port ${port}`);
});
async function generic_fund_task(TASK_ID, AMOUNT){
const connection = new Connection("https://testnet.koii.network", "confirmed");
const taskStateJSON = await getTaskStateInfo(
connection,
TASK_ID,
);
const stakePotAccount = new PublicKey(taskStateJSON.stake_pot_account, connection);
if (taskStateJSON.token_type) {
const mint_uint8 = Uint8Array.from(taskStateJSON.token_type);
// Create the PublicKey
const mint_publicKey = new PublicKey(mint_uint8);
await fund_a_KPL_task(TASK_ID, AMOUNT, stakePotAccount, connection, mint_publicKey)
}else{
await fund_a_task(TASK_ID, AMOUNT, stakePotAccount, connection)
}
}
async function fund_a_task(TASK_ID, AMOUNT, stakePotAccount,connection){
console.log("Start Funding:");
console.log("Funding task with Id: ", TASK_ID);
console.log("Funding amount: ", AMOUNT);
const payerKeypairString = process.env.funder_keypair;
// Parse the JSON string into an array
const payerKeypairArray = JSON.parse(payerKeypairString);
// Convert the array to a Uint8Array
const payerWallet = Uint8Array.from(payerKeypairArray);
const payerKeypair = Keypair.fromSecretKey(payerWallet);
const taskStateInfoAddress = new PublicKey(TASK_ID);
const amount = parseInt(AMOUNT);
// Create-task-cli package setup
await establishConnection(connection);
await checkProgram();
await FundTask(payerKeypair,taskStateInfoAddress,stakePotAccount, amount);
}
async function fund_a_KPL_task(TASK_ID, AMOUNT, stakePotAccount,connection, mint_publicKey){
console.log("Start Funding:");
console.log("Funding task with Id: ", TASK_ID);
console.log("Funding amount: ", AMOUNT);
const payerKeypairString = funder_keypair
// Parse the JSON string into an array
const payerKeypairArray = JSON.parse(payerKeypairString);
// Convert the array to a Uint8Array
const payerWallet = Uint8Array.from(payerKeypairArray);
const payerKeypair = Keypair.fromSecretKey(payerWallet);
const taskStateInfoAddress = new PublicKey(TASK_ID);
const amount = parseInt(AMOUNT);
// Create-task-cli package setup
await KPLEstablishConnection(connection);
await KPLCheckProgram();
await KPLFundTask(payerKeypair,taskStateInfoAddress, stakePotAccount, amount, mint_publicKey);
}