@@ -17,7 +17,7 @@ module.exports.get = async (event, _context, callback) => {
1717 }
1818 } ,
1919 KeyConditionExpression : 'disputeIDAndAppeal = :disputeIDAndAppeal' ,
20- TableName : ` ${ payload . network } - justifications`
20+ TableName : ' justifications'
2121 } )
2222 }
2323 } )
@@ -32,63 +32,71 @@ module.exports.put = async (event, _context, callback) => {
3232 process . env . KLEROS_LIQUID_ADDRESS
3333 )
3434
35+ // Validate signature
3536 const payload = JSON . parse ( event . body ) . payload
36-
37- // Verify votes belong to user
38- const dispute = await klerosLiquid . methods . getDispute (
39- payload . justification . disputeID
40- ) . call ( )
41-
42- // Get number of votes in current round
43- const votesInRound = dispute . votesLengths [ payload . justification . appeal ]
44-
45- let drawn = false
46- let voteID
47- for ( let i = 0 ; i < Number ( votesInRound ) ; i ++ ) {
48- const vote = await klerosLiquid . methods
49- . getVote ( payload . justification . disputeID , payload . justification . appeal , i )
50- . call ( )
51- if ( vote . account === payload . address ) {
52- // If voted, can no longer submit justification.
53- if ( vote . voted ) {
54- return callback ( null , {
55- statusCode : 403 ,
56- headers : { 'Access-Control-Allow-Origin' : '*' } ,
57- body : JSON . stringify ( {
58- error : 'This address has already cast their vote.'
59- } )
60- } )
61- }
62- // Once we know address has been drawn we can stop searching.
63- drawn = true
64- voteID = i
65- break
66- }
67- }
68-
69- if ( ! drawn ) {
37+ try {
38+ if (
39+ ( await web3 . eth . accounts . recover (
40+ JSON . stringify ( payload . justification ) ,
41+ payload . signature
42+ ) ) !==
43+ ( await dynamoDB . getItem ( {
44+ Key : { address : { S : payload . address } } ,
45+ TableName : 'user-settings' ,
46+ ProjectionExpression : 'derivedAccountAddress'
47+ } ) ) . Item . derivedAccountAddress . S
48+ )
49+ throw new Error (
50+ "Signature does not match the supplied address' derived account address for justifications."
51+ )
52+ } catch ( err ) {
53+ console . error ( err )
7054 return callback ( null , {
7155 statusCode : 403 ,
7256 headers : { 'Access-Control-Allow-Origin' : '*' } ,
7357 body : JSON . stringify ( {
74- error : 'This address was not drawn.'
58+ error :
59+ "Signature is invalid or does not match the supplied address' derived account address for justifications."
7560 } )
7661 } )
7762 }
7863
79- // Save justification.
64+ // Verify votes belong to user
65+ for ( const voteID of payload . justification . voteIDs ) {
66+ const vote = await klerosLiquid . methods
67+ . getVote (
68+ payload . justification . disputeID ,
69+ payload . justification . appeal ,
70+ voteID
71+ )
72+ . call ( )
73+ if ( vote . account !== payload . address || vote . voted )
74+ return callback ( null , {
75+ statusCode : 403 ,
76+ headers : { 'Access-Control-Allow-Origin' : '*' } ,
77+ body : JSON . stringify ( {
78+ error :
79+ 'Not all of the supplied vote IDs belong to the supplied address and are not cast.'
80+ } )
81+ } )
82+ }
83+
84+ // Save justification
8085 await dynamoDB . putItem ( {
8186 Item : {
8287 disputeIDAndAppeal : {
8388 S : `${ payload . justification . disputeID } -${ payload . justification . appeal } `
8489 } ,
85- address : {
86- S : payload . address
90+ voteID : {
91+ N : String (
92+ payload . justification . voteIDs [
93+ payload . justification . voteIDs . length - 1
94+ ]
95+ )
8796 } ,
88- voteID : { N : String ( voteID ) } ,
8997 justification : { S : payload . justification . justification }
9098 } ,
91- TableName : ` ${ payload . network } - justifications`
99+ TableName : ' justifications'
92100 } )
93101 callback ( null , {
94102 statusCode : 200 ,
0 commit comments