Skip to content

Commit

Permalink
Add support for includeTimestamp option.
Browse files Browse the repository at this point in the history
-add 'includeTimestamp' option: if not true, 'created' field deleted from proof
-add tests for includeTimestamp option
  • Loading branch information
wes-smith committed Feb 12, 2024
1 parent 1e1dc28 commit 219434b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
12 changes: 8 additions & 4 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {createVerifier} from './createVerifier.js';
import {name} from './name.js';
import {requiredAlgorithm} from './requiredAlgorithm.js';

export function createCryptosuite({extraInformation = new Uint8Array()} = {}) {
const options = {extraInformation};
export function createCryptosuite({extraInformation = new Uint8Array(),
includeTimestamp = false} = {}) {
const options = {extraInformation, includeTimestamp};
return {
name,
canonize,
Expand All @@ -25,8 +26,11 @@ async function _createSignData({cryptosuite, document, proof, documentLoader}) {
const options = {documentLoader};

// delete proof timestamp
delete proof.created;

if(cryptosuite.options.includeTimestamp == true) {
} else {
delete proof.created;
}

// create hash from `extraInformation`
const hasher = createHasher();
const externalHash = await hasher.hash(cryptosuite.options.extraInformation);
Expand Down
55 changes: 54 additions & 1 deletion test/EcdsaXi2023Cryptosuite.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const extraInformation = new Uint8Array([
12, 52, 75, 63, 74, 85, 21, 5, 62, 100,
12, 52, 75, 63
]);
const ecdsaXi2023Cryptosuite = createCryptosuite({extraInformation});
const includeTimestamp = false;
const ecdsaXi2023Cryptosuite = createCryptosuite({extraInformation, includeTimestamp});

Check failure on line 31 in test/EcdsaXi2023Cryptosuite.spec.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

This line has a length of 87. Maximum allowed is 80

describe('EcdsaXi2023Cryptosuite', () => {
describe('exports', () => {
Expand Down Expand Up @@ -150,6 +151,58 @@ describe('EcdsaXi2023Cryptosuite', () => {
expect(error).to.not.exist;
});

it('should contain a timestamp with includeTimestamp = true passed', async () => {

Check failure on line 154 in test/EcdsaXi2023Cryptosuite.spec.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

This line has a length of 86. Maximum allowed is 80
const unsignedCredential = JSON.parse(JSON.stringify(credential));
const keyPair = await EcdsaMultikey.from({...ecdsaMultikeyKeyPair});
const date = '2023-03-01T21:29:24Z';
const includeTimestamp = true;
const ecdsaXi2023CryptosuiteWithTimestamp = createCryptosuite({extraInformation, includeTimestamp});

Check failure on line 159 in test/EcdsaXi2023Cryptosuite.spec.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

This line has a length of 106. Maximum allowed is 80
const suite = new DataIntegrityProof({
signer: keyPair.signer(), date, cryptosuite: ecdsaXi2023CryptosuiteWithTimestamp

Check failure on line 161 in test/EcdsaXi2023Cryptosuite.spec.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

This line has a length of 88. Maximum allowed is 80
});

let error;
let signed;
try {
signed = await jsigs.sign(unsignedCredential, {
suite,
purpose: new AssertionProofPurpose(),
documentLoader
});
} catch(e) {
error = e;
}

expect(error).to.not.exist;
expect(signed).to.exist;
expect(signed.proof.created).to.exist;
});

it('should not contain a timestamp otherwise', async () => {
const unsignedCredential = JSON.parse(JSON.stringify(credential));
const keyPair = await EcdsaMultikey.from({...ecdsaMultikeyKeyPair});
const date = '2023-03-01T21:29:24Z';
const suite = new DataIntegrityProof({
signer: keyPair.signer(), date, cryptosuite: ecdsaXi2023Cryptosuite
});

let error;
let signed;
try {
signed = await jsigs.sign(unsignedCredential, {
suite,
purpose: new AssertionProofPurpose(),
documentLoader
});
} catch(e) {
error = e;
}

expect(error).to.not.exist;
expect(signed).to.exist;
expect(signed.proof.created).to.not.exist;
});

it('should fail to sign with undefined term', async () => {
const unsignedCredential = JSON.parse(JSON.stringify(credential));
unsignedCredential.undefinedTerm = 'foo';
Expand Down
3 changes: 2 additions & 1 deletion test/testSignVerify.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ const unsignedCredential = {
const extraInformation = new Uint8Array([12, 52, 75, 63, 74, 85,
21, 5, 62, 100]);

const includeTimestamp = false;
// create suite
const suite = new DataIntegrityProof({
signer: keyPair.signer(),
cryptosuite: createCryptosuite({extraInformation})
cryptosuite: createCryptosuite({extraInformation, includeTimestamp})
});

// create signed credential
Expand Down

0 comments on commit 219434b

Please sign in to comment.