forked from tari-project/tari
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
310a470
commit 1b33a7d
Showing
11 changed files
with
255 additions
and
63 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2024 The Tari Project | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
use core::result::Result; | ||
|
||
use borsh::{io::Write, BorshDeserialize, BorshSerialize}; | ||
|
||
// Define custom error types to replace std::io::Error | ||
#[derive(Debug)] | ||
pub struct DeserializeError; | ||
|
||
#[derive(Debug)] | ||
pub struct SerializeError; | ||
|
||
pub trait FromBytes<T: BorshDeserialize> { | ||
fn borsh_from_bytes(buf: &mut &[u8]) -> Result<T, DeserializeError>; | ||
} | ||
|
||
impl<T: BorshDeserialize> FromBytes<T> for T { | ||
fn borsh_from_bytes(buf: &mut &[u8]) -> Result<T, DeserializeError> { | ||
T::deserialize(buf).map_err(|_| DeserializeError) | ||
} | ||
} | ||
|
||
pub trait SerializedSize { | ||
fn get_serialized_size(&self) -> Result<usize, SerializeError>; | ||
} | ||
|
||
impl<T: BorshSerialize> SerializedSize for T { | ||
fn get_serialized_size(&self) -> Result<usize, SerializeError> { | ||
let mut counter = ByteCounter::new(); | ||
self.serialize(&mut counter).map_err(|_| SerializeError)?; | ||
Ok(counter.get()) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct ByteCounter { | ||
count: usize, | ||
} | ||
|
||
impl ByteCounter { | ||
pub fn new() -> Self { | ||
Default::default() | ||
} | ||
|
||
pub fn get(&self) -> usize { | ||
self.count | ||
} | ||
} | ||
|
||
impl Write for ByteCounter { | ||
fn write(&mut self, buf: &[u8]) -> Result<usize, borsh::io::Error> { | ||
let len = buf.len(); | ||
self.count += len; | ||
Ok(len) | ||
} | ||
|
||
fn flush(&mut self) -> Result<(), borsh::io::Error> { | ||
Ok(()) | ||
} | ||
} |
21 changes: 1 addition & 20 deletions
21
applications/minotari_ledger_wallet/common/src/common_types.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,17 @@ | ||
// Copyright 2024 The Tari Project | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the | ||
// following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following | ||
// disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the | ||
// following disclaimer in the documentation and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote | ||
// products derived from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
// Copyright 2024 The Tari Project | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#![no_std] | ||
#![no_main] | ||
#![feature(alloc_error_handler)] | ||
|
||
//! # Common types shared by the Ledger application and the rest of the Tari codebase. | ||
/// Note: `ledger-device-rust-sdk` cannot be included in this crate as it can only be compiled for no-std and the | ||
/// rest of the Tari code base is compiled for std. | ||
extern crate alloc; | ||
|
||
pub mod borsch; | ||
pub mod common_types; | ||
pub mod network; | ||
mod utils; | ||
pub use utils::{hex_to_bytes_serialized, PUSH_PUBKEY_IDENTIFIER}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2024 The Tari Project | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
/// Represents the available Tari p2p networks. Only nodes with matching byte values will be able to connect, so these | ||
/// should never be changed once released. | ||
#[repr(u8)] | ||
#[derive(Debug, Copy, Clone, PartialEq)] | ||
pub enum Network { | ||
MainNet = 0x00, | ||
StageNet = 0x01, | ||
NextNet = 0x02, | ||
LocalNet = 0x10, | ||
Igor = 0x24, | ||
Esmeralda = 0x26, | ||
} | ||
|
||
impl Network { | ||
pub fn as_byte(self) -> u8 { | ||
self as u8 | ||
} | ||
|
||
pub const fn as_key_str(self) -> &'static str { | ||
#[allow(clippy::enum_glob_use)] | ||
use Network::*; | ||
match self { | ||
MainNet => "mainnet", | ||
StageNet => "stagenet", | ||
NextNet => "nextnet", | ||
Igor => "igor", | ||
Esmeralda => "esmeralda", | ||
LocalNet => "localnet", | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<u8> for Network { | ||
type Error = (); | ||
|
||
fn try_from(v: u8) -> Result<Self, ()> { | ||
match v { | ||
x if x == Network::MainNet as u8 => Ok(Network::MainNet), | ||
x if x == Network::StageNet as u8 => Ok(Network::StageNet), | ||
x if x == Network::NextNet as u8 => Ok(Network::NextNet), | ||
x if x == Network::LocalNet as u8 => Ok(Network::LocalNet), | ||
x if x == Network::Igor as u8 => Ok(Network::Igor), | ||
x if x == Network::Esmeralda as u8 => Ok(Network::Esmeralda), | ||
_ => Err(()), | ||
} | ||
} | ||
} | ||
|
||
impl Default for Network { | ||
#[cfg(tari_target_network_mainnet)] | ||
fn default() -> Self { | ||
Network::StageNet | ||
} | ||
|
||
#[cfg(tari_target_network_nextnet)] | ||
fn default() -> Self { | ||
Network::NextNet | ||
} | ||
|
||
#[cfg(not(any(tari_target_network_mainnet, tari_target_network_nextnet)))] | ||
fn default() -> Self { | ||
Network::Esmeralda | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,6 @@ thiserror = "1.0.26" | |
rand = "0.8" | ||
once_cell = "1.19.0" | ||
log = "0.4.20" | ||
|
||
[dev-dependencies] | ||
borsh = "1.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,4 @@ thiserror = "1.0.30" | |
|
||
[dev-dependencies] | ||
rand = "0.8" | ||
hex = "0.4" |