This repository was archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 720
IdCard API changes (#1415) #1663
Merged
jt-nti
merged 2 commits into
hyperledger-archives:master
from
bestbeforetoday:issue1415
Jul 26, 2017
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,37 +15,44 @@ | |
| 'use strict'; | ||
|
|
||
| const JSZip = require('jszip'); | ||
| const path = require('path'); | ||
|
|
||
| const Logger = require('./log/logger'); | ||
| const LOG = Logger.getLog('IdCard'); | ||
|
|
||
| /** | ||
| * An ID card. | ||
| * An ID card. Encapsulates credentials and other information required to connect to a specific business network | ||
| * as a specific user. | ||
| * <p> | ||
| * Instances of this class should be created using {@link IdCard.fromArchive}. | ||
| * @class | ||
| * @memberof module:composer-common | ||
| */ | ||
| class IdCard { | ||
|
|
||
| /** | ||
| * Create the BusinessNetworkDefinition. | ||
| * Create the IdCard. | ||
| * <p> | ||
| * <strong>Note: Only to be called by framework code. Applications should | ||
| * retrieve instances from {@link IdCard.fromArchive}</strong> | ||
| * @param {Object} metadata - metadata associated with the card. | ||
| * @param {Object} connection - connection properties associated with the card. | ||
| * @param {Map} credentials - map of credential filename String keys to credential data Buffer objects. | ||
| * @param {Map} tlscerts - map of TLS certificate filename String keys to TLS certificate data Buffer objects. | ||
| * @param {Object} connectionProfile - connection profile associated with the card. | ||
| * @param {Object} credentials - credentials used to connect to business network. | ||
| * @private | ||
| */ | ||
| constructor(metadata, connection, credentials, tlscerts) { | ||
| constructor(metadata, connectionProfile, credentials) { | ||
| const method = 'constructor'; | ||
| LOG.entry(method); | ||
|
|
||
| if (!(metadata && metadata.name)) { | ||
| throw Error('Required metadata field not found: name'); | ||
| } | ||
| if (!(connectionProfile && connectionProfile.name)) { | ||
| throw Error('Required connection field not found: name'); | ||
| } | ||
|
|
||
| this.metadata = metadata; | ||
| this.connection = connection; | ||
| this.connectionProfile = connectionProfile; | ||
| this.credentials = credentials; | ||
| this.tlscerts = tlscerts; | ||
|
|
||
| LOG.exit(method); | ||
| } | ||
|
|
@@ -62,52 +69,55 @@ class IdCard { | |
|
|
||
| /** | ||
| * Free text description of the card. | ||
| * @return {String} description, or {@link undefined} if none exists. | ||
| * @return {String} card description. | ||
| */ | ||
| getDescription() { | ||
| return this.metadata.description; | ||
| } | ||
|
|
||
| /** | ||
| * Business network to which the ID card applies. Generally this will be present but may be omitted for system | ||
| * cards. | ||
| * @return {String} description, or {@link undefined} if none exists. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like empty string. |
||
| */ | ||
| getBusinessNetwork() { | ||
| return this.metadata.businessNetwork; | ||
| return this.metadata.description || ''; | ||
| } | ||
|
|
||
| /** | ||
| * Image associated with the card. | ||
| * @return {Object} an object of the form <i>{ name: imageFileName, data: bufferOfImageData }</i>, | ||
| * or {@link undefined} if none exists. | ||
| * Name of the business network to which the ID card applies. Generally this will be present but may be | ||
| * omitted for system cards. | ||
| * @return {String} business network name. | ||
| */ | ||
| getImage() { | ||
| return this.image; | ||
| getBusinessNetworkName() { | ||
| return this.metadata.businessNetwork || ''; | ||
| } | ||
|
|
||
| /** | ||
| * Connection profile for this card. | ||
| * <p> | ||
| * This is a mandatory field. | ||
| * @return {Object} connection profile. | ||
| */ | ||
| getConnection() { | ||
| return this.connection; | ||
| getConnectionProfile() { | ||
| return this.connectionProfile; | ||
| } | ||
|
|
||
| /** | ||
| * Credentials associated with this card. | ||
| * @return {Map} Map of filename {@link String} keys to {@link Buffer} data. | ||
| * Credentials associated with this card, and which are used to connect to the associated business network. | ||
| * @return {Object} credentials in the form <em>{ public: publicKey, private: privateKey }</em>, if they exist. | ||
| */ | ||
| getCredentials() { | ||
| return this.credentials; | ||
| } | ||
|
|
||
| /** | ||
| * TLS certificates used to connect to the business networks. | ||
| * @return {Map} Map of filename {@link String} keys to {@link Buffer} data. | ||
| * Enrollment credentials. If there are no credentials associated with this card, these credentials are used to | ||
| * enroll with a business network and obtain certificates. | ||
| * @return {Object} enrollment credentials in the form <em>{ id: enrollmentId, secret: enrollmentSecret }</em>, if | ||
| * they exist. | ||
| */ | ||
| getTlsCertificates() { | ||
| return this.tlscerts; | ||
| getEnrollmentCredentials() { | ||
| let result = null; | ||
| const id = this.metadata.enrollmentId; | ||
| const secret = this.metadata.enrollmentSecret; | ||
| if (id || secret) { | ||
| result = Object.create(null); | ||
| result.id = id; | ||
| result.secret = secret; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -123,10 +133,8 @@ class IdCard { | |
| let promise = Promise.resolve(); | ||
|
|
||
| let metadata; | ||
| let image; | ||
| let connection; | ||
| const credentials = new Map(); | ||
| const tlscerts = new Map(); | ||
| let credentials = Object.create(null); | ||
|
|
||
| LOG.debug(method, 'Loading connection.json'); | ||
| const connectionFile = zip.file('connection.json'); | ||
|
|
@@ -150,52 +158,28 @@ class IdCard { | |
| return metadataFile.async('string'); | ||
| }).then((metadataContent) => { | ||
| metadata = JSON.parse(metadataContent); | ||
| if (!metadata.name) { | ||
| throw Error('Required meta-data field not found: name'); | ||
| } | ||
|
|
||
| if (metadata.image) { | ||
| LOG.debug(method, 'Loading image ' + metadata.image); | ||
|
|
||
| const imagePromise = zip.file(metadata.image); | ||
| if (!imagePromise) { | ||
| throw Error('Image file not found: ' + metadata.image); | ||
| } | ||
|
|
||
| return imagePromise.async('nodebuffer').then((imageContent) => { | ||
| const shortFilename = path.basename(metadata.image); | ||
| image = { | ||
| name: shortFilename, | ||
| data: imageContent | ||
| }; | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| const loadDirectoryToMap = function(directoryName, map) { | ||
| const loadDirectoryToObject = function(directoryName, obj) { | ||
| // Incude '/' following directory name | ||
| const fileIndex = directoryName.length + 1; | ||
| // Find all files that are direct children of specified directory | ||
| const files = zip.file(new RegExp(`^${directoryName}/[^/]+$`)); | ||
| files && files.forEach((file) => { | ||
| promise = promise.then(() => { | ||
| return file.async('nodebuffer'); | ||
| return file.async('string'); | ||
| }).then((content) => { | ||
| const filename = file.name.slice(fileIndex); | ||
| map.set(filename, content); | ||
| obj[filename] = content; | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| LOG.debug(method, 'Loading credentials'); | ||
| loadDirectoryToMap('credentials', credentials); | ||
|
|
||
| LOG.debug(method, 'Loading tlscerts'); | ||
| loadDirectoryToMap('tlscerts', tlscerts); | ||
| loadDirectoryToObject('credentials', credentials); | ||
|
|
||
| return promise.then(() => { | ||
| const idCard = new IdCard(metadata, connection, credentials, tlscerts); | ||
| idCard.image = image; | ||
| const idCard = new IdCard(metadata, connection, credentials); | ||
| LOG.exit(method, idCard.toString()); | ||
| return idCard; | ||
| }); | ||
|
|
||
4 changes: 0 additions & 4 deletions
4
packages/composer-common/test/data/id-cards/invalid-metadata-image/metadata.json
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
packages/composer-common/test/data/id-cards/minimal/connection.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| { | ||
| "name": "hlfv1", | ||
| "type": "hlfv1", | ||
| "orderers": [ | ||
| { | ||
|
|
||
2 changes: 1 addition & 1 deletion
2
packages/composer-common/test/data/id-cards/minimal/metadata.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| { | ||
| "name" : "com.example.cards.Minimal" | ||
| "name": "Minimal" | ||
| } |
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
packages/composer-common/test/data/id-cards/missing-connection-name/metadata.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "name": "Minimal" | ||
| } |
2 changes: 1 addition & 1 deletion
2
packages/composer-common/test/data/id-cards/missing-connection/metadata.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| { | ||
| "name" : "com.example.cards.Minimal" | ||
| "name": "Minimal" | ||
| } |
1 change: 1 addition & 0 deletions
1
packages/composer-common/test/data/id-cards/missing-metadata-name/connection.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| { | ||
| "name": "hlfv1", | ||
| "type": "hlfv1", | ||
| "orderers": [ | ||
| { | ||
|
|
||
1 change: 1 addition & 0 deletions
1
packages/composer-common/test/data/id-cards/missing-metadata/connection.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| { | ||
| "name": "hlfv1", | ||
| "type": "hlfv1", | ||
| "orderers": [ | ||
| { | ||
|
|
||
39 changes: 39 additions & 0 deletions
39
packages/composer-common/test/data/id-cards/valid-with-enrollment/connection.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| { | ||
| "name": "hlfv1", | ||
| "type": "hlfv1", | ||
| "orderers": [ | ||
| { | ||
| "url": "grpcs://", | ||
| "cert": "orderer1.crt" | ||
| }, | ||
| { | ||
| "url": "grpcs://", | ||
| "cert": "orderer2.crt" | ||
| } | ||
| ], | ||
| "ca": { | ||
| "url": "https://", | ||
| "name": "", | ||
| "trustedRoots": "", | ||
| "verify": true | ||
| }, | ||
| "peers": [ | ||
| { | ||
| "requestURL": "grpcs://", | ||
| "eventURL": "grpcs://", | ||
| "cert": "peer1.crt" | ||
| }, | ||
| { | ||
| "requestURL": "grpcs://", | ||
| "eventURL": "grpcs://", | ||
| "cert": "peer2.crt" | ||
| } | ||
| ], | ||
| "keyValStore": "/YOUR_HOME_DIR/.composer-credentials", | ||
| "channel": "composerchannel", | ||
| "mspID": "Org1MSP", | ||
| "timeout": 300, | ||
| "globalcert": "", | ||
| "maxSendSize": 10, | ||
| "maxRecvSize": 15 | ||
| } |
8 changes: 8 additions & 0 deletions
8
packages/composer-common/test/data/id-cards/valid-with-enrollment/metadata.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "name": "Conga", | ||
| "description": "A valid ID card", | ||
| "businessNetwork": "org-acme-biznet", | ||
| "image": "images/conga.png", | ||
| "enrollmentId": "conga", | ||
| "enrollmentSecret": "super-secret-passphrase" | ||
| } |
1 change: 1 addition & 0 deletions
1
packages/composer-common/test/data/id-cards/valid/connection.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| { | ||
| "name": "hlfv1", | ||
| "type": "hlfv1", | ||
| "orderers": [ | ||
| { | ||
|
|
||
5 changes: 0 additions & 5 deletions
5
...s/valid/credentials/299acf40adb20268d13881833a0bed90e43e2276f0ed204e26233f906803b6ef-priv
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
...ds/valid/credentials/299acf40adb20268d13881833a0bed90e43e2276f0ed204e26233f906803b6ef-pub
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
packages/composer-common/test/data/id-cards/valid/credentials/PeerAdmin
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
packages/composer-common/test/data/id-cards/valid/credentials/admin
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
Binary file not shown.
8 changes: 4 additions & 4 deletions
8
packages/composer-common/test/data/id-cards/valid/metadata.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "name" : "com.example.cards.Dan", | ||
| "description" : "Dan's Card for Production Network", | ||
| "businessNetwork" : "org-acme-biznet", | ||
| "image" : "images/conga.png" | ||
| "name": "Conga", | ||
| "description": "A valid ID card", | ||
| "businessNetwork": "org-acme-biznet", | ||
| "image": "images/conga.png" | ||
| } |
15 changes: 0 additions & 15 deletions
15
packages/composer-common/test/data/id-cards/valid/tlscerts/ca.crt
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
packages/composer-common/test/data/id-cards/valid/tlscerts/server.crt
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
packages/composer-common/test/data/id-cards/valid/tlscerts/server.key
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you take private off please? Need to create these in the playground (currently do the same with business network definitions)