import logger from 'logger/logger' import { RelayRecord, RelayStatusEnum } from 'modules/RelayRecord' import { RelaySession } from 'modules/RelaySession' import { checkPersionalSign } from 'zutils/utils/chain.util' import { BaseController, role, ROLE_ANON, router, ZError } from 'zutils' export const ROLE_SESSION = 'session' class RelayController extends BaseController { @role(ROLE_ANON) @router('post /wallet/relay/prepare') async prepareClient(req, res) { let { msg, address, signature } = req.params if (!msg || !address || !signature) { throw new ZError(10, 'params mismatch') } // check signature if (!checkPersionalSign(msg, address, signature)) { throw new ZError(11, 'signature mismatch') } let session = new RelaySession({ msg, address, signature }) await session.save() const ztoken = await res.jwtSign({ sid: session.id, }) return { token: ztoken } } @role(ROLE_SESSION) @router('post /wallet/relay/putdata') async uploadData(req, res) { let { data, type, session_id } = req.params if (type == undefined || !data) { throw new ZError(10, 'params mismatch') } type = parseInt(type) let record = new RelayRecord({ sid: session_id, type, data }) await record.save() return { id: record.id } } @role(ROLE_SESSION) @router('post /wallet/relay/updata') async updateData(req, res) { let { data, id } = req.params if (!id || !data) { throw new ZError(10, 'params mismatch') } let record = await RelayRecord.findById(id) record.status = RelayStatusEnum.RESOLVED record.resp = data await record.save() return { id: record.id } } @role(ROLE_SESSION) @router('post /wallet/relay/getlast') async fetchLast(req, res) { let { session_id, type } = req.params if (type == undefined) { throw new ZError(10, 'params mismatch') } type = parseInt(type) let record = await RelayRecord.findLastRecord(session_id, type) if (!record) { throw new ZError(11, 'record not found') } return { id: record.id, data: record.data, status: record.status } } @role(ROLE_SESSION) @router('post /wallet/relay/getdata') async fetchData(req, res) { let { id } = req.params if (!id) { throw new ZError(10, 'params mismatch') } let record = await RelayRecord.findById(id) if (!record) { throw new ZError(11, 'record not found') } return { id: record.id, data: record.data, resp: record.resp, status: record.status } } }