Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User creation and authentication #25

Merged
merged 29 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@
"typescript": "^3.8.2"
},
"dependencies": {
"base58-universal": "^1.0.0",
"borc": "^2.1.1",
"get-ipfs": "^1.2.0",
"ipld-dag-pb": "^0.18.2",
"keystore-idb": "^0.8.0",
"keystore-idb": "^0.11.1",
"query-string": "^6.11.1"
}
}
1 change: 1 addition & 0 deletions src/@types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
declare module 'base58-universal/main.js'
declare module 'ipld-dag-pb'
declare module 'borc'
24 changes: 24 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { KeyStore } from 'keystore-idb/types'


// CONSTANTS


export const API_ENDPOINT = 'https://runfission.com'



// BASE64


export function base64UrlDecode(a: string): string {
return atob(a).replace(/\_/g, "/").replace(/\-/g, "+")
}

export function base64UrlEncode(b: string): string {
return makeBase64UrlSafe(btoa(b))
}

export function makeBase64UrlSafe(a: string): string {
return a.replace(/\//g, "_").replace(/\+/g, "-").replace(/=+$/, "")
}
26 changes: 23 additions & 3 deletions src/ffs/ffs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PublicTree from './public'
import PrivateTree from './private'
import { Tree, File, Links } from './types'
import { File, Links, SyncHook, Tree } from './types'
import { CID, FileContent } from '../ipfs'
import pathUtil from './path'
import link from './link'
Expand All @@ -12,13 +12,16 @@ export class FileSystem {
root: PublicTree
publicTree: PublicTree
privateTree: PrivateTree
syncHooks: Array<SyncHook>

private key: string

constructor(root: PublicTree, publicTree: PublicTree, privateTree: PrivateTree, key: string) {
this.root = root
this.publicTree = publicTree
this.privateTree = privateTree
this.key = key
this.syncHooks = []
}

static async empty(keyName: string = 'filesystem-root'): Promise<FileSystem> {
Expand Down Expand Up @@ -92,10 +95,27 @@ export class FileSystem {
const privCID = await this.privateTree.putEncrypted(this.key)
const pubLink = link.make('public', pubCID, false)
const privLink = link.make('private', privCID, false)

this.root = this.root
.updateLink(pubLink)
.updateLink(privLink)
return this.root.put()

const cid = await this.root.put()

this.syncHooks.forEach(hook => {
hook(cid)
})

return cid
}

addSyncHook(hook: SyncHook): Array<SyncHook> {
this.syncHooks.push(hook)
return this.syncHooks
}

removeSyncHook(hook: SyncHook): Array<SyncHook> {
return this.syncHooks.filter(h => h !== hook)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added hooks here ☝️


async runOnTree<a>(path: string, updateTree: boolean, fn: (tree: Tree, relPath: string) => Promise<a>): Promise<a> {
Expand All @@ -107,7 +127,7 @@ export class FileSystem {
result = await fn(this.publicTree, relPath)
if(updateTree && PublicTree.instanceOf(result)){
this.publicTree = result
}
}
}else if(head === 'private') {
result = await fn(this.privateTree, relPath)
if(updateTree && PrivateTree.instanceOf(result)){
Expand Down
63 changes: 40 additions & 23 deletions src/ffs/types.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
import { FileContent, CID } from '../ipfs'

export type AddLinkOpts = {
shouldOverwrite?: boolean

// FILES
// -----

export interface File {
content: FileContent
put(): Promise<CID>
}

export type NonEmptyPath = [string, ...string[]]
export interface FileStatic {
create: (content: FileContent) => File
fromCID: (cid: CID) => Promise<File>
}

export type PrivateTreeData = {
key: string
links: Links
export interface PrivateFileStatic extends FileStatic{
fromCIDWithKey: (cid: CID, key: string) => Promise<File>
}


// LINKS
// -----

export type AddLinkOpts = {
shouldOverwrite?: boolean
}

export type Link = {
name: string
cid: CID
size?: number
size?: number
mtime?: number
isFile: boolean
}

export type Links = { [name: string]: Link }

export interface FileStatic {
create: (content: FileContent) => File
fromCID: (cid: CID) => Promise<File>
}

export interface PrivateFileStatic extends FileStatic{
fromCIDWithKey: (cid: CID, key: string) => Promise<File>
}
// MISC
// ----

export interface File {
content: FileContent
put(): Promise<CID>
}
export type NonEmptyPath = [string, ...string[]]
export type SyncHook = (cid: CID) => any

export interface TreeStatic {
empty: () => Promise<Tree>
fromCID: (cid: CID) => Promise<Tree>

// TREE
// ----

export type PrivateTreeData = {
key: string
links: Links
}

export interface PrivateTreeStatic extends TreeStatic {
Expand All @@ -47,18 +59,18 @@ export interface PrivateTreeStatic extends TreeStatic {
export interface Tree {
links: Links
isFile: boolean

static: {
tree: TreeStatic
file: FileStatic
}


ls(path: string): Promise<Links>
mkdir(path: string): Promise<Tree>
cat(path: string): Promise<FileContent>
add(path: string, content: FileContent): Promise<Tree>
get(path: string): Promise<Tree | File | null>
pathExists(path: string): Promise<boolean>
pathExists(path: string): Promise<boolean>
addChild(path: string, toAdd: Tree | File): Promise<Tree>

put(): Promise<CID>
Expand All @@ -71,3 +83,8 @@ export interface Tree {
rmLink(name: string): Tree
copyWithLinks(links: Links): Tree
}

export interface TreeStatic {
empty: () => Promise<Tree>
fromCID: (cid: CID) => Promise<Tree>
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import auth from './auth'
import * as ffs from './ffs'
import ipfs from './ipfs'
import keystore from './keystore'
import user from './user'

export default {
auth,
ffs,
ipfs,
keystore
keystore,
user
}
6 changes: 6 additions & 0 deletions src/keystore/basic.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { getKeystore } from './config'
import keystore from 'keystore-idb'

export const clear = (): Promise<void> => {
return keystore.clear()
}

export const getKeyByName = async (keyName: string): Promise<string> => {
const ks = await getKeystore()
return ks.exportSymmKey(keyName)
}

export default {
clear,
getKeyByName
}
Loading