-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
120 lines (103 loc) · 3.61 KB
/
Copy pathserver.js
File metadata and controls
120 lines (103 loc) · 3.61 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
const crypto = require('crypto');
const config = require('./config');
const axios = require('axios');
var fs = require("fs");
var assert = require('chai').assert
let publicKey = fs.readFileSync("./key/user_key.pub", "utf8");
let privateKey = fs.readFileSync("./key/user_key.priv", "utf8");
let serverKey = fs.readFileSync("./key/server_key.pub", "utf8");
assert.isNotNull(publicKey, "Unable to load user public key")
assert.isNotNull(privateKey, "Unable to load user private key")
assert.isNotNull(serverKey, "Unable to load server public key")
function encrypt_data(data) {
let encrypted = ""
try {
let cipher = crypto.createCipher('aes-256-cbc', config.ENC_KEY);
encrypted = cipher.update(data, 'utf-8', 'hex');
encrypted += cipher.final('hex');
}
catch (e) {
console.log("Cannot encrypt data !!!")
console.log(e)
}
return encrypted
}
function decrypt_data(enc_data, dec_key) {
let decrypted = ""
try {
let decipher = crypto.createDecipher('aes-256-cbc', dec_key);
decrypted = decipher.update(enc_data, 'hex', 'utf-8');
decrypted += decipher.final('utf-8');
decrypted = JSON.parse(decrypted)
}
catch (e) {
console.log("Cannot decrypt data !!!")
console.log(e)
}
return decrypted
}
function encrypt_key() {
let enc_key = ""
try {
enc_key = crypto.publicEncrypt(serverKey, Buffer.from(JSON.stringify(config.ENC_KEY)));
enc_key = enc_key.toString('base64')
}
catch (e) {
console.log("Cannot encrypt key !!!")
console.log(e)
}
return enc_key
}
function decrypt_key(enc_key) {
let dec_key = ""
try {
dec_key = crypto.privateDecrypt(privateKey, Buffer.from(enc_key, "base64"));
dec_key = dec_key.toString('utf-8')
}
catch (e) {
console.log("Cannot decrypt key !!!")
console.log(e)
}
return dec_key
}
axios
.get(config.IH_URL + "/registerToken")
.then(res => {
console.log("Receiving token...")
assert.isNotNull(res.data.token, "Token is empty")
let token = 'OBC ' + res.data.token
const headers = {
'Content-Type': 'application/json',
'Authorization': token
}
const raw_data = {
password: "testobc123"
}
const data = {
encryptedkey: encrypt_key(),
encrypteddata: encrypt_data(JSON.stringify(raw_data)),
publickey: publicKey.replace(/(\r\n|\n|\r)/gm, "")
}
assert.isNotEmpty(data.encryptedkey, "Unable to encrypt key")
assert.isNotEmpty(data.encrypteddata, "Unable to encrypt data")
assert.isNotEmpty(data.publickey, "Unable to get public key")
axios
.post(config.IH_URL + config.IH_BC + "/web3.eth.personal/newAccount", data, {
headers: headers
})
.then(res => {
console.log("Receiving reply from Blockchain...")
assert.isUndefined(res.data.error, res.data.error)
assert.isNotNull(res.data.result.encryptedkey, "Unable to get encrypted key from server")
assert.isNotNull(res.data.result.encrypteddata, "Unable to get encrypted data from server")
let rcv_key = decrypt_key(res.data.result.encryptedkey)
let rcv_dat = decrypt_data(res.data.result.encrypteddata, rcv_key)
console.log("Received data:", rcv_dat)
})
.catch(error => {
console.log(error)
});
})
.catch(error => {
console.log(error)
});