diodejs 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # diode_js
2
+
3
+ ## Overview
4
+ `diode_js` is a JavaScript client for interacting with the Diode network. It provides functionalities to bind and publish ports, send RPC commands, and handle responses.
5
+
6
+ ## Installation
7
+ ```bash
8
+ npm install diodejs
9
+ ```
10
+ ## Quick Start
11
+
12
+ To get started, you need to generate a device certificate using OpenSSL. You can use this command:
13
+
14
+ ```bash
15
+ openssl ecparam -name secp256k1 -out secp256k1_params.pem
16
+ openssl req -newkey ec:./secp256k1_params.pem -nodes -keyout device_certificate.pem -x509 -days 365 -out device_certificate.pem -subj "/CN=device"
17
+ ```
18
+
19
+ ### Test RPC
20
+
21
+ Here's a quick example to get you started with RPC functions using `DiodeRPC` Class
22
+
23
+ ```javascript
24
+ const { DiodeConnection, DiodeRPC, makeReadable } = require('diodejs');
25
+
26
+ async function main() {
27
+ const host = 'eu2.prenet.diode.io';
28
+ const port = 41046;
29
+ const certPath = 'device_certificate.pem';
30
+
31
+ const connection = new DiodeConnection(host, port, certPath);
32
+ await connection.connect();
33
+
34
+ const rpc = new DiodeRPC(connection);
35
+
36
+ try {
37
+ const ping = await rpc.ping();
38
+ console.log('Ping:', ping);
39
+ const blockPeak = await rpc.getBlockPeak();
40
+ console.log('Current Block Peak:', blockPeak);
41
+
42
+ const blockHeader = await rpc.getBlockHeader(blockPeak);
43
+ console.log('Block Header:', makeReadable(blockHeader));
44
+ } catch (error) {
45
+ console.error('RPC Error:', error);
46
+ } finally {
47
+ connection.close();
48
+ }
49
+ }
50
+
51
+ main();
52
+
53
+ ```
54
+
55
+ ### Bind Port
56
+ Here's a quick example to get you started with port forwarding using the `BindPort` class.
57
+
58
+ ```javascript
59
+ const { DiodeConnection, BindPort } = require('diodejs');
60
+
61
+ async function main() {
62
+ const host = 'eu2.prenet.diode.io';
63
+ const port = 41046;
64
+ const certPath = 'device_certificate.pem';
65
+
66
+ const connection = new DiodeConnection(host, port, certPath);
67
+ await connection.connect();
68
+
69
+ const portForward = new BindPort(connection, 3002, 80, "5365baf29cb7ab58de588dfc448913cb609283e2");
70
+ portForward.bind();
71
+
72
+ }
73
+
74
+ main();
75
+ ```
76
+ ### Publish Port
77
+
78
+ Here's a quick example to get you started with publishing ports using the `PublishPort` class:
79
+
80
+ ```javascript
81
+ const { DiodeConnection, PublishPort } = require('diodejs');
82
+
83
+ async function main() {
84
+ const host = 'us2.prenet.diode.io';
85
+ const port = 41046;
86
+ const certPath = 'device_certificate.pem';
87
+
88
+ const connection = new DiodeConnection(host, port, certPath);
89
+ await connection.connect();
90
+
91
+ const publishedPorts = [8080]; // Ports you want to publish
92
+ const publishPort = new PublishPort(connection, publishedPorts, certPath);
93
+
94
+ }
95
+
96
+ main();
97
+
98
+ ```
package/bindPort.js ADDED
@@ -0,0 +1,120 @@
1
+ const net = require('net');
2
+ const { Buffer } = require('buffer');
3
+ const DiodeRPC = require('./rpc');
4
+
5
+ class BindPort {
6
+ constructor(connection, localPort, targetPort,deviceIdHex) {
7
+ this.connection = connection;
8
+ this.localPort = localPort;
9
+ this.targetPort = targetPort;
10
+ this.deviceIdHex = deviceIdHex;
11
+ }
12
+
13
+ bind () {
14
+ const deviceId = Buffer.from(this.deviceIdHex, 'hex');
15
+ const clientSockets = new Map();
16
+
17
+ const rpc = new DiodeRPC(this.connection);
18
+
19
+ // Listen for data events from the device
20
+ this.connection.on('unsolicited', (message) => {
21
+ // message is [messageId, [messageType, ...]]
22
+ const [messageIdRaw, messageContent] = message;
23
+ const messageTypeRaw = messageContent[0];
24
+ const messageType = Buffer.from(messageTypeRaw).toString('utf8');
25
+
26
+ if (messageType === 'data' || messageType === 'portsend') {
27
+ const refRaw = messageContent[1];
28
+ const dataRaw = messageContent[2];
29
+
30
+ const dataRef = Buffer.from(refRaw);
31
+ const data = Buffer.from(dataRaw);
32
+
33
+ // Find the associated client socket
34
+ const clientSocket = clientSockets.get(dataRef.toString('hex'));
35
+ if (clientSocket) {
36
+ clientSocket.write(data);
37
+ } else {
38
+ console.warn(`No client socket found for ref: ${dataRef.toString('hex')}`);
39
+ }
40
+ } else if (messageType === 'portclose') {
41
+ const refRaw = messageContent[1];
42
+
43
+ const dataRef = Buffer.from(refRaw);
44
+
45
+ // Close the associated client socket
46
+ const clientSocket = clientSockets.get(dataRef.toString('hex'));
47
+ if (clientSocket) {
48
+ clientSocket.end();
49
+ clientSockets.delete(dataRef.toString('hex'));
50
+ console.log(`Port closed for ref: ${dataRef.toString('hex')}`);
51
+ }
52
+ } else {
53
+ console.warn(`Unknown unsolicited message type: ${messageType}`);
54
+ }
55
+ });
56
+
57
+ // Set up local server
58
+ const server = net.createServer(async (clientSocket) => {
59
+ console.log('Client connected to local server');
60
+
61
+ // Open a new port on the device for this client
62
+ let ref;
63
+ try {
64
+ ref = await rpc.portOpen(deviceId, this.targetPort, 'rw');
65
+ console.log(`Port opened on device with ref: ${ref.toString('hex')} for client`);
66
+ } catch (error) {
67
+ console.error('Error opening port on device:', error);
68
+ clientSocket.destroy();
69
+ return;
70
+ }
71
+
72
+ // Store the client socket with the ref (using hex string as key)
73
+ clientSockets.set(ref.toString('hex'), clientSocket);
74
+
75
+ // When data is received from the client, send it to the device
76
+ clientSocket.on('data', async (data) => {
77
+ try {
78
+ await rpc.portSend(ref, data);
79
+ } catch (error) {
80
+ console.error('Error sending data to device:', error);
81
+ clientSocket.destroy();
82
+ }
83
+ });
84
+
85
+ // Handle client socket closure
86
+ clientSocket.on('end', async () => {
87
+ console.log('Client disconnected');
88
+ clientSockets.delete(ref.toString('hex'));
89
+ try {
90
+ console.log(`Port closed on device for ref: ${ref.toString('hex')}`);
91
+ } catch (error) {
92
+ console.error('Error closing port on device:', error);
93
+ }
94
+ });
95
+
96
+ // Handle client socket errors
97
+ clientSocket.on('error', (err) => {
98
+ console.error('Client socket error:', err);
99
+ });
100
+ });
101
+
102
+ server.listen(this.localPort, () => {
103
+ console.log(`Local server listening on port ${this.localPort}`);
104
+ });
105
+
106
+ // Handle device disconnect
107
+ this.connection.on('end', () => {
108
+ console.log('Disconnected from Diode.io server');
109
+ server.close();
110
+ });
111
+
112
+ // Handle connection errors
113
+ this.connection.on('error', (err) => {
114
+ console.error('Connection error:', err);
115
+ server.close();
116
+ });
117
+ }
118
+ }
119
+
120
+ module.exports = BindPort;