Skip to content

Commit

Permalink
chore: push
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed May 7, 2024
1 parent adb1b5e commit eec2698
Show file tree
Hide file tree
Showing 15 changed files with 360 additions and 161 deletions.
4 changes: 2 additions & 2 deletions examples/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"serve": "vite preview --port 3000"
},
"devDependencies": {
"@babel/core": "^7.24.0",
"@babel/core": "^7.24.3",
"@fission-codes/eslint-config": "workspace:^",
"@types/node": "^20.11.25",
"@types/node": "^20.11.30",
"prettier": "3.2.5",
"typescript": "5.3.3",
"vite": "^5.1.5"
Expand Down
57 changes: 43 additions & 14 deletions examples/homestar-gateway/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { fileURLToPath } from 'url'
import fs from 'fs/promises'
import { gracefulExit } from 'exit-hook'
import sade from 'sade'
import { dev } from './src/dev.js'
import { Agent } from '@fission-codes/ucan/agent'
import { FileDriver } from 'iso-kv/drivers/file.js'
import { EdDSASigner } from 'iso-signatures/signers/eddsa.js'
import { Client } from '@fission-codes/client'

// import { dev } from './src/dev.js'

// Handle any uncaught errors
process.once(
Expand All @@ -27,21 +32,45 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
// TODO change to https://github.com/sindresorhus/env-paths
const CONFIG_PATH = path.join(__dirname, 'config')

const prog = sade('gateway').option('--config', 'config file path', CONFIG_PATH)
const prog = sade('gateway')
.option('--config', 'config file path', CONFIG_PATH)
.option('--server', 'server url', 'http://localhost:3000')

prog
.command('dev')
.option('--fn', 'ts file path')
.option('--ipfsPort', 'ipfs port', 5001)
.action(async (/** @type {import('./src/types.ts').ConfigDev} */ opts) => {
try {
await fs.mkdir(CONFIG_PATH, { recursive: true })
// prog
// .command('dev')
// .option('--fn', 'ts file path')
// .option('--ipfsPort', 'ipfs port', 5001)
// .action(async (/** @type {import('./src/types.ts').ConfigDev} */ opts) => {
// try {
// await fs.mkdir(CONFIG_PATH, { recursive: true })

await dev(opts)
} catch (error) {
console.error(error)
gracefulExit(1)
}
// await dev(opts)
// } catch (error) {
// console.error(error)
// gracefulExit(1)
// }
// })

const resolveSigner = (
/** @type {string | CryptoKeyPair | undefined} */ exported
) => {
if (typeof exported === 'string') {
return EdDSASigner.import(exported)
}

return EdDSASigner.generate()
}
prog.command('login').action(async (opts) => {
const agent = await Agent.create({
driver: new FileDriver({
cwd: CONFIG_PATH,
}),
resolveSigner,
})

const client = await Client.create({
url: opts.server,
agent,
})
})
prog.parse(process.argv)
10 changes: 7 additions & 3 deletions examples/homestar-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
"@bytecodealliance/jco": "^1.0.3",
"@bytecodealliance/preview2-shim": "^0.16.0",
"@fission-codes/channel": "workspace:^",
"@fission-codes/client": "workspace:^",
"@fission-codes/homestar": "workspace:^",
"@fission-codes/ucan": "workspace:^",
"@hono/node-server": "^1.8.2",
"@paulmillr/qr": "^0.1.1",
"ajv": "^8.12.0",
"execa": "^8.0.1",
"exit-hook": "^4.0.0",
"homestar-runtime": "0.2.0",
"hono": "^4.0.10",
"iso-web": "^1.0.4",
"hono": "^4.1.4",
"iso-kv": "^3.0.2",
"iso-signatures": "^0.3.2",
"iso-web": "^1.0.5",
"kubo-rpc-client": "^3.0.4",
"listhen": "^1.7.2",
"ora": "^8.0.1",
Expand All @@ -33,7 +37,7 @@
"ts-json-schema-generator": "^1.5.0"
},
"devDependencies": {
"@types/node": "^20.11.25"
"@types/node": "^20.11.30"
},
"eslintConfig": {
"extends": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"depcheck": "^1.4.7",
"lint-staged": "^15.2.2",
"prettier": "3.2.5",
"simple-git-hooks": "^2.10.0",
"simple-git-hooks": "^2.11.0",
"typedoc": "^0.25.12",
"typedoc-plugin-missing-exports": "^2.2.0",
"typedoc-plugin-zod": "^1.1.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/channel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@
"iso-websocket": "^0.2.0"
},
"devDependencies": {
"@types/node": "^20.11.25",
"@types/node": "^20.11.30",
"p-defer": "^4.0.0",
"playwright-test": "^14.1.1",
"type-fest": "^4.11.1",
"type-fest": "^4.14.0",
"unws": "^0.2.4",
"ws": "^8.16.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
"@fission-codes/ucan": "workspace:^",
"iso-did": "^1.6.0",
"iso-signatures": "^0.3.2",
"iso-web": "^1.0.4",
"iso-web": "^1.0.5",
"zod": "^3.22.4"
},
"devDependencies": {
"@testcontainers/postgresql": "^10.7.2",
"@types/node": "^20.11.25",
"@types/node": "^20.11.30",
"execa": "^8.0.1",
"iso-websocket": "^0.2.0",
"msw": "^2.1.5",
Expand Down
51 changes: 45 additions & 6 deletions packages/client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DID } from 'iso-did'
import * as DIDFission from 'iso-did/fission'
import { UCAN } from '@fission-codes/ucan'
import * as Bearer from '@fission-codes/ucan/bearer'
import * as Schemas from './schemas.js'

// eslint-disable-next-line no-unused-vars
import * as T from './types.js'
Expand All @@ -24,6 +25,11 @@ const TTL = 15 // 15 seconds
export class Client {
#baseUrl = ''

/**
* @type {T.Session | undefined}
*/
session = undefined

/**
* @param {T.ClientOptions} opts
*/
Expand Down Expand Up @@ -88,7 +94,7 @@ export class Client {

const headers = Bearer.encode(delegation, store)
const account =
await /** @type {typeof request.json.post<import('./types.js').Account>} */ (
await /** @type {typeof request.json.post<import('./types.js').Session>} */ (
request.json.post
)(new URL('/api/v0/account', this.#baseUrl), {
body: input,
Expand All @@ -103,6 +109,7 @@ export class Client {
await Promise.all(account.result.ucans.map((ucan) => UCAN.fromUcan(ucan)))
)

this.session = account.result
return { result: account.result.account }
}

Expand Down Expand Up @@ -158,13 +165,42 @@ export class Client {
return account
}

/**
*
* @param {T.LoginInput} input
*/
async login(input) {
const parsed = Schemas.LoginInput.safeParse(input)
if (!parsed.success) {
return { error: parsed.error }
}

const accountDid = await this.resolveHandle(input.username)

if (accountDid.error) {
return { error: accountDid.error }
}

const link = await this.accountLink(accountDid.result[0], input.code)

if (link.error) {
return { error: link.error }
}

return { result: link.result }
}

async logout() {
return this.agent.store.clear()
}

/**
* Get a UCAN for a DID that hasn't been associated with this account yet, given an email verification code.
*
* @see https://github.com/fission-codes/fission-server/blob/main/design/api.md#post-apiv0accountdidlink
* @param {T.DID} accountDid
* @param {string} code
* @returns {Promise<T.MaybeResult<T.Account, T.ClientErrors>>}
* @returns {Promise<T.MaybeResult<T.AccountInfo, T.ClientErrors>>}
*/
async accountLink(accountDid, code) {
const { delegation, store } = await this.agent.delegate({
Expand All @@ -179,7 +215,7 @@ export class Client {

const headers = Bearer.encode(delegation, store)
const account =
await /** @type {typeof request.json.post<import('./types.js').Account>} */ (
await /** @type {typeof request.json.post<import('./types.js').Session>} */ (
request.json.post
)(new URL(`/api/v0/account/${accountDid}/link`, this.#baseUrl), {
body: {
Expand All @@ -196,7 +232,8 @@ export class Client {
await Promise.all(account.result.ucans.map((ucan) => UCAN.fromUcan(ucan)))
)

return account
this.session = account.result
return { result: account.result.account }
}

/**
Expand Down Expand Up @@ -295,12 +332,14 @@ export class Client {
* Resolve account DID from account handle
*
* @param {string} handle - The account handle <username>.<domain>
* @returns
*/
async resolveHandle(handle) {
return await resolve(`_did.${handle}`, 'TXT', {
/** @type {T.MaybeResult<T.DID[], import('iso-web/doh').Errors>} */
const out = await resolve(`_did.${handle}`, 'TXT', {
server: new URL(`/dns-query`, this.#baseUrl).toString(),
})

return out
}

/**
Expand Down
9 changes: 9 additions & 0 deletions packages/client/src/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { z } from 'zod'

/**
* @typedef {import('zod').z.infer<typeof AccountInput>} AccountInput
* @typedef {import('zod').z.infer<typeof LoginInput>} LoginInput
*/

export const AccountInput = z.object({
Expand All @@ -13,3 +14,11 @@ export const AccountInput = z.object({
username: z.string(),
credentialID: z.string().optional(),
})

export const LoginInput = z.object({
/**
* email verification code
*/
code: z.string().length(6),
username: z.string(),
})
2 changes: 1 addition & 1 deletion packages/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface AccountInfo {
email: string
}

export interface Account {
export interface Session {
account: AccountInfo
ucans: JWT[]
}
Expand Down
22 changes: 22 additions & 0 deletions packages/client/test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,25 @@ test.skip('should fetch capabilities', async function () {
}
// console.log('🚀 ~ caps:', caps.error?.cause)
})

test('should login', async function () {
const { account, client, email } = await createAccount()
const { codePromise, isReady } = waitForCode(email)

await client.logout()
const accountInfo = await client.accountInfo(account.did)
assert.ok(accountInfo.error)

await isReady
await client.verifyEmail(email)
const code = await codePromise

await client.login({
code,
username: account.username,
})

const accountInfo2 = await client.accountInfo(account.did)

assert.deepEqual(account, accountInfo2.result)
})
4 changes: 2 additions & 2 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
},
"devDependencies": {
"@types/assert": "^1.5.9",
"@types/eslint": "^8.56.5",
"@types/eslint": "^8.56.6",
"@types/mocha": "^10.0.4",
"@types/node": "^20.11.25",
"@types/node": "^20.11.30",
"assert": "^2.1.0",
"mocha": "^10.3.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/homestar-wit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"test:browser": "playwright-test 'test/**/!(*.node).test.js'"
},
"devDependencies": {
"@types/node": "^20.11.25",
"@types/node": "^20.11.30",
"playwright-test": "^14.1.1"
},
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions packages/homestar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"devDependencies": {
"@types/get-value": "^3.0.5",
"@types/json-templates": "^3.0.3",
"@types/node": "^20.11.26",
"@types/node": "^20.11.30",
"@types/object-path": "^0.11.4",
"execa": "^8.0.1",
"homestar-runtime": "rc",
Expand All @@ -92,7 +92,7 @@
"playwright-test": "^14.1.1",
"tempy": "^3.1.0",
"testcontainers": "^10.7.2",
"type-fest": "^4.12.0",
"type-fest": "^4.14.0",
"unws": "^0.2.4",
"ws": "^8.16.0"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/ucan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@
"@scure/bip39": "^1.2.2",
"iso-base": "^4.0.0",
"iso-did": "^1.6.0",
"iso-kv": "^3.0.1",
"iso-kv": "^3.0.2",
"iso-signatures": "^0.3.2",
"multiformats": "^13.1.0"
},
"devDependencies": {
"@types/node": "^20.11.25",
"@types/node": "^20.11.30",
"playwright-test": "^14.1.1",
"type-fest": "^4.11.1"
"type-fest": "^4.14.0"
},
"publishConfig": {
"provenance": true
Expand Down
Loading

0 comments on commit eec2698

Please sign in to comment.