-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathverify.go
154 lines (131 loc) · 3 KB
/
verify.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
package main
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"math/big"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/meshplus/bitxhub-kit/crypto/asym/ecdsa"
"github.com/meshplus/bitxhub-kit/types"
)
type response struct {
IsPass bool `json:"is_pass"`
Data []byte `json:"data"`
}
type ValidatorServer struct {
router *gin.Engine
port string
ctx context.Context
cancel context.CancelFunc
}
func NewValidatorServer(port string) (*ValidatorServer, error) {
ctx, cancel := context.WithCancel(context.Background())
gin.SetMode(gin.ReleaseMode)
router := gin.New()
return &ValidatorServer{
router: router,
port: port,
ctx: ctx,
cancel: cancel,
}, nil
}
func (g *ValidatorServer) Start() error {
g.router.Use(gin.Recovery())
v1 := g.router.Group("/v1")
{
v1.POST("verify", g.verifyMultiSign)
}
go func() {
go func() {
err := g.router.Run(fmt.Sprintf(":%s", g.port))
if err != nil {
panic(err)
}
}()
<-g.ctx.Done()
}()
return nil
}
func (g *ValidatorServer) verifyMultiSign(c *gin.Context) {
res := &response{}
signatures := c.Query("signatures")
hash := c.Query("hash")
threshold := c.Query("threshold")
validators := c.Query("validators")
var multiSignatures [][]byte
if err := json.Unmarshal([]byte(signatures), &multiSignatures); err != nil {
res.IsPass = false
res.Data = []byte("multi signatures json unmarshal error")
c.JSON(http.StatusOK, res)
return
}
var vList []string
if err := json.Unmarshal([]byte(validators), &vList); err != nil {
res.IsPass = false
res.Data = []byte("validators json unmarshal error")
c.JSON(http.StatusOK, res)
return
}
t, err := strconv.ParseUint(threshold, 10, 64)
if err != nil {
res.IsPass = false
res.Data = []byte("threshold parse error")
c.JSON(http.StatusOK, res)
return
}
var bxhSigners []string
for _, sig := range multiSignatures {
if len(sig) != 65 {
continue
}
v, r, s := getRawSignature(sig)
addr, err := ecdsa.RecoverPlain([]byte(hash), r, s, v, true)
if err != nil {
res.IsPass = false
res.Data = []byte("recover plain error")
c.JSON(http.StatusOK, res)
return
}
if addressArrayContains(vList, addr) {
if addressArrayContains(bxhSigners, addr) {
continue
}
bxhSigners = append(bxhSigners, types.NewAddress(addr).String())
if uint64(len(bxhSigners)) == t {
res.IsPass = true
c.JSON(http.StatusOK, res)
return
}
}
}
res.IsPass = false
c.JSON(http.StatusOK, res)
}
func getRawSignature(sig []byte) (v, r, s *big.Int) {
if len(sig) != 65 {
return nil, nil, nil
}
r = &big.Int{}
r.SetBytes(sig[:32])
s = &big.Int{}
s.SetBytes(sig[32:64])
v = &big.Int{}
v.SetBytes(sig[64:])
return v, r, s
}
func addressArrayContains(addrs []string, address []byte) bool {
for _, addr := range addrs {
if addr == types.NewAddress(address).String() {
return true
}
}
return false
}
func uint64ToBytesInBigEndian(i uint64) []byte {
bytes := make([]byte, 8)
binary.BigEndian.PutUint64(bytes, i)
return bytes
}