@dashevo/wasm-dpp 0.23.0-dev.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +5 -0
- package/.editorconfig +13 -0
- package/.eslintignore +1 -0
- package/.eslintrc +15 -0
- package/.mocharc.yml +4 -0
- package/CHANGELOG.md +397 -0
- package/LICENSE +20 -0
- package/README.md +37 -0
- package/karma.conf.js +53 -0
- package/package.json +107 -0
- package/test/.eslintrc +12 -0
- package/test/integration/DashPlatformProtocol.spec.js +53 -0
- package/test/integration/ajv/keywords/byteArray/addByteArrayKeyword.spec.js +148 -0
- package/test/integration/dataContract/DataContractFacade.spec.js +84 -0
- package/test/integration/dataContract/stateTransition/DataContractCreateTransition/validation/basic/validateDataContractCreateTransitionBasicFactory.spec.js +357 -0
- package/test/integration/dataContract/stateTransition/DataContractUpdateTransition/validation/basic/validateDataContractUpdateTransitionBasicFactory.spec.js +444 -0
- package/test/integration/dataContract/validation/validateDataContractFactory.spec.js +2191 -0
- package/test/integration/document/Document.spec.js +90 -0
- package/test/integration/document/DocumentFacade.spec.js +155 -0
- package/test/integration/document/stateTransition/DocumentsBatchTransition/DocumentBatchTransition.spec.js +83 -0
- package/test/integration/document/stateTransition/DocumentsBatchTransition/validation/basic/validateDocumentsBatchTransitionBasicFactory.spec.js +1146 -0
- package/test/integration/document/stateTransition/DocumentsBatchTransition/validation/state/executeDataTriggersFactory.spec.js +294 -0
- package/test/integration/document/validation/validateDocumentFactory.spec.js +447 -0
- package/test/integration/error/consensus/createConsensusError.spec.js +21 -0
- package/test/integration/identity/IdentityFacade.spec.js +187 -0
- package/test/integration/identity/stateTransition/IdentityCreateTransition/validation/basic/validateIdentityCreateTransitionBasicFactory.spec.js +422 -0
- package/test/integration/identity/stateTransition/IdentityTopUpTransition/validation/basic/validateIdentityTopUpTransitionBasicFactory.spec.js +361 -0
- package/test/integration/identity/stateTransition/IdentityUpdateTransition/validation/basic/validateIdentityUpdateTransitionBasicFactory.spec.js +595 -0
- package/test/integration/identity/stateTransition/IdentityUpdateTransition/validation/state/validateIdentityUpdateTransitionStateFactory.spec.js +299 -0
- package/test/integration/identity/stateTransition/IdentityUpdateTransition/validation/state/validatePublicKeysState.spec.js +66 -0
- package/test/integration/identity/stateTransition/assetLockProof/chain/validateChainAssetLockProofStructureFactory.spec.js +415 -0
- package/test/integration/identity/stateTransition/assetLockProof/fetchAssetLockTransactionOutputFactory.spec.js +134 -0
- package/test/integration/identity/stateTransition/assetLockProof/instant/validateInstantAssetLockProofStructureFactory.spec.js +401 -0
- package/test/integration/identity/stateTransition/assetLockProof/validateAssetLockTransactionFactory.spec.js +205 -0
- package/test/integration/identity/stateTransition/validatePublicKeySignaturesFactory.spec.js +127 -0
- package/test/integration/identity/validation/validateIdentityFactory.spec.js +370 -0
- package/test/integration/identity/validation/validatePublicKeysFactory.spec.js +497 -0
- package/test/integration/stateTransition/AbstractStateTransitionIdentitySigned.spec.js +507 -0
- package/test/integration/stateTransition/StateTransitionFacade.spec.js +418 -0
- package/test/integration/stateTransition/calculateStateTransitionFee.spec.js +35 -0
- package/test/integration/util/generateEntropy.spec.js +17 -0
- package/test/integration/util/serializer.spec.js +24 -0
- package/test/unit/DashPlatformProtocol.spec.js +79 -0
- package/test/unit/Identifier.spec.js +123 -0
- package/test/unit/Metadata.spec.js +37 -0
- package/test/unit/dataContract/DataContract.spec.js +407 -0
- package/test/unit/dataContract/DataContractFactory.spec.js +178 -0
- package/test/unit/dataContract/createDataContract.spec.js +57 -0
- package/test/unit/dataContract/errors/InvalidDataContractError.spec.js +46 -0
- package/test/unit/dataContract/generateDataContractId.spec.js +19 -0
- package/test/unit/dataContract/getBinaryPropertiesFromSchema.spec.js +103 -0
- package/test/unit/dataContract/getPropertyDefinitionByPath.spec.js +84 -0
- package/test/unit/dataContract/stateTransition/DataContractCreateTransition/DataContractCreateTransition.spec.js +160 -0
- package/test/unit/dataContract/stateTransition/DataContractCreateTransition/applyDataContractCreateTransitionFactory.spec.js +48 -0
- package/test/unit/dataContract/stateTransition/DataContractCreateTransition/validation/state/validateDataContractCreateTransitionStateFactory.spec.js +85 -0
- package/test/unit/dataContract/stateTransition/DataContractUpdateTransition/DataContractUpdateTransition.spec.js +158 -0
- package/test/unit/dataContract/stateTransition/DataContractUpdateTransition/applyDataContractUpdateTransitionFactory.spec.js +47 -0
- package/test/unit/dataContract/stateTransition/DataContractUpdateTransition/validation/basic/validateIndicesAreBackwardCompatible.spec.js +121 -0
- package/test/unit/dataContract/stateTransition/DataContractUpdateTransition/validation/state/validateDataContractUpdateTransitionStateFactory.spec.js +111 -0
- package/test/unit/dataContract/validation/validateDataContractMaxDepthFactory.spec.js +93 -0
- package/test/unit/dataContract/validation/validateDataContractPatternsFactory.spec.js +64 -0
- package/test/unit/dataTrigger/DataTrigger.spec.js +132 -0
- package/test/unit/dataTrigger/DataTriggerExecutionContext.spec.js +29 -0
- package/test/unit/dataTrigger/dashpayDataTriggers/createContactRequestDataTrigger.spec.js +109 -0
- package/test/unit/dataTrigger/dpnsTriggers/createDomainDataTrigger.spec.js +382 -0
- package/test/unit/dataTrigger/featureFlagDataTriggers/createFeatureFlagDataTrigger.spec.js +101 -0
- package/test/unit/dataTrigger/getDataTriggersFactory.spec.js +113 -0
- package/test/unit/dataTrigger/rejectDataTrigger.spec.js +51 -0
- package/test/unit/dataTrigger/rewardShareDataTriggers/createMasternodeRewardSharesDataTrigger.spec.js +200 -0
- package/test/unit/decodeProtocolEntityFactory.spec.js +75 -0
- package/test/unit/document/Document.spec.js +444 -0
- package/test/unit/document/DocumentFactory.spec.js +376 -0
- package/test/unit/document/errors/InvalidDocumentError.spec.js +46 -0
- package/test/unit/document/fetchAndValidateDataContractFactory.spec.js +66 -0
- package/test/unit/document/stateTransition/DocumetsBatchTransition/DocumentsBatchTransition.spec.js +168 -0
- package/test/unit/document/stateTransition/DocumetsBatchTransition/applyDocumentsBatchTransitionFactory.spec.js +171 -0
- package/test/unit/document/stateTransition/DocumetsBatchTransition/documentTransition/DocumentCreateTransition.spec.js +25 -0
- package/test/unit/document/stateTransition/DocumetsBatchTransition/validation/basic/findDuplicatesById.spec.js +31 -0
- package/test/unit/document/stateTransition/DocumetsBatchTransition/validation/basic/findDuplicatesByIndices.spec.js +122 -0
- package/test/unit/document/stateTransition/DocumetsBatchTransition/validation/basic/validatePartialCompoundIndices.spec.js +75 -0
- package/test/unit/document/stateTransition/DocumetsBatchTransition/validation/state/fetchDocumentsFactory.spec.js +128 -0
- package/test/unit/document/stateTransition/DocumetsBatchTransition/validation/state/validateDocumentsBatchTransitionStateFactory.spec.js +811 -0
- package/test/unit/document/stateTransition/DocumetsBatchTransition/validation/state/validateDocumentsUniquenessByIndicesFactory.spec.js +279 -0
- package/test/unit/errors/consensus/codes.spec.js +102 -0
- package/test/unit/identity/Identity.spec.js +243 -0
- package/test/unit/identity/IdentityFactory.spec.js +274 -0
- package/test/unit/identity/IdentityPublicKey.spec.js +273 -0
- package/test/unit/identity/creditsConverter.spec.js +34 -0
- package/test/unit/identity/errors/InvalidIdentityError.spec.js +46 -0
- package/test/unit/identity/stateTransition/IdentityCreateTransition/IdentityCreateTransition.spec.js +184 -0
- package/test/unit/identity/stateTransition/IdentityCreateTransition/applyIdentityCreateTransitionFactory.spec.js +90 -0
- package/test/unit/identity/stateTransition/IdentityCreateTransition/validation/state/validateIdentityCreateTransitionStateFactory.spec.js +71 -0
- package/test/unit/identity/stateTransition/IdentityTopUpTransition/IdentityTopUpTransition.spec.js +137 -0
- package/test/unit/identity/stateTransition/IdentityTopUpTransition/applyIdentityTopUpTransitionFactory.spec.js +108 -0
- package/test/unit/identity/stateTransition/IdentityTopUpTransition/validation/state/validateIdentityTopUpTransitionStateFactory.spec.js +22 -0
- package/test/unit/identity/stateTransition/IdentityUpdateTransition/IdentityUpdateTransition.spec.js +219 -0
- package/test/unit/identity/stateTransition/IdentityUpdateTransition/applyIdentityUpdateTransitionFactory.spec.js +149 -0
- package/test/unit/identity/stateTransition/assetLockProof/createAssetLockProofInstance.spec.js +21 -0
- package/test/unit/identity/stateTransition/assetLockProof/fetchAssetLockPublicKeyHashFactory.spec.js +36 -0
- package/test/unit/identity/validation/validateRequiredPurposeAndSecurityLevelFactory.spec.js +47 -0
- package/test/unit/stateTransition/StateTransitionFactory.spec.js +160 -0
- package/test/unit/stateTransition/createStateTransitionFactory.spec.js +72 -0
- package/test/unit/stateTransition/errors/InvalidStateTransitionError.spec.js +51 -0
- package/test/unit/stateTransition/validation/validateStateTransitionBasicFactory.spec.js +138 -0
- package/test/unit/stateTransition/validation/validateStateTransitionFeeFactory.spec.js +372 -0
- package/test/unit/stateTransition/validation/validateStateTransitionIdentitySignatureFactory.spec.js +339 -0
- package/test/unit/stateTransition/validation/validateStateTransitionKeySignatureFactory.spec.js +86 -0
- package/test/unit/stateTransition/validation/validateStateTransitionStateFactory.spec.js +89 -0
- package/test/unit/version/validateProtocolVersionFactory.spec.js +80 -0
- package/webpack.config.js +49 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
const { PrivateKey, crypto: { Hash } } = require('@dashevo/dashcore-lib');
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
|
|
5
|
+
const getIdentityCreateTransitionFixture = require('../../../../lib/test/fixtures/getIdentityCreateTransitionFixture');
|
|
6
|
+
const IdentityPublicKey = require('../../../../lib/identity/IdentityPublicKey');
|
|
7
|
+
const BlsSignatures = require('../../../../lib/bls/bls');
|
|
8
|
+
const validatePublicKeySignaturesFactory = require('../../../../lib/identity/stateTransition/validatePublicKeySignaturesFactory');
|
|
9
|
+
const ValidationResult = require('../../../../lib/validation/ValidationResult');
|
|
10
|
+
const { expectValidationError } = require('../../../../lib/test/expect/expectError');
|
|
11
|
+
const InvalidIdentityKeySignatureError = require('../../../../lib/errors/consensus/basic/identity/InvalidIdentityKeySignatureError');
|
|
12
|
+
|
|
13
|
+
describe('validatePublicKeySignaturesFactory', () => {
|
|
14
|
+
let identityCreateTransition;
|
|
15
|
+
let rawIdentityCreateTransition;
|
|
16
|
+
let validatePublicKeySignatures;
|
|
17
|
+
|
|
18
|
+
beforeEach(async function beforeEach() {
|
|
19
|
+
identityCreateTransition = getIdentityCreateTransitionFixture();
|
|
20
|
+
|
|
21
|
+
const privateKey1 = new PrivateKey();
|
|
22
|
+
const publicKey1 = privateKey1.toPublicKey();
|
|
23
|
+
|
|
24
|
+
const identityPublicKey1 = new IdentityPublicKey({
|
|
25
|
+
id: 0,
|
|
26
|
+
type: IdentityPublicKey.TYPES.ECDSA_SECP256K1,
|
|
27
|
+
data: publicKey1.toBuffer(),
|
|
28
|
+
purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION,
|
|
29
|
+
securityLevel: IdentityPublicKey.SECURITY_LEVELS.MASTER,
|
|
30
|
+
readOnly: false,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const privateKey2 = new PrivateKey();
|
|
34
|
+
const publicKey2 = privateKey2.toPublicKey();
|
|
35
|
+
|
|
36
|
+
const identityPublicKey2 = new IdentityPublicKey({
|
|
37
|
+
id: 1,
|
|
38
|
+
type: IdentityPublicKey.TYPES.ECDSA_HASH160,
|
|
39
|
+
data: Hash.sha256ripemd160(publicKey2.toBuffer()),
|
|
40
|
+
purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION,
|
|
41
|
+
securityLevel: IdentityPublicKey.SECURITY_LEVELS.CRITICAL,
|
|
42
|
+
readOnly: false,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const { PrivateKey: BlsPrivateKey } = await BlsSignatures.getInstance();
|
|
46
|
+
|
|
47
|
+
const randomBytes = new Uint8Array(crypto.randomBytes(256));
|
|
48
|
+
const privateKey3 = BlsPrivateKey.fromBytes(randomBytes, true);
|
|
49
|
+
// blsPrivateKeyHex = Buffer.from(blsPrivateKey.serialize()).toString('hex');
|
|
50
|
+
const publicKey3 = privateKey3.getPublicKey();
|
|
51
|
+
|
|
52
|
+
const identityPublicKey3 = new IdentityPublicKey({
|
|
53
|
+
id: 2,
|
|
54
|
+
type: IdentityPublicKey.TYPES.BLS12_381,
|
|
55
|
+
data: Buffer.from(publicKey3.serialize()),
|
|
56
|
+
purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION,
|
|
57
|
+
securityLevel: IdentityPublicKey.SECURITY_LEVELS.CRITICAL,
|
|
58
|
+
readOnly: false,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
identityCreateTransition.setPublicKeys([
|
|
62
|
+
identityPublicKey1,
|
|
63
|
+
identityPublicKey2,
|
|
64
|
+
identityPublicKey3,
|
|
65
|
+
]);
|
|
66
|
+
|
|
67
|
+
await identityCreateTransition.signByPrivateKey(
|
|
68
|
+
privateKey1,
|
|
69
|
+
IdentityPublicKey.TYPES.ECDSA_SECP256K1,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const signature1 = identityCreateTransition.getSignature();
|
|
73
|
+
|
|
74
|
+
await identityCreateTransition.signByPrivateKey(
|
|
75
|
+
privateKey2,
|
|
76
|
+
IdentityPublicKey.TYPES.ECDSA_HASH160,
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const signature2 = identityCreateTransition.getSignature();
|
|
80
|
+
|
|
81
|
+
await identityCreateTransition.signByPrivateKey(
|
|
82
|
+
Buffer.from(privateKey3.serialize()),
|
|
83
|
+
IdentityPublicKey.TYPES.BLS12_381,
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
const signature3 = identityCreateTransition.getSignature();
|
|
87
|
+
|
|
88
|
+
identityPublicKey1.setSignature(signature1);
|
|
89
|
+
identityPublicKey2.setSignature(signature2);
|
|
90
|
+
identityPublicKey3.setSignature(signature3);
|
|
91
|
+
|
|
92
|
+
rawIdentityCreateTransition = identityCreateTransition.toObject();
|
|
93
|
+
|
|
94
|
+
const createStateTransitionMock = this.sinonSandbox.stub().resolves(identityCreateTransition);
|
|
95
|
+
|
|
96
|
+
validatePublicKeySignatures = validatePublicKeySignaturesFactory(createStateTransitionMock);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('should return InvalidIdentityKeySignatureError if signature is not valid', async () => {
|
|
100
|
+
const rawPublicKey2 = rawIdentityCreateTransition.publicKeys[1];
|
|
101
|
+
|
|
102
|
+
rawPublicKey2.signature = crypto.randomBytes(65);
|
|
103
|
+
|
|
104
|
+
const result = await validatePublicKeySignatures(
|
|
105
|
+
rawIdentityCreateTransition,
|
|
106
|
+
rawIdentityCreateTransition.publicKeys,
|
|
107
|
+
identityCreateTransition.getExecutionContext(),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
expectValidationError(result, InvalidIdentityKeySignatureError);
|
|
111
|
+
|
|
112
|
+
const error = result.getFirstError();
|
|
113
|
+
|
|
114
|
+
expect(error.getPublicKeyId()).to.equals(rawPublicKey2.id);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('should return valid result', async () => {
|
|
118
|
+
const result = await validatePublicKeySignatures(
|
|
119
|
+
rawIdentityCreateTransition,
|
|
120
|
+
rawIdentityCreateTransition.publicKeys,
|
|
121
|
+
identityCreateTransition.getExecutionContext(),
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
expect(result).to.be.instanceOf(ValidationResult);
|
|
125
|
+
expect(result.isValid()).to.be.true();
|
|
126
|
+
});
|
|
127
|
+
});
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
const { getRE2Class } = require('@dashevo/wasm-re2');
|
|
2
|
+
|
|
3
|
+
const createAjv = require('../../../../lib/ajv/createAjv');
|
|
4
|
+
|
|
5
|
+
const getIdentityFixture = require('../../../../lib/test/fixtures/getIdentityFixture');
|
|
6
|
+
|
|
7
|
+
const JsonSchemaValidator = require(
|
|
8
|
+
'../../../../lib/validation/JsonSchemaValidator',
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
const { expectValidationError, expectJsonSchemaError } = require(
|
|
12
|
+
'../../../../lib/test/expect/expectError',
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
const validateIdentityFactory = require(
|
|
16
|
+
'../../../../lib/identity/validation/validateIdentityFactory',
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
const JsonSchemaError = require(
|
|
20
|
+
'../../../../lib/errors/consensus/basic/JsonSchemaError',
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const ValidationResult = require('../../../../lib/validation/ValidationResult');
|
|
24
|
+
const SomeConsensusError = require('../../../../lib/test/mocks/SomeConsensusError');
|
|
25
|
+
|
|
26
|
+
describe('validateIdentityFactory', () => {
|
|
27
|
+
let rawIdentity;
|
|
28
|
+
let validateIdentity;
|
|
29
|
+
let identity;
|
|
30
|
+
let validatePublicKeysMock;
|
|
31
|
+
let validateProtocolVersionMock;
|
|
32
|
+
|
|
33
|
+
beforeEach(async function beforeEach() {
|
|
34
|
+
const RE2 = await getRE2Class();
|
|
35
|
+
const ajv = createAjv(RE2);
|
|
36
|
+
|
|
37
|
+
const schemaValidator = new JsonSchemaValidator(ajv);
|
|
38
|
+
|
|
39
|
+
validatePublicKeysMock = this.sinonSandbox.stub().returns(new ValidationResult());
|
|
40
|
+
|
|
41
|
+
validateProtocolVersionMock = this.sinonSandbox.stub().returns(new ValidationResult());
|
|
42
|
+
|
|
43
|
+
validateIdentity = validateIdentityFactory(
|
|
44
|
+
schemaValidator,
|
|
45
|
+
validatePublicKeysMock,
|
|
46
|
+
validateProtocolVersionMock,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
identity = getIdentityFixture();
|
|
50
|
+
|
|
51
|
+
rawIdentity = identity.toObject();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('protocolVersion', () => {
|
|
55
|
+
it('should be present', async () => {
|
|
56
|
+
delete rawIdentity.protocolVersion;
|
|
57
|
+
|
|
58
|
+
const result = validateIdentity(rawIdentity);
|
|
59
|
+
|
|
60
|
+
expectJsonSchemaError(result);
|
|
61
|
+
|
|
62
|
+
const [error] = result.getErrors();
|
|
63
|
+
|
|
64
|
+
expect(error.getInstancePath()).to.equal('');
|
|
65
|
+
expect(error.getKeyword()).to.equal('required');
|
|
66
|
+
expect(error.getParams().missingProperty).to.equal('protocolVersion');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should be an integer', async () => {
|
|
70
|
+
rawIdentity.protocolVersion = '1';
|
|
71
|
+
|
|
72
|
+
const result = validateIdentity(rawIdentity);
|
|
73
|
+
|
|
74
|
+
expectJsonSchemaError(result);
|
|
75
|
+
|
|
76
|
+
const [error] = result.getErrors();
|
|
77
|
+
|
|
78
|
+
expect(error.getInstancePath()).to.equal('/protocolVersion');
|
|
79
|
+
expect(error.getKeyword()).to.equal('type');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should be valid', async () => {
|
|
83
|
+
rawIdentity.protocolVersion = -1;
|
|
84
|
+
|
|
85
|
+
const protocolVersionError = new SomeConsensusError('test');
|
|
86
|
+
const protocolVersionResult = new ValidationResult([
|
|
87
|
+
protocolVersionError,
|
|
88
|
+
]);
|
|
89
|
+
|
|
90
|
+
validateProtocolVersionMock.returns(protocolVersionResult);
|
|
91
|
+
|
|
92
|
+
const result = validateIdentity(rawIdentity);
|
|
93
|
+
|
|
94
|
+
expectValidationError(result, SomeConsensusError);
|
|
95
|
+
|
|
96
|
+
const [error] = result.getErrors();
|
|
97
|
+
|
|
98
|
+
expect(error).to.equal(protocolVersionError);
|
|
99
|
+
|
|
100
|
+
expect(validateProtocolVersionMock).to.be.calledOnceWith(
|
|
101
|
+
rawIdentity.protocolVersion,
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe('id', () => {
|
|
107
|
+
it('should be present', () => {
|
|
108
|
+
rawIdentity.id = undefined;
|
|
109
|
+
|
|
110
|
+
const result = validateIdentity(rawIdentity);
|
|
111
|
+
|
|
112
|
+
expectJsonSchemaError(result);
|
|
113
|
+
|
|
114
|
+
const [error] = result.getErrors();
|
|
115
|
+
|
|
116
|
+
expect(error.getInstancePath()).to.equal('');
|
|
117
|
+
expect(error.getParams().missingProperty).to.equal('id');
|
|
118
|
+
expect(error.getKeyword()).to.equal('required');
|
|
119
|
+
|
|
120
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('should be a byte array', () => {
|
|
124
|
+
rawIdentity.id = new Array(32).fill('string');
|
|
125
|
+
|
|
126
|
+
const result = validateIdentity(rawIdentity);
|
|
127
|
+
|
|
128
|
+
expectJsonSchemaError(result, 2);
|
|
129
|
+
|
|
130
|
+
const [error, byteArrayError] = result.getErrors();
|
|
131
|
+
|
|
132
|
+
expect(error.getInstancePath()).to.equal('/id/0');
|
|
133
|
+
expect(error.getKeyword()).to.equal('type');
|
|
134
|
+
|
|
135
|
+
expect(byteArrayError.getKeyword()).to.equal('byteArray');
|
|
136
|
+
|
|
137
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('should not be less than 32 bytes', () => {
|
|
141
|
+
rawIdentity.id = Buffer.alloc(31);
|
|
142
|
+
|
|
143
|
+
const result = validateIdentity(rawIdentity);
|
|
144
|
+
|
|
145
|
+
expectJsonSchemaError(result);
|
|
146
|
+
|
|
147
|
+
const [error] = result.getErrors();
|
|
148
|
+
|
|
149
|
+
expect(error.getKeyword()).to.equal('minItems');
|
|
150
|
+
expect(error.getInstancePath()).to.equal('/id');
|
|
151
|
+
|
|
152
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('should not be more than 32 bytes', () => {
|
|
156
|
+
rawIdentity.id = Buffer.alloc(33);
|
|
157
|
+
|
|
158
|
+
const result = validateIdentity(rawIdentity);
|
|
159
|
+
|
|
160
|
+
expectJsonSchemaError(result);
|
|
161
|
+
|
|
162
|
+
const [error] = result.getErrors();
|
|
163
|
+
|
|
164
|
+
expect(error.getKeyword()).to.equal('maxItems');
|
|
165
|
+
expect(error.getInstancePath()).to.equal('/id');
|
|
166
|
+
|
|
167
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe('balance', () => {
|
|
172
|
+
it('should be present', async () => {
|
|
173
|
+
rawIdentity.balance = undefined;
|
|
174
|
+
|
|
175
|
+
const result = validateIdentity(rawIdentity);
|
|
176
|
+
|
|
177
|
+
expectJsonSchemaError(result);
|
|
178
|
+
|
|
179
|
+
const [error] = result.getErrors();
|
|
180
|
+
|
|
181
|
+
expect(error.getInstancePath()).to.equal('');
|
|
182
|
+
expect(error.getParams().missingProperty).to.equal('balance');
|
|
183
|
+
expect(error.getKeyword()).to.equal('required');
|
|
184
|
+
|
|
185
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('should be an integer', async () => {
|
|
189
|
+
rawIdentity.balance = 1.2;
|
|
190
|
+
|
|
191
|
+
const result = validateIdentity(rawIdentity);
|
|
192
|
+
|
|
193
|
+
expectJsonSchemaError(result);
|
|
194
|
+
|
|
195
|
+
const [error] = result.getErrors();
|
|
196
|
+
|
|
197
|
+
expect(error.getKeyword()).to.equal('type');
|
|
198
|
+
expect(error.getInstancePath()).to.equal('/balance');
|
|
199
|
+
|
|
200
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('should be greater or equal 0', async () => {
|
|
204
|
+
rawIdentity.balance = -1;
|
|
205
|
+
|
|
206
|
+
let result = validateIdentity(rawIdentity);
|
|
207
|
+
|
|
208
|
+
expectJsonSchemaError(result);
|
|
209
|
+
|
|
210
|
+
const [error] = result.getErrors();
|
|
211
|
+
|
|
212
|
+
expect(error.getKeyword()).to.equal('minimum');
|
|
213
|
+
expect(error.getInstancePath()).to.equal('/balance');
|
|
214
|
+
|
|
215
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
216
|
+
|
|
217
|
+
rawIdentity.balance = 0;
|
|
218
|
+
|
|
219
|
+
result = validateIdentity(rawIdentity);
|
|
220
|
+
|
|
221
|
+
expect(result.isValid()).to.be.true();
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
describe('publicKeys', () => {
|
|
226
|
+
it('should be present', () => {
|
|
227
|
+
rawIdentity.publicKeys = undefined;
|
|
228
|
+
|
|
229
|
+
const result = validateIdentity(rawIdentity);
|
|
230
|
+
|
|
231
|
+
expectJsonSchemaError(result);
|
|
232
|
+
|
|
233
|
+
const [error] = result.getErrors();
|
|
234
|
+
|
|
235
|
+
expect(error.getInstancePath()).to.equal('');
|
|
236
|
+
expect(error.getParams().missingProperty).to.equal('publicKeys');
|
|
237
|
+
expect(error.getKeyword()).to.equal('required');
|
|
238
|
+
|
|
239
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('should be an array', () => {
|
|
243
|
+
rawIdentity.publicKeys = 1;
|
|
244
|
+
|
|
245
|
+
const result = validateIdentity(rawIdentity);
|
|
246
|
+
|
|
247
|
+
expectValidationError(result, JsonSchemaError, 1);
|
|
248
|
+
|
|
249
|
+
const [error] = result.getErrors();
|
|
250
|
+
|
|
251
|
+
expect(error.getCode()).to.equal(1005);
|
|
252
|
+
expect(error.getInstancePath()).to.equal('/publicKeys');
|
|
253
|
+
expect(error.getKeyword()).to.equal('type');
|
|
254
|
+
|
|
255
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('should not be empty', () => {
|
|
259
|
+
rawIdentity.publicKeys = [];
|
|
260
|
+
|
|
261
|
+
const result = validateIdentity(rawIdentity);
|
|
262
|
+
|
|
263
|
+
expectJsonSchemaError(result);
|
|
264
|
+
|
|
265
|
+
const [error] = result.getErrors();
|
|
266
|
+
|
|
267
|
+
expect(error.getKeyword()).to.equal('minItems');
|
|
268
|
+
expect(error.getInstancePath()).to.equal('/publicKeys');
|
|
269
|
+
|
|
270
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('should be unique', async () => {
|
|
274
|
+
rawIdentity.publicKeys.push(rawIdentity.publicKeys[0]);
|
|
275
|
+
|
|
276
|
+
const result = validateIdentity(rawIdentity);
|
|
277
|
+
|
|
278
|
+
expectJsonSchemaError(result);
|
|
279
|
+
|
|
280
|
+
const [error] = result.getErrors();
|
|
281
|
+
|
|
282
|
+
expect(error.getKeyword()).to.equal('uniqueItems');
|
|
283
|
+
expect(error.getInstancePath()).to.equal('/publicKeys');
|
|
284
|
+
|
|
285
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it('should throw an error if publicKeys have more than 100 keys', () => {
|
|
289
|
+
const [key] = rawIdentity.publicKeys;
|
|
290
|
+
|
|
291
|
+
rawIdentity.publicKeys = [];
|
|
292
|
+
for (let i = 0; i < 101; i++) {
|
|
293
|
+
rawIdentity.publicKeys.push(key);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const result = validateIdentity(rawIdentity);
|
|
297
|
+
|
|
298
|
+
expectJsonSchemaError(result);
|
|
299
|
+
|
|
300
|
+
const [error] = result.getErrors();
|
|
301
|
+
|
|
302
|
+
expect(error.getKeyword()).to.equal('maxItems');
|
|
303
|
+
expect(error.getInstancePath()).to.equal('/publicKeys');
|
|
304
|
+
|
|
305
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
describe('revision', () => {
|
|
310
|
+
it('should be present', async () => {
|
|
311
|
+
rawIdentity.revision = undefined;
|
|
312
|
+
|
|
313
|
+
const result = validateIdentity(rawIdentity);
|
|
314
|
+
|
|
315
|
+
expectJsonSchemaError(result);
|
|
316
|
+
|
|
317
|
+
const [error] = result.getErrors();
|
|
318
|
+
|
|
319
|
+
expect(error.getInstancePath()).to.equal('');
|
|
320
|
+
expect(error.getParams().missingProperty).to.equal('revision');
|
|
321
|
+
expect(error.getKeyword()).to.equal('required');
|
|
322
|
+
|
|
323
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('should be an integer', async () => {
|
|
327
|
+
rawIdentity.revision = 1.2;
|
|
328
|
+
|
|
329
|
+
const result = validateIdentity(rawIdentity);
|
|
330
|
+
|
|
331
|
+
expectJsonSchemaError(result);
|
|
332
|
+
|
|
333
|
+
const [error] = result.getErrors();
|
|
334
|
+
|
|
335
|
+
expect(error.getKeyword()).to.equal('type');
|
|
336
|
+
expect(error.getInstancePath()).to.equal('/revision');
|
|
337
|
+
|
|
338
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it('should be greater or equal 0', async () => {
|
|
342
|
+
rawIdentity.revision = -1;
|
|
343
|
+
|
|
344
|
+
let result = validateIdentity(rawIdentity);
|
|
345
|
+
|
|
346
|
+
expectJsonSchemaError(result);
|
|
347
|
+
|
|
348
|
+
const [error] = result.getErrors();
|
|
349
|
+
|
|
350
|
+
expect(error.getKeyword()).to.equal('minimum');
|
|
351
|
+
expect(error.getInstancePath()).to.equal('/revision');
|
|
352
|
+
|
|
353
|
+
expect(validatePublicKeysMock).to.not.be.called();
|
|
354
|
+
|
|
355
|
+
rawIdentity.revision = 0;
|
|
356
|
+
|
|
357
|
+
result = validateIdentity(rawIdentity);
|
|
358
|
+
|
|
359
|
+
expect(result.isValid()).to.be.true();
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it('should return valid result if a raw identity is valid', () => {
|
|
364
|
+
const result = validateIdentity(rawIdentity);
|
|
365
|
+
|
|
366
|
+
expect(validatePublicKeysMock).to.be.calledOnceWithExactly(rawIdentity.publicKeys);
|
|
367
|
+
|
|
368
|
+
expect(result.isValid()).to.be.true();
|
|
369
|
+
});
|
|
370
|
+
});
|