· 5 years ago · Nov 12, 2019, 12:42 PM
1const http = require('http');
2const Mam = require('./lib/mam.client.js');
3const IOTA = require('iota.lib.js')
4//const provider = 'https://nodes.devnet.iota.org'
5/* const iota = new IOTA({
6 provider: provider
7}) */
8const {
9 asciiToTrytes
10} = require('@iota/converter')
11const securityMode = 'restricted'
12const secretKey = 'wishmeluck'
13
14function initiateMAM(provider) {
15 //MamState is state: Object Initialised IOTA library with a provider set.
16 MamState = Mam.init(provider)
17 //Changing the secretKey to trytes: sidekey
18 sidekey = asciiToTrytes(JSON.stringify(secretKey))
19 // Set channel mode
20 mamState = Mam.changeMode(MamState, securityMode, sidekey)
21 //Depth - A value of 3 is typically used by wallets, meaning that RW starts 3 milestones back.
22 // Null value will set depth to 3
23 //MWM - Currently is 14 on mainnet & spamnnet and 9 on most other devnets.
24 // Null value will set minWeightMagnitude to 9
25}
26
27const publish = async (packet, provider) => {
28 console.log(provider)
29 initiateMAM(provider);
30 var message = Mam.create(mamState, asciiToTrytes(JSON.stringify(packet)))
31 mamState = message.state
32 console.log('Root: ', message.root)
33 var hashMessage = await Mam.attach(message.payload, message.address, 3, 9)
34 console.log("HASH : ", hashMessage[0].hash)
35 return message.root;
36}
37
38http.createServer((req, res) => {
39 console.log("server started : ", req.method);
40 req.on("data", (chunk) => {
41 console.log("iota_node: ", JSON.parse(chunk).iota_node)
42 var iota = new IOTA({
43 provider: JSON.parse(chunk).iota_node
44 })
45 iota.api.getNodeInfo(async (error, nodeInfo) => {
46 if (error) {
47 res.write("ERROR")
48 res.end()
49 } else {
50 console.log("publishing message:", JSON.parse(chunk));
51 //const root = await publish(JSON.parse(chunk));
52 await publish(JSON.parse(chunk), iota.provider);
53 res.write("OK")
54 res.end()
55 }
56 })
57 delete(iota);
58 })
59
60}).listen(3000);