-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoke.js
94 lines (76 loc) · 2.9 KB
/
invoke.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { FileSystemWallet, Gateway } = require('fabric-network');
const fs = require('fs');
const path = require('path');
const ccpPath = path.resolve(__dirname, '..', '..', 'basic-network', 'connection.json');
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);
async function main() {
try {
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
const userExists = await wallet.exists('user1');
if (!userExists) {
console.log('An identity for the user "user1" does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
return;
}
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: false } });
// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel');
// Get the contract from the network.
const contract = network.getContract('fabcar');
// Submit the specified transaction.
// createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom')
// changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR10', 'Dave')
//await contract.submitTransaction('changeCarOwner', 'CAR12', 'Dave');
const criminality = [{
charge: 'Murder',
jailtime: 2,
date: '2013-04-13'
},
{
charge: 'Fraud',
jailtime: 4,
date: '2013-04-13'
},
]
const newRecord = {
charge: 'Hate Speech',
jailtime: 7,
date: '2013-05-13'
}
const personal_details = {
birth: '2000-04-01',
fingerprint: 's435457hfgdgh45taf',
address: 'Kentucky'
}
await contract.submitTransaction(
'createCar',
`PERSON${personNumber}`,
name,
JSON.stringify(criminality),
JSON.stringify(personal_details)
);
console.log('Transaction has been submitted');
await contract.submitTransaction(
'changeCarOwner',
'PERSON3',
JSON.stringify(newRecord),
);
console.log('Transaction has been submitted');
// Disconnect from the gateway.
await gateway.disconnect();
} catch (error) {
console.error(`Failed to submit transaction: ${error}`);
process.exit(1);
}
}