Get the repo to compile again

This commit is contained in:
Luke Parker
2024-06-23 10:08:51 -04:00
parent 11dba9173f
commit 0b20004ba1
40 changed files with 1452 additions and 777 deletions

View File

@@ -0,0 +1,51 @@
[package]
name = "monero-wallet-util"
version = "0.1.0"
description = "Additional utility functions for monero-wallet"
license = "MIT"
repository = "https://github.com/serai-dex/serai/tree/develop/coins/monero/wallet/util"
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
edition = "2021"
rust-version = "1.79"
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[lints]
workspace = true
[dependencies]
std-shims = { path = "../../../../common/std-shims", version = "^0.1.1", default-features = false }
thiserror = { version = "1", default-features = false, optional = true }
zeroize = { version = "^1.5", default-features = false, features = ["zeroize_derive"] }
rand_core = { version = "0.6", default-features = false }
monero-wallet = { path = "..", default-features = false }
monero-seed = { path = "../seed", default-features = false }
polyseed = { path = "../polyseed", default-features = false }
[dev-dependencies]
hex = { version = "0.4", default-features = false, features = ["std"] }
curve25519-dalek = { version = "4", default-features = false, features = ["alloc", "zeroize"] }
[features]
std = [
"std-shims/std",
"thiserror",
"zeroize/std",
"rand_core/std",
"monero-wallet/std",
"monero-seed/std",
"polyseed/std",
]
compile-time-generators = ["monero-wallet/compile-time-generators"]
multisig = ["monero-wallet/multisig", "std"]
default = ["std", "compile-time-generators"]

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022-2024 Luke Parker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -6,6 +6,9 @@ This library is isolated as it adds a notable amount of dependencies to the
tree, and to be a subject to a distinct versioning policy. This library may
more frequently undergo breaking API changes.
This library is usable under no-std when the `std` feature (on by default) is
disabled.
### Features
- Support for Monero's seed algorithm

View File

@@ -1,2 +1,9 @@
pub use monero_seed as seed;
pub use monero_polyseed as seed;
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
pub use monero_wallet::*;
/// Seed creation and parsing functionality.
pub mod seed;

View File

@@ -0,0 +1,150 @@
use core::fmt;
use std_shims::string::String;
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
use rand_core::{RngCore, CryptoRng};
pub use monero_seed as original;
pub use polyseed;
use original::{SeedError as OriginalSeedError, Seed as OriginalSeed};
use polyseed::{PolyseedError, Polyseed};
/// An error from working with seeds.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum SeedError {
/// The seed was invalid.
#[cfg_attr(feature = "std", error("invalid seed"))]
InvalidSeed,
/// The entropy was invalid.
#[cfg_attr(feature = "std", error("invalid entropy"))]
InvalidEntropy,
/// The checksum did not match the data.
#[cfg_attr(feature = "std", error("invalid checksum"))]
InvalidChecksum,
/// Unsupported features were enabled.
#[cfg_attr(feature = "std", error("unsupported features"))]
UnsupportedFeatures,
}
impl From<OriginalSeedError> for SeedError {
fn from(error: OriginalSeedError) -> SeedError {
match error {
OriginalSeedError::DeprecatedEnglishWithChecksum | OriginalSeedError::InvalidChecksum => {
SeedError::InvalidChecksum
}
OriginalSeedError::InvalidSeed => SeedError::InvalidSeed,
}
}
}
impl From<PolyseedError> for SeedError {
fn from(error: PolyseedError) -> SeedError {
match error {
PolyseedError::UnsupportedFeatures => SeedError::UnsupportedFeatures,
PolyseedError::InvalidEntropy => SeedError::InvalidEntropy,
PolyseedError::InvalidSeed => SeedError::InvalidSeed,
PolyseedError::InvalidChecksum => SeedError::InvalidChecksum,
}
}
}
/// The type of the seed.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SeedType {
/// The seed format originally used by Monero,
Original(monero_seed::Language),
/// Polyseed.
Polyseed(polyseed::Language),
}
/// A seed, internally either the original format or a Polyseed.
#[derive(Clone, PartialEq, Eq, Zeroize, ZeroizeOnDrop)]
pub enum Seed {
/// The originally formatted seed.
Original(OriginalSeed),
/// A Polyseed.
Polyseed(Polyseed),
}
impl fmt::Debug for Seed {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Seed::Original(_) => f.debug_struct("Seed::Original").finish_non_exhaustive(),
Seed::Polyseed(_) => f.debug_struct("Seed::Polyseed").finish_non_exhaustive(),
}
}
}
impl Seed {
/// Create a new seed.
pub fn new<R: RngCore + CryptoRng>(rng: &mut R, seed_type: SeedType) -> Seed {
match seed_type {
SeedType::Original(lang) => Seed::Original(OriginalSeed::new(rng, lang)),
SeedType::Polyseed(lang) => Seed::Polyseed(Polyseed::new(rng, lang)),
}
}
/// Parse a seed from a string.
pub fn from_string(seed_type: SeedType, words: Zeroizing<String>) -> Result<Seed, SeedError> {
match seed_type {
SeedType::Original(lang) => Ok(OriginalSeed::from_string(lang, words).map(Seed::Original)?),
SeedType::Polyseed(lang) => Ok(Polyseed::from_string(lang, words).map(Seed::Polyseed)?),
}
}
/// Create a seed from entropy.
///
/// A birthday may be optionally provided, denoted in seconds since the epoch. For
/// SeedType::Original, it will be ignored. For SeedType::Polyseed, it'll be embedded into the
/// seed.
///
/// For SeedType::Polyseed, the last 13 bytes of `entropy` must be 0.
// TODO: Return Result, not Option
pub fn from_entropy(
seed_type: SeedType,
entropy: Zeroizing<[u8; 32]>,
birthday: Option<u64>,
) -> Option<Seed> {
match seed_type {
SeedType::Original(lang) => OriginalSeed::from_entropy(lang, entropy).map(Seed::Original),
SeedType::Polyseed(lang) => {
Polyseed::from(lang, 0, birthday.unwrap_or(0), entropy).ok().map(Seed::Polyseed)
}
}
}
/// Converts the seed to a string.
pub fn to_string(&self) -> Zeroizing<String> {
match self {
Seed::Original(seed) => seed.to_string(),
Seed::Polyseed(seed) => seed.to_string(),
}
}
/// Get the entropy for this seed.
pub fn entropy(&self) -> Zeroizing<[u8; 32]> {
match self {
Seed::Original(seed) => seed.entropy(),
Seed::Polyseed(seed) => seed.entropy().clone(),
}
}
/// Get the key derived from this seed.
pub fn key(&self) -> Zeroizing<[u8; 32]> {
match self {
// Original does not differentiate between its entropy and its key
Seed::Original(seed) => seed.entropy(),
Seed::Polyseed(seed) => seed.key(),
}
}
/// Get the birthday of this seed, denoted in seconds since the epoch.
pub fn birthday(&self) -> u64 {
match self {
Seed::Original(_) => 0,
Seed::Polyseed(seed) => seed.birthday(),
}
}
}