Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit db3029c

Browse files
committed
[FAB-6477] Reduce imported Fabric code
Change-Id: Ia0e978a10e167d90614c6c279ec016de7efa23ee Signed-off-by: Troy Ronda <troy@troyronda.com>
1 parent e216f82 commit db3029c

File tree

24 files changed

+154
-2571
lines changed

24 files changed

+154
-2571
lines changed

Gopkg.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/github.com/hyperledger/fabric/common/attrmgr/attrmgr.go

Lines changed: 0 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,7 @@ Please review third_party pinning scripts and patches for more details.
2626
package attrmgr
2727

2828
import (
29-
"crypto/x509"
30-
"crypto/x509/pkix"
3129
"encoding/asn1"
32-
"encoding/json"
33-
"fmt"
34-
35-
"github.com/hyperledger/fabric-sdk-go/pkg/errors"
3630
)
3731

3832
var (
@@ -59,156 +53,10 @@ type AttributeRequest interface {
5953
IsRequired() bool
6054
}
6155

62-
// New constructs an attribute manager
63-
func New() *Mgr { return &Mgr{} }
64-
6556
// Mgr is the attribute manager and is the main object for this package
6657
type Mgr struct{}
6758

68-
// ProcessAttributeRequestsForCert add attributes to an X509 certificate, given
69-
// attribute requests and attributes.
70-
func (mgr *Mgr) ProcessAttributeRequestsForCert(requests []AttributeRequest, attributes []Attribute, cert *x509.Certificate) error {
71-
attrs, err := mgr.ProcessAttributeRequests(requests, attributes)
72-
if err != nil {
73-
return err
74-
}
75-
return mgr.AddAttributesToCert(attrs, cert)
76-
}
77-
78-
// ProcessAttributeRequests takes an array of attribute requests and an identity's attributes
79-
// and returns an Attributes object containing the requested attributes.
80-
func (mgr *Mgr) ProcessAttributeRequests(requests []AttributeRequest, attributes []Attribute) (*Attributes, error) {
81-
attrsMap := map[string]string{}
82-
attrs := &Attributes{Attrs: attrsMap}
83-
missingRequiredAttrs := []string{}
84-
// For each of the attribute requests
85-
for _, req := range requests {
86-
// Get the attribute
87-
name := req.GetName()
88-
attr := getAttrByName(name, attributes)
89-
if attr == nil {
90-
if req.IsRequired() {
91-
// Didn't find attribute and it was required; return error below
92-
missingRequiredAttrs = append(missingRequiredAttrs, name)
93-
}
94-
// Skip attribute requests which aren't required
95-
continue
96-
}
97-
attrsMap[name] = attr.GetValue()
98-
}
99-
if len(missingRequiredAttrs) > 0 {
100-
return nil, errors.Errorf("The following required attributes are missing: %+v",
101-
missingRequiredAttrs)
102-
}
103-
return attrs, nil
104-
}
105-
106-
// AddAttributesToCert adds public attribute info to an X509 certificate.
107-
func (mgr *Mgr) AddAttributesToCert(attrs *Attributes, cert *x509.Certificate) error {
108-
buf, err := json.Marshal(attrs)
109-
if err != nil {
110-
return errors.Wrap(err, "Failed to marshal attributes")
111-
}
112-
ext := pkix.Extension{
113-
Id: AttrOID,
114-
Critical: false,
115-
Value: buf,
116-
}
117-
cert.Extensions = append(cert.Extensions, ext)
118-
return nil
119-
}
120-
121-
// GetAttributesFromCert gets the attributes from a certificate.
122-
func (mgr *Mgr) GetAttributesFromCert(cert *x509.Certificate) (*Attributes, error) {
123-
// Get certificate attributes from the certificate if it exists
124-
buf, err := getAttributesFromCert(cert)
125-
if err != nil {
126-
return nil, err
127-
}
128-
// Unmarshal into attributes object
129-
attrs := &Attributes{}
130-
if buf != nil {
131-
err := json.Unmarshal(buf, attrs)
132-
if err != nil {
133-
return nil, errors.Wrap(err, "Failed to unmarshal attributes from certificate")
134-
}
135-
}
136-
return attrs, nil
137-
}
138-
13959
// Attributes contains attribute names and values
14060
type Attributes struct {
14161
Attrs map[string]string `json:"attrs"`
14262
}
143-
144-
// Names returns the names of the attributes
145-
func (a *Attributes) Names() []string {
146-
i := 0
147-
names := make([]string, len(a.Attrs))
148-
for name := range a.Attrs {
149-
names[i] = name
150-
i++
151-
}
152-
return names
153-
}
154-
155-
// Contains returns true if the named attribute is found
156-
func (a *Attributes) Contains(name string) bool {
157-
_, ok := a.Attrs[name]
158-
return ok
159-
}
160-
161-
// Value returns an attribute's value
162-
func (a *Attributes) Value(name string) (string, bool, error) {
163-
attr, ok := a.Attrs[name]
164-
return attr, ok, nil
165-
}
166-
167-
// True returns nil if the value of attribute 'name' is true;
168-
// otherwise, an appropriate error is returned.
169-
func (a *Attributes) True(name string) error {
170-
val, ok, err := a.Value(name)
171-
if err != nil {
172-
return err
173-
}
174-
if !ok {
175-
return fmt.Errorf("Attribute '%s' was not found", name)
176-
}
177-
if val != "true" {
178-
return fmt.Errorf("Attribute '%s' is not true", name)
179-
}
180-
return nil
181-
}
182-
183-
// Get the attribute info from a certificate extension, or return nil if not found
184-
func getAttributesFromCert(cert *x509.Certificate) ([]byte, error) {
185-
for _, ext := range cert.Extensions {
186-
if isAttrOID(ext.Id) {
187-
return ext.Value, nil
188-
}
189-
}
190-
return nil, nil
191-
}
192-
193-
// Is the object ID equal to the attribute info object ID?
194-
func isAttrOID(oid asn1.ObjectIdentifier) bool {
195-
if len(oid) != len(AttrOID) {
196-
return false
197-
}
198-
for idx, val := range oid {
199-
if val != AttrOID[idx] {
200-
return false
201-
}
202-
}
203-
return true
204-
}
205-
206-
// Get an attribute from 'attrs' by its name, or nil if not found
207-
func getAttrByName(name string, attrs []Attribute) Attribute {
208-
for _, attr := range attrs {
209-
if attr.GetName() == name {
210-
return attr
211-
}
212-
}
213-
return nil
214-
}

internal/github.com/hyperledger/fabric/common/util/utils.go

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,10 @@ Please review third_party pinning scripts and patches for more details.
2121
package util
2222

2323
import (
24-
"crypto/rand"
2524
"fmt"
26-
"io"
27-
"math/big"
28-
"strings"
2925
"time"
3026

3127
"github.com/golang/protobuf/ptypes/timestamp"
32-
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/metadata"
3328
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/bccsp"
3429
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/bccsp/factory"
3530
)
@@ -53,45 +48,6 @@ func ComputeSHA256(data []byte) (hash []byte) {
5348
return
5449
}
5550

56-
// ComputeSHA3256 returns SHA3-256 on data
57-
func ComputeSHA3256(data []byte) (hash []byte) {
58-
hash, err := factory.GetDefault().Hash(data, &bccsp.SHA3_256Opts{})
59-
if err != nil {
60-
panic(fmt.Errorf("Failed computing SHA3_256 on [% x]", data))
61-
}
62-
return
63-
}
64-
65-
// GenerateBytesUUID returns a UUID based on RFC 4122 returning the generated bytes
66-
func GenerateBytesUUID() []byte {
67-
uuid := make([]byte, 16)
68-
_, err := io.ReadFull(rand.Reader, uuid)
69-
if err != nil {
70-
panic(fmt.Sprintf("Error generating UUID: %s", err))
71-
}
72-
73-
// variant bits; see section 4.1.1
74-
uuid[8] = uuid[8]&^0xc0 | 0x80
75-
76-
// version 4 (pseudo-random); see section 4.1.3
77-
uuid[6] = uuid[6]&^0xf0 | 0x40
78-
79-
return uuid
80-
}
81-
82-
// GenerateIntUUID returns a UUID based on RFC 4122 returning a big.Int
83-
func GenerateIntUUID() *big.Int {
84-
uuid := GenerateBytesUUID()
85-
z := big.NewInt(0)
86-
return z.SetBytes(uuid)
87-
}
88-
89-
// GenerateUUID returns a UUID based on RFC 4122
90-
func GenerateUUID() string {
91-
uuid := GenerateBytesUUID()
92-
return idBytesToStr(uuid)
93-
}
94-
9551
// CreateUtcTimestamp returns a google/protobuf/Timestamp in UTC
9652
func CreateUtcTimestamp() *timestamp.Timestamp {
9753
now := time.Now().UTC()
@@ -100,86 +56,14 @@ func CreateUtcTimestamp() *timestamp.Timestamp {
10056
return &(timestamp.Timestamp{Seconds: secs, Nanos: nanos})
10157
}
10258

103-
//GenerateHashFromSignature returns a hash of the combined parameters
104-
func GenerateHashFromSignature(path string, args []byte) []byte {
105-
return ComputeSHA256(args)
106-
}
107-
10859
// GenerateIDfromTxSHAHash generates SHA256 hash using Tx payload
10960
func GenerateIDfromTxSHAHash(payload []byte) string {
11061
return fmt.Sprintf("%x", ComputeSHA256(payload))
11162
}
11263

113-
// GenerateIDWithAlg generates an ID using a custom algorithm
114-
func GenerateIDWithAlg(customIDgenAlg string, payload []byte) (string, error) {
115-
if customIDgenAlg == "" {
116-
customIDgenAlg = defaultAlg
117-
}
118-
var alg = availableIDgenAlgs[customIDgenAlg]
119-
if alg.hashFun != nil {
120-
return alg.hashFun(payload), nil
121-
}
122-
return "", fmt.Errorf("Wrong ID generation algorithm was given: %s", customIDgenAlg)
123-
}
124-
125-
func idBytesToStr(id []byte) string {
126-
return fmt.Sprintf("%x-%x-%x-%x-%x", id[0:4], id[4:6], id[6:8], id[8:10], id[10:])
127-
}
128-
129-
// FindMissingElements identifies the elements of the first slice that are not present in the second
130-
// The second slice is expected to be a subset of the first slice
131-
func FindMissingElements(all []string, some []string) (delta []string) {
132-
all:
133-
for _, v1 := range all {
134-
for _, v2 := range some {
135-
if strings.Compare(v1, v2) == 0 {
136-
continue all
137-
}
138-
}
139-
delta = append(delta, v1)
140-
}
141-
return
142-
}
143-
144-
// ToChaincodeArgs converts string args to []byte args
145-
func ToChaincodeArgs(args ...string) [][]byte {
146-
bargs := make([][]byte, len(args))
147-
for i, arg := range args {
148-
bargs[i] = []byte(arg)
149-
}
150-
return bargs
151-
}
152-
153-
// ArrayToChaincodeArgs converts array of string args to array of []byte args
154-
func ArrayToChaincodeArgs(args []string) [][]byte {
155-
bargs := make([][]byte, len(args))
156-
for i, arg := range args {
157-
bargs[i] = []byte(arg)
158-
}
159-
return bargs
160-
}
161-
16264
const testchainid = "testchainid"
16365
const testorgid = "**TEST_ORGID**"
16466

165-
//GetTestChainID returns the CHAINID constant in use by orderer
166-
func GetTestChainID() string {
167-
return testchainid
168-
}
169-
170-
//GetTestOrgID returns the ORGID constant in use by gossip join message
171-
func GetTestOrgID() string {
172-
return testorgid
173-
}
174-
175-
//GetSysCCVersion returns the version of all system chaincodes
176-
//This needs to be revisited on policies around system chaincode
177-
//"upgrades" from user and relationship with "fabric" upgrade. For
178-
//now keep it simple and use the fabric's version stamp
179-
func GetSysCCVersion() string {
180-
return metadata.Version
181-
}
182-
18367
// ConcatenateBytes is useful for combining multiple arrays of bytes, especially for
18468
// signatures or digests over multiple fields
18569
func ConcatenateBytes(data ...[]byte) []byte {

internal/github.com/hyperledger/fabric/core/comm/config.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ Please review third_party pinning scripts and patches for more details.
1111
package comm
1212

1313
import (
14-
"time"
15-
1614
"github.com/spf13/viper"
17-
"google.golang.org/grpc"
18-
"google.golang.org/grpc/keepalive"
1915
)
2016

2117
var (
@@ -76,56 +72,8 @@ func MaxRecvMsgSize() int {
7672
return maxRecvMsgSize
7773
}
7874

79-
// SetMaxRecvMsgSize sets the maximum message size in bytes that gRPC clients
80-
// and servers can receive
81-
func SetMaxRecvMsgSize(size int) {
82-
maxRecvMsgSize = size
83-
}
84-
8575
// MaxSendMsgSize returns the maximum message size in bytes that gRPC clients
8676
// and servers can send
8777
func MaxSendMsgSize() int {
8878
return maxSendMsgSize
8979
}
90-
91-
// SetMaxSendMsgSize sets the maximum message size in bytes that gRPC clients
92-
// and servers can send
93-
func SetMaxSendMsgSize(size int) {
94-
maxSendMsgSize = size
95-
}
96-
97-
// SetKeepaliveOptions sets the gRPC keepalive options for both clients and
98-
// servers
99-
func SetKeepaliveOptions(ka KeepaliveOptions) {
100-
keepaliveOptions = ka
101-
}
102-
103-
// ServerKeepaliveOptions returns the gRPC keepalive options for servers
104-
func ServerKeepaliveOptions() []grpc.ServerOption {
105-
var serverOpts []grpc.ServerOption
106-
kap := keepalive.ServerParameters{
107-
Time: time.Duration(keepaliveOptions.ServerKeepaliveTime) * time.Second,
108-
Timeout: time.Duration(keepaliveOptions.ServerKeepaliveTimeout) * time.Second,
109-
}
110-
serverOpts = append(serverOpts, grpc.KeepaliveParams(kap))
111-
kep := keepalive.EnforcementPolicy{
112-
// needs to match clientKeepalive
113-
MinTime: time.Duration(keepaliveOptions.ClientKeepaliveTime) * time.Second,
114-
// allow keepalive w/o rpc
115-
PermitWithoutStream: true,
116-
}
117-
serverOpts = append(serverOpts, grpc.KeepaliveEnforcementPolicy(kep))
118-
return serverOpts
119-
}
120-
121-
// ClientKeepaliveOptions returns the gRPC keepalive options for clients
122-
func ClientKeepaliveOptions() []grpc.DialOption {
123-
var dialOpts []grpc.DialOption
124-
kap := keepalive.ClientParameters{
125-
Time: time.Duration(keepaliveOptions.ClientKeepaliveTime) * time.Second,
126-
Timeout: time.Duration(keepaliveOptions.ClientKeepaliveTimeout) * time.Second,
127-
PermitWithoutStream: true,
128-
}
129-
dialOpts = append(dialOpts, grpc.WithKeepaliveParams(kap))
130-
return dialOpts
131-
}

0 commit comments

Comments
 (0)