• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

FleekHQ/space-sdk: The Space SDK is a JavaScript/Typescript library for building ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

FleekHQ/space-sdk

开源软件地址:

https://github.com/FleekHQ/space-sdk

开源编程语言:

TypeScript 94.8%

开源软件介绍:

Space SDK

Fleek Dev Slack License

Javascript/Typescript library for interacting with Space in web/browser applications via an implementation of the Space API. Build websites or applications that can easily leverage Open Web protocols (IPFS, Textile, GunDB, Ethereum) to enable Web3-ready features like:

  • File and directory storage / retrieval in user-controlled, encrypted, and distributed storage.
  • Key-pair based identity management and challenge-based authentication.
  • Decentralized and secure key and bucket metadata storage.
  • Private and end-to-end encrypted, or public file sharing.

The Space SDK is a close friend of the Space Daemon, its desktop-focused counterpart.

You can find the SDK's documentation here:

Default Implementations

The Space SDK is modular and protocol agnostic. You can use the APIs and interfaces as is, with Space's default implementations, or replace them with your own custom ones.

Feature Description Service/Protocol
Users Key-pair based identity creation, and challenge authentication. Textile Users API
Storage File, directory, and bucket creation/listing. IPFS / Textile
Metadata Secure Bucket/directory schema storage GunDB
Sharing Private and public file sharing Textile

Introduction

@spacehq/sdk provides a suit of functionality to perform different action on Space.

Installation

Install the sdk using this npm command:

npm install @spacehq/sdk

Usage

Space SDK provides an interface perform the following actions:

  • Creating identities

  • Create files and directories

  • List files and directories

  • Creating buckets

  • Sharing buckets

Full SDK Documentation with examples can be found here

1. Identities

This involves managing users and their identities.

import { Users } from '@spacehq/sdk';

const users = new Users({ endpoint: 'wss://dev.space.storage' });

// createIdentity generate a random keypair identity
const identity = await users.createIdentity();

// the new keypair can be used to authenticate a new user
// `users.authenticate()` generates hub API session tokens for the keypair identity.
const user = await users.authenticate(identity);
// `user` can be used with the storage class to provide identity.

// user's identity can also be backed up with a special recovery phrase
const uuid = 'specify-uuid-representing-user-in-your-system';
const passphrase = 'specify-unique-pass-phrase-related-to-backup-type';
const backupType = VaultBackupType.Google;
await users.backupKeysByPassphrase(uuid, passphrase, backupType, user.identity);

// backed up users identity can also be recovered later
const recoveredUser = await users.recoverKeysByPassphrase(uuid, passphrase, backupType);
// `recoveredUser` has same authentication as `user` above.

Check the User's class for more examples of how to manage users with the sdk.

2. Storage

This involves operations to create and list files and directories in space storage.

import { UserStorage, AddItemsResultSummary } from '@spacehq/sdk';

const storage = new UserStorage(user);
await storage.createFolder({ bucket: 'personal', path: 'topFolder' });
const result = await storage.listDirectory({ bucket: 'personal', path: '' });
// result contains `topFolder` items

// upload a file
const uploadResponse = await spaceStorage.addItems({
   bucket: 'personal',
   files: [
     {
       path: 'file.txt',
       content: '',
     },
     {
       path: 'space.png',
       content: '',
     }
   ],
});
// uploadresponse is an event listener
uploadResponse.once('done', (data: AddItemsEventData) => {
  const summary = data as AddItemsResultSummary;
  // returns a summary of all files and their upload status
});

3. Sharing

This includes operations to share your storage items with existing user (identities)

import { UserStorage } from '@space/sdk';

const storage = new UserStorage(user);

// you can share privately with existing users via their public key:
await storage.shareViaPublicKey({
    publicKeys: [{
      id: '[email protected]', // or any identifier for the user
      pk: 'user-pk-hex-or-multibase', // optional, omit if user doesn't exist yet, it would generate temp access key
    }],
    paths: [{
        bucket: 'personal',
        path: '/file/path/here'
    }],
});

// or you could share the file publicly by generating a link. Generated link references
await spaceStorage.setFilePublicAccess({
  bucket: 'personal',
  path: '/file.txt',
  allowAccess: true, // <- set to false to revoke public access
});

Migrating from Space Daemon

If you are already familiar with the space daemon and its gRPC methods and would like to start using the space-sdk here are some pointers on how those gRPC methods correspond to functionalities exposed by the space-sdk.

Key Pairs (GenerateKeyPair)

In the sdk the concept of Key Pairs is represented as an Identity. To create a new Identity similar to the GenerateKeyPair method, you would do:

import { Users, BrowserStorage } from '@spacehq/sdk';

const users = new Users({ endpoint: 'wss://auth-dev.space.storage' });

// createIdentity generate a random keypair identity
const identity = await users.createIdentity();

identity represents a keypair and its primary key is accessible via identity.public.toString().

Managing authenticated users

In space-daemon the generated keypair is stored in the operating systems keychain but in space-sdk you would need to provide an IdentityStorage to the Users class when initializing it. For the browser environment there exists a BrowserStorage implementation you can use.

import { Users, BrowserStorage } from '@spacehq/sdk';

const users = await Users.withStorage(
    new BrowserStorage(), 
    { endpoint: 'wss://auth-dev.space.storage' }
);

Users.withStorage will load and authenticate all identities that exist inside the provided IdentityStorage. You can access all authenticated users through the Users.list method.

const spaceUsers = await users.list();

To authenticate a new user identity and get a SpaceUser, you can call the Users.authenticate method:

const spaceUser = await users.authenticate(identity);

Users.authentication would do two things:

  • Generate a SpaceUser.
  • Stores the new users information in the IdentityStorage, so subsequent initialization of Users.withStorage() would have the users loaded.

NOTE: An existing space user can also be gotten from Users.recoverKeysByPassphrase.

To delete a user from users lists, you can delete the user by pass the publicKey of that user to Users.remove.

await users.remove(spaceUser.identity.public.toString());

Managing current active user

If you have the concept of a current active user in your application that uses space-sdk. We recommend that you keep track of that users public key in your application and use it to filter the list method's result to get the authenticated SpaceUser for that public key.

On logout, you can call the remove method to stop tracking the user.

GetAPISessionToken

In space daemon GetAPISessionToken returns the message:

message GetAPISessionTokensResponse {
  string hubToken = 1;
  string servicesToken = 2;
}

In order to get the servicesToken and hubToken for a particular user, you would need to authenticate that user identity:

const spaceUser = await users.authenticate(identity);

The spaceUser.token value is the servicesToken, while the spaceUser.storageAuth.token is the hubToken.

Also, note that when an existing user is recovered via the Users.recoverKeysByPassphrase method, the SpaceUser returns is also authenticated and has the session tokens.

GetPublicKey

In space daemon GetPublicKey returned the id of the current keypair in keychain, but since space-sdk returns the identity object. You can get the public key bytes for a particular identity through identity.public.pubKey.

Also, an authenticated SpaceUser identity can be found in the identity field.

Storage Methods (createFolder, listDirectory, openFile, addItems)

The storage gRPC methods on space daemon can now be performed using the UserStorage class of the space-sdk.

import { UserStorage, AddItemsResultSummary } from '@spacehq/sdk';

const storage = new UserStorage(user);
await storage.createFolder({ bucket: 'personal', path: 'topFolder' });
const result = await storage.listDirectory({ path: '' });
// result contains `topFolder` items

// upload a file
const uploadResponse = await spaceStorage.addItems({
   bucket: 'personal',
   files: [
     {
       path: 'file.txt',
       content: 'plain-text-value',
     },
     {
       path: 'space.png',
       content: '', // could also be a ReadableStream<Uint8Array> or ArrayBuffer
     }
   ],
});
// uploadresponse is an event listener
uploadResponse.once('done', (data: AddItemsEventData) => {
  const summary = data as AddItemsResultSummary;
  // returns a summary of all files and their upload status
});

// read content of an uploaded file
const fileResponse = await storage.openFile({ bucket: 'personal', path: '/file.txt'});
const fileContent = await fileResponse.consumeStream();
// new TextDecoder('utf8').decode(actualTxtContent) == 'plain-text-value'

Contributing

All contributions are welcome. Before getting started, kindly take some time to review our contributing guidelines and code of conduct.

LICENSE

MIT




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
aschmahmann/ipshare: Sharing Utilities for IPFS发布时间:2022-06-23
下一篇:
ngngardner/dart_ipfs_client: HTTP API Client for IPFS发布时间:2022-06-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap