This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
ipfs.js
57 lines (49 loc) · 1.64 KB
/
ipfs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as io from '../utils/index.js'
import AccessController from './interface.js'
const type = 'ipfs'
export default class IPFSAccessController extends AccessController {
constructor (ipfs, options) {
super()
this._ipfs = ipfs
this._write = Array.from(options.write || [])
}
// Returns the type of the access controller
static get type () { return type }
// Return a Set of keys that have `access` capability
get write () {
return this._write
}
async canAppend (entry, identityProvider) {
// Allow if access list contain the writer's publicKey or is '*'
const key = entry.identity.id
if (this.write.includes(key) || this.write.includes('*')) {
// check identity is valid
return identityProvider.verifyIdentity(entry.identity)
}
return false
}
async load (address) {
// Transform '/ipfs/QmPFtHi3cmfZerxtH9ySLdzpg1yFhocYDZgEZywdUXHxFU'
// to 'QmPFtHi3cmfZerxtH9ySLdzpg1yFhocYDZgEZywdUXHxFU'
if (address.indexOf('/ipfs') === 0) { address = address.split('/')[2] }
try {
this._write = await io.read(this._ipfs, address)
} catch (e) {
console.log('IPFSAccessController.load ERROR:', e)
}
}
async save () {
let cid
try {
cid = await io.write(this._ipfs, 'dag-cbor', { write: JSON.stringify(this.write, null, 2) })
} catch (e) {
console.log('IPFSAccessController.save ERROR:', e)
}
// return the manifest data
return { address: cid }
}
static async create (orbitdb, options = {}) {
options = { ...options, ...{ write: options.write || [orbitdb.identity.id] } }
return new IPFSAccessController(orbitdb._ipfs, options)
}
}