This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
/
sample.js
660 lines (512 loc) · 24.1 KB
/
sample.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const AdminConnection = require('composer-admin').AdminConnection;
const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
const BusinessNetworkDefinition = require('composer-common').BusinessNetworkDefinition;
const IdCard = require('composer-common').IdCard;
const MemoryCardStore = require('composer-common').MemoryCardStore;
const path = require('path');
const chai = require('chai');
chai.should();
chai.use(require('chai-as-promised'));
describe('Sample', () => {
// In-memory card store for testing so cards are not persisted to the file system
const cardStore = new MemoryCardStore();
// Embedded connection used for local testing
const connectionProfile = {
name: 'embedded',
'x-type': 'embedded'
};
// Name of the business network card containing the administrative identity for the business network
const adminCardName = 'admin';
// Admin connection to the blockchain, used to deploy the business network
let adminConnection;
// This is the business network connection the tests will use.
let businessNetworkConnection;
// This is the factory for creating instances of types.
let factory;
// These are the identities for Alice and Bob.
const aliceCardName = 'alice';
const bobCardName = 'bob';
// These are a list of receieved events.
let events;
let businessNetworkName;
before(() => {
// Embedded connection does not need real credentials
const credentials = {
certificate: 'FAKE CERTIFICATE',
privateKey: 'FAKE PRIVATE KEY'
};
// Identity used with the admin connection to deploy business networks
const deployerMetadata = {
version: 1,
userName: 'PeerAdmin',
roles: [ 'PeerAdmin', 'ChannelAdmin' ]
};
const deployerCard = new IdCard(deployerMetadata, connectionProfile);
deployerCard.setCredentials(credentials);
const deployerCardName = 'PeerAdmin';
adminConnection = new AdminConnection({ cardStore: cardStore });
return adminConnection.importCard(deployerCardName, deployerCard).then(() => {
return adminConnection.connect(deployerCardName);
});
});
/**
*
* @param {String} cardName The card name to use for this identity
* @param {Object} identity The identity details
* @returns {Promise} resolved when the card is imported
*/
function importCardForIdentity(cardName, identity) {
const metadata = {
userName: identity.userID,
version: 1,
enrollmentSecret: identity.userSecret,
businessNetwork: businessNetworkName
};
const card = new IdCard(metadata, connectionProfile);
return adminConnection.importCard(cardName, card);
}
// This is called before each test is executed.
beforeEach(() => {
let businessNetworkDefinition;
// Generate a business network definition from the project directory.
return BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname, '..'))
.then(definition => {
businessNetworkDefinition = definition;
businessNetworkName = definition.getName();
return adminConnection.install(businessNetworkName);
})
.then(() => {
const startOptions = {
networkAdmins: [
{
userName: 'admin',
enrollmentSecret: 'adminpw'
}
]
};
return adminConnection.start(businessNetworkDefinition, startOptions);
}).then(adminCards => {
return adminConnection.importCard(adminCardName, adminCards.get('admin'));
})
.then(() => {
// Create and establish a business network connection
businessNetworkConnection = new BusinessNetworkConnection({ cardStore: cardStore });
events = [];
businessNetworkConnection.on('event', event => {
events.push(event);
});
return businessNetworkConnection.connect(adminCardName);
})
.then(() => {
// Get the factory for the business network.
factory = businessNetworkConnection.getBusinessNetwork().getFactory();
return businessNetworkConnection.getParticipantRegistry('org.acme.sample.SampleParticipant');
})
.then(participantRegistry => {
// Create the participants.
const alice = factory.newResource('org.acme.sample', 'SampleParticipant', 'alice@email.com');
alice.firstName = 'Alice';
alice.lastName = 'A';
const bob = factory.newResource('org.acme.sample', 'SampleParticipant', 'bob@email.com');
bob.firstName = 'Bob';
bob.lastName = 'B';
participantRegistry.addAll([alice, bob]);
})
.then(() => {
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset');
})
.then(assetRegistry => {
// Create the assets.
const asset1 = factory.newResource('org.acme.sample', 'SampleAsset', '1');
asset1.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'alice@email.com');
asset1.value = '10';
const asset2 = factory.newResource('org.acme.sample', 'SampleAsset', '2');
asset2.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'bob@email.com');
asset2.value = '20';
assetRegistry.addAll([asset1, asset2]);
})
.then(() => {
// Issue the identities.
return businessNetworkConnection.issueIdentity('org.acme.sample.SampleParticipant#alice@email.com', 'alice1');
})
.then(identity => {
return importCardForIdentity(aliceCardName, identity);
}).then(() => {
return businessNetworkConnection.issueIdentity('org.acme.sample.SampleParticipant#bob@email.com', 'bob1');
})
.then((identity) => {
return importCardForIdentity(bobCardName, identity);
});
});
/**
* Reconnect using a different identity.
* @param {String} cardName The name of the card for the identity to use
* @return {Promise} A promise that will be resolved when complete.
*/
function useIdentity(cardName) {
return businessNetworkConnection.disconnect()
.then(() => {
businessNetworkConnection = new BusinessNetworkConnection({ cardStore: cardStore });
events = [];
businessNetworkConnection.on('event', (event) => {
events.push(event);
});
return businessNetworkConnection.connect(cardName);
})
.then(() => {
factory = businessNetworkConnection.getBusinessNetwork().getFactory();
});
}
it('Alice can read all of the assets', () => {
// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {
// Get the assets.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.getAll();
});
})
.then((assets) => {
// Validate the assets.
assets.should.have.lengthOf(2);
const asset1 = assets[0];
asset1.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#alice@email.com');
asset1.value.should.equal('10');
const asset2 = assets[1];
asset2.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#bob@email.com');
asset2.value.should.equal('20');
});
});
it('Bob can read all of the assets', () => {
// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {
// Get the assets.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.getAll();
});
})
.then((assets) => {
// Validate the assets.
assets.should.have.lengthOf(2);
const asset1 = assets[0];
asset1.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#alice@email.com');
asset1.value.should.equal('10');
const asset2 = assets[1];
asset2.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#bob@email.com');
asset2.value.should.equal('20');
});
});
it('Alice can add assets that she owns', () => {
// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {
// Create the asset.
const asset3 = factory.newResource('org.acme.sample', 'SampleAsset', '3');
asset3.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'alice@email.com');
asset3.value = '30';
// Add the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.add(asset3)
.then(() => {
return assetRegistry.get('3');
});
});
})
.then((asset3) => {
// Validate the asset.
asset3.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#alice@email.com');
asset3.value.should.equal('30');
});
});
it('Alice cannot add assets that Bob owns', () => {
// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {
// Create the asset.
const asset3 = factory.newResource('org.acme.sample', 'SampleAsset', '3');
asset3.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'bob@email.com');
asset3.value = '30';
// Add the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.add(asset3);
});
})
.should.be.rejectedWith(/does not have .* access to resource/);
});
it('Bob can add assets that he owns', () => {
// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {
// Create the asset.
const asset4 = factory.newResource('org.acme.sample', 'SampleAsset', '4');
asset4.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'bob@email.com');
asset4.value = '40';
// Add the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.add(asset4)
.then(() => {
return assetRegistry.get('4');
});
});
})
.then((asset4) => {
// Validate the asset.
asset4.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#bob@email.com');
asset4.value.should.equal('40');
});
});
it('Bob cannot add assets that Alice owns', () => {
// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {
// Create the asset.
const asset4 = factory.newResource('org.acme.sample', 'SampleAsset', '4');
asset4.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'alice@email.com');
asset4.value = '40';
// Add the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.add(asset4);
});
})
.should.be.rejectedWith(/does not have .* access to resource/);
});
it('Alice can update her assets', () => {
// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {
// Create the asset.
const asset1 = factory.newResource('org.acme.sample', 'SampleAsset', '1');
asset1.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'alice@email.com');
asset1.value = '50';
// Update the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.update(asset1)
.then(() => {
return assetRegistry.get('1');
});
});
})
.then((asset1) => {
// Validate the asset.
asset1.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#alice@email.com');
asset1.value.should.equal('50');
});
});
it('Alice cannot update Bob\'s assets', () => {
// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {
// Create the asset.
const asset2 = factory.newResource('org.acme.sample', 'SampleAsset', '2');
asset2.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'bob@email.com');
asset2.value = '50';
// Update the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.update(asset2);
});
})
.should.be.rejectedWith(/does not have .* access to resource/);
});
it('Bob can update his assets', () => {
// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {
// Create the asset.
const asset2 = factory.newResource('org.acme.sample', 'SampleAsset', '2');
asset2.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'bob@email.com');
asset2.value = '60';
// Update the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.update(asset2)
.then(() => {
return assetRegistry.get('2');
});
});
})
.then((asset2) => {
// Validate the asset.
asset2.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#bob@email.com');
asset2.value.should.equal('60');
});
});
it('Bob cannot update Alice\'s assets', () => {
// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {
// Create the asset.
const asset1 = factory.newResource('org.acme.sample', 'SampleAsset', '1');
asset1.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'alice@email.com');
asset1.value = '60';
// Update the asset, then get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.update(asset1);
});
})
.should.be.rejectedWith(/does not have .* access to resource/);
});
it('Alice can remove her assets', () => {
// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {
// Remove the asset, then test the asset exists.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.remove('1')
.then(() => {
return assetRegistry.exists('1');
});
});
})
.should.eventually.be.false;
});
it('Alice cannot remove Bob\'s assets', () => {
// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {
// Remove the asset, then test the asset exists.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.remove('2');
});
})
.should.be.rejectedWith(/does not have .* access to resource/);
});
it('Bob can remove his assets', () => {
// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {
// Remove the asset, then test the asset exists.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.remove('2')
.then(() => {
return assetRegistry.exists('2');
});
});
})
.should.eventually.be.false;
});
it('Bob cannot remove Alice\'s assets', () => {
// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {
// Remove the asset, then test the asset exists.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.remove('1');
});
})
.should.be.rejectedWith(/does not have .* access to resource/);
});
it('Alice can submit a transaction for her assets', () => {
// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {
// Submit the transaction.
const transaction = factory.newTransaction('org.acme.sample', 'SampleTransaction');
transaction.asset = factory.newRelationship('org.acme.sample', 'SampleAsset', '1');
transaction.newValue = '50';
return businessNetworkConnection.submitTransaction(transaction);
})
.then(() => {
// Get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.get('1');
});
})
.then((asset1) => {
// Validate the asset.
asset1.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#alice@email.com');
asset1.value.should.equal('50');
// Validate the events.
events.should.have.lengthOf(1);
const event = events[0];
event.eventId.should.be.a('string');
event.timestamp.should.be.an.instanceOf(Date);
event.asset.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleAsset#1');
event.oldValue.should.equal('10');
event.newValue.should.equal('50');
});
});
it('Alice cannot submit a transaction for Bob\'s assets', () => {
// Use the identity for Alice.
return useIdentity(aliceCardName)
.then(() => {
// Submit the transaction.
const transaction = factory.newTransaction('org.acme.sample', 'SampleTransaction');
transaction.asset = factory.newRelationship('org.acme.sample', 'SampleAsset', '2');
transaction.newValue = '50';
return businessNetworkConnection.submitTransaction(transaction);
})
.should.be.rejectedWith(/does not have .* access to resource/);
});
it('Bob can submit a transaction for his assets', () => {
// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {
// Submit the transaction.
const transaction = factory.newTransaction('org.acme.sample', 'SampleTransaction');
transaction.asset = factory.newRelationship('org.acme.sample', 'SampleAsset', '2');
transaction.newValue = '60';
return businessNetworkConnection.submitTransaction(transaction);
})
.then(() => {
// Get the asset.
return businessNetworkConnection.getAssetRegistry('org.acme.sample.SampleAsset')
.then((assetRegistry) => {
return assetRegistry.get('2');
});
})
.then((asset2) => {
// Validate the asset.
asset2.owner.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleParticipant#bob@email.com');
asset2.value.should.equal('60');
// Validate the events.
events.should.have.lengthOf(1);
const event = events[0];
event.eventId.should.be.a('string');
event.timestamp.should.be.an.instanceOf(Date);
event.asset.getFullyQualifiedIdentifier().should.equal('org.acme.sample.SampleAsset#2');
event.oldValue.should.equal('20');
event.newValue.should.equal('60');
});
});
it('Bob cannot submit a transaction for Alice\'s assets', () => {
// Use the identity for Bob.
return useIdentity(bobCardName)
.then(() => {
// Submit the transaction.
const transaction = factory.newTransaction('org.acme.sample', 'SampleTransaction');
transaction.asset = factory.newRelationship('org.acme.sample', 'SampleAsset', '1');
transaction.newValue = '60';
return businessNetworkConnection.submitTransaction(transaction);
})
.should.be.rejectedWith(/does not have .* access to resource/);
});
});