Skip to content

karsrhyder/variations-on-whisper

Repository files navigation

variations-on-whisper

Persistent messages (peer-storage)

1. Use case

------------------------> t A .[===].................. B ................[===]... C ..[===================].

  • A broadcasts msg directed to B.
    • B doesn't get it because it expires before logging in.
    • C gets it and stores it. It should not be aware of any info not necessary to resend it.
  • B goes online,
    • and sends a signal to the network: "Hi, send my pending messages"
    • C is able to retrieve the message and broadcasts it to the network, which B will pick up

2.Implementation

Messages are encrypted on whisper with a symmetric key common to the SwarmCity app. The payload will be also encrypted:

// The hash should refer to a specific conversation and member of the conversation.
// It should not be derivable from the outside

import nacl from "tweetnacl";
import naclUtil from "tweetnacl-util";
nacl.util = naclUtil;
// Hash
const cipher = crypt.encrypt(message, chatPrivateKey);
const payload = msg.regular.encode(chatPrivateKey, recepientAddress, cipher);
broadcast(payload);
//

B "I'm online message" Should send the hash and prove that's this message belong to him

const payload = encode(chatPrivateKey, yourAddress, privateKey);
broadcast(payload);

Server verification

// fire this on an I'm online msg
on("imOnlineMsg", payload => {
  // Ignore unauthorized messages
  if (!msg.online.authorize(payload)) {
    // Maybe punishable action could be taken against this user
    return console.log(
      "Unauthorized user attempted to access messages of hash: " + payload
    );
  }
  const { hash } = msg.online.decode(payload);

  // Search the database for matching msgs
  const msgsToSend = msgs.filter(msg => msg.hash === hash);

  // Send messages and prune
  broadcast(msgsToSend);
  pruneMsg(msgsToSend);
});

User reception

// compute vars
on("msg", payload => {
  const { hash, cipher } = msg.regular.decode(payload);
  if (hash === hashOfInterest) {
    useInTheApplication(crypt.encrypt(cipher, chatPrivateKey));
  } else {
    store({ hash, cipher });
  }
});

About

Trials with Whisper

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published