2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2018-03-12 21:46:27 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2018-03-12 21:46:27 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2018-03-12 21:46:27 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2018-03-12 21:46:27 +01:00
|
|
|
|
|
|
|
use futures::{Future, future, IntoFuture};
|
|
|
|
use ethabi::{Address, Bytes};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use keccak_hash::keccak;
|
|
|
|
|
2018-09-13 11:04:39 +02:00
|
|
|
use_contract!(registrar, "res/registrar.json");
|
2018-03-12 21:46:27 +01:00
|
|
|
|
|
|
|
// Maps a domain name to an Ethereum address
|
|
|
|
const DNS_A_RECORD: &'static str = "A";
|
|
|
|
|
2019-08-27 17:29:33 +02:00
|
|
|
pub type Asynchronous = Box<dyn Future<Item=Bytes, Error=String> + Send>;
|
2018-03-12 21:46:27 +01:00
|
|
|
pub type Synchronous = Result<Bytes, String>;
|
|
|
|
|
|
|
|
/// Registrar is dedicated interface to access the registrar contract
|
|
|
|
/// which in turn generates an address when a client requests one
|
|
|
|
pub struct Registrar {
|
2019-08-27 17:29:33 +02:00
|
|
|
client: Arc<dyn RegistrarClient<Call=Asynchronous>>,
|
2018-03-12 21:46:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Registrar {
|
|
|
|
/// Registrar constructor
|
2019-08-27 17:29:33 +02:00
|
|
|
pub fn new(client: Arc<dyn RegistrarClient<Call=Asynchronous>>) -> Self {
|
2018-03-12 21:46:27 +01:00
|
|
|
Self {
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate an address for the given key
|
2019-08-27 17:29:33 +02:00
|
|
|
pub fn get_address<'a>(&self, key: &'a str) -> Box<dyn Future<Item = Address, Error = String> + Send> {
|
2018-03-12 21:46:27 +01:00
|
|
|
// Address of the registrar itself
|
|
|
|
let registrar_address = match self.client.registrar_address() {
|
|
|
|
Ok(a) => a,
|
|
|
|
Err(e) => return Box::new(future::err(e)),
|
|
|
|
};
|
|
|
|
|
2018-09-04 20:13:51 +02:00
|
|
|
let hashed_key: [u8; 32] = keccak(key).into();
|
2018-09-13 11:04:39 +02:00
|
|
|
let id = registrar::functions::get_address::encode_input(hashed_key, DNS_A_RECORD);
|
2018-03-12 21:46:27 +01:00
|
|
|
|
2018-09-13 11:04:39 +02:00
|
|
|
let future = self.client.call_contract(registrar_address, id)
|
|
|
|
.and_then(move |address| registrar::functions::get_address::decode_output(&address).map_err(|e| e.to_string()));
|
2018-03-12 21:46:27 +01:00
|
|
|
|
|
|
|
Box::new(future)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Registrar contract interface
|
|
|
|
/// Should execute transaction using current blockchain state.
|
|
|
|
pub trait RegistrarClient: Send + Sync {
|
|
|
|
/// Specifies synchronous or asynchronous communication
|
|
|
|
type Call: IntoFuture<Item=Bytes, Error=String>;
|
|
|
|
|
|
|
|
/// Get registrar address
|
|
|
|
fn registrar_address(&self) -> Result<Address, String>;
|
|
|
|
/// Call Contract
|
|
|
|
fn call_contract(&self, address: Address, data: Bytes) -> Self::Call;
|
|
|
|
}
|