Code Monkey home page Code Monkey logo

subspace.js's Introduction

@subspace/subspace

This JavaScript library provides a simple API to submit and retrieve user data to the Subspace network. The project is in active development with a live test network designed to provide scalable and distributed archival storage.

Storage API.

The Storage API exposes a Subspace node feature called pallet-object-store that implements RPC methods to store an Object from user-provided data. The user can send a simple text or even a file; the pallet-object-store receives an Object to store and generate an objectId.

  • put: Receive a signed transaction containing an Object to store. It emit a DataSubmitted Event with an objectId to get the Object from the network.

  • findObject: Receives an objectId to find the related Object stored, if it exists this method will return the Object data.

To expose these methods, this library implements two main classes:

  • Identity: Class to load a keyPair from different sources. An instance of this class is required to create a SubspaceClient.

    • fromWeb3: Load keyPair from web3Accounts using @polkadot/extension-dapp.

    • fromUri: Load keyPair from a secret URI, Example: //Alice///password or a mnemonic phrase.

  • SubspaceClient: Class that loads an Identity instance and creates providers to interact with the network.

    • connect: Create an ApiPromise and WsProvider, returning a SubspaceClient instance ready to call:

      • putObject: Receives an Object as Uint8Array, it create and submit a signed put transaction and return an objectId.

      • getObject: Receives an objectId calling findObject to return the Object as Uint8Array.

Please, note that object will be retrievable after it is archived on the blockchain, it won't be available right away. Archiving takes 100-120 blocks to complete. Caching mechanism for faster retrievability will be introduced soon

Run this project.

Clone this repository:

  • git clone https://github.com/subspace/subspace.js.git

Install dependencies

  • npm ci

Build the library.

  • npm run build

Usage

// Import the library.
import { SubspaceClient, Identity } from "@subspace/subspace";

// Generate an Identity from node.js
const identity = await Identity.fromUri(mnemonic);
// Generate an Identity from broser using web3Account (injected by extension)
const identity = await Identity.fromWeb3();

// Generate a SubspaceClient and connect to node and farmer rpc endpoints.
const subspaceClient = await SubspaceClient.connect(
  identity,
  NODE_WS_PROVIDER,
  FARMER_WS_PROVIDER
);

// Put the file as (Uint8Array) in to the objectStore and return the objectId
const objectId: string = await subspaceClient.putObject(objectData);

// Using the objectId get the file as (Uint8Array) from the objectStore.
const object: Uint8Array = await subspaceClient.getObject(objectId);

Run the examples.

Running a local network

You can check the Subspace repository to learn how configure and run your local network.

Make sure you have Node RPC and Farmer endpoints exposed

subspace.js's People

Contributors

isserge avatar 1devndogs 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.