Skip to content

Commit

Permalink
feat: REST API example
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Feb 11, 2020
1 parent d09a326 commit 0102582
Show file tree
Hide file tree
Showing 9 changed files with 2,201 additions and 18 deletions.
1 change: 0 additions & 1 deletion examples/expressjs-ethr/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
{ "path": "../../packages/daf-ethr-did-fs" },
{ "path": "../../packages/daf-ethr-did-react-native" },
{ "path": "../../packages/daf-node-sqlite3" },
{ "path": "../../packages/daf-node-sqlite3" },
{ "path": "../../packages/daf-resolver" },
{ "path": "../../packages/daf-resolver-universal" },
{ "path": "../../packages/daf-selective-disclosure" },
Expand Down
53 changes: 53 additions & 0 deletions examples/rest-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# DAF REST API example

## Install

```
npm i
npm start
```

## Sample queries

### List identity providers

```
curl --location --request GET 'http://localhost:8080/providers'
```

### Create identity

```
curl --location --request POST 'http://localhost:8080/create-identity' \
--header 'Content-Type: application/json' \
--data-raw '{
"type": "rinkeby-ethr-did-fs"
}'
```

### List identities

```
curl --location --request GET 'http://localhost:8080/identities'
```

### Sing VC

```
curl --location --request POST 'http://localhost:8080/handle-action' \
--header 'Content-Type: application/json' \
--data-raw '{
"type": "action.sign.w3c.vc",
"did": "did:ethr:rinkeby:0x7dd28328c8b05a852839284c304b4bd4b628e357",
"data": {
"sub": "did:web:uport.me",
"vc": {
"@context": ["https://www.w3.org/2018/credentials/v1"],
"type": ["VerifiableCredential"],
"credentialSubject": {
"you": "Rock"
}
}
}
}'
```
29 changes: 29 additions & 0 deletions examples/rest-api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import express from 'express'
const app = express()
const port = process.env.PORT || 8080

import { core } from './setup'

app.use(express.json())

app.get('/identities', async (req, res) => {
const identities = await core.identityManager.getIdentities()
res.json(identities.map(identity => identity.did))
})

app.get('/providers', async (req, res) => {
const providers = await core.identityManager.getIdentityProviderTypes()
res.json(providers)
})

app.post('/create-identity', async (req, res) => {
const identity = await core.identityManager.createIdentity(req.body.type)
res.json({ did: identity.did })
})

app.post('/handle-action', async (req, res) => {
const result = await core.handleAction(req.body)
res.json({ result })
})

app.listen(port, () => console.log(`Server running at http://localhost:${port}`))
6 changes: 6 additions & 0 deletions examples/rest-api/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"watch": ["./"],
"ext": "ts",
"ignore": ["**/*.spec.ts"],
"exec": "ts-node ./index.ts"
}
Loading

0 comments on commit 0102582

Please sign in to comment.