-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
86 lines (78 loc) · 2.08 KB
/
api.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
package main
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/RTradeLtd/VaaS/ethereum"
"github.com/gin-gonic/gin"
"github.com/lytics/grid"
)
type API struct {
client *grid.Client
Router *gin.Engine
}
// InitializeAPI is used to generate our API
func InitializeAPI(gc *grid.Client) *API {
api := API{}
router := gin.Default()
api.Router = router
api.Router.POST("/api/v1/ethereum/generate/locally", api.GenerateEthereumKeysLocally)
if gc != nil {
api.client = gc
api.Router.POST("/api/v1/ethereum/generate/distributed/:worker", api.GenerateEthereumKeysDistributedly)
}
return &api
}
// GenerateEthereumKeysLocally is used to generate our ethereum key locally, on the API node
func (api *API) GenerateEthereumKeysLocally(c *gin.Context) {
searchPrefix, exists := c.GetPostForm("search_prefix")
if !exists {
c.JSON(http.StatusBadRequest, gin.H{
"error": "search_prefix post form does not exist",
})
return
}
runTimeInSecondsString := c.PostForm("run_time_in_seconds:")
var runTime int64
var err error
if runTimeInSecondsString == "" {
runTime = 1000000000
} else {
runTime, err = strconv.ParseInt(runTimeInSecondsString, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "failed to convert run time to int",
})
return
}
}
eg := ethereum.InitializeEthereumGenerator(searchPrefix, runTime)
eg.RunAPI(c)
}
func (api *API) GenerateEthereumKeysDistributedly(c *gin.Context) {
worker := c.Param("worker")
searchPrefix, exists := c.GetPostForm("search_prefix")
if !exists {
c.JSON(http.StatusBadRequest, gin.H{
"error": "search_prefix post form does not exist",
})
return
}
genReq := &GenerationRequest{
SearchPrefix: searchPrefix,
}
resp, err := api.client.Request(time.Second*2, worker, genReq)
fmt.Printf("response %#v\nerr %v\n", resp, err)
if gr, ok := resp.(*GenerationResponse); ok {
c.JSON(http.StatusOK, gin.H{
"key": fmt.Sprintf("Resposne %s", gr.Key),
"address": fmt.Sprintf("Address %s", gr.Address),
})
return
}
c.JSON(http.StatusBadRequest, gin.H{
"error": "wrong resposne type",
})
return
}