Messaging Application

Understanding the Architecture

  • Decentralized Network: DPSN operates as a decentralized network, with nodes distributed across various locations.

  • Topics: Messages are organized into topics to facilitate routing and subscription.

  • User Accounts: Users have accounts on the DPSN network, associated with public and private key pairs.

  • Socket or MQTT Connections: Users connect to the DPSN network using WebSocket or MQTT connections.

Integration Steps

Publisher Side

  1. Generate a Salt: Create a random salt value.

  2. Encrypt Salt: Encrypt the salt using the recipient's public key.

  3. Publish Salt: Publish the encrypted salt to the designated topic.

  4. Encrypt Messages: Encrypt messages using the salt and the recipient's public key.

  5. Publish Encrypted Messages: Publish encrypted messages to the topic.

const DPSN = require('dpsn-sdk');

// ... (other initialization steps)

// Generate a salt
const salt = generateSalt();

// Encrypt the salt
const encryptedSalt = encryptMessage(salt, recipientPublicKey);

// Publish the encrypted salt
dpsn.publishMessage('project_chat.userB', encryptedSalt);

// ... (publish encrypted messages using the salt)

Send the updated messages with encrypted salt version to the subscriber so that subscriber can decide to skip decryption again to save time or decrypt if the salt was not received.

// Encrypt the message
const encryptedMessage = encryptMessage(message, recipientPublicKey, salt);

// Publish the message and salt
dpsn.publishMessage('project_chat.userB', {
  message: encryptedMessage,
  salt: salt
});

Subscriber Side

  1. Subscribe to Topic: Subscribe to the relevant topic.

  2. Receive and Decrypt Salt: When the initial salt message is received, decrypt it using the private key.

  3. Decrypt Messages: Use the salt and the recipient's private key to decrypt subsequent messages.

const DPSN = require('dpsn-sdk');

// ... (other initialization steps)

dpsn.subscribeToTopic('project_chat.userB', (message) => {
  if (isSaltMessage(message)) {
    // Decrypt and update the salt
    encryptedSalt = message;
    salt = decryptMessage(message, privateKey);
    // Store the salt for future use
  } else {
    // Decrypt the message using the salt
    // optionally check salt with encryptedSalt first to ensure you have the updated salt
    const decryptedMessage = decryptMessage(message.message, salt);
    console.log('Decrypted message:', decryptedMessage);
  }
});

Last updated