Node-litecoin

From Litecoin Wiki
Revision as of 05:37, 12 February 2018 by Admin (talk | contribs) (Created page with "{{lowercase}}[https://github.com/gferrin/node-litecoin node-litecoin] is a simple Node.js wrapper for the Litecoin client's JSON-RPC API. The API is equivalent to the L...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Template:Lowercasenode-litecoin is a simple Node.js wrapper for the Litecoin client's JSON-RPC API.

The API is equivalent to the Litecoin API document. The methods are exposed as lower camelcase methods on the litecoin.Client object.

Install

npm install node-litecoin

Setup

  1. Traverse to ~/.litecoin or ~/Library/Application Support/Litecoin and add a file called litecoin.conf if it doesn't already exist.
  2. Add these lines to the file, they are the login information for the server:
rpcuser=username
rpcpassword=password
  1. Start your Litecoin client with the -server argument or run litecoind
  2. You should now be able to communicate with Litecoin JSON-RPC API using the node-litecoin library, try it out!

Examples

Create client

var litecoin = require('node-litecoin');
var client = new litecoin.Client({
     host: 'localhost',
     port: 9332,
     user: 'username', 
     pass: 'password'
});

Create client with single object

var client = new litecoin.Client({
  host: 'localhost',
  port: 9332,
  username: 'username',
  password: 'password'
});

Get balance across all accounts with minimum confirmations of 6

client.getBalance('*', 6, function(err, balance) {
  if (err) console.log(err);
  console.log('Balance: ' + balance);
});

Get the network hash rate

client.getNetworkHashPS(function(err, hashps) {
  if (err) console.log(err);
  console.log('Network Hash Rate: ' + hashps);
});


SSL

If you're using this to connect to litecoind across a network it is highly recommended to enable ssl, otherwise an attacker may intercept your RPC credentials resulting in theft of your litecoins.

When enabling ssl by setting the configuration option to true, the sslStrict option (verifies the server certificate) will also be enabled by default. It is highly recommended to specify the sslCa as well, even if your litecoind has a certificate signed by an actual CA, to ensure you are connecting to your own litecoind.

var client = new litecoin.Client({
    host: 'localhost',
    port: 8332,
    user: 'username',
    pass: 'password',
    ssl: true,
    sslStrict: true,
    sslCa: fs.readFileSync(__dirname + '/myca.cert')
});