Skip to content

Commit

Permalink
Merge pull request #62 from BigWhaleLabs/sign-method
Browse files Browse the repository at this point in the history
add method for sing attestations for trusted resource
  • Loading branch information
MixailE authored Nov 15, 2023
2 parents 48f89e7 + b4a0937 commit c2a2e11
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ DOMAIN=verify.sealcred.xyz
ENVIRONMENT=development
MAILGUN_API_KEY=00000000000000000000000000000000
MAILGUN_DOMAIN=example.com
KETL_INVITES_BACKEND=https://example.com
KETL_INVITES_BACKEND=https://example.com
SECRET=secret
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ And you should be good to go! Feel free to fork and submit pull requests.
| `KETL_INVITES_BACKEND` | Link to merkle tree hashes for Ketl |
| `MAILGUN_API_KEY` | Mailgun API key |
| `MAILGUN_DOMAIN` | Mailgun Domain |
| `SECRET` | Bearer token |

Also, please, consider looking at `.env.sample`.

Expand Down
36 changes: 35 additions & 1 deletion src/controllers/verify-ketl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Ctx, Post, Version } from 'amala'
import { Body, Controller, Ctx, Flow, Post, Version } from 'amala'
import { Context } from 'vm'
import {
KETL_BWL_NFT_CONTRACT,
Expand All @@ -11,10 +11,12 @@ import AttestationType from '@/validators/AttestationType'
import AttestationTypeList from '@/validators/AttestationTypeList'
import BalanceUniqueVerifyBody from '@/validators/BalanceUniqueVerifyBody'
import Email from '@/validators/Email'
import SignValidator from '@/validators/SignValidator'
import Signature from '@/validators/Signature'
import Token from '@/validators/Token'
import TwitterBody from '@/validators/TwitterBody'
import VerificationType from '@/models/VerificationType'
import authenticate from '@/helpers/authenticate'
import checkInvite from '@/helpers/ketl/checkInvite'
import fetchUserProfile from '@/helpers/twitter/fetchUserProfile'
import getAttestationHash from '@/helpers/signatures/getAttestationHash'
Expand Down Expand Up @@ -50,6 +52,38 @@ export default class VerifyKetlController {
return signAttestationMessage(type, hexlifyString(token))
}

@Post('/sign')
@Flow(authenticate)
@Version('0.2.2')
async sign(
@Ctx() ctx: Context,
@Body({ required: true })
body: SignValidator
) {
const { hash, types } = body
const secretParts = []

for (const type of types) {
const { message, signature } = await signAttestationMessage(type, hash)
const hasInvite = await checkInvite(type, hash)
if (!hasInvite) continue
if (secretParts.length === 0) {
const attestationHash = message[1]
secretParts.push(attestationHash)
}
secretParts.push(`t${type}${signature}`)
}

if (!secretParts.length)
return ctx.throw(notFound(handleInvitationError('email')))

const secret = secretParts.join('')

return {
secret,
}
}

@Post('/email-unique')
@Version('0.2.2')
async sendMultipleEmailAttestation(
Expand Down
13 changes: 13 additions & 0 deletions src/helpers/authenticate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Context, Next } from 'koa'
import { forbidden } from '@hapi/boom'
import verifyAuthToken from '@/helpers/verifyAuthToken'

export default async function authenticate(ctx: Context, next: Next) {
const authHeader = ctx.headers.authorization
const token = authHeader && authHeader.split(' ')[1]
const isValidToken = await verifyAuthToken(token)

if (!isValidToken) throw forbidden()

return next()
}
1 change: 1 addition & 0 deletions src/helpers/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default cleanEnv(process.env, {
MAILGUN_API_KEY: str(),
MAILGUN_DOMAIN: str(),
PORT: num({ default: 1337 }),
SECRET: str(),
SMTP_PASS: str(),
SMTP_USER: str(),
})
11 changes: 11 additions & 0 deletions src/helpers/verifyAuthToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import env from '@/helpers/env'

export default function verifyAuthToken(authToken?: string) {
if (!authToken) return false
try {
return env.SECRET === authToken
} catch (e) {
console.log(e)
return false
}
}
10 changes: 10 additions & 0 deletions src/validators/SignValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IsEnum, IsString } from 'amala'
import AttestationType from '@/models/AttestationType'

export default class {
@IsString()
hash!: string

@IsEnum(AttestationType, { each: true })
types!: AttestationType[]
}

0 comments on commit c2a2e11

Please sign in to comment.