From 7bff034c59acf4be8fbf28a1ea7b204bdf2f4cdd Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 16 Dec 2024 09:34:45 +0000 Subject: [PATCH] zcash_transparent: Fix its dependencies to work with no-std --- Cargo.lock | 1 - Cargo.toml | 16 ++++++++-------- components/zcash_protocol/Cargo.toml | 3 ++- components/zip321/Cargo.toml | 4 ++-- zcash_primitives/Cargo.toml | 2 +- zcash_transparent/Cargo.toml | 12 +++++++++--- zcash_transparent/src/address.rs | 9 +++++---- zcash_transparent/src/bundle.rs | 17 ++++++++++------- zcash_transparent/src/lib.rs | 4 ++++ 9 files changed, 41 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d4b3a49972..f61fb3faaa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6451,7 +6451,6 @@ dependencies = [ "bip32", "blake2b_simd", "bs58", - "byteorder", "core2", "document-features", "getset", diff --git a/Cargo.toml b/Cargo.toml index b2528ee169..065b35f650 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ categories = ["cryptography::cryptocurrencies"] [workspace.dependencies] # Intra-workspace dependencies equihash = { version = "0.2", path = "components/equihash" } -zcash_address = { version = "0.6", path = "components/zcash_address" } +zcash_address = { version = "0.6", path = "components/zcash_address", default-features = false } zcash_client_backend = { version = "0.15", path = "zcash_client_backend" } zcash_encoding = { version = "0.2.1", path = "components/zcash_encoding", default-features = false } zcash_keys = { version = "0.5", path = "zcash_keys" } @@ -71,8 +71,8 @@ pasta_curves = "0.5" # - Transparent bip32 = { version = "0.5", default-features = false } -ripemd = "0.1" -secp256k1 = "0.27" +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 @@ -86,8 +86,8 @@ rand_core = "0.6" rust_decimal = { version = "1.35", default-features = false, features = ["serde"] } # Digests -blake2b_simd = "1" -sha2 = "0.10" +blake2b_simd = { version = "1", default-features = false } +sha2 = { version = "0.10", default-features = false } # Documentation document-features = "0.2" @@ -97,7 +97,7 @@ base64 = "0.22" bech32 = { version = "0.11", default-features = false, features = ["alloc"] } bs58 = { version = "0.5", default-features = false, features = ["alloc", "check"] } byteorder = "1" -hex = "0.4" +hex = { version = "0.4", default-features = false, features = ["alloc"] } percent-encoding = "2.1.0" postcard = { version = "1", features = ["alloc"] } serde = { version = "1", features = ["derive"] } @@ -129,7 +129,7 @@ tonic-build = { version = "0.12", default-features = false } # Secret management secrecy = "0.8" -subtle = "2.2.3" +subtle = { version = "2.2.3", default-features = false } # SQLite databases # - Warning: One of the downstream consumers requires that SQLite be available through @@ -167,7 +167,7 @@ trait-variant = "0.1" # ZIP 32 aes = "0.8" fpe = "0.6" -zip32 = "0.1.1" +zip32 = { version = "0.1.1", default-features = false } [profile.release] lto = true diff --git a/components/zcash_protocol/Cargo.toml b/components/zcash_protocol/Cargo.toml index ac79e5df1f..093e024d05 100644 --- a/components/zcash_protocol/Cargo.toml +++ b/components/zcash_protocol/Cargo.toml @@ -42,7 +42,8 @@ proptest.workspace = true [features] default = ["std"] -std = ["document-features", "dep:memuse"] +std = ["dep:document-features", "dep:memuse"] + ## Exposes APIs that are useful for testing, such as `proptest` strategies. test-dependencies = [ "dep:incrementalmerkletree", diff --git a/components/zip321/Cargo.toml b/components/zip321/Cargo.toml index be67284a39..a5b2f076ec 100644 --- a/components/zip321/Cargo.toml +++ b/components/zip321/Cargo.toml @@ -15,7 +15,7 @@ categories.workspace = true [dependencies] zcash_address.workspace = true -zcash_protocol.workspace = true +zcash_protocol = { workspace = true, features = ["std"] } # - Parsing and Encoding nom = "7" @@ -27,7 +27,7 @@ proptest = { workspace = true, optional = true } [dev-dependencies] zcash_address = { workspace = true, features = ["test-dependencies"] } -zcash_protocol = { workspace = true, features = ["test-dependencies"] } +zcash_protocol = { workspace = true, features = ["std", "test-dependencies"] } proptest.workspace = true [features] diff --git a/zcash_primitives/Cargo.toml b/zcash_primitives/Cargo.toml index da79c8d4e1..c9b9b49e66 100644 --- a/zcash_primitives/Cargo.toml +++ b/zcash_primitives/Cargo.toml @@ -21,7 +21,7 @@ rustdoc-args = ["--cfg", "docsrs"] [dependencies] equihash.workspace = true zcash_address.workspace = true -zcash_encoding.workspace = true +zcash_encoding = { workspace = true, features = ["std"] } zcash_protocol.workspace = true zip32.workspace = true diff --git a/zcash_transparent/Cargo.toml b/zcash_transparent/Cargo.toml index dad6617875..30fa5933c5 100644 --- a/zcash_transparent/Cargo.toml +++ b/zcash_transparent/Cargo.toml @@ -18,7 +18,6 @@ zcash_address.workspace = true zcash_encoding.workspace = true zcash_protocol.workspace = true zip32.workspace = true -core2.workspace = true # Dependencies exposed in a public API: # (Breaking upgrades to these require a breaking upgrade to this crate.) @@ -26,6 +25,9 @@ core2.workspace = true blake2b_simd.workspace = true sha2.workspace = true +# - Encodings +core2.workspace = true + # - Secret management subtle.workspace = true @@ -47,17 +49,21 @@ secp256k1 = { workspace = true, optional = true } getset.workspace = true # - Documentation -document-features.workspace = true +document-features = { workspace = true, optional = true } # - Encodings bs58.workspace = true -byteorder.workspace = true hex.workspace = true # - Transparent protocol ripemd.workspace = true [features] +default = ["std"] +std = [ + "dep:document-features", +] + ## Enables spending transparent notes with the transaction builder. transparent-inputs = ["bip32/secp256k1-ffi", "dep:secp256k1"] diff --git a/zcash_transparent/src/address.rs b/zcash_transparent/src/address.rs index 16fb5bfcfd..464652d047 100644 --- a/zcash_transparent/src/address.rs +++ b/zcash_transparent/src/address.rs @@ -1,7 +1,5 @@ //! Support for legacy transparent addresses and scripts. -use byteorder::{ReadBytesExt, WriteBytesExt}; - use alloc::string::String; use alloc::vec::Vec; use core::fmt; @@ -314,12 +312,15 @@ impl fmt::Debug for Script { impl Script { pub fn read(mut reader: R) -> io::Result { - let script = Vector::read(&mut reader, |r| r.read_u8())?; + let script = Vector::read(&mut reader, |r| { + let mut bytes = [0; 1]; + r.read_exact(&mut bytes).map(|_| bytes[0]) + })?; Ok(Script(script)) } pub fn write(&self, mut writer: W) -> io::Result<()> { - Vector::write(&mut writer, &self.0, |w, e| w.write_u8(*e)) + Vector::write(&mut writer, &self.0, |w, e| w.write_all(&[*e])) } /// Returns the length of this script as encoded (including the initial CompactSize). diff --git a/zcash_transparent/src/bundle.rs b/zcash_transparent/src/bundle.rs index 6366d3b7ec..e6235b9bc1 100644 --- a/zcash_transparent/src/bundle.rs +++ b/zcash_transparent/src/bundle.rs @@ -1,7 +1,5 @@ //! Structs representing the components within Zcash transactions. -use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; - use alloc::vec::Vec; use core::fmt::Debug; use core2::io::{self, Read, Write}; @@ -160,13 +158,14 @@ impl OutPoint { pub fn read(mut reader: R) -> io::Result { let mut hash = [0u8; 32]; reader.read_exact(&mut hash)?; - let n = reader.read_u32::()?; - Ok(OutPoint::new(hash, n)) + let mut n = [0; 4]; + reader.read_exact(&mut n)?; + Ok(OutPoint::new(hash, u32::from_le_bytes(n))) } pub fn write(&self, mut writer: W) -> io::Result<()> { writer.write_all(self.hash.as_ref())?; - writer.write_u32::(self.n) + writer.write_all(&self.n.to_le_bytes()) } /// Returns `true` if this `OutPoint` is "null" in the Bitcoin sense: it has txid set to @@ -204,7 +203,11 @@ impl TxIn { pub fn read(mut reader: &mut R) -> io::Result { let prevout = OutPoint::read(&mut reader)?; let script_sig = Script::read(&mut reader)?; - let sequence = reader.read_u32::()?; + let sequence = { + let mut sequence = [0; 4]; + reader.read_exact(&mut sequence)?; + u32::from_le_bytes(sequence) + }; Ok(TxIn { prevout, @@ -216,7 +219,7 @@ impl TxIn { pub fn write(&self, mut writer: W) -> io::Result<()> { self.prevout.write(&mut writer)?; self.script_sig.write(&mut writer)?; - writer.write_u32::(self.sequence) + writer.write_all(&self.sequence.to_le_bytes()) } } diff --git a/zcash_transparent/src/lib.rs b/zcash_transparent/src/lib.rs index dff01212bb..96f6144408 100644 --- a/zcash_transparent/src/lib.rs +++ b/zcash_transparent/src/lib.rs @@ -1,4 +1,8 @@ //! # Zcash transparent protocol +//! +#![cfg_attr(feature = "std", doc = "## Feature flags")] +#![cfg_attr(feature = "std", doc = document_features::document_features!())] +//! #![no_std]