Giter Site home page Giter Site logo

pubfred / qzeon-js Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 9.43 MB

JavaScript library for sending ZEON transactions from the client or server .... Work in progress !

License: MIT License

JavaScript 100.00%
zeon zeon-hex javascript library transaction

qzeon-js's Introduction

Building

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/qzeonjs/bundle.min.js"></script>

On the server:

var qzeonjs = require("qzeonjs");

Generating a key pair

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

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

Returning:

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

To get the private key:

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

Returning:

6484F9094E4C94A582364B37EB2911481E4D312A0C788DC627790E91D821587F

Generating an address

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

var address = qzeonjs.crypto.getAddress("02F7A1379A1B50A395FA7CCD530F06F8F6B96E1359237E029950F34FAE57A37ABD");

Returning:

ZVCmN9MfpBqqWFy2HBUr4TExWiEwwz9huM

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 = qzeonjs.transaction.createTransaction("ZVCmN9MfpBqqWFy2HBUr4TExWiEwwz9huM", 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 ZEON expressed as an integer value.
  id: "500224999259823996", // Transaction ID.
  recipientId: "ZVCmN9MfpBqqWFy2HBUr4TExWiEwwz9huM", // Recipient ID.
  senderPublicKey: "02F7A1379A1B50A395FA7CCD530F06F8F6B96E1359237E029950F34FAE57A37ABD", // 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":"00000c9c83e5970601b5af203855c305a7e426deb667e6a8b3d1e1f66b52d220", "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://qzeonapi.zeonhexalgo.fun/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://qzeonapi.zeonhexalgo.fun/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 transaction on the Client

Using jQuery:

var success = function(data) {
  console.log(data);
};

$.ajax({
  url: "https://qzeonapi.zeonhexalgo.fun/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 transaction on the Server

Using Request:

var request = require("request");

var callback = function(error, response, body) {
  console.log(error || body);
};

request({
  url: "https://qzeonapi.zeonhexalgo.fun/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 = qzeonjs.delegate.createDelegate("secret", "username", "secondSecret");

Creating a second signature transaction

var transaction = qzeonjs.signature.createSignature("secret", "secondSecret");

Creating a vote transaction

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

Authors

License

ZEONJS is licensed under the MIT License - see the LICENSE file for details.

qzeon-js's People

Contributors

dependabot[bot] avatar pubfred avatar

Watchers

 avatar

qzeon-js's Issues

Usage error: cannot find module 'qzeonjs' at Function.Module._resolveFilename

Installed this qzeon-js module on a windows platform using nvm-windows manager, npm version 12.0.0 and got this error upon trying to utilize it:

Error: Cannot find module 'qzeonjs'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:610:15)
at Function.Module._load (internal/modules/cjs/loader.js:526:27)
at Module.require (internal/modules/cjs/loader.js:666:19)
at require (internal/modules/cjs/helpers.js:16:16) { code: 'MODULE _NOT_FOUND',
requireStack: [C:\tx\qzeon-js[eval]' ]

Tried to install a linux machine and get similar error message about resolveFilename.

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.