-
Notifications
You must be signed in to change notification settings - Fork 527
Expand file tree
/
Copy pathipns.js
More file actions
80 lines (62 loc) · 2.31 KB
/
ipns.js
File metadata and controls
80 lines (62 loc) · 2.31 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import all from 'it-all'
import { readSetting, writeSetting } from '../lib/local-storage.js'
import { dispatchAsyncProvide } from './files/utils.js'
const init = () => ({
keys: [],
expectedPublishTime: readSetting('expectedPublishTime') || 60
})
const ipnsBundle = {
name: 'ipns',
reducer: (state = init(), action) => {
if (action.type === 'CACHE_IPNS_KEYS') {
return { ...state, keys: action.payload }
}
if (action.type === 'SET_EXPECTED_PUBLISH_TIME') {
return { ...state, expectedPublishTime: action.payload }
}
return state
},
selectIpnsKeys: (state) => state.ipns.keys || [],
selectExpectedPublishTime: (state) => state.ipns.expectedPublishTime,
doFetchIpnsKeys: () => async ({ getIpfs, dispatch }) => {
const ipfs = getIpfs()
const rawKeys = await ipfs.key.list()
const keys = []
for (const { id, name } of rawKeys) {
const names = await all(ipfs.name.resolve(`/ipns/${id}`, { offline: true }))
const published = names.length > 0
keys.push({ id, name, published })
}
dispatch({ type: 'CACHE_IPNS_KEYS', payload: keys })
},
doGenerateIpnsKey: (name) => async ({ getIpfs, store }) => {
const ipfs = getIpfs()
await ipfs.key.gen(name)
store.doFetchIpnsKeys()
},
doRemoveIpnsKey: (name) => async ({ getIpfs, store }) => {
const ipfs = getIpfs()
await ipfs.key.rm(name)
store.doFetchIpnsKeys()
},
doRenameIpnsKey: (oldName, newName) => async ({ getIpfs, store }) => {
const ipfs = getIpfs()
await ipfs.key.rename(oldName, newName)
store.doFetchIpnsKeys()
},
doPublishIpnsKey: (cid, key) => async ({ getIpfs, store }) => {
const ipfs = getIpfs()
await ipfs.name.publish(cid, { key })
// Trigger background provide operation for the published CID
dispatchAsyncProvide(cid, ipfs, 'IPNS')
},
doUpdateExpectedPublishTime: (time) => async ({ store, dispatch }) => {
// moderate expectation: publishing should take no longer than average
// between old expectation and the length of the last publish + some buffer
const oldExpectedTime = store.selectExpectedPublishTime()
const avg = Math.floor((time * 1.5 + oldExpectedTime) / 2)
await writeSetting('expectedPublishTime', avg)
dispatch({ type: 'SET_EXPECTED_PUBLISH_TIME', payload: avg })
}
}
export default ipnsBundle