Skip to content

Commit

Permalink
Add #![no_std] support (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat authored Sep 11, 2024
1 parent 00c7df7 commit 311bd2b
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 29 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/')
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions src/convert.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
27 changes: 14 additions & 13 deletions src/ed25519.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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())
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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())
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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())
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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())
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
20 changes: 12 additions & 8 deletions src/strkey.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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())
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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())
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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())
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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())
}
}
Expand Down
6 changes: 2 additions & 4 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down

0 comments on commit 311bd2b

Please sign in to comment.