Created
March 26, 2019 09:31
-
-
Save CraigglesO/a26b37a5bf110cb06f614c841a19a0a8 to your computer and use it in GitHub Desktop.
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
const SerialPort = require('serialport'); | |
const { asciiToTrytes } = require('@iota/converter') | |
const Mam = require('@iota/mam'); | |
const port = new SerialPort('/dev/tty.usbserial-210352A82EDB1', { | |
baudRate: 115200 | |
}); | |
port.on('open', () => { | |
console.log('port is open!'); | |
// Because there's no callback to write, write errors will be emitted on the port: | |
let mamState = Mam.init({ | |
// provider: 'https://nodes.devnet.iota.org', | |
provider: 'https://nodes.thetangle.org:443', | |
attachToTangle: (trunkTransaction, branchTransaction, minWeightMagnitude, trytes, callback) => { | |
return new Promise((res, rej) => { | |
let bytestream = new Buffer.from(JSON.stringify({ | |
command: 'attachToTangle', | |
trunkTransaction, | |
branchTransaction, | |
minWeightMagnitude, | |
timestamp: new Date().getTime(), | |
trytes | |
}) + '\n'); | |
let returnedTrytes = new Buffer.from(''); | |
// let totalSize = 0; | |
port.on('data', (data) => { | |
// console.log('data.length', data.length); | |
// totalSize += data.length; | |
// console.log('data[data.length - 1]', data[data.length - 1]); | |
returnedTrytes = Buffer.concat([returnedTrytes, data]); | |
if (data[data.length - 1] === 10) { | |
let response = JSON.parse(returnedTrytes.toString('utf8')); | |
console.log(response); | |
res(response.trytes); | |
} | |
}); | |
port.write(bytestream); | |
}); | |
} | |
}); | |
mamState = Mam.changeMode(mamState, 'public'); | |
const publish = async data => { | |
console.log('PUBLISH!'); | |
// Convert the JSON to trytes and create a MAM message | |
const trytes = asciiToTrytes(data); | |
const message = Mam.create(mamState, trytes); | |
// Update the MAM state to the state of this latest message | |
// We need this so the next publish action knows it's state | |
mamState = message.state | |
// Attach the message | |
await Mam.attach(message.payload, message.address, 3, 14, 'CRAIG9FPGA'); | |
console.log('Sent message to the Tangle!'); | |
console.log('Address: ' + message.root); | |
} | |
publish('This is an ICC FPGA TEST'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment