Git Product home page Git Product logo

blossom-drive-sdk's Introduction

๐ŸŒธ blossom-drive-sdk

blossom-drive-sdk is a library working with drives created in blossom-drive

Documentation

Working with drives

The Drive class can be used to easily manage the file tree inside a drive event k:30563 (Docs)

The Drive class takes two arguments, signer and publisher. The signer is an async method that takes an unsigned nostr event and returns a signed event. The publisher is a method used to "publish" the signed nostr event to some relays so it can be loaded later

The simplest signer and publisher would look something like

import { Relay } from "nostr-tools/relay";
const relay = await Relay.connect("wss://relay.example.com");

async function signer(template) {
  return await window.nostr.signEvent(template);
}
function publisher(event) {
  relay.publish(event);
}

There are generally two ways to create a Drive class.

// create a new empty drive
const drive = new Drive(signer, publisher);
drive.name = "Empty";
drive.description = "an empty drive";

// create a drive from an event
const drive = Drive.fromEvent(event, signer, publisher);

Manage files and folder in a drive

Once you have create a drive you need to add some files to it

// This will create a /bitcoin.pdf file in the drive set to a certain sha256
drive.setFile("/bitcoin.pdf", {
  sha256: "b1674191a88ec5cdd733e4240a81803105dc412d6c6708d53ab94fc248f4f553",
  size: 184929,
  type: "application/pdf",
});

// Create an empty folder
drive.getFolder("/New Folder", true);

// Move a file
drive.move("/bitcoin.pdf", "/New Folder/bitcoin.pdf");

// Remove a file
drive.remove("/New Folder/bitcoin.pdf");

You can see all the methods for managing files in the documentation

Working with encrypted drives

Encrypted drives use a different event kind k:30564 than normal drives

The EncryptedDrive class is every similar to the Drive class but it has a few additional methods

  • unlock(password) Attempts to decrypt and drive
  • lock() Forgets the password and resets the drive
  • setPassword(password) Used to set the password on a newly created drive

You can read more here

Examples

Create a new drive

import { Relay } from "nostr-tools/relay";
import { DRIVE_KIND, Drive } from "blossom-drive-sdk";

async function signer(template) {
  return await window.nostr.signEvent(template);
}

const relay = await Relay.connect("wss://relay.example.com");
function publisher(event) {
  relay.publish(event);
}

const drive = new Drive(signer, publisher);
drive.name = "New Drive";
drive.description = "A test drive";

// add a file
drive.setFile("/bitcoin.pdf", {
  sha256: "b1674191a88ec5cdd733e4240a81803105dc412d6c6708d53ab94fc248f4f553",
  size: 184929,
  type: "application/pdf",
});

// create an empty folder
drive.getFolder("/New Folder", true);

// finally save the drive to nostr
await drive.save();

Create a drive from an events

import { Relay } from "nostr-tools/relay";
import { DRIVE_KIND, Drive } from "blossom-drive-sdk";

function signer(template) {
  return window.nostr.signEvent(template);
}
function publisher(event) {
  relay.publish(event);
}

const relay = await Relay.connect("wss://relay.example.com");

const pubkey = await window.nostr.getPublicKey();

const driveEvents = [];
const sub = relay.subscribe([{ kinds: [DRIVE_KIND], author: [pubkey] }], {
  onevent(event) {
    driveEvents.push(event);
  },
  oneose() {
    sub.close();

    const drives = [];
    for (let event of driveEvents) {
      // create drive class from an event
      const drive = Drive.fromEvent(event, signer, publisher);
    }
  },
});

Update a drive from an event

import { Relay } from "nostr-tools/relay";
import { DRIVE_KIND, Drive } from "blossom-drive-sdk";

function signer(template) {
  return window.nostr.signEvent(template);
}

const relay = await Relay.connect("wss://relay.example.com");
function publisher(event) {
  relay.publish(event);
}

const drive = new Drive(signer, publisher);

const driveEvents = [];
const sub = relay.subscribe([{ kinds: [DRIVE_KIND], "#d": ["random"] }], {
  onevent(event) {
    drive.update(event);
  },
});

blossom-drive-sdk's People

Contributors

hzrd149 avatar

Stargazers

Sebastian Hagens avatar  avatar

Watchers

 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.