Created
June 4, 2018 16:39
-
-
Save transducer/d25dd511c561179e21d8d516ae99a5a8 to your computer and use it in GitHub Desktop.
Process MAM message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Retrieves and processes MAM messages by dispatching the type of message to | |
* its handler. | |
* | |
* @function processMamMessage | |
* @returns {undefined} | |
*/ | |
async processMamMessage() { | |
const IS_PAIRED = (typeof this.root !== 'undefined'); | |
if (!IS_PAIRED) { | |
logger.info('IOTA MAM: No root received (device not paired)'); | |
return; | |
} | |
logger.info(`IOTA MAM: Fetching from root ${DeviceClient.formatTrytes(this.root)}`); | |
try { | |
const mode = 'private'; | |
const mamMsg = await this.mam.fetchSingle(this.root, mode); | |
if (typeof mamMsg === 'undefined') { | |
// no message, you can try again later, keep root | |
logger.info('No new MAM message'); | |
return; | |
} | |
const { nextRoot, message } = mamMsg; | |
switch (message.type) { | |
case AUTHORIZED_TYPE: { | |
this.processAuthorizedMessage(message); | |
break; | |
} | |
case AUTHORIZATION_REVOKED_TYPE: { | |
this.processAuthorizationRevokedMessage(message); | |
break; | |
} | |
case DEVICE_ADDED_TYPE: { | |
this.authorizedServiceProviders.clear(); | |
break; | |
} | |
case DEVICE_DELETED_TYPE: { | |
this.authorizedServiceProviders.clear(); | |
break; | |
} | |
default: { | |
logger.info(`Unknown MAM msg type: ${message.type}`); | |
} | |
} | |
logger.info(`IOTA MAM: Setting root to ${DeviceClient.formatTrytes(nextRoot)}`); | |
this.root = nextRoot; | |
} catch (err) { | |
logger.error(`In processMamMessage: ${err}`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment