github.com/yous1230/fabric@v2.0.0-beta.0.20191224111736-74345bee6ac2+incompatible/protoutil/signeddata.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package protoutil 8 9 import ( 10 "bytes" 11 "fmt" 12 13 "github.com/golang/protobuf/proto" 14 "github.com/hyperledger/fabric-protos-go/common" 15 ) 16 17 // SignedData is used to represent the general triplet required to verify a signature 18 // This is intended to be generic across crypto schemes, while most crypto schemes will 19 // include the signing identity and a nonce within the Data, this is left to the crypto 20 // implementation. 21 type SignedData struct { 22 Data []byte 23 Identity []byte 24 Signature []byte 25 } 26 27 // ConfigUpdateEnvelopeAsSignedData returns the set of signatures for the 28 // ConfigUpdateEnvelope as SignedData or an error indicating why this was not 29 // possible. 30 func ConfigUpdateEnvelopeAsSignedData(ce *common.ConfigUpdateEnvelope) ([]*SignedData, error) { 31 if ce == nil { 32 return nil, fmt.Errorf("No signatures for nil SignedConfigItem") 33 } 34 35 result := make([]*SignedData, len(ce.Signatures)) 36 for i, configSig := range ce.Signatures { 37 sigHeader := &common.SignatureHeader{} 38 err := proto.Unmarshal(configSig.SignatureHeader, sigHeader) 39 if err != nil { 40 return nil, err 41 } 42 43 result[i] = &SignedData{ 44 Data: bytes.Join([][]byte{configSig.SignatureHeader, ce.ConfigUpdate}, nil), 45 Identity: sigHeader.Creator, 46 Signature: configSig.Signature, 47 } 48 49 } 50 51 return result, nil 52 } 53 54 // EnvelopeAsSignedData returns the signatures for the Envelope as SignedData 55 // slice of length 1 or an error indicating why this was not possible. 56 func EnvelopeAsSignedData(env *common.Envelope) ([]*SignedData, error) { 57 if env == nil { 58 return nil, fmt.Errorf("No signatures for nil Envelope") 59 } 60 61 payload := &common.Payload{} 62 err := proto.Unmarshal(env.Payload, payload) 63 if err != nil { 64 return nil, err 65 } 66 67 if payload.Header == nil /* || payload.Header.SignatureHeader == nil */ { 68 return nil, fmt.Errorf("Missing Header") 69 } 70 71 shdr := &common.SignatureHeader{} 72 err = proto.Unmarshal(payload.Header.SignatureHeader, shdr) 73 if err != nil { 74 return nil, fmt.Errorf("GetSignatureHeaderFromBytes failed, err %s", err) 75 } 76 77 return []*SignedData{{ 78 Data: env.Payload, 79 Identity: shdr.Creator, 80 Signature: env.Signature, 81 }}, nil 82 }