展示 HN:Bithoven – 一种用于比特币智能合约的高级、命令式语言
Show HN: Bithoven – A high-level, imperative language for Bitcoin Smart Contract

原始链接: https://github.com/ChrisCho-H/bithoven

## Bithoven:一种用于比特币智能合约的现代语言 Bithoven 是一种新型的、类型安全的编程语言,旨在简化比特币智能合约的开发。它直接编译为原生比特币脚本,弥合了复杂逻辑与比特币虚拟机 (VM) 之间的差距。 与传统的比特币脚本不同,Bithoven 提供了一种熟悉的命令式语法,具有 `if/else` 语句、命名变量和内置类型检查(布尔值、签名、字符串、数字)等功能,以提高可读性并防止错误。它支持多种花费路径,使像哈希时间锁定合约 (HTLC) 这样的复杂合约更容易实现。 Bithoven 通过 pragmas 针对 SegWit 和 Taproot,并包含用于常见比特币原语(如时间锁、密码学 (SHA256, checksig) 和验证)的本机关键字。该语言在编译期间自动处理堆栈管理,将高级代码转换为优化的比特币脚本操作码。 Bithoven 可通过 Cargo (Rust) 和 npm (JavaScript) 获取,旨在使比特币智能合约开发更易于访问和更安全。

## Bithoven:一种用于比特币智能合约的新语言 一位研究人员发布了Bithoven,一种新的高级、命令式编程语言,旨在简化比特币智能合约的开发。目前,编写比特币智能合约需要直接使用复杂的低级“Script”代码——类似于汇编语言。Bithoven旨在弥合这一差距,提供更易读、更安全的替代方案,具有熟悉的`if/else`语句和常见数据类型的内置类型安全等特性。 该语言编译为适用于Legacy、SegWit和Taproot的本地比特币Script,并包含时间锁和密码学等功能的基本元素。一个关键优势是编译时安全性,降低了可能导致脚本崩溃的错误风险。 Bithoven是开源的,可以通过WASM在浏览器中试用:[https://bithoven-lang.github.io/bithoven/ide/](https://bithoven-lang.github.io/bithoven/ide/)。开发者欢迎反馈,并提供了文档:[https://bithoven-lang.github.io/bithoven/docs/](https://bithoven-lang.github.io/bithoven/docs/)。
相关文章

原文

A High-Level, Imperative Language for Bitcoin Smart Contracts

Bithoven is a type-safe, developer-friendly programming language designed to compile down to native Bitcoin Script. It bridges the gap between complex smart contract logic and the low-level stack machine of the Bitcoin Virtual Machine (VM).

Write readable, auditable code with modern control flow (if/else), named variables, and built-in safety checks—then compile it to highly optimized Bitcoin Script for SegWit or Taproot.

  • Imperative Syntax: Write logic using familiar if, else, and return statements instead of mental stack juggling.
  • Type Safety: First-class support for bool, signature, string, and number types to prevent common runtime errors.
  • Multiple Spending Paths: Define complex contracts (like HTLCs) with distinct execution branches and input stack requirements.
  • Targeted Compilation: Support for legacy, segwit, and taproot compilation targets via pragmas.
  • Native Bitcoin Primitives: Built-in keywords for timelocks (older, after), cryptography (sha256, checksig), and verification (verify).
# For CLI user
cargo install bithoven
# For rust user
cargo add bithoven
# For js user
npm install bithoven

Writing Your First Contract

Bithoven contracts are defined with a .bithoven extension. Below is an implementation of a standard Hashed Time-Locked Contract (HTLC), demonstrating how Bithoven simplifies conditional spending paths.

htlc.bithoven

pragma bithoven version 0.0.1;
pragma bithoven target segwit;

/* * Stack Input Definitions
 * Each line defines a valid input stack configuration for a spending path.
 */
(condition: bool, sig_alice: signature)
(condition: bool, preimage: string, sig_bob: signature)

{
    // If 'condition' is true, we enter the Refund Path (Alice)
    if condition {
        // Enforce relative timelock of 1000 blocks
        older 1000;

        // If timelock is satisfied, Alice can spend with her signature
        return checksig(sig_alice, "0245a6b3f8eeab8e88501a9a25391318dce9bf35e24c377ee82799543606bf5212");

    } else {
        // Redeem Path (Bob)
        // Bob must reveal the secret preimage that hashes to the expected value
        verify sha256 sha256 preimage == "53de742e2e323e3290234052a702458589c30d2c813bf9f866bef1b651c4e45f";

        // If hash matches, Bob can spend with his signature
        return checksig(sig_bob, "0345a6b3f8eeab8e88501a9a25391318dce9bf35e24c377ee82799543606bf5212");
    }
}

See more examples here: Examples

When compiled, Bithoven translates the high-level imperative logic into the equivalent Bitcoin Script opcodes, handling the control flow and stack management automatically.

Command:

bithoven compile htlc.bithoven

Generated Bitcoin Script (ASM):

OP_IF
    <0xe803> OP_CHECKSEQUENCEVERIFY OP_DROP
    <pubkey_alice> OP_CHECKSIG
OP_ELSE
    OP_HASH256 OP_TOALTSTACK <hash_digest> OP_FROMALTSTACK OP_SWAP OP_EQUALVERIFY
    <pubkey_bob> OP_CHECKSIG
OP_ENDIF
  • older <n>: Enforces relative timelock (Sequence).
  • after <n>: Enforces absolute timelock (LockTime).
  • checksig(sig, pubkey): Validates a signature against a public key.
  • verify <expr>: Ensures an expression evaluates to true, otherwise fails the script.
  • bool: Boolean values (true, false).
  • signature: ECSDA or Schnorr signatures.
  • string: Hex or ASCII string data.
  • number: Integer values.

Contributions are welcome! Please check out the issues page for roadmap items or submit a PR.

This project is licensed under the MIT License - see the LICENSE file for details.

联系我们 contact @ memedata.com