@automata-network/pom-socket
Advanced tools
Comparing version 0.0.1 to 0.0.2
import { Client } from 'diffie-hellman-ts'; | ||
import { Socket, ManagerOptions, SocketOptions } from 'socket.io-client'; | ||
declare function createRandomString(length: number): string; | ||
interface SocketConnectionResult { | ||
@@ -49,2 +51,2 @@ sessionId: string; | ||
export { type Callback, ERR_MESSAGE_SALT_IS_MISSING, ERR_MESSAGE_TYPE_IS_UNKNOWN, ERR_SESSION_IS_EXPIRED, ERR_SHARED_SECRET_NOT_EXIST, ERR_SOCKET_CONNECT_TO_SERVER_FAILED, ERR_SOCKET_IS_NOT_CONNECTED, POMSocket, type SocketConnectionResult, SocketStatus, getSocket }; | ||
export { type Callback, ERR_MESSAGE_SALT_IS_MISSING, ERR_MESSAGE_TYPE_IS_UNKNOWN, ERR_SESSION_IS_EXPIRED, ERR_SHARED_SECRET_NOT_EXIST, ERR_SOCKET_CONNECT_TO_SERVER_FAILED, ERR_SOCKET_IS_NOT_CONNECTED, POMSocket, type SocketConnectionResult, SocketStatus, createRandomString, getSocket }; |
@@ -1,2 +0,2 @@ | ||
"use strict";var e,t=require("diffie-hellman-ts"),s=require("socket.io-client"),n=require("./encryption.js");exports.SocketStatus=void 0,(e=exports.SocketStatus||(exports.SocketStatus={}))[e.Disconnected=1]="Disconnected",e[e.Connecting=2]="Connecting",e[e.Connected=3]="Connected";const c=new Error("shared secret is not exist"),o=new Error("socket is not connected"),i=new Error("socket connect to server failed"),r=new Error("session is expired"),a=new Error("message salt is missing"),h=new Error("message type is unknown");var d;!function(e){e.KeyExchange="key-exchange",e.EncryptedMessage="encrypted-message"}(d||(d={}));class u{aesKey;manualReconnectionCount=0;connectedEventReceived=!1;connectedEventListener;keyExchangeEventListener;listeners={};dhClient;sharedPublicNumber;socket;constructor(e,n){this.dhClient=new t.Client,this.sharedPublicNumber=this.dhClient.generatePublicNumber(),this.socket=s.io(e,{...n||{},autoConnect:!1})}get status(){return this.socket.connected&&this.aesKey&&this.connectedEventReceived?exports.SocketStatus.Connected:this.socket.disconnected?exports.SocketStatus.Disconnected:exports.SocketStatus.Connecting}connect(){let e,t;const s="object"==typeof this.socket.auth&&!!this.socket.auth.sessionID,c=n.createRandomString(32),o=e=>{try{const t=y({message:e});t.type===d.KeyExchange&&(this.generateAESKey(t.data,c),this.socket.off("data",o),this.keyExchangeEventListener&&this.keyExchangeEventListener.resolve())}catch(e){this.keyExchangeEventListener&&this.keyExchangeEventListener.reject(e),this.socket.off("data",o)}};return new Promise(((n,i)=>{this.socket.once("connect",(()=>{s?n({dhSharedPublicNumber:this.sharedPublicNumber,sessionId:this.socket.auth.sessionID,hkdfSalt:c}):e?n({dhSharedPublicNumber:this.sharedPublicNumber,sessionId:e,hkdfSalt:c}):t=!0})),s||this.socket.once("setupSession",(s=>{t?n({dhSharedPublicNumber:this.sharedPublicNumber,sessionId:s,hkdfSalt:c}):e=s})),this.socket.once("disconnect",(e=>{this.manualReconnectionCount<3?(this.manualReconnectionCount+=1,this.connect().then((e=>{n(e)})).catch((e=>{i(e)}))):(this.manualReconnectionCount=0,i(new Error(e)))})),this.socket.once("connect_error",(e=>{i(e)})),this.socket.once("connected",(()=>{this.connectedEventReceived=!0,this.connectedEventListener&&(this.connectedEventListener.resolve(),this.connectedEventListener=void 0)})),this.socket.on("data",o),this.socket.connect()}))}generateAESKey(e,t){const s=this.dhClient.generateSharedSecret(e);this.aesKey=n.generateAESKey(s,t)}async waitForConnectedEvent(){return this.connectedEventReceived?Promise.resolve():new Promise(((e,t)=>{this.connectedEventListener={resolve:e,reject:t}}))}async waitForKeyExchangeEvent(){return this.aesKey?Promise.resolve():new Promise(((e,t)=>{this.keyExchangeEventListener={resolve:e,reject:t}}))}emitSharedPublicKey(){const e=k({type:d.KeyExchange,data:this.sharedPublicNumber});this.socket.emit("data",e)}disconnect(){this.socket.disconnect(),this.connectedEventReceived=!1,this.aesKey=void 0,this.sharedPublicNumber=this.dhClient.generatePublicNumber()}on(e){const t=t=>{E({message:t,key:this.aesKey,cb:e})};this.listeners.data=this.listeners.data||[],this.listeners.data.push({handler:t,cb:e}),this.socket.on("data",t)}off(e){const t=this.listeners.data;if(!t||0===t.length)return;const s=t.findIndex((t=>t.cb===e));-1!==s&&(this.socket.off("data",t[s].handler),t.splice(s,1))}once(e){this.socket.once("data",(t=>{E({message:t,key:this.aesKey,cb:e})}))}send(e){if(!this.aesKey)throw c;if(this.status!==exports.SocketStatus.Connected)throw o;this.socket.emit("data",k({type:d.EncryptedMessage,data:e,key:this.aesKey}))}}function E(e){const{message:t,key:s,cb:n}=e;try{const e=y({message:t,key:s});n.success(e.data)}catch(e){n.error(e)}}function y(e){const{message:t,key:s}=e,o=JSON.parse(t),i=o.t;switch(i){case d.KeyExchange:return{type:i,data:o.d};case d.EncryptedMessage:if(!o.s)throw a;if(!s)throw c;return{type:i,data:n.decryptMessage(o.d,s,o.s).decryptedMessage};default:throw h}}function k(e){const{type:t,data:s,key:o}=e,i={t:t,d:s};switch(t){case d.KeyExchange:break;case d.EncryptedMessage:if(!o)throw c;const e=n.encryptMessage(s,o);i.d=e.encryptedMessage,i.s=e.iv;break;default:throw h}return JSON.stringify(i)}exports.ERR_MESSAGE_SALT_IS_MISSING=a,exports.ERR_MESSAGE_TYPE_IS_UNKNOWN=h,exports.ERR_SESSION_IS_EXPIRED=r,exports.ERR_SHARED_SECRET_NOT_EXIST=c,exports.ERR_SOCKET_CONNECT_TO_SERVER_FAILED=i,exports.ERR_SOCKET_IS_NOT_CONNECTED=o,exports.POMSocket=u,exports.getSocket=function(e,t){return new u(e,t)}; | ||
"use strict";var e,t=require("diffie-hellman-ts"),s=require("socket.io-client"),n=require("./encryption.js");exports.SocketStatus=void 0,(e=exports.SocketStatus||(exports.SocketStatus={}))[e.Disconnected=1]="Disconnected",e[e.Connecting=2]="Connecting",e[e.Connected=3]="Connected";const c=new Error("shared secret is not exist"),o=new Error("socket is not connected"),i=new Error("socket connect to server failed"),r=new Error("session is expired"),a=new Error("message salt is missing"),h=new Error("message type is unknown");var d;!function(e){e.KeyExchange="key-exchange",e.EncryptedMessage="encrypted-message"}(d||(d={}));class u{aesKey;manualReconnectionCount=0;connectedEventReceived=!1;connectedEventListener;keyExchangeEventListener;listeners={};dhClient;sharedPublicNumber;socket;constructor(e,n){this.dhClient=new t.Client,this.sharedPublicNumber=this.dhClient.generatePublicNumber(),this.socket=s.io(e,{...n||{},autoConnect:!1})}get status(){return this.socket.connected&&this.aesKey&&this.connectedEventReceived?exports.SocketStatus.Connected:this.socket.disconnected?exports.SocketStatus.Disconnected:exports.SocketStatus.Connecting}connect(){let e,t;const s="object"==typeof this.socket.auth&&!!this.socket.auth.sessionID,c=n.createRandomString(32),o=e=>{try{const t=y({message:e});t.type===d.KeyExchange&&(this.generateAESKey(t.data,c),this.socket.off("data",o),this.keyExchangeEventListener&&this.keyExchangeEventListener.resolve())}catch(e){this.keyExchangeEventListener&&this.keyExchangeEventListener.reject(e),this.socket.off("data",o)}};return new Promise(((n,i)=>{this.socket.once("connect",(()=>{s?n({dhSharedPublicNumber:this.sharedPublicNumber,sessionId:this.socket.auth.sessionID,hkdfSalt:c}):e?n({dhSharedPublicNumber:this.sharedPublicNumber,sessionId:e,hkdfSalt:c}):t=!0})),s||this.socket.once("setupSession",(s=>{t?n({dhSharedPublicNumber:this.sharedPublicNumber,sessionId:s,hkdfSalt:c}):e=s})),this.socket.once("disconnect",(e=>{this.manualReconnectionCount<3?(this.manualReconnectionCount+=1,this.connect().then((e=>{n(e)})).catch((e=>{i(e)}))):(this.manualReconnectionCount=0,i(new Error(e)))})),this.socket.once("connect_error",(e=>{i(e)})),this.socket.once("connected",(()=>{this.connectedEventReceived=!0,this.connectedEventListener&&(this.connectedEventListener.resolve(),this.connectedEventListener=void 0)})),this.socket.on("data",o),this.socket.connect()}))}generateAESKey(e,t){const s=this.dhClient.generateSharedSecret(e);this.aesKey=n.generateAESKey(s,t)}async waitForConnectedEvent(){return this.connectedEventReceived?Promise.resolve():new Promise(((e,t)=>{this.connectedEventListener={resolve:e,reject:t}}))}async waitForKeyExchangeEvent(){return this.aesKey?Promise.resolve():new Promise(((e,t)=>{this.keyExchangeEventListener={resolve:e,reject:t}}))}emitSharedPublicKey(){const e=k({type:d.KeyExchange,data:this.sharedPublicNumber});this.socket.emit("data",e)}disconnect(){this.socket.disconnect(),this.connectedEventReceived=!1,this.aesKey=void 0,this.sharedPublicNumber=this.dhClient.generatePublicNumber()}on(e){const t=t=>{E({message:t,key:this.aesKey,cb:e})};this.listeners.data=this.listeners.data||[],this.listeners.data.push({handler:t,cb:e}),this.socket.on("data",t)}off(e){const t=this.listeners.data;if(!t||0===t.length)return;const s=t.findIndex((t=>t.cb===e));-1!==s&&(this.socket.off("data",t[s].handler),t.splice(s,1))}once(e){this.socket.once("data",(t=>{E({message:t,key:this.aesKey,cb:e})}))}send(e){if(!this.aesKey)throw c;if(this.status!==exports.SocketStatus.Connected)throw o;this.socket.emit("data",k({type:d.EncryptedMessage,data:e,key:this.aesKey}))}}function E(e){const{message:t,key:s,cb:n}=e;try{const e=y({message:t,key:s});n.success(e.data)}catch(e){n.error(e)}}function y(e){const{message:t,key:s}=e,o=JSON.parse(t),i=o.t;switch(i){case d.KeyExchange:return{type:i,data:o.d};case d.EncryptedMessage:if(!o.s)throw a;if(!s)throw c;return{type:i,data:n.decryptMessage(o.d,s,o.s).decryptedMessage};default:throw h}}function k(e){const{type:t,data:s,key:o}=e,i={t:t,d:s};switch(t){case d.KeyExchange:break;case d.EncryptedMessage:if(!o)throw c;const e=n.encryptMessage(s,o);i.d=e.encryptedMessage,i.s=e.iv;break;default:throw h}return JSON.stringify(i)}exports.createRandomString=n.createRandomString,exports.ERR_MESSAGE_SALT_IS_MISSING=a,exports.ERR_MESSAGE_TYPE_IS_UNKNOWN=h,exports.ERR_SESSION_IS_EXPIRED=r,exports.ERR_SHARED_SECRET_NOT_EXIST=c,exports.ERR_SOCKET_CONNECT_TO_SERVER_FAILED=i,exports.ERR_SOCKET_IS_NOT_CONNECTED=o,exports.POMSocket=u,exports.getSocket=function(e,t){return new u(e,t)}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@automata-network/pom-socket", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A socket clinet for automata's Proof Of Machinehood", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -10,2 +10,4 @@ import {Client} from 'diffie-hellman-ts'; | ||
export {createRandomString} from './encryption'; | ||
export interface SocketConnectionResult { | ||
@@ -12,0 +14,0 @@ sessionId: string; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39112
462