Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,10 @@ functions:
events:
- { http: { path: justifications, method: put, cors: true } }
environment:
INFURA_URL: '${self:custom.kmsSecrets.secrets.${self:custom.environments.${self:provider.stage}}_INFURA_URL}'
KLEROS_LIQUID_ADDRESS: '${self:custom.kmsSecrets.secrets.${self:custom.environments.${self:provider.stage}}_KLEROS_LIQUID_ADDRESS}'
INFURA_URL_KOVAN: '${self:custom.kmsSecrets.secrets.STAGING_INFURA_URL}'
INFURA_URL_MAINNET: '${self:custom.kmsSecrets.secrets.PRODUCTION_INFURA_URL}'
KLEROS_LIQUID_ADDRESS_KOVAN: '${self:custom.kmsSecrets.secrets.STAGING_KLEROS_LIQUID_ADDRESS}'
KLEROS_LIQUID_ADDRESS_MAINNET: '${self:custom.kmsSecrets.secrets.PRODUCTION_KLEROS_LIQUID_ADDRESS}'
iamRoleStatementsName: 'putJustifications-${self:provider.stage}-lambda-role'
iamRoleStatements:
- {
Expand Down
59 changes: 51 additions & 8 deletions src/court/justifications.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const _web3 = require('../utils/web3')
const Web3 = require('web3')

const getEnvVars = require('../utils/get-env-vars')
const KlerosLiquid = require('../assets/contracts/KlerosLiquid.json')
const dynamoDB = require('../utils/dynamo-db')

Expand All @@ -25,15 +27,51 @@ module.exports.get = async (event, _context, callback) => {
}

module.exports.put = async (event, _context, callback) => {
// Initialize web3 and contracts
const web3 = await _web3()
const klerosLiquid = new web3.eth.Contract(
KlerosLiquid.abi,
process.env.KLEROS_LIQUID_ADDRESS
)

// Validate signature
const payload = JSON.parse(event.body).payload

if (!payload.network)
return callback(null, {
statusCode: 403,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
error:
"Network not specified"
})
})

let klerosLiquidAddress
let web3Uri
switch (payload.network) {
case 'mainnet':
const {
INFURA_URL_MAINNET
} = await getEnvVars(['INFURA_URL_MAINNET'])
klerosLiquidAddress = process.env.KLEROS_LIQUID_ADDRESS_MAINNET
web3Uri = INFURA_URL_MAINNET
break
case 'kovan':
const {
INFURA_URL_KOVAN
} = await getEnvVars([
'INFURA_URL_KOVAN'
])
klerosLiquidAddress = process.env.KLEROS_LIQUID_ADDRESS_KOVAN
web3Uri = INFURA_URL_KOVAN
break;
default:
return callback(null, {
statusCode: 403,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({
error:
`No Kleros Liquid address found for network ${payload.network}`
})
})
}

const web3 = new Web3(new Web3.providers.HttpProvider(web3Uri))

try {
if (
(await web3.eth.accounts.recover(
Expand Down Expand Up @@ -61,6 +99,11 @@ module.exports.put = async (event, _context, callback) => {
})
}

const klerosLiquid = new web3.eth.Contract(
KlerosLiquid.abi,
klerosLiquidAddress
)

// Verify votes belong to user
for (const voteID of payload.justification.voteIDs) {
const vote = await klerosLiquid.methods
Expand Down