@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.
Files changed (110) hide show
  1. package/.babelrc +5 -0
  2. package/.editorconfig +13 -0
  3. package/.eslintignore +1 -0
  4. package/.eslintrc +15 -0
  5. package/.mocharc.yml +4 -0
  6. package/CHANGELOG.md +397 -0
  7. package/LICENSE +20 -0
  8. package/README.md +37 -0
  9. package/karma.conf.js +53 -0
  10. package/package.json +107 -0
  11. package/test/.eslintrc +12 -0
  12. package/test/integration/DashPlatformProtocol.spec.js +53 -0
  13. package/test/integration/ajv/keywords/byteArray/addByteArrayKeyword.spec.js +148 -0
  14. package/test/integration/dataContract/DataContractFacade.spec.js +84 -0
  15. package/test/integration/dataContract/stateTransition/DataContractCreateTransition/validation/basic/validateDataContractCreateTransitionBasicFactory.spec.js +357 -0
  16. package/test/integration/dataContract/stateTransition/DataContractUpdateTransition/validation/basic/validateDataContractUpdateTransitionBasicFactory.spec.js +444 -0
  17. package/test/integration/dataContract/validation/validateDataContractFactory.spec.js +2191 -0
  18. package/test/integration/document/Document.spec.js +90 -0
  19. package/test/integration/document/DocumentFacade.spec.js +155 -0
  20. package/test/integration/document/stateTransition/DocumentsBatchTransition/DocumentBatchTransition.spec.js +83 -0
  21. package/test/integration/document/stateTransition/DocumentsBatchTransition/validation/basic/validateDocumentsBatchTransitionBasicFactory.spec.js +1146 -0
  22. package/test/integration/document/stateTransition/DocumentsBatchTransition/validation/state/executeDataTriggersFactory.spec.js +294 -0
  23. package/test/integration/document/validation/validateDocumentFactory.spec.js +447 -0
  24. package/test/integration/error/consensus/createConsensusError.spec.js +21 -0
  25. package/test/integration/identity/IdentityFacade.spec.js +187 -0
  26. package/test/integration/identity/stateTransition/IdentityCreateTransition/validation/basic/validateIdentityCreateTransitionBasicFactory.spec.js +422 -0
  27. package/test/integration/identity/stateTransition/IdentityTopUpTransition/validation/basic/validateIdentityTopUpTransitionBasicFactory.spec.js +361 -0
  28. package/test/integration/identity/stateTransition/IdentityUpdateTransition/validation/basic/validateIdentityUpdateTransitionBasicFactory.spec.js +595 -0
  29. package/test/integration/identity/stateTransition/IdentityUpdateTransition/validation/state/validateIdentityUpdateTransitionStateFactory.spec.js +299 -0
  30. package/test/integration/identity/stateTransition/IdentityUpdateTransition/validation/state/validatePublicKeysState.spec.js +66 -0
  31. package/test/integration/identity/stateTransition/assetLockProof/chain/validateChainAssetLockProofStructureFactory.spec.js +415 -0
  32. package/test/integration/identity/stateTransition/assetLockProof/fetchAssetLockTransactionOutputFactory.spec.js +134 -0
  33. package/test/integration/identity/stateTransition/assetLockProof/instant/validateInstantAssetLockProofStructureFactory.spec.js +401 -0
  34. package/test/integration/identity/stateTransition/assetLockProof/validateAssetLockTransactionFactory.spec.js +205 -0
  35. package/test/integration/identity/stateTransition/validatePublicKeySignaturesFactory.spec.js +127 -0
  36. package/test/integration/identity/validation/validateIdentityFactory.spec.js +370 -0
  37. package/test/integration/identity/validation/validatePublicKeysFactory.spec.js +497 -0
  38. package/test/integration/stateTransition/AbstractStateTransitionIdentitySigned.spec.js +507 -0
  39. package/test/integration/stateTransition/StateTransitionFacade.spec.js +418 -0
  40. package/test/integration/stateTransition/calculateStateTransitionFee.spec.js +35 -0
  41. package/test/integration/util/generateEntropy.spec.js +17 -0
  42. package/test/integration/util/serializer.spec.js +24 -0
  43. package/test/unit/DashPlatformProtocol.spec.js +79 -0
  44. package/test/unit/Identifier.spec.js +123 -0
  45. package/test/unit/Metadata.spec.js +37 -0
  46. package/test/unit/dataContract/DataContract.spec.js +407 -0
  47. package/test/unit/dataContract/DataContractFactory.spec.js +178 -0
  48. package/test/unit/dataContract/createDataContract.spec.js +57 -0
  49. package/test/unit/dataContract/errors/InvalidDataContractError.spec.js +46 -0
  50. package/test/unit/dataContract/generateDataContractId.spec.js +19 -0
  51. package/test/unit/dataContract/getBinaryPropertiesFromSchema.spec.js +103 -0
  52. package/test/unit/dataContract/getPropertyDefinitionByPath.spec.js +84 -0
  53. package/test/unit/dataContract/stateTransition/DataContractCreateTransition/DataContractCreateTransition.spec.js +160 -0
  54. package/test/unit/dataContract/stateTransition/DataContractCreateTransition/applyDataContractCreateTransitionFactory.spec.js +48 -0
  55. package/test/unit/dataContract/stateTransition/DataContractCreateTransition/validation/state/validateDataContractCreateTransitionStateFactory.spec.js +85 -0
  56. package/test/unit/dataContract/stateTransition/DataContractUpdateTransition/DataContractUpdateTransition.spec.js +158 -0
  57. package/test/unit/dataContract/stateTransition/DataContractUpdateTransition/applyDataContractUpdateTransitionFactory.spec.js +47 -0
  58. package/test/unit/dataContract/stateTransition/DataContractUpdateTransition/validation/basic/validateIndicesAreBackwardCompatible.spec.js +121 -0
  59. package/test/unit/dataContract/stateTransition/DataContractUpdateTransition/validation/state/validateDataContractUpdateTransitionStateFactory.spec.js +111 -0
  60. package/test/unit/dataContract/validation/validateDataContractMaxDepthFactory.spec.js +93 -0
  61. package/test/unit/dataContract/validation/validateDataContractPatternsFactory.spec.js +64 -0
  62. package/test/unit/dataTrigger/DataTrigger.spec.js +132 -0
  63. package/test/unit/dataTrigger/DataTriggerExecutionContext.spec.js +29 -0
  64. package/test/unit/dataTrigger/dashpayDataTriggers/createContactRequestDataTrigger.spec.js +109 -0
  65. package/test/unit/dataTrigger/dpnsTriggers/createDomainDataTrigger.spec.js +382 -0
  66. package/test/unit/dataTrigger/featureFlagDataTriggers/createFeatureFlagDataTrigger.spec.js +101 -0
  67. package/test/unit/dataTrigger/getDataTriggersFactory.spec.js +113 -0
  68. package/test/unit/dataTrigger/rejectDataTrigger.spec.js +51 -0
  69. package/test/unit/dataTrigger/rewardShareDataTriggers/createMasternodeRewardSharesDataTrigger.spec.js +200 -0
  70. package/test/unit/decodeProtocolEntityFactory.spec.js +75 -0
  71. package/test/unit/document/Document.spec.js +444 -0
  72. package/test/unit/document/DocumentFactory.spec.js +376 -0
  73. package/test/unit/document/errors/InvalidDocumentError.spec.js +46 -0
  74. package/test/unit/document/fetchAndValidateDataContractFactory.spec.js +66 -0
  75. package/test/unit/document/stateTransition/DocumetsBatchTransition/DocumentsBatchTransition.spec.js +168 -0
  76. package/test/unit/document/stateTransition/DocumetsBatchTransition/applyDocumentsBatchTransitionFactory.spec.js +171 -0
  77. package/test/unit/document/stateTransition/DocumetsBatchTransition/documentTransition/DocumentCreateTransition.spec.js +25 -0
  78. package/test/unit/document/stateTransition/DocumetsBatchTransition/validation/basic/findDuplicatesById.spec.js +31 -0
  79. package/test/unit/document/stateTransition/DocumetsBatchTransition/validation/basic/findDuplicatesByIndices.spec.js +122 -0
  80. package/test/unit/document/stateTransition/DocumetsBatchTransition/validation/basic/validatePartialCompoundIndices.spec.js +75 -0
  81. package/test/unit/document/stateTransition/DocumetsBatchTransition/validation/state/fetchDocumentsFactory.spec.js +128 -0
  82. package/test/unit/document/stateTransition/DocumetsBatchTransition/validation/state/validateDocumentsBatchTransitionStateFactory.spec.js +811 -0
  83. package/test/unit/document/stateTransition/DocumetsBatchTransition/validation/state/validateDocumentsUniquenessByIndicesFactory.spec.js +279 -0
  84. package/test/unit/errors/consensus/codes.spec.js +102 -0
  85. package/test/unit/identity/Identity.spec.js +243 -0
  86. package/test/unit/identity/IdentityFactory.spec.js +274 -0
  87. package/test/unit/identity/IdentityPublicKey.spec.js +273 -0
  88. package/test/unit/identity/creditsConverter.spec.js +34 -0
  89. package/test/unit/identity/errors/InvalidIdentityError.spec.js +46 -0
  90. package/test/unit/identity/stateTransition/IdentityCreateTransition/IdentityCreateTransition.spec.js +184 -0
  91. package/test/unit/identity/stateTransition/IdentityCreateTransition/applyIdentityCreateTransitionFactory.spec.js +90 -0
  92. package/test/unit/identity/stateTransition/IdentityCreateTransition/validation/state/validateIdentityCreateTransitionStateFactory.spec.js +71 -0
  93. package/test/unit/identity/stateTransition/IdentityTopUpTransition/IdentityTopUpTransition.spec.js +137 -0
  94. package/test/unit/identity/stateTransition/IdentityTopUpTransition/applyIdentityTopUpTransitionFactory.spec.js +108 -0
  95. package/test/unit/identity/stateTransition/IdentityTopUpTransition/validation/state/validateIdentityTopUpTransitionStateFactory.spec.js +22 -0
  96. package/test/unit/identity/stateTransition/IdentityUpdateTransition/IdentityUpdateTransition.spec.js +219 -0
  97. package/test/unit/identity/stateTransition/IdentityUpdateTransition/applyIdentityUpdateTransitionFactory.spec.js +149 -0
  98. package/test/unit/identity/stateTransition/assetLockProof/createAssetLockProofInstance.spec.js +21 -0
  99. package/test/unit/identity/stateTransition/assetLockProof/fetchAssetLockPublicKeyHashFactory.spec.js +36 -0
  100. package/test/unit/identity/validation/validateRequiredPurposeAndSecurityLevelFactory.spec.js +47 -0
  101. package/test/unit/stateTransition/StateTransitionFactory.spec.js +160 -0
  102. package/test/unit/stateTransition/createStateTransitionFactory.spec.js +72 -0
  103. package/test/unit/stateTransition/errors/InvalidStateTransitionError.spec.js +51 -0
  104. package/test/unit/stateTransition/validation/validateStateTransitionBasicFactory.spec.js +138 -0
  105. package/test/unit/stateTransition/validation/validateStateTransitionFeeFactory.spec.js +372 -0
  106. package/test/unit/stateTransition/validation/validateStateTransitionIdentitySignatureFactory.spec.js +339 -0
  107. package/test/unit/stateTransition/validation/validateStateTransitionKeySignatureFactory.spec.js +86 -0
  108. package/test/unit/stateTransition/validation/validateStateTransitionStateFactory.spec.js +89 -0
  109. package/test/unit/version/validateProtocolVersionFactory.spec.js +80 -0
  110. package/webpack.config.js +49 -0
@@ -0,0 +1,415 @@
1
+ const { getRE2Class } = require('@dashevo/wasm-re2');
2
+
3
+ const createAjv = require('../../../../../../lib/ajv/createAjv');
4
+
5
+ const getChainAssetLockFixture = require('../../../../../../lib/test/fixtures/getChainAssetLockProofFixture');
6
+ const JsonSchemaValidator = require('../../../../../../lib/validation/JsonSchemaValidator');
7
+ const createStateRepositoryMock = require('../../../../../../lib/test/mocks/createStateRepositoryMock');
8
+
9
+ const { expectValidationError, expectJsonSchemaError } = require(
10
+ '../../../../../../lib/test/expect/expectError',
11
+ );
12
+
13
+ const validateChainAssetLockProofStructureFactory = require('../../../../../../lib/identity/stateTransition/assetLockProof/chain/validateChainAssetLockProofStructureFactory');
14
+ const ValidationResult = require('../../../../../../lib/validation/ValidationResult');
15
+ const IdentityAssetLockTransactionIsNotFoundError = require('../../../../../../lib/errors/consensus/basic/identity/IdentityAssetLockTransactionIsNotFoundError');
16
+ const InvalidAssetLockProofCoreChainHeightError = require('../../../../../../lib/errors/consensus/basic/identity/InvalidAssetLockProofCoreChainHeightError');
17
+ const InvalidAssetLockProofTransactionHeightError = require('../../../../../../lib/errors/consensus/basic/identity/InvalidAssetLockProofTransactionHeightError');
18
+ const SomeConsensusError = require('../../../../../../lib/test/mocks/SomeConsensusError');
19
+ const StateTransitionExecutionContext = require('../../../../../../lib/stateTransition/StateTransitionExecutionContext');
20
+
21
+ describe('validateChainAssetLockProofStructureFactory', () => {
22
+ let rawProof;
23
+ let stateRepositoryMock;
24
+ let validateChainAssetLockProofStructure;
25
+ let jsonSchemaValidator;
26
+ let validateAssetLockTransactionMock;
27
+ let validateAssetLockTransactionResult;
28
+ let publicKeyHash;
29
+ let rawTransaction;
30
+ let transactionHash;
31
+ let executionContext;
32
+
33
+ beforeEach(async function beforeEach() {
34
+ rawTransaction = '030000000137feb5676d0851337ea3c9a992496aab7a0b3eee60aeeb9774000b7f4bababa5000000006b483045022100d91557de37645c641b948c6cd03b4ae3791a63a650db3e2fee1dcf5185d1b10402200e8bd410bf516ca61715867666d31e44495428ce5c1090bf2294a829ebcfa4ef0121025c3cc7fbfc52f710c941497fd01876c189171ea227458f501afcb38a297d65b4ffffffff021027000000000000166a14152073ca2300a86b510fa2f123d3ea7da3af68dcf77cb0090a0000001976a914152073ca2300a86b510fa2f123d3ea7da3af68dc88ac00000000';
35
+ transactionHash = '6e200d059fb567ba19e92f5c2dcd3dde522fd4e0a50af223752db16158dabb1d';
36
+
37
+ const assetLock = getChainAssetLockFixture();
38
+
39
+ rawProof = assetLock.toObject();
40
+
41
+ const RE2 = await getRE2Class();
42
+ const ajv = createAjv(RE2);
43
+
44
+ jsonSchemaValidator = new JsonSchemaValidator(ajv);
45
+
46
+ stateRepositoryMock = createStateRepositoryMock(this.sinonSandbox);
47
+
48
+ stateRepositoryMock.fetchLatestPlatformBlockHeader.resolves({
49
+ coreChainLockedHeight: 42,
50
+ });
51
+
52
+ stateRepositoryMock.fetchTransaction.resolves({
53
+ data: Buffer.from(rawTransaction, 'hex'),
54
+ height: 42,
55
+ });
56
+
57
+ executionContext = new StateTransitionExecutionContext();
58
+
59
+ publicKeyHash = Buffer.from('152073ca2300a86b510fa2f123d3ea7da3af68dc', 'hex');
60
+
61
+ validateAssetLockTransactionResult = new ValidationResult();
62
+ validateAssetLockTransactionResult.setData({
63
+ publicKeyHash,
64
+ });
65
+ validateAssetLockTransactionMock = this.sinonSandbox.stub().resolves(
66
+ validateAssetLockTransactionResult,
67
+ );
68
+
69
+ validateChainAssetLockProofStructure = validateChainAssetLockProofStructureFactory(
70
+ jsonSchemaValidator,
71
+ stateRepositoryMock,
72
+ validateAssetLockTransactionMock,
73
+ );
74
+ });
75
+
76
+ describe('type', () => {
77
+ it('should be present', async () => {
78
+ delete rawProof.type;
79
+
80
+ const result = await validateChainAssetLockProofStructure(
81
+ rawProof,
82
+ executionContext,
83
+ );
84
+
85
+ expectJsonSchemaError(result);
86
+
87
+ const [error] = result.getErrors();
88
+
89
+ expect(error.getInstancePath()).to.equal('');
90
+ expect(error.getKeyword()).to.equal('required');
91
+ expect(error.getParams().missingProperty).to.equal('type');
92
+
93
+ expect(stateRepositoryMock.fetchTransaction).to.not.be.called();
94
+ expect(validateAssetLockTransactionMock).to.not.be.called();
95
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.not.be.called();
96
+ });
97
+
98
+ it('should be equal to 1', async () => {
99
+ rawProof.type = -1;
100
+
101
+ const result = await validateChainAssetLockProofStructure(
102
+ rawProof,
103
+ executionContext,
104
+ );
105
+
106
+ expectJsonSchemaError(result);
107
+
108
+ const [error] = result.getErrors();
109
+
110
+ expect(error.getInstancePath()).to.equal('/type');
111
+ expect(error.getKeyword()).to.equal('const');
112
+
113
+ expect(stateRepositoryMock.fetchTransaction).to.not.be.called();
114
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.not.be.called();
115
+ });
116
+ });
117
+
118
+ describe('coreChainLockedHeight', () => {
119
+ it('should be preset', async () => {
120
+ delete rawProof.coreChainLockedHeight;
121
+
122
+ const result = await validateChainAssetLockProofStructure(
123
+ rawProof,
124
+ executionContext,
125
+ );
126
+
127
+ expectJsonSchemaError(result);
128
+
129
+ const [error] = result.getErrors();
130
+
131
+ expect(error.getInstancePath()).to.equal('');
132
+ expect(error.getKeyword()).to.equal('required');
133
+ expect(error.getParams().missingProperty).to.equal('coreChainLockedHeight');
134
+
135
+ expect(stateRepositoryMock.fetchTransaction).to.not.be.called();
136
+ expect(validateAssetLockTransactionMock).to.not.be.called();
137
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.not.be.called();
138
+ });
139
+
140
+ it('should be an integer', async () => {
141
+ rawProof.coreChainLockedHeight = 1.5;
142
+
143
+ const result = await validateChainAssetLockProofStructure(
144
+ rawProof,
145
+ executionContext,
146
+ );
147
+
148
+ expectJsonSchemaError(result);
149
+
150
+ const [error] = result.getErrors();
151
+
152
+ expect(error.getInstancePath()).to.equal('/coreChainLockedHeight');
153
+ expect(error.getKeyword()).to.equal('type');
154
+
155
+ expect(stateRepositoryMock.fetchTransaction).to.not.be.called();
156
+ expect(validateAssetLockTransactionMock).to.not.be.called();
157
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.not.be.called();
158
+ });
159
+
160
+ it('should be a number', async () => {
161
+ rawProof.coreChainLockedHeight = '42';
162
+
163
+ const result = await validateChainAssetLockProofStructure(
164
+ rawProof,
165
+ executionContext,
166
+ );
167
+
168
+ expectJsonSchemaError(result);
169
+
170
+ const [error] = result.getErrors();
171
+
172
+ expect(error.getInstancePath()).to.equal('/coreChainLockedHeight');
173
+ expect(error.getKeyword()).to.equal('type');
174
+
175
+ expect(stateRepositoryMock.fetchTransaction).to.not.be.called();
176
+ expect(validateAssetLockTransactionMock).to.not.be.called();
177
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.not.be.called();
178
+ });
179
+
180
+ it('should be greater than 0', async () => {
181
+ rawProof.coreChainLockedHeight = 0;
182
+
183
+ const result = await validateChainAssetLockProofStructure(
184
+ rawProof,
185
+ executionContext,
186
+ );
187
+
188
+ expectJsonSchemaError(result);
189
+
190
+ const [error] = result.getErrors();
191
+
192
+ expect(error.getInstancePath()).to.equal('/coreChainLockedHeight');
193
+ expect(error.getKeyword()).to.equal('minimum');
194
+
195
+ expect(stateRepositoryMock.fetchTransaction).to.not.be.called();
196
+ expect(validateAssetLockTransactionMock).to.not.be.called();
197
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.not.be.called();
198
+ });
199
+
200
+ it('should be less than 4294967296', async () => {
201
+ rawProof.coreChainLockedHeight = 4294967296;
202
+
203
+ const result = await validateChainAssetLockProofStructure(
204
+ rawProof,
205
+ executionContext,
206
+ );
207
+
208
+ expectJsonSchemaError(result);
209
+
210
+ const [error] = result.getErrors();
211
+
212
+ expect(error.instancePath).to.equal('/coreChainLockedHeight');
213
+ expect(error.getKeyword()).to.equal('maximum');
214
+
215
+ expect(stateRepositoryMock.fetchTransaction).to.not.be.called();
216
+ expect(validateAssetLockTransactionMock).to.not.be.called();
217
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.not.be.called();
218
+ });
219
+
220
+ it('should be less or equal to consensus core height', async () => {
221
+ stateRepositoryMock.fetchLatestPlatformBlockHeader.resolves({
222
+ coreChainLockedHeight: 41,
223
+ });
224
+
225
+ const result = await validateChainAssetLockProofStructure(
226
+ rawProof,
227
+ executionContext,
228
+ );
229
+
230
+ expectValidationError(result, InvalidAssetLockProofCoreChainHeightError);
231
+
232
+ const [error] = result.getErrors();
233
+
234
+ expect(error.getCode()).to.equal(1035);
235
+ expect(error.getProofCoreChainLockedHeight()).to.equal(42);
236
+ expect(error.getCurrentCoreChainLockedHeight()).to.equal(41);
237
+ });
238
+ });
239
+
240
+ describe('outPoint', () => {
241
+ it('should be present', async () => {
242
+ delete rawProof.outPoint;
243
+
244
+ const result = await validateChainAssetLockProofStructure(
245
+ rawProof,
246
+ executionContext,
247
+ );
248
+
249
+ expectJsonSchemaError(result);
250
+
251
+ const [error] = result.getErrors();
252
+
253
+ expect(error.instancePath).to.equal('');
254
+ expect(error.getKeyword()).to.equal('required');
255
+ expect(error.getParams().missingProperty).to.equal('outPoint');
256
+
257
+ expect(stateRepositoryMock.fetchTransaction).to.not.be.called();
258
+ expect(validateAssetLockTransactionMock).to.not.be.called();
259
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.not.be.called();
260
+ });
261
+
262
+ it('should be a byte array', async () => {
263
+ rawProof.outPoint = new Array(36).fill('string');
264
+
265
+ const result = await validateChainAssetLockProofStructure(
266
+ rawProof,
267
+ executionContext,
268
+ );
269
+
270
+ expectJsonSchemaError(result, 2);
271
+
272
+ const [error, byteArrayError] = result.getErrors();
273
+
274
+ expect(error.instancePath).to.equal('/outPoint/0');
275
+ expect(error.getKeyword()).to.equal('type');
276
+
277
+ expect(byteArrayError.getKeyword()).to.equal('byteArray');
278
+
279
+ expect(stateRepositoryMock.fetchTransaction).to.not.be.called();
280
+ expect(validateAssetLockTransactionMock).to.not.be.called();
281
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.not.be.called();
282
+ });
283
+
284
+ it('should not be shorter than 36 bytes', async () => {
285
+ rawProof.outPoint = Buffer.alloc(35);
286
+
287
+ const result = await validateChainAssetLockProofStructure(
288
+ rawProof,
289
+ executionContext,
290
+ );
291
+
292
+ expectJsonSchemaError(result);
293
+
294
+ const [error] = result.getErrors();
295
+
296
+ expect(error.instancePath).to.equal('/outPoint');
297
+ expect(error.getKeyword()).to.equal('minItems');
298
+
299
+ expect(stateRepositoryMock.fetchTransaction).to.not.be.called();
300
+ expect(validateAssetLockTransactionMock).to.not.be.called();
301
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.not.be.called();
302
+ });
303
+
304
+ it('should not be longer than 36 bytes', async () => {
305
+ rawProof.outPoint = Buffer.alloc(37);
306
+
307
+ const result = await validateChainAssetLockProofStructure(
308
+ rawProof,
309
+ executionContext,
310
+ );
311
+
312
+ expectJsonSchemaError(result);
313
+
314
+ const [error] = result.getErrors();
315
+
316
+ expect(error.instancePath).to.equal('/outPoint');
317
+ expect(error.getKeyword()).to.equal('maxItems');
318
+
319
+ expect(stateRepositoryMock.fetchTransaction).to.not.be.called();
320
+ expect(validateAssetLockTransactionMock).to.not.be.called();
321
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.not.be.called();
322
+ });
323
+
324
+ it('should point to existing transaction', async () => {
325
+ stateRepositoryMock.fetchTransaction.resolves(null);
326
+
327
+ const result = await validateChainAssetLockProofStructure(
328
+ rawProof,
329
+ executionContext,
330
+ );
331
+
332
+ expectValidationError(result, IdentityAssetLockTransactionIsNotFoundError);
333
+
334
+ const [error] = result.getErrors();
335
+
336
+ expect(error.getCode()).to.equal(1032);
337
+ expect(error.getTransactionId()).to.deep.equal(
338
+ Buffer.from(transactionHash, 'hex'),
339
+ );
340
+
341
+ expect(stateRepositoryMock.fetchTransaction).to.be.calledOnceWithExactly(
342
+ transactionHash,
343
+ executionContext,
344
+ );
345
+ expect(validateAssetLockTransactionMock).to.not.be.called();
346
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.be.calledOnce();
347
+ });
348
+
349
+ it('should point to valid transaction', async () => {
350
+ const consensusError = new SomeConsensusError('test');
351
+
352
+ validateAssetLockTransactionResult.addError(consensusError);
353
+
354
+ const result = await validateChainAssetLockProofStructure(
355
+ rawProof,
356
+ executionContext,
357
+ );
358
+
359
+ expectValidationError(result, SomeConsensusError);
360
+
361
+ const [error] = result.getErrors();
362
+
363
+ expect(error).to.deep.equal(consensusError);
364
+ });
365
+
366
+ it('should point to transaction from block lower than core chain locked height', async () => {
367
+ rawProof.coreChainLockedHeight = 41;
368
+ stateRepositoryMock.fetchLatestPlatformBlockHeader.resolves({
369
+ coreChainLockedHeight: 41,
370
+ });
371
+
372
+ const result = await validateChainAssetLockProofStructure(
373
+ rawProof,
374
+ executionContext,
375
+ );
376
+
377
+ expectValidationError(result, InvalidAssetLockProofTransactionHeightError);
378
+
379
+ const [error] = result.getErrors();
380
+
381
+ expect(error.getCode()).to.equal(1036);
382
+ expect(error.getProofCoreChainLockedHeight()).to.equal(41);
383
+ expect(error.getTransactionHeight()).to.equal(42);
384
+
385
+ expect(stateRepositoryMock.fetchTransaction).to.be.calledOnceWithExactly(
386
+ transactionHash,
387
+ executionContext,
388
+ );
389
+ expect(validateAssetLockTransactionMock).to.not.be.called();
390
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.be.calledOnce();
391
+ });
392
+ });
393
+
394
+ it('should return valid result', async () => {
395
+ const result = await validateChainAssetLockProofStructure(
396
+ rawProof,
397
+ executionContext,
398
+ );
399
+
400
+ expect(result).to.be.an.instanceOf(ValidationResult);
401
+ expect(result.isValid()).to.be.true();
402
+ expect(result.getData()).to.deep.equal(publicKeyHash);
403
+
404
+ expect(stateRepositoryMock.fetchTransaction).to.be.calledOnceWithExactly(
405
+ transactionHash,
406
+ executionContext,
407
+ );
408
+ expect(validateAssetLockTransactionMock).to.be.calledOnceWithExactly(
409
+ Buffer.from(rawTransaction, 'hex'),
410
+ 0,
411
+ executionContext,
412
+ );
413
+ expect(stateRepositoryMock.fetchLatestPlatformBlockHeader).to.be.calledOnce();
414
+ });
415
+ });
@@ -0,0 +1,134 @@
1
+ const { Transaction, Script } = require('@dashevo/dashcore-lib');
2
+ const Output = require('@dashevo/dashcore-lib/lib/transaction/output');
3
+
4
+ const fetchAssetLockTransactionOutputFactory = require('../../../../../lib/identity/stateTransition/assetLockProof/fetchAssetLockTransactionOutputFactory');
5
+ const getChainAssetLockFixture = require('../../../../../lib/test/fixtures/getChainAssetLockProofFixture');
6
+ const getInstantAssetLockProofFixture = require('../../../../../lib/test/fixtures/getInstantAssetLockProofFixture');
7
+ const createStateRepositoryMock = require('../../../../../lib/test/mocks/createStateRepositoryMock');
8
+
9
+ const UnknownAssetLockProofError = require('../../../../../lib/identity/errors/UnknownAssetLockProofTypeError');
10
+ const AssetLockTransactionIsNotFoundError = require('../../../../../lib/identity/errors/AssetLockTransactionIsNotFoundError');
11
+ const StateTransitionExecutionContext = require('../../../../../lib/stateTransition/StateTransitionExecutionContext');
12
+
13
+ describe('fetchAssetLockTransactionOutputFactory', () => {
14
+ let fetchAssetLockTransactionOutput;
15
+ let stateRepositoryMock;
16
+ let executionContext;
17
+
18
+ beforeEach(function beforeEach() {
19
+ stateRepositoryMock = createStateRepositoryMock(this.sinonSandbox);
20
+ fetchAssetLockTransactionOutput = fetchAssetLockTransactionOutputFactory(stateRepositoryMock);
21
+
22
+ executionContext = new StateTransitionExecutionContext();
23
+ });
24
+
25
+ describe('InstantAssetLockProof', () => {
26
+ let assetLockProofFixture;
27
+
28
+ beforeEach(() => {
29
+ assetLockProofFixture = getInstantAssetLockProofFixture();
30
+ });
31
+
32
+ it('should return asset lock output', async () => {
33
+ const assetLockTransactionOutput = await fetchAssetLockTransactionOutput(
34
+ assetLockProofFixture,
35
+ executionContext,
36
+ );
37
+
38
+ expect(assetLockTransactionOutput).to.deep.equal(assetLockProofFixture.getOutput());
39
+ expect(stateRepositoryMock.fetchTransaction).to.not.be.called();
40
+ });
41
+ });
42
+
43
+ describe('ChainAssetLockProof', () => {
44
+ let assetLockProofFixture;
45
+ let output;
46
+ let transactionHash;
47
+
48
+ beforeEach(() => {
49
+ const rawTransaction = '030000000137feb5676d0851337ea3c9a992496aab7a0b3eee60aeeb9774000b7f4bababa5000000006b483045022100d91557de37645c641b948c6cd03b4ae3791a63a650db3e2fee1dcf5185d1b10402200e8bd410bf516ca61715867666d31e44495428ce5c1090bf2294a829ebcfa4ef0121025c3cc7fbfc52f710c941497fd01876c189171ea227458f501afcb38a297d65b4ffffffff021027000000000000166a14152073ca2300a86b510fa2f123d3ea7da3af68dcf77cb0090a0000001976a914152073ca2300a86b510fa2f123d3ea7da3af68dc88ac00000000';
50
+ assetLockProofFixture = getChainAssetLockFixture();
51
+ stateRepositoryMock.fetchTransaction.resolves({
52
+ data: Buffer.from(rawTransaction, 'hex'),
53
+ height: 42,
54
+ });
55
+
56
+ const transaction = new Transaction(rawTransaction);
57
+ ([output] = transaction.outputs);
58
+
59
+ const outPoint = Transaction.parseOutPointBuffer(assetLockProofFixture.getOutPoint());
60
+ ({ transactionHash } = outPoint);
61
+ });
62
+
63
+ it('should fetch output from state repository', async () => {
64
+ const assetLockTransactionOutput = await fetchAssetLockTransactionOutput(
65
+ assetLockProofFixture,
66
+ executionContext,
67
+ );
68
+
69
+ expect(assetLockTransactionOutput).to.deep.equal(output);
70
+
71
+ expect(stateRepositoryMock.fetchTransaction).to.be.calledOnceWithExactly(
72
+ transactionHash,
73
+ executionContext,
74
+ );
75
+ });
76
+
77
+ it('should throw IdentityAssetLockTransactionIsNotFoundError when transaction is not found', async () => {
78
+ stateRepositoryMock.fetchTransaction.resolves(null);
79
+
80
+ try {
81
+ await fetchAssetLockTransactionOutput(
82
+ assetLockProofFixture,
83
+ executionContext,
84
+ );
85
+
86
+ expect.fail('should throw IdentityAssetLockTransactionIsNotFoundError');
87
+ } catch (e) {
88
+ expect(e).to.be.an.instanceOf(AssetLockTransactionIsNotFoundError);
89
+ expect(e.getTransactionId()).to.deep.equal(transactionHash);
90
+ }
91
+ });
92
+
93
+ it('should return mocked output on dry run', async () => {
94
+ executionContext.enableDryRun();
95
+
96
+ const result = await fetchAssetLockTransactionOutput(
97
+ assetLockProofFixture,
98
+ executionContext,
99
+ );
100
+
101
+ executionContext.disableDryRun();
102
+
103
+ expect(result).to.deep.equal(new Output({
104
+ satoshis: 1000,
105
+ script: new Script(),
106
+ }));
107
+
108
+ expect(stateRepositoryMock.fetchTransaction).to.be.calledOnceWithExactly(
109
+ transactionHash,
110
+ executionContext,
111
+ );
112
+ });
113
+ });
114
+
115
+ it('should throw UnknownAssetLockProofError for unknown assetLockProof', async function it() {
116
+ const type = 666;
117
+
118
+ const assetLockProofFixture = {
119
+ getType: this.sinonSandbox.stub().returns(type),
120
+ };
121
+
122
+ try {
123
+ await fetchAssetLockTransactionOutput(
124
+ assetLockProofFixture,
125
+ executionContext,
126
+ );
127
+
128
+ expect.fail('should throw UnknownAssetLockProofError');
129
+ } catch (e) {
130
+ expect(e).to.be.an.instanceOf(UnknownAssetLockProofError);
131
+ expect(e.getType()).to.equal(type);
132
+ }
133
+ });
134
+ });