mirror of
https://github.com/jwinterm/grin-wallet.git
synced 2026-11-16 04:03:53 +01:00
Normalize line endings
This commit is contained in:
@@ -1,384 +0,0 @@
|
||||
// Copyright 2021 The Grin Developers
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Simple serde byte array serializer, assumes target already
|
||||
//! knows how to serialize itself into binary (because that all
|
||||
//! this serializer can do)
|
||||
|
||||
use serde::de::Visitor;
|
||||
use serde::{de, ser, Deserialize, Serialize};
|
||||
use std;
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
Message(String),
|
||||
}
|
||||
|
||||
impl ser::Error for Error {
|
||||
fn custom<T: Display>(msg: T) -> Self {
|
||||
Error::Message(msg.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Error for Error {
|
||||
fn custom<T: Display>(msg: T) -> Self {
|
||||
Error::Message(msg.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Error::Message(msg) => formatter.write_str(msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
pub struct ByteSerializer {
|
||||
output: Vec<u8>,
|
||||
}
|
||||
|
||||
pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let mut serializer = ByteSerializer { output: vec![] };
|
||||
value.serialize(&mut serializer)?;
|
||||
Ok(serializer.output)
|
||||
}
|
||||
|
||||
impl<'a> ser::Serializer for &'a mut ByteSerializer {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
type SerializeSeq = Self;
|
||||
type SerializeTuple = Self;
|
||||
type SerializeTupleStruct = Self;
|
||||
type SerializeTupleVariant = Self;
|
||||
type SerializeMap = Self;
|
||||
type SerializeStruct = Self;
|
||||
type SerializeStructVariant = Self;
|
||||
|
||||
fn serialize_bool(self, _: bool) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_i8(self, _: i8) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_i16(self, _: i16) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_i32(self, _: i32) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_i64(self, _: i64) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_u8(self, _: u8) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_u16(self, _: u16) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_u32(self, _: u32) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_u64(self, _: u64) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_f32(self, _: f32) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_f64(self, _: f64) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_char(self, _: char) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_str(self, _: &str) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_bytes(self, v: &[u8]) -> Result<()> {
|
||||
for byte in v {
|
||||
self.output.push(*byte)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_none(self) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_some<T>(self, _value: &T) -> Result<()>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_unit(self) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: u32,
|
||||
_variant: &'static str,
|
||||
) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<()>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_newtype_variant<T>(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: u32,
|
||||
_variant: &'static str,
|
||||
_value: &T,
|
||||
) -> Result<()>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_len: usize,
|
||||
) -> Result<Self::SerializeTupleStruct> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: u32,
|
||||
_variant: &'static str,
|
||||
_len: usize,
|
||||
) -> Result<Self::SerializeTupleVariant> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: u32,
|
||||
_variant: &'static str,
|
||||
_len: usize,
|
||||
) -> Result<Self::SerializeStructVariant> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeSeq for &'a mut ByteSerializer {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_element<T>(&mut self, _value: &T) -> Result<()>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn end(self) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeTuple for &'a mut ByteSerializer {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_element<T>(&mut self, _value: &T) -> Result<()>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn end(self) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeTupleStruct for &'a mut ByteSerializer {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T>(&mut self, _value: &T) -> Result<()>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn end(self) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeTupleVariant for &'a mut ByteSerializer {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T>(&mut self, _value: &T) -> Result<()>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn end(self) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeMap for &'a mut ByteSerializer {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_key<T>(&mut self, _key: &T) -> Result<()>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_value<T>(&mut self, _value: &T) -> Result<()>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn end(self) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeStruct for &'a mut ByteSerializer {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<()>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn end(self) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeStructVariant for &'a mut ByteSerializer {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<()>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn end(self) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
// Simple Deserializer
|
||||
|
||||
pub struct ByteDeserializer<'de> {
|
||||
input: &'de [u8],
|
||||
}
|
||||
|
||||
impl<'de> ByteDeserializer<'de> {
|
||||
pub fn from_bytes(input: &'de [u8]) -> Self {
|
||||
ByteDeserializer { input }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_bytes<'a, T>(b: &'a [u8]) -> Result<T>
|
||||
where
|
||||
T: Deserialize<'a>,
|
||||
{
|
||||
let mut deserializer = ByteDeserializer::from_bytes(b);
|
||||
let t = T::deserialize(&mut deserializer)?;
|
||||
Ok(t)
|
||||
}
|
||||
|
||||
impl<'de, 'a> de::Deserializer<'de> for &'a mut ByteDeserializer<'de> {
|
||||
type Error = Error;
|
||||
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_bytes(self.input)
|
||||
}
|
||||
|
||||
serde::forward_to_deserialize_any! {
|
||||
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
|
||||
bytes byte_buf option unit unit_struct newtype_struct seq tuple
|
||||
tuple_struct map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
// Copyright 2021 The Grin Developers
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Utilities and re-exports
|
||||
|
||||
#![deny(non_upper_case_globals)]
|
||||
#![deny(non_camel_case_types)]
|
||||
#![deny(non_snake_case)]
|
||||
#![deny(unused_mut)]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
mod ov3;
|
||||
pub use ov3::OnionV3Address;
|
||||
pub use ov3::OnionV3Error as OnionV3AddressError;
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub mod byte_ser;
|
||||
-204
@@ -1,204 +0,0 @@
|
||||
// Copyright 2021 The Grin Developers
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use data_encoding::BASE32;
|
||||
use ed25519_dalek::PublicKey as DalekPublicKey;
|
||||
use ed25519_dalek::SecretKey as DalekSecretKey;
|
||||
use grin_util::from_hex;
|
||||
use sha3::{Digest, Sha3_256};
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq, thiserror::Error, Serialize, Deserialize)]
|
||||
/// OnionV3 Address Errors
|
||||
pub enum OnionV3Error {
|
||||
/// Error decoding an address from a string
|
||||
#[error("Address Decoding: {0}")]
|
||||
AddressDecoding(String),
|
||||
/// Error with given private key
|
||||
#[error("Invalid Private Key: {0}")]
|
||||
InvalidPrivateKey(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
/// Struct to hold an onion V3 address, represented internally as a raw
|
||||
/// ed25519 public key
|
||||
pub struct OnionV3Address([u8; 32]);
|
||||
|
||||
impl OnionV3Address {
|
||||
/// from bytes
|
||||
pub fn from_bytes(bytes: [u8; 32]) -> Self {
|
||||
OnionV3Address(bytes)
|
||||
}
|
||||
|
||||
/// as bytes
|
||||
pub fn as_bytes(&self) -> &[u8; 32] {
|
||||
&self.0
|
||||
}
|
||||
|
||||
/// populate from a private key
|
||||
pub fn from_private(key: &[u8; 32]) -> Result<Self, OnionV3Error> {
|
||||
let d_skey = match DalekSecretKey::from_bytes(key) {
|
||||
Ok(k) => k,
|
||||
Err(e) => {
|
||||
return Err(OnionV3Error::InvalidPrivateKey(format!(
|
||||
"Unable to create public key: {}",
|
||||
e
|
||||
)));
|
||||
}
|
||||
};
|
||||
let d_pub_key: DalekPublicKey = (&d_skey).into();
|
||||
Ok(OnionV3Address(*d_pub_key.as_bytes()))
|
||||
}
|
||||
|
||||
/// return dalek public key
|
||||
pub fn to_ed25519(&self) -> Result<DalekPublicKey, OnionV3Error> {
|
||||
let d_skey = match DalekPublicKey::from_bytes(&self.0) {
|
||||
Ok(k) => k,
|
||||
Err(e) => {
|
||||
return Err(OnionV3Error::InvalidPrivateKey(format!(
|
||||
"Unable to create dalek public key: {}",
|
||||
e
|
||||
)));
|
||||
}
|
||||
};
|
||||
Ok(d_skey)
|
||||
}
|
||||
|
||||
/// Return as onion v3 address string
|
||||
pub fn to_ov3_str(&self) -> String {
|
||||
// calculate checksum
|
||||
let mut hasher = Sha3_256::new();
|
||||
hasher.input(b".onion checksum");
|
||||
hasher.input(self.0);
|
||||
hasher.input([0x03u8]);
|
||||
let checksum = hasher.result();
|
||||
|
||||
let mut address_bytes = self.0.to_vec();
|
||||
address_bytes.push(checksum[0]);
|
||||
address_bytes.push(checksum[1]);
|
||||
address_bytes.push(0x03u8);
|
||||
|
||||
let ret = BASE32.encode(&address_bytes);
|
||||
ret.to_lowercase()
|
||||
}
|
||||
|
||||
/// return as http url
|
||||
pub fn to_http_str(&self) -> String {
|
||||
format!("http://{}.onion", self.to_ov3_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for OnionV3Address {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.to_ov3_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for OnionV3Address {
|
||||
type Error = OnionV3Error;
|
||||
|
||||
fn try_from(input: &str) -> Result<Self, Self::Error> {
|
||||
// First attempt to decode a pubkey from hex
|
||||
if let Ok(b) = from_hex(input) {
|
||||
if b.len() == 32 {
|
||||
let mut retval = OnionV3Address([0; 32]);
|
||||
retval.0.copy_from_slice(&b[0..32]);
|
||||
return Ok(retval);
|
||||
} else {
|
||||
return Err(OnionV3Error::AddressDecoding(
|
||||
"(Interpreted as Hex String) Public key is wrong length".to_owned(),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
// Otherwise try to parse as onion V3 address
|
||||
let mut input = input.to_uppercase();
|
||||
if input.starts_with("HTTP://") || input.starts_with("HTTPS://") {
|
||||
input = input.replace("HTTP://", "");
|
||||
input = input.replace("HTTPS://", "");
|
||||
}
|
||||
if input.ends_with(".ONION") {
|
||||
input = input.replace(".ONION", "");
|
||||
}
|
||||
let orig_address_raw = input.clone();
|
||||
// for now, just check input is the right length and try and decode from base32
|
||||
if input.len() != 56 {
|
||||
return Err(OnionV3Error::AddressDecoding(
|
||||
"(Interpreted as Base32 String) Input address is wrong length".to_owned(),
|
||||
));
|
||||
}
|
||||
let address = match BASE32.decode(input.as_bytes()) {
|
||||
Ok(a) => a,
|
||||
Err(_) => {
|
||||
return Err(OnionV3Error::AddressDecoding(
|
||||
"(Interpreted as Base32 String) Input address is not base 32".to_owned(),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let mut retval = OnionV3Address([0; 32]);
|
||||
retval.0.copy_from_slice(&address[0..32]);
|
||||
|
||||
let test_v3 = retval.to_ov3_str();
|
||||
if test_v3.to_uppercase() != orig_address_raw.to_uppercase() {
|
||||
return Err(OnionV3Error::AddressDecoding(
|
||||
"(Interpreted as Base32 String) Provided onion V3 address is invalid (no match)"
|
||||
.to_owned(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(retval)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use std::convert::TryInto;
|
||||
|
||||
#[test]
|
||||
fn onion_v3() -> Result<(), OnionV3Error> {
|
||||
let onion_address_str = "2a6at2obto3uvkpkitqp4wxcg6u36qf534eucbskqciturczzc5suyid";
|
||||
let onion_address: OnionV3Address = onion_address_str.try_into()?;
|
||||
|
||||
println!("Onion address: {:?}", onion_address);
|
||||
let raw_pubkey_str = "d03c09e9c19bb74aa9ea44e0fe5ae237a9bf40bddf0941064a80913a4459c8bb";
|
||||
let onion_address_2: OnionV3Address = raw_pubkey_str.try_into()?;
|
||||
|
||||
assert_eq!(onion_address, onion_address_2);
|
||||
|
||||
// invalid hex string, should be interpreted as base32 and fail
|
||||
let raw_pubkey_str = "d03c09e9c19bb74aa9ea44e0fe5ae237a9bf40bddf0941064a80913a4459c8bx";
|
||||
let ret: Result<OnionV3Address, OnionV3Error> = raw_pubkey_str.try_into();
|
||||
assert!(ret.is_err());
|
||||
|
||||
// wrong length hex string, should be interpreted as base32 and fail
|
||||
let raw_pubkey_str = "d03c09e9c19bb74aa9ea44e0fe5ae237a9bf40bddf0941064a80913a4459c8bbff";
|
||||
let ret: Result<OnionV3Address, OnionV3Error> = raw_pubkey_str.try_into();
|
||||
assert!(ret.is_err());
|
||||
|
||||
// wrong length ov3 string
|
||||
let onion_address_str = "2a6at2obto3uvkpkitqp4wxcg6u36qf534eucbskqciturczzc5suyidx";
|
||||
let ret: Result<OnionV3Address, OnionV3Error> = onion_address_str.try_into();
|
||||
assert!(ret.is_err());
|
||||
|
||||
// not base 32 ov3 string
|
||||
let onion_address_str = "2a6at2obto3uvkpkitqp4wxcg6u36qf534eucbskqciturczzc5suyi-";
|
||||
let ret: Result<OnionV3Address, OnionV3Error> = onion_address_str.try_into();
|
||||
assert!(ret.is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user