|
| 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 util |
| 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 | + "github.com/golang/protobuf/proto" |
| 27 | + "github.com/golang/protobuf/ptypes/timestamp" |
| 28 | +) |
| 29 | + |
| 30 | +// MarshalOrPanic serializes a protobuf message and panics if this operation fails. |
| 31 | +func MarshalOrPanic(pb proto.Message) []byte { |
| 32 | + data, err := proto.Marshal(pb) |
| 33 | + if err != nil { |
| 34 | + panic(err) |
| 35 | + } |
| 36 | + return data |
| 37 | +} |
| 38 | + |
| 39 | +// Marshal serializes a protobuf message. |
| 40 | +func Marshal(pb proto.Message) ([]byte, error) { |
| 41 | + return proto.Marshal(pb) |
| 42 | +} |
| 43 | + |
| 44 | +// CreateNonceOrPanic generates a nonce using the crypto/primitives package |
| 45 | +// and panics if this operation fails. |
| 46 | +func CreateNonceOrPanic() []byte { |
| 47 | + nonce, err := primitives.GetRandomNonce() |
| 48 | + if err != nil { |
| 49 | + panic(fmt.Errorf("Cannot generate random nonce: %s", err)) |
| 50 | + } |
| 51 | + return nonce |
| 52 | +} |
| 53 | + |
| 54 | +// CreateNonce generates a nonce using the crypto/primitives package. |
| 55 | +func CreateNonce() ([]byte, error) { |
| 56 | + nonce, err := primitives.GetRandomNonce() |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("Cannot generate random nonce: %s", err) |
| 59 | + } |
| 60 | + return nonce, nil |
| 61 | +} |
| 62 | + |
| 63 | +// ExtractEnvelopeOrPanic retrieves the requested envelope from a given block and unmarshals it -- it panics if either of these operation fail. |
| 64 | +func ExtractEnvelopeOrPanic(block *cb.Block, index int) *cb.Envelope { |
| 65 | + envelopeCount := len(block.Data.Data) |
| 66 | + if index < 0 || index >= envelopeCount { |
| 67 | + panic("Envelope index out of bounds") |
| 68 | + } |
| 69 | + marshaledEnvelope := block.Data.Data[index] |
| 70 | + envelope := &cb.Envelope{} |
| 71 | + if err := proto.Unmarshal(marshaledEnvelope, envelope); err != nil { |
| 72 | + panic(fmt.Errorf("Block data does not carry an envelope at index %d: %s", index, err)) |
| 73 | + } |
| 74 | + return envelope |
| 75 | +} |
| 76 | + |
| 77 | +// ExtractEnvelope retrieves the requested envelope from a given block and unmarshals it. |
| 78 | +func ExtractEnvelope(block *cb.Block, index int) (*cb.Envelope, error) { |
| 79 | + envelopeCount := len(block.Data.Data) |
| 80 | + if index < 0 || index >= envelopeCount { |
| 81 | + return nil, fmt.Errorf("Envelope index out of bounds") |
| 82 | + } |
| 83 | + marshaledEnvelope := block.Data.Data[index] |
| 84 | + envelope := &cb.Envelope{} |
| 85 | + if err := proto.Unmarshal(marshaledEnvelope, envelope); err != nil { |
| 86 | + return nil, fmt.Errorf("Block data does not carry an envelope at index %d: %s", index, err) |
| 87 | + } |
| 88 | + return envelope, nil |
| 89 | +} |
| 90 | + |
| 91 | +// ExtractPayloadOrPanic retrieves the payload of a given envelope and unmarshals it -- it panics if either of these operations fail. |
| 92 | +func ExtractPayloadOrPanic(envelope *cb.Envelope) *cb.Payload { |
| 93 | + payload := &cb.Payload{} |
| 94 | + if err := proto.Unmarshal(envelope.Payload, payload); err != nil { |
| 95 | + panic(fmt.Errorf("Envelope does not carry a Payload: %s", err)) |
| 96 | + } |
| 97 | + return payload |
| 98 | +} |
| 99 | + |
| 100 | +// ExtractPayload retrieves the payload of a given envelope and unmarshals it. |
| 101 | +func ExtractPayload(envelope *cb.Envelope) (*cb.Payload, error) { |
| 102 | + payload := &cb.Payload{} |
| 103 | + if err := proto.Unmarshal(envelope.Payload, payload); err != nil { |
| 104 | + return nil, fmt.Errorf("Envelope does not carry a Payload: %s", err) |
| 105 | + } |
| 106 | + return payload, nil |
| 107 | +} |
| 108 | + |
| 109 | +// MakeChainHeader creates a ChainHeader. |
| 110 | +func MakeChainHeader(headerType cb.HeaderType, version int32, chainID []byte, epoch uint64) *cb.ChainHeader { |
| 111 | + return &cb.ChainHeader{ |
| 112 | + Type: int32(headerType), |
| 113 | + Version: version, |
| 114 | + Timestamp: ×tamp.Timestamp{ |
| 115 | + Seconds: time.Now().Unix(), |
| 116 | + Nanos: 0, |
| 117 | + }, |
| 118 | + ChainID: chainID, |
| 119 | + Epoch: epoch, |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// MakeSignatureHeader creates a SignatureHeader. |
| 124 | +func MakeSignatureHeader(serializedCreatorCertChain []byte, nonce []byte) *cb.SignatureHeader { |
| 125 | + return &cb.SignatureHeader{ |
| 126 | + Creator: serializedCreatorCertChain, |
| 127 | + Nonce: nonce, |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// MakePayloadHeader creates a Payload Header. |
| 132 | +func MakePayloadHeader(ch *cb.ChainHeader, sh *cb.SignatureHeader) *cb.Header { |
| 133 | + return &cb.Header{ |
| 134 | + ChainHeader: ch, |
| 135 | + SignatureHeader: sh, |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// MakeConfigurationItem makes a ConfigurationItem. |
| 140 | +func MakeConfigurationItem(ch *cb.ChainHeader, configItemType cb.ConfigurationItem_ConfigurationType, lastModified uint64, modPolicyID string, key string, value []byte) *cb.ConfigurationItem { |
| 141 | + return &cb.ConfigurationItem{ |
| 142 | + Header: ch, |
| 143 | + Type: configItemType, |
| 144 | + LastModified: lastModified, |
| 145 | + ModificationPolicy: modPolicyID, |
| 146 | + Key: key, |
| 147 | + Value: value, |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// MakeConfigurationEnvelope makes a ConfigurationEnvelope. |
| 152 | +func MakeConfigurationEnvelope(items ...*cb.SignedConfigurationItem) *cb.ConfigurationEnvelope { |
| 153 | + return &cb.ConfigurationEnvelope{Items: items} |
| 154 | +} |
| 155 | + |
| 156 | +// MakePolicyOrPanic creates a Policy proto message out of a SignaturePolicyEnvelope, and panics if this operation fails. |
| 157 | +// NOTE Expand this as more policy types as supported. |
| 158 | +func MakePolicyOrPanic(policyEnvelope interface{}) *cb.Policy { |
| 159 | + switch pe := policyEnvelope.(type) { |
| 160 | + case *cb.SignaturePolicyEnvelope: |
| 161 | + return &cb.Policy{ |
| 162 | + Type: &cb.Policy_SignaturePolicy{ |
| 163 | + SignaturePolicy: pe, |
| 164 | + }, |
| 165 | + } |
| 166 | + default: |
| 167 | + panic("Unknown policy envelope type given") |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// MakePolicy creates a Policy proto message out of a SignaturePolicyEnvelope. |
| 172 | +// NOTE Expand this as more policy types as supported. |
| 173 | +func MakePolicy(policyEnvelope interface{}) (*cb.Policy, error) { |
| 174 | + switch pe := policyEnvelope.(type) { |
| 175 | + case *cb.SignaturePolicyEnvelope: |
| 176 | + return &cb.Policy{ |
| 177 | + Type: &cb.Policy_SignaturePolicy{ |
| 178 | + SignaturePolicy: pe, |
| 179 | + }, |
| 180 | + }, nil |
| 181 | + default: |
| 182 | + return nil, fmt.Errorf("Unknown policy envelope type given") |
| 183 | + } |
| 184 | +} |
0 commit comments