@dashevo/dash-spv 1.1.5 → 1.1.7-1

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/.eslintignore ADDED
@@ -0,0 +1,2 @@
1
+ dist/
2
+ .nyc_output/
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017-2022 Dash Core Group, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # dash-spv
2
2
 
3
- > Temporary repo until spv functions moved into dashcore-lib
4
-
5
- No new features should be added to this repo.
3
+ Set of SPV function used by js-dapi-client package
6
4
 
7
5
  ## License
8
6
 
package/config/config.js CHANGED
@@ -35,6 +35,16 @@ module.exports = {
35
35
  nonce: 3861367235,
36
36
  });
37
37
  },
38
+ getRegtestGenesis() {
39
+ return utils.normalizeHeader({
40
+ version: 1,
41
+ previousblockhash: '0000000000000000000000000000000000000000000000000000000000000000',
42
+ merkleroot: 'e0028eb9648db56b1ac77cf090b99048a8007e2bb64b68f092c03c7f56a662c7',
43
+ time: 1390666206,
44
+ bits: '1e0ffff0',
45
+ nonce: 3861367235,
46
+ });
47
+ },
38
48
  getLivenetGenesis() {
39
49
  throw Error('Livenet genesis not yet implemented');
40
50
  },
package/lib/consensus.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const { hasValidTarget } = require('@dashevo/dark-gravity-wave');
2
+ const merkleProofs = require('./merkleproofs');
2
3
  const utils = require('./utils');
3
4
 
4
5
  const MIN_TIMESTAMP_HEADERS = 11;
@@ -39,6 +40,26 @@ function isValidBlockHeader(newHeader, previousHeaders, network = 'mainnet') {
39
40
  && hasGreaterThanMedianTimestamp(newHeader, previousHeaders);
40
41
  }
41
42
 
43
+ /**
44
+ * validates an array of tx hashes or Transaction instances
45
+ * against a merkleblock and the local header chain
46
+ * @param {Transaction[]|string[]} transactions
47
+ * @param {MerkleBlock} merkleBlock - a MerkleBlock instance
48
+ * @param {SpvChain} headerChain - an instance of an SpvChain
49
+ * @return {boolean}
50
+ */
51
+ async function areValidTransactions(transactions, merkleBlock, headerChain) {
52
+ if (!Array.isArray(transactions) || transactions.length <= 0) {
53
+ throw new Error('Please check that transactions parameter is a non-empty array');
54
+ }
55
+ const localHeader = await headerChain.getHeader(merkleBlock.header.hash);
56
+ if (!localHeader) {
57
+ return false;
58
+ }
59
+ return merkleProofs.validateTxProofs(merkleBlock, transactions);
60
+ }
61
+
42
62
  module.exports = {
43
63
  isValidBlockHeader,
64
+ areValidTransactions,
44
65
  };
@@ -1,7 +1,21 @@
1
- const merkleproofs = {
1
+ const DashUtil = require('@dashevo/dash-util');
2
2
 
3
- validateTxProofs: (merkleBlock, transactions) => merkleBlock.validMerkleTree()
4
- && transactions.filter(t => merkleBlock.hasTransaction(t)).length === transactions.length,
3
+ const merkleproofs = {
4
+ /**
5
+ * validates an array of tx hashes or Transaction instances
6
+ * against a merkleblock
7
+ * @param {MerkleBlock} merkleBlock - a MerkleBlock instance
8
+ * @param {Transaction[]|string[]} transactions
9
+ * @return {boolean}
10
+ */
11
+ validateTxProofs: (merkleBlock, transactions) => {
12
+ let txToFilter = transactions.slice();
13
+ if (typeof transactions[0] === 'string') {
14
+ txToFilter = txToFilter.map(tx => DashUtil.toHash(tx).toString('hex'));
15
+ }
16
+ return merkleBlock.validMerkleTree
17
+ && txToFilter.filter(tx => merkleBlock.hasTransaction(tx)).length === transactions.length;
18
+ },
5
19
  };
6
20
 
7
21
  module.exports = merkleproofs;
package/lib/spvchain.js CHANGED
@@ -32,6 +32,14 @@ const SpvChain = class {
32
32
  }
33
33
  this.root = config.getDevnetGenesis();
34
34
  break;
35
+ case 'regtest':
36
+ this.network = 'regtest';
37
+ if (startBlock) {
38
+ this.root = startBlock;
39
+ break;
40
+ }
41
+ this.root = config.getRegtestGenesis();
42
+ break;
35
43
  case 'livenet':
36
44
  this.network = 'mainnet';
37
45
  if (startBlock) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dashevo/dash-spv",
3
- "version": "1.1.5",
4
- "description": "Temporary repo until spv functions moved into dashcore-lib",
3
+ "version": "1.1.7-1",
4
+ "description": "Repository containing SPV functions used by @dashevo",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "mocha test/ --no-timeouts --recursive",
@@ -12,16 +12,16 @@
12
12
  "license": "MIT",
13
13
  "dependencies": {
14
14
  "@dashevo/dark-gravity-wave": "^1.1.1",
15
- "@dashevo/dashcore-lib": "^0.17.4",
16
15
  "@dashevo/dash-util": "^2.0.3",
17
- "levelup": "^4.0.1",
18
- "memdown": "^3.0.0"
16
+ "@dashevo/dashcore-lib": "~0.19.39",
17
+ "levelup": "^4.4.0",
18
+ "memdown": "^5.1.0"
19
19
  },
20
20
  "devDependencies": {
21
- "eslint": "^5.16.0",
22
- "eslint-config-airbnb-base": "^13.1.0",
23
- "eslint-plugin-import": "^2.17.3",
24
- "mocha": "^5.2.0",
21
+ "eslint": "^7.32.0",
22
+ "eslint-config-airbnb-base": "^14.2.1",
23
+ "eslint-plugin-import": "^2.24.2",
24
+ "mocha": "^9.1.2",
25
25
  "should": "^13.2.3"
26
26
  }
27
- }
27
+ }
@@ -22,29 +22,15 @@ const merkleJSON = {
22
22
  ],
23
23
  flags: [219, 63],
24
24
  },
25
- mnProof: { // Mainnet Block 300100
26
- header: {
27
- hash: '00000000000051b99f3fa12da6997bb54327c7b81509829467b499a7ce03136a',
28
- version: 3,
29
- prevHash: '0000000000154ef1f88653a6757f1182680a4d5831d815010d5fa646ae79ce35',
30
- merkleRoot: 'a0055d45ad9b35e77fb01c59a4feb9976921493d2557a5ac0798b49e82ea1e99',
31
- time: 1436550250,
32
- bits: 454590659,
33
- nonce: 2601264896,
34
- },
35
- numTransactions: 12,
36
- hashes: [
37
- '9d0a368bc9923c6cb966135a4ceda30cc5f259f72c8843ce015056375f8a06ec',
38
- '39e5cd533567ac0a8602bcc4c29e2f01a4abb0fe68ffbc7be6c393db188b72e0',
39
- 'cd75b421157eca03eff664bdc165730f91ef2fa52df19ff415ab5acb30045425',
40
- '2ef9795147caaeecee5bc2520704bb372cde06dbd2e871750f31336fd3f02be3',
41
- '2241d3448560f8b1d3a07ea5c31e79eb595632984a20f50944809a61fdd9fe0b',
42
- '45afbfe270014d5593cb065562f1fed726f767fe334d8b3f4379025cfa5be8c5',
43
- '198c03da0ccf871db91fe436e2795908eac5cc7d164232182e9445f7f9db1ab2',
44
- 'ed07c181ce5ba7cb66d205bc970f43e1ca11996d611aa8e91e305eb8608c543c',
45
- ],
46
- flags: [219, 63],
47
- },
25
+ // merkleblock for inserted tx hash
26
+ // '7262476912a96b9a6226cfa3a8f231ba3e2b1f75c396e88367e532c79c43c95b' in testnet block 10000
27
+ // (bloom filter: '03359be1100000000000000001')
28
+ rawMerkleBlock: '000000200e71587f863213690c24b8ad07d0753b38abe6ff1328ef7661689404000000000f1e7ce895614047d5c8d3e7b537b26b0482302301aa104947eb09c55521b316b10b1e5cff7d191c166b25b80500000002a8f781d45a5b12abffcf52bac3b3fc7824cc91ef4f9bb04bc97500a1d98d91305bc9439cc732e56783e896c3751f2b3eba31f2a8a3cf26629a6ba91269476272011d',
29
+ // merkleblock for inserted tx hashes
30
+ // '7262476912a96b9a6226cfa3a8f231ba3e2b1f75c396e88367e532c79c43c95b'
31
+ // '3f3517ee8fa95621fe8abdd81c1e0dfb50e21dd4c5a3c01eee2c47cf664821b6' in testnet block 10000
32
+ // (bloom filter: '0797b88e7d21b31f130000000000000001')
33
+ rawMerkleBlock2: '000000200e71587f863213690c24b8ad07d0753b38abe6ff1328ef7661689404000000000f1e7ce895614047d5c8d3e7b537b26b0482302301aa104947eb09c55521b316b10b1e5cff7d191c166b25b80500000004acff07e45bfb50dd8e2fb10109f1988423cbcc9423ff07b7acb09715bd1a11e89153210725b8e219b91e433180092226c7b56649843fc51bc012c587f50030a1b6214866cf472cee1ec0a3c5d41de250fb0d1e1cd8bd8afe2156a98fee17353f5bc9439cc732e56783e896c3751f2b3eba31f2a8a3cf26629a6ba9126947627202eb01',
48
34
  };
49
35
 
50
36
  module.exports = merkleJSON;
package/test/index.js CHANGED
@@ -1,8 +1,8 @@
1
- const dashcore = require('@dashevo/dashcore-lib');
1
+ const { MerkleBlock, Transaction } = require('@dashevo/dashcore-lib');
2
2
  const Blockchain = require('../lib/spvchain');
3
3
  const utils = require('../lib/utils');
4
4
  const merkleProofs = require('../lib/merkleproofs');
5
-
5
+ const consensus = require('../lib/consensus');
6
6
  const {
7
7
  testnet, testnet2, testnet3, mainnet, badRawHeaders,
8
8
  } = require('./data/rawHeaders');
@@ -10,6 +10,8 @@ const headers = require('./data/headers');
10
10
  const merkleData = require('./data/merkleproofs');
11
11
 
12
12
  let chain = null;
13
+ let merkleBlock = null;
14
+ let merkleBlock2 = null;
13
15
 
14
16
  require('should');
15
17
 
@@ -365,17 +367,89 @@ describe('Blockstore', () => {
365
367
  });
366
368
 
367
369
  // TODO:
368
- // Create scenarios where chain splits occur to form competing brances
369
- // Difficult with current chain provided by chainmanager as this is actual hardcoded
370
+ // Create scenarios where chain splits occur to form competing branches
371
+ // Difficult with current chain provided by chainmanager as this is actually hardcoded
370
372
  // Dash testnet headers which requires significant CPU power to create forked chains from
371
373
 
372
374
  describe('MerkleProofs', () => {
373
375
  it('should validate tx inclusion in merkleblock', () => {
374
- const merkleBlock = new dashcore.MerkleBlock(merkleData.merkleBlock);
375
- const validTx = '45afbfe270014d5593cb065562f1fed726f767fe334d8b3f4379025cfa5be8c5';
376
+ merkleBlock = new MerkleBlock(merkleData.merkleBlock);
377
+ const validTx = 'c5e85bfa5c0279433f8b4d33fe67f726d7fef1625506cb93554d0170e2bfaf45';
376
378
  const invalidTx = `${validTx.substring(0, validTx.length - 1)}0`;
377
-
378
379
  merkleProofs.validateTxProofs(merkleBlock, [validTx]).should.equal(true);
379
380
  merkleProofs.validateTxProofs(merkleBlock, [invalidTx]).should.equal(false);
380
381
  });
381
382
  });
383
+
384
+ describe('Transaction validation', () => {
385
+ before(() => {
386
+ chain = new Blockchain('testnet', 10000, utils.normalizeHeader(testnet[0]));
387
+ chain.addHeaders(testnet.slice(1, 500));
388
+ });
389
+
390
+ beforeEach(() => {
391
+ merkleBlock = new MerkleBlock(Buffer.from(merkleData.rawMerkleBlock, 'hex'));
392
+ merkleBlock2 = new MerkleBlock(Buffer.from(merkleData.rawMerkleBlock2, 'hex'));
393
+ });
394
+
395
+ it('should throw an error if wrong type is passed', async () => {
396
+ const validTx = '7262476912a96b9a6226cfa3a8f231ba3e2b1f75c396e88367e532c79c43c95b';
397
+ const invalid = Buffer.from(validTx);
398
+ try {
399
+ await consensus.areValidTransactions(invalid, merkleBlock, chain);
400
+ throw new Error('Transaction validation failed to throw an error');
401
+ } catch (e) {
402
+ e.message.should.equal('Please check that transactions parameter is a non-empty array');
403
+ }
404
+ });
405
+
406
+ it('should throw an error if empty transactions array', async () => {
407
+ const transactions = [];
408
+ try {
409
+ await consensus.areValidTransactions(transactions, merkleBlock, chain);
410
+ throw new Error('Transaction validation failed to throw an error');
411
+ } catch (e) {
412
+ e.message.should.equal('Please check that transactions parameter is a non-empty array');
413
+ }
414
+ });
415
+
416
+ it('should not validate an array of raw transactions for a merkleblock that was generated with a filter containing only one of them', async () => {
417
+ const validTx = new Transaction('020000000100de7192338db34fe9bb25f34122893d94f3b43bd4c881e37924c8e95a068cc8000000006b483045022100b185b4b86b613e3ffc796db90f95dc88f82561c50ba49fa610d8090f61f38ff002201473466bddee2672ed0dba75b81c07bdade734441e005c8c7fdf12a039ff9312012102bdfedbfe6ea05de8094d18442e08c98ebd695acf489f1bdf68fe1e3aff6f488effffffff010000000000000000016a00000000');
418
+ const validTx2 = new Transaction('0200000001ccc68ff58b7b02247f3e05440ab7fc7c8c599453de4a49e35393981890a1e984010000006b483045022100e141365c4916fa09d03aac58f215b926b777a3acec918a6becdbb03d59d28d9f02204edc1ce68d596e28e55f114c67a270302d488707e754af1261fdfc043891651c012102d5b7c0dfb2fd9591a4a98555ce806e17842f401979f2e0cc0689c91d6ca9ef87feffffff025d4c0000000000001976a9146d14b25994e4036d70eeafd4a706640337db5a5e88ac409c0000000000001976a9148b4a9da5a46c7b89b26b649bac8e34e7aa5aa63188acc2260000');
419
+ const transactions = [];
420
+ transactions.push(validTx);
421
+ transactions.push(validTx2);
422
+ const result = await consensus.areValidTransactions(transactions, merkleBlock, chain);
423
+ result.should.equal(false);
424
+ });
425
+
426
+ it('should validate an array of raw transactions for a merkleblock that was generated with a filter containing both of them', async () => {
427
+ const validTx = new Transaction('020000000100de7192338db34fe9bb25f34122893d94f3b43bd4c881e37924c8e95a068cc8000000006b483045022100b185b4b86b613e3ffc796db90f95dc88f82561c50ba49fa610d8090f61f38ff002201473466bddee2672ed0dba75b81c07bdade734441e005c8c7fdf12a039ff9312012102bdfedbfe6ea05de8094d18442e08c98ebd695acf489f1bdf68fe1e3aff6f488effffffff010000000000000000016a00000000');
428
+ const validTx2 = new Transaction('0200000001ccc68ff58b7b02247f3e05440ab7fc7c8c599453de4a49e35393981890a1e984010000006b483045022100e141365c4916fa09d03aac58f215b926b777a3acec918a6becdbb03d59d28d9f02204edc1ce68d596e28e55f114c67a270302d488707e754af1261fdfc043891651c012102d5b7c0dfb2fd9591a4a98555ce806e17842f401979f2e0cc0689c91d6ca9ef87feffffff025d4c0000000000001976a9146d14b25994e4036d70eeafd4a706640337db5a5e88ac409c0000000000001976a9148b4a9da5a46c7b89b26b649bac8e34e7aa5aa63188acc2260000');
429
+ const transactions = [];
430
+ transactions.push(validTx);
431
+ transactions.push(validTx2);
432
+ const result = await consensus.areValidTransactions(transactions, merkleBlock2, chain);
433
+ result.should.equal(true);
434
+ });
435
+
436
+ it('should not validate an array of transactions hashes for a merkleblock that was generated with a filter containing only one of them', async () => {
437
+ const validTxHash = '7262476912a96b9a6226cfa3a8f231ba3e2b1f75c396e88367e532c79c43c95b';
438
+ const validTxHash2 = '3f3517ee8fa95621fe8abdd81c1e0dfb50e21dd4c5a3c01eee2c47cf664821b6';
439
+ const transactions = [];
440
+ transactions.push(validTxHash);
441
+ transactions.push(validTxHash2);
442
+ const result = await consensus.areValidTransactions(transactions, merkleBlock, chain);
443
+ result.should.equal(false);
444
+ });
445
+
446
+ it('should validate an array of transactions hashes for a merkleblock that was generated with a filter containing both of them', async () => {
447
+ const validTxHash = '7262476912a96b9a6226cfa3a8f231ba3e2b1f75c396e88367e532c79c43c95b';
448
+ const validTxHash2 = '3f3517ee8fa95621fe8abdd81c1e0dfb50e21dd4c5a3c01eee2c47cf664821b6';
449
+ const transactions = [];
450
+ transactions.push(validTxHash);
451
+ transactions.push(validTxHash2);
452
+ const result = await consensus.areValidTransactions(transactions, merkleBlock2, chain);
453
+ result.should.equal(true);
454
+ });
455
+ });
package/.travis.yml DELETED
@@ -1,10 +0,0 @@
1
- os: linux
2
- language: node_js
3
- node_js:
4
- - '8'
5
- install:
6
- - npm install
7
- before_install:
8
- - echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" > .npmrc
9
- before_script:
10
- - npm run lint