Skip to content

Commit

Permalink
fix: space create and register improvements (#8)
Browse files Browse the repository at this point in the history
- pull in fix for buffer serialization error
- better output and bug fixes

```bash
❯ ./bin.js space create
did:key:z6MkkEDpGF4R75APkzpcAAVeQjzHRug5kf15nbCdGzbungR6
```

```bash
❯ ./bin.js space register oli@protocol.ai
⠴ 🔗 please click the link we sent to oli@protocol.ai to register your space
```

License: MIT
Signed-off-by: Oli Evans <oli@protocol.ai>
  • Loading branch information
olizilla authored Dec 10, 2022
1 parent cf52922 commit 8617b49
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
11 changes: 3 additions & 8 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@ cli.command('open <cid>')
cli.command('space')
.describe('Create and mangage w3 spaces')

cli.command('space create <name>')
cli.command('space create [name]')
.describe('Create a new w3 space')
.action(name => {
createSpace(name)
console.log(`Created ${name}`)
})
.action(createSpace)

cli.command('space register <email>')
.describe('Claim the space by associating it with your email address')
.action(email => {
registerSpace(email)
})
.action(registerSpace)

cli.command('delegation create <audience-did>')
.describe('Create a delegation to the passed audience for the given abilities with the _current_ space as the resource.')
Expand Down
27 changes: 21 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs'
import ora from 'ora'
import { Readable } from 'stream'
import { create } from '@web3-storage/w3up-client'
import * as DID from '@ipld/dag-ucan/did'
import { CarWriter } from '@ipld/car'
import ora from 'ora'
import { filesFromPath } from 'files-from-path'
import { checkPathsExist, filesize } from './lib.js'

Expand Down Expand Up @@ -39,18 +39,33 @@ export async function createSpace (name) {
const client = await create()
const space = await client.createSpace(name)
await client.setCurrentSpace(space.did)
console.log(space.did)
}

export async function registerSpace (address) {
export async function registerSpace (email) {
const client = await create()
if (await client.currentSpace() === undefined) {
await client.setCurrentSpace((await client.createSpace()).did)
let space = client.currentSpace()
if (space === undefined) {
space = await client.setCurrentSpace(space.did())
await client.setCurrentSpace(space.did())
}
let spinner
setTimeout(() => {
spinner = ora(`🔗 please click the link we sent to ${email} to register your space`).start()
}, 1000)
try {
await client.registerSpace(address)
await client.registerSpace(email)
} catch (err) {
console.error('registration failed: ', err)
if (spinner) spinner.stop()
if (err.message.startsWith('Space already registered')) {
console.error('Error: space already registered.')
} else {
console.error(err)
}
process.exit(1)
}
if (spinner) spinner.stop()
console.log(`⁂ space registered to ${email}`)
}

export async function createDelegation (audienceDID, opts) {
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/bin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ test('w3 --version', (t) => {
const { stdout } = execaSync('./bin.js', ['--version'])
t.regex(stdout, /w3, \d.\d.\d/)
})

test('w3 space create', (t) => {
const { stdout } = execaSync('./bin.js', ['space', 'create'])
t.regex(stdout, /^did:key:/)
})

0 comments on commit 8617b49

Please sign in to comment.