P2P Network

This tool is used to create a peer-to-peer network between a series of servers and clients. In fact it is a NodeJS / Express application that allows the creation of real-time communications signed by Lyra addresses.

This tool is still under development and lacks some features including the discovery of new peers (servers), an efficient message relay system and the stabilization of the message encryption system itself via RSA keys.

IIn theory, this tool should be coupled to a dApp, because every time a message is received it is sent back to a hook, defined in the .env file.

This tool will be natively inserted into IdaNode JS, so that all IdaNodes can communicate with each other and allow communications between various clients.

The basic technology used is that of https://socket.io/.

Installation

Clone the repository and install all dependencies:

git clone https://github.com/scryptachain/scrypta-p2p-network
cd scrypta-p2p-network
npm install

Now you need to create a .env file of this type:

PORT=42224
NODE_KEY=YOUR_LYRA_PRIVKEY
ENCRYPT_COMMUNICATIONS=false
HOOK=http://localhost:9000/p2p-hook
EXPRESS_PORT=4000
BOOTSTRAP_NODES=idanodejs01.scryptachain.org,idanodejs02.scryptachain.org

Use

To start the connection to the network, just execute the command:

npm start

The response from the console will be something like this:

npm start

As we can see, the engine loads the identity written on the .env file and connects to the bootstrap nodes. If something should go wrong with the nodes or with your connection, you will not see the connection lines:

Connected to peer: http://idanodejs01.scryptachain.org:42224
Connected to peer: http://idanodejs02.scryptachain.org:42224

Now it is possible to send messages simply by invoking the endpoint /broadcast!

[POST] /broadcast

This endpoint involves sending the only message field. The message will then be signed by the private key and transformed into JSON with this formatting:

{
signature: '2b37797321241e28b889c3cd1b7eec6bd01d0b47a2a0fff21dc7d873954e0baa7e3b77f860c57ad53ab2009c030b885444cab6f0b94e37988bb3771768f8642e',
pubKey: '027460a5f721e97f5e241d9767fb1b7fd778892e7c40684b79fcb4420d07d240e0',
address: 'LdRQokR1i3XDtj1V3jnCRqMPrVc7sYkeE2',
message: 'Hello P2P!'
}

As in the case of messages signed by Scrypta Core, these also present the signature, the address that sent it, the pubKey to verify the message and the message itself.

At this point all the nodes that receive the message replicate it to the other connected nodes. This system guarantees information relaying. It will send messages both to online nodes with open ports, and to clients, which usually have their inbound connection ports closed.

This type of activity can be analyzed by starting the tool on a remote server, whose port (42224) is open:

enter image description here

Anyone who receives the message, before starting the related post call to the hook, checks that the message has actually been sent by that address and, if so, transmits the message.

Possible use cases

This tool can be used for any application that needs to exchange verified information in real time. Combined with all the other Scrypta technologies, this tool allows the effective sending of information from certified sources and, if desired, by encrypting the information with RSA keys, it is possible to send absolutely private information in real time.

It is important to underline that no information is saved either by IdaNodes, nor by the engine. This means that messages cannot be retrieved and only connected peers can view them. This architectural choice is based on the desire to create a lean system that does not become overloaded over time, leaving the actual need for message storage to the developers who want to use them.