0.3.16 • Published 6 months ago

@seedprotocol/sdk v0.3.16

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

Seed Protocol SDK

The official SDK for Seed Protocol.

The SDK is a heavily opinionated ORM that saves its database (SQLite) and files (OPFS) within the user's browser. All user data is fetched from, or written to, the Ethereum Attestation Service (EAS) and Arweave (More attestation services and decentralized storage providers will be supported in the future).

With all the remote storage on decentralized, public infrastructure, there's no server component to manage or rely on.

The hope is that this local-first and distributed approach will make it easier for developers to build apps with Seed Protocol without ever custodying user data on their own infrastructure.

The SDK is currently used and developed by PermaPress, the first client for Seed Protocol. PermaPress is a product developed by JournoLabs (formerly JournoDAO).

Installing

yarn add @seedprotocol/sdk

Getting Started

The first thing to do when integrating Seed SDK is define your data model by placing a schema.ts file in the root of your project.

As an example, here's the actual data model for PermaPress:

import { ImageSrc, List, Model, Relation, Text } from '@/browser/schema'

@Model
class Image {
  @Text() storageTransactionId!: string
  @Text() uri!: string
  @Text() alt!: string
  @ImageSrc() src!: string
}

@Model
class Post {
  @Text() title!: string
  @Text() summary!: string
  @Relation('Image', 'ImageSrc') featureImage!: string
  @Text('ItemStorage', '/html', '.html') html!: string
  @Text('ItemStorage', '/json', '.json') json!: string
  @Text() storageTransactionId!: string
  @List('Identity') authors!: string[]
  @Text() importUrl!: string
}

@Model
class Identity {
  @Text() name!: string
  @Text() profile!: string
  @Text() displayName!: string
  @Relation('Image', 'ImageSrc') avatarImage!: string
  @Relation('Image', 'ImageSrc') coverImage!: string
}

@Model
class Link {
  @Text() url!: string
  @Text() text!: string
}

const models = {
  Identity,
  Image,
  Link,
  Post,
}

const endpoints = {
  filePaths: '/api/seed/migrations',
  files: '/app-files',
}

export { models, endpoints }

export default { models, endpoints }

This will create a database locally in the browser with all the tables and fields necessary to support your Models.

!NOTE The database is a SQLite file stored in OPFS. We recommend using the OPFS Explorer browser extension to see the files managed by the SDK.

Notice that we create relationships by defining a Property that takes its related Model as its type. For one-to-many relationships, we use the List type and pass in the Model type we want.

So creating a Post would look like this:

import { Post, Image, Identity } from './seed/models'
import html                      from './index.html'

const image = await Image.create({
  src: 'https://imgr.com/image.jpg',
})

const author = await Identity.create({
  name: 'Keith Axline',
  profile: 'Developer for Seed Protocol',
})

const authors = [
  author
]

const post = await Post.create({
  title: 'Some title',
  summary: 'My summary',
  featureImage: image,
  authors,
})

await post.publish()

// And later when we want to update the post
post.title = 'Something else'

await post.publish()

Usage

Item

Create

Files

Subscribe to events:

import { SeedFile } from '@seedprotocol/sdk'

const fileFromUrl = new SeedFile('https://example.com/file.txt')

fileFromUrl.subscribe(( event, status ) => {
  console.log(`Event: ${event}, Status: ${status}`)
})

await fileFromUrl.save()

Create files from various sources:

const fileFromPath = new SeedFile('path/to/file.txt')

const fileFromBlob = new SeedFile(new Blob([ 'Hello, World!' ]))

const fileFromBuffer = new SeedFile(Buffer.from('Hello, World!'))

const fileFromFile = new SeedFile(new File([ 'Hello, World!' ], 'file.txt'))

if ( fileFromPath.isSaved ) {
  console.log('File is saved to Arweave and EAS')
}

Use the SeedFileSystem

import { SeedFileSystem } from '@seedprotocol/sdk'

const fs = new SeedFileSystem()

fs.subscribe(( event, status ) => {
  console.log(`Event: ${event}, Status: ${status}`)
  if ( event === 'connection.success' && status === 'connected' ) {
    console.log('Connected to user\'s file system')
  }

  if ( event === 'connection.error' ) {
    console.error('Error connecting to user\'s file system', event.error)
  }
})

await fs.connect() // User prompted to connect browser wallet

const files = await fs.listFiles('/')

files.forEach(file => {
  console.log(file.name)
  console.log(file.size)
  console.log(file.seedPath) // Each wallet address has a virtual root directory with directories and file system paths
})

Images

import { SeedImage } from '@seedprotocol/sdk'

const imgFromUrl = new SeedImage('https://example.com/image.png')

const imgFromDataUrl = new SeedImage('data:image/png;base64,iVB....')

await imgFromUrl.save()

const imgBlob    = imgFromUrl.blob()
const imgBuffer  = imgFromUrl.buffer()
const imgDataUrl = imgFromUrl.dataUrl()
---
Title: Seed Protocol SDK
---
%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#e0f2fe',
      'primaryTextColor': '#0c4a6e',
      'primaryBorderColor': '#38bdf8',
      'lineColor': '#475569',
      'secondaryColor': '#d9f99d',
      'tertiaryColor': '#fff',
      'tertiaryBorderColor': '#7e22ce',
      'tertiaryTextColor': '#7e22ce',
      'clusterBkg': '#fff'
    }
  }
}%%
flowchart TD
    A{Developer}
    B{User}
    C{Seed Dev}
    N{SDK}
    Q[App Code]
    D[(sdk_config_db)]
    E[(app_db)]
    L[(seeds_db)]
    G[Schema/Migration files
    browser/db/seedSchema]
H[Schema/Migration files
.seed/app/schema]
R[Schema/Migration files
node/db/sdkSchema]

I[browser.seed.db.config.mjs]
J[sdk.db.config.mjs]
K[browser.app.db.config.mjs]

O[files.json
seed/endpoint/list]
P[Files
seed/endpoint/files]

S[$> seed init]
T[scripts/bin]

subgraph SDK Code
C--changes Seeds or Versions --> I
C-- changes Models or Properties --> J
J-- generates -->R
I-- generates --> G
subgraph Package Bundle
R
G
end
end



subgraph App Code
A-- adds Models and Properties --> K
subgraph SDK
D
H
end
A-- runs --> S
S-- calls --> T
T-- reads--> K
T-- generates --> D
T-- generates --> H
end

subgraph App Server
H-- served at endpoint --> O
H-- served at endpoint --> P
end



subgraph Browser
B-- adds data--> Q
subgraph App
Q-- calls SDK--> N
subgraph SDK
E-- notifies -->N
N<-- files . json --> O
N<-- files --> P
N-- updates -->E
N-- updates --> L

end
end
end

AVersions From EAS BSeeds From EAS CList of Items DList of Properties E{Property Machine} F{Item Machine} G{Items Machine}

sequenceDiagram
    Items Service ->>+ EAS: Seeds Request
    EAS -->>+ Items Service: Seed Attestations
    loop Each Seed Attestation
        Items Service ->>+ Items Service: Extract Seed Uid
    end
    Note over Items Service: Now we have SeedUid[]
    Items Service ->>+ EAS: Versions Request w/ SeedUid[]
    EAS -->>+ Items Service: Version Attestations
    loop Each SeedUid
        Items Service ->>+ Items Service: Sort Version Uids DESC
    end
    loop Each most recent Version for Seed
        Items Service ->>+ Item Service: Create Item
    end
    Note over Items Service: Now we have VersionUid[] for each SeedUid<br />sorted by `createdAt` DESC
    Item Service ->>+ EAS: Properties Request w/ VersionUid[]
    EAS -->>+ Item Service: Properties Attestations
    loop Each Property Attestation
        Item Service ->>+ Property Service: Create Property Service
    end
    Note over Item Service: Now has all Property Services<br />under this._services
0.3.16

6 months ago

0.3.15

6 months ago

0.3.14

6 months ago

0.3.13

6 months ago

0.3.12

6 months ago

0.3.11

7 months ago

0.3.10

7 months ago

0.3.9

7 months ago

0.3.8

7 months ago

0.3.7

7 months ago

0.3.6

7 months ago

0.3.5

8 months ago

0.3.4

8 months ago

0.3.3

8 months ago

0.3.2

8 months ago

0.3.1

9 months ago

0.3.0

9 months ago

0.2.54

9 months ago

0.2.53

9 months ago

0.2.52

9 months ago

0.2.51

9 months ago

0.2.50

9 months ago

0.2.49

9 months ago

0.2.48

9 months ago

0.2.47

9 months ago

0.2.46

9 months ago

0.2.45

9 months ago

0.2.44

9 months ago

0.2.43

10 months ago

0.2.42

10 months ago

0.2.41

10 months ago

0.2.40

10 months ago

0.2.39

10 months ago

0.2.38

10 months ago

0.2.37

10 months ago

0.2.36

10 months ago

0.2.35

10 months ago

0.2.34

10 months ago

0.2.33

10 months ago

0.2.32

10 months ago

0.2.31

10 months ago

0.2.30

10 months ago

0.2.29

10 months ago

0.2.28

10 months ago

0.2.27

10 months ago

0.2.26

10 months ago

0.2.25

10 months ago

0.2.24

10 months ago

0.2.23

10 months ago

0.2.22

10 months ago

0.2.21

10 months ago

0.2.20

10 months ago

0.2.19

10 months ago

0.2.18

10 months ago

0.2.17

10 months ago

0.2.16

10 months ago

0.2.15

10 months ago

0.2.14

10 months ago

0.2.13

10 months ago

0.2.12

10 months ago

0.2.11

10 months ago

0.2.10

10 months ago

0.2.9

10 months ago

0.2.8

10 months ago

0.2.7

10 months ago

0.2.6

10 months ago

0.2.5

10 months ago

0.2.4

10 months ago

0.2.3

10 months ago

0.2.2

10 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.129

10 months ago

0.1.128

10 months ago

0.1.127

10 months ago

0.1.126

10 months ago

0.1.125

10 months ago

0.1.124

10 months ago

0.1.122

10 months ago

0.1.121

10 months ago

0.1.120

10 months ago

0.1.119

10 months ago

0.1.118

10 months ago

0.1.117

10 months ago

0.1.116

10 months ago

0.1.115

10 months ago

0.1.114

10 months ago

0.1.113

10 months ago

0.1.112

10 months ago

0.1.111

10 months ago

0.1.110

10 months ago

0.1.109

10 months ago

0.1.108

10 months ago

0.1.107

10 months ago

0.1.106

10 months ago

0.1.105

10 months ago

0.1.104

10 months ago

0.1.103

11 months ago

0.1.102

11 months ago

0.1.101

11 months ago

0.1.100

11 months ago

0.1.99

11 months ago

0.1.98

11 months ago

0.1.97

11 months ago

0.1.96

11 months ago

0.1.95

11 months ago

0.1.94

11 months ago

0.1.93

11 months ago

0.1.92

11 months ago

0.1.91

11 months ago

0.1.90

11 months ago

0.1.89

11 months ago

0.1.88

11 months ago

0.1.87

11 months ago

0.1.86

11 months ago

0.1.85

11 months ago

0.1.84

11 months ago

0.1.83

11 months ago

0.1.82

11 months ago

0.1.81

11 months ago

0.1.80

11 months ago

0.1.79

11 months ago

0.1.78

11 months ago

0.1.77

11 months ago

0.1.76

11 months ago

0.1.75

11 months ago

0.1.74

11 months ago

0.1.73

11 months ago

0.1.72

11 months ago

0.1.71

11 months ago

0.1.70

11 months ago

0.1.69

11 months ago

0.1.67

11 months ago

0.1.66

11 months ago

0.1.65

11 months ago

0.1.64

11 months ago

0.1.63

11 months ago

0.1.62

11 months ago

0.1.61

11 months ago

0.1.60

11 months ago

0.1.59

11 months ago

0.1.58

11 months ago

0.1.57

11 months ago

0.1.56

11 months ago

0.1.55

11 months ago

0.1.54

11 months ago

0.1.53

11 months ago

0.1.52

11 months ago

0.1.51

11 months ago

0.1.50

11 months ago

0.1.49

11 months ago

0.1.48

11 months ago

0.1.47

11 months ago

0.1.46

11 months ago

0.1.45

11 months ago

0.1.44

11 months ago

0.1.43

11 months ago

0.1.42

11 months ago

0.1.41

12 months ago

0.1.40

12 months ago

0.1.38

12 months ago

0.1.37

12 months ago

0.1.36

12 months ago

0.1.35

12 months ago

0.1.34

12 months ago

0.1.33

12 months ago

0.1.32

12 months ago

0.1.31

12 months ago

0.1.30

12 months ago

0.1.29

12 months ago

0.1.28

12 months ago

0.1.27

12 months ago

0.1.26

12 months ago

0.1.25

12 months ago

0.1.24

12 months ago

0.1.23

12 months ago

0.1.22

12 months ago

0.1.21

12 months ago

0.1.20

12 months ago

0.1.19

12 months ago

0.1.18

12 months ago

0.1.17

12 months ago

0.1.16

12 months ago

0.1.15

12 months ago

0.1.14

12 months ago

0.1.13

12 months ago

0.1.12

12 months ago

0.1.11

12 months ago

0.1.10

12 months ago

0.1.8

12 months ago

0.1.7

12 months ago

0.1.6

12 months ago

0.1.5

12 months ago

0.1.4

12 months ago

0.1.3

12 months ago

0.1.2

12 months ago

0.1.1

12 months ago

0.1.0

12 months ago