Repository navigation
fix(TBC777): credit only the deposit recorded for the listed revision - #523
Conversation
computeDepositAmount credited a listed revision's whole balance drop whenever the next revision still had escrow set. deposit() sets escrow and only a regular withdraw clears it, so a later transfer or burn could be counted as a deposit, and so could a transfer made in the same transaction as the deposit. That lets an escrow pay out more than it received, on both TBC777 and Commodity. deposit() now records the revision it spends (depositFrom) and the amount (depositAmount). The auditor credits a listed revision only if the next revision's depositFrom is that revision, and at most the recorded amount. The constructor and transfer children drop these fields.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The new Commodity tests will likely run against stale @bitcoin-computer/TBC777 build output unless packages/TBC777/build/** is regenerated/committed or CI explicitly rebuilds that package first.
Review effort: Lite
Findings: 1
Open (2)
What changed in this PR
Fixes an inflation vulnerability in TBC777/Commodity escrow auditing where a later transfer/burn could be miscounted as a deposit if an escrow listed a post-deposit revision, by binding each credited deposit to the specific revision transition that performed deposit().
Changes:
- Track deposit provenance on-token via
depositFromanddepositAmount, and cap credited deposits accordingly. - Update escrow deposit auditing (
computeDepositAmount) to only credit deposits recorded for that exact transition and never more than the recorded deposited amount. - Add regression tests in both TBC777 and Commodity for “transfer/burn after deposit must not be credited as deposit”.
| File | Description |
|---|---|
| packages/TBC777/src/tbc777.ts | Adds depositFrom/depositAmount, omits them from transfer children, and tightens computeDepositAmount to prevent over-crediting. |
| packages/TBC777/test/tbc777.test.ts | Adds regression tests ensuring transfer/burn after deposit can’t be miscredited as a deposit. |
| packages/commodity/test/commodity.test.ts | Adds analogous regression tests for Commodity’s escrow-deposit behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| describe('escrow deposits', () => { | ||
| // An escrow that records any deposit revision it is given. | ||
| class ListEscrow extends Contract { | ||
| deposits!: [string, string][] | ||
| withdraws!: [string, string, bigint][] |
| protected _createTransferToken(to: string, amount: bigint): this { | ||
| const ctor = this.constructor as Constructor<this> | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const { _id, _root, _rev, _owners, withdrawn, finalWithdrawn, escrow, ...preserved } = this | ||
| /* eslint-disable @typescript-eslint/no-unused-vars */ |
There was a problem hiding this comment.
Fixed in 5dde2e5. The build strips comments, so build/ is unchanged.
… from transfer children
…eposit Transfer then deposit leaves escrow set and includes the transferred balance in the drop. The credit stays at the deposited amount for both TBC777 and Commodity.
ClemensLey
left a comment
There was a problem hiding this comment.
Thank you for tracking this down and for the fix. Recording the spent revision and the deposited amount, and crediting a listed revision only when it is that transition, is the right way to stop a transfer or burn from being counted as a deposit. That holds when the transfer happens in a later transaction and when it happens in the same transaction as the deposit.
I regenerated packages/TBC777/build, added a same-transaction transfer-then-deposit test for TBC777 and Commodity, and pushed both to this branch. I re-ran the Commodity and TBC777 suites. They pass, including the new test.
The same-transaction cases you described, where a deposit is combined with claim() or withdraw(), or where one transaction deposits into two escrows, can credit less than was deposited. That can lock funds. It does not create supply. I am fine leaving those out of this pull request. A deposit in one transaction and a withdraw in a later one still credits the deposit in full.


Summary
EscrowAuditorcounts a deposit as the drop inamountfrom a listed revision R to its next revision, provided thenext revision has
escrow === <this escrow>.deposit()setsescrow, and only a regularwithdrawclears it.The inherited
transfer()andburn()loweramountand leaveescrowset, and so do Commodity's overrides. Anescrow can therefore list a revision after the deposit, and a later transfer or burn is credited as a deposit.
Demonstrated on LTC regtest against unmodified
staging(4d89340):9n is transferred to another key. A claim for 10n is accepted, and the total supply becomes 19n.
accepted, so the supply roughly doubles.
burn()in place of the transfer, which undoes the burn.Repeating the steps creates arbitrary amounts.
tbc777.test.ts:747asserts that the sender keepsescrowafter atransfer, which is the state these steps rely on.
Why not just clear
escrowintransfer()/burn()We tried that first. It isn't enough: in one transaction a holder can call
transferand thendeposit. The nextrevision then has
escrow === E, and the balance drop includes the transfer. It also ran into a lib issue: in themodule-deployed TBC777, assigning
this.escrow = undefinedwhile it is alreadyundefinedfails deterministicallywith
mandatory-script-verify-flag-failed (Operation not valid with the current stack size). That broke theexisting test "fast path returns true for same-lineage tokens".
Changes (
packages/TBC777/src/tbc777.ts)The fix binds each deposit to the transition that made it:
deposit()recordsdepositFrom = this._rev(the revision being spent) anddepositAmount. Repeated depositsinto the same escrow from the same revision add up.
computeDepositAmountcredits a listed R only ifnext(R).depositFrom === R, and at mostmin(R.amount − next(R).amount, depositAmount), floored at 0.escrow,depositFromanddepositAmountfrom its arguments, and transfer children omitthem, so only
deposit()sets these fields.Commodity inherits
deposit()and the auditor, so it needs no change.transferandburnare unchanged.Tests
New tests, the same scenario in both packages ("does not count a transfer / burn after a deposit as a deposit"):
stagingExisting suites on LTC regtest, with this change:
txn-mempool-conflict,bad-txns-inputs-missingorspentandnot found by prev, from the node's indexing lag. All 3 passed in tworeruns, and "fast path returns true for same-lineage tokens" passes.
code ran, and passed in two reruns.
too-long-mempool-chain).Commodity and chess-contracts import TBC777 through its
build/, so runnpm run buildinpackages/TBC777before their tests. Without that, the two new Commodity tests run against the old auditor and fail. The runs above
used a
build/regenerated from this change, and it isn't part of this PR (see Notes).Not covered here (for your review)
Regression tests for the same-transaction cases (for example, a transfer and then a deposit in one
transaction). By design the auditor credits at most
depositAmount, which doesn't include the transfer, butno test in this PR checks it.
Under-credits in some unusual honest combinations within one transaction. I found no remaining way to
over-credit in the cases I worked through, but these cases credit less than was deposited:
escrow;Recording deposits per escrow would cover these, or the contract could disallow those combinations.
Which to do is your call.
Notes
Commodity tokens keep the old behaviour.
build/is not rebuilt here, so chess-contracts, chess-app and commodity pick up the fix only after a rebuild.I can add it to this PR if you'd like.
_rootfrom the running method #521 (fix/security-review) doesn't touch the auditor. This patch applies cleanly to it fortbc777.tsandtbc777.test.ts. The Commodity test file conflicts with fix(lib): replay reconciliation, author cache, and_rootfrom the running method #521's changes, and I'll rebase whichever lands second.Its re-enabled
merge()refuses bags withescrowset. A clean bag could still merge and then deposit in onetransaction, but the merge raises
amount, so the credit is capped by the net drop and bydepositAmount. Thatcan under-credit but not over-credit. This is reasoning only; no test covers it.
tbc777.test.ts. The two willconflict there, and I'll rebase whichever lands second.