@@ -22,10 +22,11 @@ const {
22
22
const {
23
23
BlockchainTreeDataProvider,
24
24
} = require ( "./src/blockReader/blockchainExplorer.js" ) ;
25
+ const { log } = require ( "console" ) ;
25
26
let loadedConnectionProfile = null ;
26
27
27
28
function activate ( context ) {
28
- const fabricDebuggerPath = "C:\\Users\\chinm\\fabric-debugger" ;
29
+ const fabricDebuggerPath = context . extensionPath ;
29
30
30
31
let greenButton = vscode . commands . registerCommand ( "myview.button1" , ( ) => {
31
32
const platform = process . platform ;
@@ -56,7 +57,6 @@ function activate(context) {
56
57
command = `cd "${ fabricDebuggerPath } " && bash local-networkdown.sh` ;
57
58
}
58
59
59
- // Execute the command
60
60
exec ( command , ( err , stdout , stderr ) => {
61
61
if ( err ) {
62
62
vscode . window . showErrorMessage ( `Error: ${ stderr } ` ) ;
@@ -69,13 +69,124 @@ function activate(context) {
69
69
70
70
context . subscriptions . push ( greenButton ) ;
71
71
context . subscriptions . push ( redButton ) ;
72
+ const outputChannel = vscode . window . createOutputChannel ( "Chaincode Invocation" ) ;
73
+ let disposableExtractFunctions = vscode . commands . registerCommand ( 'extension.extractFunctions' , function ( ) {
74
+ const editor = vscode . window . activeTextEditor ;
75
+ if ( ! editor ) {
76
+ vscode . window . showInformationMessage ( 'No active editor. Open a chaincode file.' ) ;
77
+ return ;
78
+ }
79
+ const filePath = editor . document . fileName ;
80
+ const text = editor . document . getText ( ) ;
81
+ let functions = [ ] ;
82
+
83
+ if ( isGoChaincodeFile ( filePath ) ) {
84
+ functions = extractGoFunctions ( text ) ;
85
+ }
86
+
87
+ const filteredFunctions = filterIntAndStringFunctions ( functions ) ;
88
+ const uniqueFunctions = [ ...new Set ( filteredFunctions ) ] ;
89
+ storeFunctions ( uniqueFunctions , context ) ;
90
+
91
+ vscode . window . showInformationMessage ( `Extracted and stored ${ uniqueFunctions . length } unique functions with int or string parameters.` ) ;
92
+
93
+ showStoredFunctions ( context , outputChannel ) ;
94
+ } ) ;
95
+
96
+ context . subscriptions . push ( disposableExtractFunctions ) ;
97
+ function isGoChaincodeFile ( filePath ) {
98
+ return filePath . toLowerCase ( ) . endsWith ( '.go' ) ;
99
+ }
100
+
101
+ function extractGoFunctions ( code ) {
102
+ const functionDetails = [ ] ;
103
+ const regex = / f u n c \s * \( ( \w + ) \s + \* S m a r t C o n t r a c t \) \s * ( \w + ) \s * \( ( .* ?) \) \s * ( \w * ) / g;
104
+ let match ;
105
+
106
+ while ( ( match = regex . exec ( code ) ) !== null ) {
107
+ const functionName = match [ 2 ] ;
108
+ const params = match [ 3 ] ;
109
+ functionDetails . push ( { name : functionName , params } ) ;
110
+ }
111
+
112
+ return functionDetails ;
113
+ }
114
+
115
+ function filterIntAndStringFunctions ( functions ) {
116
+ return functions . filter ( func => / i n t | s t r i n g / . test ( func . params ) ) . map ( func => `${ func . name } (${ func . params } )` ) ;
117
+ }
118
+
119
+ function storeFunctions ( functions , context ) {
120
+ let storedFunctions = context . workspaceState . get ( 'storedFunctions' , [ ] ) ;
121
+ storedFunctions = [ ...new Set ( [ ...storedFunctions , ...functions ] ) ] ;
122
+ context . workspaceState . update ( 'storedFunctions' , storedFunctions ) ;
123
+ }
124
+
125
+ function showStoredFunctions ( context , outputChannel ) {
126
+ const storedFunctions = context . workspaceState . get ( 'storedFunctions' , [ ] ) ;
127
+
128
+ vscode . window . showQuickPick ( storedFunctions , {
129
+ placeHolder : 'Select a function to invoke' ,
130
+ canPickMany : false
131
+ } ) . then ( selectedFunction => {
132
+ if ( selectedFunction ) {
133
+ vscode . window . showInformationMessage ( `Selected: ${ selectedFunction } ` ) ;
134
+ promptForArgumentsSequentially ( selectedFunction , outputChannel ) ;
135
+ }
136
+ } ) ;
137
+ }
138
+
139
+ async function promptForArgumentsSequentially ( selectedFunction , outputChannel ) {
140
+ const functionPattern = / ( \w + ) \( ( .* ) \) / ;
141
+ const match = functionPattern . exec ( selectedFunction ) ;
142
+
143
+ if ( ! match ) {
144
+ vscode . window . showErrorMessage ( "Invalid function format." ) ;
145
+ return ;
146
+ }
147
+
148
+ const functionName = match [ 1 ] ;
149
+ const paramList = match [ 2 ] . split ( ',' ) . map ( param => param . trim ( ) ) ;
150
+
151
+ let argumentValues = [ ] ;
152
+
153
+ for ( let param of paramList ) {
154
+ if ( / i n t / . test ( param ) ) {
155
+ const input = await vscode . window . showInputBox ( { prompt : `Enter an integer value for ${ param } ` } ) ;
156
+ const intValue = parseInt ( input , 10 ) ;
157
+ if ( isNaN ( intValue ) ) {
158
+ vscode . window . showErrorMessage ( `Invalid integer value for ${ param } .` ) ;
159
+ return ;
160
+ }
161
+ argumentValues . push ( intValue ) ;
162
+ } else if ( / s t r i n g / . test ( param ) ) {
163
+ const input = await vscode . window . showInputBox ( { prompt : `Enter a string value for ${ param } ` } ) ;
164
+ if ( ! input ) {
165
+ vscode . window . showErrorMessage ( `Invalid string value for ${ param } .` ) ;
166
+ return ;
167
+ }
168
+ argumentValues . push ( `"${ input } "` ) ;
169
+ }
170
+ }
171
+
172
+ const finalArgs = argumentValues . join ( ', ' ) ;
173
+ outputChannel . show ( ) ;
174
+ outputChannel . appendLine ( `Function: ${ functionName } ` ) ;
175
+ outputChannel . appendLine ( `Arguments: ${ finalArgs } ` ) ;
176
+
177
+ vscode . window . showInformationMessage ( `Arguments captured. Press "Invoke" to execute the command.` , "Invoke" ) . then ( selection => {
178
+ if ( selection === "Invoke" ) {
179
+ invokeChaincode ( functionName , argumentValues ) ;
180
+ }
181
+ } ) ;
182
+ }
72
183
73
184
factory = new DelveDebugAdapterDescriptorFactory ( ) ;
74
185
context . subscriptions . push (
75
186
vscode . debug . registerDebugAdapterDescriptorFactory ( "delve" , factory )
76
187
) ;
77
188
console . log ( "Delve Debug Adapter Registered" ) ;
78
-
189
+
79
190
const hyperledgerProvider = new fabricsamples ( ) ;
80
191
const treeViewProviderFabric = new TreeViewProvider (
81
192
"fabric-network" ,
@@ -1046,6 +1157,66 @@ function activate(context) {
1046
1157
}
1047
1158
}
1048
1159
}
1160
+ async function invokeChaincode ( functionName , args ) {
1161
+ try {
1162
+ const walletPath = path . join ( os . homedir ( ) , "wallets" ) ;
1163
+ const wallet = await Wallets . newFileSystemWallet ( walletPath ) ;
1164
+
1165
+
1166
+ if ( ! loadedConnectionProfile ) {
1167
+ vscode . window . showErrorMessage ( "No connection profile loaded." ) ;
1168
+ return ;
1169
+ }
1170
+
1171
+ const identities = await wallet . list ( ) ;
1172
+ if ( ! identities . length ) {
1173
+ vscode . window . showErrorMessage ( "No identities found in the wallet." ) ;
1174
+ return ;
1175
+ }
1176
+
1177
+ const identityName = identities [ 0 ] ;
1178
+ const gateway = new Gateway ( ) ;
1179
+
1180
+ await gateway . connect ( loadedConnectionProfile , {
1181
+ wallet,
1182
+ identity : identityName ,
1183
+ discovery : { enabled : true , asLocalhost : false } ,
1184
+ } ) ;
1185
+
1186
+
1187
+ const channelName = Object . keys ( loadedConnectionProfile . channels || { } ) [ 0 ] ;
1188
+ if ( ! channelName ) {
1189
+ vscode . window . showErrorMessage ( "No channel found in the connection profile." ) ;
1190
+ return ;
1191
+ }
1192
+
1193
+ const chaincodes =
1194
+ loadedConnectionProfile . channels [ channelName ] ?. chaincodes || [ ] ;
1195
+ if ( ! chaincodes . length ) {
1196
+ vscode . window . showErrorMessage (
1197
+ `No chaincodes found for channel "${ channelName } ".`
1198
+ ) ;
1199
+ return ;
1200
+ }
1201
+
1202
+ const chaincodeName = chaincodes [ 0 ] ;
1203
+ const network = await gateway . getNetwork ( channelName ) ;
1204
+ const contract = network . getContract ( chaincodeName ) ;
1205
+
1206
+
1207
+ const result = await contract . submitTransaction ( functionName , ...args ) ;
1208
+ vscode . window . showInformationMessage (
1209
+ `Transaction invoked successfully. Result: ${ result . toString ( ) } `
1210
+ ) ;
1211
+ console . log ( "Transaction result:" , result . toString ( ) ) ;
1212
+
1213
+
1214
+ await gateway . disconnect ( ) ;
1215
+ } catch ( error ) {
1216
+ vscode . window . showErrorMessage ( `Error invoking chaincode: ${ error . message } ` ) ;
1217
+ console . error ( "Error invoking chaincode:" , error ) ;
1218
+ }
1219
+ }
1049
1220
}
1050
1221
1051
1222
function extractNetworkDetails ( profile ) {
@@ -1152,23 +1323,13 @@ function extractWalletDetails(walletData) {
1152
1323
console . warn ( "Missing required wallet data fields:" ) ;
1153
1324
}
1154
1325
}
1155
- return null ;
1156
- }
1157
1326
1158
- function packageChaincode ( chaincodePath ) {
1159
- class chaincodePackager extends BasePackager {
1160
- async package ( directoryPath ) {
1161
- const files = fs . readdirSync ( directoryPath ) . map ( ( file ) => path . join ( directoryPath , file ) ) ;
1162
- return this . generateTarGz ( files ) ;
1163
- }
1164
- }
1165
-
1166
- const packager = new chaincodePackager ( ) ;
1167
- return await packager . package ( chaincodePath ) ;
1327
+ return null ;
1168
1328
}
1169
1329
1170
1330
function deactivate ( ) { }
1171
1331
1332
+
1172
1333
module . exports = {
1173
1334
activate,
1174
1335
deactivate,
0 commit comments