Last active
June 4, 2018 16:34
-
-
Save transducer/dc3e9fdad21c9ad64eabebb2bb5ade88 to your computer and use it in GitHub Desktop.
Wrapper to attach MAM message to Tangle
This file contains hidden or 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
/** | |
* Attach an MAM message. | |
* @function attach | |
* @param {JSON} packet JSON packet to attach. | |
* @returns {Promise} Containing the root or error | |
*/ | |
async attach(packet) { | |
this.logger.info(`Attaching packet ${util.inspect(packet)} to the Tangle`); | |
const trytes = iota.api.toTrytes(JSON.stringify(packet)); | |
const { state, payload, root, address } = MAM.create( | |
this.mamState | |
trytes, | |
); | |
this.mamState = state; | |
try { | |
await MAM.attach(payload, address, this.iotaDepth, this.iotaMinWeightMagnitude); | |
this.logger.info(`Successfully attached to Tangle at address ${address} and root ${root}.`); | |
return root; | |
} catch (err) { | |
this.logger.error(err); | |
throw err; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment