-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbn-connection.1.js
More file actions
81 lines (68 loc) · 2.48 KB
/
bn-connection.1.js
File metadata and controls
81 lines (68 loc) · 2.48 KB
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
'use strict';
/**
* Part of a course on Hyperledger Fabric:
* http://ACloudFan.com
*
* DO NOT USE THIS FILE.
* Dec 21, 2018
*
* Demonstrates the use of the business network connection
*
* Pre-Reqs
* 1. The local fabric is up
* 2. Deployed the application to fabric
* 3. Card imported to the card storage on file system
*/
// Need the card store instance
const FileSystemCardStore = require('composer-common').FileSystemCardStore;
const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
// Used as the card for all calls
var cardName = "admin@airlinev7";
const registryId = "org.acme.airline.aircraft.Aircraft";
// 1. Create instance of file system card store
const cardStore = new FileSystemCardStore();
// 2. Connection object for the fabric
const cardStoreObj = { cardStore: cardStore };
const bnConnection = new BusinessNetworkConnection(cardStoreObj);
// 3. Initiate a connection
return bnConnection.connect(cardName).then(function(){
console.log("Connected Successfully!!!");
// 4. Display the name and version of the network app
getBusinessNetwork();
// 5. Ping the network
ping();
// 6. Get all assets in a registry
getAssets();
}).catch((error)=>{
console.log(error);
});
// Extracts information about the network
function getBusinessNetwork(){
// Returns an object of type BusinessNetworkDefinition
let businessNetworkDefinition = bnConnection.getBusinessNetwork();
// Dump package.json to the console
console.log("Connected to: ",businessNetworkDefinition.metadata.packageJson.name,
" version ",businessNetworkDefinition.metadata.packageJson.version);
}
// Ping the network app
function ping(){
bnConnection.ping().then(function(response){
console.log("Ping Response:");
console.log(response);
});
}
// Get all the Asset from a registry Registry
// 1. Get an instance of the AssetRegistry
// 2. Get all he objects in the asset registry
function getAssets(){
return bnConnection.getAssetRegistry(registryId).then(function(registry){
console.log("Received the Registry Instance: ", registryId)
// This would get a collection of assets
return registry.getAll().then(function(resourceCollection){
console.log("Received count=",resourceCollection.length)
});
}).catch(function(error){
console.log(error);
});
}
// function update