Skip to content
/ pwrj Public

PWRJ is a lightweight, simple, easy to use Java library for integrating with the PWR Chain and building wallets and virtual machines on it.

Notifications You must be signed in to change notification settings

pwrlabs/pwrj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PWRJ

PWRJ is a Java library for interacting with the PWR network. It provides an easy interface for wallet management and sending transactions on PWR.

Features

  • Generate wallets and manage keys
  • Get wallet balance and nonce
  • Build, sign and broadcast transactions
  • Transfer PWR tokens
  • Send data to PWR virtual machines
  • Interact with PWR nodes via RPC

Getting Started

Prerequisites

  • Java 8+

Installation

PWRJ is available on Maven Central. Add this dependency to your pom.xml:

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.github.pwrlabs</groupId>
            <artifactId>pwrj</artifactId>
            <version>1.0.6</version>
        </dependency>
    </dependencies>

Or Gradle:

Add it in your root build.gradle at the end of repositories:

dependencyResolutionManagement {
	repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
	repositories {
		mavenCentral()
		maven { url 'https://jitpack.io' }
	}
}

Step 2. Add the dependency

dependencies {
	implementation 'com.github.pwrlabs:pwrj:1.0.6'
}

Usage

Import the library:

import com.github.pwrlabs.pwrj.*;

Set your RPC node:

PWRJ.setRpcNodeUrl("https://pwrrpc.pwrlabs.io/");

Generate a new wallet:

PWRWallet wallet = new PWRWallet(); 

You also have the flexibility to import existing wallets using a variety of constructors

String privateKey = "private key"; //Replace with hex private key
PWRWallet wallet = new PWRWallet(privateKey); 
byte[] privateKey = ...; 
PWRWallet wallet = new PWRWallet(privateKey); 
ECKeyPair ecKeyPair = ...; //Generate or import ecKeyPair 
PWRWallet wallet = new PWRWallet(ecKeyPair); 

Get wallet address:

String address = wallet.getAddress();

Get wallet balance:

long balance = wallet.getBalance();

Get private key:

BigInteger privateKey = wallet.getPrivateKey();

Transfer PWR tokens:

wallet.transferPWR("recipientAddress", 1000); 

Sending a transcation to the PWR Chain returns a Response object, which specified if the transaction was a success, and returns relevant data. If the transaction was a success, you can retrieive the transaction hash, if it failed, you can fetch the error.

Response r = wallet.transferPWR("recipientAddress", 1000); 

if(r.isSuccess()) {
   System.out.println("Transcation Hash: " + r.getMessage());
} else {
   System.out.println("Error: " + r.getError());
}

Send data to a VM:

int vmId = 123;
byte[] data = ...;
Response r = wallet.sendVmDataTxn(vmId, data);

if(r.isSuccess()) {
   System.out.println("Transcation Hash: " + r.getMessage());
} else {
   System.out.println("Error: " + r.getError());
}

Other Static Calls

Update fee per byte:

Fetches latest fee-per-byte rate from the RPC node and updates the local fee rate.

PWRJ.updateFeePerByte();

Get RPC Node Url:

Returns currently set RPC node URL.

String url = PWRJ.getRpcNodeUrl();

**Get Fee Per Byte: **

Gets the latest fee-per-byte rate.

long fee = PWRJ.getFeePerByte();

Get Balance Of Address:

Gets the balance of a specific address.

long balance = PWRJ.getBalanceOfAddress("0x...");

Get Nonce Of Address:

Gets the nonce/transaction count of a specific address.

int nonce = PWRJ.getNonceOfAddress("0x..."); 

Broadcast Txn:

Broadcasts a signed transaction to the network.

byte[] signedTransaction = ...;
PWRJ.broadcastTxn(signedTransaction);

Contributing

Pull requests are welcome!

For major changes, please open an issue first to discuss what you would like to change.

License

MIT

About

PWRJ is a lightweight, simple, easy to use Java library for integrating with the PWR Chain and building wallets and virtual machines on it.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages