This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgetSimHistory.js
76 lines (62 loc) · 2.77 KB
/
getSimHistory.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
'use strict';
/*
* Copyright IBM Corp All Rights Reserved
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Invoke the telco-roaming-contract to get the history of a SubscriberSim object.
* node getSimHistory.js <simPublicKey>
* eg. node getSimHistory.js sim1
*
*
* 1. call getHistoryForSim which returns the log of all changes made to the SubscriberSim object with the given publicKey.
*
* Will throw an error if sim does not exist.
* Otherwise, should successfully complete without errors and return the history for the SubscriberSim.
*/
const fs = require('fs');
const path = require('path');
// Bring Fabric SDK network class
const { FileSystemWallet, Gateway } = require('fabric-network');
// A wallet stores a collection of identities for use
let walletDir = path.join(path.dirname(require.main.filename),'fabric/_idwallet');
const wallet = new FileSystemWallet(walletDir);
const configDirectory = path.join(process.cwd(), 'fabric');
const configPath = path.join(configDirectory, 'config.json');
const configJSON = fs.readFileSync(configPath, 'utf8');
const config = JSON.parse(configJSON);
let channelName = config.channel_name;
let smartContractName = config.smart_contract_name;
let connection_file = config.connection_file;
let appAdmin = config.appAdmin;
let gatewayDiscoveryEnabled = 'enabled' in config.gatewayDiscovery?config.gatewayDiscovery.enabled:true;
let gatewayDiscoveryAsLocalhost = 'asLocalhost' in config.gatewayDiscovery?config.gatewayDiscovery.asLocalhost:true;
const ccpPath = path.join(configDirectory, connection_file);
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);
const gateway = new Gateway();
process.on('unhandledRejection', error => {
// Will print "unhandledRejection err is not defined"
console.log('An unhandled rejection was found - ', error.message);
return process.exit(1);
});
async function main() {
if(process.argv.length !== 3){
throw new Error('Process argv length is ' + process.argv.length + '. It should be 3');
}
let simPublicKey = process.argv[2];
// A gateway defines the peers used to access Fabric networks
await gateway.connect(ccp, { wallet, identity: appAdmin , discovery: {enabled: gatewayDiscoveryEnabled, asLocalhost:gatewayDiscoveryAsLocalhost }});
// eslint-disable-next-line no-unused-vars
const network = await gateway.getNetwork(channelName);
const contract = network.getContract(smartContractName);
const simHistory = await contract.evaluateTransaction('getHistoryForSim',simPublicKey);
const simHistoryArray = JSON.parse(simHistory);
for(let i = 0; i < simHistoryArray.length; i++){
console.log('Key: ' + simHistoryArray[i].Key + '\nRecord:');
console.log(simHistoryArray[i].Record);
console.log('\n');
}
}
main();