Repository navigation
unified htlc templates - #29
Conversation
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
arkana-ai-bot
left a comment
There was a problem hiding this comment.
Review: unified htlc templates
Not protocol-critical (no forfeit/round/exit/signing-tree changes). New templates are additive; existing paths are unaffected. One correctness defect and two minor points follow.
Bug — latent slot-index mismatch in heldAssets() for Proof-type templates
pkg/template/internal/version/v1/draft.go:221-225
func (d *draft) heldAssets() (asset.Packet, error) {
sources := d.sources
if len(sources) > 0 && sources[0] == nil {
sources = sources[1:] // ← re-indexes from 0
}
// …
ins, err := assetInputs(sources, id) // slot 0 = first real sourceFor model.Proof templates, newDraft() prepends a nil entry for the message input, so d.sources = [nil, src0, …]. After the strip, assetInputs() assigns Vin=0 to src0. But eval() is called with d.index(slot) which returns slot+1 for Proof types, so OP_PUSHCURRENTINPUTINDEX returns 1 inside the VM. The opcode OP_INSPECTINASSETLOOKUP then searches for i=1 in the packet, finds only Vin=0, and returns 0 — a silent false-ineligibility for any Proof template that uses OP_INSPECTINASSET* in a locking expression.
Currently not exploitable: every template that now uses these opcodes in a locking expression is model.Virtual (no nil prepended by newDraft). The htlc.json template is Virtual, so the ship is not on fire. But the design is a trap: a future Proof-type template with an asset-aware locking expression would get wrong results with no error, only coins silently not claimed.
Root cause: assetInputs() panics on nil sources (s.Assets on a nil pointer), so the nil had to be removed. The clean fix is to guard nil inside assetInputs() and not strip before calling, so slot numbers stay aligned with transaction indices:
for slot, s := range sources {
if s == nil { // message input holds no assets
continue
}
for _, as := range s.Assets {Then pass d.sources (unstripped) to assetInputs() from heldAssets(). Slots 0…N then match the tx input indices, and both Virtual (input = slot) and Proof (input = slot + 1) cases stay correct.
Minor — test helper masks byte-order coverage
pkg/template/internal/version/v1/draft_test.go:279-283
TestEvalReadsHeldAssets uses assetID(9) (a palindromic byte sequence of all 9s) in both the lookup expression and model.Asset.ID. Because the first 32 bytes are uniform, reversing them is a no-op, and byte-order inconsistencies between OP_INSPECTINASSETLOOKUP's expected format and asset.NewAssetIdFromString's internal representation would not be caught. TestHTLCCovenant does use a non-symmetric byte sequence (bytes 1…34) and explicitly reverses to match deserializeTxHash's reversal, so the byte-order logic is covered there — but TestEvalReadsHeldAssets should use a non-palindromic asset ID for the same confidence at the unit level.
Minor — silent default change for packets.unmatched
templates/htlc.json line 51 (diff)
- "packets": {"output_index": 1, "unmatched": "drop"},
+ "packets": {"output_index": 1},The removal of "unmatched": "drop" changes how surplus input packets are handled and is necessary to pass assets through. A one-line comment in the template or a commit note explaining the intent would help future readers distinguish "intentionally omitted" from "accidentally forgotten." Not blocking, but documentation is cheap here.
What looks correct
OP_INSPECTINASSET*unblocking (pkg/template/internal/asm/expr.go): moving these opcodes fromforbiddenPrefixesto the non-context-free set is correct. Watch instantiation is context-free and therefore blocked; coin-level evaluation (non-context-free) is allowed. The test updates toContextFree()andCheckExpression()match.assetGrouprefactoring (assets.go): the extractedassetInputs()function is a straight extraction with no logic change;assetGroup()retains the same in/out construction.- Covenant logic (
templates/artifacts/htlc.json):payReceiverenforcesoutput.value ≥ max(minClaimSat, inputValue − claimFeeCapSat), correct receiver program, the asset tunnel, and the per-asset minimum.nonInteractiveClaimrequires both preimage hash and operator sig (tweaked with the covenant). No path allows bypassing the hash check or redirecting funds. - Onchain eligibility filter (
templates/htlc_onchain.json): the locking expressioninputValue ≥ minClaimSat + claimFeeCapSatcorrectly guards the coin-after-fee floor, andminAssetAmount OP_1 OP_LESSTHAN OP_VERIFYrefuses asset-required claims on-chain (documented in README, design intent). - Byte ordering for
asset_txid:holds()reverses the display-order bytes to internalchainhash.Hashorder; bothOP_INSPECTINASSETLOOKUPandOP_INSPECTOUTASSETLOOKUPuse the samepopAssetIDpath, so the same variable works for both opcodes. - Daemon and e2e tests:
TestDefaultHTLCClaimOfAnAsset/TestDefaultHTLCClaimSkipsACoinWithoutTheAssetexercise the asset path end-to-end through the daemon stand-in and confirm the template refuses wrongly-funded coins without disabling itself.
No description provided.