Wrap ripple-lib

ripple-lib is a JavaScript API for interacting with Ripple in Node.js and the browser.

ripple-lib GitHub

build the browser-compatible version

RippleAPI beginners guide has explain how to prepare ripple-lib project and build the single JavaScript file. Then frontend developer can import ripple-lib likes below:

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.js"></script>
  <script src="ripple-0.17.7-min.js"></script>
  <script>
    var api = new ripple.RippleAPI({server:'wss://s1.ripple.com/'});
    ......
  </script>
</head>

But I do not want to import lodash.js externally, so I have to modify the config of Gulp.

modify config

/Gulpfile.js

......

function webpackConfig(extension, overrides) {
  overrides = overrides || {};
  var defaults = {
    // cache: true,
    // externals: [{
    //   'lodash': '_'
    // }],
    entry: './src/index.js',
    output: {
      library: 'ripple',
      path: './build/',
      filename: ['ripple-', extension].join(pkg.version)
    },
    plugins: [
      new webpack.NormalModuleReplacementPlugin(/^ws$/, './wswrapper'),
      new webpack.NormalModuleReplacementPlugin(/^\.\/wallet$/, './wallet-web'),
      new webpack.NormalModuleReplacementPlugin(/^.*setup-api$/,
        './setup-api-web')
    ],
    module: {
      loaders: [{
        test: /jayson/,
        loader: 'null'
      }, {
        test: /\.js$/,
        exclude: [/node_modules/],
        loader: 'babel-loader'
      }, {
        test: /\.json/,
        loader: 'json-loader'
      }]
    }
  };
  return _.assign({}, defaults, overrides);
}

......

I also notice that I only need the Gulp tasks for bower version.

/Gulpfile.js

......

gulp.task('bower', ['bower-build', 'bower-build-min', 'bower-build-debug',
                    'bower-version']);

......

gulp.task('default', ['bower']);

package

$ npm run build

> [email protected] build /Users/yen/Desktop/ripple-lib-0.17.7
> gulp

[16:47:46] Using gulpfile ~/Desktop/ripple-lib-0.17.7/gulpfile.js
[16:47:46] Starting 'build'...
[16:47:46] Starting 'build-debug'...
[16:47:46] Starting 'bower-version'...
[16:47:46] Finished 'bower-version' after 8.04 ms
[16:47:54] Finished 'build-debug' after 7.98 s
[16:47:54] Starting 'bower-build-debug'...
[16:47:54] Finished 'build' after 8.03 s
[16:47:54] Starting 'bower-build'...
[16:47:54] Starting 'build-min'...
[16:48:04] Finished 'bower-build-debug' after 9.72 s
[16:48:04] Finished 'bower-build' after 9.71 s
[16:48:04] Finished 'build-min' after 9.71 s
[16:48:04] Starting 'bower-build-min'...
[16:48:04] Finished 'bower-build-min' after 6.98 ms
[16:48:04] Starting 'bower'...
[16:48:04] Finished 'bower' after 42 μs
[16:48:04] Starting 'default'...
[16:48:04] Finished 'default' after 1.11 μs

The output files are located as below:

$ tree
.
├── build
│   ├── ripple-0.17.7-debug.js
│   ├── ripple-0.17.7-min.js
│   ├── ripple-0.17.7.js
│   ├── ripple-latest-min.js
│   └── ripple-latest.js
└── dist
    └── bower
        ├── ripple-debug.js
        ├── ripple-min.js
        └── ripple.js

3 directories, 8 files

RippleAPI in web browser

run in Android Studio

I copy /build/ripple-0.17.7-min.js to/src/main/assets/ripple.js. And append var rippleApi = new ripple.RippleAPI(); to it.

ripple.js

var ripple=function(t){function e(r){if(n[r])return n[r].exports;......fer=t.isBuffer}).call(e,n(58).Buffer)}]);var rippleApi = new ripple.RippleAPI();

I also use the online editor of W3C school to test the ripple.js.

I would like to wrap sign function of Ripple API to test that is this idea work or not.

sign(txJSON: string, secret: string, options: Object): {signedTransaction: string, id: string}

RippleAPI.java

package com.hsy.ripple;

import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Array;
import com.eclipsesource.v8.V8Object;

public class RippleAPI {
    ......

    public String sign(String txJSON, String secret) {
        V8 runtime = V8.createV8Runtime();
        runtime.executeVoidScript(readFromAsset("ripple.js"));

        // prepare parameters and call Ripple API
        V8Object rippleAPI = runtime.getObject("rippleApi");
        V8Array parameters = new V8Array(runtime).push(txJSON).push(secret);
        V8Object result = rippleAPI.executeObjectFunction("sign", parameters);

        // parse result from JSON object to String
        V8Object json = runtime.getObject("JSON");
        V8Array parameters2 = new V8Array(runtime).push(result);
        String response = json.executeStringFunction("stringify", parameters2);

        parameters2.release();
        json.release();
        result.release();
        parameters.release();
        rippleAPI.release();
        runtime.release();

        return response;
    }

    ......
}
  • result
D/App: sign is {
    "signedTransaction":"12000322800000002400000017201B0086955368400000000000000C732102F89EAEC7667B30F33D0687BBA86C3FE2A08CCA40A9186C5BDE2DAA6FA97A37D874473045022100BDE09A1F6670403F341C21A77CF35BA47E45CDE974096E1AA5FC39811D8269E702203D60291B9A27F1DCABA9CF5DED307B4F23223E0B6F156991DB601DFB9C41CE1C770A726970706C652E636F6D81145E7B112523F68D2F5E879DB4EAC51C6698A69304",
    "id":"02ACE87F1996E3A23690A5BB7F1774BF71CCBA68F79805831B42ABAD5913D6F4"
}

results matching ""

    No results matching ""