npm package: @niomon/secretd-js
Description: Secretd SDK for Javascript
Author: Enyk Limited
Latest version: 1.0.5

# secretd-js

Javascript Client SDK for Secretd

## Overview

The `secretd-js` allows you to generate, store and use cryptographic keys in
[Secretd](https://secretd.io/).

You can use this library to manage cryptograph secrets from web browsers through Secretd.
This library establish an end-to-end secure channel from web browsers
to a Secretd instance, protecting the secrets from any intermediary services.

The library also exposes a high-level API for managing private wallet keys for blockchain
networks. It supports all digital assets based on Ethereum and Cosmos Network.

It also supports making calls to Secretd instances from a server (i.e. in NodeJS).

## Installation

`secretd-js` can be installed as a npm package using `npm` or `yarn`.

Installing with `npm`

```
npm install --save https://gitlab.com/blocksq/secretd-js.git
```

Or, with `yarn`

```
yarn add https://gitlab.com/blocksq/secretd-js.git
```

### React Native

This library and its dependencies require several Node core modules like `buffer` and `crypto`
that are not included in React Native stack.

You need to install compatible implementations of these modules and expose them to your app.

```
yarn add buffer util readable-stream vm-browserify process react-native-crypto react-native-randombytes cbor-rn-prereqs
```

Modify `metro.config` file in the root directory of your React Native project and set
resolver.extraNodeModules:

```js
module.exports = (async () => {
  return {
    // ...
    resolver: {
      // ...
      extraNodeModules: {
        buffer: require.resolve('buffer'),
        crypto: require.resolve('react-native-crypto'),
        process: require.resolve('process/browser'),
        stream: require.resolve('readable-stream'),
        util: require.resolve('util/util'),
        vm: require.resolve('vm-browserify')
      }
    }
  }
})()
```

Node has certain globals that modules may expect, such as `Buffer` or `process`. React Native does
not provide these globals. You can add these globals to React Native global environment.

Add a `global.js`:

```js
global.Buffer = require('buffer').Buffer
global.process = require('process')
require('cbor-rn-prereqs')
```

Require `global.js` in your app before anything else (e.g. in `index.js`)

```js
import "./global"
// ...
import "./app/app.tsx"
```

## Usage

```js
var { SecretdClient } = require('secretd-js')

var client = new SecretdClient({
    websocketURL: 'ws://example.secretd.io/',
    staticKey: 'a secretd secret key',
    clusterIdentity: 'secretd cluster identity'
})
```
## Cosmos Integration

```js
var { SecretdCosmosProvider } = require('secretd-js')

var cosmosProvider = SecretdCosmosProvider({
    client, // an SecretdClient instance
    oid: 'hdwallet oid', // OID of a hdwallet object. Default to %%USER%%/hdwallet_default
    path: 'hdwallet derive path', // derive path for the default address. Default to m/44'/118'/0'/0/0
})
```

Getting addresses and public keys. These method will generate a new hdwallet object at the
specified oid if it hasn't been generated. Note that they return an array of addresses and
public keys.

```js
var addresses = await cosmosProvider.getAddresses()
var publicKeys = await cosmosProvider.getPublicKeys()
```

Signing a transactions

```js
var data = {
    'account_number': '0',
    'chain_id': '0',
    'fee': {
        // ...
    },
    'memo': '',
    'msgs': [
        // ...
    ],
    'sequence': '0'
}
var sigData = await cosmosProvider.sign(data)
```

## Creating objects on behalf of another user

Secretd supports a special user namespace `user/**uid**/`. Secretd users with
`SYSTEM_CREATE_OBJECT` capability can create new objects under these namespaces. And the user
referred by `uid` will automatically gain access to objects in their user namespaces.

Hence, a privileged user can create objects on behalf of another user.

```js
var oid = `user/${uid}/hdwallet_default`
var cosmosProvider = new SecretdCosmosProvider({ client, oid })
var addresses = await cosmosProvider.getAddresses() // This method initialize a new wallet
```

See [examples](examples/generate_user_wallet/index.js) for a demonstration.