2020-01-17 14:27:28 +01:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
2019-01-07 11:33:07 +01:00
|
|
|
// 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
|
|
|
|
2019-10-03 15:15:25 +02:00
|
|
|
use call_contract::CallContract;
|
|
|
|
use ethabi::Address;
|
2018-03-12 21:46:27 +01:00
|
|
|
use keccak_hash::keccak;
|
2019-10-03 15:15:25 +02:00
|
|
|
use types::ids::BlockId;
|
2018-03-12 21:46:27 +01:00
|
|
|
|
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-10-03 15:15:25 +02:00
|
|
|
/// Registrar contract interface
|
|
|
|
pub trait RegistrarClient: CallContract + Send + Sync {
|
|
|
|
/// Get address of the registrar itself
|
|
|
|
fn registrar_address(&self) -> Option<Address>;
|
2018-03-12 21:46:27 +01:00
|
|
|
|
2019-10-03 15:15:25 +02:00
|
|
|
/// Get address from registrar for the specified key.
|
|
|
|
fn get_address(&self, key: &str, block: BlockId) -> Result<Option<Address>, String> {
|
|
|
|
use registrar::registrar::functions::get_address::{encode_input, decode_output};
|
2018-03-12 21:46:27 +01:00
|
|
|
|
2019-10-03 15:15:25 +02:00
|
|
|
let registrar_address = match self.registrar_address() {
|
|
|
|
Some(address) => address,
|
|
|
|
None => return Err("Registrar address not defined.".to_owned())
|
2018-03-12 21:46:27 +01:00
|
|
|
};
|
|
|
|
|
2018-09-04 20:13:51 +02:00
|
|
|
let hashed_key: [u8; 32] = keccak(key).into();
|
2019-10-03 15:15:25 +02:00
|
|
|
let id = encode_input(hashed_key, DNS_A_RECORD);
|
2018-03-12 21:46:27 +01:00
|
|
|
|
2019-10-03 15:15:25 +02:00
|
|
|
let address_bytes = self.call_contract(block, registrar_address, id)?;
|
2019-11-15 14:27:34 +01:00
|
|
|
if address_bytes.is_empty() {
|
|
|
|
return Ok(None)
|
|
|
|
}
|
2019-10-03 15:15:25 +02:00
|
|
|
let address = decode_output(&address_bytes).map_err(|e| e.to_string())?;
|
|
|
|
if address.is_zero() {
|
|
|
|
Ok(None)
|
|
|
|
} else {
|
|
|
|
Ok(Some(address))
|
|
|
|
}
|
|
|
|
}
|
2018-03-12 21:46:27 +01:00
|
|
|
}
|