feat(cross-chain): DGB-side HTLC primitives for the atomic-swap rail
Extends services/correspondent/dgb.mjs with the DGB-mainnet P2SH-HTLC
primitives the atomic-swap rail (snapshot 0271) needs. Complements
snapshot 0270's BCH-side covenant triangle (WithdrawalHTLC.cash +
DepositHTLC.cash + Deposit.burn) — same shared preimage on both sides,
so a reveal on either chain settles both.
Redeem script (Bitcoin-lineage HTLC — works on DGB Script without
Taproot):
OP_IF
OP_SHA256 <hashLock> OP_EQUALVERIFY <counterpartyPk> OP_CHECKSIG
OP_ELSE
<refundLockTime> OP_CHECKLOCKTIMEVERIFY OP_DROP <ownerPk> OP_CHECKSIG
OP_ENDIF
New exports (all in dgb.mjs, appended after the bonded-operator rail):
- buildHtlcRedeemScript({ hashLock, counterpartyPubkey, ownerPubkey,
refundLockTime }) → Buffer. 32-byte sha256 hashLock, 33-byte
compressed pubkeys, positive-int CLTV. Rejects malformed inputs.
- htlcAddress(redeemScript, network) → string. DGB P2SH address
(S… prefix; scriptHash 0x3f).
- buildFundHtlcTx({ funder, fromAddress, htlcAddress, amountSats,
… }) → { hex, htlcTxid, htlcVout, htlcAddress, sats, fee }.
Delegates to buildLockTx for the UTXO selection + signing logic;
pins HTLC output at vout 0.
- verifyHtlc({ txid, vout, redeemScript, expectedSats, api }) →
{ ok, confirmations, blockHeight, actualSats, error? }. Verifies
the funded HTLC on-chain via digiexplorer.info; checks output
script matches the P2SH of the redeem script and value matches
expected.
- buildClaimHtlcTx({ htlcTxid, htlcVout, htlcSats, redeemScript,
preimage, claimer, toAddress, feeSatsPerVByte }) →
{ hex, txid, sats, fee }. scriptSig = <sig> <preimage> OP_1
<redeemScript>. Uses SIGHASH_ALL over the redeem script for the
legacy P2SH sighash.
- buildRefundHtlcTx({ htlcTxid, htlcVout, htlcSats, redeemScript,
refundLockTime, owner, toAddress, feeSatsPerVByte }) →
{ hex, txid, sats, fee }. scriptSig = <sig> OP_0 <redeemScript>.
tx.nLockTime = refundLockTime, input sequence = 0xfffffffe so
CLTV is enforced.
Fee defaults are 200 sat/vB (matches the bonded-operator rail default;
DGB min-relay-fee is ~10× BTC's). Fee > HTLC value throws.
test/regtest/dgb-htlc.test.ts — 7/7 passing:
1. Redeem script disassembly matches expected structure (accepts
both OP_CHECKLOCKTIMEVERIFY and its pre-BIP65 name OP_NOP2 — same
opcode; bitcoinjs-lib prints the legacy label).
2. buildHtlcRedeemScript rejects malformed hashLock / pubkey / CLTV.
3. htlcAddress derives a valid DGB P2SH address (S… prefix) that
round-trips through bitcoin.address.toOutputScript.
4. Claim tx scriptSig has 4 chunks: sig, preimage, OP_1,
redeemScript.
5. Refund tx has nLockTime == refundLockTime and sequence 0xfffffffe,
scriptSig has 3 chunks: sig, OP_0, redeemScript.
6. buildClaimHtlcTx throws when fee > HTLC value.
7. sha256(preimage) matches the hashLock embedded at bytes 3..34 of
the redeem script.
Mainnet-live testing (broadcast + mempool acceptance + chipnet-DGB
testnet dry-run of the full swap) is the next lift — snapshot 0272.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>