-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
216 lines (184 loc) · 5.89 KB
/
main.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"strconv"
"time"
"mwalimu/blockchain"
"mwalimu/functions"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
const (
ethLink = "https://mainnet.infura.io/v3/436051433bd54480aca5957b7d0c77bb"
ethPrivateKey = "436051433bd54480aca5957b7d0c77bb"
)
func main() {
// Initialize blockchain with genesis block
blockchain := functions.Blockchain{}
genesisBlock := functions.Block{
Timestamp: time.Now().Unix(),
Data: []byte("Genesis Block"),
PreviousHash: []byte{},
}
genesisBlock.Hash = functions.CalculateHash(genesisBlock)
blockchain.Blocks = append(blockchain.Blocks, genesisBlock)
// Connect to the Ethereum client
client, err := ethclient.Dial(ethLink)
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
}
defer client.Close()
// Load the contract's address and private key
contractAddress := common.HexToAddress("0x...") // deployed contract address
privateKey, err := crypto.HexToECDSA(ethPrivateKey) // API key
if err != nil {
log.Fatalf("Failed to parse private key: %v", err)
}
userStorage, err := blockchain.NewUserStorageContract(contractAddress, client)
if err != nil {
log.Fatal(err)
}
contract, err := blockchain.NewContract(contractAddress, privateKey)
if err != nil {
log.Fatalf("Failed to deploy contract: %v", err)
}
defer contract.Stop()
// Create a new instance of the contract
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, contract)
if err != nil {
log.Fatal(err)
}
tx, err := userStorage.CreateUser(auth, "John Doe", "[email protected]", "Learner")
if err != nil {
log.Fatal(err)
}
// Create an instance of SessionChain
sessionChain, err := blockchain.NewSessionChain(client, contractAddress, privateKey) // <--- create function
if err != nil {
log.Fatal(err)
}
// Retrieve session ID
sessionID, err := sessionChain.GetSessionID()
if err != nil {
log.Fatal(err)
}
fmt.Println("Session ID:", sessionID)
// Store a session ID
tx, err = sessionChain.StoreSessionID("my_session_id")
if err != nil {
log.Fatalf("Failed to store session ID: %v", err)
}
fmt.Printf("Transaction sent: %s\n", tx.Hash().Hex())
sessionID, err = sessionChain.GetSessionID()
if err != nil {
log.Fatalf("Failed to retrieve session ID: %v", err)
}
log.Printf("Retrieved session ID: %s\n", sessionID)
// Serve static files (CSS, JS)
http.HandleFunc("/scripts/", fileServer)
http.HandleFunc("/static/", fileServer)
// Serve dynamic HTML templates
http.HandleFunc("/", servePage)
// // Handle routes
http.HandleFunc("/forgot-password", functions.ServeForgotPasswordForm)
http.HandleFunc("/handle-forgot-password", functions.HandleForgotPassword)
http.HandleFunc("/reset-password", functions.ServeResetPasswordForm)
http.HandleFunc("/handle-reset-password", functions.HandleResetPassword)
// serve signup
http.HandleFunc("/signup", functions.SignupHandler)
// Start the server
log.Printf("Server started at http://localhost:8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("Server failed: %v", err)
}
}
// Serve static files (CSS, JS)
func fileServer(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/"+r.URL.Path[len("/scripts/"):])
}
// Serve HTML pages based on referral
func servePage(w http.ResponseWriter, r *http.Request) {
page := r.URL.Path[1:]
if page == "" || page == "/" {
page = "index.html"
}
// Define template path
tmplPath := "./templates/" + page
// Parse and execute the template
tmpl, err := template.ParseFiles(tmplPath)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
log.Printf("Template parsing error: %v", err)
return
}
err = tmpl.Execute(w, nil)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
log.Printf("Template execution error: %v", err)
return
}
}
// The handler function for signup route
func handler(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
functions.SignupHandler(w, r)
default:
code := http.StatusNotFound
codefin := strconv.Itoa(code)
fmt.Printf(codefin, "Page not found")
os.Exit(1)
}
}
func SignupHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.ServeFile(w, r, "templates/signup.html")
return
}
name := r.FormValue("name")
email := r.FormValue("email")
password := r.FormValue("password")
role := r.FormValue("role")
// Create a new Ethereum account for the user
account, err := blockchain.createEthereumAccount() // <--- create this function
if err != nil {
http.Error(w, "Failed to create Ethereum account", http.StatusInternalServerError)
return
}
// Store user credentials
err = functions.storeUserCredentials(email, password, account.Address.Hex()) // <--- create this function
if err != nil {
http.Error(w, "Failed to store user credentials", http.StatusInternalServerError)
return
}
// Connect to Ethereum client and get user storage contract
client, err := ethclient.Dial(ethLink)
if err != nil {
http.Error(w, "Failed to connect to Ethereum client", http.StatusInternalServerError)
return
}
contractAddress := common.HexToAddress(ethPrivateKey)
userStorage, err := blockchain.NewUserStorage(contractAddress, client)
if err != nil {
http.Error(w, "Failed to initialize user storage", http.StatusInternalServerError)
return
}
// Create transaction options
auth, err := bind.NewKeyedTransactorWithChainID(account.PrivateKey, err)
if err != nil {
log.Fatal(err)
}
// Create user on the blockchain
err = userStorage.CreateUser(auth, name, email, role)
if err != nil {
http.Error(w, "Failed to create user on blockchain", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/login", http.StatusSeeOther)
}