-
Notifications
You must be signed in to change notification settings - Fork 19
/
channel.go
112 lines (91 loc) · 2.95 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
106
107
108
109
110
111
112
package channel
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/hyperledger/fabric-admin-sdk/internal/osnadmin"
"github.com/hyperledger/fabric-admin-sdk/internal/protoutil"
"github.com/hyperledger/fabric-admin-sdk/pkg/identity"
"github.com/hyperledger/fabric-admin-sdk/pkg/internal/proposal"
"github.com/hyperledger/fabric-protos-go-apiv2/peer"
cb "github.com/hyperledger/fabric-protos-go-apiv2/common"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
)
type ChannelList struct {
SystemChannel interface{} `json:"systemChannel"`
Channels []struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"channels"`
}
func CreateChannel(osnURL string, block *cb.Block, caCertPool *x509.CertPool, tlsClientCert tls.Certificate) (*http.Response, error) {
blockBytes := protoutil.MarshalOrPanic(block)
return osnadmin.Join(osnURL, blockBytes, caCertPool, tlsClientCert)
}
func JoinChannel(ctx context.Context, connection grpc.ClientConnInterface, id identity.SigningIdentity, block *cb.Block) error {
blockBytes, err := proto.Marshal(block)
if err != nil {
return fmt.Errorf("failed to marshal block: %w", err)
}
prop, err := proposal.NewProposal(id, "cscc", "JoinChain", proposal.WithArguments(blockBytes), proposal.WithType(cb.HeaderType_CONFIG))
if err != nil {
return err
}
signedProp, err := proposal.NewSignedProposal(prop, id)
if err != nil {
return err
}
endorser := peer.NewEndorserClient(connection)
proposalResp, err := endorser.ProcessProposal(ctx, signedProp)
if err != nil {
return err
}
if err := proposal.CheckSuccessfulResponse(proposalResp); err != nil {
return err
}
return nil
}
func ListChannel(osnURL string, caCertPool *x509.CertPool, tlsClientCert tls.Certificate) (ChannelList, error) {
var channels ChannelList
resp, err := osnadmin.ListAllChannels(osnURL, caCertPool, tlsClientCert)
if err != nil {
return channels, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return channels, err
}
if err := json.Unmarshal(body, &channels); err != nil {
return channels, err
}
return channels, nil
}
func ListChannelOnPeer(ctx context.Context, connection grpc.ClientConnInterface, id identity.SigningIdentity) ([]*peer.ChannelInfo, error) {
prop, err := proposal.NewProposal(id, "cscc", "GetChannels", proposal.WithType(cb.HeaderType_ENDORSER_TRANSACTION))
if err != nil {
return nil, err
}
signedProp, err := proposal.NewSignedProposal(prop, id)
if err != nil {
return nil, err
}
endorser := peer.NewEndorserClient(connection)
proposalResp, err := endorser.ProcessProposal(ctx, signedProp)
if err != nil {
return nil, err
}
if err := proposal.CheckSuccessfulResponse(proposalResp); err != nil {
return nil, err
}
var channelQueryResponse peer.ChannelQueryResponse
err = proto.Unmarshal(proposalResp.Response.Payload, &channelQueryResponse)
if err != nil {
return nil, err
}
return channelQueryResponse.Channels, nil
}