Ether1Project / ethofs-sdk

MIT License
1 stars 3 forks source link

Etho Protocol SDK

Official NodeJS SDK for Etho Protocol

Overview

The Etho Protocol NodeJS SDK provides the quickest / easiest path for interacting with the Etho Protocol Network.

Installation

npm install --save @ethoprotocol/sdk

Setup

To start, simply require the ethoFS SDK and set up an instance with your ethoFS Upload Address/Key (Etho Protocol Key). Register a new upload address using the addUser function or by registering at: Etho Protocol Uploads.

Initialization Without Authentication

const ethofsSDK = require('@ethoprotocol/sdk');
const ethofs = ethofsSDK();

Initialization With Authentication

const ethofsSDK = require('@ethoprotocol/sdk');
const ethofs = ethofsSDK('yourETHOPrivateKey');

Initialization With Custom RPC/Gateway Locations

Params

Quickly test that you can connect to the API with the following call:

ethofs.testAuthentication().then((result) => {
    //handle successful authentication here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

Usage

Once you've set up your instance, using the ethoFS SDK is easy. Simply call your desired function and handle the results of the promise.

Authentication Not Required (ethoFS key not required on initialization)

Authentication Required (ethoFS key required on initialization)

ethofs.networkStats()
Params

Response

{
    activeUploadContracts: This is number of active upload contracts on the network,
    totalNetworkStorageUse: This is total used storage space(in bytes) used on the network,
    networkStorageAvailable: This is total used storage space(in bytes) available on the network,
    active_gatewaynodes: This is total number of active Gateway Nodes on the network,
    active_masternodes: This is total number of active Masternodes on the network,
    active_servicenodes: This is total number of active Service Nodes on the network,
    gatewaynode_reward: This is the previous daily reward payment for Gateway Nodes,
    masternode_reward: This is the previous daily reward payment for Masternodes,
    servicenode_reward: This is the previous daily reward payment for Service Nodes
}
Example Code
ethofs.networkStats().then((result) => {
    //handle results here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

nodeLocations

Retrieve Etho Protocol Node Location Information.

ethofs.nodeLocations()
Params

Response Array

[
    {
        type: This is type of node (Gateway Node, Masternode, Service Node),
        id: This is the unique node id,
        latitude: This is the latitude of node (geolocated by IP address),
        longitude: This is the longitude of node (geolocated by IP address)
    }
]
Example Code
ethofs.nodeLocations().then((result) => {
    //handle results here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

calculateCost

Estimate cost of a data upload by sending a request with upload duration and estimated size.

ethofs.calculateCost(options)
Params
ethofs.addUser(userName)
Params

ethofs.addUser(userName).then((result) => { //handle results here console.log(result); }).catch((err) => { //handle error here console.log(err); });

<a name="pinFileToIPFS-anchor"></a>
### `pinFileToIPFS`
Send a file to ethoFS for direct pinning to IPFS.

##### `ethofs.pinFileToIPFS(readableStream, options)`
##### Params
* `readableStream` - A [readableStream](https://nodejs.org/api/stream.html) of the file to be added 
* `options` : A JSON object that contains the following keyvalues:
  * `ethofsData` : A JSON object with (#ethofsData-anchor) for the data being pinned
  * `ethofsOptions` : A JSON object with additional [options](#ethofsData-anchor) for the data being pinned
#### Response

{ ipfsHash: This is the IPFS multi-hash provided back for your content, ethoTXHash: This is transaction hash of the confirmed upload contract on the Ether-1 Network, uploadCost: This is the total cost in ETHO for the upload, initiationBlock: This is the block number the upload contract was initialized/created on, expirationBlock: This is the block number that the upload contract will expire on }

##### Example Code
```javascript
const fs = require('fs');
const readableStreamForFile = fs.createReadStream('./yourfile.png');
const options = {
    ethofsData: {
        name: 'MyCustomUploadName',
        keyvalues: {
            customKey: 'customValue',
            customKey2: 'customValue2'
        }
    },
    ethofsOptions: {
        hostingContractDuration: 100000
    }
};
ethofs.pinFileToIPFS(readableStreamForFile, options).then((result) => {
    //handle results here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

pinFolderToIPFS

Send a folder to ethoFS for direct pinning to IPFS.

ethofs.pinFolderToIPFS(readableStream, options)
Params
ethofs.pinFromFS(fsLocation, options)
Params
ethofs.extendPin(uploadContractAddress, options)
Params
ethofs.unpin(uploadContractAddress)
Params

testAuthentication

Tests that you can authenticate with ethoFS correctly and the authentication key is registered

ethofs.testAuthentication()
Params

None

Response

{
    authenticated: true
}
Example Code
ethofs.testAuthentication().then((result) => {
    //handle results here
    console.log(result);
}).catch((err) => {
    //handle error here
    console.log(err);
});

pinList

List pin contracts stored in ethoFS.

ethofs.pinList(options)
Params

ethoFS Options

Some endpoints allow you to pass additional options for ethoFS to take into account when adding content to IPFS.

The options object can consist of the following values:

Example ethofsOptions object
{
    hostingContractDuration: 100000
}

ethoFS Data

Some endpoints allow you to pass additional data to store with your IPFS upload.

The options object can consist of the following values:

Example ethofsData object
{
    name: 'UploadContractName',
    keyvalues: {
        customKey: 'customValue',
        customKey2: 'customValue2'
    }
}

ethoFS Data Filter

Some endpoints allow you to pass additional data filters to filter out existing contracts.

The options object can consist of the following values:

Example ethofsDataFilter object
{
    name: 'UploadContractNameFilter',
    keyvalues: {
        customKeyFilter: 'customValueFilter',
        customKey2Filter: 'customValue2Filter'
    }
}

Questions? Issues? Suggestions?

Feel free to file a github issue or email us at admin@ethoprotocol.com

We'd love to hear from you!