fix(validateTxid): Fixed bug in SLP validateTxid()

This commit is contained in:
Chris Troutner
2019-10-30 12:33:29 -07:00
parent 3183b81f6a
commit 9090d1b991
3 changed files with 149 additions and 42 deletions
+30 -5
View File
@@ -999,34 +999,59 @@ async function validateBulk(req, res, next) {
// Get data from SLPDB.
const tokenRes = await axios.get(url, options)
// console.log(`tokenRes.data: ${JSON.stringify(tokenRes.data, null, 2)}`)
const formattedTokens = []
let formattedTokens = []
// Combine the arrays. Why? Generally there is nothing in the u array.
const concatArray = tokenRes.data.c.concat(tokenRes.data.u)
const tokenIds = []
if (concatArray.length > 0) {
concatArray.forEach(token => {
tokenIds.push(token.tx.h)
tokenIds.push(token.tx.h) // txid
const validationResult = {
txid: token.tx.h,
valid: token.slp.valid
}
// If the txid is invalid, add the reason it's invalid.
if (!validationResult.valid)
validationResult.invalidReason = token.slp.invalidReason
formattedTokens.push(validationResult)
})
txids.forEach(tokenId => {
if (!tokenIds.includes(tokenId)) {
// If a user-provided txid doesn't exist in the data, add it with
// valid:false property.
txids.forEach(txid => {
if (!tokenIds.includes(txid)) {
formattedTokens.push({
txid: tokenId,
txid: txid,
valid: false
})
}
})
}
// Catch a corner case of repeated txids. SLPDB will remove redundent TXIDs,
// which will cause the output array to be smaller than the input array.
if (txids.length > formattedTokens.length) {
const newOutput = []
for (let i = 0; i < txids.length; i++) {
const thisTxid = txids[i]
// Find the element that matches the current txid.
const elem = formattedTokens.filter(x => x.txid === thisTxid)
newOutput.push(elem[0])
}
// Replace the original output object with the new output object.
formattedTokens = newOutput
}
res.status(200)
return res.json(formattedTokens)
} catch (err) {