psf-multisig-approval 2.0.9 → 2.1.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.
Files changed (2) hide show
  1. package/index.js +83 -15
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -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 = {}
@@ -242,10 +242,20 @@ class MultisigApproval {
242
242
  throw new Error('cid a required input')
243
243
  }
244
244
 
245
- const urlStr = `${this.ipfsGateway}/ipfs/view/${cid}/data.json`
246
- // console.log('urlStr: ', urlStr)
245
+ let request = null
246
+ try {
247
+ // Try older token-tiger format first (directory-based CID)
248
+ const urlStr = `${this.ipfsGateway}/ipfs/view/${cid}/data.json`
249
+ // console.log('urlStr: ', urlStr)
247
250
 
248
- const request = await this.axios.get(urlStr)
251
+ request = await this.axios.get(urlStr)
252
+ } catch (err) {
253
+ // Try the new format (single file CID)
254
+ const urlStr = `${this.ipfsGateway}/ipfs/view/${cid}`
255
+ // console.log('urlStr: ', urlStr)
256
+
257
+ request = await this.axios.get(urlStr)
258
+ }
249
259
 
250
260
  return request.data
251
261
  } catch (err) {
@@ -330,20 +340,78 @@ class MultisigApproval {
330
340
  }
331
341
  }
332
342
 
333
- // ToDo: This function will return an array of objects, where each object is
334
- // a validated update transaction. It will provide a validated historical list
335
- // of all the price updates.
336
- // Each object in the array should contain a timestamp, a block height of the
337
- // validated update transaction, and the price that was set with that tx.
338
- //
339
- getUpdateHistory () {
340
- // 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
341
353
 
342
- // Loop through the transaction history, and retrieve all APPROVAL transactions
354
+ const approvalTxs = []
343
355
 
344
- // 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
+ }
345
363
 
346
- // 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
+ }
347
415
  }
348
416
  }
349
417
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "psf-multisig-approval",
3
- "version": "2.0.9",
3
+ "version": "2.1.0",
4
4
  "description": "Utility library implementing the PS009 specification",
5
5
  "main": "index.js",
6
6
  "scripts": {