@atomicfinance/bitcoin-ddk-provider
v4.2.7
Published
Bitcoin Abstraction Layer Ddk Provider
Readme
Bitcoin DDK Provider
A Bitcoin provider that implements DLC (Discreet Log Contract) functionality using the DDK (Discreet Log Contract Development Kit) interface.
Features
- Interface-based design: Uses a
DdkInterfacethat allows different DDK implementations to be injected - Flexible: Can work with
ddk-ts,ddk-rn, or any custom DDK implementation - Type-safe: Full TypeScript support with proper type definitions
- Testable: Easy to mock for unit testing
Installation
npm install @atomicfinance/bitcoin-ddk-providerUsage
Basic Usage with ddk-ts
import BitcoinDdkProvider from '@atomicfinance/bitcoin-ddk-provider';
import * as ddkTs from '@bennyblader/ddk-ts';
import { BitcoinNetwork } from 'bitcoin-network';
// Create provider with ddk-ts implementation
const network = BitcoinNetwork.MAINNET;
const provider = new BitcoinDdkProvider(network, ddkTs);
// Use the provider
const version = await provider.getVersion();
console.log(`DDK Version: ${version}`);Usage with ddk-rn
import BitcoinDdkProvider from '@atomicfinance/bitcoin-ddk-provider';
import * as ddkRn from '@bennyblader/ddk-rn';
import { BitcoinNetwork } from 'bitcoin-network';
// Create provider with ddk-rn implementation
const network = BitcoinNetwork.MAINNET;
const provider = new BitcoinDdkProvider(network, ddkRn);Custom DDK Implementation
import BitcoinDdkProvider from '@atomicfinance/bitcoin-ddk-provider';
import { DdkInterface, DlcOutcome, PartyParams, DlcTransactions } from '@atomicfinance/types';
// Create your own DDK implementation
class MyCustomDdk implements DdkInterface {
createDlcTransactions(
outcomes: DlcOutcome[],
localParams: PartyParams,
remoteParams: PartyParams,
refundLocktime: number,
feeRate: bigint,
fundLockTime: number,
cetLockTime: number,
fundOutputSerialId: bigint,
): DlcTransactions {
// Your custom implementation
return {
fund: { /* ... */ },
cets: [],
refund: { /* ... */ },
fundingScriptPubkey: Buffer.alloc(0),
};
}
// Implement all other required methods...
createCet(/* ... */) { /* ... */ }
createCets(/* ... */) { /* ... */ }
// ... etc
}
// Use your custom implementation
const network = BitcoinNetwork.MAINNET;
const customDdk = new MyCustomDdk();
const provider = new BitcoinDdkProvider(network, customDdk);Testing with Mock Implementation
import BitcoinDdkProvider from '@atomicfinance/bitcoin-ddk-provider';
import { DdkInterface } from '@atomicfinance/types';
// Create a mock for testing
const mockDdk: DdkInterface = {
createDlcTransactions: jest.fn().mockResolvedValue(/* mock data */),
createFundTxLockingScript: jest.fn().mockReturnValue(Buffer.alloc(0)),
// ... implement other methods as needed
};
const provider = new BitcoinDdkProvider(BitcoinNetwork.TESTNET, mockDdk);API Reference
Constructor
constructor(network: BitcoinNetwork, ddkLib: DdkInterface)network: The Bitcoin network to use (mainnet, testnet, etc.)ddkLib: A DDK implementation that conforms to theDdkInterface
Methods
The provider implements all the standard DLC methods:
createDlcTransactions()- Create DLC transactionscreateFundTx()- Create funding transactioncreateCet()- Create CET (Contract Execution Transaction)createCets()- Create multiple CETscreateRefundTransaction()- Create refund transactioncreateCetAdaptorSignature()- Create CET adaptor signaturesignFundTransactionInput()- Sign funding transaction inputverifyFundTxSignature()- Verify funding transaction signaturegetVersion()- Get DDK versiongetChangeOutputAndFees()- Calculate change output and feesgetTotalInputVsize()- Get total input vsizeisDustOutput()- Check if output is dustcreateSplicedDlcTransactions()- Create spliced DLC transactions
Benefits of Interface-Based Design
- Dependency Injection: Easy to swap implementations
- Testing: Simple to mock for unit tests
- Flexibility: Support multiple DDK libraries
- Maintainability: Clear contract for what any DDK implementation must provide
- Future-Proof: Easy to add new DDK implementations or remove dependencies
Type Safety
All methods are fully typed using TypeScript interfaces from @atomicfinance/types. The provider ensures type safety while maintaining flexibility through the interface-based design.
