Skip to content

Commit

Permalink
zcash_keys: Enable no_std usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Dec 16, 2024
1 parent c04a71e commit 100512a
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 48 deletions.
51 changes: 38 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,22 @@ pczt = { version = "0.0", path = "pczt" }

# Shielded protocols
bellman = { version = "0.14", default-features = false, features = ["groth16"] }
ff = "0.13"
ff = { version = "0.13", default-features = false }
group = "0.13"
incrementalmerkletree = "0.7"
incrementalmerkletree = { version = "0.7.1", default-features = false }
shardtree = "0.5"
zcash_spec = "0.1"

# Payment protocols
# - Sapling
bitvec = "1"
blake2s_simd = "1"
bitvec = { version = "1", default-features = false, features = ["alloc"] }
blake2s_simd = { version = "1", default-features = false }
bls12_381 = "0.8"
jubjub = "0.10"
redjubjub = "0.7"
sapling = { package = "sapling-crypto", version = "0.3", default-features = false }

# - Orchard
nonempty = "0.7"
orchard = { version = "0.10", default-features = false }
pasta_curves = "0.5"

Expand All @@ -75,12 +74,13 @@ ripemd = { version = "0.1", default-features = false }
secp256k1 = { version = "0.27", default-features = false, features = ["alloc"] }
transparent = { package = "zcash_transparent", version = "0.0", path = "zcash_transparent", default-features = false }

# Boilerplate
# Boilerplate & missing stdlib
getset = "0.1"
nonempty = { version = "0.10", default-features = false }

# CSPRNG
rand = "0.8"
rand_core = "0.6"
rand = { version = "0.8", default-features = false }
rand_core = { version = "0.6", default-features = false }

# Currency conversions
rust_decimal = { version = "1.35", default-features = false, features = ["serde"] }
Expand All @@ -100,7 +100,7 @@ byteorder = "1"
hex = { version = "0.4", default-features = false, features = ["alloc"] }
percent-encoding = "2.1.0"
postcard = { version = "1", features = ["alloc"] }
serde = { version = "1", features = ["derive"] }
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = "1"

# HTTP
Expand All @@ -111,8 +111,8 @@ tokio-rustls = "0.24"
webpki-roots = "0.25"

# Logging and metrics
memuse = "0.2.1"
tracing = "0.1"
memuse = { version = "0.2.2", default-features = false }
tracing = { version = "0.1", default-features = false }

# No-std support
core2 = { version = "0.3", default-features = false, features = ["alloc"] }
Expand Down Expand Up @@ -166,7 +166,7 @@ trait-variant = "0.1"

# ZIP 32
aes = "0.8"
fpe = "0.6"
fpe = { version = "0.6", default-features = false, features = ["alloc"] }
zip32 = { version = "0.1.1", default-features = false }

[profile.release]
Expand Down Expand Up @@ -195,5 +195,6 @@ debug = true
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(zcash_unstable, values("zfuture"))'] }

[patch.crates-io]
orchard = { git = "https://github.com/zcash/orchard.git", rev = "7a44e3279b5747819022c4d8f4474fa79b2d9746" }
nonempty = { git = "https://github.com/nuttycom/nonempty.git", rev = "090815ca02ff969d23c6331f9f5b1e4c7720160a" }
orchard = { git = "https://github.com/zcash/orchard.git", rev = "1d60917a6092ff4628e8cf89538f7cd5b339110d" }
sapling-crypto = { git = "https://github.com/zcash/sapling-crypto.git", rev = "e47d57f5c9c46f05740328f8ef9601f6d697cf34" }
4 changes: 4 additions & 0 deletions zcash_keys/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this library adheres to Rust's notion of

## [Unreleased]

### Added
- `no-std` compatibility (`alloc` is required). A default-enabled `std` feature
flag has been added gating the `std::error::Error` usage.

### Changed
- Migrated to `bech32 0.11`.
- The `UnifiedAddressRequest` argument to the following methods is now optional:
Expand Down
6 changes: 5 additions & 1 deletion zcash_keys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ rand_core.workspace = true
# - Encodings
bech32.workspace = true
bs58.workspace = true
core2.workspace = true

# - Transparent protocols
bip32 = { workspace = true, optional = true }
Expand All @@ -58,7 +59,7 @@ proptest = { workspace = true, optional = true }
# Dependencies used internally:
# (Breaking upgrades to these are usually backwards-compatible, but check MSRVs.)
# - Documentation
document-features.workspace = true
document-features = { workspace = true, optional = true }

# - Encodings
byteorder = { workspace = true, optional = true }
Expand All @@ -75,6 +76,9 @@ zcash_address = { workspace = true, features = ["test-dependencies"] }
zcash_primitives = { workspace = true, features = ["test-dependencies"] }

[features]
default = ["std"]
std = ["dep:document-features"]

## Enables use of transparent key parts and addresses
transparent-inputs = ["dep:bip32", "zcash_primitives/transparent-inputs"]

Expand Down
5 changes: 4 additions & 1 deletion zcash_keys/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Structs for handling supported address types.
use alloc::vec::Vec;
use alloc::string::{String, ToString};

use transparent::address::TransparentAddress;
use zcash_address::{
unified::{self, Container, Encoding, Typecode},
Expand Down Expand Up @@ -215,7 +218,7 @@ impl UnifiedAddress {

/// Returns the set of receiver typecodes.
pub fn receiver_types(&self) -> Vec<Typecode> {
let result = std::iter::empty();
let result = core::iter::empty();

Check warning on line 221 in zcash_keys/src/address.rs

View check run for this annotation

Codecov / codecov/patch

zcash_keys/src/address.rs#L221

Added line #L221 was not covered by tests
#[cfg(feature = "orchard")]
let result = result.chain(self.orchard.map(|_| Typecode::Orchard));
#[cfg(feature = "sapling")]
Expand Down
12 changes: 8 additions & 4 deletions zcash_keys/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
use crate::address::UnifiedAddress;
use bs58::{self, decode::Error as Bs58Error};
use std::fmt;
use core::fmt;
use alloc::borrow::ToOwned;
use alloc::string::{String, ToString};

use transparent::address::TransparentAddress;
use zcash_address::unified::{self, Encoding};
use zcash_protocol::consensus::{self, NetworkConstants};

#[cfg(feature = "sapling")]
use {
alloc::vec::Vec,
bech32::{
primitives::decode::{CheckedHrpstring, CheckedHrpstringError},
Bech32, Hrp,
},
sapling::zip32::{ExtendedFullViewingKey, ExtendedSpendingKey},
std::io::{self, Write},
core2::io::{self, Write},
zcash_protocol::consensus::NetworkType,
};

Expand Down Expand Up @@ -73,7 +76,7 @@ impl fmt::Display for Bech32DecodeError {
}
}

#[cfg(feature = "sapling")]
#[cfg(all(feature = "sapling", feature = "std"))]
impl std::error::Error for Bech32DecodeError {}

#[cfg(feature = "sapling")]
Expand All @@ -95,7 +98,7 @@ where
/// A trait for encoding and decoding Zcash addresses.
pub trait AddressCodec<P>
where
Self: std::marker::Sized,
Self: core::marker::Sized,
{
type Error;

Expand Down Expand Up @@ -132,6 +135,7 @@ impl fmt::Display for TransparentCodecError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for TransparentCodecError {}

impl<P: consensus::Parameters> AddressCodec<P> for TransparentAddress {
Expand Down
Loading

0 comments on commit 100512a

Please sign in to comment.