-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
86 lines (77 loc) · 2.1 KB
/
response.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
package pbftTypes
import (
"encoding/json"
"fmt"
"github.com/DhunterAO/goAuthChain/common"
"github.com/DhunterAO/goAuthChain/crypto"
"github.com/DhunterAO/goAuthChain/crypto/types"
)
type Response struct {
InfoID uint8
NodeID uint64
ViewID uint64
BlockHash types.Hash
Signature *types.Signature
}
func NewResponse(nodeID uint64, viewID uint64, blockHash types.Hash) *Response {
newResponse := &Response{
InfoID: ResponseInfo,
NodeID: nodeID,
ViewID: viewID,
BlockHash: blockHash,
Signature: &types.Signature{},
}
return newResponse
}
func (resp *Response) ToJson() []byte {
respJson, err := json.Marshal(resp)
if err != nil {
fmt.Println(err)
}
return respJson
}
//func (resp *Response) ToMap() map[string]interface{} {
// respMap := make(map[string]interface{})
// respMap["InfoID"] = resp.InfoID
// respMap["NodeID"] = resp.NodeID
// respMap["ViewID"] = resp.ViewID
// respMap["BlockHash"] = resp.BlockHash.Hex()
// respMap["Signature"] = resp.Signature.Hex()
// return respMap
//}
//
//func (resp *Response) ToPrettyJson() []byte {
// respPreJson, err := json.Marshal(resp.ToMap())
// util.LogError(err)
// return util.PrettyPrintJson(respPreJson)
//}
//
//func (resp *Response) ToString() string {
// return string(resp.ToPrettyJson())
//}
//func JsonToResponse(respJson []byte) *Response {
// resp := new(Response)
// err := json.Unmarshal(respJson, resp)
// if err != nil {
// fmt.Println(err)
// }
// return resp
//}
func (resp *Response) Bytes() []byte {
respBytes := make([]byte, 0)
respBytes = append(respBytes, resp.InfoID)
respBytes = append(respBytes, common.Uint64ToBytes(resp.NodeID)...)
respBytes = append(respBytes, common.Uint64ToBytes(resp.ViewID)...)
respBytes = append(respBytes, resp.BlockHash.Bytes()...)
return respBytes
}
func (resp *Response) CalcHash() *types.Hash {
return crypto.CalcHash(resp.Bytes())
}
func (resp *Response) CheckSignature() (bool, error) {
pk, err := crypto.RecoverPkFromSig(resp.CalcHash(), resp.Signature)
if err != nil {
return false, err
}
return crypto.VerifySignature(pk, crypto.CalcHash(resp.Bytes()), resp.Signature), nil
}