hubot-openassets
Advanced tools
Comparing version 0.5.1 to 0.7.0
{ | ||
"name": "hubot-openassets", | ||
"version": "0.5.1", | ||
"version": "0.7.0", | ||
"description": "Hubot script for managing and sending assets based on the Open Assets protocol", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -49,3 +49,3 @@ // Description: | ||
add(nick, address) { | ||
if (typeof nick !== 'string' || typeof address !== 'string') { | ||
if (typeof nick !== 'string' || typeof address !== 'string' || nick === '' || address === '' ) { | ||
return 'Sorry Dave, I can\'t do that. Need both a nickname and address to add an addressbook entry.'; | ||
@@ -60,3 +60,3 @@ } | ||
remove(nick) { | ||
if (typeof nick !== 'string') { | ||
if (typeof nick !== 'string' || nick === '') { | ||
return 'If you want me to delete someone\'s address, how about you give me their name?'; | ||
@@ -74,5 +74,14 @@ } | ||
} else { | ||
let content = this.getContent(); | ||
let names = Object.keys(content); | ||
return names.map(name => `${name}: ${content[name]}`); | ||
let content = this.getContent(); | ||
let names = Object.keys(content); | ||
let targetLength = names.sort((a,b) => b.length > a.length)[0].length; | ||
let padding = ''; | ||
return names.map(name => { | ||
if (name.length < targetLength) { | ||
let spaces = targetLength - name.length; | ||
for (var i=0; i<spaces; i++) { padding += ' '; } | ||
} | ||
return `${name}${padding} | ${content[name]}`; | ||
}); | ||
} | ||
@@ -94,3 +103,3 @@ }, | ||
robot.hear(new RegExp(`${robotKeyword} address (add|remove|list)\\s*(\\w*)\\s*(\\w*)`, 'i'), function(res) { | ||
robot.hear(new RegExp(`${robotKeyword} address (add|remove|list)\\s*([a-zA-Z0-9_\-]*)\\s*([a-zA-Z0-9_\-]*)`, 'i'), function(res) { | ||
let command = res.match[1]; | ||
@@ -130,9 +139,14 @@ let nick = res.match[2]; | ||
function sha256(data) { | ||
return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary'); | ||
} | ||
function balanceOfAddress(address, cb) { | ||
var balanceUrl = 'https://api.coinprism.com/v1/addresses/' + address; | ||
let balanceUrl = 'https://api.coinprism.com/v1/addresses/' + address; | ||
robot.http(balanceUrl).header('Content-Type', 'application/json') | ||
.get()(function(err, res, body) { | ||
if (!err && res.statusCode === 200) { | ||
var assets = JSON.parse(body).assets; | ||
var assetDetails = assets.filter(function(details) { return details.id === process.env.OA_ASSET_ID; })[0]; | ||
let assets = JSON.parse(body).assets; | ||
let assetDetails = assets.filter(function(details) { return details.id === process.env.OA_ASSET_ID; })[0]; | ||
cb(assetDetails, err, res, body); | ||
@@ -154,12 +168,8 @@ } else { | ||
function addressFromBitcoinAddress(btcAddress) { | ||
function sha256(data) { | ||
return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary'); | ||
} | ||
var btcAddr = new Buffer(Base58.decode(btcAddress)); | ||
var btcBuff = new Put() | ||
.word8(19) | ||
.put(btcAddr.slice(0, -4)) | ||
var btcCheck = sha256(sha256(btcBuff.buffer())); | ||
btcBuff.put(btcCheck.slice(0,4)) | ||
let btcAddr = new Buffer(Base58.decode(btcAddress)); | ||
let btcBuff = new Put().word8(19).put(btcAddr.slice(0, -4)); | ||
let btcCheck = sha256(sha256(btcBuff.buffer())); | ||
btcBuff.put(btcCheck.slice(0,4)); | ||
return Base58.encode(btcBuff.buffer()); | ||
@@ -171,3 +181,4 @@ } | ||
if (assetDetails) { | ||
hearResponse.send( hearResponse.match[1] + ' has ' + totalBalanceOfAsset(assetDetails) + ' ' + robotKeyword); | ||
let msg = `${hearResponse.match[1]} has ${totalBalanceOfAsset(assetDetails)} ${robotKeyword}`; | ||
hearResponse.send(msg); | ||
} else { | ||
@@ -180,3 +191,4 @@ hearResponse.reply('not found'); | ||
robot.hear(new RegExp(`${robotKeyword} list`, 'i'), function(hearResponse) { | ||
var assetUrl = 'https://api.coinprism.com/v1/assets/' + process.env.OA_ASSET_ID + '/owners'; | ||
let assetUrl = 'https://api.coinprism.com/v1/assets/' + process.env.OA_ASSET_ID + '/owners'; | ||
robot.http(assetUrl).header('Content-Type', 'application/json') | ||
@@ -188,12 +200,31 @@ .get()(function(err, res, body) { | ||
} | ||
var asset = JSON.parse(body); | ||
var owners = asset.owners; | ||
var displayTotal = owners.length > 10 ? 10 : owners.length; | ||
var totalAssets = 0; | ||
owners.forEach(function(o) { totalAssets += parseInt(o.asset_quantity); }); | ||
let asset = JSON.parse(body); | ||
let owners = asset.owners; | ||
let displayTotal = owners.length > 10 ? 10 : owners.length; | ||
let totalAssets = 0; | ||
owners.forEach(owner => { | ||
totalAssets += parseInt(owner.asset_quantity); | ||
owner.assetAddress = addressFromBitcoinAddress(owner.address); | ||
owner.name = addressBook.lookupName(owner.assetAddress) || | ||
owner.assetAddress.substr(0,6)+'...'; | ||
}); | ||
let longestName = owners.map(o => o.name).sort((a,b) => b.length > a.length)[0]; | ||
let nameTargetLength = longestName.length > 9 ? longestName.length : 9; | ||
for (var i=0; i<displayTotal; i++) { | ||
var oaAddress = addressFromBitcoinAddress(owners[i].address); | ||
var name = addressBook.lookupName(oaAddress) || oaAddress; | ||
hearResponse.send(name + ': ' + owners[i].asset_quantity); | ||
let owner = owners[i]; | ||
let padding = ''; | ||
if (owner.name.length < nameTargetLength) { | ||
let spaces = nameTargetLength - owner.name.length; | ||
for (var ii=0; ii<spaces; ii++) { padding += ' '; } | ||
} | ||
hearResponse.send(`${owner.name}${padding} | ${owner.asset_quantity}`); | ||
} | ||
hearResponse.send(`${totalAssets} ${robotKeyword} total, owned by ${owners.length} addresses. details: https://www.coinprism.info/asset/${process.env.OA_ASSET_ID}/owners`); | ||
@@ -205,12 +236,12 @@ }); | ||
let user = hearResponse.message.user; | ||
if (!robot.auth.isAdmin(user)) { | ||
return; | ||
} | ||
var recipient = hearResponse.match[1]; | ||
var destination = addressBook.lookupAddress(recipient); | ||
if(!destination) { return false; } | ||
if (!robot.auth.isAdmin(user)) { return; } | ||
var quantity = process.env.OA_DEFAULT_QUANTITY; | ||
let recipient = hearResponse.match[1]; | ||
let destination = addressBook.lookupAddress(recipient); | ||
if (!destination) { return false; } | ||
let quantity = process.env.OA_DEFAULT_QUANTITY; | ||
sendKredits(destination, quantity, function(err, res, body) { | ||
@@ -227,2 +258,3 @@ if (err || res.statusCode !== 200) { | ||
let user = hearResponse.message.user; | ||
if (!robot.auth.isAdmin(user)) { | ||
@@ -233,12 +265,13 @@ hearResponse.reply('Sorry amigo, I\'m afraid I can not do that.'); | ||
var recipient = hearResponse.match[2]; | ||
var destination = addressBook.lookupAddress(recipient); | ||
if(!destination) { | ||
let recipient = hearResponse.match[2]; | ||
let destination = addressBook.lookupAddress(recipient); | ||
if (!destination) { | ||
hearResponse.reply("sorry, I don't know the address of " + recipient); | ||
return false; | ||
} | ||
var quantity = hearResponse.match[1]; | ||
if(!quantity) { quantity = process.env.OA_DEFAULT_QUANTITY; } | ||
if(process.env.OA_MAX_QUANTITY && quantity > process.env.OA_MAX_QUANTITY) { | ||
let quantity = hearResponse.match[1] || process.env.OA_DEFAULT_QUANTITY; | ||
if (process.env.OA_MAX_QUANTITY && quantity > process.env.OA_MAX_QUANTITY) { | ||
robot.logger.info("quantity exceeds maximum. ignoring"); | ||
@@ -252,11 +285,11 @@ hearResponse.reply("oh, that's a bit too much, isn't it?"); | ||
sendKredits(destination, quantity, function(err, res, body) { | ||
if(err || res.statusCode !== 200) { | ||
if (err || res.statusCode !== 200) { | ||
console.log(err); | ||
console.log(body); | ||
var error = JSON.parse(body); | ||
hearResponse.send("damn, something is wrong with the asset server: " + error.message); | ||
let error = JSON.parse(body); | ||
hearResponse.send("Something is wrong with the asset server: " + error.message); | ||
return false; | ||
} | ||
var tx = JSON.parse(body); | ||
hearResponse.reply(`OK, done! (transaction should be propagated in a bit: https://www.coinprism.info/tx/${tx.hash} )`); | ||
let tx = JSON.parse(body); | ||
hearResponse.reply(`OK, done! (transaction should appear soon: https://www.coinprism.info/tx/${tx.hash} )`); | ||
}); | ||
@@ -269,3 +302,3 @@ | ||
var params = { | ||
let params = { | ||
"from": process.env.OA_ASSET_FROM_ADDRESS, | ||
@@ -277,3 +310,2 @@ "to": destination, | ||
console.log(params); | ||
robot.http(process.env.OA_SERVER_URL + '/send_asset') | ||
@@ -284,3 +316,3 @@ .header('Content-Type', 'application/json') | ||
if(err || res.statusCode !== 200) { | ||
console.log("sending assets failed"); | ||
console.log("sending assets failed", params); | ||
} | ||
@@ -287,0 +319,0 @@ console.log(body); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13485
6
256