ERC-3135 · Stagnant · ERC · 2020-08-10
ERC-3135: Exclusive Claimable Token
An ERC-20 extension for off-chain micropayments: users lock tokens in a deposit, sign running consumption totals, and only the token's issuer can claim them on-chain.
What problem does it solve?
Paying a small business in tokens one transaction at a time is slow and costs gas on every purchase. Payment channels fix that by moving most of the interaction off-chain, but each merchant would normally need its own channel contract and dapp, which small businesses rarely have the resources to build, and every implementation would be different.
ERC-3135 puts a simple one-to-many payment channel directly into the token contract. Any wallet that understands the standard could offer a common "pay with signature" UI, and the merchant only needs a server that verifies signatures.
How it works
- The token has a single issuer (the service provider). Only the issuer can call
claim. - A user calls
deposit(amount)to move tokens from their normal balance into a deposit balance, which has an epoch counter. - For each purchase, the user's client signs a message containing the token address, payer, issuer, the cumulative consumption for the current epoch, and the epoch number. The merchant verifies it off-chain and keeps serving while the signed amount covers what the user has used (with an optional tolerance).
- When it wants to settle, the issuer calls
claim(from, consumption, epoch, signature). The contract checks the signature and deposit, movesconsumptionto the issuer, and increments the epoch so old signatures cannot be reused. withdraw(to, amount)returns unused deposit. The spec describes two models:- prepayment (like a store gift card): the issuer calls
withdrawto refund; - lock-release (pay-per-use services): the payer can withdraw, for example after the epoch has not changed for a set time, so funds are not held hostage by a misbehaving server.
- prepayment (like a store gift card): the issuer calls
Events: Deposit, Withdraw, Claim, TransferIssuer. Other views: issuer(), iconUrl(), depositBalanceOf(user).
Sample code
Building the payment message off-chain, following the spec's hashing (ethers v6):
import { AbiCoder, keccak256, getBytes, Wallet } from "ethers";
const coder = AbiCoder.defaultAbiCoder();
const inner = keccak256(coder.encode(
["address", "address", "address", "uint256", "uint256"],
[token, payer, issuer, cumulativeConsumption, epoch],
));
// The spec's reference implementation then hashes
// abi.encode("\x19Ethereum Signed Message:\n32", inner) and recovers with ecrecover.
// Match whatever the deployed contract actually does before signing.
The on-chain interface additions:
function issuer() external view returns (address);
function deposit(uint256 amount) external;
function withdraw(address to, uint256 amount) external;
function claim(address from, uint256 consumption, uint256 epoch, bytes calldata signature) external;
function depositBalanceOf(address user) external view returns (uint256 depositBalance, uint256 epoch);
Things to know
- Status: Stagnant.
- The reference implementation prefixes with
abi.encoderather than the usualabi.encodePacked, so its digest does not match the standard EIP-191personal_signhash. A wallet's normalsignMessagewill not produce a valid signature for it unchanged. A modern design would use EIP-712 typed data instead. - If the merchant runs several verification servers, a payer can sign competing messages to different servers. Only the highest consumption can be claimed, so the spec suggests an "echo server" that sequences messages.
- Payments are only final once the issuer claims on-chain; until then the merchant carries the risk up to its tolerance.