forked from CognitionFoundry/gohfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
105 lines (92 loc) · 2.98 KB
/
channel.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
/*
Copyright: Cognition Foundry. All Rights Reserved.
License: Apache License Version 2.0
*/
package gohfc
import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/protos/common"
"io/ioutil"
"fmt"
)
const LSCC = "lscc"
const QSCC = "qscc"
const CSCC = "cscc"
// Channel define channel
type Channel struct {
// ChannelName is channel name over which operations will be executed.
ChannelName string
// MspId identify which member service in peer must be used to verify operation.
MspId string
}
// QueryChannelsResponse holds the result from querying which channels peer is currently joined
type QueryChannelsResponse struct {
PeerName string
Error error
Channels []string
}
// QueryChannelInfoResponse hold the response for querying channel info from particular peer
type QueryChannelInfoResponse struct {
PeerName string
Error error
Info *common.BlockchainInfo
}
// decodeChannelFromFs reads channel.tx file from file system and decode it in Envelope structure
func decodeChannelFromFs(path string) (*common.Envelope, error) {
channel, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
envelope := new(common.Envelope)
if err := proto.Unmarshal(channel, envelope); err != nil {
return nil, err
}
return envelope, nil
}
// buildAndSignChannelConfig take channel config payload and prepare the structure need for join transaction
func buildAndSignChannelConfig(identity *Identity, configPayload []byte, crypto CryptoSuite, channel *Channel) (*common.Envelope, error) {
pl := &common.Payload{}
if err := proto.Unmarshal(configPayload, pl); err != nil {
return nil, fmt.Errorf("envelope does not carry a valid payload: %s", err)
}
configUpdateEnvelope := &common.ConfigUpdateEnvelope{}
err := proto.Unmarshal(pl.GetData(), configUpdateEnvelope)
if err != nil {
return nil, err
}
creator, err := marshalProtoIdentity(identity, channel)
if err != nil {
return nil, err
}
txId, err := newTransactionId(creator)
if err != nil {
return nil, err
}
sigHeaderBytes, err := signatureHeader(creator, txId)
if err != nil {
return nil, err
}
sig, err := crypto.Sign(append(sigHeaderBytes, configUpdateEnvelope.GetConfigUpdate()...), identity.PrivateKey)
if err != nil {
return nil, err
}
configSignature := new(common.ConfigSignature)
configSignature.SignatureHeader = sigHeaderBytes
configSignature.Signature = sig
configUpdateEnvelope.Signatures = append(configUpdateEnvelope.GetSignatures(), configSignature)
channelHeaderBytes, err := channelHeader(common.HeaderType_CONFIG_UPDATE, txId, channel,0,nil)
header := header(sigHeaderBytes, channelHeaderBytes)
envelopeBytes, err := proto.Marshal(configUpdateEnvelope)
if err != nil {
return nil, err
}
commonPayload, err := payload(header, envelopeBytes)
if err != nil {
return nil, err
}
signedCommonPayload, err := crypto.Sign(commonPayload, identity.PrivateKey)
if err != nil {
return nil, err
}
return &common.Envelope{Payload: commonPayload, Signature: signedCommonPayload }, nil
}