~ev/tildeprotocol

e1a1cd02db94f437232a971d43bdacfc72741080 — Ev Bogue 4 years ago 898ceaa
walk backwards and print messages first -- fix unbox
1 files changed, 8 insertions(+), 9 deletions(-)

M util.js
M util.js => util.js +8 -9
@@ 9,13 9,12 @@ async function open (msg) {
  tildemsg = msg.split('~')

  if (tildemsg.length > 2) {
    console.log(tildemsg)
    var pubkey = nacl.util.decodeBase64(tildemsg[1])
    var sig = nacl.util.decodeBase64(tildemsg[2])
    var pubkey = nacl.util.decodeBase64(tildemsg[tildemsg.length - 2])
    var sig = nacl.util.decodeBase64(tildemsg[tildemsg.length - 1])

    var open = nacl.sign.open(sig, pubkey)
    if (open) {
      var opened = msg + '~' + nacl.util.encodeUTF8(open)
      var opened = nacl.util.encodeUTF8(open) + msg
    }

    return opened


@@ 27,7 26,7 @@ async function publish (msg, home, keys) {

  var timestamp = Date.now()

  var signature = nacl.util.encodeBase64(nacl.sign(nacl.util.decodeUTF8(home + '~' + timestamp + '~' + msg), nacl.util.decodeBase64(tildekeys[2])))
  var signature = nacl.util.encodeBase64(nacl.sign(nacl.util.decodeUTF8(msg + '~' + home + '~' + timestamp), nacl.util.decodeBase64(tildekeys[2])))

  var tildemsg = '~' + tildekeys[1] + '~' + signature



@@ 40,7 39,7 @@ async function box (msg, recp, home, keys) {
  var timestamp = Date.now()

  var nonce = nacl.randomBytes(nacl.box.nonceLength)
  var message = nacl.util.decodeUTF8(home + '~' + timestamp + '~' +  msg)
  var message = nacl.util.decodeUTF8(msg + '~' + home + '~' + timestamp)
  var encrypted = nacl.box(message, nonce, ed2curve.convertPublicKey(nacl.util.decodeBase64(recp.substring(1))), ed2curve.convertSecretKey(nacl.util.decodeBase64(tildekeys[2])))
  var tildebox = '~' + tildekeys[1] + '~' + nacl.util.encodeBase64(nonce) + nacl.util.encodeBase64(encrypted)
  return tildebox 


@@ 51,13 50,13 @@ async function unbox (boxed, keys) {

  var tildemsg = boxed.split('~')

  console.log(boxed)
  console.log(tildemsg)

  var nonceMsg = nacl.util.decodeBase64(tildemsg[2])
  var nonceMsg = nacl.util.decodeBase64(tildemsg[tildemsg.length - 1 ])

  var nonce = nonceMsg.slice(0, nacl.box.nonceLength)
  var msg = nonceMsg.slice(nacl.box.nonceLength, nonceMsg.length)
  var message = nacl.util.encodeUTF8(nacl.box.open(msg, nonce, ed2curve.convertPublicKey(nacl.util.decodeBase64(tildekeys[1])), ed2curve.convertSecretKey(nacl.util.decodeBase64(tildekeys[2]))))
  var message = nacl.util.encodeUTF8(nacl.box.open(msg, nonce, ed2curve.convertPublicKey(nacl.util.decodeBase64(tildemsg[tildemsg.length - 2])), ed2curve.convertSecretKey(nacl.util.decodeBase64(tildekeys[2])))) + boxed
  return message
}