no-std support for monero-serai (#311)

* Move monero-serai from std to std-shims, where possible

* no-std fixes

* Make the HttpRpc its own feature, thiserror only on std

* Drop monero-rs's epee for a homegrown one

We only need it for a single function. While I tried jeffro's, it didn't work
out of the box, had three unimplemented!s, and is no where near viable for
no_std.

Fixes #182, though should be further tested.

* no-std monero-serai

* Allow base58-monero via git

* cargo fmt
This commit is contained in:
Luke Parker
2023-06-29 04:14:29 -04:00
committed by GitHub
parent d25c668ee4
commit ac708b3b2a
30 changed files with 487 additions and 261 deletions

View File

@@ -1,7 +1,5 @@
use core::{marker::PhantomData, fmt::Debug};
use std::string::ToString;
use thiserror::Error;
use std_shims::string::{String, ToString};
use zeroize::Zeroize;
@@ -114,19 +112,20 @@ impl<B: AddressBytes> Zeroize for AddressMeta<B> {
}
/// Error when decoding an address.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum AddressError {
#[error("invalid address byte")]
#[cfg_attr(feature = "std", error("invalid address byte"))]
InvalidByte,
#[error("invalid address encoding")]
#[cfg_attr(feature = "std", error("invalid address encoding"))]
InvalidEncoding,
#[error("invalid length")]
#[cfg_attr(feature = "std", error("invalid length"))]
InvalidLength,
#[error("invalid key")]
#[cfg_attr(feature = "std", error("invalid key"))]
InvalidKey,
#[error("unknown features")]
#[cfg_attr(feature = "std", error("unknown features"))]
UnknownFeatures,
#[error("different network than expected")]
#[cfg_attr(feature = "std", error("different network than expected"))]
DifferentNetwork,
}

View File

@@ -1,11 +1,16 @@
use std_shims::{sync::OnceLock, collections::HashSet};
use std_shims::{sync::OnceLock, vec::Vec, collections::HashSet};
#[cfg(not(feature = "std"))]
use std_shims::sync::Mutex;
#[cfg(feature = "std")]
use futures::lock::Mutex;
use zeroize::{Zeroize, ZeroizeOnDrop};
use rand_core::{RngCore, CryptoRng};
use rand_distr::{Distribution, Gamma};
use zeroize::{Zeroize, ZeroizeOnDrop};
#[cfg(not(feature = "std"))]
use rand_distr::num_traits::Float;
use curve25519_dalek::edwards::EdwardsPoint;
@@ -143,6 +148,9 @@ impl Decoys {
height: usize,
inputs: &[SpendableOutput],
) -> Result<Vec<Decoys>, RpcError> {
#[cfg(not(feature = "std"))]
let mut distribution = DISTRIBUTION().lock();
#[cfg(feature = "std")]
let mut distribution = DISTRIBUTION().lock().await;
let decoy_count = ring_len - 1;

View File

@@ -1,5 +1,8 @@
use core::ops::BitXor;
use std::io::{self, Read, Write};
use std_shims::{
vec::Vec,
io::{self, Read, Write},
};
use zeroize::Zeroize;

View File

@@ -1,5 +1,5 @@
use core::ops::Deref;
use std::collections::{HashSet, HashMap};
use std_shims::collections::{HashSet, HashMap};
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
@@ -28,15 +28,15 @@ pub(crate) mod decoys;
pub(crate) use decoys::Decoys;
mod send;
pub use send::{
Fee, TransactionError, Change, SignableTransaction, SignableTransactionBuilder, Eventuality,
};
pub use send::{Fee, TransactionError, Change, SignableTransaction, Eventuality};
#[cfg(feature = "std")]
pub use send::SignableTransactionBuilder;
#[cfg(feature = "multisig")]
pub(crate) use send::InternalPayment;
#[cfg(feature = "multisig")]
pub use send::TransactionMachine;
fn key_image_sort(x: &EdwardsPoint, y: &EdwardsPoint) -> std::cmp::Ordering {
fn key_image_sort(x: &EdwardsPoint, y: &EdwardsPoint) -> core::cmp::Ordering {
x.compress().to_bytes().cmp(&y.compress().to_bytes()).reverse()
}

View File

@@ -1,5 +1,8 @@
use core::ops::Deref;
use std::io::{self, Read, Write};
use std_shims::{
vec::Vec,
io::{self, Read, Write},
};
use zeroize::{Zeroize, ZeroizeOnDrop};

View File

@@ -1,5 +1,10 @@
use core::ops::Deref;
use std_shims::{sync::OnceLock, collections::HashMap};
use std_shims::{
sync::OnceLock,
vec::Vec,
string::{String, ToString},
collections::HashMap,
};
use zeroize::{Zeroize, Zeroizing};
use rand_core::{RngCore, CryptoRng};

View File

@@ -1,25 +1,25 @@
use core::fmt;
use std_shims::string::String;
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
use rand_core::{RngCore, CryptoRng};
use thiserror::Error;
pub(crate) mod classic;
use classic::{CLASSIC_SEED_LENGTH, CLASSIC_SEED_LENGTH_WITH_CHECKSUM, ClassicSeed};
/// Error when decoding a seed.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum SeedError {
#[error("invalid number of words in seed")]
#[cfg_attr(feature = "std", error("invalid number of words in seed"))]
InvalidSeedLength,
#[error("unknown language")]
#[cfg_attr(feature = "std", error("unknown language"))]
UnknownLanguage,
#[error("invalid checksum")]
#[cfg_attr(feature = "std", error("invalid checksum"))]
InvalidChecksum,
#[error("english old seeds don't support checksums")]
#[cfg_attr(feature = "std", error("english old seeds don't support checksums"))]
EnglishOldWithChecksum,
#[error("invalid seed")]
#[cfg_attr(feature = "std", error("invalid seed"))]
InvalidSeed,
}

View File

@@ -1,7 +1,9 @@
use core::{ops::Deref, fmt};
use std::io;
use thiserror::Error;
use std_shims::{
vec::Vec,
io,
string::{String, ToString},
};
use rand_core::{RngCore, CryptoRng, SeedableRng};
use rand_chacha::ChaCha20Rng;
@@ -42,7 +44,9 @@ use crate::{
},
};
#[cfg(feature = "std")]
mod builder;
#[cfg(feature = "std")]
pub use builder::SignableTransactionBuilder;
#[cfg(feature = "multisig")]
@@ -116,34 +120,35 @@ impl SendOutput {
}
}
#[derive(Clone, PartialEq, Eq, Debug, Error)]
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum TransactionError {
#[error("multiple addresses with payment IDs")]
#[cfg_attr(feature = "std", error("multiple addresses with payment IDs"))]
MultiplePaymentIds,
#[error("no inputs")]
#[cfg_attr(feature = "std", error("no inputs"))]
NoInputs,
#[error("no outputs")]
#[cfg_attr(feature = "std", error("no outputs"))]
NoOutputs,
#[error("only one output and no change address")]
#[cfg_attr(feature = "std", error("only one output and no change address"))]
NoChange,
#[error("too many outputs")]
#[cfg_attr(feature = "std", error("too many outputs"))]
TooManyOutputs,
#[error("too much data")]
#[cfg_attr(feature = "std", error("too much data"))]
TooMuchData,
#[error("too many inputs/too much arbitrary data")]
#[cfg_attr(feature = "std", error("too many inputs/too much arbitrary data"))]
TooLargeTransaction,
#[error("not enough funds (in {0}, out {1})")]
#[cfg_attr(feature = "std", error("not enough funds (in {0}, out {1})"))]
NotEnoughFunds(u64, u64),
#[error("wrong spend private key")]
#[cfg_attr(feature = "std", error("wrong spend private key"))]
WrongPrivateKey,
#[error("rpc error ({0})")]
#[cfg_attr(feature = "std", error("rpc error ({0})"))]
RpcError(RpcError),
#[error("clsag error ({0})")]
#[cfg_attr(feature = "std", error("clsag error ({0})"))]
ClsagError(ClsagError),
#[error("invalid transaction ({0})")]
#[cfg_attr(feature = "std", error("invalid transaction ({0})"))]
InvalidTransaction(RpcError),
#[cfg(feature = "multisig")]
#[error("frost error {0}")]
#[cfg_attr(feature = "std", error("frost error {0}"))]
FrostError(FrostError),
}

View File

@@ -1,8 +1,9 @@
use std::{
use std_shims::{
vec::Vec,
io::{self, Read},
sync::{Arc, RwLock},
collections::HashMap,
};
use std::sync::{Arc, RwLock};
use zeroize::Zeroizing;