From e8fff73af95ff31a6e6511129d0dd296a34cafeb Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Thu, 22 Nov 2018 06:35:31 -0200 Subject: [PATCH 1/3] Changing injection of SecureRandom --- dist/browser/creds/VerifiableCredential.js | 2 +- dist/browser/services/index.js | 2 +- dist/browser/uca/UserCollectableAttribute.js | 2 +- dist/cjs/creds/VerifiableCredential.js | 3 +-- dist/cjs/services/index.js | 2 +- dist/cjs/uca/UserCollectableAttribute.js | 6 +++--- dist/es/creds/VerifiableCredential.js | 3 +-- dist/es/services/index.js | 2 +- dist/es/uca/UserCollectableAttribute.js | 6 +++--- package.json | 2 +- src/creds/VerifiableCredential.js | 7 ++----- src/uca/UserCollectableAttribute.js | 7 ++----- 12 files changed, 18 insertions(+), 26 deletions(-) diff --git a/dist/browser/creds/VerifiableCredential.js b/dist/browser/creds/VerifiableCredential.js index dadf34a6..98c79b68 100644 --- a/dist/browser/creds/VerifiableCredential.js +++ b/dist/browser/creds/VerifiableCredential.js @@ -1 +1 @@ -"use strict";var _toConsumableArray2=require("babel-runtime/helpers/toConsumableArray"),_toConsumableArray3=_interopRequireDefault(_toConsumableArray2),_slicedToArray2=require("babel-runtime/helpers/slicedToArray"),_slicedToArray3=_interopRequireDefault(_slicedToArray2),_regenerator=require("babel-runtime/regenerator"),_regenerator2=_interopRequireDefault(_regenerator),_asyncToGenerator2=require("babel-runtime/helpers/asyncToGenerator"),_asyncToGenerator3=_interopRequireDefault(_asyncToGenerator2),_classCallCheck2=require("babel-runtime/helpers/classCallCheck"),_classCallCheck3=_interopRequireDefault(_classCallCheck2),_createClass2=require("babel-runtime/helpers/createClass"),_createClass3=_interopRequireDefault(_createClass2),_typeof2=require("babel-runtime/helpers/typeof"),_typeof3=_interopRequireDefault(_typeof2),_keys=require("babel-runtime/core-js/object/keys"),_keys2=_interopRequireDefault(_keys);function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}var _=require("lodash"),sift=require("sift"),MerkleTools=require("merkle-tools"),sjcl=require("sjcl"),timestamp=require("unix-timestamp"),flatten=require("flat"),uuidv4=require("uuid/v4"),definitions=require("./definitions"),UCA=require("../uca/UserCollectableAttribute"),_require=require("../services"),services=_require.services,secureRandom=services.container.SecureRandom;function getAnchorService(){return services.container.AnchorService}function sha256(a){return sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(a))}function getClaimPath(a,b){var c=_.split(a,":"),d=_.camelCase(c[1]),e=d+"."+c[2],f=_.find(b,function(a){return _.endsWith(a,e)});return f||e}function validIdentifiers(){var a=_.map(definitions,function(a){return a.identifier});return a}function getClaimsWithFlatKeys(a){var b=flatten(a,{maxDepth:3}),c=flatten(a,{maxDepth:2}),d=_.merge({},b,c),e=_(d).toPairs().sortBy(0).fromPairs().value();return e}function paths(a){for(var b=[],c=[{obj:a,path:[]}],d=function(){var a=c.pop();(0,_keys2.default)(a.obj).forEach(function(d){if("object"===(0,_typeof3.default)(a.obj[d])){var e=a.path.concat(d);b.push(e),c.unshift({obj:a.obj[d],path:e})}})};0t.getTime()&&k.push(a)}}return _.isEmpty(j)&&_.isEmpty(l)&&_.isEmpty(m)&&_.isEmpty(n)&&_.isEmpty(k)&&(e=!0),e},this.verify=function(a){var b=a||VERIFY_LEVELS.PROOFS,c=VERIFY_LEVELS.INVALID;return b>=VERIFY_LEVELS.PROOFS&&f.verifyProofs()&&(c=VERIFY_LEVELS.PROOFS),c},this.verifySignature=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",getAnchorService().verifySignature(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.verifyAttestation=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",getAnchorService().verifyAttestation(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.revokeAttestation=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",getAnchorService().revokeAttestation(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.isRevoked=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",getAnchorService().isRevoked(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.isMatch=function(a){var b=transformConstraint(a),c=!0;return _.forEach(b,function(a){return c=-1t.getTime()&&k.push(a)}}return _.isEmpty(j)&&_.isEmpty(l)&&_.isEmpty(m)&&_.isEmpty(n)&&_.isEmpty(k)&&(e=!0),e},this.verify=function(a){var b=a||VERIFY_LEVELS.PROOFS,c=VERIFY_LEVELS.INVALID;return b>=VERIFY_LEVELS.PROOFS&&f.verifyProofs()&&(c=VERIFY_LEVELS.PROOFS),c},this.verifySignature=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",getAnchorService().verifySignature(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.verifyAttestation=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",getAnchorService().verifyAttestation(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.revokeAttestation=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",getAnchorService().revokeAttestation(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.isRevoked=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",getAnchorService().isRevoked(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.isMatch=function(a){var b=transformConstraint(a),c=!0;return _.forEach(b,function(a){return c=-1=c.minimumLength)&&(!c.maximumLength||a.length<=c.minimumLength):"Number"===b?((!_.isNil(c.minimum)&&c.exclusiveMinimum?a>c.minimum:a>=c.minimum)||_.isNil(c.minimum))&&((!_.isNil(c.maximum)&&c.exclusiveMaximum?a=c.minimumLength)&&(!c.maximumLength||a.length<=c.minimumLength):"Number"===b?((!_.isNil(c.minimum)&&c.exclusiveMinimum?a>c.minimum:a>=c.minimum)||_.isNil(c.minimum))&&((!_.isNil(c.maximum)&&c.exclusiveMaximum?a config); logger.info('Registering request-promise-native as Http service implementation.'); services.service('Http', HttpServiceConstructor); -services.service('SecureRandom', SecureRandom); +services.factory('SecureRandom', () => new SecureRandom()); services.service('AnchorService', CurrentCivicAnchor, 'Config', 'Http'); diff --git a/dist/cjs/uca/UserCollectableAttribute.js b/dist/cjs/uca/UserCollectableAttribute.js index 6e84c244..53123435 100644 --- a/dist/cjs/uca/UserCollectableAttribute.js +++ b/dist/cjs/uca/UserCollectableAttribute.js @@ -6,8 +6,6 @@ const sjcl = require('sjcl'); const definitions = require('./definitions'); const { services } = require('../services'); -const secureRandom = services.container.SecureRandom; - const validIdentifiers = _.map(definitions, d => d.identifier); /** @@ -75,6 +73,7 @@ const findDefinitionByAttestableValue = (attestableValuePropertyName, rootDefini // eslint-disable-next-line no-restricted-syntax for (const property of rootDefinition.type.properties) { const resolvedDefinition = _.find(definitions, { identifier: property.type }); + resolvedDefinition.type = resolveType(resolvedDefinition); if (!resolvedDefinition.type.properties && property.name === attestableValuePropertyName) { return property.type; } @@ -166,6 +165,7 @@ const parseAttestableValue = value => { function UCABaseConstructor(identifier, value, version) { this.timestamp = null; this.id = null; + this.secureRandom = services.container.SecureRandom; if (!_.includes(validIdentifiers, identifier)) { throw new Error(`${identifier} is not defined`); @@ -217,7 +217,7 @@ function UCABaseConstructor(identifier, value, version) { } this.value = value; - this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(secureRandom.wordWith(64))); + this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(this.secureRandom.wordWith(64))); } else if (_.isEmpty(definition.type.properties)) { throw new Error(`${JSON.stringify(value)} is not valid for ${identifier}`); } else { diff --git a/dist/es/creds/VerifiableCredential.js b/dist/es/creds/VerifiableCredential.js index 1e6aa389..9b5bd819 100644 --- a/dist/es/creds/VerifiableCredential.js +++ b/dist/es/creds/VerifiableCredential.js @@ -9,8 +9,6 @@ const definitions = require('./definitions'); const UCA = require('../uca/UserCollectableAttribute'); const { services } = require('../services'); -const secureRandom = services.container.SecureRandom; - function getAnchorService() { return services.container.AnchorService; } @@ -160,6 +158,7 @@ class CvcMerkleProof { const currentLength = nodes.length; const targetLength = currentLength < CvcMerkleProof.PADDING_INCREMENTS ? CvcMerkleProof.PADDING_INCREMENTS : _.ceil(currentLength / CvcMerkleProof.PADDING_INCREMENTS) * CvcMerkleProof.PADDING_INCREMENTS; const newNodes = _.clone(nodes); + const secureRandom = services.container.SecureRandom; while (newNodes.length < targetLength) { newNodes.push(new UCA('cvc:Random:node', secureRandom.wordWith(16))); } diff --git a/dist/es/services/index.js b/dist/es/services/index.js index 161fe999..b59dd0fc 100644 --- a/dist/es/services/index.js +++ b/dist/es/services/index.js @@ -40,7 +40,7 @@ services.factory('Config', () => config); logger.info('Registering request-promise-native as Http service implementation.'); services.service('Http', HttpServiceConstructor); -services.service('SecureRandom', SecureRandom); +services.factory('SecureRandom', () => new SecureRandom()); services.service('AnchorService', CurrentCivicAnchor, 'Config', 'Http'); diff --git a/dist/es/uca/UserCollectableAttribute.js b/dist/es/uca/UserCollectableAttribute.js index f25dbc9f..5365cb08 100644 --- a/dist/es/uca/UserCollectableAttribute.js +++ b/dist/es/uca/UserCollectableAttribute.js @@ -4,8 +4,6 @@ const sjcl = require('sjcl'); const definitions = require('./definitions'); const { services } = require('../services'); -const secureRandom = services.container.SecureRandom; - const validIdentifiers = _.map(definitions, d => d.identifier); /** @@ -73,6 +71,7 @@ const findDefinitionByAttestableValue = (attestableValuePropertyName, rootDefini // eslint-disable-next-line no-restricted-syntax for (const property of rootDefinition.type.properties) { const resolvedDefinition = _.find(definitions, { identifier: property.type }); + resolvedDefinition.type = resolveType(resolvedDefinition); if (!resolvedDefinition.type.properties && property.name === attestableValuePropertyName) { return property.type; } @@ -164,6 +163,7 @@ const parseAttestableValue = value => { function UCABaseConstructor(identifier, value, version) { this.timestamp = null; this.id = null; + this.secureRandom = services.container.SecureRandom; if (!_.includes(validIdentifiers, identifier)) { throw new Error(`${identifier} is not defined`); @@ -215,7 +215,7 @@ function UCABaseConstructor(identifier, value, version) { } this.value = value; - this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(secureRandom.wordWith(64))); + this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(this.secureRandom.wordWith(64))); } else if (_.isEmpty(definition.type.properties)) { throw new Error(`${JSON.stringify(value)} is not valid for ${identifier}`); } else { diff --git a/package.json b/package.json index b8317edd..7e91daa0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@identity.com/credential-commons", - "version": "0.2.27", + "version": "0.2.28", "author": "Identity.com Community", "license": "MIT", "description": "Verifiable Credential and Attestation Library", diff --git a/src/creds/VerifiableCredential.js b/src/creds/VerifiableCredential.js index f9f771d9..41c0005d 100644 --- a/src/creds/VerifiableCredential.js +++ b/src/creds/VerifiableCredential.js @@ -9,10 +9,6 @@ const definitions = require('./definitions'); const UCA = require('../uca/UserCollectableAttribute'); const { services } = require('../services'); -function getSecureRandom() { - return services.container.SecureRandom; -} - function getAnchorService() { return services.container.AnchorService; } @@ -168,8 +164,9 @@ class CvcMerkleProof { const targetLength = currentLength < CvcMerkleProof.PADDING_INCREMENTS ? CvcMerkleProof.PADDING_INCREMENTS : _.ceil(currentLength / CvcMerkleProof.PADDING_INCREMENTS) * CvcMerkleProof.PADDING_INCREMENTS; const newNodes = _.clone(nodes); + const secureRandom = services.container.SecureRandom; while (newNodes.length < targetLength) { - newNodes.push(new UCA('cvc:Random:node', getSecureRandom().wordWith(16))); + newNodes.push(new UCA('cvc:Random:node', secureRandom.wordWith(16))); } return newNodes; } diff --git a/src/uca/UserCollectableAttribute.js b/src/uca/UserCollectableAttribute.js index 24e2d237..3a18d541 100644 --- a/src/uca/UserCollectableAttribute.js +++ b/src/uca/UserCollectableAttribute.js @@ -4,10 +4,6 @@ const sjcl = require('sjcl'); const definitions = require('./definitions'); const { services } = require('../services'); -function getSecureRandom() { - return services.container.SecureRandom; -} - const validIdentifiers = _.map(definitions, d => d.identifier); /** @@ -176,6 +172,7 @@ const parseAttestableValue = (value) => { function UCABaseConstructor(identifier, value, version) { this.timestamp = null; this.id = null; + this.secureRandom = services.container.SecureRandom; if (!_.includes(validIdentifiers, identifier)) { throw new Error(`${identifier} is not defined`); @@ -229,7 +226,7 @@ function UCABaseConstructor(identifier, value, version) { } this.value = value; - this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(getSecureRandom().wordWith(64))); + this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(this.secureRandom.wordWith(64))); } else if (_.isEmpty(definition.type.properties)) { throw new Error(`${JSON.stringify(value)} is not valid for ${identifier}`); } else { From 2da92688395871ab5b6fcb89c3f53fddebd78c3f Mon Sep 17 00:00:00 2001 From: Bruno Carneiro Date: Thu, 22 Nov 2018 11:01:01 -0200 Subject: [PATCH 2/3] Changing injection of AnchorService --- src/creds/VerifiableCredential.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/creds/VerifiableCredential.js b/src/creds/VerifiableCredential.js index 41c0005d..3c32ceb3 100644 --- a/src/creds/VerifiableCredential.js +++ b/src/creds/VerifiableCredential.js @@ -9,10 +9,6 @@ const definitions = require('./definitions'); const UCA = require('../uca/UserCollectableAttribute'); const { services } = require('../services'); -function getAnchorService() { - return services.container.AnchorService; -} - function sha256(string) { return sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(string)); } @@ -279,7 +275,8 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, * @param {*} options */ this.requestAnchor = async (options) => { - const anchor = await getAnchorService().anchor(this.identifier, this.proof.merkleRoot, options); + const anchorService = services.container.AnchorService; + const anchor = await anchorService.anchor(this.identifier, this.proof.merkleRoot, options); this.proof.anchor = anchor; return this; }; @@ -289,7 +286,8 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, * already confirmed on the blockchain. */ this.updateAnchor = async () => { - const anchor = await getAnchorService().update(this.proof.anchor); + const anchorService = services.container.AnchorService; + const anchor = await anchorService.update(this.proof.anchor); this.proof.anchor = anchor; return this; }; @@ -382,24 +380,24 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, * This method checks if the signature matches for the root of the Merkle Tree * @return true or false for the validation */ - this.verifySignature = async () => getAnchorService().verifySignature(this.proof); + this.verifySignature = async () => services.container.AnchorService.verifySignature(this.proof); /** * This method checks that the attestation / anchor exists on the BC */ - this.verifyAttestation = async () => getAnchorService().verifyAttestation(this.proof); + this.verifyAttestation = async () => services.container.AnchorService.verifyAttestation(this.proof); /** * This method will revoke the attestation on the chain * @returns {Promise|void>} */ - this.revokeAttestation = async () => getAnchorService().revokeAttestation(this.proof); + this.revokeAttestation = async () => services.container.AnchorService.revokeAttestation(this.proof); /** * This method will check on the chain the balance of the transaction and if it's still unspent, than it's not revoked * @returns {Promise|void>} */ - this.isRevoked = async () => getAnchorService().isRevoked(this.proof); + this.isRevoked = async () => services.container.AnchorService.isRevoked(this.proof); this.isMatch = (constraints) => { const siftConstraints = transformConstraint(constraints); From e10f93f316cd785442fc099fcb3eea1181aed6ce Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Thu, 22 Nov 2018 11:49:29 -0200 Subject: [PATCH 3/3] Changing injection of SecureRandom --- dist/browser/creds/VerifiableCredential.js | 2 +- dist/browser/uca/UserCollectableAttribute.js | 2 +- dist/cjs/creds/VerifiableCredential.js | 18 ++++++++---------- dist/cjs/uca/UserCollectableAttribute.js | 4 ++-- dist/es/creds/VerifiableCredential.js | 18 ++++++++---------- dist/es/uca/UserCollectableAttribute.js | 4 ++-- src/uca/UserCollectableAttribute.js | 4 ++-- 7 files changed, 24 insertions(+), 28 deletions(-) diff --git a/dist/browser/creds/VerifiableCredential.js b/dist/browser/creds/VerifiableCredential.js index 98c79b68..262cafb9 100644 --- a/dist/browser/creds/VerifiableCredential.js +++ b/dist/browser/creds/VerifiableCredential.js @@ -1 +1 @@ -"use strict";var _toConsumableArray2=require("babel-runtime/helpers/toConsumableArray"),_toConsumableArray3=_interopRequireDefault(_toConsumableArray2),_slicedToArray2=require("babel-runtime/helpers/slicedToArray"),_slicedToArray3=_interopRequireDefault(_slicedToArray2),_regenerator=require("babel-runtime/regenerator"),_regenerator2=_interopRequireDefault(_regenerator),_asyncToGenerator2=require("babel-runtime/helpers/asyncToGenerator"),_asyncToGenerator3=_interopRequireDefault(_asyncToGenerator2),_classCallCheck2=require("babel-runtime/helpers/classCallCheck"),_classCallCheck3=_interopRequireDefault(_classCallCheck2),_createClass2=require("babel-runtime/helpers/createClass"),_createClass3=_interopRequireDefault(_createClass2),_typeof2=require("babel-runtime/helpers/typeof"),_typeof3=_interopRequireDefault(_typeof2),_keys=require("babel-runtime/core-js/object/keys"),_keys2=_interopRequireDefault(_keys);function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}var _=require("lodash"),sift=require("sift"),MerkleTools=require("merkle-tools"),sjcl=require("sjcl"),timestamp=require("unix-timestamp"),flatten=require("flat"),uuidv4=require("uuid/v4"),definitions=require("./definitions"),UCA=require("../uca/UserCollectableAttribute"),_require=require("../services"),services=_require.services;function getAnchorService(){return services.container.AnchorService}function sha256(a){return sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(a))}function getClaimPath(a,b){var c=_.split(a,":"),d=_.camelCase(c[1]),e=d+"."+c[2],f=_.find(b,function(a){return _.endsWith(a,e)});return f||e}function validIdentifiers(){var a=_.map(definitions,function(a){return a.identifier});return a}function getClaimsWithFlatKeys(a){var b=flatten(a,{maxDepth:3}),c=flatten(a,{maxDepth:2}),d=_.merge({},b,c),e=_(d).toPairs().sortBy(0).fromPairs().value();return e}function paths(a){for(var b=[],c=[{obj:a,path:[]}],d=function(){var a=c.pop();(0,_keys2.default)(a.obj).forEach(function(d){if("object"===(0,_typeof3.default)(a.obj[d])){var e=a.path.concat(d);b.push(e),c.unshift({obj:a.obj[d],path:e})}})};0t.getTime()&&k.push(a)}}return _.isEmpty(j)&&_.isEmpty(l)&&_.isEmpty(m)&&_.isEmpty(n)&&_.isEmpty(k)&&(e=!0),e},this.verify=function(a){var b=a||VERIFY_LEVELS.PROOFS,c=VERIFY_LEVELS.INVALID;return b>=VERIFY_LEVELS.PROOFS&&f.verifyProofs()&&(c=VERIFY_LEVELS.PROOFS),c},this.verifySignature=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",getAnchorService().verifySignature(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.verifyAttestation=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",getAnchorService().verifyAttestation(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.revokeAttestation=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",getAnchorService().revokeAttestation(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.isRevoked=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",getAnchorService().isRevoked(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.isMatch=function(a){var b=transformConstraint(a),c=!0;return _.forEach(b,function(a){return c=-1t.getTime()&&k.push(a)}}return _.isEmpty(j)&&_.isEmpty(l)&&_.isEmpty(m)&&_.isEmpty(n)&&_.isEmpty(k)&&(e=!0),e},this.verify=function(a){var b=a||VERIFY_LEVELS.PROOFS,c=VERIFY_LEVELS.INVALID;return b>=VERIFY_LEVELS.PROOFS&&f.verifyProofs()&&(c=VERIFY_LEVELS.PROOFS),c},this.verifySignature=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",services.container.AnchorService.verifySignature(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.verifyAttestation=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",services.container.AnchorService.verifyAttestation(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.revokeAttestation=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",services.container.AnchorService.revokeAttestation(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.isRevoked=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function a(){return _regenerator2.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",services.container.AnchorService.isRevoked(f.proof));case 1:case"end":return a.stop();}},a,f)})),this.isMatch=function(a){var b=transformConstraint(a),c=!0;return _.forEach(b,function(a){return c=-1=c.minimumLength)&&(!c.maximumLength||a.length<=c.minimumLength):"Number"===b?((!_.isNil(c.minimum)&&c.exclusiveMinimum?a>c.minimum:a>=c.minimum)||_.isNil(c.minimum))&&((!_.isNil(c.maximum)&&c.exclusiveMaximum?a=c.minimumLength)&&(!c.maximumLength||a.length<=c.minimumLength):"Number"===b?((!_.isNil(c.minimum)&&c.exclusiveMinimum?a>c.minimum:a>=c.minimum)||_.isNil(c.minimum))&&((!_.isNil(c.maximum)&&c.exclusiveMaximum?a { var _ref = _asyncToGenerator(function* (options) { - const anchor = yield getAnchorService().anchor(_this.identifier, _this.proof.merkleRoot, options); + const anchorService = services.container.AnchorService; + const anchor = yield anchorService.anchor(_this.identifier, _this.proof.merkleRoot, options); _this.proof.anchor = anchor; return _this; }); @@ -293,7 +290,8 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, * already confirmed on the blockchain. */ this.updateAnchor = _asyncToGenerator(function* () { - const anchor = yield getAnchorService().update(_this.proof.anchor); + const anchorService = services.container.AnchorService; + const anchor = yield anchorService.update(_this.proof.anchor); _this.proof.anchor = anchor; return _this; }); @@ -383,14 +381,14 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, * @return true or false for the validation */ this.verifySignature = _asyncToGenerator(function* () { - return getAnchorService().verifySignature(_this.proof); + return services.container.AnchorService.verifySignature(_this.proof); }); /** * This method checks that the attestation / anchor exists on the BC */ this.verifyAttestation = _asyncToGenerator(function* () { - return getAnchorService().verifyAttestation(_this.proof); + return services.container.AnchorService.verifyAttestation(_this.proof); }); /** @@ -398,7 +396,7 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, * @returns {Promise|void>} */ this.revokeAttestation = _asyncToGenerator(function* () { - return getAnchorService().revokeAttestation(_this.proof); + return services.container.AnchorService.revokeAttestation(_this.proof); }); /** @@ -406,7 +404,7 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, * @returns {Promise|void>} */ this.isRevoked = _asyncToGenerator(function* () { - return getAnchorService().isRevoked(_this.proof); + return services.container.AnchorService.isRevoked(_this.proof); }); this.isMatch = constraints => { diff --git a/dist/cjs/uca/UserCollectableAttribute.js b/dist/cjs/uca/UserCollectableAttribute.js index 53123435..6c38d942 100644 --- a/dist/cjs/uca/UserCollectableAttribute.js +++ b/dist/cjs/uca/UserCollectableAttribute.js @@ -165,7 +165,6 @@ const parseAttestableValue = value => { function UCABaseConstructor(identifier, value, version) { this.timestamp = null; this.id = null; - this.secureRandom = services.container.SecureRandom; if (!_.includes(validIdentifiers, identifier)) { throw new Error(`${identifier} is not defined`); @@ -217,7 +216,8 @@ function UCABaseConstructor(identifier, value, version) { } this.value = value; - this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(this.secureRandom.wordWith(64))); + const secureRandom = services.container.SecureRandom; + this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(secureRandom.wordWith(64))); } else if (_.isEmpty(definition.type.properties)) { throw new Error(`${JSON.stringify(value)} is not valid for ${identifier}`); } else { diff --git a/dist/es/creds/VerifiableCredential.js b/dist/es/creds/VerifiableCredential.js index 9b5bd819..159c1a50 100644 --- a/dist/es/creds/VerifiableCredential.js +++ b/dist/es/creds/VerifiableCredential.js @@ -9,10 +9,6 @@ const definitions = require('./definitions'); const UCA = require('../uca/UserCollectableAttribute'); const { services } = require('../services'); -function getAnchorService() { - return services.container.AnchorService; -} - function sha256(string) { return sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(string)); } @@ -271,7 +267,8 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, * @param {*} options */ this.requestAnchor = async options => { - const anchor = await getAnchorService().anchor(this.identifier, this.proof.merkleRoot, options); + const anchorService = services.container.AnchorService; + const anchor = await anchorService.anchor(this.identifier, this.proof.merkleRoot, options); this.proof.anchor = anchor; return this; }; @@ -281,7 +278,8 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, * already confirmed on the blockchain. */ this.updateAnchor = async () => { - const anchor = await getAnchorService().update(this.proof.anchor); + const anchorService = services.container.AnchorService; + const anchor = await anchorService.update(this.proof.anchor); this.proof.anchor = anchor; return this; }; @@ -370,24 +368,24 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, * This method checks if the signature matches for the root of the Merkle Tree * @return true or false for the validation */ - this.verifySignature = async () => getAnchorService().verifySignature(this.proof); + this.verifySignature = async () => services.container.AnchorService.verifySignature(this.proof); /** * This method checks that the attestation / anchor exists on the BC */ - this.verifyAttestation = async () => getAnchorService().verifyAttestation(this.proof); + this.verifyAttestation = async () => services.container.AnchorService.verifyAttestation(this.proof); /** * This method will revoke the attestation on the chain * @returns {Promise|void>} */ - this.revokeAttestation = async () => getAnchorService().revokeAttestation(this.proof); + this.revokeAttestation = async () => services.container.AnchorService.revokeAttestation(this.proof); /** * This method will check on the chain the balance of the transaction and if it's still unspent, than it's not revoked * @returns {Promise|void>} */ - this.isRevoked = async () => getAnchorService().isRevoked(this.proof); + this.isRevoked = async () => services.container.AnchorService.isRevoked(this.proof); this.isMatch = constraints => { const siftConstraints = transformConstraint(constraints); diff --git a/dist/es/uca/UserCollectableAttribute.js b/dist/es/uca/UserCollectableAttribute.js index 5365cb08..b47a83b7 100644 --- a/dist/es/uca/UserCollectableAttribute.js +++ b/dist/es/uca/UserCollectableAttribute.js @@ -163,7 +163,6 @@ const parseAttestableValue = value => { function UCABaseConstructor(identifier, value, version) { this.timestamp = null; this.id = null; - this.secureRandom = services.container.SecureRandom; if (!_.includes(validIdentifiers, identifier)) { throw new Error(`${identifier} is not defined`); @@ -215,7 +214,8 @@ function UCABaseConstructor(identifier, value, version) { } this.value = value; - this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(this.secureRandom.wordWith(64))); + const secureRandom = services.container.SecureRandom; + this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(secureRandom.wordWith(64))); } else if (_.isEmpty(definition.type.properties)) { throw new Error(`${JSON.stringify(value)} is not valid for ${identifier}`); } else { diff --git a/src/uca/UserCollectableAttribute.js b/src/uca/UserCollectableAttribute.js index 3a18d541..67ea1a4f 100644 --- a/src/uca/UserCollectableAttribute.js +++ b/src/uca/UserCollectableAttribute.js @@ -172,7 +172,6 @@ const parseAttestableValue = (value) => { function UCABaseConstructor(identifier, value, version) { this.timestamp = null; this.id = null; - this.secureRandom = services.container.SecureRandom; if (!_.includes(validIdentifiers, identifier)) { throw new Error(`${identifier} is not defined`); @@ -226,7 +225,8 @@ function UCABaseConstructor(identifier, value, version) { } this.value = value; - this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(this.secureRandom.wordWith(64))); + const secureRandom = services.container.SecureRandom; + this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(secureRandom.wordWith(64))); } else if (_.isEmpty(definition.type.properties)) { throw new Error(`${JSON.stringify(value)} is not valid for ${identifier}`); } else {