fix(getTokenData): Updated to reflect changes in PS002 spec

This commit is contained in:
Chris Troutner
2022-04-26 08:35:36 -07:00
parent ec6bb25278
commit 160366c17a
3 changed files with 56 additions and 528 deletions
+20 -6
View File
@@ -229,7 +229,7 @@ class PsfSlpIndexer {
// such data.
//
// Example:
// curl -H "Content-Type: application/json" -X POST -d '{ "tokenId": "afca62f07560b4c72c2ed6c9c3995315f964ccdfc37dded316d182640b42d88a" }' localhost:3000/v5/psf/slp/token/data
// curl -H "Content-Type: application/json" -X POST -d '{ "tokenId": "f055256b938f1ecfa270459d6f12c7c8c82b66d3263c03d5074445a2b1a498a3" }' localhost:3000/v5/psf/slp/token/data
async getTokenData (req, res, next) {
try {
// Verify env var is set for interacting with the indexer.
@@ -292,17 +292,18 @@ class PsfSlpIndexer {
// Get the OP_RETURN data and decode it.
const mutableData = await _this.decodeOpReturn(documentHash)
const jsonData = JSON.parse(mutableData)
// console.log(`jsonData: ${JSON.stringify(jsonData, null, 2)}`)
const mspAddress = jsonData.mspAddress
const mutableDataAddr = jsonData.mda
// Gets the mutable data address (MDA) transaction history.
const transactions = await _this.bchjs.Electrumx.transactions(mspAddress)
const transactions = await _this.bchjs.Electrumx.transactions(mutableDataAddr)
const mspTxs = transactions.transactions
// console.log(`mspTxs: ${JSON.stringify(mspTxs, null, 2)}`)
let data = false
// Maps each transaction of the mspAddress
// Maps each transaction of the mutableDataAddr
// if finds an OP_RETURN decode is and closes the loop
// Start with the newest TXID entry and scan the history to find the first
// entry with an IPFS CID.
@@ -326,8 +327,15 @@ class PsfSlpIndexer {
// Keep searching if this TX does not have a cid value.
if (!obj.cid) continue
// TODO: Ensure data was generated by the MSP address and not an
// update from a different address.
// Ensure data was generated by the MDA
const txData = await this.bchjs.Transaction.get(txid)
const vinAddress = txData.txData.vin[0].address
// Skip entry if it was not made by the MDA private key.
if (mutableDataAddr !== vinAddress) {
console.log('Data is not generated by MDA')
continue
}
break
} catch (error) {
@@ -352,6 +360,7 @@ class PsfSlpIndexer {
return result
} catch (err) {
console.log('Error in getMutableData().')
// console.log('Error in getMutableData(): ', err)
throw err
}
}
@@ -395,7 +404,12 @@ class PsfSlpIndexer {
if (!cid || typeof cid !== 'string') {
throw new Error('cid must be a string.')
}
// Assuming that CID starts with ipfs://. Cutting out that prefix.
cid = cid.substring(7)
const dataUrl = `https://${cid}.ipfs.dweb.link/data.json`
console.log(`dataUrl: ${dataUrl}`)
const response = await _this.axios.get(dataUrl)
// console.log(`response.data: ${JSON.stringify(response.data, null, 2)}`)