@bitsocial/evm-contract-challenge
v0.3.0
Published
Standalone EVM contract call challenge for pkc-js
Readme
@bitsocial/evm-contract-challenge
An automatic challenge for @pkcprotocol/pkc-js communities that verifies an author's EVM wallet address meets a condition from a smart contract call.
How it works
When an author publishes to a community with this challenge enabled, the community node calls a read-only smart contract method with the author's wallet address as the argument and compares the return value against a configured condition (e.g. >1000). The challenge tries three sources for the wallet address:
- Wallet address — the
author.wallets[chainTicker]address, verified with a BSIP-22 wallet proof (see Wallet proofs) - ENS/BSO domain — if the author's address is a
.ethor.bsodomain, it resolves to an on-chain address - NFT avatar — currently disabled. The avatar proof is signed over
author.addresswith a legacyplebbit-separator, and there is no BSIP for avatar proofs yet (BSIP-22 covers wallets only). The code stays in the repository and will be re-enabled once a BSIP defines the proof (BSIP discussion, tracking issue).
If any source produces a wallet that passes the contract call condition, the challenge succeeds. No user interaction is required.
Wallet proofs
The wallet source follows BSIP-22, Author Wallet Proofs. The wallet signs this exact string with EIP-191 (personal_sign), and signature.type is eip191:
{"domainSeparator":"bitsocial-author-wallet","authorPublicKey":"<key address>","walletAddress":"<wallet address>","timestamp":<timestamp>}authorPublicKeyis the key address (12D3KooW…) of the author's signing key, which pkc-js exposes asauthor.publicKey. Do not useauthor.address. It isauthor.name || publicKey, so it follows a name when the name is repointed to another key.walletAddressis the wallet entry'saddressin lowercase.timestampis the entry'stimestamp, an unquoted integer. It may be at most 300 seconds ahead of the community node's clock.- The field order is fixed and there is no whitespace. Build the string by concatenation, not with a serializer whose key order you have not confirmed.
Verification is stateless and needs no RPC: there is no "newest timestamp wins" and nothing is stored per wallet or per author. A signature with v other than 27 or 28, or with s in the upper half of the curve order, is rejected. A wallet entry with a signature.type other than eip191 is unproven and does not satisfy this challenge, but it does not reject the publication: the ENS source can still pass.
A publication whose author.wallets contains an invalid entry, for any chain, is rejected outright, as BSIP-22 requires of a community. It does not fall through to the ENS source, and the reason names the chain ticker of the failing entry. A proof counts only when the signature of the publication that carries it is valid too, which pkc-js checks before the challenge runs. The address of an eip191 entry must be 0x followed by 40 hex digits, not a name. An entry of another type is unproven before its address is looked at, so it may use another chain's address format. Every entry, whatever its type, must still have the shape { address: string, timestamp: integer, signature: { signature: string, type: string } }, or it is invalid.
Proofs signed with the old plebbit-author-wallet or pkc-author-wallet separators, over author.address, or without walletAddress are not accepted. Authors re-sign.
One wallet can still back several author keys. Whether to limit that is a separate question for this challenge, not a part of wallet-proof validity (#12).
Requirements
- Node.js
>=22 - ESM-only environment
@pkcprotocol/pkc-js>=0.0.165(a peer dependency: the challenge logs through thelogit passes)
Install
With bitsocial-cli
bitsocial challenge install @bitsocial/evm-contract-challengeEdit your community to use the challenge:
bitsocial community edit your-community.bso \
'--settings.challenges[0].name' @bitsocial/evm-contract-challenge \
'--settings.challenges[0].options.chainTicker' eth \
'--settings.challenges[0].options.address' '0xEA81DaB2e0EcBc6B5c4172DE4c22B6Ef6E55Bd8f' \
'--settings.challenges[0].options.abi' '{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}' \
'--settings.challenges[0].options.condition' '>10000000000000000000' \
'--settings.challenges[0].options.error' 'You need at least 10 Bitsocial tokens to post.'With pkc-js over RPC
If your RPC server is already running, first install the challenge on the server:
bitsocial challenge install @bitsocial/evm-contract-challengeThen from your RPC client, connect and set the challenge on your community by name — no npm install or challenge registration needed on the client side:
import PKC from "@pkcprotocol/pkc-js";
const pkc = await PKC({
pkcRpcClientsOptions: ["ws://localhost:9138"]
});
const community = await pkc.createCommunity({ address: "your-community-address.bso" });
await community.edit({
settings: {
challenges: [
{
name: "@bitsocial/evm-contract-challenge",
options: {
chainTicker: "eth",
address: "0xEA81DaB2e0EcBc6B5c4172DE4c22B6Ef6E55Bd8f",
abi: '{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}',
condition: ">10000000000000000000",
error: "You need at least 10 Bitsocial tokens to post."
}
}
]
}
});With pkc-js (TypeScript)
If you are running your own node locally without connecting over RPC, you can install via npm and register the challenge manually:
npm install @bitsocial/evm-contract-challengeimport PKC from "@pkcprotocol/pkc-js";
import { evmContractChallenge } from "@bitsocial/evm-contract-challenge";
PKC.challenges["@bitsocial/evm-contract-challenge"] = evmContractChallenge;Then set the challenge on your community:
await community.edit({
settings: {
challenges: [
{
name: "@bitsocial/evm-contract-challenge",
options: {
chainTicker: "eth",
address: "0xEA81DaB2e0EcBc6B5c4172DE4c22B6Ef6E55Bd8f",
abi: '{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}',
condition: ">10000000000000000000",
error: "You need at least 10 Bitsocial tokens to post."
}
}
]
}
});Example Challenges
Each example uses a read-only contract function that takes a single address argument. The condition compares against the raw return value including decimal places (e.g. 10 USDC with 6 decimals = 10000000 raw).
Common ABIs
balanceOf — standard ERC-20 / ERC-721 token balance:
{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}getScore — Gitcoin Passport score (returns uint256 with 4 decimals):
{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"getScore","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}Examples
| Description | chainTicker | address | ABI | condition |
|---|---|---|---|---|
| At least 10 Bitsocial (BSO) tokens | eth | 0xEA81DaB2e0EcBc6B5c4172DE4c22B6Ef6E55Bd8f | balanceOf | >10000000000000000000 |
| Minimum 10 USDC | eth | 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 | balanceOf | >10000000 |
| Any WETH balance | eth | 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 | balanceOf | >0 |
| Gitcoin Passport score above 20 (proof of personhood) | op | 0xd6c51bB9E23bD7f1fEa22A3F2f85E3BFC8338Cb0 | getScore | >200000 |
| At least 10 MATIC on Polygon | matic | 0x0000000000000000000000000000000000001010 | balanceOf | >10000000000000000000 |
| Any stETH balance (Lido staked ETH) | eth | 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84 | balanceOf | >0 |
Setting
rpcUrlsis recommended on every chain, including Ethereum mainnet. See Challenge Options.
Challenge Options
All option values must be strings.
| Option | Required | Default | Description |
|--------|----------|---------|-------------|
| chainTicker | yes | "eth" | The chain ticker (e.g. eth, matic) |
| rpcUrls | see below | — | Comma-separated JSON-RPC URLs for the chain |
| address | yes | — | The contract address to call |
| abi | yes | — | The ABI of the contract method as a JSON object (not an array) |
| condition | yes | — | Condition the return value must pass (=, >, or < followed by a value, e.g. >1000) |
| error | no | "Contract call response doesn't pass condition." | Custom error message shown when the condition fails |
When rpcUrls is required
rpcUrls is required unless chainTicker is one of the tickers below, which have a built-in RPC endpoint:
eth, matic / pol, op, arb, base, avax, bnb / bsc, gno / xdai, celo, ftm, linea, scroll, zksync, blast
Setting it is still recommended even for those: the built-in endpoints are shared public RPCs and will rate-limit a busy community, which shows up as authors being rejected at random rather than as an obvious failure.
An unsupported ticker with no rpcUrls is rejected when you save the challenge settings, rather than failing later for every author who tries to publish.
Publishing Options
community.settings.challenges[i].options is private. Nothing in it is published unless you name the option in publicOptions, which copies it into the public community.challenges[i].publicOptions record.
Recommended: publish everything except rpcUrls.
await community.edit({
settings: {
challenges: [
{
name: "@bitsocial/evm-contract-challenge",
options: {
/* ... */
},
publicOptions: ["chainTicker", "address", "abi", "condition", "error"]
}
]
}
});| Option | Publish? | Why |
|--------|----------|-----|
| chainTicker | recommended | Already public in effect — the challenge's type is chain/<chainTicker> |
| address | recommended | A contract address is public on-chain data; hiding it buys no security |
| abi | recommended | The signature of a public method on that contract |
| condition | recommended | The threshold that makes the other three actionable |
| error | recommended | Authors see this string anyway when they fail |
| rpcUrls | never — refused | RPC URLs commonly embed a provider API key |
Published together, chainTicker + address + abi + condition let a client tell an author exactly what they are missing before they publish and burn a challenge attempt. Published partially, none of that works, so treat the four as one unit.
Keeping them private is a legitimate choice — you may not want the exact threshold known — and this package will not stop you. rpcUrls is the one exception: naming it in publicOptions is refused outright, because publishing a URL like https://eth-mainnet.g.alchemy.com/v2/<KEY> leaks a paid credential to everyone, permanently, and no client ever uses these endpoints. Only the community node does.
Settings Validation
The challenge validates its own settings whenever you create a community, edit one, or start one, via pkc-js's validateChallengeSettings hook. A bad setting is rejected as a failed edit instead of silently rejecting every author at publish time.
Rejected settings:
publicOptionsnamingrpcUrlsrpcUrlsentries that are not valid URLs, or that do not usehttp:/https:rpcUrlsomitted for achainTickerwith no built-in RPCaddressthat is not a well-formed EVM addressabithat is not valid JSON, or not a single function entry taking oneaddressand returning at least one valueconditionwith no supported operator (=,>,<), no value after the operator, or an ordering comparison against a non-numeric value (e.g.>abc, which would otherwise compare as text)
pkc-js additionally rejects an option name that no option above declares, and any required option left unset, before this challenge's own validation runs.
Logging
The challenge writes only to the log pkc-js passes it, which pkc-js tags with the community's publicKey, the publication's signature and the challengeIndex. Lines are returned by pkc.getLogs and the getLogs RPC method, so an owner can look up why a publication was rejected.
log.error: a failed contract call, a failed ENS lookup, and rejected challenge settings.log.trace: an invalid or unproven wallet proof, and the final rejection with every source's reason. Shown only whenDEBUGenables trace.
A publication that passes logs nothing. Every client of the node's RPC server can read these lines, so they carry addresses and reasons only: no publication content, no option values other than the contract address, the chain and, in a settings rejection, the rejected value, and never rpcUrls. Errors are cut to their first line and any configured RPC URL left in them is masked as <rpcUrl>.
Multiple RPC URLs
You can provide multiple RPC endpoints as a comma-separated string:
https://eth.llamarpc.com,https://rpc.ankr.com/eth,https://eth.drpc.orgWhen multiple URLs are provided, viem's fallback transport is used with automatic ranking enabled (rank: true). This means:
- Requests are sent to the highest-ranked RPC endpoint
- If a request fails, it automatically falls back to the next endpoint
- viem periodically pings all endpoints in the background and reorders them by latency and stability
- A single URL works the same as before (no fallback overhead)
- If
rpcUrlsis omitted, the chain's built-in RPC is used, which requires a supportedchainTicker(see WhenrpcUrlsis required)
This improves reliability — if one RPC provider goes down, the challenge automatically uses the next available endpoint.
Scripts
npm run typecheck
npm run build
npm test