/
main.ts
299 lines (264 loc) · 9.36 KB
/
main.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
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
import * as index from './index';
import * as httpsRateLimit from 'https-rate-limit';
const bananodeUrl = 'https://kaliumapi.appditto.com/api';
const commands = new Map<string, (arg0: string, arg1: string, arg2: string, arg3: string) => Promise<string>>();
commands.set('getseed', async (arg0: string, arg1: string, arg2: string, arg3: string): Promise<string> => {
const response = index.getSeed();
console.log('getseed response', response);
return response;
});
commands.set('bgetprivatekey', async (arg0: string, arg1: string, arg2: string, arg3: string): Promise<string> => {
const response = index.getPrivateKeyFromSeed(arg0, Number(arg1));
console.log('banano getprivatekey response', response);
return response;
});
commands.set('bgetaccount', async (arg0: string, arg1: string, arg2: string, arg3: string): Promise<string> => {
const publicKey = await index.getPublicKeyFromPrivateKey(arg0);
console.log('banano getaccount publicKey', publicKey);
const account = index.getAccountFromPublicKey(publicKey);
console.log('banano getaccount account', account);
return account;
});
commands.set('bcheckpending', async (arg0: string, arg1: string, arg2: string, arg3: string): Promise<string> => {
let account = arg0;
let countStr: string = arg1;
if (countStr == undefined) {
countStr = '1';
}
const count = parseInt(countStr, 10);
const accounts = [account];
// https://docs.nano.org/commands/rpc-protocol/#accounts_pending
const formData = {
action: 'accounts_pending',
accounts: accounts,
count: count,
threshold: 1,
};
httpsRateLimit.setUrl(bananodeUrl);
console.log('formData', formData);
const pending = await httpsRateLimit.sendRequest(formData);
console.log('banano checkpending response', pending);
return pending;
});
commands.set('breceive', async (arg0: string, arg1: string, arg2: string, arg3: string): Promise<string> => {
const privateKey = arg0;
const hash = arg1;
const publicKey = await index.getPublicKeyFromPrivateKey(privateKey);
const account = index.getAccountFromPublicKey(publicKey);
httpsRateLimit.setUrl(bananodeUrl);
const repReq = {
action: 'account_representative',
accounts: account,
};
const repResp = await httpsRateLimit.sendRequest(repReq);
let representative = repResp.representative;
if (!representative) {
representative = account;
}
const historyReq = {
action: 'account_history',
account: account,
count: 1,
};
const historyResp = await httpsRateLimit.sendRequest(historyReq);
const historyHistory = historyResp.history;
const pendingReq = {
action: 'accounts_pending',
accounts: [account],
count: 1,
threshold: 1,
};
const pendingResp = await httpsRateLimit.sendRequest(pendingReq);
let badPendingResponse: string = '';
if (pendingResp.blocks == undefined) {
badPendingResponse = 'pendingResp.blocks == undefined';
} else if (pendingResp.blocks[account] == undefined) {
badPendingResponse = 'pendingResp.blocks[account] == undefined';
} else if (pendingResp.blocks[account][hash] == undefined) {
badPendingResponse = 'pendingResp.blocks[account][hash] == undefined';
}
if (badPendingResponse.length != 0) {
console.log('banano breceive badPendingResponse', badPendingResponse, pendingResp);
return '';
}
const pendingValueRaw = pendingResp.blocks[account][hash];
if (historyHistory.length == 0) {
const block: index.Block = {
type: 'state',
account: account,
previous: '0000000000000000000000000000000000000000000000000000000000000000',
representative: representative,
balance: pendingValueRaw,
link: hash,
signature: '',
};
block.signature = await index.signBlock(privateKey, block);
const processReq = {
action: 'process',
json_block: 'true',
subtype: 'open',
block: block,
do_work: false,
};
// kalium API specific code.
if (block.work == undefined) {
processReq.do_work = true;
}
console.log('banano receive processReq', processReq);
const processResp = await httpsRateLimit.sendRequest(processReq);
console.log('banano receive processResp', processResp);
return processResp;
} else {
const accountInfoReq = {
action: 'account_info',
account: account,
count: 1,
};
const accountInfoResp = await httpsRateLimit.sendRequest(accountInfoReq);
const previous = accountInfoResp.frontier;
const accountBalanceRaw = accountInfoResp.balance;
const valueRaw = (BigInt(pendingValueRaw) + BigInt(accountBalanceRaw)).toString();
const block: index.Block = {
type: 'state',
account: account,
previous: previous,
representative: representative,
balance: valueRaw,
link: hash,
signature: '',
};
block.signature = await index.signBlock(privateKey, block);
const processReq = {
action: 'process',
json_block: 'true',
subtype: 'receive',
block: block,
do_work: false,
};
if (block.work == undefined) {
processReq.do_work = true;
}
console.log('banano receive processReq', processReq);
const processResp = await httpsRateLimit.sendRequest(processReq);
console.log('banano receive processResp', processResp);
return processResp;
}
});
commands.set('bsendraw', async (arg0: string, arg1: string, arg2: string, arg3: string): Promise<string> => {
const privateKey = arg0;
const destAccount = arg1;
const amountRaw = arg2;
httpsRateLimit.setUrl(bananodeUrl);
const publicKey = await index.getPublicKeyFromPrivateKey(privateKey);
const accountAddress = index.getAccountFromPublicKey(publicKey);
const accountInfoReq = {
action: 'account_info',
account: accountAddress,
count: 1,
representative: true,
};
const accountInfoResp = await httpsRateLimit.sendRequest(accountInfoReq);
if (accountInfoResp == undefined) {
throw Error(`The server's account info cannot be retrieved, please try again.`);
}
const previous = accountInfoResp.frontier;
const balanceRaw = accountInfoResp.balance;
const representative = accountInfoResp.representative;
if (balanceRaw == undefined) {
throw Error(`The server's account balance cannot be retrieved, please try again.`);
}
if (BigInt(balanceRaw) < BigInt(amountRaw)) {
const balance = index.getAmountPartsFromRaw(balanceRaw);
const amount = index.getAmountPartsFromRaw(amountRaw);
const balanceMajorAmount = balance.banano;
const amountMajorAmount = amount.banano;
throw Error(`The server's account balance of ${balanceMajorAmount} banano is too small, cannot withdraw ${amountMajorAmount} banano. In raw ${balanceRaw} < ${amountRaw}.`);
}
const remaining = BigInt(balanceRaw) - BigInt(amountRaw);
const remainingDecimal = remaining.toString(10);
const remainingPadded = remaining.toString(16).padStart(32, '0');
const destPublicKey = index.getPublicKeyFromAccount(destAccount);
const block: index.Block = {
type: 'state',
account: accountAddress,
previous: previous,
representative: representative,
balance: remainingDecimal,
link: destPublicKey,
signature: '',
};
block.signature = await index.signBlock(privateKey, block);
const processReq = {
action: 'process',
json_block: 'true',
subtype: 'send',
block: block,
do_work: false,
};
if (block.work == undefined) {
processReq.do_work = true;
}
console.log('banano send processReq', processReq);
const processResp = await httpsRateLimit.sendRequest(processReq);
console.log('banano send processResp', processResp);
return processResp;
});
commands.set('bchangerep', async (arg0: string, arg1: string, arg2: string, arg3: string): Promise<string> => {
const privateKey = arg0;
const representative = arg1;
httpsRateLimit.setUrl(bananodeUrl);
const publicKey = await index.getPublicKeyFromPrivateKey(privateKey);
const accountAddress = index.getAccountFromPublicKey(publicKey);
const accountInfoReq = {
action: 'account_info',
account: accountAddress,
count: 1,
representative: true,
};
const accountInfoResp = await httpsRateLimit.sendRequest(accountInfoReq);
const previous = accountInfoResp.frontier;
const balanceRaw = accountInfoResp.balance;
const block: index.Block = {
type: 'state',
account: accountAddress,
previous: previous,
representative: representative,
balance: balanceRaw,
link: '0000000000000000000000000000000000000000000000000000000000000000',
signature: '',
};
block.signature = await index.signBlock(privateKey, block);
const processReq = {
action: 'process',
json_block: 'true',
subtype: 'change',
block: block,
do_work: false,
};
if (block.work == undefined) {
processReq.do_work = true;
}
console.log('banano send processReq', processReq);
const processResp = await httpsRateLimit.sendRequest(processReq);
console.log('banano send processResp', processResp);
return processResp;
});
const run = async () => {
console.log('bananots');
if (process.argv.length < 3) {
console.log('#usage:');
} else {
const command = process.argv[2];
const arg0 = process.argv[3];
const arg1 = process.argv[4];
const arg2 = process.argv[5];
const arg3 = process.argv[6];
const fn = commands.get(command);
if (fn == undefined) {
console.log('unknown command', command);
} else {
await fn(arg0, arg1, arg2, arg3);
}
}
};
run();