This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
153 lines (130 loc) · 3.85 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
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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/google/uuid"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"math/big"
"net/http"
"strconv"
"time"
)
type Config struct {
PayoutContractAddress string `json:"payoutContractAddress"`
ChainId int64 `json:"chainId"`
Env string `json:"env"`
}
type PayoutRequest2 struct {
UserId uuid.UUID `json:"userId"`
Amount *big.Int `json:"amount"`
}
func sign(w http.ResponseWriter, r *http.Request) {
var data PayoutRequest2
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
writeErr(w, http.StatusInternalServerError, err.Error())
return
}
privateKey, err := crypto.HexToECDSA(opts.Ethereum.PrivateKey)
if err != nil {
writeErr(w, http.StatusBadRequest, "private key error %v", err)
return
}
b1, _ := data.UserId.MarshalBinary()
b2 := make([]byte, 32-len(b1))
b3 := append(b1, b2...)
b4 := math.U256Bytes(data.Amount)
hashRaw := crypto.Keccak256(b3, []byte{'#'}, b4)
signature, err := crypto.Sign(hashRaw, privateKey)
if err != nil {
writeErr(w, http.StatusBadRequest, "private key error %v", err)
return
}
//https://ethereum.stackexchange.com/questions/45580/validating-go-ethereum-key-signature-with-ecrecover
sig := Signature{
signature,
bytes32(hashRaw),
bytes32(signature[:32]),
bytes32(signature[32:64]),
uint8(int(signature[65])) + 27, // Yes add 27, weird Ethereum quirk
}
writeJson(w, sig)
}
func serverTime(w http.ResponseWriter, r *http.Request, email string) {
currentTime := timeNow()
writeJsonStr(w, `{"time":"`+currentTime.Format("2006-01-02 15:04:05")+`","offset":`+strconv.Itoa(secondsAdd)+`}`)
}
func serverTimeEth(w http.ResponseWriter, r *http.Request, email string) {
header, err := ethClient.c.HeaderByNumber(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
currentTime := time.Unix(int64(header.Time), 0)
writeJsonStr(w, `{"time":"`+currentTime.Format("2006-01-02 15:04:05")+`","offset":`+strconv.Itoa(secondsAdd)+`}`)
}
func timeWarp(w http.ResponseWriter, r *http.Request, _ string) {
m := mux.Vars(r)
h := m["hours"]
if h == "" {
writeErr(w, http.StatusBadRequest, "Parameter hours not set: %v", m)
return
}
hours, err := strconv.Atoi(h)
if err != nil {
writeErr(w, http.StatusBadRequest, "Parameter hours not set: %v", m)
return
}
seconds := hours * 60 * 60
err = warpChain(seconds, ethClient.rpc)
//TODO: warp chain on in-memory NEO chain
if err != nil {
writeErr(w, http.StatusBadRequest, "Could not warp time: %v", m)
return
}
secondsAdd += seconds
log.Printf("time warp: %v", timeNow())
}
func config(w http.ResponseWriter, _ *http.Request) {
cfg := Config{
PayoutContractAddress: opts.Ethereum.Contract,
ChainId: ethClient.chainId.Int64(),
Env: opts.Env,
}
writeJson(w, cfg)
}
//Generic helpers
func bytes32(b []byte) [32]byte {
var ret [32]byte
copy(ret[:], b)
return ret
}
//Helpers to respond to the api calls
func writeErr(w http.ResponseWriter, code int, format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
log.Printf(msg)
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Pragma", "no-cache")
w.WriteHeader(code)
if debug {
w.Write([]byte(`{"error":"` + msg + `"}`))
}
}
func writeJson(w http.ResponseWriter, obj interface{}) {
w.Header().Set("Content-Type", "application/json")
var err = json.NewEncoder(w).Encode(obj)
if err != nil {
writeErr(w, http.StatusBadRequest, "Could encode json: %v", err)
}
}
func writeJsonStr(w http.ResponseWriter, obj string) {
w.Header().Set("Content-Type", "application/json")
_, err := w.Write([]byte(obj))
if err != nil {
writeErr(w, http.StatusBadRequest, "Could write json: %v", err)
}
}