Implemented DNS on blockchain. Beautified a lot of code, fixed some things.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use sqlite::{Connection, State, Statement};
|
||||
|
||||
use crate::{Block, Bytes, Keystore, Transaction, Settings};
|
||||
use crate::{Block, Bytes, Keystore, Transaction};
|
||||
use crate::settings::Settings;
|
||||
|
||||
const DB_NAME: &str = "blockchain.db";
|
||||
|
||||
@@ -175,6 +176,30 @@ impl Blockchain {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn get_domain_info(&self, domain: &str) -> Option<String> {
|
||||
if domain.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let identity_hash = Transaction::hash_identity(domain);
|
||||
|
||||
let mut statement = self.db.prepare("SELECT * FROM transactions WHERE identity = ? ORDER BY id DESC LIMIT 1;").unwrap();
|
||||
statement.bind(1, identity_hash.as_bytes()).expect("Error in bind");
|
||||
while let State::Row = statement.next().unwrap() {
|
||||
let identity = Bytes::from_bytes(statement.read::<Vec<u8>>(1).unwrap().as_slice());
|
||||
let confirmation = Bytes::from_bytes(statement.read::<Vec<u8>>(2).unwrap().as_slice());
|
||||
let method = statement.read::<String>(3).unwrap();
|
||||
let data = statement.read::<String>(4).unwrap();
|
||||
let pub_key = Bytes::from_bytes(statement.read::<Vec<u8>>(5).unwrap().as_slice());
|
||||
let signature = Bytes::from_bytes(statement.read::<Vec<u8>>(6).unwrap().as_slice());
|
||||
let transaction = Transaction { identity, confirmation, method, data, pub_key, signature };
|
||||
println!("Got transaction: {:?}", &transaction);
|
||||
if transaction.check_for(domain) {
|
||||
return Some(transaction.data);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn last_block(&self) -> Option<Block> {
|
||||
self.last_block.clone()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
use crate::Context;
|
||||
use std::sync::{Mutex, Arc};
|
||||
use crate::dns::filter::DnsFilter;
|
||||
use crate::dns::protocol::{DnsPacket, QueryType, DnsRecord, DnsQuestion};
|
||||
|
||||
pub struct BlockchainFilter {
|
||||
context: Arc<Mutex<Context>>
|
||||
}
|
||||
|
||||
impl BlockchainFilter {
|
||||
pub fn new(context: Arc<Mutex<Context>>) -> Self {
|
||||
BlockchainFilter { context }
|
||||
}
|
||||
}
|
||||
|
||||
impl DnsFilter for BlockchainFilter {
|
||||
fn lookup(&self, qname: &str, qtype: QueryType) -> Option<DnsPacket> {
|
||||
let data = self.context.lock().unwrap().blockchain.get_domain_info(qname);
|
||||
match data {
|
||||
None => { println!("Not found info for domain {}", &qname); }
|
||||
Some(data) => {
|
||||
let records: Vec<DnsRecord> = match serde_json::from_str(&data) {
|
||||
Err(_) => { return None; }
|
||||
Ok(records) => { records }
|
||||
};
|
||||
let mut answers: Vec<DnsRecord> = Vec::new();
|
||||
for mut record in records {
|
||||
if record.get_querytype() == qtype {
|
||||
match &mut record {
|
||||
// TODO make it for all types of records
|
||||
DnsRecord::A { domain, .. } | DnsRecord::AAAA { domain, .. } if domain == "@" => {
|
||||
*domain = String::from(qname);
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
|
||||
answers.push(record);
|
||||
}
|
||||
}
|
||||
if !answers.is_empty() {
|
||||
// Create DnsPacket
|
||||
let mut packet = DnsPacket::new();
|
||||
packet.questions.push(DnsQuestion::new(String::from(qname), qtype));
|
||||
for answer in answers {
|
||||
packet.answers.push(answer);
|
||||
}
|
||||
return Some(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
pub mod transaction;
|
||||
pub mod block;
|
||||
pub mod blockchain;
|
||||
pub mod filter;
|
||||
|
||||
pub use transaction::Transaction;
|
||||
pub use block::Block;
|
||||
|
||||
@@ -67,6 +67,12 @@ impl Transaction {
|
||||
digest.result(&mut buf);
|
||||
Bytes::from_bytes(&buf)
|
||||
}
|
||||
|
||||
pub fn check_for(&self, domain: &str) -> bool {
|
||||
let hash = Self::hash_identity(&domain);
|
||||
let confirmation = Self::hash_with_key(&domain, &self.pub_key);
|
||||
self.identity.eq(&hash) && self.confirmation.eq(&confirmation)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Transaction {
|
||||
|
||||
Reference in New Issue
Block a user