-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
83 lines (73 loc) · 2.34 KB
/
server.js
File metadata and controls
83 lines (73 loc) · 2.34 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
// server.js
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const oracledb = require('oracledb');
const Web3 = require('web3');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 8080;
// Middleware
app.use(cors());
app.use(helmet());
app.use(express.json());
// Web3 (Ethereum Public)
const web3 = new Web3("https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID");
// Oracle DB Setup
oracledb.initOracleClient({ libDir: process.env.ORACLE_LIB_PATH });
async function getDBConnection() {
return await oracledb.getConnection({
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
connectionString: process.env.DB_CONNECT_STRING,
});
}
// Root
app.get('/', (req, res) => {
res.send('SADC CBDC Public Backend is live.');
});
// 1. Public FAQs
app.get('/api/faqs', async (req, res) => {
try {
const conn = await getDBConnection();
const result = await conn.execute(`SELECT question, answer FROM cbdc_faqs`);
res.json(result.rows.map(([q, a]) => ({ q, a })));
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// 2. Wallet Registration
app.post('/api/register-wallet', async (req, res) => {
const { idNumber, phone, walletAddress } = req.body;
try {
const conn = await getDBConnection();
await conn.execute(
`INSERT INTO cbdc_wallets (id_number, phone, wallet_address) VALUES (:id, :phone, :wallet)`,
[idNumber, phone, walletAddress],
{ autoCommit: true }
);
res.json({ status: 'Wallet registered successfully', walletAddress });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// 3. Wallet Balance Check
app.get('/api/wallet-balance', async (req, res) => {
const { walletAddress } = req.query;
try {
const balance = await web3.eth.getBalance(walletAddress);
res.json({ balance: web3.utils.fromWei(balance, 'ether') });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// 4. Help / Contact Form
app.post('/api/contact', (req, res) => {
const { name, email, message } = req.body;
console.log(`[HELP] ${name} | ${email} | ${message}`);
res.json({ status: 'Message received. Our team will contact you shortly.' });
});
// Start Server
app.listen(port, () => {
console.log(`CBDC Backend running at http://localhost:${port}`);
});