Skip to content
Permalink
Newer
Older
100644 171 lines (147 sloc) 4.39 KB
1
/*
2
This library controls the IPFS interface for the app.
3
*/
4
5
/*
6
This library contains the logic around the browser-based IPFS full node.
7
*/
8
9
import IPFS from '@chris.troutner/ipfs'
10
import IpfsCoord from 'ipfs-coord'
11
12
// CHANGE THESE VARIABLES
13
const CHAT_ROOM_NAME = 'psf-ipfs-chat-001'
14
15
// JSON-LD schema used in announcement.
16
// Customize this data for your own app.
17
const name = 'Browser Chat ' + Math.floor(Math.random() * 1000)
18
const announceJsonLd = {
19
'@context': 'https://schema.org/',
20
'@type': 'WebAPI',
21
name: name,
22
description: 'This is a browser-based IPFS node.',
23
documentation: '',
24
provider: {
25
'@type': 'Organization',
26
name: 'Permissionless Software Foundation',
27
url: 'https://PSFoundation.cash'
28
}
29
}
30
31
let _this
32
33
class IpfsControl {
34
constructor (ipfsConfig) {
35
this.statusLog = ipfsConfig.statusLog
36
this.handleChatLog = ipfsConfig.handleChatLog
37
this.wallet = ipfsConfig.bchWallet
38
this.privateLog = ipfsConfig.privateLog
39
40
_this = this
41
}
42
43
// Top level function for controlling the IPFS node. This funciton is called
44
// by the componentDidMount() function of the page.
45
async startIpfs () {
46
try {
47
console.log('Setting up instance of IPFS...')
48
this.statusLog('Setting up instance of IPFS...')
50
// Use DHT routing and ipfs.io delegates.
51
const ipfsOptions = {
52
config: {
53
Bootstrap: [],
56
HighWater: 30,
57
LowWater: 10
58
},
59
AddrFilters: []
60
},
61
Routing: {
62
Type: 'dhtclient'
63
},
64
preload: {
65
enabled: false
66
},
67
offline: true
68
},
69
libp2p: {
70
config: {
71
dht: {
72
enabled: true,
73
clientMode: true
74
}
79
// const ipfsOptions = {
80
// Bootstrap: [],
81
// Swarm: {
82
// ConnMgr: {
83
// HighWater: 30,
84
// LowWater: 10
85
// },
86
// AddrFilters: []
87
// }
88
// }
89
90
this.ipfs = await IPFS.create(ipfsOptions)
91
this.statusLog('IPFS node created.')
93
// Set a 'low-power' profile for the IPFS node.
94
await this.ipfs.config.profiles.apply('lowpower')
95
96
// Generate a new wallet.
97
// this.wallet = new BchWallet()
98
// console.log("this.wallet: ", this.wallet);
99
100
if (!this.wallet) {
101
throw new Error('Wallet Not Found.! . Create or import a wallet')
102
}
103
// Wait for the wallet to initialize.
104
await this.wallet.walletInfoPromise
105
106
// Instantiate the IPFS Coordination library.
107
this.ipfsCoord = new IpfsCoord({
108
ipfs: this.ipfs,
109
type: 'browser',
110
statusLog: this.statusLog, // Status log
111
bchjs: this.wallet.bchjs,
112
mnemonic: this.wallet.walletInfo.mnemonic,
113
privateLog: this.privateLog,
114
announceJsonLd
116
this.statusLog('ipfs-coord library instantiated.')
117
118
// Wait for the coordination stuff to be setup.
119
await this.ipfsCoord.start()
121
const nodeConfig = await this.ipfs.config.getAll()
122
console.log(
123
`IPFS node configuration: ${JSON.stringify(nodeConfig, null, 2)}`
124
)
126
// subscribe to the 'chat' chatroom.
127
await this.ipfsCoord.adapters.pubsub.subscribeToPubsubChannel(
128
CHAT_ROOM_NAME,
129
this.handleChatLog,
130
this.ipfsCoord.thisNode
131
)
132
133
// Pass the IPFS instance to the window object. Makes it easy to debug IPFS
134
// issues in the browser console.
135
if (typeof window !== 'undefined') window.ipfs = this.ipfs
136
137
// Get this nodes IPFS ID
138
const id = await this.ipfs.id()
139
this.ipfsId = id.id
140
this.statusLog(`This IPFS node ID: ${this.ipfsId}`)
141
142
console.log('IPFS node setup complete.')
143
this.statusLog('IPFS node setup complete.')
144
_this.statusLog(' ')
145
} catch (err) {
146
console.error('Error in startIpfs(): ', err)
147
this.statusLog(
148
'Error trying to initialize IPFS node! Have you created a wallet?'
149
)
150
}
151
}
152
153
// This funciton handles incoming chat messages.
154
handleChatMsg (msg) {
155
try {
156
console.log('handleChatMsg msg: ', msg)
157
} catch (err) {
158
console.error('Error in handleChatMsg(): ', err)
159
}
160
}
161
162
getNodeInfo () {
163
return {
164
ipfsId: this.ipfsId,
165
announceJsonLd
166
}
167
}
168
}
169
170
// module.exports = AppIpfs
171
export default IpfsControl