forked from storacha/w3up
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: blob, web3.storage and ucan conclude capabilities together with…
… api handlers (storacha#1342) Adds implementation of `blob/*`, `web3.storage/*` and `ucan/conclude` handlers and capabilities.
- Loading branch information
1 parent
232dadd
commit 00735a8
Showing
46 changed files
with
3,903 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Blob Capabilities. | ||
* | ||
* Blob is a fixed size byte array addressed by the multihash. | ||
* Usually blobs are used to represent set of IPLD blocks at different byte ranges. | ||
* | ||
* These can be imported directly with: | ||
* ```js | ||
* import * as Blob from '@web3-storage/capabilities/blob' | ||
* ``` | ||
* | ||
* @module | ||
*/ | ||
import { capability, Schema } from '@ucanto/validator' | ||
import { equalBlob, equalWith, SpaceDID } from './utils.js' | ||
|
||
/** | ||
* Agent capabilities for Blob protocol | ||
*/ | ||
|
||
/** | ||
* Capability can only be delegated (but not invoked) allowing audience to | ||
* derived any `blob/` prefixed capability for the (memory) space identified | ||
* by DID in the `with` field. | ||
*/ | ||
export const blob = capability({ | ||
can: 'blob/*', | ||
/** | ||
* DID of the (memory) space where Blob is intended to | ||
* be stored. | ||
*/ | ||
with: SpaceDID, | ||
derives: equalWith, | ||
}) | ||
|
||
/** | ||
* Blob description for being ingested by the service. | ||
*/ | ||
export const content = Schema.struct({ | ||
/** | ||
* A multihash digest of the blob payload bytes, uniquely identifying blob. | ||
*/ | ||
digest: Schema.bytes(), | ||
/** | ||
* Number of bytes contained by this blob. Service will provision write target | ||
* for this exact size. Attempt to write a larger Blob file will fail. | ||
*/ | ||
size: Schema.integer(), | ||
}) | ||
|
||
/** | ||
* `blob/add` capability allows agent to store a Blob into a (memory) space | ||
* identified by did:key in the `with` field. Agent should compute blob multihash | ||
* and size and provide it under `nb.blob` field, allowing a service to provision | ||
* a write location for the agent to PUT desired Blob into. | ||
*/ | ||
export const add = capability({ | ||
can: 'blob/add', | ||
/** | ||
* DID of the (memory) space where Blob is intended to | ||
* be stored. | ||
*/ | ||
with: SpaceDID, | ||
nb: Schema.struct({ | ||
/** | ||
* Blob to be added on the space. | ||
*/ | ||
blob: content, | ||
}), | ||
derives: equalBlob, | ||
}) | ||
|
||
// ⚠️ We export imports here so they are not omitted in generated typedefs | ||
// @see https://github.com/microsoft/TypeScript/issues/51548 | ||
export { Schema } |
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,49 @@ | ||
/** | ||
* HTTP Capabilities. | ||
* | ||
* These can be imported directly with: | ||
* ```js | ||
* import * as HTTP from '@web3-storage/capabilities/http' | ||
* ``` | ||
* | ||
* @module | ||
*/ | ||
import { capability, Schema, ok } from '@ucanto/validator' | ||
import { content } from './blob.js' | ||
import { equal, equalBody, equalWith, SpaceDID, Await, and } from './utils.js' | ||
|
||
/** | ||
* `http/put` capability invocation MAY be performed by any authorized agent on behalf of the subject | ||
* as long as they have referenced `body` content to do so. | ||
*/ | ||
export const put = capability({ | ||
can: 'http/put', | ||
/** | ||
* DID of the (memory) space where Blob is intended to | ||
* be stored. | ||
*/ | ||
with: SpaceDID, | ||
nb: Schema.struct({ | ||
/** | ||
* Description of body to send (digest/size). | ||
*/ | ||
body: content, | ||
/** | ||
* HTTP(S) location that can receive blob content via HTTP PUT request. | ||
*/ | ||
url: Schema.string().or(Await), | ||
/** | ||
* HTTP headers. | ||
*/ | ||
headers: Schema.dictionary({ value: Schema.string() }).or(Await), | ||
}), | ||
derives: (claim, from) => { | ||
return ( | ||
and(equalWith(claim, from)) || | ||
and(equalBody(claim, from)) || | ||
and(equal(claim.nb.url, from.nb, 'url')) || | ||
and(equal(claim.nb.headers, from.nb, 'headers')) || | ||
ok({}) | ||
) | ||
}, | ||
}) |
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
Oops, something went wrong.