forked from LynxLine/node-red-contrib-iota-tx0
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmamPublish.js
63 lines (59 loc) · 3.09 KB
/
mamPublish.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const MAM = require('@iota/mam');
const IOTA_CONVERTER = require('@iota/converter');
const { isTrytes } = require('@iota/validators');
module.exports = function(RED) {
function mamPublish(config) {
RED.nodes.createNode(this,config);
var node = this;
node.iotaNode = RED.nodes.getNode(config.iotaNode);
//const iota = new IOTA({'host': this.iotaNode.host, 'port': this.iotaNode.port});
//console.log("MAM publish INIT on iota node: " + node.iotaNode.host + ":" + node.iotaNode.port);
if (config.channelseed != null && config.channelseed != "") {
if (isTrytes(config.channelseed,81)) {
//console.log("Right User Channel Seed");
} else {
config.channelseed = null;
//console.log("Wrong user Channel Seed, generated random seed");
}
}
node._state = MAM.init({ provider: node.iotaNode.host, 'port': node.iotaNode.port },config.channelseed,2);
//console.log('GetRootInit: ' + MAM.getRoot(node._state));
node._state = MAM.changeMode(node._state, config.mode, config.sidkey);
//console.log('GetRootMode: ' + MAM.getRoot(node._state));
node.tag = config.tag;
node.readyMAM = true;
node.arrayPackets = [];
node.mamLink = 'https://mam-explorer.firebaseapp.com/?provider=' + node.iotaNode.host + ':' + node.iotaNode.port + '&mode=' + config.mode + '&root=';
node.tangleLink = 'https://thetangle.org/address/'
node.on('input', function(msg) {
let trytestag = IOTA_CONVERTER.asciiToTrytes(JSON.stringify(node.tag));
const packet = { time: Date.now(), tag: trytestag, data: msg.payload };
this.arrayPackets.push(packet);
//console.log(this.arrayPackets.length);
//console.log(JSON.stringify(this.arrayPackets));
//console.log(this.readyMAM);
if (this.readyMAM) {
this.status({fill:"red",shape:"ring",text:"publishing"});
this.readyMAM = false;
let trytes = IOTA_CONVERTER.asciiToTrytes(JSON.stringify(this.arrayPackets));
let message = MAM.create(this._state, trytes);
// Update the mam state so we can keep adding messages.
this._state = message.state;
//console.log("Uploading dataset via MAM Attach.");
let link2 = node.tangleLink + message.address;
let resp = MAM.attach(message.payload, message.address,4,14,trytestag);
resp.then(function(result) {
//console.log(result); //will log results.
//console.log('Verify with MAM Explorer: ' + node.mamLink + message.root);
let link = node.mamLink + message.root;
node.send({payload: {address:message.address, root:message.root, state:message.state, link:link, tangleLink:link2}});
});
this.status({});
this.readyMAM = true;
this.arrayPackets = [];
} else {
}
});
}
RED.nodes.registerType("mamPublish",mamPublish);
}