github.com/yimialmonte/fabric@v2.1.1+incompatible/common/tools/protolator/protoext/commonext/common.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package commonext
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/golang/protobuf/proto"
    13  	"github.com/hyperledger/fabric-protos-go/common"
    14  	"github.com/hyperledger/fabric-protos-go/msp"
    15  	"github.com/hyperledger/fabric-protos-go/peer"
    16  )
    17  
    18  type Envelope struct{ *common.Envelope }
    19  
    20  func (e *Envelope) Underlying() proto.Message {
    21  	return e.Envelope
    22  }
    23  
    24  func (e *Envelope) StaticallyOpaqueFields() []string {
    25  	return []string{"payload"}
    26  }
    27  
    28  func (e *Envelope) StaticallyOpaqueFieldProto(name string) (proto.Message, error) {
    29  	if name != e.StaticallyOpaqueFields()[0] {
    30  		return nil, fmt.Errorf("not a marshaled field: %s", name)
    31  	}
    32  	return &common.Payload{}, nil
    33  }
    34  
    35  type Payload struct{ *common.Payload }
    36  
    37  func (p *Payload) Underlying() proto.Message {
    38  	return p.Payload
    39  }
    40  
    41  func (p *Payload) VariablyOpaqueFields() []string {
    42  	return []string{"data"}
    43  }
    44  
    45  func (p *Payload) VariablyOpaqueFieldProto(name string) (proto.Message, error) {
    46  	if name != p.VariablyOpaqueFields()[0] {
    47  		return nil, fmt.Errorf("not a marshaled field: %s", name)
    48  	}
    49  	if p.Header == nil {
    50  		return nil, fmt.Errorf("cannot determine payload type when header is missing")
    51  	}
    52  	ch := &common.ChannelHeader{}
    53  	if err := proto.Unmarshal(p.Header.ChannelHeader, ch); err != nil {
    54  		return nil, fmt.Errorf("corrupt channel header: %s", err)
    55  	}
    56  
    57  	switch ch.Type {
    58  	case int32(common.HeaderType_CONFIG):
    59  		return &common.ConfigEnvelope{}, nil
    60  	case int32(common.HeaderType_ORDERER_TRANSACTION):
    61  		return &common.Envelope{}, nil
    62  	case int32(common.HeaderType_CONFIG_UPDATE):
    63  		return &common.ConfigUpdateEnvelope{}, nil
    64  	case int32(common.HeaderType_MESSAGE):
    65  		// Only used by broadcast_msg sample client
    66  		return &common.ConfigValue{}, nil
    67  	case int32(common.HeaderType_ENDORSER_TRANSACTION):
    68  		return &peer.Transaction{}, nil
    69  	default:
    70  		return nil, fmt.Errorf("decoding type %v is unimplemented", ch.Type)
    71  	}
    72  }
    73  
    74  type ChannelHeader struct{ *common.ChannelHeader }
    75  
    76  func (ch *ChannelHeader) Underlying() proto.Message {
    77  	return ch.ChannelHeader
    78  }
    79  
    80  func (ch *ChannelHeader) VariablyOpaqueFields() []string {
    81  	return []string{"extension"}
    82  }
    83  
    84  func (ch *ChannelHeader) VariablyOpaqueFieldProto(name string) (proto.Message, error) {
    85  	if name != "extension" {
    86  		return nil, fmt.Errorf("not an opaque field")
    87  	}
    88  
    89  	switch ch.Type {
    90  	case int32(common.HeaderType_ENDORSER_TRANSACTION):
    91  		return &peer.ChaincodeHeaderExtension{}, nil
    92  	default:
    93  		return nil, fmt.Errorf("channel header extension only valid for endorser transactions")
    94  	}
    95  }
    96  
    97  type Header struct{ *common.Header }
    98  
    99  func (h *Header) Underlying() proto.Message {
   100  	return h.Header
   101  }
   102  
   103  func (h *Header) StaticallyOpaqueFields() []string {
   104  	return []string{"channel_header", "signature_header"}
   105  }
   106  
   107  func (h *Header) StaticallyOpaqueFieldProto(name string) (proto.Message, error) {
   108  	switch name {
   109  	case h.StaticallyOpaqueFields()[0]: // channel_header
   110  		return &common.ChannelHeader{}, nil
   111  	case h.StaticallyOpaqueFields()[1]: // signature_header
   112  		return &common.SignatureHeader{}, nil
   113  	default:
   114  		return nil, fmt.Errorf("unknown header field: %s", name)
   115  	}
   116  }
   117  
   118  type SignatureHeader struct{ *common.SignatureHeader }
   119  
   120  func (sh *SignatureHeader) Underlying() proto.Message {
   121  	return sh.SignatureHeader
   122  }
   123  
   124  func (sh *SignatureHeader) StaticallyOpaqueFields() []string {
   125  	return []string{"creator"}
   126  }
   127  
   128  func (sh *SignatureHeader) StaticallyOpaqueFieldProto(name string) (proto.Message, error) {
   129  	switch name {
   130  	case sh.StaticallyOpaqueFields()[0]: // creator
   131  		return &msp.SerializedIdentity{}, nil
   132  	default:
   133  		return nil, fmt.Errorf("unknown header field: %s", name)
   134  	}
   135  }
   136  
   137  type BlockData struct{ *common.BlockData }
   138  
   139  func (bd *BlockData) Underlying() proto.Message {
   140  	return bd.BlockData
   141  }
   142  
   143  func (bd *BlockData) StaticallyOpaqueSliceFields() []string {
   144  	return []string{"data"}
   145  }
   146  
   147  func (bd *BlockData) StaticallyOpaqueSliceFieldProto(fieldName string, index int) (proto.Message, error) {
   148  	if fieldName != bd.StaticallyOpaqueSliceFields()[0] {
   149  		return nil, fmt.Errorf("not an opaque slice field: %s", fieldName)
   150  	}
   151  
   152  	return &common.Envelope{}, nil
   153  }