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.4.1
Maven
<dependency>
<groupId>com.rotilho.jnano</groupId>
<artifactId>jnano-commons</artifactId>
<version>1.4.1</version>
</dependency>
All low level operations are handled by NanoSeeds
, NanoKeys
, NanoAccounts
, NanoBlocks
, NanoWorks
*, NanoSignatures
and NanoMnemonics
.
byte[] seed = NanoSeeds.generateSeed();
assertTrue(NanoSeeds.isValid(seed));
byte[] privateKey = NanoKeys.createPrivateKey(seed, 0);
byte[] publicKey = NanoKeys.createPublicKey(privateKey);
String account = NanoAccounts.createAccount(publicKey);
assertTrue(NanoAccounts.isValid(account));
String bananoAccount = NanoAccounts.createAccount(NanoBaseAccountType.BANANO, publicKey);
assertTrue(NanoAccounts.isValid(NanoBaseAccountType.BANANO, account));
assertTrue(Arrays.equals(NanoAccounts.toPublicKey(account), publicKey));
String hash = NanoBlocks.hashStateBlock(
account,
NanoBlocks.MAIN_NET_GENESIS,
account,
BigInteger.ONE,
NanoAccounts.MAIN_NET_GENESIS_ACCOUNT
);
assertTrue(NanoBlocks.isValid(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