/
main.go
200 lines (173 loc) · 5.01 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2023 The AmazeChain Authors
// This file is part of the AmazeChain library.
//
// The AmazeChain library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The AmazeChain library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/amazechain/amc/common/crypto"
"github.com/amazechain/amc/common/crypto/bls"
"github.com/amazechain/amc/common/types"
"github.com/amazechain/amc/internal/api"
"github.com/amazechain/amc/log"
"github.com/amazechain/amc/modules/state"
"github.com/go-kit/kit/transport/http/jsonrpc"
"github.com/gorilla/websocket"
"os"
"os/signal"
"syscall"
)
var privateKey bls.SecretKey
var addressKey types.Address
func RootContext() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
go func() {
defer cancel()
ch := make(chan os.Signal, 1)
defer close(ch)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(ch)
select {
case sig := <-ch:
log.Info("Got interrupt, shutting down...", "sig", sig)
case <-ctx.Done():
}
}()
return ctx, cancel
}
func main() {
var err error
sByte, err := hex.DecodeString("2d09d9f4e166f35a4ab0a2edd599e2a23bbe86b312b2e05b34d9fbe5693b1e48")
if nil != err {
panic(err)
}
var sb [32]byte
copy(sb[:], sByte)
privateKey, err = bls.SecretKeyFromRandom32Byte(sb)
if nil != err {
panic(err)
}
ecdPk, err := crypto.HexToECDSA("2d09d9f4e166f35a4ab0a2edd599e2a23bbe86b312b2e05b34d9fbe5693b1e48")
if nil != err {
panic(err)
}
addressKey = crypto.PubkeyToAddress(ecdPk.PublicKey)
ctx, cancle := RootContext()
defer cancle()
con, _, err := websocket.DefaultDialer.DialContext(ctx, "ws://127.0.0.1:20013", nil)
defer con.Close()
end := make(chan struct{})
defer close(end)
go func() {
for {
select {
case <-ctx.Done():
end <- struct{}{}
return
default:
typ, msg, err := con.ReadMessage()
if nil != err {
log.Errorf("read msg failed: %v", err)
continue
}
if typ == websocket.TextMessage {
fmt.Println("read msg: ", string(msg))
params, err := unwrapJSONRPC(msg)
if nil != err {
log.Warn(err.Error())
continue
}
bean := new(state.EntireCode)
if err := json.Unmarshal(params, bean); err != nil {
log.Errorf("unmarshal entire failed, %v", err)
continue
}
root := verify(ctx, bean)
res := api.AggSign{}
res.Number = bean.Entire.Header.Number.Uint64()
res.Address = addressKey
res.StateRoot = root
copy(res.Sign[:], privateKey.Sign(root[:]).Marshal())
in, err := json.Marshal(res)
if nil != err {
panic(err)
}
wrapRequest, _ := wrapJSONRPCRequest(in)
if err := con.WriteMessage(websocket.TextMessage, wrapRequest); nil != err {
log.Error("write msg failed: ", err)
}
log.Infof("write msg: %s", wrapRequest)
}
}
}
}()
if err = con.PingHandler()(""); nil != err {
panic(err)
}
if err := con.WriteMessage(websocket.TextMessage, []byte(`{
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": [
"minedBlock",
"`+addressKey.String()+`"
],
"id": 1
}`)); err != nil {
cancle()
}
<-end
}
func unwrapJSONRPC(in []byte) ([]byte, error) {
//"{\"jsonrpc\":\"2.0\",\"id\":1,\"error\":{\"code\":-32000,\"message\":\"unauthed address: 0xeB156a42dcaFcf155B07f3638892440C7dE5d564\"}}\n"
//ws consumer received msg:%!(EXTRA string=ws consumer received msg:, string={"jsonrpc":"2.0","id":1,"result":"0x96410b68a9f8875bb20fde06823eb861"}
req := new(jsonrpc.Request)
if err := json.Unmarshal(in, req); err != nil {
return nil, err
}
if len(req.Params) == 0 {
return []byte{}, errors.New("empty request params")
}
//type innerProtocolEntire struct {
// Entire json.RawMessage `json:"Entire"`
//}
type innerProtocol struct {
Subscription string `json:"subscription"`
Result json.RawMessage `json:"result"`
}
innerReq := new(innerProtocol)
if err := json.Unmarshal(req.Params, innerReq); err != nil {
return nil, err
}
return innerReq.Result, nil
}
type JSONRPCRequest struct {
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
ID int `json:"id"`
Params []json.RawMessage `json:"params"`
}
func wrapJSONRPCRequest(in []byte) ([]byte, error) {
d := &JSONRPCRequest{
JsonRpc: "2.0",
Method: "eth_submitSign",
ID: 1,
Params: make([]json.RawMessage, 1),
}
d.Params[0] = in
return json.Marshal(d)
}