GithubHelp home page GithubHelp logo

00mjk / beowulf-java Goto Github PK

View Code? Open in Web Editor NEW

This project forked from beowulf-foundation/beowulf-java

0.0 0.0 0.0 429 KB

beowulf-java is the official Beowulf library for Java.

License: GNU General Public License v3.0

Java 100.00%

beowulf-java's Introduction

Official Java BEOWULF Library

beowulf-java is the official Beowulf library for Java.

Main Functions Supported

  1. CHAIN
  • get_block
  • get_transaction
  • get_balance
  • get operation
  1. TRANSACTION
  • broadcast_transaction
  • create transaction transfer
  • create account
  • create token

Requirements

  • Java 8
  • Maven 3.5

Maven

File: pom.xml

<dependency>
    <groupId>com.beowulfchain</groupId>
    <artifactId>beowulfj-core</artifactId>
    <version>0.0.4</version>
</dependency>

Installation

The project requires Maven and Java to be installed on your machine. It can be build with the default maven command:

mvn clean install

The resulting JAR can be found in the target directory as usual. Please notice that some integration tests require different private keys. If you do not want to execute tests at all add "-DskipTests=true" to the mvn call which skips the test execution during the build.

Configuration

Create a new client instance of BeowulfJ

// Define config:
NetworkProperties network = new Mainnet();
// if you want to use testnet:
// NetworkProperties network = new Testnet();
BeowulfJConfig myConfig = BeowulfJConfig.getInstance();
myConfig.setResponseTimeout(100000);
// default account will be used if transaction doesn't define
myConfig.setDefaultAccount(new AccountName("default-account"));
myConfig.setNetwork(network);
// init beowulf node
myConfig.setDefaultBeowulfApiUri("https://bw.beowulfchain.com/rpc");


// Add and manage private keys follow base58 format:
AccountName user1 = new AccountName("user1");
ImmutablePair<PrivateKeyType, String> defaultAccountPrivkey = new ImmutablePair<>(PrivateKeyType.OWNER, "5Hv****");
ImmutablePair<PrivateKeyType, String> user1PrivKey = new ImmutablePair<>(PrivateKeyType.OWNER, "5Hv****");
myConfig.getPrivateKeyStorage().addPrivateKeyToAccount(myConfig.getDefaultAccount(), defaultAccountPrivkey);
myConfig.getPrivateKeyStorage().addPrivateKeyToAccount(user1, user1PrivKey);

// Create a new apiWrapper with your config object.
BeowulfJ beowulfJ = BeowulfJ.getInstance();

Example Usage

Get block
/*
 * Get block from block number
 */
Block block = beowulfJ.getBlock(165099L);
Get transaction
TransactionId transactionId = block.getTransactionIds().get(0);
CompletedTransaction transaction = beowulfJ.getTransactionDetail(transactionId.toString());
Transfer native coin
Transfer BWF
/*
 * Transfer 1.0 W from sender to receiver
 */
AccountName from = new AccountName("sender");
AccountName to = new AccountName("receiver");
Asset amount = Asset.createSmtAsset(new BigDecimal("1.00000"), "BWF");
TransferOperation transferOperation = beowulfJ.transfer(from, to, amount, network.getTransactionFee(), "Transfer 1.0 BWF from sender to receiver");
TransactionId transactionId = beowulfJ.signAndBroadcast(Collections.singletonList(transferOperation));
System.out.println("Transaction id: " + transactionId);
Transfer W
/*
 * Transfer 1.0 W from sender to receiver
 */
AccountName from = new AccountName("sender");
AccountName to = new AccountName("receiver");
Asset amount = Asset.createSmtAsset(new BigDecimal("1.00000"), "W");
TransferOperation transferOperation = beowulfJ.transfer(from, to, amount, network.getTransactionFee(), "Transfer 1.0 W from sender to receiver");
TransactionId transactionId = beowulfJ.signAndBroadcast(Collections.singletonList(transferOperation));
System.out.println("Transaction id: " + transactionId);
Create wallet
/*
 * Create new account and get private key
 */
AccountName creator = new AccountName("creator");
AccountName newAccount = new AccountName("newaccount");

// create new keypair
ECKey ecKey = new ECKey().decompress();
// get private key
String newprivkey = BeowulfJUtils.getPrivkeyBase58FromEckey(ecKey);
System.out.println(newprivkey);
// add new account to memory storage
ImmutablePair<PrivateKeyType, String> newAccountKey = new ImmutablePair<>(PrivateKeyType.OWNER, newprivkey);
myConfig.getPrivateKeyStorage().addAccount(newAccount, Collections.singletonList(newAccountKey));

// get public key
PublicKey publicKey = new PublicKey(ecKey);
System.out.println(publicKey.getAddressFromPublicKey());

// create authority for new account by public key
// more authority if needed
Authority owner = new Authority();
Map<PublicKey, Integer> keyAuths = new HashMap<>();
keyAuths.put(publicKey, 1);
owner.setKeyAuths(keyAuths);

String jsonMetadata = "{}";

AccountCreateOperation accountCreateOperation = beowulfJ.createAccount(creator, network.getAccountCreationFee(), newAccount, owner, "{}");
TransactionId transactionId1 = beowulfJ.signAndBroadcast(Collections.singletonList(accountCreateOperation));
System.out.println("Transaction id: " + transactionId1);

beowulf-java's People

Contributors

cauta avatar congnghia0609 avatar nhpquy avatar nnanhthu avatar

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.