Skip to content

Instantly share code, notes, and snippets.

@getlarge
Created November 13, 2019 19:50
Show Gist options
  • Save getlarge/262294459ab627b7e5f24bf65de291e4 to your computer and use it in GitHub Desktop.
Save getlarge/262294459ab627b7e5f24bf65de291e4 to your computer and use it in GitHub Desktop.
node-red-bridge-iota/iota-flow
[
{
"id": "9e1cd9d1.387b",
"type": "subflow",
"name": "init-MAM",
"info": "",
"category": "iota",
"in": [
{
"x": 80,
"y": 120,
"wires": [
{
"id": "d542e3f4.c262a8"
}
]
}
],
"out": [],
"env": [
{
"name": "IOTA_PROVIDER",
"type": "env",
"value": "IOTA_PROVIDER"
},
{
"name": "IOTA_SEED",
"type": "env",
"value": "IOTA_SEED"
},
{
"name": "NODE_RED_STORE_TYPE",
"type": "env",
"value": "NODE_RED_STORE_TYPE"
},
{
"name": "debug",
"type": "bool",
"value": "true"
}
]
},
{
"id": "ac4850fc.cf69f8",
"type": "function",
"z": "9e1cd9d1.387b",
"name": "mam-init",
"func": "try {\n if (!msg || !msg.payload) throw new Error(\"No IOTA dependencies found\");\n const Mam = msg.payload[\"iota-mam\"];\n if (!Mam || Mam === null) throw new Error(\"Cannot get IOTA MAM module\");\n const iota = msg.payload[\"iota-api\"];\n if (!iota || iota === null) throw new Error(\"Cannot get IOTA API instance\");\n\n const storeType = env.get(\"NODE_RED_STORE_TYPE\");\n const seed = env.get(\"IOTA_SEED\");\n const security = 1;\n \n let mamState;\n global.get(\"iota-mam-state\", storeType, (err, res) => {\n if (err) throw err;\n mamState = res;\n });\n\n if (!mamState || mamState === null) {\n mamState = Mam.init(iota, seed, security);\n const mamType = 'restricted'\n const mamSecret = \"very_secret_secret\";\n mamState = Mam.changeMode(mamState, mamType, mamSecret);\n global.set(\"iota-mam-state\", mamState, storeType, err => {\n if (err) throw err;\n });\n }\n return mamState;\n} catch(error) {\n console.log(\"mam-init err\", error);\n return error;\n}\n",
"outputs": 1,
"noerr": 0,
"x": 460,
"y": 120,
"wires": [
[]
]
},
{
"id": "d542e3f4.c262a8",
"type": "subflow:bbb4a70e.34bed8",
"z": "9e1cd9d1.387b",
"name": "",
"env": [
{
"name": "modules",
"type": "json",
"value": "[\"iota-api\", \"iota-mam\"]"
}
],
"x": 250,
"y": 120,
"wires": [
[
"ac4850fc.cf69f8"
]
]
},
{
"id": "a368f878.3c04d8",
"type": "subflow",
"name": "get-iota-price",
"info": "",
"category": "iota",
"in": [
{
"x": 120,
"y": 140,
"wires": [
{
"id": "c4cded62.f28f68"
}
]
}
],
"out": [
{
"x": 1060,
"y": 140,
"wires": [
{
"id": "c9315f3c.594d8",
"port": 0
}
]
}
],
"env": [],
"status": {
"x": 1060,
"y": 60,
"wires": [
{
"id": "3ce8f1bd.5ee72e",
"port": 0
}
]
}
},
{
"id": "a6775bd7.1c9ed8",
"type": "http request",
"z": "a368f878.3c04d8",
"name": "",
"method": "use",
"ret": "txt",
"paytoqs": false,
"url": "",
"tls": "",
"proxy": "",
"authType": "",
"x": 440,
"y": 140,
"wires": [
[
"6c255199.e19a18"
]
]
},
{
"id": "c4cded62.f28f68",
"type": "function",
"z": "a368f878.3c04d8",
"name": "set-query",
"func": "msg.url = 'https://api.coinmarketcap.com/v1/ticker/iota/';\nmsg.method = \"GET\";\nreturn msg; \n\n",
"outputs": 1,
"noerr": 0,
"x": 280,
"y": 140,
"wires": [
[
"a6775bd7.1c9ed8"
]
]
},
{
"id": "6c255199.e19a18",
"type": "function",
"z": "a368f878.3c04d8",
"name": "parse-response",
"func": "try {\n if (JSON.parse(msg.payload)) {\n let price = JSON.parse(msg.payload)[0].price_usd;\n price = Number(price);\n msg.payload = Number(price.toFixed(4));\n node.status({fill:'green', shape: 'ring', text:'iota-price-get'});\n return msg;\n }\n throw new Error(\"unable to parse HTTP response\");\n} catch(error) {\n node.status({fill:'red', shape: 'ring', text:'iota-price-error'});\n return error;\n}\n",
"outputs": 1,
"noerr": 0,
"x": 640,
"y": 140,
"wires": [
[
"c9315f3c.594d8"
]
]
},
{
"id": "c9315f3c.594d8",
"type": "change",
"z": "a368f878.3c04d8",
"name": "",
"rules": [
{
"t": "set",
"p": "iota-price-dollar",
"pt": "global",
"to": "payload",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 880,
"y": 140,
"wires": [
[]
]
},
{
"id": "3ce8f1bd.5ee72e",
"type": "status",
"z": "a368f878.3c04d8",
"name": "",
"scope": null,
"x": 640,
"y": 60,
"wires": [
[]
]
},
{
"id": "76b7192.e6e5de8",
"type": "subflow",
"name": "get-new-address",
"info": "",
"category": "iota",
"in": [
{
"x": 100,
"y": 100,
"wires": [
{
"id": "b76702d4.6468a"
}
]
}
],
"out": [
{
"x": 720,
"y": 100,
"wires": [
{
"id": "9654c7d8.58a26",
"port": 0
}
]
}
],
"env": [
{
"name": "IOTA_SEED",
"type": "env",
"value": "IOTA_SEED"
},
{
"name": "IOTA_ADDRESS_INDEX",
"type": "num",
"value": ""
},
{
"name": "IOTA_ADDRESS_SECURITY",
"type": "num",
"value": ""
},
{
"name": "debug",
"type": "bool",
"value": "false"
}
],
"status": {
"x": 720,
"y": 40,
"wires": [
{
"id": "de20b342.d6e2f",
"port": 0
}
]
}
},
{
"id": "9654c7d8.58a26",
"type": "function",
"z": "76b7192.e6e5de8",
"name": "create-address",
"func": "try {\n if (!msg || !msg.payload) throw new Error(\"No IOTA dependencies found\");\n const iota = msg.payload[\"iota-api\"];\n if (!iota || iota === null) throw new Error(\"Cannot get IOTA API instance\");\n const debug = env.get(\"debug\");\n const seed = env.get(\"IOTA_SEED\");\n \n const index = msg.addressIndex || env.get(\"IOTA_ADDRESS_INDEX\") || 1;\n const security = msg.addressSecurity || env.get(\"IOTA_ADDRESS_SECURITY\") || 1;\n iota.getNewAddress(seed, {index, security}, (err, res) => {\n if (err) throw err;\n msg.address = res;\n if (debug) {\n console.log(`Address created: ${res}`);\n }\n node.status({fill:\"green\",shape:\"ring\",text:\"address-create\"});\n node.send(msg);\n });\n \n} catch(error) {\n node.status({fill:\"red\",shape:\"ring\",text:\"address-create-error\"});\n // console.log(\"create address:err\", error);\n // node.error(\"Iota Error:\", error);\n return error;\n}\n",
"outputs": 1,
"noerr": 0,
"x": 520,
"y": 100,
"wires": [
[]
]
},
{
"id": "b76702d4.6468a",
"type": "subflow:bbb4a70e.34bed8",
"z": "76b7192.e6e5de8",
"name": "",
"env": [
{
"name": "modules",
"type": "json",
"value": "[\"iota-api\"]"
}
],
"x": 260,
"y": 100,
"wires": [
[
"9654c7d8.58a26"
]
]
},
{
"id": "de20b342.d6e2f",
"type": "status",
"z": "76b7192.e6e5de8",
"name": "",
"scope": null,
"x": 540,
"y": 40,
"wires": [
[]
]
},
{
"id": "bbb4a70e.34bed8",
"type": "subflow",
"name": "get-IOTA-modules",
"info": "",
"category": "iota",
"in": [
{
"x": 140,
"y": 160,
"wires": [
{
"id": "2b2d4a34.fcdf56"
}
]
}
],
"out": [
{
"x": 1120,
"y": 120,
"wires": [
{
"id": "592d579a.e2176",
"port": 0
}
]
}
],
"env": [
{
"name": "modules",
"type": "json",
"value": "[\"iota-core\",\"iota-converter\"]"
}
],
"status": {
"x": 1120,
"y": 40,
"wires": [
{
"id": "68c8e66c.86d1b8",
"port": 0
}
]
}
},
{
"id": "2b2d4a34.fcdf56",
"type": "function",
"z": "bbb4a70e.34bed8",
"name": "get-iota-dependencies",
"func": "try {\n const modules = env.get(\"modules\");\n if (!modules || modules === null) return null;\n modules.forEach((module, index) => {\n msg.payload = module;\n if (index === modules.length - 1) {\n msg.complete = true;\n node.status({fill:'green', shape:'ring', text: 'iota-module-get'});\n node.send(msg);\n } else {\n node.send(msg);\n node.status({fill:'yellow', shape:'ring', text: 'iota-module-get'});\n }\n return null;\n });\n \n} catch(error) {\n node.status({fill:'red', shape:'ring', text: 'iota-module-get-error'});\n return error;\n}\n",
"outputs": 1,
"noerr": 0,
"x": 300,
"y": 160,
"wires": [
[
"f19f1218.5fc69"
]
]
},
{
"id": "f19f1218.5fc69",
"type": "switch",
"z": "bbb4a70e.34bed8",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "iota-api",
"vt": "str"
},
{
"t": "eq",
"v": "iota-converter",
"vt": "str"
},
{
"t": "eq",
"v": "iota-extract-json",
"vt": "str"
},
{
"t": "eq",
"v": "iota-mam",
"vt": "str"
},
{
"t": "eq",
"v": "iota-signing",
"vt": "str"
},
{
"t": "eq",
"v": "iota-transaction-converter",
"vt": "str"
},
{
"t": "else"
}
],
"checkall": "false",
"repair": false,
"outputs": 7,
"x": 490,
"y": 160,
"wires": [
[
"fd1791ea.2ebe78"
],
[
"d64e2a.9f72d1d8"
],
[
"e31aa374.c23f7"
],
[
"67cafff0.632b38"
],
[
"4d2eeaf4.787c84"
],
[
"ff1b3978.9e5cb8"
],
[]
]
},
{
"id": "592d579a.e2176",
"type": "join",
"z": "bbb4a70e.34bed8",
"name": "",
"mode": "custom",
"build": "object",
"property": "payload",
"propertyType": "msg",
"key": "topic",
"joiner": "\\n",
"joinerType": "str",
"accumulate": false,
"timeout": "",
"count": "",
"reduceRight": false,
"reduceExp": "",
"reduceInit": "",
"reduceInitType": "",
"reduceFixup": "",
"x": 1010,
"y": 120,
"wires": [
[]
]
},
{
"id": "fd1791ea.2ebe78",
"type": "change",
"z": "bbb4a70e.34bed8",
"name": "iota-api",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "iota-api",
"tot": "global"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "iota-api",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 740,
"y": 80,
"wires": [
[
"592d579a.e2176"
]
]
},
{
"id": "d64e2a.9f72d1d8",
"type": "change",
"z": "bbb4a70e.34bed8",
"name": "iota-converter",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "iota-converter",
"tot": "global"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "iota-converter",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 760,
"y": 120,
"wires": [
[
"592d579a.e2176"
]
]
},
{
"id": "4d2eeaf4.787c84",
"type": "change",
"z": "bbb4a70e.34bed8",
"name": "iota-signing",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "iota-signing",
"tot": "global"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "iota-signing",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 750,
"y": 240,
"wires": [
[
"592d579a.e2176"
]
]
},
{
"id": "67cafff0.632b38",
"type": "change",
"z": "bbb4a70e.34bed8",
"name": "iota-mam",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "iota-mam",
"tot": "global"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "iota-mam",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 740,
"y": 200,
"wires": [
[
"592d579a.e2176"
]
]
},
{
"id": "68c8e66c.86d1b8",
"type": "status",
"z": "bbb4a70e.34bed8",
"name": "",
"scope": [
"2b2d4a34.fcdf56"
],
"x": 940,
"y": 40,
"wires": [
[]
]
},
{
"id": "ff1b3978.9e5cb8",
"type": "change",
"z": "bbb4a70e.34bed8",
"name": "iota-transaction-converter",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "iota-transaction-converter",
"tot": "global"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "iota-transaction-converter",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 790,
"y": 280,
"wires": [
[
"592d579a.e2176"
]
]
},
{
"id": "e31aa374.c23f7",
"type": "change",
"z": "bbb4a70e.34bed8",
"name": "iota-extract-json",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "iota-extract-json",
"tot": "global"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "iota-extract-json",
"tot": "str"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 760,
"y": 160,
"wires": [
[
"592d579a.e2176"
]
]
},
{
"id": "f3e73a7b.4ab6a",
"type": "subflow",
"name": "init-IOTA",
"info": "",
"category": "iota",
"in": [
{
"x": 80,
"y": 120,
"wires": [
{
"id": "90347b2f.f0956"
},
{
"id": "7738e782.0e34d8"
}
]
}
],
"out": [
{
"x": 740,
"y": 120,
"wires": [
{
"id": "cb0b3b25.332858",
"port": 0
}
]
}
],
"env": [
{
"name": "IOTA_PROVIDER",
"type": "env",
"value": "IOTA_PROVIDER"
},
{
"name": "IOTA_SEED",
"type": "env",
"value": "IOTA_SEED"
},
{
"name": "ALOES_USER_ID",
"type": "env",
"value": "ALOES_USER_ID"
},
{
"name": "NODE_RED_STORE_TYPE",
"type": "env",
"value": "NODE_RED_STORE_TYPE"
}
]
},
{
"id": "90347b2f.f0956",
"type": "function",
"z": "f3e73a7b.4ab6a",
"name": "iota-compose-api",
"func": "const provider = env.get('IOTA_PROVIDER');\nif (!provider || provider === null) return null;\nconst iotaCore = global.get('iota-core');\nif (!iotaCore || iotaCore === null) return null;\n\nmsg.iota = iotaCore.composeAPI({provider, requestBatchSize: 500});\nglobal.set(\"iota-api\", msg.iota);\n\nif (provider.search(\"dev\") > -1) {\n global.set(\"iota-depth\", 3);\n global.set(\"iota-mwm\", 9);\n} else {\n global.set(\"iota-depth\", 3);\n global.set(\"iota-mwm\", 14);\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 270,
"y": 120,
"wires": [
[
"cb0b3b25.332858"
]
]
},
{
"id": "7738e782.0e34d8",
"type": "function",
"z": "f3e73a7b.4ab6a",
"name": "init-seeds-index",
"func": " \ntry {\n const ownerId = env.get(\"ALOES_USER_ID\");\n const initialSeed = env.get(\"IOTA_SEED\");\n let seedsIndex = {};\n global.get(\"seeds-index\", \"file\", (err, res) => {\n if (err) throw err;\n seedsIndex = res || {};\n });\n if (initialSeed && ownerId) {\n if (seedsIndex[initialSeed]) {\n seedsIndex[initialSeed].ownerId = ownerId;\n seedsIndex[seed].id = initialSeed;\n } else {\n seedsIndex[initialSeed] = {id: initialSeed, ownerId};\n }\n }\n console.log(\"SEEDS INDEX : \", seedsIndex);\n global.set(\"seeds-index\", seedsIndex, \"file\", err => {\n if (err) throw err;\n });\n return msg;\n} catch(error) {\n return error;\n}",
"outputs": 1,
"noerr": 0,
"x": 260,
"y": 180,
"wires": [
[]
]
},
{
"id": "cb0b3b25.332858",
"type": "function",
"z": "f3e73a7b.4ab6a",
"name": "get-node-info",
"func": "try {\n const storeType = env.get(\"NODE_RED_STORE_TYPE\");\n global.get(\"iota-api\", storeType, (err, res) => {\n if (err) throw err;\n msg.iota = res;\n });\n if (!msg.iota || msg.iota === null) throw new Error(\"Cannot get IOTA API instance\") \n msg.iota.getNodeInfo()\n .then(info => {\n msg.info = JSON.stringify(info, null, 1);\n node.send(msg);\n })\n \n} catch(error) {\n // node.error(\"Iota Error:\", error);\n return error;\n}\n",
"outputs": 1,
"noerr": 0,
"x": 520,
"y": 120,
"wires": [
[]
]
},
{
"id": "8a6988e5.7cb778",
"type": "tab",
"label": "IOTA flow",
"disabled": false,
"info": ""
},
{
"id": "c7b3fe7a.b85ed8",
"type": "function",
"z": "8a6988e5.7cb778",
"name": "test-empty-transaction",
"func": "try {\n if (!msg || !msg.payload) throw new Error(\"No IOTA dependencies found\");\n const iota = msg.payload[\"iota-api\"];\n const Converter = msg.payload[\"iota-converter\"];\n if (!iota || iota === null) throw new Error(\"Cannot get IOTA API instance\");\n if (!Converter || Converter === null) throw new Error(\"Cannot get IOTA Converter module\");\n \n const address = 'CPCFIKV9BYEHYVZEHKGYXDEXTGCIOLAKEOGJVSITA9UCTNAVUEBZCOZKHAHKWPPZEMCJRPNNWENXEQXQW';\n const seed = env.get(\"IOTA_SEED\");\n\n const message = Converter.asciiToTrytes('Hello World!');\n const transfers = [{\n value: 0,\n address: address,\n message: message\n }];\n console.log(`Message to send: ${message}`)\n\n const depth = global.get(\"iota-depth\") || 3;\n const mwm = global.get(\"iota-mwm\") || 9;\n iota.prepareTransfers(seed, transfers)\n .then(trytes => iota.sendTrytes(trytes, depth, mwm))\n .then(bundle => {\n // console.log(`Bundle: ${JSON.stringify(bundle, null, 1)}`)\n msg.bundle = bundle;\n node.send(msg);\n return bundle;\n })\n\n // return msg;\n} catch(error) {\n // node.error(\"Iota Error:\", error);\n return error;\n}\n",
"outputs": 1,
"noerr": 0,
"x": 440,
"y": 660,
"wires": [
[
"cc151845.28f3a"
]
]
},
{
"id": "d36d5687.0b178",
"type": "inject",
"z": "8a6988e5.7cb778",
"name": "transaction",
"topic": "",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 180,
"y": 620,
"wires": [
[
"159a9c90.d71ec3"
]
]
},
{
"id": "cc151845.28f3a",
"type": "debug",
"z": "8a6988e5.7cb778",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 650,
"y": 660,
"wires": []
},
{
"id": "d4fc4907.7c43c8",
"type": "function",
"z": "8a6988e5.7cb778",
"name": "test-transaction",
"func": "try {\n if (!msg || !msg.payload) throw new Error(\"No IOTA dependencies found\");\n const iota = msg.payload[\"iota-api\"];\n const Converter = msg.payload[\"iota-converter\"];\n if (!iota || iota === null) throw new Error(\"Cannot get IOTA API instance\");\n if (!Converter || Converter === null) throw new Error(\"Cannot get IOTA Converter module\");\n \n const seed = env.get(\"IOTA_SEED\");\n const address = \"NQISMGMFMZLSIHJHVCOHMUZCLOURSVQRTNPLVZTUQZWONCZJDQGGSWYIZUQYQPJTOLZDHMDUGKKVVBMDY\";\n const message = Converter.asciiToTrytes('Hello World!');\n const tag = 'MYXXXVALUETRANSACTION';\n const depth = global.get(\"iota-depth\") || 3;\n const mwm = global.get(\"iota-mwm\") || 9;\n \n const transfers = [{\n value: 2,\n address: address,\n message, \n // tag\n }];\n \n console.log('transfers to send: ', transfers[0]);\n\n iota.prepareTransfers(seed, transfers)\n .then(trytes => iota.sendTrytes(trytes, depth, mwm))\n .then(bundle => {\n // console.log(`Bundle: ${JSON.stringify(bundle, null, 1)}`)\n msg.bundle = bundle;\n node.send(msg);\n return bundle;\n })\n\n // return msg;\n} catch(error) {\n return error;\n}\n",
"outputs": 1,
"noerr": 0,
"x": 420,
"y": 780,
"wires": [
[
"3ad891d0.7e6fa6"
]
]
},
{
"id": "5ab8c8c3.bd32d",
"type": "inject",
"z": "8a6988e5.7cb778",
"name": "transaction",
"topic": "",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 180,
"y": 740,
"wires": [
[
"a24f6476.150248"
]
]
},
{
"id": "3ad891d0.7e6fa6",
"type": "debug",
"z": "8a6988e5.7cb778",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 650,
"y": 780,
"wires": []
},
{
"id": "78b3431d.9d7f1c",
"type": "subflow:f3e73a7b.4ab6a",
"z": "8a6988e5.7cb778",
"name": "",
"env": [],
"x": 360,
"y": 140,
"wires": [
[
"fafbbf8e.d64948",
"d9f0cac3.23809"
]
]
},
{
"id": "9ad79137.778bb8",
"type": "inject",
"z": "8a6988e5.7cb778",
"name": "init-IOTA",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": "1.5",
"x": 180,
"y": 140,
"wires": [
[
"78b3431d.9d7f1c"
]
]
},
{
"id": "3adc8d2e.83fa2a",
"type": "debug",
"z": "8a6988e5.7cb778",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 610,
"y": 500,
"wires": []
},
{
"id": "9f028a6b.91ed4",
"type": "inject",
"z": "8a6988e5.7cb778",
"name": "address",
"topic": "",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 180,
"y": 500,
"wires": [
[
"bd7dfde5.93c5d"
]
]
},
{
"id": "159a9c90.d71ec3",
"type": "subflow:bbb4a70e.34bed8",
"z": "8a6988e5.7cb778",
"name": "",
"env": [
{
"name": "modules",
"type": "json",
"value": "[\"iota-api\", \"iota-converter\"]"
}
],
"x": 390,
"y": 620,
"wires": [
[
"c7b3fe7a.b85ed8"
]
]
},
{
"id": "a24f6476.150248",
"type": "subflow:bbb4a70e.34bed8",
"z": "8a6988e5.7cb778",
"name": "",
"env": [
{
"name": "modules",
"type": "json",
"value": "[\"iota-api\", \"iota-converter\"]"
}
],
"x": 370,
"y": 740,
"wires": [
[
"d4fc4907.7c43c8"
]
]
},
{
"id": "ab00f3c2.b38d7",
"type": "subflow:bbb4a70e.34bed8",
"z": "8a6988e5.7cb778",
"name": "",
"env": [
{
"name": "modules",
"type": "json",
"value": "[\"iota-api\"]"
}
],
"x": 370,
"y": 1060,
"wires": [
[
"e9f302ce.9589c"
]
]
},
{
"id": "e9f302ce.9589c",
"type": "function",
"z": "8a6988e5.7cb778",
"name": "check-pending-bundle",
"func": "try {\n if (!msg || !msg.payload) throw new Error(\"No IOTA dependencies found\");\n const iota = msg.payload[\"iota-api\"];\n if (!iota || iota === null) throw new Error(\"Cannot get IOTA API instance\") \n const tails = [\"tail transaction hash\"];\n let seconds = 0;\n const stopWatch = () => {\n seconds++\n }\n const timer = setInterval(stopWatch, 1000);\n \n const depth = 3;\n const mwm = 9; // 9 in dev; 14 in prod\n \n const autoPromoteReattach = (tail) => {\n iota.isPromotable(tail)\n .then(promote => promote ? iota.promoteTransaction(tail, depth, mwm)\n .then(()=> {\n console.log(`Promoted transaction hash: ${tail}`);\n })\n : iota.replayBundle(tail, depth, mwm)\n .then(([reattachedTail]) => {\n const newTailHash = reattachedTail.hash;\n console.log(`Reattached transaction hash: ${tail}`);\n // Keep track of all reattached tail transaction hashes to check for confirmation\n tails.push(newTailHash);\n })\n )\n .catch((error)=>{\n console.log(error);\n });\n }\n \n const autoConfirm = (tails) => {\n console.log(tails);\n iota.getLatestInclusion(tails)\n .then(states => {\n // Check that none of the transactions have been confirmed\n if (states.indexOf(true) === -1) {\n // Get latest tail hash\n const tail = tails[tails.length - 1]\n autoPromoteReattach(tail);\n } else {\n console.log(JSON.stringify(states,null, 1));\n clearInterval(interval);\n clearInterval(timer);\n const minutes = (seconds / 60).toFixed(2);\n const confirmedTail = tails[states.indexOf(true)];\n console.log(`Confirmed transaction hash in ${minutes} minutes: ${confirmedTail}`);\n return;\n }\n }).catch(error => {\n console.log(error);\n });\n }\n\n const anonymousMainFunction = autoConfirm.bind(null, tails);\n // Run the autoConfirm() function every 30 seconds to check for confirmations\n const interval = setInterval(anonymousMainFunction, 30000);\n\n} catch(error) {\n return error;\n}\n",
"outputs": 1,
"noerr": 0,
"x": 420,
"y": 1100,
"wires": [
[]
]
},
{
"id": "b0595e34.6b6c08",
"type": "function",
"z": "8a6988e5.7cb778",
"name": "test-keys",
"func": "try {\n if (!msg || !msg.payload) throw new Error(\"No IOTA dependencies found\");\n const iota = msg.payload[\"iota-core\"];\n if (!iota || iota === null) throw new Error(\"Cannot get IOTA API instance\");\n const Sign = msg.payload[\"iota-signing\"];\n if (!Sign || Sign === null) throw new Error(\"Cannot get IOTA Sign module\");\n const Converter = msg.payload[\"iota-converter\"];\n if (!Converter || Converter === null) throw new Error(\"Cannot get IOTA Converter module\");\n \n const seed = env.get(\"IOTA_SEED\");\n \n const addressIndex = 0;\n const addressSecurityLevel = 1\n const subseed = Sign.subseed(Converter.trytesToTrits(seed), addressIndex);\n \n const privateKey1 = Sign.key(subseed, addressSecurityLevel);\n console.log('Private key length for security level 1: ' + Converter.tritsToTrytes(privateKey1).length);\n \n const privateKey1Digests = Sign.digests(privateKey1);\n console.log(`Total key digests for security level 1: ` + Converter.tritsToTrytes(privateKey1Digests).length/81);\n var privateKey1Address = Sign.address(privateKey1Digests);\n \n console.log('Address with security level 1: ' + Converter.tritsToTrytes(privateKey1Address));\n let address;\n iota.getNewAddress(seed, { index: addressIndex, security: addressSecurityLevel}, (err,res) => {\n if(err) throw err;\n address = res;\n console.log(`Address created:`, address);\n return address;\n });\n // const address = iota.generateAddress(seed, addressIndex, addressSecurityLevel);\n\n return msg;\n} catch(error) {\n return error;\n}\n",
"outputs": 1,
"noerr": 0,
"x": 1020,
"y": 180,
"wires": [
[
"8ee243df.2379e"
]
]
},
{
"id": "4caae296.33a734",
"type": "subflow:bbb4a70e.34bed8",
"z": "8a6988e5.7cb778",
"name": "",
"env": [
{
"name": "modules",
"type": "json",
"value": "[\"iota-core\", \"iota-converter\", \"iota-signing\"]"
}
],
"x": 970,
"y": 140,
"wires": [
[
"b0595e34.6b6c08"
]
]
},
{
"id": "c083c729.f32958",
"type": "inject",
"z": "8a6988e5.7cb778",
"name": "test-keys",
"topic": "",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 780,
"y": 140,
"wires": [
[
"4caae296.33a734"
]
]
},
{
"id": "8ee243df.2379e",
"type": "debug",
"z": "8a6988e5.7cb778",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 1210,
"y": 180,
"wires": []
},
{
"id": "9f808b75.7fe8b8",
"type": "subflow:bbb4a70e.34bed8",
"z": "8a6988e5.7cb778",
"name": "",
"env": [
{
"name": "modules",
"type": "json",
"value": "[\"iota-api\"]"
}
],
"x": 370,
"y": 940,
"wires": [
[
"2fb805cb.afc202"
]
]
},
{
"id": "2fb805cb.afc202",
"type": "function",
"z": "8a6988e5.7cb778",
"name": "get-account-data",
"func": "try {\n if (!msg || !msg.payload) throw new Error(\"No IOTA dependencies found\");\n const iota = msg.payload[\"iota-api\"];\n if (!iota || iota === null) throw new Error(\"Cannot get IOTA API\");\n\n const seed = env.get(\"IOTA_SEED\");\n\n // iota.getAccountData(seed, {start: 0}, (err, res) => {\n // if (err) throw err;\n // console.log(\"account data: \", res);\n // });\n\n iota.getInputs(seed, { start: 0, threshold: 100 })\n .then(res => {\n console.log(\"account inputs: \", res);\n return res;\n })\n .catch(err => {\n if (err.message === errors.INSUFFICIENT_BALANCE) {\n console.log(\"account insufficient balance: \", err.message);\n }\n throw error;\n });\n \n return msg;\n} catch(error) {\n console.log(\"account info err: \", err);\n return error;\n}\n",
"outputs": 1,
"noerr": 0,
"x": 430,
"y": 980,
"wires": [
[
"e6c5f83b.b12f6"
]
]
},
{
"id": "b1a15901.811e4",
"type": "inject",
"z": "8a6988e5.7cb778",
"name": "account",
"topic": "",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 170,
"y": 940,
"wires": [
[
"9f808b75.7fe8b8"
]
]
},
{
"id": "e6c5f83b.b12f6",
"type": "debug",
"z": "8a6988e5.7cb778",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 650,
"y": 980,
"wires": []
},
{
"id": "bd7dfde5.93c5d",
"type": "subflow:76b7192.e6e5de8",
"z": "8a6988e5.7cb778",
"name": "",
"env": [
{
"name": "IOTA_ADDRESS_INDEX",
"type": "num",
"value": "0"
},
{
"name": "IOTA_ADDRESS_SECURITY",
"type": "num",
"value": "2"
}
],
"x": 380,
"y": 500,
"wires": [
[
"3adc8d2e.83fa2a"
]
]
},
{
"id": "22a6e78e.0da5b",
"type": "comment",
"z": "8a6988e5.7cb778",
"name": "RSQRVRUXXMKWFQ.....",
"info": "",
"x": 210,
"y": 460,
"wires": []
},
{
"id": "f7728cba.4b9108",
"type": "subflow:a368f878.3c04d8",
"z": "8a6988e5.7cb778",
"name": "",
"env": [],
"x": 390,
"y": 360,
"wires": [
[]
]
},
{
"id": "7e99196d.e8c54",
"type": "inject",
"z": "8a6988e5.7cb778",
"name": "IOTA-price",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "30",
"crontab": "",
"once": true,
"onceDelay": "5",
"x": 190,
"y": 360,
"wires": [
[
"f7728cba.4b9108"
]
]
},
{
"id": "fafbbf8e.d64948",
"type": "debug",
"z": "8a6988e5.7cb778",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 530,
"y": 140,
"wires": []
},
{
"id": "4818c18e.5213d",
"type": "function",
"z": "8a6988e5.7cb778",
"name": "get-node-info",
"func": "try {\n const storeType = env.get(\"NODE_RED_STORE_TYPE\");\n global.get(\"@iota-core\", storeType, (err, res) => {\n if (err) throw err;\n msg.iota = res;\n });\n // console.log(\"IOTA ?\", msg.iota);\n if (!msg.iota || msg.iota === null) throw new Error(\"Cannot get IOTA API instance\") \n msg.iota.getNodeInfo()\n .then(info => node.send({payload:JSON.stringify(info, null, 1)}))\n \n} catch(error) {\n // node.error(\"Iota Error:\", error);\n return error;\n}\n",
"outputs": 1,
"noerr": 0,
"x": 380,
"y": 200,
"wires": [
[]
]
},
{
"id": "f541ecfd.666318",
"type": "subflow:9e1cd9d1.387b",
"z": "8a6988e5.7cb778",
"name": "",
"env": [],
"x": 360,
"y": 240,
"wires": []
},
{
"id": "d9f0cac3.23809",
"type": "delay",
"z": "8a6988e5.7cb778",
"name": "",
"pauseType": "delay",
"timeout": "5",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"x": 180,
"y": 200,
"wires": [
[
"4818c18e.5213d"
]
]
}
]
@getlarge
Copy link
Author

Initialize IOTA environment, import modules add function helpers.

Prerequisites :

  • Your own instance of device-manager or an account on aloes.io
  • A machine running node-red-bridge-iota configured to your Aloes account, and IOTA provider + initial seed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment