An open API service indexing awesome lists of open source software.

https://github.com/fibercrypto/libjs-skycoin

Javascript (web & node) client library for the Skycoin API
https://github.com/fibercrypto/libjs-skycoin

client-lib client-lib-js client-library javascript javascript-library js js-library js-libs nodejs nodejs-library nodejs-module nodejs-modules openapi rest-api restapi skycoin swagger swig

Last synced: 3 months ago
JSON representation

Javascript (web & node) client library for the Skycoin API

Awesome Lists containing this project

README

        

# LibSkycoin for Javascript

[![Build Status](https://travis-ci.org/simelo/libjs-skycoin.svg?branch=develop)](https://travis-ci.org/simelo/libjs-skycoin)

Javascript client library for Skycoin API. This library is a Javascript assembly generated with SWIG to access Skycoin API from Javascript.

## Table of Contents

- [LibJS-skycoin wrappers for the Skycoin cipher](#libjs-skycoin-wrappers-for-the-skycoin-cipher)
- [Installation](#installation)
- [Using the API](#usage)
- [Naming](#naming)
- [Parameters](#parameters)
- [Handles](#handles)
- [Byte Slices](#byte-slices)
- [Structures](#structures)
- [Fixed Sized Arrays](#fixed-sized-arrays)
- [Other Slices](#other-slices)
- [Memory Managemanet](#memory-management)
- [Make rules](#make-rules)
- [Development setup](#development-setup)
- [LibJS Skycoin wrappers for the Skycoin client API](#libjs-skycoin-wrappers-for-the-skycoin-client-api)
- [Clients](#clients)
- [LibSky-Axios Client](#libsky-axios-client)
- [LibSky-Node Client](#libsky-node-client)

## LibJS Skycoin wrappers for the Skycoin cipher

**LibJS Skycoin** is an assembly that provides wrappers to the Skycoin core cipher API's, implemented in `golang`, hence linking directly to the original node code. No transpilation involved.

### Installation

Before installing, make sure you understand the choices available to [install a NPM package](https://docs.npmjs.com/about-npm) . For instance, in case of [installing NPM client tools](https://docs.npmjs.com/getting-started/) the process would look like this, using [`npm install` command](https://docs.npmjs.com/packages-and-modules/).

```sh

npm install libskycoin
```

### Install from sources

Download the repository from http://github.com/simelo/libjs-skycoin.git.
Execute (`make build`) to install the library.

### Usage

#### Naming

The exported function in Libskycoin JS have the following naming format: `SKY_package_func_name` where package is replace by the package where the original Skycoin function is and func_name is the name of the function. For example, `LoadConfig` function from `cli` package is called in .Net `SKY_cli_LoadConfig`

#### Parameters

All skycoin exported functions return an error object as the last of the return parameters. In .NET error is return as an `uint` and it is the first return parameter. The rest of the parameters are returned in the same order.

Receivers in Skycoin are the first of the input parameters. Simple types, like integer, float, string will be used as the corresponding types in JS, except what act as pointers.

##### Handles

Some of Skycoin types are too complex to be exported to a scripting language. So, handles are used instead. Therefore all functions taking a complex type will receive a handle instead of the original Skycoin type. For example, having these functions exported from Skycoin:

```go
func LoadConfig() (Config, error)
func (c Config) FullWalletPath() string
```

Config is a struct type that is treated as a handle in Libskycoin JS . The usage in .Net will be:

```js
var skycoin = require('./lib/skycoin/build/Release/skycoin');
var configHandle = skycoin.new_Config_HandlePtr();
var err = skycoin.SKY_cli_LoadConfig(configHandle);
if(err == skycoin.SKY_OK) {
// SkY_OK means no error
var fullWalletPath = new _GoString()_;
err = skycoin.SKY_cli_FullWalletPath(configHandle,fullWallerPath);
//Close the handle after using the it
//so the garbage collector can delete the object associated with it.
skycoin.SKY_handle_close( configHandle );
} else {
// #Error
}
}
}
}
```

##### Byte slices

Parameters of type byte[] will treated as string . Example, this function in Skycoin:

```go
func (s ScryptChacha20poly1305) Encrypt(data, password []byte) ([]byte, error)
```

... should be invoked like this:

```js
var encrypt_settings = new skycoin.encrypt__ScryptChacha20poly1305();
var data = new skycoin.GoSlice(); //It will be passed as a parameter of type []byte
var pwd = new skycoin.GoSlice(); //As []byte too
var dataStr = new skycoin._GoString();
var pwdStr = new skycoin._GoString();
var encrypted = new skycoin.GoSlice();

dataStr.setString("Data to encrypt" );
data.convertString(dataStr);
pwdStr.SetString("password");
pwd.convertString(pwdStr);

var err = skycoin.SKY_encrypt_ScryptChacha20poly1305_Encrypt(encrypt_settings, data, pwd,encrypted);
if(err == skycoin.SKY_OK){
encrypted.getString().p); //Encrypted is GoSlice
}
```

##### Structures

Structures that are not exported as handles are treated like JS classes. In the previous example type ScryptChacha20poly1305 is created in JS like:

```js
var encrypt_settings = new skycoin.encrypt__ScryptChacha20poly1305()
```

And passed as first parameter in call to `SKY_encrypt_ScryptChacha20poly1305_Encrypt`.

##### Fixed Sized Arrays

Parameters of fixed size array are wrapped in structures when called from JS.

Given these types in Skycoin and this exported function:

```go
type PubKey [33]byte
type SecKey [32]byte

func GenerateDeterministicKeyPair(seed []byte) (PubKey, SecKey)
```

This is a way to use them to write assertions in JS:

```js
//Generates random seed
var data = new skycoin.GoSlice();
var err = skycoin.SKY_cipher_RandByte(32,data);

var pubkey = new skycoin.cipher_PubKey();
var seckey = new skycoin.cipher_SecKey();

err = skycoin.SKY_cipher_GenerateDeterministicKeyPair(data, pubkey,seckey);
```

In the example above `pubkey` and `seckey` are objects of an structure type containing a field named `data` holding the corresponding instance of `PubKey` and `SecKey`. Something like:

```cpp
cipher_PubKey struct{
data [33]byte;
} cipher_PubKey;

cipher_SecKey struct{
data [32]byte;
} ;
```

##### Other Slices

Other slices of base type different from `byte` are indeed wrapped inside classes. Let's see how to call the following function:

```go
func GenerateDeterministicKeyPairs(seed []byte, n int) []SecKey
```

In Javascript this how it should be used to generate a deterministic sequence of pairs of public / private keys given a random seed:

```js
//Generates random seed
var seed = new skycoin.GoSlice();
var err = skycoin.SKY_cipher_RandByte(32,seed);
var seckeys = new skycoin.cipher__SecKeys();

err = skycoin.SKY_cipher_GenerateDeterministicKeyPairs(seed, 2,seckeys);

for(int i=0;i {
console.log(result.data);
// Print the Version Object in result.data
}
);
```

### LibSky-Node Client

**LibSky-Node** is the client library generated using the typescript-node Swagger generator.

#### Installation

Before installing, make sure you understand the choices available to [install a NPM package](https://docs.npmjs.com/about-npm) . For instance, in case of [installing NPM client tools](https://docs.npmjs.com/getting-started/) the process would look like this, using [`npm install` command](https://docs.npmjs.com/packages-and-modules/).

```sh

npm install libsky-node
```

#### Install from sources

Download the repository from http://github.com/simelo/libjs-skycoin.git.
Execute (`make build`) to build the library. Then

```sh
npm install PATH_TO_REPOSITORY/lib/skyapi/node
```

#### Usage

Once installed to your project you can use the library as in the example below.

```js
"use strict";

let libsky = require('libsky-node'); // Import the library

const nodeUrl = 'https://node.skycoin.net'; // Url to a Skycoin Node

let client = new libsky.DefaultApi(nodeUrl); // Create a client object

client.version().then( // Get version data from nodeUrl
result => {
console.log(result.data);
// Print the Version Object in result.data
}
);
```