Skip to content

bennyhodl/ddk-ffi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

DLC Dev Kit FFI Bindings

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:

GitHub

πŸ“¦ Packages

@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-ts

Features:

  • 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-rn

Features:

  • JSI-based high-performance bridge
  • iOS and Android support
  • React Native 0.75+ with new architecture
  • TurboModule optimizations

View package documentation β†’

🎯 API Reference

Both packages expose the same API, ensuring complete compatibility across platforms. The API is generated from UniFFI definitions, guaranteeing consistency.

Core Functions

version(): string

Returns the version of the DDK library.

const ddkVersion = version();
console.log(`DDK Version: ${ddkVersion}`);

Transaction Creation

createDlcTransactions()

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
): DlcTransactions

createFundTxLockingScript()

Creates a 2-of-2 multisig locking script for the funding transaction.

createFundTxLockingScript(
  localFundPubkey: Buffer,
  remoteFundPubkey: Buffer
): Buffer

createCets()

Creates 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[]

createRefundTransaction()

Creates a refund transaction with CSV timelock.

createRefundTransaction(
  localFinalScriptPubkey: Buffer,
  remoteFinalScriptPubkey: Buffer,
  localAmount: bigint,
  remoteAmount: bigint,
  lockTime: number,
  fundTxId: string,
  fundVout: number
): Transaction

Signing & Verification

signFundTransactionInput()

Signs a funding transaction input.

signFundTransactionInput(
  fundTransaction: Transaction,
  privkey: Buffer,
  prevTxId: string,
  prevTxVout: number,
  value: bigint
): Transaction

verifyFundTxSignature()

Verifies a signature on a funding transaction.

verifyFundTxSignature(
  fundTx: Transaction,
  signature: Buffer,
  pubkey: Buffer,
  txid: string,
  vout: number,
  inputAmount: bigint
): boolean

createCetAdaptorSignatureFromOracleInfo()

Creates adaptor signatures for oracle-based execution.

createCetAdaptorSignatureFromOracleInfo(
  cet: Transaction,
  oracleInfo: OracleInfo,
  fundingSk: Buffer,
  fundingScriptPubkey: Buffer,
  totalCollateral: bigint,
  msgs: Buffer[]
): AdaptorSignature

Utility Functions

isDustOutput()

Checks if an output is below the dust threshold.

isDustOutput(output: TxOutput): boolean

getTotalInputVsize()

Calculates the virtual size of inputs for fee estimation.

getTotalInputVsize(inputs: TxInputInfo[]): number

getChangeOutputAndFees()

Calculates change outputs and fees for a party.

getChangeOutputAndFees(
  params: PartyParams,
  feeRate: bigint
): ChangeOutputAndFees

Type Definitions

interface 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;
}

πŸ—οΈ Architecture

Both packages follow a pure wrapper approach around rust-dlc:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   JavaScript    β”‚    β”‚   Generated  β”‚    β”‚    Rust     β”‚
β”‚   Application   │───▢│   Bindings   │───▢│   rust-dlc  β”‚
β”‚                 β”‚    β”‚  (TS + FFI)  β”‚    β”‚   (Core)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

API Compatibility

The packages maintain 100% API compatibility through:

  1. Shared UDL: Single UniFFI Definition Language file defines the interface
  2. Verification Scripts: Automated checks ensure parity between implementations
  3. Type Safety: Full TypeScript definitions generated from UDL
  4. Testing: Comprehensive test suites verify behavior consistency

View the compatibility verification script that ensures both packages expose identical APIs.

πŸ› οΈ Development

Prerequisites

  • Rust (latest stable)
  • Node.js 18+
  • Just (cargo install just)

Project Structure

.
β”œβ”€β”€ 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

Quick Commands

# 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 clean

πŸ“„ License

MIT License - see LICENSE file for details.

🀝 Contributing

Contributions welcome! Please ensure:

  1. All tests pass (cargo test, pnpm test)
  2. Bindings are regenerated when changing Rust code
  3. API compatibility is maintained
  4. Documentation is updated

πŸ”— Links


Built with ❀️ using rust-dlc

About

dlcdevkit language bindings

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •