1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

backend routes

This commit is contained in:
Jernej Pregelj 2019-04-17 12:06:41 +02:00 committed by Matthias Kretschmann
parent 82906093e6
commit cdf9609e1d
Signed by: m
GPG Key ID: 606EEEF3C479A91F
8 changed files with 2701 additions and 76 deletions

2574
server/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,7 @@
"test:watch": "jest --coverage --watch" "test:watch": "jest --coverage --watch"
}, },
"dependencies": { "dependencies": {
"@oceanprotocol/squid": "^0.5.5",
"body-parser": "^1.18.3", "body-parser": "^1.18.3",
"color-js": "^1.0.5", "color-js": "^1.0.5",
"compression": "^1.7.4", "compression": "^1.7.4",
@ -22,7 +23,8 @@
"morgan": "^1.9.1", "morgan": "^1.9.1",
"multer": "^1.4.1", "multer": "^1.4.1",
"request": "^2.88.0", "request": "^2.88.0",
"uuid": "^3.3.2" "uuid": "^3.3.2",
"web3": "^1.0.0-beta.52"
}, },
"devDependencies": { "devDependencies": {
"@types/body-parser": "^1.17.0", "@types/body-parser": "^1.17.0",

View File

@ -1,6 +1,26 @@
const config = { const config = {
app: { app: {
port: 4000 port: 4000,
serviceScheme: 'http',
serviceHost: 'localhost',
servicePort: 4000,
nodeScheme: 'http',
nodeHost: 'localhost',
nodePort: 8545,
aquariusScheme: 'http',
aquariusHost: 'aquarius',
aquariusPort: 5000,
brizoScheme: 'http',
brizoHost: 'localhost',
brizoPort: 8030,
brizoAddress: '0x376817c638d2a04f475a73af37f7b51a2862d567',
parityScheme: 'http',
parityHost: 'localhost',
parityPort: 8545,
secretStoreScheme: 'http',
secretStoreHost: 'localhost',
secretStorePort: 12001,
verbose: true
} }
} }

View File

@ -0,0 +1,65 @@
import { Router, Request, Response } from 'express'
import { getProviders } from '../utils'
export class RetireRouter {
public router: Router
/**
* Initialize the RetireRouter
*/
public constructor() {
this.router = Router()
}
public async retireDid(req: Request, res: Response) {
if (!req.body.did || !req.body.signature) {
return res.send({
status: 'error',
message: 'Missing did or signature'
})
}
const providers = await getProviders()
try {
const userAccount = await providers.web3.eth.personal.ecRecover(
`You are retiring ${req.body.did}`,
req.body.signature
)
const events = await providers.ocean.keeper.didRegistry.contract.getPastEvents(
'DIDAttributeRegistered',
{
filter: {
_owner: userAccount,
_did: req.body.did.replace('did:op:', '0x')
},
fromBlock: 0,
toBlock: 'latest'
}
)
if (events.length > 0) {
// TODO: retire asset in Aquarius
res.send({ status: 'success' })
} else {
return res.send({
status: 'error',
message: 'Not owner of asset'
})
}
} catch (error) {
return res.send({ status: 'error' })
}
}
/**
* Take each handler, and attach to one of the Express.Router's
* endpoints.
*/
public init() {
this.router.post('/', this.retireDid)
}
}
// Create the retireRouter, and export its configured Express.Router
const retireRouter = new RetireRouter()
retireRouter.init()
export default retireRouter.router

View File

@ -0,0 +1,65 @@
import { Router, Request, Response } from 'express'
import { getProviders } from '../utils'
export class UpdateRouter {
public router: Router
/**
* Initialize the UpdateRouter
*/
public constructor() {
this.router = Router()
}
public async updateDid(req: Request, res: Response) {
if (!req.body.did || !req.body.signature) {
return res.send({
status: 'error',
message: 'Missing did or signature'
})
}
const providers = await getProviders()
try {
const userAccount = await providers.web3.eth.personal.ecRecover(
`You are retiring ${req.body.did}`,
req.body.signature
)
const events = await providers.ocean.keeper.didRegistry.contract.getPastEvents(
'DIDAttributeRegistered',
{
filter: {
_owner: userAccount,
_did: req.body.did.replace('did:op:', '0x')
},
fromBlock: 0,
toBlock: 'latest'
}
)
if (events.length > 0) {
// TODO: update asset in Aquarius
res.send({ status: 'success' })
} else {
return res.send({
status: 'error',
message: 'Not owner of asset'
})
}
} catch (error) {
return res.send({ status: 'error' })
}
}
/**
* Take each handler, and attach to one of the Express.Router's
* endpoints.
*/
public init() {
this.router.post('/', this.updateDid)
}
}
// Create the updateRouter, and export its configured Express.Router
const updateRouter = new UpdateRouter()
updateRouter.init()
export default updateRouter.router

View File

@ -11,7 +11,7 @@ export class UrlCheckRouter {
this.router = Router() this.router = Router()
} }
public checkUrl(req: Request, res: Response, next: NextFunction) { public checkUrl(req: Request, res: Response) {
if (!req.body.url) { if (!req.body.url) {
return res.send({ status: 'error', message: 'missing url' }) return res.send({ status: 'error', message: 'missing url' })
} }

View File

@ -6,6 +6,8 @@ import bodyParser from 'body-parser'
// routes // routes
import UrlCheckRouter from './routes/UrlCheckRouter' import UrlCheckRouter from './routes/UrlCheckRouter'
import UpdateRouter from './routes/UpdateRouter'
import RetireRouter from './routes/RetireRouter'
// config // config
import config from './config/config' import config from './config/config'
@ -51,9 +53,11 @@ app.use(bodyParser.urlencoded({ extended: false }))
app.use(compression()) app.use(compression())
// routes // routes
app.use('/api/v1/urlcheck', UrlCheckRouter) app.use('/api/v1/urlcheck', UrlCheckRouter)
app.use('/api/v1/updateDdo', UpdateRouter)
app.use('/api/v1/retireDdo', RetireRouter)
/// catch 404 /// catch 404
app.use((req, res, next) => { app.use((req, res) => {
res.status(404).send() res.status(404).send()
}) })

39
server/src/utils.ts Normal file
View File

@ -0,0 +1,39 @@
import Web3 from 'web3'
import { Ocean } from '@oceanprotocol/squid'
import config from './config/config'
export function getProviders() {
return new Promise<any>(async (resolve, reject) => {
const nodeUri = `${config.app.nodeScheme}://${config.app.nodeHost}:${
config.app.nodePort
}`
const aquariusUri = `${config.app.aquariusScheme}://${
config.app.aquariusHost
}:${config.app.aquariusPort}`
const brizoUri = `${config.app.brizoScheme}://${config.app.brizoHost}:${
config.app.brizoPort
}`
const parityUri = `${config.app.parityScheme}://${
config.app.parityHost
}:${config.app.parityPort}`
const secretStoreUri = `${config.app.secretStoreScheme}://${
config.app.secretStoreHost
}:${config.app.secretStorePort}`
const web3 = new Web3(nodeUri)
const oceanConfig = {
web3Provider: web3,
nodeUri,
aquariusUri,
brizoUri,
brizoAddress: config.app.brizoAddress,
parityUri,
secretStoreUri,
verbose: config.app.verbose
}
const ocean = await Ocean.getInstance(oceanConfig)
resolve({
ocean,
web3
})
})
}