-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
68 lines (53 loc) · 1.31 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
package main
import (
"context"
"fmt"
"strconv"
"net/http"
"encoding/json"
)
func main() {
ctx := context.Background()
node := CreateNewNode(ctx)
node.StartMiner()
http.HandleFunc("/sendtx", func(w http.ResponseWriter, r *http.Request) {
from := r.FormValue("from")
to := r.FormValue("to")
amount := r.FormValue("amount")
memo := r.FormValue("memo")
fmt.Println("call sendtx", from, to, amount, memo)
amt, err := strconv.ParseInt(amount, 10, 64)
if err != nil {
panic(err)
}
tx := Transaction{
Sender: from,
Receiver: to,
Amount: uint64(amt),
Memo: memo,
}
err = json.NewEncoder(w).Encode(node.SendTransaction(&tx))
if err != nil {
panic(err)
}
})
http.HandleFunc("/getinfo", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Call getinfo")
err := json.NewEncoder(w).Encode(node.GetInfo())
// res, err := json.Marshal(node.GetInfo())
if err != nil {
panic(err)
}
// fmt.Fprintf(w, string(res))
})
http.HandleFunc("/getnewaddress", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Call getnewaddress")
err := json.NewEncoder(w).Encode(node.GetNewAddress())
// res, err := json.Marshal(node.GetInfo())
if err != nil {
panic(err)
}
// fmt.Fprintf(w, string(res))
})
http.ListenAndServe(":1234", nil)
}