github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/protos/utils/commonutils.go (about) 1 /* 2 Copyright IBM Corp. 2016 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 utils 18 19 import ( 20 "fmt" 21 "time" 22 23 "github.com/hyperledger/fabric/core/crypto/primitives" 24 cb "github.com/hyperledger/fabric/protos/common" 25 26 "errors" 27 28 "github.com/golang/protobuf/proto" 29 "github.com/golang/protobuf/ptypes/timestamp" 30 "github.com/hyperledger/fabric/common/crypto" 31 ) 32 33 // MarshalOrPanic serializes a protobuf message and panics if this operation fails. 34 func MarshalOrPanic(pb proto.Message) []byte { 35 data, err := proto.Marshal(pb) 36 if err != nil { 37 panic(err) 38 } 39 return data 40 } 41 42 // Marshal serializes a protobuf message. 43 func Marshal(pb proto.Message) ([]byte, error) { 44 return proto.Marshal(pb) 45 } 46 47 // CreateNonceOrPanic generates a nonce using the crypto/primitives package 48 // and panics if this operation fails. 49 func CreateNonceOrPanic() []byte { 50 nonce, err := primitives.GetRandomNonce() 51 if err != nil { 52 panic(fmt.Errorf("Cannot generate random nonce: %s", err)) 53 } 54 return nonce 55 } 56 57 // CreateNonce generates a nonce using the crypto/primitives package. 58 func CreateNonce() ([]byte, error) { 59 nonce, err := primitives.GetRandomNonce() 60 if err != nil { 61 return nil, fmt.Errorf("Cannot generate random nonce: %s", err) 62 } 63 return nonce, nil 64 } 65 66 // UnmarshalPayloadOrPanic unmarshals bytes to a Payload structure or panics on error 67 func UnmarshalPayloadOrPanic(encoded []byte) *cb.Payload { 68 payload, err := UnmarshalPayload(encoded) 69 if err != nil { 70 panic(fmt.Errorf("Error unmarshaling data to payload: %s", err)) 71 } 72 return payload 73 } 74 75 // UnmarshalPayload unmarshals bytes to a Payload structure 76 func UnmarshalPayload(encoded []byte) (*cb.Payload, error) { 77 payload := &cb.Payload{} 78 err := proto.Unmarshal(encoded, payload) 79 if err != nil { 80 return nil, err 81 } 82 return payload, err 83 } 84 85 // UnmarshalEnvelopeOrPanic unmarshals bytes to an Envelope structure or panics on error 86 func UnmarshalEnvelopeOrPanic(encoded []byte) *cb.Envelope { 87 envelope, err := UnmarshalEnvelope(encoded) 88 if err != nil { 89 panic(fmt.Errorf("Error unmarshaling data to envelope: %s", err)) 90 } 91 return envelope 92 } 93 94 // UnmarshalEnvelope unmarshals bytes to an Envelope structure 95 func UnmarshalEnvelope(encoded []byte) (*cb.Envelope, error) { 96 envelope := &cb.Envelope{} 97 err := proto.Unmarshal(encoded, envelope) 98 if err != nil { 99 return nil, err 100 } 101 return envelope, err 102 } 103 104 // ExtractEnvelopeOrPanic retrieves the requested envelope from a given block and unmarshals it -- it panics if either of these operation fail. 105 func ExtractEnvelopeOrPanic(block *cb.Block, index int) *cb.Envelope { 106 envelope, err := ExtractEnvelope(block, index) 107 if err != nil { 108 panic(err) 109 } 110 return envelope 111 } 112 113 // ExtractEnvelope retrieves the requested envelope from a given block and unmarshals it. 114 func ExtractEnvelope(block *cb.Block, index int) (*cb.Envelope, error) { 115 envelopeCount := len(block.Data.Data) 116 if index < 0 || index >= envelopeCount { 117 return nil, fmt.Errorf("Envelope index out of bounds") 118 } 119 marshaledEnvelope := block.Data.Data[index] 120 envelope, err := GetEnvelopeFromBlock(marshaledEnvelope) 121 if err != nil { 122 return nil, fmt.Errorf("Block data does not carry an envelope at index %d: %s", index, err) 123 } 124 return envelope, nil 125 } 126 127 // ExtractPayloadOrPanic retrieves the payload of a given envelope and unmarshals it -- it panics if either of these operations fail. 128 func ExtractPayloadOrPanic(envelope *cb.Envelope) *cb.Payload { 129 payload, err := ExtractPayload(envelope) 130 if err != nil { 131 panic(err) 132 } 133 return payload 134 } 135 136 // ExtractPayload retrieves the payload of a given envelope and unmarshals it. 137 func ExtractPayload(envelope *cb.Envelope) (*cb.Payload, error) { 138 payload := &cb.Payload{} 139 if err := proto.Unmarshal(envelope.Payload, payload); err != nil { 140 return nil, fmt.Errorf("Envelope does not carry a Payload: %s", err) 141 } 142 return payload, nil 143 } 144 145 // MakeChannelHeader creates a ChannelHeader. 146 func MakeChannelHeader(headerType cb.HeaderType, version int32, chainID string, epoch uint64) *cb.ChannelHeader { 147 return &cb.ChannelHeader{ 148 Type: int32(headerType), 149 Version: version, 150 Timestamp: ×tamp.Timestamp{ 151 Seconds: time.Now().Unix(), 152 Nanos: 0, 153 }, 154 ChannelId: chainID, 155 Epoch: epoch, 156 } 157 } 158 159 // MakeSignatureHeader creates a SignatureHeader. 160 func MakeSignatureHeader(serializedCreatorCertChain []byte, nonce []byte) *cb.SignatureHeader { 161 return &cb.SignatureHeader{ 162 Creator: serializedCreatorCertChain, 163 Nonce: nonce, 164 } 165 } 166 167 func SetTxID(channelHeader *cb.ChannelHeader, signatureHeader *cb.SignatureHeader) error { 168 txid, err := ComputeProposalTxID( 169 signatureHeader.Nonce, 170 signatureHeader.Creator, 171 ) 172 if err != nil { 173 return err 174 } 175 channelHeader.TxId = txid 176 return nil 177 } 178 179 // MakePayloadHeader creates a Payload Header. 180 func MakePayloadHeader(ch *cb.ChannelHeader, sh *cb.SignatureHeader) *cb.Header { 181 return &cb.Header{ 182 ChannelHeader: MarshalOrPanic(ch), 183 SignatureHeader: MarshalOrPanic(sh), 184 } 185 } 186 187 // NewSignatureHeaderOrPanic returns a signature header and panics on error. 188 func NewSignatureHeaderOrPanic(signer crypto.LocalSigner) *cb.SignatureHeader { 189 if signer == nil { 190 panic(errors.New("Invalid signer. Must be different from nil.")) 191 } 192 193 signatureHeader, err := signer.NewSignatureHeader() 194 if err != nil { 195 panic(fmt.Errorf("Failed generating a new SignatureHeader [%s]", err)) 196 } 197 return signatureHeader 198 } 199 200 // SignOrPanic signs a message and panics on error. 201 func SignOrPanic(signer crypto.LocalSigner, msg []byte) []byte { 202 if signer == nil { 203 panic(errors.New("Invalid signer. Must be different from nil.")) 204 } 205 206 sigma, err := signer.Sign(msg) 207 if err != nil { 208 panic(fmt.Errorf("Failed generting signature [%s]", err)) 209 } 210 return sigma 211 } 212 213 // UnmarshalChannelHeader returns a ChannelHeader from bytes 214 func UnmarshalChannelHeader(bytes []byte) (*cb.ChannelHeader, error) { 215 chdr := &cb.ChannelHeader{} 216 err := proto.Unmarshal(bytes, chdr) 217 if err != nil { 218 return nil, fmt.Errorf("UnmarshalChannelHeader failed, err %s", err) 219 } 220 221 return chdr, nil 222 }