diff --git a/app/modules/ws2p/lib/WS2PClient.ts b/app/modules/ws2p/lib/WS2PClient.ts index bf0b906f85df5280a658e412b9700566d6040456..b8ce38b93fb055e3b32606ded2b42ac44f2786ad 100644 --- a/app/modules/ws2p/lib/WS2PClient.ts +++ b/app/modules/ws2p/lib/WS2PClient.ts @@ -13,8 +13,8 @@ export class WS2PClient { const c = WS2PConnection.newConnectionToAddress( [host, port].join(':'), messageHandler, - new WS2PPubkeyLocalAuth(k2), - new WS2PPubkeyRemoteAuth(k2) + new WS2PPubkeyLocalAuth(server.conf.currency , k2), + new WS2PPubkeyRemoteAuth(server.conf.currency, k2) ) // Streaming const streamer = new WS2PStreamer(c) diff --git a/app/modules/ws2p/lib/WS2PConnection.ts b/app/modules/ws2p/lib/WS2PConnection.ts index 4e698aa4152ec43b7210158f4e92b13f2c56e331..7aa117da1789a7151baf2334748761ba985407b4 100644 --- a/app/modules/ws2p/lib/WS2PConnection.ts +++ b/app/modules/ws2p/lib/WS2PConnection.ts @@ -74,6 +74,7 @@ export class WS2PPubkeyRemoteAuth implements WS2PRemoteAuth { protected serverAuthReject:(err:any)=>void constructor( + protected currency:string, protected pair:Key, protected tellIsAuthorizedPubkey:(pub: string) => Promise<boolean> = () => Promise.resolve(true) ) { @@ -89,7 +90,7 @@ export class WS2PPubkeyRemoteAuth implements WS2PRemoteAuth { } async sendACK(ws: any): Promise<void> { - const challengeMessage = `WS2P:ACK:${this.pair.pub}:${this.challenge}` + const challengeMessage = `WS2P:ACK:${this.currency}:${this.pair.pub}:${this.challenge}` Logger.log('sendACK >>> ' + challengeMessage) const sig = this.pair.signSync(challengeMessage) await ws.send(JSON.stringify({ @@ -104,7 +105,7 @@ export class WS2PPubkeyRemoteAuth implements WS2PRemoteAuth { if (!allow) { return false } - const challengeMessage = `WS2P:CONNECT:${pub}:${challenge}` + const challengeMessage = `WS2P:CONNECT:${this.currency}:${pub}:${challenge}` Logger.log('registerCONNECT >>> ' + challengeMessage) const verified = verify(challengeMessage, sig, pub) if (verified) { @@ -115,7 +116,7 @@ export class WS2PPubkeyRemoteAuth implements WS2PRemoteAuth { } async registerOK(sig: string): Promise<boolean> { - const challengeMessage = `WS2P:OK:${this.remotePub}:${this.challenge}` + const challengeMessage = `WS2P:OK:${this.currency}:${this.remotePub}:${this.challenge}` Logger.log('registerOK >>> ' + challengeMessage) this.authenticatedByRemote = verify(challengeMessage, sig, this.remotePub) if (!this.authenticatedByRemote) { @@ -147,6 +148,7 @@ export class WS2PPubkeyLocalAuth implements WS2PLocalAuth { protected serverAuthReject:(err:any)=>void constructor( + protected currency:string, protected pair:Key, protected tellIsAuthorizedPubkey:(pub: string) => Promise<boolean> = () => Promise.resolve(true) ) { @@ -158,7 +160,7 @@ export class WS2PPubkeyLocalAuth implements WS2PLocalAuth { } async sendCONNECT(ws:any): Promise<void> { - const challengeMessage = `WS2P:CONNECT:${this.pair.pub}:${this.challenge}` + const challengeMessage = `WS2P:CONNECT:${this.currency}:${this.pair.pub}:${this.challenge}` Logger.log('sendCONNECT >>> ' + challengeMessage) const sig = this.pair.signSync(challengeMessage) await ws.send(JSON.stringify({ @@ -175,7 +177,7 @@ export class WS2PPubkeyLocalAuth implements WS2PLocalAuth { if (!allow) { return false } - const challengeMessage = `WS2P:ACK:${pub}:${this.challenge}` + const challengeMessage = `WS2P:ACK:${this.currency}:${pub}:${this.challenge}` Logger.log('registerACK >>> ' + challengeMessage) this.authenticated = verify(challengeMessage, sig, pub) if (!this.authenticated) { @@ -187,7 +189,7 @@ export class WS2PPubkeyLocalAuth implements WS2PLocalAuth { } async sendOK(ws:any): Promise<void> { - const challengeMessage = `WS2P:OK:${this.pair.pub}:${this.challenge}` + const challengeMessage = `WS2P:OK:${this.currency}:${this.pair.pub}:${this.challenge}` Logger.log('sendOK >>> ' + challengeMessage) const sig = this.pair.signSync(challengeMessage) await ws.send(JSON.stringify({ diff --git a/app/modules/ws2p/lib/WS2PServer.ts b/app/modules/ws2p/lib/WS2PServer.ts index f58fc0ffcd8e86390809e3cd227e945dcaf8472d..90baaa49b0825e6b59eec0bc65beb34c82ad145a 100644 --- a/app/modules/ws2p/lib/WS2PServer.ts +++ b/app/modules/ws2p/lib/WS2PServer.ts @@ -62,8 +62,8 @@ export class WS2PServer extends events.EventEmitter { const c = WS2PConnection.newConnectionFromWebSocketServer( ws, messageHandler, - new WS2PPubkeyLocalAuth(key, acceptPubkey), - new WS2PPubkeyRemoteAuth(key, acceptPubkey), + new WS2PPubkeyLocalAuth(this.server.conf.currency, key, acceptPubkey), + new WS2PPubkeyRemoteAuth(this.server.conf.currency, key, acceptPubkey), { connectionTimeout: WS2PConstants.CONNEXION_TIMEOUT, requestTimeout: WS2PConstants.REQUEST_TIMEOUT diff --git a/test/integration/tools/toolbox.ts b/test/integration/tools/toolbox.ts index 119d8da53482108f3542ef2d3d903df8bf68295e..47febf3ff68b5acffecfab32158dac371221edec 100644 --- a/test/integration/tools/toolbox.ts +++ b/test/integration/tools/toolbox.ts @@ -628,7 +628,7 @@ export class TestingServer { } } -export async function newWS2PBidirectionnalConnection(k1:Key, k2:Key, serverHandler:WS2PMessageHandler) { +export async function newWS2PBidirectionnalConnection(currency:string, k1:Key, k2:Key, serverHandler:WS2PMessageHandler) { let i = 1 let port = PORT++ const wss = new WebSocketServer({ port }) @@ -642,7 +642,7 @@ export async function newWS2PBidirectionnalConnection(k1:Key, k2:Key, serverHand wss.on('connection', async (ws:any) => { switch (i) { case 1: - s1 = WS2PConnection.newConnectionFromWebSocketServer(ws, serverHandler, new WS2PPubkeyLocalAuth(k1), new WS2PPubkeyRemoteAuth(k1), { + s1 = WS2PConnection.newConnectionFromWebSocketServer(ws, serverHandler, new WS2PPubkeyLocalAuth(currency, k1), new WS2PPubkeyRemoteAuth(currency, k1), { connectionTimeout: 100, requestTimeout: 100 }); @@ -662,7 +662,7 @@ export async function newWS2PBidirectionnalConnection(k1:Key, k2:Key, serverHand async answerToRequest(json: any): Promise<WS2PResponse> { return {} } - }), new WS2PPubkeyLocalAuth(k2), new WS2PPubkeyRemoteAuth(k2)) + }), new WS2PPubkeyLocalAuth(currency, k2), new WS2PPubkeyRemoteAuth(currency, k2)) }) } diff --git a/test/integration/ws2p_connection.ts b/test/integration/ws2p_connection.ts index 43f737704edf8e0e5a3e6db63bae395cf19d8888..6708ac7533193264df404fcd48a5ca6551a718e7 100644 --- a/test/integration/ws2p_connection.ts +++ b/test/integration/ws2p_connection.ts @@ -12,6 +12,7 @@ import {WS2PResponse} from "../../app/modules/ws2p/lib/impl/WS2PResponse" const assert = require('assert'); const WebSocketServer = require('ws').Server const logger = require('../../app/lib/logger').NewLogger('ws2p') +const gtest = "gtest" describe('WS2P', () => { @@ -61,7 +62,7 @@ describe('WS2P', () => { } if (obj.auth) { if (nbAsk == 1 || nbAsk == 3) { - const challengeMessage = `WS2P:ACK:${serverKeypair.pub}:${obj.challenge}` + const challengeMessage = `WS2P:ACK:gtest:${serverKeypair.pub}:${obj.challenge}` const sig = serverKeypair.signSync(challengeMessage) if (nbAsk == 1) { ws.send(JSON.stringify({ auth: 'ACK', pub: serverKeypair.pub, sig: 'hiohoihio' })) @@ -90,7 +91,7 @@ describe('WS2P', () => { it('should refuse the connection if the server does not answer', async () => { const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP') - const ws2p = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(keypair), new WS2PPubkeyRemoteAuth(keypair), { + const ws2p = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair), { connectionTimeout: 100, requestTimeout: 100 }) @@ -99,7 +100,7 @@ describe('WS2P', () => { it('should refuse the connection if the server answers with a wrong signature', async () => { const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP') - const ws2p = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(keypair), new WS2PPubkeyRemoteAuth(keypair), { + const ws2p = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair), { connectionTimeout: 100, requestTimeout: 100 }) @@ -108,7 +109,7 @@ describe('WS2P', () => { it('should refuse the connection if the server refuses our signature', async () => { const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP') - const ws2p = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(keypair), new WS2PPubkeyRemoteAuth(keypair), { + const ws2p = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair), { connectionTimeout: 100, requestTimeout: 100 }) @@ -118,7 +119,7 @@ describe('WS2P', () => { it('should accept the connection if the server answers with a good signature', async () => { const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP') - const ws2p = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(keypair), new WS2PNoRemoteAuth(), { + const ws2p = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PNoRemoteAuth(), { connectionTimeout: 1000, requestTimeout: 1000 }) @@ -225,7 +226,7 @@ describe('WS2P', () => { wss.on('connection', async (ws:any) => { switch (i) { case 1: - resolveS1(WS2PConnection.newConnectionFromWebSocketServer(ws, new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(serverKeypair), new WS2PPubkeyRemoteAuth(serverKeypair), { + resolveS1(WS2PConnection.newConnectionFromWebSocketServer(ws, new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, serverKeypair), new WS2PPubkeyRemoteAuth(gtest, serverKeypair), { connectionTimeout: 100, requestTimeout: 100 })); @@ -239,7 +240,7 @@ describe('WS2P', () => { } } - resolveS2(WS2PConnection.newConnectionFromWebSocketServer(ws, new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(serverKeypair), new WS2PPubkeyNotAnsweringWithOKAuth(serverKeypair), { + resolveS2(WS2PConnection.newConnectionFromWebSocketServer(ws, new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, serverKeypair), new WS2PPubkeyNotAnsweringWithOKAuth(gtest, serverKeypair), { connectionTimeout: 100, requestTimeout: 100 })); @@ -247,7 +248,7 @@ describe('WS2P', () => { break case 3: - resolveS3(WS2PConnection.newConnectionFromWebSocketServer(ws, new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(serverKeypair), new WS2PPubkeyRemoteAuth(serverKeypair), { + resolveS3(WS2PConnection.newConnectionFromWebSocketServer(ws, new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, serverKeypair), new WS2PPubkeyRemoteAuth(gtest, serverKeypair), { connectionTimeout: 100, requestTimeout: 100 })); @@ -255,7 +256,7 @@ describe('WS2P', () => { break case 4: - resolveS4(WS2PConnection.newConnectionFromWebSocketServer(ws, new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(serverKeypair), new WS2PPubkeyRemoteAuth(serverKeypair), { + resolveS4(WS2PConnection.newConnectionFromWebSocketServer(ws, new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, serverKeypair), new WS2PPubkeyRemoteAuth(gtest, serverKeypair), { connectionTimeout: 100, requestTimeout: 100 })); @@ -263,13 +264,13 @@ describe('WS2P', () => { break case 5: - resolveS5(WS2PConnection.newConnectionFromWebSocketServer(ws, new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(serverKeypair), new WS2PPubkeyRemoteAuth(serverKeypair))); + resolveS5(WS2PConnection.newConnectionFromWebSocketServer(ws, new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, serverKeypair), new WS2PPubkeyRemoteAuth(gtest, serverKeypair))); (await s5p).connect().catch((e:any) => logger.error('WS2P: newConnectionFromWebSocketServer connection error')) break case 6: - resolveS6(WS2PConnection.newConnectionFromWebSocketServer(ws, new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(serverKeypair), new WS2PPubkeyRemoteAuth(serverKeypair), { + resolveS6(WS2PConnection.newConnectionFromWebSocketServer(ws, new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, serverKeypair), new WS2PPubkeyRemoteAuth(gtest, serverKeypair), { connectionTimeout: 100, requestTimeout: 100 })); @@ -293,7 +294,7 @@ describe('WS2P', () => { } const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP') - const c1 = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(keypair), new WS2PPubkeyNotAnsweringWithACKAuth(keypair)) + const c1 = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyNotAnsweringWithACKAuth(gtest, keypair)) c1.connect().catch((e:any) => logger.error('WS2P: connection error')) const s1 = await s1p await assertThrows(s1.request({ name: 'something' }), "WS2P connection timeout") @@ -301,7 +302,7 @@ describe('WS2P', () => { it('should refuse the connection if the client not confirm with OK', async () => { const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP') - WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(keypair), new WS2PPubkeyRemoteAuth(keypair)) + WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair)) const s2 = await s2p await assertThrows(s2.request({ name: 'something' }), "WS2P connection timeout") }) @@ -321,7 +322,7 @@ describe('WS2P', () => { } const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP') - const c3 = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(keypair), new WS2PPubkeyAnsweringWithWrongSigForACK(keypair)) + const c3 = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyAnsweringWithWrongSigForACK(gtest, keypair)) c3.connect().catch((e:any) => logger.error('WS2P: connection error')) const s3 = await s3p await assertThrows(s3.request({ name: 'something' }), "Wrong signature from server ACK") @@ -344,7 +345,7 @@ describe('WS2P', () => { } const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP') - const c4 = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyRefusingACKSignature(keypair), new WS2PPubkeyRemoteAuth(keypair)) + const c4 = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyRefusingACKSignature(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair)) const s4 = await s4p await assertThrows(c4.connect(), "Wrong signature from server ACK") }) @@ -357,7 +358,7 @@ describe('WS2P', () => { async answerToRequest(json: any): Promise<WS2PResponse> { return { answer: 'success!' } } - }), new WS2PPubkeyLocalAuth(keypair), new WS2PPubkeyRemoteAuth(keypair)) + }), new WS2PPubkeyLocalAuth(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair)) await c5.connect().catch((e:any) => logger.error('WS2P: connection error')) const s5 = await s5p assert.deepEqual({ answer: 'success!' }, await s5.request({ name: 'connection?'} )) @@ -371,7 +372,7 @@ describe('WS2P', () => { } const keypair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP') - const c6 = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyNotAnsweringWithOKAuth(keypair), new WS2PPubkeyRemoteAuth(keypair)) + const c6 = WS2PConnection.newConnectionToAddress('localhost:20903', new WS2PMutedHandler(), new WS2PPubkeyNotAnsweringWithOKAuth(gtest, keypair), new WS2PPubkeyRemoteAuth(gtest, keypair)) c6.connect().catch((e:any) => logger.error('WS2P: connection error')) const s6 = await s6p await assertThrows(s6.request({ name: 'something' }), "WS2P connection timeout") diff --git a/test/integration/ws2p_exchange.ts b/test/integration/ws2p_exchange.ts index 5d7f927e279e5f51e866059ff78105837a15c040..ad94beb03134e35c69e822bf895777adcef4bc78 100644 --- a/test/integration/ws2p_exchange.ts +++ b/test/integration/ws2p_exchange.ts @@ -15,7 +15,7 @@ describe('WS2P exchange', () => { before(async () => { const serverPair = new Key('DKpQPUL4ckzXYdnDRvCRKAm1gNvSdmAXnTrJZ7LvM5Qo', '64EYRvdPpTfLGGmaX5nijLXRqWXaVz8r1Z1GtaahXwVSJGQRn7tqkxLb288zwSYzELMEG5ZhXSBYSxsTsz1m9y8F') const clientPair = new Key('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP') - const res = await newWS2PBidirectionnalConnection(serverPair, clientPair, new (class TestingHandler implements WS2PMessageHandler { + const res = await newWS2PBidirectionnalConnection("gtest", serverPair, clientPair, new (class TestingHandler implements WS2PMessageHandler { async handlePushMessage(json: any): Promise<void> { }