/
txutils.go
68 lines (56 loc) · 2.5 KB
/
txutils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
Please review third_party pinning scripts and patches for more details.
*/
package utils
import (
"errors"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
)
// GetEnvelopeFromBlock gets an envelope from a block's Data field.
func GetEnvelopeFromBlock(data []byte) (*common.Envelope, error) {
//Block always begins with an envelope
var err error
env := &common.Envelope{}
if err = proto.Unmarshal(data, env); err != nil {
return nil, fmt.Errorf("Error getting envelope(%s)", err)
}
return env, nil
}
// GetBytesProposalPayloadForTx takes a ChaincodeProposalPayload and returns its serialized
// version according to the visibility field
func GetBytesProposalPayloadForTx(payload *peer.ChaincodeProposalPayload, visibility []byte) ([]byte, error) {
// check for nil argument
if payload == nil /* || visibility == nil */ {
return nil, fmt.Errorf("Nil arguments")
}
// strip the transient bytes off the payload - this needs to be done no matter the visibility mode
cppNoTransient := &peer.ChaincodeProposalPayload{Input: payload.Input, TransientMap: nil}
cppBytes, err := GetBytesChaincodeProposalPayload(cppNoTransient)
if err != nil {
return nil, errors.New("Failure while marshalling the ChaincodeProposalPayload!")
}
// currently the fabric only supports full visibility: this means that
// there are no restrictions on which parts of the proposal payload will
// be visible in the final transaction; this default approach requires
// no additional instructions in the PayloadVisibility field; however
// the fabric may be extended to encode more elaborate visibility
// mechanisms that shall be encoded in this field (and handled
// appropriately by the peer)
return cppBytes, nil
}