Code Monkey home page Code Monkey logo

jnano's Introduction

jNano | Java library for Nano

Maven Central

jNano is a Java library used to make integrations with the Nano cryptocurrency simple. This library allows you to interface with the node via RPC requests, and also provides many native tools and utilities so that a node isn't always necessary.

Nano is a crypto-currency which offers instantaneous and fee-less transactions. For more information, visit https://nano.org.


Technical details

  • This library requires Java 8+ (module uk.oczadly.karl.jnano for versions 9+)
  • Fully compatible with clone cryptocurrencies (eg. Banano)
  • Built and managed through Maven
  • List of dependencies

Features

This library provides simple access to the following facilities:


Usage

Download

This project is hosted on Maven Central. You can also download the compiled JAR directly from here.

Maven

<dependency>
    <groupId>uk.oczadly.karl</groupId>
    <artifactId>jnano</artifactId>
    <version>2.20.1</version>
</dependency>

Gradle

dependencies {
    implementation 'uk.oczadly.karl:jnano:2.20.1'
}

Documentation

Examples

RPC Queries [Wiki]

To make queries to an external Nano node through the RPC system, you will need to use the RpcQueryNode class. You can customize these objects even further by constructing using the nested Builder class.

RpcQueryNode rpc = new RpcQueryNode();          // Using localhost:7076
RpcQueryNode rpc = RpcServiceProviders.nanex(); // Using nanex.cc public API

This example will print an account's balance to the console using a synchronous (blocking) call.

// Construct and execute the request, and obtain the response
ResponseBalance balance = rpc.processRequest(new RequestAccountBalance(
        "nano_34qjpc8t1u6wnb584pc4iwsukwa8jhrobpx4oea5gbaitnqafm6qsgoacpiz"));

// Handle the result object however you wish (eg. print the balance)
System.out.println("Account balance: " + balance.getTotal());

WebSockets (listening for blocks) [Wiki]

The following will create a WebSocket listener which connects to port 7078 on localhost. For each new block confirmed by the node, the hash and type will be printed to the console.

NanoWebSocketClient ws = new NanoWebSocketClient(); // Defaults to localhost:7078
ws.connect(); // Connect to the endpoint

// Register a listener (will be called for each new block)
ws.getTopics().topicConfirmedBlocks().registerListener((message, context) -> {
    // Print the hash and type of all confirmed blocks
    System.out.printf("Confirmed block: %s (%s)%n",
            message.getHash(), message.getBlock().getType());
});

// Subscribe to the block confirmations topic
ws.getTopics().topicConfirmedBlocks().subscribeBlocking(new TopicConfirmation.SubArgs()
        .includeBlockContents() // Include block info in messages
        .filterAccounts( // Only receive blocks for these accounts
                "nano_34qjpc8t1u6wnb584pc4iwsukwa8jhrobpx4oea5gbaitnqafm6qsgoacpiz",
                "nano_1ipx847tk8o46pwxt5qjdbncjqcbwcc1rrmqnkztrfjy5k7z4imsrata9est"));

Block Creation [Wiki]

The following sample will create a new state block. The block will be signed using the provided private key, and work will be generated in the JVM using the CPU.

WorkGenerator workGenerator = new CPUWorkGenerator(); // Note: construct once and re-use

StateBlock block = StateBlock.builder()
        .subtype(StateBlockSubType.OPEN)
        .link("BF4A559FEF44D4A9C9CEF4972886A51FC83AD1A2BEE4CDD732F62F3C166D6D4F")
        .balance("123000000000000000000000000")
        .generateWork(workGenerator)
        .buildAndSign("A3293644AC105DEE5A0202B7EF976A06E790908EE0E8CC43AEF845380BFF954E"); // Private key

String hash = block.getHash().toHexString(); // Hashes the block
String blockJson = block.toJsonString(); // Serializes the block to JSON

Dependencies

This project requires the following dependencies, which are included automatically through Maven:


If you found this library useful and would like to support my work, donations may be sent to nano_34qjpc8t1u6wnb584pc4iwsukwa8jhrobpx4oea5gbaitnqafm6qsgoacpiz - any amount would be greatly appreciated :D

jnano's People

Contributors

dependabot[bot] avatar koczadly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jnano's Issues

uk.oczadly.karl.jnano.model.block.BlockDeserializer$BlockParseException: Could not parse block (malformed json?).

uk.oczadly.karl.jnano.model.block.BlockDeserializer$BlockParseException: Could not parse block (malformed json?).
at uk.oczadly.karl.jnano.model.block.BlockDeserializer.deserialize(BlockDeserializer.java:105)
at uk.oczadly.karl.jnano.model.block.BlockDeserializer$JsonAdapter.deserialize(BlockDeserializer.java:174)
at uk.oczadly.karl.jnano.model.block.BlockDeserializer$JsonAdapter.deserialize(BlockDeserializer.java:144)
at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:69)
at com.google.gson.TypeAdapter$1.read(TypeAdapter.java:199)
at com.google.gson.Gson.fromJson(Gson.java:932)
at com.google.gson.Gson.fromJson(Gson.java:1003)
at com.google.gson.Gson.fromJson(Gson.java:975)
at uk.oczadly.karl.jnano.callback.BlockCallbackServer$HttpCallbackProcessor.onRequest(BlockCallbackServer.java:129)
at uk.oczadly.karl.jnano.callback.httpserver.HttpRequestHandler.run(HttpRequestHandler.java:67)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
at java.base/java.lang.Thread.run(Thread.java:851)
Caused by: java.lang.IllegalArgumentException: Subtype cannot be null.
at uk.oczadly.karl.jnano.model.block.StateBlock.(StateBlock.java:154)
at uk.oczadly.karl.jnano.model.block.StateBlock.lambda$static$0(StateBlock.java:40)
at uk.oczadly.karl.jnano.model.block.StateBlock$$Lambda$108/0000000000000000.apply(Unknown Source)
at uk.oczadly.karl.jnano.model.block.BlockDeserializer.deserialize(BlockDeserializer.java:103)

Using version: 2.8.0-V21.2
Using the latest nano node version as well.

Not really sure what causes this error and need some guidance on this.

Thanks!

RpcInvalidArgumentException: Bad account number

public void receiveAll(int bananoAmountThreshold)
    {
        try
        {
            WorkDifficultyPolicy banano = NetworkConstants.BANANO.getWorkDifficulties();
            WorkGenerator workGenerator = new CPUWorkGenerator(banano);

            BlockProducer blockProducer = new StateBlockProducer
            (
                BlockProducerSpecification.builder()
                    .defaultRepresentative("ban_1ka1ium4pfue3uxtntqsrib8mumxgazsjf58gidh1xeo5te3whsq8z476goo")
                    .workGenerator(workGenerator) // Generate work on the node
                    .build()
            );

            LocalRpcWalletAccount account = new LocalRpcWalletAccount(privateKey, Banano.KaliumRPC, blockProducer);

            BigDecimal bigDecimal = new BigDecimal(bananoAmountThreshold*0.1).setScale(1,RoundingMode.DOWN);
            Bukkit.getLogger().warning(bigDecimal.toString());
            NanoAmount amount = NanoAmount.valueOfNano(bigDecimal);
            account.receiveAll(amount);
        }
        catch(WalletActionException e) { e.printStackTrace(); }
    }

It probably is because i'm trying to generate a banano block instead of a nano block, but i don't know what to change to make it work for banano (Except for the WorkDifficulty)

IlligalArgumentException on creating new account.

java.lang.IllegalArgumentException: class uk.oczadly.karl.jnano.rpc.request.wallet.RequestAccountCreate declares multiple JSON fields named work
Thats what I am getting while attempting your example.

Update NanoAmount/NanoUnit friendly amount to support new currency symbol Ӿ

Currently, when calling toString() on a NanoAmount, it produces something like "1 NANO". I am proposing that it should now return something more along the lines of "Ӿ1" or "Ӿ 1". This should be a pretty easy change, but it does require some awkwardness around appending or prepending the unit description.

In my opinion, NanoUnit needs a more general update to reflect the move away from the terms Gnano, NANO, knano, nano, mnano, etc. These days, there are just two common denominations, nano and raw. NANO, Nano, and nano mean the same thing now. I think we could get rid of these other denominations, and just use nano and raw. And then if nano, prepend "Ӿ", and if raw append "raw". Alternatively, it might be best to just leave off any sort of label, and leave that up to the user of the library.

I'd be happy to work on this myself and make a pull request. If work on this is already begun, please let me know, and if anyone has opinions on this I'd be happy to hear them.

Comptability with pippin

Hello!

I'm getting a 500 server error with pippin on creating an account.

Was wondering if you could make jnano more compatible with pippin.
Java log:
java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:11338
Pippin log:
raise ValueError ('badly formed hexadecimal UUID string')

Error is raised on RequestAccountCreate

Notes about pippin:
APIs that are different between Pippin and the Nano node wallet.
account_list accepts a count parameter that defaults to 1000
Pippin has an auto_receive_on_send option that will automatically receive pending blocks when you do a send, it will only do this if balance isnt high enough to make the transaction.
account_create does not accept an index

Thanks.

RpcWalletSpecification Missing?/Can't be imported

RpcWalletSpecification can't be imported in the latest version. Reverting back to 2.19.1 works.

Haven't really looked into too much detail as to why this is happening, I just know that 2.20 broke RpcWalletSpecification for me.

RPC Exception: Cancelled

I get this error every now and again when processing a request.

The request is to generate work. What causes it?

Thanks,

Letskingit

uk.oczadly.karl.jnano.rpc.exception.RpcEntityNotFoundException: Wallet not found

Hey again,
I am getting the error shown in the title within this code block(on the processRequest line) code:
String seed = 978B57EDDE3DA88D97930A1B58082920A2A496B5BF3F6085F4EFF3AB0411942C;
RequestAccountCreate request = new RequestAccountCreate(seed);
ResponseAccount response = node.processRequest(request); //error caught here
accountAddress = response.getAccountAddress();

What do I need to do?
Heres some free nano for your help - (awesome tipping service for nano) https://heytip.me/?claim=puqwv4935

Cheers

Examples for constructing Send blocks?

Hi, nice work here encapsulating the API, couple thoughts -

Just looking through the docs and tests but couldn't find an easy example for how to properly construct a Send or Receive block and publish it. We can look up Nano docs and use that as a guide, but it would be nice to have a simple api or some quick examples, as from what I recall the block requirements/structure can be tricky and can change over time.

It would also be nice to have the low level block construction abstracted away so the api user would only need to provide a few basic details - construct a wallet from address/seed, then Send() from the wallet to a target address a given amount.

Lastly is nanex.cc (the one in example) rpc node still usable?

java.lang.NoClassDefFoundError

Every time i try to make a request to the rpc, the program crashes with this error. Example:

RpcQueryNode rpc = RpcServiceProviders.nanex(); RequestAccountBalance req = new RequestAccountBalance("nano_34qjpc8t1u6wnb584pc4iwsukwa8jhrobpx4oea5gbaitnqafm6qsgoacpiz"); ResponseBalance response = rpc.processRequest(req);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.