Ripple JavaScript library

From Ripple Wiki
Jump to: navigation, search

Contents

Overview

Ripple comes with a JavaScript library, ripple-lib, that provides useful functionality for communicating with rippled. The library is designed to support both node.js and browser applications.

Github repository:

Applications of the library:

Using the library with node.js

Installation

The library is available from https://npmjs.org as ripple-lib.

In your project, install the library with npm:

# npm install ripple-lib

Accessing the library

Access the various classes as shown:

var Amount = require("ripple-lib").Amount;
var Currency = require("ripple-lib").Currency;

Class Listing

Remote

The Remote object represents a connection to an instance of rippled either on the local or remote machine.

Initializing a Remote

var Remote = require('ripple-lib').Remote;
var example_config = {
  trusted: true,
  trace: true,
  servers: [ { host: '127.0.0.1', port: 5006, secure: true } ]
}
var remote = new Remote(example_config);
remote.connect(function() {
  //Remote is connected to a rippled
});

Examples

rsign.js

bin/rsign.js
This utility demonstrates local signing with the library.

Using the library in a web page

Example of signing a transaction:

<html>
 <head>
   <script src="js/ripple-0.7.19.js"></script>
    <script type="text/javascript">
    function signtx(secret, tx_in) {
       var tx_JSON = JSON.parse(tx_in);
       var tx = new ripple.Transaction();
       tx.tx_json = tx_JSON;
       tx._secret = secret;
       tx.complete();
       var unsigned = tx.serialize().to_hex();
       tx.sign();
       return (tx.serialize().to_hex());
    }
    var tx = '{ "TransactionType" : "Payment", 
                "Account" : "raSv7ZM4KvK99REGHfPSGn8QpdpJWtNTrN", 
                "Destination" : "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV", 
                 "Amount" : "10", "Fee" : "10", "Sequence" : 1 }';
    var signature = signtx('sxxxxxxxxxxxxxx',tx);
    </script>
 </head>
 <body>
 </body>
</html>

Build the library

submodules

Git submodules are other code bases that are imported into the ripple source directories from another git repository.

cryptojs
Provides JavaScript BigInteger support.
sjcl
Provides JavaScript cryptography functionality.

Preparation

First, install the library's dependencies:

# git submodule update --init
# npm install

NPM will install the required packages in the current directory under: node_modules

If you want to have the grunt build command installed globally (recommended), run:

# sudo npm install -g grunt-cli

Or, instead of installing grunt-cli globally, you can install it locally and add node_modules/grunt-cli/bin to your execution path.

tcsh:

# npm install grunt-cli
# set path=( node_modules/grunt-cli/bin $path )

bash:

# npm install grunt-cli
# PATH=node_modules/grunt-cli/bin:$PATH

Updating

Later, you can update the submodules:

# git submodule update

Building

To build SJCL and compile the browser versions of the library, run this command:

# grunt

The build system will produce three different files:

Name Description
ripple-x.x.x.js Standard build containing uncompressed code
ripple-x.x.x-debug.js Debug build using SourceURLs
ripple-x.x.x-min.js Standard build, minified using UglifyJS

Watch for changes

You can also set the build system to automatically rebuild when one of the input files changes, like this:

# grunt watch

Note that this command will not exit, instead it will continue running until interrupted.

Personal tools
Namespaces

Variants
Actions
Navigation
Tools