Skip to content

feat: Implement DLC Input Splicing for Reusing Funding Outputs#172

Merged
matthewjablack merged 8 commits into
masterfrom
feat/dlc-splicing-support
Jul 30, 2025
Merged

feat: Implement DLC Input Splicing for Reusing Funding Outputs#172
matthewjablack merged 8 commits into
masterfrom
feat/dlc-splicing-support

Conversation

@matthewjablack
Copy link
Copy Markdown
Contributor

@matthewjablack matthewjablack commented Jul 22, 2025

Summary

This PR implements DLC input splicing functionality, enabling DLC funding outputs to be reused as inputs in subsequent DLC transactions. This is essential for advanced DLC workflows like splicing, rolling contracts, and multi-party DLC chains.

Dependencies

This PR requires AtomicFinance/node-dlc#228 to be merged first, which adds the underlying DLC Input support for spliced contracts.

Key Features

DLC Input Splicing

  • Reuse DLC funding outputs: Previous DLC funding outputs can now be used as inputs in new DLC transactions
  • Mixed input support: Combine DLC inputs with regular wallet inputs in a single transaction
  • Proper multisig handling: Automatic detection and signing of 2-of-2 multisig DLC inputs

Explicit Input Control

  • InputSupplementationMode: Fine-grained control over input supplementation (None, Optional, Required)
  • Fixed input support: Use exact inputs without automatic supplementation when needed
  • Collateral calculation: Calculate maximum possible collateral with available inputs

Advanced Key Management

  • Dynamic key derivation: Automatically find private keys for DLC funding pubkeys across wallet addresses
  • Two-phase signing: Support offerer-first, acceptor-completion signing flow for DLC inputs
  • Cross-wallet compatibility: Handle DLC inputs created by different wallet instances

Enhanced Transaction Creation

  • Spliced transactions: Automatic detection and routing to CreateSplicedDlcTransactions when DLC inputs are present
  • Backward compatibility: Regular DLC creation unchanged when no DLC inputs are used
  • Proper witness construction: Correct P2WSH multisig witness handling for DLC inputs

Technical Implementation

Core Types

  • InputSupplementationMode enum for explicit input control
  • DlcInputInfo interface for DLC-specific input metadata
  • Enhanced Input class with isDlcInput() and DLC input support

Key Methods Added

  • calculateMaxCollateral() - Determine maximum funding with given inputs
  • createDlcFundingInput() - Create DLC inputs from funding transactions
  • findDlcFundingPrivateKey() - Derive keys across wallet addresses
  • GetInputsForAmountWithMode() - Input supplementation with explicit control

CFD-DLC-JS Integration

  • GetRawDlcFundingInputSignature - DLC-specific input signing
  • SignDlcFundingInput - Complete DLC multisig input signing
  • VerifyDlcFundingInputSignature - Verify DLC input signatures
  • CreateSplicedDlcTransactions - Handle mixed input types

Testing

  • Integration test covering full splicing workflow
  • End-to-end test: single-funded DLC → spliced DLC with mixed inputs

Use Cases Enabled

  • DLC Splicing: Add/remove collateral from existing DLCs
  • DLC Chains: Create sequences of dependent DLCs
  • Contract Rolling: Extend or modify existing DLC terms

Breaking Changes

None - all changes are backward compatible. Existing DLC creation flows work unchanged.

Migration Guide

Existing code requires no changes. New splicing functionality is opt-in through new parameters:

// Before: Regular DLC creation (unchanged)
const dlcOffer = await alice.dlc.createDlcOffer(contractInfo, collateral, feeRate, cetLocktime, refundLocktime);

// New: DLC splicing with explicit input control  
const dlcOffer = await alice.dlc.createDlcOffer(
  contractInfo, 
  collateral, 
  feeRate, 
  cetLocktime, 
  refundLocktime,
  [dlcFundingInput, regularInput], // Mix DLC and regular inputs
  InputSupplementationMode.None    // Explicit control
);

Test Results

  • All existing tests pass
  • New DLC splicing integration test passes
  • Proper handling of DLC input signing and verification
  • Mixed input types work correctly in single transaction

This implementation enables sophisticated DLC workflows while maintaining full backward compatibility with existing code.

Related Work

- add InputSupplementationMode enum for explicit input control
- add DlcInputInfo interface for splice transaction metadata
- add DLC-specific input creation and validation methods
- add client API method signatures for splicing operations
- extend Input class with isDlcInput() and DLC input support
- add GetInputsForAmountWithMode for explicit input supplementation control
- implement findDlcFundingPrivateKey to derive keys across wallet addresses
- enhance GetPrivKeysForInputs to handle DLC inputs via key derivation
- add spliced DLC transaction creation with mixed input type support
- implement DLC-specific signing: GetRawDlcFundingInputSignature, SignDlcFundingInput
- fix CreateFundingTx to sign clean transaction for proper multisig completion
- add calculateMaxCollateral and createDlcFundingInput client methods
- support FundingInput[] parameters in createDlcOffer and acceptDlcOffer
- preserve DLC metadata in fundingInputToInput/inputToFundingInput conversions
- handle two-phase DLC signing: offerer creates signature, acceptor completes
- add end-to-end test for single-funded to spliced DLC flow
- test DLC funding output reuse in subsequent DLC transactions
- add generateEnumCollateralContractInfo with proper outcome hashing
- verify DLC input signing, transaction creation, and execution
- test proper handling of mixed input types (DLC + regular inputs)
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jul 22, 2025

🦋 Changeset detected

Latest commit: aeb2027

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@atomicfinance/bitcoin-js-wallet-provider Patch
@atomicfinance/bitcoin-wallet-provider Patch
@atomicfinance/bitcoin-dlc-provider Patch
@atomicfinance/client Patch
@atomicfinance/types Patch
@atomicfinance/bitcoin-cfd-provider Patch
@atomicfinance/bitcoin-esplora-api-provider Patch
@atomicfinance/bitcoin-esplora-batch-api-provider Patch
@atomicfinance/bitcoin-node-wallet-provider Patch
@atomicfinance/bitcoin-rpc-provider Patch
@atomicfinance/bitcoin-utils Patch
@atomicfinance/crypto Patch
@atomicfinance/errors Patch
@atomicfinance/jsonrpc-provider Patch
@atomicfinance/node-provider Patch
@atomicfinance/provider Patch
@atomicfinance/utils Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

- node-dlc 1.1.0 includes support for splices contracts through dlcinput
- enhanced private key search range from 20 to 1000 addresses
- fixed UTXO to Input object conversion in funding methods
- used SignDlcFundingInput for proper DLC input signatures
- added derivation path validation and error handling
- fixed input supplementation mode for unusual addresses
- added dynamic pubkey ordering detection in splicing tests
@matthewjablack matthewjablack requested a review from bennyhodl July 27, 2025 22:41
Comment thread packages/bitcoin-dlc-provider/lib/BitcoinDlcProvider.ts Outdated
Comment thread packages/bitcoin-dlc-provider/lib/BitcoinDlcProvider.ts
Comment thread packages/bitcoin-dlc-provider/lib/BitcoinDlcProvider.ts Outdated
Comment thread packages/client/lib/Dlc.ts Outdated
Comment thread packages/types/lib/models/Input.ts Outdated
Comment thread tests/integration/dlc/dlc.test.ts
- replace duplicated private key finding logic with dedicated findDlcFundingPrivateKey method
- add contractId field to DlcInputInfo interfaces and related structures
- replace placeholder contract IDs with actual contract ID from hex
- update method signatures to include contractId parameter
- update tests to pass contract IDs for DLC input creation

Addresses review comments about code duplication, placeholder values, and missing contract ID handling
Replace inline type definition with proper DlcInputInfoRequest type
and handle resulting number|bigint type coercions
@matthewjablack matthewjablack merged commit 03c6974 into master Jul 30, 2025
2 checks passed
@matthewjablack matthewjablack deleted the feat/dlc-splicing-support branch July 30, 2025 20:30
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