From 311bd2b4bc4ef5672fa06475aabee591e524ebdd Mon Sep 17 00:00:00 2001 From: Jun Luo <4catcode@gmail.com> Date: Wed, 11 Sep 2024 09:07:22 +0800 Subject: [PATCH] Add `#![no_std]` support (#64) --- .github/workflows/rust.yml | 8 ++++++-- Cargo.toml | 4 ++-- README.md | 3 +++ src/convert.rs | 3 +++ src/crc.rs | 1 + src/ed25519.rs | 27 ++++++++++++++------------- src/lib.rs | 3 +++ src/strkey.rs | 20 ++++++++++++-------- tests/tests.rs | 6 ++---- 9 files changed, 46 insertions(+), 29 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b4e9abb..815319e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -46,8 +46,12 @@ jobs: run: echo RUSTFLAGS='-Dwarnings' >> $GITHUB_ENV - run: rustup update - run: cargo version - - run: cargo build --target ${{ matrix.target }} - - run: cargo test --target ${{ matrix.target }} + - uses: stellar/binaries@v30 + with: + name: cargo-hack + version: 0.5.28 + - run: cargo-hack hack build --feature-powerset --target ${{ matrix.target }} + - run: cargo-hack hack test --feature-powerset --target ${{ matrix.target }} publish-dry-run: if: startsWith(github.head_ref, 'release/') diff --git a/Cargo.toml b/Cargo.toml index e748b90..6e0a0ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,12 +20,12 @@ path = "src/bin/stellar-strkey/main.rs" required-features = ["cli"] doctest = false -[build_dependencies] +[build-dependencies] crate-git-revision = "0.0.6" [dev-dependencies] proptest = "1.0.0" [dependencies] -data-encoding = "2.6.0" +data-encoding = { version = "2.6.0", default-features = false, features = ["alloc"] } clap = { version = "4.2.4", default-features = false, features = ["std", "derive", "usage", "help"], optional = true } diff --git a/README.md b/README.md index 29b8a4a..dba55fb 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,9 @@ To use the library, include in your toml: stellar-strkey = "..." ``` +This crate does not depend on the `std` crate and can be used in `no_std` environments. +However, please note that it relies on the [`alloc`](https://docs.rust-embedded.org/book/collections/#using-alloc) crate for certain types such as `Vec`. + #### CLI To use the CLI: diff --git a/src/convert.rs b/src/convert.rs index 08b579b..0a212ba 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -1,6 +1,9 @@ // TODO: Could encode and decode, and the functions upstream that call them, be // const fn's? +use alloc::string::String; +use alloc::vec::Vec; + use crate::{crc::checksum, error::DecodeError}; pub fn encode(ver: u8, payload: &[u8]) -> String { diff --git a/src/crc.rs b/src/crc.rs index 8df7d7e..715b753 100644 --- a/src/crc.rs +++ b/src/crc.rs @@ -86,6 +86,7 @@ pub fn checksum(data: &[u8]) -> [u8; 2] { mod tests { use super::checksum; extern crate proptest; + use alloc::vec::Vec; use proptest::prelude::*; #[test] diff --git a/src/ed25519.rs b/src/ed25519.rs index 52be632..24c048d 100644 --- a/src/ed25519.rs +++ b/src/ed25519.rs @@ -1,19 +1,20 @@ -use std::{ - fmt::{Debug, Display}, - str::FromStr, -}; - use crate::{ convert::{decode, encode}, error::DecodeError, version, }; +use alloc::{format, string::String, vec, vec::Vec}; +use core::{ + fmt::{Debug, Display}, + str::FromStr, +}; + #[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct PrivateKey(pub [u8; 32]); impl Debug for PrivateKey { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "PrivateKey(")?; write!( f, @@ -51,7 +52,7 @@ impl PrivateKey { } impl Display for PrivateKey { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{}", self.to_string()) } } @@ -68,7 +69,7 @@ impl FromStr for PrivateKey { pub struct PublicKey(pub [u8; 32]); impl Debug for PublicKey { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "PublicKey(")?; write!( f, @@ -106,7 +107,7 @@ impl PublicKey { } impl Display for PublicKey { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{}", self.to_string()) } } @@ -126,7 +127,7 @@ pub struct MuxedAccount { } impl Debug for MuxedAccount { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "MuxedAccount(")?; write!( f, @@ -174,7 +175,7 @@ impl MuxedAccount { } impl Display for MuxedAccount { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{}", self.to_string()) } } @@ -197,7 +198,7 @@ pub struct SignedPayload { } impl Debug for SignedPayload { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "MuxedAccount(")?; write!( f, @@ -321,7 +322,7 @@ impl SignedPayload { } impl Display for SignedPayload { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{}", self.to_string()) } } diff --git a/src/lib.rs b/src/lib.rs index 4e36263..038a89b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,6 @@ +#![no_std] +extern crate alloc; + #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)] pub struct Version<'a> { pub pkg: &'a str, diff --git a/src/strkey.rs b/src/strkey.rs index 5f04709..caa5e46 100644 --- a/src/strkey.rs +++ b/src/strkey.rs @@ -1,4 +1,8 @@ -use std::{fmt::Debug, fmt::Display, str::FromStr}; +use alloc::{format, string::String}; +use core::{ + fmt::{Debug, Display}, + str::FromStr, +}; use crate::{ convert::{decode, encode}, @@ -55,7 +59,7 @@ impl Strkey { } impl Display for Strkey { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{}", self.to_string()) } } @@ -72,7 +76,7 @@ impl FromStr for Strkey { pub struct PreAuthTx(pub [u8; 32]); impl Debug for PreAuthTx { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "PreAuthTx(")?; write!( f, @@ -107,7 +111,7 @@ impl PreAuthTx { } impl Display for PreAuthTx { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{}", self.to_string()) } } @@ -124,7 +128,7 @@ impl FromStr for PreAuthTx { pub struct HashX(pub [u8; 32]); impl Debug for HashX { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "HashX(")?; write!( f, @@ -159,7 +163,7 @@ impl HashX { } impl Display for HashX { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{}", self.to_string()) } } @@ -176,7 +180,7 @@ impl FromStr for HashX { pub struct Contract(pub [u8; 32]); impl Debug for Contract { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "Contract(")?; write!( f, @@ -211,7 +215,7 @@ impl Contract { } impl Display for Contract { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "{}", self.to_string()) } } diff --git a/tests/tests.rs b/tests/tests.rs index f17fc72..de91b45 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,8 +1,6 @@ -use stellar_strkey::*; - extern crate proptest; - -use proptest::prelude::*; +use proptest::proptest; +use stellar_strkey::*; #[test] fn test_valid_public_keys() {