Skip to content

Commit

Permalink
Add get_public_key_from_identity function
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewDarnell committed Mar 1, 2024
1 parent 53fb87a commit 85f0fe4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
4 changes: 2 additions & 2 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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 +43,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
16 changes: 9 additions & 7 deletions api/src/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +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 };
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);
Expand Down Expand Up @@ -33,18 +34,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,13 +54,14 @@ 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];
//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!");
unsafe {
getSubseed(source_identity.seed.as_str().as_ptr(), sub_seed.as_mut_ptr());
}
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());
sign(sub_seed.as_slice().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());
}
t._signature = sig.to_vec();
Expand Down
45 changes: 44 additions & 1 deletion crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,41 @@ pub mod qubic_identities {
String::from_utf8(identity.to_vec()).unwrap()
}

pub fn get_public_key_from_identity(identity: &String) -> Result<[u8; 32], bool> {
let id: &[u8] = identity.as_bytes();
let mut public_key: [u8; 32] = [0; 32];
for i in 0..4 {
public_key[i << 3..((i<<3) + 8)].copy_from_slice(&u64::to_le_bytes(0u64));
for j in 0..14 {
let index = 14 - j - 1;
if id[i * 14 + index] < b'A' || (id[i * 14 + index]) > b'Z' {
return Err(false);
}
let _bytes: [u8; 8] = public_key[i << 3..((i << 3) + 8)].try_into().unwrap();
let temp: u64 = u64::from_le_bytes(_bytes) * 26u64 +
((id[i * 14 + index] - b'A') as u64);
public_key[i << 3..((i<<3) + 8)].copy_from_slice(&u64::to_le_bytes(temp));

}
}
#[allow(unused_assignments)]
let mut identity_bytes_checksum: u32 = 0;
let hash: Vec<u8> = k12_bytes(&public_key.to_vec());
let bytes: [u8; 4] = hash[0..4].try_into().unwrap();
identity_bytes_checksum = u32::from_le_bytes(bytes);
identity_bytes_checksum &= 0x3FFFF;
for i in 0..4 {
if (identity_bytes_checksum % 26) as u8 + b'A' != identity.as_bytes()[56 + i] {
return Err(false)
}
identity_bytes_checksum /= 26;
}
Ok(public_key)
}

#[cfg(test)]
pub mod qubic_identity_primitive_tests {
use crate::qubic_identities::{get_identity, get_private_key, get_public_key, get_subseed};
use crate::qubic_identities::{get_identity, get_private_key, get_public_key, get_public_key_from_identity, get_subseed};
#[test]
fn get_a_subseed() {
let seed = "lcehvbvddggkjfnokduyjuiyvkklrvrmsaozwbvjlzvgvfipqpnkkuf";
Expand Down Expand Up @@ -146,6 +178,17 @@ pub mod qubic_identities {
let identity = get_identity(&public_key);
assert_eq!(identity, "EPYWDREDNLHXOFYVGQUKPHJGOMPBSLDDGZDPKVQUMFXAIQYMZGEHPZTAAWON".to_string())
}
#[test]
fn get_a_public_key_from_identity() {
let seed = "lcehvbvddggkjfnokduyjuiyvkklrvrmsaozwbvjlzvgvfipqpnkkuf";
let subseed = get_subseed(seed).unwrap();
let private_key = get_private_key(&subseed);
let public_key = get_public_key(&private_key);
let identity = get_identity(&public_key);
let pub_key_from_id = get_public_key_from_identity(&identity).unwrap();

assert_eq!(public_key, pub_key_from_id)
}
}
}

Expand Down

0 comments on commit 85f0fe4

Please sign in to comment.