Repository navigation
fix(TBC777M): enforce the escrow audit invariant - #522
codenlighten wants to merge 2 commits into
Conversation
withdraw() rejected when the audited balance was positive and accepted when it was negative, so an escrow that authorized more than it received could pay out, and an escrow that kept a remainder could not pay out at all. Reject a negative balance instead, as TBC777 does. getBalance() now also subtracts final withdraws, using the existing computeFinalWithdraws helper, so several final claims can no longer together exceed the deposits. Claim amounts must be non-negative bigints, so a negative claim cannot offset a larger one in the totals. The constructor now defaults finalWithdrawn, without which withdrawFinal() always threw.
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The security invariant enforcement and accompanying regression tests look correct, with only a minor doc-comment wording fix suggested.
Review effort: Lite
Findings: 1
Open (1)
What changed in this PR
This PR hardens TBC777M’s on-chain escrow audit so withdrawals (regular and final) cannot inflate supply even if an escrow contract over-authorizes or behaves incorrectly, bringing TBC777M in line with TBC777’s existing audit invariant.
Changes:
- Fixes the escrow balance check in
withdraw()/withdrawFinal()to reject only when the audited balance is negative, and ensuresfinalWithdrawnis initialized. - Updates
getBalance()to subtract authorizedfinalWithdraws(counted conservatively from the supplied revision) and addsclaimAmounts()validation to reject negative / non-bigint claim amounts. - Adds an “escrow audit” test suite that exercises over-claims, negative-claim offsets, partial withdrawals, and final-withdraw behavior.
| File | Description |
|---|---|
| packages/TBC777/src/tbc777m.ts | Enforces non-negative audited escrow balance including final-withdraw authorizations; initializes finalWithdrawn; validates claim amount types/signs. |
| packages/TBC777/test/tbc777m.test.ts | Adds regression tests for malicious/buggy escrow authorization scenarios and indexing/confirmation timing. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if (this.finalWithdrawn.includes(rev)) throw new Error('Cannot withdraw multiple times') | ||
|
|
||
| const balance = await TBC777M.getBalance(rev, _root) | ||
| if (balance < 0n) throw new Error(`Escrow balance (${balance}) too low`) |
There was a problem hiding this comment.
Fixed in 41e0266, which also regenerates build/src/tbc777m.*.
|
Thank you for the report. The inverted withdraw check, the missing finalWithdrawn initialization, and final claims being left out of the balance are all real. We removed TBC777M in #530. It was the earlier token. Chess already uses TBC777, and nothing else in the repository imported TBC777M. TBC777 already enforces the same audit: it rejects a negative available balance, counts final claims, and refuses a non-positive claim. Closing this pull request because the contract is gone. |

Summary
TBC777M's header promises that a withdraw succeeds only if
totalDeposits >= totalWithdraws + totalFinalWithdraws,"even if the escrow is malicious or buggy". The audit didn't enforce it:
withdraw()had its check inverted.if (0 < balance) throwrejected every escrow that still held fundsand accepted every escrow that had authorized more than it received. On LTC regtest, a token that minted 3n
deposited 2n into an escrow that authorized 1000n. The claim succeeded, and the token held 1001n.
withdrawFinal()always threwTypeError: Cannot read properties of undefined (reading 'includes'),because the constructor never set
finalWithdrawn.computeFinalWithdrawsexisted but was never called, andwithdrawFinalcompared the balance with the caller's own final amount only. Two final claims of 2n each againsta 2n deposit both paid (3n minted, 5n afterwards).
This PR makes TBC777M check the same thing TBC777's
EscrowAuditoralready checks.Changes (
packages/TBC777/src/tbc777m.ts)finalWithdrawn: [].getBalance()subtractscomputeFinalWithdraws(states, root). Finals are counted from the claimed revisiononly, since they can only be paid from the last revision.
withdraw()andwithdrawFinal()both reject whenbalance < 0n.claimAmounts()helper, used bycomputeWithdrawsandcomputeFinalWithdraws, requires claim amounts tobe non-negative bigints, so a negative claim for a token that never withdraws can't offset a larger claim. Today
such a claim happens to fail anyway: in-contract
computer.syncreturns a negative bigint as a string (-998nbecomes
"-998n"), and the sum throwsCannot mix BigInt and other types. This makes the rejection explicit andkeeps it working if that changes.
finalWithdraws ?? []incomputeFinalWithdraws, so an escrow without the field still works for regularwithdraws.
getBalance()didn't read the field before.Tests (
packages/TBC777/test/tbc777m.test.ts, "escrow audit")stagingCannot mix BigIntTypeErrorEscrow balance (1) too lowwaitForIndexedtimeout in the node'sindexer, not an assertion.
npx turbo run types lintpasses: 33/33 tasks.The new tests wait for the node where the audit needs it. They call
waitForIndexedafter each write, as in#487, and before a withdraw they poll
getTXOs({ rev, isConfirmed: true })for the escrow revision, because thein-contract
prevneeds a confirmed revision andmine()returns before the node has indexed the block. Withoutthose waits the tests failed intermittently with
txn-mempool-conflictandRevision … not found by prev.Notes
build/is not rebuilt here, following howsrc/changes usually land. Packages importbuild/src/index.js,so the fix reaches chess-app, chess-contracts and commodity only after a rebuild. I'm happy to add it to this PR
if you prefer.
behaviour.
_rootfrom the running method #521 (fix/security-review) only changes the TBC20 import intbc777m.ts, and this patch applies cleanly on top of it.