Skip to content

Commit

Permalink
add get message command
Browse files Browse the repository at this point in the history
  • Loading branch information
zyuhel committed Jun 14, 2019
1 parent cf114db commit 210058a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
35 changes: 35 additions & 0 deletions helpers/decrypter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var sodium = require('sodium-browserify-tweetnacl')
var crypto = require('crypto')
var Mnemonic = require('bitcore-mnemonic')
var bignum = require('./bignumber.js')
var keys = require('./keys.js')
var nacl = require('tweetnacl/nacl-fast')
var ed2curve = require('ed2curve')
var ByteBuffer = require('bytebuffer')
const constants = require('./constants.js')
var utf8 = require('@stablelib/utf8')

module.exports = {
bytesToHex: function (bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16))
hex.push((bytes[i] & 0xF).toString(16))
}
return hex.join('')
},
hexToBytes: function (hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2) {
bytes.push(parseInt(hex.substr(c, 2), 16))
}
return bytes
},
decodeMessage: function (msg, nonce, keypair, recipientPublicKey) {

var text = msg
var DHPublicKey = ed2curve.convertPublicKey(new Uint8Array(this.hexToBytes(recipientPublicKey)))
var DHSecretKey = ed2curve.convertSecretKey(keypair.privateKey)
var decrypted = nacl.box.open(text, nonce, DHPublicKey, DHSecretKey)
decrypted = utf8.decode(decrypted)
return decrypted;
}
}
44 changes: 41 additions & 3 deletions ops/get.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/*
*/
const config = require('../helpers/configReader.js')
const keys = require('../helpers/keys.js');
const passArgs = require('../helpers/passArgs.js')
const popsicle = require('popsicle')
var possibleTypes=['account', 'address', 'block', 'blocks', 'delegate', 'transaction', 'transactions']
const decrypter = require('../helpers/decrypter.js')
var possibleTypes=['account', 'address', 'block', 'blocks', 'delegate', 'transaction', 'transactions', 'message']
module.exports=function (vorpal) {
return vorpal.command('get <type> <input> [params...]').description('Fetch info. Available types: address, block, blocks, delegate, transaction, transactions, state').autocomplete(possibleTypes).action(function(args, callback) {
return vorpal.command('get <type> <input> [params...]').allowUnknownOptions().description('Fetch info. Available types: address, block, blocks, delegate, transaction, transactions, state').autocomplete(possibleTypes).action(function(args, callback) {
if (!possibleTypes.includes(args.type)) {
this.log('Not valid type')
if (callback)
Expand All @@ -31,6 +34,9 @@ module.exports=function (vorpal) {
case 'state':
endpoint = '/api/states/get?id=' + args.input
break
case 'message':
endpoint = '/api/transactions/get?returnAsset=1&id=' + args.input
break
case 'delegate':
endpoint = '/api/delegates/get?username=' + args.input
break
Expand Down Expand Up @@ -58,11 +64,43 @@ module.exports=function (vorpal) {
url: config.getNodeConnectString() + endpoint
}).then(function (res) {
var answer = JSON.parse(res.body)
if (args.type=='message') {
if (answer.transaction.asset.chat.own_message) {
var keypair = keys.createKeypairFromPassPhrase(passArgs.getPassPhrase(args))
var reader_address=keys.createAddressFromPublicKey(keypair.publicKey)
if (reader_address!=answer.transaction.senderId && reader_address!=answer.transaction.recipientId) {
self.log("Can't decode message, key is not available");
if (callback)
callback()
return false
}
recipient_name=answer.transaction.senderId
if (recipient_name===reader_address)
recipient_name=answer.transaction.recipientId
return popsicle.request({
method: 'GET',
url: config.getNodeConnectString()+'/api/accounts/getPublicKey?address=' + recipient_name
}).then(function (res) {
var pk_answer = JSON.parse(res.body)
if (pk_answer.success) {
var decoded = decrypter.decodeMessage(Buffer.from(answer.transaction.asset.chat.message,'hex'), Buffer.from(answer.transaction.asset.chat.own_message,'hex'),keypair, pk_answer.publicKey)
answer.transaction.asset.chat.message = decoded
delete answer.transaction.asset.chat.own_message
self.log(JSON.stringify(answer,null,4))
if (callback)
callback()
return answer
}
})

}
}
else
self.log(JSON.stringify(answer,null,4))
if (callback)
callback()
return answer
})
}
})
}
}

0 comments on commit 210058a

Please sign in to comment.