github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/protos/common/common.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package common 18 19 import ( 20 "fmt" 21 22 "github.com/golang/protobuf/proto" 23 ) 24 25 func (e *Envelope) StaticallyOpaqueFields() []string { 26 return []string{"payload"} 27 } 28 29 func (e *Envelope) StaticallyOpaqueFieldProto(name string) (proto.Message, error) { 30 if name != e.StaticallyOpaqueFields()[0] { 31 return nil, fmt.Errorf("not a marshaled field: %s", name) 32 } 33 return &Payload{}, nil 34 } 35 36 func (p *Payload) VariablyOpaqueFields() []string { 37 return []string{"data"} 38 } 39 40 func (p *Payload) VariablyOpaqueFieldProto(name string) (proto.Message, error) { 41 if name != p.VariablyOpaqueFields()[0] { 42 return nil, fmt.Errorf("not a marshaled field: %s", name) 43 } 44 if p.Header == nil { 45 return nil, fmt.Errorf("cannot determine payload type when header is missing") 46 } 47 ch := &ChannelHeader{} 48 if err := proto.Unmarshal(p.Header.ChannelHeader, ch); err != nil { 49 return nil, fmt.Errorf("corrupt channel header: %s", err) 50 } 51 52 switch ch.Type { 53 case int32(HeaderType_CONFIG): 54 return &ConfigEnvelope{}, nil 55 case int32(HeaderType_CONFIG_UPDATE): 56 return &ConfigUpdateEnvelope{}, nil 57 // TODO implement the other well known types, particularly endorser txs 58 default: 59 return nil, fmt.Errorf("decoding type %v is unimplemented", ch.Type) 60 } 61 } 62 63 func (h *Header) StaticallyOpaqueFields() []string { 64 return []string{"channel_header", "signature_header"} 65 } 66 67 func (h *Header) StaticallyOpaqueFieldProto(name string) (proto.Message, error) { 68 switch name { 69 case h.StaticallyOpaqueFields()[0]: // channel_header 70 return &ChannelHeader{}, nil 71 case h.StaticallyOpaqueFields()[1]: // signature_header 72 return &SignatureHeader{}, nil 73 default: 74 return nil, fmt.Errorf("unknown header field: %s", name) 75 } 76 } 77 78 func (bd *BlockData) StaticallyOpaqueSliceFields() []string { 79 return []string{"data"} 80 } 81 82 func (bd *BlockData) StaticallyOpaqueSliceFieldProto(fieldName string, index int) (proto.Message, error) { 83 if fieldName != bd.StaticallyOpaqueSliceFields()[0] { 84 return nil, fmt.Errorf("not an opaque slice field: %s", fieldName) 85 } 86 87 return &Envelope{}, nil 88 }