Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/#153 #174

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/release-please-config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"separate-pull-requests": true,
"packages": {
"packages/access": {},
"packages/access-client": {},
"packages/access-api": {},
"packages/upload-client": {},
"packages/wallet": {},
Expand Down
2 changes: 1 addition & 1 deletion .github/release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"packages/wallet": "1.0.0",
"packages/access": "4.0.2",
"packages/access-client": "4.0.2",
"packages/access-api": "3.0.0",
"packages/store": "2.0.0"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Access SDK
name: Access Client
env:
CI: true
FORCE_COLOR: 1
Expand All @@ -7,12 +7,12 @@ on:
branches:
- main
paths:
- 'packages/access/**'
- 'packages/access-client/**'
- '.github/workflows/access.yml'
- 'pnpm-lock.yaml'
pull_request:
paths:
- 'packages/access/**'
- 'packages/access-client/**'
- '.github/workflows/access.yml'
- 'pnpm-lock.yaml'
jobs:
Expand Down
32 changes: 29 additions & 3 deletions .github/workflows/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ on:
default: 'access-api'
options:
- access-api
- access
- upload-client
- access-client
- wallet
- docs
environment:
description: 'Environment to deploy'
required: true
Expand Down Expand Up @@ -52,9 +53,9 @@ jobs:
with:
environment: ${{ github.event.inputs.environment }}
secrets: inherit
deploy-access:
deploy-access-client:
runs-on: ubuntu-latest
if: github.event.inputs.package == 'access'
if: github.event.inputs.package == 'access-client'
steps:
- uses: actions/checkout@v3
- uses: pnpm/[email protected]
Expand Down Expand Up @@ -96,3 +97,28 @@ jobs:
- run: pnpm -r --filter @web3-storage/upload-client publish --tag next --access public
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
deploy-docs:
runs-on: ubuntu-latest
if: github.event.inputs.package == 'docs'
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/checkout@v3
- uses: pnpm/[email protected]
with:
version: 7
- uses: actions/setup-node@v3
with:
node-version: 18
registry-url: 'https://registry.npmjs.org'
cache: 'pnpm'
- run: pnpm install
- run: pnpm run docs
- uses: actions/configure-pages@v2
- uses: actions/upload-pages-artifact@v1
with:
path: './docs'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
release-type: node
publish-access:
needs: release
if: contains(fromJson(needs.release.outputs.paths_released), 'packages/access')
if: contains(fromJson(needs.release.outputs.paths_released), 'packages/access-client')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ucan-protocol",
"name": "w3-protocol",
"version": "0.0.0",
"private": true,
"workspaces": [
Expand All @@ -11,14 +11,15 @@
"clean": "rm -rf docs node_modules pnpm-lock.yaml packages/*/{pnpm-lock.yaml,coverage,.nyc_output,dist,node_modules,.next}",
"lint": "pnpm -r --if-present run lint",
"test": "pnpm -r --if-present run test",
"check": "pnpm -r --if-present run check"
"check": "pnpm -r --if-present run check",
"docs": "typedoc --out docs"
},
"devDependencies": {
"lint-staged": "^13.0.3",
"prettier": "2.7.1",
"simple-git-hooks": "^2.8.1",
"typescript": "^4.8.4",
"wrangler": "^2.1.13"
"typescript": "4.8.4",
"wrangler": "^2.3.2"
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
Expand All @@ -35,5 +36,9 @@
},
"engines": {
"node": ">=16"
},
"dependencies": {
"typedoc": "^0.23.21",
"typedoc-plugin-missing-exports": "^1.0.0"
}
}
41 changes: 20 additions & 21 deletions packages/access-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@
"dev": "scripts/cli.js dev",
"build": "scripts/cli.js build",
"check": "tsc --build",
"test": "tsc --build && ava --timeout 10s"
"test": "pnpm build && tsc --build && ava --timeout 10s"
},
"author": "Hugo Dias <[email protected]> (hugodias.me)",
"license": "(Apache-2.0 OR MIT)",
"dependencies": {
"@ipld/dag-ucan": "^2.0.1",
"@ucanto/client": "^3.0.1",
"@ucanto/core": "^3.0.1",
"@ucanto/interface": "^3.0.0",
"@ucanto/principal": "^3.0.0",
"@ucanto/server": "^3.0.1",
"@ucanto/transport": "^3.0.1",
"@ucanto/validator": "^3.0.1",
"@ucanto/client": "^3.0.2",
"@ucanto/core": "^3.0.2",
"@ucanto/interface": "^3.0.1",
"@ucanto/principal": "^3.0.1",
"@ucanto/server": "^3.0.4",
"@ucanto/transport": "^3.0.2",
"@ucanto/validator": "^3.0.4",
"@web3-storage/access": "workspace:^",
"@web3-storage/worker-utils": "0.4.3-dev",
"multiformats": "^10.0.2",
"nanoid": "^4.0.0",
"p-retry": "^5.1.1",
"preact": "^10.11.2",
"preact": "^10.11.3",
"preact-render-to-string": "^5.2.6",
"qrcode": "^1.5.1",
"toucan-js": "^2.7.0",
Expand All @@ -39,28 +39,29 @@
"@cloudflare/workers-types": "^3.18.0",
"@databases/escape-identifier": "^1.0.3",
"@databases/sql": "^3.2.0",
"@sentry/cli": "^2.8.0",
"@sentry/webpack-plugin": "^1.19.1",
"@sentry/cli": "^2.9.0",
"@sentry/webpack-plugin": "^1.20.0",
"@types/assert": "^1.5.6",
"@types/git-rev-sync": "^2.0.0",
"@types/node": "^18.11.7",
"@types/node": "^18.11.9",
"@types/qrcode": "^1.5.0",
"assert": "^2.0.0",
"ava": "^5.0.1",
"ava": "^5.1.0",
"better-sqlite3": "7.6.2",
"buffer": "^6.0.3",
"delay": "^5.0.0",
"dotenv": "^16.0.3",
"esbuild": "^0.15.12",
"esbuild": "^0.15.14",
"execa": "^6.1.0",
"git-rev-sync": "^3.0.1",
"git-rev-sync": "^3.0.2",
"hd-scripts": "^3.0.2",
"miniflare": "^2.10.0",
"miniflare": "^2.11.0",
"p-wait-for": "^5.0.0",
"process": "^0.11.10",
"readable-stream": "^4.1.0",
"sade": "^1.7.4",
"readable-stream": "^4.2.0",
"sade": "^1.8.1",
"typescript": "4.8.4",
"wrangler": "^2.1.13"
"wrangler": "^2.3.2"
},
"eslintConfig": {
"extends": [
Expand Down Expand Up @@ -92,8 +93,6 @@
],
"ava": {
"failFast": true,
"concurrency": 1,
"workerThreads": false,
"files": [
"test/**/*.test.js"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/access-api/sql/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function migrate(db) {
} catch (error) {
const err = /** @type {Error} */ (error)
// eslint-disable-next-line no-console
console.log({
console.error('D1 Error', {
message: err.message,
// @ts-ignore
cause: err.cause?.message,
Expand Down
29 changes: 24 additions & 5 deletions packages/access-api/src/kvs/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class Accounts {
tableName: 'accounts',
fields: '*',
where: {
conditions: 'did =?1',
conditions: 'did=?1',
params: [did],
},
})
Expand All @@ -68,10 +68,12 @@ export class Accounts {
}

/**
* @param {string} email
* Save account delegation per email
*
* @param {`mailto:${string}`} email
* @param {Ucanto.Delegation<Ucanto.Capabilities>} delegation
*/
async saveAccount(email, delegation) {
async saveDelegation(email, delegation) {
const accs = /** @type {string[] | undefined} */ (
await this.kv.get(email, {
type: 'json',
Expand All @@ -90,10 +92,27 @@ export class Accounts {
}

/**
* @param {string} email
* Check if we have delegations for an email
*
* @param {`mailto:${string}`} email
*/
async hasAccounts(email) {
async hasDelegations(email) {
const r = await this.kv.get(email)
return Boolean(r)
}

/**
* @param {`mailto:${string}`} email
*/
async getDelegations(email) {
const r = await this.kv.get(email, { type: 'json' })

if (!r) {
return
}

return /** @type {import('@web3-storage/access/types').EncodedDelegation<[import('@web3-storage/access/types').Any]>[]} */ (
r
)
}
}
5 changes: 3 additions & 2 deletions packages/access-api/src/kvs/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ export class Validations {
}

/**
* @param {string} ucan
* @template {import('@ucanto/interface').Capabilities} [T=import('@ucanto/interface').Capabilities]
* @param {import('@web3-storage/access/src/types').EncodedDelegation<T>} ucan
*/
async put(ucan) {
const delegation =
/** @type {import('@ucanto/interface').Delegation<[import('@web3-storage/access/capabilities/types').VoucherClaim]>} */ (
/** @type {import('@ucanto/interface').Delegation<T>} */ (
await stringToDelegation(ucan)
)

Expand Down
50 changes: 49 additions & 1 deletion packages/access-api/src/routes/validate-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ import {
* @param {import('../bindings.js').RouteContext} env
*/
export async function validateEmail(req, env) {
if (req.query && req.query.ucan && req.query.mode === 'recover') {
return recover(req, env)
}
if (req.query && req.query.ucan) {
try {
const delegation = await env.kvs.validations.put(req.query.ucan)
const delegation = await env.kvs.validations.put(
/** @type {import('@web3-storage/access/src/types.js').EncodedDelegation<[import('@web3-storage/access/src/types.js').VoucherClaim]>} */ (
req.query.ucan
)
)

return new HtmlResponse(
(
Expand Down Expand Up @@ -48,3 +55,44 @@ export async function validateEmail(req, env) {
<ValidateEmailError msg={'Missing delegation in the URL.'} />
)
}

/**
* @param {import('@web3-storage/worker-utils/router').ParsedRequest} req
* @param {import('../bindings.js').RouteContext} env
*/
async function recover(req, env) {
try {
const delegation = await env.kvs.validations.put(
/** @type {import('@web3-storage/access/src/types.js').EncodedDelegation<[import('@web3-storage/access/src/types.js').AccountRecover]>} */ (
req.query.ucan
)
)

return new HtmlResponse(
(
<ValidateEmail
delegation={delegation}
ucan={req.query.ucan}
qrcode={await QRCode.toString(req.query.ucan, {
type: 'svg',
errorCorrectionLevel: 'M',
margin: 10,
})}
/>
)
)
} catch (error) {
const err = /** @type {Error} */ (error)

if (err.message.includes('Invalid expiration')) {
return new HtmlResponse(
<ValidateEmailError msg={'Email confirmation expired.'} />
)
}

env.log.error(err)
return new HtmlResponse(
<ValidateEmailError msg={'Oops something went wrong.'} />
)
}
}
2 changes: 1 addition & 1 deletion packages/access-api/src/routes/validate-ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import pRetry from 'p-retry'
const run = async (
/** @type {import('../kvs/validations').Validations} */ kv,
/** @type {WebSocket} */ server,
/** @type {any} */ did
/** @type {string} */ did
) => {
const d = await kv.get(did)

Expand Down
Loading