-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathevent.go
More file actions
75 lines (65 loc) · 1.6 KB
/
event.go
File metadata and controls
75 lines (65 loc) · 1.6 KB
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
package main
import (
"strings"
"github.com/cloudflare/cfssl/log"
"github.com/ethereum/go-ethereum/crypto"
"github.com/meshplus/bitxhub-model/pb"
)
type Event struct {
Index uint64 `json:"index"`
DstFullID string `json:"dst_full_id"`
SrcFullID string `json:"src_full_id"`
Encrypt bool `json:"encrypt"`
CallFunc CallFunc `json:"call_func"`
CallBack CallFunc `json:"callback"`
RollBack CallFunc `json:"rollback"`
}
func (ev *Event) Convert2IBTP(timeoutHeight int64, ibtpType pb.IBTP_Type) *pb.IBTP {
pd, err := ev.encryptPayload()
if err != nil {
log.Fatalf("Get ibtp payload :%s", err)
}
return &pb.IBTP{
From: ev.SrcFullID,
To: ev.DstFullID,
Index: ev.Index,
Type: ibtpType,
TimeoutHeight: timeoutHeight,
Payload: pd,
}
}
func handleArgs(args string) [][]byte {
argsBytes := make([][]byte, 0)
as := strings.Split(args, ",")
for _, a := range as {
argsBytes = append(argsBytes, []byte(a))
}
return argsBytes
}
func (ev *Event) encryptPayload() ([]byte, error) {
content := &pb.Content{
Func: ev.CallFunc.Func,
Args: ev.CallFunc.Args,
}
data, err := content.Marshal()
if err != nil {
return nil, err
}
var packed []byte
packed = append(packed, []byte(ev.CallFunc.Func)...)
for _, arg := range ev.CallFunc.Args {
packed = append(packed, arg...)
}
hash := crypto.Keccak256(packed)
ibtppd := &pb.Payload{
Encrypted: ev.Encrypt,
Content: data,
Hash: hash,
}
return ibtppd.Marshal()
}
type Response struct {
OK bool `json:"ok"`
Message string `json:"message"`
Data []byte `json:"data"`
}