JNano provides a set of low level Nano operations that includes signing, seed generation, block hashing and account creation.
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.
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.
- Harry and his Rain project which were used in the first implementation
- Scott Lanoue for all help
- NovaCrypto and it BIP39 which were used as inspiration