-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
139 lines (103 loc) · 4.28 KB
/
main.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
require('./consts.js');
let crypto = require('crypto');
let ed25519 = require('ed25519');
let http = require('http');
let url = require('url');
function SHA256(bytes){
let sha256 = crypto.createHash('sha256'); //sha256
sha256.update(bytes);
return sha256.digest();
}
function RIPEMD160(bytes){
let ripemd160 = crypto.createHash('ripemd160'); // RIPEMD160
ripemd160.update(bytes);
return ripemd160.digest();
}
function encodeBase64(buffer) {
if (!Buffer.isBuffer(buffer))
buffer = new Buffer(buffer);
let result = buffer.toString('base64');
let newStr = '';
for (let i = 0; i < result.length; i++) {
if (result[i] === 'O') newStr += '#'; else
if (result[i] === 'l') newStr += '@'; else
if (result[i] === '/') newStr += '$';
else newStr += result[i];
}
return newStr;
}
function calculateChecksum(privateKeyAndVersion){
if (!Buffer.isBuffer(privateKeyAndVersion) && typeof privateKeyAndVersion === 'string')
privateKeyAndVersion = Buffer.from(privateKeyAndVersion, 'hex');
let secondSHA = SHA256(SHA256(privateKeyAndVersion));
let checksum = secondSHA.slice(0, consts_ADDRESSES.PRIVATE_KEY.WIF.CHECK_SUM_LENGTH);
return checksum;
}
function generateAddressWIF(address){
let prefix = ( consts_ADDRESSES.ADDRESS.USE_BASE64 ? consts_ADDRESSES.ADDRESS.WIF.PREFIX_BASE64 : consts_ADDRESSES.ADDRESS.WIF.PREFIX_BASE58);
let suffix = ( consts_ADDRESSES.ADDRESS.USE_BASE64 ? consts_ADDRESSES.ADDRESS.WIF.SUFFIX_BASE64 : consts_ADDRESSES.ADDRESS.WIF.SUFFIX_BASE58);
address = Buffer.concat ( [ Buffer.from(consts_ADDRESSES.ADDRESS.WIF.VERSION_PREFIX,"hex"), address ]) ;
let checksum = calculateChecksum(address);
let addressWIF = Buffer.concat([
Buffer.from( prefix , "hex"),
address,
checksum,
Buffer.from( suffix, "hex")
]);
return encodeBase64(addressWIF);
}
function genwallet(WebdSeed){
let WebdKeypair, privateKey, publicKey, privateKeyAndVersion, checksum, privateKeyWIF, unencodedAddress, addressWIF, Wallet;
WebdKeypair = ed25519.MakeKeypair(WebdSeed);
privateKey = WebdKeypair.privateKey;
publicKey = WebdKeypair.publicKey;
privateKeyAndVersion = Buffer.concat( [ Buffer.from(consts_ADDRESSES.PRIVATE_KEY.WIF.VERSION_PREFIX, "hex"), privateKey] );
checksum = calculateChecksum(privateKeyAndVersion);
privateKeyWIF = Buffer.concat( [privateKeyAndVersion, checksum]);
unencodedAddress = RIPEMD160(SHA256(publicKey));
addressWIF = generateAddressWIF(unencodedAddress);
Wallet = '{"version":"0.1","address":"' + addressWIF + '","publicKey":"' + publicKey.toString('hex') + '","privateKey":"' + privateKeyWIF.toString('hex') + '"}';
return Wallet;
}
http.createServer(function(req,res){
var pathName= url.parse(req.url).pathname;
switch(pathName) {
case '/':
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
break;
case '/wallet/simple':
res.writeHead(200, { 'Content-Type': 'text/plain' });
var Seed = crypto.randomBytes(32);
var wallet = genwallet(Seed);
res.write(wallet);
res.end();
break;
case '/wallet/mnemonic':
var query = url.parse(req.url,true).query;
res.writeHead(200, { 'Content-Type': 'text/plain' });
if(Object.keys(query).length){
if(query.mnemonic){
var mnemonic = decodeURI(query.mnemonic);
var Seed = crypto.createHash('sha256').update(mnemonic).digest();
var wallet = genwallet(Seed);
res.write(wallet);
res.end();
break;
} else {
res.write('No mnemonic provided');
res.end();
break;
}
} else {
res.write('No mnemonic provided');
res.end();
break;
}
default:
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
break;
}
}).listen(3333);
console.log('Server started on localhost:3333; press Ctrl-C to terminate....');