TyDIDs-P2P - Implement a simple MicroService
Demonstrates how to implement a full decentralized microservice from within Node-RED using the open-source TyDIDs - P2P Data Identity Framework within Node-RED
On the user side two random numbers get generated on Inject
. Afterwards a TyDIDs-Sender
-Node provides a DID which is picked up by a TyDIDs-Receiver
-Node. After the sum got calculated by a Function
the result is provided using another well-defined DID the original user is subscribed to.
Customize
All parameters required by the micro-service get defined in the Provide Random Numbers
function and pushed in this case as msg.payload.A
and msg.payload.B
. In the actual implementation function Calculate A + B
those are the fields expected.
Limitation
This sample is reduced for simplicity. Please do always check on the TyDIDs-Receiver
if provided msg.payload
is originated by an expected address.
[{"id":"4f7dd22c850beaf1","type":"tab","label":"Microservice","disabled":false,"info":""},{"id":"7551015dd75de21d","type":"comment","z":"4f7dd22c850beaf1","name":"Consumer","info":"","x":140,"y":60,"wires":[]},{"id":"55742d2b9db845e4","type":"inject","z":"4f7dd22c850beaf1","name":"What is A+B?","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"1","topic":"","payload":"{\"A\":1,\"B\":3}","payloadType":"json","x":150,"y":120,"wires":[["73b090fc002e54e0"]]},{"id":"2f4191f57f0bee5a","type":"Tydids-Sender","z":"4f7dd22c850beaf1","name":"","address":"0x42c14BcA7593208a4797a77d4f232d6AE1736b67","privateKey":"","extendMode":true,"x":420,"y":200,"wires":[[]]},{"id":"cbbceff45188adb2","type":"Tydids-Receiver","z":"4f7dd22c850beaf1","name":"","address":"0x42c14BcA7593208a4797a77d4f232d6AE1736b67","privateKey":"","x":860,"y":180,"wires":[["c7bb4c504740462b"],[],[],[]]},{"id":"89f1a4baa628d027","type":"comment","z":"4f7dd22c850beaf1","name":"Microservice","info":"","x":850,"y":60,"wires":[]},{"id":"c7bb4c504740462b","type":"function","z":"4f7dd22c850beaf1","name":"Calculate A + B","func":"msg.topic = msg.payload._revision;\nmsg.payload = {\n result:msg.payload.A + msg.payload.B\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1060,"y":180,"wires":[["cc312c4ce2517523"]]},{"id":"73b090fc002e54e0","type":"function","z":"4f7dd22c850beaf1","name":"Provide Random Numbers","func":"msg.payload = {\n A:Math.round(Math.random()*100),\n B:Math.round(Math.random()*100)\n}\nnode.status(\"A=\"+msg.payload.A+\" B:\"+msg.payload.B)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":180,"y":200,"wires":[["2f4191f57f0bee5a"]]},{"id":"cc312c4ce2517523","type":"Tydids-Sender","z":"4f7dd22c850beaf1","name":"","address":"0xb03c5738A48c5Ab0c09E651E993bcC3851Ed15eB","privateKey":"","extendMode":true,"x":1260,"y":180,"wires":[[]]},{"id":"41eeb23ceff77dc0","type":"Tydids-Receiver","z":"4f7dd22c850beaf1","name":"","address":"0xb03c5738A48c5Ab0c09E651E993bcC3851Ed15eB","privateKey":"","x":130,"y":320,"wires":[["e4723da5080aa9ec"],[],[],[]]},{"id":"e4723da5080aa9ec","type":"debug","z":"4f7dd22c850beaf1","name":"Result Received","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload.result","statusType":"msg","x":400,"y":300,"wires":[]}]