Skip to content

Commit 41d82a8

Browse files
committed
fix: be able to use test and main nets for justifications
1 parent 3fa8460 commit 41d82a8

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

serverless.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,10 @@ functions:
278278
events:
279279
- { http: { path: justifications, method: put, cors: true } }
280280
environment:
281-
INFURA_URL: '${self:custom.kmsSecrets.secrets.${self:custom.environments.${self:provider.stage}}_INFURA_URL}'
282-
KLEROS_LIQUID_ADDRESS: '${self:custom.kmsSecrets.secrets.${self:custom.environments.${self:provider.stage}}_KLEROS_LIQUID_ADDRESS}'
281+
INFURA_URL_KOVAN: '${self:custom.kmsSecrets.secrets.STAGING_INFURA_URL}'
282+
INFURA_URL_MAINNET: '${self:custom.kmsSecrets.secrets.PRODUCTION_INFURA_URL}'
283+
KLEROS_LIQUID_ADDRESS_KOVAN: '${self:custom.kmsSecrets.secrets.STAGING_KLEROS_LIQUID_ADDRESS}'
284+
KLEROS_LIQUID_ADDRESS_MAINNET: '${self:custom.kmsSecrets.secrets.PRODUCTION_KLEROS_LIQUID_ADDRESS}'
283285
iamRoleStatementsName: 'putJustifications-${self:provider.stage}-lambda-role'
284286
iamRoleStatements:
285287
- {

src/court/justifications.js

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const _web3 = require('../utils/web3')
1+
const Web3 = require('web3')
2+
3+
const getEnvVars = require('../utils/get-env-vars')
24
const KlerosLiquid = require('../assets/contracts/KlerosLiquid.json')
35
const dynamoDB = require('../utils/dynamo-db')
46

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

2729
module.exports.put = async (event, _context, callback) => {
28-
// Initialize web3 and contracts
29-
const web3 = await _web3()
30-
const klerosLiquid = new web3.eth.Contract(
31-
KlerosLiquid.abi,
32-
process.env.KLEROS_LIQUID_ADDRESS
33-
)
34-
3530
// Validate signature
3631
const payload = JSON.parse(event.body).payload
32+
33+
if (!payload.network)
34+
return callback(null, {
35+
statusCode: 403,
36+
headers: { 'Access-Control-Allow-Origin': '*' },
37+
body: JSON.stringify({
38+
error:
39+
"Network not specified"
40+
})
41+
})
42+
43+
let klerosLiquidAddress
44+
let web3Uri
45+
switch (payload.network) {
46+
case 'mainnet':
47+
const {
48+
INFURA_URL_MAINNET
49+
} = await getEnvVars(['INFURA_URL_MAINNET'])
50+
klerosLiquidAddress = process.env.KLEROS_LIQUID_ADDRESS_MAINNET
51+
web3Uri = INFURA_URL_MAINNET
52+
break
53+
case 'kovan':
54+
const {
55+
INFURA_URL_KOVAN
56+
} = await getEnvVars([
57+
'INFURA_URL_KOVAN'
58+
])
59+
klerosLiquidAddress = process.env.KLEROS_LIQUID_ADDRESS_KOVAN
60+
web3Uri = INFURA_URL_KOVAN
61+
break;
62+
default:
63+
return callback(null, {
64+
statusCode: 403,
65+
headers: { 'Access-Control-Allow-Origin': '*' },
66+
body: JSON.stringify({
67+
error:
68+
`No Kleros Liquid address found for network ${payload.network}`
69+
})
70+
})
71+
}
72+
73+
const web3 = new Web3(new Web3.providers.HttpProvider(web3Uri))
74+
3775
try {
3876
if (
3977
(await web3.eth.accounts.recover(
@@ -61,6 +99,11 @@ module.exports.put = async (event, _context, callback) => {
6199
})
62100
}
63101

102+
const klerosLiquid = new web3.eth.Contract(
103+
KlerosLiquid.abi,
104+
klerosLiquidAddress
105+
)
106+
64107
// Verify votes belong to user
65108
for (const voteID of payload.justification.voteIDs) {
66109
const vote = await klerosLiquid.methods

0 commit comments

Comments
 (0)