Skip to content

Commit

Permalink
Merge branch 'main' into optional-admin-api
Browse files Browse the repository at this point in the history
  • Loading branch information
forgetso committed Jan 24, 2024
2 parents a0ad554 + d3178ea commit 882be59
Show file tree
Hide file tree
Showing 26 changed files with 1,652 additions and 1,255 deletions.
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default {
'.*.yaml',
'.*.yml',
'.*.toml',
'tsconfig.json',
],
},
],
Expand Down
1,883 changes: 842 additions & 1,041 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/cli/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function startApi(env: ProviderEnvironment, admin = false): Server {
const apiPort = env.config.server.port

apiApp.use(cors())
apiApp.use(express.json())
apiApp.use(express.json({ limit: '50mb' }))
apiApp.use(i18nMiddleware({}))
apiApp.use(prosopoRouter(env))
if (admin) {
Expand Down
3 changes: 2 additions & 1 deletion packages/common/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"INVALID_DIR_FORMAT": "Invalid directory format"
},
"PROGUI": {
"NO_STATE_PROVIDER": "useGlobalState must be used within a GlobalStateProvider"
"NO_STATE_PROVIDER": "useGlobalState must be used within a GlobalStateProvider",
"NO_POLKADOT_EXTENSION": "Polkadot extension not found"
}
}
34 changes: 17 additions & 17 deletions packages/datasets/src/tests/mocks/data/captchas.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"datasetId": "0x80f2e82ed079330c5fd9ea849a2b068e5b5c3473061219e5b33f2f210e6f1715",
"captchas": [
{
"salt": "0x01",
"salt": "0x0ee5e8130bf5834978971abbb3e94703",
"solution": [1, 2, 3],
"timeLimit": 20,
"items": [
Expand Down Expand Up @@ -55,7 +55,7 @@
"target": "bus"
},
{
"salt": "0x02",
"salt": "0xc91bc6807f3b240802b4fbb77b7a64cc",
"solution": [0, 7, 8],
"items": [
{
Expand Down Expand Up @@ -107,7 +107,7 @@
"target": "train"
},
{
"salt": "0x03",
"salt": "0x2b1bab8943b8a24ffdaa23e16b66ad91",
"solution": [],
"items": [
{
Expand Down Expand Up @@ -159,7 +159,7 @@
"target": "car"
},
{
"salt": "0x05",
"salt": "0x70c4756934dbb66b1aafa44178aa9e2e",
"items": [
{
"hash": "0x7f4313a6d4240e206d7c7f16d368ddf12d5f1d78296fd3de7e91df1d7739a35f",
Expand Down Expand Up @@ -210,7 +210,7 @@
"target": "plane"
},
{
"salt": "0x01",
"salt": "0x5f3e0f226785935762337f97aee6eb9b",
"solution": [2, 3, 8],
"items": [
{
Expand Down Expand Up @@ -262,7 +262,7 @@
"target": "dog"
},
{
"salt": "0x02",
"salt": "0x2e81556317a19f15942558cc17715afc",
"solution": [0, 1, 4],
"items": [
{
Expand Down Expand Up @@ -314,7 +314,7 @@
"target": "cat"
},
{
"salt": "0x03",
"salt": "0xdc77a6623da63d4379ebacd5ad440907",
"items": [
{
"hash": "0x0138ab8b1951c2b36284f6abacfc40cc3ec8280adc38462cc5f6b852e17ffe59",
Expand Down Expand Up @@ -365,7 +365,7 @@
"target": "horse"
},
{
"salt": "0x01",
"salt": "0x0e33da9deb5f1cbeef4dce867c33c0c5",
"solution": [0, 7],
"items": [
{
Expand Down Expand Up @@ -417,7 +417,7 @@
"target": "plane"
},
{
"salt": "0x02",
"salt": "0x71ed6ca839978fc286f0651dfcef7862",
"solution": [4, 6, 8],
"items": [
{
Expand Down Expand Up @@ -469,7 +469,7 @@
"target": "dog"
},
{
"salt": "0x03",
"salt": "0xcc8730f359718c4d94c8dc565938621c",
"solution": [2, 3, 5],
"items": [
{
Expand Down Expand Up @@ -521,7 +521,7 @@
"target": "horse"
},
{
"salt": "0x04",
"salt": "0x234e4dc3fe6db2588645a66b78b9128a",
"items": [
{
"hash": "0x6e0784791bf2edbb348ed69fb8080f69f4c38d3a8169f9204b5600a44bdaa600",
Expand Down Expand Up @@ -572,7 +572,7 @@
"target": "bird"
},
{
"salt": "0x01",
"salt": "0xe7988f0244c7bbf76a8f3986c9e0e136",
"solution": [1, 6, 8],
"items": [
{
Expand Down Expand Up @@ -624,7 +624,7 @@
"target": "cat"
},
{
"salt": "0x01",
"salt": "0x743db1f415ed40fc96d83957034e624c",
"solution": [0, 3, 4],
"items": [
{
Expand Down Expand Up @@ -676,7 +676,7 @@
"target": "plane"
},
{
"salt": "0x01",
"salt": "0xaeb51120352c09c503caae1c811421ba",
"solution": [2, 5, 7],
"items": [
{
Expand Down Expand Up @@ -728,7 +728,7 @@
"target": "car"
},
{
"salt": "0x010101",
"salt": "0x1ef72a0d9424c9d616f41537a5dc6add",
"solution": [3, 6, 7],
"items": [
{
Expand Down Expand Up @@ -780,7 +780,7 @@
"target": "cat"
},
{
"salt": "0x020202",
"salt": "0xf6abd9232e3c3dd786be74e31a2dde62",
"solution": [8, 5, 1],
"items": [
{
Expand Down Expand Up @@ -832,7 +832,7 @@
"target": "deer"
},
{
"salt": "0x030303",
"salt": "0xc567e7b688bd615d75b8ae7d108c3dfb",
"items": [
{
"hash": "0xeba2c2c032639c02627f28c6fb77bdd48de56d7a7260f4898a2cfbaa916aa488",
Expand Down
2 changes: 1 addition & 1 deletion packages/provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@polkadot/types": "10.11.1",
"@polkadot/util": "12.6.1",
"@polkadot/util-crypto": "12.6.1",
"@prosopo/captcha-contract": "0.2.30",
"@prosopo/common": "0.2.30",
"@prosopo/contract": "0.2.30",
"@prosopo/database": "0.2.30",
Expand All @@ -45,7 +46,6 @@
"@prosopo/types": "0.2.30",
"@prosopo/types-database": "0.2.30",
"@prosopo/types-env": "0.2.30",
"@prosopo/captcha-contract": "0.2.30",
"cron": "^2.1.0",
"cron-parser": "^4.5.0",
"express": "^4.18.1",
Expand Down
156 changes: 98 additions & 58 deletions packages/provider/src/api/admin.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,109 @@
// import { Keyring } from '@polkadot/keyring'
// import { ProviderEnvironment } from '@prosopo/types-env'
// import express, { Router } from 'express'
// import jwt from 'jsonwebtoken'
import * as z from 'zod'
import { AdminApiPaths } from '@prosopo/types'
import { BatchCommitmentsTask, Tasks } from '../index.js'
import { Payee } from '@prosopo/captcha-contract/types-returns'
import { ProsopoEnvError, UrlConverter } from '@prosopo/common'
import { ProviderEnvironment } from '@prosopo/types-env'
import { Router } from 'express'
import { authMiddleware } from './authMiddleware.js'
import { wrapQuery } from '@prosopo/contract'

// /**
// * Admin router with auth, temp example
// *
// * @return {Router} - router stuff
// * @param {Environment} env - env stuff
// */
// export function adminRouter(env: ProviderEnvironment): Router {
// const keyring = new Keyring({ type: 'sr25519' })
// Setting batch commit interval to 0 for API calls
const apiBatchCommitConfig = {
interval: 0,
maxBatchExtrinsicPercentage: 59,
}

// const router = express.Router()
export function prosopoAdminRouter(env: ProviderEnvironment): Router {
const router = Router()
const tasks = new Tasks(env)

// // Middleware to verify auth token
// const authMiddleware = (req, res, next) => {
// const authHeader = req.headers.authorization
// Use the authMiddleware for all routes in this router
router.use(authMiddleware(tasks, env))

// if (authHeader) {
// const token = authHeader.split(' ')[1]
router.post(AdminApiPaths.BatchCommit, async (req, res, next) => {
if (env.db) {
try {
const batchCommitter = new BatchCommitmentsTask(
apiBatchCommitConfig,
env.getContractInterface(),
env.db,
0n,
env.logger
)
const result = await batchCommitter.run()

// // Verify token using the public key
// jwt.verify(token, env.config.account.address, (err, user) => {
// if (err) {
// return res.sendStatus(403)
// }
console.info(`Batch commit complete: ${result}`)
res.status(200).send(result)
} catch (err) {
console.error(err)
res.status(500).send(err)
}
} else {
console.error('No database configured')
res.status(500).send('No database configured')
}
})

// req.user = user
// next()
// })
// } else {
// res.sendStatus(401)
// }
// }
router.post(AdminApiPaths.UpdateDataset, async (req, res, next) => {
try {
const result = await tasks.providerSetDataset(req.body)

// // Endpoint to authenticate user using Polkadot network for signing messages
// router.post('/auth', async (req, res) => {
// const { message, signature } = req.body
console.info(`Dataset update complete: ${result}`)
res.status(200).send(result)
} catch (err) {
console.error(err)
res.status(500).send(err)
}
})

// try {
// const address = env.config.account.address
// const pair = keyring.addFromAddress(env.config.account.address)
// const isValid = pair.verify(message, signature, address)
router.post(AdminApiPaths.ProviderDeregister, async (req, res, next) => {
try {
const address = env.pair?.address
if (!address) {
throw new ProsopoEnvError('DEVELOPER.MISSING_ENV_VARIABLE', { context: { error: 'No address' } })
}
await tasks.contract.tx.providerDeregister()
} catch (err) {
console.error(err)
res.status(500).send(err)
}
})

// if (isValid) {
// const user = { address }
router.post(AdminApiPaths.ProviderUpdate, async (req, res, next) => {
try {
const { url, fee, payee, value, address } = z
.object({
url: z.string(),
fee: z.number().optional(),
payee: z.nativeEnum(Payee).optional(),
value: z.number().optional(),
address: z.string(),
})
.parse(req.body)
const provider = (await tasks.contract.query.getProvider(address, {})).value.unwrap().unwrap()
if (provider && (url || fee || payee || value)) {
const urlConverted = url ? Array.from(new UrlConverter().encode(url.toString())) : provider.url
await wrapQuery(tasks.contract.query.providerUpdate, tasks.contract.query)(
urlConverted,
fee || provider.fee,
payee || provider.payee,
{ value: value || 0 }
)
const result = await tasks.contract.tx.providerUpdate(
urlConverted,
fee || provider.fee,
payee || provider.payee,
{ value: value || 0 }
)

// // temp jwt, need to verify usage with correct imports and types
// const accessToken = jwt.sign(user, env.config.account.secret, { expiresIn: '1h' })
console.info(JSON.stringify(result, null, 2))
}
} catch (err) {
console.error(err)
res.status(500).send(err)
}
})

// return res.json({ accessToken })
// } else {
// return res.status(401).json({ message: 'bad auth' })
// }
// } catch (err) {
// // todo needs proper prosopo error handling
// return res.status(500).json({ message: 'An error occurred' })
// }
// })

// // Secure endpoint which requires auth token to access
// router.get('/secure', authMiddleware, (req, res) => {
// res.json({ message: 'correct auth' })
// })

// return router
// }
return router
}
Loading

0 comments on commit 882be59

Please sign in to comment.