-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathutils.ts
86 lines (75 loc) · 3.11 KB
/
utils.ts
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
81
82
83
84
85
86
import { generateKeyPair } from '@libp2p/crypto/keys'
import { createVerifiedFetch, VerifiedFetch } from '@helia/verified-fetch'
import { createDelegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
import { createHelia } from 'helia'
import * as libp2pInfo from 'libp2p/version'
import { httpGatewayRouting, libp2pRouting } from '@helia/routers'
import { enable } from '@libp2p/logger'
import { createLibp2p, Libp2pOptions } from 'libp2p'
import { webTransport } from '@libp2p/webtransport'
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { dcutr } from '@libp2p/dcutr'
import { identify, identifyPush } from '@libp2p/identify'
import { keychain } from '@libp2p/keychain'
import { ping } from '@libp2p/ping'
import { webRTCDirect } from '@libp2p/webrtc'
import { webSockets } from '@libp2p/websockets'
// Singleton verifiedFetch instances
let verifiedFetch: VerifiedFetch | undefined
let verifiedFetchP2P: VerifiedFetch | undefined
interface VerifiedFetchOptions {
useLibp2p?: boolean
useRecursiveGateways?: boolean
}
export async function getVerifiedFetch(
options: VerifiedFetchOptions = { useLibp2p: false, useRecursiveGateways: true },
): Promise<VerifiedFetch> {
if (!options.useLibp2p) {
verifiedFetch = verifiedFetch ?? (await createVerifiedFetch())
return verifiedFetch
}
enable('*')
verifiedFetchP2P = verifiedFetchP2P ?? (await createVerifiedFetchP2P(options))
return verifiedFetchP2P
}
async function createVerifiedFetchP2P(options: VerifiedFetchOptions): Promise<VerifiedFetch> {
const libp2pOptions = await libp2pDefaults()
const libp2p = await createLibp2p(libp2pOptions)
const helia = await createHelia({
libp2p,
// TODO: Do we want `libp2pRouting` or `delegatedHTTPRouting` routers since libp2p only has a delegated routing client?
// For now sticking to `libp2pRouting` since it allows setting the filters (until https://github.com/ipfs/helia/pull/654 is merged)
routers: options.useRecursiveGateways
? [httpGatewayRouting(), libp2pRouting(libp2p)]
: [libp2pRouting(libp2p)],
})
return createVerifiedFetch(helia)
}
export async function libp2pDefaults(): Promise<Libp2pOptions> {
const agentVersion = `@helia/verified-fetch ${libp2pInfo.name}/${libp2pInfo.version} UserAgent=${globalThis.navigator.userAgent}`
const privateKey = await generateKeyPair('Ed25519')
return {
privateKey: privateKey,
addresses: {}, // no need to listen on any addresses
transports: [webRTCDirect(), webTransport(), webSockets()],
connectionEncrypters: [noise()],
streamMuxers: [yamux()],
services: {
delegatedRouting: () =>
createDelegatedRoutingV1HttpApiClient('https://delegated-ipfs.dev', {
filterProtocols: ['unknown', 'transport-bitswap', 'transport-ipfs-gateway-http'],
filterAddrs: ['https', 'webtransport', 'webrtc', 'webrtc-direct', 'wss'],
}),
dcutr: dcutr(),
identify: identify({
agentVersion,
}),
identifyPush: identifyPush({
agentVersion,
}),
keychain: keychain(),
ping: ping(),
},
}
}