From 4fa887288b119372e590710942d15a4bbcd9266d Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Thu, 1 Nov 2018 13:08:52 -0300 Subject: [PATCH 1/6] Any call to credential-commons-js inside the CW gives errors with RandomSeed --- __test__/SecureRandom.test.js | 3 +- __test__/SecureRandomMock.test.js | 3 +- __test__/creds/VerifiableCredential.test.js | 2 - .../creds/VerifiableCredentialSchema.test.js | 11 +- .../creds/fixtures/CredentialAddress.json | 241 ++++++++++++++++++ package-lock.json | 49 ++-- src/SecureRandom.js | 36 +-- src/creds/VerifiableCredential.js | 41 +-- src/uca/UserCollectableAttribute.js | 23 +- 9 files changed, 338 insertions(+), 71 deletions(-) create mode 100644 __test__/creds/fixtures/CredentialAddress.json diff --git a/__test__/SecureRandom.test.js b/__test__/SecureRandom.test.js index 987278a8..edd39cea 100644 --- a/__test__/SecureRandom.test.js +++ b/__test__/SecureRandom.test.js @@ -2,7 +2,8 @@ const SecureRandom = require('../src/SecureRandom'); describe('Secure Random Tests', () => { it('should generate an random word', () => { - const random = SecureRandom.wordWith(16); + const secureRandom = new SecureRandom(); + const random = secureRandom.wordWith(16); expect(random).toBeDefined(); expect(random).toHaveLength(16); }); diff --git a/__test__/SecureRandomMock.test.js b/__test__/SecureRandomMock.test.js index 0dd150dd..583be3dd 100644 --- a/__test__/SecureRandomMock.test.js +++ b/__test__/SecureRandomMock.test.js @@ -4,6 +4,7 @@ const SecureRandom = require('../src/SecureRandom'); jest.mock('crypto'); describe('Secure Random Tests', () => { it('should fail since we are mocking the crypto class', () => { - expect(() => SecureRandom.wordWith(16)).toThrow(); + const secureRandom = new SecureRandom(); + expect(() => secureRandom.wordWith(16)).toThrow(); }); }); diff --git a/__test__/creds/VerifiableCredential.test.js b/__test__/creds/VerifiableCredential.test.js index 710ba4fa..8e9e9bbf 100644 --- a/__test__/creds/VerifiableCredential.test.js +++ b/__test__/creds/VerifiableCredential.test.js @@ -137,7 +137,6 @@ describe('Unit tests for Verifiable Credentials', () => { expect(filtered.claim.identity.name.familyNames).not.toBeDefined(); }); - test('cred verifyProofs', () => { const credJSon = require('./fixtures/Cred1.json'); // eslint-disable-line const cred = VC.fromJSON(credJSon); @@ -541,5 +540,4 @@ describe('Unit tests for Verifiable Credentials', () => { expect(properties).toContain('contact.email.domain.name'); expect(properties).toContain('contact.email.domain.tld'); }); - }); diff --git a/__test__/creds/VerifiableCredentialSchema.test.js b/__test__/creds/VerifiableCredentialSchema.test.js index 8de74b48..bfe9584f 100644 --- a/__test__/creds/VerifiableCredentialSchema.test.js +++ b/__test__/creds/VerifiableCredentialSchema.test.js @@ -26,8 +26,8 @@ describe('VerifiableCredentials SchemaGenerator validation', () => { expect(jsonSchema.properties.proof.type).toBe('object'); }); - // Skiped while dmelosantos is working on this - test.skip('Should validate the generated VC against it\'s generated schema looping the definitions', async (done) => { + // This is skipped because it's not possible to validate the schemas against S3 buckets for now + test('Should validate the generated VC against it\'s generated schema looping the definitions', async (done) => { const validateSchemaJestStep = async (credentialDefinition) => { const ucaArray = []; credentialDefinition.depends.forEach((ucaDefinitionIdentifier) => { @@ -47,11 +47,14 @@ describe('VerifiableCredentials SchemaGenerator validation', () => { const jsonString = JSON.stringify(credential, null, 2); const generatedJson = JSON.parse(jsonString); - const jsonSchema = SchemaGenerator.process(credential, generatedJson); + console.log(credentialDefinition.identifier); + console.log(jsonString); + /*const jsonSchema = SchemaGenerator.process(credential, generatedJson); const ajv = new Ajv(); const validate = ajv.compile(jsonSchema); const isValid = validate(generatedJson); - return isValid; + return isValid;*/ + return true; }; const promises = []; credentialDefinitions.forEach((credentialDefinition) => { promises.push(validateSchemaJestStep(credentialDefinition)); }); diff --git a/__test__/creds/fixtures/CredentialAddress.json b/__test__/creds/fixtures/CredentialAddress.json new file mode 100644 index 00000000..bda3df08 --- /dev/null +++ b/__test__/creds/fixtures/CredentialAddress.json @@ -0,0 +1,241 @@ +{ + "id": "fc60be8f-ea01-4881-a6e9-b4ae9a63e55e", + "issuer": "jest:test:9ff1e700-dd46-11e8-958d-e5793374641e", + "issuanceDate": "2018-10-31T19:53:23.568Z", + "identifier": "cvc:Credential:PhoneNumber", + "expirationDate": null, + "version": "1", + "type": [ + "Credential", + "cvc:Credential:PhoneNumber" + ], + "claim": { + "contact": { + "phoneNumber": { + "country": "WTYqO3zRU0", + "countryCode": "nmQfVTPkEM", + "extension": "lo6CoXdj2N", + "lineType": "zkdkjiX1eP", + "number": "h7CrPkWRoA" + } + } + }, + "proof": { + "type": "CivicMerkleProof2018", + "merkleRoot": "77ca9a60724d007173136465fa6bdcaa27d12a1801b1d1e8ab974552344d2e39", + "anchor": { + "subject": { + "pub": "xpub:dummy", + "label": "cvc:Credential:PhoneNumber", + "data": "77ca9a60724d007173136465fa6bdcaa27d12a1801b1d1e8ab974552344d2e39", + "signature": "signed:dummy" + }, + "walletId": "none", + "cosigners": [ + { + "pub": "xpub:dummy" + }, + { + "pub": "xpub:dummy" + } + ], + "authority": { + "pub": "xpub:dummy", + "path": "/" + }, + "coin": "dummycoin", + "tx": {}, + "network": "dummynet", + "type": "permanent", + "civicAsPrimary": false, + "schema": "dummy-20180201", + "value": {} + }, + "leaves": [ + { + "identifier": "cvc:Contact:phoneNumber", + "value": "urn:country:ca3bce5c4b3256888c8fa9937d3025516b49b422112bd99cdaf9be66087984e9:WTYqO3zRU0|urn:countryCode:81a228f771ef72126b22d6d17f08222f4241efedf5f13fbb635d8568686a0b6f:nmQfVTPkEM|urn:extension:42cab0927b370d5ee047152aaf52c902f80d7de312bdeca6e4c4e3d4c6603abc:lo6CoXdj2N|urn:lineType:0a71c9a31246f2f6ecfd76a7c71f34a6f741cef3afb2466cc88017bdc16d95a7:zkdkjiX1eP|urn:number:53a8ba743f83a16e533d5572c24dad080820baef958e80c39899838ba1cdd674:h7CrPkWRoA|", + "claimPath": "contact.phoneNumber", + "targetHash": "cc8f00eaf13969a880fe5d57204b509dd7cc087b99019af1c6e678d4fe072499", + "node": [ + { + "right": "082eeee25eae17f13a74b4d0801d182f9e490ddd70969d1b1a23a709fe87a184" + }, + { + "right": "cd0013b49b3d52aa5c6bf6ac854e77fcac1eab934d4c388ae5837f70172ee91e" + }, + { + "right": "a1d95e970de891aa6d227426d071a28f3440ee0a3e99f2a76c1a49193cd999f8" + }, + { + "right": "ac9e75c2d832091f3b18e974dc224ed5ef970673ed679ff931c1f4a33db3f929" + }, + { + "right": "3b2728ce00cdc42c8c524574ab20a8ea1e78efa805bba6e988c10e0c13f77d3f" + } + ] + }, + { + "identifier": "cvc:Phone:countryCode", + "value": "urn:countryCode:81a228f771ef72126b22d6d17f08222f4241efedf5f13fbb635d8568686a0b6f:nmQfVTPkEM", + "claimPath": "phone.countryCode", + "targetHash": "082eeee25eae17f13a74b4d0801d182f9e490ddd70969d1b1a23a709fe87a184", + "node": [ + { + "left": "cc8f00eaf13969a880fe5d57204b509dd7cc087b99019af1c6e678d4fe072499" + }, + { + "right": "cd0013b49b3d52aa5c6bf6ac854e77fcac1eab934d4c388ae5837f70172ee91e" + }, + { + "right": "a1d95e970de891aa6d227426d071a28f3440ee0a3e99f2a76c1a49193cd999f8" + }, + { + "right": "ac9e75c2d832091f3b18e974dc224ed5ef970673ed679ff931c1f4a33db3f929" + }, + { + "right": "3b2728ce00cdc42c8c524574ab20a8ea1e78efa805bba6e988c10e0c13f77d3f" + } + ] + }, + { + "identifier": "cvc:Phone:number", + "value": "urn:number:53a8ba743f83a16e533d5572c24dad080820baef958e80c39899838ba1cdd674:h7CrPkWRoA", + "claimPath": "phone.number", + "targetHash": "bf6403908a22f3e8c5d477498d6828411801280128289ecd8680ea8496a92d4d", + "node": [ + { + "right": "1b52b0600694813ce1992fafe6d106d10ecef752721f7c79aae6db6be8eede54" + }, + { + "left": "a9cb50eddf122581028336ac675bd83d681eaeda754aced0630e8c765914a656" + }, + { + "right": "a1d95e970de891aa6d227426d071a28f3440ee0a3e99f2a76c1a49193cd999f8" + }, + { + "right": "ac9e75c2d832091f3b18e974dc224ed5ef970673ed679ff931c1f4a33db3f929" + }, + { + "right": "3b2728ce00cdc42c8c524574ab20a8ea1e78efa805bba6e988c10e0c13f77d3f" + } + ] + }, + { + "identifier": "cvc:Phone:extension", + "value": "urn:extension:42cab0927b370d5ee047152aaf52c902f80d7de312bdeca6e4c4e3d4c6603abc:lo6CoXdj2N", + "claimPath": "phone.extension", + "targetHash": "1b52b0600694813ce1992fafe6d106d10ecef752721f7c79aae6db6be8eede54", + "node": [ + { + "left": "bf6403908a22f3e8c5d477498d6828411801280128289ecd8680ea8496a92d4d" + }, + { + "left": "a9cb50eddf122581028336ac675bd83d681eaeda754aced0630e8c765914a656" + }, + { + "right": "a1d95e970de891aa6d227426d071a28f3440ee0a3e99f2a76c1a49193cd999f8" + }, + { + "right": "ac9e75c2d832091f3b18e974dc224ed5ef970673ed679ff931c1f4a33db3f929" + }, + { + "right": "3b2728ce00cdc42c8c524574ab20a8ea1e78efa805bba6e988c10e0c13f77d3f" + } + ] + }, + { + "identifier": "cvc:Phone:lineType", + "value": "urn:lineType:0a71c9a31246f2f6ecfd76a7c71f34a6f741cef3afb2466cc88017bdc16d95a7:zkdkjiX1eP", + "claimPath": "phone.lineType", + "targetHash": "90891289e7dd6c36dd1fc38187fce024899bdd1c20bf857ff1fe439b2bd18754", + "node": [ + { + "right": "059d1df65ffdc8a1c0cff86412f716745531466cdd1c9c2c1264364bba325e01" + }, + { + "right": "7d167b4360413c5016d7bdf6b080767275e925e07c18f0a16729b882ef245ace" + }, + { + "left": "0d4f56aae6f29bab5d8f83fb595673bdc629d5d0973e78b53ad43a1bb1dd9515" + }, + { + "right": "ac9e75c2d832091f3b18e974dc224ed5ef970673ed679ff931c1f4a33db3f929" + }, + { + "right": "3b2728ce00cdc42c8c524574ab20a8ea1e78efa805bba6e988c10e0c13f77d3f" + } + ] + }, + { + "identifier": "cvc:Meta:issuer", + "value": "urn:issuer:5bd06324dc242c17e811f56c8d449b4c8f966503811abc1f5a0ff35b876fee2d:jest:test:9ff1e700-dd46-11e8-958d-e5793374641e", + "claimPath": "meta.issuer", + "targetHash": "059d1df65ffdc8a1c0cff86412f716745531466cdd1c9c2c1264364bba325e01", + "node": [ + { + "left": "90891289e7dd6c36dd1fc38187fce024899bdd1c20bf857ff1fe439b2bd18754" + }, + { + "right": "7d167b4360413c5016d7bdf6b080767275e925e07c18f0a16729b882ef245ace" + }, + { + "left": "0d4f56aae6f29bab5d8f83fb595673bdc629d5d0973e78b53ad43a1bb1dd9515" + }, + { + "right": "ac9e75c2d832091f3b18e974dc224ed5ef970673ed679ff931c1f4a33db3f929" + }, + { + "right": "3b2728ce00cdc42c8c524574ab20a8ea1e78efa805bba6e988c10e0c13f77d3f" + } + ] + }, + { + "identifier": "cvc:Meta:issuanceDate", + "value": "urn:issuanceDate:b8b3000ae66079b85aaadb9adfe6658ae09452f77fb8eefeddf96c40692c8bd7:2018-10-31T19:53:23.568Z", + "claimPath": "meta.issuanceDate", + "targetHash": "93c37224a3cd5e6dd47a98e55f42854ee5b3b9fda63f60b8076c57a59bd84f0b", + "node": [ + { + "right": "292ed3b6a33a406ae43bf0d43d159a04a5417e1a244017c174194889898de1e1" + }, + { + "left": "785d97b0b9fe7809bf55803fc3ded62180e9b01fac15a488ac84e6238eb466f3" + }, + { + "left": "0d4f56aae6f29bab5d8f83fb595673bdc629d5d0973e78b53ad43a1bb1dd9515" + }, + { + "right": "ac9e75c2d832091f3b18e974dc224ed5ef970673ed679ff931c1f4a33db3f929" + }, + { + "right": "3b2728ce00cdc42c8c524574ab20a8ea1e78efa805bba6e988c10e0c13f77d3f" + } + ] + }, + { + "identifier": "cvc:Meta:expirationDate", + "value": "urn:expirationDate:a43b2ec05674437f2e440be5c72cfcee323fe544c88e212f4bc6c463a8c35dda:null", + "claimPath": "meta.expirationDate", + "targetHash": "292ed3b6a33a406ae43bf0d43d159a04a5417e1a244017c174194889898de1e1", + "node": [ + { + "left": "93c37224a3cd5e6dd47a98e55f42854ee5b3b9fda63f60b8076c57a59bd84f0b" + }, + { + "left": "785d97b0b9fe7809bf55803fc3ded62180e9b01fac15a488ac84e6238eb466f3" + }, + { + "left": "0d4f56aae6f29bab5d8f83fb595673bdc629d5d0973e78b53ad43a1bb1dd9515" + }, + { + "right": "ac9e75c2d832091f3b18e974dc224ed5ef970673ed679ff931c1f4a33db3f929" + }, + { + "right": "3b2728ce00cdc42c8c524574ab20a8ea1e78efa805bba6e988c10e0c13f77d3f" + } + ] + } + ] + } +} diff --git a/package-lock.json b/package-lock.json index ae706ef0..22c39649 100644 --- a/package-lock.json +++ b/package-lock.json @@ -228,7 +228,7 @@ }, "acorn-jsx": { "version": "3.0.1", - "resolved": "http://registry.npmjs.org/acorn-jsx/-/acorn-jsx-3.0.1.tgz", + "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-3.0.1.tgz", "integrity": "sha1-r9+UiPsezvyDSPb7IvRk4ypYs2s=", "dev": true, "requires": { @@ -237,7 +237,7 @@ "dependencies": { "acorn": { "version": "3.3.0", - "resolved": "http://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-3.3.0.tgz", "integrity": "sha1-ReN/s56No/JbruP/U2niu18iAXo=", "dev": true } @@ -516,7 +516,7 @@ }, "chalk": { "version": "1.1.3", - "resolved": "http://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", "dev": true, "requires": { @@ -680,7 +680,7 @@ }, "babel-helper-is-nodes-equiv": { "version": "0.0.1", - "resolved": "http://registry.npmjs.org/babel-helper-is-nodes-equiv/-/babel-helper-is-nodes-equiv-0.0.1.tgz", + "resolved": "https://registry.npmjs.org/babel-helper-is-nodes-equiv/-/babel-helper-is-nodes-equiv-0.0.1.tgz", "integrity": "sha1-NOmzALFHnd2Y7HfqC76TQt/jloQ=", "dev": true }, @@ -810,7 +810,7 @@ }, "babel-plugin-istanbul": { "version": "4.1.6", - "resolved": "http://registry.npmjs.org/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.6.tgz", + "resolved": "https://registry.npmjs.org/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.6.tgz", "integrity": "sha512-PWP9FQ1AhZhS01T/4qLSKoHGY/xvkZdVBGlKM/HuxxS3+sC66HhTNR7+MpbO/so/cz/wY94MeSWJuP1hXIPfwQ==", "dev": true, "requires": { @@ -923,19 +923,19 @@ }, "babel-plugin-syntax-async-functions": { "version": "6.13.0", - "resolved": "http://registry.npmjs.org/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz", + "resolved": "https://registry.npmjs.org/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz", "integrity": "sha1-ytnK0RkbWtY0vzCuCHI5HgZHvpU=", "dev": true }, "babel-plugin-syntax-exponentiation-operator": { "version": "6.13.0", - "resolved": "http://registry.npmjs.org/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz", + "resolved": "https://registry.npmjs.org/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz", "integrity": "sha1-nufoM3KQ2pUoggGmpX9BcDF4MN4=", "dev": true }, "babel-plugin-syntax-object-rest-spread": { "version": "6.13.0", - "resolved": "http://registry.npmjs.org/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz", + "resolved": "https://registry.npmjs.org/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz", "integrity": "sha1-/WU28rzhODb/o6VFjEkDpZe7O/U=", "dev": true }, @@ -2022,6 +2022,11 @@ "resolved": "https://registry.npmjs.org/crypt/-/crypt-0.0.2.tgz", "integrity": "sha1-iNf/fsDfuG9xPch7u0LQRNPmxBs=" }, + "crypto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz", + "integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==" + }, "cssom": { "version": "0.3.4", "resolved": "https://registry.npmjs.org/cssom/-/cssom-0.3.4.tgz", @@ -2383,7 +2388,7 @@ }, "eslint": { "version": "4.19.1", - "resolved": "http://registry.npmjs.org/eslint/-/eslint-4.19.1.tgz", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-4.19.1.tgz", "integrity": "sha512-bT3/1x1EbZB7phzYu7vCr1v3ONuzDtX8WjuM9c0iYxe+cq+pwcKEoQjl7zd3RpC6YOLgnSy3cTN58M2jcoPDIQ==", "dev": true, "requires": { @@ -2467,7 +2472,7 @@ }, "external-editor": { "version": "2.2.0", - "resolved": "http://registry.npmjs.org/external-editor/-/external-editor-2.2.0.tgz", + "resolved": "https://registry.npmjs.org/external-editor/-/external-editor-2.2.0.tgz", "integrity": "sha512-bSn6gvGxKt+b7+6TKEv1ZycHleA7aHhRHyAqJyp5pbUFuYYNIzpZnQDk7AsYckyWdEnTeAnay0aCy2aV6iTk9A==", "dev": true, "requires": { @@ -2603,7 +2608,7 @@ }, "load-json-file": { "version": "2.0.0", - "resolved": "http://registry.npmjs.org/load-json-file/-/load-json-file-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-2.0.0.tgz", "integrity": "sha1-eUfkIUmvgNaWy/eXvKq8/h/inKg=", "dev": true, "requires": { @@ -4160,7 +4165,7 @@ }, "is-builtin-module": { "version": "1.0.0", - "resolved": "http://registry.npmjs.org/is-builtin-module/-/is-builtin-module-1.0.0.tgz", + "resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-1.0.0.tgz", "integrity": "sha1-VAVy0096wxGfj3bDDLwbHgN6/74=", "dev": true, "requires": { @@ -5064,7 +5069,7 @@ }, "json5": { "version": "0.5.1", - "resolved": "http://registry.npmjs.org/json5/-/json5-0.5.1.tgz", + "resolved": "https://registry.npmjs.org/json5/-/json5-0.5.1.tgz", "integrity": "sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE=", "dev": true }, @@ -5152,7 +5157,7 @@ }, "load-json-file": { "version": "1.1.0", - "resolved": "http://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz", + "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz", "integrity": "sha1-lWkFcI1YtLq0wiYbBPWfMcmTdMA=", "dev": true, "requires": { @@ -5376,7 +5381,7 @@ }, "minimist": { "version": "0.0.8", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", "dev": true }, @@ -5403,7 +5408,7 @@ }, "mkdirp": { "version": "0.5.1", - "resolved": "http://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", "dev": true, "requires": { @@ -8797,7 +8802,7 @@ }, "readable-stream": { "version": "2.3.6", - "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", "requires": { "core-util-is": "~1.0.0", @@ -9767,7 +9772,7 @@ }, "minimist": { "version": "1.2.0", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true } @@ -10282,7 +10287,7 @@ }, "through": { "version": "2.3.8", - "resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" }, "tmp": { @@ -10640,7 +10645,7 @@ "dependencies": { "minimist": { "version": "1.2.0", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true } @@ -10725,7 +10730,7 @@ }, "wrap-ansi": { "version": "2.1.0", - "resolved": "http://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", "integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=", "dev": true, "requires": { @@ -10830,7 +10835,7 @@ }, "yargs": { "version": "11.1.0", - "resolved": "http://registry.npmjs.org/yargs/-/yargs-11.1.0.tgz", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-11.1.0.tgz", "integrity": "sha512-NwW69J42EsCSanF8kyn5upxvjp5ds+t3+udGBeTbFnERA+lF541DDpMawzo4z6W/QrzNM18D+BPMiOBibnFV5A==", "dev": true, "requires": { diff --git a/src/SecureRandom.js b/src/SecureRandom.js index 5d763a16..a06be5d8 100644 --- a/src/SecureRandom.js +++ b/src/SecureRandom.js @@ -1,29 +1,35 @@ const sjcl = require('sjcl'); const logger = require('./logger'); -class SecureRandomizer { - constructor() { - logger.debug('Init Secure Randon'); - // eslint-disable-next-line +class SecureRandom { + constructor(seedHexString) { + logger.debug('Init Secure Random'); + // eslint-disable-next-line new-cap this.sjclRandom = new sjcl.prng(10); - try { - logger.debug('Trying crypto'); - /* eslint-disable global-require */ - const hexString = require('crypto').randomBytes(1024).toString('hex'); - /* eslint-enable global-require */ - const seed = sjcl.codec.hex.toBits(hexString); + if (seedHexString) { + const seed = sjcl.codec.hex.toBits(seedHexString); this.sjclRandom.addEntropy(seed, undefined, 'csprng'); this.isSeeded = true; - } catch (error) { - logger.warn(`Crypto: ${error}`); - this.isSeeded = false; + } else { + try { + logger.debug('Trying crypto'); + /* eslint-disable global-require */ + const hexString = require('crypto').randomBytes(1024).toString('hex'); + /* eslint-enable global-require */ + const seed = sjcl.codec.hex.toBits(hexString); + this.sjclRandom.addEntropy(seed, undefined, 'csprng'); + this.isSeeded = true; + } catch (error) { + logger.warn(`Crypto: ${error}`); + this.isSeeded = false; + } } } wordWith(size) { if (!this.isSeeded) { - throw new Error("Can't user SecureRandon before seeding"); + throw new Error("Can't user SecureRandom before seeding"); } const randomBytes = this.sjclRandom.randomWords(size / 8, 10); @@ -31,4 +37,4 @@ class SecureRandomizer { } } -module.exports = new SecureRandomizer(); +module.exports = SecureRandom; diff --git a/src/creds/VerifiableCredential.js b/src/creds/VerifiableCredential.js index a5cfa8e4..3b5738ff 100644 --- a/src/creds/VerifiableCredential.js +++ b/src/creds/VerifiableCredential.js @@ -7,8 +7,8 @@ const flatten = require('flat'); const uuidv4 = require('uuid/v4'); const definitions = require('./definitions'); const UCA = require('../uca/UserCollectableAttribute'); -const SecureRandom = require('../SecureRandom'); const { services } = require('../services'); +const SecureRandom = require('../SecureRandom'); const anchorService = services.container.AnchorService; @@ -103,19 +103,19 @@ function transformConstraint(constraints) { } /** - * Transforms a list of UCAs into the signature property of the verifiable cliams + * Transforms a list of UCAs into the signature property of the verifiable claims */ -class CivicMerkleProof { +class CvcMerkleProof { static get PADDING_INCREMENTS() { return 16; } - constructor(ucas, claimsPathRef) { - const withRandomUcas = CivicMerkleProof.padTree(ucas); - this.type = 'CivicMerkleProof2018'; + constructor(ucas, claimsPathRef, seedHexString) { + const withRandomUcas = CvcMerkleProof.padTree(ucas, seedHexString); + this.type = 'CvcMerkleProof2018'; this.merkleRoot = null; this.anchor = 'TBD (Civic Blockchain Attestation)'; - this.leaves = CivicMerkleProof.getAllAttestableValue(withRandomUcas); + this.leaves = CvcMerkleProof.getAllAttestableValue(withRandomUcas); this.buildMerkleTree(claimsPathRef); } @@ -133,13 +133,19 @@ class CivicMerkleProof { this.merkleRoot = merkleTools.getMerkleRoot().toString('hex'); } - static padTree(nodes) { + static padTree(nodes, seedHexString) { const currentLength = nodes.length; - const targetLength = currentLength < CivicMerkleProof.PADDING_INCREMENTS ? CivicMerkleProof.PADDING_INCREMENTS - : _.ceil(currentLength / CivicMerkleProof.PADDING_INCREMENTS) * CivicMerkleProof.PADDING_INCREMENTS; + const targetLength = currentLength < CvcMerkleProof.PADDING_INCREMENTS ? CvcMerkleProof.PADDING_INCREMENTS + : _.ceil(currentLength / CvcMerkleProof.PADDING_INCREMENTS) * CvcMerkleProof.PADDING_INCREMENTS; const newNodes = _.clone(nodes); + let secureRandom; + if (seedHexString) { + secureRandom = new SecureRandom(seedHexString); + } else { + secureRandom = new SecureRandom(); + } while (newNodes.length < targetLength) { - newNodes.push(new UCA('cvc:Random:node', SecureRandom.wordWith(16))); + newNodes.push(new UCA('cvc:Random:node', secureRandom.wordWith(16))); } return newNodes; } @@ -185,15 +191,15 @@ const VERIFY_LEVELS = { * @param {*} ucas * @param {*} version */ -function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, version) { +function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, version, seedHexString) { this.id = uuidv4(); this.issuer = issuer; - const issuerUCA = new UCA('cvc:Meta:issuer', this.issuer); + const issuerUCA = new UCA('cvc:Meta:issuer', this.issuer, seedHexString); this.issuanceDate = (new Date()).toISOString(); - const issuanceDateUCA = new UCA('cvc:Meta:issuanceDate', this.issuanceDate); + const issuanceDateUCA = new UCA('cvc:Meta:issuanceDate', this.issuanceDate, seedHexString); this.identifier = identifier; this.expirationDate = expiryIn ? timestamp.toDate(timestamp.now(expiryIn)).toISOString() : null; - const expiryUCA = new UCA('cvc:Meta:expirationDate', this.expirationDate ? this.expirationDate : 'null'); + const expiryUCA = new UCA('cvc:Meta:expirationDate', this.expirationDate ? this.expirationDate : 'null', seedHexString); const proofUCAs = expiryUCA ? _.concat(ucas, issuerUCA, issuanceDateUCA, expiryUCA) : _.concat(ucas, issuerUCA, issuanceDateUCA); @@ -212,7 +218,7 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, if (!_.isEmpty(ucas)) { this.claim = new ClaimModel(ucas); const claimsPathRef = _.keys(flatten(this.claim, { safe: true })); - this.proof = new CivicMerkleProof(proofUCAs, claimsPathRef); + this.proof = new CvcMerkleProof(proofUCAs, claimsPathRef, seedHexString); if (!_.isEmpty(definition.excludes)) { const removed = _.remove(this.proof.leaves, el => _.includes(definition.excludes, el.identifier)); _.forEach(removed, (r) => { @@ -236,6 +242,7 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, filtered.claim = {}; _.forEach(filtered.proof.leaves, (el) => { + _.set(filtered.claim, el.claimPath, _.get(this.claim, el.claimPath)); }); @@ -339,7 +346,7 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, }; /** - * Verify the Credencial and return a verification level. + * Verify the Credential and return a verification level. * @return Any of VC.VERIFY_LEVELS */ this.verify = (higherVerifyLevel) => { diff --git a/src/uca/UserCollectableAttribute.js b/src/uca/UserCollectableAttribute.js index eda5c1a0..2e36af74 100644 --- a/src/uca/UserCollectableAttribute.js +++ b/src/uca/UserCollectableAttribute.js @@ -100,23 +100,23 @@ const getAllProperties = (identifier, pathName) => { } if (_.includes(['String', 'Number', 'Boolean'], `${typeDefProps.type}`)) { - // Propertie is not an object + // Properties is not an object properties.push(`${basePropName}.${typeDefProps.name}`); } else { _.forEach(typeDefProps, (prop) => { - const typeSufix = _.split(prop.type, ':')[2]; - const newBasePropName = prop.name === typeSufix ? basePropName : `${basePropName}.${prop.name}`; + const typeSuffix = _.split(prop.type, ':')[2]; + const newBasePropName = prop.name === typeSuffix ? basePropName : `${basePropName}.${prop.name}`; const proProperties = getAllProperties(prop.type, newBasePropName); _.forEach(proProperties, p => properties.push(p)); }); } } else if (pathName) { - const propertieName = `${pathName}.${_.split(definition.identifier, ':')[2]}`; - properties.push(propertieName); + const propertiesName = `${pathName}.${_.split(definition.identifier, ':')[2]}`; + properties.push(propertiesName); } else { const identifierComponents = _.split(identifier, ':'); - const propertieName = `${_.lowerCase(identifierComponents[1])}.${identifierComponents[2]}`; - properties.push(propertieName); + const propertiesName = `${_.lowerCase(identifierComponents[1])}.${identifierComponents[2]}`; + properties.push(propertiesName); } return properties; }; @@ -152,7 +152,7 @@ const parseAttestableValue = (value) => { * @param {*} identifier * @param {*} value */ -function UCABaseConstructor(identifier, value, version) { +function UCABaseConstructor(identifier, value, version, seedHexString) { this.timestamp = null; this.id = null; @@ -195,7 +195,12 @@ function UCABaseConstructor(identifier, value, version) { throw new Error(`${JSON.stringify(value)} is not valid for ${identifier}`); } this.value = value; - this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(SecureRandom.wordWith(64))); + if (seedHexString) { + this.secureRandom = new SecureRandom(seedHexString); + } else { + this.secureRandom = new SecureRandom(); + } + 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 9834f0d2db16c3365b2e7c862da74a8813e6a53c Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Thu, 1 Nov 2018 13:28:05 -0300 Subject: [PATCH 2/6] Leftover console.log removed --- __test__/creds/VerifiableCredentialSchema.test.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/__test__/creds/VerifiableCredentialSchema.test.js b/__test__/creds/VerifiableCredentialSchema.test.js index bfe9584f..15edcd92 100644 --- a/__test__/creds/VerifiableCredentialSchema.test.js +++ b/__test__/creds/VerifiableCredentialSchema.test.js @@ -27,7 +27,7 @@ describe('VerifiableCredentials SchemaGenerator validation', () => { }); // This is skipped because it's not possible to validate the schemas against S3 buckets for now - test('Should validate the generated VC against it\'s generated schema looping the definitions', async (done) => { + test.skip('Should validate the generated VC against it\'s generated schema looping the definitions', async (done) => { const validateSchemaJestStep = async (credentialDefinition) => { const ucaArray = []; credentialDefinition.depends.forEach((ucaDefinitionIdentifier) => { @@ -47,14 +47,11 @@ describe('VerifiableCredentials SchemaGenerator validation', () => { const jsonString = JSON.stringify(credential, null, 2); const generatedJson = JSON.parse(jsonString); - console.log(credentialDefinition.identifier); - console.log(jsonString); - /*const jsonSchema = SchemaGenerator.process(credential, generatedJson); + const jsonSchema = SchemaGenerator.process(credential, generatedJson); const ajv = new Ajv(); const validate = ajv.compile(jsonSchema); const isValid = validate(generatedJson); - return isValid;*/ - return true; + return isValid; }; const promises = []; credentialDefinitions.forEach((credentialDefinition) => { promises.push(validateSchemaJestStep(credentialDefinition)); }); From 63522bf091bbc31d6de2036a8d3679c48b75f572 Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Thu, 1 Nov 2018 13:28:25 -0300 Subject: [PATCH 3/6] Added seed hex string to method fromJSON --- src/creds/VerifiableCredential.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/creds/VerifiableCredential.js b/src/creds/VerifiableCredential.js index 3b5738ff..2ec07f9d 100644 --- a/src/creds/VerifiableCredential.js +++ b/src/creds/VerifiableCredential.js @@ -464,9 +464,10 @@ VerifiableCredentialBaseConstructor.isMatchCredentialMeta = isMatchCredentialMet /** * Factory function that creates a new Verifiable Credential based on a JSON object * @param {*} verifiableCredentialJSON + * @param seedHexString */ -VerifiableCredentialBaseConstructor.fromJSON = (verifiableCredentialJSON) => { - const newObj = new VerifiableCredentialBaseConstructor(verifiableCredentialJSON.identifier, verifiableCredentialJSON.issuer); +VerifiableCredentialBaseConstructor.fromJSON = (verifiableCredentialJSON, seedHexString) => { + const newObj = new VerifiableCredentialBaseConstructor(verifiableCredentialJSON.identifier, verifiableCredentialJSON.issuer, seedHexString); newObj.id = _.clone(verifiableCredentialJSON.id); newObj.issuanceDate = _.clone(verifiableCredentialJSON.issuanceDate); newObj.expirationDate = _.clone(verifiableCredentialJSON.expirationDate); From 3ccde4ded869a9fb43eb9b54d228fa434715ecfd Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Thu, 1 Nov 2018 13:30:27 -0300 Subject: [PATCH 4/6] Skipped wrong test --- __test__/creds/VerifiableCredentialSchema.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/__test__/creds/VerifiableCredentialSchema.test.js b/__test__/creds/VerifiableCredentialSchema.test.js index 15edcd92..cefaaf33 100644 --- a/__test__/creds/VerifiableCredentialSchema.test.js +++ b/__test__/creds/VerifiableCredentialSchema.test.js @@ -26,8 +26,7 @@ describe('VerifiableCredentials SchemaGenerator validation', () => { expect(jsonSchema.properties.proof.type).toBe('object'); }); - // This is skipped because it's not possible to validate the schemas against S3 buckets for now - test.skip('Should validate the generated VC against it\'s generated schema looping the definitions', async (done) => { + test('Should validate the generated VC against it\'s generated schema looping the definitions', async (done) => { const validateSchemaJestStep = async (credentialDefinition) => { const ucaArray = []; credentialDefinition.depends.forEach((ucaDefinitionIdentifier) => { From 3dbc6c44f16f98884670338969bc3d2f6613342d Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Thu, 1 Nov 2018 15:50:05 -0300 Subject: [PATCH 5/6] Change SecureRandom to BottleJS --- __test__/creds/VerifiableCredential.test.js | 18 +++++++++++++ src/creds/VerifiableCredential.js | 30 ++++++++------------- src/services/httpService.js | 2 +- src/services/index.js | 17 ++++++++---- src/uca/UserCollectableAttribute.js | 14 +++++----- 5 files changed, 48 insertions(+), 33 deletions(-) diff --git a/__test__/creds/VerifiableCredential.test.js b/__test__/creds/VerifiableCredential.test.js index 8e9e9bbf..46b02942 100644 --- a/__test__/creds/VerifiableCredential.test.js +++ b/__test__/creds/VerifiableCredential.test.js @@ -113,6 +113,7 @@ describe('Unit tests for Verifiable Credentials', () => { }); }); }); + test('Filter claims', () => { const civIdentityName = { givenNames: 'Joao', @@ -137,6 +138,23 @@ describe('Unit tests for Verifiable Credentials', () => { expect(filtered.claim.identity.name.familyNames).not.toBeDefined(); }); + // TODO enable me when CCS-514 is done + test.skip('Filter claims of composite UCA', () => { + const phoneNumber = { + country: 'BRZ', + countryCode: '+55', + number: '31995383635', + lineType: 'mobile', + }; + + const phoneNumberUca = new UCA('cvc:Contact:phoneNumber', phoneNumber); + const phoneNumberCredential = new VC('cvc:Credential:PhoneNumber', 'Civic-Identity-Verifier', null, [phoneNumberUca], '1'); + + const filtered = phoneNumberCredential.filter(['cvc:Phone:countryCode']); + console.log(filtered); + expect(filtered.claim.contact.phoneNumber.countryCode).toBeDefined(); + }); + test('cred verifyProofs', () => { const credJSon = require('./fixtures/Cred1.json'); // eslint-disable-line const cred = VC.fromJSON(credJSon); diff --git a/src/creds/VerifiableCredential.js b/src/creds/VerifiableCredential.js index 2ec07f9d..89a60886 100644 --- a/src/creds/VerifiableCredential.js +++ b/src/creds/VerifiableCredential.js @@ -8,9 +8,9 @@ const uuidv4 = require('uuid/v4'); const definitions = require('./definitions'); const UCA = require('../uca/UserCollectableAttribute'); const { services } = require('../services'); -const SecureRandom = require('../SecureRandom'); const anchorService = services.container.AnchorService; +const secureRandom = services.container.SecureRandom; function sha256(string) { return sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(string)); @@ -110,8 +110,8 @@ class CvcMerkleProof { return 16; } - constructor(ucas, claimsPathRef, seedHexString) { - const withRandomUcas = CvcMerkleProof.padTree(ucas, seedHexString); + constructor(ucas, claimsPathRef) { + const withRandomUcas = CvcMerkleProof.padTree(ucas); this.type = 'CvcMerkleProof2018'; this.merkleRoot = null; this.anchor = 'TBD (Civic Blockchain Attestation)'; @@ -133,17 +133,11 @@ class CvcMerkleProof { this.merkleRoot = merkleTools.getMerkleRoot().toString('hex'); } - static padTree(nodes, seedHexString) { + static padTree(nodes) { 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); - let secureRandom; - if (seedHexString) { - secureRandom = new SecureRandom(seedHexString); - } else { - secureRandom = new SecureRandom(); - } while (newNodes.length < targetLength) { newNodes.push(new UCA('cvc:Random:node', secureRandom.wordWith(16))); } @@ -191,15 +185,15 @@ const VERIFY_LEVELS = { * @param {*} ucas * @param {*} version */ -function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, version, seedHexString) { +function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, version) { this.id = uuidv4(); this.issuer = issuer; - const issuerUCA = new UCA('cvc:Meta:issuer', this.issuer, seedHexString); + const issuerUCA = new UCA('cvc:Meta:issuer', this.issuer); this.issuanceDate = (new Date()).toISOString(); - const issuanceDateUCA = new UCA('cvc:Meta:issuanceDate', this.issuanceDate, seedHexString); + const issuanceDateUCA = new UCA('cvc:Meta:issuanceDate', this.issuanceDate); this.identifier = identifier; this.expirationDate = expiryIn ? timestamp.toDate(timestamp.now(expiryIn)).toISOString() : null; - const expiryUCA = new UCA('cvc:Meta:expirationDate', this.expirationDate ? this.expirationDate : 'null', seedHexString); + const expiryUCA = new UCA('cvc:Meta:expirationDate', this.expirationDate ? this.expirationDate : 'null'); const proofUCAs = expiryUCA ? _.concat(ucas, issuerUCA, issuanceDateUCA, expiryUCA) : _.concat(ucas, issuerUCA, issuanceDateUCA); @@ -218,7 +212,7 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, if (!_.isEmpty(ucas)) { this.claim = new ClaimModel(ucas); const claimsPathRef = _.keys(flatten(this.claim, { safe: true })); - this.proof = new CvcMerkleProof(proofUCAs, claimsPathRef, seedHexString); + this.proof = new CvcMerkleProof(proofUCAs, claimsPathRef); if (!_.isEmpty(definition.excludes)) { const removed = _.remove(this.proof.leaves, el => _.includes(definition.excludes, el.identifier)); _.forEach(removed, (r) => { @@ -242,7 +236,6 @@ function VerifiableCredentialBaseConstructor(identifier, issuer, expiryIn, ucas, filtered.claim = {}; _.forEach(filtered.proof.leaves, (el) => { - _.set(filtered.claim, el.claimPath, _.get(this.claim, el.claimPath)); }); @@ -464,10 +457,9 @@ VerifiableCredentialBaseConstructor.isMatchCredentialMeta = isMatchCredentialMet /** * Factory function that creates a new Verifiable Credential based on a JSON object * @param {*} verifiableCredentialJSON - * @param seedHexString */ -VerifiableCredentialBaseConstructor.fromJSON = (verifiableCredentialJSON, seedHexString) => { - const newObj = new VerifiableCredentialBaseConstructor(verifiableCredentialJSON.identifier, verifiableCredentialJSON.issuer, seedHexString); +VerifiableCredentialBaseConstructor.fromJSON = (verifiableCredentialJSON) => { + const newObj = new VerifiableCredentialBaseConstructor(verifiableCredentialJSON.identifier, verifiableCredentialJSON.issuer); newObj.id = _.clone(verifiableCredentialJSON.id); newObj.issuanceDate = _.clone(verifiableCredentialJSON.issuanceDate); newObj.expirationDate = _.clone(verifiableCredentialJSON.expirationDate); diff --git a/src/services/httpService.js b/src/services/httpService.js index e13348f0..d9d713e9 100644 --- a/src/services/httpService.js +++ b/src/services/httpService.js @@ -2,7 +2,7 @@ * A simple node HTTP services */ const request = require('request-promise-native'); -// uncloment to debug requests +// uncomment to debug requests // require('request-debug')(request); function HttpServiceConstructor() { diff --git a/src/services/index.js b/src/services/index.js index b2077d0d..9a61ccf7 100644 --- a/src/services/index.js +++ b/src/services/index.js @@ -7,26 +7,32 @@ const AnchorService = require('./anchorService'); const logger = require('../logger'); const HttpServiceConstructor = require('./httpService'); const config = require('./config'); +const SecureRandom = require('../SecureRandom'); const services = new Bottle(); /** * Init services with new values to config and http services - * @param {*} conf - * @param {*} http + * @param {*} conf + * @param {*} http + * @param secureRandom */ -const initServices = (conf, http) => { +const initServices = (conf, http, secureRandom) => { if (http) { services.resetProviders(['Http']); logger.debug('Registering custom HTTP service implementation'); services.factory('Http', () => http); } if (conf) { - services.resetProviders(['Http']); + services.resetProviders(['Config']); logger.debug('Registering custom Config service implementation'); services.factory('Config', () => conf); } - + if (secureRandom) { + services.resetProviders(['SecureRandom']); + logger.debug('Registering custom SecureRandom service implementation'); + services.factory('SecureRandom', () => secureRandom); + } return services; }; @@ -35,6 +41,7 @@ services.factory('Config', () => config); logger.info('Registering request-promise-native as Http service implementation.'); services.service('Http', HttpServiceConstructor); +services.service('SecureRandom', SecureRandom); services.service('CivicAnchor', CurrentCivicAnchor, 'Config', 'Http'); diff --git a/src/uca/UserCollectableAttribute.js b/src/uca/UserCollectableAttribute.js index 2e36af74..f473603f 100644 --- a/src/uca/UserCollectableAttribute.js +++ b/src/uca/UserCollectableAttribute.js @@ -1,8 +1,10 @@ const _ = require('lodash'); const timestamp = require('unix-timestamp'); const sjcl = require('sjcl'); -const SecureRandom = require('../SecureRandom'); const definitions = require('./definitions'); +const { services } = require('../services'); + +const secureRandom = services.container.SecureRandom; const validIdentifiers = _.map(definitions, d => d.identifier); @@ -152,7 +154,7 @@ const parseAttestableValue = (value) => { * @param {*} identifier * @param {*} value */ -function UCABaseConstructor(identifier, value, version, seedHexString) { +function UCABaseConstructor(identifier, value, version) { this.timestamp = null; this.id = null; @@ -195,12 +197,8 @@ function UCABaseConstructor(identifier, value, version, seedHexString) { throw new Error(`${JSON.stringify(value)} is not valid for ${identifier}`); } this.value = value; - if (seedHexString) { - this.secureRandom = new SecureRandom(seedHexString); - } else { - this.secureRandom = new SecureRandom(); - } - this.salt = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(this.secureRandom.wordWith(64))); + + 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 { From 37f3ebaac8b129a4e9953e775794b51edfa1cc73 Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Thu, 1 Nov 2018 15:50:43 -0300 Subject: [PATCH 6/6] Fixing lint --- __test__/creds/VerifiableCredential.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/__test__/creds/VerifiableCredential.test.js b/__test__/creds/VerifiableCredential.test.js index 46b02942..1c27c60d 100644 --- a/__test__/creds/VerifiableCredential.test.js +++ b/__test__/creds/VerifiableCredential.test.js @@ -151,7 +151,6 @@ describe('Unit tests for Verifiable Credentials', () => { const phoneNumberCredential = new VC('cvc:Credential:PhoneNumber', 'Civic-Identity-Verifier', null, [phoneNumberUca], '1'); const filtered = phoneNumberCredential.filter(['cvc:Phone:countryCode']); - console.log(filtered); expect(filtered.claim.contact.phoneNumber.countryCode).toBeDefined(); });