UNPKG

thali

Version:
253 lines (193 loc) 12.1 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: identityExchange/identityExchangeUtils.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: identityExchange/identityExchangeUtils.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>'use strict'; var crypto = require('crypto'); var ThaliReplicationManager = require('../thalireplicationmanager'); var Promise = require('lie'); var urlSafeBase64 = require('urlsafe-base64'); // Exports for Identity Exchange var rnBufferLength = module.exports.rnBufferLength = 16; // We only use the first 16 bytes of the 32 byte hash for space reasons var pkBufferLength = module.exports.pkBufferLength = 16; var cbBufferLength = module.exports.cbBufferLength = 32; var validateAndGetBase64Object = module.exports.validateAndGetBase64Object = function(base64Value, expectedRawLength) { if (!base64Value || typeof base64Value !== 'string') { return null; } var valueBuffer = urlSafeBase64.decode(base64Value); return valueBuffer.length !== expectedRawLength ? null : valueBuffer; }; module.exports.validateRnAndGetBase64Object = function(base64Value) { return validateAndGetBase64Object(base64Value, rnBufferLength); }; module.exports.validatePkAndGetBase64Object = function(base64Value) { return validateAndGetBase64Object(base64Value, pkBufferLength); }; module.exports.validateCbAndGetBase64Object = function(base64Value) { return validateAndGetBase64Object(base64Value, cbBufferLength); }; exports.fourHundredErrorCodes = { notDoingIdentityExchange: 'notDoingIdentityExchange', malformed: 'malformed', wrongPeer: 'wrongPeer', skippedAhead: 'skippedAhead' }; module.exports.cbPath = '/identity/cb'; module.exports.rnMinePath = '/identity/rnmine'; function generateHashBuffer(arrayOfBuffers, key) { var buffer = Buffer.concat(arrayOfBuffers); var hash = crypto.createHmac('sha256', key); hash.write(buffer); hash.end(); return hash.read(); } module.exports.generateCb = function(rnForHash, firstPkBuffer, secondPkBuffer) { return generateHashBuffer([firstPkBuffer, secondPkBuffer], rnForHash); }; module.exports.generateValidationCode = function( rnForHash, firstPkBuffer, secondPkBuffer, rnBufferToHash) { var hashBuffer = generateHashBuffer( [ firstPkBuffer, secondPkBuffer, rnBufferToHash], rnForHash ); return parseInt(hashBuffer.toString('hex'), 16) % Math.pow(10, 6); }; Promise.prototype.thenIfNotInExit = function(self, userFun) { return this.then(function(data) { if (self.smallHashStateMachine.current !== 'Exit') { userFun.call(this, data); } }); }; Promise.prototype.catchIfNotInExit = function(self, userFun) { return this.catch(function(err) { if (self.smallHashStateMachine.current !== 'Exit') { userFun.call(this, err); } }); }; /** * This function is intended primarily for teardown where we want to stop the * Thali Replication Manager and if we never started it, that's o.k. we just * want a NOP. That is normally a great way to hide programming errors. * @param thaliReplicationManager * @returns {Promise|exports|module.exports} */ module.exports.stopThaliReplicationManager = function(thaliReplicationManager) { return new Promise(function(resolve, reject) { var stoppedHandler = function() { thaliReplicationManager.removeListener( ThaliReplicationManager.events.STOP_ERROR, stoppedErrorHandler ); resolve(); }; var stoppedErrorHandler = function(err) { thaliReplicationManager.removeListener( ThaliReplicationManager.events.STOPPED, stoppedHandler ); reject(err || new Error('Unknown Thali replication manager stop error')); }; thaliReplicationManager.once( ThaliReplicationManager.events.STOPPED, stoppedHandler ); thaliReplicationManager.once( ThaliReplicationManager.events.STOP_ERROR, stoppedErrorHandler ); thaliReplicationManager.stop(); }); }; module.exports.startThaliReplicationManager = function(thaliReplicationManager, port, dbName, deviceName) { return new Promise(function(resolve, reject) { var startHandler = function() { thaliReplicationManager.removeListener( ThaliReplicationManager.events.START_ERROR, startHandlerError ); resolve(); }; var startHandlerError = function(err) { thaliReplicationManager.removeListener( ThaliReplicationManager.events.STARTED, startHandler ); reject(err || new Error('Unknown Thali replication manager start error')); }; thaliReplicationManager.once( ThaliReplicationManager.events.STARTED, startHandler ); thaliReplicationManager.once( ThaliReplicationManager.events.START_ERROR, startHandlerError ); thaliReplicationManager.start(port, dbName, deviceName); }); }; module.exports.getDeviceIdentityFromThaliReplicationManager = function(thaliReplicationManager) { return new Promise(function(resolve, reject) { thaliReplicationManager.getDeviceIdentity(function(err, deviceName) { if (err) { return reject(err); } return resolve(deviceName); }); }); }; exports.compareEqualSizeBuffers = function(buffer1, buffer2) { if (!Buffer.isBuffer(buffer1) || !Buffer.isBuffer(buffer2)) { throw new Error('buffer1 and buffer2 have to actually be buffers'); } if (buffer1.length !== buffer2.length) { throw new Error('Buffers must be of the same size.'); } for(var i = 0; i &lt; buffer1.length; ++i) { if (buffer1[i] > buffer2[i]) { return 1; } if (buffer1[i] &lt; buffer2[i]) { return -1; } } return 0; }; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-TCPServersManager.html">TCPServersManager</a></li><li><a href="module-thaliMobile.html">thaliMobile</a></li><li><a href="module-thaliMobileNative.html">thaliMobileNative</a></li><li><a href="module-thaliMobileNativeWrapper.html">thaliMobileNativeWrapper</a></li><li><a href="module-thaliNotificationAction.html">thaliNotificationAction</a></li><li><a href="module-thaliNotificationBeacons.html">thaliNotificationBeacons</a></li><li><a href="module-thaliNotificationClient.html">thaliNotificationClient</a></li><li><a href="module-thaliNotificationServer.html">thaliNotificationServer</a></li><li><a href="module-thaliPeerAction.html">thaliPeerAction</a></li><li><a href="module-thaliPeerDictionary.html">thaliPeerDictionary</a></li><li><a href="module-thaliPeerPoolInterface.html">thaliPeerPoolInterface</a></li><li><a href="module-ThaliWifiInfrastructure.html">ThaliWifiInfrastructure</a></li><li><a href="module-WifiBasedNativeMock.html">WifiBasedNativeMock</a></li></ul><h3>Externals</h3><ul><li><a href="external-_Mobile(_connect_)_.html">Mobile('connect')</a></li><li><a href="external-_Mobile(_discoveryAdvertisingStateUpdateNonTCP_)_.html">Mobile('discoveryAdvertisingStateUpdateNonTCP')</a></li><li><a href="external-_Mobile(_incomingConnectionToPortNumberFailed_)_.html">Mobile('incomingConnectionToPortNumberFailed')</a></li><li><a href="external-_Mobile(_killConnections_)_.html">Mobile('killConnections')</a></li><li><a href="external-_Mobile(_networkChanged_)_.html">Mobile('networkChanged')</a></li><li><a href="external-_Mobile(_peerAvailabilityChanged_)_.html">Mobile('peerAvailabilityChanged')</a></li><li><a href="external-_Mobile(_startListeningForAdvertisements_)_.html">Mobile('startListeningForAdvertisements')</a></li><li><a href="external-_Mobile(_startUpdateAdvertisingAndListening_)_.html">Mobile('startUpdateAdvertisingAndListening')</a></li><li><a href="external-_Mobile(_stopAdvertisingAndListening_)_.html">Mobile('stopAdvertisingAndListening')</a></li><li><a href="external-_Mobile(_stopListeningForAdvertisements_)_.html">Mobile('stopListeningForAdvertisements')</a></li></ul><h3>Classes</h3><ul><li><a href="ConnectionTable.html">ConnectionTable</a></li><li><a href="module-TCPServersManager-TCPServersManager.html">TCPServersManager</a></li><li><a href="module-thaliNotificationAction-NotificationAction.html">NotificationAction</a></li><li><a href="module-thaliNotificationBeacons-ParseBeaconsResponse.html">ParseBeaconsResponse</a></li><li><a href="module-thaliNotificationClient-ThaliNotificationClient.html">ThaliNotificationClient</a></li><li><a href="module-thaliNotificationServer-ThaliNotificationServer.html">ThaliNotificationServer</a></li><li><a href="module-thaliPeerAction-PeerAction.html">PeerAction</a></li><li><a href="module-thaliPeerDictionary-NotificationPeerDictionaryEntry.html">NotificationPeerDictionaryEntry</a></li><li><a href="module-thaliPeerDictionary-PeerConnectionInformation.html">PeerConnectionInformation</a></li><li><a href="module-thaliPeerDictionary-PeerDictionary.html">PeerDictionary</a></li><li><a href="module-thaliPeerPoolInterface-ThaliPeerPoolInterface.html">ThaliPeerPoolInterface</a></li><li><a href="module-ThaliWifiInfrastructure-ThaliWifiInfrastructure.html">ThaliWifiInfrastructure</a></li><li><a href="module-WifiBasedNativeMock-MobileCallInstance.html">MobileCallInstance</a></li><li><a href="module-WifiBasedNativeMock-WifiBasedNativeMock.html">WifiBasedNativeMock</a></li></ul><h3>Events</h3><ul><li><a href="module-thaliMobileNativeWrapper.html#~event:discoveryAdvertisingStateUpdateNonTCPEvent">discoveryAdvertisingStateUpdateNonTCPEvent</a></li><li><a href="module-ThaliWifiInfrastructure.html#~event:discoveryAdvertisingStateUpdateWifiEvent">discoveryAdvertisingStateUpdateWifiEvent</a></li><li><a href="module-TCPServersManager.html#~event:failedConnection">failedConnection</a></li><li><a href="module-thaliMobileNativeWrapper.html#~event:incomingConnectionToPortNumberFailed">incomingConnectionToPortNumberFailed</a></li><li><a href="module-thaliMobileNativeWrapper.html#~event:networkChangedNonTCP">networkChangedNonTCP</a></li><li><a href="module-ThaliWifiInfrastructure.html#~event:networkChangedWifi">networkChangedWifi</a></li><li><a href="module-thaliMobileNativeWrapper.html#~event:nonTCPPeerAvailabilityChangedEvent">nonTCPPeerAvailabilityChangedEvent</a></li><li><a href="module-TCPServersManager.html#~event:routerPortConnectionFailed">routerPortConnectionFailed</a></li><li><a href="module-ThaliWifiInfrastructure.html#~event:wifiPeerAvailabilityChanged">wifiPeerAvailabilityChanged</a></li><li><a href="module-thaliMobile.html#.event:event:discoveryAdvertisingStateUpdate">discoveryAdvertisingStateUpdate</a></li><li><a href="module-thaliMobile.html#.event:event:networkChanged">networkChanged</a></li><li><a href="module-thaliMobile.html#.event:event:peerAvailabilityChanged">peerAvailabilityChanged</a></li><li><a href="module-thaliNotificationAction-NotificationAction.html#.event:event:Resolved">Resolved</a></li><li><a href="module-thaliNotificationClient.html#.event:event:peerAdvertisesDataForUs">peerAdvertisesDataForUs</a></li></ul><h3>Global</h3><ul><li><a href="global.html#getPKCS12Content">getPKCS12Content</a></li><li><a href="global.html#getPublicKeyHash">getPublicKeyHash</a></li><li><a href="global.html#stopThaliReplicationManager">stopThaliReplicationManager</a></li><li><a href="global.html#ThaliEmitter">ThaliEmitter</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon Jan 18 2016 11:19:31 GMT+0200 (EET) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>