Skip to content

Commit

Permalink
Algorithm context (#49)
Browse files Browse the repository at this point in the history
* fix: reflect context changes in hashing algorithm
  • Loading branch information
clementh59 authored Nov 2, 2022
1 parent 2584ac1 commit b038214
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import * as epcis from 'epcis2.js';

Or use a simple script tag to load it from the CDN:
```html
<script src="https://cdn.jsdelivr.net/npm/epcis2.js@2.6.0/dist/epcis2.browser.js"></script>
<script src="https://cdn.jsdelivr.net/npm/epcis2.js@2.6.1/dist/epcis2.browser.js"></script>
```

Then use in a browser `script` tag using the `epcis2` global variable:
Expand Down
20 changes: 12 additions & 8 deletions example/node_example/example_with_full_possibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,17 @@ const buildObjectEvent = () => {
}

const buildAssociationEvent = () => {
const context = {
example: 'http://ns.example.com/epcis/',
ext1: 'http://example.com/ext1/',
ext2: 'http://example.com/ext2/',
ext3: 'http://example.com/ext3/'
};

return new AssociationEvent(
{
"type": "AssociationEvent",
"@context": {
example: 'http://ns.example.com/epcis/',
ext1: 'http://example.com/ext1/'
},
"@context": context,
"eventTime": "2019-11-01T13:00:00.000Z",
"recordTime": "2005-04-05T02:33:31.116Z",
"certificationInfo": "https://accreditation-council.example.org/certificate/ABC12345",
Expand Down Expand Up @@ -379,9 +383,9 @@ const buildAssociationEvent = () => {
},
"ext1:default": "stringAsDefaultValue",
"ext1:int": "10",
"ext1:string": "string"
}
);
"ext1:string": "string",
"cbvmda:site": 402
}).generateHashID(context);
}

const buildExtendedEvent = () => {
Expand Down Expand Up @@ -511,7 +515,7 @@ const sendACaptureRequestExample = async () => {
console.log('\nEPCISDocument is valid ? ' + epcisDocument.isValid())

// capture
console.log('Capture:')
console.log('Capture:');
let res = await capture(epcisDocument);
const cr = new CaptureResponse(res);
await cr.pollForTheCaptureToFinish();
Expand Down
2 changes: 1 addition & 1 deletion example/web-example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<title>EPCIS2.js demo</title>
<script src="https://cdn.jsdelivr.net/npm/epcis2.js@2.6.0/dist/epcis2.browser.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/epcis2.js@2.6.1/dist/epcis2.browser.js" defer></script>
<script>
window.onload = () => {
const doc = new epcis2.EPCISDocument();
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "epcis2.js",
"version": "2.6.0",
"version": "2.6.1",
"description": "Javascript SDK for the EPCIS 2.0 standard",
"main": "./dist/epcis2.node.js",
"browser": "./dist/epcis2.browser.js",
Expand Down
18 changes: 16 additions & 2 deletions src/hash_generator/EPCISEventToPreHashedString.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ import cbv from '../cbv/cbv';
// The rules of the algorithm are defined here :
// https://github.com/RalphTro/epcis-event-hash-generator#algorithm

const epcisDefaultContext = {
epcis: 'https://ref.gs1.org/epcis/',
cbv: 'https://ref.gs1.org/cbv/',
cbvmda: 'urn:epcglobal:cbv:mda:',
gs1: 'https://gs1.org/voc/',
rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
owl: 'http://www.w3.org/2002/07/owl#',
xsd: 'http://www.w3.org/2001/XMLSchema#',
dcterms: 'http://purl.org/dc/terms/',
};

/**
* Return the pre-hashed string corresponding to the field-value passed in parameter
* @param {string} field
Expand Down Expand Up @@ -90,6 +101,9 @@ export const getPreHashStringFromCustomFieldElementList = (prefix, value, throwE
export const getPreHashStringFromCustomFieldElement = (key, value, context, throwError) => {
let field = key;

// comments are excluded from the pre-hash string.
if (key === 'rdfs:comment') return '';

const splitKey = key.split(':');

if (splitKey.length > 1) {
Expand All @@ -108,7 +122,7 @@ export const getPreHashStringFromCustomFieldElement = (key, value, context, thro

field = key.replace(`${splitKey[0]}:`, `{${context[splitKey[0]]}}`);
} else if (key.startsWith('#')) {
// if the key is '#text' for example, we don't want ot add the
// if the key is '#text' for example, we don't want to add the
// key to the pre-hashed string
return `=${value}`;
}
Expand Down Expand Up @@ -547,7 +561,7 @@ export const eventToPreHashedString = (event, context, throwError = true) => {
} else if (context instanceof Object) {
contextObject = context;
}
const extendedContext = { ...contextObject, ...getEventContexts(event) };
const extendedContext = { ...epcisDefaultContext, ...contextObject, ...getEventContexts(event) };
const res = getOrderedPreHashString(
event,
extendedContext,
Expand Down
3 changes: 2 additions & 1 deletion test/data/hashing/event-hash-reference-document3.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"example:userExt": {
"@xmlns:example": "https://ns.example.com/epcis",
"#text": "CD-34"
}
},
"cbvmda:sst": 201
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion test/data/hashing/samplePrehashesAndHashes.js

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

23 changes: 23 additions & 0 deletions test/hashing/preHashing/preHashingUnitTests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,29 @@ describe('unit tests for pre-hashing', () => {
)).to.throw();
});

it('should return a valid pre-hash of custom fields', () => {
const str = eventToPreHashedString(
{
'cbvmda:key': 'test',
},
sampleContext,
);
expect(str).to.be.equal(
'{urn:epcglobal:cbv:mda:}key=test',
);

const str2 = eventToPreHashedString(
{
'dcterms:term': 'value',
action: 'OBSERVE',
},
sampleContext,
);
expect(str2).to.be.equal(
'action=OBSERVE{http://purl.org/dc/terms/}term=value',
);
});

it('should automatically add the context from custom fields', () => {
const str = eventToPreHashedString(
{
Expand Down

0 comments on commit b038214

Please sign in to comment.