psf-multisig-approval 2.0.10 → 2.2.0

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/index.js CHANGED
@@ -4,12 +4,12 @@
4
4
  */
5
5
 
6
6
  // global libraries
7
- const bitcore = require('@chris.troutner/bitcore-lib-cash')
8
- const axios = require('axios')
7
+ import bitcore from '@chris.troutner/bitcore-lib-cash'
8
+ import axios from 'axios'
9
9
 
10
10
  // Local libraries
11
- const NFTs = require('./lib/nfts')
12
- const UtilLib = require('./lib/util')
11
+ import NFTs from './lib/nfts.js'
12
+ import UtilLib from './lib/util.js'
13
13
 
14
14
  class MultisigApproval {
15
15
  constructor (localConfig = {}) {
@@ -41,7 +41,7 @@ class MultisigApproval {
41
41
  this.getUpdateTx = this.getUpdateTx.bind(this)
42
42
  this.getCidData = this.getCidData.bind(this)
43
43
  this.validateApproval = this.validateApproval.bind(this)
44
- this.getUpdateHistory = this.getUpdateHistory.bind(this)
44
+ this.getAllApprovalTxs = this.getAllApprovalTxs.bind(this)
45
45
 
46
46
  // Create a transaction details cache, to reduce the number of API calls.
47
47
  this.txCache = {}
@@ -340,21 +340,79 @@ class MultisigApproval {
340
340
  }
341
341
  }
342
342
 
343
- // ToDo: This function will return an array of objects, where each object is
344
- // a validated update transaction. It will provide a validated historical list
345
- // of all the price updates.
346
- // Each object in the array should contain a timestamp, a block height of the
347
- // validated update transaction, and the price that was set with that tx.
348
- //
349
- getUpdateHistory () {
350
- // address is an input value.
343
+ // Given a BCH address, scan its transaction history to find all the
344
+ // APPROVAL transactions. This function returns an array of objects. Each
345
+ // object represents an APPROVAL transaction.
346
+ // If no APPROVAL transaction can be found, then function returns null.
347
+ // An optional input, filterTxids, is an array of transaction IDs to ignore. This can
348
+ // be used to ignore/skip any known, fake approval transactions.
349
+ async getAllApprovalTxs (inObj = {}) {
350
+ try {
351
+ let address = inObj.address
352
+ const { filterTxids } = inObj
351
353
 
352
- // Loop through the transaction history, and retrieve all APPROVAL transactions
354
+ const approvalTxs = []
353
355
 
354
- // For each APPROVAL transaction, retrieve the UPDATE tx that it points to.
356
+ // Input validation
357
+ if (address.includes('simpleledger:')) {
358
+ address = this.bchjs.SLP.Address.toCashAddress(address)
359
+ }
360
+ if (!address.includes('bitcoincash:')) {
361
+ throw new Error('Input address must start with bitcoincash: or simpleledger:')
362
+ }
355
363
 
356
- // Validate the UPDATE tx, if it passes, then add it to the output array
364
+ // Get the transaction history for the address
365
+ const txHistory = await this.wallet.getTransactions(address)
366
+ // console.log('txHistory: ', JSON.stringify(txHistory, null, 2))
367
+
368
+ // Loop through the transaction history
369
+ for (let i = 0; i < txHistory.length; i++) {
370
+ const thisTxid = txHistory[i]
371
+
372
+ // const height = thisTxid.height
373
+ const txid = thisTxid.tx_hash
374
+
375
+ // Skip the txid if it is in the filter list.
376
+ if (Array.isArray(filterTxids)) {
377
+ const txidFound = filterTxids.find(x => x === txid)
378
+ // console.log('txidFound: ', txidFound)
379
+ if (txidFound) {
380
+ continue
381
+ }
382
+ }
383
+
384
+ // Get the transaction details for the transaction
385
+ const txDetails = await this.util.getTxData(txid)
386
+ // console.log('txDetails: ', JSON.stringify(txDetails, null, 2))
387
+ // console.log(`txid: ${txid}`)
388
+
389
+ const out2ascii = Buffer.from(txDetails.vout[0].scriptPubKey.hex, 'hex').toString('ascii')
390
+ // console.log('out2ascii: ', out2ascii)
391
+
392
+ // If the first output is not an OP_RETURN, then the tx can be discarded.
393
+ if (!out2ascii.includes('APPROVE')) {
394
+ continue
395
+ }
396
+
397
+ const updateTxid = out2ascii.slice(10)
398
+ // console.log('updateTxid: ', updateTxid)
399
+
400
+ const outObj = {
401
+ approvalTxid: txid,
402
+ updateTxid,
403
+ approvalTxDetails: txDetails,
404
+ opReturn: out2ascii
405
+ }
406
+
407
+ approvalTxs.push(outObj)
408
+ }
409
+
410
+ return approvalTxs
411
+ } catch (err) {
412
+ console.error('Error in getAllApprovalTxs(): ', err)
413
+ throw err
414
+ }
357
415
  }
358
416
  }
359
417
 
360
- module.exports = MultisigApproval
418
+ export default MultisigApproval
package/lib/nfts.js CHANGED
@@ -110,4 +110,4 @@ class NFTs {
110
110
  }
111
111
  }
112
112
 
113
- module.exports = NFTs
113
+ export default NFTs
package/lib/util.js CHANGED
@@ -47,4 +47,4 @@ class UtilLib {
47
47
  }
48
48
  }
49
49
 
50
- module.exports = UtilLib
50
+ export default UtilLib
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "psf-multisig-approval",
3
- "version": "2.0.10",
3
+ "version": "2.2.0",
4
4
  "description": "Utility library implementing the PS009 specification",
5
+ "type": "module",
5
6
  "main": "index.js",
6
7
  "scripts": {
7
8
  "start": "node index.js",
@@ -40,7 +41,7 @@
40
41
  "chai": "4.3.7",
41
42
  "husky": "8.0.3",
42
43
  "lodash.clonedeep": "4.5.0",
43
- "minimal-slp-wallet": "5.11.2",
44
+ "minimal-slp-wallet": "7.1.3",
44
45
  "mocha": "10.2.0",
45
46
  "nyc": "15.1.0",
46
47
  "semantic-release": "19.0.5",
@@ -3,11 +3,11 @@
3
3
  */
4
4
 
5
5
  // Global npm libraries
6
- const SlpWallet = require('minimal-slp-wallet')
7
- const assert = require('chai').assert
6
+ import SlpWallet from 'minimal-slp-wallet'
7
+ import { assert } from 'chai'
8
8
 
9
9
  // Local libraries
10
- const MultisigApproval = require('../../index')
10
+ import MultisigApproval from '../../index.js'
11
11
 
12
12
  describe('#psf-multisig-approval', () => {
13
13
  let uut
@@ -3,16 +3,16 @@
3
3
  */
4
4
 
5
5
  // npm libraries
6
- const assert = require('chai').assert
7
- const sinon = require('sinon')
8
- const cloneDeep = require('lodash.clonedeep')
9
- const SlpWallet = require('minimal-slp-wallet')
6
+ import { assert } from 'chai'
7
+ import sinon from 'sinon'
8
+ import cloneDeep from 'lodash.clonedeep'
9
+ import SlpWallet from 'minimal-slp-wallet'
10
10
 
11
11
  // Mocking data libraries.
12
12
 
13
13
  // Local libraries
14
- const MultisigApproval = require('../../index')
15
- const mockDataLib = require('./mocks/main-index-mocks')
14
+ import MultisigApproval from '../../index.js'
15
+ import mockDataLib from './mocks/main-index-mocks.js'
16
16
 
17
17
  describe('#MultisigApproval.js', () => {
18
18
  let sandbox
@@ -277,7 +277,19 @@ const updateObj01 = {
277
277
  txDetails: updateTxDetails01
278
278
  }
279
279
 
280
- module.exports = {
280
+ export {
281
+ pubkeys01,
282
+ txHistory01,
283
+ txHistory02,
284
+ approvalTxDetails01,
285
+ updateTxDetails01,
286
+ tokenHolderInfo01,
287
+ updateData01,
288
+ approvalObj01,
289
+ updateObj01
290
+ }
291
+
292
+ export default {
281
293
  pubkeys01,
282
294
  txHistory01,
283
295
  txHistory02,
@@ -26,7 +26,7 @@ const mockUtxos = {
26
26
  ]
27
27
  }
28
28
 
29
- module.exports = {
29
+ export {
30
30
  mockBalance,
31
31
  mockUtxos
32
32
  }
@@ -3,19 +3,16 @@
3
3
  */
4
4
 
5
5
  // npm libraries
6
- const chai = require('chai')
7
- const sinon = require('sinon')
8
- // const cloneDeep = require('lodash.clonedeep')
9
- const SlpWallet = require('minimal-slp-wallet')
10
-
11
- // Locally global variables.
12
- const assert = chai.assert
6
+ import { assert } from 'chai'
7
+ import sinon from 'sinon'
8
+ // import cloneDeep from 'lodash.clonedeep'
9
+ import SlpWallet from 'minimal-slp-wallet'
13
10
 
14
11
  // Mocking data libraries.
15
- // const mockDataLib = require('./mocks/util-mocks')
12
+ // import mockDataLib from './mocks/util-mocks.js'
16
13
 
17
14
  // Unit under test
18
- const NftsLib = require('../../lib/nfts')
15
+ import NftsLib from '../../lib/nfts.js'
19
16
 
20
17
  describe('#NFTs', () => {
21
18
  let sandbox
@@ -3,16 +3,16 @@
3
3
  */
4
4
 
5
5
  // npm libraries
6
- const assert = require('chai').assert
7
- const sinon = require('sinon')
8
- // const cloneDeep = require('lodash.clonedeep')
9
- const SlpWallet = require('minimal-slp-wallet')
6
+ import { assert } from 'chai'
7
+ import sinon from 'sinon'
8
+ // import cloneDeep from 'lodash.clonedeep'
9
+ import SlpWallet from 'minimal-slp-wallet'
10
10
 
11
11
  // Local libraries
12
- // const mockDataLib = require('./mocks/util-mocks')
12
+ // import mockDataLib from './mocks/util-mocks.js'
13
13
 
14
14
  // Unit under test
15
- const UtilLib = require('../../lib/util')
15
+ import UtilLib from '../../lib/util.js'
16
16
 
17
17
  describe('#Util', () => {
18
18
  let sandbox