-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
262 lines (237 loc) · 7.09 KB
/
cli.js
File metadata and controls
262 lines (237 loc) · 7.09 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
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
const crypto = require('crypto');
const { generateKeyPair } = require('crypto');
const axios = require('axios');
const fs = require('fs');
const rl = require('readline-sync');
const env = JSON.parse(fs.readFileSync('./config.json'));
const me = env["me"];
const opts = ["Add Alias", "Generate Key Pair", "View Balance", "Do a Transactions"];
console.log(me);
let ind = rl.keyInSelect(opts, 'Press the correct digit to perform the action');
switch(ind)
{
case 0:
addAlias();
break;
case 1:
keygen();
break;
case 2:
viewBal();
break;
case 3:
doTxn();
break;
default:
console.log("Exiting");
f = 1;
break;
}
function addAlias()
{
const alias = rl.question("Enter new alias: ");
const path = rl.question("Enter path to public key: ");
const pubKey = fs.readFileSync(path, 'utf-8');
axios.post(me + '/addAlias', {
"alias": alias,
"publicKey" : pubKey
}).then(res => {
console.log('Alias added');
}).catch(err => {
console.log('Not added');
});
}
function keygen()
{
generateKeyPair('rsa', {
modulusLength: 2048,
publicKeyEncoding:
{
type: 'spki',
format: 'pem'
},
privateKeyEncoding:
{
type: 'pkcs8',
format: 'pem'
}
}, (err, pubKey, privKey) =>
{
if(err)
console.log("Error encountered: ", err);
else
{
console.log("Key pair generated successfully");
fs.writeFileSync("pubKey.pem", pubKey);
fs.writeFileSync("privKey.pem", privKey);
console.log("contents written to ./pubKey.pem and ./privKey.pem");
}
});
}
function viewBal()
{
let ans = rl.keyIn('Enter 1 to enter alias or 2 to give path to public key: ');
if(ans === '2')
{
let pubKey = fs.readFileSync(rl.question("Enter path to public key file: "), 'utf-8');
axios.post(me + '/getUnusedOutputs', {
publicKey: pubKey
}).then(res => {
let unusedOutputs = res.data.unusedOutputs;
let bal = 0n;
for (let i = 0; i < unusedOutputs.length; i++)
bal += BigInt(unusedOutputs[i].amount);
console.log("Your balance is: " + bal + " coins");
}).catch(err => {
console.log(err.response.data);
});
}
else if(ans === '1')
{
let alias = rl.question("Enter the alias: ");
axios.post(me + '/getUnusedOutputs', {
alias: alias
}).then(res => {
let unusedOutputs = res.data.unusedOutputs;
let bal = 0n;
for (let i = 0; i < unusedOutputs.length; i++)
bal += BigInt(unusedOutputs[i].amount);
console.log("Your balance is: " + bal + " coins");
}).catch(err => {
console.log(err.response.data);
});
}
}
async function doTxn()
{
let pubKey = fs.readFileSync(rl.question('Enter path to your public Key: '), 'utf-8');
let privKey = fs.readFileSync(rl.question('Enter path to your private Key: '), 'utf-8');
let unusedOutputs = [];
let bal = 0n;
await axios.post(me + '/getUnusedOutputs', {
publicKey: pubKey
}).then(res => {
unusedOutputs = res.data.unusedOutputs;
for (let i = 0; i < unusedOutputs.length; i++)
bal += BigInt(unusedOutputs[i].amount);
console.log("Your balance is: " + bal + " coins");
}).catch(err => {
console.log(err.response.data);
});
let numOutputs = rl.questionInt("Enter number of outputs: ");
let outputs = [];
let total = 0n;
for(let i = 0; i < numOutputs; ++i)
{
let ans = rl.question("Enter alias of recipient, or path to public Key of the recipient: ");
let output = {};
try{
output["recipient"] = fs.readFileSync(ans, 'utf-8');
} catch (err) {
await axios.post(me + '/getPublicKey', {
alias: ans
}).then(res => {
output["recipient"] = res.data.publicKey;
console.log("Recipient public Key received");
}).catch(err => {
console.log(err.response.data);
});
}
output["amount"] = rl.question("Enter the number of coins to pay: ");
total += BigInt(output["amount"]);
console.log(output);
outputs.push(output);
}
let fee = BigInt(rl.question("Pls enter the amount to leave as a transaction fee, and make a miner's day :)"));
total += fee;
if(bal < total)
{
console.log("You spent more than you own :( ");
return;
}
let left = bal;
left = BigInt(left.toString() - total.toString());
if(left > 0n)
{
let output = {};
output["recipient"] = pubKey;
output["amount"] = left.toString();
outputs.push(output);
numOutputs++;
}
let mainBuf = Buffer.alloc(68);
let outHash = getOutputsHash(numOutputs, outputs);
mainBuf.write(outHash, 36,32,'hex');
let inputs = [];
for(let i = 0; i < unusedOutputs.length; ++i)
{
mainBuf.write(unusedOutputs[i]["transactionId"], 0, 32, 'hex');
mainBuf.write(pushInt(unusedOutputs[i]["index"],4,false), 32, 4, 'hex');
console.log(mainBuf.toString('hex'));
const sign = crypto.createSign('SHA256');
sign.update(mainBuf);
let signature = sign.sign({key:privKey, padding:crypto.constants.RSA_PKCS1_PSS_PADDING, saltLength:32}, 'hex');
let input = {};
input["transactionId"] = unusedOutputs[i]["transactionId"];
input["index"] = unusedOutputs[i]["index"];
input["signature"] = signature;
console.log(input);
inputs.push(input);
}
axios.post(me + '/newTransaction', {
inputs : inputs,
outputs : outputs
}).then(res => {
console.log("Txn sent!");
}).catch(err => {
console.log(err);
})
}
function pushInt(num, size = 4, file = true)
{
let arr = new Uint8Array(size);
if(size === 4)
for(let i = 0; i < size; ++i)
{
arr[size-i-1] = num%256;
num = num >> 8;
}
else
{
num = BigInt(num);
for(let i = 0; i < size; ++i)
{
arr[size-i-1] = parseInt(num%256n);
num = num/256n;
}
}
if (file === true)
{
fs.appendFileSync("tempcli.dat", arr);
return;
}
else
{
return Buffer.from(arr).toString('hex');
}
}
function getOutputsHash(numOutputs, outputs)
{
pushInt(numOutputs);
for(let output of outputs)
{
pushInt(output["amount"], 8);
pushInt(output["recipient"].length);
pushText(output["recipient"]);
}
let buf = fs.readFileSync("tempcli.dat");
let hash = crypto.createHash('sha256').update(buf).digest('hex');
fs.unlinkSync("tempcli.dat");
return hash;
}
function pushText(txt)
{
let arr = new Uint8Array(Buffer.from(txt, 'utf-8'));
fs.appendFileSync("tempcli.dat", arr);
return;
}