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

feat(ref-imp): #781 - make jcs long form do size check on delta #866

Merged
merged 5 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/core/versions/latest/Delta.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ErrorCode from './ErrorCode';
import ProtocolParameters from './ProtocolParameters';
import SidetreeError from '../../../common/SidetreeError';
import JsonCanonicalizer from './util/JsonCanonicalizer';
isaacJChen marked this conversation as resolved.
Show resolved Hide resolved

/**
* Class containing reusable operation delta functionalities.
Expand All @@ -9,6 +10,7 @@ export default class Delta {

/**
* Validates size of the encoded delta string.
* TODO: SIP 2 #781 delete this and use validateDeltaSize only
* @throws `SidetreeError` if fails validation.
*/
public static validateEncodedDeltaSize (encodedDelta: string) {
Expand All @@ -19,4 +21,16 @@ export default class Delta {
throw new SidetreeError(ErrorCode.DeltaExceedsMaximumSize, errorMessage);
}
}

/**
* Validates size of the delta object
*/
public static validateDeltaSize (delta: object) {
const size = Buffer.byteLength(JsonCanonicalizer.canonicalizeAsBuffer(delta));
if (size > ProtocolParameters.maxDeltaSizeInBytes) {
const errorMessage = `${size} bytes of 'delta' exceeded limit of ${ProtocolParameters.maxDeltaSizeInBytes} bytes.`;
console.info(errorMessage);
throw new SidetreeError(ErrorCode.DeltaExceedsMaximumSize, errorMessage);
}
}
}
1 change: 1 addition & 0 deletions lib/core/versions/latest/Did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export default class Did {
}

Did.validateInitialState(initialStateEncodedJcs, initialStateObject);
Delta.validateDeltaSize(initialStateObject.delta);

const createOperationRequest = {
type: OperationType.Create,
Expand Down
2 changes: 1 addition & 1 deletion lib/core/versions/latest/protocol-parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"maxNumberOfOperationsPerTransactionTime": 600000,
"maxNumberOfTransactionsPerTransactionTime": 300,
"maxOperationsPerBatch": 10000,
"maxDeltaSizeInBytes": 1000,
"maxDeltaSizeInBytes": 2000,
isaacJChen marked this conversation as resolved.
Show resolved Hide resolved
"maxNumberOfOperationsForNoValueTimeLock": 100,
"maxWriterLockIdInBytes": 200,
"normalizedFeeToPerOperationFeeMultiplier": 0.01,
Expand Down
14 changes: 14 additions & 0 deletions tests/core/Did.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Did from '../../lib/core/versions/latest/Did';
import Encoder from '../../lib/core/versions/latest/Encoder';
import ErrorCode from '../../lib/core/versions/latest/ErrorCode';
import JasmineSidetreeErrorValidator from '../JasmineSidetreeErrorValidator';
import JsonCanonicalizer from '../../lib/core/versions/latest/util/JsonCanonicalizer';
import OperationGenerator from '../generators/OperationGenerator';
import SidetreeError from '../../lib/common/SidetreeError';

Expand All @@ -27,6 +28,19 @@ describe('DID', async () => {
expect(e).toEqual(new SidetreeError(ErrorCode.DidInitialStateJcsIsNotJcs, 'Initial state object and JCS string mismatch.'));
}
});

it('should throw sidetree error if delta exceeds size limit', () => {
const largeData = crypto.randomBytes(2000).toString('hex');// Intentionally exceeding max size.
const largeDelta = { data: largeData };
const testInitialState = Encoder.encode(JsonCanonicalizer.canonicalizeAsBuffer({ suffix_data: 'some data', delta: largeDelta }));

try {
isaacJChen marked this conversation as resolved.
Show resolved Hide resolved
Did['constructCreateOperationFromEncodedJcs'](testInitialState);
fail('expect to throw sidetree error but did not');
} catch (e) {
expect(e.code).toEqual(ErrorCode.DeltaExceedsMaximumSize);
}
})
});

describe('create()', async () => {
Expand Down