Skip to content

Commit

Permalink
feat!: new accounts (#72)
Browse files Browse the repository at this point in the history
- feat: Add optional origin to store/add (#73)

Co-authored-by: Hugo Dias <hugomrdias@gmail.com>

Co-authored-by: ice.breaker <94936890+ice-breaker-tg@users.noreply.github.com>
  • Loading branch information
hugomrdias and ice-breaker-tg authored Sep 28, 2022
1 parent 6a7ea62 commit 9f6cb41
Show file tree
Hide file tree
Showing 65 changed files with 2,406 additions and 1,497 deletions.
3 changes: 1 addition & 2 deletions .github/release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"packages/access": {},
"packages/access-api": {},
"packages/wallet": {},
"packages/store": {},
"packages/sigv4": {}
"packages/store": {}
}
}
1 change: 0 additions & 1 deletion .github/release-please-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"packages/access": "1.0.0",
"packages/wallet": "0.3.0",
"packages/access-api": "1.0.0",
"packages/sigv4": "1.0.0",
"packages/store": "1.1.0"
}
78 changes: 0 additions & 78 deletions .github/workflows/sigv4.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_modules
# testing
coverage
.nyc_output
.wrangler


# misc
Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"prettier": "2.7.1",
"simple-git-hooks": "^2.8.0",
"typescript": "^4.8.3",
"wrangler": "^2.1.6"
"wrangler": "^2.1.8"
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
Expand All @@ -35,5 +35,13 @@
},
"engines": {
"node": ">=14"
},
"pnpm": {
"overrides": {
"npx-import": "1.1.3"
},
"patchedDependencies": {
"npx-import@1.1.3": "patches/npx-import@1.1.3.patch"
}
}
}
23 changes: 14 additions & 9 deletions packages/access-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"scripts": {
"lint": "tsc --build && eslint '**/*.{js,ts}' && prettier --check '**/*.{js,ts,yml,json}' --ignore-path ../../.gitignore",
"deploy": "wrangler publish",
"dev": "miniflare --watch --debug --env ../../.env --wrangler-env dev",
"dev": "miniflare --watch --debug --wrangler-env dev --env ../../.env",
"build": "scripts/cli.js build",
"check": "tsc --build",
"test": "tsc --build && ava --serial"
"test": "tsc --build && ava --timeout 10s"
},
"author": "Hugo Dias <hugomrdias@gmail.com> (hugodias.me)",
"license": "(Apache-2.0 OR MIT)",
Expand All @@ -28,17 +28,20 @@
"@web3-storage/worker-utils": "0.4.3-dev",
"multiformats": "^9.8.1",
"nanoid": "^4.0.0",
"toucan-js": "^2.6.0"
"p-retry": "^5.1.1",
"toucan-js": "^2.7.0",
"workers-qb": "^0.1.2"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.16.0",
"@sentry/cli": "^2.5.2",
"@sentry/cli": "^2.6.0",
"@sentry/webpack-plugin": "^1.16.0",
"@types/assert": "^1.5.6",
"@types/git-rev-sync": "^2.0.0",
"@types/node": "^18.7.18",
"@types/node": "^18.7.23",
"assert": "^2.0.0",
"ava": "^4.3.3",
"better-sqlite3": "7.6.2",
"buffer": "^6.0.3",
"delay": "^5.0.0",
"dotenv": "^16.0.2",
Expand All @@ -51,7 +54,7 @@
"readable-stream": "^4.1.0",
"sade": "^1.7.4",
"typescript": "4.8.3",
"wrangler": "^2.1.6"
"wrangler": "^2.1.8"
},
"eslintConfig": {
"extends": [
Expand All @@ -68,7 +71,8 @@
"ACCOUNTS": "writable",
"VALIDATIONS": "writable",
"BUCKET": "writable",
"W3ACCESS_METRICS": "writable"
"W3ACCESS_METRICS": "writable",
"WebSocketPair": "readonly"
}
},
"eslintIgnore": [
Expand All @@ -78,12 +82,13 @@
"docs"
],
"ava": {
"concurrency": 1,
"failFast": true,
"workerThreads": false,
"files": [
"test/**/*.test.js"
],
"nodeArguments": [
"--no-warnings"
],
"ignoredByWatcher": [
"./dist/*"
]
Expand Down
11 changes: 11 additions & 0 deletions packages/access-api/sql/tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DROP TABLE IF EXISTS accounts;

CREATE TABLE accounts (
did TEXT NOT NULL PRIMARY KEY
, product TEXT NOT NULL
, email TEXT NOT NULL
, agent TEXT NOT NULL
, inserted_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
, updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
, UNIQUE(did)
)
8 changes: 6 additions & 2 deletions packages/access-api/src/bindings.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Logging } from '@web3-storage/worker-utils/logging'
import type { SigningAuthority } from '@ucanto/interface'
import type { SigningPrincipal } from '@ucanto/interface'
import type { config } from './config'
import { Email } from './utils/email.js'
import { Accounts } from './kvs/accounts.js'
import { Validations } from './kvs/validations.js'
import { D1QB } from 'workers-qb'

export {}

Expand All @@ -21,12 +22,14 @@ declare global {
const ACCOUNTS: KVNamespace
const VALIDATIONS: KVNamespace
const W3ACCESS_METRICS: AnalyticsEngine
// eslint-disable-next-line @typescript-eslint/naming-convention
const __D1_BETA__: D1Database
}

export interface RouteContext {
params: Record<string, string>
log: Logging
keypair: SigningAuthority
keypair: SigningPrincipal
config: typeof config
url: URL
event: FetchEvent
Expand All @@ -35,6 +38,7 @@ export interface RouteContext {
accounts: Accounts
validations: Validations
}
db: D1QB
}

export type Handler = (
Expand Down
1 change: 1 addition & 0 deletions packages/access-api/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function loadConfigVariables() {
) || createAnalyticsEngine(),
ACCOUNTS,
VALIDATIONS,
DB: /** @type {D1Database} */ (globals.__D1_BETA__),
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/access-api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { notFound } from '@web3-storage/worker-utils/response'
import { Router } from '@web3-storage/worker-utils/router'
import { postRaw } from './routes/raw.js'
import { postRoot } from './routes/root.js'
import { validateEmail } from './routes/validate-email.js'
import { validateWS } from './routes/validate-ws.js'
import { validate } from './routes/validate.js'
import { version } from './routes/version.js'
import { getContext } from './utils/context.js'
Expand All @@ -14,6 +16,8 @@ const r = new Router({ onNotFound: notFound })
r.add('options', '*', preflight)
r.add('get', '/version', version)
r.add('get', '/validate', validate)
r.add('get', '/validate-email', validateEmail)
r.add('get', '/validate-ws', validateWS)
r.add('post', '/', postRoot)
r.add('post', '/raw', postRaw)

Expand Down
2 changes: 1 addition & 1 deletion packages/access-api/src/kvs/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Validations {
// @ts-ignore
const ucan = UCAN.parse(delegation)
await this.kv.put(ucan.audience.did(), delegation, {
expirationTtl: 2 * 60,
expirationTtl: 20 * 60,
})

return ucan
Expand Down
15 changes: 15 additions & 0 deletions packages/access-api/src/routes/validate-email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {import('@web3-storage/worker-utils/router').ParsedRequest} req
* @param {import('../bindings.js').RouteContext} env
*/
export async function validateEmail(req, env) {
if (req.query && req.query.ucan && req.query.did) {
// TODO parse and set KV ttl to delegation ttl

await env.config.VALIDATIONS.put(req.query.did, req.query.ucan)

return new Response('Done')
}

throw new Error('needs ucan or did query')
}
63 changes: 63 additions & 0 deletions packages/access-api/src/routes/validate-ws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import pRetry from 'p-retry'

const run = async (
/** @type {KVNamespace<string>} */ kv,
/** @type {WebSocket} */ server,
/** @type {any} */ did
) => {
const d = await kv.get(did)

if (!d) {
throw new Error('Not found.')
} else {
server.send(
JSON.stringify({
type: 'delegation',
delegation: d,
})
)
server.close()
await kv.delete(did)
return d
}
}

/**
* @param {import('@web3-storage/worker-utils/router').ParsedRequest} req
* @param {import('../bindings.js').RouteContext} env
*/
export async function validateWS(req, env) {
const upgradeHeader = req.headers.get('Upgrade')
if (!upgradeHeader || upgradeHeader !== 'websocket') {
return new Response('Expected Upgrade: websocket', { status: 426 })
}

const [client, server] = Object.values(new WebSocketPair())
server.accept()
server.addEventListener('message', async (msg) => {
// @ts-ignore
const { did } = JSON.parse(msg.data)

try {
await pRetry(() => run(env.config.VALIDATIONS, server, did), {
retries: 200,
minTimeout: 1000,
factor: 1,
})
} catch (error) {
const err = /** @type {Error} */ (error)
server.send(
JSON.stringify({
type: 'timeout',
error: err.message,
})
)
server.close()
}
})

return new Response(undefined, {
status: 101,
webSocket: client,
})
}
15 changes: 13 additions & 2 deletions packages/access-api/src/routes/version.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { JSONResponse } from '@web3-storage/worker-utils/response'

/**
* @param {import('@web3-storage/worker-utils/router').ParsedRequest} event
* @param {import('../bindings.js').RouteContext} env
*/
export function version(event, env) {
export async function version(event, env) {
// const inserted = await env.db.insert({
// tableName: 'accounts',
// data: {
// did: 'did:key:ztest',
// product: 'free',
// email: 'hugomrdias@gmail.com',
// agent: 'did:key:zagent',
// },
// returning: '*',
// })
// console.log('🚀 ~ file: version.js ~ line 15 ~ version ~ inserted', inserted)
// console.log(new Date(inserted.results[0].inserted_at).toISOString())
return new JSONResponse({
version: env.config.VERSION,
commit: env.config.COMMITHASH,
Expand Down
Loading

0 comments on commit 9f6cb41

Please sign in to comment.