@@ -871,16 +871,21 @@ var Chain = class {
871
871
*/
872
872
queryInfo ( ) {
873
873
logger . debug ( 'queryInfo - start' ) ;
874
- var request = {
875
- targets : [ this . getPrimaryPeer ( ) ] ,
876
- chaincodeId : 'qscc' ,
877
- chainId : '' ,
878
- txId : utils . buildTransactionID ( ) ,
879
- nonce : utils . getNonce ( ) ,
880
- fcn : 'GetChainInfo' ,
881
- args : [ this . _name ]
882
- } ;
883
- return this . sendTransactionProposal ( request )
874
+ var self = this ;
875
+ var nonce = utils . getNonce ( ) ;
876
+ return this . buildTransactionID_getUserContext ( nonce )
877
+ . then ( function ( txId ) {
878
+ var request = {
879
+ targets : [ self . getPrimaryPeer ( ) ] ,
880
+ chaincodeId : 'qscc' ,
881
+ chainId : '' ,
882
+ txId : txId ,
883
+ nonce : nonce ,
884
+ fcn : 'GetChainInfo' ,
885
+ args : [ self . _name ]
886
+ } ;
887
+ return self . sendTransactionProposal ( request ) ;
888
+ } )
884
889
. then (
885
890
function ( results ) {
886
891
var responses = results [ 0 ] ;
@@ -923,17 +928,22 @@ var Chain = class {
923
928
if ( ! blockHash ) {
924
929
return Promise . reject ( new Error ( 'Blockhash bytes are required' ) ) ;
925
930
}
926
- var request = {
927
- targets : [ this . getPrimaryPeer ( ) ] ,
928
- chaincodeId : 'qscc' ,
929
- chainId : '' ,
930
- txId : utils . buildTransactionID ( ) ,
931
- nonce : utils . getNonce ( ) ,
932
- fcn : 'GetBlockByHash' ,
933
- args : [ this . _name ] ,
934
- argbytes : blockHash
935
- } ;
936
- return this . sendTransactionProposal ( request )
931
+ var self = this ;
932
+ var nonce = utils . getNonce ( ) ;
933
+ return this . buildTransactionID_getUserContext ( nonce )
934
+ . then ( function ( txId ) {
935
+ var request = {
936
+ targets : [ self . getPrimaryPeer ( ) ] ,
937
+ chaincodeId : 'qscc' ,
938
+ chainId : '' ,
939
+ txId : txId ,
940
+ nonce : nonce ,
941
+ fcn : 'GetBlockByHash' ,
942
+ args : [ self . _name ] ,
943
+ argbytes : blockHash
944
+ } ;
945
+ return self . sendTransactionProposal ( request ) ;
946
+ } )
937
947
. then (
938
948
function ( results ) {
939
949
var responses = results [ 0 ] ;
@@ -980,16 +990,21 @@ var Chain = class {
980
990
} else {
981
991
return Promise . reject ( new Error ( 'Block number must be a postive integer' ) ) ;
982
992
}
983
- var request = {
984
- targets : [ this . getPrimaryPeer ( ) ] ,
985
- chaincodeId : 'qscc' ,
986
- chainId : '' ,
987
- txId : utils . buildTransactionID ( ) ,
988
- nonce : utils . getNonce ( ) ,
989
- fcn : 'GetBlockByNumber' ,
990
- args : [ this . _name , block_number ]
991
- } ;
992
- return this . sendTransactionProposal ( request )
993
+ var self = this ;
994
+ var nonce = utils . getNonce ( ) ;
995
+ return this . buildTransactionID_getUserContext ( nonce )
996
+ . then ( function ( txId ) {
997
+ var request = {
998
+ targets : [ self . getPrimaryPeer ( ) ] ,
999
+ chaincodeId : 'qscc' ,
1000
+ chainId : '' ,
1001
+ txId : txId ,
1002
+ nonce : nonce ,
1003
+ fcn : 'GetBlockByNumber' ,
1004
+ args : [ self . _name , block_number ]
1005
+ } ;
1006
+ return self . sendTransactionProposal ( request ) ;
1007
+ } )
993
1008
. then (
994
1009
function ( results ) {
995
1010
var responses = results [ 0 ] ;
@@ -1040,8 +1055,8 @@ var Chain = class {
1040
1055
targets : [ this . getPrimaryPeer ( ) ] ,
1041
1056
chaincodeId : 'qscc' ,
1042
1057
chainId : '' ,
1043
- txId : utils . buildTransactionID ( ) ,
1044
- nonce : utils . getNonce ( ) ,
1058
+ txId : transactionID ,
1059
+ nonce : utils . getNonce ( ) , //to do - get nonce from transaction id
1045
1060
fcn : 'GetTransactionByID' ,
1046
1061
args : [ this . _name , transaction_id ]
1047
1062
} ;
@@ -1190,10 +1205,11 @@ var Chain = class {
1190
1205
return self . _clientContext . getUserContext ( )
1191
1206
. then (
1192
1207
function ( userContext ) {
1208
+ var txId = self . buildTransactionID ( request . nonce , userContext ) ;
1193
1209
var channelHeader = buildChannelHeader (
1194
1210
_commonProto . HeaderType . ENDORSER_TRANSACTION ,
1195
1211
'' , //install does not target a channel
1196
- request . txId ,
1212
+ txId ,
1197
1213
null ,
1198
1214
'lccc'
1199
1215
) ;
@@ -1708,6 +1724,43 @@ var Chain = class {
1708
1724
return errorMsg ;
1709
1725
}
1710
1726
1727
+ /**
1728
+ * Utility method to build an unique transaction id
1729
+ * based on a nonce and this chain's user.
1730
+ * @param {int } nonce - a one time use number
1731
+ * @param {User } userContext - the user context
1732
+ * @returns {string } An unique string
1733
+ */
1734
+ buildTransactionID ( nonce , userContext ) {
1735
+ logger . debug ( 'buildTransactionID - start' ) ;
1736
+ var creator_bytes = userContext . getIdentity ( ) . serialize ( ) ; //same as signatureHeader.Creator
1737
+ var nonce_bytes = nonce ; //nonce is already in bytes
1738
+ var trans_bytes = Buffer . concat ( [ nonce_bytes , creator_bytes ] ) ;
1739
+ var trans_hash = this . cryptoPrimitives . hash ( trans_bytes ) ;
1740
+ var transaction_id = Buffer . from ( trans_hash ) . toString ( ) ;
1741
+ logger . debug ( 'buildTransactionID - transaction_id %s' , transaction_id ) ;
1742
+ return transaction_id ;
1743
+ }
1744
+
1745
+ /**
1746
+ * Utility method to build an unique transaction id
1747
+ * based on a nonce and this chain's user.
1748
+ * Gets the user context.
1749
+ * @param {int } nonce - a one time use number
1750
+ * @returns {Promise } A promise for the transaction id
1751
+ */
1752
+ buildTransactionID_getUserContext ( nonce ) {
1753
+ return this . _clientContext . getUserContext ( )
1754
+ . then ( ( userContext ) => {
1755
+ logger . debug ( 'buildTransactionID_getUserContext - got userContext' ) ;
1756
+ return this . buildTransactionID ( nonce , userContext ) ;
1757
+ } )
1758
+ . catch ( function ( error ) {
1759
+ logger . debug ( 'buildTransactionID_getUserContext - caught error ::' + error . stack ? error . stack : error ) ;
1760
+ return Promise . reject ( new Error ( error ) ) ;
1761
+ } ) ;
1762
+ }
1763
+
1711
1764
/**
1712
1765
* return a printable representation of this object
1713
1766
*/
0 commit comments