99import com .google .protobuf .ByteString ;
1010import com .google .protobuf .InvalidProtocolBufferException ;
1111import com .google .protobuf .Timestamp ;
12+ import org .hyperledger .fabric .protos .common .Common ;
1213import org .hyperledger .fabric .protos .common .Common .ChannelHeader ;
1314import org .hyperledger .fabric .protos .common .Common .Header ;
15+ import org .hyperledger .fabric .protos .common .Common .HeaderType ;
16+ import org .hyperledger .fabric .protos .common .Common .SignatureHeader ;
1417import org .hyperledger .fabric .protos .ledger .queryresult .KvQueryResult ;
1518import org .hyperledger .fabric .protos .ledger .queryresult .KvQueryResult .KV ;
1619import org .hyperledger .fabric .protos .peer .ChaincodeEventPackage .ChaincodeEvent ;
@@ -40,6 +43,7 @@ class ChaincodeStubImpl implements ChaincodeStub {
4043 private final List <ByteString > args ;
4144 private final SignedProposal signedProposal ;
4245 private final Instant txTimestamp ;
46+ private final ByteString creator ;
4347 private ChaincodeEvent event ;
4448
4549 ChaincodeStubImpl (String txId , Handler handler , List <ByteString > args , SignedProposal signedProposal ) {
@@ -48,20 +52,35 @@ class ChaincodeStubImpl implements ChaincodeStub {
4852 this .args = Collections .unmodifiableList (args );
4953 this .signedProposal = signedProposal ;
5054 if (this .signedProposal == null ) {
55+ this .creator = null ;
5156 this .txTimestamp = null ;
5257 } else {
5358 try {
5459 final Proposal proposal = Proposal .parseFrom (signedProposal .getProposalBytes ());
5560 final Header header = Header .parseFrom (proposal .getHeader ());
5661 final ChannelHeader channelHeader = ChannelHeader .parseFrom (header .getChannelHeader ());
62+ validateProposalType (channelHeader );
63+ final SignatureHeader signatureHeader = SignatureHeader .parseFrom (header .getSignatureHeader ());
64+
5765 final Timestamp timestamp = channelHeader .getTimestamp ();
5866 this .txTimestamp = Instant .ofEpochSecond (timestamp .getSeconds (), timestamp .getNanos ());
67+ this .creator = signatureHeader .getCreator ();
5968 } catch (InvalidProtocolBufferException e ) {
6069 throw new RuntimeException (e );
6170 }
6271 }
6372 }
6473
74+ private void validateProposalType (ChannelHeader channelHeader ) {
75+ switch (Common .HeaderType .forNumber (channelHeader .getType ())) {
76+ case ENDORSER_TRANSACTION :
77+ case CONFIG :
78+ return ;
79+ default :
80+ throw new RuntimeException (String .format ("Unexpected transaction type: %s" , HeaderType .forNumber (channelHeader .getType ())));
81+ }
82+ }
83+
6584 @ Override
6685 public List <byte []> getArgs () {
6786 return args .stream ().map (x -> x .toByteArray ()).collect (Collectors .toList ());
@@ -212,4 +231,9 @@ public Instant getTxTimestamp() {
212231 return txTimestamp ;
213232 }
214233
234+ @ Override
235+ public byte [] getCreator () {
236+ if (creator == null ) return null ;
237+ return creator .toByteArray ();
238+ }
215239}
0 commit comments