~ev/tildeprotocol

febc5d632fec6cd3c1780b70d1bbced5437e9869 — Ev Bogue 4 years ago 9d2eeff
save messages to the db
2 files changed, 37 insertions(+), 3 deletions(-)

M chat.js
M util.js
M chat.js => chat.js +29 -2
@@ 2,7 2,7 @@ var tilde = require('./util')
var readline = require('readline')
var WS = require('ws')
var chalk = require('chalk')

var fs = require('fs')

var rl = readline.createInterface({
  input: process.stdin,


@@ 27,13 27,38 @@ wss.on('connection', function (ws) {
  })
})


if (!fs.existsSync(__dirname + '/db')) {
  var db = []
} else {
  var db = JSON.parse(fs.readFileSync(__dirname + '/db', 'UTF-8'))
}

if (!fs.existsSync(__dirname + '/tildes')) {fs.mkdirSync(__dirname + '/tildes')}

tilde.keys().then(keys => {
  var ws = new WS('ws://tilde.evbogue.com/')

  for (i = 0; i < db.length; i++) {
    var msg = fs.readFileSync(__dirname + '/tildes/' + db[i], 'UTF-8')
    tilde.open(msg, keys).then(opened => {
      var tilde = opened.split('~')
      var msg = ''
      for (i = 0; i < tilde.length - 3; i++) {
        msg = msg + tilde[i] + ' - '
      }
      console.log(chalk.cyan(msg + tilde[tilde.length - 3]))
      console.log('~' + chalk.magenta(tilde[tilde.length - 2]) + '~' + chalk.blackBright(tilde[tilde.length -1]))
    })
  }

  ws.on('open', function () {
    ws.on('message', function (message) {
      tilde.hash(message).then(hash => {
        db.push(hash)
        fs.writeFileSync(__dirname + '/tildes/' + hash, message, 'UTF-8')
      })
      tilde.open(message, keys).then(opened => {
        var tillde
        var tilde = opened.split('~')
        var msg = '' 
        for (i = 0; i < tilde.length - 3; i++) {


@@ 64,6 89,8 @@ tilde.keys().then(keys => {
        ws.send(msg)
      })
    }).on('close', () => {
      fs.writeFileSync(__dirname + '/db', JSON.stringify(db), 'UTF-8')
      console.log('\nWriting db to disk')
      console.log('\nExiting Tilde')
      process.exit(0)
    }) 

M util.js => util.js +8 -1
@@ 5,6 5,12 @@ if ((typeof process !== 'undefined') && (process.release.name === 'node')) {
  var ed2curve = require('ed2curve')
}

async function hash (msg) {
  var hash = nacl.hash(nacl.util.decodeUTF8(msg))
  var hex = Buffer.from(hash).toString('hex')
  return hex
}

async function open (msg, keys) {
  tildekeys = keys.split('~')
  tildemsg = msg.split('~')


@@ 74,6 80,7 @@ if ((typeof process !== 'undefined') && (process.release.name === 'node')) {
  module.exports = {
    keys,
    open,
    publish
    publish,
    hash
  }
}