Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 78 additions & 2 deletions src/destination.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import EventEmitter from "./utils/events.js";
import Constants from "./constants.js";
import Cryptography from "./cryptography.js";
import Fernet from "./fernet.js";
import Packet from "./packet.js";
import Transport from "./transport.js";
import Identity from "./identity.js";
Expand All @@ -13,9 +14,9 @@ import Link from "./link.js";
*/
class Destination extends EventEmitter {

// // constants
// constants
static SINGLE = 0x00;
// static GROUP = 0x01;
static GROUP = 0x01;
static PLAIN = 0x02;
static LINK = 0x03;
// static DESTINATION_TYPES = [this.SINGLE, this.GROUP, this.PLAIN, this.LINK];
Expand Down Expand Up @@ -87,6 +88,42 @@ class Destination extends EventEmitter {
return Destination.hash(identity, appName, ...aspects);
}

/**
* For a GROUP type destination, creates a new symmetric key.
* @returns {Buffer} the generated key
*/
createKeys() {
if(this.type !== Destination.GROUP){
throw new Error("Only GROUP destinations can create symmetric keys");
}
this.privateKeyBytes = Fernet.generateKey();
this.fernet = new Fernet(this.privateKeyBytes);
return this.privateKeyBytes;
}

/**
* For a GROUP type destination, loads a symmetric private key.
* @param key a 32-byte Buffer containing the symmetric key
*/
loadPrivateKey(key) {
if(this.type !== Destination.GROUP){
throw new Error("Only GROUP destinations can hold symmetric keys");
}
this.privateKeyBytes = key;
this.fernet = new Fernet(key);
}

/**
* For a GROUP type destination, returns the symmetric private key.
* @returns {Buffer}
*/
getPrivateKey() {
if(this.type !== Destination.GROUP){
throw new Error("Only GROUP destinations can hold symmetric keys");
}
return this.privateKeyBytes;
}

/**
* Encrypts data for this Destination.
* @param data the data to encrypt
Expand All @@ -105,6 +142,14 @@ class Destination extends EventEmitter {
return this.identity.encrypt(data);
}

// handle group destination type with symmetric key
if(this.type === Destination.GROUP){
if(this.fernet != null){
return this.fernet.encrypt(data);
}
throw new Error("No private key held by GROUP destination. Did you create or load one?");
}

throw new Error("Not Implemented");

}
Expand All @@ -127,6 +172,19 @@ class Destination extends EventEmitter {
return this.identity.decrypt(data);
}

// handle group destination type with symmetric key
if(this.type === Destination.GROUP){
if(this.fernet != null){
try {
return this.fernet.decrypt(data);
} catch(e) {
console.log("The GROUP destination could not decrypt data", e);
return null;
}
}
throw new Error("No private key held by GROUP destination. Did you create or load one?");
}

throw new Error("Not Implemented");

}
Expand All @@ -151,6 +209,16 @@ class Destination extends EventEmitter {

// handle incoming data
if(packet.packetType === Packet.DATA){

// fire packet callback if registered
if(this.packetCallback != null){
try {
this.packetCallback(plaintext, packet);
} catch(e) {
console.log("Error while executing packet callback", e);
}
}

this.emit("packet", {
packet: packet,
data: plaintext,
Expand Down Expand Up @@ -180,6 +248,14 @@ class Destination extends EventEmitter {

}

/**
* Registers a function to be called when a packet has been received by this destination.
* @param callback a function with the signature callback(data, packet)
*/
setPacketCallback(callback) {
this.packetCallback = callback;
}

/**
* Send an announce on all interfaces with the provided app data.
* @param appDataBytes
Expand Down