github.com/amazechain/amc@v0.1.3/cmd/verify/main.go (about) 1 // Copyright 2023 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The AmazeChain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "context" 21 "encoding/hex" 22 "encoding/json" 23 "errors" 24 "fmt" 25 "github.com/amazechain/amc/common/crypto" 26 "github.com/amazechain/amc/common/crypto/bls" 27 "github.com/amazechain/amc/common/types" 28 "github.com/amazechain/amc/internal/api" 29 "github.com/amazechain/amc/log" 30 "github.com/amazechain/amc/modules/state" 31 "github.com/go-kit/kit/transport/http/jsonrpc" 32 "github.com/gorilla/websocket" 33 "os" 34 "os/signal" 35 "syscall" 36 ) 37 38 var privateKey bls.SecretKey 39 var addressKey types.Address 40 41 func RootContext() (context.Context, context.CancelFunc) { 42 ctx, cancel := context.WithCancel(context.Background()) 43 go func() { 44 defer cancel() 45 46 ch := make(chan os.Signal, 1) 47 defer close(ch) 48 49 signal.Notify(ch, os.Interrupt, syscall.SIGTERM) 50 defer signal.Stop(ch) 51 52 select { 53 case sig := <-ch: 54 log.Info("Got interrupt, shutting down...", "sig", sig) 55 case <-ctx.Done(): 56 } 57 }() 58 return ctx, cancel 59 } 60 61 func main() { 62 var err error 63 sByte, err := hex.DecodeString("2d09d9f4e166f35a4ab0a2edd599e2a23bbe86b312b2e05b34d9fbe5693b1e48") 64 if nil != err { 65 panic(err) 66 } 67 68 var sb [32]byte 69 copy(sb[:], sByte) 70 privateKey, err = bls.SecretKeyFromRandom32Byte(sb) 71 if nil != err { 72 panic(err) 73 } 74 75 ecdPk, err := crypto.HexToECDSA("2d09d9f4e166f35a4ab0a2edd599e2a23bbe86b312b2e05b34d9fbe5693b1e48") 76 if nil != err { 77 panic(err) 78 } 79 addressKey = crypto.PubkeyToAddress(ecdPk.PublicKey) 80 81 ctx, cancle := RootContext() 82 defer cancle() 83 84 con, _, err := websocket.DefaultDialer.DialContext(ctx, "ws://127.0.0.1:20013", nil) 85 defer con.Close() 86 87 end := make(chan struct{}) 88 defer close(end) 89 90 go func() { 91 for { 92 select { 93 case <-ctx.Done(): 94 end <- struct{}{} 95 return 96 default: 97 typ, msg, err := con.ReadMessage() 98 if nil != err { 99 log.Errorf("read msg failed: %v", err) 100 continue 101 } 102 if typ == websocket.TextMessage { 103 fmt.Println("read msg: ", string(msg)) 104 params, err := unwrapJSONRPC(msg) 105 if nil != err { 106 log.Warn(err.Error()) 107 continue 108 } 109 110 bean := new(state.EntireCode) 111 if err := json.Unmarshal(params, bean); err != nil { 112 log.Errorf("unmarshal entire failed, %v", err) 113 continue 114 } 115 116 root := verify(ctx, bean) 117 res := api.AggSign{} 118 res.Number = bean.Entire.Header.Number.Uint64() 119 res.Address = addressKey 120 res.StateRoot = root 121 copy(res.Sign[:], privateKey.Sign(root[:]).Marshal()) 122 in, err := json.Marshal(res) 123 if nil != err { 124 panic(err) 125 } 126 127 wrapRequest, _ := wrapJSONRPCRequest(in) 128 if err := con.WriteMessage(websocket.TextMessage, wrapRequest); nil != err { 129 log.Error("write msg failed: ", err) 130 } 131 log.Infof("write msg: %s", wrapRequest) 132 } 133 } 134 } 135 }() 136 137 if err = con.PingHandler()(""); nil != err { 138 panic(err) 139 } 140 141 if err := con.WriteMessage(websocket.TextMessage, []byte(`{ 142 "jsonrpc": "2.0", 143 "method": "eth_subscribe", 144 "params": [ 145 "minedBlock", 146 "`+addressKey.String()+`" 147 ], 148 "id": 1 149 }`)); err != nil { 150 151 cancle() 152 } 153 154 <-end 155 } 156 157 func unwrapJSONRPC(in []byte) ([]byte, error) { 158 //"{\"jsonrpc\":\"2.0\",\"id\":1,\"error\":{\"code\":-32000,\"message\":\"unauthed address: 0xeB156a42dcaFcf155B07f3638892440C7dE5d564\"}}\n" 159 //ws consumer received msg:%!(EXTRA string=ws consumer received msg:, string={"jsonrpc":"2.0","id":1,"result":"0x96410b68a9f8875bb20fde06823eb861"} 160 req := new(jsonrpc.Request) 161 if err := json.Unmarshal(in, req); err != nil { 162 return nil, err 163 } 164 if len(req.Params) == 0 { 165 return []byte{}, errors.New("empty request params") 166 } 167 168 //type innerProtocolEntire struct { 169 // Entire json.RawMessage `json:"Entire"` 170 //} 171 type innerProtocol struct { 172 Subscription string `json:"subscription"` 173 Result json.RawMessage `json:"result"` 174 } 175 176 innerReq := new(innerProtocol) 177 if err := json.Unmarshal(req.Params, innerReq); err != nil { 178 return nil, err 179 } 180 181 return innerReq.Result, nil 182 } 183 184 type JSONRPCRequest struct { 185 JsonRpc string `json:"jsonrpc"` 186 Method string `json:"method"` 187 ID int `json:"id"` 188 Params []json.RawMessage `json:"params"` 189 } 190 191 func wrapJSONRPCRequest(in []byte) ([]byte, error) { 192 d := &JSONRPCRequest{ 193 JsonRpc: "2.0", 194 Method: "eth_submitSign", 195 ID: 1, 196 Params: make([]json.RawMessage, 1), 197 } 198 d.Params[0] = in 199 return json.Marshal(d) 200 }