10

I have an idea for an IXI module that I'd like to try out, but I don't understand how I'm supposed to implement it.

I'd like to start with an example; what would an "Hello world!" version of an IXI module look like?

todofixthis
  • 1,320
  • 8
  • 23
  • I don't know of any documentation for IXI modules (or whether the API is even considered stable). Generally, they are just JavaScript modules which use the Rhino JavaScript engine to directly call the Java methods of iri. There is the parsing of package.json etc implemented: https://github.com/iotaledger/iri/blob/5244112b00941ea58f07485d9289d576741d1f66/src/main/java/com/iota/iri/IXI.java and here is a test case including a minimal ixi module: https://github.com/iotaledger/iri/blob/5244112b00941ea58f07485d9289d576741d1f66/src/test/java/com/iota/iri/IXITest.java – mihi Dec 10 '17 at 21:16
  • Question edited to be more specific. – todofixthis Dec 11 '17 at 07:25

1 Answers1

6

ixi/hello_world/package.json

{"main": "index.js"}

ixi/hello_world/index.js

function hello_world_impl(req) {
    return com.iota.iri.service.dto.IXIResponse.create({greeting: "Hello, world!"});
}

API.put("helloWorld", new com.iota.iri.service.CallableRequest({call: hello_world_impl}));

How to call:

curl http://localhost:14700 -X POST -H 'X-IOTA-API-Version: 1.4.1' -H 'Content-Type: application/json' -d '{"command": "hello_world.helloWorld"}'

Reply:

{"ixi":{"greeting":"Hello, world!"},"duration":2}

There is also Snapshot.ixi for a more realistic example.

mihi
  • 7,324
  • 2
  • 15
  • 34
  • Is this still working on 1.4.1.4? I'm trying to load this module but get an exception Exception during IOTA node initialisation: --- java.lang.NullPointerException: null --- at com.iota.iri.IXI.attach(IXI.java:249) ~[iri.jar:na] --- at com.iota.iri.IXI.loadModule(IXI.java:223) ~[iri.jar:na] – Daniel F Dec 21 '17 at 08:22
  • Your error looks like it was unable to load Rhino scripting engine. What Java version are you using (OpenJDK for example does not contain Rhino as far as I know)? – mihi Dec 21 '17 at 13:24
  • Running a Docker image https://github.com/anapsix/docker-alpine-java (anapsix/alpine-java:8) Which contains https://github.com/anapsix/docker-alpine-java/tree/master/8/152b16 Thanks for the hint, I will check that (Oracle Java 8). – Daniel F Dec 21 '17 at 13:27
  • You do not want to remove that one: https://github.com/anapsix/docker-alpine-java/blob/master/8/152b16/jdk/unlimited/Dockerfile#L76 – mihi Dec 21 '17 at 13:30
  • From https://github.com/bluedigits/nstats/blob/master/index.js I was able to get Neighbor stats extension started... printed into the log, but then it crashed. So I believe that it has the JavaScript engine enabled. – Daniel F Dec 21 '17 at 13:32
  • turned out that this line was the problem, because it is part of a rm -rf. See https://github.com/iotaledger/iri/issues/426#issuecomment-353566453 . It's working now :) – Daniel F Dec 22 '17 at 17:33
  • @DanielF sorry that my comment that you "do not want to remove this" was apparently misleading. I saw that it removed the file and you do not want to remove that as it is needed (I guess you understood it like you do not want to remove this line?) – mihi Dec 22 '17 at 17:49
  • Aaahh. Stuff like this happens, so no worries. I just had my share of laughing because of it. – Daniel F Dec 22 '17 at 21:40