Skip to content

Commit

Permalink
feat: w3filecoin new client and api
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Aug 2, 2023
1 parent 31730f0 commit f0a1217
Show file tree
Hide file tree
Showing 31 changed files with 1,771 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"bootstrap-sha": "c918ffc59eafa01fbc63d5df11ba621cc1888c64",
"packages": {
"packages/access-client": {},
"packages/aggregate-api": {},
"packages/aggregate-client": {},
"packages/filecoin-api": {},
"packages/filecoin-client": {},
"packages/capabilities": {},
"packages/did-mailto": {},
"packages/upload-api": {},
Expand Down
4 changes: 2 additions & 2 deletions .github/release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"packages/access-client": "15.0.0",
"packages/aggregate-api": "1.0.0",
"packages/aggregate-client": "1.0.0",
"packages/filecoin-api": "1.0.0",
"packages/filecoin-client": "1.0.0",
"packages/capabilities": "7.0.0",
"packages/upload-api": "4.1.0",
"packages/upload-client": "9.1.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Aggregate API
name: Filecoin API
env:
CI: true
FORCE_COLOR: 1
Expand All @@ -8,14 +8,14 @@ on:
branches:
- main
paths:
- 'packages/aggregate-api/**'
- '.github/workflows/aggregate-api.yml'
- 'packages/filecoin-api/**'
- '.github/workflows/filecoin-api.yml'
- 'pnpm-lock.yaml'
- '.env.tpl'
pull_request:
paths:
- 'packages/aggregate-api/**'
- '.github/workflows/aggregate-api.yml'
- 'packages/filecoin-api/**'
- '.github/workflows/filecoin-api.yml'
- 'pnpm-lock.yaml'
- '.env.tpl'
jobs:
Expand Down Expand Up @@ -44,6 +44,6 @@ jobs:
pnpm run --if-present build
- name: Lint
run: pnpm -r --filter @web3-storage/aggregate-api run lint
run: pnpm -r --filter @web3-storage/filecoin-api run lint
- name: Test
run: pnpm -r --filter @web3-storage/aggregate-api run test
run: pnpm -r --filter @web3-storage/filecoin-api run test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Aggregate Client
name: Filecoin Client
env:
CI: true
FORCE_COLOR: 1
Expand All @@ -7,13 +7,13 @@ on:
branches:
- main
paths:
- 'packages/aggregate-client/**'
- '.github/workflows/aggregate-client.yml'
- 'packages/filecoin-client/**'
- '.github/workflows/filecoin-client.yml'
- 'pnpm-lock.yaml'
pull_request:
paths:
- 'packages/aggregate-client/**'
- '.github/workflows/aggregate-client.yml'
- 'packages/filecoin-client/**'
- '.github/workflows/filecoin-client.yml'
- 'pnpm-lock.yaml'
jobs:
test:
Expand All @@ -34,5 +34,5 @@ jobs:
cache: 'pnpm'
- run: pnpm install
- run: pnpm run build
- run: pnpm -r --filter @web3-storage/aggregate-client run lint
- run: pnpm -r --filter @web3-storage/aggregate-client run test
- run: pnpm -r --filter @web3-storage/filecoin-client run lint
- run: pnpm -r --filter @web3-storage/filecoin-client run test
16 changes: 11 additions & 5 deletions packages/aggregate-api/src/aggregate/offer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as Server from '@ucanto/server'
import { CBOR } from '@ucanto/core'
import { Node, Piece, Aggregate as AggregateBuilder } from '@web3-storage/data-segment'
import {
Node,
Piece,
Aggregate as AggregateBuilder,
} from '@web3-storage/data-segment'
import * as Aggregate from '@web3-storage/capabilities/aggregate'
import * as Offer from '@web3-storage/capabilities/offer'
import * as API from '../types.js'
Expand Down Expand Up @@ -62,10 +66,12 @@ export const claim = async (
// Validate commP of commPs
const aggregateBuild = AggregateBuilder.build({
size: aggregateSize,
pieces: offers.map(offer => Piece.fromJSON({
height: offer.height,
link: { '/': offer.link.toString() }
}))
pieces: offers.map((offer) =>
Piece.fromJSON({
height: offer.height,
link: { '/': offer.link.toString() },
})
),
})
if (!aggregateBuild.link.equals(piece.link)) {
return {
Expand Down
187 changes: 187 additions & 0 deletions packages/capabilities/src/filecoin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/**
* Filecoin Capabilities
*
* These can be imported directly with:
* ```js
* import * as Filecoin from '@web3-storage/capabilities/filecoin'
* ```
*
* @module
*/

import { capability, Schema, ok } from '@ucanto/validator'
import { equal, equalWith, checkLink, and } from './utils.js'

/**
* @see https://github.com/multiformats/go-multihash/blob/dc3bd6897fcd17f6acd8d4d6ffd2cea3d4d3ebeb/multihash.go#L73
*/
const SHA2_256_TRUNC254_PADDED = 0x1012
/**
* @see https://github.com/ipfs/go-cid/blob/829c826f6be23320846f4b7318aee4d17bf8e094/cid.go#L104
*/
const FilCommitmentUnsealed = 0xf101

/**
* `filecoin/add` capability allows agent to add a filecoin piece to be aggregated
* so that it can be stored by a Storage provider on a future time.
*/
export const filecoinAdd = capability({
can: 'filecoin/add',
/**
* did:key identifier of the broker authority where offer is made available.
*/
with: Schema.did(),
nb: Schema.struct({
/**
* CID of the content that resulted in Filecoin piece.
*/
content: Schema.link(),
/**
* CID of the piece.
*/
piece: /** @type {import('./types').PieceLinkSchema} */ (
Schema.link({
code: FilCommitmentUnsealed,
version: 1,
multihash: {
code: SHA2_256_TRUNC254_PADDED,
},
})
),
}),
derives: (claim, from) => {
return (
and(equalWith(claim, from)) ||
and(checkLink(claim.nb.content, from.nb.content, 'nb.content')) ||
and(checkLink(claim.nb.piece, from.nb.piece, 'nb.piece')) ||
ok({})
)
},
})

/**
* `piece/add` capability allows agent to add a piece piece to be aggregated
* so that it can be stored by a Storage provider on a future time.
*/
export const pieceAdd = capability({
can: 'piece/add',
/**
* did:key identifier of the broker authority where offer is made available.
*/
with: Schema.did(),
nb: Schema.struct({
/**
* CID of the piece.
*/
piece: /** @type {import('./types').PieceLinkSchema} */ (
Schema.link({
code: FilCommitmentUnsealed,
version: 1,
multihash: {
code: SHA2_256_TRUNC254_PADDED,
},
})
),
/**
* CID of the content that resulted in Filecoin piece.
*/
group: Schema.text(),
}),
derives: (claim, from) => {
return (
and(equalWith(claim, from)) ||
and(checkLink(claim.nb.piece, from.nb.piece, 'nb.piece')) ||
and(equal(claim.nb.group, from.nb.group, 'nb.group')) ||
ok({})
)
},
})

/**
* `aggregate/add` capability allows agent to create an offer to get an aggregate
* of CARs files in the market to be fetched and stored by a Storage provider.
*/
export const aggregateAdd = capability({
can: 'aggregate/add',
/**
* did:key identifier of the broker authority where offer is made available.
*/
with: Schema.did(),
nb: Schema.struct({
/**
* CID of the DAG-CBOR encoded block with offer details.
* Service will queue given offer to be validated and handled.
*/
offer: Schema.link(),
/**
* Commitment proof for the aggregate being offered.
* https://github.com/filecoin-project/go-state-types/blob/1e6cf0d47cdda75383ef036fc2725d1cf51dbde8/abi/piece.go#L47-L50
*/
piece: /** @type {import('./types').PieceLinkSchema} */ (
Schema.link({
code: FilCommitmentUnsealed,
version: 1,
multihash: {
code: SHA2_256_TRUNC254_PADDED,
},
})
),
/**
* Necessary fields for a Filecoin Deal proposal.
*/
deal: Schema.struct({
/**
* with tenantId broker can select one of their configured wallets
*/
tenantId: Schema.text(),
/**
* arbitrary label to be added to the deal on chain
*/
label: Schema.text().optional(),
}),
}),
derives: (claim, from) => {
return (
and(equalWith(claim, from)) ||
and(checkLink(claim.nb.offer, from.nb.offer, 'nb.offer')) ||
and(checkLink(claim.nb.piece, from.nb.piece, 'nb.piece')) ||
and(
equal(claim.nb.deal.tenantId, from.nb.deal.tenantId, 'nb.deal.tenantId')
) ||
and(equal(claim.nb.deal.label, from.nb.deal.label, 'nb.deal.label')) ||
ok({})
)
},
})

/**
* `chain/info` capability allows agent to get chain info of a given piece.
*/
export const chainInfo = capability({
can: 'chain/info',
/**
* did:key identifier of the broker authority where offer is made available.
*/
with: Schema.did(),
nb: Schema.struct({
/**
* CID of the piece.
*/
piece: /** @type {import('./types').PieceLinkSchema} */ (
Schema.link({
code: FilCommitmentUnsealed,
version: 1,
multihash: {
code: SHA2_256_TRUNC254_PADDED,
},
})
),
}),
derives: (claim, from) => {
return (
and(equalWith(claim, from)) ||
and(checkLink(claim.nb.piece, from.nb.piece, 'nb.piece')) ||
ok({})
)
},
})
13 changes: 6 additions & 7 deletions packages/capabilities/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import * as Utils from './utils.js'
import * as Consumer from './consumer.js'
import * as Customer from './customer.js'
import * as Console from './console.js'
import * as Offer from './offer.js'
import * as Aggregate from './aggregate.js'
import * as Filecoin from './filecoin.js'

export {
Access,
Expand All @@ -24,8 +23,7 @@ export {
Customer,
Console,
Utils,
Aggregate,
Offer,
Filecoin,
}

/** @type {import('./types.js').AbilitiesArray} */
Expand All @@ -49,7 +47,8 @@ export const abilitiesAsStrings = [
Access.access.can,
Access.authorize.can,
Access.session.can,
Aggregate.offer.can,
Aggregate.get.can,
Offer.arrange.can,
Filecoin.filecoinAdd.can,
Filecoin.pieceAdd.can,
Filecoin.aggregateAdd.can,
Filecoin.chainInfo.can,
]
Loading

0 comments on commit f0a1217

Please sign in to comment.