This commit is contained in:
Daniel Holmgren 2022-01-28 14:07:57 -07:00
parent 563597c0a8
commit 6d09f13d81
5 changed files with 20 additions and 18 deletions
frontend/pages
server/src
third-party/src

@ -40,7 +40,7 @@ function Home(props: {}) {
const car = await service.fetchUser(localUser.keypair.did())
userStore = await UserStore.fromCarFile(car, MemoryDB.getGlobal(), localUser.keypair)
} catch (_err) {
// @@TODO: show error instead of an empty store
// @TODO: show error instead of an empty store
userStore = await createNewStore()
}
setStore(userStore)

@ -4,7 +4,7 @@ import { ucanCheck, UserStore, MemoryDB } from '@bluesky-demo/common'
import * as UserDids from '../user-dids'
import * as UserRoots from '../user-roots'
import { readReqBytes } from '../util'
import { SERVER_KEY, SERVER_DID } from '../server-identity'
import { SERVER_KEYPAIR, SERVER_DID } from '../server-identity'
const router = express.Router()
@ -19,13 +19,14 @@ router.post('/register', async (req, res) => {
ucanCheck.isRoot()
)
} catch(err) {
res.status(401).send(err)
return res.status(401).send(`Invalid Ucan: ${err.toString()}`)
}
let userStore: UserStore
try {
const bytes = await readReqBytes(req)
userStore = await UserStore.fromCarFile(bytes, MemoryDB.getGlobal(), SERVER_KEY)
// @TODO: make a read-only vesion of user store that doesn't require keypair
userStore = await UserStore.fromCarFile(bytes, MemoryDB.getGlobal(), SERVER_KEYPAIR)
}catch(err) {
return res.status(400).send("Could not parse UserStore from CAR File")
}
@ -48,7 +49,7 @@ router.post('/update', async (req, res) => {
let userStore: UserStore
try {
const bytes = await readReqBytes(req)
userStore = await UserStore.fromCarFile(bytes, MemoryDB.getGlobal(), SERVER_KEY)
userStore = await UserStore.fromCarFile(bytes, MemoryDB.getGlobal(), SERVER_KEYPAIR)
}catch(err) {
return res.status(400).send("Could not parse UserStore from CAR File")
}
@ -64,10 +65,10 @@ router.post('/update', async (req, res) => {
ucanCheck.hasPostingPermission(user.name, userDid)
)
} catch(err) {
res.status(401).send(err)
return res.status(401).send(err)
}
// @@TODO: verify signature on data structure
// @TODO: verify signature on data structure
await UserRoots.set(userDid, userStore.root)
@ -79,10 +80,10 @@ router.get('/:id', async (req, res) => {
const userRoot = await UserRoots.get(id)
const userStore = await UserStore.get(userRoot, MemoryDB.getGlobal(), SERVER_KEY)
const userStore = await UserStore.get(userRoot, MemoryDB.getGlobal(), SERVER_KEYPAIR)
const bytes = await userStore.getCarFile()
res.status(200).send(Buffer.from(bytes))
return res.status(200).send(Buffer.from(bytes))
})
export default router

@ -7,7 +7,7 @@ const router = express.Router()
// Return the server's did
router.get('/did.json', (_req, res) => {
res.send({ id: SERVER_DID })}
return res.send({ id: SERVER_DID })}
)
// Retrieve a user's DID by their username
@ -21,7 +21,8 @@ router.get('/webfinger', async (req, res) => {
if (!did) {
return res.status(404).send("User DID not found")
}
res.status(200).send({ id: did })
// @TODO: sketch out more of a webfinger doc
return res.status(200).send({ id: did })
})
export default router

@ -1,6 +1,6 @@
import * as ucan from 'ucans'
// // WARNING: For demo only, do not actually store secret keys in plaintext.
// @TODO: For demo only, do not actually store secret keys in plaintext.
const SECRET_KEY = 'JjiTSPfawSBQrxSTEakN8GPvNxwb30MF+R3guCzu78hrzKHjNcFqkF6lTyYuLVpbMCVpPKFJTyju27mw1TA1aQ=='
export const SERVER_KEY = ucan.EdKeypair.fromSecretKey(SECRET_KEY)
export const SERVER_DID = SERVER_KEY.did()
export const SERVER_KEYPAIR = ucan.EdKeypair.fromSecretKey(SECRET_KEY)
export const SERVER_DID = SERVER_KEYPAIR.did()

@ -5,8 +5,8 @@ import { service, UserStore, MemoryDB, ucanCheck } from '@bluesky-demo/common'
// WARNING: For demo only, do not actually store secret keys in plaintext.
const SECRET_KEY = 'I0HyDksQcCRdJBGVuE78Ts34SzyF7+xNprEQw/IRa51OuFZQc5ugqfgjeWRMehyfr7A1vXICRoUD5kqVadsRHA=='
const SERVER_KEY = ucan.EdKeypair.fromSecretKey(SECRET_KEY)
const SERVER_DID = SERVER_KEY.did()
const SERVER_KEYPAIR = ucan.EdKeypair.fromSecretKey(SECRET_KEY)
const SERVER_DID = SERVER_KEYPAIR.did()
const app = express()
app.use(express.json())
@ -46,14 +46,14 @@ app.post('/post', async (req, res) => {
const blueskyDid = await service.getServerDid()
const extendUcan = await ucan.build({
audience: blueskyDid,
issuer: SERVER_KEY,
issuer: SERVER_KEYPAIR,
capabilities: u.attenuation(),
proofs: [u.encoded()]
})
const encoded = ucan.encode(extendUcan)
const car = await service.fetchUser(userDid)
const userStore = await UserStore.fromCarFile(car, MemoryDB.getGlobal(), SERVER_KEY)
const userStore = await UserStore.fromCarFile(car, MemoryDB.getGlobal(), SERVER_KEYPAIR)
await userStore.addPost({
user: username,
text: `Hey there! I'm posting on ${username}'s behalf`