-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpiece.js
61 lines (55 loc) · 1.85 KB
/
piece.js
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
import { read } from '@web3-storage/content-claims/client'
import { PIECE_V1_CODE, PIECE_V1_MULTIHASH, PIECE_V2_MULTIHASH, RAW_CODE } from './constants.js'
/**
* @typedef {import('multiformats/cid').CID} CID
* @typedef {import('@web3-storage/content-claims/client/api').Claim} Claim
**/
/**
* Return the cid if it is a Piece CID or undefined if not
*
* @param {CID} cid
*/
export function asPieceCidV2 (cid) {
if (cid.multihash.code === PIECE_V2_MULTIHASH && cid.code === RAW_CODE) {
return cid
}
}
/**
* Return the cid if it is a v1 Piece CID or undefined if not
*
* @param {CID} cid
*/
export function asPieceCidV1 (cid) {
if (cid.multihash.code === PIECE_V1_MULTIHASH && cid.code === PIECE_V1_CODE) {
return cid
}
}
/**
* Find the set of CIDs that are claimed to be equivalent to the Piece CID.
*
* @param {CID} piece
* @param {(CID) => Promise<Claim[]>} [fetchClaims] - returns content claims for a cid
*/
export async function findEquivalentCids (piece, fetchClaims = createClaimsClientForEnv()) {
/** @type {Set<import('multiformats').UnknownLink>} */
const cids = new Set()
const claims = await fetchClaims(piece)
for (const claim of claims) {
// claims will include _all_ claims about this cid, so we filter to `equals`
if (claim.type !== 'assert/equals') {
continue
}
// an equivalence claim may have the pieceCid as the content cid _or_ the equals cid
// so if content does not equal piece, we can grab the content. Otherwise equals
const equivalentCid = claim.content.equals(piece) ? claim.equals : claim.content
cids.add(equivalentCid)
}
return cids
}
/** @param {'prod' | *} env */
export function createClaimsClientForEnv (env = process.env.SST_STAGE) {
if (env === 'prod') {
return read
}
return (cid, opts) => read(cid, { serviceURL: 'https://staging.claims.web3.storage', ...opts })
}