Skip to content

Commit

Permalink
feat: trigger content claims equals on piece cid computed
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Sep 18, 2023
1 parent e7df15f commit 6c34000
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 13 deletions.
28 changes: 20 additions & 8 deletions filecoin/functions/piece-cid-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Sentry from '@sentry/serverless'
import { Config } from '@serverless-stack/node/config/index.js'
import { unmarshall } from '@aws-sdk/util-dynamodb'
import { Piece } from '@web3-storage/data-segment'
import { CID } from 'multiformats/cid'

import { reportPieceCid } from '../index.js'
import { getServiceConnection, getServiceSigner } from '../service.js'
Expand All @@ -28,27 +29,36 @@ async function pieceCidReport (event) {
// @ts-expect-error can't figure out type of new
const pieceRecord = unmarshall(records[0].new)
const piece = Piece.fromString(pieceRecord.piece).link
const content = CID.parse(pieceRecord.link)

const aggregateServiceConnection = getServiceConnection({
did: aggregatorDid,
url: aggregatorUrl
})
const claimsServiceConnection = getServiceConnection({
did: aggregatorDid,
url: aggregatorUrl
})
const issuer = getServiceSigner({
privateKey
})
const audience = aggregateServiceConnection.id
/** @type {import('@web3-storage/filecoin-client/types').InvocationConfig} */
const invocationConfig = {
issuer,
audience,
with: issuer.did(),
}

const { ok, error } = await reportPieceCid({
piece,
content,
group: issuer.did(),
aggregateServiceConnection,
invocationConfig
aggregateInvocationConfig: /** @type {import('@web3-storage/filecoin-client/types').InvocationConfig} */ ({
issuer,
audience: aggregateServiceConnection.id,
with: issuer.did(),
}),
claimsServiceConnection,
claimsInvocationConfig: /** @type {import('@web3-storage/filecoin-client/types').InvocationConfig} */ ({
issuer,
audience: claimsServiceConnection.id,
with: issuer.did(),
})
})

if (error) {
Expand All @@ -73,6 +83,8 @@ function getEnv() {
return {
aggregatorDid: mustGetEnv('AGGREGATOR_DID'),
aggregatorUrl: mustGetEnv('AGGREGATOR_URL'),
contentClaimsDid: mustGetEnv('CONTENT_CLAIMS_DID'),
contentClaimsUrl: mustGetEnv('CONTENT_CLAIMS_URL'),
}
}

Expand Down
35 changes: 31 additions & 4 deletions filecoin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as Hasher from 'fr32-sha2-256-trunc254-padded-binary-tree-multihash'
import * as Digest from 'multiformats/hashes/digest'
import { Piece } from '@web3-storage/data-segment'
import { CID } from 'multiformats/cid'
import { Assert } from '@web3-storage/content-claims/capability'
import { Aggregator } from '@web3-storage/filecoin-client'

import { GetCarFailed, ComputePieceFailed } from './errors.js'
Expand Down Expand Up @@ -83,19 +84,25 @@ export async function computePieceCid({
/**
* @param {object} props
* @param {import('@web3-storage/data-segment').PieceLink} props.piece
* @param {import('multiformats').CID} props.content
* @param {string} props.group
* @param {import('@web3-storage/filecoin-client/types').InvocationConfig} props.invocationConfig
* @param {import('@ucanto/principal/ed25519').ConnectionView<any>} props.aggregateServiceConnection
* @param {import('@web3-storage/filecoin-client/types').InvocationConfig} props.aggregateInvocationConfig
* @param {import('@ucanto/principal/ed25519').ConnectionView<any>} props.claimsServiceConnection
* @param {import('@web3-storage/filecoin-client/types').InvocationConfig} props.claimsInvocationConfig
*/
export async function reportPieceCid ({
piece,
content,
group,
invocationConfig,
aggregateServiceConnection
aggregateServiceConnection,
aggregateInvocationConfig,
claimsServiceConnection,
claimsInvocationConfig
}) {
// Add piece for aggregation
const aggregateQueue = await Aggregator.aggregateQueue(
invocationConfig,
aggregateInvocationConfig,
piece,
group,
{ connection: aggregateServiceConnection }
Expand All @@ -106,6 +113,26 @@ export async function reportPieceCid ({
error: aggregateQueue.out.error
}
}

// Add claim for reading
const claimResult = await Assert.equals

Check failure on line 118 in filecoin/index.js

View workflow job for this annotation

GitHub Actions / Test

Property 'equals' does not exist on type 'typeof import("/home/runner/work/w3infra/w3infra/node_modules/@web3-storage/content-claims/types/capability/assert")'.
.invoke({
issuer: claimsInvocationConfig.issuer,
audience: claimsInvocationConfig.audience,
with: claimsInvocationConfig.with,
nb: {
content,
equals: piece
}
})
.execute(claimsServiceConnection)

if (claimResult.out.error) {
return {
error: claimResult.out.error
}
}

return {
ok: {},
}
Expand Down
1 change: 1 addition & 0 deletions filecoin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@ucanto/client": "^8.0.1",
"@ucanto/principal": "^8.1.0",
"@ucanto/transport": "^8.0.0",
"@web3-storage/content-claims": "^3.0.1",
"@web3-storage/data-segment": "^3.0.1",
"@web3-storage/filecoin-client": "^1.3.0",
"fr32-sha2-256-trunc254-padded-binary-tree-multihash": "github:web3-storage/fr32-sha2-256-trunc254-padded-binary-tree-multihash",
Expand Down
98 changes: 98 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions stacks/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export function getEnv() {
UPLOAD_API_DID: mustGetEnv('UPLOAD_API_DID'),
AGGREGATOR_DID: mustGetEnv('AGGREGATOR_DID'),
AGGREGATOR_URL: mustGetEnv('AGGREGATOR_URL'),
CONTENT_CLAIMS_DID: mustGetEnv('CONTENT_CLAIMS_DID'),
CONTENT_CLAIMS_URL: mustGetEnv('CONTENT_CLAIMS_URL'),
}
}

Expand Down
4 changes: 3 additions & 1 deletion stacks/filecoin-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function FilecoinStack({ stack, app }) {
srcPath: 'filecoin'
})

const { AGGREGATOR_DID, AGGREGATOR_URL } = getEnv()
const { AGGREGATOR_DID, AGGREGATOR_URL, CONTENT_CLAIMS_DID, CONTENT_CLAIMS_URL } = getEnv()

// Setup app monitoring with Sentry
setupSentry(app, stack)
Expand All @@ -40,6 +40,8 @@ export function FilecoinStack({ stack, app }) {
environment: {
AGGREGATOR_DID,
AGGREGATOR_URL,
CONTENT_CLAIMS_DID,
CONTENT_CLAIMS_URL,
},
timeout: 3 * 60,
bind: [
Expand Down

0 comments on commit 6c34000

Please sign in to comment.