Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
146 lines (112 loc) · 5.01 KB
/
Copy pathmain.js
File metadata and controls
146 lines (112 loc) · 5.01 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
const express = require('express');
const { Gateway, Wallets } = require('fabric-network');
const path = require('path');
const fs = require('fs');
const app = express();
app.use(express.json());
const PORT = 3000;
// Helper function to connect to the network and get the contract
async function getContract() {
const ccpPath = path.resolve('../organizations/peerOrganizations/org1.example.com/connection-org1.json');
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
// Create a new wallet for managing identities.
const walletPath = path.join(__dirname, 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
// Check if the admin user exists in the wallet
const identity = await wallet.get('admin');
if (!identity) {
throw new Error('Admin identity not found in wallet');
}
// Connect to the network gateway
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: 'admin', discovery: { enabled: true, asLocalhost: true } });
// Get the network and the contract
const network = await gateway.getNetwork('mychannel');
const contract = network.getContract('keyvaluechaincode'); // Replace with your chaincode name
return { contract, gateway };
}
// Add Certificate
app.post('/addCertificate', async (req, res) => {
try {
const { certificateID, userID, certificateType, issueDate, status } = req.body;
const { contract, gateway } = await getContract();
// Submit the transaction to add the certificate
await contract.submitTransaction('AddCertificate', certificateID, userID, certificateType, issueDate, status);
// Disconnect from the gateway
await gateway.disconnect();
res.status(200).json({ message: 'Certificate added successfully' });
} catch (error) {
console.error(`Failed to add certificate: ${error}`);
res.status(500).json({ message: 'Error adding certificate' });
}
});
// Update Certificate Status
app.post('/updateCertificateStatus', async (req, res) => {
try {
const { certificateID, newStatus } = req.body;
const { contract, gateway } = await getContract();
// Submit the transaction to update the certificate status
await contract.submitTransaction('UpdateCertificateStatus', certificateID, newStatus);
// Disconnect from the gateway
await gateway.disconnect();
res.status(200).json({ message: 'Certificate status updated successfully' });
} catch (error) {
console.error(`Failed to update certificate status: ${error}`);
res.status(500).json({ message: 'Error updating certificate status' });
}
});
// Get Certificate History
app.post('/getCertificateHistory', async (req, res) => {
try {
const { certificateID } = req.body;
const { contract, gateway } = await getContract();
// Evaluate the transaction to get the certificate history
const history = await contract.evaluateTransaction('GetCertificateHistory', certificateID);
// Disconnect from the gateway
await gateway.disconnect();
res.status(200).json(JSON.parse(history.toString()));
} catch (error) {
console.error(`Failed to get certificate history: ${error}`);
res.status(500).json({ message: 'Error fetching certificate history' });
}
});
// Get User Identity
app.post('/getUserIdentity', async (req, res) => {
try {
const { contract, gateway } = await getContract();
// Evaluate the transaction to get the user identity
const identity = await contract.evaluateTransaction('GetUserIdentity');
// Disconnect from the gateway
await gateway.disconnect();
res.status(200).json({ identity: identity.toString() });
} catch (error) {
console.error(`Failed to get user identity: ${error}`);
res.status(500).json({ message: 'Error fetching user identity' });
}
});
// Get Certificate
app.post('/getCertificate', async (req, res) => {
try {
const { certificateID } = req.body;
const { contract, gateway } = await getContract();
// Evaluate the transaction to get the certificate by ID
const certificate = await contract.evaluateTransaction('GetCertificate', certificateID);
// Disconnect from the gateway
await gateway.disconnect();
res.status(200).json(JSON.parse(certificate.toString()));
} catch (error) {
console.error(`Failed to get certificate: ${error}`);
res.status(500).json({ message: 'Error fetching certificate' });
}
});
// Serve the HTML form
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/user', (req, res) => {
res.sendFile(path.join(__dirname, 'user.html'));
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});