0.1.0 • Published 2 years ago
kmes v0.1.0
node-kmes
Kigin Message Exchange System in Node.js.
Example
(async () => {
// Import KMES and some tools
const kmes = require("kmes");
const fs = require("fs");
const path = require("path");
// Our "database"
let messages = [];
// Initialize KMES
const system = kmes({
// Define how to store the messages
store(content, from, to) {
// content = Encrypted message
// from = Them
// to = You
messages.push({ from, to, content, time: Math.floor(Date.now() / 1000) });
messages = messages.sort((a, b) => b.time - a.time);
},
// Get an array of (encrypted) messages that have been sent from [from] to [to]
get(from, to, decrypt) {
// from = Them
// to = You
// decrypt = Function to decrypt the messages
// Get all messages that have been sent from [from] to [to]
const encryptedInbox = messages.filter((item) => {
if (item.from == from && item.to == to) return true;
else return false;
});
let inbox = [];
// Iterate over all encrypted messages and decrypt them
for (const item of encryptedInbox) {
let newItem = item;
newItem.content = decrypt(item.content);
inbox.push(newItem);
}
// Return array of decrypted messages (inbox)
return inbox;
}
});
// Import public and private keys of both Alice and Bob
const alicePublic = fs.readFileSync(path.join(__dirname, "./keys/alice.public"), "utf-8");
const alicePrivate = fs.readFileSync(path.join(__dirname, "./keys/alice.private"), "utf-8");
const bobPublic = fs.readFileSync(path.join(__dirname, "./keys/bob.public"), "utf-8");
const bobPrivate = fs.readFileSync(path.join(__dirname, "./keys/bob.private"), "utf-8");
// Alice sends 2 messages to Bob
system.send(alicePublic, bobPublic, alicePrivate, "Hey honey!");
system.send(alicePublic, bobPublic, alicePrivate, "Can you pick up milk from the grocery store?");
// Get inbox
const results = await system.inbox(alicePublic, bobPublic, bobPrivate);
for (const item of results) {
console.log(item.content);
// "Hey honey!"
// "Can you pick up milk from the grocery store?"
}
})();
0.1.0
2 years ago