Skip to content

Commit

Permalink
Merge pull request #10 from web3-storage/feat/get_caps_from_protocol_…
Browse files Browse the repository at this point in the history
…repo

Feat/get caps from protocol repo
  • Loading branch information
the-simian committed Sep 14, 2022
2 parents e26a13a + 54a32f9 commit 85ef53d
Show file tree
Hide file tree
Showing 8 changed files with 771 additions and 203 deletions.
13 changes: 0 additions & 13 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
* [.list()](#Client+list)[<code>Promise.&lt;Result&gt;</code>](#Result)
* [.upload(bytes)](#Client+upload)[<code>Promise.&lt;strResult&gt;</code>](#strResult)
* [.remove(link)](#Client+remove)
* [.linkroot(root, links)](#Client+linkroot)
* [.insights(link)](#Client+insights) ⇒ <code>Promise.&lt;object&gt;</code>

<a name="new_Client_new"></a>
Expand Down Expand Up @@ -120,18 +119,6 @@ Remove an uploaded file by CID
| --- | --- | --- |
| link | <code>API.Link</code> | the CID to remove |

<a name="Client+linkroot"></a>

### client.linkroot(root, links)
Remove an uploaded file by CID

**Kind**: instance method of [<code>Client</code>](#Client)

| Param | Type | Description |
| --- | --- | --- |
| root | [<code>Link</code>](#Link) | the CID to link as root. |
| links | [<code>Array.&lt;Link&gt;</code>](#Link) | the CIDs to link as 'children' |

<a name="Client+insights"></a>

### client.insights(link) ⇒ <code>Promise.&lt;object&gt;</code>
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "w3up-client",
"version": "0.2.0",
"version": "0.3.0",
"description": "",
"license": "Apache-2.0 OR MIT",
"type": "module",
Expand All @@ -27,7 +27,8 @@
"@ucanto/transport": "^0.7.0",
"@ucanto/validator": "^0.6.0",
"cross-fetch": "^3.1.5",
"tweetnacl": "^1.0.3"
"tweetnacl": "^1.0.3",
"@web3-storage/access": "^0.1.2-rc.2"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^3.3.0",
Expand Down
127 changes: 70 additions & 57 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { SigningAuthority } from '@ucanto/authority'
import { Delegation, UCAN } from '@ucanto/core'
import * as API from '@ucanto/interface'
import { Failure } from '@ucanto/validator'
// @ts-ignore
import * as capabilities from '@web3-storage/access/capabilities'
import fetch from 'cross-fetch'

import * as CAR from '../patches/@ucanto/transport/car'
Expand Down Expand Up @@ -134,14 +136,16 @@ class Client {
throw new Error(`Invalid email provided for registration: ${email}`)
}
const issuer = await this.identity()
await Access.Validate.invoke({
issuer,
audience: this.accessClient.id,
with: issuer.did(),
caveats: {
as: `mailto:${email}`,
},
}).execute(this.accessClient)
const result = await capabilities.identityValidate
.invoke({
issuer,
audience: this.accessClient.id,
with: issuer.did(),
caveats: {
as: `mailto:${email}`,
},
})
.execute(this.accessClient)

const proofString = await this.checkRegistration()
const ucan = UCAN.parse(proofString)
Expand All @@ -151,18 +155,19 @@ class Client {
// TODO: this should be better.
// Use access API/client to do all of this.
const first = proof.capabilities[0]

const validate = await Access.Register.invoke({
issuer,
audience: this.accessClient.id,
// @ts-ignore
with: first.with,
caveats: {
const validate = await capabilities.identityRegister
.invoke({
issuer,
audience: this.accessClient.id,
// @ts-ignore
as: first.as,
},
proofs: [proof],
}).execute(this.accessClient)
with: first.with,
caveats: {
// @ts-ignore
as: first.as,
},
proofs: [proof],
})
.execute(this.accessClient)

if (validate?.error) {
// @ts-ignore
Expand Down Expand Up @@ -214,11 +219,13 @@ class Client {
*/
async whoami() {
const issuer = await this.identity()
return await Access.Identify.invoke({
issuer,
audience: this.accessClient.id,
with: issuer.did(),
}).execute(this.accessClient)
return await capabilities.identityIdentify
.invoke({
issuer,
audience: this.accessClient.id,
with: issuer.did(),
})
.execute(this.accessClient)
}

/**
Expand All @@ -228,11 +235,13 @@ class Client {
*/
async list() {
const id = await this.identity()
return Store.List.invoke({
issuer: id,
audience: this.storeClient.id,
with: id.did(),
}).execute(this.storeClient)
return capabilities.storeList
.invoke({
issuer: id,
audience: this.storeClient.id,
with: id.did(),
})
.execute(this.storeClient)
}

/**
Expand All @@ -245,14 +254,16 @@ class Client {
try {
const id = await this.identity()
const link = await CAR.codec.link(bytes)
const result = await Store.Add.invoke({
issuer: id,
audience: this.storeClient.id,
with: id.did(),
caveats: {
link,
},
}).execute(this.storeClient)
const result = await capabilities.storeAdd
.invoke({
issuer: id,
audience: this.storeClient.id,
with: id.did(),
caveats: {
link,
},
})
.execute(this.storeClient)

if (result?.error !== undefined) {
throw new Error(JSON.stringify(result))
Expand Down Expand Up @@ -293,33 +304,35 @@ class Client {
*/
async remove(link) {
const id = await this.identity()
return await Store.Remove.invoke({
issuer: id,
audience: this.storeClient.id,
with: id.did(),
caveats: {
link,
},
}).execute(this.storeClient)
return await capabilities.storeRemove
.invoke({
issuer: id,
audience: this.storeClient.id,
with: id.did(),
caveats: {
link,
},
})
.execute(this.storeClient)
}

/**
* Remove an uploaded file by CID
* @param {Link} root - the CID to link as root.
* @param {Array<Link>} links - the CIDs to link as 'children'
*/
async linkroot(root, links) {
const id = await this.identity()
return await Store.LinkRoot.invoke({
issuer: id,
audience: this.storeClient.id,
with: id.did(),
caveats: {
rootLink: root,
links,
},
}).execute(this.storeClient)
}
// async linkroot(root, links) {
// const id = await this.identity()
// return await Store.LinkRoot.invoke({
// issuer: id,
// audience: this.storeClient.id,
// with: id.did(),
// caveats: {
// rootLink: root,
// links,
// },
// }).execute(this.storeClient)
// }

/**
* @async
Expand Down
54 changes: 0 additions & 54 deletions src/store/access/capability.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/store/access/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import webfetch from 'cross-fetch'

import * as CAR from '../../../patches/@ucanto/transport/car'

export * from './capability.js'

/**
* @param {object} options
* @param {API.DID} options.id
Expand Down
71 changes: 0 additions & 71 deletions src/store/store/capability.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/store/store/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import webfetch from 'cross-fetch'

import * as CAR from '../../../patches/@ucanto/transport/car'

export * from './capability.js'

/**
* @param {object} options
* @param {API.DID} options.id
Expand Down
Loading

0 comments on commit 85ef53d

Please sign in to comment.