Skip to content

Commit

Permalink
Merge pull request #122 from identity-com/handle-alias-ucas
Browse files Browse the repository at this point in the history
Handle alias UCAs on Claim transformation
  • Loading branch information
lucmir authored Jul 12, 2019
2 parents ad5215b + 660a662 commit c74057a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion __test__/claim/Claim.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ describe('Claim Constructions tests', () => {

// converting UCAs to Claims
const evidencesClaim = new Claim(identifier, evidencesUCA.getPlainValue());
const evidencesClaimForAliasUCA = new Claim(identifier, evidencesAliasUCA.getPlainValue());
const evidencesClaimForAliasUCA = new Claim(aliasIdentifier, evidencesAliasUCA.getPlainValue());

// should map to the same claim
expect(evidencesClaimForAliasUCA.identifier).toEqual(evidencesClaim.identifier);
Expand Down
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@identity.com/credential-commons",
"version": "1.0.22",
"version": "1.0.23",
"author": "Identity.com Community",
"license": "MIT",
"description": "Verifiable Credential and Attestation Library",
Expand Down Expand Up @@ -60,7 +60,7 @@
"rimraf": "^2.6.2"
},
"dependencies": {
"@identity.com/uca": "1.0.13",
"@identity.com/uca": "1.0.14",
"ajv": "^6.10.0",
"babel-runtime": "^6.26.0",
"bitcoinjs-lib": "git+https://github.com/dabura667/bitcoinjs-lib.git#bcash330",
Expand Down
21 changes: 13 additions & 8 deletions src/claim/Claim.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const { services } = require('../services');

const validIdentifiers = _.map(definitions, d => d.identifier);

const getDefinition = (identifier, version) => (
version ? _.find(definitions, { identifier, version }) : _.find(definitions, { identifier })
);

function getBaseIdentifiers(identifier) {
const claimRegex = /claim-cvc:(.*)\.(.*)-v\d*/;
let isNewIdentifier = true;
Expand All @@ -18,23 +22,26 @@ function getBaseIdentifiers(identifier) {
return { identifierComponents, isNewIdentifier };
}

function adaptIdentifierIfNeeded(identifier) {
const { isNewIdentifier, identifierComponents } = getBaseIdentifiers(identifier);
function adaptIdentifierIfNeeded(identifier, version) {
const definition = getDefinition(identifier, version);
const resolvedIdentifier = (definition && definition.alias) ? definition.type : identifier;

const { isNewIdentifier, identifierComponents } = getBaseIdentifiers(resolvedIdentifier);

if (!isNewIdentifier && !_.find(definitions, { identifier })) {
if (!isNewIdentifier && !getDefinition(resolvedIdentifier, version)) {
const newIdentifier = `claim-cvc:${identifierComponents[1]}.${identifierComponents[2]}-v1`;
const foundNewIdentifier = _.find(definitions, { identifier: newIdentifier });
if (foundNewIdentifier) {
return newIdentifier;
}
throw new Error(`${identifier} is not defined`);
throw new Error(`${resolvedIdentifier} is not defined`);
}
return identifier;
}

class Claim extends UserCollectableAttribute {
constructor(identifier, value, version) {
const currentIdentifier = adaptIdentifierIfNeeded(identifier);
const currentIdentifier = adaptIdentifierIfNeeded(identifier, version);
super(currentIdentifier, value, version, definitions);
this.initialize(currentIdentifier, value, version);
}
Expand All @@ -49,9 +56,7 @@ class Claim extends UserCollectableAttribute {

initializeAttestableValue() {
const { value } = this;
const definition = this.version
? _.find(definitions, { identifier: this.identifier, version: this.version })
: _.find(definitions, { identifier: this.identifier });
const definition = getDefinition(this.identifier, this.version);

// Trying to construct UCA with a existing attestableValue
const parsedAttestableValue = Claim.parseAttestableValue(value);
Expand Down

0 comments on commit c74057a

Please sign in to comment.