Skip to content

Commit

Permalink
Merge pull request #4 from MatthewDarnell/rust-k12
Browse files Browse the repository at this point in the history
Rust k12
  • Loading branch information
MatthewDarnell authored Mar 1, 2024
2 parents 01b0a81 + 5985bf3 commit 958e179
Show file tree
Hide file tree
Showing 260 changed files with 2,118 additions and 132,805 deletions.
12 changes: 6 additions & 6 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ pub mod response;
pub mod transfer;
extern crate crypto;
extern crate identity;
extern crate core;

use identity::get_public_key_from_identity;
use crypto::qubic_identities::get_public_key_from_identity;
use crate::header::{ EntityType, RequestResponseHeader };
use crate::transfer::TransferTransaction;

Expand Down Expand Up @@ -43,7 +42,7 @@ impl QubicApiPacket {
let mut header = RequestResponseHeader::new();
header.set_type(EntityType::RequestEntity);

let data: Vec<u8> = get_public_key_from_identity(id).unwrap();
let data: Vec<u8> = get_public_key_from_identity(&String::from(id)).unwrap().to_vec();
let size = std::mem::size_of::<RequestResponseHeader>() + data.len();
header.set_size(size);
QubicApiPacket {
Expand Down Expand Up @@ -104,16 +103,17 @@ pub mod api_formatting_tests {
#[test]
fn create_identity_balance_request_entity() {
let req = QubicApiPacket::get_identity_balance("EPYWDREDNLHXOFYVGQUKPHJGOMPBSLDDGZDPKVQUMFXAIQYMZGEHPZTAAWON");
println!("{:?}", req);
assert_eq!(req.header._size[0], 40u8);
}

#[test]
fn create_entity_get_full_request_as_bytes() {
let mut req = QubicApiPacket::get_identity_balance("EPYWDREDNLHXOFYVGQUKPHJGOMPBSLDDGZDPKVQUMFXAIQYMZGEHPZTAAWON");
req.header.zero_dejavu(); //Dejavu is random 3 byte value
let bytes = req.as_bytes();
assert_eq!(bytes.len(), 68);
assert_eq!(bytes.len(), 40);
assert_eq!(bytes.as_slice(),
vec![68, 0, 0, 0, 0, 0, 0, 31, 69, 80, 89, 87, 68, 82, 69, 68, 78, 76, 72, 88, 79, 70, 89, 86, 71, 81, 85, 75, 80, 72, 74, 71, 79, 77, 80, 66, 83, 76, 68, 68, 71, 90, 68, 80, 75, 86, 81, 85, 77, 70, 88, 65, 73, 81, 89, 77, 90, 71, 69, 72, 80, 90, 84, 65, 65, 87, 79, 78]);
vec![40, 0, 0, 31, 0, 0, 0, 0, 170, 135, 62, 76, 253, 55, 228, 191, 82, 138, 42, 160, 30, 236, 239, 54, 84, 124, 153, 202, 170, 189, 27, 189, 247, 37, 58, 101, 176, 65, 119, 26]
);
}
}
6 changes: 3 additions & 3 deletions api/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ pub fn get_formatted_response(response: &mut QubicApiPacket) {
match response.api_type {
EntityType::RespondCurrentTickInfo => {
if let Some(peer_id) = &response.peer {
if response.data.len() == 12 {
if response.data.len() < 12 {
println!("Malformed Current Tick Response.");
} else {
let mut data: [u8; 4] = [0; 4];
data[0] = response.data[4];
data[1] = response.data[5];
Expand All @@ -28,8 +30,6 @@ pub fn get_formatted_response(response: &mut QubicApiPacket) {
Ok(_) => {},
Err(_err) => {}
}
} else {
println!("Malformed Current Tick Response.");
}
}
},
Expand Down
9 changes: 7 additions & 2 deletions api/src/response/response_entity.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::QubicApiPacket;
use crate::response::FormatQubicResponseDataToStructure;
use crate::identity::get_identity_from_pub_key;
use crate::crypto::qubic_identities::get_identity;
#[derive(Debug, Clone)]
pub struct ResponseEntity {
pub identity: String,
Expand Down Expand Up @@ -63,7 +63,12 @@ pub fn handle_response_entity(response: &mut QubicApiPacket) -> Option<ResponseE
return None;
}
//println!("Got Response Entity Peer Data: {:?}", data);
let sliced_identity = get_identity_from_pub_key(&data.as_slice()[8..40]);
let mut slice: [u8; 32] = [0; 32];
for (idx, p) in data.as_slice()[8..40].iter().enumerate() {
slice[idx] = *p;
}
let sliced_identity = get_identity(&slice);
//let sliced_identity = get_identity_from_pub_key(&data.as_slice()[8..40]);
let sliced_incoming = &data.as_slice()[8 + 32..8+32 + 8];
let sliced_outgoing = &data.as_slice()[8 + 32 + 8..8+32 + 8 + 8];

Expand Down
55 changes: 30 additions & 25 deletions api/src/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
use std::ffi::c_uchar;
use identity::{Identity, get_public_key_from_identity};
use identity::Identity;
use crypto::hash::k12_bytes;
use crypto::qubic_identities::{get_subseed, get_public_key_from_identity, sign_raw};
use logger::info;
extern {
//extern ECCRYPTO_STATUS SchnorrQ_Sign(const unsigned char* SecretKey, const unsigned char* PublicKey, const unsigned char* Message, const unsigned int SizeMessage, unsigned char* Signature);
fn sign(subseed: *const u8, publicKey: *const c_uchar, messageDigest: *const c_uchar, signature: *mut c_uchar);
//fn SchnorrQ_Sign(subseed: *const u8, publicKey: *const c_uchar, messageDigest: *const c_uchar, SizeMessage: u32, signature: *mut c_uchar);
fn getSubseed(seed: *const c_uchar, subseed: *mut c_uchar) -> bool;
//bool getSubseed(const unsigned char* seed, unsigned char* subseed)
//void sign(const unsigned char* subseed, const unsigned char* publicKey, const unsigned char* messageDigest, unsigned char* signature)
}


#[derive(Debug, Clone)]
pub struct TransferTransaction {
Expand All @@ -33,18 +24,18 @@ impl TransferTransaction {
if source_identity.seed.len() != 55 {
panic!("Trying To Transfer From Corrupted Identity!");
}
let pub_key_src = match get_public_key_from_identity(source_identity.identity.as_str()) {
let pub_key_src = match get_public_key_from_identity(&source_identity.identity) {
Ok(pub_key) => pub_key,
Err(err) => panic!("{:?}", err)
};
let pub_key_dest = match get_public_key_from_identity(dest) {
let pub_key_dest = match get_public_key_from_identity(&String::from(dest)) {
Ok(pub_key) => pub_key,
Err(err) => panic!("{:?}", err)
};

let mut t: TransferTransaction = TransferTransaction {
_source_public_key: pub_key_src.clone(),
_source_destination_public_key: pub_key_dest.clone(),
_source_public_key: pub_key_src.to_vec(),
_source_destination_public_key: pub_key_dest.to_vec(),
_amount: amount,
_tick: tick + TICK_OFFSET,
_input_type: 0,
Expand All @@ -53,15 +44,11 @@ impl TransferTransaction {
};
info!("Setting Expiration Tick For Transaction To {}", tick + TICK_OFFSET);
let digest: Vec<u8> = k12_bytes(&t.as_bytes_without_signature());
let mut sub_seed: [u8; 32] = [0; 32];
unsafe {
getSubseed(source_identity.seed.as_str().as_ptr(), sub_seed.as_mut_ptr());
}
//let mut sub_seed: [u8; 32] = [0; 32];
let mut sub_seed: Vec<u8> = get_subseed(source_identity.seed.as_str()).expect("Failed To Get SubSeed!");
let mut sig: [u8; 64] = [0; 64];
unsafe {
sign(sub_seed.as_ptr(), pub_key_src.as_ptr(), digest.as_ptr(), sig.as_mut_ptr());
//SchnorrQ_Sign(sub_seed.as_ptr(), pub_key_src.as_ptr(), digest.as_ptr(), 32, sig.as_mut_ptr());
}

sig = sign_raw(&sub_seed, &pub_key_src, digest.as_slice().try_into().unwrap());
t._signature = sig.to_vec();
t
}
Expand Down Expand Up @@ -130,9 +117,27 @@ impl TransferTransaction {
#[test]
fn create_transfer() {
let id: Identity = Identity::new("lcehvbvddggkjfnokduyjuiyvkklrvrmsaozwbvjlzvgvfipqpnkkuf");
let t: TransferTransaction = TransferTransaction::from_vars(&id, "EPYWDREDNLHXOFYVGQUKPHJGOMPBSLDDGZDPKVQUMFXAIQYMZGEHPZTAAWON", 100, 100);
let expected: Vec<u8> = vec![170, 135, 62, 76, 253, 55, 228, 191, 82, 138, 42, 160, 30, 236, 239, 54, 84, 124, 153, 202, 170, 189, 27, 189, 247, 37, 58, 101, 176, 65, 119, 26, 170, 135, 62, 76, 253, 55, 228, 191, 82, 138, 42, 160, 30, 236, 239, 54, 84, 124, 153, 202, 170, 189, 27, 189, 247, 37, 58, 101, 176, 65, 119, 26, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 0, 0, 0, 105, 55, 108, 214, 255, 246, 151, 81, 6, 214, 129, 65, 96, 14, 146, 66, 206, 140, 212, 149, 217, 230, 189, 217, 106, 16, 216, 3, 208, 51, 185, 179, 25, 89, 215, 168, 85, 62, 9, 204, 52, 238, 245, 199, 48, 2, 43, 52, 117, 72, 109, 119, 84, 236, 135, 240, 56, 179, 194, 36, 96, 124, 32, 0];
let t: TransferTransaction = TransferTransaction::from_vars(&id, "EPYWDREDNLHXOFYVGQUKPHJGOMPBSLDDGZDPKVQUMFXAIQYMZGEHPZTAAWON", 100, 80);
let expected: Vec<u8> = vec![
//source pub key: u32
170, 135, 62, 76, 253, 55, 228, 191, 82, 138, 42, 160, 30, 236, 239, 54, 84, 124, 153, 202, 170, 189, 27, 189, 247, 37, 58, 101, 176, 65, 119, 26,
//dest pub key: u32
170, 135, 62, 76, 253, 55, 228, 191, 82, 138, 42, 160, 30, 236, 239, 54, 84, 124, 153, 202, 170, 189, 27, 189, 247, 37, 58, 101, 176, 65, 119, 26,
//amount: u64
100, 0, 0, 0, 0, 0, 0, 0,
//tick: u32
110, 0, 0, 0,
//input type: u16
0, 0,
//input size: u16
0, 0,
//signature: u64
179, 108, 100, 1, 209, 21, 45, 198, 110, 190, 137, 194, 107, 157, 36, 76, 124, 94, 142, 45, 125, 220, 238, 70, 17, 253, 181, 125, 147, 192, 126,
93, 7, 155, 196, 186, 185, 143, 220, 131, 215, 170, 241, 92, 83, 71, 181, 143, 107, 62, 90, 232, 10, 164, 55, 202, 24, 189, 84, 156, 203, 51, 27, 0
];

assert_eq!(t.as_bytes().as_slice(), expected.as_slice());

}


Expand Down
6 changes: 1 addition & 5 deletions crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
name = "crypto"
version = "0.1.0"
edition = "2021"
build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
base64 = "0.21.2"
bcrypt = "0.15.0"
libc = "0.2.147"
sodiumoxide = "0.2.7"
logger = { path = '../logger' }

[build-dependencies]
cc = "1.0.79"
tiny-keccak = { version = "2.0.2", default-features = false, features = ["k12"]}

[features]
random = []
Expand Down
86 changes: 0 additions & 86 deletions crypto/build.rs

This file was deleted.

7 changes: 7 additions & 0 deletions crypto/src/fourq.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Thanks To Mineco for Native Rust FourQ!: https://github.com/Mineco1006/qubic-utils
*/

pub mod consts;
pub mod types;
pub mod ops;
Loading

0 comments on commit 958e179

Please sign in to comment.