-
Notifications
You must be signed in to change notification settings - Fork 0
/
joingroup.go
243 lines (219 loc) · 8.33 KB
/
joingroup.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Package api provides API for chestnut.
package api
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/lixvyang/chestnut/chain"
localcrypto "github.com/lixvyang/chestnut/crypto"
"github.com/lixvyang/chestnut/nodectx"
chestnutpb "github.com/lixvyang/chestnut/pb"
"github.com/lixvyang/chestnut/utils/options"
)
type JoinGroupParam struct {
GenesisBlock *chestnutpb.Block `from:"genesis_block" json:"genesis_block" validate:"required"`
GroupId string `from:"group_id" json:"group_id" validate:"required"`
GroupName string `from:"group_name" json:"group_name" validate:"required"`
OwnerPubKey string `from:"owner_pubkey" json:"owner_pubkey" validate:"required"`
ConsensusType string `from:"consensus_type" json:"consensus_type" validate:"required"`
EncryptionType string `from:"encryption_type" json:"encryption_type" validate:"required"`
CipherKey string `from:"cipher_key" json:"cipher_key" validate:"required"`
AppKey string `from:"app_key" json:"app_key" validate:"required"`
Signature string `from:"signature" json:"signature" validate:"required"`
}
type JoinGroupResult struct {
GroupId string `json:"group_id"`
GroupName string `json:"group_name"`
OwnerPubkey string `json:"owner_pubkey"`
UserPubkey string `json:"user_pubkey"`
UserEncryptPubkey string `json:"user_encryptpubkey"`
ConsensusType string `json:"consensus_type"`
EncryptionType string `json:"encryption_type"`
CipherKey string `json:"cipher_key"`
AppKey string `json:"app_key"`
Signature string `json:"signature"`
}
func (h *Handler) JoinGroup(c echo.Context) (err error) {
output := make(map[string]string)
validate := validator.New()
params := new(JoinGroupParam)
if err = c.Bind(params); err != nil {
output[ERROR_INFO] = err.Error()
return c.JSON(http.StatusBadRequest, output)
}
if err = validate.Struct(params); err != nil {
output[ERROR_INFO] = "unmarshal genesis block failed with msg:" + err.Error()
return c.JSON(http.StatusBadRequest, output)
}
genesisBlockBytes, err := json.Marshal(params.GenesisBlock)
if err != nil {
output[ERROR_INFO] = "unmarshal genesis block failed with msg:" + err.Error()
return c.JSON(http.StatusBadRequest, output)
}
nodeoptions := options.GetNodeOptions()
var groupSignPubkey []byte
ks := nodectx.GetNodeCtx().Keystore
dirks, ok := ks.(*localcrypto.DirKeyStore)
if ok {
hexkey, err := dirks.GetEncodedPubkey(params.GroupId, localcrypto.Sign)
if err != nil && strings.HasPrefix(err.Error(), "key not exist ") {
newsignaddr, err := dirks.NewKeyWithDefaultPassword(params.GroupId, localcrypto.Sign)
if err == nil && newsignaddr != "" {
err = nodeoptions.SetSignKeyMap(params.GroupId, newsignaddr)
if err != nil {
output[ERROR_INFO] = fmt.Sprintf("save key map %s err: %s", newsignaddr, err.Error())
return c.JSON(http.StatusBadRequest, output)
}
hexkey, err = dirks.GetEncodedPubkey(params.GroupId, localcrypto.Sign)
} else {
output[ERROR_INFO] = "create new group key err:" + err.Error()
return c.JSON(http.StatusBadRequest, output)
}
}
pubkeybytes, err := hex.DecodeString(hexkey)
p2ppubkey, err := p2pcrypto.UnmarshalSecp256k1PublicKey(pubkeybytes)
groupSignPubkey, err = p2pcrypto.MarshalPublicKey(p2ppubkey)
if err != nil {
output[ERROR_INFO] = "group key can't be decoded, err:" + err.Error()
return c.JSON(http.StatusBadRequest, output)
}
} else {
output[ERROR_INFO] = fmt.Sprintf("unknown keystore type %v:", ks)
return c.JSON(http.StatusBadRequest, output)
}
ownerPubkeyBytes, err := p2pcrypto.ConfigDecodeKey(params.OwnerPubKey)
if err != nil {
output[ERROR_INFO] = "Decode OwnerPubkey failed " + err.Error()
return c.JSON(http.StatusBadRequest, output)
}
ownerPubkey, err := p2pcrypto.UnmarshalPublicKey(ownerPubkeyBytes)
if err != nil {
return c.JSON(http.StatusBadRequest, output)
}
// decode signature
decodedSignature, err := hex.DecodeString(params.Signature)
if err != nil {
return c.JSON(http.StatusBadRequest, output)
}
cipherKey, err := hex.DecodeString(params.CipherKey)
if err != nil {
return c.JSON(http.StatusBadRequest, output)
}
groupEncryptkey, err := dirks.GetEncodedPubkey(params.GroupId, localcrypto.Encrypt)
if err != nil {
if strings.HasPrefix(err.Error(), "key not exist ") {
groupEncryptkey, err = dirks.NewKeyWithDefaultPassword(params.GroupId, localcrypto.Encrypt)
if err != nil {
output[ERROR_INFO] = "Create key pair failed with msg:" + err.Error()
return c.JSON(http.StatusBadRequest, output)
}
} else {
output[ERROR_INFO] = "Create key pair failed with msg:" + err.Error()
return c.JSON(http.StatusBadRequest, output)
}
}
var buffer bytes.Buffer
buffer.Write(genesisBlockBytes)
buffer.Write([]byte(params.GroupId))
buffer.Write([]byte(params.GroupName))
buffer.Write(ownerPubkeyBytes)
buffer.Write([]byte(params.ConsensusType))
buffer.Write([]byte(params.EncryptionType))
buffer.Write([]byte(params.AppKey))
buffer.Write(cipherKey)
hash := localcrypto.Hash(buffer.Bytes())
verifiy, err := ownerPubkey.Verify(hash, decodedSignature)
if err != nil {
output[ERROR_INFO] = err.Error()
return c.JSON(http.StatusBadRequest, output)
}
if !verifiy {
output[ERROR_INFO] = "Join Group failed, can not verify signature"
return c.JSON(http.StatusBadRequest, output)
}
var item *chestnutpb.GroupItem
item = &chestnutpb.GroupItem{}
item.OwnerPubKey = params.OwnerPubKey
item.GroupId = params.GroupId
item.GroupName = params.GroupName
item.OwnerPubKey = p2pcrypto.ConfigEncodeKey(ownerPubkeyBytes)
item.CipherKey = params.CipherKey
item.AppKey = params.AppKey
item.ConsenseType = chestnutpb.GroupConsenseType_POA
item.UserSignPubkey = p2pcrypto.ConfigEncodeKey(groupSignPubkey)
userEncryptKey, err := dirks.GetEncodedPubkey(params.GroupId, localcrypto.Encrypt)
if err != nil {
if strings.HasPrefix(err.Error(), "key not exist ") {
userEncryptKey, err = dirks.NewKeyWithDefaultPassword(params.GroupId, localcrypto.Encrypt)
if err != nil {
output[ERROR_INFO] = "Create key pair failed with msg:" + err.Error()
return c.JSON(http.StatusBadRequest, output)
}
} else {
output[ERROR_INFO] = "Create key pair failed with msg:" + err.Error()
return c.JSON(http.StatusBadRequest, output)
}
}
item.UserEncryptPubkey = userEncryptKey
item.UserSignPubkey = p2pcrypto.ConfigEncodeKey(groupSignPubkey)
if params.EncryptionType == "public" {
item.EncryptType = chestnutpb.GroupEncryptType_PUBLIC
} else {
item.EncryptType = chestnutpb.GroupEncryptType_PRIVATE
}
item.HighestHeight = 0
item.HighestBlockId = params.GenesisBlock.BlockId
item.LastUpdate = time.Now().UnixNano()
item.GenesisBlock = params.GenesisBlock
// create the group
var group *chain.Group
group = &chain.Group{}
err = group.CreateGrp(item)
if err != nil {
output[ERROR_INFO] = err.Error()
return c.JSON(http.StatusBadRequest, output)
}
// start sync
err = group.StartSync()
if err != nil {
output[ERROR_INFO] = err.Error()
return c.JSON(http.StatusBadRequest, output)
}
// add group to content
groupmgr := chain.GetGroupMgr()
groupmgr.Groups[group.Item.GroupId] = group
var bufferResult bytes.Buffer
bufferResult.Write(genesisBlockBytes)
bufferResult.Write([]byte(item.GroupId))
bufferResult.Write([]byte(item.GroupName))
bufferResult.Write(ownerPubkeyBytes)
bufferResult.Write(groupSignPubkey)
bufferResult.Write([]byte(groupEncryptkey))
buffer.Write([]byte(params.ConsensusType))
buffer.Write([]byte(params.EncryptionType))
buffer.Write([]byte(item.CipherKey))
buffer.Write([]byte(item.AppKey))
hashResult := chain.Hash(bufferResult.Bytes())
signature, err := ks.SignByKeyName(item.GroupId, hashResult)
encodedSign := hex.EncodeToString(signature)
joinGrpResult := &JoinGroupResult{
GroupId: item.GroupId,
GroupName: item.GroupName,
OwnerPubkey: item.OwnerPubKey,
ConsensusType: params.ConsensusType,
EncryptionType: params.EncryptionType,
UserPubkey: item.UserSignPubkey,
UserEncryptPubkey: groupEncryptkey,
CipherKey: item.CipherKey,
AppKey: item.AppKey,
Signature: encodedSign,
}
return c.JSON(http.StatusOK, joinGrpResult)
}