JNano Commons

JNano provides a set of low level Nano operations that includes signing, seed generation, block hashing and account creation.

License

License

Categories

Categories

JNA Development Tools Native
GroupId

GroupId

com.rotilho.jnano
ArtifactId

ArtifactId

jnano-commons
Last Version

Last Version

1.5.0
Release Date

Release Date

Type

Type

jar
Description

Description

JNano Commons
JNano provides a set of low level Nano operations that includes signing, seed generation, block hashing and account creation.
Project URL

Project URL

https://github.com/rotilho/jnano-commons
Source Code Management

Source Code Management

https://github.com/rotilho/jnano-commons

Download jnano-commons

How to add to project

<!-- https://jarcasting.com/artifacts/com.rotilho.jnano/jnano-commons/ -->
<dependency>
    <groupId>com.rotilho.jnano</groupId>
    <artifactId>jnano-commons</artifactId>
    <version>1.5.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.rotilho.jnano/jnano-commons/
implementation 'com.rotilho.jnano:jnano-commons:1.5.0'
// https://jarcasting.com/artifacts/com.rotilho.jnano/jnano-commons/
implementation ("com.rotilho.jnano:jnano-commons:1.5.0")
'com.rotilho.jnano:jnano-commons:jar:1.5.0'
<dependency org="com.rotilho.jnano" name="jnano-commons" rev="1.5.0">
  <artifact name="jnano-commons" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.rotilho.jnano', module='jnano-commons', version='1.5.0')
)
libraryDependencies += "com.rotilho.jnano" % "jnano-commons" % "1.5.0"
[com.rotilho.jnano/jnano-commons "1.5.0"]

Dependencies

compile (2)

Group / Artifact Type Version
com.rfksystems : blake2b jar 1.0.0
net.i2p.crypto : eddsa jar 0.3.0

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Build Status Codacy Badge Codacy Badge Maven Central

JNano Commons

JNano provides a set of low level Nano operations that includes signing, seed generation, block hashing and account creation.

How to use it?

Gradle compile 'com.rotilho.jnano:jnano-commons:1.5.0

Maven

<dependency>
    <groupId>com.rotilho.jnano</groupId>
    <artifactId>jnano-commons</artifactId>
    <version>1.5.0</version>
</dependency>

All low level operations are handled by NanoSeeds, NanoKeys, NanoAccounts, NanoBlocks, NanoWorks*, NanoSignatures and NanoMnemonics.

// create seed
byte[] seed = NanoSeeds.generateSeed();
assertTrue(NanoSeeds.isValid(seed));

// create private key
byte[] privateKey = NanoKeys.createPrivateKey(seed, 0);
byte[] publicKey = NanoKeys.createPublicKey(privateKey);

// create account
String account = NanoAccounts.createAccount(publicKey);
assertTrue(NanoAccounts.isValid(account));

// by default Nano account type is used, but you can also use Banano
String bananoAccount = NanoAccounts.createAccount(NanoBaseAccountType.BANANO, publicKey);
assertTrue(NanoAccounts.isValid(NanoBaseAccountType.BANANO, account));

// convert account to publicKey
assertTrue(Arrays.equals(NanoAccounts.toPublicKey(account), publicKey));

// create block hash
String hash = NanoBlocks.hashStateBlock(
        account, // account
        NanoBlocks.MAIN_NET_GENESIS, //previous block
        account, // representative
        BigInteger.ONE, // balance
        NanoAccounts.MAIN_NET_GENESIS_ACCOUNT // link: target address in this case
);
assertTrue(NanoBlocks.isValid(hash));

// sign a block hash
String signature = NanoSignatures.sign(privateKey, hash);
assertTrue(NanoSignatures.isValid(account, hash, signature));

* Java use just CPU and calculate PoW with CPU is bloody slow. For production ready application please use a work server.

Why not use string for Seed and Keys?

As stated by Baeldung, "strings in Java are immutable which means that we cannot change them using any high-level APIs. Any change on a String object will produce a new String, keeping the old one in memory.

Therefore, the password stored in a String will be available in memory until Garbage Collector clears it. We cannot control when it happens, but this period can be significantly longer than for regular objects since Strings are kept in a String Pool for re-usability purpose".

Most of the time work with byte array will be enough but if there is the need to convert it to String you can easily achieve it using NanoHelper.toHex.

If you want to reduce the time the privets keys or seed stay in memory you can use NanoHelper.wipe and clear out the byte array content.

Special thanks to

Versions

Version
1.5.0
1.4.1
1.4.0
1.3.0
1.2.0
1.1.0
1.0.0