Biglup/cardano-c
GitHub: Biglup/cardano-c
一个符合 MISRA 标准的纯 C 语言 Cardano 区块链交互库,提供交易构建、签名与治理操作等全面功能,并支持多语言绑定。
Stars: 25 | Forks: 4
  [](https://codecov.io/gh/Biglup/cardano-c) [](https://app.codacy.com/gh/Biglup/cardano-c/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade) [](https://cardano-c.readthedocs.io/en/latest/?badge=latest) [](https://projectcatalyst.io/) ## 功能 - 地址解析与生成 - Ed25519 加密 - 交易序列化与反序列化 - 强大的交易构建器 - 健壮的 C99 实现 - 分层架构同时提供控制力与便捷性 - 灵活的内存管理 - 无共享全局状态 - 利于线程处理 - 正确处理 UTF-8 - 详尽的文档和测试套件 - 没有运行时依赖(该库依赖于 [libsodium](https://github.com/jedisct1/libsodium) 和 [libgmp](https://gmplib.org/),但它们都是静态链接的) ## 绑定 以下是所有可用语言/绑定的列表。 | 名称 | libcardano-c 版本 | 语言 | 许可证 | | :--------------------------------------------------------------------------------------- | :------------------: | :------------------------------------------------------------------: | :------------------: | | [libcarcano-c](https://github.com/Biglup/cardano-c) | **1.2.0** | [C](https://en.wikipedia.org/wiki/C_(programming_language)) | Apache 2.0 | | [cometa.js](https://github.com/Biglup/cometa.js) | **1.2.0** | [JavaScript](https://en.wikipedia.org/wiki/JavaScript) | Apache 2.0 | | [cometa.py](https://github.com/Biglup/cometa.py) | **1.2.0** | [Python](https://www.python.org) | Apache 2.0 | 更多绑定即将推出,请查看 https://cometa.dev 获取即将推出的列表 ## 基础示例 这是一个基础的 cardano-c 示例,它会发送 `LOVELACE_TO_SEND` 个代币到 `RECEIVING_ADDRESS`。在[发送 lovelace](examples/src/send_lovelace_example.c) 查看完整示例。 ``` // 2 hours from now in UNIX time (seconds) const uint64_t invalid_after = cardano_utils_get_time() + SECONDS_IN_TWO_HOURS; // 1.- Build transaction cardano_tx_builder_t* tx_builder = cardano_tx_builder_new(protocol_params, &CARDANO_PREPROD_SLOT_CONFIG); cardano_tx_builder_set_utxos(tx_builder, utxo_list); cardano_tx_builder_set_change_address(tx_builder, payment_address); cardano_tx_builder_set_invalid_after_ex(tx_builder, invalid_after); cardano_tx_builder_send_lovelace_ex(tx_builder, RECEIVING_ADDRESS, cardano_utils_safe_strlen(RECEIVING_ADDRESS, 128), LOVELACE_TO_SEND); cardano_transaction_t* transaction = NULL; cardano_error_t result = cardano_tx_builder_build(tx_builder, &transaction); if (result != CARDANO_SUCCESS) { console_error("Failed to build transaction"); console_error("Error [%d]: %s", result, cardano_error_to_string(result)); console_error("%s", cardano_tx_builder_get_last_error(tx_builder)); return result; } // 2.- Sign transaction sign_transaction(key_handler, SIGNER_DERIVATION_PATH, transaction); // 3.- Submit transaction & confirm submit_transaction(provider, CONFIRM_TX_TIMEOUT_MS, transaction); ``` ## Conway 时代支持 Cardano-C 支持 Conway 时代及之前的所有功能,Conway 时代是 Cardano 区块链当前的时代。Conway 时代为 Cardano 带来了去中心化治理。您可以在 [examples](examples/) 目录中看到一些与治理相关的示例: - [注册为 DRep (PubKey)](examples/src/drep_pubkey_example.c) - [注册为 DRep (Script)](examples/src/drep_script_example.c) - [提交治理行动提案(从国库提取)](examples/src/propose_treasury_withdrawal_example.c) - [为提案投票 (PubKey DRep)](examples/src/vote_for_proposal_pubkey_example.c) - [为提案投票 (Script DRep)](examples/src/vote_for_proposal_script_example.c) 这些是在 [examples](examples/) 目录中展示的部分示例。不过,您应该能够为当前时代构建任何有效的交易。有关更多信息,请参阅[文档](https://cardano-c.readthedocs.io/)。 ## 内存管理 Cardano C 库使用了一个简单的引用计数模型。其主要目标是使该库能够轻松集成到使用不同内存管理模型(例如垃圾回收)的应用程序和语言中。 我们库中的每个对象都提供了增加和减少其引用计数的函数。例如,您可以使用 `cardano_cbor_writer_ref` 来增加 `cardano_cbor_writer_t` 对象的引用计数,并使用 `cardano_cbor_writer_unref` 来减少引用计数。 通过像 `cardano_cbor_writer_new` 这样的构造函数创建对象时,对象的引用计数被初始化为 1。 这意味着调用者成为了新创建引用的唯一所有者。 当引用计数降为零时——通常是当最后一个持有引用的实体调用 `*_unref` 函数时——该对象将被释放。 请注意,**所有** getter 函数都会在返回对象时一致地增加其引用计数。因此,调用者有责任在用完结果后调用 `*_unref`。 ## 设置 Git hooks 自定义目录 克隆存储库后,在存储库根目录中运行以下命令: ``` git config core.hooksPath .githooks ``` ## Clang format 集成 存储库附带始终保持最新的 `.clang-format` 文件,这是 `clang-format` 工具的输入配置文件(最低要求版本 15.x)。 - https://apt.llvm.org/ ## 单元测试 本项目使用 Google test 框架来编写单元测试。 首先安装 gtest 开发包: ``` sudo apt-get install libgtest-dev ``` 请注意,此软件包仅安装源文件。您必须自己编译代码以创建必要的库文件。 这些源文件应位于 /usr/src/googletest。 ``` cd $(mktemp -d) cmake /usr/src/googletest make sudo make install ``` ## 文档 为了生成该项目的文档,我们使用 Doxygen 进行源代码处理,使用 Breathe 将 Doxygen 内容与 Sphinx 集成,并使用 Sphinx 将整个文档编译成对 Web 友好的格式。 ### 设置您的环境 1. **安装 Python 3 和 pip**:Sphinx 和 Breathe 必需。大多数 Linux 发行版默认包含 Python 3。对于 Windows,请从 [Python 网站](https://www.python.org/downloads/) 下载,并确保将 Python 添加到您的 PATH 中。 Linux 示例: sudo apt install python3 python3-pip 2. **安装 Sphinx 和 Breathe**:这些 Python 包通过 pip 安装。Breathe 充当 Doxygen 生成的 XML 与 Sphinx 之间的桥梁,实现无缝的文档集成。此外,还安装了 Sphinx Immaterial 主题以实现现代 Web 设计。 pip install sphinx sphinx-immaterial breathe 3. **安装 Doxygen**:用于从源代码生成 XML 文件所必需。通过您的 Linux 包管理器安装,或者如果是 Windows,则从 [Doxygen 网站](https://www.doxygen.nl/download.html) 下载。 Linux 示例: sudo apt-get install doxygen ### 生成文档 设置好环境后,通过导航到存储库根目录并执行以下命令来生成项目文档: ``` cmake -DDOXYGEN_ENABLED=ON . make doc ``` 生成的文档将位于 `build/release/doc/html/index.html`,提供有关项目 API 和架构的全面指南。 ## 贡献
标签:Bash脚本, Cardano, 交易构建, 加密货币, 区块链, 客户端加密, 底层库