Skip to content

Commit

Permalink
add delegation check in batcher's address validator
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Nov 7, 2023
1 parent 477ba74 commit 2a9f066
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/batcher/address-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"dependencies": {
"pg": "^8.7.3",
"web3": "1.10.0",
"assert-never": "^1.2.1"
"assert-never": "^1.2.1",
"@dcspark/carp-client": "../../../tmp-carp-client"
},
"devDependencies": {
"@types/pg": "^8.6.5"
Expand Down
34 changes: 34 additions & 0 deletions packages/batcher/address-validator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { CryptoManager } from '@paima/crypto';
import { createMessageForBatcher } from '@paima/concise';
import { initWeb3, AddressType, getReadNamespaces } from '@paima/utils';
import assertNever from 'assert-never';
import { query, getErrorResponse } from '@dcspark/carp-client/client/src/index';
import { Routes } from '@dcspark/carp-client/shared/routes';

class PaimaAddressValidator {
private web3: Web3 | undefined;
Expand Down Expand Up @@ -59,6 +61,13 @@ class PaimaAddressValidator {
return GenericRejectionCode.ADDRESS_NOT_ALLOWED;
}

// If a list of allowed cardano pools is provided, check that this user is delegating to that address
const isDelegatingToAllowedPools = await this.isDelegatingToAllowedPools(input.userAddress);
if (!isDelegatingToAllowedPools) {
console.log('[address-validator] Address not allowed!');
return GenericRejectionCode.ADDRESS_NOT_ALLOWED;
}

// All tests passed:
return 0;
};
Expand Down Expand Up @@ -208,6 +217,31 @@ class PaimaAddressValidator {
): boolean {
return inputsMinute < ENV.MAX_USER_INPUTS_PER_MINUTE && inputsDay < ENV.MAX_USER_INPUTS_PER_DAY;
}

private isDelegatingToAllowedPools = async (userAddress: string): Promise<boolean> => {
const pools = ENV.BATCHER_CARDANO_ENABLED_POOLS;

if (!pools) {
return true;
}

if (!ENV.CARP_URL) {
throw new Error(`[address-validator] missing CARP_URL setting`);
}

const latest = await query(ENV.CARP_URL, Routes.blockLatest, {
offset: 0,
});

const delegatingTo = await query(ENV.CARP_URL, Routes.delegationForAddress, {
address: userAddress,
until: {
absoluteSlot: latest.block.slot,
},
});

return !!pools.find(pool => delegatingTo.pool === pool);
};
}

export default PaimaAddressValidator;
8 changes: 8 additions & 0 deletions packages/batcher/utils/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ export class ENV {
return parseInt(process.env.MAX_USER_INPUTS_PER_DAY || '0', 10);
}

static get CARP_URL(): string | undefined {
return process.env.BATCHER_CARP_URL;
}

static get BATCHER_CARDANO_ENABLED_POOLS(): string[] | undefined {
return process.env.BATCHER_CARDANO_ENABLED_POOLS?.split(',');
}

// NOTE: this variable is not currently used, with DEFAULT_VALIDATION_ACTIVE determining the type.
static get GAME_INPUT_VALIDATION_TYPE_NAME(): string {
return process.env.GAME_INPUT_VALIDATION_TYPE_NAME || '';
Expand Down

0 comments on commit 2a9f066

Please sign in to comment.