Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a MockDataIntegrityProof Class #47

Open
aljones15 opened this issue Jun 18, 2024 · 0 comments
Open

Create a MockDataIntegrityProof Class #47

aljones15 opened this issue Jun 18, 2024 · 0 comments
Assignees

Comments

@aljones15
Copy link
Contributor

Currently we simply mutate a new instance of DataIntegrityProof to produce invalid test data.
This works, but the

export function invalidCreateProof({
addCreated = true,
addVm = true,
addType = true,
addProofPurpose = true,
mockPurpose
}) {
return async function({
document,
purpose,
proofSet,
documentLoader
}) {
// build proof (currently known as `signature options` in spec)
let proof;
if(this.proof) {
// shallow copy
proof = {...this.proof};
} else {
// create proof JSON-LD document
proof = {};
}
if(addType) {
// ensure proof type is set
proof.type = this.type;
}
if(addCreated) {
// set default `now` date if not given in `proof` or `options`
let date = this.date;
if(proof.created === undefined && date === undefined) {
date = new Date();
}
// ensure date is in string format
if(date && (date instanceof Date)) {
// replace ms block with Z for seconds precision
date = date.toISOString().replace(/\.\d+Z$/, 'Z');
}
// add API overrides
if(date) {
proof.created = date;
}
}
if(addVm) {
proof.verificationMethod = this.verificationMethod;
}
proof.cryptosuite = this.cryptosuite;
// add any extensions to proof (mostly for legacy support)
proof = await this.updateProof({
document, proof, purpose, documentLoader
});
if(addProofPurpose) {
if(mockPurpose) {
proof.proofPurpose = mockPurpose;
} else {
// allow purpose to update the proof; the `proof` is in the
// SECURITY_CONTEXT_URL `@context` -- therefore the `purpose` must
// ensure any added fields are also represented in that same `@context`
proof = await purpose.update(
proof, {document, suite: this, documentLoader});
}
}
// create data to sign
let verifyData;
// use custom cryptosuite `createVerifyData` if available
if(this._cryptosuite.createVerifyData) {
verifyData = await this._cryptosuite.createVerifyData({
cryptosuite: this._cryptosuite,
document, proof, proofSet, documentLoader,
dataIntegrityProof: this
});
} else {
verifyData = await this.createVerifyData(
{document, proof, proofSet, documentLoader});
}
// use custom `createProofValue` if available
if(this._cryptosuite.createProofValue) {
proof.proofValue = await this._cryptosuite.createProofValue({
cryptosuite: this._cryptosuite,
verifyData, document, proof, proofSet,
documentLoader, dataIntegrityProof: this
});
} else {
// default to simple signing of data
proof = await this.sign(
{verifyData, document, proof, proofSet, documentLoader});
}
return proof;
};
}

invalidCreateProof function has a closure that can not be ammended later. This causes issues when chaining generators, for instance if you want an invalid proof lacking both a type and a verificationMethod it would be hard to do that.

so replace the invalidCreateProof with something like this:

export class MockDataIntegrityProof extends DataIntegrityProof {
  constructor({stubs, mocks, ...args}) {
    super({...args});
    this.stubs = stubs;
    this.mocks = mocks;
  }
  createProof(){
    ..contains current invalidCreateProof
  }
}

The advantage here is a single MockDataIntegrityProof can contain multiple mocks and stubs in one go reducing the need for chaining generators. Generators might still be needed for documentLoaders containing invalid contexts that make the mock proof possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants