-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: trigger aggregator on computed piece
- Loading branch information
1 parent
2429663
commit 39fdd12
Showing
14 changed files
with
324 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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 { reportPieceCid } from '../index.js' | ||
import { getServiceConnection, getServiceSigner } from '../service.js' | ||
import { mustGetEnv } from './utils.js' | ||
|
||
Sentry.AWSLambda.init({ | ||
environment: process.env.SST_STAGE, | ||
dsn: process.env.SENTRY_DSN, | ||
tracesSampleRate: 1.0, | ||
}) | ||
|
||
/** | ||
* @param {import('aws-lambda').DynamoDBStreamEvent} event | ||
*/ | ||
async function pieceCidReport (event) { | ||
const { aggregatorDid, aggregatorUrl } = getEnv() | ||
const { PRIVATE_KEY: privateKey } = Config | ||
|
||
const records = parseDynamoDbEvent(event) | ||
if (records.length > 1) { | ||
throw new Error('Should only receive one ferry to update') | ||
} | ||
|
||
// @ts-expect-error can't figure out type of new | ||
const pieceRecord = unmarshall(records[0].new) | ||
const piece = Piece.fromString(pieceRecord.piece).link | ||
|
||
const aggregateServiceConnection = 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, | ||
group: issuer.did(), | ||
aggregateServiceConnection, | ||
invocationConfig | ||
}) | ||
|
||
if (error) { | ||
return { | ||
statusCode: 500, | ||
body: error.message || 'failed to add aggregate' | ||
} | ||
} | ||
|
||
return { | ||
statusCode: 200, | ||
body: ok | ||
} | ||
} | ||
|
||
export const handler = Sentry.AWSLambda.wrapHandler(pieceCidReport) | ||
|
||
/** | ||
* Get Env validating it is set. | ||
*/ | ||
function getEnv() { | ||
return { | ||
aggregatorDid: mustGetEnv('AGGREGATOR_DID'), | ||
aggregatorUrl: mustGetEnv('AGGREGATOR_URL'), | ||
} | ||
} | ||
|
||
/** | ||
* @param {import('aws-lambda').DynamoDBStreamEvent} event | ||
*/ | ||
function parseDynamoDbEvent (event) { | ||
return event.Records.map(r => ({ | ||
new: r.dynamodb?.NewImage, | ||
old: r.dynamodb?.OldImage | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @param {string} name | ||
* @returns {string} | ||
*/ | ||
export function mustGetEnv (name) { | ||
const value = process.env[name] | ||
if (!value) throw new Error(`Missing env var: ${name}`) | ||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as ed25519 from '@ucanto/principal/ed25519' | ||
import * as DID from '@ipld/dag-ucan/did' | ||
import { CAR, HTTP } from '@ucanto/transport' | ||
import { connect } from '@ucanto/client' | ||
|
||
/** | ||
* Given a config, return a ucanto Signer object representing the service | ||
* | ||
* @param {object} config | ||
* @param {string} config.privateKey - multiformats private key of primary signing key | ||
* @returns {import('@ucanto/principal/ed25519').Signer.Signer} | ||
*/ | ||
export function getServiceSigner(config) { | ||
return ed25519.parse(config.privateKey) | ||
} | ||
|
||
/** | ||
* | ||
* @param {{ did: string, url: string }} config | ||
* @returns | ||
*/ | ||
export function getServiceConnection (config) { | ||
const servicePrincipal = DID.parse(config.did) // 'did:web:filecoin.web3.storage' | ||
const serviceURL = new URL(config.url) // 'https://filecoin.web3.storage' | ||
|
||
const serviceConnection = connect({ | ||
id: servicePrincipal, | ||
codec: CAR.outbound, | ||
channel: HTTP.open({ | ||
url: serviceURL, | ||
method: 'POST', | ||
}), | ||
}) | ||
|
||
return serviceConnection | ||
} |
Oops, something went wrong.