|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const { FileSystemWallet, Gateway } = require('fabric-network'); |
| 8 | +const fs = require('fs'); |
| 9 | +const path = require('path'); |
| 10 | + |
| 11 | +const ccpPath = path.resolve(__dirname, '..', '..', 'basic-network', 'connection.json'); |
| 12 | +const ccpJSON = fs.readFileSync(ccpPath, 'utf8'); |
| 13 | +const ccp = JSON.parse(ccpJSON); |
| 14 | + |
| 15 | +async function main() { |
| 16 | + try { |
| 17 | + |
| 18 | + // Create a new file system based wallet for managing identities. |
| 19 | + const walletPath = path.join(process.cwd(), 'wallet'); |
| 20 | + const wallet = new FileSystemWallet(walletPath); |
| 21 | + console.log(`Wallet path: ${walletPath}`); |
| 22 | + |
| 23 | + // Check to see if we've already enrolled the user. |
| 24 | + const userExists = await wallet.exists('user1'); |
| 25 | + if (!userExists) { |
| 26 | + console.log('An identity for the user "user1" does not exist in the wallet'); |
| 27 | + console.log('Run the registerUser.js application before retrying'); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + // Create a new gateway for connecting to our peer node. |
| 32 | + const gateway = new Gateway(); |
| 33 | + await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { useLocalhost: true } }); |
| 34 | + |
| 35 | + // Get the network (channel) our contract is deployed to. |
| 36 | + const network = await gateway.getNetwork('mychannel'); |
| 37 | + |
| 38 | + // Get the contract from the network. |
| 39 | + const contract = network.getContract('fabcar'); |
| 40 | + |
| 41 | + // Submit the specified transaction. |
| 42 | + // createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom') |
| 43 | + // changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR10', 'Dave') |
| 44 | + await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom'); |
| 45 | + console.log('Transaction has been submitted'); |
| 46 | + |
| 47 | + // Disconnect from the gateway. |
| 48 | + await gateway.disconnect(); |
| 49 | + |
| 50 | + } catch (error) { |
| 51 | + console.error(`Failed to submit transaction: ${error}`); |
| 52 | + process.exit(1); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +main(); |
0 commit comments