在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:FleekHQ/space-sdk开源软件地址:https://github.com/FleekHQ/space-sdk开源编程语言:TypeScript 94.8%开源软件介绍:Space SDKJavascript/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:
The Space SDK is a close friend of the Space Daemon, its desktop-focused counterpart. You can find the SDK's documentation here: Default ImplementationsThe 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.
Introduction
InstallationInstall the sdk using this npm command:
UsageSpace SDK provides an interface perform the following actions:
Full SDK Documentation with examples can be found here 1. IdentitiesThis 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. StorageThis 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. SharingThis 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 DaemonIf 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 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();
Managing authenticated usersIn 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 import { Users, BrowserStorage } from '@spacehq/sdk';
const users = await Users.withStorage(
new BrowserStorage(),
{ endpoint: 'wss://auth-dev.space.storage' }
);
const spaceUsers = await users.list(); To authenticate a new user identity and get a SpaceUser,
you can call the const spaceUser = await users.authenticate(identity);
NOTE: An existing space user can also be gotten from To delete a user from users lists, you can delete the user by pass the await users.remove(spaceUser.identity.public.toString()); Managing current active userIf 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 On logout, you can call the remove method to stop tracking the user. GetAPISessionTokenIn space daemon GetAPISessionToken returns the message:
In order to get the const spaceUser = await users.authenticate(identity); The Also, note that when an existing user is recovered via the GetPublicKeyIn space daemon Also, an authenticated Storage Methods (createFolder, listDirectory, openFile, addItems)The storage gRPC methods on space daemon can now be performed using the 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' ContributingAll contributions are welcome. Before getting started, kindly take some time to review our contributing guidelines and code of conduct. LICENSE |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论