-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
150 lines (123 loc) · 5.1 KB
/
index.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
/*
Copyright (c) 2021 Frigyes06
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
const elliptic = require('elliptic');
const MicroCoin = require('micro_coin')
const bs58 = require('bs58');
const Discord = require('discord.js');
require('dotenv').config();
const DISCORD_CHANNELS = process.env.DISCORD_CHANNELS.split(',');
var ec = new elliptic.ec("secp256k1");
var myKey = ec.keyPair({ "priv": process.env.PRIVATE_KEY, "privEnc": "hex" });
var accountApi = new MicroCoin.AccountApi();
var request = new MicroCoin.ChangeKeyRequest();
const client = new Discord.Client();
function hexFromBase58(base58) {
return bs58.decode(base58).toString('hex').toUpperCase();
}
try {
client.on('message', message => {
if (message.author.bot) {
return;
}
if (DISCORD_CHANNELS.indexOf(message.channel.id) < 0) { //channel id is set to Microcoin's
console.log("sent into wrong channel " + message.channel.id);
return;
}
const msg = message.content;
var words = msg.split(" ");
if (words.length > 1) {
message.channel.send('Csak a publikus kulcsodat küld!');
console.log('multiple word');
return;
}
else {
var decoded = "";
try {
decoded = hexFromBase58(msg);
}
catch (err) {
message.channel.send("Rossz a kulcsod!");
console.log(err);
return
}
console.log(decoded)
if (decoded[1] !== '1' || decoded[2] !== 'C' || decoded[3] !== 'A') {
message.channel.send("Rossz a kulcsod!");
console.log("wrong key");
return;
}
var sliced = decoded.slice(6, decoded.length - 8);
var length = sliced.length;
var X = sliced.slice(4, 68);
var Y = sliced.slice(length - 64, length);
accountApi.myAccounts({
"curveType": "secp256k1",
"x": myKey.getPublic().getX("hex"),
"y": myKey.getPublic().getY("hex")
}).then(myAccounts => {
var AccToChange = "";
var Accounts = myAccounts;
for (var i = 1; i < Accounts.length; i++) {
if (Accounts[i].balance == 0) {
AccToChange = Accounts[i].accountNumber;
break;
}
}
if (AccToChange === "") {
message.channel.send("Sajnos nincs most szabad számlám. Próbáld meg később!");
console.log(Accounts);
return;
}
request.setAccountNumber(AccToChange);
console.log("will give " + AccToChange);
request.setNewOwnerPublicKey({
"curveType": "secp256k1",
"x": X,
"y": Y
});
console.log("set new owner");
try {
accountApi.startChangeKey(request).then(function (transaction) {
var signature = myKey.sign(transaction.getHash());
transaction.signature = { "r": signature.r, "s": signature.s };
accountApi.commitChangeKey(transaction).then((response) => console.log(response)).catch(err => {
console.log("Api error: " + err);
message.channel.send("Hoppá! Valami hiba volt a tranzakció feldolgozásában. Kérlek próbálkozz újra kb. 4 perc múlva!");
return;
});
});
console.log("executed transaction");
message.channel.send("A(z) " + AccToChange.toString() + " számla mostantól a tied!");
}
catch (err) {
console.error(err);
return;
}
}).catch(err => {
console.error(err);
message.channel.send("Hoppá! Valami hiba volt a tranzakció feldolgozásában. Kérlek próbálkozz újra kb. 4 perc múlva!");
return;
});
}
});
}
catch (err) {
console.error(err);
return;
}
try {
client.login(process.env.DISCORD_TOKEN);
console.log('The bot is up and running!')
}
catch (err) {
console.error(err);
}