Skip to content

Commit

Permalink
feat(ref-imp): #781 - address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacJChen committed Oct 10, 2020
1 parent 9059aa7 commit bbd40df
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
10 changes: 5 additions & 5 deletions lib/core/versions/latest/AnchorFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ export default class AnchorFile {
}

const createOperations = createOperationArray.map(operation => {
const suffixData = { delta_hash: operation.suffixData.deltaHash, recovery_commitment: operation.suffixData.recoveryCommitment, type: operation.suffixData.type };
if (suffixData.type === undefined) {
delete suffixData.type;
};
return {
suffix_data: suffixData
suffix_data: {
delta_hash: operation.suffixData.deltaHash,
recovery_commitment: operation.suffixData.recoveryCommitment,
type: operation.suffixData.type
}
};
});

Expand Down
2 changes: 1 addition & 1 deletion lib/core/versions/latest/Multihash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export default class Multihash {
/**
* Verifies the multihash against the content `Buffer`.
*/
private static verify (content: Buffer, encodedMultihash: string): boolean {
public static verify (content: Buffer, encodedMultihash: string): boolean {

try {
const expectedMultihashBuffer = Encoder.decodeAsBuffer(encodedMultihash);
Expand Down
43 changes: 27 additions & 16 deletions lib/core/versions/latest/OperationProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import CreateOperation from './CreateOperation';
import DeactivateOperation from './DeactivateOperation';
import DocumentComposer from './DocumentComposer';
import DidState from '../../models/DidState';
import Encoder from './Encoder';
import ErrorCode from './ErrorCode';
import IOperationProcessor from '../../interfaces/IOperationProcessor';
import JsonCanonicalizer from './util/JsonCanonicalizer';
Expand Down Expand Up @@ -115,14 +114,18 @@ export default class OperationProcessor implements IOperationProcessor {
};

// Verify the delta hash against the expected delta hash.
const expectedDeltaHash = operation.delta ? Encoder.encode(JsonCanonicalizer.canonicalizeAsBuffer({
const deltaPayload = operation.delta ? JsonCanonicalizer.canonicalizeAsBuffer({
update_commitment: operation.delta.updateCommitment,
patches: operation.delta.patches
})) : undefined;
const isMatchingDelta = Multihash.isValidHash(expectedDeltaHash, operation.suffixData.deltaHash);
}) : undefined;
if (deltaPayload === undefined) {
return newDidState;
};

const isMatchingDelta = Multihash.verify(deltaPayload, operation.suffixData.deltaHash);
if (!isMatchingDelta) {
return newDidState;
}
};

// Apply the given patches against an empty object.
const delta = operation.delta;
Expand Down Expand Up @@ -168,14 +171,18 @@ export default class OperationProcessor implements IOperationProcessor {
}

// Verify the delta hash against the expected delta hash.
const expectedDeltaHash = operation.delta ? Encoder.encode(JsonCanonicalizer.canonicalizeAsBuffer({
const deltaPayload = operation.delta ? JsonCanonicalizer.canonicalizeAsBuffer({
update_commitment: operation.delta.updateCommitment,
patches: operation.delta.patches
})) : undefined;
const isValidDelta = Multihash.isValidHash(expectedDeltaHash, operation.signedData.deltaHash);
if (!isValidDelta) {
}) : undefined;
if (deltaPayload === undefined) {
return didState;
}
};

const isMatchingDelta = Multihash.verify(deltaPayload, operation.signedData.deltaHash);
if (!isMatchingDelta) {
return didState;
};

let resultingDocument;
try {
Expand Down Expand Up @@ -230,15 +237,19 @@ export default class OperationProcessor implements IOperationProcessor {
lastOperationTransactionNumber: anchoredOperationModel.transactionNumber
};

// Verify the actual delta hash against the expected delta hash.
const expectedDeltaHash = operation.delta ? Encoder.encode(JsonCanonicalizer.canonicalizeAsBuffer({
// Verify the delta hash against the expected delta hash.
const deltaPayload = operation.delta ? JsonCanonicalizer.canonicalizeAsBuffer({
update_commitment: operation.delta.updateCommitment,
patches: operation.delta.patches
})) : undefined;
const isMatchingDelta = Multihash.isValidHash(expectedDeltaHash, operation.signedData.deltaHash);
}) : undefined;
if (deltaPayload === undefined) {
return didState;
};

const isMatchingDelta = Multihash.verify(deltaPayload, operation.signedData.deltaHash);
if (!isMatchingDelta) {
return newDidState;
}
return didState;
};

// Apply the given patches against an empty object.
const delta = operation.delta;
Expand Down
2 changes: 1 addition & 1 deletion tests/core/AnchorFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ describe('AnchorFile', async () => {

expect(anchorFileModel.map_file_uri).toEqual(mapFileUri);
expect(anchorFileModel.operations.create![0].suffix_data).toEqual({
delta_hash: createOperation.suffixData.deltaHash, recovery_commitment: createOperation.suffixData.recoveryCommitment
delta_hash: createOperation.suffixData.deltaHash, recovery_commitment: createOperation.suffixData.recoveryCommitment, type: undefined
});

// Verify recover operation.
Expand Down
2 changes: 1 addition & 1 deletion tests/core/TransactionProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ describe('TransactionProcessor', () => {

const paidBatchSize = 2;
const downloadedAnchorFile = await transactionProcessor['downloadAndVerifyAnchorFile'](mockTransaction, 'mock_hash', paidBatchSize);
expect(downloadedAnchorFile.model).toEqual(mockAnchorFileModel);
expect(JSON.stringify(downloadedAnchorFile.model)).toEqual(JSON.stringify(mockAnchorFileModel));
done();
});
});
Expand Down

0 comments on commit bbd40df

Please sign in to comment.