-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
151 lines (106 loc) · 3.49 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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/CoreumFoundation/coreum/app"
coreumconfig "github.com/CoreumFoundation/coreum/pkg/config"
"github.com/CoreumFoundation/coreum/pkg/tx"
cosmosclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
func main() {
network, err := coreumconfig.NetworkByChainID(coreumconfig.ChainID("coreum-devnet-1"))
checkErr(err)
network.SetSDKConfig()
// Init the mux router
router := mux.NewRouter()
// Get Balance of Account.
router.HandleFunc("/get-balance/{address}", GetBalance).Methods("GET")
// Create a new wallet
router.HandleFunc("/create-new-wallet", CreateNewWallet).Methods("GET")
// Recover wallet from Mnemonic
router.HandleFunc("/recovery-wallet", RecoveryWallet).Methods("POST")
// serve the app
fmt.Println("Server at 5432")
log.Fatal(http.ListenAndServe(":5432", router))
}
// type concurrentSafeKeyring struct {
// mu *sync.RWMutex
// kr keyring.Keyring
// }
// func newConcurrentSafeKeyring(kr keyring.Keyring) concurrentSafeKeyring {
// return concurrentSafeKeyring{
// mu: &sync.RWMutex{},
// kr: kr,
// }
// }
func GetBalance(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
address := params["address"]
type JsonResponse struct {
Type string `json:"type"`
Data string `json:"data"`
Message string `json:"message"`
}
var response = JsonResponse{}
if address == "" {
response = JsonResponse{Type: "error", Message: "You are missing wallet address parameter."}
} else {
rpcAddress := "https://s-0.devnet-1.coreum.dev:443/"
ctx := context.Background()
rpcClient, err := cosmosclient.NewClientFromNode(rpcAddress)
clientCtx := tx.NewClientContext(app.ModuleBasics).WithChainID("coreum-devnet-1").
WithClient(rpcClient).
WithKeyring(keyring.NewInMemory()).
WithBroadcastMode(flags.BroadcastBlock)
checkErr(err)
// Getting Balance from Coreum Blockchain
bankClient := banktypes.NewQueryClient(clientCtx)
balance, err := bankClient.Balance(ctx, &banktypes.QueryBalanceRequest{
Address: address,
Denom: "ducore",
})
checkErr(err)
response = JsonResponse{Type: "success", Message: "The Balance of the wallet is gotten successfully!", Data: balance.Balance.Amount.String()}
}
json.NewEncoder(w).Encode(response)
}
func CreateNewWallet(w http.ResponseWriter, r *http.Request) {
kr := keyring.NewInMemory()
info, mnemonic, err := kr.NewMnemonic("", keyring.English, sdk.GetConfig().GetFullBIP44Path(), "", hd.Secp256k1)
checkErr(err)
type JsonResponse struct {
Mnemonic string `json:"mnemonic"`
Address string `json:"address"`
}
sdkAddr := info.GetAddress()
var response = JsonResponse{Mnemonic: mnemonic, Address: sdkAddr.String()}
json.NewEncoder(w).Encode(response)
}
func RecoveryWallet(w http.ResponseWriter, r *http.Request) {
mnemonic := r.FormValue("mnemonic")
kr := keyring.NewInMemory()
info, err := kr.NewAccount("wallet", mnemonic, "", sdk.GetConfig().GetFullBIP44Path(), hd.Secp256k1)
checkErr(err)
json.NewEncoder(w).Encode(info.GetAddress())
}
// Function for handling messages
func printMessage(message string) {
fmt.Println("")
fmt.Println(message)
fmt.Println("")
}
// Function for handling errors
func checkErr(err error) {
if err != nil {
panic(err)
}
}