-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathcontenthash.ts
More file actions
87 lines (78 loc) · 2.19 KB
/
contenthash.ts
File metadata and controls
87 lines (78 loc) · 2.19 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
81
82
83
84
85
86
87
import { DecodedContentHash } from '@ensdomains/ensjs/utils'
export type ContentHashProtocol =
| 'ipfs'
| 'ipns'
| 'bzz'
| 'onion'
| 'onion3'
| 'sia'
| 'arweave'
| 'ar'
| 'walrus'
export type ContentHashProvider = 'ipfs' | 'swarm' | 'onion' | 'skynet' | 'arweave' | 'walrus'
type GetContentHashLinkParameters = {
name: string
chainId: number
decodedContentHash: DecodedContentHash
}
export const getContentHashLink = ({
name,
chainId,
decodedContentHash,
}: GetContentHashLinkParameters) => {
const protocol = decodedContentHash.protocolType
const hash = decodedContentHash.decoded
const useEthLink =
name.endsWith('.eth') && chainId === 1 && (protocol === 'ipfs' || protocol === 'ipns')
if (useEthLink) {
return `https://${name}.limo`
}
if (protocol === 'ipfs') {
return `https://${hash}.ipfs.cf-ipfs.com` // using ipfs's secured origin gateway
}
if (protocol === 'ipns') {
return `https://ipfs.euc.li/ipns/${hash}`
}
if (protocol === 'bzz') {
return `https://gateway.ethswarm.org/bzz/${hash}`
}
if (protocol === 'onion' || protocol === 'onion3') {
return `http://${hash}.onion`
}
if (protocol === 'sia') {
return `https://siasky.net/${hash}`
}
if (protocol === 'ar') {
return `https://arweave.net/${hash}`
}
if (protocol === 'walrus') {
return `https://aggregator.walrus-testnet.walrus.space/v1/blobs/${hash}`
}
return null
}
export const contentHashToString = (
decodedContentHash: DecodedContentHash | string | null | undefined,
): string => {
if (typeof decodedContentHash === 'string') return decodedContentHash
if (
decodedContentHash &&
typeof decodedContentHash === 'object' &&
decodedContentHash?.decoded &&
decodedContentHash?.protocolType
)
return `${decodedContentHash.protocolType}://${decodedContentHash.decoded}`
return ''
}
const contentHashProtocolToProviderMap = {
ipfs: 'ipfs',
ipns: 'ipfs',
bzz: 'swarm',
onion: 'onion',
onion3: 'onion',
sia: 'skynet',
arweave: 'arweave',
ar: 'arweave',
walrus: 'walrus',
} as const
export const getContentHashProvider = (protocol: ContentHashProtocol): ContentHashProvider =>
contentHashProtocolToProviderMap[protocol]