-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (61 loc) · 1.72 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
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"log"
R "math/rand"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/zhenligod/CudaDecode/socket"
"github.com/zhenligod/go-ethereum/crypto/secp256k1"
)
var KeyInputs []KeyInput
//KeyInput struct
type KeyInput struct {
ID string `json:"id"`
Key string `json:"Key"`
}
func secp256k1Decode(privkey []byte) (pubkey []byte) {
pubkey, err := secp256k1.GeneratePubKey(privkey)
if err != nil {
panic(err)
}
fmt.Println("secp256k1 pubkey:", pubkey)
return pubkey
}
//secp256k1 handle function
func DecodeSecp256k1Input(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var keyInput KeyInput
_ = json.NewDecoder(r.Body).Decode(&keyInput)
keyInput.ID = strconv.Itoa(R.Intn(100))
KeyInputs = append(KeyInputs, keyInput)
seckey, _ := hex.DecodeString(keyInput.Key)
pubkey := secp256k1Decode(seckey)
json.NewEncoder(w).Encode(pubkey)
}
//sha256 handle function
func DecodeSha256Input(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var keyInput KeyInput
_ = json.NewDecoder(r.Body).Decode(&keyInput)
keyInput.ID = strconv.Itoa(R.Intn(100))
KeyInputs = append(KeyInputs, keyInput)
seckey, _ := hex.DecodeString(keyInput.Key)
serverAddr := "localhost:8000"
err, res := socket.SendPacketClient(serverAddr, string(seckey))
if err != nil {
panic(err)
}
json.NewEncoder(w).Encode(res)
}
// main the function where execution of the program begins
func main() {
//Init router
r := mux.NewRouter()
r.HandleFunc("/api/secp256k1", DecodeSecp256k1Input).Methods("POST")
r.HandleFunc("/api/sha256", DecodeSha256Input).Methods("POST")
log.Fatal(http.ListenAndServe(":8000", r))
}