Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hansieodendaal committed Aug 11, 2024
1 parent 310a470 commit 1b33a7d
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 63 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions applications/minotari_ledger_wallet/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,17 @@ license = "BSD-3-Clause"
edition = "2021"

[dependencies]
#tari_crypto = { version = "0.20.3", default-features = false, features = ["borsh"]}
#tari_hashing = { path = "../../../hashing", version = "1.1.0-pre.2" }

#blake2 = { version = "0.10", default-features = false }
borsh = { version = "1.2", default-features = false, features = ["unstable__schema"] }
#digest = { version = "0.10", default-features = false }
#embedded-alloc = "0.5.0"
#spin = "0.9.8"

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tari_target_network_mainnet)', 'cfg(tari_target_network_nextnet)'] }

[dev-dependencies]
tari_script = { path = "../../../infrastructure/tari_script", version = "1.1.0-pre.2" }
62 changes: 62 additions & 0 deletions applications/minotari_ledger_wallet/common/src/borsch.rs
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 applications/minotari_ledger_wallet/common/src/common_types.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
// 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.
// SPDX-License-Identifier: BSD-3-Clause

use alloc::string::String;

Expand Down
28 changes: 7 additions & 21 deletions applications/minotari_ledger_wallet/common/src/lib.rs
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};
67 changes: 67 additions & 0 deletions applications/minotari_ledger_wallet/common/src/network.rs
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
}
}
52 changes: 30 additions & 22 deletions applications/minotari_ledger_wallet/common/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
// 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.

use alloc::string::{String, ToString};
// SPDX-License-Identifier: BSD-3-Clause

use alloc::{
borrow::ToOwned,
string::{String, ToString},
vec::Vec,
};

pub const PUSH_PUBKEY_IDENTIFIER: &str = "217e";

/// Convert a u16 to a string
pub fn u16_to_string(number: u16) -> String {
let mut buffer = [0u8; 6]; // Maximum length for a 16-bit integer (including null terminator)
let mut pos = 0;
Expand Down Expand Up @@ -50,3 +38,23 @@ pub fn u16_to_string(number: u16) -> String {

String::from_utf8_lossy(&buffer[..pos]).to_string()
}

/// Convert a hex string to serialized bytes made up as an identifier concatenated with data
pub fn hex_to_bytes_serialized(identifier: &str, data: &str) -> Result<Vec<u8>, String> {
if identifier.len() % 2 != 0 {
return Err("Invalid identifier".to_string());
}
if data.len() % 2 != 0 {
return Err("Invalid payload".to_string());
}

let hex = identifier.to_owned() + data;
let hex = hex.as_str();

let mut serialized = Vec::new();
for i in 0..hex.len() / 2 {
let byte = u8::from_str_radix(&hex[i * 2..i * 2 + 2], 16).map_err(|_e| "Invalid hex string".to_string())?;
serialized.push(byte);
}
Ok(serialized)
}
3 changes: 3 additions & 0 deletions applications/minotari_ledger_wallet/comms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
63 changes: 63 additions & 0 deletions applications/minotari_ledger_wallet/comms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,66 @@
pub mod accessor_methods;
pub mod error;
pub mod ledger_wallet;

#[cfg(test)]
mod test {
use borsh::BorshSerialize;
use minotari_ledger_wallet_common::{hex_to_bytes_serialized, PUSH_PUBKEY_IDENTIFIER};
use rand::rngs::OsRng;
use tari_crypto::{
keys::{PublicKey, SecretKey},
ristretto::{RistrettoPublicKey, RistrettoSecretKey},
};
use tari_script::{script, slice_to_boxed_message};
use tari_utilities::{hex::Hex, ByteArray};

const NOP_IDENTIFIER: &str = "0173";
const PUSH_ONE_IDENTIFIER: &str = "017c";
const CHECK_SIG_VERIFY_IDENTIFIER: &str = "21ad";

#[test]
// This is testing the serialization of the 'PushPubKey' script and the byte representation of the script as needed
// for native serialization in the tari ledger wallet code. Other script types where the exact hex representation of
// the script payload could easily be determined are tested in the same way to verify the concept.
// This test should highlight if any changes are made to the script serialization. Primary script:
// - 'script!(PushPubKey(Box::new(<PUB_KEY>)))'
// and additional ones used for testing:
// - 'script!(Nop)'
// - 'script!(PushOne)'
// - `CheckSigVerify(<MESSAGE>))`
fn test_push_pub_key_serialized_byte_representation() {
let mut scripts = Vec::new();

scripts.push((script!(Nop), NOP_IDENTIFIER, "".to_string()));
scripts.push((script!(PushOne), PUSH_ONE_IDENTIFIER, "".to_string()));

for pub_key in [
RistrettoPublicKey::default(),
RistrettoPublicKey::from_secret_key(&RistrettoSecretKey::random(&mut OsRng)),
] {
scripts.push((
script!(PushPubKey(Box::new(pub_key.clone()))),
PUSH_PUBKEY_IDENTIFIER,
pub_key.to_hex(),
));
}

let key = RistrettoSecretKey::random(&mut OsRng);
let msg = slice_to_boxed_message(key.as_bytes());
scripts.push((script!(CheckSigVerify(msg)), CHECK_SIG_VERIFY_IDENTIFIER, key.to_hex()));

for (script, hex_identifier, hex_payload) in scripts {
let mut serialized = Vec::new();
script.serialize(&mut serialized).unwrap();
let hex_data = hex_identifier.to_owned() + &hex_payload;
assert_eq!(hex_data, serialized.to_vec().to_hex());
assert_eq!(
hex_to_bytes_serialized(hex_identifier, &hex_payload).unwrap(),
serialized.as_slice(),
"Change in script serialization detected: {:?}, expected {}",
script,
hex_identifier
);
}
}
}
1 change: 1 addition & 0 deletions common/src/configuration/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl FromStr for Network {
}
}
}

impl TryFrom<String> for Network {
type Error = ConfigurationError;

Expand Down
1 change: 1 addition & 0 deletions infrastructure/tari_script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ thiserror = "1.0.30"

[dev-dependencies]
rand = "0.8"
hex = "0.4"

0 comments on commit 1b33a7d

Please sign in to comment.