Skip to content

Instantly share code, notes, and snippets.

@transducer
Last active June 4, 2018 16:34
Show Gist options
  • Save transducer/dc3e9fdad21c9ad64eabebb2bb5ade88 to your computer and use it in GitHub Desktop.
Save transducer/dc3e9fdad21c9ad64eabebb2bb5ade88 to your computer and use it in GitHub Desktop.
Wrapper to attach MAM message to Tangle
/**
* 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