kapujs
JavaScript library for sending kapu transactions from the client or server
Last updated 3 years ago by kapuone .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install kapujs 
SYNC missed versions from official npm registry.

KAPU JavaScript

Build Status

KAPU JS

KAPU JS is a JavaScript library for sending KAPU transactions. It's main benefit is that it does not require a locally installed KAPU node, and instead utilizes the existing peers on the network. It can be used from the client as a browserify compiled module, or on the server as a standard Node.js module.

Installation

npm package

Building

Build the browserify module for client use:

npm run build:browserify

Clean:

npm run clean:browserify

Tests

npm test

Tests written using mocha + schedule.js.


Usage

On the client:

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

On the server:

var kapu = require("kapujs");

Generating a key pair

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

var keys = kapu.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 KAPU address from a given public key:

var address = kapu.crypto.getAddress("02e012f0a7cac12a74bdc17d844cbc9f637177b470019c32a53cef94c7a56e2ea9");

Returning:

KFE8YyadtYHnjPVdHNq3xVsQYCgWsKrKU6

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 = kapu.transaction.createTransaction("KFE8YyadtYHnjPVdHNq3xVsQYCgWsKrKU6", 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 KAPU expressed as an integer value.
  id: "500224999259823996", // Transaction ID.
  recipientId: "KFE8YyadtYHnjPVdHNq3xVsQYCgWsKrKU6", // Recipient ID.
  senderPublicKey: "56e106a1d4a53dbe22cac52fefd8fc4123cfb4ee482f8f25a4fc72eb459b38a5", // 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 (devnet, 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":"313ea34c8eb705f79e7bc298b788417ff3f7116c9596f5c9875e769ee2f4ede1", "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://walletapi.kapu.one/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://walletapi.kapu.one/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://walletapi.kapu.one/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://walletapi.kapu.one/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 = kapu.delegate.createDelegate("secret", "username", "secondSecret");

Creating a second signature transaction

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

Creating a vote transaction

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

Authors

License

The MIT License (MIT)

Copyright (c) 2017-2018 KAPU.one
Copyright (c) 2016-2017 ARK.io
Copyright (c) 2016 Lisk

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Current Tags

  • 0.2.6                                ...           latest (2 years ago)

6 Versions

  • 0.2.6                                ...           2 years ago
  • 0.2.5                                ...           2 years ago
  • 0.2.4                                ...           3 years ago
  • 0.2.3                                ...           3 years ago
  • 0.2.2                                ...           3 years ago
  • 0.2.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 2
Dependencies (16)
Dev Dependencies (10)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |