Skip to content

unified htlc templates - #29

Merged
louisinger merged 1 commit into
masterfrom
feat/unified-htlc-claim-template
Dec 3, 2026
Merged

louisinger merged 1 commit into
masterfrom
feat/unified-htlc-claim-template

Conversation

@louisinger

Copy link
Copy Markdown
Collaborator

No description provided.

@coderabbitai

coderabbitai Bot commented Dec 3, 2026

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ecf48c60-9112-4762-bf60-be436e738bdb
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@louisinger
louisinger merged commit 0e80b8a into master Dec 3, 2026
7 checks passed
@louisinger
louisinger deleted the feat/unified-htlc-claim-template branch December 3, 2026 14:31

@arkana-ai-bot arkana-ai-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 source

For 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 from forbiddenPrefixes to 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 to ContextFree() and CheckExpression() match.
  • assetGroup refactoring (assets.go): the extracted assetInputs() function is a straight extraction with no logic change; assetGroup() retains the same in/out construction.
  • Covenant logic (templates/artifacts/htlc.json): payReceiver enforces output.value ≥ max(minClaimSat, inputValue − claimFeeCapSat), correct receiver program, the asset tunnel, and the per-asset minimum. nonInteractiveClaim requires 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 expression inputValue ≥ minClaimSat + claimFeeCapSat correctly guards the coin-after-fee floor, and minAssetAmount OP_1 OP_LESSTHAN OP_VERIFY refuses asset-required claims on-chain (documented in README, design intent).
  • Byte ordering for asset_txid: holds() reverses the display-order bytes to internal chainhash.Hash order; both OP_INSPECTINASSETLOOKUP and OP_INSPECTOUTASSETLOOKUP use the same popAssetID path, so the same variable works for both opcodes.
  • Daemon and e2e tests: TestDefaultHTLCClaimOfAnAsset / TestDefaultHTLCClaimSkipsACoinWithoutTheAsset exercise the asset path end-to-end through the daemon stand-in and confirm the template refuses wrongly-funded coins without disabling itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants