-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileread.js
More file actions
106 lines (84 loc) · 3.29 KB
/
fileread.js
File metadata and controls
106 lines (84 loc) · 3.29 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
var fs = require('fs').promises;
async function load_certificates_from_wallet(user_name) {
let certificates_json = {};
let cert_path = './wallet/'+user_name+'/';
try {
//get all filenames under a directory
var file_list = await fs.readdir(cert_path);
console.log(file_list);
for (index = 0; index < file_list.length; index++) {
console.log(file_list[index]);
if (file_list[index].includes('-priv')) {
let full_file_path = cert_path + file_list[index];
//read file contents to a string
certificates_json['User_Private_Key'] = await fs.readFile(full_file_path, "utf8");
}
if (file_list[index].includes('-pub')) {
let full_file_path = cert_path + file_list[index];
//read file contents to a string
certificates_json['User_Public_Key'] = await fs.readFile(full_file_path, "utf8");
}
if (file_list[index].includes(user_name)) {
let full_file_path = cert_path + file_list[index];
//read file contents to a string
certificates_json['User_Enrollment_Certificate'] = await fs.readFile(full_file_path, "utf8");
}
}
}
catch(e) {
console.log(e);
}
finally {
return certificates_json;
}
}
async function checkDirectorySync(directory) {
try {
await fs.stat(directory);
} catch(e) {
await fs.mkdir(directory);
}
}
async function write_certificates_from_db_to_wallet(db_user_info,enrollment_signingIdentity) {
let write_status;
let user_name = db_user_info['User_Name'];
let cert_path = './wallet/'+user_name+'/';
try {
//get all filenames under a directory
await checkDirectorySync(cert_path);
var file_list = await fs.readdir(cert_path);
if(file_list.length==0){
console.log(db_user_info["User_Private_Key"]);
await fs.writeFile(cert_path+enrollment_signingIdentity+"-priv", db_user_info["User_Private_Key"]);
await fs.writeFile(cert_path+enrollment_signingIdentity+"-pub", db_user_info["User_Public_Key"]);
await fs.writeFile(cert_path+user_name, db_user_info["User_Enrollment_Certificate"]);
write_status = "success";
}
else {
write_status = "success";
}
}
catch(e) {
write_status = e;
}
finally {
return write_status;
}
}
// async function check_file_load_to_wallet(user_name) {
// let load_status = await write_certificates_from_db_to_wallet(user_name);
// console.log(load_status);
// }
// let user_info = {};
// user_info["User_Name"] = "ark";
// user_info["User_Password"] = "ark";
// check_file_load_to_wallet(user_info);
// async function check_file_load_to_db(user_name) {
// let load_status = await load_certificates_from_wallet(user_name);
// console.log(load_status);
// }
// await check_file_load_to_db('ark');
module.exports = {
load_certificates_from_wallet : load_certificates_from_wallet,
write_certificates_from_db_to_wallet : write_certificates_from_db_to_wallet
};