Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@
},
"dependencies": {
"babel-runtime": "^6.26.0",
"eth-sig-util": "^1.4.2",
"kleros": "^0.0.5",
"kleros-interaction": "^0.0.8",
"lodash": "^4.17.4",
"truffle-contract": "^2.0.5",
"web3": "^0.20.1"
"web3": "^0.20.1",
"web3-eth-personal": "^1.0.0-beta.34"
}
}
5 changes: 5 additions & 0 deletions src/constants/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,8 @@ export const MISSING_STORE_PROVIDER =
export const MISSING_CONTRACT_INSTANCE = contractAddress =>
`No contract instance stored for ${contractAddress}. Please call addContractInstance.`
export const ERROR_FETCHING_EVENTS = error => `Unable to fetch events: ${error}`

// Auth
export const UNABLE_TO_SIGN_TOKEN = `There was an error signing the auth token. Please try again`
export const INVALID_AUTH_TOKEN = msg =>
`Authorization Token is invalid: ${msg}`
1 change: 0 additions & 1 deletion src/contracts/ContractImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ class ContractImplementation {
this._contractLoadedRejecter = reject
})

// we have getters so that abstract classes can provide public access to implementations variables
/**
* Get the contract address for the currently instantiated contract.
* @returns {string} - The address of the contract.
Expand Down
10 changes: 6 additions & 4 deletions src/contracts/abstractions/Arbitrable.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ class ArbitrableContract extends AbstractContract {
getData = async account => {
const contractData = await this._contractImplementation.getData()

const storeData = await this._StoreProvider.getContractByAddress(
account,
this._contractImplementation.contractAddress
)
let storeData = {}
if (account)
storeData = await this._StoreProvider.getContractByAddress(
account,
this._contractImplementation.contractAddress
)

return Object.assign({}, storeData, contractData)
}
Expand Down
4 changes: 2 additions & 2 deletions src/contracts/abstractions/Arbitrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Arbitrator extends AbstractContract {

const _getDisputesForUserFromStore = async account =>
Promise.all(
(await this._StoreProvider.getDisputesForUser(account)).map(dispute =>
this._contractImplementation.getDispute(dispute.disputeId, account)
(await this._StoreProvider.getDisputes(account)).map(dispute =>
this._contractImplementation.getDispute(dispute.disputeId)
)
)

Expand Down
13 changes: 7 additions & 6 deletions src/contracts/implementations/arbitrator/KlerosPOC.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,12 @@ class KlerosPOC extends ContractImplementation {
const openDisputes = await this.getOpenDisputesForSession()

const disputes = await Promise.all(
openDisputes.map(async disputeId => {
const draws = await this.getDrawsForJuror(disputeId, account)

const disputeData = await this.getDispute(disputeId)
disputeData.appealDraws = []
openDisputes.map(async disputeData => {
const draws = await this.getDrawsForJuror(
disputeData.disputeId,
account
)
disputeData.appealDraws = disputeData.appealDraws || []
disputeData.appealDraws[disputeData.numberOfAppeals] = draws

return disputeData
Expand Down Expand Up @@ -539,7 +540,7 @@ class KlerosPOC extends ContractImplementation {

// If dispute is in the current session, add it to the result array
if (dispute.firstSession + dispute.numberOfAppeals === currentSession)
openDisputes.push(disputeId)
openDisputes.push(dispute)

// Advance to the next dispute
disputeId++
Expand Down
9 changes: 7 additions & 2 deletions src/kleros.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ class Kleros {
* use when initializing KlerosPOC
* @param {string} arbitrableContractAddress - Address of the arbitrator contract we should
* use when initializing KlerosPOC
* @param {string} authToken - Signed token cooresponding to the user profile address.
*/
constructor(
ethereumProvider = isRequired('ethereumProvider'),
storeUri = isRequired('storeUri'),
arbitratorAddress,
arbitrableContractAddress
arbitrableContractAddress,
authToken
) {
// NOTE we default to KlerosPOC and ArbitrableTransaction
const _klerosPOC = new contracts.implementations.arbitrator.KlerosPOC(
Expand All @@ -55,7 +57,7 @@ class Kleros {
// **************************** //
// KLEROS WRAPPERS
this.web3Wrapper = new Web3Wrapper(ethereumProvider)
this.storeWrapper = new StoreProviderWrapper(storeUri)
this.storeWrapper = new StoreProviderWrapper(storeUri, authToken)
// ARBITRATOR
this.arbitrator = new contracts.abstractions.Arbitrator(
_klerosPOC,
Expand All @@ -78,6 +80,8 @@ class Kleros {
this.arbitrable,
this.storeWrapper
)
// AUTH
this.auth = new resources.Auth(this.web3Wrapper, this.storeWrapper)
}

/**
Expand Down Expand Up @@ -137,6 +141,7 @@ class Kleros {
this.arbitrable.setStoreProviderInstance(this.storeWrapper)
this.arbitrator.setStoreProviderInstance(this.storeWrapper)
this.notifications.setStoreProviderInstance(this.storeWrapper)
this.auth.setStoreProviderInstance(this.storeWrapper)
}
}

Expand Down
78 changes: 78 additions & 0 deletions src/resources/Auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import Personal from 'web3-eth-personal'

import isRequired from '../utils/isRequired'
import { UNABLE_TO_SIGN_TOKEN } from '../constants/error'

class Auth {
constructor(
web3Wrapper = isRequired('web3Wrapper'),
storeProviderInstance = isRequired('storeProviderInstance')
) {
this._Web3Wrapper = web3Wrapper
this._StoreProviderInstance = storeProviderInstance
}

/**
* Set store provider instance.
* @param {object} storeProviderInstance - instance of store provider wrapper.
*/
setStoreProviderInstance = storeProviderInstance => {
this._StoreProviderInstance = storeProviderInstance
}

/**
* Set an auth token in the Store Provider. Call this instead of validateNewAuthToken
* if you have a signed token saved.
* @param {string} token - Hex representation of signed token.
*/
setAuthToken = token => {
this._StoreProviderInstance.setAuthToken(token)
}

/**
* Validate a new auth token. Note if you validate a new token old signed tokens
* will not longer be valid regardless of their expiration time.
* @param {string} userAddress - Address of the user profile
* @returns {string} Signed token for future use.
*/
getNewAuthToken = async userAddress => {
const unsignedToken = (await this._StoreProviderInstance.newAuthToken(
userAddress
)).unsignedToken

const signedToken = await this.signMessage(userAddress, unsignedToken)
// make sure token is valid
if (!await this.validateAuthToken(userAddress, signedToken))
throw new Error(UNABLE_TO_SIGN_TOKEN)

return signedToken
}

/**
* Sign a message with your private key. Uses web3 1.0 personal sign
* @param {string} userAddress - The address with which we want to sign the message
* @param {string} data - Hex encoded data to sign
* @returns {string} signed data
*/
signMessage = (userAddress, data) => {
const ethPersonal = new Personal(this._Web3Wrapper.getProvider())
return new Promise((resolve, reject) => {
ethPersonal.sign(data, userAddress, (error, result) => {
if (error) reject(error)

resolve(result)
})
})
}

/**
* Validate an auth token.
* @param {string} userAddress - The address of the user.
* @param {string} authToken - Token to check.
* @returns {Promise} resolves to True if token is valid.
*/
validateAuthToken = (userAddress, authToken) =>
this._StoreProviderInstance.isTokenValid(userAddress, authToken)
}

export default Auth
120 changes: 61 additions & 59 deletions src/resources/Disputes.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,37 +79,43 @@ class Disputes {
// There is no need to handle this event if we are not using the store
const disputeId = event.args._disputeID.toNumber()

const contractAddress = this._ArbitratorInstance.getContractAddress()
const existingDispute = await this._StoreProviderInstance.getDispute(
contractAddress,
disputeId
const arbitratorAddress = this._ArbitratorInstance.getContractAddress()
// arbitrator data
const disputeData = await this._ArbitratorInstance.getDispute(disputeId)
// arbitrable contract data
await this._ArbitrableInstance.setContractInstance(
disputeData.arbitrableContractAddress
)

// Add dispute to store if not there
if (_.isNull(existingDispute)) {
// arbitrator data
const disputeData = await this.getDataForDispute(disputeId, account)
// arbitrable contract data
await this._ArbitrableInstance.setContractInstance(
disputeData.arbitrableContractAddress
)
const arbitrableContractData = await this._ArbitrableInstance.getData()
const arbitrableContractData = await this._ArbitrableInstance.getData(
account
)
if (
account === arbitrableContractData.partyA ||
account === arbitrableContractData.partyB
) {
// timestamp
const blockTimestamp = (await this._ArbitratorInstance.getBlock(
event.blockNumber
)).timestamp

const appealCreatedAt = disputeData.appealCreatedAt
// Check if dispute has already been stored. This can happen if there was an appeal
const storedDispute = this._StoreProviderInstance.getDisputeDataForUser(
arbitrableContractData.partyA,
arbitratorAddress,
disputeId
)
const appealCreatedAt = storedDispute.appealCreatedAt || []
appealCreatedAt[disputeData.numberOfAppeals] = blockTimestamp * 1000

await this._StoreProviderInstance.updateDispute(
contractAddress,
await this._StoreProviderInstance.updateDisputeProfile(
account,
arbitratorAddress,
disputeId,
{
contractAddress: disputeData.arbitrableContractAddress,
partyA: arbitrableContractData.partyA,
partyB: arbitrableContractData.partyB,
status: disputeData.status,
appealCreatedAt
}
)
Expand Down Expand Up @@ -162,35 +168,32 @@ class Disputes {
const newPeriod = await this._ArbitratorInstance.getPeriod()
// send appeal possible notifications
if (newPeriod === arbitratorConstants.PERIOD.APPEAL) {
const disputes = await this._StoreProviderInstance.getDisputesForUser(
account
)
const disputes = await this._StoreProviderInstance.getDisputes(account)
const openDisputes = await this._ArbitratorInstance.getOpenDisputesForSession()
const contractAddress = this._ArbitratorInstance.getContractAddress()

await Promise.all(
openDisputes.map(async disputeId => {
if (
_.findIndex(
disputes,
dispute =>
dispute.disputeId === disputeId &&
dispute.arbitratorAddress === contractAddress
) >= 0
) {
openDisputes.map(async openDispute => {
const disputeIndex = _.findIndex(
disputes,
dispute =>
dispute.disputeId === openDispute.disputeId &&
dispute.arbitratorAddress === contractAddress
)
if (disputeIndex >= 0) {
// get ruledAt from block timestamp
const blockNumber = event.blockNumber
const blockTimestamp = (await this._ArbitratorInstance.getBlock(
blockNumber
)).timestamp

const disputeData = await this.getDataForDispute(disputeId, account)
const appealRuledAt = disputeData.appealRuledAt
appealRuledAt[disputeData.numberOfAppeals] = blockTimestamp * 1000
const appealRuledAt = disputes[disputeIndex].appealRuledAt || []
appealRuledAt[openDispute.numberOfAppeals] = blockTimestamp * 1000

this._StoreProviderInstance.updateDispute(
this._StoreProviderInstance.updateDisputeProfile(
account,
contractAddress,
disputeId,
openDispute.disputeId,
{
appealRuledAt
}
Expand All @@ -211,30 +214,27 @@ class Disputes {
const newPeriod = await this._ArbitratorInstance.getPeriod()
// send appeal possible notifications
if (newPeriod === arbitratorConstants.PERIOD.VOTE) {
const disputes = await this._StoreProviderInstance.getDisputesForUser(
account
)
const disputes = await this._StoreProviderInstance.getDisputes(account)
// contract data
const openDisputes = await this._ArbitratorInstance.getOpenDisputesForSession()
const contractAddress = this._ArbitratorInstance.getContractAddress()
await Promise.all(
openDisputes.map(async disputeId => {
if (
_.findIndex(
disputes,
dispute =>
dispute.disputeId === disputeId &&
dispute.arbitratorAddress === contractAddress
) >= 0
) {
openDisputes.map(async openDispute => {
const disputeIndex = _.findIndex(
disputes,
dispute =>
dispute.disputeId === openDispute.disputeId &&
dispute.arbitratorAddress === contractAddress
)
if (disputeIndex >= 0) {
const deadline = await this._ArbitratorInstance.getDeadlineForOpenDispute()
const disputeData = await this.getDataForDispute(disputeId, account)
const appealDeadlines = disputeData.appealDeadlines
appealDeadlines[disputeData.numberOfAppeals] = deadline
const appealDeadlines = disputes[disputeIndex].appealDeadlines || []
appealDeadlines[openDispute.numberOfAppeals] = deadline

this._StoreProviderInstance.updateDispute(
this._StoreProviderInstance.updateDisputeProfile(
account,
contractAddress,
disputeId,
openDispute.disputeId,
{
appealDeadlines
}
Expand Down Expand Up @@ -281,7 +281,7 @@ class Disputes {
arbitrableContractAddress
)
const [arbitrableContractData, evidence] = await Promise.all([
this._ArbitrableInstance.getData(),
this._ArbitrableInstance.getData(account),
this._ArbitrableInstance.getEvidenceForArbitrableContract(
arbitrableContractAddress
)
Expand All @@ -298,15 +298,17 @@ class Disputes {
let appealRuledAt = []
let netPNK = 0
try {
const userData = await this._StoreProviderInstance.getDisputeData(
const userData = await this._StoreProviderInstance.getDisputeDataForUser(
account,
arbitratorAddress,
disputeId,
account
disputeId
)
if (userData.appealDraws) appealDraws = userData.appealDraws
if (userData.appealCreatedAt) appealCreatedAt = userData.appealCreatedAt
if (userData.appealDeadlines) appealDeadlines = userData.appealDeadlines
if (userData.appealRuledAt) appealRuledAt = userData.appealRuledAt
if (userData.appealDraws) appealDraws = userData.appealDraws || []
if (userData.appealCreatedAt)
appealCreatedAt = userData.appealCreatedAt || []
if (userData.appealDeadlines)
appealDeadlines = userData.appealDeadlines || []
if (userData.appealRuledAt) appealRuledAt = userData.appealRuledAt || []
if (userData.netPNK) netPNK = userData.netPNK
// eslint-disable-next-line no-unused-vars
} catch (err) {
Expand Down
Loading