Permalink
Newer
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
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
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...')
79
// const ipfsOptions = {
80
// Bootstrap: [],
81
// Swarm: {
82
// ConnMgr: {
83
// HighWater: 30,
84
// LowWater: 10
85
// },
86
// AddrFilters: []
87
// }
88
// }
89
93
// Set a 'low-power' profile for the IPFS node.
94
await this.ipfs.config.profiles.apply('lowpower')
95
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',
122
console.log(
123
`IPFS node configuration: ${JSON.stringify(nodeConfig, null, 2)}`
124
)
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
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 {
161
162
getNodeInfo () {
163
return {
164
ipfsId: this.ipfsId,
165
announceJsonLd
166
}
167
}