NEM Development 101 (episode 02 - idea intellij + nem.core = vanity gen)


#1

If you’ve read the first episode, you should be able to compile nem.core, now it’s time for something bit more interesting. We’re gonna install idea, and create a very primitive project in it.

IntelliJ IDEA

Download and install idea. Installation is straightforward, next, next, next.

On first start idea will ask few questions, but you can quickly skip over them. I personally prefer dark theme, so screenshots will use dark theme.

Creating a project

We’re gonna create a “New Project”, and to make things easier, and since we’ve used maven in previous episode, it’s gonna be a maven project to make things easier and quicker.

Initially the list of SDKs will be empty, so we’ll need to add new SDK. Select directory, where you’ve installed the JDK.

We’ll need to set a few more things, we’ll give our project a “group id” org.nem.samples, and the “artifact id” episode02.

We’ll name it KeyGenerator and place it in the projects dir from the previous episode.

This is more or less how the idea should look like after finishing

Coding

First thing we’re gonna do is adding nem.core dependency in pom.xml. Since the version we were compiling
in a previous episode was 0.6.43-BETA, we need to specify that version.

    <dependencies>
        <dependency>
            <groupId>org.nem.core</groupId>
            <artifactId>nem-core</artifactId>
            <version>0.6.43-BETA</version>
        </dependency>
    </dependencies>

If IDEA will ask about MAVEN changes, select “Import”.
It should download and set up dependencies, so if you unwind “External Libraries”, it should look as follows.

Now, inside src/main/java, lets create a new java class, we’ll name it KeyGenerator.
(Normally we should create it in some sensible namespace, but since we’re just playing around this will suffice).

Put the following content inside the newly created file:

public class KeyGenerator {
    public static void main(String[] args) {
        System.out.println("Hello nem");
    }
}

From the menubar, select Run -> Run, we’ll need to add new “run configuration”, it’s gonna be application, and the class containing our main() method is KeyGenerator, the following picture should be helpful.

After running, at the bottom a small output window should popup, with not a very surprising content.

Key generation

Now it’s time to actually use nem.core.*

First, let’s take a look at the documentation of KeyPair
As stated, we can use the constructor without any arguments, to generate a new random key, this leads us to a slightly modified example:

import org.nem.core.crypto.KeyPair;

public class KeyGenerator {
    public static void main(String[] args) {
        final KeyPair someKey = new KeyPair();
        System.out.println(String.format("Private key: %s", someKey.getPrivateKey()));
        System.out.println(String.format(" Public key: %s", someKey.getPublicKey()));
    }
}

It would be convenient to have an address too. Let’s take a look at the documentation for Address

There is a constructor, but it’s protected, instead there are static factory methods starting with from*.
We’re gonna use the version where we can pass both network version, and a public key.
Now the question is, where do we get the version byte from. We could hard-code it, but that would be ugly, so instead we’re gonna use NetworkInfos

It’s got methods to get NetworkInfo for both testnet and mainnet, and from there we can get the network byte. Hence our next version.

import org.nem.core.crypto.KeyPair;
import org.nem.core.model.Address;
import org.nem.core.model.NetworkInfos;

public class KeyGenerator {
    public static void main(String[] args) {
        final KeyPair someKey = new KeyPair();
        System.out.println(String.format("Private key: %s", someKey.getPrivateKey()));
        System.out.println(String.format(" Public key: %s", someKey.getPublicKey()));

        final Address anAddress = Address.fromPublicKey(
                NetworkInfos.getTestNetworkInfo().getVersion(),
                someKey.getPublicKey());
        System.out.println(String.format("    Address: %s", anAddress));
    }
}

And after running it…

Now that’s bit boring and would be troublesome, so last thing we’re gonna do in this episode is adding a simple loop, that will find some interesting address.

import org.nem.core.crypto.KeyPair;
import org.nem.core.model.Address;
import org.nem.core.model.NetworkInfos;

public class KeyGenerator {
    public static void main(String[] args) {
        while (true) {
            final KeyPair someKey = new KeyPair();
            final Address anAddress = Address.fromPublicKey(
                    NetworkInfos.getTestNetworkInfo().getVersion(),
                    someKey.getPublicKey());

            if (anAddress.getEncoded().contains("TEST")) {
                printKey(someKey, anAddress);
                break;
            }
        }
    }

    private static void printKey(KeyPair someKey, Address anAddress) {
        System.out.println(String.format("Private key: %s", someKey.getPrivateKey()));
        System.out.println(String.format(" Public key: %s", someKey.getPublicKey()));
        System.out.println(String.format("    Address: %s", anAddress));
    }
}

Hope you’ve enjoyed this episode, and have fun experimenting.


Полезные ссылки для разработчиков
Working on NEM 102 guide
11 from TOP 13 harvesters addresses on the testnet are started from the same letters
#2

nem vanity address generator nice


#3

Hey, it works! :smile:


#4

any screenshots are welcome :wink:


#5

:smile:


#6

Finally got some time to play with this very briefly! :smile:

:+1:


#7

Nice!


#8

excellent!


#9

The only change to the lesson is
NEM Core is now :0.6.77-BETA
That was pretty slick!


#11

Are there some more episodes or tutorials like this?
I tried to use some other classes from nem core like in this tutorial, but i failed most ot the time. I would love to see how i could get some accountinformation or how to do a transaction, with the methodes and programms of this tutorial, if that is possible :slight_smile:
Thanks in advance for some information or tutorials


#12

Can we revive this somehow?


#13

I don’t have much time to create more episodes, but would be nice if someone from the community figure out how to do some other things


#14

Very well documented everything works as described. Thanks for the detailed documentation.


#15

I am getting an error: Error:(1, 27) java: package org.nem.core.crypto does not exist

Can someone help?


#16

Nevermind, figured it out.


#17

Are there any more episodes? Is NEM dead?


#18

It’s very strange that I haven’t found org.nem.core in maven central repository and my jar does not download automatically.
It shows something like this:
Failed to execute goal on project episode02: Could not resolve dependencies for project org.nem.samples:episode02:jar:1.0-SNAPSHOT: Could not find artifact org.nem.core:nem-core:jar:0.6.43-BETA in central (https://repo.maven.apache.org/maven2) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project episode02: Could not resolve dependencies for project org.nem.samples:episode02:jar:1.0-SNAPSHOT: Could not find artifact org.nem.core:nem-core:jar:0.6.43-BETA in central (https://repo.maven.apache.org/maven2).
Is anybody know how to deal with it?