Coder Social home page Coder Social logo

aiswaryawalter / js-powergate-client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from textileio/js-powergate-client

0.0 0.0 0.0 2.28 MB

Typescript/Javascript client for Textile's Powergate

Home Page: https://docs.textile.io/powergate

License: MIT License

TypeScript 97.40% JavaScript 1.24% Shell 1.35%

js-powergate-client's Introduction

Powergate JS Client (@textile/powergate-client)

GitHub license GitHub package.json version npm (scoped) Release standard-readme compliant

Tests Docs

Typescript/Javascript client for Textile's Powergate.

Use Powergate's multitiered file storage API built on Filecoin and IPFS from javascript environments such as Node, React Native, web browsers, and more.

Table of Contents

Background

The Powergate JS Client is built on top of the Powergate gRPC APIs and contains the logic that makes it straight-forward to build those APIs into JavaScript-based systems. Using the Powergate JS Client requires access to a running instance of the Powergate. Find details on setting up the Powergate here.

The JS Client provides access to the full Powergate API and therefore, does not directly manage access-control. If you plan to use the Powergate JS Client in user-facing systems, we recommend running additional middleware.

Install

npm i @textile/powergate-client

Usage

Start by creating an instance of the client.

import { createPow } from "@textile/powergate-client"

const host = "http://0.0.0.0:6002" // or whatever powergate instance you want

const pow = createPow({ host })

Many APIs are immediately available and don't require authorization.

const { status, messageList } = await pow.health.check()

const { peersList } = await pow.net.peers()

Other APIs require authorization. The main API you'll interact with is the Filecoin File System (FFS), and it requires authorization. First, create a new FFS instance.

const { token } = await pow.ffs.create() // save this token for later use!

Currently, the returned auth token is the only thing that gives you access to your FFS instance at a later time, so be sure to save it securely.

Once you have an auth token, either by creating a new FFS instance or by reading one you previously saved, set the auth token you'd like the Powergate client to use.

pow.setToken(authToken)

Now, the FFS API is available for you to use.

import fs from "fs"
import { ffsTypes } from "@textile/powergate-client"

// get wallet addresses associated with your FFS instance
const { addrsList } = await pow.ffs.addrs()

// create a new address associated with your ffs instance
const { addr } = await pow.ffs.newAddr("my new addr")

// get general info about your ffs instance
const { info } = await pow.ffs.info()

// cache data in IPFS in preparation to store it using FFS
const buffer = fs.readFileSync(`path/to/a/file`)
const { cid } = await pow.ffs.addToHot(buffer)

// store the data in FFS using the default storage configuration
const { jobId } = await pow.ffs.pushConfig(cid)

// watch the FFS job status to see the storage process progressing
const cancel = pow.ffs.watchJobs((job) => {
  if (job.status === ffsTypes.JobStatus.CANCELED) {
    console.log("job canceled")
  } else if (job.status === ffsTypes.JobStatus.FAILED) {
    console.log("job failed")
  } else if (job.status === ffsTypes.JobStatus.SUCCESS) {
    console.log("job success!")
  }
}, jobId)

// watch all FFS events for a cid
const cancel = pow.ffs.watchLogs((logEvent) => {
  console.log(`received event for cid ${logEvent.cid}`)
}, cid)

// get the current desired storage configuration for a cid (this configuration may not be realized yet)
const { config } = await pow.ffs.getCidConfig(cid)

// get the current actual storage configuration for a cid
const { cidinfo } = await pow.ffs.show(cid)

// retreive data from FFS by cid
const bytes = await pow.ffs.get(cid)

// senf FIL from an address managed by your FFS instance to any other address
await pow.ffs.sendFil(addrsList[0].addr, "<some other address>", 1000)

See the Node.js example app in this repository's examples directory.

There are also several useful examples included in the *.spec.ts files of this repo.

API

You can read the generated API docs to see the available Powergate API.

Maintainers

Textile

Contributing

See the contributing file!

PRs accepted.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT (c) 2020 Textile

js-powergate-client's People

Contributors

andrewxhill avatar asutula avatar dependabot-preview[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.