feat(endpoint): Added new /generateSendOpReturn POST endpoint

This commit is contained in:
Daniel Gonzalez
2020-07-14 01:45:07 -04:00
parent 1c00ea3cc6
commit 95789903f0
2 changed files with 147 additions and 0 deletions
+55
View File
@@ -82,6 +82,7 @@ class Slp {
'/transactionHistoryAllTokens/:address',
_this.txsByAddressSingle
)
_this.router.post('/generateSendOpReturn', _this.generateSendOpReturn)
}
// DRY error handler.
@@ -1609,6 +1610,60 @@ class Slp {
})
}
}
// Get OP_RETURN script and outputs
async generateSendOpReturn (req, res, next) {
try {
const tokenUtxos = req.body.tokenUtxos
const _sendQty = req.body.sendQty
const sendQty = Number(_sendQty)
// Reject if tokenUtxos is not an array.
if (!Array.isArray(tokenUtxos)) {
res.status(400)
return res.json({
error: 'tokenUtxos needs to be an array.'
})
}
// Reject if tokenUtxos array is empty.
if (!tokenUtxos.length) {
res.status(400)
return res.json({
error: 'tokenUtxos array can not be empty.'
})
}
// Reject if sendQty is not an number.
if (!sendQty) {
res.status(400)
return res.json({
error: 'sendQty must be a number.'
})
}
const opReturn = await _this.bchjs.SLP.TokenType1.generateSendOpReturn(
tokenUtxos,
sendQty
)
return opReturn
} catch (err) {
wlogger.error('Error in slp.js/generateSendOpReturn().', err)
// Decode the error message.
const { msg, status } = routeUtils.decodeError(err)
if (msg) {
res.status(status)
return res.json({ error: msg })
}
res.status(500)
return res.json({
error: 'Error in /generateSendOpReturn()'
})
}
}
}
module.exports = Slp