matiru
Blockchain-Based Supply Chain Transparency for Agricultural Producegit clone https://github.com/maydayv7/matiru
backend/initUsers.js (3444B)
1"use strict";
2require("dotenv").config();
3const fs = require("fs");
4const path = require("path");
5const bcrypt = require("bcryptjs");
6const { Gateway, Wallets } = require("fabric-network");
7
8// Demo Users
9const users = [
10 {
11 id: "farmer1",
12 org: "Org1",
13 username: "farmer1",
14 password: "password",
15 role: "Farmer",
16 name: "Farmer One",
17 location: "Village A",
18 certification: ["Organic Certified"],
19 },
20 {
21 id: "distributor1",
22 org: "Org2",
23 username: "dist1",
24 password: "password",
25 role: "Distributor",
26 name: "Distributor One",
27 location: "City Warehouse",
28 },
29 {
30 id: "retailer1",
31 org: "Org3",
32 username: "ret1",
33 password: "password",
34 role: "Retailer",
35 name: "Retailer One",
36 location: "Market B",
37 },
38 {
39 id: "inspector1",
40 org: "Org4",
41 username: "insp1",
42 password: "password",
43 role: "Inspector",
44 name: "Inspector One",
45 location: "Lab C",
46 },
47];
48
49async function seedBackendUsers() {
50 const out = users.map((u) => ({
51 id: u.id,
52 username: u.username,
53 passwordHash: bcrypt.hashSync(u.password, 10),
54 role: u.role,
55 org: u.org,
56 }));
57 const dest = path.join(__dirname, "users.json");
58 fs.writeFileSync(dest, JSON.stringify(out, null, 2));
59 console.log("users.json written to", dest);
60 console.log("Demo accounts: (username / password)");
61 out.forEach((u) =>
62 console.log(`${u.username} / password (role: ${u.role}, org: ${u.org})`)
63 );
64}
65
66async function seedBlockchainUsers() {
67 const walletPath = path.resolve(__dirname, process.env.WALLET_PATH);
68 const wallet = await Wallets.newFileSystemWallet(walletPath);
69
70 for (const orgNum of [1, 2, 3, 4]) {
71 const orgName = `Org${orgNum}`;
72 const ccpPath = path.resolve(
73 __dirname,
74 process.env[`CCP_PATH_ORG${orgNum}`]
75 );
76 const identityLabel = process.env[`IDENTITY_ORG${orgNum}`];
77
78 console.log(`Processing users for ${orgName}`);
79 const identity = await wallet.get(identityLabel);
80 if (!identity) {
81 console.error(
82 `Identity "${identityLabel}" not found in wallet. Run initWallet.js first.`
83 );
84 continue;
85 }
86
87 const ccp = JSON.parse(fs.readFileSync(ccpPath, "utf8"));
88 const gateway = new Gateway();
89
90 try {
91 await gateway.connect(ccp, {
92 wallet,
93 identity: identityLabel,
94 discovery: {
95 enabled: true,
96 asLocalhost: process.env.AS_LOCALHOST === "true",
97 },
98 });
99
100 const network = await gateway.getNetwork(process.env.CHANNEL);
101 const contract = network.getContract(process.env.CHAINCODE);
102
103 const orgUsers = users.filter((u) => u.org === orgName);
104 for (const u of orgUsers) {
105 const userKey = `${u.role.toUpperCase()}-${u.id}`;
106 try {
107 await contract.evaluateTransaction("getUserDetails", userKey);
108 console.log(`User ${userKey} already exists, skipping`);
109 } catch {
110 console.log(`Registering ${u.role}: ${u.id} from ${orgName}`);
111 await contract.submitTransaction(
112 "registerUser",
113 u.role,
114 JSON.stringify(u)
115 );
116 }
117 }
118 } catch (err) {
119 console.error(`Error processing users for ${orgName}:`, err);
120 } finally {
121 gateway.disconnect();
122 }
123 }
124 console.log("\nBlockchain users seeding finished");
125}
126
127(async () => {
128 await seedBackendUsers();
129 await seedBlockchainUsers();
130})();