-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
181 lines (144 loc) · 4.71 KB
/
app.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
package main
import (
"fmt"
"encoding/json"
"github.com/rs/cors"
"log"
"time"
"net/http"
"github.com/hashgraph/hedera-sdk-go"
"github.com/gorilla/mux"
)
type Balance struct {
bal uint64
}
// Generate Keys
func GenKeys(w http.ResponseWriter, r *http.Request) {
secret, mnemonic := hedera.GenerateSecretKey()
fmt.Printf("secret = %v\n", secret)
fmt.Printf("mnemonic = %v\n", mnemonic)
public := secret.Public()
fmt.Printf("public = %v\n", public)
}
func GetMyBalance(w http.ResponseWriter, r *http.Request) {
accountID := hedera.AccountID{Account: 1001}
client, err := hedera.Dial("testnet.hedera.com:51003")
if err != nil {
panic(err)
}
client.SetNode(hedera.AccountID{Account: 3})
client.SetOperator(accountID, func() hedera.SecretKey {
operatorSecret, err := hedera.SecretKeyFromString("<SECRET_KEY>")
if err != nil {
panic(err)
}
return operatorSecret
})
defer client.Close()
// Get the _answer_ for the query of getting the account balance
balance, err := client.Account(accountID).Balance().Get()
if err != nil {
panic(err)
}
respondWithJson(w, http.StatusOK, map[string]uint64{"Balance": balance/100000000.0})
fmt.Printf("balance = %v tinybars\n", balance)
fmt.Printf("balance = %.5f hbars\n", float64(balance)/100000000.0)
}
func TransferTokens(w http.ResponseWriter, r *http.Request) {
// Read and decode the operator secret key
operatorAccountID := hedera.AccountID{Account: 1001}
operatorSecret, err := hedera.SecretKeyFromString("<SECRET_KEY>")
if err != nil {
panic(err)
}
// Read and decode target account
targetAccountID, err := hedera.AccountIDFromString("0:0:1004")
if err != nil {
panic(err)
}
//
// Connect to Hedera
//
client, err := hedera.Dial("testnet.hedera.com:51003")
if err != nil {
panic(err)
}
client.SetNode(hedera.AccountID{Account: 3})
client.SetOperator(operatorAccountID, func() hedera.SecretKey {
return operatorSecret
})
defer client.Close()
balance, err := client.Account(targetAccountID).Balance().Get()
if err != nil {
panic(err)
}
fmt.Printf("account balance = %v\n", balance)
nodeAccountID := hedera.AccountID{Account: 3}
response, err := client.TransferCrypto().
// Move 100 out of operator account
Transfer(operatorAccountID, -100000000).
// And place in our new account
Transfer(targetAccountID, 100000000).
Operator(operatorAccountID).
Node(nodeAccountID).
Memo("[test] hedera-sdk-go v2").
Sign(operatorSecret). // Sign it once as operator
Sign(operatorSecret). // And again as sender
Execute()
if err != nil {
panic(err)
}
transactionID := response.ID
fmt.Printf("transferred; transaction = %v\n", transactionID)
fmt.Printf("wait for 2s...\n")
time.Sleep(2 * time.Second)
receipt, err := client.Transaction(*transactionID).Receipt().Get()
if err != nil {
panic(err)
}
if receipt.Status != hedera.StatusSuccess {
panic(fmt.Errorf("transaction has a non-successful status: %v", receipt.Status.String()))
}
fmt.Printf("wait for 2s...\n")
time.Sleep(2 * time.Second)
balance, err = client.Account(operatorAccountID).Balance().Get()
if err != nil {
panic(err)
}
respondWithJson(w, http.StatusOK, map[string]uint64{"Balance": balance/100000000.0})
}
func respondWithError(w http.ResponseWriter, code int, msg string) {
respondWithJson(w, code, map[string]string{"error": msg})
}
func respondWithJson(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
func init() {
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/generatekeys", GenKeys).Methods("GET")
r.HandleFunc("/getbalance", GetMyBalance).Methods("GET")
r.HandleFunc("/pay", TransferTokens).Methods("POST")
corsOpts := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, //you service is available and allowed for this base url
AllowedMethods: []string{
http.MethodGet,//http methods for your app
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodOptions,
http.MethodHead,
},
AllowedHeaders: []string{
"*",//or you can your header key values which you are using in your application
},
})
if err := http.ListenAndServe(":3000", corsOpts.Handler(r)); err != nil {
log.Fatal(err)
}
}