Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@automata-network/pom-socket

Package Overview
Dependencies
Maintainers
0
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@automata-network/pom-socket - npm Package Compare versions

Comparing version 0.0.12 to 0.0.13

2

dist/encryption.d.ts
/// <reference path="../src/ecdh.d.ts" />
import ecdh from 'ecdh';
export declare const defaultCurve = "secp256r1";
export declare function encryptMessage(message: string, keyBytes: Uint8Array): {

@@ -15,3 +14,4 @@ encryptedMessage: string;

};
export declare function generateECDHPubKey(sharedPublicNumber: string): ecdh.PublicKey;
export declare function generateAESKey(dhSharedSecret: Uint8Array, hkdfSalt: string): Uint8Array;
export declare function createRandomString(length: number): string;

@@ -1,2 +0,2 @@

"use strict";var e=require("aes-js"),t=require("hashes-grs/hkdf"),r=require("hashes-grs/sha256"),s=require("ecdh");const n="secp256r1";function o(e){const t="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";let r="";for(let s=0;s<e;s++)r+=t.charAt(Math.floor(36*Math.random()));return r}exports.createRandomString=o,exports.decryptMessage=function(t,r,s){const n=e.utils.hex.toBytes(t),o=e.utils.hex.toBytes(s),u=new e.ModeOfOperation.cbc(r,o).decrypt(n);return{decryptedMessage:e.utils.utf8.fromBytes(e.padding.pkcs7.strip(u))}},exports.defaultCurve=n,exports.encryptMessage=function(t,r){const s=e.utils.utf8.toBytes(t),n=e.utils.utf8.toBytes(o(16)),u=new e.ModeOfOperation.cbc(r,n),c=e.padding.pkcs7.pad(s),a=u.encrypt(c);return{encryptedMessage:e.utils.hex.fromBytes(a),iv:e.utils.hex.fromBytes(n)}},exports.generateAESKey=function(e,s){return t.hkdf(r.sha256,e,s,void 0,32)},exports.generateECDHKeys=function(){return s.generateKeys(s.getCurve(n))};
"use strict";var e=require("aes-js"),t=require("hashes-grs/hkdf"),r=require("hashes-grs/sha256"),s=require("ecdh");const n="secp256r1";function o(e){const t="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";let r="";for(let s=0;s<e;s++)r+=t.charAt(Math.floor(36*Math.random()));return r}exports.createRandomString=o,exports.decryptMessage=function(t,r,s){const n=e.utils.hex.toBytes(t),o=e.utils.hex.toBytes(s),u=new e.ModeOfOperation.cbc(r,o).decrypt(n);return{decryptedMessage:e.utils.utf8.fromBytes(e.padding.pkcs7.strip(u))}},exports.encryptMessage=function(t,r){const s=e.utils.utf8.toBytes(t),n=e.utils.utf8.toBytes(o(16)),u=new e.ModeOfOperation.cbc(r,n),c=e.padding.pkcs7.pad(s),i=u.encrypt(c);return{encryptedMessage:e.utils.hex.fromBytes(i),iv:e.utils.hex.fromBytes(n)}},exports.generateAESKey=function(e,s){return t.hkdf(r.sha256,e,s,void 0,32)},exports.generateECDHKeys=function(){return s.generateKeys(s.getCurve(n))},exports.generateECDHPubKey=function(e){return s.PublicKey.fromBuffer(s.getCurve(n),Buffer.from(e,"hex"))};
//# sourceMappingURL=encryption.js.map

@@ -1,2 +0,2 @@

"use strict";var e,t=require("socket.io-client"),s=require("./encryption.js"),n=require("ecdh");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={};ecdh;socket;constructor(e,n){this.ecdh=s.generateECDHKeys(),this.socket=t.io(e,{...n||{},autoConnect:!1,reconnection:!0,reconnectionAttempts:60,reconnectionDelay:5e3})}getSocketStatus(){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 n="object"==typeof this.socket.auth&&!!this.socket.auth.sessionID,c=s.createRandomString(32),o=e=>{try{const t=k({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)}},i=()=>{this.connectedEventReceived=!0,this.connectedEventListener&&(this.connectedEventListener.resolve(),this.connectedEventListener=void 0),this.socket.off("connected",i)};return new Promise(((s,a)=>{const h=()=>{n?E({dhSharedPublicNumber:this.ecdh.publicKey.buffer.toString("hex"),sessionId:this.socket.auth.sessionID,hkdfSalt:c}):e?E({dhSharedPublicNumber:this.ecdh?.publicKey.buffer.toString("hex"),sessionId:e,hkdfSalt:c}):t=!0,this.socket.off("connect",h)},d=s=>{t?E({dhSharedPublicNumber:this.ecdh.publicKey.buffer.toString("hex"),sessionId:s,hkdfSalt:c}):e=s,this.socket.off("setupSession",d)},u=e=>{"io server disconnect"===e?k(r):this.manualReconnectionCount<3?(this.manualReconnectionCount+=1,this.connect().then((e=>{E(e)})).catch((e=>{k(e)}))):(this.manualReconnectionCount=0,k(new Error(e))),this.socket.off("disconnect",d)},f=e=>{"invalid session or expired!"===e.message?k(r):k(e),this.socket.off("connect_error",f)},k=e=>{this.socket.off("connect",h),this.socket.off("setupSession",d),this.socket.off("disconnect",u),this.socket.off("connect_error",f),a(e)},E=e=>{this.socket.off("connect",h),this.socket.off("setupSession",d),this.socket.off("disconnect",u),this.socket.off("connect_error",f),s(e)};this.socket.on("connect",h),this.socket.on("disconnect",u),this.socket.on("connect_error",f),n||this.socket.on("setupSession",d),this.socket.on("connected",i),this.socket.on("data",o),this.socket.connect()}))}generateAESKey(e,t){console.log("sharedPublicNumber1",e);const c=n.PublicKey.fromBuffer(s.defaultCurve,Buffer.from(e,"hex"));console.log("sharedPublicNumber2",c);const o=this.ecdh.privateKey.deriveSharedSecret(c);console.log("sharedPublicNumber3",o),this.aesKey=s.generateAESKey(new Uint8Array(o),t),console.log("sharedPublicNumber4",this.aesKey)}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=E({type:d.KeyExchange,data:this.ecdh.publicKey.buffer.toString("hex")});this.socket.emit("data",e)}disconnect(){this.connectedEventReceived=!1,this.aesKey=void 0,this.ecdh=s.generateECDHKeys(),this.socket.disconnect()}onDisconnect(e){const t=t=>{"io client disconnect"===t||"io server disconnect"===t?e():(this.socket.once("connect",(()=>{s.countdownTimer&&clearTimeout(s.countdownTimer)})),s.countdownTimer=setTimeout((()=>{e()}),3e5))},s={handler:t,cb:e,countdownTimer:void 0};this.listeners.disconnect=this.listeners.disconnect||[],this.listeners.disconnect.push(s),this.socket.on("disconnect",t)}offDisconnect(e){const t=this.listeners.disconnect;if(!t||0===t.length)return;const s=t.findIndex((t=>t.cb===e));if(-1!==s){const e=t[s];this.socket.off("disconnect",e.handler),e.countdownTimer&&clearTimeout(e.countdownTimer),t.splice(s,1)}}on(e){const t=t=>{f({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){const t=t=>{f({message:t,key:this.aesKey,cb:e}),this.off(e)};this.listeners.data=this.listeners.data||[],this.listeners.data.push({handler:t,cb:e}),this.socket.on("data",t)}send(e){if(!this.aesKey)throw c;if(this.getSocketStatus()!==exports.SocketStatus.Connected)throw o;this.socket.emit("data",E({type:d.EncryptedMessage,data:e,key:this.aesKey}))}}function f(e){const{message:t,key:s,cb:n}=e;try{const e=k({message:t,key:s});n.success(e.data)}catch(e){n.error(e)}}function k(e){const{message:t,key:n}=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(!n)throw c;return{type:i,data:s.decryptMessage(o.d,n,o.s).decryptedMessage};default:throw h}}function E(e){const{type:t,data:n,key:o}=e,i={t:t,d:n};switch(t){case d.KeyExchange:break;case d.EncryptedMessage:if(!o)throw c;const e=s.encryptMessage(n,o);i.d=e.encryptedMessage,i.s=e.iv;break;default:throw h}return JSON.stringify(i)}exports.createRandomString=s.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)};
"use strict";var e,t=require("socket.io-client"),s=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 n=new Error("shared secret is not exist"),c=new Error("socket is not connected"),o=new Error("socket connect to server failed"),i=new Error("session is expired"),r=new Error("message salt is missing"),a=new Error("message type is unknown");var h;!function(e){e.KeyExchange="key-exchange",e.EncryptedMessage="encrypted-message"}(h||(h={}));class d{aesKey;manualReconnectionCount=0;connectedEventReceived=!1;connectedEventListener;keyExchangeEventListener;listeners={};ecdh;socket;constructor(e,n){this.ecdh=s.generateECDHKeys(),this.socket=t.io(e,{...n||{},autoConnect:!1,reconnection:!0,reconnectionAttempts:60,reconnectionDelay:5e3})}getSocketStatus(){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 n="object"==typeof this.socket.auth&&!!this.socket.auth.sessionID,c=s.createRandomString(32),o=e=>{try{const t=E({message:e});t.type===h.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)}},r=()=>{this.connectedEventReceived=!0,this.connectedEventListener&&(this.connectedEventListener.resolve(),this.connectedEventListener=void 0),this.socket.off("connected",r)};return new Promise(((s,a)=>{const h=()=>{n?f({dhSharedPublicNumber:this.ecdh.publicKey.buffer.toString("hex"),sessionId:this.socket.auth.sessionID,hkdfSalt:c}):e?f({dhSharedPublicNumber:this.ecdh?.publicKey.buffer.toString("hex"),sessionId:e,hkdfSalt:c}):t=!0,this.socket.off("connect",h)},d=s=>{t?f({dhSharedPublicNumber:this.ecdh.publicKey.buffer.toString("hex"),sessionId:s,hkdfSalt:c}):e=s,this.socket.off("setupSession",d)},u=e=>{"io server disconnect"===e?k(i):this.manualReconnectionCount<3?(this.manualReconnectionCount+=1,this.connect().then((e=>{f(e)})).catch((e=>{k(e)}))):(this.manualReconnectionCount=0,k(new Error(e))),this.socket.off("disconnect",d)},E=e=>{"invalid session or expired!"===e.message?k(i):k(e),this.socket.off("connect_error",E)},k=e=>{this.socket.off("connect",h),this.socket.off("setupSession",d),this.socket.off("disconnect",u),this.socket.off("connect_error",E),a(e)},f=e=>{this.socket.off("connect",h),this.socket.off("setupSession",d),this.socket.off("disconnect",u),this.socket.off("connect_error",E),s(e)};this.socket.on("connect",h),this.socket.on("disconnect",u),this.socket.on("connect_error",E),n||this.socket.on("setupSession",d),this.socket.on("connected",r),this.socket.on("data",o),this.socket.connect()}))}generateAESKey(e,t){const n=s.generateECDHPubKey(e),c=this.ecdh.privateKey.deriveSharedSecret(n);this.aesKey=s.generateAESKey(new Uint8Array(c),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:h.KeyExchange,data:this.ecdh.publicKey.buffer.toString("hex")});this.socket.emit("data",e)}disconnect(){this.connectedEventReceived=!1,this.aesKey=void 0,this.ecdh=s.generateECDHKeys(),this.socket.disconnect()}onDisconnect(e){const t=t=>{"io client disconnect"===t||"io server disconnect"===t?e():(this.socket.once("connect",(()=>{s.countdownTimer&&clearTimeout(s.countdownTimer)})),s.countdownTimer=setTimeout((()=>{e()}),3e5))},s={handler:t,cb:e,countdownTimer:void 0};this.listeners.disconnect=this.listeners.disconnect||[],this.listeners.disconnect.push(s),this.socket.on("disconnect",t)}offDisconnect(e){const t=this.listeners.disconnect;if(!t||0===t.length)return;const s=t.findIndex((t=>t.cb===e));if(-1!==s){const e=t[s];this.socket.off("disconnect",e.handler),e.countdownTimer&&clearTimeout(e.countdownTimer),t.splice(s,1)}}on(e){const t=t=>{u({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){const t=t=>{u({message:t,key:this.aesKey,cb:e}),this.off(e)};this.listeners.data=this.listeners.data||[],this.listeners.data.push({handler:t,cb:e}),this.socket.on("data",t)}send(e){if(!this.aesKey)throw n;if(this.getSocketStatus()!==exports.SocketStatus.Connected)throw c;this.socket.emit("data",k({type:h.EncryptedMessage,data:e,key:this.aesKey}))}}function u(e){const{message:t,key:s,cb:n}=e;try{const e=E({message:t,key:s});n.success(e.data)}catch(e){n.error(e)}}function E(e){const{message:t,key:c}=e,o=JSON.parse(t),i=o.t;switch(i){case h.KeyExchange:return{type:i,data:o.d};case h.EncryptedMessage:if(!o.s)throw r;if(!c)throw n;return{type:i,data:s.decryptMessage(o.d,c,o.s).decryptedMessage};default:throw a}}function k(e){const{type:t,data:c,key:o}=e,i={t:t,d:c};switch(t){case h.KeyExchange:break;case h.EncryptedMessage:if(!o)throw n;const e=s.encryptMessage(c,o);i.d=e.encryptedMessage,i.s=e.iv;break;default:throw a}return JSON.stringify(i)}exports.createRandomString=s.createRandomString,exports.ERR_MESSAGE_SALT_IS_MISSING=r,exports.ERR_MESSAGE_TYPE_IS_UNKNOWN=a,exports.ERR_SESSION_IS_EXPIRED=i,exports.ERR_SHARED_SECRET_NOT_EXIST=n,exports.ERR_SOCKET_CONNECT_TO_SERVER_FAILED=o,exports.ERR_SOCKET_IS_NOT_CONNECTED=c,exports.POMSocket=d,exports.getSocket=function(e,t){return new d(e,t)};
//# sourceMappingURL=index.js.map
{
"name": "@automata-network/pom-socket",
"version": "0.0.12",
"version": "0.0.13",
"description": "A socket clinet for automata's Proof Of Machinehood",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -6,3 +6,3 @@ import aesjs from 'aes-js';

export const defaultCurve = 'secp256r1';
const defaultCurve = 'secp256r1';

@@ -44,2 +44,9 @@ export function encryptMessage(message: string, keyBytes: Uint8Array) {

export function generateECDHPubKey(sharedPublicNumber: string) {
return ecdh.PublicKey.fromBuffer(
ecdh.getCurve(defaultCurve),
Buffer.from(sharedPublicNumber, 'hex'),
);
}
export function generateAESKey(dhSharedSecret: Uint8Array, hkdfSalt: string) {

@@ -46,0 +53,0 @@ const keyLen = 32; // bytes = 256 bits

@@ -5,6 +5,6 @@ import {ManagerOptions, Socket, SocketOptions, io} from 'socket.io-client';

decryptMessage,
defaultCurve,
encryptMessage,
generateAESKey,
generateECDHKeys,
generateECDHPubKey,
} from './encryption';

@@ -248,14 +248,7 @@ import ecdh from 'ecdh';

generateAESKey(sharedPublicNumber: string, hkdfSalt: string) {
console.log('sharedPublicNumber1', sharedPublicNumber);
const pubkey = ecdh.PublicKey.fromBuffer(
defaultCurve,
Buffer.from(sharedPublicNumber, 'hex'),
);
console.log('sharedPublicNumber2', pubkey);
const pubkey = generateECDHPubKey(sharedPublicNumber);
const sharedSecret = this.ecdh.privateKey.deriveSharedSecret(pubkey);
console.log('sharedPublicNumber3', sharedSecret);
this.aesKey = generateAESKey(new Uint8Array(sharedSecret), hkdfSalt);
console.log('sharedPublicNumber4', this.aesKey);
}

@@ -262,0 +255,0 @@

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc