bcorejs

0.4.2 • Public • Published

bCoreJS

BenchCore's Javascript Library For Sending BEX and other BenchCore Root and SideChain Asset-Based Transactions. It can be used from the client as a browserify compiled module, or on the server as a standard Node.js module.

Table of Contents

Background

bCoreJS was built from Ark's original javascript library ark-js, which derived from Lisk and was originally known as liskJS.

Installation

Install With NPM

npm install @benchcore/bcorejs

Install With YARN

yarn add @benchcore/bcorejs

Web Build

Build the browserify module for client use:

npm build:browserify

Clean:

npm clean:browserify

Tests

npm test

Tests written using mocha + schedule.js.


Usage

On the client:

<script src="node_modules/bcorejs/bundle.min.js"></script>

On the server:

var bench = require("bcorejs");

Generating a key pair

To generate a public / private key pair from a given passphrase:

var keys = bench.crypto.getKeys("passphrase");

Returning:

{
  publicKey: "02e012f0a7cac12a74bdc17d844cbc9f637177b470019c32a53cef94c7a56e2ea9",
  privateKey: ""
}

To get the private key:

keys.d.toBuffer().toString("hex");

Returning:

1e089e3c5323ad80a90767bdd5907297b4138163f027097fd3bdbeab528d2d68

Generating an address

To generate a unique BEX address from a given public key:

var address = bench.crypto.getAddress("5d036a858ce89f844491762eb89e2bfbd50a4a0a0da658e4b2628b25b117ae09");

Returning:

AGihocTkwDygiFvmg6aG8jThYTic47GzU9

Creating a transaction

To create a signed transaction object, which can then be broadcasted onto the network:

var amount      = 1000 * Math.pow(10, 8); // 100000000000
var transaction = bench.transaction.createTransaction("AGihocTkwDygiFvmg6aG8jThYTic47GzU9", amount, null, "passphrase", "secondPassphrase");

Returning:

{
  type: 0, // Transaction type. 0 = Normal transaction.
  amount: 100000000000, // The amount to send expressed as an integer value.
  asset: {}, // Transaction asset, dependent on tx type.
  fee: 100000000, // 0.1 BEX expressed as an integer value.
  id: "500224999259823996", // Transaction ID.
  recipientId: "BPznHFjiWBVXgW51ojD1wKBhucSryidzgz", // Recipient ID.
  senderPublicKey: "03c60519bf6a704949b80e25a5e1a68fddd008ae23ea5728ec8f36c0bb003357a3", // Sender's public key.
  signSignature: "03fdd33bed30270b97e77ada44764cc8628f6ad3bbd84718571695262a5a18baa37bd76a62dd25bc21beacd61eaf2c63af0cf34edb0d191d225f4974cd3aa509", // Sender's second passphrase signature.
  signature: "9419ca3cf11ed2e3fa4c63bc9a4dc18b5001648e74522bc0f22bda46a188e462da4785e5c71a43cfc0486af08d447b9340ba8b93258c4c7f50798060fff2d709", // Transaction signature.
  timestamp: 27953413 // Based on UTC time of genesis since epoch.
}

Network identification with Nethash

You need to obtain the nethash in order to be sure you are broadcasting to the right network (testnet, mainnet or others). The nethash is simply the payload hash from the genesisBlock. If no nethash or wrong nethash is provided in the headers, the request will be rejected returning the expected nethash.

{ "success": false, "message": "Request is made on the wrong network", "expected":"e2f8f69ec6ab4b12550a314bd867c46e64e429961bb427514a3a534c602ff467", "received":"wrong-nethash" }

The nethash for a given network can be obtained at the following API endpoint:

/api/blocks/getNetHash

You can also get the nethash from a peer this way:

On the client using jQuery:

var nethash;
$.ajax({
  url: "https://michaelx.bex.life/peer/transactions/",
  data: JSON.stringify({}),
  dataType: "json",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "os": "linux3.2.0-4-amd64",
    "version": "0.3.0",
    "port": 1,
    "nethash": "wrong-nethash"
  },
  success: function(data) {
    nethash = data.body.expected;
  }
});

From a server using Request:

var nethash;
request({
  url: "https://michaelx.bex.life/peer/transactions",
  json: { },
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "os": "linux3.2.0-4-amd64",
    "version": "0.3.0",
    "port": 1,
    "nethash": "wrong-nethash"
  }
}, function(error, response, body) {
    nethash = body.expected;
  });

Posting a transaction

Transaction objects are sent to /peer/transactions, using the POST method.

Example:

Method: POST
Content-Type: application/json
 
{
    "transactions" : [{
        ...
    }]
}

Sending Transactions On A Client

Using jQuery:

var success = function(data) {
  console.log(data);
};
 
$.ajax({
  url: "https://michaelx.bex.life/peer/transactions",
  data: JSON.stringify({ transactions: [transaction] }),
  dataType: "json",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "os": "linux3.2.0-4-amd64",
    "version": "0.3.0",
    "port": 1,
    "nethash":nethash
  },
  success: success
});

Sending Transactions On A Server

Using Request:

var request = require("request");
 
var callback = function(error, response, body) {
  console.log(error || body);
};
 
request({
  url: "https://michaelx.bex.life/peer/transactions",
  json: { transactions: [transaction] },
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "os": "linux3.2.0-4-amd64",
    "version": "0.3.0",
    "port": 1,
    "nethash": nethash
  }
}, callback);

Peer Response

Upon successfully accepting a transaction, the receiving node will respond with:

{ "success": true, "result": "5318121831703437738" }

If the transaction is deemed invalid, or an error is encountered, the receiving node will respond with:

{ "success": false, "message": "Error message" }

Other transaction types

Creating a delegate transaction

var transaction = bench.delegate.createDelegate("secret", "username", "secondSecret");

Creating a second signature transaction

var transaction = bench.signature.createTransaction("secret", "secondSecret");

Creating a vote transaction

var transaction = bench.vote.createVote("secret", ["+58199578191950019299181920120128129"], "secondSecret");

Libraries

Resources

Why The Internet Must Have A Decentralized Alternative

Today, the internet is more censored than ever and it's only getting worse. Our mission with the dWeb Protocol was to create a truly powerful P2P protocol, around benOS, dBrowser and many of benOS' underlying libraries to bring the most powerful P2P products to life. In the last few months, by rebuilding P2P technologies that have existed since the early 2000s, we have built a powerful suite of decentralized libraries for benOS and the Bench Network, that will only improve over time. But we also brought new ideas to life, like:

and more! These were the protocols and libraries that we needed to create a completely decentralized operating system, where everything was distributed, protected and people were once again in control of their data. benOS is made up of over 1100+ different libraries that we are releasing on a day-by-day basis as we move them to a stable/production state. While financial support is great for this open source project, we need developers who want to be some of the first to build the dApps and dSites of the future. We have to take back what our forefathers originally designed for freedom, by making our code the law, instead of releasing weak and highly centralized applications where law cannot be applied because the code lacks the foundation to implement a legal framework for itself. Join us for a truly historic journey on the BenchLabs Telegram. See you there.

Bench On The dWeb

dweb://bench.dnames.io // dNames Short Link dweb://3EDAE09848B77401445B7739CAFCE442DDE1752AED63025A1F94E6A86D7E9F04 // dWeb Key Link

In order to make the links above clickable or to view these links period, you will need dBrowser (Available for Mac OSX, Linux, Windows and soon to be available on iOS/Android)

"The Code Is The Law" - Stan Larimer - Godfather of BitShares.

License

MIT

JavaScript Style Guide
forthebadge
dWebShield

Copyright

Copyright (c) 2018 Distributed Webs Project. All rights reserved.

Package Sidebar

Install

npm i bcorejs

Weekly Downloads

1

Version

0.4.2

License

MIT

Unpacked Size

1.36 MB

Total Files

71

Last publish

Collaborators

  • benchpay