Rust-powered DLC (Discreet Log Contracts) bindings for JavaScript environments
This repository provides high-performance Rust bindings for the rust-dlc library, making DLC functionality available in:
- Node.js/TypeScript: @bennyblader/ddk-ts - NAPI-RS based native bindings
- React Native: @bennyblader/ddk-rn - UniFFI-based native bindings with JSI
@bennyblader/ddk-ts - Node.js/TypeScript
Native Node.js bindings using NAPI-RS for server-side applications, CLI tools, and desktop apps.
npm install @bennyblader/ddk-tsFeatures:
- Zero-copy data transfer via NAPI
- Prebuilt binaries for macOS ARM64 and Linux x64
- Full TypeScript support
- Synchronous API for performance
View package documentation β
@bennyblader/ddk-rn - React Native
React Native bindings using UniFFI for mobile DLC applications.
npm install @bennyblader/ddk-rnFeatures:
- JSI-based high-performance bridge
- iOS and Android support
- React Native 0.75+ with new architecture
- TurboModule optimizations
View package documentation β
Both packages expose the same API, ensuring complete compatibility across platforms. The API is generated from UniFFI definitions, guaranteeing consistency.
Returns the version of the DDK library.
const ddkVersion = version();
console.log(`DDK Version: ${ddkVersion}`);Creates a complete set of DLC transactions including funding, CETs, and refund.
createDlcTransactions(
outcomes: DlcOutcome[],
localParams: PartyParams,
remoteParams: PartyParams,
refundLocktime: number,
feeRate: bigint,
fundLockTime: number,
cetLockTime: number,
fundOutputSerialId: bigint
): DlcTransactionsCreates a 2-of-2 multisig locking script for the funding transaction.
createFundTxLockingScript(
localFundPubkey: Buffer,
remoteFundPubkey: Buffer
): BufferCreates Contract Execution Transactions for all possible outcomes.
createCets(
fundTxId: string,
fundVout: number,
localFinalScriptPubkey: Buffer,
remoteFinalScriptPubkey: Buffer,
outcomes: DlcOutcome[],
lockTime: number,
localSerialId: bigint,
remoteSerialId: bigint
): Transaction[]Creates a refund transaction with CSV timelock.
createRefundTransaction(
localFinalScriptPubkey: Buffer,
remoteFinalScriptPubkey: Buffer,
localAmount: bigint,
remoteAmount: bigint,
lockTime: number,
fundTxId: string,
fundVout: number
): TransactionSigns a funding transaction input.
signFundTransactionInput(
fundTransaction: Transaction,
privkey: Buffer,
prevTxId: string,
prevTxVout: number,
value: bigint
): TransactionVerifies a signature on a funding transaction.
verifyFundTxSignature(
fundTx: Transaction,
signature: Buffer,
pubkey: Buffer,
txid: string,
vout: number,
inputAmount: bigint
): booleanCreates adaptor signatures for oracle-based execution.
createCetAdaptorSignatureFromOracleInfo(
cet: Transaction,
oracleInfo: OracleInfo,
fundingSk: Buffer,
fundingScriptPubkey: Buffer,
totalCollateral: bigint,
msgs: Buffer[]
): AdaptorSignatureChecks if an output is below the dust threshold.
isDustOutput(output: TxOutput): booleanCalculates the virtual size of inputs for fee estimation.
getTotalInputVsize(inputs: TxInputInfo[]): numberCalculates change outputs and fees for a party.
getChangeOutputAndFees(
params: PartyParams,
feeRate: bigint
): ChangeOutputAndFeesinterface Transaction {
version: number;
lockTime: number;
inputs: TxInput[];
outputs: TxOutput[];
rawBytes: Buffer;
}
interface TxOutput {
value: bigint;
scriptPubkey: Buffer;
}
interface TxInput {
txid: string;
vout: number;
scriptSig: Buffer;
sequence: number;
witness: Buffer[];
}
interface TxInputInfo {
txid: string;
vout: number;
scriptSig: Buffer;
maxWitnessLength: number;
serialId: bigint;
}
interface DlcOutcome {
localPayout: bigint;
remotePayout: bigint;
}
interface PartyParams {
fundPubkey: Buffer;
changeScriptPubkey: Buffer;
changeSerialId: bigint;
payoutScriptPubkey: Buffer;
payoutSerialId: bigint;
inputs: TxInputInfo[];
inputAmount: bigint;
collateral: bigint;
dlcInputs: DlcInputInfo[];
}
interface DlcTransactions {
fund: Transaction;
cets: Transaction[];
refund: Transaction;
fundingScriptPubkey: Buffer;
}
interface OracleInfo {
publicKey: Buffer;
nonces: Buffer[];
}
interface AdaptorSignature {
signature: Buffer;
proof: Buffer;
}
interface ChangeOutputAndFees {
changeOutput: TxOutput;
fundFee: bigint;
cetFee: bigint;
}Both packages follow a pure wrapper approach around rust-dlc:
βββββββββββββββββββ ββββββββββββββββ βββββββββββββββ
β JavaScript β β Generated β β Rust β
β Application βββββΆβ Bindings βββββΆβ rust-dlc β
β β β (TS + FFI) β β (Core) β
βββββββββββββββββββ ββββββββββββββββ βββββββββββββββ
The packages maintain 100% API compatibility through:
- Shared UDL: Single UniFFI Definition Language file defines the interface
- Verification Scripts: Automated checks ensure parity between implementations
- Type Safety: Full TypeScript definitions generated from UDL
- Testing: Comprehensive test suites verify behavior consistency
View the compatibility verification script that ensures both packages expose identical APIs.
- Rust (latest stable)
- Node.js 18+
- Just (
cargo install just)
.
βββ ddk-ffi/ # Rust crate with UniFFI definitions
β βββ src/
β β βββ lib.rs # Rust implementation
β β βββ ddk_ffi.udl # UniFFI interface definitions
β βββ Cargo.toml
β
βββ ddk-ts/ # Node.js/TypeScript package
β βββ src-napi/ # NAPI-RS Rust source
β βββ src/ # Generated JS/TS
β βββ README.md
β
βββ ddk-rn/ # React Native package
β βββ src/ # Generated TypeScript
β βββ cpp/ # Generated C++ JSI bindings
β βββ ios/ # iOS native module
β βββ android/ # Android native module
β βββ README.md
β
βββ justfile # Build automation
# TypeScript/Node.js
just ts-build # Build for current platform
just ts-build-all # Build for all platforms
just ts-test # Run tests
just ts-release 0.2.0 # Release new version
# React Native
just uniffi # Generate all bindings
just build-ios # Build iOS
just build-android # Build Android
just release # Release new version
# Clean everything
just cleanMIT License - see LICENSE file for details.
Contributions welcome! Please ensure:
- All tests pass (
cargo test,pnpm test) - Bindings are regenerated when changing Rust code
- API compatibility is maintained
- Documentation is updated
- GitHub: https://github.com/bennyhodl/ddk-ffi
- rust-dlc: https://github.com/p2pderivatives/rust-dlc
- NAPI-RS: https://napi.rs
- UniFFI: https://mozilla.github.io/uniffi-rs/
Built with β€οΈ using rust-dlc