lamassu-server/bin/lamassu-remove-from-blacklist
2019-07-31 18:12:16 +01:00

58 lines
1.9 KiB
JavaScript

#!/usr/bin/env node
const os = require('os')
const readline = require('readline')
const coinUtils = require('../lib/coin-utils')
const db = require('../lib/db')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const sql = `select regexp_replace(crypto_code, '$', ' ') as code,regexp_replace(address, '^', ' ') as address from blacklist`
const deleteSql = `delete from blacklist where crypto_code = $1 and address = $2`
const makeDimAndReset = '\x1b[2m%s\x1b[0m'
db.query(sql)
.then(many => {
console.log(os.EOL, 'Here is your current list of prohibited addresses:', os.EOL)
console.log(many.map(it => `${it.code} | ${it.address}`).join(os.EOL), os.EOL)
console.log('What address would you like to remove from the list?', os.EOL)
console.log(makeDimAndReset, 'Example:', 'bc1q8pu2zlfxg8jf4cyuf844rl3uxmmw8sj9yaa393', os.EOL)
rl.question('Address: ', (address) => {
var aphaNumericRegex = /^([0-9]|[a-z])+([0-9a-z]+)$/i
if (!address.match(aphaNumericRegex)) {
return errorHandler(new Error('Invalid address'))
}
console.log(os.EOL, 'What is the ticker symbol of the coin that this address belongs to?', os.EOL)
console.log(makeDimAndReset, 'Example:', 'BTC', os.EOL)
rl.question('Ticker symbol: ', (cryptoCode) => {
try {
coinUtils.getCryptoCurrency(cryptoCode)
} catch (err) {
errorHandler(err)
}
db.none(deleteSql, [cryptoCode, address])
.then(() => db.query(sql))
.then(many2 => {
console.log(os.EOL, `Address removed. Here's the new list of prohibited addresses:`, os.EOL)
console.log(many2.map(it => `${it.code} | ${it.address}`).join(os.EOL))
rl.close()
process.exit(0)
})
.catch(errorHandler)
})
})
}).catch(errorHandler)
function errorHandler (err) {
rl.close()
console.log(err)
process.exit(1)
}